Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --{program="aBirchFarm",version="1.00",date="2024-03-26"}
- -- Pastebin "fJpMqxKt"
- ---------------------------------------
- -- Turtle-automated tree farm.
- -- based on aTreeFarm by Kaikaku
- -- v1.04b", date="2018-01-07
- -- Pastebin Qn008fPa
- ---------------------------------------
- ---------------------------------------
- ---- PARAMETERS -----------------------
- ---------------------------------------
- local cVer ="1.00"
- local cMinFuel = 2*64*15 -- fuel from 2 stacks of planks
- local cMaxFuel = 20000
- local cSleep = 1 -- os.sleep value used to pace operation
- local cPlantChance = 1/3 -- how often to plant on bare ground
- -- inventory slot allocation
- local cSlotSapling = 1 -- birch saplings for farming
- local cSlotWood = 2 -- birch log for farming
- local cSlotBaseChest = 3 -- collection chest
- local cSlotPathBlocks = 4 -- for setting up path
- local cSlotTorches = 5 -- to illuminate path
- local cSlotChest = 16 -- chest for crafty turtle
- -- 8 path segments (run + corner) make up total path
- local cR = 1 -- right turn
- local cL = 2 -- left turn
- local cX = 3 -- final right turn over collection chest
- local pathLengths = { 8, 2, 5, 2, 5, 2, 8, 8}
- local pathCorners = {cR, cR, cL, cL, cR, cR, cR, cX}
- ---------------------------------------
- -- BASIC FUNCTIONS FOR TURTLE CONTROL -
- ---------------------------------------
- local function safeUp()
- while not turtle.up()
- do
- os.sleep(cSleep)
- turtle.digUp() -- possible that leaves may grow above.
- end
- end
- local function safeDown()
- while not turtle.down()
- do
- os.sleep(cSleep)
- turtle.digDown() -- possible that leaves may grow above.
- end
- end
- -- calling with no parameter moves one block forward
- local function safeForward(n)
- if n == nil
- then
- turtle.suck()
- while not turtle.forward() do os.sleep(cSleep) end
- else
- for i = 1, n do
- turtle.suck()
- while not turtle.forward() do os.sleep(cSleep) end
- end
- end
- end
- local function safeDig() -- digs forward
- turtle.suck()
- os.sleep(cSleep)
- turtle.dig()
- end
- local function safeDigDown()
- turtle.suckDown() -- might be a sapling or stick
- os.sleep(cSleep)
- turtle.digDown() -- might not be anything to dig
- end
- local function safeDigUp()
- turtle.suckUp() -- remove drek on top
- os.sleep(cSleep)
- turtle.digUp() -- might not be anything to dig
- end
- local function safeTurnRight()
- while not turtle.turnRight() do os.sleep(cSleep) end
- end
- local function safeTurnLeft()
- while not turtle.turnLeft() do os.sleep(cSleep) end
- end
- ---------------------------------------
- ---- functions ------------------------
- ---------------------------------------
- -- wait for enter to be pressed
- local function waitEnter()
- write("Press enter to start:")
- read()
- end
- -- make fuel for turtle to move
- local function craftFuel()
- if turtle.getItemCount(cSlotChest) == 1 -- only refuel if chest in slot cSlotChest
- then
- if turtle.getItemCount(cSlotWood) > 1 -- enough materials to refuel in slot 2?
- then
- -- store away stuff
- turtle.select(cSlotChest)
- while not turtle.placeUp() do end
- for i = 1, 15 do
- turtle.select(i)
- if i == cSlotWood
- then
- turtle.dropUp(1) -- to keep one log in slot 2 when we restore
- else
- turtle.dropUp() -- put entire stack in chest
- end
- end
- -- a log = 15 fuel and a plank = 15 fuel, so craft planks from logs.
- turtle.craft() -- craft planks, 1 log -> 4 planks, max 63*4*15 = 3780 fuel
- -- now empty of everything but planks, so refuel all
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel(64)
- end
- print("New fuel level ("..turtle.getFuelLevel().."/"..cMinFuel..")")
- turtle.select(cSlotSapling) -- replace contents
- while turtle.suckUp() do end
- turtle.select(cSlotChest) -- retrieve chest
- safeDigUp()
- turtle.select(cSlotSapling)
- else
- print("Need chest in slot 15.")
- end
- end
- end
- -- check fuel in turtle, attempt to replenish if low
- local function checkRefuel()
- if turtle.getFuelLevel() < cMaxFuel * 0.8
- then
- print("Fuel level is low, "..turtle.getFuelLevel()..".")
- craftFuel()
- else
- print("Fuel level ok, "..turtle.getFuelLevel()..".")
- end
- end
- ---------------------------------------
- -- plant a sapling
- ---------------------------------------
- local function plantTree()
- -- assume facing planting site
- turtle.select(cSlotSapling)
- turtle.suck() -- vaccuum up sticks or loose saplings
- -- check for enough saplings
- if turtle.getItemCount(cSlotSapling) > 1
- then
- turtle.place() -- plant sapling
- end
- end
- ---------------------------------------
- ---- cut down a tree, and replant -----
- ---------------------------------------
- local function cutTree()
- -- assume facing bottom of tree
- turtle.select(cSlotSapling)
- -- get into tree column
- safeDig() -- cut into trunk of tree
- safeForward() -- move into trunk of tree
- for i = 1, 7 do -- cut and go up
- safeDigUp()
- safeUp()
- turtle.select(cSlotWood)
- if turtle.detect() and not turtle.compare() -- if not wood might be foliage
- then
- turtle.select(cSlotSapling)
- safeDig() -- cut foliage on the other side of trunk
- else
- turtle.select(cSlotSapling) -- restore active slot to sapling
- end
- end;
- safeTurnRight() -- turn back toward path
- safeTurnRight()
- safeDig() -- cut possible foliage
- safeForward() -- move over path
- for i = 1, 7 do -- down to path, cutting possible foliage
- safeDigDown()
- safeDown()
- end;
- safeTurnRight() -- retore orientation
- safeTurnRight()
- plantTree() -- plant replacement tree
- end
- -- deliver all unneeded inventory to collection chest
- local function emptyTurtle()
- turtle.select(cSlotWood)
- if turtle.getItemCount(cSlotWood) > 1
- then -- drop all but 1 log
- turtle.dropDown(math.max(turtle.getItemCount(cSlotWood) - 1, 0))
- end
- for i = 3, 15 do -- drop all other slots from wood to chest
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(cSlotSapling) -- restore slot selection to slotSapling
- os.sleep(cSleep)
- end
- -- Test for log ahead
- local function woodAhead()
- local woodFound = false
- turtle.select(cSlotWood)
- woodFound, er = turtle.compare()
- turtle.select(cSlotSapling)
- return woodFound
- end
- -- Test for planted sapling ahead
- local function saplingAhead()
- local saplingFound = false
- turtle.select(cSlotSapling)
- saplingFound, er = turtle.compare()
- return saplingFound
- end
- -- Move onto next path block, check two non-path locations.
- local function pathForward()
- safeForward()
- safeTurnLeft() -- check lefthand side
- turtle.suck()
- if woodAhead() then
- cutTree()
- elseif not saplingAhead() then
- if math.random() < cPlantChance then
- plantTree()
- end
- end
- safeTurnRight()
- safeTurnRight() -- check righhand side
- turtle.suck()
- if woodAhead() then
- cutTree()
- elseif not saplingAhead() then
- if math.random() < cPlantChance then
- plantTree()
- end
- end
- for i = 1, 3 do
- turtle.turnRight() -- facing backwards, left, and finally forward
- turtle.suck()
- end
- end
- -- Move into path corner, check two non-path locations.
- local function pathRight()
- safeForward() -- move onto corner
- if woodAhead() then -- cheak ahead
- cutTree()
- elseif not saplingAhead() then
- plantTree()
- end
- safeTurnLeft() -- check left
- if woodAhead() then
- cutTree()
- elseif not saplingAhead() then
- plantTree()
- end
- safeTurnLeft() -- clean behind path
- turtle.suck()
- safeTurnLeft() -- clean forward path
- turtle.suck()
- end
- -- Move into path corner, check two non-path locations.
- local function pathLeft()
- safeForward()
- if woodAhead() then
- cutTree()
- elseif not saplingAhead() then
- plantTree()
- end
- safeTurnRight()
- if woodAhead() then
- cutTree()
- elseif not saplingAhead() then
- plantTree()
- end
- safeTurnRight() -- clean behind path
- turtle.suck()
- safeTurnRight() -- clean forward path
- turtle.suck()
- end
- -- Move into final path corner, no planting
- local function pathFinal()
- safeForward()
- for i = 1, 3 do -- three lefts make a right
- safeTurnLeft()
- turtle.suck()
- end
- turtle.suck()
- -- do not plant to the left of collection chest, reserved for redstone control.
- -- do not plant behind the collection chest, reserved for turtle access.
- end
- -- pause at end of loop if redstone signal at left
- local function waitRedstone()
- if rs.getInput("left") then
- print("Pause while redstone signal active.")
- while rs.getInput("left") do
- os.sleep(5)
- end
- print("Resuming after redstone signal.")
- end
- end
- ---------------------------------------
- ---- birch tree farm ------------------------
- ---------------------------------------
- local function doTreeFarm()
- while true do
- for i=1, 8 do -- 8 segments to path, one segment is a run and a corner
- for j=1,pathLengths[i] do
- pathForward() -- do run
- end
- if pathCorners[i] == cR then -- do corner
- pathRight()
- elseif pathCorners[i] == cL then
- pathLeft()
- else
- pathFinal() -- position over chest, facing next path segment
- end
- end
- checkRefuel() -- add fuel if needed
- emptyTurtle() -- put inventory into collection chest
- waitRedstone() -- pause if requested
- end
- end
- ---------------------------------------
- ---- setup birch tree farm
- ---------------------------------------
- local function doSetup()
- safeTurnRight() -- move from chunk corner to path corner
- safeForward(3)
- safeTurnLeft()
- safeForward(3)
- safeDigDown() -- place collection chest
- turtle.select(cSlotBaseChest)
- turtle.placeDown()
- for i=1, 8 do -- there are 8 segments of the farm path
- turtle.select(cSlotPathBlocks)
- for j = 1, pathLengths[i] do
- safeForward()
- safeDigDown()
- turtle.placeDown()
- end
- safeForward() -- move onto next corner
- if pathCorners[i] ~= cX -- last corner has collection chest already placed
- then
- safeDigDown()
- turtle.placeDown() -- place path at corner
- end
- turtle.select(cSlotTorches)
- if pathCorners[i] == cR or pathCorners[i] == cX then
- safeForward() -- off of the path to place torch
- safeTurnLeft()
- turtle.place()
- safeTurnLeft()
- safeForward() -- back onto corner
- safeTurnLeft() -- three lefts make a right
- elseif pathCorners[i] == cL then
- safeForward() -- off of the path to place torch
- safeTurnRight()
- turtle.place()
- safeTurnRight()
- safeForward() -- back onto corner
- safeTurnRight() -- three rights make a left
- end
- end
- end
- ---------------------------------------
- ---- main -----------------------------
- ---------------------------------------
- term.clear()
- term.setCursorPos(1,1)
- local saplingsNeeded = 20
- if turtle.getFuelLevel() < cMinFuel
- then
- print("Fuel level "..turtle.getFuelLevel().." is too low.")
- print("Use command \"refuel all\" with 3 stacks of planks.") -- need to add fuel
- return
- end
- if arg[1] == nil or arg[1] == "resume"
- then -- run farm.
- print("+-------------------------------------+")
- print("| aBirchFarm "..cVer..", by NortWind |")
- print("+-------------------------------------+")
- print("| Running the farm: |")
- print("| Felling turtle sits above chest |")
- print("| (after running setup). Turtle |")
- print("| needs some initial fuel to start. |")
- print("| Turtle inventory: |")
- if arg[1] == "resume"
- then
- print("| slot 1: birch saplings (1+x) |")
- saplingsNeeded = 1
- else
- print("| slot 1: birch saplings (20+x) |")
- end
- print("| slot 2: birch log (1+x) |")
- print("| slot 16: chest (1) |")
- print("+-------------------------------------+")
- waitEnter()
- if turtle.getItemCount(1)<saplingsNeeded or turtle.getItemCount(2)==0 or turtle.getItemCount(16)~=1
- then
- print("Inventory not correct.")
- else
- doTreeFarm()
- end
- else -- setup farm
- print("+-------------------------------------+")
- print("| aBirchFarm "..cVer..", by NortWind |")
- print("+-------------------------------------+")
- print("| Farm set-up: Place crafty felling |")
- print("| turtle down in the south west |")
- print("| corner of a chunk facing north. |")
- print("| Materials for auto set-up: |")
- print("| slot 3: chest (1) |")
- print("| slot 4: path stones (47) |")
- print("| slot 5: torches (8) |")
- print("+-------------------------------------+")
- waitEnter()
- if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)~=47 or turtle.getItemCount(5)~=8
- then
- print("Inventory not correct.")
- else
- doSetup()
- end
- end
Add Comment
Please, Sign In to add comment