Advertisement
Inksaver

harvestTree03

Nov 22nd, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. --[[
  2.     harvestTree03.lua
  3.     http://pastebin.com/UMkQ8uiY
  4.     Turtle needs to move around and find more trees
  5.     Suggested pattern explore a 10 x 10 square, with
  6.     starting tree at the lower left side
  7.    
  8. ]]--
  9. function refuel()
  10.     --use wood to refuel if needed
  11.     if turtle.getFuelLevel() < 15 then
  12.         turtle.craft()   -- craft wood to planks(1 wood, 1 planks = 15 fuel)
  13.         turtle.refuel()  -- add 4 x 15 fuel
  14.     end
  15. end
  16.  
  17. function harvestTree()
  18.     turtle.dig()       -- dig base of tree
  19.     refuel()
  20.     turtle.forward() -- go under tree
  21.     -- Loop to climb up tree and harvest trunk and surrounding leaves
  22.     while turtle.detectUp() do -- continue loop while block detected above
  23.         turtle.digUp() -- Dig block above
  24.         turtle.up()    -- Move up
  25.         -- Inner loop to check for leaves
  26.         for i = 1, 4 do
  27.             if turtle.detect() then -- check if leaves in front
  28.                 turtle.dig() --Dig leaves
  29.             end
  30.             turtle.turnRight()
  31.         end
  32.     end
  33.     -- At top of the tree. New loop to return to ground
  34.     while not turtle.detectDown() do -- While nothing detected below
  35.         turtle.down() -- Go down
  36.     end
  37. end
  38.  
  39. function lookForTrees()
  40.     --[[ Try to move forward 10 squares.
  41.             - make sure on solid ground
  42.             - If wood in front harvest tree.
  43.             - else if leaves in front dig then move
  44.             - else any other block climb up and over
  45.     ]]--
  46.     -- move 10 squares forward, move to next column and return
  47.     -- repeat 5 x
  48.     for i = 1, 5 do
  49.         refuel()       -- check if fuel needed
  50.         harvestRun(10) -- move 10 blocks
  51.         turtle.turnRight()
  52.         harvestRun(1)
  53.         turtle.turnRight() --now facing opposite direction
  54.         harvestRun(10) -- move 10 blocks
  55.         if i < 5 then -- turn left if not on last run
  56.             turtle.turnLeft()
  57.             harvestRun(1)
  58.             turtle.turnLeft()
  59.         end
  60.     end
  61.     --return to starting position
  62.     turtle.turnRight()
  63.     harvestRun(10)
  64.     turtle.turnRight()
  65. end
  66.  
  67. function harvestRun(runLength)
  68.     local success = false
  69.     local data = {} --initialise empty table variable
  70.    
  71.     --[[
  72.     turtle.inspect() returns two values
  73.     1) boolean (true/false) success
  74.     2) table with two values:
  75.         .name (string) e.g. "minecraft:log"
  76.         .metadata (integer) e.g. 0
  77.         oak has metadata of 0, spruce 1, birch 2 etc
  78.     ]]--
  79.     for i = 1, runLength do
  80.         while not turtle.inspectDown() do -- must be air below
  81.             turtle.down()
  82.         end
  83.         if not turtle.forward() then --can't move forward
  84.             success, data = turtle.inspect() -- store information about the block in front in a table
  85.             if success then
  86.                 if data.name == "minecraft:log" then -- tree in front, so harvest it
  87.                     harvestTree()
  88.                 -- check if leaves or grass in front, dig through  
  89.                 elseif data.name == "minecraft:leaves" or data.name == "minecraft:tallgrass"
  90.                     or data.name == "minecraft:double_plant" then
  91.                     turtle.dig()
  92.                     turtle.forward()
  93.                 else -- some other solid block so climb up and over it
  94.                     while turtle.detect() do
  95.                         turtle.digUp() -- in case any blocks above
  96.                         turtle.up()
  97.                     end
  98.                     turtle.forward()
  99.                 end
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. function main()
  106.     os.sleep(2)    -- pause for 2 secs to allow time to press esc
  107.     harvestTree()  -- Call the harvestTree function
  108.     lookForTrees() -- Find more trees
  109. end
  110.  
  111. --*********************Program runs from here*****************
  112. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement