Advertisement
Siirtan05

Strip 4

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