Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require("computer")
- local c = require("component")
- local sides = require("sides")
- local event = require("event")
- local term = require("term")
- local fs = require("filesystem")
- local shell = require("shell")
- local crafting = c.crafting
- local inv = c.inventory_controller
- local rob = c.robot
- local rs = c.redstone
- local args, options = shell.parse(...)
- local run = true
- local width, height = c.gpu.getResolution()
- local resumePath = os.getenv("_") .. ".resume"
- local resume = 0
- local slotMem = 0
- local res = fs.open(resumePath, "r")
- if res ~= nil then
- resume = tonumber(res:read(5))
- res:close()
- end
- if resume == nil then
- resume = 0
- end
- if resume > 0 then
- print("Resume to " .. tostring(resume))
- end
- local items = { ["238"] = "Uranium 238", plutonium = "Plutonium", rod = "Fuel Rod (MOX)", dualroddepleted = "Dual Fuel Rod (Depleted MOX)", plate = "Iron Plate", mox = "MOX Nuclear Fuel", ["238Block"] = "Uranium Block" }
- local recipes = { enrich = {items["238"],items["238"],items["238"],items["plutonium"],items["plutonium"],items["plutonium"],items["238"],items["238"],items["238"]}, dualrod = {items["rod"],items["plate"],items["rod"]}, ["Uranium 238"] = {items["238Block"]} }
- local craftSlots = {1,2,3,5,6,7,9,10,11}
- local slotPlaceBlacklist = {4}
- function setResume(from)
- local res = fs.open(resumePath, "w")
- res:write(tostring(from))
- res:close()
- checkPower()
- end
- function checkPower()
- local level = computer.energy() / computer.maxEnergy()
- level = level * 100
- print("Power level " .. level .. "%")
- if level < 50 and args[1] == nil or args[1] ~= "true" then
- print("Power level too low, shutting down!")
- computer.shutdown()
- end
- end
- function inTable(table, value)
- for k,v in pairs(table) do
- if v == value then
- return true
- end
- end
- return false
- end
- function findEmptySlot(internal, start, item)
- local slot = start
- local size = 1
- if internal then
- size = rob.inventorySize()
- else
- size = inv.getInventorySize(sides.front)
- end
- if item ~= nil then
- while slot <= size do
- local content = nil
- if internal then
- content = inv.getStackInInternalSlot(slot)
- else
- content = inv.getStackInSlot(sides.front, slot)
- end
- if inTable(craftSlots, slot) == false and inTable(slotPlaceBlacklist, slot) == false then
- if content ~= nil and content.label == item and content.size ~= content.maxSize then
- return slot
- end
- end
- slot = slot + 1
- end
- slot = start
- end
- while slot <= size do
- local content = nil
- if internal then
- content = inv.getStackInInternalSlot(slot)
- else
- content = inv.getStackInSlot(sides.front, slot)
- end
- if inTable(craftSlots, slot) == false and inTable(slotPlaceBlacklist, slot) == false then
- if content == nil then
- return slot
- end
- end
- slot = slot + 1
- end
- return nil
- end
- function findAndCount(item, internal)
- if internal then
- size = rob.inventorySize()
- else
- size = inv.getInventorySize(sides.front)
- end
- local slot = 1
- local total = 0
- local content
- while slot < size do
- if internal then
- content = inv.getStackInInternalSlot(slot)
- else
- content = inv.getStackInSlot(sides.front, slot)
- end
- if content ~= nil and content.label == item then
- total = total + content.size
- end
- slot = slot + 1
- end
- return total
- end
- function getLabelForItemInSlot(slot, internal)
- if internal then
- content = inv.getStackInInternalSlot(slot)
- else
- content = inv.getStackInSlot(sides.front, slot)
- end
- if content ~= nil then
- return content.label
- end
- return nil
- end
- function craft(recipe)
- print("Processing recipe")
- if recipe == nil then
- print("Recipe does not exist!")
- error()
- end
- local outputSlot = 1
- if #recipe > 3 then
- outputSlot = 4
- else
- outputSlot = #recipe + 1
- end
- local components = {}
- print("Count required components...")
- for key,item in pairs(recipe) do
- if components[item] == nil then
- components[item] = 1
- else
- components[item] = components[item] + 1
- end
- end
- print("Look for required components...")
- for key,count in pairs(components) do
- print("Need " .. count .. " of " .. key)
- local found = findAndCount(key, true)
- print("Found " .. found)
- if found < count then
- print("Could not find sufficent amount of " .. key)
- if recipes[key] ~= nil then
- print("Attempt to craft missing component " .. key)
- craft(recipes[key])
- else
- print(key .. " does not have a valid recipe registered.")
- end
- end
- end
- for key,item in pairs(recipe) do
- local targetSlot = craftSlots[key]
- print("Move item " .. item .. " to slot " .. targetSlot .. " for craft")
- findAndMoveTo(item, targetSlot, 1, true, 10, false)
- end
- rob.select(outputSlot)
- crafting.craft()
- print("Clearing output slot")
- local slot = findEmptySlot(true, 1, getLabelForItemInSlot(outputSlot, true))
- if slot ~= nil then
- moveItem(outputSlot, slot, 64, true)
- rob.select(slot)
- end
- end
- function moveItem(from, to, amount, internal)
- if internal then
- rob.select(from)
- rob.transferTo(to, amount)
- else
- rob.select(to)
- inv.suckFromSlot(sides.front, from, amount)
- end
- end
- function findAndMoveTo(item, targetSlot, amount, internal, repeatUntilSuccess, searchAllSlots)
- local found = false
- local slot = 1
- local size = 1
- if internal then
- size = rob.inventorySize()
- else
- size = inv.getInventorySize(sides.front)
- end
- print("Looking for " .. item)
- print("Until found: " .. tostring(repeatUntilSuccess) .. " Internally: " .. tostring(internal))
- while found == false do
- slot = 1
- while slot <= size do
- term.setCursor(1, height)
- term.clearLine()
- term.write("Slot " .. slot)
- local content = nil
- if internal then
- content = inv.getStackInInternalSlot(slot)
- else
- content = inv.getStackInSlot(sides.front, slot)
- end
- local sourceSlotOk = true
- if searchAllSlots == false then
- if inTable(craftSlots, slot) == true then
- sourceSlotOk = false
- end
- end
- local targetSlotOk = true
- if inTable(slotPlaceBlacklist, targetSlot) then
- targetSlotOk = false
- end
- if sourceSlotOk and targetSlotOk and content and content.label == item then
- print()
- print("Taking item from slot " .. tostring(slot))
- moveItem(slot, targetSlot, amount, internal)
- rob.setLightColor(0xFFFF00)
- return
- else
- if content and sourceSlotOk == false and content.label == item then
- print()
- print("Ignoring matching item in inaccessible slot")
- end
- end
- slot = slot + 1
- end
- checkPower()
- if repeatUntilSuccess == 0 then
- return false
- else
- term.setCursor(1, height)
- term.clearLine()
- term.write("No item found, holding " .. tostring(repeatUntilSuccess) .. " seconds")
- rob.setLightColor(0x990000)
- local ev = event.pull(repeatUntilSuccess, "interrupted")
- if ev == "interrupted" then
- print()
- error("Interrupted in findAndMoveLoop")
- end
- end
- end
- end
- rob.setLightColor(0x00DB00)
- rs.setOutput(sides.front, 15)
- while run do
- slotMem = findEmptySlot(true, 1)
- if slotMem ~= false then
- if resume == 0 then
- findAndMoveTo(items["dualroddepleted"], slotMem, 1, false, 60, false)
- rs.setOutput(sides.left, 0)
- rob.setLightColor(0xFFFF00)
- findAndMoveTo(items["dualroddepleted"], slotMem, 1, false, 0, false)
- findAndMoveTo(items["dualroddepleted"], slotMem, 1, false, 0, false)
- findAndMoveTo(items["dualroddepleted"], slotMem, 1, false, 0, false)
- rob.select(slotMem)
- rob.turn(true)
- rob.turn(true)
- end
- slotMem = findEmptySlot(false, 5)
- if slotMem ~= false then
- inv.dropIntoSlot(sides.front, slotMem)
- slotMem = findEmptySlot(true, 1)
- if slotMem ~= false then
- setResume(1)
- if resume <= 1 then
- findAndMoveTo(items["plutonium"], slotMem, 64, true, 10, true)
- end
- setResume(2)
- if resume <= 2 then
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- craft(recipes["enrich"])
- end
- slotMem = findEmptySlot(false, 5)
- if slotMem ~= false then
- inv.dropIntoSlot(sides.front, slotMem, 64)
- slotMem = findEmptySlot(true, 5)
- if slotMem ~= false then
- setResume(3)
- if resume <= 3 then
- findAndMoveTo(items["rod"], slotMem, 64, true, 10, true)
- end
- setResume(4)
- if resume <= 4 then
- craft(recipes["dualrod"])
- craft(recipes["dualrod"])
- craft(recipes["dualrod"])
- craft(recipes["dualrod"])
- end
- rob.turn(true)
- rob.turn(true)
- setResume(5)
- inv.dropIntoSlot(sides.front, findEmptySlot(false, 1))
- inv.dropIntoSlot(sides.front, findEmptySlot(false, 1))
- inv.dropIntoSlot(sides.front, findEmptySlot(false, 1))
- inv.dropIntoSlot(sides.front, findEmptySlot(false, 1))
- print("Refuelling operation complete!")
- rs.setOutput(sides.left, 15)
- rob.setLightColor(0x00DB00)
- else
- error("No empty slots in sorter")
- end
- else
- error("No empty slots in sorter")
- end
- else
- error("No empty slots in robot")
- end
- else
- error("No empty slots in sorter")
- end
- else
- error("No empty slots in robot")
- end
- local ev = event.pull(1, "interrupted")
- if ev == "interrupted" then
- error("Interrupted in main")
- end
- setResume(0)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement