Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local TARGET_WOOD = arg[1] or "minecraft:spruce"
- local MAX_HEIGHT = 50
- local FUEL_THRESHOLD = MAX_HEIGHT * 2 + 20
- local LOG = TARGET_WOOD .. "_log"
- local SAPLING = TARGET_WOOD .. "_sapling"
- local function forward()
- while not turtle.forward() do
- turtle.dig()
- end
- end
- local function hasItem(name)
- for slot = 1, 16 do
- local info = turtle.getItemDetail(slot)
- if info and info.name == name then
- return true, info.count, slot
- end
- end
- return false, 0, 1
- end
- local function checkEnvironment()
- turtle.turnLeft()
- if not turtle.suck(4) then
- turtle.turnRight()
- return false, "Missing sapling storage, or no saplings in storage!"
- end
- if not hasItem(SAPLING) then
- turtle.turnRight()
- return false, "Non-sapling items in sapling storage!"
- end
- turtle.drop(4)
- turtle.turnRight()
- local exists, info = turtle.inspect()
- if not exists or not info.name:match(TARGET_WOOD) then
- return false, "Missing tree!"
- end
- return true
- end
- -- cycle functions
- local function waitForTree()
- while true do
- local exists, block = turtle.inspect()
- if exists and block.name == LOG then
- break
- end
- if not exists or block.name ~= SAPLING then -- we're not looking at a sapling? look around, maybe we're out of alignment
- turtle.turnRight()
- end
- sleep(1)
- end
- end
- local function chopTree()
- forward()
- local y = 0
- while turtle.detectUp() do
- turtle.dig()
- turtle.turnRight()
- turtle.dig()
- turtle.turnLeft()
- turtle.digUp()
- turtle.up()
- if y > MAX_HEIGHT then
- break
- end
- y = y + 1
- end
- turtle.dig()
- turtle.turnRight()
- forward()
- turtle.turnLeft()
- forward()
- for _ = 1, y do
- turtle.digDown()
- turtle.down()
- end
- turtle.back()
- turtle.turnRight()
- turtle.back()
- turtle.turnLeft()
- turtle.back()
- end
- local function checkFuel()
- if turtle.getFuelLevel() > FUEL_THRESHOLD then
- return
- end
- for slot = 1, 16 do
- local info = turtle.getItemDetail(slot)
- if info then
- turtle.select(slot)
- local isCombustable = turtle.refuel(0)
- if isCombustable then
- turtle.refuel(16)
- end
- end
- end
- checkFuel()
- end
- local function dropoffItems()
- turtle.turnLeft()
- turtle.turnLeft()
- for slot = 1, 16 do
- local info = turtle.getItemDetail(slot)
- if info then
- turtle.select(slot)
- if info.name == LOG then
- turtle.drop(info.count)
- elseif info.name == SAPLING then
- turtle.turnRight()
- turtle.drop(info.count)
- turtle.turnLeft()
- else
- turtle.turnLeft()
- turtle.drop(info.count)
- turtle.turnRight()
- end
- end
- end
- turtle.select(1)
- turtle.turnRight()
- turtle.turnRight()
- end
- local function replantTree()
- local exists, count, slot = hasItem(SAPLING)
- if not exists or count < 4 then
- turtle.turnLeft()
- if slot then
- turtle.select(slot)
- turtle.transferTo(1)
- turtle.select(1)
- end
- turtle.suck(4 - count)
- turtle.turnRight()
- end
- local _, newCount = hasItem(SAPLING)
- if newCount < 4 then
- printError("Not enough saplings in storage!")
- sleep(30)
- return replantTree()
- end
- turtle.forward()
- turtle.forward()
- for _ = 1, 2 do
- turtle.turnRight()
- turtle.place()
- turtle.turnLeft()
- turtle.back()
- turtle.place()
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("checking environment")
- local good, reason = checkEnvironment()
- if not good then
- printError(reason)
- return
- end
- print("env good")
- local cycles = 0
- while true do
- print("waiting for tree...")
- waitForTree()
- print("chop cycle " .. cycles)
- chopTree()
- print("checking fuel...")
- checkFuel()
- dropoffItems()
- print("replanting...")
- replantTree()
- cycles = cycles + 1
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement