70 lines
1.8 KiB
Lua
70 lines
1.8 KiB
Lua
-- Program to gather excess from farms and compost it
|
|
|
|
local CHECKS = {}
|
|
CHECKS["minecraft:kelp"] = 8 * 64
|
|
|
|
local SOURCES =
|
|
{ ""
|
|
}
|
|
|
|
local COMPOST_INPUT = ""
|
|
local COMPOST_OUTPUT = ""
|
|
local STORAGE = ""
|
|
|
|
local SLEEP_TIME = 40
|
|
|
|
function tally_up (src)
|
|
local counts = {}
|
|
for _, item in pairs(peripheral.call(src, "list")) do
|
|
if counts[item.name] == nil then counts[item.name] = 0 end
|
|
counts[item.name] = counts[item.name] + item.count
|
|
end
|
|
return counts
|
|
end
|
|
|
|
function compare_counts (count1, count2)
|
|
local diffs = {}
|
|
for item, count in pairs(count1) do
|
|
if count2[item] ~= nil then
|
|
diffs[item] = count - count2[item]
|
|
end
|
|
end
|
|
return diffs
|
|
end
|
|
|
|
function compost (src, item_name, amount)
|
|
if amount < 1 then return end
|
|
local amount_moved = 0
|
|
-- 1. scan through inventory until {item} is found
|
|
for slot, item in pairs(peripheral.call(src, "list")) do
|
|
if item.name == item_name then
|
|
-- 2. move up to {amount} to compost, recording amount moved
|
|
amount_moved = peripheral.call(
|
|
COMPOST_INPUT, "pullItems", src, slot, amount
|
|
)
|
|
break
|
|
end
|
|
end
|
|
-- 3. call again recursively
|
|
return compost(src, item_name, amount - amount_moved)
|
|
end
|
|
|
|
function move_all (src, dest)
|
|
for slot, item in pairs(peripheral.call(src, "list")) do
|
|
peripheral.call(src, "pushItems", dest, slot)
|
|
end
|
|
end
|
|
|
|
while true do
|
|
move_all(COMPOST_OUTPUT, STORAGE)
|
|
for _, source in ipairs(SOURCES) do
|
|
local totals = tally_up(source)
|
|
local diffs = compare_counts(totals, CHECKS)
|
|
for item, diff in pairs(diffs) do
|
|
if diff > 0 then
|
|
compost(source, item, diff)
|
|
end
|
|
end
|
|
end
|
|
os.sleep(SLEEP_TIME)
|
|
end
|