Siirtan05

Strip 5

Aug 24th, 2025 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 KB | None | 0 0
  1. -- Strip mining turtle program with save-state, vein mining, and inventory management
  2. -- Fixed: robust vein mining (stack-based) and branch resumption
  3.  
  4. -- CONFIGURATION
  5. local MAIN_LENGTH = 1500
  6. local BRANCH_EVERY = 3
  7. local BRANCH_LENGTH = 5
  8.  
  9. local ORE_WHITELIST = {
  10.   ["minecraft:coal_ore"] = true,
  11.   ["minecraft:iron_ore"] = true,
  12.   ["minecraft:gold_ore"] = true,
  13.   ["minecraft:redstone_ore"] = true,
  14.   ["minecraft:lapis_ore"] = true,
  15.   ["minecraft:diamond_ore"] = true,
  16.   ["minecraft:emerald_ore"] = true,
  17. }
  18.  
  19. local DROP_BLACKLIST = {
  20.   ["minecraft:cobblestone"] = true,
  21.   ["minecraft:dirt"] = true,
  22. }
  23.  
  24. -- STATE
  25. local state = {
  26.   x=0,y=0,z=0,
  27.   facing=0, -- 0=north,1=east,2=south,3=west
  28.   mode="main",
  29.   mainProgress=0,
  30.   branchSide="left",
  31.   branchProgress=0,
  32.   nextBranchAt=BRANCH_EVERY,
  33.   branchMainDir=0,
  34.   -- Branch resumption fields
  35.   branchReturning=false,
  36.   branchBackLeft=0,
  37.   -- Vein mining resumption stack: holds reverse-move directions to backtrack (e.g., "b","l","r","u","d")
  38.   veinStack={},
  39. }
  40.  
  41. -- HELPERS
  42. local function saveState()
  43.   local f = fs.open("resume.dat","w")
  44.   f.write(textutils.serialize(state))
  45.   f.close()
  46. end
  47.  
  48. local function loadState()
  49.   if fs.exists("resume.dat") then
  50.     local f = fs.open("resume.dat","r")
  51.     state = textutils.unserialize(f.readAll())
  52.     f.close()
  53.   end
  54. end
  55.  
  56. local function turnLeft() turtle.turnLeft(); state.facing=(state.facing+3)%4 end
  57. local function turnRight() turtle.turnRight(); state.facing=(state.facing+1)%4 end
  58. local function face(dir)
  59.   while state.facing~=dir do turnRight() end
  60. end
  61. local function forward()
  62.   while not turtle.forward() do turtle.dig(); sleep(0.5) end
  63.   if state.facing==0 then state.z=state.z-1
  64.   elseif state.facing==1 then state.x=state.x+1
  65.   elseif state.facing==2 then state.z=state.z+1
  66.   elseif state.facing==3 then state.x=state.x-1 end
  67. end
  68.  
  69. -- ORE DETECTION / VEIN MINING (resumable)
  70. local function inspectDir(direction)
  71.   if direction == "forward" then return turtle.inspect()
  72.   elseif direction == "up" then return turtle.inspectUp()
  73.   elseif direction == "down" then return turtle.inspectDown()
  74.   elseif direction == "left" then
  75.     turtle.turnLeft()
  76.     local ok, data = turtle.inspect()
  77.     turtle.turnRight()
  78.     return ok, data
  79.   elseif direction == "right" then
  80.     turtle.turnRight()
  81.     local ok, data = turtle.inspect()
  82.     turtle.turnLeft()
  83.     return ok, data
  84.   elseif direction == "back" then
  85.     turtle.turnLeft(); turtle.turnLeft()
  86.     local ok, data = turtle.inspect()
  87.     turtle.turnRight(); turtle.turnRight()
  88.     return ok, data
  89.   end
  90. end
  91.  
  92. local function moveDir(dir)
  93.   if dir=="f" then turtle.dig(); forward()
  94.   elseif dir=="b" then turtle.back(); if state.facing==0 then state.z=state.z+1 elseif state.facing==1 then state.x=state.x-1 elseif state.facing==2 then state.z=state.z-1 elseif state.facing==3 then state.x=state.x+1 end
  95.   elseif dir=="l" then turtle.turnLeft(); turtle.dig(); forward(); turtle.turnRight()
  96.   elseif dir=="r" then turtle.turnRight(); turtle.dig(); forward(); turtle.turnLeft()
  97.   elseif dir=="u" then while not turtle.up() do turtle.digUp(); sleep(0.2) end; state.y=state.y+1
  98.   elseif dir=="d" then while not turtle.down() do turtle.digDown(); sleep(0.2) end; state.y=state.y-1 end
  99. end
  100.  
  101. local function reverseDir(dir)
  102.   if dir=="f" then return "b" elseif dir=="b" then return "f" elseif dir=="l" then return "r" elseif dir=="r" then return "l" elseif dir=="u" then return "d" else return "u" end
  103. end
  104.  
  105. local function tryFindOreDir()
  106.   -- Prefer lateral/forward first; we intentionally skip checking "back" to avoid thrashing
  107.   for _,probe in ipairs({"forward","left","right","up","down"}) do
  108.     local ok, data = inspectDir(probe)
  109.     if ok and data and ORE_WHITELIST[data.name] then
  110.       if probe=="forward" then return "f"
  111.       elseif probe=="left" then return "l"
  112.       elseif probe=="right" then return "r"
  113.       elseif probe=="up" then return "u"
  114.       elseif probe=="down" then return "d" end
  115.     end
  116.   end
  117.   return nil
  118. end
  119.  
  120. local function continueVein()
  121.   state.mode="vein"; saveState()
  122.   while true do
  123.     local dir = tryFindOreDir()
  124.     if dir then
  125.       -- Move into the ore block and push the reverse move for backtracking
  126.       moveDir(dir)
  127.       table.insert(state.veinStack, reverseDir(dir))
  128.       saveState()
  129.     else
  130.       -- No adjacent ore; backtrack one step if we have a path
  131.       if #state.veinStack == 0 then
  132.         -- Vein complete
  133.         state.mode = "main"; saveState()
  134.         return
  135.       end
  136.       local back = table.remove(state.veinStack)
  137.       moveDir(back)
  138.       saveState()
  139.     end
  140.   end
  141. end
  142.  
  143. local function maybeVeinMine()
  144.   if tryFindOreDir() then
  145.     -- Start/resume a vein
  146.     if state.mode ~= "vein" then state.veinStack = {} end
  147.     continueVein()
  148.   end
  149. end
  150.   end
  151. end
  152.  
  153. -- INVENTORY
  154. local function dropInventory()
  155.   for slot=2,16 do
  156.     turtle.select(slot)
  157.     turtle.dropDown()
  158.   end
  159.   turtle.select(1)
  160. end
  161.  
  162. local function isInventoryFull()
  163.   for slot=2,16 do
  164.     if turtle.getItemCount(slot)==0 then return false end
  165.   end
  166.   return true
  167. end
  168.  
  169. -- BRANCHING
  170. local function resumeBranch()
  171.   local branchDir = (state.branchSide=="left") and (state.branchMainDir+3)%4 or (state.branchMainDir+1)%4
  172.   face(branchDir)
  173.   for i=state.branchProgress+1,BRANCH_LENGTH do
  174.     turtle.dig(); forward()
  175.     state.branchProgress=i
  176.     maybeVeinMine()
  177.     saveState()
  178.   end
  179.   -- return to tunnel
  180.   face((branchDir+2)%4)
  181.   for i=1,BRANCH_LENGTH do forward() end
  182.   face(state.branchMainDir)
  183.   state.mode="main"
  184.   saveState()
  185. end
  186.  
  187. local function doOneBranch(side)
  188.   -- Initialize branch state if starting fresh
  189.   if state.mode ~= "branch" then
  190.     state.mode="branch"
  191.     state.branchSide=side
  192.     state.branchProgress=state.branchProgress or 0
  193.     state.branchMainDir=state.facing
  194.     state.branchReturning=false
  195.     state.branchBackLeft=BRANCH_LENGTH
  196.     saveState()
  197.   end
  198.  
  199.   local branchDir = (state.branchSide=="left") and (state.branchMainDir+3)%4 or (state.branchMainDir+1)%4
  200.  
  201.   -- Forward leg
  202.   if not state.branchReturning then
  203.     face(branchDir)
  204.     for i=state.branchProgress+1,BRANCH_LENGTH do
  205.       turtle.dig(); forward()
  206.       state.branchProgress=i
  207.       saveState()
  208.       maybeVeinMine()
  209.     end
  210.     -- Switch to return leg
  211.     state.branchReturning=true
  212.     state.branchBackLeft=BRANCH_LENGTH
  213.     saveState()
  214.   end
  215.  
  216.   -- Return leg
  217.   face((branchDir+2)%4)
  218.   while state.branchBackLeft>0 do
  219.     forward()
  220.     state.branchBackLeft=state.branchBackLeft-1
  221.     saveState()
  222.   end
  223.  
  224.   -- Finish: restore facing and clear branch state
  225.   face(state.branchMainDir)
  226.   state.mode="main"
  227.   state.branchProgress=0
  228.   state.branchBackLeft=0
  229.   state.branchReturning=false
  230.   saveState()
  231. end
  232.  
  233. -- MAIN MINING
  234. local function mineMain()
  235.   state.mode="main"
  236.   while state.mainProgress<MAIN_LENGTH do
  237.     turtle.dig(); forward()
  238.     state.mainProgress=state.mainProgress+1
  239.     saveState()
  240.     maybeVeinMine()
  241.     if state.mainProgress==state.nextBranchAt then
  242.       -- Left branch (resumable)
  243.       if state.mode=="main" then doOneBranch("left") end
  244.       -- Right branch (resumable)
  245.       if state.mode=="main" then doOneBranch("right") end
  246.       state.nextBranchAt=state.nextBranchAt+BRANCH_EVERY
  247.       if isInventoryFull() then
  248.         local stepsBack=state.mainProgress
  249.         face((state.facing+2)%4)
  250.         for i=1,stepsBack do forward() end
  251.         dropInventory()
  252.         -- go forward again to resume
  253.         face((state.facing+2)%4)
  254.         for i=1,stepsBack do forward() end
  255.         face((state.facing+2)%4)
  256.       end
  257.     end
  258.   end
  259. end
  260.         dropInventory()
  261.         face((state.facing+2)%4)
  262.         for i=1,stepsBack do forward() end
  263.         face((state.facing+2)%4)
  264.       end
  265.     end
  266.     saveState()
  267.   end
  268. end
  269.  
  270. -- MAIN
  271. local function main()
  272.   loadState()
  273.   -- If we crashed mid-vein or mid-branch, resume that first
  274.   if state.mode=="vein" then
  275.     continueVein()
  276.   end
  277.   if state.mode=="branch" then
  278.     doOneBranch(state.branchSide)
  279.   end
  280.   -- Continue main plan
  281.   mineMain()
  282.   print("Mining complete.")
  283. end
  284.   print("Mining complete.")
  285. end
  286.  
  287. main()
  288.  
Advertisement
Add Comment
Please, Sign In to add comment