Siirtan05

strip 3

May 10th, 2025
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.94 KB | Gaming | 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 (GLOBAL)
  14. function turnLeft()
  15.     turtle.turnLeft()
  16.     dir = (dir - 1) % 4
  17. end
  18.  
  19. function turnRight()
  20.     turtle.turnRight()
  21.     dir = (dir + 1) % 4
  22. end
  23.  
  24. function faceDirection(targetDir)
  25.     while dir ~= targetDir do
  26.         turnRight()
  27.     end
  28. end
  29.  
  30. 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. function moveUp()
  48.     while not turtle.up() do turtle.digUp(); sleep(0.2) end
  49.     pos.y = pos.y + 1
  50. end
  51.  
  52. function moveDown()
  53.     while not turtle.down() do turtle.digDown(); sleep(0.2) end
  54.     pos.y = pos.y - 1
  55. end
  56.  
  57. function moveBack()
  58.     while not turtle.back() do
  59.         turtle.turnLeft()
  60.         turtle.turnLeft()
  61.         turtle.dig()
  62.         turtle.turnLeft()
  63.         turtle.turnLeft()
  64.         sleep(0.2)
  65.     end
  66.     if dir == 0 then pos.z = pos.z + 1
  67.     elseif dir == 1 then pos.x = pos.x - 1
  68.     elseif dir == 2 then pos.z = pos.z - 1
  69.     elseif dir == 3 then pos.x = pos.x + 1 end
  70. end
  71.  
  72. -- Refuel
  73. local 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. -- Keep only ores
  87. local function keepItem(name)
  88.     local patterns = {"cobblestone", "gravel", "flint", "marble", "dirt", "apatite", "thaumcraft:nugget"}
  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. local function savePosition()
  116.     resumePoint = {x = pos.x, y = pos.y, z = pos.z, dir = dir}
  117. end
  118.  
  119. local function resumePosition()
  120.     if not resumePoint then return end
  121.  
  122.     if pos.x < resumePoint.x then faceDirection(1)
  123.     elseif pos.x > resumePoint.x then faceDirection(3) end
  124.     while pos.x ~= resumePoint.x do moveForward() end
  125.  
  126.     if pos.z < resumePoint.z then faceDirection(2)
  127.     elseif pos.z > resumePoint.z then faceDirection(0) end
  128.     while pos.z ~= resumePoint.z do moveForward() end
  129.  
  130.     while pos.y < resumePoint.y do moveUp() end
  131.     while pos.y > resumePoint.y do moveDown() end
  132.  
  133.     faceDirection(resumePoint.dir)
  134. end
  135.  
  136. -- Ore detection
  137. local function isOre(name)
  138.     local patterns = {"ore", "Ore", "ic2", "thermal", "gregtech", "forestry", "redpower"}
  139.     for _, pattern in ipairs(patterns) do
  140.         if string.find(name, pattern) then return true end
  141.     end
  142.     return false
  143. end
  144.  
  145. local function inspectBlock(inspectFunc)
  146.     local success, data = inspectFunc()
  147.     return success and data.name and isOre(data.name)
  148. end
  149.  
  150. local function mineOreVein(depth)
  151.     if depth > maxOreSearchDepth then return end
  152.  
  153.     local function tryMine(dirFunc, moveFunc, returnFunc)
  154.         if dirFunc() then
  155.             moveFunc()
  156.             mineOreVein(depth + 1)
  157.             returnFunc()
  158.         end
  159.     end
  160.  
  161.     tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
  162.  
  163.     turnLeft()
  164.     tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
  165.     turnRight()
  166.     turnRight()
  167.     tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
  168.     turnLeft()
  169.  
  170.     tryMine(function() return inspectBlock(turtle.inspectUp) end, moveUp, moveDown)
  171.     tryMine(function() return inspectBlock(turtle.inspectDown) end, moveDown, moveUp)
  172. end
  173.  
  174. -- Branch mining
  175. local function mineBranch()
  176.     for i = 1, branchLength do
  177.         moveForward()
  178.         mineOreVein(0)
  179.     end
  180.     turnLeft()
  181.     turnLeft()
  182.     for i = 1, branchLength do
  183.         moveForward()
  184.     end
  185.     if isInventoryFull() then
  186.         savePosition()
  187.         returnToStart()
  188.         dropAllItems()
  189.         resumePosition()
  190.     end
  191.     cleanInventory()
  192.     turnLeft()
  193.     turnLeft()
  194. end
  195.  
  196. -- Main strip mine
  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.")
Advertisement
Add Comment
Please, Sign In to add comment