Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SylLumberjack
- local monitorWaitTime = 30
- local nextTurn = "left"
- local shouldStop = false
- local shouldStopThreshold = 10
- local function outOfSaplings()
- return turtle.getItemCount(3) == 0
- end
- local function tryUp()
- if turtle.getFuelLevel() == 0 then
- shouldStop = true
- else
- turtle.up()
- end
- end
- local function tryDown()
- if turtle.getFuelLevel() == 0 then
- shouldStop = true
- else
- turtle.down()
- end
- end
- local function tryForward()
- if turtle.getFuelLevel() == 0 then
- shouldStop = true
- else
- turtle.forward()
- end
- end
- local function chopTree()
- -- assumes we are facing the base of the tree
- local stepsUp = 0
- turtle.dig()
- tryForward()
- while turtle.detectUp() do
- turtle.digUp()
- tryUp()
- stepsUp = stepsUp + 1
- end
- for i=1,stepsUp do
- turtle.digDown()
- tryDown()
- end
- if turtle.getFuelLevel() < shouldStopThreshold then
- shouldStop = true
- end
- end
- local function plantTree()
- turtle.select(3)
- turtle.place()
- end
- local function checkTreeLeft()
- turtle.select(3)
- turtle.turnLeft()
- if turtle.detect() and not turtle.compare() then
- chopTree()
- turtle.turnRight()
- turtle.turnRight()
- tryForward()
- turtle.turnLeft()
- return true
- else
- turtle.turnRight()
- return false
- end
- end
- local function checkTreeRight()
- turtle.select(3)
- turtle.turnRight()
- if turtle.detect() and not turtle.compare() then
- chopTree()
- turtle.turnRight()
- turtle.turnRight()
- tryForward()
- turtle.turnRight()
- return true
- else
- turtle.turnLeft()
- return false
- end
- end
- local function checkTreeRow()
- while not turtle.detect() and not shouldStop do
- turtle.forward()
- if checkTreeLeft() then
- turtle.turnLeft()
- plantTree()
- turtle.turnRight()
- end
- if checkTreeRight() then
- turtle.turnRight()
- plantTree()
- turtle.turnLeft()
- end
- end
- end
- local function toggleNextTurn()
- if nextTurn == "left" then
- nextTurn = "right"
- else
- nextTurn = "left"
- end
- end
- local function goToNextRow()
- if nextTurn == "left" then
- turtle.turnLeft()
- for i=1,4 do
- tryForward()
- end
- turtle.turnLeft()
- else
- turtle.turnRight()
- for i=1,4 do
- tryForward()
- end
- turtle.turnRight()
- end
- toggleNextTurn()
- end
- local function unload()
- -- assumes the turtle is over a chest
- for n=4,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- turtle.dropDown()
- end
- end
- end
- local function runTreePath()
- local done = false
- while not done do
- checkTreeRow()
- if shouldStop then
- return
- end
- -- once we get here, something is in front of us
- turtle.select(1)
- if turtle.compare() then
- -- Stop marker hit
- done = true
- end
- turtle.select(2)
- if turtle.compare() then
- -- Turn marker hit
- goToNextRow()
- end
- end
- unload()
- turtle.turnRight()
- turtle.turnRight()
- end
- local function run()
- -- fuel for run
- turtle.select(4)
- turtle.refuel()
- while not shouldStop do
- -- assumes we start on the row
- -- that ends with a left turn
- runTreePath()
- sleep(monitorWaitTime)
- end
- end
- term.clear()
- textutils.slowPrint("SylLumberjack v0.1")
- print("Slots: 1 - Stop Marker | 2 - Turn Marker | 3 - Saplings | 4 - Fuel")
- local input = "none"
- while (input ~= "left") and (input ~= "right") do
- print("Enter first turn direction (left/right): ")
- input = read()
- end
- nextTurn = input
- run()
Add Comment
Please, Sign In to add comment