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
- resX, resY = gpu.getResolution()
- materials = {}
- smeltery = {}
- recipes = {}
- verbose = false
- stopme = false
- transposer = false
- me = false
- interface = false
- ingotFormer = false
- function splitName(input)
- return { modid = string.sub(string.match(input, ".*:"), 1, -2), name = string.sub(string.match(input, ":.*"), 2) }
- end
- function findInventory(name)
- gpu.setForeground(0x969696)
- for i=1,#sides do
- inv = transposer.getInventoryName(i-1)
- if inv ~= nil then
- if string.match(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.controller).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 = smeltery.size
- 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.controller, 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(smeltery.drain)
- for fluidIndex=1,#fluid do
- if fluid[fluidIndex].name == recipe.fluid then
- perc = math.floor(((fluid[fluidIndex].amount + recipe.alloy.done) / recipe.alloy.total * 100) * 10 + 0.5) / 10
- recipe.alloy.percent = perc
- if fluidIndex == 1 then
- recipe.alloy.waiting = math.floor(fluid[fluidIndex].amount)
- if recipe.alloy.waiting > 0 and recipe.alloy.total > fluid[fluidIndex].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
- end
- 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(smeltery.drain, 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 castLiquid()
- fluid = transposer.getFluidInTank(smeltery.drain)
- 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(smeltery.drain, 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(smeltery.drain)
- if #fluid > 0 and fluid[1].name == fakeRecipe.fluid then
- print("theres fluid left, starting another run")
- os.sleep(2)
- castLiquid()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement