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