cc-stuff/biofuel-manager.lua
Emerson Rosen-Jones 0be62dbccc feat: add error handling for unloaded peripherals
Future work: cap the number of retries
2025-12-20 22:02:37 -05:00

150 lines
4.4 KiB
Lua

-- program to manage a biofuel factory with create and some addons + tweaks
-- NOTE: this design does not handle stocking cinder flour or sugar
local STORAGE = ""
local OIL_TANK = ""
local FUEL_TANK = ""
local FACTORY_ADDRESS = ""
local MAIN_SEED_REQUEST = "minecraft:melon_seeds"
local MAIN_PLANT_REQUEST = "minecraft:bamboo"
local SPEEDOMETER = ""
local MAIN_REQUESTER = ""
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 = {
["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)
-- TODO
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 getStockRequirements ()
-- TODO
end
function getOrders ()
-- TODO
end
function run (requester, ticker)
if peripheral.call(SPEEDOMETER, "getSpeed") ~= 0 then
local need_to_stock = getStockRequirements()
local need_to_create = getOrders()
for type, amt in pairs(need_to_stock) do
stock[type](amt, requester)
end
for type, amt in pairs(need_to_create) do
order[type](amt, ticker)
end
end
os.sleep(SLEEP_T)
return run(requester, ticker)
end
if arg ~= nil and arg[1] == "run" then
local requester = peripheral.wrap(MAIN_REQUESTER)
requester.setAddress(FACTORY_ADDRESS)
local ticker = peripheral.wrap(SUBNET_TICKER)
local ran = false
while not ran do
ran, result = run(ticker, requester)
if not ran then print(result) end
os.sleep(SLEEP_T)
end
end