Siirtan05

Strip Mine 2

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