Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component = require("component")
- sides = require("sides")
- term = require("term")
- event = require("event")
- db = component.database
- gpu = component.gpu
- transposer = component.proxy("308a501d-1ec1-4a87-8d98-add1c0b2cee5")
- smelteryDrain = sides.front
- resX, resY = gpu.getResolution()
- verbose = false
- stopme = false
- --todo: account smeltery size to max inputs
- function findInventory(name)
- gpu.setForeground(0x969696)
- for i=1,#sides do
- inv = transposer.getInventoryName(i-1)
- if inv ~= nil then
- if inv == name then
- print("(i) found " .. inv .. " on side " .. sides[i-1])
- gpu.setForeground(0xFFFFFF)
- return (i-1)
- end end end
- gpu.setForeground(0xFF8A00)
- print("(!) no inventory found for '" .. name .. "'")
- gpu.setForeground(0xFFFFFF)
- return nil
- end
- function readFile(file)
- fd = io.open(file, "r")
- local data = fd:read("*a")
- fd:close()
- return data
- end
- function getFreeSlots()
- emptySlots = 0
- stacks = transposer.getAllStacks(smeltery).getAll()
- for i=1,#stacks do
- if stacks[i].size == 0 then
- emptySlots = emptySlots + 1
- end
- end
- return emptySlots
- end
- function updateStorage()
- print("updating storage stats")
- for i=1,#materials do
- storage = me.getItemsInNetwork({ name = materials[i].name })
- if #storage > 0 then
- materials[i].stored = math.floor(storage[1].size)
- else
- materials[i].stored = 0
- end
- end
- end
- function loadMaterials()
- print("\n\n(!) reading material db")
- gpu.setForeground(0x969696)
- for i=1,25 do
- material = db.get(i)
- if material ~= nil then
- print("(i) " .. material.name .. " added")
- material.slot = i
- material.stored = 0
- table.insert(materials, material)
- end end
- gpu.setForeground(0xFFFFFF)
- print("(i) "..#materials .. " materials loaded\n\n")
- end
- function getSlot(materialName)
- for i=1,#materials do
- if materials[i].name == materialName then
- return materials[i].slot
- end end
- return nil
- end
- function moveIngots(recipe)
- ingots = transposer.getStackInSlot(ingotFormer, 1)
- if ingots ~= nil and ingots.size > 0 then
- if transposer.transferItem(ingotFormer, interface, ingots.size) then
- gpu.setForeground(0x969696)
- if verbose then print("moved " .. math.floor(ingots.size) .. " ingots to storage") end
- recipe.ingots.done = math.floor(recipe.ingots.done + ingots.size)
- gpu.setForeground(0xFFFFFF)
- else
- gpu.setForeground(0xFF0000)
- print("failed to move " .. math.floor(ingots.size) .. " ingots to storage")
- gpu.setForeground(0xFFFFFF)
- end end
- end
- function canCraft(recipe)
- maxCraft = math.huge
- for i=1,#recipe.input do
- for m=1,#materials do
- if materials[m].name == recipe.input[i].name then
- c = math.floor(materials[m].stored / recipe.input[i].count)
- maxCraft = math.min(maxCraft, c)
- end end end
- if maxCraft ~= math.huge then
- return maxCraft
- else
- return 0
- end
- end
- function setupInterfaceSlot(recipe, slot)
- gpu.setForeground(0x969696)
- io.write("setting up interface for material " .. recipe.input[slot].name .. "...")
- gpu.setForeground(0xFFFFFF)
- recipe.input[slot].useCount = recipe.input[slot].count * recipe.amount
- dbSlot = getSlot(recipe.input[slot].name)
- if slot == nil then
- gpu.setForeground(0xFF0000)
- io.write("no slot found for material '".. recipe.input[slot].name "'\n")
- gpu.setForeground(0xFFFFFF)
- return false
- end
- me.setInterfaceConfiguration(slot, db.address, dbSlot, math.min(64, recipe.input[slot].useCount))
- gpu.setForeground(0x00FF00)
- io.write("done!\n")
- gpu.setForeground(0xFFFFFF)
- end
- function resetInterface(slot)
- gpu.setForeground(0x969696)
- print("resetting interface slot " .. slot)
- gpu.setForeground(0xFFFFFF)
- me.setInterfaceConfiguration(slot)
- end
- function moveItemsToSmeltery(recipe)
- freeSlots = smelterySize
- print("transfering items to smeltery")
- for i=1,#recipe.input do
- setupInterfaceSlot(recipe, i)
- while recipe.input[i].useCount > 0 do
- if freeSlots == 0 then
- gpu.setForeground(0x969696)
- io.write("waiting for free slots ..")
- while freeSlots == 0 do
- io.write(".")
- freeSlots = getFreeSlots()
- os.sleep(0.5)
- end
- io.write("\n")
- gpu.setForeground(0xFFFFFF)
- end
- canTransfer = math.min(freeSlots, transposer.getStackInSlot(interface, i).size)
- if transposer.transferItem(interface, smeltery, math.min(canTransfer, recipe.input[i].useCount), i) then
- recipe.input[i].useCount = recipe.input[i].useCount - canTransfer
- freeSlots = freeSlots - canTransfer
- end
- end
- resetInterface(i)
- end
- end
- function updateAlloy(recipe)
- fluid = transposer.getFluidInTank(smelteryDrain)
- if #fluid > 0 and fluid[1].name == recipe.fluid then
- perc = math.floor(((fluid[1].amount + recipe.alloy.done) / recipe.alloy.total * 100) * 10 + 0.5) / 10
- recipe.alloy.percent = perc
- recipe.alloy.waiting = math.floor(fluid[1].amount)
- if recipe.alloy.waiting > 0 and recipe.alloy.total > fluid[1].amount + recipe.alloy.done then
- recipe.alloy.waiting = recipe.alloy.waiting - 1 --remove 1mb from the batch to keep 1mb on bottom of the smeltery
- end
- else
- recipe.alloy.waiting = 0
- end
- marginRight = 50
- gpu.setForeground(0xFFBD00)
- gpu.fill(resX-marginRight, 3, marginRight, 1, " ")
- gpu.set(resX-marginRight, 3, recipe.alloy.percent .. "%")
- gpu.set(resX-marginRight+10, 3, "("..(recipe.alloy.done + recipe.alloy.waiting).."mB of "..recipe.alloy.total.."mB)");
- gpu.setForeground(0xFFFFFF)
- end
- function moveAlloy(recipe)
- if recipe.alloy.waiting < 144 and recipe.alloy.waiting + recipe.alloy.done < recipe.alloy.total then return false; end
- transfer = math.min(8000, recipe.alloy.waiting)
- if transposer.transferFluid(smelteryDrain, ingotFormer, transfer) then
- if verbose then print("moved " .. transfer .. "mB of alloy to ingot former") end
- recipe.alloy.done = recipe.alloy.done + transfer
- else
- gpu.setForeground(0xFF0000)
- print("failed to move " .. transfer .. "mB of alloy to ingot former")
- gpu.setForeground(0xFFFFFF)
- end
- end
- function updateIngots(recipe)
- marginRight = 50
- gpu.setForeground(0xFFBD00)
- gpu.fill(resX-marginRight, 5, marginRight, 1, " ")
- gpu.set(resX-marginRight, 5, math.floor(recipe.ingots.done/recipe.ingots.total * 100) .. "%")
- gpu.set(resX-marginRight+10, 5, "("..recipe.ingots.done.." of "..recipe.ingots.total.." ingots done)");
- gpu.setForeground(0xFFFFFF)
- end
- function craftRecipe(recipe, amount)
- recipe.amount = amount
- recipe.alloy = { done = 0, percent = 0, waiting = 0, total = (recipe.output.count * recipe.amount * 144) }
- recipe.ingots = { total = (recipe.output.count * amount), done = 0 }
- term.clear()
- print("\n### crafting: "..recipe.output.count .. "x " .. recipe.output.name.." ###\n")
- if recipe.input_fluids ~= nil then
- for i=1,#recipe.input_fluids do
- print("please add " .. (recipe.input_fluids[i].amount * amount) .. "mB of " .. recipe.input_fluids[i].name)
- end
- end
- moveItemsToSmeltery(recipe)
- print("waiting for alloy")
- repeat
- updateAlloy(recipe)
- moveAlloy(recipe)
- moveIngots(recipe)
- updateIngots(recipe)
- os.sleep(0.5)
- until recipe.alloy.done >= recipe.alloy.total and recipe.ingots.done >= recipe.ingots.total
- os.sleep(1)
- updateStorage()
- end
- function deposeLiquid()
- transposer.transferFluid(smelteryDrain, ingotFormer, 16000)
- end
- function castLiquid()
- term.clear()
- fluid = transposer.getFluidInTank(smelteryDrain)
- if #fluid == 0 then return false; end
- fakeRecipe = {}
- fakeRecipe.alloy = { done = 0, percent = 0, waiting = 0, total = math.min(math.floor(fluid[1].amount / 144) * 144, 15984) }
- fakeRecipe.ingots = { done = 0, total = math.floor(fakeRecipe.alloy.total / 144) }
- fakeRecipe.ingots.done = 0
- fakeRecipe.fluid = fluid[1].name
- if fakeRecipe.ingots == 0 then
- gpu.setForeground(0xFFC900)
- print("theres not enough liquid left to cast a ingot")
- gpu.setForeground(0xFFFFFF)
- os.sleep(2)
- return;
- end
- transposer.transferFluid(smelteryDrain, ingotFormer, fakeRecipe.alloy.total)
- print("casting ".. fakeRecipe.ingots.total .. " ingots from " .. fakeRecipe.fluid)
- gpu.setForeground(0x969696)
- while fakeRecipe.ingots.done < fakeRecipe.ingots.total do
- moveIngots(fakeRecipe)
- io.write(".")
- os.sleep(1)
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setForeground(0x00FF00)
- print("")
- print("done!")
- gpu.setForeground(0xFFFFFF)
- os.sleep(1)
- fluid = transposer.getFluidInTank(smelteryDrain)
- if #fluid > 0 and fluid[1].name == fakeRecipe.fluid then
- print("theres fluid left, starting another run")
- os.sleep(2)
- castLiquid()
- end
- end
- term.clear()
- print("(!) setting up devices")
- me = component.me_interface
- ingotFormer = findInventory("nuclearcraft:ingot_former_active")
- if ingotFormer == nil then ingotFormer = findInventory("nuclearcraft:ingot_former_idle") end
- smeltery = findInventory("tconstruct:smeltery_controller")
- interface = findInventory("appliedenergistics2:interface")
- materials = {}
- smelterySize = transposer.getInventorySize(smeltery)
- recipes = require("serialization").unserialize(readFile("/home/smeltery.recipes"))
- loadMaterials()
- updateStorage()
- print("\n\ninit done! press [enter] to continue...")
- io.read()
- os.sleep(0.5)
- while not stopme do
- term.clear()
- if verbose then verboseText = "true"; else verboseText = "false"; end
- print("### select alloy/command ### (verbose: " .. verboseText .. ")\n")
- for i=1,#recipes do
- io.write("#" .. i .. ": " .. recipes[i].output.count .. "x " .. recipes[i].output.name .. ", ")
- maxCraft = canCraft(recipes[i])
- if maxCraft > 0 then
- gpu.setForeground(0x00FF00)
- print("can craft "..maxCraft.." times ("..(maxCraft * recipes[i].output.count).." ingots)")
- else
- gpu.setForeground(0xFF0000)
- print("cant craft")
- end
- gpu.setForeground(0x969696)
- for j=1,#recipes[i].input do
- stored = 0
- for m=1,#materials do
- if materials[m].name == recipes[i].input[j].name then
- stored = materials[m].stored
- end
- end
- print("\t" .. recipes[i].input[j].count .. "x " .. recipes[i].input[j].name .. " ("..stored.." ingots in ME Network)")
- end
- gpu.setForeground(0xFFFFFF)
- end
- print("")
- print("c: cast liquid")
- print("d: depose 16b of fluid in smeltery to ingot former")
- print("v: toggle verbose")
- print("u: update inventory")
- print("e: exit\n")
- io.write("select recipe/command: ")
- input = io.read()
- if input == "e" then
- stopme = true
- elseif input == "d" then
- deposeLiquid()
- elseif input == "c" then
- castLiquid()
- elseif input == "u" then
- updateStorage()
- elseif input == "v" then
- verbose = not verbose
- elseif recipes[tonumber(input)] ~= nil then
- io.write("amount: ")
- amount = tonumber(io.read())
- if amount > 0 then
- craftRecipe(recipes[tonumber(input)], math.max(1, amount))
- else
- print("invalid input: '"..input.."'")
- end
- else
- print("invalid input: '"..input.."'")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement