osmarks

Untitled

Mar 28th, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local modem = peripheral.find "modem"
  2. local websocket = http.websocket "ws://osmarks.ml:8774"
  3.  
  4. local geofenceChannel = 40560
  5. modem.open(geofenceChannel)
  6.  
  7. function loadPastebinAPI(paste, name)
  8.     shell.run("pastebin get " .. paste .. " " .. name)
  9.     os.loadAPI(name)
  10. end
  11.  
  12. loadPastebinAPI("4nRg9CHU", "json")
  13.  
  14. local designation = tostring(os.getComputerID())
  15.  
  16. os.setComputerLabel(designation)
  17.  
  18. -- Closest waypoint currently
  19. local closest = {"None", 1000}
  20. -- Threshold for going away
  21. local threshold = 20
  22.  
  23. -- Report to control system
  24. function report(data)
  25.     data["id"] = os.getComputerID()
  26.     local enc = json.encode(data)
  27.     websocket.send(enc)
  28. end
  29.  
  30. report({type = "connect", ["node-type"] = "turtle"})
  31.  
  32. function distance(x1, x2, y1, y2, z1, z2)
  33.     local dx, dy, dz = x1 - x2, y1 - y2, z1 - z2
  34.     return math.sqrt(dx ^ 2 + dy ^ 2 + dz ^ 2)
  35. end
  36.  
  37. -- Repeats a turtle movement function X times.
  38. -- Will turn if a wall is hit.
  39. function repeatF(i, f)
  40.     for _ = 1, i do
  41.         if not f() then
  42.             turtle.turnLeft()
  43.         end
  44.     end
  45. end
  46.  
  47. -- If number of times is less than zero, perform a different function
  48. function pick(i, f1, f2)
  49.     if i < 0 then
  50.         repeatF(math.abs(i), f2)
  51.     else
  52.         repeatF(i, f1)
  53.     end
  54. end
  55.  
  56. function move(x, y, z)
  57.     pick(x, turtle.forward, turtle.back)
  58.     pick(y, turtle.up, turtle.down)
  59.    
  60.     if z < 0 then
  61.         turtle.turnRight()
  62.     elseif z > 0 then
  63.         turtle.turnLeft()
  64.     end
  65.    
  66.     repeatF(math.abs(z), turtle.forward)
  67.  
  68.     if closest[2] < threshold then
  69.         move(-x, -y, -z)
  70.     end
  71. end
  72.  
  73. -- Check whether kill code enabled and if so shutdown
  74. function killCode()
  75.     while true do
  76.         http.request "https://osmarks.ml/killcode"
  77.         _, _, h = os.pullEvent "http_success"
  78.  
  79.         if h then
  80.             local t = h.readAll()
  81.             if t == "KILLCODE" then
  82.                 os.shutdown()
  83.             elseif t == "REBOOT" then
  84.                 sleep(5)
  85.                 os.reboot()
  86.             end
  87.         end
  88.         sleep(20)
  89.     end
  90. end
  91.  
  92. -- Report back position
  93. function reportPos()
  94.     while true do
  95.         local x, y, z = gps.locate()
  96.         report({type = "position", position = {x = x, y = y, z = z}})
  97.         sleep(15)
  98.     end
  99. end
  100.  
  101. -- Upon a modem message being transmitted (presumably on waypoint channels),
  102. -- log its identifier + distance, and attempt to find nearest waypoint
  103. function updateWaypoints()
  104.     local waypoints = {}
  105.  
  106.     while true do
  107.         local _, _, chan, _, msg, distance = os.pullEvent "modem_message"
  108.  
  109.         if chan == geofenceChannel then
  110.             waypoints[msg] = distance
  111.  
  112.             closest = {msg, distance}
  113.  
  114.             for identifier, d in pairs(waypoints) do
  115.                 if d < closest[2] then closest = {identifier, d} end
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. -- Listen to websocketed commands
  122. function runCommands()
  123.     while true do
  124.         local msg = websocket.receive()
  125.         local data = json.decode(msg)
  126.  
  127.         print(textutils.serialise(data))
  128.  
  129.         if data and data.command then
  130.             local cmd = data.command
  131.  
  132.             if cmd == "reboot" then
  133.                 os.reboot()
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. local run = {reportPos, killCode, updateWaypoints, runCommands}
  140.  
  141. -- If it has ended, it's not as if it's NOT going to be with an error.
  142. local _, err = pcall(parallel.waitForAll, unpack(run))
  143.  
  144. report({type = "report-error", error = err})
  145.  
  146. sleep(10)
  147. os.reboot()
Add Comment
Please, Sign In to add comment