Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chat = part "chat"
- local nav = part "nav"
- local prefix = "$d "
- -- A bunch of colors for the rotory areas in different situations
- local idleCol = 0x00FF00
- local movingCol = 0x0000FF
- local problemCol = 0xFF0000
- local transferCol = 0xFFFF00
- local calculatingCol = 0x00FFFF
- -- ROUTING TABLE
- -- Format: {["waypointName"] = {"placeWaypointIsReachableFrom"}}
- local routing = {
- ["GTech Tree"] = {"Home Base", "Tree On Mountain"},
- ["Tree On Mountain"] = {"GTech Tree", "Gaia"},
- ["Gaia"] = {"Tree On Mountain", "Desert 01"},
- ["Desert 01"] = {"Gaia", "Link2006"},
- ["Link2006"] = {"Desert 01", "Mushroom 01"},
- ["Wojbie Incorporated"] = {"Home Base"},
- ["Home Base"] = {"Wojbie Incorporated", "GTech Tree", "RandomMountainBit"},
- ["TerraInc"] = {"RandomMountainBit"},
- ["RandomMountainBit"] = {"TerraInc", "Home Base", "Dank Lords HQ"},
- ["Dank Lords HQ"] = {"RandomMountainBit"},
- ["Mushroom 01"] = {"Link2006", "Mushroom 02"},
- ["Mushroom 02"] = {"Mushroom 01", "Admins"},
- ["Admins"] = {"Mushroom 02"}
- }
- chat.setName(drone.name())
- status(drone.name())
- function setCol(col)
- drone.setLightColor(col)
- end
- -- Gets the drone's power level as a float between 0 and 1
- function getPower()
- return computer.energy() / computer.maxEnergy()
- end
- -- Slightly safer movement between two reasonably far points
- function go(rx, ry, rz)
- drone.move(0, 100, 0)
- sleep(3)
- drone.move(rx, ry, rz)
- drone.move(0, -100, 0)
- end
- -- Gets position of a nearby named waypoint
- function getWaypoint(name)
- local waypoints = nav.findWaypoints(128)
- waypoints.n = nil -- This seems to turn up in the returned value. I don't really know why it exists.
- for _, point in pairs(waypoints) do
- if point.label == name then
- return point.position[1], point.position[2], point.position[3]
- end
- end
- end
- -- Checks whether a waypoint is near enough to be located, and existent
- function waypointExists(name)
- local result = getWaypoint(name)
- if result then
- return true
- else
- return false
- end
- end
- -- Goes to a waypoint within navigation range
- function moveToSimple(name)
- local x, y, z = getWaypoint(name)
- chat.say("Going to relative coords " .. x .. ", " .. y .. ", " .. z .. " (waypoint " .. name .. ")")
- setCol(movingCol)
- go(x, y, z)
- while drone.getVelocity() > 0.001 do
- sleep(0.5)
- end
- end
- -- Reverses a table - does not do it in-place
- function reverse(tab)
- local ret = {}
- for k, v in pairs(tab) do
- ret[#tab - k + 1] = v
- end
- return ret
- end
- -- from https://www.redblobgames.com/pathfinding/a-star/introduction.html
- -- Gets a route from from to to using the routing table as a graph to search
- function getRoute(from, to)
- -- places to search next
- local frontier = {from}
- -- where each place was reached from
- local cameFrom = {[from] = nil}
- while #frontier > 0 do
- local current = table.remove(frontier, 1)
- for _, nextLoc in pairs(routing[current]) do
- if not cameFrom[nextLoc] then -- check if nextLoc has an entry in cameFrom
- table.insert(frontier, nextLoc)
- cameFrom[nextLoc] = current
- end
- end
- end
- -- Extrapolate from reversed path available in cameFrom
- local current = to
- local path = {}
- while current ~= from do
- table.insert(path, current)
- current = cameFrom[current]
- end
- return reverse(path)
- end
- function moveTo(from, to)
- if waypointExists(to) then
- moveToSimple(to)
- else
- setCol(calculatingCol)
- for _, waypoint in pairs(getRoute(from, to)) do
- moveToSimple(waypoint)
- end
- end
- end
- function chatListen()
- while true do
- local type, _, user, msg = computer.pullSignal()
- if type == "chat_message" then
- return user, msg
- end
- end
- end
- chat.say("Navigation range is " .. tostring(nav.getRange()) .. ".")
- while true do
- setCol(idleCol)
- if getPower() < 0.2 then
- setCol(problemCol)
- chat.say "Please return me to gollark. I am stranded and on low power."
- status "POWER LOW"
- sleep(100)
- end
- local usr, msg = chatListen()
- if string.sub(msg, 1, #prefix) == prefix then
- local loc = string.sub(msg, #prefix + 1)
- chat.say('"' .. loc .. '"')
- setCol(transferCol)
- for i = 1, 4 do
- drone.select(i)
- drone.suck(0, 64)
- sleep(1)
- end
- moveTo("Home Base", loc)
- setCol(transferCol)
- for i = 1, 4 do
- drone.select(i)
- drone.drop(0)
- end
- chat.say("Enjoy your things!")
- moveTo(loc, "Home Base")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement