Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local sides = require("sides")
- local colors = require("colors")
- local computer = require("computer")
- do_debug = true
- DEBUG = function(msg)
- if do_debug then
- print(msg)
- end
- end
- local robot = nil--require("robot")
- local rs = nil-- = component.list("redstone")()
- function loadrs()
- rs = component.redstone
- end
- if pcall(loadrs) then
- -- no errors while running `foo'
- print("loaded component.redstone!")
- else
- print("unable to load component.redstone")
- rs = nil
- end
- function loadrst2()
- rs = component.list("redstone")()
- end
- if rs == nil then
- if pcall(loadrst2) then
- print("loaded component.list(\"redstone\")()")
- else
- print("unable to load component.list(\"redstone\")()")
- end
- end
- local inv = nil
- function loadinv()
- inv = component.inventory_controller
- end
- if pcall(loadinv) then
- -- no errors while running `foo'
- print("loaded component.inventory_controller!")
- else
- print("unable to load component.inventory_controller")
- inv = nil
- end
- function loadrobot()
- robot = require("robot")
- end
- if pcall(loadrobot) then
- print("loaded robot module!")
- -- no errors while running `foo'
- else
- print("unable to load robot module")
- robot = nil
- end
- local NOP0 = function(...) return 0 end
- local NOP1 = function(...) return 3 end
- local NOPT = function(...) return {label = "testlabel", size = 1} end
- -- OCEmu has no redstone.
- if rs == nil then
- print("generating dummy redstone module")
- rs = {}
- rs.getInput = NOP0
- rs.setOutput = NOP0
- end
- if inv == nil then
- print("generating dummy inv module")
- inv = {}
- inv.getInventorySize = NOP1
- inv.getStackInSlot = NOPT
- end
- if robot == nil then
- print("generating dummy robot module")
- robot = {}
- robot.inventorySize = NOP
- robot.select = NOP
- robot.suckUp = NOP
- robot.compareTo = NOP
- robot.dropDown = NOP
- robot.use = NOP
- end
- -- Template is on top
- --local template_inv_size = inv.getInventorySize(sides.top)
- --local interface_inv_size = inv.getInventorySize(sides.east)
- --local dest_inv_size = inv.getInventorySize(sides.north)
- getInvSummary = function(side)
- local inv_slots = inv.getInventorySize(side)
- if not inv_slots then
- DEBUG(tostring(side).." has no inv\n")
- return nil
- end
- local summary = {}
- for slot = 1, inv_slots do
- local item = inv.getStackInSlot(side, slot)
- if item ~= nil then
- if summary[item.label] ~= nil then
- summary[item.label] = summary[item.label] + item.size;
- else
- summary[item.label] = item.size;
- end
- end
- end
- return summary;
- end
- local left_sum = getInvSummary(sides.north);
- local right_sum = getInvSummary(sides.south);
- retrieveItem = function(label, amount)
- local side = nil
- if left_sum[label] ~= nil then side = sides.north end
- if right_sum[label] ~= nil then side = sides.south end
- if side ~= nil then
- local inv_slots = inv.getInventorySize(side)
- for slot = 1, inv_slots do
- local item = inv.getStackInSlot(side, slot)
- if item ~= nil then
- if item.label == label then
- suckFromSlot(side, slot, amount)
- return true
- end
- end
- end
- --suckFromSlot(side:number, slot:number[, count:number])
- end
- return false
- end
- insertItem = function()
- --sides.north
- --we're lazy today
- local inv_slots = inv.getInventorySize(sides.west)
- for slot = 1, inv_slots do
- local item = inv.getStackInSlot(side, slot)
- if item == nil then
- dropIntoSlot(sides.north, slot)
- return true
- end
- end
- return false
- end
- compactInventory = function(side)
- local changed = true
- while changed do
- changed = false
- local inv_slots = inv.getInventorySize(side)
- print("slots: "..inv_slots)
- local first_with_space = {}
- for slot_start = 1, inv_slots do
- local item = inv.getStackInSlot(side, slot_start)
- if item ~= nil then
- --print("scanning "..item.label.." amt "..item.size)
- if item.size < 64 then
- --print("under 64")
- if first_with_space[item.label] == nil then
- --print("adding as backslot")
- first_with_space[item.label] = {slot = slot_start, amount = item.size}
- end
- end
- local fin = first_with_space[item.label]
- if fin ~= nil then
- --print("found it in backtable")
- local slot_end = fin["slot"]
- local slot_end_amt = fin["amount"]
- if slot_end ~= slot_start then
- --print("compacting slot "..slot_start.." into "..slot_end)
- inv.suckFromSlot(side, slot_start, 64-slot_end_amt)
- inv.dropIntoSlot(side, slot_end)
- first_with_space[item.label] = nil
- --print("skipping back to "..slot_end)
- slot_start = slot_end - 1
- changed = true
- end
- else
- --print("no prior for "..item.label.." at slot "..slot_start)
- end
- end
- end
- if changed then
- --print("looping back")
- end
- end
- end
- stockRightFromLeftWithUp = function()
- local sum_top = getInvSummary(sides.top)
- local sum_west = getInvSummary(sides.west)
- local up = computer.uptime()
- print("compacting on dest inventory... (may take a bit)")
- compactInventory(sides.west)
- print("compacting completed in "..(computer.uptime() - up).." seconds")
- up = computer.uptime()
- for k,v in pairs(sum_top) do
- local name = k
- local amount = v
- if sum_west[name] == nil or sum_west[name] < amount then
- -- gotta add some
- while sum_west[name] < amount do
- retrieveItem(name)
- insertItem()
- sum_west[name] = sum_west[name] + 64
- print("added 64 "..name)
- end
- end
- end
- end
- local up = computer.uptime()
- --compactInventory(sides.top)
- --print("done, in "..(computer.uptime() - up))
- --suckFromSlot(sides.top, 0)
- stockRightFromLeftWithUp()
- print("stockRightFromLeftWithUp completed in "..(computer.uptime() - up).." seconds")
- --local up = computer.uptime()
- --print("test")
- --local sum = getInvSummary(sides.top)
- --for k,v in pairs(sum) do
- -- io.write(k.." : "..v.."\n")
- --end
- --print("done, in "..(computer.uptime() - up))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement