Files
AcScripts/fuelAutocraft.lua
T

89 lines
3.8 KiB
Lua

print("Reactor fuel autocrafter :D")
local function agc(name)
return assert(components:getFirst(name), "failed to get component "..name)
end
local computer = agc("computer")
local fs = require("filesystem")
local kernel = require("kernel")
local crafter = agc("digitalCrafter")
local interface = agc("adapter")
-- to be prefixed with nuclearcraft:fuel_
local possibleFuelsRaw = --"leu-235,len-236,hen-236,hecf-249,hecm-245,mix-241,mix-239,lep-241,hep-241,heu-233,lecm-245,leu-233,lecf-249,hecm-243,lep-239,hep-239,lecm-243,heu-235,lea-242,tbu,hea_242,lecf_251,heb_248,leb_248"
([[
uranium_leu_235,neptunium_len_236,neptunium_hen_236,californium_hecf_249,curium_hecm_245,mixed_mix_241,
mixed_mix_239,plutonium_lep_241,plutonium_hep_241,uranium_heu_233,curium_lecm_245,uranium_leu_233,californium_lecf_249,curium_hecm_243,plutonium_lep_239,
plutonium_hep_239,curium_lecm_243,uranium_heu_235,americium_lea_242,thorium_tbu,americium_hea_242,californium_lecf_251,berkelium_heb_248,
berkelium_leb_248,curium_hecm_247,californium_hecf_251,curium_lecm_247
]]):gsub("[^,%w_]","")
local possibleFuels = {}
for match in possibleFuelsRaw:gmatch("[^,]+") do
table.insert(possibleFuels, "nuclearcraft:fuel_"..match)
end
print("Possible fuels: ", table.concat(possibleFuels, ";"))
local function pp(tbl)
for k,v in pairs(tbl) do print(k, tostring(v)) end
end
local function safeCraft(...)
while true do
local cooldown = crafter.cooldownRemaining
if cooldown <= 0 then break end
sleep(cooldown)
end
return crafter:craft(...)
end
local skipSleep = true
while true do
if not skipSleep then print("nothing to craft, waiting 30 seconds"); sleep(30) end
print("New iteration started.")
for _, fuelToCraft in ipairs(possibleFuels) do
local recipesForFuel = crafter:searchRecipesWithResult(fuelToCraft)
assert(#recipesForFuel == 1, "somehow there was not exactly one recipe for "..tostring(fuelToCraft))
local recipe = recipesForFuel[1]
local ingredientsStacked = {}
local stockOfIngredient = {}
for i = 1, 9 do
local currIngredient = recipe.ingredients[i][1]
assert(currIngredient, "somehow this is a recipe that has an empty slot :C for "..tostring(fuelToCraft))
recipe.ingredients[i] = currIngredient
ingredientsStacked[currIngredient] = (ingredientsStacked[currIngredient] or 0) + 1
if stockOfIngredient[currIngredient] == nil then
stockOfIngredient[currIngredient] = interface:getCount(currIngredient)
end
end
local maxStackSize = 64 -- fuels always stack 64, same for ingredients, so in principle we could craft 64 fuel at once
local recipeCraftCount = 64 -- how many recipes to craft
for ingredient, count in pairs(ingredientsStacked) do
local maxCraftForCurrIngredient = math.floor(stockOfIngredient[ingredient]/count) -- how often we could craft with this current ingredient
recipeCraftCount = math.min(recipeCraftCount, maxCraftForCurrIngredient)
end
recipeCraftCount = math.min(recipeCraftCount, math.floor(maxStackSize/recipe.resultCount))
if recipeCraftCount == 0 then
print(("We dont have enough to craft %s."):format(fuelToCraft))
else
print(("Time to craft %dx %s."):format(recipeCraftCount, fuelToCraft))
for itemName, count in pairs(ingredientsStacked) do
local remainingCount = recipeCraftCount * count
while remainingCount > 0 do
local extracted = interface:extract(itemName, 1, remainingCount)
assert(extracted > 0)
remainingCount = remainingCount - extracted
end
end
safeCraft(recipe.ingredients, recipe.result, recipeCraftCount, false)
end
end
end