cc-stuff/biofuel-manager.lua
Emerson Rosen-Jones 341e0bf433 fix: properly requests biomass
Fixes handling of tags
2025-12-20 22:47:54 -05:00

172 lines
5.2 KiB
Lua

-- program to manage a biofuel factory with create and some addons + tweaks
-- NOTE: this design does not handle stocking the system
local STORAGE = ""
local OIL_TANK = ""
local FUEL_TANK = ""
local OIL_SETPOINT = 64000
local BIOMASS_SETPOINT = 4 * 64
local BIOFUEL_SETPOINT = 128000
local SPEEDOMETER = ""
local SUBNET_TICKER = ""
local SLEEP_T = 10
local OIL_PKG = "Oil"
local BIOMASS_PKG = "Biomass"
local BIOFUEL_PKG = "Biofuel"
local order = {
oil = function (amt, ticker)
local orderCount = math.floor(amt / 100)
local request = {
tags = {
["c:seeds"] = true,
},
_requestCount = orderCount,
}
return ticker.requestFiltered(OIL_PKG, request)
end,
biomass = function (amt, ticker)
-- TODO make order_amt calculation less naive
local order_amt = amt * 3
local request = {
_op = "any",
_requestCount = order_amt,
value = {
{ name = {
_op = "any",
value = {
"minecraft:stick",
"minecraft:honeycomb"
}
} },
{ tags = { _op = "any", value = {
{ ["minecraft:flowers"] = true },
{ ["c:crops"] = true },
{ ["createaddition:plant_foods"] = true },
{ ["createaddition:plants"] = true },
{ ["minecraft:saplings"] = true },
{ ["minecraft:leaves"] = true },
} } },
}
}
return ticker.requestFiltered(BIOMASS_PKG, request)
end,
biofuel = function (amt, ticker)
local request = function ()
local biomass = {
name = "createaddition:biomass",
_requestCount = 32
}
local sugar = {
name = "minecraft:sugar",
_requestCount = 16
}
local cinder_flour = {
name = "create:cinder_flour",
_requestCount = 16
}
return biomass, sugar, cinder_flour
end
local enoughMaterials = function ()
-- use ticker to test ( biomass >= 32, sugar >= 16, and cinder flour >= 16
local check = {}
check["createaddition:biomass"] = 32
check["minecraft:sugar"] = 16
check["create:cinder_flour"] = 16
for _, item in pairs(ticker.stock()) do
if check[item.name] then
check[item.name] = check[item.name] - item.count
end
end
local enough_materials = true
for name, count in pairs(check) do
if count > 0 then enough_materials = false end
end
return enough_materials
end
local amt_made = 0
while amt_made < amt do
if not enoughMaterials() then break end
ticker.requestFiltered(BIOFUEL_PKG, request())
amt_made = amt_made + 2000
os.sleep(1)
end
return amt_made
end,
}
local stock = {
seeds = function (amt, requester)
requester.setRequest({ name = MAIN_SEED_REQUEST, count = amt })
requester.request()
end,
plants = function (amt, requester)
requester.setRequest({ name = MAIN_PLANT_REQUEST, count = amt })
requester.request()
end,
}
function listItems (storage)
local tally = {}
for _, item in pairs(peripheral.call(storage, "list")) do
if tally[item.name] == nil then tally[item.name] = 0 end
tally[item.name] = tally[item.name] + item.count
end
return tally
end
function getOrders ()
local orders = {}
-- Oil
local oil_amt = table.remove(peripheral.call(OIL_TANK, "tanks"))
if oil_amt == nil then
oil_amt = 0
else
oil_amt = oil_amt.amount
end
local oil_needed = OIL_SETPOINT - oil_amt
if oil_needed > 0 then orders.oil = oil_needed end
-- Biomass
-- TODO use up excess material
local biomass_amt = listItems(STORAGE)["createaddition:biomass"]
if biomass_amt == nil then biomass_amt = 0 end
local biomass_needed = BIOMASS_SETPOINT - biomass_amt
if biomass_needed > 0 then orders.biomass = biomass_needed end
-- Biofuel
-- TODO use up excess material?
local biofuel_amt = table.remove(peripheral.call(FUEL_TANK, "tanks"))
if biofuel_amt == nil then
biofuel_amt = 0
else
biofuel_amt = biofuel_amt.amount
end
local biofuel_needed = BIOFUEL_SETPOINT - biofuel_amt
if biofuel_needed > 0 then orders.biofuel = biofuel_needed end
return orders
end
function run (ticker)
if peripheral.call(SPEEDOMETER, "getSpeed") ~= 0 then
local need_to_create = getOrders()
for type, amt in pairs(need_to_create) do
order[type](amt, ticker)
end
end
os.sleep(SLEEP_T)
return run(ticker)
end
if arg ~= nil and arg[1] == "run" then
local ticker = peripheral.wrap(SUBNET_TICKER)
local ran = false
while not ran do
ran, result = run(ticker)
if not ran then print(result) end
os.sleep(SLEEP_T)
end
end