View difference between Paste ID: 3EPSHvn2 and CTGHjq4Z
SHOW: | | - or go back to the newest paste.
1
--variables
2
--len = shaft length
3
--notDig = number of blocks that are not mined in the shaft
4
--torch = location of torches
5
--curDist = distance from start
6
--heightPos = current relative height
7
--runProgram = what program has been selected for the turtle
8
--isAtHome = remember if you're at home
9
--height = shaft height
10
--miningLog = info about where the vein the miner is following
11
--curDir = current direction (in relative terms, 1 is forward, 2 is right, 3 is back, 4 is left)
12
--fuelSlot = slot where fuel was last found, 0 if none
13
local len = 1
14
local notDig = 3
15
local torch = 16 - notDig
16
local curDist = 0
17
local heightPos = 0
18
local runProgram = "main"
19
local isAtHome = true
20
local height = 2
21
local miningLog = {}
22
local curDir = 1
23
local fuelSlot = 0
24
25
26
--clear the screen
27
function clrScr()
28
	term.clear()
29
	term.setCursorPos(1,1)
30
end
31
32
--argument parsing
33
local tArgs = {...}
34
if #tArgs > 3 or #tArgs == 0 then
35
	print( "Type 'shaft help' for instructions" )
36
elseif #tArgs == 1 then
37
	if tonumber(tArgs[1]) ~= nil then
38
		runProgram = "main"
39
		len = tonumber( tArgs[1] )
40
	else
41
		runProgram = tArgs[1]	
42
	end
43
elseif #tArgs == 3 then
44
	if tonumber(tArgs[1]) ~= nil or tonumber(tArgs[2]) ~= nil or tonumber(tArgs[3]) ~= nil then
45
		len = tonumber( tArgs[1] )
46
		height = tonumber( tArgs[2] )
47
		notDig = tonumber ( tArgs[3] )
48
		torch = 16 - notDig
49
	else
50
		runProgram = "help"
51
	end
52
elseif #tArgs == 2 then
53
	if tonumber(tArgs[1]) ~= nil or tonumber(tArgs[2]) ~= nil then
54
		len = tonumber( tArgs[1] )
55
		height = tonumber( tArgs[2] )
56
	else
57
		runProgram = "help"
58
	end	
59
end
60
61
62
63
--help output
64
function runHelp()
65
	clrScr()
66
	print("Usage: shaft [length] [height] [blocks]")
67
	print("Length[0+] and height[1-60] are the dimensions of the mining shaft.")
68
	print("Blocks is the number of blocks that will not be mined [range: 0-10]. By default it's 3 for stone, gravel and dirt and they need to be put in slots 16, 15 and 14")
69
	print("Enter for more...")
70
	io.read()
71
	term.clear()
72
	print("Torches are placed every 8 blocks and go 1 slot before the ores that won't be mined, by default 13.")
73
	print("Place chest underneath the starting position.")
74
	print("use 'shaft init' to make a starting room")
75
	print("Enter for more...")
76
	io.read()
77
	term.clear()
78
	print("'shaft left' will move the turtle 4 blocks to the left to start a new mining shaft.")
79
	print("'shaft right' will do the same thing on the right side.")
80
	print("While repositioning it also mines a 2 high and 3 wide tunnel.")
81
	print("Enter to continue.")
82
	io.read()
83
end
84
85
--build the starting room
86
function initRoom()
87
	refuel(30, "Please input fuel to continue and press enter.")
88
	turtle.select(1)
89
	mineUp()
90
	digAndMove()
91
	turtle.digDown()
92
	turtle.turnLeft()
93
	digAndMove()
94
	turtle.digDown()
95
	turtle.turnLeft()
96
	digAndMove()
97
	turtle.digDown()
98
	digAndMove()
99
	turtle.digDown()
100
	turtle.back()
101
	turtle.turnRight()
102
	turtle.back()
103
	turtle.down()
104
	chestCheck()
105
end
106
107
--checks if there's a chest(currently block) in front
108
function chestCheck()
109
	clrScr()
110
	while not turtle.detect() do
111
		print("Place down a chest in front of the turtle and press enter to finish.")
112
		io.read()
113
	end
114
115
	turtle.turnRight()
116
	clrScr()
117
	print("Starting position complete!")
118
end
119
120
--setup for another shaft on the left
121
function shaftLeft()
122
	refuel(30, "Please input fuel to continue and press enter.")
123
	turtle.turnLeft()
124
	clrScr()
125
	if turtle.detect() then
126
		print("WARNING! Clear out the chest before moving on! This is your only warning! Once you hit enter the turtle will move on!")
127
		io.read()
128
	end
129
	clrScr()
130
	print("Moving on to the left!")
131
	mineUp()
132
	for i = 1, 5 do
133
		digAndMove()
134
		turtle.digDown()
135
		turtle.turnLeft()
136
		digAndMove()
137
		turtle.digDown()
138
		turtle.back()
139
		turtle.turnLeft()
140
		turtle.turnLeft()
141
		digAndMove()
142
		turtle.digDown()
143
		turtle.back()
144
		turtle.turnLeft()
145
	end
146
	turtle.back()
147
	turtle.down()
148
	chestCheck()
149
end
150
151
--setup for another shaft on the right
152
function shaftRight()
153
	refuel(30, "Please input fuel to continue and press enter.")
154
	turtle.turnRight()
155
	clrScr()
156
	print("Moving on to the right!")
157
	mineUp()
158
	for i = 1, 5 do
159
		digAndMove()
160
		turtle.digDown()
161
		turtle.turnLeft()
162
		digAndMove()
163
		turtle.digDown()
164
		turtle.back()
165
		turtle.turnLeft()
166
		turtle.turnLeft()
167
		digAndMove()
168
		turtle.digDown()
169
		turtle.back()
170
		turtle.turnLeft()
171
	end
172
	turtle.back()
173
	turtle.down()
174
	turtle.turnLeft()
175
	turtle.turnLeft()
176
	chestCheck()
177
end
178
179
--refueling
180
function refuel(need, message)
181
	while turtle.getFuelLevel() < (need) do
182
		for n=1,(16-notDig) do
183
			local nCount = turtle.getItemCount(n)
184
			if nCount > 0 then
185
				turtle.select(n)
186
				refueled = turtle.refuel(1)
187
				while turtle.getFuelLevel() < need and refueled == true do
188
					refueled = turtle.refuel(1)
189
					if turtle.getFuelLevel() > need and refueled == true then
190
						if turtle.getItemCount(n) > 0 then fuelSlot = n end
191
						break
192
					end
193
				end
194
				if turtle.getFuelLevel() > need and refueled == true then
195
					if turtle.getItemCount(n) > 0 then fuelSlot = n end
196-
			print("Mining distance: ",curDist)
196+
					break
197
				end				
198
			end
199
		end
200
		if turtle.getFuelLevel() < need and isAtHome == true then
201
			print(message)
202
			print("Fuel level = ",turtle.getFuelLevel()," need: ",need)
203
			print("Mining distance,height,position: ",curDist,",",heightPos,",",#miningLog)
204
			io.read()
205
			refuel(need,message)
206
		elseif turtle.getFuelLevel() < need and isAtHome == false then
207
			goHome()
208
			emptyInventory()
209
			refuel(need,message)
210
			goBack()
211
		end
212
		clrScr()
213
		print("Turtle refuled. Returning to work...")
214
	end
215
end
216
217
--check if blocks that should not be mined and torches are inserted
218
function validateInv()
219
	while turtle.getItemCount(torch) < 2 do
220
		clrScr()
221
		print("")
222
		print("Please put torches into slot ",torch," and press enter to continue.")
223
		io.read()
224
	end
225
	while not blocksPresent do
226
		blocksPresent = true
227
		for i = 17-notDig, 16 do
228
			if turtle.getItemCount(i) == 0 then blocksPresent = false end
229
		end
230
		if not blocksPresent then
231
			print("Please put at least 1 block into slots ",(17-notDig),"-16 for the items you don't want to mine and press enter.")
232
			io.read()
233
		end
234
		clrScr()
235
	end
236
end
237
238
function mineUp()
239
	while not turtle.up() do
240
      if not turtle.digUp() then
241
		return false
242
	  end
243
	  sleep(0.5)
244
    end
245
	return true
246
end
247
248
function digAndMove()
249
	while not turtle.forward() do
250
		if not turtle.dig() then
251
			return false
252
		end
253
		sleep(0.5)
254
	end
255
	return true
256
end
257
258
--check the 6 sides around the miner and mine if you find ore, then move in and repeat
259
function mineOre()
260
	direction = detectOre()
261
	while #miningLog > 0 do
262
		checkInventory()
263
		refuel(curDist+heightPos+(#miningLog)+20,"Out of fuel. Moving back to base.")
264
		direction = detectOre()
265
		if direction == 7 then
266
			backtrack = miningLog[#miningLog]
267
			table.remove(miningLog,#miningLog)
268
			if backtrack == 5 then
269
				while not turtle.down() do end
270
				makeFile(curDist, heightPos, #miningLog, durDir)
271
			elseif backtrack == 6 then
272
				while not turtle.up() do end
273
				makeFile(curDist, heightPos, #miningLog, durDir)
274
			else
275
				while backtrack ~= curDir do
276
					turtle.turnLeft()
277
					curDir = curDir - 1
278
					if curDir == 0 then curDir = 4 end
279
				end
280
				while not turtle.back() do end
281
				makeFile(curDist, heightPos, #miningLog, durDir)
282
			end
283
		end
284
	end
285
	--curDir
286
end
287
288
--detect if there's ore around you and return the direction
289
function detectOre()
290
	dig = true
291
	if turtle.detectUp() then
292
		for i = (16-notDig), 16 do
293
			turtle.select(i)
294
			if turtle.compareUp() then dig = false end
295
		end
296
		if dig then
297
			if mineUp() then
298
				table.insert(miningLog,5)
299-
				turtle.down()
299+
				makeFile(curDist, heightPos, #miningLog, durDir)
300
				return 5
301
			end
302
		end
303
	end
304
	dig = true
305
	if turtle.detectDown() then
306
		for i = (16-notDig), 16 do
307
			turtle.select(i)
308
			if turtle.compareDown() then dig = false end
309
		end
310
		if dig then
311
			if turtle.digDown() then
312
				while not turtle.down() do end
313
				table.insert(miningLog,6)
314
				makeFile(curDist, heightPos, #miningLog, durDir)
315
				return 6
316
			end
317
		end
318
	end
319
	
320
	for i = 1, 4 do
321
		dig = true
322
		if turtle.detect() then
323
			for j = (16-notDig), 16 do
324
				turtle.select(j)
325
				if turtle.compare() then dig = false end
326
			end	
327
			if dig == true then 
328
				if digAndMove() then
329
					table.insert(miningLog,curDir)
330
					makeFile(curDist, heightPos, #miningLog, durDir)
331
					return curDir
332
				end
333
			end
334
		end
335
		turtle.turnRight()
336
		curDir = curDir + 1
337
		if curDir == 5 then curDir = 1 end
338
	end
339
	
340
	return 7
341
end
342
343
--torch placemen
344
function placeTorch()
345-
		refuel(curDist*2 + 150, "Not enough fuel to return to job. Insert fuel and press enter.")
345+
346
	i = curDist
347
	while i > 0 do
348
		i = i-8
349
	end
350
	if turtle.getItemCount(torch) < 2 then
351
		clrScr()
352
		print("Almost out of torches! Returning home...")
353
		goHome()
354
		emptyInventory()
355
		while turtle.getItemCount(torch) < 2 do
356
			print("")
357
			print("Please insert torches into slot ",torch," and press enter.")
358
			io.read()
359
		end
360
		refuel((len+1)+((len-curDist-1)*height)+height+20, "Not enough fuel to return to job. Insert fuel and press enter.")
361
		clrScr()
362
		print("Turtle ready! Resuming work...")
363
		goBack()
364
	end
365
	if i == 0 then
366
		while not turtle.back() do end
367
		turtle.placeUp()
368
		digAndMove()
369
	end
370
end
371
372
--return home
373
function goHome()
374
	if #miningLog > 0 then
375
		for i = #miningLog, 1, -1 do
376
			getDir = miningLog[i]
377
			if getDir == 6 then
378
				while not turtle.up() do end
379
			elseif getDir == 5 then
380-
		if turtle.getItemCount(i) > 0 then
380+
381
			else
382
				while getDir ~= curDir do
383
					turtle.turnRight()
384
					curDir = curDir + 1
385
					if curDir == 5 then curDir = 1 end
386
				end
387
				while not turtle.back() do end
388
			end
389
		end
390
	end
391
	if heightPos > 0 then
392
		i = heightPos
393
		while i ~= 0 do
394
			while not turtle.down() do end
395
			i = i - 1
396
		end
397
	end
398
	while curDir ~= 1 do
399
		turtle.turnRight()
400
		curDir = curDir + 1
401
		if curDir == 5 then curDir = 1 end
402
	end
403-
		refuel(curDist*2 + 150, "Not enough fuel to return to work. Insert fuel and press enter.")
403+
404
	while i ~= curDist do
405
		if turtle.back() then i = i+1 end
406
	end
407
	makeFile(0,0,0,1)
408
	isAtHome = true
409
end
410
411
--go back to work you lazy sucker!
412
function goBack()
413
	i=0
414
	while i ~= curDist do
415
		if turtle.forward() then i = i+1 end
416
	end
417
	if heightPos > 0 then
418-
	refuel(95, "Input fuel and press enter!")
418+
		i = heightPos
419
		while i ~= 0 do
420
			while not turtle.up() do end
421
			i = i - 1
422
		end
423
	end
424-
		refuel(curDist + 95, "Out of fuel, please refil and press enter.")
424+
	if #miningLog > 0 then
425
		for i = #miningLog, 1, -1 do
426
			getDir = miningLog[i]
427
			if getDir == 6 then
428
				while not turtle.down() do end
429
			elseif getDir == 5 then
430
				while not turtle.up() do end
431
			else
432
				while getDir ~= curDir do
433
					turtle.turnRight()
434
					curDir = curDir + 1
435-
		while i ~= height do
435+
					if curDir == 5 then curDir = 1 end
436
				end
437-
				i = i + 1
437+
				while not turtle.forward() do end
438-
				heightPos = heightPos + 1
438+
439
		end
440
	end
441
	makeFile(curDist, heightPos, #miningLog, durDir)
442
	isAtHome = false
443
end
444
445
--function to clean inventory
446
function emptyInventory()
447
	turtle.turnLeft()
448
	for i = 1, 15-notDig do
449
		turtle.select(i)
450
		if turtle.getItemCount(i) > 0 and i ~= fuelSlot then
451
			while not turtle.drop() do
452-
	i = 0
452+
453-
	while i ~= len do
453+
454-
		if turtle.back() then i = i + 1 end
454+
455
			end
456
		end
457
	end
458
	turtle.turnRight()
459
end
460
461
--check if there's a need to empty the inventory
462
function checkInventory()
463
	drop = 15-notDig
464
	tempCounter = 0
465
	for i = 1, drop do
466
		if turtle.getItemCount(i) > 0 then tempCounter = tempCounter + 1 end
467
	end
468
	if tempCounter == drop then
469
		clrScr()
470
		print("Inventory full, returning home.")
471
		goHome()
472
		emptyInventory()
473
		refuel((len+1)+((len-curDist-1)*height)+height+20, "Not enough fuel to return to job. Insert fuel and press enter.")
474
		clrScr()
475
		print("Turtle ready! Resuming work...")
476
		goBack()
477
	end
478
end
479
480
--main diging program
481
function mainDig() 
482
	clrScr()
483
	print("Orders received.")
484
	print("Starting to mine a shaft ",len," long with ",notDig," block exceptions and a height of ",height,".")
485
	print("")
486
	
487
	--validate the inventory before starting
488
	refuel(len*height+50, "Input fuel and press enter!")
489
	validateInv()
490
	
491
	--start diging the shaft 1 by 1
492
	for i = 1, len do
493
		checkInventory()
494
		refuel(curDist+heightPos + 20, "Out of fuel, please refil and press enter.")
495
		if i == 1 then isAtHome = false end
496
		digAndMove()
497
		curDist = curDist + 1
498
		makeFile(curDist, heightPos, #miningLog, durDir)
499
		mineOre()
500
		for i = 2, height do
501
			mineUp()
502
			heightPos = heightPos + 1
503
			makeFile(curDist, heightPos, #miningLog, durDir)
504
			mineOre()
505
		end
506
		i = 1
507
		while heightPos ~= 0 do
508
			if turtle.down() then
509
				heightPos = heightPos - 1
510
				makeFile(curDist, heightPos, #miningLog, durDir)
511
			end
512
		end
513
		placeTorch()
514
		if curDir ~= 1 then
515
			for i = 2, curDir do
516
				turtle.turnLeft()
517
			end
518
			curDir = 1
519
		end
520
	end
521
	
522
	--return to base
523
	print("Done mining. Returning home.")
524
	while curDist ~= 0 do
525
		if turtle.back() then
526
			curDist = curDist - 1
527
			makeFile(curDist, heightPos, #miningLog, durDir)
528
		end
529
	end
530
	
531
	emptyInventory()
532
	isAtHome = true
533
	
534
	--clrScr()
535
	print("Work complete!")
536
end
537
538
function makeFile(front, up, entry)
539
	local file = fs.open("shaft.txt","w")
540
	file.writeLine(front)
541
	file.writeLine(up)
542
	file.writeLine(entry)
543
	if entry > 0 then
544
		i = 0
545
		while i ~= entry do
546
			i = i + 1
547
			file.writeLine(miningLog[i])
548
		end
549
	end
550
	file.writeLine(curDir)
551
	file.close()
552
end
553
554
--choose what to run
555
function main()
556
	--clrScr()
557
	if runProgram == "init" then
558
		initRoom()
559
	elseif runProgram == "help" then
560
		runHelp()
561
	elseif runProgram == "left" then
562
		shaftLeft()
563
	elseif runProgram == "right" then
564
		shaftRight()
565
	else
566
		if len > 0 and notDig < 11 and notDig > -1 and height > 0 and height < 61 and #tArgs > 0 and #tArgs < 4 then
567
			mainDig()
568
		elseif #tArgs > 0 and #tArgs < 4 then
569
			print("Shaft needs to be at least 1 long, height needs to be between 1 and 60 and maximum block exception is 10.")
570
		end
571
	end
572
end
573
574
575
--and in the end, just run the main! :)
576
main()