Advertisement
Siirtan05

strip_mine.lua

May 9th, 2025 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. -- Configuration
  2. local mainTunnelLength = 400
  3. local branchLength = 10
  4. local branchSpacing = 5
  5. local fuelLimit = 100
  6. local maxOreSearchDepth = 30
  7.  
  8. -- Position tracking
  9. local pos = {x=0, y=0, z=0}
  10. local dir = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
  11.  
  12. -- Movement helpers
  13. local function turnLeft()
  14. turtle.turnLeft()
  15. dir = (dir - 1) % 4
  16. end
  17.  
  18. local function turnRight()
  19. turtle.turnRight()
  20. dir = (dir + 1) % 4
  21. end
  22.  
  23. local function faceDirection(targetDir)
  24. while dir ~= targetDir do
  25. turnRight()
  26. end
  27. end
  28.  
  29. -- Refuel
  30. local function refuelIfNeeded()
  31. if turtle.getFuelLevel() < fuelLimit then
  32. for i = 1, 16 do
  33. turtle.select(i)
  34. if turtle.refuel(1) then
  35. print("Refueled with slot " .. i)
  36. break
  37. end
  38. end
  39. turtle.select(1)
  40. end
  41. end
  42.  
  43. -- Dig and move
  44. local function dig()
  45. while turtle.detect() do
  46. turtle.dig()
  47. sleep(0.2)
  48. end
  49. end
  50.  
  51. -- Keep only ores
  52. local function keepItem(name)
  53. local patterns = {"cobblestone", "gravel", "flint", "marble", "dirt", }
  54. for _, pattern in ipairs(patterns) do
  55. if string.find(name, pattern) then return false end
  56. end
  57. return true
  58. end
  59.  
  60. local function cleanInventory()
  61. for i = 1, 16 do
  62. turtle.select(i)
  63. local item = turtle.getItemDetail()
  64. if item and not keepItem(item.name) then
  65. turtle.drop()
  66. end
  67. end
  68. turtle.select(1)
  69. end
  70.  
  71. local function moveForward()
  72. refuelIfNeeded()
  73. dig()
  74. while not turtle.forward() do sleep(0.2) end
  75.  
  76. if dir == 0 then pos.z = pos.z - 1
  77. elseif dir == 1 then pos.x = pos.x + 1
  78. elseif dir == 2 then pos.z = pos.z + 1
  79. elseif dir == 3 then pos.x = pos.x - 1 end
  80. end
  81.  
  82. local function moveUp()
  83. while not turtle.up() do turtle.digUp(); sleep(0.2) end
  84. pos.y = pos.y + 1
  85. end
  86.  
  87. local function moveDown()
  88. while not turtle.down() do turtle.digDown(); sleep(0.2) end
  89. pos.y = pos.y - 1
  90. end
  91.  
  92. local function moveBack()
  93. while not turtle.back() do
  94. turtle.turnLeft()
  95. turtle.turnLeft()
  96. dig()
  97. turtle.turnLeft()
  98. turtle.turnLeft()
  99. sleep(0.2)
  100. end
  101. if dir == 0 then pos.z = pos.z + 1
  102. elseif dir == 1 then pos.x = pos.x - 1
  103. elseif dir == 2 then pos.z = pos.z - 1
  104. elseif dir == 3 then pos.x = pos.x + 1 end
  105. end
  106.  
  107. -- Ore detection
  108. local function isOre(name)
  109. local patterns = {"ore", "Ore", "ic2", "thermal", "gregtech", "forestry", "redpower"}
  110. for _, pattern in ipairs(patterns) do
  111. if string.find(name, pattern) then return true end
  112. end
  113. return false
  114. end
  115.  
  116. local function inspectBlock(inspectFunc)
  117. local success, data = inspectFunc()
  118. return success and data.name and isOre(data.name)
  119. end
  120.  
  121. local function mineOreVein(depth)
  122. if depth > maxOreSearchDepth then return end
  123.  
  124. local function tryMine(dirFunc, moveFunc, returnFunc)
  125. if dirFunc() then
  126. moveFunc()
  127. mineOreVein(depth + 1)
  128. returnFunc()
  129. end
  130. end
  131.  
  132. tryMine(function() return inspectBlock(turtle.inspect) end,
  133. function() moveForward() end,
  134. function() moveBack() end)
  135.  
  136. turnLeft()
  137. tryMine(function() return inspectBlock(turtle.inspect) end,
  138. function() moveForward() end,
  139. function() moveBack() end)
  140. turnRight()
  141. turnRight()
  142. tryMine(function() return inspectBlock(turtle.inspect) end,
  143. function() moveForward() end,
  144. function() moveBack() end)
  145. turnLeft()
  146.  
  147. tryMine(function() return inspectBlock(turtle.inspectUp) end, moveUp, moveDown)
  148. tryMine(function() return inspectBlock(turtle.inspectDown) end, moveDown, moveUp)
  149. end
  150.  
  151. -- Branch logic
  152. local function mineBranch()
  153. for i = 1, branchLength do
  154. moveForward()
  155. mineOreVein(0)
  156. end
  157. turnLeft()
  158. turnLeft()
  159. for i = 1, branchLength do
  160. moveForward()
  161. end
  162. cleanInventory()
  163. turnLeft()
  164. turnLeft()
  165. end
  166.  
  167. -- Main mining logic
  168. local function stripMine()
  169. for i = 0, mainTunnelLength - 1 do
  170. moveForward()
  171. mineOreVein(0)
  172. if i % branchSpacing == 0 then
  173. turnLeft()
  174. mineBranch()
  175. turnRight()
  176. turnRight()
  177. mineBranch()
  178. turnLeft()
  179. end
  180. end
  181. end
  182.  
  183. -- Return to start and face forward
  184. local function returnToStart()
  185. print("Returning to start...")
  186. -- Move to X = 0
  187. if pos.x > 0 then faceDirection(3)
  188. elseif pos.x < 0 then faceDirection(1) end
  189. while pos.x ~= 0 do moveForward() end
  190.  
  191. -- Move to Z = 0
  192. if pos.z > 0 then faceDirection(0)
  193. elseif pos.z < 0 then faceDirection(2) end
  194. while pos.z ~= 0 do moveForward() end
  195.  
  196. -- Move to Y = 0
  197. while pos.y > 0 do moveDown() end
  198. while pos.y < 0 do moveUp() end
  199.  
  200. faceDirection(0)
  201. end
  202.  
  203. -- Drop off items
  204. local function dropAllItems()
  205. print("Dropping off items...")
  206. for i = 1, 16 do
  207. turtle.select(i)
  208. turtle.drop()
  209. end
  210. turtle.select(1)
  211. end
  212.  
  213. -- Main
  214. print("Starting enhanced strip mining...")
  215. stripMine()
  216. returnToStart()
  217. dropAllItems()
  218. print("Mining complete. Turtle has returned.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement