toxicfrazzles

Turtle Swarm CC: Tweaked

Feb 10th, 2021 (edited)
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. term.clear()
  2. print("Turtle Swarm Drone")
  3.  
  4. os.loadAPI("json")
  5. os.loadAPI("turtle2")
  6.  
  7. local ws_url = "ws://172.16.1.4:8000/ws/main/turtle/"
  8. local ws, err
  9.  
  10. while true do
  11.     ws, err = http.websocket(ws_url)
  12.  
  13.     if not ws then
  14.         printError(err)
  15.         sleep(5)
  16.     else
  17.         print("Connected to server")
  18.         break
  19.     end
  20. end
  21.  
  22. function move(direction, force)
  23.     if direction == "forward" then
  24.         if not turtle2.forward() and force then
  25.             turtle.dig()
  26.             turtle2.forward()
  27.         end
  28.     elseif direction == "back" then
  29.         turtle2.back()
  30.     elseif direction == "left" then
  31.         turtle2.turnLeft()
  32.     elseif direction == "right" then
  33.         turtle2.turnRight()
  34.     elseif direction == "down" then
  35.         if not turtle2.down() and force then
  36.             turtle.digDown()
  37.             turtle2.down()
  38.         end
  39.     elseif direction == "up" then
  40.         if not turtle2.up() and force then
  41.             turtle.digUp()
  42.             turtle2.up()
  43.         end
  44.     end
  45. end
  46.  
  47. function sendInfo()
  48.     local data = {
  49.         type = "turtleInfo",
  50.         id = os.getComputerID(),
  51.         label = os.getComputerLabel(),
  52.         fuel = turtle.getFuelLevel(),
  53.         maxFuel = turtle.getFuelLimit()
  54.     }
  55.     ws.send(json.encode(data))
  56. end
  57.  
  58. function sendPosition()
  59.     local data = {
  60.         type = "position",
  61.         xPos = turtle2.xpos,
  62.         yPos = turtle2.ypos,
  63.         zPos = turtle2.zPos,
  64.         direction = turtle2.facing,
  65.     }
  66.     ws.send(json.encode(data))
  67. end
  68.  
  69.  
  70. sendInfo()
  71. while true do
  72.     local event, url, response, isBinary = os.pullEvent()
  73.     if event == "websocket_message" then
  74.         local message_obj = json.decode(response)
  75.         if message_obj.type == "command" then
  76.             local func, err = loadstring("return " .. message_obj.command)
  77.             if func then
  78.                 local result = func()
  79.                 ws.send(json.encode({
  80.                             type = "command_response",
  81.                             command = message_obj.command,
  82.                             response = tostring(result)
  83.                         }))
  84.             else
  85.                 return printError(err)
  86.             end
  87.         elseif message_obj.type == "move" then
  88.             move(message_obj.direction, message_obj.force)
  89.             sendPosition()
  90.         else
  91.             print(response)
  92.         end
  93.     elseif event == "websocket_closed" then
  94.         print("Disconnected")
  95.         while true do
  96.             ws, err = http.websocket(ws_url)
  97.  
  98.             if not ws then
  99.                 printError(err)
  100.                 sleep(5)
  101.             else
  102.                 print("Connected to server")
  103.                 break
  104.             end
  105.         end
  106.     else
  107.         print(event)
  108.     end
  109. end
  110. ws.close()
  111.  
Add Comment
Please, Sign In to add comment