New program for composting from farms.
This commit is contained in:
parent
58765ec296
commit
19636ac7c5
1 changed files with 60 additions and 0 deletions
60
compost_control.lua
Normal file
60
compost_control.lua
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue