wyvern/IO.lua

51 lines
1.6 KiB
Lua
Raw Normal View History

2018-08-14 21:13:12 +00:00
local d = require "luadash"
local w = require "lib"
local conf = w.load_config({
"chest",
2018-08-14 21:13:42 +00:00
"items",
"sleep_time"
2018-08-14 21:13:12 +00:00
}, {
2018-08-14 21:17:54 +00:00
sleep_time = 1,
2018-08-14 21:13:12 +00:00
items = {}
})
w.init()
local chest = peripheral.wrap(conf.chest)
2018-08-15 16:33:37 +00:00
local function get_num_stacks(total_items)
return math.ceil(total_items / 64)
end
2018-08-14 21:13:12 +00:00
while true do
2018-08-14 21:21:42 +00:00
local stacks_stored = d.map(chest.list(), w.to_wyvern_item)
2018-08-14 21:13:12 +00:00
local items_stored = w.collate_stacks(stacks_stored)
for item_name, quantity_desired in pairs(conf.items) do
2018-08-15 06:34:07 +00:00
local quantity_stocked = items_stored[item_name].count or 0
2018-08-14 21:13:12 +00:00
if quantity_desired > quantity_stocked then -- if we have fewer items than are desired, extract some from store
local request = w.string_to_item(item_name)
request.type = "extract"
2018-08-15 06:33:27 +00:00
request.destination_inventory = conf.chest
2018-08-15 06:30:25 +00:00
local result = w.unwrap(w.query_by_type("storage", request), "extracting items")
print("Moved", result.moved, item_name, "from storage.")
2018-08-14 21:13:12 +00:00
end
end
for slot, item in pairs(stacks_stored) do
2018-08-15 06:30:25 +00:00
local ii = w.get_internal_identifier(item)
2018-08-15 16:35:43 +00:00
local stored = (items_stored[ii] or {count = 0}).count
2018-08-15 16:33:37 +00:00
local wanted = conf.items[ii] or 0
2018-08-15 16:34:53 +00:00
if (get_num_stacks(stored) * 64) >= wanted then -- if item is not in want list or we have too many, send it back to storage
2018-08-15 06:30:25 +00:00
local result = w.unwrap(w.query_by_type("storage", {
2018-08-14 21:13:12 +00:00
type = "insert",
from_inventory = conf.chest,
from_slot = slot
2018-08-14 21:28:57 +00:00
}), "inserting items")
2018-08-15 06:30:25 +00:00
print("Moved", result.moved, ii, "to storage.")
2018-08-14 21:13:12 +00:00
end
end
2018-08-14 21:13:42 +00:00
sleep(conf.sleep_time)
2018-08-14 21:13:12 +00:00
end