Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Author: Axten
- -- Simple drone program to move a drone from a Botania pool to a set of pools for storing
- -- Waypoint range
- local range = 16
- -- Colors
- local colorFill = 0x69fcff
- local colorWaiting = 0x9ab7b8
- local colorCharging = 0x8de354
- local colorMoving = 0xc2a3cf
- 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 chatbox = proxyFor("chat")
- --tracking local position
- local px, py, pz = 0, 0, 0
- local function moveTo(x, y, z)
- drone.setLightColor(colorMoving)
- 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 moveX(x)
- moveTo(px +x, py, pz)
- end
- local function moveZ(z)
- moveTo(px, py, pz+z)
- end
- local function recharge()
- --io.write("recharging")
- moveTo(0, 0, 0)
- drone.setLightColor(colorCharging)
- if computer.energy() < computer.maxEnergy() * 0.1 then
- while computer.energy() < computer.maxEnergy() * 0.9 do
- computer.pullSignal(1)
- end
- end
- end
- local function getCurrentMana()
- return drone.durability()
- end
- local function fillBattery(mX, mZ)
- for i= 0, mX do
- for j= 0, mZ do
- drone.setLightColor(colorWaiting)
- if not dip() then --if ring empty start over
- moveTo(0,0,0)
- return true
- end
- moveZ(1)
- end
- moveX(1)
- moveZ(-j)
- end
- end
- local function dip()
- repeat
- local prevMana = getCurrentMana()
- drop()
- os.sleep(5)
- suck()
- until getCurrentMana() == 0 or getCurrentMana() == prevMana
- if getCurrentMana() == 0 then
- return false --ring is empty
- else
- return true --pool is full
- end
- end
- local function suck()
- if drone.suck() == false then
- chatError("can't suck item")
- return false
- end
- end
- local function drop()
- if drone.drop(0) == false then
- chatError("can't drop item")
- return false
- end
- end
- local function chatError(message)
- --io.write("Error: " .. message)
- moveTo(0,0,0)
- end
- local function updateWaypoints()
- --io.write("updating waypoints")
- 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 generators = filterWaypoints(function(w) return w.redstone > 0 end)
- local battery = filterWaypoints(function(w) return w.redstone < 1 end)
- for _, pool in ipairs(battery) do
- if pool.getLabel == "start" then
- local batteryStartPos=pool.position
- elseif pool.getLabel == "end" then
- local batteryEndPos=pool.position
- end
- end
- if batteryStartPos == nil or batteryEndPos == nil then
- chatError("battery start and end waypoints not defined")
- return false;
- else
- batteryX= math.abs(batteryStartPos[1] - batteryEndPos[1])
- batteryZ= math.abs(batteryStartPos[3] - batteryEndPos[3])
- end
- for _, generator in ipairs(generators) do
- moveTo(generator.position)
- dip()
- drone.setLightColor(colorFill)
- end
- moveTo(batteryStartPos)
- fillBattery()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement