wyvern/IO.lua

67 lines
2.3 KiB
Lua
Raw Permalink 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-15 18:55:28 +00:00
local function get_stacks()
local s = d.map(d.filter(chest.list(), function(x) return x ~= nil end), w.to_wyvern_item)
return s, w.collate_stacks(s)
end
local function main()
while true do
local stacks_stored, items_stored = get_stacks()
local function get_item_count(ii)
return (items_stored[ii] or {count = 0}).count
2018-08-14 21:13:12 +00:00
end
for item_name, quantity_desired in pairs(conf.items) do
local quantity_stocked = get_item_count(item_name)
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"
request.destination_inventory = conf.chest
2018-08-16 08:58:01 +00:00
request.quantity = quantity_desired - quantity_stocked
2018-08-15 18:10:52 +00:00
local result = w.unwrap(w.query_by_type("storage", request), "extracting items", { w.errors.NOITEMS })
2018-08-15 18:50:55 +00:00
if result then print("Moved", result.moved, item_name, "from storage.") end
end
end
for slot, item in pairs(stacks_stored) do
local ii = w.get_internal_identifier(item)
local stored = get_item_count(ii)
local wanted = 0
if conf.items[ii] then wanted = conf.items[ii] + 1 end
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
local result = w.unwrap(w.query_by_type("storage", {
type = "insert",
from_inventory = conf.chest,
from_slot = slot
}), "inserting items")
2018-08-15 18:50:55 +00:00
if result then print("Moved", result.moved, ii, "to storage.") end
end
2018-08-14 21:13:12 +00:00
end
sleep(conf.sleep_time)
2018-08-14 21:13:12 +00:00
end
end
2018-08-14 21:13:42 +00:00
local ok, err = pcall(main)
2018-08-15 18:01:43 +00:00
if type(err) == "table" then err = w.errors.format(err) end
print(err)