Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local range = 32
- local level = 1
- local function proxyFor(name, required)
- local address = component and component.list(name)()
- if not address and required then
- error("missing component '" .. name .. "'")
- end
- return address and component.proxy(address) or nil
- end
- local drone = proxyFor("drone", true)
- local nav = proxyFor("navigation", true)
- local invctrl = proxyFor("inventory_controller")
- local colorCharing = 0xFFCC33
- local colorSearching = 0x66CC66
- local colorDelivering = 0x6699FF
- local colorThinking = 0xFF0000
- local colorThinking2 = 0xFF00FF
- local colorThinking3 = 0xFFFFFF
- local px, py, pz = 0, 0, 0
- local function moveTo(x, y, z)
- if type(x) == "table" then
- x, y, z = x[1], x[2], x[3]
- end
- local rx, ry, rz = x - px, y - py, z - pz
- drone.move(rx, ry, rz)
- while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
- computer.pullSignal(0.5)
- end
- px, py, pz = x, y, z
- end
- local function recharge()
- drone.setLightColor(colorCharing)
- moveTo(0, 2, 0)
- moveTo(0, 0, 0)
- if computer.energy() < computer.maxEnergy() * 0.1 then
- while computer.energy() < computer.maxEnergy() * 0.9
- do computer.pullSignal(1)
- end
- end
- drone.setLightColor(colorSearching)
- end
- local function cargoSize()
- local result = 0
- for slot = 1, drone.inventorySize() do
- result = result + drone.count(slot)
- end
- return result
- end
- local function shuffleTable(t)
- local rand = math.random
- local ite = #t
- local j
- for i = ite, 2, -1 do
- j = rand(i)
- t[i], t[j] = t[j], t[i]
- end
- end
- local function pullItems()
- local start = computer.uptime()
- repeat until not drone.suck(0) or computer.uptime() - start > 5
- end
- local function matchCargo(slot, filter)
- if not invctrl or not filter or filter == "" then
- return true
- end
- local stack = invctrl.getStackInInternalSlot(slot)
- if stack then
- local result = string.sub(invctrl.getStackInInternalSlot(slot).name, 2)..invctrl.getStackInInternalSlot(slot).damage
- local stack = invctrl.getStackInInternalSlot(slot)
- return stack and result == filter
- end
- return stack and false
- end
- local function haveCargoFor(filter)
- drone.setLightColor(colorThinking3)
- for slot = 1, drone.inventorySize() do
- if matchCargo(slot, filter) then
- return true
- end
- end
- end
- local function dropItems(filter)
- for slot = 1, drone.inventorySize() do
- if matchCargo(slot, filter) then
- drone.select(slot)
- drone.drop(0)
- end
- end
- end
- local waypoints
- local function updateWaypoints()
- waypoints = nav.findWaypoints(range)
- end
- local function filterWaypoints(filter)
- local result = {}
- for _, w in ipairs(waypoints) do
- if filter(w) then
- table.insert(result, w)
- end
- end
- return result
- end
- while true do
- recharge()
- updateWaypoints()
- local inputs = filterWaypoints(function(w) return w.redstone > 0 end)
- local outputs = filterWaypoints(function(w) return w.redstone < 1 end)
- shuffleTable(outputs)
- for _, input in ipairs(inputs) do
- lx, ly, lz = input.position[1], input.position[2], input.position[3]
- moveTo(lx, ly+2, lz)
- moveTo(input.position)
- pullItems()
- drone.setLightColor(colorDelivering)
- if next(outputs) == nil then
- drone.setLightColor(colorThinking)
- os.sleep(2)
- end
- for _, output in ipairs(outputs) do
- drone.setLightColor(colorThinking)
- if cargoSize() == 0 then break end
- if tonumber(string.sub(output.label,1,1)) ~=level then break end
- drone.setLightColor(colorThinking2)
- if haveCargoFor(string.sub(output.label,2)) then
- llx, lly, llz = output.position[1], output.position[2], output.position[3]
- moveTo(llx, lly+2, llz)
- moveTo(output.position)
- dropItems(output.label)
- end
- end
- drone.setLightColor(colorSearching)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement