Advertisement
HappySunChild

veinmine

Mar 7th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. local facing = 0 -- everything will be done in local space just because it's simple and easy to add
  2. local startPos = vector.new(0,0,0)
  3. local currentPos = startPos
  4. local lastMinedBlockPos = startPos
  5. local lastMinedTurtlePos = startPos
  6.  
  7. function getVeinmineBlockData() -- called at the start of the program
  8.     local block, data = turtle.inspect()
  9.  
  10.     if not block then
  11.         block, data = turtle.inspectDown()
  12.         if not block then
  13.             block, data = turtle.inspectUp()
  14.         end
  15.     end
  16.  
  17.     return data
  18. end
  19.  
  20. local veinblock = getVeinmineBlockData()
  21.  
  22. function inspect(dir)
  23.     local block, data = turtle.inspect()
  24.  
  25.     if dir < 0 then
  26.         block, data = turtle.inspectDown()
  27.     elseif dir > 0 then
  28.         block, data = turtle.inspectUp()
  29.     end
  30.  
  31.     if block then  
  32.         if data.name == veinblock.name then
  33.             return true
  34.         else
  35.             return false
  36.         end
  37.     else
  38.         return false
  39.     end
  40. end
  41.  
  42. function dig(dir)
  43.     if dir == 0 then
  44.         turtle.dig()
  45.  
  46.         local change = vector.new(0,0,0)
  47.  
  48.         if facing == 0 then
  49.             change = vector.new(0,0,-1)
  50.         elseif facing == 1 then
  51.             change = vector.new(1,0,0)
  52.         elseif facing == 2 then
  53.             change = vector.new(0,0,1)
  54.         elseif facing == 3 then
  55.             change = vector.new(-1,0,0)
  56.         end
  57.  
  58.         lastMinedBlockPos = currentPos + change
  59.     end
  60.  
  61.     if dir < 0 then
  62.         turtle.digDown()
  63.  
  64.         lastMinedBlockPos = currentPos + vector.new(0,-1,0)
  65.     end
  66.  
  67.     if dir > 0 then
  68.         turtle.digUp()
  69.  
  70.         lastMinedBlockPos = currentPos + vector.new(0,1,0)
  71.     end
  72. end
  73.  
  74. function turn(dir)
  75.     if dir < 0 then
  76.         turtle.turnLeft()
  77.  
  78.         facing = facing - 1
  79.  
  80.         if facing < 0 then
  81.             facing = 3
  82.         end
  83.     elseif dir > 0 then
  84.         turtle.turnRight()
  85.  
  86.         facing = facing + 1
  87.        
  88.         if facing > 3 then
  89.             facing = 0
  90.         end
  91.     end
  92.  
  93.     --print((facing == 0 and "North") or (facing == 1 and "East") or (facing == 2 and "South") or (facing == 3 and "West"))
  94. end
  95.  
  96. function lookForVeinmineBlock(depth) -- call if we can't find any blocks
  97.     local ahead, above, below = inspect(0) , inspect(1), inspect(-1)
  98.     local canSeeVeinBlock = ahead or above or below
  99.  
  100.     print(depth, canSeeVeinBlock)
  101.  
  102.     if not canSeeVeinBlock then
  103.         turn(1)
  104.  
  105.         if depth > 3 then
  106.             repeat turtle.down() until turtle.detectDown()
  107.             return true
  108.         end
  109.  
  110.         lookForVeinmineBlock(depth + 1)
  111.         return false
  112.     end
  113.    
  114.     if above then
  115.         dig(1)
  116.     end
  117.  
  118.     if ahead then
  119.         dig(0)
  120.     end
  121.  
  122.     if below then
  123.         dig(-1)
  124.     end
  125.  
  126.     local difference = currentPos - lastMinedBlockPos
  127.  
  128.     if difference.z == 1 then -- north* of the turtle *(north in relative space)
  129.         if facing == 0 then
  130.             turtle.forward()
  131.         elseif facing == 1 then
  132.             turn(-1)
  133.             turtle.forward()
  134.         elseif facing == 2 then
  135.             turn(-1)
  136.             turn(-1)
  137.             turtle.forward()
  138.         elseif facing == 3 then
  139.             turn(1)
  140.             turtle.forward()
  141.         end
  142.     elseif difference.z == -1 then -- south of the turtle
  143.         if facing == 0 then
  144.             turn(-1)
  145.             turn(-1)
  146.             turtle.forward()
  147.         elseif facing == 1 then
  148.             turn(1)
  149.             turtle.forward()
  150.         elseif facing == 2 then
  151.             turtle.forward()
  152.         elseif facing == 3 then
  153.             turn(-1)
  154.             turtle.forward()
  155.         end
  156.     elseif difference.x == 1 then -- east of the turtle
  157.         if facing == 0 then
  158.            turn(1)
  159.            turtle.forward()
  160.         elseif facing == 1 then
  161.             turn(-1)
  162.             turn(-1)
  163.             turtle.forward()
  164.         elseif facing == 2 then
  165.             turn(-1)
  166.             turtle.forward()
  167.         elseif facing == 3 then
  168.             turtle.forward()
  169.         end
  170.     elseif difference.x == -1 then -- west of the turtle
  171.         if facing == 0 then
  172.             turn(-1)
  173.             turtle.forward()
  174.         elseif facing == 1 then
  175.             turtle.forward()
  176.         elseif facing == 2 then
  177.             turn(1)
  178.             turtle.forward()
  179.         elseif facing == 3 then
  180.             turn(1)
  181.             turn(1)
  182.             turtle.forward()
  183.         end
  184.     elseif difference.y == -1 then
  185.         turtle.up()
  186.     elseif difference.y == 1 then
  187.         turtle.down()
  188.     end
  189.  
  190.     return false
  191. end
  192.  
  193. while true do
  194.     if lookForVeinmineBlock(0) then
  195.         break
  196.     end
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement