SHARE
TWEET

edited kaikaku treefarm

a guest May 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --{program="aTreeFarm",version="1.04b",date="2018-01-07"}
  2. ---------------------------------------
  3. -- aTreeFarm           by Kaikaku
  4. -- 2018-01-07, v1.04   bugfix (turtle start position)
  5. -- 2018-01-05, v1.03   bugfix (turtle digged drop chest)
  6. -- 2017-12-02, v1.02   start with craft fuel and empty to allow tHome
  7. -- 2015-01-31, v1.01   fixed initial refuelling
  8. -- 2015-01-31, v1.00   finalized UI + counter
  9. -- 2015-01-30, v0.80   auto set-up option
  10. -- 2015-01-26, v0.70   preparing for video
  11. -- 2014-01-12, v0.61   replant limited tries
  12. -- 2014-01-04, v0.60   redstone stop
  13. -- 2013-12-15, v0.51   initial
  14. ---------------------------------------
  15.  
  16.  
  17. ---------------------------------------
  18. ---- DESCRIPTION ----------------------
  19. ---------------------------------------
  20. -- Turtle-automated tree farm.
  21. -- Details see information during program
  22. --   execution or YouTube video.
  23.  
  24.  
  25. ---------------------------------------
  26. ---- PARAMETERS -----------------------
  27. ---------------------------------------
  28. local cVersion  ="1.04"
  29. local cPrgName  ="aTreeFarm"
  30. local cMinFuel  = 960*2      -- 2 stacks of planks
  31.  
  32. local minRandomCheckSapling=0.1 -- below this will check replant
  33. local actRandomCheckSapling=minRandomCheckSapling*2
  34. local cIncreaseCheckSapling_Sapling=0.02
  35. local cIncreaseCheckSapling_Stub=0.04
  36. local cMaxCheckSapling=0.6
  37. local strC="tReeTreESdig!diG;-)FaRmKaIKAKUudIgYdIgyTreEAndsOrRygUYsd"
  38.  
  39. local cSlotChest=16           -- chest for crafty turtle
  40. local cCraftRefuelMaxItems=32 -- use how many logs to refuel at max
  41. local cSlotRefuel=15          -- where to put fuel
  42. local cExtraDigUp=1           -- go how many extra levels to reach jungle branches
  43. local cLoopEnd=56             -- one loop
  44. local cWaitingTime=20         -- if redstone signal in back is on
  45.  
  46. ---------------------------------------
  47. ---- VARIABLES ------------------------
  48. ---------------------------------------
  49. local strC_now=""
  50. local strC_next=""
  51.  
  52. local tmpResult=""
  53. local blnAskForParameters=true
  54. local blnShowUsage=false
  55. local blnAutoSetup=false
  56. local strSimpleCheck="Press enter to start:"
  57. local intCounter=0
  58. local maxCounter=0
  59.  
  60. ---------------------------------------
  61. ---- tArgs ----------------------------
  62. ---------------------------------------
  63. local tArgs = {...}
  64. if #tArgs >= 1 then -- no error check
  65.   blnAskForParameters=false
  66.   if tArgs[1]=="help" then blnShowUsage=true end
  67.   if tArgs[1]=="setup" then blnAutoSetup=true end
  68.   if tArgs[1]=="set-up" then blnAutoSetup=true end
  69.   if tonumber(tArgs[1])~=nil then
  70.     maxCounter=tonumber(tArgs[1])
  71.   end
  72. end
  73.  
  74. if blnShowUsage then
  75.   print("+-------------------------------------+")
  76.   print("  "..cPrgName..", by Kaikaku")
  77.   print("+-------------------------------------+")
  78.   print("Usage: aTreeFarm [setup/set-up]")
  79.   print("   or: aTreeFarm [maxCounter]")
  80.   print("setup or set-up:")
  81.   print("   Will start auto set-up")
  82.   print("maxCounter:")
  83.   print("   0=will farm infinitely")
  84.   print("   x=will farm x rounds")
  85.   print("More details on YouTube ;)")
  86.   return  
  87. end
  88.  
  89. ---------------------------------------
  90. -- BASIC FUNCTIONS FOR TURTLE CONTROL -
  91. ---------------------------------------
  92. local function gf(n)
  93.   if n==nil then n=1 end
  94.   for i=1,n,1 do while not turtle.forward() do end end
  95. end
  96. local function gb(n)
  97.   if n==nil then n=1 end
  98.   for i=1,n,1 do while not turtle.back() do end end
  99. end
  100. local function gu(n)
  101.   if n==nil then n=1 end
  102.   for i=1,n,1 do while not turtle.up() do end end
  103. end
  104. local function gd(n)
  105.   if n==nil then n=1 end
  106.   for i=1,n,1 do while not turtle.down() do end end
  107. end
  108. local function gl(n)
  109.   if n==nil then n=1 end
  110.   for i=1,n,1 do while not turtle.turnLeft() do end end
  111. end
  112. local function gr(n)
  113.   if n==nil then n=1 end
  114.   for i=1,n,1 do while not turtle.turnRight() do end end
  115. end
  116. local function pf(n)
  117.   -- moves backwards if n>1
  118.   if n==nil then n=1 end
  119.   for i=1,n,1 do if i~=1 then gb() end turtle.place() end
  120. end
  121. local function pu()  turtle.placeUp()    end
  122. local function pd()  turtle.placeDown()  end
  123. local function df()  return turtle.dig() end
  124. local function du()  turtle.digUp()      end
  125. local function dd()  turtle.digDown()    end
  126. local function sf()  turtle.suck()       end
  127. local function su()  turtle.suckUp()     end
  128. local function sd(n)
  129.   if n==nil then
  130.     while turtle.suckDown() do end
  131.   else
  132.     for i=1,n do
  133.       turtle.suckDown()
  134.     end
  135.   end
  136. end
  137. local function Df()  turtle.drop()      end
  138. local function Du(n) if n==nil then n=64 end turtle.dropUp(n) end
  139. local function Dd(n) if n==nil then n=64 end turtle.dropDown(n) end
  140. local function ss(s) turtle.select(s)   end
  141.  
  142. local function askForInputText(textt)
  143.   local at=""
  144.   -- check prompting texts
  145.   if textt==nil then textt="Enter text:" end
  146.  
  147.   -- ask for input
  148.   write(textt)
  149.   at=read()
  150.   return at
  151. end
  152.  
  153. local function checkFuel()
  154.   local tmp=turtle.getFuelLevel()
  155.   return tmp
  156. end
  157.  
  158. function checkRefuel(minFuel, slotFuel)
  159.   if slotFuel==nil then slotFuel=16 end
  160.   if minFuel==nil then minFuel=1000 end
  161.   local tmpFuel=0 tmpFuel2=0
  162.   local tmpItems=65 --turtle.getItemCount(slotFuel)
  163.   local cSleep=5
  164.  
  165.   -- step 1 check if more fuel is required
  166.   tmpFuel=turtle.getFuelLevel()
  167.   tmpFuel2=tmpFuel-1 -- triggers print at least once
  168.   if tmpFuel<minFuel then
  169.     ss(slotFuel)
  170.     -- step 2 refuel loop
  171.     while tmpFuel<minFuel do
  172.       -- step 2.1 need to update fuel level?
  173.       if tmpFuel2~=tmpFuel then --tmpItems~=turtle.getItemCount(slotFuel) then
  174.         -- fuel still too low and there have been items consumed
  175.         print("Need more fuel ("..tmpFuel.."/"..minFuel..") in slot "..slotFuel)
  176.       end
  177.       -- step 2.2 try to refuel
  178.       if tmpItems>0 then
  179.         -- try to refuel this items
  180.         turtle.refuel()
  181.       else
  182.         os.sleep(cSleep)       
  183.       end
  184.       -- step 2.3 update variables
  185.       tmpItems=turtle.getItemCount(slotFuel)
  186.       tmpFuel2=tmpFuel
  187.       tmpFuel=turtle.getFuelLevel()
  188.     end
  189.   end
  190.   -- step 3 either no need to refuel
  191.   --        or successfully refuelled
  192.   print("Fuel level ok  ("..tmpFuel.."/"..minFuel..")")
  193. end
  194.  
  195.  
  196. ---------------------------------------
  197. ---- functions ------------------------
  198. ---------------------------------------
  199.  
  200. local function cutTree()
  201. local tmpExtraDigUp=cExtraDigUp
  202.  
  203.   ---- assumptions
  204.   -- turtle faces trunk one block below bottom
  205.   ---- variables
  206.   local intUpCount = 0
  207.   local intFace = 0 -- -1=left, 1=right
  208.   local blnDigSomething=false
  209.  
  210.   term.write("  cutting tree: ")
  211.  
  212.   -- get into tree column
  213.   df()
  214.   while not turtle.forward() do df() end
  215.   gr() df() gl() df() gl() df()
  216.   local intFace=-1
  217.  
  218.   -- cut and go up
  219.   repeat
  220.     blnDigSomething=false
  221.     du()
  222.     while not turtle.up() do du() end
  223.     blnDigSomething=df() or blnDigSomething
  224.     if intFace==-1 then
  225.       gr()
  226.       blnDigSomething=df() or blnDigSomething
  227.       gr()
  228.     elseif intFace==1 then
  229.       gl()
  230.       blnDigSomething=df() or blnDigSomething
  231.       gl()
  232.     end
  233.     intFace=intFace * -1
  234.     blnDigSomething=df() or blnDigSomething
  235.     intUpCount = intUpCount +1
  236.     term.write(".")
  237.    
  238.     -- check for 2 conditions
  239.     -- either
  240.     -- 1) nothing above the turtle
  241.     -- or
  242.     -- 2) nothing dig on the other columns blnDigSomething
  243.     turtle.sleep(2)
  244.     if not (turtle.detectUp() or blnDigSomething) then
  245.       tmpExtraDigUp=tmpExtraDigUp-1  
  246.     else
  247.       tmpExtraDigUp=cExtraDigUp -- restore it  
  248.     end    
  249.   until tmpExtraDigUp<0 --not (turtle.detectUp() or blnDigSomething) ----- NOT kai_2
  250.  
  251.   -- go off tree column  
  252.   if intFace==-1 then
  253.     gl()
  254.   elseif intFace==1 then
  255.     gr()
  256.   end
  257.   df()
  258.   while not turtle.forward() do df() end
  259.   gl()
  260.   intFace = 0
  261.  
  262.   intFace = 1 -- 1=forward,-1=backwards
  263.   -- go back down  
  264.   -- hint: only digging front and back in order
  265.   --       to not cut into larger neighbouring,
  266.   --       as this may leave upper tree parts left
  267.   for i = 1,intUpCount+1 do
  268.     dd() df() gl(2) df()
  269.     intFace=intFace* -1
  270.     while not turtle.down() do dd() end
  271.   end
  272.   if intFace==1 then
  273.     gl()
  274.   elseif intFace==-1 then
  275.     gr()
  276.   end
  277.   sf() df() term.write(".")
  278.   print(" done!")
  279.  
  280.   -- plant new
  281.   plantTree()
  282.   while not turtle.up() do du() end
  283.   sd()  
  284. end
  285.  
  286. ---------------------------------------
  287. function plantTree()
  288. local tmpCount=0
  289.   ---- assumptions
  290.   -- turtle faces place to plant
  291.  
  292.   -- check for enough saplings
  293.   sf()
  294.   if turtle.getItemCount(1) > 1 then
  295.     -- plant
  296.     print("  plant new sapling")
  297.     while not turtle.place() do
  298.       print("  hard to plant here...")
  299.       tmpCount=tmpCount+1
  300.       if tmpCount>3 then break end
  301.       os.sleep(1)
  302.     end  -- NOT kai_2
  303.   else
  304.     -- error
  305.     print("  Out of saplings...") -- prog name
  306.     os.sleep(5)
  307.     actRandomCheckSapling=cMaxCheckSapling
  308.     return
  309.   end
  310. end
  311.  
  312. ---------------------------------------
  313. local function replantStub()
  314.   ss(2) -- compare with wood in slot 2
  315.   if turtle.compare() then
  316.     -- assumption: there is only a stub left, so replant
  317.     -- if there is a tree on top of it, it will be harvested next round
  318.     print("  Replanting a stub")
  319.     df() ss(1)
  320.     if pf() then
  321.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Stub
  322.     else
  323.       print("    failure!")  
  324.     end
  325.   else
  326.     ss(1)
  327.     end
  328.   end
  329.   local function eS(sI,sA,eA)
  330.   local sO=""
  331.   local sR=""
  332.   if sA==nil then sA=1 end
  333.   if eA==nil then eA=string.len(sI) end
  334.   for i=sA,eA,1 do
  335.     sO=string.sub(sI,i,i)
  336.     if sR~="" then break end
  337.     if sO=="a" then
  338.       gl()
  339.     elseif sO=="d" then
  340.       gr()
  341.     else
  342.       while not turtle.forward() do df() end
  343.     end
  344.   end
  345.   return sR
  346. end
  347.  
  348. ---------------------------------------
  349. local function randomReplant()
  350. local intSuccess=0
  351.   if turtle.getItemCount(1) > 10 then
  352.     -- try to plant
  353.     while not turtle.down() do dd() end
  354.     sf() gl() sf()
  355.     if turtle.place() then
  356.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  357.     else
  358.     turtle.sleep(2)  
  359.     if turtle.detect() then replantStub() end
  360.     end
  361.     gl() sf() gl() sf()
  362.     if turtle.place() then
  363.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  364.     else
  365.       turtle.sleep(2)
  366.       if turtle.detect() then replantStub() end
  367.     end
  368.     gl() sf()
  369.     while not turtle.up() do du() end
  370.     -- ensure min probability and max 100%
  371.     actRandomCheckSapling=math.max(actRandomCheckSapling-0.01,minRandomCheckSapling)
  372.     actRandomCheckSapling=math.min(actRandomCheckSapling,cMaxCheckSapling)
  373.     print((actRandomCheckSapling*100).."% check probability")
  374.   else
  375.     -- extra suck
  376.     while not turtle.down() do dd() end
  377.     sf() gr() sf() gr() sf() gr() sf() gr() sf()
  378.     while not turtle.up() do du() end
  379.     sd()
  380.   end
  381. end
  382.  
  383. ---------------------------------------
  384. local function craftFuel()
  385. local tmpFuelItems=turtle.getItemCount(2)
  386.  
  387.   -- step 1 need fuel?
  388.   if (turtle.getFuelLevel()<cMinFuel) and (turtle.getItemCount(cSlotChest)==1) then
  389.   -- no refuelling if not exactly 1 item in slot cSlotChest (=chest)
  390.     print("Auto refuel    ("..turtle.getFuelLevel().."/"..cMinFuel..") ...")
  391.  
  392.     -- step 2 enough mats to refuel?
  393.     --        assumption: slot 2 has wood
  394.     if tmpFuelItems>1 then  
  395.       -- step 2 store away stuff!
  396.       ss(cSlotChest) while not turtle.placeUp() do du() end
  397.      
  398.       for i=1,15,1 do
  399.         ss(i)
  400.         if i~=2 then
  401.           Du()
  402.         else
  403.           -- cCraftRefuelMaxItems
  404.           Du(math.max(1,turtle.getItemCount(2)-cCraftRefuelMaxItems)) -- to keep the wood
  405.         end
  406.       end
  407.      
  408.       -- step 3 craft planks!
  409.       turtle.craft()
  410.      
  411.       -- step 4 refuel!
  412.       for i=1,16,1 do
  413.         ss(i)
  414.         turtle.refuel()
  415.       end
  416.       print("New fuel level ("..turtle.getFuelLevel().."/"..cMinFuel..")")
  417.      
  418.       -- step 5 get back stuff!
  419.       ss(1) --su(64)
  420.       while turtle.suckUp() do end
  421.       ss(cSlotChest) du() ss(1)
  422.     else   
  423.       print("Ups, not enough wood for auto refuel!")
  424.     end
  425.   end
  426. end
  427.  
  428.  
  429. ---------------------------------------
  430. local function emptyTurtle()
  431.   print("  Drop what I've harvested!")
  432.   while not turtle.down() do dd() end
  433.   ss(2)
  434.  
  435.   if turtle.compareTo(1) then
  436.     print("Error: Ups, in slot 2 is the same as in slot 1??")
  437.     --Dd()
  438.   else
  439.     -- if slot 2 has other item (wood) than slot 1
  440.     --   keep one of them for comparison
  441.     if turtle.getItemCount(2)>1 then
  442.       Dd(math.max(turtle.getItemCount(2)-1,0))
  443.     end
  444.   end
  445.   for i=3,15,1 do
  446.     -- assumption slot 16 contains a chest
  447.     ss(i) Dd()
  448.   end
  449.   os.sleep(0)
  450.   ss(1)
  451. end
  452.  
  453. ---------------------------------------
  454. ---- main -----------------------------
  455. ---------------------------------------
  456. -- step 0 info and initial check
  457. term.clear() term.setCursorPos(1,1)
  458. repeat
  459.  print("+-------------------------------------+")
  460.  print("| aTreeFarm "..cVersion..", by Kaikaku (1/2)    |")
  461.  print("+-------------------------------------+")
  462.  print("| Farm set-up: Place crafty felling   |")
  463.  print("|   turtle down (e.g. bottom left     |")
  464.  print("|   corner of chunk). Run program with|")
  465.  print("|   parameter 'setup' (one time).     |")
  466.  print("| Materials for auto set-up:          |")
  467.  print("|   slot 3: chest   (1)               |")
  468.  print("|   slot 4: cobble  (47)              |")
  469.  print("|   slot 5: torches (8)               |")
  470.  print("+-------------------------------------+")
  471.  
  472. if blnAutoSetup then
  473.   if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)<47 or turtle.getItemCount(5)<8 then
  474.   -- inventory not ready for set-up
  475.     strSimpleCheck="Fill in slots 3-5 and press enter:"
  476.   else
  477.     strSimpleCheck="Press enter to start:"
  478.   end
  479. else  
  480.   strSimpleCheck="Press enter to start:"
  481. end
  482. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  483. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  484.  
  485.  
  486.  
  487. term.clear() term.setCursorPos(1,1)
  488. repeat
  489.  print("+-------------------------------------+")
  490.  print("| aTreeFarm "..cVersion..", by Kaikaku (2/2)    |")
  491.  print("+-------------------------------------+")
  492.  print("| Running the farm:                   |")
  493.  print("|   Felling turtle sits above chest   |")
  494.  print("|   (as after running set-up). Turtle |")
  495.  print("|   needs some initial fuel to start. |")
  496.  print("| Turtle inventory:                   |")
  497.  print("|   slot  1: saplings          (20+x) |")
  498.  print("|   slot  2: wood from sapling (1+x)  |")
  499.  print("|   slot 16: chest             (1)    |")
  500.  print("+-------------------------------------+")
  501.  
  502. if turtle.getItemCount(1)<11 or turtle.getItemCount(2)==0 or turtle.getItemCount(16)~=1 then
  503.   -- inventory not ready
  504.   strSimpleCheck="Provide materials and press enter:"
  505. else  
  506.   strSimpleCheck="Press enter to start:"
  507. end
  508. --strSimpleCheck="Press enter to start:"
  509. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  510. if blnAutoSetup then strSimpleCheck="Press enter to start:" end
  511. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  512.  
  513.  
  514. ---------------------------------------
  515. ---- set-up farm ----------------------
  516. ---------------------------------------
  517. -- set-up = not running the farm
  518. if blnAutoSetup then
  519.   write("Setting up tree farm...")
  520.   checkRefuel(cMinFuel,cSlotRefuel)
  521.   -- chest
  522.   gf(3) gr() gf(3) gl() ss(3) dd() pd()
  523.   -- path
  524.   ss(4)
  525.   for i=1,9,1 do gf() dd() pd() end gr()
  526.   for i=1,3,1 do gf() dd() pd() end gr()
  527.   for i=1,6,1 do gf() dd() pd() end gl()
  528.   for i=1,3,1 do gf() dd() pd() end gl()
  529.   for i=1,6,1 do gf() dd() pd() end gr()
  530.   for i=1,3,1 do gf() dd() pd() end gr()
  531.   for i=1,9,1 do gf() dd() pd() end gr()
  532.   for i=1,8,1 do gf() dd() pd() end
  533.   -- torches
  534.   ss(5) gf(2) gl() pf() gu() gb(10) pd()
  535.   gl() gf(5) pd() gf() pd() gf(5) pd() gr() gf(11) pd()
  536.   gb(3) gr() gf(3) pd() gf(5) pd() gf(2) gr() gb(2) gd()
  537.   print(" done!")
  538.   print("You can now run the tree farm with: ",cPrgName)
  539.   return
  540. end
  541.  
  542.  
  543. ---------------------------------------
  544. ---- tree farm ------------------------
  545. ---------------------------------------
  546. strC_next=string.sub(strC,1,1)
  547.  
  548. -- initial up
  549. while not turtle.up() do du() end  
  550.  
  551. while true do  
  552.    
  553.   -- step 6 need to craft some fuel?
  554.   craftFuel()
  555.  
  556.   -- step 7 empty into chest
  557.   emptyTurtle()
  558.  
  559.   -- step 0 check exit
  560.   if maxCounter>0 then
  561.     if intCounter==maxCounter then
  562.       print("Completed all ",maxCounter,"  farming rounds.")
  563.       print("I'm awaiting new commands, master!")
  564.       --while not turtle.up() do du() end
  565.       return
  566.     end
  567.   end
  568.  
  569.   -- step 1 check fuel
  570.   checkRefuel(cMinFuel,cSlotRefuel)
  571.  
  572.   -- step 2 wait if redstone signal
  573.   while rs.getInput("back") do
  574.     print("Waiting due to redstone signal ",cWaitingTime,"s.")
  575.     os.sleep(cWaitingTime)
  576.   end
  577.  
  578.   -- step 3 new round
  579.   while not turtle.up() do du() end
  580.   ss(1)
  581.   intCounter=intCounter+1
  582.   print("Starting round ",intCounter," with "..turtle.getItemCount(1).." saplings.")
  583.  
  584.   for i=1,cLoopEnd,1 do
  585.  
  586.     -- update commands
  587.     strC_now=strC_next
  588.     if i<cLoopEnd then
  589.       strC_next=string.sub(strC,i+1,i+1)
  590.     else
  591.       strC_next=string.sub(strC,1,1)
  592.     end
  593.    
  594.     -- step 4 one step on the road
  595.     tmpResult=eS(strC,i,i)
  596.     if tmpResult~="" then
  597.       print("found special command: "..tmpResult)
  598.     end
  599.    
  600.     -- step 5 check for blocks
  601.     -- step 5.1 check left hand side
  602.     if strC_now~="a" and strC_next~="a" then
  603.       -- now  a=>just turned left
  604.       -- next a=>will turned left
  605.       gl()
  606.       turtle.sleep(2)
  607.       if turtle.detect() then cutTree() end
  608.       gr()
  609.     end
  610.     -- step 5.2 check right hand side
  611.     if strC_now~="d" and strC_next~="d" then   
  612.       -- now  d=>just turned right
  613.       -- next d=>will turn right
  614.       gr()
  615.       turtle.sleep(2)
  616.       if turtle.detect() then cutTree() end
  617.       gl()
  618.     end
  619.     sd()  
  620.    
  621.     if math.random()<=actRandomCheckSapling then
  622.       if strC_now~="d" and strC_now~="a" then  
  623.         randomReplant()
  624.       end
  625.     end
  626.   end
  627. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top