From 19636ac7c564301b210e084f4f9749ea022cdd4b Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 14 Nov 2025 20:30:13 -0500 Subject: [PATCH] New program for composting from farms. --- compost_control.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 compost_control.lua diff --git a/compost_control.lua b/compost_control.lua new file mode 100644 index 0000000..dc9b63f --- /dev/null +++ b/compost_control.lua @@ -0,0 +1,60 @@ +-- 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