sCCripts/tree.lua

101 lines
2.7 KiB
Lua

-- Created by Jef Roosens
-- TODO: fix bug where if you start a 2x2 tree in the middle, the turtle will still end up on the bottom
local fuel = require('jlib.fuel')
local move = require('jlib.move').move
-- Chop a tree in the given direction
local function chop(tree_type, two_deep, go_down, return_to_start)
local i, data = 0
if go_down then _, data = turtle.inspectDown() else _, data = turtle.inspectUp() end
while data.name == tree_type do
-- Make sure we have enough fuel to get back up
fuel.check(i + 1)
if two_deep then turtle.dig() end
-- This also mines the block
if go_down then move('d') else move('u') end
i = i + 1
if go_down then _, data = turtle.inspectDown() else _, data = turtle.inspectUp() end
end
-- Dig one higher. This allows us to chop jungle trees, which often leave one block higher
if i > 0 and not go_down then
fuel.check(i + 1)
if two_deep then turtle.dig() end
move('u')
i = i + 1
end
-- Dig the final block
if two_deep then turtle.dig() end
-- Because we check the fuel level every time, we know for certain we can get back up/down
if return_to_start then
if go_down then move('u', i) else move('d', i) end
end
end
-- Determine tree type
local _, data = turtle.inspect()
local tree_type = data.name
if not fuel.check(1) then
print("I need 1 fuel to start.")
return
end
-- Get into position
turtle.dig()
turtle.forward()
-- Check if it's a two-deep tree
local _, data = turtle.inspect()
local two_deep, two_by_two, right = data.name == tree_type, false, false
-- Check if it's a 2x2 tree
if two_deep then
turtle.turnRight()
local _, data = turtle.inspect()
turtle.turnLeft()
two_by_two = data.name == tree_type
-- Check if we're on the right side instead
if not two_by_two then
turtle.turnLeft()
_, data = turtle.inspect()
turtle.turnRight()
two_by_two = data.name == tree_type
right = two_by_two
end
end
-- Chop the actual tree
-- Chop left half
chop(tree_type, two_deep, true, true)
-- Come back down if it's not a two_by_two, else don't
chop(tree_type, two_deep, false, not two_by_two)
-- Get into position if it's a two-by-two
if two_by_two then
if right then turtle.turnLeft() else turtle.turnRight() end
turtle.dig()
turtle.forward()
if right then turtle.turnRight() else turtle.turnLeft() end
-- Go back down, or dig second half
chop(tree_type, two_deep, true, false)
if right then turtle.turnRight() else turtle.turnLeft() end
turtle.forward()
if right then turtle.turnLeft() else turtle.turnRight() end
end