Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local drone = component.proxy(component.list("drone")())
- local nav = component.proxy(component.list("navigation")())
- local modem = component.proxy(component.list("modem")())
- local waypointLookRadius = 64
- local flightAltitude = 80
- local commsPort = 1337
- local colors = {["travelling"] = 0xFFFFFF, ["charging"] = 0xE6E600, ["charged"] = 0x00FF00, ["waiting"] = 0xFF9900, ["error"] = 0xFF0000, ["active"] = 0x0000FF }
- local cx, cy, cz
- local BASE
- function callibrate()
- cx, cy, cz = nav:getPosition()
- end
- function getWaypoints()
- BASE = {}
- callibrate()
- local waypoints = nav.findWaypoints(waypointLookRadius)
- for i=1, waypoints.n do
- if waypoints[i].label == "Charger" then
- BASE.x = cx+waypoints[i].position[1]
- BASE.y = cy+waypoints[i].position[2]
- BASE.z = cz+waypoints[i].position[3]
- end
- end
- if BASE == nil then
- error("Missing waypoints")
- end
- end
- function getCharge()
- return computer.energy()/computer.maxEnergy()
- end
- function color(state)
- drone.setLightColor(colors[state] or 0x000000)
- end
- function move(tx, ty, tz)
- local dx = tx - cx
- local dy = ty - cy
- local dz = tz - cz
- drone.move(dx, dy, dz)
- while drone.getOffset() > 0.5 and drone.getVelocity() > 0.2 do
- computer.pullSignal(0.2)
- end
- cx, cy, cz = tx, ty, tz
- end
- function fly(tx, ty, tz)
- move(cx, flightAltitude, cz)
- move(tx, flightAltitude, tz)
- move(tx, ty, tz)
- callibrate()
- if cx ~= tx or cy ~= ty or cz ~= tz then
- computer.pullSignal(1)
- callibrate()
- move(tx,ty,tz)
- end
- end
- function goHome()
- color("travelling")
- fly(BASE.x, BASE.y+1, BASE.z)
- color("charging")
- while getCharge() <= .95 do computer.pullSignal(.1) end
- color("charged")
- computer.beep(1000, 1)
- end
- function runDelivery(ds)
- color("travelling")
- fly(ds.gx,ds.gy+1,ds.gz)
- color("active")
- for i = 1, ds.sx do
- drone.suck(0)
- end
- color("travelling")
- fly(ds.dx, ds.dy+1, ds.dz)
- color("active")
- for i = 1, drone.inventorySize() do
- drone.select(i)
- drone.drop(0)
- end
- end
- function waitForCommand()
- color("waiting")
- while true do
- evt, self, requester, _, _, request = computer.pullSignal()
- if evt == "modem_message" then
- if request == "getDrone" then
- modem.send(requester, commsPort, "confirmed", drone.inventorySize())
- local confirmed = false
- local start = computer.uptime()
- while computer.uptime() - start < 3 do
- evt, _, check, _, _, msg = computer.pullSignal(1)
- if evt == "modem_message" and check == requester then
- if msg == self then
- confirmed = true
- end
- break
- end
- end
- if confirmed then
- color("active")
- modem.send(requester, commsPort, "sendData")
- local ds = {}
- local start = computer.uptime()
- while computer.uptime() - start < 10 do
- evt, _, check, _, _, entry, data = computer.pullSignal(1)
- if evt == "modem_message" and check == requester then
- if entry == "end" then
- break
- else
- ds[entry] = data
- end
- end
- end
- if ds.sx and ds.gx and ds.gy and ds.gz and ds.dx and ds.dy and ds.dz then
- modem.close(commsPort)
- runDelivery(ds)
- modem.open(commsPort)
- else
- color("error")
- computer.beep(1000, 2)
- end
- break
- end
- end
- end
- end
- end
- function init()
- modem.open(commsPort)
- getWaypoints()
- while true do
- goHome()
- getWaypoints()
- waitForCommand()
- end
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement