From c5bb5ff42589acf77e08b4b9480a309edc9ba317 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Sun, 28 Sep 2025 18:30:21 -0400 Subject: [PATCH] First pass at a farm control program. --- farm_control.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 farm_control.lua diff --git a/farm_control.lua b/farm_control.lua new file mode 100644 index 0000000..f9a791b --- /dev/null +++ b/farm_control.lua @@ -0,0 +1,81 @@ +-- Program to control a farm made with the create mod + +-- establish amounts of items to ensure +local LEVELS = {} +LEVELS["minecraft:oak_log"] = {4 * 64, "minecraft:oak_sapling"} + +local STORAGE = "" +local HARVESTER = "" + +local HARVESTER_RELAY = "" + +local SLEEP_TIME = 10 +local HARVESTER_SIGNAL_TIME = 2 + +-------------------------------- PROGRAM -------------------------------------- + +function pull_items (src, dest) + local src = peripheral.wrap(src) + if src.size() < 1 return nil end + for slot, _ in pairs(src.list()) do + src.pushItems(dest, slot) + end +end + +-- need to return the sources for items that need farming +-- e.g. a sapling for wood +function check_levels (levels, src) + local src = peripheral.wrap(src) + local amounts = {} + for _, item in pairs(src.list()) do + if amounts[item.name] == nil then + amounts[item.name] = item.count + else + amounts[item.name] = amounts[item.name] + item.count + end + end + local needs_farming = {} + for item, data in pairs(levels) do + if amounts[item] ~= nil and amounts[item] < data[1] then + table.insert(needs_farming, data[2]) + end + end + if needs_farming[1] == nil then + return nil + else + return needs_farming + end +end + +function load_stack(item_name, src, dest) + local src = peripheral.wrap(src) + for slot, item in pairs(src.list()) do + if item.name == item_name then + src.pushItems(dest, slot) + break + end + end +end + +function send_harvester (relay_and_side) + local RELAY_COMMAND = "setOutput" + local relay, side = relay_and_side:match"(.+):(.+)" + peripheral.call(relay, RELAY_COMMAND, side, true) + os.sleep(HARVESTER_SIGNAL_TIME) + peripheral.call(relay, RELAY_COMMAND, side, false) +end + +-- TODO make a different loop for crops controlled by a CLI arg +-- the other loop doesn't need to load the harvester, but also doesn't need +-- to send the harvester unless stock drops below a level +while true do + local needs_farming = check_levels(LEVELS, STORAGE) + if needs_farming ~= nil then + for _, item in ipairs(needs_farming) do + load_stack(item, STORAGE, HARVESTER) + end + end + send_harvester() + os.sleep(SLEEP_TIME) + pull_items(HARVESTER, STORAGE) +end