View difference between Paste ID: 5UZ0244J and 1MzKST2F
SHOW: | | - or go back to the newest paste.
1
-- Usage (program name) <Width> <Depth> <Length> <Direction>
2
-- Place a chest with enough space for what you are digging behind the turtle before beginning
3
-- Put any fuel in the first slot, if you don't want to have to refuel, use refuel<amount> before running this
4
-- Make sure you keep the chunk(s) the turtle is digging in loaded, otherwise it will break
5
6
rednet.open("left")
7
8
local tArgs = { ... }
9
if #tArgs ~= 4 then
10
	print( "Usage: excavate <Width> <Depth> <Length> <Direction>" )
11
	print( "Direction - 0 for down, 1 for up" )
12
	return
13
end
14
15
16
local size1    = tonumber( tArgs[1] )
17
local size2    = tonumber( tArgs[2] )
18
local size3    = tonumber( tArgs[3] )
19
local upordown = tonumber( tArgs[4] )
20
if size1 < 1 then
21
	print( "X size must be positive" )
22
	return
23
end
24
if size2 < 1 then
25
	print( "Depth must be positive" )
26
	return
27
end
28
if size3 < 1 then
29
	print( "Z size must be positive" )
30
	return
31
end
32
if upordown~=0 and upordown~=1 then
33
	print( "Invalid direction" )
34
	return
35
end
36
37
	
38
local depth = 0
39
local unloaded = 0
40
local collected = 0
41
42
local xPos,zPos = 0,0
43
local xDir,zDir = 0,1
44
45
local goTo -- Filled in further down
46
local refuel -- Filled in further down
47
48-
	print( "Unloading items..." )
48+
49
	rednet.send(38, "unloading")
50
	for n=2,16 do
51
		unloaded = unloaded + turtle.getItemCount(n)
52
		turtle.select(n)
53
		turtle.drop()
54
	end
55
	collected = 0
56
	turtle.select(1)
57
end
58
59
local function returnSupplies()
60-
	print( "Returning to surface..." )
60+
61
	rednet.send(38, "surface")
62
	goTo( 0,0,0,0,-1 )
63
	
64
	local fuelNeeded = x+y+z + x+y+z + 1
65
	if not refuel( fuelNeeded ) then
66
		unload()
67
		print( "Waiting for fuel" )
68
		while not refuel( fuelNeeded ) do
69
			sleep(1)
70
		end
71
	else
72
		unload()	
73
	end
74-
	print( "Resuming mining..." )
74+
75
	rednet.send(38, "mining")
76
	goTo( x,y,z,xd,zd )
77
end
78
79
local function collect()	
80
	local bFull = true
81
	local nTotalItems = 0
82
	for n=1,16 do
83
		local nCount = turtle.getItemCount(n)
84
		if nCount == 0 then
85
			bFull = false
86
		end
87
		nTotalItems = nTotalItems + nCount
88
	end
89
	
90
	if nTotalItems > collected then
91
		collected = nTotalItems
92
		if math.fmod(collected + unloaded, 50) == 0 then
93
			print( "Mined "..(collected + unloaded).." items." )
94
		end
95
	end
96
	
97
	if bFull then
98
		print( "No empty slots left." )
99
		return false
100
	end
101
	return true
102
end
103
104
function refuel( ammount )
105
	local fuelLevel = turtle.getFuelLevel()
106
	if fuelLevel == "unlimited" then
107
		return true
108
	end
109
	
110
	local needed = ammount or (xPos + zPos + depth + 1)
111
	if turtle.getFuelLevel() < needed then
112
		local fueled = false
113
		for n=1,16 do
114
			if turtle.getItemCount(n) > 0 then
115
				turtle.select(n)
116
				if turtle.refuel(1) then
117
					while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
118
						turtle.refuel(1)
119
					end
120
					if turtle.getFuelLevel() >= needed then
121
						turtle.select(1)
122
						return true
123
					end
124
				end
125
			end
126
		end
127
		turtle.select(1)
128
		return false
129
	end
130
	
131
	return true
132
end
133
134
local function tryForwards()
135-
		print( "Not enough Fuel" )
135+
136
		rednet.send(38, "Needs fuel")
137
		returnSupplies()
138
	end
139
	
140
	while not turtle.forward() do
141
		if turtle.detect() then
142
			if turtle.dig() then
143
				if not collect() then
144
					returnSupplies()
145
				end
146
			else
147
				return false
148
			end
149
		elseif turtle.attack() then
150
			if not collect() then
151
				returnSupplies()
152
			end
153
		else
154
			sleep( 0.5 )
155
		end
156
	end
157
	
158
	xPos = xPos + xDir
159
	zPos = zPos + zDir
160
	return true
161
end
162
163
local function tryDown()
164-
		print( "Not enough Fuel" )
164+
165
		rednet.send(38, "Needs fuel")
166
		returnSupplies()
167
	end
168
	
169
	while not turtle.down() do
170
		if turtle.detectDown() then
171
			if turtle.digDown() then
172
				if not collect() then
173
					returnSupplies()
174
				end
175
			else
176
				return false
177
			end
178
		elseif turtle.attackDown() then
179
			if not collect() then
180
				returnSupplies()
181
			end
182
		else
183
			sleep( 0.5 )
184
		end
185
	end
186
187
	depth = depth + 1
188
189
	return true
190
end
191
192
local function tryUp()
193-
		print( "Not enough Fuel" )
193+
194
		rednet.send(38,"Needs fuel")
195
		returnSupplies()
196
	end
197
	
198
	while not turtle.up() do
199
		if turtle.detectUp() then
200
			if turtle.digUp() then
201
				if not collect() then
202
					returnSupplies()
203
				end
204
			else
205
				return false
206
			end
207
		elseif turtle.attackUp() then
208
			if not collect() then
209
				returnSupplies()
210
			end
211
		else
212
			sleep( 0.5 )
213
		end
214
	end
215
216
	depth = depth - 1
217
218
	return true
219
end
220
221
local function turnLeft()
222
	turtle.turnLeft()
223
	xDir, zDir = -zDir, xDir
224
end
225
226
local function turnRight()
227
	turtle.turnRight()
228
	xDir, zDir = zDir, -xDir
229
end
230
231
function goTo( x, y, z, xd, zd )
232
	while depth > y do
233
		if turtle.up() then
234
			depth = depth - 1
235
		elseif turtle.digUp() or turtle.attackUp() then
236
			collect()
237
		else
238
			sleep( 0.5 )
239
		end
240
	end
241
242
	if xPos > x then
243
		while xDir ~= -1 do
244
			turnLeft()
245
		end
246
		while xPos > x do
247
			if turtle.forward() then
248
				xPos = xPos - 1
249
			elseif turtle.dig() or turtle.attack() then
250
				collect()
251
			else
252
				sleep( 0.5 )
253
			end
254
		end
255
	elseif xPos < x then
256
		while xDir ~= 1 do
257
			turnLeft()
258
		end
259
		while xPos < x do
260
			if turtle.forward() then
261
				xPos = xPos + 1
262
			elseif turtle.dig() or turtle.attack() then
263
				collect()
264
			else
265
				sleep( 0.5 )
266
			end
267
		end
268
	end
269
	
270
	if zPos > z then
271
		while zDir ~= -1 do
272
			turnLeft()
273
		end
274
		while zPos > z do
275
			if turtle.forward() then
276
				zPos = zPos - 1
277
			elseif turtle.dig() or turtle.attack() then
278
				collect()
279
			else
280
				sleep( 0.5 )
281
			end
282
		end
283
	elseif zPos < z then
284
		while zDir ~= 1 do
285
			turnLeft()
286
		end
287
		while zPos < z do
288
			if turtle.forward() then
289
				zPos = zPos + 1
290
			elseif turtle.dig() or turtle.attack() then
291
				collect()
292
			else
293
				sleep( 0.5 )
294
			end
295
		end	
296
	end
297
	
298
	while depth < y do
299
		if turtle.down() then
300
			depth = depth + 1
301
		elseif turtle.digDown() or turtle.attackDown() then
302
			collect()
303
		else
304
			sleep( 0.5 )
305
		end
306
	end
307
	
308
	while zDir ~= zd or xDir ~= xd do
309
		turnLeft()
310
	end
311
end
312
313-
	print( "Out of Fuel" )
313+
314
	rednet.send(38,"Out of fuel!")
315
	return
316
end
317-
print( "Excavating..." )
317+
318
	rednet.send(38,"Mining")
319
320
local reseal = false
321
turtle.select(1)
322
if upordown == 0 then
323
	if turtle.digDown() then
324
	end
325
elseif upordown == 1 then
326
	if turtle.digUp() then
327
	end
328
end
329
330
local alternate = 0
331
local done = 0
332
while done~=size2 do
333
	for varx=1,size1 do
334
		for varz=1,size3-1 do
335
			if not tryForwards() then
336
				done = size2
337
				break
338
			end
339
		end
340
		if done==size2 then
341
			break
342
		end
343
		if varx<size1 then
344
			if math.fmod(varx + alternate,2) == 0 then
345
				turnLeft()
346
				if not tryForwards() then
347
					done = size2
348
					break
349
				end
350
				turnLeft()
351
			else
352
				turnRight()
353
				if not tryForwards() then
354
					done = size2
355
					break
356
				end
357
				turnRight()
358
			end
359
		end
360
	end
361
	if done==size2 then
362
		break
363
	end
364
	
365
	if size1 > 1 then
366
		goTo( 0,depth,0,0,1 )
367
	end
368
	if done ~= size2-1 then
369
		if upordown == 0 then
370
			if not tryDown() then
371
				done = size2
372
				break
373
			end
374
		elseif upordown == 1 then
375
			if not tryUp() then
376
				done = size2
377
				break
378
			end
379
		end
380
	end
381
	done = done + 1
382
end
383-
print( "Returning to surface..." )
383+
384
rednet.send(38,"Returning to surface")
385
386
-- Return to where we started
387
goTo( 0,0,0,0,-1 )
388
unload()
389
goTo( 0,0,0,0,1 )
390
391
-- Seal the hole
392
if reseal then
393
	turtle.placeDown()
394
end
395
396
print( "Mined "..(collected + unloaded).." items total." )