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