Advertisement
DOGGYWOOF

Untitled

Dec 31st, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. -- Doggy OS Turtle AI with Remote Control Feature (Turtle Side)
  2.  
  3. -- Configuration
  4. local gpsFile = "/turtle_gps_data.json"
  5. local modemSide = "left" -- Change to the side your modem is on
  6. local gpsData = {}
  7.  
  8. -- Utility functions
  9. local function log(message)
  10. print("[Doggy OS Turtle] " .. message)
  11. end
  12.  
  13. local function isBlocked()
  14. return turtle.detect()
  15. end
  16.  
  17. local function tryMoveForward()
  18. if isBlocked() then
  19. if turtle.dig() then
  20. return turtle.forward()
  21. else
  22. return false
  23. end
  24. else
  25. return turtle.forward()
  26. end
  27. end
  28.  
  29. local function tryTurn(direction)
  30. if direction == "left" then
  31. turtle.turnLeft()
  32. elseif direction == "right" then
  33. turtle.turnRight()
  34. end
  35. end
  36.  
  37. local function saveGPSData(data)
  38. local file = fs.open(gpsFile, "w")
  39. file.write(textutils.serializeJSON(data))
  40. file.close()
  41. end
  42.  
  43. local function loadGPSData()
  44. if fs.exists(gpsFile) then
  45. local file = fs.open(gpsFile, "r")
  46. local data = textutils.unserializeJSON(file.readAll())
  47. file.close()
  48. return data
  49. else
  50. return {}
  51. end
  52. end
  53.  
  54. local function getGPSLocation()
  55. local x, y, z = gps.locate(5) -- 5-second timeout
  56. if x and y and z then
  57. return { x = x, y = y, z = z }
  58. else
  59. return nil
  60. end
  61. end
  62.  
  63. -- Initialize modem
  64. local function initModem()
  65. rednet.open(modemSide) -- Opens the modem for communication
  66. log("Modem initialized, waiting for remote control commands...")
  67. end
  68.  
  69. -- Command handling
  70. local function handleCommand(command)
  71. if command == "mine" then
  72. navigateAndMine()
  73. elseif command == "explore" then
  74. exploreAndMap()
  75. elseif command == "stop" then
  76. log("Stopping Doggy OS Turtle. Returning to main menu.")
  77. return true -- Stop the turtle
  78. elseif command == "status" then
  79. local location = getGPSLocation()
  80. local fuel = turtle.getFuelLevel()
  81. -- Send back a status message
  82. rednet.send(senderID, string.format("Location: (%.2f, %.2f, %.2f) Fuel: %d", location.x, location.y, location.z, fuel))
  83. else
  84. log("Unknown command: " .. command)
  85. end
  86. return false
  87. end
  88.  
  89. -- Main server loop
  90. local function main()
  91. initModem()
  92.  
  93. while true do
  94. local senderID, message = rednet.receive() -- Receive command from the controller
  95. if message then
  96. log("Received command: " .. message)
  97. local stop = handleCommand(message)
  98. if stop then
  99. break -- Stop the turtle if the 'stop' command is received
  100. end
  101. end
  102. end
  103. end
  104.  
  105. main()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement