Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- chtreefarm
- -- refill fuel in slot FUEL_SLOT
- -- requires at least farmLength saplings in slot SAPLING_SLOT
- -- wood deposit at the beginning of the farm, saplings deposit at the end
- local FUEL_SLOT = 1
- local SAPLING_SLOT = 2
- local SAPLING_TMP_SLOT = 3
- local WOOD_SLOT = 4
- local MIN_FUEL_FOR_STEP = 26
- local args = {...}
- local farmLength = tonumber(args[1])
- local posX = 0
- local orient = 0
- local cycleCount = 0
- local treesChopped = 0
- local saplingsPlanted = 0
- local distanceTraveled = 0
- local farmingStarted = false
- function forward(n)
- for i=1,n do
- turtle.forward()
- distanceTraveled = distanceTraveled + 1
- if orient==0 then
- posX = posX+1
- end
- if orient==2 then
- posX = posX-1
- end
- end
- end
- function left()
- turtle.turnLeft()
- orient = orient-1
- while orient<0 do
- orient = orient+4
- end
- end
- function right()
- turtle.turnRight()
- orient = orient+1
- while orient>3 do
- orient = orient-4
- end
- end
- function transferSaplings()
- result = true
- for i = 1,16 do
- if not i==SAPLING_SLOT then
- turtle.select(i)
- if turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
- turtle.transferTo(SAPLING_SLOT)
- end
- end
- end
- if turtle.getItemCount(SAPLING_SLOT) < 2 then
- print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
- -- result = false
- while turtle.getItemCount(SAPLING_SLOT) < 2 do
- os.sleep(10)
- end
- end
- return result
- end
- function stripLeaves()
- turtle.select(SAPLING_TMP_SLOT)
- if turtle.detectUp() then
- turtle.digUp()
- end
- for i=1,3 do
- right()
- if turtle.detect() then
- turtle.dig()
- end
- end
- transferSaplings()
- right()
- end
- function chop()
- local height = 0
- while turtle.detect() do
- turtle.select(WOOD_SLOT)
- turtle.dig()
- stripLeaves()
- if turtle.up() then
- height = height + 1
- distanceTraveled = distanceTraveled + 1
- end
- end
- turtle.select(SAPLING_TMP_SLOT)
- for i=1,height do
- turtle.digDown()
- turtle.down()
- distanceTraveled = distanceTraveled + 1
- end
- if height>2 then
- treesChopped = treesChopped + 1
- end
- end
- function plant()
- turtle.select(SAPLING_SLOT)
- if turtle.getItemCount(SAPLING_SLOT) < 2 then
- if not transferSaplings() then
- return false
- end
- end
- turtle.place()
- os.sleep(0.5)
- if turtle.detect() and turtle.compare() then
- saplingsPlanted = saplingsPlanted + 1
- end
- os.sleep(2)
- return true
- end
- function waitForTree()
- local treeDetectedCount = 0
- local waitingTime = 0
- local height = 0
- turtle.select(SAPLING_SLOT)
- while treeDetectedCount < 7 do -- because sometimes turtle does not compare reliably
- if turtle.compare() then
- treeDetectedCount = 0
- else
- treeDetectedCount = treeDetectedCount + 1
- end
- os.sleep(2)
- waitingTime = waitingTime + 2
- if waitingTime > 30 and turtle.detectUp() then
- height = 0
- while turtle.detectUp() do
- turtle.select(SAPLING_TMP_SLOT)
- turtle.digUp()
- if turtle.up() then
- height = height + 1
- distanceTraveled = distanceTraveled + 1
- turtle.dig()
- end
- end
- for i=1,height do
- turtle.down()
- distanceTraveled = distanceTraveled + 1
- end
- end
- end
- end
- function handleTree()
- if not turtle.detect() then
- if not plant() then
- return false
- end
- else
- waitForTree()
- chop()
- if not plant() then
- return false
- end
- end
- return true
- end
- function dumpWood()
- for i = 1,16 do
- if (not i==SAPLING_SLOT) and (not i==FUEL_SLOT) then
- turtle.select(i)
- if not turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
- turtle.dropDown()
- end
- end
- end
- end
- function step()
- local result = true
- forward(2)
- if posX <= 0 then
- right()
- right()
- dumpWood()
- cycleCount = cycleCount + 1
- print("cycles finished: "..cycleCount)
- print("saplings planted: "..saplingsPlanted)
- print("trees chopped: "..treesChopped)
- print("distance traveled: "..distanceTraveled)
- os.sleep(10)
- elseif posX >= farmLength then
- right()
- right()
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suckDown()
- transferSaplings()
- os.sleep(10)
- elseif (posX % 4) == orient then
- right()
- result = handleTree()
- left()
- elseif (posX % 4) == (2-orient) then
- left()
- result = handleTree()
- right()
- end
- return result
- end
- function checkFuel()
- local result = true
- if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
- turtle.select(FUEL_SLOT)
- turtle.refuel()
- end
- if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
- result = false
- end
- return result
- end
- if farmLength==nil or farmLength<2 then
- print("Farm length too short.\n")
- return
- end
- if turtle.getItemCount(SAPLING_SLOT) < farmLength then
- print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
- return
- end
- while true do
- if not checkFuel() then
- print("Not enough fuel\n")
- os.sleep(10)
- while not checkFuel() do
- os.sleep(10)
- end
- return false
- end
- if not step() then
- print("Step unsuccessfull, exiting.\n")
- return false
- end
- os.sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement