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