cc-stuff/compost_control.lua
2025-11-14 20:30:13 -05:00

60 lines
1.5 KiB
Lua

-- Program to gather excess from farms and compost it
local CHECKS = {}
CHECKS["minecraft:kelp"] = 8 * 64
local SOURCES =
{ ""
}
local COMPOST_INV = ""
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
end
function compost (src, item_name, amount)
if amount < 1 return end
local amount_moved = 0
-- 1. scan through inventory until {item} is found
for slot, item in pairs(src.list())
if item.name == item_name then
-- 2. move up to {amount} to compost, recording amount moved
amount_moved = peripheral.call(
COMPOST_INV, "pullItems", src, slot, amount
)
break
end
end
-- 3. call again recursively
return compost(src, item_name, amount - amount_moved)
end
while true do
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