Maxwelljonez

MJZTreeFarm LUA Computality

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