View difference between Paste ID: hC3YAy5q and 6qmBBLpz
SHOW: | | - or go back to the newest paste.
1
--[[Copyright PrinceTommen - Script developed for CyanideEpic (twitch.tv/cyanideepic)]]--
2
--[[You are all allowed to use it as long as you don't pretend it's yours]]--
3
--[[Have fun !]]--
4
version =" 3.5"
5
--[[
6
Release Note:
7
3.5:	Added:	Multiple item slots system, including the items to throw (suggested by Portsanta and CyanideEpic)
8
		Major interface enhancements to make the configuration faster in spite of the new options
9
		Enhanced item shortage security, supporting the multiple slots
10
		New functions to manage I/O in a more compact way (suggested by Niseg)
11
3.41:	Fixed:	Important glitch when the turtle returned to its starting position with a certain configuration
12
		Displaying issues
13
3.4:	Added:	Favorite configuration system
14
		Ability to choose the direction of the series of parallel tunnels (right or left)
15
		Support of execution without torches or chests (will not try to place them)
16
		Security that stops the turtle when it runs out of chests or torches (suggested by CyanideEpic)
17
		Chests and torches status updated automatically
18
		Security that requires the user to press enter before the turtle starts (suggested by CyanideEpic)
19
		The turtle now returns to its starting position after it finishes
20
		New rotation function as well a a specific torch placing function
21
	Fixed:	The turtle will now properly finish the hub after mining an odd number of tunnels	
22
		The torch placement has been modified to avoid conflicts with chests
23
3.3:	Added:	Enderchest Support (suggested by Niseg and Daniteh)
24
		Release note
25
3.2:	Fixed:	Very important chest placing issue (only appeared in 3.1)
26
3.1:	Added:	New mining pattern for more efficiency (suggested by Niseg)
27
		Smarter fuel management:	Will now consume the "stored" fuel before refueling
28
						Can now consume any type of fuel supported by turtles (check the wiki)
29
						Fuel type can be changed while the turtle is mining
30
		Optimized the mining of 3 blocks high tunnels
31
		Better interface, instructions remain visible and a line is dedicated to the fuel status (updated automatically)
32
		Option to throw cobblestone automatically (suggested by Niseg)
33
	Fixed:	Refueling issue in certain circumstances (reported by CyanideEpic)			
34
]]--
35
function resetScreen()
36
	term.clear()
37
	term.setCursorPos(14,1)
38
	write("Mining Turtle")
39
	term.setCursorPos(5,2)
40
	write("For CyanideEpic and his friends")
41
	term.setCursorPos(1,13)
42
	write("By PrinceTommen, version "..version)
43
	term.setCursorPos(1,4)
44
end	  
45
function textOutput(output_message, x_screen_pos, z_screen_pos, clear_area)
46
	term.setCursorPos(x_screen_pos,z_screen_pos)
47
	if clear_area == 0 then
48
		clear_area = string.len(output_message)
49
	end	
50
	write(output_message..string.rep(" ", (clear_area - string.len(output_message))))
51
end
52
function securedInput(x_screen_pos, z_screen_pos, nature, lower_value, upper_value, example1, example2)
53
	example = {example1, example2}
54
	local function shortExample(example_int, example, boolStringPart)
55
		tableShortExample = {}
56-
		tableShortExample[example_int] = example[example_int].." ("..string.sub(string.lower(example[example_int]), 1, 1)..") "
56+
        local exampleIntThingy = example[example_int];
57
        local exampleIntLower = string.lower(example[example_int])
58
        local subbed = string.sub(exampleIntLower, 1, 1);
59
		tableShortExample[example_int] = exampleIntThingy .. " (" .. subbed .. ") "
60
		if boolStringPart then
61
			return string.sub(string.lower(example[example_int]), 1, 1) 
62
		else
63
			return tableShortExample[example_int]
64
		end	
65
	end
66
	incipit = shortExample(1, example, false).."/ "..shortExample(2, example, false)..": "
67
	if nature == "text" then
68
		repeat
69
			textOutput(incipit, x_screen_pos, z_screen_pos, 39)
70
			term.setCursorPos(string.len(incipit)+1,z_screen_pos)	
71
			user_input = string.sub(string.lower(read()), 1, 1)
72
		until (user_input == shortExample(1, example, true) or user_input == shortExample(2, example, true))
73
	elseif nature == "integer" then
74
		repeat
75
			textOutput(" ", x_screen_pos, z_screen_pos, (39 - x_screen_pos))
76
			term.setCursorPos(x_screen_pos,z_screen_pos)
77
			user_input = tonumber(read())	
78
		until (user_input >= lower_value and user_input <= upper_value)
79
	end	
80
	return user_input
81
end
82
function clearLines(firstLine, lastLine)
83
	a = 1
84
	for a=1, (lastLine-firstLine+1) do
85
		textOutput("", 1, (firstLine+a-1), 40)
86
	end
87
end
88
function convertToBool(var, boolTrue)
89
	if var == boolTrue then
90
		var = true
91
	else
92
		var = false
93
	end
94
	return var
95
end
96
function turn(FacingAngle, Bool_rotation, Rotation_integer)
97
	if Bool_rotation then
98
		for u=1, Rotation_integer do
99
			turtle.turnRight()
100
		end
101
		FacingAngle = FacingAngle + Rotation_integer
102
	else
103
		for u=1, Rotation_integer do
104
			turtle.turnLeft()
105
		end
106
		FacingAngle = FacingAngle - Rotation_integer
107
	end
108
	FacingAngle = math.abs((FacingAngle - 1)%4+1)
109
	return FacingAngle
110
end
111
local function refuel()
112
	turtle.select(torches_slots+current_slot[2])
113
	while not(turtle.refuel(1)) do
114
		for f=1, fuel_slots do
115
			current_slot[2], shortage[2] = rotateSlot(2, torches_slots+1, fuel_slots)
116
			turtle.select(torches_slots+current_slot[2])
117
			if turtle.refuel(1) then
118
				boolRefuel = true
119
				break
120
			else
121
				boolRefuel = false
122
			end
123
		end
124
		if not(boolRefuel) then
125
			textOutput("No Fuel -", 1, 11, 0)
126
			current_slot[2], shortage[2] = manageShortage(2, torches_slots+1, torches_slots+fuel_slots) 
127
		end
128
	end
129
	refuel_count = 80 - turtle.getFuelLevel()
130
	textOutput("Fuel OK -", 1, 11, 0)
131
	return refuel_count  
132
end
133
function moveForward(FacingAngle, Boolfb, moving_integer, digUpBool, digDownBool, refuel_count)
134
	local moving_count = 1
135
	for moving_count=1,moving_integer do
136
		if (refuel_count == 80) then
137
			refuel_count = refuel()
138
		end
139
		Bool1 = false
140
		while not(Bool1) do
141
			if (Boolfb) then
142
				turtle.dig()
143
				Bool1 = turtle.forward()
144
				if (digUpBool) then
145
					turtle.digUp()
146
				end
147
				if (digDownBool) then
148
					turtle.digDown()
149
				end  
150
			else
151
				Bool1 = turtle.back()
152
				if not(Bool1) then
153
					turn(FacingAngle, true, 2)
154
					turtle.dig()
155
					turn(FacingAngle, false, 2)
156
				end
157
			end    
158
		end
159
		moving_count = moving_count + 1
160
		refuel_count = refuel_count + 1
161
	end 
162
	return refuel_count  
163
end
164
function moveUp(Boolud, moving_integer, refuel_count, Bool_DigFront)
165
	local moving_count = 1
166
	for moving_count=1, moving_integer do
167
		if (refuel_count == 80) then
168
			refuel_count = refuel()
169
		end
170
		Bool2 = false
171
		if Bool_DigFront then
172
			turtle.dig()
173
		end
174
		while not(Bool2) do
175
			if (Boolud) then
176
				turtle.digUp()   
177
				Bool2 = turtle.up()
178
			else
179
				turtle.digDown()
180
				Bool2 = turtle.down()
181
			end
182
		end
183
		moving_count = moving_count + 1
184
		refuel_count = refuel_count + 1
185
	end
186
	return refuel_count
187
end
188
function manageShortage(managedItem, initial_item_slot, final_item_slot)
189
	textOutput("The turtle has used all the "..(itemNames[managedItem+3]).." intitially given. Have you refilled all the "..(itemNames[managedItem+3]).." slots ?", 1, 4, 0)
190
	textOutput("Press enter if all the "..(itemNames[managedItem+3]).." slots are refilled (slots "..(initial_item_slot).." to "..(final_item_slot)..").", 1, 7, 0)
191
	repeat
192
		turn(FacingAngle, true, 4)
193
		os.startTimer(1)
194
		press, key = os.pullEvent()
195
	until (key == 28)
196
	clearLines(4,10)
197
	current_slot[managedItem] = 1
198
	shortage[managedItem] = false
199
	return current_slot[managedItem], shortage[managedItem]
200
end
201
function rotateSlot(managedItem, control_slot, rotation_controler)
202
	if (turtle.getItemCount(control_slot) == 0) or (managedItem == 2) then			
203
		if current_slot[managedItem]==rotation_controler and (managedItem ~= 2) then
204
			shortage[managedItem] = true
205
		else
206
			current_slot[managedItem]=((current_slot[managedItem])%rotation_controler)+1
207
		end
208
	end
209
	return current_slot[managedItem], shortage[managedItem]
210
end					
211
function inventoryManagement(refuel_count,Right_or_Left,throw_cobble)
212
	function fullInventory(n)
213
		n = m + 1
214
		repeat
215
			item_count = turtle.getItemCount(n)
216
			if (item_count ~= 0) then          
217
				boolSlotOccupied = true
218
				n = n + 1  
219
			else
220
				boolSlotOccupied = false  
221
			end  
222
		until (boolSlotOccupied == false) or (n == 17)
223
		return n
224
	end
225
	if Chest_approval then
226
		m = torches_slots + chests_slots + fuel_slots + garbage_slots
227
		thrown_slots = 0
228
		if (turtle.getItemCount(16) ~= 0) and (m~=16) then
229
			if fullInventory(m)==17 then
230
				if throw_stuff then
231
					for k=1, garbage_slots do
232
						for j=1, (16-m) do
233
							turtle.select(m - garbage_slots + k)
234
							Bool_match_stuff = turtle.compareTo(m+j)
235
							if Bool_match_stuff then
236
								thrown_slots = thrown_slots + 1
237
								turtle.select(m+j)
238
								turtle.drop()	 
239
							end
240
						end
241
						turtle.select(m - garbage_slots + k)
242
						turtle.drop(turtle.getItemCount(m - garbage_slots + k)-1)
243
					end	
244
					for z = (m+1), 16 do
245
						for u = (z+1), 16 do
246
							if turtle.getItemCount(u)~=0 then
247
								turtle.select(u)
248
								turtle.transferTo(z)
249
							end
250
						end
251
					end
252
				end
253
				if not(throw_stuff) or ((thrown_slots <= 2) and (fullInventory(n)>15)) then
254
					if shortage[3] then
255
						textOutput("No Chests", 24, 11, 0)
256
						current_slot[3], shortage[3] = manageShortage(3, torches_slots+fuel_slots+1, torches_slots+fuel_slots+chests_slots)
257
					end
258
					textOutput("Chests OK", 24, 11, 0)
259
					if (Right_or_Left == "left") then
260
						FacingAngle = turn(FacingAngle, true, 1)
261
					else
262
						FacingAngle = turn(FacingAngle, false, 1)
263
					end  
264
					refuel_count = moveForward(FacingAngle, true, 1, false, true, refuel_count)
265
					turtle.select(torches_slots+fuel_slots+current_slot[3])
266
					turtle.digDown()
267
					turtle.placeDown()
268
					for u=(m+1),16 do
269
						if turtle.getItemCount(u)~=0 then
270
							turtle.select(u)
271
							turtle.dropDown()
272
						end
273
					end
274
					if enderchest then
275
						turtle.select(torches_slots+fuel_slots+1)
276
						turtle.drop()
277
						turtle.digDown()
278
					end
279
					current_slot[3], shortage[3] = rotateSlot(3, torches_slots+fuel_slots+current_slot[3], chests_slots)
280
					refuel_count = moveForward(FacingAngle, false, 1, false, false, refuel_count)
281
					if (Right_or_Left == "left") then
282
						FacingAngle = turn(FacingAngle, false, 1)
283
					else
284
						FacingAngle = turn(FacingAngle, true, 1)
285
					end
286
				end	  
287
			end
288
		end
289
	end
290
	turtle.select(1)
291
	return refuel_count  
292
end
293
function placeTorch(Position)
294
	if Torch_approval then
295
		if shortage[1] then
296
			textOutput("No Torches -", 11, 11, 0)
297
			current_slot[1], shortage[1] = manageShortage(1, 1, torches_slots) 
298
		end
299
		textOutput("Torches OK -", 11, 11, 0)
300
		turtle.select(current_slot[1])
301
		if Position == "front" then
302
			turtle.dig()
303
			turtle.place()
304
		elseif Position ==	"below" then
305
			turtle.digDown()
306
			turtle.placeDown()
307
		elseif Position == "up" then
308
			turtle.digUp()
309
			turtle.placeUp()
310
		end
311
		current_slot[1], shortage[1] = rotateSlot(1, current_slot[1], torches_slots)
312
	end
313
end
314
function digVerticalLayer(refuel_count, FacingAngle, Width, Height, Height_Position, Bool_Torches, Right_or_Left)
315
	if Right_or_Left then
316
		Right_or_Left = "left"
317
	else
318
		Right_or_Left = "right"
319
	end	
320
	done_columns = 0
321
	if (Height_Position == "up") then
322
		for columns=1, math.floor(Width/4) do
323
			turtle.digUp()
324
			if (Height > 3) then
325
				refuel_count = moveUp(true, 1, refuel_count, false)
326
				turtle.dig()
327
				refuel_count = moveUp(false, (Height-2), refuel_count, true)
328
				turtle.digDown()
329
			end
330
			refuel_count = moveForward(FacingAngle, true, 2, true, true, refuel_count)
331
			refuel_count = inventoryManagement(refuel_count, Right_or_Left, throw_cobble)
332
			if (Height > 3) then
333
				refuel_count = moveUp(false, 1, refuel_count, false)
334
				turtle.dig()
335
				refuel_count = moveUp(true, (Height-2), refuel_count, true)
336
				turtle.digUp()
337
			end
338
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
339
			done_columns = done_columns + 1
340
			if (Width - 4*done_columns ~= 0) then
341
				refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
342
			end
343
		end  
344
		if ((Width - 4*math.floor(Width/4)) == 0) then
345
			Height_Position = "up"
346
		elseif ((Width - 4*math.floor(Width/4)) == 1) then
347
			turtle.digUp()
348
			refuel_count = moveUp(false, (Height-3), refuel_count, false)
349
			turtle.digDown()
350
			refuel_count = inventoryManagement(refuel_count, Right_or_Left, throw_cobble)
351
			Height_Position = "down"
352
		elseif ((Width - 4*math.floor(Width/4)) >= 2) then
353
			if (Height > 3) then
354
				refuel_count = moveUp(true, 1, refuel_count, false)
355
				turtle.dig()
356
				refuel_count = moveUp(false, (Height-2), refuel_count, true)
357
				turtle.digDown()
358
			end
359
			turtle.digUp()
360
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
361
			refuel_count = inventoryManagement(refuel_count, Right_or_Left, throw_cobble)
362
			Height_Position = "down"
363
			if ((Width - 4*math.floor(Width/4)) == 3) then
364
				refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
365
				refuel_count = moveUp(true, (Height - 3), refuel_count, false)
366
				turtle.digUp()
367
				Height_Position = "up"
368
			end
369
		end	
370
	elseif (Height_Position == "down") then
371
		for columns=1, math.floor(Width/4) do
372
			turtle.digDown()
373
			if (Height > 3) then
374
				refuel_count = moveUp(false, 1, refuel_count, false)
375
				turtle.dig()
376
				refuel_count = moveUp(true, (Height - 2), refuel_count, true)
377
				turtle.digUp()
378
			end
379
			refuel_count = moveForward(FacingAngle, true, 2, true, true, refuel_count)
380
			if (Height > 3) then
381
				refuel_count = moveUp(true, 1, refuel_count, false)
382
				turtle.dig()
383
				refuel_count = moveUp(false, (Height - 2), refuel_count, true)
384
				turtle.digDown()
385
			end
386
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
387
			done_columns = done_columns + 1
388
			if (Width - 4*done_columns ~= 0) then
389
				refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
390
			end
391
			refuel_count = inventoryManagement(refuel_count, Right_or_Left, throw_cobble)
392
			if (done_columns%2 == 0) and Bool_Torches then
393
				FacingAngle = turn(FacingAngle,true , 1)
394
				placeTorch("front")
395
				FacingAngle = turn(FacingAngle, false, 1)
396
			end
397
		end
398
		if ((Width - 4*math.floor(Width/4)) == 0) then
399
			Height_Position = "down"
400
		elseif ((Width - 4*math.floor(Width/4)) == 1) then
401
			turtle.digDown()	  
402
			refuel_count = moveUp(true, (Height - 3), refuel_count, false)
403
			turtle.digUp()
404
			Height_Position = "up"
405
		elseif ((Width - 4*math.floor(Width/4)) >= 2) then
406
			if (Height > 3) then
407
				refuel_count = moveUp(false, 1, refuel_count, false)
408
				turtle.dig()	  
409
				refuel_count = moveUp(true, (Height - 2), refuel_count, true)
410
				turtle.digUp()
411
			end
412
			turtle.digDown()
413
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
414
			Height_Position = "up"
415
			if ((Width - 4*math.floor(Width/4)) == 3) then
416
				refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
417
				refuel_count = moveUp(false, (Height - 3), refuel_count, false)
418
				turtle.digDown()
419
				refuel_count = inventoryManagement(refuel_count, Right_or_Left, throw_cobble)
420
				Height_Position = "down"
421
			end		
422
		end  	  
423
	end
424
	return refuel_count, Height_Position	  
425
end
426
427
enderchest, throw_stuff, Chest_approval, Torch_approval, Chest_mismatch, Torch_mismatch = false, false, false, false, false, false
428
shortage, itemNames = {false, false, false}, {"torch", "fuel", "chest", "torches", "fuel", "chests"}
429
430
resetScreen()
431
if (io.open("favorite", "r") ~= nil) then
432
		resetScreen()
433
		textOutput("Do you wish to use your favorite configuration ?", 1, 4, 0)
434
		Favorite_approval = securedInput(1, 6, "text", 0, 0, "Yes", "No")
435
	if (Favorite_approval == "y") then
436
		handle = fs.open("favorite", "r")
437
		input = handle.readAll()
438
		handle.close()
439
		favorite = textutils.unserialize(input)
440
		tunnels_integer = favorite.tunnels_integer
441
		Width = favorite.Width
442
		Height = favorite.Height
443
		Length = favorite.Length
444
		tunnels_separation = favorite.tunnels_separation
445
		throw_stuff = favorite.throw_stuff
446
		enderchest = favorite.enderchest
447
		Torch_approval = favorite.Torch_approval
448
		Chest_approval = favorite.Chest_approval
449
	end
450
end	
451
if (io.open("favorite", "r") == nil) or ((io.open("favorite", "r") ~= nil) and (Favorite_approval == "n")) then
452
	resetScreen()
453
	textOutput("Number of parallel tunnels ? ", 1, 4, 0)
454
	tunnels_integer = securedInput(37, 4, "integer", 1, 1000, " ", " ")
455
	textOutput("Width of the tunnels ? ", 1, 5, 0)
456
	Width = securedInput(37, 5, "integer", 1, 1000, " ", " ")
457
	term.setCursorPos(1,6)
458
	textOutput("Height of the tunnels ? ", 1, 6, 0)
459
	Height = securedInput(37, 6, "integer", 1, 200, " ", " ")
460
	if (Height < 3) then
461
		Height = 3
462
	end
463
	term.setCursorPos(1,7)
464
	textOutput("Length of the tunnels ? ", 1, 7, 0)
465
	Length = securedInput(37, 7, "integer", 1, 100000, " ", " ")
466
	if (tunnels_integer > 1) then
467
		term.setCursorPos(1,8)
468
		textOutput("Separating blocks between tunnels ? ", 1, 8, 0)
469
		tunnels_separation = securedInput(37, 8, "integer", 1, 1000, " ", " ")
470
	else
471
		tunnels_separation = 0
472
	end	
473
	
474
	resetScreen()
475
	textOutput("To use regular chests, press c", 1, 4, 0)
476
	textOutput("To use an enderchest, press e", 1, 5, 0)
477
	textOutput("To use torches, press t", 1, 6, 0)
478
	textOutput("To throw away specific items, press p", 1, 7, 0)
479
	textOutput("Press enter once you have chosen all the options you wanted to activate.", 1, 11, 0)
480
	while true do
481
		press, key = os.pullEvent()
482
		if press == "key" and key == 28 then
483
			break
484
		elseif key == 46 then
485
			if Chest_approval then
486
				Chest_approval = false
487
				textOutput("", 10, 9, 11)
488
			else	
489
				Chest_approval = true
490
				textOutput("Chests,", 10, 9, 11)
491
			end
492
		elseif key == 18 then
493
			if enderchest then
494
				enderchest = not(enderchest)
495
				textOutput("", 10, 9, 11)
496
			else
497
				Chest_approval = true
498
				enderchest = true
499
				textOutput("Enderchest,", 10, 9, 11)
500
			end
501
		elseif key == 20 then
502
			if Torch_approval then
503
				Torch_approval = false
504
				textOutput("", 1, 9, 8)
505
			else
506
				Torch_approval = true
507
				textOutput("Torches,", 1, 9, 8)
508
			end
509
		elseif key == 25 then
510
			if throw_stuff then
511
				throw_stuff = not(throw_stuff)
512
				textOutput("", 22, 9, 12)			
513
			else	
514
				throw_stuff = true
515
				textOutput("Throw items.", 22, 9, 12)
516
			end	
517
		end	
518
	end
519
	resetScreen()
520
	
521
	textOutput("Do you want to save this configuration as your favorite ?", 1, 4, 0)
522
	New_favorite = securedInput(1, 6, "text", 0, 0, "Yes", "No")
523
	
524
	if (New_favorite == "y") then
525
		favorite = {}
526
		favorite.tunnels_integer = tunnels_integer
527
		favorite.Width = Width
528
		favorite.Height = Height
529
		favorite.Length = Length 
530
		favorite.tunnels_separation = tunnels_separation
531
		favorite.Torch_approval = Torch_approval
532
		favorite.Chest_approval = Chest_approval
533
		favorite.throw_stuff = throw_stuff
534
		favorite.enderchest = enderchest
535
		output = textutils.serialize(favorite)
536
		handle = fs.open("favorite", "w")
537
		handle.write(output)
538
		handle.close()
539
	end
540
end
541
resetScreen()
542
textOutput("To manage extra slots, press s", 1, 4, 0)
543
textOutput("This option allows you to have several torches/fuel/chests slots, as well as different items to throw", 1, 6, 0)
544
textOutput("Else, press enter to skip this step.", 1, 10, 0)
545
torches_slots, chests_slots, garbage_slots = 0, 0, 0	
546
while true do
547
	press, key = os.pullEvent()
548
	if press == "key" and key == 28 then
549
		fuel_slots = 1
550
		break
551
	elseif key == 31 then
552
		repeat
553
			turtle.select(1)
554
			resetScreen()
555
			textOutput("Number of fuel slots ? ", 1, 4, 0)
556
			fuel_slots = securedInput(29, 4, "integer", 1, 16, " ", " ")	
557
			slot_total = fuel_slots
558
			if Torch_approval then	
559
				textOutput("Number of torches slots ? ", 1, 5, 0)
560
				torches_slots = securedInput(29, 5, "integer", 1, 16, " ", " ")
561
				slot_total = slot_total + torches_slots
562
			end	
563
			if Chest_approval  and not(enderchest) then	
564
				textOutput("Number of chests slots ? ", 1, 6, 0)
565
				chests_slots = securedInput(29, 6, "integer", 1, 16, " ", " ")
566
				slot_total = slot_total + chests_slots
567
			end	
568
			if throw_stuff then	
569
				textOutput("Number of undesired items ? ", 1, 7, 0)
570
				garbage_slots = securedInput(29, 7, "integer", 1, 16, " ", " ")
571
				slot_total = slot_total + garbage_slots
572
			end
573
		until (slot_total < 16)
574
		break
575
	end
576
end
577
resetScreen()
578
if (tunnels_integer > 1) then
579
	textOutput("The first tunnel will be in front of the turtle. Do you want the tunnels to be dug on the right or on the left of the first tunnel (They will be parallel to the first one) ?", 1, 4, 0)
580
	Bool_direction = securedInput(1, 9, "text", 0, 0, "Right", "Left")
581
end
582
if (tunnels_integer == 1) and (Width > 1) then
583
	textOutput("In front of the turtle will be one side of the tunnel. Do you want it to mine the rest on the left or on the right ?", 1, 4, 0)
584
	Bool_direction = securedInput(1, 9, "text", 0, 0, "Right", "Left")
585
end
586
resetScreen()
587
if Torch_approval then
588
	if torches_slots > 1 then
589
		textOutput("Torches in the slots 1 to "..torches_slots, 1, 4, 0) 
590
	else
591
		torches_slots = 1
592
		textOutput("Torches in the slot 1", 1, 4, 0)
593
	end
594
end
595
if fuel_slots > 1 then
596
	textOutput("Fuel in the slots "..(torches_slots+1).." to "..(torches_slots+fuel_slots), 1, 5, 0)
597
else
598
	fuel_slots = 1
599
	textOutput("Fuel in the slot "..(torches_slots+1), 1, 5, 0)
600
end
601
if Chest_approval  and not(enderchest) then
602
	if chests_slots > 1 then
603
		textOutput("Chests in the slots "..(torches_slots+fuel_slots+1).." to "..(torches_slots+fuel_slots+chests_slots), 1, 6, 0)
604
	else
605
		chests_slots = 1
606
		textOutput("Chests in the slot "..(torches_slots+fuel_slots+1), 1, 6, 0)
607
	end
608
end		
609
if enderchest then
610
	textOutput("The enderchest in the slot "..(torches_slots+fuel_slots+1), 1, 6, 0)
611
	chests_slots = 1
612
end
613
if throw_stuff then
614
	if garbage_slots > 1 then
615
		textOutput("Please make sure there are samples of the items to throw in the slots "..(torches_slots+fuel_slots+chests_slots+1).." to "..(torches_slots+fuel_slots+chests_slots+garbage_slots), 1, 8, 0)
616
	else
617
		garbage_slots = 1
618
		textOutput("Please make sure there is a sample of the item to throw in the slot "..(torches_slots+fuel_slots+chests_slots+1), 1, 8, 0)
619
	end 
620
end  
621
if (Bool_direction == "r") then
622
	Bool_direction = true
623
else
624
	Bool_direction = false
625
end
626
textOutput("Press enter to start", 1, 11, 0)
627
while true do
628
	press, key = os.pullEvent()
629
	if press == "key" and key == 28 then
630
		break
631
	end	
632-
if Torch_approval and (turtle.getItemCount(1) == 0) then
632+
633
resetScreen()
634
textOutput("", 1, 11, 20)
635
local itemsCounter = turtle.getItemCount(1)
636
if Torch_approval and (itemsCounter == 0) then
637
	textOutput("The torches slot is empty. Are you sure you want to use torches ?", 1, 4, 0)
638
	Torch_approval = convertToBool(securedInput(1, 6, "text", 0, 0, "Yes", "No"), "y")
639
elseif Torch_approval and (turtle.getItemCount(1) ~= 0) then
640
	for u = 1, torches_slots-1 do
641
		turtle.select(u+1)
642
		if  not(turtle.compareTo(1)) then
643
			Torch_mismatch = true
644
		end
645
	end
646
	if Torch_mismatch then
647
		resetScreen()
648
		textOutput("All the slots dedicated to the torches have not been set up correctly. Are you sure you want to use torches ?", 1, 4, 0)
649
		Torch_approval = convertToBool(securedInput(1, 7, "text", 0, 0, "Yes", "No"), "y")
650
	end
651
end
652
653
if Chest_approval and (turtle.getItemCount(torches_slots + fuel_slots + 1) == 0) then
654
	resetScreen()
655
	textOutput("The chests slot is empty. Are you sure you want to use chests ?", 1, 4, 0)
656
	Chest_approval = convertToBool(securedInput(1, 6, "text", 0, 0, "Yes", "No"), "y")
657
elseif Chest_approval and (turtle.getItemCount(torches_slots + fuel_slots + 1) ~= 0) then
658
	for u = 1, chests_slots-1 do
659
		turtle.select(torches_slots + fuel_slots + u + 1)
660
		if  not(turtle.compareTo(torches_slots + fuel_slots + 1)) then
661
			Chest_mismatch = true
662
		end
663
	end	
664
	if Chest_mismatch then
665
		resetScreen()
666
		textOutput("All the slots dedicated to the chests have not been set up correctly. Are you sure you want to use chests ?", 1, 4, 0)
667
		Chest_approval = convertToBool(securedInput(1, 7, "text", 0, 0, "Yes", "No"), "y")
668
	end
669
end
670
if Torch_approval then
671
	empty_torches_slots = 0
672
	for u = 1, torches_slots do
673
		if turtle.getItemCount(u) == 0 then
674
		    empty_torches_slots = empty_torches_slots + 1
675
		end
676
	end
677
	if empty_torches_slots == torches_slots then
678
		shortage[1] = true
679
	end
680
	textOutput("No Torches -", 11, 11, 0)
681
end	
682
if Torch_approval and (turtle.getItemCount(1) ~= 0) then
683
	shortage[1] = false
684
	textOutput("Torches OK -", 11, 11, 0)
685
end
686
if Chest_approval then
687
	empty_chests_slots = 0
688
	for u = 1, chests_slots do
689
		if turtle.getItemCount(torches_slots + fuel_slots + u) == 0 then
690
		    empty_chests_slots = empty_chests_slots + 1
691
		end
692
	end
693
	if empty_chests_slots == chests_slots then
694
		shortage[3] = true
695
	end
696
	textOutput("No Chests -", 24, 11, 0)
697
end	
698
if Chest_approval and (turtle.getItemCount(torches_slots + fuel_slots + 1) ~= 0) then
699
	shortage[3] = false
700
	textOutput("Chests OK -", 24, 11, 0)
701
end
702
textOutput("Fuel OK -", 1, 11, 0)
703
refuel_count = 80 - turtle.getFuelLevel()
704
FacingAngle, tunnel_forth, current_slot= 0, true, {1, 1, 1}
705
refuel_count = moveUp(true, 1, refuel_count, false)
706
if (Width == 1) then
707
	refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
708
end
709
for done_tunnels=1, tunnels_integer do
710
	if (Width >= 2) then
711
		for done_layers=1, math.ceil(Length/2) do
712
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
713
			FacingAngle = turn(FacingAngle, Bool_direction, 1)
714
			refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, Width, Height, "down", false, Bool_direction)
715
			FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
716
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
717
			FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
718
			refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, Width, Height, Height_Position, false, not(Bool_direction))
719
			FacingAngle = turn(FacingAngle, Bool_direction, 1)
720
			if (done_layers%4 == 0) then
721
				refuel_count = moveUp(false, 1, refuel_count, false)
722
				FacingAngle = turn(FacingAngle, Bool_direction, 1)
723
				placeTorch("front")
724
				FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
725
				refuel_count = moveUp(true, 1, refuel_count, false)
726
			end
727
		end
728
	elseif (Width == 1) then
729
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, 2*math.ceil(Length/2), Height, "down", true, Bool_direction) 
730
	end
731
	if (Height_Position == "up") then
732
		refuel_count = moveUp(false, (Height - 3), refuel_count, false)
733
		Height_Position = "down"
734
	end
735
	if tunnel_forth and (tunnels_integer - done_tunnels >= 1) then
736
		refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
737
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
738
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, ((2*Width)+tunnels_separation), Height, "down", false, not(Bool_direction))
739
		if (Height_Position == "up") then
740
			refuel_count = moveUp(false, (Height - 3), refuel_count, false)
741
			Height_Position = "down"
742
		end
743
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
744
		placeTorch("below")
745
	elseif not(tunnel_forth) then
746
		refuel_count = moveForward(FacingAngle, true, 1, true, false, refuel_count)
747
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
748
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, ((2*Width)-1+tunnels_separation), Height, "down", false, Bool_direction)
749
		FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
750
		refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
751
		FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
752
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, ((2*Width)-1+tunnels_separation), Height, Height_Position, false, not(Bool_direction))
753
		if (Height_Position == "up") then
754
			refuel_count = moveUp(false, (Height - 3), refuel_count, false)
755
			Height_Position = "down"
756
		end
757
		FacingAngle = turn(FacingAngle, Bool_direction, 2)
758
		refuel_count = moveForward(FacingAngle, true, (Width-2), true, true, refuel_count)
759
		placeTorch("front")
760
		FacingAngle = turn(FacingAngle, not(Bool_direction), 2)
761
		refuel_count = moveForward(FacingAngle, true, (Width-2), true, true, refuel_count)
762
		if (tunnels_integer - done_tunnels ~= 0) then
763
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
764
			refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, (tunnels_separation+1), Height, Height_Position, false, Bool_direction)
765
			FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
766
			refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
767
			FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
768
			refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, (tunnels_separation+1), Height, Height_Position, false, not(Bool_direction))
769
			refuel_count = moveForward(FacingAngle, false, tunnels_separation, true, true, refuel_count)
770
			FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
771
			placeTorch("front")
772
			FacingAngle = turn(FacingAngle, not(Bool_direction), 2)
773
		end	
774
	end
775
	if tunnel_forth and (tunnels_integer - done_tunnels == 0) and (Width > 1) then
776
		refuel_count = moveForward(FacingAngle, false, 2*math.ceil(Length/2), false, false, refuel_count)
777
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
778
		refuel_count = moveForward(FacingAngle, true, 1, false, false, refuel_count)
779
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, (Width - 1), Height, Height_Position, false, Bool_direction)
780
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
781
		refuel_count = moveForward(FacingAngle, true, 1, false, false, refuel_count)
782
		FacingAngle = turn(FacingAngle, Bool_direction, 1)
783
		refuel_count, Height_Position = digVerticalLayer(refuel_count, FacingAngle, (Width - 1), Height, Height_Position, false, not(Bool_direction))
784
		if (Height_Position == "up") then
785
			refuel_count = moveUp(false, (Height - 3), refuel_count, false)
786
			Height_Position = "down"
787
		end
788
		refuel_count = moveForward(FacingAngle, false, (Width - 2), false, false, refuel_count)
789
		FacingAngle = turn(FacingAngle, Bool_direction, 2)
790
	end
791
	if (Width == 1) and (tunnels_integer - done_tunnels ~= 0) then
792
		refuel_count = moveForward(FacingAngle, true, 1, true, true, refuel_count)
793
	elseif (Width == 1) and (tunnels_integer - done_tunnels == 0) and tunnel_forth then
794
		refuel_count = moveForward(FacingAngle, false, (2*math.ceil(Length/2)), false, false, refuel_count)
795
	end
796
	tunnel_forth = not(tunnel_forth)
797
end
798
refuel_count = moveUp(false, 1, refuel_count, false)
799
if (Width == 1) and not(tunnel_forth) then
800
	refuel_count = moveForward(FacingAngle, false, 1, false, false, refuel_count)
801
	turn(FacingAngle, Bool_direction, 1)
802
end
803
refuel_count = moveForward(FacingAngle, false, ((tunnels_integer*Width) - 1 + (tunnels_separation*(tunnels_integer - 1))), false, false, refuel_count)
804
FacingAngle = turn(FacingAngle, not(Bool_direction), 1)
805
refuel_count = moveForward(FacingAngle, true, 1, false, false, refuel_count)
806
resetScreen()
807
write("Done. I hope I worked well !")
808
term.setCursorPos(1,8)