View difference between Paste ID: dE0eFDtS and dCgDqSND
SHOW: | | - or go back to the newest paste.
1
local tArgs = { ... }
2
if #tArgs ~= 1 then
3
	print( "Usage: excavate <diameter>" )
4
	return
5
end
6
7
-- Mine in a quarry pattern until we hit something we can't dig
8
local size = tonumber( tArgs[1] )
9
if size < 1 then
10
	print( "Excavate diameter must be positive" )
11
	return
12
end
13
	
14
local depth = 0
15
local unloaded = 0
16
local collected = 0
17
18
local xPos,zPos = 0,0
19
local xDir,zDir = 0,1
20
21
local goTo -- Filled in further down
22
local refuel -- Filled in further down
23
 
24
local function unload( _bKeepOneFuelStack )
25
	print( "Unloading items..." )
26
	for n=1,16 do
27
		local nCount = turtle.getItemCount(n)
28
		if nCount > 0 then
29
			turtle.select(n)			
30
			local bDrop = true
31
			if _bKeepOneFuelStack and turtle.refuel(0) then
32
				bDrop = false
33
				_bKeepOneFuelStack = false
34
			end			
35
			if bDrop then
36
				turtle.drop()
37
				unloaded = unloaded + nCount
38
			end
39
		end
40
	end
41
	collected = 0
42
	turtle.select(1)
43
end
44
45
local function returnSupplies()
46
	local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
47
	print( "Returning to surface..." )
48
	goTo( 0,0,0,0,-1 )
49
	
50
	local fuelNeeded = 2*(x+y+z) + 1
51
	if not refuel( fuelNeeded ) then
52
		unload( true )
53
		print( "Waiting for fuel" )
54
		while not refuel( fuelNeeded ) do
55
			os.pullEvent( "turtle_inventory" )
56
		end
57
	else
58
		unload( true )	
59
	end
60
	
61
	print( "Resuming mining..." )
62
	goTo( x,y,z,xd,zd )
63
end
64
65
local function collect()	
66
	local bFull = true
67
	local nTotalItems = 0
68
	for n=1,16 do
69
		local nCount = turtle.getItemCount(n)
70
		if nCount == 0 then
71
			bFull = false
72
		end
73
		nTotalItems = nTotalItems + nCount
74
	end
75
	
76
	if nTotalItems > collected then
77
		collected = nTotalItems
78
		if math.fmod(collected + unloaded, 50) == 0 then
79
			print( "Mined "..(collected + unloaded).." items." )
80
		end
81
	end
82
	
83
	if bFull then
84
		print( "No empty slots left." )
85
		return false
86
	end
87
	return true
88
end
89
90
function refuel( ammount )
91
	local fuelLevel = turtle.getFuelLevel()
92
	if fuelLevel == "unlimited" then
93
		return true
94
	end
95
	
96
	local needed = ammount or (xPos + zPos + depth + 2)
97
	if turtle.getFuelLevel() < needed then
98
		local fueled = false
99
		for n=1,16 do
100
			if turtle.getItemCount(n) > 0 then
101
				turtle.select(n)
102
				if turtle.refuel(1) then
103
					while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
104
						turtle.refuel(1)
105
					end
106
					if turtle.getFuelLevel() >= needed then
107
						turtle.select(1)
108
						return true
109
					end
110
				end
111
			end
112
		end
113
		turtle.select(1)
114
		return false
115
	end
116
	
117
	return true
118
end
119
120
local iup
121
local idown
122
local ifront
123
local dup
124
local ddown
125
local dfront
126
127
local function isBlacklist(name)
128
	if (name == "minecraft:stone") then
129
		return true
130
	end
131
	if (name == "minecraft:water") then
132
		return true
133
	end
134
	if (name == "minecraft:lava") then
135
		return true
136
	end
137
	if (name == "minecraft:flowing_water") then
138
		return true
139
	end
140
	return false
141
end
142
143
local function pick()
144
	ifront, dfront = turtle.inspect()
145
	if ifront then
146
		if not (isBlacklist(dfront.name)) then
147
			if turtle.dig() then
148
				if not collect() then
149
					returnSupplies()
150
				end
151
			end
152
		end
153
	end
154
end
155
156
local function hitBedrock()
157
	idown, ddown = turtle.inspectDown()
158
	if idown then 
159
		return (ddown.name == "minecraft:bedrock")
160
	end
161
end
162
163
local function tryUp()
164
	if turtle.up() then
165
			depth = depth - 1
166
	elseif turtle.digUp() or turtle.attackUp() then
167
			collect()
168
	else
169
		sleep( 0.5 )
170
	end
171
end
172
173
local function boreUp()
174
	while depth > 0 do
175
		pick()
176
		tryUp()
177
	end
178
end
179
180
local function tryForwards()
181
	if not refuel() then
182
		print( "Not enough Fuel" )
183
		returnSupplies()
184
	end
185
186
	while not turtle.forward() do
187
		if turtle.detect() then
188
			if turtle.dig() then
189
				if not collect() then
190
					returnSupplies()
191
				end
192
			else
193
				return false
194
			end
195
		elseif turtle.attack() then
196
			if not collect() then
197
				returnSupplies()
198
			end
199
		else
200
			sleep( 0.5 )
201
		end
202
	end
203
	
204
	
205
	xPos = xPos + xDir
206
	zPos = zPos + zDir
207
208
	iup, dup = turtle.inspectUp()
209
	idown, ddown = turtle.inspectDown()
210
211
	if iup then
212
		-- if not (isBlacklist(dup.name)) then
213
			-- print ("Ooo piece of candy.")
214
			if turtle.digUp() then
215
				if not collect() then
216
					returnSupplies()
217
				end
218
			end
219
		-- end
220
	end
221
222
	if idown then
223
		if not (isBlacklist(ddown.name)) then
224
			print ("Ooo piece of candy.")
225
			if turtle.digDown() then
226
				if not collect() then
227
					returnSupplies()
228
				end
229
			end
230
		end
231
	end
232
233
	return true
234
end
235
236
local function tryDown()
237
	if not refuel() then
238
		print( "Not enough Fuel" )
239
		returnSupplies()
240
	end
241
	
242
	while not turtle.down() do
243
		if turtle.detectDown() then
244
			if turtle.digDown() then
245
				if not collect() then
246
					returnSupplies()
247
				end
248
			else
249
				return false
250
			end
251
		elseif turtle.attackDown() then
252
			if not collect() then
253
				returnSupplies()
254
			end
255
		else
256
			sleep( 0.5 )
257
		end
258
	end
259
260
	depth = depth + 1
261
	if math.fmod( depth, 10 ) == 0 then
262
		print( "Descended "..depth.." metres." )
263
	end
264
265
	return true
266
end
267
268
local function boreDown()
269
	while not hitBedrock() do
270
		tryDown()
271
		pick()
272
	end
273
end
274
275
local function turnLeft()
276
	turtle.turnLeft()
277
	xDir, zDir = -zDir, xDir
278
end
279
280
local function turnRight()
281
	turtle.turnRight()
282
	xDir, zDir = zDir, -xDir
283
end
284
285
286
local function bore()
287
	boreDown()
288
	turnLeft()
289
	boreUp()
290
	turnLeft()
291
	boreDown()
292
	turnLeft()
293
	boreUp()
294
	turnLeft()
295
end
296
297
function goTo( x, y, z, xd, zd )
298
	while depth > y do
299
		if turtle.up() then
300
			depth = depth - 1
301
		elseif turtle.digUp() or turtle.attackUp() then
302
			collect()
303
		else
304
			sleep( 0.5 )
305
		end
306
	end
307
308
	if xPos > x then
309
		while xDir ~= -1 do
310
			turnLeft()
311
		end
312
		while xPos > x do
313
			if turtle.forward() then
314
				xPos = xPos - 1
315
			elseif turtle.dig() or turtle.attack() then
316
				collect()
317
			else
318
				sleep( 0.5 )
319
			end
320
		end
321
	elseif xPos < x then
322
		while xDir ~= 1 do
323
			turnLeft()
324
		end
325
		while xPos < x do
326
			if turtle.forward() then
327
				xPos = xPos + 1
328
			elseif turtle.dig() or turtle.attack() then
329
				collect()
330
			else
331
				sleep( 0.5 )
332
			end
333
		end
334
	end
335
	
336
	if zPos > z then
337
		while zDir ~= -1 do
338
			turnLeft()
339
		end
340
		while zPos > z do
341
			if turtle.forward() then
342
				zPos = zPos - 1
343
			elseif turtle.dig() or turtle.attack() then
344
				collect()
345
			else
346
				sleep( 0.5 )
347
			end
348
		end
349
	elseif zPos < z then
350
		while zDir ~= 1 do
351
			turnLeft()
352
		end
353
		while zPos < z do
354
			if turtle.forward() then
355
				zPos = zPos + 1
356
			elseif turtle.dig() or turtle.attack() then
357
				collect()
358
			else
359
				sleep( 0.5 )
360
			end
361
		end	
362
	end
363
	
364
	while depth < y do
365
		if turtle.down() then
366
			depth = depth + 1
367
		elseif turtle.digDown() or turtle.attackDown() then
368
			collect()
369
		else
370
			sleep( 0.5 )
371
		end
372
	end
373
	
374
	while zDir ~= zd or xDir ~= xd do
375
		turnLeft()
376
	end
377
end
378
379
if not refuel() then
380
	print( "Out of Fuel" )
381
	return
382
end
383
384
-- print("Finding Excavation Floor...")
385
-- while not turtle.detectDown() do
386
--	tryDown()
387
-- end
388
389
print( "Excavating..." )
390
391
local reseal = false
392-
turtle.select(1)
392+
393-
if turtle.digDown() then
393+
394-
	reseal = true
394+
395
396
local map = {}
397
398
for x = 1, size do
399
	map[x] = {}
400
	for z = 1, size do
401
		map[x][z] = 0
402-
		while true do
402+
403-
			if ((xPos+(5*xDir)) > size) or ((xPos+(5*xDir)) < 0) then
403+
404-
			 	break
404+
405
for x = 1, size,5 do
406-
			if ((zPos+(5*zDir)) > size) or ((zPos+(5*zDir)) < 0) then
406+
	map[x] = {}
407-
				break
407+
	for z = 1, size,5 do
408
		map[x][z] = 1
409-
			bore()
409+
		
410-
			if not tryForwards() then
410+
		if ((x+1) <= size) and ((z+2) <= size) then
411-
				done = true
411+
			map[x+1][z+2] = 1
412-
				break
412+
413
		if ((x+2) <= size) and ((z-1) > 0) then 
414-
			if not tryForwards() then
414+
			map[x+2][z-1] = 1
415-
				done = true
415+
416-
				break
416+
		if ((x+3) <= size) and ((z+1) <= size) then
417
			map[x+3][z+1] = 1
418-
			if not tryForwards() then
418+
419-
				done = true
419+
420-
				break
420+
		if ((x-2) > 0) and ((z+1) <= size) then
421
			map[x-2][z+1] = 1
422-
			if not tryForwards() then
422+
423-
				done = true
423+
		if ((x-1) > 0) and ((z+3) <= size) then
424-
				break
424+
			map[x-1][z+3] = 1
425
		end
426-
			if not tryForwards() then
426+
427-
				done = true
427+
428-
				break
428+
429
for z = size, 1, -1 do
430
	local s = ""
431-
		if done then
431+
	for x = 1, size do
432-
			break
432+
		if map[x][z] == 1 then
433
			s = s .. "O"
434-
		if n<size then
434+
435-
			if math.fmod(n + alternate,2) == 0 then
435+
			s = s .. "X"
436-
				bore()
436+
437-
				turnLeft()
437+
438-
				if not tryForwards() then
438+
	print(s)
439-
					done = true
439+
440-
					break
440+
441
while not done do
442-
				turnLeft()
442+
443-
				if not tryForwards() then
443+
		if (math.fmod(n,2) == 0) then
444-
					done = true
444+
			for z = size-1, 0, -1 do
445-
					break
445+
				if map[n][z+1] == 1 then
446
					goTo(n-1, 0, z, xDir, -1)
447-
				if not tryForwards() then
447+
					bore()
448-
					done = true
448+
					map[xPos+1][zPos+1] = 2
449-
					break
449+
450
			end
451
		else
452-
				bore()
452+
			for z = 0, size-1, 1 do
453-
				turnRight()
453+
				if map[n][z+1] == 1 then
454-
				if not tryForwards() then
454+
					goTo(n-1, 0, z, xDir, 1)
455-
					done = true
455+
					bore()
456-
					break
456+
					map[xPos+1][zPos+1] = 2
457
				end
458-
				turnRight()
458+
459-
				if not tryForwards() then
459+
460-
					done = true
460+
461-
					break
461+
462
end
463-
				if not tryForwards() then
463+
464-
					done = true
464+
465-
					break
465+
466
-- Return to where we started
467-
				if not tryForwards() then
467+
468-
					done = true
468+
469-
					break
469+
470
471
-- Seal the hole
472
if reseal then
473
	turtle.placeDown()
474
end
475
476
print( "Mined "..(collected + unloaded).." items total." )