Advertisement
Towtow10

Tree Farm Unlimited Fuel

Jan 25th, 2016
1,957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.73 KB | None | 0 0
  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.  
  165.  
  166. ---------------------------------------
  167. ---- functions ------------------------
  168. ---------------------------------------
  169.  
  170. local function cutTree()
  171. local tmpExtraDigUp=cExtraDigUp
  172.  
  173. ---- assumptions
  174. -- turtle faces trunk one block below bottom
  175. ---- variables
  176. local intUpCount = 0
  177. local intFace = 0 -- -1=left, 1=right
  178. local blnDigSomething=false
  179.  
  180. term.write(" cutting tree: ")
  181.  
  182. -- get into tree column
  183. df()
  184. while not turtle.forward() do df() end
  185. gr() df() gl() df() gl() df()
  186. local intFace=-1
  187.  
  188. -- cut and go up
  189. repeat
  190. blnDigSomething=false
  191. du()
  192. while not turtle.up() do du() end
  193. blnDigSomething=df() or blnDigSomething
  194. if intFace==-1 then
  195. gr()
  196. blnDigSomething=df() or blnDigSomething
  197. gr()
  198. elseif intFace==1 then
  199. gl()
  200. blnDigSomething=df() or blnDigSomething
  201. gl()
  202. end
  203. intFace=intFace * -1
  204. blnDigSomething=df() or blnDigSomething
  205. intUpCount = intUpCount +1
  206. term.write(".")
  207.  
  208. -- check for 2 conditions
  209. -- either
  210. -- 1) nothing above the turtle
  211. -- or
  212. -- 2) nothing dig on the other columns blnDigSomething
  213. if not (turtle.detectUp() or blnDigSomething) then
  214. tmpExtraDigUp=tmpExtraDigUp-1
  215. else
  216. tmpExtraDigUp=cExtraDigUp -- restore it
  217. end
  218. until tmpExtraDigUp<0 --not (turtle.detectUp() or blnDigSomething) ----- NOT kai_2
  219.  
  220. -- go off tree column
  221. if intFace==-1 then
  222. gl()
  223. elseif intFace==1 then
  224. gr()
  225. end
  226. df()
  227. while not turtle.forward() do df() end
  228. gl()
  229. intFace = 0
  230.  
  231. intFace = 1 -- 1=forward,-1=backwards
  232. -- go back down
  233. -- hint: only digging front and back in order
  234. -- to not cut into larger neighbouring,
  235. -- as this may leave upper tree parts left
  236. for i = 1,intUpCount+1 do
  237. dd() df() gl(2) df()
  238. intFace=intFace* -1
  239. while not turtle.down() do dd() end
  240. end
  241. if intFace==1 then
  242. gl()
  243. elseif intFace==-1 then
  244. gr()
  245. end
  246. sf() df() term.write(".")
  247. print(" done!")
  248.  
  249. -- plant new
  250. plantTree()
  251. while not turtle.up() do du() end
  252. sd()
  253. end
  254.  
  255. ---------------------------------------
  256. function plantTree()
  257. local tmpCount=0
  258. ---- assumptions
  259. -- turtle faces place to plant
  260.  
  261. -- check for enough saplings
  262. sf()
  263. if turtle.getItemCount(1) > 1 then
  264. -- plant
  265. print(" plant new sapling")
  266. while not turtle.place() do
  267. print(" hard to plant here...")
  268. tmpCount=tmpCount+1
  269. if tmpCount>3 then break end
  270. os.sleep(1)
  271. end -- NOT kai_2
  272. else
  273. -- error
  274. print(" Out of saplings...") -- prog name
  275. os.sleep(5)
  276. actRandomCheckSapling=cMaxCheckSapling
  277. return
  278. end
  279. end
  280.  
  281. ---------------------------------------
  282. local function replantStub()
  283. ss(2) -- compare with wood in slot 2
  284. if turtle.compare() then
  285. -- assumption: there is only a stub left, so replant
  286. -- if there is a tree on top of it, it will be harvested next round
  287. print(" Replanting a stub")
  288. df() ss(1)
  289. if pf() then
  290. actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Stub
  291. else
  292. print(" failure!")
  293. end
  294. else
  295. ss(1)
  296. end
  297. end
  298. local function eS(sI,sA,eA)
  299. local sO=""
  300. local sR=""
  301. if sA==nil then sA=1 end
  302. if eA==nil then eA=string.len(sI) end
  303. for i=sA,eA,1 do
  304. sO=string.sub(sI,i,i)
  305. if sR~="" then break end
  306. if sO=="a" then
  307. gl()
  308. elseif sO=="d" then
  309. gr()
  310. else
  311. while not turtle.forward() do df() end
  312. end
  313. end
  314. return sR
  315. end
  316.  
  317. ---------------------------------------
  318. local function randomReplant()
  319. local intSuccess=0
  320. if turtle.getItemCount(1) > 10 then
  321. -- try to plant
  322. while not turtle.down() do dd() end
  323. sf() gl() sf()
  324. if turtle.place() then
  325. actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  326. else
  327. if turtle.detect() then replantStub() end
  328. end
  329. gl() sf() gl() sf()
  330. if turtle.place() then
  331. actRandomCheckSapling=actRandomCheckSapling+cIncreaseCheckSapling_Sapling
  332. else
  333. if turtle.detect() then replantStub() end
  334. end
  335. gl() sf()
  336. while not turtle.up() do du() end
  337. -- ensure min probability and max 100%
  338. actRandomCheckSapling=math.max(actRandomCheckSapling-0.01,minRandomCheckSapling)
  339. actRandomCheckSapling=math.min(actRandomCheckSapling,cMaxCheckSapling)
  340. print((actRandomCheckSapling*100).."% check probability")
  341. else
  342. -- extra suck
  343. while not turtle.down() do dd() end
  344. sf() gr() sf() gr() sf() gr() sf() gr() sf()
  345. while not turtle.up() do du() end
  346. sd()
  347. end
  348. end
  349.  
  350. ---------------------------------------
  351. ---- main -----------------------------
  352. ---------------------------------------
  353. -- step 0 info and inital check
  354. term.clear() term.setCursorPos(1,1)
  355. repeat
  356. print("+-------------------------------------+")
  357. print("| aTreeFarm ",cVersion,", by Kaikaku (1/2) |")
  358. print("+-------------------------------------+")
  359. print("| Farm set-up: Place crafty felling |")
  360. print("| turtle down (e.g. bottom left |")
  361. print("| corner of chunk). Run program with|")
  362. print("| parameter 'setup' (one time). |")
  363. print("| Materials for auto set-up: |")
  364. print("| slot 3: chest (1) |")
  365. print("| slot 4: cobble (47) |")
  366. print("| slot 5: torches (8) |")
  367. print("+-------------------------------------+")
  368.  
  369. if blnAutoSetup then
  370. if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)<47 or turtle.getItemCount(5)<8 then
  371. -- inventory not ready for set-up
  372. strSimpleCheck="Fill in slots 3-5 and press enter:"
  373. else
  374. strSimpleCheck="Press enter to start:"
  375. end
  376. else
  377. strSimpleCheck="Press enter to start:"
  378. end
  379. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  380. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  381.  
  382.  
  383.  
  384. term.clear() term.setCursorPos(1,1)
  385. repeat
  386. print("+-------------------------------------+")
  387. print("| aTreeFarm ",cVersion,", by Kaikaku (2/2) |")
  388. print("+-------------------------------------+")
  389. print("| Running the farm: |")
  390. print("| Felling turtle sits above chest |")
  391. print("| (as after running set-up). Turtle |")
  392. print("| needs some initial fuel to start. |")
  393. print("| Turtle inventory: |")
  394. print("| slot 1: saplings (20+x) |")
  395. print("| slot 2: wood from sapling (1) |")
  396. print("| slot 16: chest (1) |")
  397. print("+-------------------------------------+")
  398.  
  399. if turtle.getItemCount(1)<11 or turtle.getItemCount(2)~=1 or turtle.getItemCount(16)~=1 then
  400. -- inventory not ready
  401. strSimpleCheck="Provide materials and press enter:"
  402. else
  403. strSimpleCheck="Press enter to start:"
  404. end
  405. --strSimpleCheck="Press enter to start:"
  406. if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
  407. if blnAutoSetup then strSimpleCheck="Press enter to start:" end
  408. until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
  409.  
  410.  
  411. ---------------------------------------
  412. ---- set-up farm ----------------------
  413. ---------------------------------------
  414. -- set-up = not running the farm
  415. if blnAutoSetup then
  416. write("Setting up tree farm...")
  417. -- chest
  418. gf(3) gr() gf(3) gl() ss(3) dd() pd()
  419. -- path
  420. ss(4)
  421. for i=1,9,1 do gf() dd() pd() end gr()
  422. for i=1,3,1 do gf() dd() pd() end gr()
  423. for i=1,6,1 do gf() dd() pd() end gl()
  424. for i=1,3,1 do gf() dd() pd() end gl()
  425. for i=1,6,1 do gf() dd() pd() end gr()
  426. for i=1,3,1 do gf() dd() pd() end gr()
  427. for i=1,9,1 do gf() dd() pd() end gr()
  428. for i=1,8,1 do gf() dd() pd() end
  429. -- torches
  430. ss(5) gf(2) gl() pf() gu() gb(10) pd()
  431. gl() gf(5) pd() gf() pd() gf(5) pd() gr() gf(11) pd()
  432. gb(3) gr() gf(3) pd() gf(5) pd() gf(2) gr() gb(2) gd()
  433. print(" done!")
  434. print("You can now run the tree farm with: ",cPrgName)
  435. return
  436. end
  437.  
  438.  
  439. ---------------------------------------
  440. ---- tree farm ------------------------
  441. ---------------------------------------
  442. strC_next=string.sub(strC,1,1)
  443.  
  444. while true do
  445.  
  446. -- step 0 check exit
  447. if maxCounter>0 then
  448. if intCounter==maxCounter then
  449. print("Completed all ",maxCounter," farming rounds.")
  450. print("I'm awaiting new commands, master!")
  451. return
  452. end
  453. end
  454.  
  455. -- step 2 wait if redstone signal
  456. while rs.getInput("back") do
  457. print("Waiting due to redstone signal ",cWaitingTime,"s.")
  458. os.sleep(cWaitingTime)
  459. end
  460.  
  461. -- step 3 new round
  462. while not turtle.up() do du() end
  463. ss(1)
  464. intCounter=intCounter+1
  465. print("Starting round ",intCounter," with "..turtle.getItemCount(1).." saplings.")
  466.  
  467. for i=1,cLoopEnd,1 do
  468.  
  469. -- update commands
  470. strC_now=strC_next
  471. if i<cLoopEnd then
  472. strC_next=string.sub(strC,i+1,i+1)
  473. else
  474. strC_next=string.sub(strC,1,1)
  475. end
  476.  
  477. -- step 4 one step on the road
  478. tmpResult=eS(strC,i,i)
  479. if tmpResult~="" then
  480. print("found special command: "..tmpResult)
  481. end
  482.  
  483. -- step 5 check for blocks
  484. -- step 5.1 check left hand side
  485. if strC_now~="a" and strC_next~="a" then
  486. -- now a=>just turned left
  487. -- next a=>will turned left
  488. gl()
  489. if turtle.detect() then cutTree() end
  490. gr()
  491. end
  492. -- step 5.2 check right hand side
  493. if strC_now~="d" and strC_next~="d" then
  494. -- now d=>just turned right
  495. -- next d=>will turn right
  496. gr()
  497. if turtle.detect() then cutTree() end
  498. gl()
  499. end
  500. sd()
  501.  
  502. if math.random()<=actRandomCheckSapling then
  503. if strC_now~="d" and strC_now~="a" then
  504. randomReplant()
  505. end
  506. end
  507. end
  508.  
  509. -- step 6 need to craft some fuel?
  510. craftFuel()
  511.  
  512. -- step 7 empty into chest
  513. print(" Drop what I've harvested!")
  514. while not turtle.down() do dd() end
  515. ss(2)
  516.  
  517. if turtle.compareTo(1) then
  518. print("Ups, in slot 2 is the same as in slot 1??")
  519. Dd()
  520. else
  521. -- if slot 2 has other item (wood) than slot 1
  522. -- keep one of them for comparison
  523. Dd(math.max(turtle.getItemCount(2)-1,0))
  524. end
  525. for i=3,15,1 do
  526. -- assumption slot 16 contains a chest
  527. ss(i) Dd()
  528. end
  529. os.sleep(0)
  530. ss(1)
  531.  
  532. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement