Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- harvestTree03.lua
- http://pastebin.com/UMkQ8uiY
- Turtle needs to move around and find more trees
- Suggested pattern explore a 10 x 10 square, with
- starting tree at the lower left side
- ]]--
- function refuel()
- --use wood to refuel if needed
- if turtle.getFuelLevel() < 15 then
- turtle.craft() -- craft wood to planks(1 wood, 1 planks = 15 fuel)
- turtle.refuel() -- add 4 x 15 fuel
- end
- end
- function harvestTree()
- turtle.dig() -- dig base of tree
- refuel()
- turtle.forward() -- go under tree
- -- Loop to climb up tree and harvest trunk and surrounding leaves
- while turtle.detectUp() do -- continue loop while block detected above
- turtle.digUp() -- Dig block above
- turtle.up() -- Move up
- -- Inner loop to check for leaves
- for i = 1, 4 do
- if turtle.detect() then -- check if leaves in front
- turtle.dig() --Dig leaves
- end
- turtle.turnRight()
- end
- end
- -- At top of the tree. New loop to return to ground
- while not turtle.detectDown() do -- While nothing detected below
- turtle.down() -- Go down
- end
- end
- function lookForTrees()
- --[[ Try to move forward 10 squares.
- - make sure on solid ground
- - If wood in front harvest tree.
- - else if leaves in front dig then move
- - else any other block climb up and over
- ]]--
- -- move 10 squares forward, move to next column and return
- -- repeat 5 x
- for i = 1, 5 do
- refuel() -- check if fuel needed
- harvestRun(10) -- move 10 blocks
- turtle.turnRight()
- harvestRun(1)
- turtle.turnRight() --now facing opposite direction
- harvestRun(10) -- move 10 blocks
- if i < 5 then -- turn left if not on last run
- turtle.turnLeft()
- harvestRun(1)
- turtle.turnLeft()
- end
- end
- --return to starting position
- turtle.turnRight()
- harvestRun(10)
- turtle.turnRight()
- end
- function harvestRun(runLength)
- local success = false
- local data = {} --initialise empty table variable
- --[[
- turtle.inspect() returns two values
- 1) boolean (true/false) success
- 2) table with two values:
- .name (string) e.g. "minecraft:log"
- .metadata (integer) e.g. 0
- oak has metadata of 0, spruce 1, birch 2 etc
- ]]--
- for i = 1, runLength do
- while not turtle.inspectDown() do -- must be air below
- turtle.down()
- end
- if not turtle.forward() then --can't move forward
- success, data = turtle.inspect() -- store information about the block in front in a table
- if success then
- if data.name == "minecraft:log" then -- tree in front, so harvest it
- harvestTree()
- -- check if leaves or grass in front, dig through
- elseif data.name == "minecraft:leaves" or data.name == "minecraft:tallgrass"
- or data.name == "minecraft:double_plant" then
- turtle.dig()
- turtle.forward()
- else -- some other solid block so climb up and over it
- while turtle.detect() do
- turtle.digUp() -- in case any blocks above
- turtle.up()
- end
- turtle.forward()
- end
- end
- end
- end
- end
- function main()
- os.sleep(2) -- pause for 2 secs to allow time to press esc
- harvestTree() -- Call the harvestTree function
- lookForTrees() -- Find more trees
- end
- --*********************Program runs from here*****************
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement