NortWind

aBirchFarm

Mar 18th, 2024 (edited)
1,507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.89 KB | Gaming | 0 0
  1. --{program="aBirchFarm",version="1.00",date="2024-03-26"}
  2. -- Pastebin "fJpMqxKt"
  3. ---------------------------------------
  4. -- Turtle-automated tree farm.
  5. -- based on aTreeFarm by Kaikaku
  6. -- v1.04b", date="2018-01-07
  7. -- Pastebin Qn008fPa
  8. ---------------------------------------
  9.  
  10. ---------------------------------------
  11. ---- PARAMETERS -----------------------
  12. ---------------------------------------
  13. local cVer      ="1.00"
  14. local cMinFuel  = 2*64*15       -- fuel from 2 stacks of planks
  15. local cMaxFuel  = 20000
  16. local cSleep    = 1             -- os.sleep value used to pace operation
  17. local cPlantChance = 1/3        -- how often to plant on bare ground
  18.  
  19. -- inventory slot allocation
  20. local cSlotSapling = 1          -- birch saplings for farming
  21. local cSlotWood = 2             -- birch log for farming
  22. local cSlotBaseChest = 3        -- collection chest
  23. local cSlotPathBlocks = 4       -- for setting up path
  24. local cSlotTorches = 5          -- to illuminate path
  25. local cSlotChest = 16           -- chest for crafty turtle
  26.  
  27. -- 8 path segments (run + corner) make up total path
  28. local cR = 1  -- right turn
  29. local cL = 2  -- left turn
  30. local cX = 3  -- final right turn over collection chest
  31. local pathLengths = { 8,  2,  5,  2,  5,  2,  8,  8}
  32. local pathCorners = {cR, cR, cL, cL, cR, cR, cR, cX}
  33.  
  34. ---------------------------------------
  35. -- BASIC FUNCTIONS FOR TURTLE CONTROL -
  36. ---------------------------------------
  37. local function safeUp()
  38.   while not turtle.up()
  39.   do
  40.     os.sleep(cSleep)
  41.     turtle.digUp()           -- possible that leaves may grow above.
  42.   end  
  43. end
  44.  
  45. local function safeDown()
  46.   while not turtle.down()
  47.   do
  48.     os.sleep(cSleep)
  49.     turtle.digDown()         -- possible that leaves may grow above.
  50.   end  
  51. end
  52.  
  53. -- calling with no parameter moves one block forward
  54. local function safeForward(n)
  55.   if n == nil
  56.   then
  57.     turtle.suck()
  58.     while not turtle.forward() do os.sleep(cSleep) end  
  59.   else
  60.     for i = 1, n do
  61.       turtle.suck()
  62.       while not turtle.forward() do os.sleep(cSleep) end    
  63.     end
  64.   end
  65. end
  66.  
  67. local function safeDig()     -- digs forward
  68.   turtle.suck()
  69.   os.sleep(cSleep)
  70.   turtle.dig()
  71. end
  72.  
  73. local function safeDigDown()
  74.   turtle.suckDown()          -- might be a sapling or stick
  75.   os.sleep(cSleep)
  76.   turtle.digDown()           -- might not be anything to dig
  77. end
  78.  
  79. local function safeDigUp()
  80.   turtle.suckUp()            -- remove drek on top
  81.   os.sleep(cSleep)
  82.   turtle.digUp()             -- might not be anything to dig
  83. end
  84.  
  85. local function safeTurnRight()
  86.   while not turtle.turnRight() do os.sleep(cSleep) end  
  87. end
  88.  
  89. local function safeTurnLeft()
  90.   while not turtle.turnLeft() do os.sleep(cSleep) end  
  91. end
  92.  
  93. ---------------------------------------
  94. ---- functions ------------------------
  95. ---------------------------------------
  96. -- wait for enter to be pressed
  97. local function waitEnter()
  98.   write("Press enter to start:")
  99.   read()
  100. end
  101.  
  102. -- make fuel for turtle to move
  103. local function craftFuel()
  104.   if turtle.getItemCount(cSlotChest) == 1   -- only refuel if chest in slot cSlotChest
  105.   then    
  106.     if turtle.getItemCount(cSlotWood) > 1   -- enough materials to refuel in slot 2?
  107.     then  
  108.       -- store away stuff
  109.       turtle.select(cSlotChest)
  110.       while not turtle.placeUp() do end
  111.       for i = 1, 15 do
  112.         turtle.select(i)
  113.         if i == cSlotWood
  114.         then
  115.           turtle.dropUp(1)   -- to keep one log in slot 2 when we restore
  116.         else
  117.           turtle.dropUp()    -- put entire stack in chest
  118.         end
  119.       end  
  120.  
  121.       -- a log = 15 fuel and a plank = 15 fuel, so craft planks from logs.
  122.       turtle.craft()         -- craft planks, 1 log -> 4 planks, max 63*4*15 = 3780 fuel
  123.  
  124.       -- now empty of everything but planks, so refuel all
  125.       for i = 1, 16 do
  126.           turtle.select(i)
  127.           turtle.refuel(64)
  128.       end
  129.  
  130.       print("New fuel level ("..turtle.getFuelLevel().."/"..cMinFuel..")")
  131.       turtle.select(cSlotSapling)      -- replace contents
  132.       while turtle.suckUp() do end
  133.  
  134.       turtle.select(cSlotChest)        -- retrieve chest
  135.       safeDigUp()
  136.       turtle.select(cSlotSapling)
  137.     else   
  138.       print("Need chest in slot 15.")
  139.     end  
  140.   end
  141. end
  142.  
  143. -- check fuel in turtle, attempt to replenish if low
  144. local function checkRefuel()  
  145.   if turtle.getFuelLevel() < cMaxFuel * 0.8
  146.   then
  147.       print("Fuel level is low, "..turtle.getFuelLevel()..".")
  148.       craftFuel()  
  149.   else
  150.     print("Fuel level ok, "..turtle.getFuelLevel()..".")
  151.   end
  152. end
  153.  
  154. ---------------------------------------
  155. -- plant a sapling
  156. ---------------------------------------
  157. local function plantTree()
  158.   -- assume facing planting site
  159.   turtle.select(cSlotSapling)
  160.   turtle.suck()              -- vaccuum up sticks or loose saplings
  161.   -- check for enough saplings
  162.   if turtle.getItemCount(cSlotSapling) > 1
  163.   then
  164.     turtle.place()           -- plant sapling
  165.   end
  166. end
  167.  
  168. ---------------------------------------
  169. ---- cut down a tree, and replant -----
  170. ---------------------------------------
  171. local function cutTree()
  172.   -- assume facing bottom of tree
  173.   turtle.select(cSlotSapling)
  174.   -- get into tree column
  175.   safeDig()                  -- cut into trunk of tree
  176.   safeForward()              -- move into trunk of tree
  177.  
  178.   for i = 1, 7 do            -- cut and go up
  179.     safeDigUp()  
  180.     safeUp()
  181.     turtle.select(cSlotWood)
  182.     if turtle.detect() and not turtle.compare()  -- if not wood might be foliage
  183.     then
  184.       turtle.select(cSlotSapling)
  185.       safeDig()                                  -- cut foliage on the other side of trunk
  186.     else
  187.       turtle.select(cSlotSapling)                -- restore active slot to sapling
  188.     end
  189.   end;
  190.  
  191.   safeTurnRight()            -- turn back toward path
  192.   safeTurnRight()
  193.   safeDig()                  -- cut possible foliage
  194.   safeForward()              -- move over path
  195.  
  196.   for i = 1, 7 do            -- down to path, cutting possible foliage
  197.     safeDigDown()
  198.     safeDown()
  199.   end;
  200.  
  201.   safeTurnRight()            -- retore orientation
  202.   safeTurnRight()
  203.  
  204.   plantTree()                -- plant replacement tree
  205. end
  206.  
  207. -- deliver all unneeded inventory to collection chest
  208. local function emptyTurtle()
  209.   turtle.select(cSlotWood)
  210.     if turtle.getItemCount(cSlotWood) > 1
  211.   then                       -- drop all but 1 log
  212.       turtle.dropDown(math.max(turtle.getItemCount(cSlotWood) - 1, 0))
  213.     end
  214.   for i = 3, 15 do           -- drop all other slots from wood to chest
  215.     turtle.select(i)
  216.     turtle.dropDown()
  217.   end
  218.   turtle.select(cSlotSapling)          -- restore slot selection to slotSapling
  219.   os.sleep(cSleep)
  220. end
  221.  
  222. -- Test for log ahead
  223. local function woodAhead()
  224.   local woodFound = false
  225.   turtle.select(cSlotWood)
  226.   woodFound, er = turtle.compare()
  227.   turtle.select(cSlotSapling)
  228.   return woodFound
  229. end
  230.  
  231. -- Test for planted sapling ahead
  232. local function saplingAhead()
  233.   local saplingFound = false
  234.   turtle.select(cSlotSapling)
  235.   saplingFound, er = turtle.compare()
  236.   return saplingFound
  237. end
  238.  
  239. -- Move onto next path block, check two non-path locations.
  240. local function pathForward()
  241.   safeForward()
  242.   safeTurnLeft()             -- check lefthand side
  243.   turtle.suck()
  244.   if woodAhead() then
  245.     cutTree()
  246.   elseif not saplingAhead() then
  247.     if math.random() < cPlantChance then
  248.       plantTree()      
  249.     end
  250.   end
  251.  
  252.   safeTurnRight()
  253.   safeTurnRight()            -- check righhand side
  254.   turtle.suck()
  255.   if woodAhead() then
  256.     cutTree()
  257.   elseif not saplingAhead() then
  258.     if math.random() < cPlantChance then
  259.       plantTree()      
  260.     end
  261.   end
  262.  
  263.   for i = 1, 3 do
  264.     turtle.turnRight()       -- facing backwards, left, and finally forward
  265.     turtle.suck()  
  266.   end
  267. end
  268.  
  269. -- Move into path corner, check two non-path locations.
  270. local function pathRight()
  271.   safeForward()                        -- move onto corner
  272.  
  273.   if woodAhead() then                  -- cheak ahead
  274.     cutTree()
  275.   elseif not saplingAhead() then
  276.     plantTree()
  277.   end
  278.  
  279.   safeTurnLeft()                       -- check left
  280.   if woodAhead() then
  281.     cutTree()
  282.   elseif not saplingAhead() then
  283.     plantTree()
  284.   end
  285.  
  286.   safeTurnLeft()                       -- clean behind path
  287.   turtle.suck()
  288.   safeTurnLeft()                       -- clean forward path
  289.   turtle.suck()
  290. end
  291.  
  292. -- Move into path corner, check two non-path locations.
  293. local function pathLeft()
  294.   safeForward()
  295.  
  296.   if woodAhead() then
  297.     cutTree()
  298.   elseif not saplingAhead() then
  299.     plantTree()
  300.   end
  301.  
  302.   safeTurnRight()
  303.   if woodAhead() then
  304.     cutTree()
  305.   elseif not saplingAhead() then
  306.     plantTree()
  307.   end
  308.  
  309.   safeTurnRight()                       -- clean behind path
  310.   turtle.suck()
  311.   safeTurnRight()                       -- clean forward path
  312.   turtle.suck()
  313. end
  314.  
  315. -- Move into final path corner, no planting
  316. local function pathFinal()
  317.   safeForward()
  318.   for i = 1, 3 do            -- three lefts make a right
  319.     safeTurnLeft()
  320.     turtle.suck()
  321.   end
  322.   turtle.suck()
  323.   -- do not plant to the left of collection chest, reserved for redstone control.
  324.   -- do not plant behind the collection chest, reserved for turtle access.
  325. end
  326.  
  327. -- pause at end of loop if redstone signal at left
  328. local function waitRedstone()
  329.   if rs.getInput("left") then
  330.     print("Pause while redstone signal active.")
  331.     while rs.getInput("left") do      
  332.       os.sleep(5)
  333.     end  
  334.     print("Resuming after redstone signal.")
  335.   end
  336. end
  337.  
  338.   ---------------------------------------
  339.   ---- birch tree farm ------------------------
  340.   ---------------------------------------
  341. local function doTreeFarm()
  342.   while true do    
  343.    
  344.     for i=1, 8 do                      -- 8 segments to path, one segment is a run and a corner
  345.       for j=1,pathLengths[i] do
  346.         pathForward()                  -- do run
  347.       end
  348.  
  349.       if pathCorners[i] == cR then     -- do corner
  350.         pathRight()
  351.       elseif pathCorners[i] == cL then
  352.         pathLeft()  
  353.       else
  354.         pathFinal()                    -- position over chest, facing next path segment
  355.       end
  356.     end
  357.  
  358.     checkRefuel()                      -- add fuel if needed
  359.     emptyTurtle()                      -- put inventory into collection chest
  360.     waitRedstone()                     -- pause if requested
  361.   end
  362. end
  363.  
  364. ---------------------------------------
  365. ---- setup birch tree farm
  366. ---------------------------------------
  367. local function doSetup()
  368.   safeTurnRight()            -- move from chunk corner to path corner
  369.   safeForward(3)
  370.   safeTurnLeft()
  371.   safeForward(3)
  372.  
  373.   safeDigDown()              -- place collection chest
  374.   turtle.select(cSlotBaseChest)
  375.   turtle.placeDown()
  376.  
  377.   for i=1, 8 do              -- there are 8 segments of the farm path
  378.     turtle.select(cSlotPathBlocks)
  379.     for j = 1, pathLengths[i] do
  380.       safeForward()
  381.       safeDigDown()
  382.       turtle.placeDown()
  383.     end
  384.  
  385.     safeForward()            -- move onto next corner
  386.     if pathCorners[i] ~= cX  -- last corner has collection chest already placed
  387.     then
  388.       safeDigDown()
  389.       turtle.placeDown()     -- place path at corner
  390.     end
  391.  
  392.     turtle.select(cSlotTorches)
  393.     if pathCorners[i] == cR or pathCorners[i] == cX then
  394.       safeForward()          -- off of the path to place torch
  395.       safeTurnLeft()
  396.       turtle.place()
  397.       safeTurnLeft()
  398.       safeForward()          -- back onto corner
  399.       safeTurnLeft()         -- three lefts make a right
  400.     elseif pathCorners[i] == cL then
  401.       safeForward()          -- off of the path to place torch
  402.       safeTurnRight()
  403.       turtle.place()
  404.       safeTurnRight()
  405.       safeForward()          -- back onto corner
  406.       safeTurnRight()        -- three rights make a left
  407.     end
  408.   end
  409. end
  410.  
  411. ---------------------------------------
  412. ---- main -----------------------------
  413. ---------------------------------------
  414. term.clear()
  415. term.setCursorPos(1,1)
  416. local saplingsNeeded = 20
  417. if turtle.getFuelLevel() < cMinFuel
  418.   then
  419.       print("Fuel level "..turtle.getFuelLevel().." is too low.")
  420.     print("Use command \"refuel all\" with 3 stacks of planks.")     -- need to add fuel
  421.     return
  422.   end
  423.  
  424. if arg[1] == nil or arg[1] == "resume"
  425. then                         -- run farm.
  426.   print("+-------------------------------------+")
  427.   print("| aBirchFarm "..cVer..", by NortWind        |")
  428.   print("+-------------------------------------+")
  429.   print("| Running the farm:                   |")
  430.   print("|   Felling turtle sits above chest   |")
  431.   print("|   (after running setup). Turtle     |")
  432.   print("|   needs some initial fuel to start. |")
  433.   print("| Turtle inventory:                   |")
  434.   if arg[1] == "resume"
  435.   then
  436.     print("|   slot  1: birch saplings    (1+x)  |")
  437.     saplingsNeeded = 1
  438.   else
  439.     print("|   slot  1: birch saplings    (20+x) |")
  440.   end
  441.   print("|   slot  2: birch log         (1+x)  |")
  442.   print("|   slot 16: chest             (1)    |")
  443.   print("+-------------------------------------+")
  444.   waitEnter()
  445.  
  446.   if turtle.getItemCount(1)<saplingsNeeded or turtle.getItemCount(2)==0 or turtle.getItemCount(16)~=1
  447.   then
  448.     print("Inventory not correct.")
  449.   else
  450.     doTreeFarm()
  451.   end
  452.  
  453. else                         -- setup farm
  454.   print("+-------------------------------------+")
  455.   print("| aBirchFarm "..cVer..", by NortWind        |")
  456.   print("+-------------------------------------+")
  457.   print("| Farm set-up: Place crafty felling   |")
  458.   print("|   turtle down in the south west     |")
  459.   print("|   corner of a chunk facing north.   |")
  460.   print("| Materials for auto set-up:          |")
  461.   print("|   slot 3: chest       (1)           |")
  462.   print("|   slot 4: path stones (47)          |")
  463.   print("|   slot 5: torches     (8)           |")
  464.   print("+-------------------------------------+")
  465.   waitEnter()
  466.   if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)~=47 or turtle.getItemCount(5)~=8
  467.   then
  468.     print("Inventory not correct.")
  469.   else  
  470.     doSetup()
  471.   end
  472.  
  473. end
  474.  
Tags: minecraft
Add Comment
Please, Sign In to add comment