Advertisement
DOGGYWOOF

Doggy OS Turtle System

Dec 31st, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. -- Doggy OS Turtle AI with Settings, GPS Display, Password Protection, and Update Script
  2.  
  3. -- Configuration
  4. local gpsFile = "/turtle_gps_data.json"
  5. local settingsFile = "/turtle_settings.json"
  6.  
  7. -- Utility functions
  8. local function log(message)
  9. print("[Doggy OS Turtle] " .. message)
  10. end
  11.  
  12. local function isBlocked()
  13. return turtle.detect()
  14. end
  15.  
  16. local function tryMoveForward()
  17. if isBlocked() then
  18. if turtle.dig() then
  19. return turtle.forward()
  20. else
  21. return false
  22. end
  23. else
  24. return turtle.forward()
  25. end
  26. end
  27.  
  28. local function tryTurn(direction)
  29. if direction == "left" then
  30. turtle.turnLeft()
  31. elseif direction == "right" then
  32. turtle.turnRight()
  33. end
  34. end
  35.  
  36. local function saveGPSData(data)
  37. local file = fs.open(gpsFile, "w")
  38. file.write(textutils.serializeJSON(data))
  39. file.close()
  40. end
  41.  
  42. local function loadGPSData()
  43. if fs.exists(gpsFile) then
  44. local file = fs.open(gpsFile, "r")
  45. local data = textutils.unserializeJSON(file.readAll())
  46. file.close()
  47. return data
  48. else
  49. return {}
  50. end
  51. end
  52.  
  53. -- Ensure that settings file exists or create with default values
  54. local function ensureSettingsExist()
  55. if not fs.exists(settingsFile) then
  56. -- Create the settings file with default values if it doesn't exist
  57. local defaultSettings = { password = nil, gpsEnabled = true } -- Default GPS enabled
  58. local file = fs.open(settingsFile, "w")
  59. file.write(textutils.serializeJSON(defaultSettings))
  60. file.close()
  61. end
  62. end
  63.  
  64. local function loadSettings()
  65. ensureSettingsExist() -- Ensure settings file exists
  66. local file = fs.open(settingsFile, "r")
  67. local data = textutils.unserializeJSON(file.readAll())
  68. file.close()
  69. return data
  70. end
  71.  
  72. local function saveSettings(settings)
  73. local file = fs.open(settingsFile, "w")
  74. file.write(textutils.serializeJSON(settings))
  75. file.close()
  76. end
  77.  
  78. local function getGPSLocation()
  79. local gpsSettings = loadSettings()
  80. if gpsSettings.gpsEnabled then
  81. local x, y, z = gps.locate(5) -- 5-second timeout
  82. if x and y and z then
  83. return { x = x, y = y, z = z }
  84. else
  85. return nil
  86. end
  87. else
  88. return nil
  89. end
  90. end
  91.  
  92. local function updateDashboard(mode, location, fuel, steps)
  93. os.pullEvent = os.pullEventRaw
  94. term.clear()
  95. term.setCursorPos(1, 1)
  96. print("Doggy OS Turtle Dashboard")
  97.  
  98. -- Dynamic number of dashes based on screen width
  99. local screenWidth, _ = term.getSize()
  100. local dashLine = string.rep("-", screenWidth)
  101. print(dashLine)
  102.  
  103. print("Mode : " .. mode)
  104. print("Steps : " .. steps)
  105.  
  106. -- Fuel level indicator with blinking colors based on fuel
  107. if fuel == 0 then
  108. term.setTextColor(colors.red) -- Red for out of fuel
  109. print("Fuel : 0 (Out of Fuel)")
  110. elseif fuel < 20 then
  111. term.setTextColor(colors.red) -- Red for critical fuel level
  112. print("Fuel : " .. fuel .. " (Critical)")
  113. elseif fuel < 60 then
  114. term.setTextColor(colors.orange) -- Orange for low fuel level
  115. print("Fuel : " .. fuel .. " (Low)")
  116. else
  117. term.setTextColor(colors.green) -- Green for sufficient fuel
  118. print("Fuel : " .. fuel)
  119. end
  120.  
  121. -- Reset to default text color
  122. term.setTextColor(colors.white)
  123.  
  124. if location then
  125. print(string.format("Location : (%.2f, %.2f, %.2f)", location.x, location.y, location.z))
  126. else
  127. -- If GPS is disabled, print "GPS Disabled"
  128. if location == nil and not loadSettings().gpsEnabled then
  129. print("Location : GPS Disabled")
  130. else
  131. print("Location : GPS unavailable")
  132. end
  133. end
  134. print(dashLine)
  135. end
  136.  
  137. local function refuel()
  138. local fuel = turtle.getFuelLevel()
  139. if fuel == 0 then
  140. log("Fuel level is 0. Please refuel immediately!")
  141.  
  142. -- Check for fuel in the Turtle's inventory
  143. for slot = 1, 16 do
  144. turtle.select(slot)
  145. if turtle.getItemCount() > 0 then
  146. local item = turtle.getItemDetail()
  147. if item.name == "minecraft:coal" or item.name == "minecraft:charcoal" then
  148. if turtle.refuel(1) then
  149. log("Refueled with " .. item.name)
  150. return true
  151. end
  152. end
  153. end
  154. end
  155. log("No fuel available in inventory. Stopping.")
  156. end
  157. return false
  158. end
  159.  
  160. -- Core AI logic
  161. local function navigateAndMine()
  162. local steps = 0
  163. local gpsData = loadGPSData()
  164.  
  165. while true do
  166. local location = getGPSLocation()
  167. local fuel = turtle.getFuelLevel()
  168. updateDashboard("Mining", location, fuel, steps)
  169.  
  170. if fuel == 0 then
  171. log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
  172. break -- Stop movement and break out of the loop to return to main menu
  173. end
  174.  
  175. if location then
  176. gpsData[tostring(os.clock())] = location
  177. saveGPSData(gpsData)
  178. end
  179.  
  180. if tryMoveForward() then
  181. steps = steps + 1
  182. else
  183. tryTurn("right")
  184. end
  185.  
  186. sleep(0.5)
  187. end
  188. end
  189.  
  190. local function exploreAndMap()
  191. local gpsData = loadGPSData()
  192. local steps = 0
  193.  
  194. while true do
  195. local location = getGPSLocation()
  196. local fuel = turtle.getFuelLevel()
  197. updateDashboard("Exploring", location, fuel, steps)
  198.  
  199. if fuel == 0 then
  200. log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
  201. break -- Stop movement and break out of the loop to return to main menu
  202. end
  203.  
  204. if location then
  205. gpsData[tostring(os.clock())] = location
  206. saveGPSData(gpsData)
  207. end
  208.  
  209. if not tryMoveForward() then
  210. tryTurn("right")
  211. end
  212.  
  213. steps = steps + 1
  214. sleep(0.5)
  215. end
  216. end
  217.  
  218. -- Settings Page
  219. local function settingsPage()
  220. local settings = loadSettings()
  221. local gpsData = loadGPSData()
  222. term.clear()
  223. term.setCursorPos(1, 1)
  224. print("Doggy OS Turtle - Settings")
  225. print("---------------------------")
  226. print("1. Set Password")
  227. print("2. Remove Password")
  228. print("3. Clear GPS Data")
  229. print("4. View GPS Data")
  230. print("5. Enable/Disable GPS")
  231. print("6. Run Update Script")
  232. print("7. Back to Main Menu")
  233. print("---------------------------")
  234.  
  235. local option = read()
  236.  
  237. if option == "1" then
  238. print("Enter new password:")
  239. local newPassword = read("*")
  240. settings.password = newPassword
  241. saveSettings(settings)
  242. log("Password set.")
  243. elseif option == "2" then
  244. settings.password = nil
  245. saveSettings(settings)
  246. log("Password removed.")
  247. elseif option == "3" then
  248. fs.delete(gpsFile)
  249. log("GPS data cleared.")
  250. elseif option == "4" then
  251. print("GPS Data:")
  252. if next(gpsData) == nil then
  253. print("No GPS data available.")
  254. else
  255. for timestamp, location in pairs(gpsData) do
  256. print(string.format("Time: %.2f | X: %.2f | Y: %.2f | Z: %.2f", tonumber(timestamp), location.x, location.y, location.z))
  257. end
  258. end
  259. print("Press Enter to go back...")
  260. read()
  261. elseif option == "5" then
  262. -- Toggle GPS
  263. settings.gpsEnabled = not settings.gpsEnabled
  264. if settings.gpsEnabled then
  265. log("GPS is now enabled.")
  266. else
  267. log("GPS is now disabled.")
  268. end
  269. saveSettings(settings)
  270. elseif option == "6" then
  271. -- Run the update script from Pastebin
  272. log("Running update script from Pastebin...")
  273. local pastebinURL = "https://pastebin.com/raw/FQZUNr1j"
  274. local response = http.get(pastebinURL)
  275. if response then
  276. local updateScript = response.readAll()
  277. loadstring(updateScript)()
  278. log("Update completed successfully.")
  279. else
  280. log("Failed to download update script.")
  281. end
  282. elseif option == "7" then
  283. return
  284. else
  285. log("Invalid option!")
  286. end
  287.  
  288. settingsPage() -- Recursively call to allow more changes
  289. end
  290.  
  291. -- Password Protection
  292. local function passwordProtection()
  293. local settings = loadSettings()
  294.  
  295. if settings.password then
  296. os.pullEvent = os.pullEventRaw
  297. term.clear()
  298. term.setCursorPos(1, 1)
  299. print("Enter password to unlock:")
  300. local input = read("*")
  301.  
  302. if input == settings.password then
  303. return true
  304. else
  305. log("Incorrect password!")
  306. sleep(1)
  307. os.reboot()
  308. return false
  309. end
  310. end
  311.  
  312. return true
  313. end
  314.  
  315. -- Main program
  316. local function main()
  317. while true do
  318. if not passwordProtection() then
  319. return
  320. end
  321.  
  322. updateDashboard("Idle", nil, turtle.getFuelLevel(), 0)
  323. print("Commands: mine, explore, refuel, settings, stop")
  324. local command = read()
  325.  
  326. if command == "mine" then
  327. navigateAndMine()
  328. elseif command == "explore" then
  329. exploreAndMap()
  330. elseif command == "refuel" then
  331. refuel()
  332. elseif command == "settings" then
  333. settingsPage()
  334. elseif command == "stop" then
  335. log("Stopping the Turtle.")
  336. os.shutdown()
  337. else
  338. log("Invalid command.")
  339. end
  340. end
  341. end
  342.  
  343. -- Start the main program
  344. main()
  345.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement