Advertisement
osmarks

OCDroneOS

Dec 30th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. local chat = part "chat"
  2. local nav = part "nav"
  3. local prefix = "$d "
  4.  
  5. -- A bunch of colors for the rotory areas in different situations
  6. local idleCol = 0x00FF00
  7. local movingCol = 0x0000FF
  8. local problemCol = 0xFF0000
  9. local transferCol = 0xFFFF00
  10. local calculatingCol = 0x00FFFF
  11.  
  12. -- ROUTING TABLE
  13. -- Format: {["waypointName"] = {"placeWaypointIsReachableFrom"}}
  14. local routing = {
  15.     ["GTech Tree"] = {"Home Base", "Tree On Mountain"},
  16.     ["Tree On Mountain"] = {"GTech Tree", "Gaia"},
  17.     ["Gaia"] = {"Tree On Mountain", "Desert 01"},
  18.     ["Desert 01"] = {"Gaia", "Link2006"},
  19.     ["Link2006"] = {"Desert 01", "Mushroom 01"},
  20.     ["Wojbie Incorporated"] = {"Home Base"},
  21.     ["Home Base"] = {"Wojbie Incorporated", "GTech Tree", "RandomMountainBit"},
  22.     ["TerraInc"] = {"RandomMountainBit"},
  23.     ["RandomMountainBit"] = {"TerraInc", "Home Base", "Dank Lords HQ"},
  24.     ["Dank Lords HQ"] = {"RandomMountainBit"},
  25.     ["Mushroom 01"] = {"Link2006", "Mushroom 02"},
  26.     ["Mushroom 02"] = {"Mushroom 01", "Admins"},
  27.     ["Admins"] = {"Mushroom 02"}
  28. }
  29.  
  30. chat.setName(drone.name())
  31. status(drone.name())
  32.  
  33. function setCol(col)
  34.     drone.setLightColor(col)
  35. end
  36.  
  37. -- Gets the drone's power level as a float between 0 and 1
  38. function getPower()
  39.     return computer.energy() / computer.maxEnergy()
  40. end
  41.  
  42. -- Slightly safer movement between two reasonably far points
  43. function go(rx, ry, rz)
  44.     drone.move(0, 100, 0)
  45.     sleep(3)
  46.     drone.move(rx, ry, rz)
  47.     drone.move(0, -100, 0)
  48. end
  49.  
  50. -- Gets position of a nearby named waypoint
  51. function getWaypoint(name)
  52.     local waypoints = nav.findWaypoints(128)
  53.     waypoints.n = nil -- This seems to turn up in the returned value. I don't really know why it exists.
  54.  
  55.     for _, point in pairs(waypoints) do
  56.         if point.label == name then
  57.             return point.position[1], point.position[2], point.position[3]
  58.         end
  59.     end
  60. end
  61.  
  62. -- Checks whether a waypoint is near enough to be located, and existent
  63. function waypointExists(name)
  64.     local result = getWaypoint(name)
  65.  
  66.     if result then
  67.         return true
  68.     else
  69.         return false
  70.     end
  71. end
  72.  
  73. -- Goes to a waypoint within navigation range
  74. function moveToSimple(name)
  75.     local x, y, z = getWaypoint(name)
  76.     chat.say("Going to relative coords " .. x .. ", " .. y .. ", " .. z .. " (waypoint " .. name .. ")")
  77.     setCol(movingCol)
  78.     go(x, y, z)
  79.  
  80.     while drone.getVelocity() > 0.001 do
  81.             sleep(0.5)
  82.     end
  83. end
  84.  
  85. -- Reverses a table - does not do it in-place
  86. function reverse(tab)
  87.     local ret = {}
  88.     for k, v in pairs(tab) do
  89.         ret[#tab - k + 1] = v
  90.     end
  91.  
  92.     return ret
  93. end
  94.  
  95. -- from https://www.redblobgames.com/pathfinding/a-star/introduction.html
  96. -- Gets a route from from to to using the routing table as a graph to search
  97. function getRoute(from, to)
  98.     -- places to search next
  99.     local frontier = {from}
  100.     -- where each place was reached from
  101.     local cameFrom = {[from] = nil}
  102.  
  103.     while #frontier > 0 do
  104.         local current = table.remove(frontier, 1)
  105.         for _, nextLoc in pairs(routing[current]) do
  106.             if not cameFrom[nextLoc] then -- check if nextLoc has an entry in cameFrom
  107.                 table.insert(frontier, nextLoc)
  108.                 cameFrom[nextLoc] = current
  109.             end
  110.         end
  111.     end
  112.  
  113.     -- Extrapolate from reversed path available in cameFrom
  114.     local current = to
  115.     local path = {}
  116.    
  117.     while current ~= from do
  118.         table.insert(path, current)
  119.         current = cameFrom[current]
  120.     end
  121.  
  122.     return reverse(path)
  123. end
  124.  
  125. function moveTo(from, to)
  126.     if waypointExists(to) then
  127.         moveToSimple(to)
  128.     else
  129.         setCol(calculatingCol)
  130.         for _, waypoint in pairs(getRoute(from, to)) do
  131.             moveToSimple(waypoint)
  132.         end
  133.     end
  134. end
  135.  
  136. function chatListen()
  137.     while true do
  138.         local type, _, user, msg = computer.pullSignal()
  139.  
  140.         if type == "chat_message" then
  141.             return user, msg
  142.         end
  143.     end
  144. end
  145.  
  146. chat.say("Navigation range is " .. tostring(nav.getRange()) .. ".")
  147.  
  148. while true do
  149.     setCol(idleCol)
  150.  
  151.     if getPower() < 0.2 then
  152.         setCol(problemCol)
  153.         chat.say "Please return me to gollark. I am stranded and on low power."
  154.         status "POWER LOW"
  155.         sleep(100)
  156.     end
  157.  
  158.     local usr, msg = chatListen()
  159.    
  160.     if string.sub(msg, 1, #prefix) == prefix then
  161.         local loc = string.sub(msg, #prefix + 1)
  162.         chat.say('"' .. loc .. '"')
  163.  
  164.         setCol(transferCol)
  165.  
  166.         for i = 1, 4 do
  167.             drone.select(i)
  168.             drone.suck(0, 64)
  169.             sleep(1)
  170.         end
  171.  
  172.         moveTo("Home Base", loc)
  173.        
  174.         setCol(transferCol)
  175.  
  176.         for i = 1, 4 do
  177.             drone.select(i)
  178.             drone.drop(0)
  179.         end
  180.  
  181.         chat.say("Enjoy your things!")
  182.  
  183.         moveTo(loc, "Home Base")
  184.     end
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement