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