Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local xPos,zPos = 0,-1
- local xDir,zDir = 0,1
- local depth = 0
- local trees = 0 -- trees or saplings detected
- local choppedTrees = 0
- local tArgs = { ... }
- if #tArgs < 2 then
- print( "Usage: chop <front_size> <right_size>" )
- print( "Pattern: ")
- print( "###" )
- print( "###" )
- print( "###" )
- print( "^ <-turtle, chest" )
- return
- end
- zSize = tonumber( tArgs[1] ) -- front_size
- xSize = tonumber( tArgs[2] ) -- right_size
- cycleDelay = 10
- if #tArgs > 2 then
- cycleDelay = tonumber( tArgs[3] )
- end
- local function checkFuel(needed)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- return fuelLevel >= needed
- end
- -- position: in front of a tree
- local function chopForward()
- turtle.dig()
- while not turtle.forward() do
- turtle.attack()
- sleep(1)
- end
- local height = 0
- while true do
- local r, item = turtle.inspectUp()
- if r then
- if item.name == "minecraft:log" then
- turtle.digUp()
- while not turtle.up() do
- turtle.digUp()
- sleep(1)
- end
- height = height + 1
- else
- break
- end
- else
- break
- end
- end
- while height > 0 do
- while not turtle.down() do
- turtle.digDown()
- sleep(1)
- end
- height = height - 1
- end
- while not turtle.digDown() do
- sleep(1)
- end
- choppedTrees = choppedTrees + 1
- for slot=1,16 do
- local t = turtle.getItemDetail(slot)
- if t then
- if t.name == "minecraft:sapling" then
- turtle.select(slot)
- while not turtle.placeDown() do
- sleep(1)
- end
- turtle.select(1)
- return
- end
- end
- end
- end
- local function countSaplings()
- local count = 0
- for slot=1,16 do
- local t = turtle.getItemDetail(slot)
- if t then
- if t.name == "minecraft:sapling" then
- count = count + t.count
- end
- end
- end
- return count
- end
- -- position: above the chest
- local function unload()
- local r, block = turtle.inspectDown()
- if not r then
- return false
- end
- if block.name ~= "minecraft:chest" then
- return false
- end
- for slot=1,16 do
- local t = turtle.getItemDetail(slot)
- if t then
- if t.name ~= "minecraft:sapling" then
- turtle.select(slot)
- if not turtle.dropDown() then
- -- no space
- return false
- end
- end
- end
- end
- -- optimize saplings
- for slot=2,16 do
- for i=1,slot-1 do
- if turtle.getItemCount(slot) == 0 then
- break
- end
- turtle.select(slot)
- turtle.transferTo(i)
- end
- end
- turtle.select(1)
- return true
- end
- -- position: above the chest
- local function unloadSaplings()
- local toUnload = 0
- if trees > 0 then
- -- unload saplings
- local saplings = countSaplings()
- if saplings > trees then
- toUnload = saplings - trees
- end
- end
- if toUnload == 0 then
- return true
- end
- for slot=1,16 do
- local t = turtle.getItemDetail(slot)
- if t and toUnload > 0 then
- if t.name == "minecraft:sapling" then
- local slotUnload = t.count
- if toUnload < t.count then
- slotUnload = toUnload
- end
- turtle.select(slot)
- if not turtle.dropDown(slotUnload) then
- -- no space
- return false
- end
- toUnload = toUnload - slotUnload
- end
- end
- end
- turtle.select(1)
- return true
- end
- local function forceForward()
- while true do
- if turtle.forward() then
- xPos = xPos + xDir
- zPos = zPos + zDir
- if not turtle.inspectDown() then
- -- suck only if there is no chest below
- turtle.suckDown()
- end
- return
- end
- local r, block = turtle.inspect()
- if r then
- if block.name == "minecraft:log" then
- chopForward()
- xPos = xPos + xDir
- zPos = zPos + zDir
- return
- end
- else
- turtle.attack()
- sleep(0.5)
- end
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- local function gotoDir(xd, zd)
- while (xDir ~= xd) or (zDir ~= zd) do
- turnLeft()
- end
- end
- local function goto(x, z, xd0, zd0)
- if (xPos ~= x) or (zPos ~= y) then
- local xd = 0
- if (xPos-x) < 0 then
- xd = 1
- else
- xd = -1
- end
- local zd = 0
- if (zPos-z) < 0 then
- zd = 1
- else
- zd = -1
- end
- gotoDir(xd, 0)
- while xPos ~= x do
- forceForward()
- end
- gotoDir(0, zd)
- while zPos ~= z do
- forceForward()
- end
- end
- gotoDir(xd0, zd0)
- end
- local function goNext()
- local z = zPos + zDir
- if (z>=0) and (z<zSize) then
- forceForward()
- return true
- end
- -- can't move forward, try to shift
- if (xPos+1) >= xSize then
- -- can't shift
- return false
- end
- local zd = zDir
- if zd==1 then
- turnRight()
- else
- turnLeft()
- end
- forceForward()
- if zd==1 then
- turnRight()
- else
- turnLeft()
- end
- return true
- end
- -- position: above the chest
- local function chopCycle()
- if not checkFuel((zSize + 1) * (xSize + 1) * 2) then
- print("Not enough fuel")
- return false
- end
- if not unload() then
- print("Unload failed")
- return false
- end
- -- reset tree counter
- trees = 0
- while goNext() do
- turtle.suckDown()
- local r, block = turtle.inspectDown()
- if r then
- if block.name == "minecraft:sapling" then
- trees = trees + 1
- end
- end
- end
- -- save a copy to ignore trees on a way back
- local trees2 = trees
- goto(0, -1, 0, 1)
- trees = trees2
- print("Trees: "..trees)
- return true
- end
- while true do
- -- initial position: above the chest
- local saplingsBefore = countSaplings()
- local fuelBefore = turtle.getFuelLevel()
- choppedTrees = 0
- if not chopCycle() then
- return
- end
- local saplingsAfter = countSaplings()
- local fuelAfter = turtle.getFuelLevel()
- local deltaSaplings = saplingsAfter - saplingsBefore
- local saplingsBudget = saplingsAfter - trees
- local deltaFuel = 0
- if fuelAfter ~= "unlimited" then
- deltaFuel = fuelAfter - fuelBefore
- end
- print("chopped: "..choppedTrees..", saplings: "..deltaSaplings..", fuel: "..deltaFuel)
- if not unload() then
- print("Unload failed")
- return
- end
- if not unloadSaplings() then
- print("Unload failed")
- return
- end
- if choppedTrees == 0 or saplingsBudget >= 0 then
- sleep(cycleDelay)
- end
- end
- -- turtle.inspectDown()
- -- true, {name="minecraft:sapling", metadata=2}
- -- true, {name="minecraft:log", metadata=2}
- -- turtle.getItemDetail(slot)
- -- {count=1, name="minecraft:sapling", damage=2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement