View difference between Paste ID: 6rhjX8WV and EmtCKWH7
SHOW: | | - or go back to the newest paste.
1
-- Author: KROM
2
-- 
3
-- --------------------------------
4
local bUseModem = false
5
local bBoneMeal = true
6
local nRuns = 0
7
local bNoError = true
8
local bRedNet = false
9
local nMasterComputerID = nil
10
-- 30
11
local vX, vY = 0,-1
12
local posX, posY, posZ = 0,0,0
13
14
local stopCause = 0
15
-- --------------------------------
16
local function scanForSaplings()
17
    return turtle.getItemCount(2)
18
end
19
-- --------------------------------
20
local function scanForBoneMeal()
21
	return turtle.getItemCount(3)
22
end
23
-- --------------------------------
24
local function say(message)
25
	if bRedNet == true then
26
		rednet.send(nMasterComputerID, message)
27
	end
28
	print(message)
29
end
30
-- --------------------------------
31
local function turnRight()
32
    turtle.turnRight()
33
	vX, vY = vY, -vX
34
end
35
-- --------------------------------
36
local function turnLeft()
37
    turtle.turnLeft()
38
	vX, vY = -vY, vX
39
end
40
-- ----------------------------------------------------------
41
local function tryDown()
42
	stopCause = 0
43
	if posZ == 0 then
44
		say("tryDown: already at max depth")
45
		return false
46
	end
47
48
	if not turtle.down() then
49
		if turtle.digDown() then
50
		end
51
		if not turtle.down() then
52
			stopCause = 1
53
			return false
54
		end
55
	end	
56
	posZ = posZ - 1
57
	return true
58
end
59
-- ----------------------------------------------------------
60
local function tryForwards()
61
	stopCause = 0
62
	while not turtle.forward() do
63
		if turtle.dig() then
64
			-- all cool
65
	    else
66
 			sleep(0.8)
67
	    	if turtle.dig() then
68
				-- all cool
69
	    	else
70
	    		stopCause = 1
71
		    	return false
72
		    end
73
	    end
74
	end
75
76
	return true
77
end
78
-- --------------------------------
79
local function work(nHowDeep, nSize) 
80
	local depth,x,y
81
 	local startZ = posZ
82
	local bFirstLoop = false
83
	
84
	for depth=0,nHowDeep do
85
		for x=0,(nSize-1) do
86
		
87
			-- first row has no turn move
88
			-- which needs to be offset
89
			local startY = 1
90
91
			if x == 0 and startZ == 0 and depth == 0 then
92
				startY = 0
93
			elseif bFirstLoop == true then
94
				startY = 0
95
			end
96
97
			if x == 0 then
98
				say("SY: "..startY)
99
			end
100
101
			for y=startY,(nSize-1) do
102
103
				local start = posX..","..posY..","..posZ
104
				
105
				v = math.fmod(x,2)
106
				-- v == 0 = facing away. like start
107
				-- v == 1 = facing to start
108
				-- say("V: "..v.." LD: "..layerdirection.." SY: "..startY.." xyz: "..x..","..y..","..depth)
109
110
111
				if not tryForwards() then
112
					if stopCause == 2 then
113
						-- inventory full?!
114
						say("Failed to move forward. Inv. full?!")
115
						return false
116
					else 
117
						say("Failed to move forward.")
118
						return false
119
					end
120
				end
121
122
				if layerdirection == 1 then
123
					if v == 0 then
124
						posY = posY + 1
125
					else
126
						posY = posY - 1
127
					end
128
				else
129
					if v == 0 then
130
						posY = posY - 1
131
					else
132
						posY = posY + 1
133
					end
134
				end
135
136
				-- say("P: "..start.." -> "..posX..","..posY..","..posZ)
137
				-- say("Dir: "..xDir..","..yDir)
138
			-- end y loop
139
			end	
140
141
			bFirstLoop = false;
142
143
			say("completed one line")
144
145
			-- if not on last line
146
			if  x < nSize-1 then
147
				-- move to next line (turn, move, turn)
148
				if v == 0 then
149
150
					say("Moving to next row (1)")
151
					turnRight()
152
153
					if not tryForwards() then
154
						if stopCause == 2 then
155
							-- inventory full?!
156
							say("Failed to move forward. Inv. full?!")
157
							return false
158
						else 
159
							return false
160
						end						
161
					end
162
					turnRight()
163
				else 
164
					say("Moving to next row (2)")
165
					turnLeft()
166
167
					if not tryForwards() then
168
						if stopCause == 2 then
169
							-- inventory full?!
170
							say("Failed to move forward. Inv. full?!")
171
							return false
172
						else 
173
							return false
174
						end
175
					end
176
					turnLeft()
177
				end
178
179
				if layerdirection == 1 then
180
					posX = posX + 1
181
				else
182
					posX = posX - 1
183
				end
184
			end
185
186
		-- end x loop
187
		end
188
189
		say("completed one layer")
190
191
		-- checks if depth reached
192
		if depth < nHowDeep then
193
194
			if not tryDown() then
195
				-- unable to move down?
196
				-- assume we hit bedrock
197
				say("! we failed to move down")
198
				return false
199
			end
200
201
			if layerdirection == 1 then
202
				layerdirection = 0
203
			else
204
				layerdirection = 1
205
			end
206
207
			turnLeft()
208
			turnLeft()
209
		else
210
			say("finished all layer")
211
			-- finished
212
			return true
213
		end
214
		-- end z loop
215
	end
216
	say("should not hit this code line")
217
end
218
-- --------------------------------
219
local function giantTreeMode(nCurrentHeight)
220
221
	local startX, startY, startZ = 0,0,nCurrentHeight
222
	posX, posY, posZ = 0,0,nCurrentHeight
223
	vX, vY = 0,-1
224
	local nRadius = 4
225
	local nMinHeight = 3
226
	local nMaxHeight = 9
227
	
228
	-- go to start position
229
	while posZ < nCurrentHeight do
230
		if turtle.detectUp() then
231
			turtle.digUp()
232
		end
233
		turtle.up()
234
		posZ = posZ + 1
235
	end
236
	
237
	for i=0,nRadius do
238
		if turtle.detect() then
239
			turtle.dig()
240
		end
241
		turtle.forward()
242
		posY = posY + 1
243
	end
244
	
245
	turnLeft()
246
	
247
	for i=0,nRadius do
248
		if turtle.detect() then
249
			turtle.dig()
250
		end
251
		turtle.forward()
252
		posX = posX - 1
253
	end
254
	
255
	turnRight()
256
	turnRight()
257
	
258
	work(nMaxHeight - nMinHeight, nRadius) 
259
	
260
end
261
-- --------------------------------
262
local function harvestTree()
263
	local nHeight = 0
264
    say("Harvesting Tree")
265
	turtle.dig()
266
	turtle.forward()
267
	while turtle.detectUp() do
268
		turtle.digUp()
269
		turtle.up()
270
		nHeight = nHeight + 1
271
	end
272
	
273
	-- check if giant tree. 
274
	-- check by height...
275
	if nHeight > 6 then
276
		say("*** GIANT TREE MODE ***")
277
		return giantTreeMode(nHeight)
278
	else
279
		-- normal tree. go back down	
280
		while not turtle.detectDown() do
281
			turtle.down()
282
		end
283
		turtle.back()
284
	end
285
		
286
    say("Finished harvesting tree")
287
end
288
-- --------------------------------
289
local function detectTree()
290
    if turtle.detect() == false then
291
	    say("No tree detected(1)")
292
	    return false
293
    end
294
	
295
	if turtle.up() == false then
296
		say("Cannot move up, must be a tree")
297
		return true
298
	end
299
	
300
	if turtle.detect() == true then
301
		turtle.down()
302
		say("Tree detected")		
303
		return true
304
	else
305
	    turtle.down()
306
		say("Possible sapling detected")
307
		return false
308
	end
309
	
310
	return false
311
end
312
-- --------------------------------
313
local function applyBoneMeal() 
314
	say("Trying to apply BoneMeal")
315
	
316
	-- place while there is no tree
317
	local isatree
318
	local abort = false
319
	local nBoneMeal = 0
320
	local nMax = 5
321
	
322
	while abort == false do
323
	
324
		say("Looping for BoneMeal")
325
		
326
		isatree = detectTree()
327
		nBoneMeal = scanForBoneMeal()
328
		
329
		if isatree == false and nBoneMeal > 0 and turtle.detect() == true then
330
			turtle.select(3)
331
			turtle.place()	
332
			say("Applied BoneMeal")
333
		end	
334
		
335
		if isatree == true or nBoneMeal == 0 then
336
			abort = true
337
		end		
338
		
339
		nMax = nMax - 1
340
		
341
		if nMax <= 0 then
342
			abort = true
343
			say("BoneMeal: Max tries")
344
		end
345
	end	
346
end
347
-- --------------------------------
348
local function plantTree(harvested)
349
    say("Planting new Tree")
350
    turtle.select(2)
351
    turtle.place()	
352
    if bBoneMeal == true and harvested == false then	
353
		applyBoneMeal()		
354
    end
355
    say("Finished planting new Tree")
356
end
357
-- --------------------------------
358
local function spaceLeft()
359
    local nSpace = 0
360
    for n=1,16 do
361
        local x = turtle.getItemCount(n)
362
        if x == 0 then
363
            nSpace = nSpace + 1
364
        end
365
    end
366
    return nSpace
367
end
368
369
-- --------------------------------
370
local function moveForward(nSteps, pickupStuff)
371
    if nSteps == 0 then
372
	    return true
373
    end
374
	while nSteps > 0 do
375
		if pickupStuff == true then
376
			turtle.suck()			
377
		end	
378
	
379
	    if turtle.forward() == false then
380
		    return false
381
		end
382
		nSteps = nSteps - 1
383
	end
384
end
385
386
-- --------------------------------
387
local function dump(keepBattery)
388
	local x = 1
389
	if keepBattery == true then
390
		x = 2
391
	end
392
    for n=x,16 do
393
	    turtle.select(n)
394
		turtle.drop()
395
	end
396
end
397
-- --------------------------------
398
local function restock()
399
    -- move to first box (energy)
400
	say("move to first box (energy)");
401
	turnLeft()
402
	moveForward(2,true)
403
	turnRight()
404
	-- pick up energy cells
405
	turtle.select(1)
406
	turtle.suck()
407
	
408
    -- move to second box (saplings)
409
	say("move to second box (saplings)");
410
	turnLeft()
411
	moveForward(2,true)
412
	turnRight()
413
	-- pick up saplings
414
	turtle.select(2)
415
	turtle.suck()
416
	
417
	if bBoneMeal then		
418
		-- move to third box (bone meal)
419
		say("move to third box (bone meal)");
420
		turnLeft()
421
		moveForward(2,true)
422
		turnRight()
423
		-- pick up bone meal
424
		turtle.select(3)
425
		turtle.suck()
426
	end
427
	
428
	turnRight()
429
	
430
	-- return to start
431
	say("return to start");
432
	if bBoneMeal then
433
		moveForward(6,true)
434
	else
435
		moveForward(4,true)
436
	end
437
end
438
-- --------------------------------
439
440
441
-- ----------------------------------------
442
-- Main -----------------------------------
443
-- ----------------------------------------
444
445
local function mainRun() 
446
447
	-- go to restock position
448
	say("go to restock position")
449
	turnRight()
450
451
	-- dump any contents
452
	say("dump any contents")
453
	dump(false)
454
	-- wait?
455
	-- restock
456
	say("restock")
457
	restock()
458
459
	-- face in original position
460
	say("face in original position")
461
	turnRight()
462
463
464
	-- check fuel
465
	nFuel = turtle.getFuelLevel()
466-
	if nFuel < 100 then
466+
	bHasFuel = true
467
468-
		turtle.refuel(2)
468+
	while nFuel < 100 and bHasFuel == true do
469-
		sleep(1)
469+
470
		bHasFuel = turtle.refuel(1)
471-
		if nFuel < 100 then
471+
472-
			say("Fuel level below minimum treshold of 100")
472+
473
474
	if bHasFuel == false then
475
		say("Fuel level below minimum treshold of 100")
476
		return false
477
	end
478
479
	-- inital checks if everything is on board
480
481
	-- check for saplings
482
	if scanForSaplings() == false then
483
		say("We have no saplings")
484
		return false
485
	end
486
487
	-- check for BoneMeal
488
	if bBoneMeal == true and scanForBoneMeal() == 0 then
489
		say("We have no bonemeal. Continue, but disable feature.")
490
		bBoneMeal = false
491
	end
492
493
	-- check if space in inventory
494
	if spaceLeft() == 0 then
495
		say("Inventory is full. Need attention.")
496
		return false
497
	end
498
499
500
501
	-- move to first position ------------------------
502
	if moveForward(2,true) == false then
503
		say("Move(1): Failed to move forward")
504
		return false
505
	end
506
507
	turnRight()
508
509
	if moveForward(3,true) == false then
510
		say("Move(1,2): Failed to move forward")
511
		return false
512
	end
513
514
	turnLeft()
515
516
	-- check for tree
517
	if detectTree() == true then
518
		harvestTree()
519
		plantTree(true)
520
	elseif turtle.detect() == false then
521
		say("No Tree found. Planting")
522
		plantTree(false)
523
	elseif bBoneMeal == true then
524
		applyBoneMeal()
525
	end
526
527
	turnRight()
528
529-
	if moveForward(7,true) == false then
529+
530
531
	if moveForward(4,true) == false then
532
		say("Move(2,1): Failed to move forward")
533
		return false
534
	end
535
536
	turnLeft()
537
538
	-- check for tree
539
	if detectTree() == true then
540
		harvestTree()
541
		plantTree(true)
542
	elseif turtle.detect() == false then
543
		say("No Tree found. Planting")
544
		plantTree(false)
545
	elseif bBoneMeal == true then
546
		applyBoneMeal()
547
	end
548
549-
	-- move to third position ------------------------
549+
550
551
	-- move to second position ------------------------
552
553
	if moveForward(4,true) == false then
554
		say("Move(3,1): Failed to move forward")
555
		return false
556
	end
557
558
	turnLeft()
559-
	if moveForward(9,true) == false then
559+
560
	-- check for tree
561
	if detectTree() == true then
562
		harvestTree()
563
		plantTree(true)
564
	elseif turtle.detect() == false then
565
		say("No Tree found. Planting")
566
		plantTree(false)
567
	elseif bBoneMeal == true then
568
		applyBoneMeal()
569
	end
570
571
	turnRight()	
572
573
	-- move to 4th position ------------------------
574
575
576
	if moveForward(1,true) == false then
577
		say("Move(4,1): Failed to move forward")
578
		return false
579
	end
580
581
	turnLeft()
582
583
	if moveForward(6,true) == false then
584
		say("Move(3,2): Failed to move forward")
585
		return false
586
	end
587
588-
	if moveForward(7,true) == false then
588+
589
590
	if moveForward(1,true) == false then
591
		say("Move(3,1): Failed to move forward")
592
		return false
593
	end
594
595
	turnLeft()
596
597
	-- check for tree
598
	if detectTree() == true then
599
		harvestTree()
600
		plantTree(true)
601
	elseif turtle.detect() == false then
602
		say("No Tree found. Planting")
603
		plantTree(false)
604
	elseif bBoneMeal == true then
605
		applyBoneMeal()
606
	end
607
608
	turnRight()
609
610
	-- move to fourth position ------------------------
611
612
	if moveForward(4,true) == false then
613
		say("Move(2,1): Failed to move forward")
614
		return false
615
	end
616
617-
	if moveForward(11,true) == false then
617+
618
619
	-- check for tree
620
	if detectTree() == true then
621
		harvestTree()
622
		plantTree(true)
623
	elseif turtle.detect() == false then
624
		say("No Tree found. Planting")
625
		plantTree(false)
626
	elseif bBoneMeal == true then
627
		applyBoneMeal()
628
	end
629
630
	turnRight()
631
632
	-- move to fourth position ------------------------
633
634
	if moveForward(4,true) == false then
635
		say("Move(2,1): Failed to move forward")
636
		return false
637
	end
638
639
	turnLeft()
640
641
	-- check for tree
642
	if detectTree() == true then
643
		harvestTree()
644
		plantTree(true)
645
	elseif turtle.detect() == false then
646
		say("No Tree found. Planting")
647
		plantTree(false)
648
	elseif bBoneMeal == true then
649
		applyBoneMeal()
650
	end
651
652
	turnRight()
653
654
	-- return ------------------------
655
656
	if moveForward(3,true) == false then
657
		say("Move(4,1): Failed to move forward")
658
		return false
659
	end
660
661
	turnLeft()
662
663
	if moveForward(8,true) == false then
664
		say("Move(4,2): Failed to move forward")
665
		return false
666
	end
667
668
--	say("Should be in home position, facing chest. Dumping stuff.")
669
670
	say("Dumping stuff.")
671
	dump(false)
672
	say("Face start position")	
673
	turnLeft()
674
	local c = turtle.getFuelLevel()
675
	c = nFuel - c
676
	say("==== Run "..nRuns.." complete. "..c.." E consumed ====")
677
	nRuns = nRuns + 1
678
	return true
679
end
680
681
682
-- -------------------------
683
-- init wireless
684
----------------------------
685
if bUserModem == true then
686
  bRedNet = rednet.open("right")
687
  if not bRedNet == true then
688
	bRedNet = rednet.open("left")
689
  end
690
  bRedNet = true
691
end
692
693
694
695
if bRedNet == true then
696
	say("Rednet enabled")
697
	rednet.send(nMasterComputerID, "HarvestBot 01 reporting in.")
698
else 
699
	say("no rednet available")
700
end
701
702
-- check fuel
703
nFuel = turtle.getFuelLevel()
704
bHasFuel = true
705
706
while nFuel < 100 and bHasFuel == true do
707
	turtle.select(1)
708
	bHasFuel = turtle.refuel(1)
709
	nFuel = turtle.getFuelLevel()
710
end
711
712
if bHasFuel == false then
713
	say("Fuel level below minimum treshold of 100")
714
	return false
715
end
716
717
while bNoError == true do
718
    print ("Starting Run "..(nRuns+1))
719
    bNoError = mainRun()
720
	nFuel = turtle.getFuelLevel()
721
	print ("Fuel level: "..nFuel)
722
	if bNoError == true then
723
		say("Sleeping for 60 seconds")
724
		sleep(10)
725
		say("Sleeping for 50 seconds")
726
		sleep(10)
727
		say("Sleeping for 40 seconds")
728
		sleep(10)
729
		say("Sleeping for 30 seconds")
730
		sleep(10)
731
		say("Sleeping for 20 seconds")
732
		sleep(10)
733
		say("Sleeping for 10 seconds")
734
		sleep(10)
735
	else
736
		say("Aborting after "..nRuns.." runs")
737
	end
738
end