Last active
June 29, 2019 00:29
-
-
Save Spyboticsguy/e17683bb9ffd7a35647816f71c034609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local port = 0x7AC0 -- port for 'TACO' delivery | |
local waypoint = "station_1" | |
local component = require("component") | |
local modem = component.modem | |
modem.open(port) | |
-- simple test program: ask for delivery to the configured waypoint | |
modem.broadcast(port, "DELIVER_TACO", waypoint) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local modem = component.proxy(component.list("modem")()) | |
local nav = component.proxy(component.list("navigation")()) | |
local drone = component.proxy(component.list("drone")()) | |
local range = nav.getRange() | |
local port = 0x7AC0 -- port for 'TACO' delivery | |
modem.open(port) | |
local function pickUpTaco() | |
-- attempt to retrieve 1 taco from storage, then check if we got one | |
drone.suck(0, 1) | |
return drone.count() >= 1 | |
end | |
local function getWaypointsByName() | |
local got_waypoints = nav.findWaypoints(range) | |
local waypoints = {} | |
for i,waypoint in ipairs(got_waypoints) do | |
local label = waypoint.label | |
local pos = waypoint.position | |
-- discards waypoints with no name | |
if label ~= "" then | |
waypoints[label] = pos | |
end | |
end | |
return waypoints | |
end | |
drone.setStatusText("BOOTING\n") | |
local waypoints = getWaypointsByName() | |
drone.setStatusText("READY\n") | |
local function deliverToWaypoint(waypoint_name) | |
local pos = waypoints[waypoint_name] | |
if not pos == nil then | |
drone.setStatusText("Delivering taco...") | |
-- TODO: change to some kind of error message | |
if not pickUpTaco() then return end | |
-- move to waypoint and store position to return later | |
local dx, dy, dz = pos | |
drone.move(dx, dy + 1, dz) | |
-- drop off the taco | |
drone.drop(0, 1) | |
-- return home | |
drone.setStatusText("Returning home...") | |
drone.move(-dx, -dy, -dz) | |
else | |
return false | |
-- TODO: change to some kind of error message | |
end | |
drone.setStatusText("") | |
return true | |
end | |
local function loop() | |
local evt,_,sender,_,dist,message,destination = computer.pullSignal() | |
-- verify that this message was meant for us | |
if evt == "modem_message" and message == "DELIVER_TACO" then | |
-- argument checking | |
if dist > range then | |
return | |
elseif destination == nil then | |
return | |
end | |
-- attempt delivery to the destination | |
if not deliverToWaypoint(destination) then | |
return false | |
end | |
end | |
return true | |
end | |
while true do | |
loop() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment