Lorc520

Quarry:autonomous

Apr 30th, 2025 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. print("Enter length:")
  2. local length = tonumber(read())
  3. print("Enter width:")
  4. local width = tonumber(read())
  5. print("Enter depth:")
  6. local depth = tonumber(read())
  7.  
  8. -- STATE
  9. local x, y, z = 0, 0, 0 -- relative coordinates
  10. local dir = 0 -- 0=N, 1=E, 2=S, 3=W
  11. local bucketSlot = 16 -- Lava bucket slot
  12. local lastFuelPercent = -1
  13.  
  14. -- === BLACKLIST ===
  15. local blacklist = {
  16. ["minecraft:cobbled_deepslate"] = true,
  17. ["minecraft:cobblestone"] = true
  18. }
  19.  
  20. -- === REFUELING SYSTEM ===
  21. function getFuelPercent()
  22. local current = turtle.getFuelLevel()
  23. local max = 20000 -- Assumed max fuel
  24. return current / max
  25. end
  26.  
  27. function tryConsumeFuel()
  28. local percent = getFuelPercent()
  29.  
  30. -- Skip fuel consumption if above 5%
  31. if percent >= 0.05 then return end
  32.  
  33. -- After lava refueling, if fuel is still below 5%, consume from inventory
  34. for i = 1, 15 do
  35. turtle.select(i)
  36. while turtle.getItemCount(i) > 0 do
  37. if turtle.refuel(1) then
  38. print("Consumed fuel from slot " .. i .. ", current fuel level: " .. turtle.getFuelLevel())
  39. else
  40. break
  41. end
  42. end
  43. end
  44. turtle.select(1)
  45. end
  46.  
  47. -- === LAVA COLLECTION FUNCTION ===
  48. function tryCollectLava()
  49. turtle.select(bucketSlot)
  50. local detail = turtle.getItemDetail(bucketSlot)
  51. if detail and detail.name == "minecraft:bucket" then
  52. if turtle.place() then
  53. sleep(0.2)
  54. local newDetail = turtle.getItemDetail(bucketSlot)
  55. if newDetail and newDetail.name == "minecraft:lava_bucket" then
  56. if turtle.refuel(1) then
  57. print("Lava bucket consumed for fuel, current fuel level: " .. turtle.getFuelLevel())
  58. else
  59. print("Lava bucket not consumed.")
  60. end
  61. else
  62. print("No lava source in front.")
  63. end
  64. end
  65. end
  66. turtle.select(1)
  67. end
  68.  
  69. -- === BLACKLIST FILTER ===
  70. function dropBlacklistedItems()
  71. for i = 1, 15 do
  72. turtle.select(i)
  73. local detail = turtle.getItemDetail(i)
  74. if detail and blacklist[detail.name] then
  75. turtle.dropDown()
  76. print("Dropped blacklisted item: " .. detail.name)
  77. end
  78. end
  79. turtle.select(1)
  80. end
  81.  
  82. -- === FUEL PERCENT MONITOR ===
  83. function monitorFuel()
  84. local percent = getFuelPercent()
  85. if lastFuelPercent == -1 then
  86. lastFuelPercent = percent
  87. elseif math.abs(percent - lastFuelPercent) >= 0.05 then
  88. print("Fuel level changed: " .. math.floor(percent * 100) .. "%")
  89. lastFuelPercent = percent
  90. end
  91. end
  92.  
  93. -- === MOVEMENT HELPERS ===
  94. function tryDig() while turtle.detect() do turtle.dig(); sleep(0.4) end end
  95. function tryDigDown() while turtle.detectDown() do turtle.digDown(); sleep(0.4) end end
  96. function tryDigUp() while turtle.detectUp() do turtle.digUp(); sleep(0.4) end end
  97.  
  98. function forward()
  99. tryConsumeFuel()
  100. tryCollectLava()
  101. tryDig()
  102. dropBlacklistedItems()
  103. monitorFuel()
  104. while not turtle.forward() do sleep(0.2) end
  105. if dir == 0 then z = z + 1
  106. elseif dir == 1 then x = x + 1
  107. elseif dir == 2 then z = z - 1
  108. elseif dir == 3 then x = x - 1 end
  109. end
  110.  
  111. function back()
  112. tryConsumeFuel()
  113. tryCollectLava()
  114. tryDig()
  115. dropBlacklistedItems()
  116. monitorFuel()
  117. while not turtle.back() do sleep(0.2) end
  118. if dir == 0 then z = z - 1
  119. elseif dir == 1 then x = x - 1
  120. elseif dir == 2 then z = z + 1
  121. elseif dir == 3 then x = x + 1 end
  122. end
  123.  
  124. function down()
  125. tryConsumeFuel()
  126. tryCollectLava()
  127. tryDigDown()
  128. monitorFuel()
  129. while not turtle.down() do sleep(0.2) end
  130. y = y - 1
  131. end
  132.  
  133. function up()
  134. tryConsumeFuel()
  135. tryCollectLava()
  136. tryDigUp()
  137. monitorFuel()
  138. while not turtle.up() do sleep(0.2) end
  139. y = y + 1
  140. end
  141.  
  142. function turnLeft()
  143. turtle.turnLeft()
  144. dir = (dir - 1) % 4
  145. if dir < 0 then dir = dir + 4 end
  146. end
  147.  
  148. function turnRight()
  149. turtle.turnRight()
  150. dir = (dir + 1) % 4
  151. end
  152.  
  153. function face(target)
  154. while dir ~= target do turnRight() end
  155. end
  156.  
  157. -- === GO TO COORDINATE ===
  158. function goTo(targetX, targetY, targetZ, targetDir)
  159. while y < targetY do up() end
  160. while y > targetY do down() end
  161.  
  162. if x < targetX then face(1) while x < targetX do forward() end
  163. elseif x > targetX then face(3) while x > targetX do forward() end end
  164.  
  165. if z < targetZ then face(0) while z < targetZ do forward() end
  166. elseif z > targetZ then face(2) while z > targetZ do forward() end end
  167.  
  168. face(targetDir or 0)
  169. end
  170.  
  171. -- === INVENTORY HANDLING ===
  172. function isInventoryFull()
  173. for i = 1, 15 do
  174. if turtle.getItemCount(i) == 0 then return false end
  175. end
  176. return true
  177. end
  178.  
  179. function dropItems()
  180. local saveX, saveY, saveZ, saveDir = x, y, z, dir
  181. goTo(0, 0, 0, 2)
  182. for i = 1, 15 do
  183. turtle.select(i)
  184. turtle.drop()
  185. end
  186. turtle.select(1)
  187. goTo(saveX, saveY, saveZ, saveDir)
  188. end
  189.  
  190. -- === MINING FUNCTION ===
  191. function mineLayer()
  192. for row = 1, width do
  193. for col = 1, length - 1 do
  194. forward()
  195. if isInventoryFull() then dropItems() end
  196. end
  197. if row < width then
  198. if row % 2 == 1 then turnRight(); forward(); turnRight()
  199. else turnLeft(); forward(); turnLeft() end
  200. end
  201. end
  202. if width % 2 == 1 then face(2) for i = 1, length - 1 do forward() end end
  203. face(3) for i = 1, width - 1 do forward() end
  204. face(0)
  205. dropBlacklistedItems()
  206. end
  207.  
  208. -- === MAIN EXECUTION ===
  209. print("Starting mining...")
  210. turtle.digDown()
  211. down()
  212.  
  213. for d = 1, depth do
  214. mineLayer()
  215. if d < depth then down() end
  216. end
  217.  
  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. print("Mining complete. Ready to unload into chest.")
Add Comment
Please, Sign In to add comment