Wassaa

Trees

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