Advertisement
DOGGYWOOF

Doggy OS Turtle Dashboard

Dec 31st, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. -- Doggy OS Turtle AI with UI Dashboard and Refuel Option (No Auto-Refuel, Refuel at 0)
  2.  
  3. -- Configuration
  4. local gpsFile = "/turtle_gps_data.json"
  5.  
  6. -- Utility functions
  7. local function log(message)
  8. print("[Doggy OS Turtle] " .. 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("Doggy OS Turtle Dashboard")
  65.  
  66. -- Dynamic number of dashes based on screen width
  67. local screenWidth, _ = term.getSize()
  68. local dashLine = string.rep("-", screenWidth)
  69. print(dashLine)
  70.  
  71. print("Mode : " .. mode)
  72. print("Steps : " .. steps)
  73.  
  74. -- Fuel level indicator with blinking colors based on fuel
  75. if fuel == 0 then
  76. term.setTextColor(colors.red) -- Red for out of fuel
  77. print("Fuel : 0 (Out of Fuel)")
  78. elseif fuel < 20 then
  79. term.setTextColor(colors.red) -- Red for critical fuel level
  80. print("Fuel : " .. fuel .. " (Critical)")
  81. elseif fuel < 60 then
  82. term.setTextColor(colors.orange) -- Orange for low fuel level
  83. print("Fuel : " .. fuel .. " (Low)")
  84. else
  85. term.setTextColor(colors.green) -- Green for sufficient fuel
  86. print("Fuel : " .. fuel)
  87. end
  88.  
  89. -- Reset to default text color
  90. term.setTextColor(colors.white)
  91.  
  92. if location then
  93. print(string.format("Location : (%.2f, %.2f, %.2f)", location.x, location.y, location.z))
  94. else
  95. print("Location : GPS unavailable")
  96. end
  97. print(dashLine)
  98. end
  99.  
  100. local function refuel()
  101. local fuel = turtle.getFuelLevel()
  102. if fuel == 0 then
  103. log("Fuel level is 0. Please refuel immediately!")
  104.  
  105. -- Check for fuel in the Turtle's inventory
  106. for slot = 1, 16 do
  107. turtle.select(slot)
  108. if turtle.getItemCount() > 0 then
  109. local item = turtle.getItemDetail()
  110. if item.name == "minecraft:coal" or item.name == "minecraft:charcoal" then
  111. if turtle.refuel(1) then
  112. log("Refueled with " .. item.name)
  113. return true
  114. end
  115. end
  116. end
  117. end
  118. log("No fuel available in inventory. Stopping.")
  119. end
  120. return false
  121. end
  122.  
  123. -- Core AI logic
  124. local function navigateAndMine()
  125. local steps = 0
  126. local gpsData = loadGPSData()
  127.  
  128. while true do
  129. local location = getGPSLocation()
  130. local fuel = turtle.getFuelLevel()
  131. updateDashboard("Mining", location, fuel, steps)
  132.  
  133. if fuel == 0 then
  134. log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
  135. break -- Stop movement and break out of the loop to return to main menu
  136. end
  137.  
  138. if location then
  139. gpsData[tostring(os.clock())] = location
  140. saveGPSData(gpsData)
  141. end
  142.  
  143. if tryMoveForward() then
  144. steps = steps + 1
  145. else
  146. tryTurn("right")
  147. end
  148.  
  149. sleep(0.5)
  150. end
  151. end
  152.  
  153. local function exploreAndMap()
  154. local gpsData = loadGPSData()
  155. local steps = 0
  156.  
  157. while true do
  158. local location = getGPSLocation()
  159. local fuel = turtle.getFuelLevel()
  160. updateDashboard("Exploring", location, fuel, steps)
  161.  
  162. if fuel == 0 then
  163. log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
  164. break -- Stop movement and break out of the loop to return to main menu
  165. end
  166.  
  167. if location then
  168. gpsData[tostring(os.clock())] = location
  169. saveGPSData(gpsData)
  170. end
  171.  
  172. if not tryMoveForward() then
  173. tryTurn("right")
  174. end
  175.  
  176. steps = steps + 1
  177. sleep(0.5)
  178. end
  179. end
  180.  
  181. -- Main program
  182. local function main()
  183. while true do
  184. updateDashboard("Idle", nil, turtle.getFuelLevel(), 0)
  185. print("Commands: mine, explore, refuel, stop")
  186. local command = read()
  187.  
  188. if command == "mine" then
  189. navigateAndMine()
  190. elseif command == "explore" then
  191. exploreAndMap()
  192. elseif command == "refuel" then
  193. if refuel() then
  194. updateDashboard("Idle", getGPSLocation(), turtle.getFuelLevel(), 0)
  195. end
  196. elseif command == "stop" then
  197. log("Stopping Doggy OS Turtle. Returning to main menu.")
  198. break
  199. else
  200. print("Unknown command!")
  201. end
  202. end
  203. end
  204.  
  205. main()
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement