First pass at a farm control program.
This commit is contained in:
parent
ea9bd903ac
commit
c5bb5ff425
1 changed files with 81 additions and 0 deletions
81
farm_control.lua
Normal file
81
farm_control.lua
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue