Lorc520

myminer_v1

Apr 24th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. -- USER INPUT
  2. print("Enter length:")
  3. local length = tonumber(read())
  4. print("Enter width:")
  5. local width = tonumber(read())
  6. print("Enter depth:")
  7. local depth = tonumber(read())
  8.  
  9. local fuelSlot = 16
  10. local bucketSlot = 1 -- The slot for the bucket
  11. local stateFile = "state.txt" -- File to store the turtle's state
  12.  
  13. -- STATE
  14. local x, y, z = 0, 0, 0 -- relative coordinates
  15. local dir = 0 -- 0=N, 1=E, 2=S, 3=W
  16. local currentDepth = 0
  17. local isMining = true
  18.  
  19. -- === MOVEMENT HELPERS ===
  20. function refuel()
  21. if turtle.getFuelLevel() < 20 then
  22. -- First check if we have lava in the bucket
  23. turtle.select(bucketSlot)
  24. if turtle.getItemCount(bucketSlot) > 0 then
  25. turtle.place() -- Place lava and fuel from it
  26. if turtle.refuel(1) then
  27. print("Refueled using lava.")
  28. else
  29. print("Failed to refuel using lava.")
  30. end
  31. else
  32. turtle.select(fuelSlot)
  33. if not turtle.refuel(1) then
  34. print("Out of fuel, please add more!")
  35. else
  36. print("Refueled successfully.")
  37. end
  38. end
  39. end
  40. end
  41.  
  42. function tryDig()
  43. while turtle.detect() do
  44. turtle.dig()
  45. sleep(0.4)
  46. end
  47. end
  48.  
  49. function tryDigDown()
  50. while turtle.detectDown() do
  51. turtle.digDown()
  52. sleep(0.4)
  53. end
  54. end
  55.  
  56. function tryDigUp()
  57. while turtle.detectUp() do
  58. turtle.digUp()
  59. sleep(0.4)
  60. end
  61. end
  62.  
  63. function forward()
  64. refuel()
  65. tryDig()
  66. while not turtle.forward() do
  67. sleep(0.2)
  68. end
  69. if dir == 0 then z = z + 1
  70. elseif dir == 1 then x = x + 1
  71. elseif dir == 2 then z = z - 1
  72. elseif dir == 3 then x = x - 1 end
  73. end
  74.  
  75. function back()
  76. refuel()
  77. while not turtle.back() do
  78. sleep(0.2)
  79. end
  80. if dir == 0 then z = z - 1
  81. elseif dir == 1 then x = x - 1
  82. elseif dir == 2 then z = z + 1
  83. elseif dir == 3 then x = x + 1 end
  84. end
  85.  
  86. function down()
  87. refuel()
  88. tryDigDown()
  89. while not turtle.down() do
  90. sleep(0.2)
  91. end
  92. y = y - 1
  93. end
  94.  
  95. function up()
  96. refuel()
  97. tryDigUp()
  98. while not turtle.up() do
  99. sleep(0.2)
  100. end
  101. y = y + 1
  102. end
  103.  
  104. function turnLeft()
  105. turtle.turnLeft()
  106. dir = (dir - 1) % 4
  107. if dir < 0 then dir = dir + 4 end
  108. end
  109.  
  110. function turnRight()
  111. turtle.turnRight()
  112. dir = (dir + 1) % 4
  113. end
  114.  
  115. function face(target)
  116. while dir ~= target do
  117. turnRight()
  118. end
  119. end
  120.  
  121. -- === GO TO COORDINATE ===
  122. function goTo(targetX, targetY, targetZ, targetDir)
  123. while y < targetY do up() end
  124. while y > targetY do down() end
  125.  
  126. if x < targetX then face(1) while x < targetX do forward() end
  127. elseif x > targetX then face(3) while x > targetX do forward() end end
  128.  
  129. if z < targetZ then face(0) while z < targetZ do forward() end
  130. elseif z > targetZ then face(2) while z > targetZ do forward() end end
  131.  
  132. face(targetDir or 0)
  133. end
  134.  
  135. -- === INVENTORY HANDLING ===
  136. function isInventoryFull()
  137. for i = 1, 15 do
  138. if turtle.getItemCount(i) == 0 then return false end
  139. end
  140. return true
  141. end
  142.  
  143. function dropItems()
  144. local saveX, saveY, saveZ, saveDir = x, y, z, dir
  145. goTo(0, 0, 0, 2)
  146. for i = 1, 15 do
  147. turtle.select(i)
  148. turtle.drop()
  149. end
  150. turtle.select(1)
  151. goTo(saveX, saveY, saveZ, saveDir)
  152. end
  153.  
  154. -- === MINING FUNCTION ===
  155. function mineLayer()
  156. for row = 1, width do
  157. for col = 1, length - 1 do
  158. forward()
  159. if isInventoryFull() then dropItems() end
  160. end
  161. if row < width then
  162. if row % 2 == 1 then turnRight(); forward(); turnRight()
  163. else turnLeft(); forward(); turnLeft() end
  164. end
  165. end
  166. if width % 2 == 1 then face(2) for i = 1, length - 1 do forward() end end
  167. face(3) for i = 1, width - 1 do forward() end
  168. face(0)
  169. end
  170.  
  171. -- === SAVE STATE ===
  172. function saveState()
  173. local file = fs.open(stateFile, "w")
  174. file.writeLine(x)
  175. file.writeLine(y)
  176. file.writeLine(z)
  177. file.writeLine(dir)
  178. file.writeLine(currentDepth)
  179. file.writeLine(isMining and "true" or "false")
  180. file.close()
  181. end
  182.  
  183. -- === LOAD STATE ===
  184. function loadState()
  185. if fs.exists(stateFile) then
  186. local file = fs.open(stateFile, "r")
  187. x = tonumber(file.readLine())
  188. y = tonumber(file.readLine())
  189. z = tonumber(file.readLine())
  190. dir = tonumber(file.readLine())
  191. currentDepth = tonumber(file.readLine())
  192. isMining = file.readLine() == "true"
  193. file.close()
  194. print("Loaded saved state.")
  195. else
  196. print("No saved state found.")
  197. end
  198. end
  199.  
  200. -- === MAIN EXECUTION ===
  201. loadState()
  202.  
  203. print("Starting mining...")
  204.  
  205. if y == 0 then
  206. turtle.digDown()
  207. down()
  208. end
  209.  
  210. for d = currentDepth, depth do
  211. mineLayer()
  212. if d < depth then down() end
  213. currentDepth = d
  214. saveState() -- Save state after each layer
  215. end
  216.  
  217. -- Final return to origin, face chest
  218. goTo(0, 0, 0, 2)
  219. for i = 1, 15 do
  220. turtle.select(i)
  221. turtle.drop()
  222. end
  223. turtle.select(1)
  224. goTo(0, 0, 0, 0)
  225.  
  226. print("Mining complete. Ready to unload into chest.")
  227.  
Advertisement
Add Comment
Please, Sign In to add comment