Advertisement
montur

turtle_main

Sep 18th, 2023 (edited)
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. if not os.getComputerLabel() then
  2.   print(" ")
  3.   print("Give this turtle a name: ")
  4.   local label = read()
  5.   shell.execute("label", "set", label)
  6.   print("Thank you for giving me a wonderfull name UwU: ")
  7. end
  8.  
  9. local serverUrl = "ws://84.86.255.191:8765"
  10.  
  11. -- login info
  12. loginInfo = {
  13.   ["name"] = os.getComputerLabel(),
  14.   ["device"] = "turtle",
  15.   ["loggedIn"] = false,
  16. }
  17.  
  18. -- general info
  19. info = {
  20.   ["response"] = "none",
  21.   ["fuel"] = turtle.getFuelLevel(),
  22.   ["inspect"] = inspect,
  23.  
  24.   ["device"] = loginInfo.device,
  25.   ["loggedIn"] = loginInfo.loggedIn
  26. }
  27.  
  28. os.loadAPI("json")
  29.  
  30. if fs.exists("latestPos") then
  31.  
  32.   local file = fs.open("latestPos","r")
  33.   pos = json.decode(file.readLine())
  34.  
  35. else
  36.   print(" ")
  37.   print("This turtle does not know where it is!")
  38.   print("x =")
  39.   local newX = tonumber(read())
  40.   print("y =")
  41.   local newY = tonumber(read())
  42.   print("z =")
  43.   local newZ = tonumber(read())
  44.   print("this turtle is facing: <north> <south> <east> <west>")
  45.   local newFacing = read()
  46.   pos = {
  47.     ["x"] = newX,
  48.     ["y"] = newY,
  49.     ["z"] = newZ,
  50.     ["facing"] = newFacing,
  51.     ["world"] = "overworld"
  52.   }
  53.   print("This turtle now knows where it is. becouse it knows where it isnt. thanks to you <3")
  54.   local file = fs.open("latestPos","a")
  55.   file.write(json.encode(pos))
  56.   file.close()
  57. end
  58.  
  59. -- Connect to the WebSocket server
  60. function connect()
  61.   local attemptCount = 0
  62.   local connected = false
  63.  
  64.   while not connected do
  65.     attemptCount = attemptCount + 1
  66.  
  67.     print("Attempt #" .. attemptCount .. " to connect to: ", serverUrl)
  68.  
  69.     ws, err = http.websocket(serverUrl)
  70.  
  71.     if ws then
  72.         connected = true
  73.         print("Connection successful!")
  74.     else
  75.         print("Connection failed. Retrying in 5 seconds...")
  76.         os.sleep(5)  -- Adjust the sleep duration as needed
  77.     end
  78.   end
  79. end
  80.  
  81. function close()
  82.   ws.close()
  83.   for i = 30, 0, -1 do
  84.     term.clear()
  85.     print("server has closed.....")
  86.     print("rebooting: " .. i .. " seconds")
  87.     sleep(1)
  88.   end
  89.   os.reboot()
  90.   return true
  91. end
  92.  
  93. -- Communication functions
  94. local function sendData(data)
  95.   if ws then
  96.     local success, error_message = pcall(function()
  97.       ws.send(json.encode(data))
  98.     end)
  99.  
  100.     if not success then
  101.       print("Error sending data:", error_message)
  102.       shell.execute("turtle_main")
  103.     end
  104.   else
  105.     print("WebSocket connection does not exist. Unable to send data.")
  106.     -- Optionally, you can attempt to reconnect or handle the situation accordingly.
  107.   end
  108. end
  109.  
  110. function forward()
  111.   local result = turtle.forward()
  112.   if result then
  113.     if pos["facing"] == "north" then
  114.       pos["z"] = pos["z"] - 1
  115.     elseif pos["facing"] == "east" then
  116.       pos["x"] = pos["x"] + 1
  117.     elseif pos["facing"] == "south" then
  118.       pos["z"] = pos["z"] + 1
  119.     elseif pos["facing"] == "west" then
  120.       pos["x"] = pos["x"] - 1
  121.     end
  122.     print(textutils.serialise(pos))
  123.   end
  124.   return result
  125. end
  126.  
  127. function back()
  128.   local result = turtle.back()
  129.   if result then
  130.     if pos["facing"] == "north" then
  131.       pos["z"] = pos["z"] + 1
  132.     elseif pos["facing"] == "east" then
  133.       pos["x"] = pos["x"] - 1
  134.     elseif pos["facing"] == "south" then
  135.       pos["z"] = pos["z"] - 1
  136.     elseif pos["facing"] == "west" then
  137.       pos["x"] = pos["x"] + 1
  138.     end
  139.     print(textutils.serialise(pos))
  140.   end
  141.   return result
  142. end
  143.  
  144. function left()
  145.   local result = turtle.turnLeft()
  146.   if result then
  147.     if pos["facing"] == "north" then
  148.       pos["facing"] = "west"
  149.     elseif pos["facing"] == "east" then
  150.       pos["facing"] = "north"
  151.     elseif pos["facing"] == "south" then
  152.       pos["facing"] = "east"
  153.     elseif pos["facing"] == "west" then
  154.       pos["facing"] = "south"
  155.     end
  156.     print(textutils.serialise(pos))
  157.   end
  158.   return result
  159. end
  160.  
  161. function right()
  162.   local result = turtle.turnRight()
  163.   if result then
  164.     if pos["facing"] == "north" then
  165.       pos["facing"] = "east"
  166.     elseif pos["facing"] == "east" then
  167.       pos["facing"] = "south"
  168.     elseif pos["facing"] == "south" then
  169.       pos["facing"] = "west"
  170.     elseif pos["facing"] == "west" then
  171.       pos["facing"] = "north"
  172.     end
  173.     print(textutils.serialise(pos))
  174.   end
  175.   return result
  176. end
  177.  
  178. function up()
  179.   local result = turtle.up()
  180.   if result then
  181.     pos["y"] = pos["y"] + 1
  182.     print(textutils.serialise(pos))
  183.   end
  184.   return result
  185. end
  186.  
  187. function down()
  188.   local result = turtle.down()
  189.   if result then
  190.     pos["y"] = pos["y"] - 1
  191.     print(textutils.serialise(pos))
  192.   end
  193.   return result
  194. end
  195.  
  196. connect()
  197.  
  198. -- Check WebSocket server
  199. if not ws then
  200.   print("Failed to connect to WebSocket server:", err)
  201.   return
  202. end
  203.  
  204. print("Connected to Server. trying to login...")
  205. sendData(loginInfo)
  206.  
  207. -- Receive and handle messages from the WebSocket server
  208. while true do
  209.   --print("Waiting on server...")
  210.   local inMessageStr, err = ws.receive()
  211.   local inMessage = json.decode(inMessageStr)
  212.   if not inMessageStr then
  213.  
  214.     print("Failed to receive message:", err)
  215.     break
  216.  
  217.   else
  218.  
  219.     if not loginInfo["loggedIn"] then
  220.       loginInfo["loggedIn"] = inMessage["loggedIn"]
  221.       print("Loginstatus: " .. tostring(loginInfo["loggedIn"]))
  222.     end
  223.  
  224.     if loginInfo["loggedIn"] then
  225.  
  226.       local serverCode, err = load(inMessage["code"], "whatever", "t", _ENV)
  227.  
  228.       if serverCode then
  229.         response = serverCode()  -- Execute the loaded code
  230.       else
  231.           print("Error:", err)
  232.       end
  233.  
  234.       --inspecting ground
  235.       local frontBlock, frontInfo = turtle.inspect()
  236.       local upBlock, upInfo = turtle.inspectUp()
  237.       local downBlock, downInfo = turtle.inspectDown()
  238.       inspect = {
  239.         ["frontBlock"] = frontBlock,
  240.         ["upBlock"] = upBlock,
  241.         ["downBlock"] = downBlock,
  242.         ["frontInfo"] = frontInfo,
  243.         ["upInfo"] = upInfo,
  244.         ["downInfo"] = downInfo
  245.       }
  246.      
  247.       --gathering information to send
  248.       info = {
  249.         ["response"] = response,
  250.         ["fuel"] = turtle.getFuelLevel(),
  251.         ["inspect"] = inspect,
  252.         ["pos"] = pos,
  253.  
  254.         ["device"] = loginInfo.device,
  255.         ["loggedIn"] = loginInfo.loggedIn,
  256.         ["name"] = os.getComputerLabel()
  257.       }
  258.  
  259.       fs.delete("latestPos")
  260.       local file = fs.open("latestPos","a")
  261.       file.write(json.encode(pos))
  262.       file.close()
  263.  
  264.       sendData(info)
  265.     end
  266.   end
  267. end
  268.  
  269. -- Close the WebSocket connection when done
  270. ws.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement