Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local scriptName = "lumberjack"
- if #args < 3 then
- print("Usage: " .. scriptName.. " <rows> <number of trees> <sleep interval>")
- return
- end
- local rowsToChop = tonumber(args[1])
- local treesPerRow = tonumber(args[2])
- local sleepInterval = tonumber(args[3])
- local treeCount = rowsToChop * treesPerRow * 2
- local logTally = 0
- local depositErrors = 0
- dofile("turtle_bot")
- local t = TurtleBot.new(scriptName, "West")
- t:connect()
- local function plant()
- if turtle.detect() then return end
- if t:selectItem(ITEMS.sapling.name) then return turtle.place() end
- error("Ran out of saplings to plant", 2)
- end
- local function isLog(direction)
- direction = direction or "front"
- return t:detectBlock({BLOCKS.log.name}, direction)
- end
- local function isChoppable(direction)
- return t:detectBlock({BLOCKS.log.name, BLOCKS.leaves.name}, direction)
- end
- local function goDown()
- while true do
- local success, _ = turtle.inspectDown()
- if success then
- if isChoppable("down") then
- t:dig("down")
- else
- return true
- end
- end
- t:down()
- end
- end
- local function chopLog(direction)
- direction = direction or "front"
- if not isChoppable(direction) then return false end
- if isLog(direction) then logTally = logTally + 1 end
- t:dig(direction)
- end
- local function chopLayer(iteration)
- chopLog("up")
- for i = 1, 4 do
- chopLog()
- if iteration ~= 1 then
- turtle.turnRight()
- turtle.suck()
- end
- end
- end
- local function chopTree()
- local i = 1
- while isLog() do
- chopLayer(i)
- t:up()
- i = i + 1
- end
- goDown()
- end
- local function chopRow()
- for tree=1, treesPerRow do
- chopTree()
- turtle.suck()
- plant()
- t:right()
- turtle.suck()
- if tree ~= treesPerRow then
- t:forward()
- t:left()
- else
- t:right()
- t:forward()
- end
- end
- t:updateData()
- for tree=1, treesPerRow do
- chopTree()
- turtle.suck()
- plant()
- t:right()
- turtle.suck()
- if tree ~= treesPerRow then
- t:forward()
- t:left()
- end
- end
- t:updateData()
- end
- local function switchRow()
- t:forward()
- t:left()
- t:forward(3)
- t:left()
- t:forward()
- t:left()
- end
- local function printInfo()
- shell.run("clear")
- print("Deposit errors: " .. depositErrors .. ".")
- print("Logs chopped: " .. logTally .. ".")
- print("Trees in my farm: " .. treeCount .. "!")
- print("Current fuel level: " .. turtle.getFuelLevel() .. ".")
- print("Sleeping for " .. sleepInterval .. " seconds...")
- end
- local function reset()
- local blocks_to_path = (rowsToChop - 1) * 4 + 1
- t:forward()
- t:right()
- t:forward(blocks_to_path)
- t:right()
- t:combine()
- t:enderDeposit(COLOR_CHANNELS.farmingDepot, {})
- t:enderRefuel(2000, 5000, ITEMS.charcoal)
- end
- local function main()
- while true do
- t:enderCollect(COLOR_CHANNELS.sapling, treeCount)
- t:forward()
- t:left()
- for row = 1, rowsToChop do
- chopRow()
- if row ~= rowsToChop then
- switchRow()
- end
- end
- reset()
- printInfo()
- sleep(sleepInterval)
- end
- end
- t:execute(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement