nintendowfi

pancakesTreeFarm2.1

Aug 5th, 2022
64
0
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=2           -- 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=2 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.         if turtle.getItemCount(2)<64 then
  177.             turtle.turnLeft()
  178.             turtle.turnLeft()
  179.             sf(64 - turtle.getItemCount(15))
  180.             turtle.turnRight()
  181.             turtle.turnRight() end
  182.       end
  183.       -- step 2.2 try to refuel
  184.       for i=3,15,1 do
  185.         ss(i)
  186.         turtle.refuel()
  187.       end
  188.       print("New fuel level ("..turtle.getFuelLevel().."/"..cMinFuel..")")
  189.       -- step 2.3 update variables
  190.       tmpItems=turtle.getItemCount(slotFuel)
  191.       tmpFuel2=tmpFuel
  192.       tmpFuel=turtle.getFuelLevel()
  193.     end
  194.   end
  195.   -- step 3 either no need to refuel
  196.   --        or successfully refuelled
  197.   print("Fuel level ok  ("..tmpFuel.."/"..minFuel..")")
  198. end
  199.  
  200.  
  201. ---------------------------------------
  202. ---- functions ------------------------
  203. ---------------------------------------
  204.  
  205. local function cutTree()
  206. local tmpExtraDigUp=cExtraDigUp
  207.  
  208.   ---- assumptions
  209.   -- turtle faces trunk one block below bottom
  210.   ---- variables
  211.   local intUpCount = 0
  212.   local intFace = 0 -- -1=left, 1=right
  213.   local blnDigSomething=false
  214.  
  215.   term.write("  cutting tree: ")
  216.  
  217.   -- get into tree column
  218.   df()
  219.   while not turtle.forward() do df() end
  220.   gr() df() gl() df() gl() df()
  221.   local intFace=-1
  222.  
  223.   -- cut and go up
  224.   repeat
  225.     blnDigSomething=false
  226.     du()
  227.     while not turtle.up() do du() end
  228.     blnDigSomething=df() or blnDigSomething
  229.     if intFace==-1 then
  230.       gr()
  231.       blnDigSomething=df() or blnDigSomething
  232.       gr()
  233.     elseif intFace==1 then
  234.       gl()
  235.       blnDigSomething=df() or blnDigSomething
  236.       gl()
  237.     end
  238.     intFace=intFace * -1
  239.     blnDigSomething=df() or blnDigSomething
  240.     intUpCount = intUpCount +1
  241.     term.write(".")
  242.    
  243.     -- check for 2 conditions
  244.     -- either
  245.     -- 1) nothing above the turtle
  246.     -- or
  247.     -- 2) nothing dig on the other columns blnDigSomething
  248.     if not (turtle.detectUp() or blnDigSomething) then
  249.       tmpExtraDigUp=tmpExtraDigUp-1  
  250.     else
  251.       tmpExtraDigUp=cExtraDigUp -- restore it  
  252.     end    
  253.   until tmpExtraDigUp<0 --not (turtle.detectUp() or blnDigSomething) ----- NOT kai_2
  254.  
  255.   -- go off tree column  
  256.   if intFace==-1 then
  257.     gl()
  258.   elseif intFace==1 then
  259.     gr()
  260.   end
  261.   df()
  262.   while not turtle.forward() do df() end
  263.   gl()
  264.   intFace = 0
  265.  
  266.   intFace = 1 -- 1=forward,-1=backwards
  267.   -- go back down  
  268.   -- hint: only digging front and back in order
  269.   --       to not cut into larger neighbouring,
  270.   --       as this may leave upper tree parts left
  271.   for i = 1,intUpCount+1 do
  272.     dd() df() gl(2) df()
  273.     intFace=intFace* -1
  274.     while not turtle.down() do dd() end
  275.   end
  276.   if intFace==1 then
  277.     gl()
  278.   elseif intFace==-1 then
  279.     gr()
  280.   end
  281.   sf() df() term.write(".")
  282.   print(" done!")
  283.  
  284.   -- plant new
  285.   plantTree()
  286.   while not turtle.up() do du() end
  287.   sd()  
  288. end
  289.  
  290. ---------------------------------------
  291. function plantTree()
  292. local tmpCount=0
  293.   ---- assumptions
  294.   -- turtle faces place to plant
  295.  
  296.   -- check for enough saplings
  297.   sf()
  298.   if turtle.getItemCount(1) > 1 then
  299.     -- plant
  300.     print("  plant new sapling")
  301.     while not turtle.place() do
  302.       print("  hard to plant here...")
  303.       tmpCount=tmpCount+1
  304.       if tmpCount>3 then break end
  305.       os.sleep(1)
  306.     end  -- NOT kai_2
  307.   else
  308.     -- error
  309.     print("  Out of saplings...") -- prog name
  310.     os.sleep(5)
  311.     actRandomCheckSapling=cMaxCheckSapling
  312.     return
  313.   end
  314. end
  315.  
  316. ---------------------------------------
  317. local function replantStub()
  318.   ss(2) -- compare with wood in slot 2
  319.   if turtle.compare() then
  320.     -- assumption: there is only a stub left, so replant
  321.     -- if there is a tree on top of it, it will be harvested next round
  322.     print("  Replanting a stub")
  323.     df() ss(1)
  324.     if pf() then
  325.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Stub
  326.     else
  327.       print("    failure!")  
  328.     end
  329.   else
  330.     ss(1)
  331.     end
  332.   end
  333.   local function eS(sI,sA,eA)
  334.   local sO=""
  335.   local sR=""
  336.   if sA==nil then sA=1 end
  337.   if eA==nil then eA=string.len(sI) end
  338.   for i=sA,eA,1 do
  339.     sO=string.sub(sI,i,i)
  340.     if sR~="" then break end
  341.     if sO=="a" then
  342.       gl()
  343.     elseif sO=="d" then
  344.       gr()
  345.     else
  346.       while not turtle.forward() do df() end
  347.     end
  348.   end
  349.   return sR
  350. end
  351.  
  352. ---------------------------------------
  353. local function randomReplant()
  354. local intSuccess=0
  355.   if turtle.getItemCount(1) > 10 then
  356.     -- try to plant
  357.     while not turtle.down() do dd() end
  358.     sf() gl() sf()
  359.     if turtle.place() then
  360.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  361.     else
  362.       if turtle.detect() then replantStub() end
  363.     end
  364.     gl() sf() gl() sf()
  365.     if turtle.place() then
  366.       actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  367.     else
  368.       if turtle.detect() then replantStub() end
  369.     end
  370.     gl() sf()
  371.     while not turtle.up() do du() end
  372.     -- ensure min probability and max 100%
  373.     actRandomCheckSapling=math.max(actRandomCheckSapling-0.01,minRandomCheckSapling)
  374.     actRandomCheckSapling=math.min(actRandomCheckSapling,cMaxCheckSapling)
  375.     print((actRandomCheckSapling*100).."% check probability")
  376.   else
  377.     -- extra suck
  378.     while not turtle.down() do dd() end
  379.     sf() gr() sf() gr() sf() gr() sf() gr() sf()
  380.     while not turtle.up() do du() end
  381.     sd()
  382.   end
  383. end
  384.  
  385. ---------------------------------------
  386. local function craftFuel()
  387. local tmpFuelItems=turtle.getItemCount(15)
  388.  
  389.   -- step 1 need fuel?
  390.   if (turtle.getFuelLevel()<cMinFuel) and (turtle.getItemCount(cSlotChest)==1) then
  391.   -- no refuelling if not exactly 1 item in slot cSlotChest (=chest)
  392.     print("Auto refuel    ("..turtle.getFuelLevel().."/"..cMinFuel..") ...")
  393.  
  394.     -- step 2 enough mats to refuel?
  395.     --        assumption: slot 2 has wood
  396.     if tmpFuelItems>1 then  
  397.       -- step 2 store away stuff!
  398.       ss(cSlotChest) while not turtle.placeUp() do du() end
  399.      
  400.       for i=1,15,1 do
  401.         ss(i)
  402.         if i~=2 then
  403.           Du()
  404.         else
  405.           -- cCraftRefuelMaxItems
  406.           Du(math.max(1,turtle.getItemCount(2)-cCraftRefuelMaxItems)) -- to keep the wood
  407.         end
  408.       end
  409.      
  410.       -- step 3 get coal from chest behind
  411.       if tmpFuelItems<64 then
  412.         turtle.turnLeft()
  413.         turtle.turnLeft()
  414.         sf(64 - tmpFuelItems)
  415.         turtle.turnRight()
  416.         turtle.turnRight() end
  417.      
  418.      
  419.       -- step 4 refuel!
  420.       for i=1,16,1 do
  421.         ss(i)
  422.         turtle.refuel()
  423.       end
  424.       print("New fuel level ("..turtle.getFuelLevel().."/"..cMinFuel..")")
  425.      
  426.       -- step 5 get back stuff!
  427.       ss(1) --su(64)
  428.       while turtle.suckUp() do end
  429.       ss(cSlotChest) du() ss(1)
  430.     else   
  431.         if tmpFuelItems<64 then
  432.             turtle.turnLeft()
  433.             turtle.turnLeft()
  434.             sf(64 - tmpFuelItems)
  435.             turtle.turnRight()
  436.             turtle.turnRight() end
  437.     end
  438.   end
  439. end
  440.  
  441.  
  442. ---------------------------------------
  443. local function emptyTurtle()
  444.   print("  Drop what I've harvested!")
  445.   while not turtle.down() do dd() end
  446.   ss(2)
  447.  
  448.   if turtle.compareTo(1) then
  449.     print("Error: Ups, in slot 2 is the same as in slot 1??")
  450.     --Dd()
  451.   else
  452.     -- if slot 2 has other item (wood) than slot 1
  453.     --   keep one of them for comparison
  454.     if turtle.getItemCount(2)>1 then
  455.       Dd(math.max(turtle.getItemCount(2)-1,0))
  456.     end
  457.   end
  458.   for i=3,15,1 do
  459.     -- assumption slot 16 contains a chest
  460.     ss(i) Dd()
  461.   end
  462.   os.sleep(0)
  463.   ss(1)
  464. end
  465.  
  466. ---------------------------------------
  467. ---- main -----------------------------
  468. ---------------------------------------
  469. -- step 0 info and initial check
  470. term.clear() term.setCursorPos(1,1)
  471. repeat
  472.  print("+-------------------------------------+")
  473.  print("| aTreeFarm "..cVersion..", by Kaikaku (1/2)    |")
  474.  print("+-------------------------------------+")
  475.  print("| Farm set-up: Place crafty felling   |")
  476.  print("|   turtle down (e.g. bottom left     |")
  477.  print("|   corner of chunk). Run program with|")
  478.  print("|   parameter 'setup' (one time).     |")
  479.  print("| Materials for auto set-up:          |")
  480.  print("|   slot 3: chest   (1)               |")
  481.  print("|   slot 4: cobble  (47)              |")
  482.  print("|   slot 5: torches (8)               |")
  483.  print("+-------------------------------------+")
  484.  
  485. if blnAutoSetup then
  486.   if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)<47 or turtle.getItemCount(5)<8 then
  487.   -- inventory not ready for set-up
  488.     strSimpleCheck="Fill in slots 3-5 and press enter:"
  489.   else
  490.     strSimpleCheck="Press enter to start:"
  491.   end
  492. else  
  493.   strSimpleCheck="Press enter to start:"
  494. end
  495. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  496. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  497.  
  498.  
  499.  
  500. term.clear() term.setCursorPos(1,1)
  501. repeat
  502.  print("+-------------------------------------+")
  503.  print("| aTreeFarm "..cVersion..", by Kaikaku (2/2)    |")
  504.  print("+-------------------------------------+")
  505.  print("| Running the farm:                   |")
  506.  print("|   Felling turtle sits above chest   |")
  507.  print("|   (as after running set-up). Turtle |")
  508.  print("|   needs some initial fuel to start. |")
  509.  print("| Turtle inventory:                   |")
  510.  print("|   slot  1: saplings          (20+x) |")
  511.  print("|   slot  2: wood from sapling (1+x)  |")
  512.  print("|   slot 16: chest             (1)    |")
  513.  print("+-------------------------------------+")
  514.  
  515. if turtle.getItemCount(1)<11 or turtle.getItemCount(2)==0 or turtle.getItemCount(16)~=1 then
  516.   -- inventory not ready
  517.   strSimpleCheck="Provide materials and press enter:"
  518. else  
  519.   strSimpleCheck="Press enter to start:"
  520. end
  521. --strSimpleCheck="Press enter to start:"
  522. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  523. if blnAutoSetup then strSimpleCheck="Press enter to start:" end
  524. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  525.  
  526.  
  527. ---------------------------------------
  528. ---- set-up farm ----------------------
  529. ---------------------------------------
  530. -- set-up = not running the farm
  531. if blnAutoSetup then
  532.   write("Setting up tree farm...")
  533.   checkRefuel(cMinFuel,cSlotRefuel)
  534.   -- chest
  535.   gf(3) gr() gf(3) gl() ss(3) dd() pd()
  536.   -- path
  537.   ss(4)
  538.   for i=1,9,1 do gf() dd() pd() end gr()
  539.   for i=1,3,1 do gf() dd() pd() end gr()
  540.   for i=1,6,1 do gf() dd() pd() end gl()
  541.   for i=1,3,1 do gf() dd() pd() end gl()
  542.   for i=1,6,1 do gf() dd() pd() end gr()
  543.   for i=1,3,1 do gf() dd() pd() end gr()
  544.   for i=1,9,1 do gf() dd() pd() end gr()
  545.   for i=1,8,1 do gf() dd() pd() end
  546.   -- torches
  547.   ss(5) gf(2) gl() pf() gu() gb(10) pd()
  548.   gl() gf(5) pd() gf() pd() gf(5) pd() gr() gf(11) pd()
  549.   gb(3) gr() gf(3) pd() gf(5) pd() gf(2) gr() gb(2) gd()
  550.   print(" done!")
  551.   print("You can now run the tree farm with: ",cPrgName)
  552.   return
  553. end
  554.  
  555.  
  556. ---------------------------------------
  557. ---- tree farm ------------------------
  558. ---------------------------------------
  559. strC_next=string.sub(strC,1,1)
  560.  
  561. -- initial up
  562. while not turtle.up() do du() end  
  563.  
  564. while true do  
  565.    
  566.   -- step 6 need to craft some fuel?
  567.   craftFuel()
  568.  
  569.   -- step 7 empty into chest
  570.   emptyTurtle()
  571.  
  572.   -- step 0 check exit
  573.   if maxCounter>0 then
  574.     if intCounter==maxCounter then
  575.       print("Completed all ",maxCounter,"  farming rounds.")
  576.       print("I'm awaiting new commands, master!")
  577.       --while not turtle.up() do du() end
  578.       return
  579.     end
  580.   end
  581.  
  582.   -- step 1 check fuel
  583.   checkRefuel(cMinFuel,cSlotRefuel)
  584.  
  585.   -- step 2 wait if redstone signal
  586.   while rs.getInput("back") do
  587.     print("Waiting due to redstone signal ",cWaitingTime,"s.")
  588.     os.sleep(cWaitingTime)
  589.   end
  590.  
  591.   -- step 3 new round
  592.   while not turtle.up() do du() end
  593.   ss(1)
  594.   intCounter=intCounter+1
  595.   print("Starting round ",intCounter," with "..turtle.getItemCount(1).." saplings.")
  596.  
  597.   for i=1,cLoopEnd,1 do
  598.  
  599.     -- update commands
  600.     strC_now=strC_next
  601.     if i<cLoopEnd then
  602.       strC_next=string.sub(strC,i+1,i+1)
  603.     else
  604.       strC_next=string.sub(strC,1,1)
  605.     end
  606.    
  607.     -- step 4 one step on the road
  608.     tmpResult=eS(strC,i,i)
  609.     if tmpResult~="" then
  610.       print("found special command: "..tmpResult)
  611.     end
  612.    
  613.     -- step 5 check for blocks
  614.     -- step 5.1 check left hand side
  615.     if strC_now~="a" and strC_next~="a" then
  616.       -- now  a=>just turned left
  617.       -- next a=>will turned left
  618.       gl()
  619.       if turtle.detect() then cutTree() end
  620.       gr()
  621.     end
  622.     -- step 5.2 check right hand side
  623.     if strC_now~="d" and strC_next~="d" then   
  624.       -- now  d=>just turned right
  625.       -- next d=>will turn right
  626.       gr()
  627.       if turtle.detect() then cutTree() end
  628.       gl()
  629.     end
  630.     sd()  
  631.    
  632.     if math.random()<=actRandomCheckSapling then
  633.       if strC_now~="d" and strC_now~="a" then  
  634.         randomReplant()
  635.       end
  636.     end
  637.   end
  638. end
Add Comment
Please, Sign In to add comment