View difference between Paste ID: JYYEHcy5 and WKYUFbqd
SHOW: | | - or go back to the newest paste.
1
--todo: 
2
--		autorefuel from found coal
3
--		fix the edge extra block
4
--		display current fuel level while refueling
5
--		refueling during doChest etc
6
--		maybe refuel limit = 1
7-
--needs update/test
7+
-- WKYUFbqd
8
-- GeyqYHnM
9
-- i6J1bA3Q
10
-- sCpfzzgV
11
-- xq91ssuy
12
-- aC20GtgU
13
-- gK7A19W4
14
-- pGdmLjjr
15
-- eYCve9Eb
16
-- ge4QnKbY
17
18
19
local programState = "done"
20
local refuelingState = ""
21
22
local curX = 0
23
local curY = 0
24
local curZ = 0
25
local subStep = 0
26
local isReversed = false
27
28
local sizeX = 0
29
local sizeY = 0
30
local sizeZ = 0
31
32
local useEnderChest = false
33
local useEnderFuel = false
34
local useChest = false
35
local useCobblestone = false
36
local useCoal = false
37
38
local saveFile = "savedata"
39
40
local outputSlot = 0
41
local cobbleSlot = 0
42
43
local function getYesNo()
44
	local i
45
	local event, param
46
47
	while true do
48
		event, param = os.pullEvent("key")
49
50
		if param == 21 then
51
			return true
52
		end
53
		if param == 49 then
54
			return false
55
		end
56
	end
57
end                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
58
59
local function requestFuel()
60
	local i
61
	local c = false
62
	local event, param
63
64
	if useCoal and turtle.getItemCount(coalSlot) > 1 then
65
		turtle.select(coalSlot)
66
		if turtle.refuel(1) then
67
			turtle.select(cobbleSlot)
68
			return true
69
		end
70
	end
71
72
	print("Press F to refuel from slot 16")
73
	print("Press Q to exit the program")
74
	
75
	if turtle.getFuelLevel() >= 64 then
76
		c = true
77
		print("Press C to continue quarry")
78
	end
79
80
	while true do
81
		event, param = os.pullEvent("key")
82
83
		if param == 33 then --f
84
			turtle.select(16)
85
			turtle.refuel()
86
			if not c and turtle.getFuelLevel() >= 64 then
87
				c = true
88
				print("Press C to continue quarry")
89
			end
90
		end
91
		if param == 16 then --q
92
			return false
93
		end
94
		if c and param == 46 then --c
95
			turtle.select(cobbleSlot)
96
			return true
97
		end
98
	end
99
end
100
101
102
local function restoreProgress()
103
	if fs.exists(saveFile) then
104
		local file = fs.open(saveFile,"r")
105
106
		programState = tostring(file.readLine())
107
		refuelingState = tostring(file.readLine())
108
		
109
		curX = tonumber(file.readLine())
110
		curY = tonumber(file.readLine())
111
		curZ = tonumber(file.readLine())
112
		subStep = tonumber(file.readLine())
113
		sizeX = tonumber(file.readLine())
114
		sizeY = tonumber(file.readLine())
115
		sizeZ = tonumber(file.readLine())
116
117
		if file.readLine() == "true" then isReversed = true else isReversed = false end
118
	
119
		if file.readLine() == "true" then useEnderChest = true else useEnderChest = false end
120
		if file.readLine() == "true" then useEnderFuel = true else useEnderFuel = false end
121
		if file.readLine() == "true" then useChest = true else useChest = false end
122
		if file.readLine() == "true" then useCobblestone = true else useCobblestone = false end
123
		if file.readLine() == "true" then useCoal = true else useCoal = false end
124
125
		file.close()
126
	end
127
end
128
129
local function saveProgress()
130
	local file = fs.open(saveFile,"w")
131
132
	file.write(tostring(programState).."\n")
133
	file.write(tostring(refuelingState).."\n")
134
	
135
	file.write(tostring(curX).."\n")
136
	file.write(tostring(curY).."\n")
137
	file.write(tostring(curZ).."\n")
138
	file.write(tostring(subStep).."\n")
139
140
	file.write(tostring(sizeX).."\n")
141
	file.write(tostring(sizeY).."\n")
142
	file.write(tostring(sizeZ).."\n")
143
	
144
	file.write(tostring(isReversed).."\n")
145
146
	file.write(tostring(useEnderChest).."\n")
147
	file.write(tostring(useEnderFuel).."\n")
148
	file.write(tostring(useChest).."\n")
149
	file.write(tostring(useCobblestone).."\n")
150
	file.write(tostring(useCoal).."\n")
151
152
	file.close()
153
end
154
155
local function moveForward()
156
	turtle.dig()
157
	while not turtle.forward() do		
158
		sleep(0)
159
		turtle.dig()
160
	end
161
end
162
local function moveUp()
163
	turtle.digUp()
164
	while not turtle.up() do		
165
		sleep(0)
166
		turtle.digUp()
167
	end
168
end
169
local function moveDown()
170
	turtle.digDown()
171
	while not turtle.down() do		
172
		sleep(0)
173
		turtle.digDown()
174
	end
175
end
176
177
local function doRefuel()
178
	refuelingState = "refuel"
179
	saveProgress()
180
181
	while turtle.getFuelLevel() < 64 do
182
183
		if turtle.getItemCount(2) > 0 then
184
			turtle.select(2)
185
			while not turtle.placeUp() do
186
				sleep(0)
187
				turtle.digUp()
188
			end
189
		end
190
	
191
		turtle.select(16)
192
		turtle.drop()
193
		for maxstacks = 1, 1 do
194
			turtle.suckUp()
195
			if not turtle.refuel() then turtle.drop() end
196
		end
197
	end
198
	
199
	turtle.select(2)
200
	turtle.digUp()
201
	refuelingState = ""
202
	saveProgress()
203
end
204
205
local function doEmpty()
206
	refuelingState = "empty"
207
	saveProgress()
208
209
	if turtle.getItemCount(1) > 0 then
210
		turtle.select(1)
211
		while not turtle.placeUp() do
212
			sleep(0)
213
			turtle.digUp()
214
		end
215
	end
216
	
217
	for slot = outputSlot, 16 do
218
		while turtle.getItemCount(slot) > 0 do
219
			turtle.select(slot)
220
			turtle.dropUp()
221
			sleep(1)
222
		end
223
	end
224
225
	turtle.select(1)
226
	turtle.digUp()
227
	refuelingState = ""
228
	saveProgress()
229
end
230
231
local function doChest()	
232
	programState = "chest"
233
	saveProgress()
234
	
235
	--orient toward x
236
	local limit
237
		
238
	limit = 1
239
	while subStep < limit do
240
		subStep = subStep + 1
241
		saveProgress()
242
		if (curX % 2 == 0) == isReversed then
243
			--facing = 1
244
			turtle.turnRight()
245
		else
246
			--facing = 3
247
			turtle.turnLeft()
248
		end
249
	end
250
	
251
	limit = limit + curY
252
	while subStep < limit do
253
		subStep = subStep + 1
254
		saveProgress()
255
		moveUp()
256
	end
257
	
258
	limit = limit + curX
259
	while subStep < limit do
260
		subStep = subStep + 1
261
		saveProgress()
262
		moveForward()
263
	end
264
265
	limit = limit + 1
266
	while subStep < limit do
267
		subStep = subStep + 1
268
		saveProgress()
269
		turtle.turnLeft()
270
	end
271
	
272
	limit = limit + curZ
273
	while subStep < limit do
274
		subStep = subStep + 1
275
		saveProgress()
276
		moveForward()
277
	end
278
	
279
	limit = limit + 1
280
	while subStep < limit do
281
		for slot = outputSlot, 16 do
282
			while turtle.getItemCount(slot) ~= 0 do
283
				turtle.select(slot)
284
				turtle.drop()
285
				sleep(1)
286
			end
287
		end		
288
		subStep = subStep + 1
289
		saveProgress()
290
	end
291
	
292
	limit = limit + 2
293
	while subStep < limit do
294
		subStep = subStep + 1
295
		saveProgress()
296
		turtle.turnRight()
297
	end
298
	
299
	limit = limit + curZ
300
	while subStep < limit do
301
		subStep = subStep + 1
302
		saveProgress()
303
		moveForward()
304
	end
305
	
306
	limit = limit + 1
307
	while subStep < limit do
308
		subStep = subStep + 1
309
		saveProgress()
310
		turtle.turnRight()
311
	end
312
	
313
	limit = limit + curX
314
	while subStep < limit do
315
		subStep = subStep + 1
316
		saveProgress()
317
		moveForward()
318
	end
319
	
320
	limit = limit + curY
321
	while subStep < limit do
322
		subStep = subStep + 1
323
		saveProgress()
324
		moveDown()
325
	end
326
	
327
	limit = limit + 1
328
	while subStep < limit do
329
		subStep = subStep + 1
330
		saveProgress()
331
		if (curX % 2 == 0) == isReversed then
332
			--facing = 1
333
			turtle.turnRight()
334
		else
335
			--facing = 3
336
			turtle.turnLeft()
337
		end
338
	end
339
	
340
	turtle.select(cobbleSlot)
341
	programState = "quarry"
342
	subStep = 0
343
	saveProgress()
344
end
345
346
local function doReturn()	
347
	programState = "return"
348
	saveProgress()
349
	
350
	--orient toward x
351
	local limit
352
		
353
	limit = 1
354
	while subStep < limit do
355
		subStep = subStep + 1
356
		saveProgress()
357
		if (curX % 2 == 0) == isReversed then
358
			--facing = 1
359
			turtle.turnRight()
360
		else
361
			--facing = 3
362
			turtle.turnLeft()
363
		end
364
	end
365
	
366
	limit = limit + curY
367
	while subStep < limit do
368
		subStep = subStep + 1
369
		saveProgress()
370
		moveUp()
371
	end
372
	
373
	limit = limit + curX
374
	while subStep < limit do
375
		subStep = subStep + 1
376
		saveProgress()
377
		moveForward()
378
	end
379
380
	limit = limit + 1
381
	while subStep < limit do
382
		subStep = subStep + 1
383
		saveProgress()
384
		turtle.turnLeft()
385
	end
386
	
387
	limit = limit + curZ
388
	while subStep < limit do
389
		subStep = subStep + 1
390
		saveProgress()
391
		moveForward()
392
	end
393
	
394
	if useChest then
395
		limit = limit + 1
396
		while subStep < limit do
397
			for slot = outputSlot, 16 do
398
				while turtle.getItemCount(slot) ~= 0 do
399
					turtle.select(slot)
400
					turtle.drop()
401
					sleep(0)
402
				end
403
			end		
404
			subStep = subStep + 1
405
			saveProgress()
406
		end
407
	end
408
	
409
	turtle.turnRight()
410
	turtle.turnRight()
411
	turtle.select(1)
412
	programState = "done"
413
	subStep = 0
414
	saveProgress()
415
end
416
417
local function checkInventory()
418
419
	if turtle.getItemCount(16) == 0 then
420
		return
421
	end
422
		
423
	if useCobblestone then
424
		turtle.select(cobbleSlot)
425
		turtle.drop(turtle.getItemCount(cobbleSlot) - 1)
426
		for slot = outputSlot, 16 do	
427
			while turtle.compareTo(slot) do
428
				turtle.select(slot)
429
				turtle.drop()
430
431
				for swap = 16, slot, -1 do				
432
					if turtle.getItemCount(swap) > 0 then
433
						turtle.select(swap)
434
						turtle.transferTo(slot)
435
						break
436
					end
437
				end
438
				
439
				turtle.select(cobbleSlot)
440
			end
441
		end
442
		if turtle.getItemCount(16) == 0 then
443
			return
444
		end
445
	end
446
447
448
	if useEnderChest then
449
		doEmpty()
450
	elseif useChest then
451
		doChest()
452
	else
453
		print("Inventory Full")
454
		local full = true
455
		while full do
456
			full = false
457
			sleep(5)
458
			for slot = outputSlot, 16 do
459
				if turtle.getItemCount(slot) > 0 then
460
					full = true
461
					break
462
				end
463
			end
464
		end
465
	end
466
467
end
468
469
local function doQuarryDig()
470
471
	if turtle.getFuelLevel() < 64 then
472
		if useEnderFuel then
473
			doRefuel()
474
		else
475
			if not requestFuel() then
476
				programState = "done"
477
				return
478
			end
479
		end
480
	end
481
482
	if curY > 0 then
483
		checkInventory()
484
		turtle.digUp()
485
	end
486
	if curY < sizeY - 1 then
487
		checkInventory()
488
		turtle.digDown()
489
	end
490
491
	checkInventory()
492
493
end
494
495
local function doQuarry()
496
497
	turtle.select(cobbleSlot)
498
499
while true do
500
501
	
502
	if curX % 2 == 0 then
503
		if isReversed then
504
		
505
			if curZ == 0 then
506
				if curX == 0 then
507
					if curY >= sizeY - 2 then
508
						programState = "return"
509
						return
510
					else
511
						if subStep < 3 then
512
							if curY > 0 then
513
								turtle.digUp()
514
							end
515
							subStep = subStep + 1
516
							if curY < sizeY - 2 then
517
								curY = curY + 1
518
								saveProgress()
519
								moveDown()
520
							end
521
						elseif subStep == 3 then
522
							subStep = subStep + 1
523
							saveProgress()
524
							turtle.turnRight()
525
						elseif subStep == 4 then
526
							subStep = 0
527
							isReversed = false
528
							saveProgress()
529
							turtle.turnRight()
530
						end
531
					end
532
				else
533
					if subStep == 0 then
534
						subStep = subStep + 1
535
						saveProgress()
536
						turtle.turnRight()
537
					elseif subStep == 1 then
538
						doQuarryDig()	
539
						subStep = subStep + 1
540
						saveProgress()
541
						moveForward()
542
					else
543
						subStep = 0
544
						curX = curX - 1
545
						saveProgress()
546
						turtle.turnRight()
547
					end
548
				end
549
			else
550
				doQuarryDig()	
551
				saveProgress()
552
				curZ = curZ - 1	
553
				moveForward()	
554
			end
555
556
		else
557
558
			if curZ == sizeZ - 1 then
559
				if curX == sizeX - 1 then
560
					if curY >= sizeY - 2 then
561
						programState = "return"
562
						return
563
					else
564
						if subStep < 3 then
565
							if curY > 0 then
566
								turtle.digUp()
567
							end
568
							subStep = subStep + 1
569
							if curY < sizeY - 2 then
570
								curY = curY + 1
571
								saveProgress()
572
								moveDown()
573
							end
574
						elseif subStep == 3 then
575
							subStep = subStep + 1
576
							saveProgress()
577
							turtle.turnRight()
578
						elseif subStep == 4 then
579
							subStep = 0
580
							isReversed = true
581
							saveProgress()
582
							turtle.turnRight()
583
						end
584
					end
585
				else
586
					if subStep == 0 then
587
						subStep = subStep + 1
588
						saveProgress()
589
						turtle.turnRight()
590
					elseif subStep == 1 then
591
						doQuarryDig()	
592
						subStep = subStep + 1
593
						saveProgress()
594
						moveForward()
595
					else
596
						subStep = 0
597
						curX = curX + 1
598
						saveProgress()
599
						turtle.turnRight()
600
					end
601
				end
602
			else
603
				doQuarryDig()	
604
				curZ = curZ + 1
605
				saveProgress()
606
				moveForward()				
607
			end
608
609
		end
610
611
	else --  curX % 2 ~= 0
612
613
		if isReversed then
614
		
615
			if curZ == sizeZ - 1 then
616
				if curX == 0 then
617
					if curY >= sizeY - 2 then
618
						programState = "return"
619
						return
620
					else
621
						if subStep < 3 then
622
							if curY > 0 then
623
								turtle.digUp()
624
							end
625
							subStep = subStep + 1
626
							if curY < sizeY - 2 then
627
								curY = curY + 1
628
								saveProgress()
629
								moveDown()
630
							end
631
						elseif subStep == 3 then
632
							subStep = subStep + 1
633
							saveProgress()
634
							turtle.turnLeft()
635
						elseif subStep == 4 then
636
							subStep = 0
637
							isReversed = false
638
							saveProgress()
639
							turtle.turnLeft()
640
						end
641
					end
642
				else
643
					if subStep == 0 then
644
						subStep = subStep + 1
645
						saveProgress()
646
						turtle.turnLeft()
647
					elseif subStep == 1 then
648
						doQuarryDig()	
649
						subStep = subStep + 1
650
						saveProgress()
651
						moveForward()
652
					else
653
						subStep = 0
654
						curX = curX - 1
655
						saveProgress()
656
						turtle.turnLeft()
657
					end
658
				end
659
			else
660
				doQuarryDig()	
661
				curZ = curZ + 1
662
				saveProgress()
663
				moveForward()				
664
			end
665
666
		else
667
		
668
			if curZ == 0 then
669
				if curX == sizeX - 1 then
670
					if curY >= sizeY - 2 then
671
						programState = "return"
672
						return
673
					else
674
						if subStep < 3 then
675
							if curY > 0 then
676
								turtle.digUp()
677
							end
678
							subStep = subStep + 1
679
							if curY < sizeY - 2 then
680
								curY = curY + 1
681
								saveProgress()
682
								moveDown()
683
							end
684
						elseif subStep == 3 then
685
							subStep = subStep + 1
686
							saveProgress()
687
							turtle.turnLeft()
688
						elseif subStep == 4 then
689
							subStep = 0
690
							isReversed = true
691
							saveProgress()
692
							turtle.turnLeft()
693
						end
694
					end
695
				else
696
					if subStep == 0 then
697
						subStep = subStep + 1
698
						saveProgress()
699
						turtle.turnLeft()
700
					elseif subStep == 1 then
701
						doQuarryDig()	
702
						subStep = subStep + 1
703
						saveProgress()
704
						moveForward()
705
					else
706
						subStep = 0
707
						curX = curX + 1
708
						saveProgress()
709
						turtle.turnLeft()
710
					end
711
				end
712
			else
713
				doQuarryDig()	
714
				curZ = curZ - 1
715
				saveProgress()
716
				moveForward()				
717
			end
718
719
		end
720
721
	end
722
723
end
724
end
725
726
local function getArgs()
727
728
	if sizeX == nil or sizeY == nil or sizeZ == nil then
729
		return false
730
	end
731
	
732
	sizeX = tonumber(sizeX)
733
	sizeY = tonumber(sizeY)
734
	sizeZ = tonumber(sizeZ)
735
	
736
	programState = "done"
737
	refuelingState = ""
738
	curX = 0
739
	curY = 0
740
	curZ = 0
741
	subStep = 0
742
	
743
	useEnderChest = false
744
	useEnderFuel = false
745
	useChest = false
746
	useCobblestone = false
747
748
	print("Excavate right " .. sizeX .. ", down " .. sizeY .. ", forward " .. sizeZ)
749
	print("Is this correct?")
750
	if not getYesNo() then
751
		return false
752
	end
753
754
	print("Use Ender Chest in slot 1 for output?")
755
	useEnderChest = getYesNo()
756
	if useEnderChest then
757
		print("Use Ender Chest in slot 2 for fuel?")
758
		useEnderFuel = getYesNo()
759
	else
760
		print("Use Chest behind turtle for output?")
761
		useChest = getYesNo()
762
	end
763
	
764
	if useEnderChest then
765
		if useEnderFuel then
766
			outputSlot = 3
767
		else
768
			outputSlot = 2
769
		end
770
	else
771
		outputSlot = 1
772
	end
773
	
774
	print("Discard Cobblestone? (requires cobblestone in slot" .. outputSlot .. ")")
775
	useCobblestone = getYesNo()
776
	cobbleSlot = outputSlot
777-
	if useCobbleStone then
777+
778
		outputSlot = outputSlot + 1
779
	end
780
781
	print("Use Mined Coal? (requires coal in slot" .. outputSlot .. ")")
782
	useCoal = getYesNo()
783
	coalSlot = outputSlot
784
785
	local fuelRequired = (sizeX * sizeZ) * math.ceil(sizeY / 3) + sizeY
786
787
	print("Fuel Required: " .. fuelRequired)
788
	print("Fuel Current: " .. turtle.getFuelLevel())
789
	if not useEnderFuel and turtle.getFuelLevel() < fuelRequired then
790
		if not requestFuel() then
791
			return false
792
		end
793
	end
794
795
	print("Start Quarry with these parameters?")
796
	if not getYesNo() then 
797
		return false
798
	end
799
	
800
	programState = "init"
801
	saveProgress()
802
	return true
803
end            
804
805
local function main()
806
	
807
	if useEnderChest then
808
		if turtle.getItemCount(1) == 0 then
809
			print("Place Ender Chest in slot 1")
810
			while turtle.getItemCount(1) == 0 do
811
				sleep(1)
812
			end
813
		end
814
		if useEnderFuel then
815
			if turtle.getItemCount(2) == 0 then
816
				print("Place Ender Chest in slot 2")
817
				while turtle.getItemCount(2) == 0 do
818
					sleep(1)
819
				end
820
			end
821
			outputSlot = 3
822
		else
823
			outputSlot = 2
824
		end
825
	else
826
		outputSlot = 1
827
	end
828
	cobbleSlot = outputSlot
829
	if useCobblestone then
830
		if turtle.getItemCount(cobbleSlot) == 0 then
831
			print("Place Cobblestone in slot " .. cobbleSlot)
832
			while turtle.getItemCount(cobbleSlot) == 0 do
833
				sleep(1)
834
			end
835
		end
836
		outputSlot = outputSlot + 1
837
	end
838
839
	coalSlot = outputSlot
840
	if useCoal then
841
		if turtle.getItemCount(coalSlot) == 0 then
842
			print("Place Coal in slot " .. coalSlot)
843
			while turtle.getItemCount(coalSlot) == 0 do
844
				sleep(1)
845
			end
846
		end
847
		outputSlot = outputSlot + 1
848
	end
849
850
	if refuelingState == "refuel" then
851
		doRefuel()
852
	end
853
854
	if refuelingState == "empty" then
855
		doEmpty()
856
	end
857
858
	while true do
859
	
860
		if programState == "quarry" then
861
			doQuarry()
862
		end
863
		if programState == "chest" then
864
			doChest()
865
		end
866
		if programState == "init" then
867
			if useEnderFuel then doRefuel() end
868
			turtle.select(cobbleSlot)
869
			if curY < sizeY - 1 then
870
				curY = curY + 1
871
				moveDown()
872
			end
873
			programState = "quarry"
874
		end
875
		if programState == "return" then
876
			doReturn()
877
			break
878
		end
879
880
	end
881
882
end                                            
883
884
885
restoreProgress()
886
if programState == "done" then
887
	sizeX, sizeY, sizeZ = ...
888
	if not getArgs() then
889
		print("Usage")
890
		print("    startup x y z")
891
		return
892
	end
893
end
894
895
main()