Advertisement
Guest User

ATreeFarm Without Crafting

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