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