aTreeFarm.lua

622 lines · 16.9 KB

Open raw

{program="aTreeFarm",version="1.04",date="2018-01-07"}

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Kaikaku/aTreeFarm.lua
1--{program="aTreeFarm",version="1.04",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---------------------------------------
28local cVersion ="1.04"
29local cPrgName ="aTreeFarm"
30local cMinFuel = 960*2 -- 2 stacks of planks
31
32local minRandomCheckSapling=0.1 -- below this will check replant
33local actRandomCheckSapling=minRandomCheckSapling*2
34local cIncreaseCheckSapling_Sapling=0.02
35local cIncreaseCheckSapling_Stub=0.04
36local cMaxCheckSapling=0.6
37local strC="tReeTreESdig!diG;-)FaRmKaIKAKUudIgYdIgyTreEAndsOrRygUYsd"
38
39local cSlotChest=16 -- chest for crafty turtle
40local cCraftRefuelMaxItems=32 -- use how many logs to refuel at max
41local cSlotRefuel=15 -- where to put fuel
42local cExtraDigUp=1 -- go how many extra levels to reach jungle branches
43local cLoopEnd=56 -- one loop
44local cWaitingTime=20 -- if redstone signal in back is on
45
46---------------------------------------
47---- VARIABLES ------------------------
48---------------------------------------
49local strC_now=""
50local strC_next=""
51
52local tmpResult=""
53local blnAskForParameters=true
54local blnShowUsage=false
55local blnAutoSetup=false
56local strSimpleCheck="Press enter to start:"
57local intCounter=0
58local maxCounter=0
59
60---------------------------------------
61---- tArgs ----------------------------
62---------------------------------------
63local tArgs = {...}
64if #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
72end
73
74if 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
87end
88
89---------------------------------------
90-- BASIC FUNCTIONS FOR TURTLE CONTROL -
91---------------------------------------
92local 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
95end
96local 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
99end
100local 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
103end
104local 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
107end
108local 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
111end
112local 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
115end
116local 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
120end
121local function pu() turtle.placeUp() end
122local function pd() turtle.placeDown() end
123local function df() return turtle.dig() end
124local function du() turtle.digUp() end
125local function dd() turtle.digDown() end
126local function sf() turtle.suck() end
127local function su() turtle.suckUp() end
128local 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
136end
137local function Df() turtle.drop() end
138local function Du(n) if n==nil then n=64 end turtle.dropUp(n) end
139local function Dd(n) if n==nil then n=64 end turtle.dropDown(n) end
140local function ss(s) turtle.select(s) end
141
142local 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
151end
152
153local function checkFuel()
154 local tmp=turtle.getFuelLevel()
155 return tmp
156end
157
158function 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..")")
193end
194
195
196---------------------------------------
197---- functions ------------------------
198---------------------------------------
199
200local function cutTree()
201local 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()
283end
284
285---------------------------------------
286function plantTree()
287local 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
309end
310
311---------------------------------------
312local 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
345end
346
347---------------------------------------
348local function randomReplant()
349local 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
378end
379
380---------------------------------------
381local function craftFuel()
382local 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 turtle.craft()
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 refuel!")
421 end
422 end
423end
424
425
426---------------------------------------
427local function emptyTurtle()
428 print(" Drop what I've harvested!")
429 while not turtle.down() do dd() end
430 ss(2)
431
432 if turtle.compareTo(1) then
433 print("Error: Ups, in slot 2 is the same as in slot 1??")
434 --Dd()
435 else
436 -- if slot 2 has other item (wood) than slot 1
437 -- keep one of them for comparison
438 if turtle.getItemCount(2)>1 then
439 Dd(math.max(turtle.getItemCount(2)-1,0))
440 end
441 end
442 for i=3,15,1 do
443 -- assumption slot 16 contains a chest
444 ss(i) Dd()
445 end
446 os.sleep(0)
447 ss(1)
448end
449
450---------------------------------------
451---- main -----------------------------
452---------------------------------------
453-- step 0 info and initial check
454term.clear() term.setCursorPos(1,1)
455repeat
456 print("+-------------------------------------+")
457 print("| aTreeFarm "..cVersion..", by Kaikaku (1/2) |")
458 print("+-------------------------------------+")
459 print("| Farm set-up: Place crafty felling |")
460 print("| turtle down (e.g. bottom left |")
461 print("| corner of chunk). Run program with|")
462 print("| parameter 'setup' (one time). |")
463 print("| Materials for auto set-up: |")
464 print("| slot 3: chest (1) |")
465 print("| slot 4: cobble (47) |")
466 print("| slot 5: torches (8) |")
467 print("+-------------------------------------+")
468
469if blnAutoSetup then
470 if turtle.getItemCount(3)~=1 or turtle.getItemCount(4)<47 or turtle.getItemCount(5)<8 then
471 -- inventory not ready for set-up
472 strSimpleCheck="Fill in slots 3-5 and press enter:"
473 else
474 strSimpleCheck="Press enter to start:"
475 end
476else
477 strSimpleCheck="Press enter to start:"
478end
479if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
480until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
481
482
483
484term.clear() term.setCursorPos(1,1)
485repeat
486 print("+-------------------------------------+")
487 print("| aTreeFarm "..cVersion..", by Kaikaku (2/2) |")
488 print("+-------------------------------------+")
489 print("| Running the farm: |")
490 print("| Felling turtle sits above chest |")
491 print("| (as after running set-up). Turtle |")
492 print("| needs some initial fuel to start. |")
493 print("| Turtle inventory: |")
494 print("| slot 1: saplings (20+x) |")
495 print("| slot 2: wood from sapling (1+x) |")
496 print("| slot 16: chest (1) |")
497 print("+-------------------------------------+")
498
499if turtle.getItemCount(1)<11 or turtle.getItemCount(2)==0 or turtle.getItemCount(16)~=1 then
500 -- inventory not ready
501 strSimpleCheck="Provide materials and press enter:"
502else
503 strSimpleCheck="Press enter to start:"
504end
505--strSimpleCheck="Press enter to start:"
506if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
507if blnAutoSetup then strSimpleCheck="Press enter to start:" end
508until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
509
510
511---------------------------------------
512---- set-up farm ----------------------
513---------------------------------------
514-- set-up = not running the farm
515if blnAutoSetup then
516 write("Setting up tree farm...")
517 checkRefuel(cMinFuel,cSlotRefuel)
518 -- chest
519 gf(3) gr() gf(3) gl() ss(3) dd() pd()
520 -- path
521 ss(4)
522 for i=1,9,1 do gf() dd() pd() end gr()
523 for i=1,3,1 do gf() dd() pd() end gr()
524 for i=1,6,1 do gf() dd() pd() end gl()
525 for i=1,3,1 do gf() dd() pd() end gl()
526 for i=1,6,1 do gf() dd() pd() end gr()
527 for i=1,3,1 do gf() dd() pd() end gr()
528 for i=1,9,1 do gf() dd() pd() end gr()
529 for i=1,8,1 do gf() dd() pd() end
530 -- torches
531 ss(5) gf(2) gl() pf() gu() gb(10) pd()
532 gl() gf(5) pd() gf() pd() gf(5) pd() gr() gf(11) pd()
533 gb(3) gr() gf(3) pd() gf(5) pd() gf(2) gr() gb(2) gd()
534 print(" done!")
535 print("You can now run the tree farm with: ",cPrgName)
536 return
537end
538
539
540---------------------------------------
541---- tree farm ------------------------
542---------------------------------------
543strC_next=string.sub(strC,1,1)
544
545-- initial up
546while not turtle.up() do du() end
547
548while true do
549
550 -- step 6 need to craft some fuel?
551 craftFuel()
552
553 -- step 7 empty into chest
554 emptyTurtle()
555
556 -- step 0 check exit
557 if maxCounter>0 then
558 if intCounter==maxCounter then
559 print("Completed all ",maxCounter," farming rounds.")
560 print("I'm awaiting new commands, master!")
561 --while not turtle.up() do du() end
562 return
563 end
564 end
565
566 -- step 1 check fuel
567 checkRefuel(cMinFuel,cSlotRefuel)
568
569 -- step 2 wait if redstone signal
570 while rs.getInput("back") do
571 print("Waiting due to redstone signal ",cWaitingTime,"s.")
572 os.sleep(cWaitingTime)
573 end
574
575 -- step 3 new round
576 while not turtle.up() do du() end
577 ss(1)
578 intCounter=intCounter+1
579 print("Starting round ",intCounter," with "..turtle.getItemCount(1).." saplings.")
580
581 for i=1,cLoopEnd,1 do
582
583 -- update commands
584 strC_now=strC_next
585 if i<cLoopEnd then
586 strC_next=string.sub(strC,i+1,i+1)
587 else
588 strC_next=string.sub(strC,1,1)
589 end
590
591 -- step 4 one step on the road
592 tmpResult=eS(strC,i,i)
593 if tmpResult~="" then
594 print("found special command: "..tmpResult)
595 end
596
597 -- step 5 check for blocks
598 -- step 5.1 check left hand side
599 if strC_now~="a" and strC_next~="a" then
600 -- now a=>just turned left
601 -- next a=>will turned left
602 gl()
603 if turtle.detect() then cutTree() end
604 gr()
605 end
606 -- step 5.2 check right hand side
607 if strC_now~="d" and strC_next~="d" then
608 -- now d=>just turned right
609 -- next d=>will turn right
610 gr()
611 if turtle.detect() then cutTree() end
612 gl()
613 end
614 sd()
615
616 if math.random()<=actRandomCheckSapling then
617 if strC_now~="d" and strC_now~="a" then
618 randomReplant()
619 end
620 end
621 end
622end