dealingwith

branch_miner

Jan 2nd, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. -- This work is licensed under a CC BY-NC-SA 3.0 license.
  2. -- http://creativecommons.org/licenses/by-nc-sa/3.0/
  3. --
  4. -- by the Forlorn Hedgehog
  5. --
  6. -- Usage: branchMine <length>
  7. -- Fuel goes in slot 1. Torches go in slot 2. Floor blocks (usually cobblestone) go in slot 3.
  8. -- Mines a trunk <length> blocks long, with an 8 block long branch, on each side, every 3 blocks, starting 2 blocks in (n=3n-1)
  9.  
  10. -- If fuel level is less than 10, refuel
  11. function fuel()
  12.     if turtle.getFuelLevel() < 10 then
  13.         turtle.select(1)
  14.         if turtle.refuel(1) then
  15.             return true
  16.         end
  17.         print("Refuelling failed.")
  18.         return false
  19.     end
  20. end
  21.  
  22. -- Place floor block, if there is not already one.
  23. function placeFloorBlock()
  24.     if not turtle.detectDown() then
  25.         turtle.select(3)
  26.         if turtle.placeDown() then
  27.             return true
  28.         end
  29.         print("Placing floor block failed.")
  30.         return false
  31.     end
  32. end
  33.  
  34. -- Digs and moves into a 1x2x1 (xyz) area.
  35. function basicDigAndMove()
  36.     while not turtle.forward() do
  37.         if not turtle.dig() then
  38.             turtle.attack()
  39.         end
  40.     end
  41.     while turtle.detectUp() do
  42.         turtle.digUp()
  43.         sleep(0.5)
  44.     end
  45.     placeFloorBlock()
  46. end
  47.  
  48. -- Turn turtle 180 degrees
  49. function turnAround()
  50.     turtle.turnLeft()
  51.     turtle.turnLeft()
  52. end
  53.  
  54. -- Place torch on floor, in front of the turtle
  55. function placeTorch()
  56.     turtle.select(2)
  57.     if turtle.place() then
  58.         return true
  59.     end
  60.     print("Placing torch failed.")
  61.     return false
  62. end
  63.  
  64. -- This function mines a 1x2x8 (xyz) tunnel directly infront of the turtle, placing a torch near the end, and returning to start point (but in opposite orientation)
  65. function branch()
  66.     local blocksMovedSideways = 0
  67.     while blocksMovedSideways < 8 do
  68.         fuel()
  69.         basicDigAndMove()
  70.         blocksMovedSideways = blocksMovedSideways + 1
  71.  
  72.         if blocksMovedSideways == 8 then
  73.             turnAround()
  74.             placeTorch()
  75.         end
  76.     end
  77.    
  78.     -- Back to start point (goes up to avoid the torch on floor)
  79.     turtle.up()
  80.     for i=1,8 do
  81.         turtle.forward()
  82.         blocksMovedSideways = blocksMovedSideways - 1
  83.     end
  84.     turtle.down()
  85. end
  86.  
  87. -- Arguments
  88. local tArgs = {...}
  89. if #tArgs ~= 1 or tonumber(tArgs[1]) == nil or math.floor(tonumber(tArgs[1])) ~= tonumber(tArgs[1]) or tonumber(tArgs[1]) < 1 then
  90.     print("Usage: branchMine <length>")
  91.     return
  92. end
  93.  
  94. local length = tonumber(tArgs[1])
  95.  
  96. local blocksMovedForward = 0
  97. while blocksMovedForward < length do
  98.     fuel()
  99.     basicDigAndMove()
  100.     blocksMovedForward = blocksMovedForward + 1
  101.  
  102.     -- Add torch (on floor) every 8 blocks
  103.     if (blocksMovedForward % 8) == 0 then
  104.         turnAround()
  105.         placeTorch()
  106.         turnAround()
  107.     end
  108.  
  109.     -- n=3n-1
  110.     if ((blocksMovedForward + 1) % 3) == 0 then
  111.         turtle.turnLeft()
  112.         branch()
  113.         branch()
  114.         turtle.turnRight()
  115.     end
  116. end
Add Comment
Please, Sign In to add comment