Advertisement
ItsNoah

CC DFS Mining

Aug 7th, 2021
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ITER = 10
  2. local TORCH_DIST = 13
  3.  
  4. local MINE_BLOCKS = {
  5.   ["minecraft:gold_ore"]=true,
  6.   ["minecraft:iron_ore"]=true,
  7.   ["minecraft:coal_ore"]=true,
  8.   ["minecraft:redstone_ore"]=true,
  9.   ["minecraft:emerald_ore"]=true
  10. }
  11.  
  12. local REPORT_BLOCKS = {
  13.   ["minecraft:diamond_ore"]=true,
  14.   ["minecraft:lapis_ore"]=true
  15. }
  16.  
  17. local FILL_BLOCK = "minecraft:cobblestone"
  18.  
  19. local Dir = {
  20.   Front = 0,
  21.   Back = 1,
  22.   Left = 2,
  23.   Right = 3,
  24.   Up = 4,
  25.   Down = 5
  26. }
  27.  
  28. local logFile = fs.open("mine.log", "w")
  29.  
  30. local function log(content)
  31.   logFile.writeLine(content)
  32.   logFile.flush()
  33. end
  34.  
  35. local function myVec(depth, dir)
  36.   return {depth=depth, dir=dir}
  37. end
  38.  
  39. local function alignDir(dir, depth, track)
  40.   if (dir == Dir.Left) then
  41.     turtle.turnLeft()
  42.     table.insert(track, myVec(depth, Dir.Left))
  43.   elseif (dir == Dir.Right) then
  44.     turtle.turnRight()
  45.     table.insert(track, myVec(depth, Dir.Right))
  46.   elseif (dir == Dir.Back) then
  47.     turtle.turnRight()
  48.     turtle.turnRight()
  49.     table.insert(track, myVec(depth, Dir.Right))
  50.     table.insert(track, myVec(depth, Dir.Right))
  51.   end
  52. end
  53.  
  54. local function undoAlignDir(depth, track)
  55.   while (#track > 0 and track[#track].depth == depth) do
  56.     local vec = table.remove(track)
  57.     if (vec.dir == Dir.Left) then
  58.       turtle.turnRight()
  59.     elseif (vec.dir == Dir.Right) then
  60.       turtle.turnLeft()
  61.     end
  62.   end
  63. end
  64.  
  65. local function isMine(hasBlock, blockInfo)
  66.   if (hasBlock and REPORT_BLOCKS[blockInfo.name]) then
  67.     log("found " .. blockInfo.name)
  68.   end
  69.   return hasBlock and MINE_BLOCKS[blockInfo.name]
  70. end
  71.  
  72. local function inspectDirIsMine(dir)
  73.   local hasBlock, blockInfo
  74.   if (dir == Dir.Up) then
  75.     hasBlock, blockInfo = turtle.inspectUp()
  76.   elseif (dir == Dir.Down) then
  77.     hasBlock, blockInfo = turtle.inspectDown()
  78.   else
  79.     hasBlock, blockInfo = turtle.inspect()
  80.   end
  81.  
  82.   return isMine(hasBlock, blockInfo)
  83. end
  84.  
  85. local function excavateDir(dir, depth, track)
  86.   if (dir == Dir.Up) then
  87.     turtle.digUp()
  88.     turtle.up()
  89.     table.insert(track, myVec(depth, Dir.Up))
  90.   elseif (dir == Dir.Down) then
  91.     turtle.digDown()
  92.     turtle.down()
  93.     table.insert(track, myVec(depth, Dir.Down))
  94.   else
  95.     local counter = 0
  96.     repeat
  97.       turtle.dig()
  98.       counter = counter + 1
  99.     until turtle.forward()
  100.     table.insert(track, myVec(depth, Dir.Front))
  101.     if counter > 1 then
  102.       log("WARNING: encountered gravity block during DFS")
  103.     end
  104.   end
  105. end
  106.  
  107. local function selectFillBlock()
  108.   for i = 1, 16 do
  109.     local info = turtle.getItemDetail(i)
  110.     if info and info.name == FILL_BLOCK then
  111.       turtle.select(i)
  112.       return
  113.     end
  114.   end
  115.   log("WARNING: not enough blocks to fill in")
  116. end
  117.  
  118. local function backtrack(currentDepth, targetDepth, moveTrack, turnTrack)
  119.   while (currentDepth ~= targetDepth) do
  120.     while (#moveTrack > 0 and moveTrack[#moveTrack].depth == currentDepth - 1) do
  121.       local vec = table.remove(moveTrack)
  122.       if (vec.dir == Dir.Front) then
  123.         turtle.back()
  124.         selectFillBlock()
  125.         turtle.place()
  126.       elseif (vec.dir == Dir.Up) then
  127.         turtle.down()
  128.         selectFillBlock()
  129.         turtle.placeUp()
  130.       elseif (vec.dir == Dir.Down) then
  131.         turtle.up()
  132.         selectFillBlock()
  133.         turtle.placeDown()
  134.       end
  135.     end
  136.     undoAlignDir(currentDepth - 1, turnTrack)
  137.     currentDepth = currentDepth - 1
  138.   end
  139. end
  140.  
  141. local function dfs(initDir)
  142.   local stack = {}
  143.   local turnTrack = {}
  144.   local moveTrack = {}
  145.   local depth = 0
  146.   table.insert(stack, myVec(depth, initDir))
  147.  
  148.   while (#stack > 0) do
  149.     local vec = table.remove(stack)
  150.     backtrack(depth, vec.depth, moveTrack, turnTrack)
  151.     depth = vec.depth
  152.  
  153.     alignDir(vec.dir, depth, turnTrack)
  154.  
  155.     if (inspectDirIsMine(vec.dir)) then
  156.       excavateDir(vec.dir, depth, moveTrack)
  157.       depth = depth + 1
  158.  
  159.       if isMine(turtle.inspect()) then
  160.         table.insert(stack, myVec(depth, Dir.Front))
  161.       end
  162.  
  163.       if isMine(turtle.inspectUp()) then
  164.         table.insert(stack, myVec(depth, Dir.Up))
  165.       end
  166.       if isMine(turtle.inspectDown()) then
  167.         table.insert(stack, myVec(depth, Dir.Down))
  168.       end
  169.       turtle.turnRight()
  170.       if isMine(turtle.inspect()) then
  171.         table.insert(stack, myVec(depth, Dir.Right))
  172.       end
  173.       turtle.turnRight()
  174.       if isMine(turtle.inspect()) then
  175.         table.insert(stack, myVec(depth, Dir.Back))
  176.       end
  177.       turtle.turnRight()
  178.       if isMine(turtle.inspect()) then
  179.         table.insert(stack, myVec(depth, Dir.Left))
  180.       end
  181.       turtle.turnRight()
  182.     else
  183.       undoAlignDir(depth, turnTrack)
  184.     end
  185.   end
  186.  
  187.   backtrack(depth, 0, moveTrack, turnTrack)
  188. end
  189.  
  190. local function triggerDfs(dir)
  191.   if inspectDirIsMine(dir) then
  192.     dfs(dir)
  193.   end
  194. end
  195.  
  196. local function inspectAround()
  197.   turtle.turnRight()
  198.   triggerDfs(Dir.Front)
  199.  
  200.   turtle.turnLeft()
  201.   triggerDfs(Dir.Up)
  202.  
  203.   turtle.turnLeft()
  204.   triggerDfs(Dir.Front)
  205.  
  206.   turtle.down()
  207.   triggerDfs(Dir.Front)
  208.  
  209.   turtle.turnRight()
  210.   triggerDfs(Dir.Down)
  211.  
  212.   turtle.turnRight()
  213.   triggerDfs(Dir.Front)
  214.  
  215.   turtle.turnLeft()
  216.   turtle.up()
  217. end
  218.  
  219. local function advance()
  220.   repeat
  221.     turtle.dig()
  222.   until turtle.forward()
  223.   turtle.digDown()
  224. end
  225.  
  226. log("I'm minning!")
  227.  
  228. advance()
  229. inspectAround()
  230. turtle.select(1)
  231. turtle.placeDown()
  232.  
  233. for i = 1, ITER do
  234.   for j = 1, TORCH_DIST do
  235.     advance()
  236.     inspectAround()
  237.   end
  238.   turtle.select(1)
  239.   turtle.placeDown()
  240. end
  241.  
  242. logFile.close()
  243.  
  244. turtle.turnRight()
  245. turtle.turnRight()
  246. for i = 1, ITER * TORCH_DIST do
  247.   turtle.forward()
  248. end
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement