Advertisement
DOGGYWOOF

Doggy OS TurtleAI

Dec 31st, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. -- Turtle AI with UI Dashboard
  2.  
  3. -- Configuration
  4. local gpsFile = "/turtle_gps_data.json"
  5.  
  6. -- Utility functions
  7. local function log(message)
  8. print("[Turtle AI] " .. message)
  9. end
  10.  
  11. local function isBlocked()
  12. return turtle.detect()
  13. end
  14.  
  15. local function tryMoveForward()
  16. if isBlocked() then
  17. if turtle.dig() then
  18. return turtle.forward()
  19. else
  20. return false
  21. end
  22. else
  23. return turtle.forward()
  24. end
  25. end
  26.  
  27. local function tryTurn(direction)
  28. if direction == "left" then
  29. turtle.turnLeft()
  30. elseif direction == "right" then
  31. turtle.turnRight()
  32. end
  33. end
  34.  
  35. local function saveGPSData(data)
  36. local file = fs.open(gpsFile, "w")
  37. file.write(textutils.serializeJSON(data))
  38. file.close()
  39. end
  40.  
  41. local function loadGPSData()
  42. if fs.exists(gpsFile) then
  43. local file = fs.open(gpsFile, "r")
  44. local data = textutils.unserializeJSON(file.readAll())
  45. file.close()
  46. return data
  47. else
  48. return {}
  49. end
  50. end
  51.  
  52. local function getGPSLocation()
  53. local x, y, z = gps.locate(5) -- 5-second timeout
  54. if x and y and z then
  55. return { x = x, y = y, z = z }
  56. else
  57. return nil
  58. end
  59. end
  60.  
  61. local function updateDashboard(mode, location, fuel, steps)
  62. term.clear()
  63. term.setCursorPos(1, 1)
  64. print("Turtle AI Dashboard")
  65. print("--------------------")
  66. print("Mode : " .. mode)
  67. print("Steps : " .. steps)
  68. print("Fuel : " .. fuel)
  69. if location then
  70. print(string.format("Location : (%.2f, %.2f, %.2f)", location.x, location.y, location.z))
  71. else
  72. print("Location : GPS unavailable")
  73. end
  74. print("--------------------")
  75. end
  76.  
  77. -- Core AI logic
  78. local function navigateAndMine()
  79. local steps = 0
  80. local gpsData = loadGPSData()
  81.  
  82. while true do
  83. local location = getGPSLocation()
  84. local fuel = turtle.getFuelLevel()
  85. updateDashboard("Mining", location, fuel, steps)
  86.  
  87. if fuel < 100 then
  88. if turtle.refuel(1) then
  89. updateDashboard("Refueling", location, fuel, steps)
  90. else
  91. break
  92. end
  93. end
  94.  
  95. if location then
  96. gpsData[tostring(os.clock())] = location
  97. saveGPSData(gpsData)
  98. end
  99.  
  100. if tryMoveForward() then
  101. steps = steps + 1
  102. else
  103. tryTurn("right")
  104. end
  105.  
  106. sleep(0.5)
  107. end
  108. end
  109.  
  110. local function exploreAndMap()
  111. local gpsData = loadGPSData()
  112. local steps = 0
  113.  
  114. while true do
  115. local location = getGPSLocation()
  116. local fuel = turtle.getFuelLevel()
  117. updateDashboard("Exploring", location, fuel, steps)
  118.  
  119. if location then
  120. gpsData[tostring(os.clock())] = location
  121. saveGPSData(gpsData)
  122. end
  123.  
  124. if not tryMoveForward() then
  125. tryTurn("right")
  126. end
  127.  
  128. steps = steps + 1
  129. sleep(0.5)
  130. end
  131. end
  132.  
  133. -- Main program
  134. local function main()
  135. while true do
  136. updateDashboard("Idle", nil, turtle.getFuelLevel(), 0)
  137. print("Commands: mine, explore, stop")
  138. local command = read()
  139.  
  140. if command == "mine" then
  141. navigateAndMine()
  142. elseif command == "explore" then
  143. exploreAndMap()
  144. elseif command == "stop" then
  145. break
  146. else
  147. print("Unknown command!")
  148. end
  149. end
  150. end
  151.  
  152. main()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement