Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- chfirfarm
- -- refill fuel in slot FUEL_SLOT
- -- requires at least 4 saplings in slot SAPLING_SLOT
- -- 2x2 planting area in front (and to the right) of the turtle
- -- must start with 4 saplings planted in the planting area or even the tree can be already grown
- -- wood deposit anywhere behind the turtle, but there has to be some stopping block
- -- auxiliary sapling storage somewhere below the starting turtle position (directly under it) where fallen saplings should be collected
- -- if the turtle stops at the wood deposit, it means it's full and needs to be emptied
- local FUEL_SLOT = 1
- local SAPLING_SLOT = 2
- local SAPLING_TMP_SLOT = 3
- local MIN_SAPLINGS = 5
- local SAPLINGS_BUFFER = 64
- local WOOD_SLOT = 4
- local MAX_WOOD = 620
- local MIN_FUEL_FOR_CYCLE = 360
- local MAX_WOOD_STORAGE_DISTANCE = 32
- local REPEATED_DETECTION_COUNT = 3
- local REPEATED_DETECTION_TICK = 1
- local NEXT_DETECTION_TICK = 1
- local TREE_GROW_SLEEP_TIME = 40
- local posX = 0
- local orient = 0
- local cycleCount = 0
- local distanceTraveled = 0
- local auxiliaryDepth = 0
- function forward(n)
- for i=1,n do
- if turtle.forward() then
- distanceTraveled = distanceTraveled + 1
- if orient==0 then
- posX = posX+1
- end
- if orient==2 then
- posX = posX-1
- end
- else
- return false
- end
- end
- return true
- end
- function back(n)
- for i=1,n do
- if turtle.back() then
- distanceTraveled = distanceTraveled + 1
- if orient==0 then
- posX = posX-1
- end
- if orient==2 then
- posX = posX+1
- end
- else
- return false
- end
- end
- return true
- 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 reorderSaplings()
- local saplingsReordered = 0
- for i = 1,16 do
- if i~=SAPLING_SLOT then
- turtle.select(i)
- os.sleep(0.1)
- if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
- while (turtle.getItemCount(i)>0) and (turtle.getItemCount(SAPLING_SLOT)<64) and (turtle.transferTo(SAPLING_SLOT,1)) do
- saplingsReordered = saplingsReordered + 1
- os.sleep(0.1)
- end
- end
- end
- end
- print("reordered "..saplingsReordered.." saplings")
- end
- function dumpExcessSaplings()
- for i = 1,16 do
- if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
- turtle.select(i)
- if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
- turtle.drop()
- end
- end
- end
- end
- function checkSaplings()
- reorderSaplings()
- if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
- print("insufficient saplings, waiting for decaying leaves...")
- os.sleep(60)
- print("refilling saplings from auxiliary storage...")
- while turtle.down() do
- auxiliaryDepth = auxiliaryDepth + 1
- distanceTraveled = distanceTraveled + 1
- end
- turtle.select(SAPLING_TMP_SLOT)
- if turtle.suckDown(SAPLINGS_BUFFER) then
- print("some saplings found in auxiliary storage")
- else
- print("getting more saplings failed")
- end
- reorderSaplings()
- if turtle.getItemCount(SAPLING_SLOT) >= SAPLINGS_BUFFER then
- print("returning excess saplings...")
- dumpExcessSaplings()
- end
- while auxiliaryDepth>0 do
- turtle.up()
- auxiliaryDepth = auxiliaryDepth - 1
- distanceTraveled = distanceTraveled + 1
- end
- end
- if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
- print("Not enough saplings in slot "..SAPLING_SLOT..", waiting for saplings...\n")
- while turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS do
- os.sleep(10)
- end
- end
- end
- function chop() --needs 5 saplings and 80 fuel
- print("chopping tree...")
- local height = 0
- turtle.select(WOOD_SLOT)
- turtle.dig()
- forward(1)
- while turtle.detect() or turtle.detectUp() do
- turtle.select(WOOD_SLOT)
- turtle.dig()
- turtle.digUp()
- if turtle.up() then
- height = height + 1
- distanceTraveled = distanceTraveled + 1
- end
- end
- right()
- turtle.dig() --just in case
- forward(1)
- left()
- --just in case
- while turtle.detectUp() do
- turtle.select(WOOD_SLOT)
- turtle.dig()
- turtle.digUp()
- if turtle.up() then
- height = height + 1
- distanceTraveled = distanceTraveled + 1
- end
- end
- for i=1,height do
- turtle.select(WOOD_SLOT)
- turtle.dig()
- turtle.digDown()
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suckDown()
- turtle.down()
- turtle.suck()
- distanceTraveled = distanceTraveled + 1
- end
- turtle.select(WOOD_SLOT)
- turtle.dig()
- -- replant while repositioning
- print("replanting saplings...")
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suck()
- turtle.select(SAPLING_SLOT)
- turtle.place()
- right()
- back(1)
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suck()
- turtle.select(SAPLING_SLOT)
- turtle.place()
- left()
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suck()
- turtle.select(SAPLING_SLOT)
- turtle.place()
- back(1)
- turtle.select(SAPLING_TMP_SLOT)
- turtle.suck()
- turtle.select(SAPLING_SLOT)
- turtle.place()
- end
- function checkTreeIsReallyThere()
- local result = false
- if turtle.up() then
- os.sleep(2)
- turtle.select(SAPLING_SLOT)
- os.sleep(2)
- if turtle.detect() and (turtle.getItemCount(SAPLING_SLOT)>0) and (not turtle.compare()) then
- result = true
- end
- turtle.down()
- distanceTraveled = distanceTraveled + 2
- else -- couldn't move up, assume there are tree leaves
- result = true
- end
- return result
- end
- function waitForTree() -- needs at least 1 sapling
- print("waiting for tree...")
- local result = false
- local treeDetectedCount = 0
- os.sleep(TREE_GROW_SLEEP_TIME)
- while treeDetectedCount < REPEATED_DETECTION_COUNT do -- because sometimes the turtle does not compare reliably
- turtle.select(SAPLING_SLOT)
- os.sleep(1)
- if turtle.compare() then
- if treeDetectedCount>0 then
- print("correcting invalid tree detection/comparison ("..treeDetectedCount..")")
- end
- treeDetectedCount = 0
- else
- treeDetectedCount = treeDetectedCount + 1
- left()
- os.sleep(REPEATED_DETECTION_TICK)
- right()
- end
- os.sleep(NEXT_DETECTION_TICK)
- end
- os.sleep(1)
- if treeDetectedCount < REPEATED_DETECTION_COUNT then
- print("correct tree detection failed")
- else
- if checkTreeIsReallyThere() then
- print("tree detected")
- result = true
- else
- print("tree detected, but not found")
- end
- end
- return result
- end
- function dumpWood() -- dump everything that is not a sapling
- while back(1) and (posX>(0-MAX_WOOD_STORAGE_DISTANCE)) do
- end
- for i = 1,16 do
- if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
- turtle.select(i)
- if not turtle.compareTo(SAPLING_SLOT) then
- turtle.dropDown()
- if turtle.getItemCount(i)>0 then
- print("cannot dump wood, storage probably full...")
- while turtle.getItemCount(i)>0 do
- turtle.dropDown()
- os.sleep(10)
- end
- end
- end
- end
- end
- forward(0-posX)
- end
- function checkWood()
- print("checking wood amount...")
- local woodAmount = 0
- for i = 1,16 do
- if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
- turtle.select(i)
- if not turtle.compareTo(SAPLING_SLOT) then
- woodAmount = woodAmount + turtle.getItemCount(i)
- end
- end
- end
- if woodAmount > MAX_WOOD then
- print("going to dump "..woodAmount.." of wood...")
- dumpWood()
- else
- print("holding "..woodAmount.." of wood")
- end
- end
- function checkFuel()
- local result = true
- if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then
- turtle.select(FUEL_SLOT)
- turtle.refuel()
- end
- if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then
- result = false
- end
- return result
- end
- function cycle()
- local result = true
- checkSaplings()
- checkFuel()
- while not waitForTree() do
- os.sleep(1)
- end
- chop()
- checkWood()
- cycleCount = cycleCount + 1
- print("cycles finished: "..cycleCount)
- print("distance traveled: "..distanceTraveled)
- print("waiting between cycles...")
- os.sleep(2)
- return result
- end
- if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
- print("Not enough saplings in slot "..SAPLING_SLOT..", exiting.\n")
- return false
- end
- while true do
- if not cycle() then
- print("Cycle unsuccessfull, exiting.\n")
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement