NindyBun

VerticalMiner

Nov 17th, 2021 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. local curr_depth = 0
  2. local radius = 0
  3.  
  4. local ORE = {
  5.     ["minecraft:cobblestone"] = true
  6. }
  7.  
  8. local DIRECTION = {
  9.     Front = 0,
  10.     Right = 1,
  11.     Back = 2,
  12.     Left = 3,
  13.     Up = 4,
  14.     Down = 5
  15. }
  16.  
  17. function markVec(mineDepth, direction)
  18.     return {mineDepth=mineDepth, direction=direction}
  19. end
  20.  
  21. function isOre(direction)
  22.     local hasBlock, blockInfo
  23.     if (direction == DIRECTION.Up) then
  24.         hasBlock, blockInfo = turtle.inspectUp()
  25.     elseif (direction == DIRECTION.Down) then
  26.         hasBlock, blockInfo = turtle.inspectDown()
  27.     else
  28.         hasBlock, blockInfo = turtle.inspect()
  29.     end
  30.     return hasBlock and ORE[blockInfo.name]
  31. end
  32.  
  33. function reverseTurns(mineDepth, turnList)
  34.     while (#turnList > 0 and turnList[#turnList].mineDepth == mineDepth) do
  35.         local vec = table.remove(turnList)
  36.         if (vec.direction == DIRECTION.Right) then
  37.             turtle.turnLeft()
  38.         elseif (vec.direction == DIRECTION.Left) then
  39.             turtle.turnRight()
  40.         end
  41.     end
  42. end
  43.  
  44. function correctTurn(direction, mineDepth, turnList)
  45.     if (direction == DIRECTION.Left) then
  46.         turtle.turnLeft()
  47.         table.insert(turnList, markVec(mineDepth, DIRECTION.Left))
  48.     elseif (direction == DIRECTION.Right) then
  49.         turtle.turnRight()
  50.         table.insert(turnList, markVec(mineDepth, DIRECTION.Right))
  51.     elseif (direction == DIRECTION.Back) then
  52.         turtle.turnRight()
  53.         turtle.turnRight()
  54.         table.insert(turnList, markVec(mineDepth, DIRECTION.Right))
  55.         table.insert(turnList, markVec(mineDepth, DIRECTION.Right))
  56.     end
  57. end
  58.  
  59. function reverseMoves(mineDepth, origin, moveList, turnList)
  60.     while (mineDepth ~= origin) do
  61.         while (#moveList > 0 and moveList[#moveList].mineDepth == mineDepth - 1) do
  62.             local vec = table.remove(moveList)
  63.             if (vec.direction == DIRECTION.Front) then
  64.                 turtle.back()
  65.             elseif (vec.direction == DIRECTION.Up) then
  66.                 turtle.down()
  67.             elseif (vec.direction == DIRECTION.Down) then
  68.                 turtle.up()
  69.             end
  70.         end
  71.         mineDepth = mineDepth - 1
  72.         reverseTurns(mineDepth, turnList)
  73.     end
  74. end
  75.  
  76. function mine(direction, mineDepth, moveList)
  77.     if (direction == DIRECTION.Up) then
  78.         while (turtle.digUp()) do end
  79.         turtle.up()
  80.         table.insert(moveList, markVec(mineDepth, DIRECTION.Up))
  81.       elseif (direction == DIRECTION.Down) then
  82.         while (turtle.digDown()) do end
  83.         turtle.down()
  84.         table.insert(moveList, markVec(mineDepth, DIRECTION.Down))
  85.       else
  86.         while (turtle.dig()) do end
  87.         turtle.forward()
  88.         table.insert(moveList, markVec(mineDepth, DIRECTION.Front))
  89.       end
  90. end
  91.  
  92. function veinMine(direction)
  93.     local mineList = {} --Stores list of ores
  94.     local moveList = {} --Keeps track of movement
  95.     local turnList = {} --Keeps track of turns
  96.     local mineDepth = 0 --Keep track of # of mines in a vein
  97.  
  98.     --Adds current position into list for mining
  99.     table.insert(mineList, markVec(mineDepth, direction))
  100.  
  101.     while (#mineList > 0) do
  102.         local vec = table.remove(mineList)
  103.         reverseMoves(mineDepth, vec.mineDepth, moveList, turnList)
  104.         mineDepth = vec.mineDepth
  105.         correctTurn(vec.direction, mineDepth, turnList)
  106.  
  107.         if (isOre(vec.direction)) then
  108.             mine(vec.direction, mineDepth, moveList)
  109.             mineDepth = mineDepth + 1
  110.  
  111.             if (isOre(DIRECTION.Front)) then
  112.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Front))
  113.             end
  114.  
  115.             if (isOre(DIRECTION.Up)) then
  116.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Up))
  117.             end
  118.             if (isOre(DIRECTION.Down)) then
  119.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Down))
  120.             end
  121.             turtle.turnRight()
  122.             if (isOre(DIRECTION.Front)) then
  123.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Right))
  124.             end
  125.             turtle.turnRight()
  126.             if (isOre(DIRECTION.Front)) then
  127.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Back))
  128.             end
  129.             turtle.turnRight()
  130.             if (isOre(DIRECTION.Front)) then
  131.                 table.insert(mineList, markVec(mineDepth, DIRECTION.Left))
  132.             end
  133.             turtle.turnRight()
  134.         else
  135.             reverseTurns(mineDepth, turnList)
  136.         end
  137.     end
  138.  
  139.     --Go back to before vein mining
  140.     reverseMoves(mineDepth, 0, moveList, turnList)
  141. end
  142.  
  143.  
  144. function triggerVeinMine(direction)
  145.     if (isOre(direction)) then
  146.         veinMine(direction)
  147.     end
  148. end
  149.  
  150. function inspectBlock()
  151.     triggerVeinMine(DIRECTION.Front)
  152.  
  153.     turtle.turnRight()
  154.     triggerVeinMine(DIRECTION.Front)
  155.  
  156.     turtle.turnRight()
  157.     triggerVeinMine(DIRECTION.Front)
  158.  
  159.     turtle.turnRight()
  160.     triggerVeinMine(DIRECTION.Front)
  161.  
  162.     turtle.turnRight()
  163. end
  164.  
  165. function mineDownUp()
  166.     --Going down
  167.     while (turtle.digDown()) do end
  168.     while (turtle.down()) do
  169.         curr_depth = curr_depth + 1
  170.         inspectBlock()
  171.         while (turtle.digDown()) do end
  172.     end
  173.     --Going up
  174.     while (curr_depth > 0) do
  175.         while (turtle.digUp()) do end
  176.         turtle.up()
  177.         curr_depth = curr_depth - 1
  178.     end
  179. end
  180.  
  181. function advance()
  182.     --Face center after advancing to next radius
  183.     for o = 1, 4 do
  184.         while (turtle.dig()) do end
  185.         turtle.forward()
  186.     end
  187.     turtle.turnRight()
  188.     turtle.turnRight()
  189.     radius = radius + 2
  190. end
  191.  
  192. mineDownUp()
  193. for i = 1, 0 do
  194.     advance()
  195.     for u = 1, 4 do
  196.         for k = 1, radius do
  197.             mineDownUp();
  198.             if (radius ~= 0) then
  199.                 --Go to next diagonal
  200.                 for l = 1, 2 do
  201.                     while (turtle.dig()) do end
  202.                     turtle.forward()
  203.                 end
  204.                 turtle.turnLeft()
  205.                 for p = 1, 2 do
  206.                     while (turtle.dig()) do end
  207.                     turtle.forward()
  208.                 end
  209.                 turtle.turnRight()
  210.             else
  211.                 return true
  212.             end
  213.         end
  214.         turtle.turnRight()
  215.     end
  216.     turtle.turnRight()
  217.     turtle.turnRight()
  218. end
Advertisement
Add Comment
Please, Sign In to add comment