View difference between Paste ID: W86Lp9wu and xwwVsp5s
SHOW: | | - or go back to the newest paste.
1
-- For some reason the turtle had trouble DLing the last paste
2
-- Turtle Excavator w/ EnderChest support
3
-- Stop wasting precious fuel by returning to the surface to unload.
4
-- Best used with a Chunk Loader Mining Turtle.
5
-- Mods Required - EnderStorage, ComputerCraft
6
-- Recommended Mods - MiscPeripherals (for the Chunk Loader Turtle ad-on)
7
-- Brought to you by Lizarddemon94 and TheCountChuckula
8
local tArgs = { ... }
9
if #tArgs ~= 1 then
10
	print( "Usage: excavate <diameter>" )
11
	return
12
end
13
14
print( "Place your Auto-Sorting Ender Chest" )
15
print( "into Slot 1. A small stack of coal" )
16
print( "(5-10) into Slot 2." )
17
print( "Press any key to continue..." )
18
os.pullEvent("char")
19
20
-- Mine in a quarry pattern until we hit something we can't dig
21
local size = tonumber( tArgs[1] )
22
if size < 1 then
23
	print( "Excavate diameter must be positive" )
24
	return
25
end
26
	
27
local depth = 0
28
local unloaded = 0
29
local collected = 0
30
31
local ender = 1
32
local fuel = 2
33
34
local xPos,zPos = 0,0
35
local xDir,zDir = 0,1
36
37
local goTo -- Filled in further down
38
local refuel -- Filled in further down
39
 
40
local function unload( _bKeepOneFuelStack )
41
	print( "Unloading items..." )
42
 turtle.select(ender)
43
 turtle.placeUp()
44
	for n=3,16 do
45
		local nCount = turtle.getItemCount(n)
46
		if nCount > 0 then
47
			turtle.select(n)			
48
   turtle.dropUp()
49
  end
50
  unloaded = unloaded + nCount
51
 end
52
 turtle.select(ender)
53
 turtle.digUp()
54
	collected = 0
55
	turtle.select(2)
56
end
57
58
local function returnSupplies()
59
	local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
60
	print( "Emptying slots..." )
61
	
62
	local fuelNeeded = 2*(x+y+z) + 1
63
	if not refuel( fuelNeeded ) then
64
		unload( true )
65
		print( "Waiting for fuel" )
66
		while not refuel( fuelNeeded ) do
67
			sleep(1)
68
		end
69
	else
70
		unload( true )	
71
	end
72
	
73
	print( "Resuming mining..." )
74
	goTo( x,y,z,xd,zd )
75
end
76
77
local function collect()	
78
	local bFull = true
79
	local nTotalItems = 0
80
	for n=3,16 do
81
		local nCount = turtle.getItemCount(n)
82
		if nCount == 0 then
83
			bFull = false
84
		end
85
		nTotalItems = nTotalItems + nCount
86
	end
87
	
88
	if nTotalItems > collected then
89
		collected = nTotalItems
90
		if math.fmod(collected + unloaded, 50) == 0 then
91
			print( "Mined "..(collected + unloaded).." items." )
92
		end
93
	end
94
	
95
	if bFull then
96
		print( "No empty slots left." )
97
		return false
98
	end
99
	return true
100
end
101
102
function refuel( ammount )
103
	local fuelLevel = turtle.getFuelLevel()
104
	if fuelLevel == "unlimited" then
105
		return true
106
	end
107
	
108
	local needed = ammount or (xPos + zPos + depth + 1)
109
	if turtle.getFuelLevel() < needed then
110
		local fueled = false
111
		for n=2,2 do
112
			if turtle.getItemCount(n) > 0 then
113
				turtle.select(n)
114
				if turtle.refuel(1) then
115
					while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
116
						turtle.refuel(1)
117
					end
118
					if turtle.getFuelLevel() >= needed then
119
						turtle.select(2)
120
						return true
121
					end
122
				end
123
			end
124
		end
125
		turtle.select(2)
126
		return false
127
	end
128
	
129
	return true
130
end
131
132
local function tryForwards()
133
	if not refuel() then
134
		print( "Not enough Fuel" )
135
		returnSupplies()
136
	end
137
	
138
	while not turtle.forward() do
139
		if turtle.detect() then
140
			if turtle.dig() then
141
				if not collect() then
142
					returnSupplies()
143
				end
144
			else
145
				return false
146
			end
147
		elseif turtle.attack() then
148
			if not collect() then
149
				returnSupplies()
150
			end
151
		else
152
			sleep( 0.5 )
153
		end
154
	end
155
	
156
	xPos = xPos + xDir
157
	zPos = zPos + zDir
158
	return true
159
end
160
161
local function tryDown()
162
	if not refuel() then
163
		print( "Not enough Fuel" )
164
		returnSupplies()
165
	end
166
	
167
	while not turtle.down() do
168
		if turtle.detectDown() then
169
			if turtle.digDown() then
170
				if not collect() then
171
					returnSupplies()
172
				end
173
			else
174
				return false
175
			end
176
		elseif turtle.attackDown() then
177
			if not collect() then
178
				returnSupplies()
179
			end
180
		else
181
			sleep( 0.5 )
182
		end
183
	end
184
185
	depth = depth + 1
186
	if math.fmod( depth, 10 ) == 0 then
187
		print( "Descended "..depth.." metres." )
188
	end
189
190
	return true
191
end
192
193
local function turnLeft()
194
	turtle.turnLeft()
195
	xDir, zDir = -zDir, xDir
196
end
197
198
local function turnRight()
199
	turtle.turnRight()
200
	xDir, zDir = zDir, -xDir
201
end
202
203
function goTo( x, y, z, xd, zd )
204
	while depth > y do
205
		if turtle.up() then
206
			depth = depth - 1
207
		elseif turtle.digUp() or turtle.attackUp() then
208
			collect()
209
		else
210
			sleep( 0.5 )
211
		end
212
	end
213
214
	if xPos > x then
215
		while xDir ~= -1 do
216
			turnLeft()
217
		end
218
		while xPos > x do
219
			if turtle.forward() then
220
				xPos = xPos - 1
221
			elseif turtle.dig() or turtle.attack() then
222
				collect()
223
			else
224
				sleep( 0.5 )
225
			end
226
		end
227
	elseif xPos < x then
228
		while xDir ~= 1 do
229
			turnLeft()
230
		end
231
		while xPos < x do
232
			if turtle.forward() then
233
				xPos = xPos + 1
234
			elseif turtle.dig() or turtle.attack() then
235
				collect()
236
			else
237
				sleep( 0.5 )
238
			end
239
		end
240
	end
241
	
242
	if zPos > z then
243
		while zDir ~= -1 do
244
			turnLeft()
245
		end
246
		while zPos > z do
247
			if turtle.forward() then
248
				zPos = zPos - 1
249
			elseif turtle.dig() or turtle.attack() then
250
				collect()
251
			else
252
				sleep( 0.5 )
253
			end
254
		end
255
	elseif zPos < z then
256
		while zDir ~= 1 do
257
			turnLeft()
258
		end
259
		while zPos < z do
260
			if turtle.forward() then
261
				zPos = zPos + 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
	while depth < y do
271
		if turtle.down() then
272
			depth = depth + 1
273
		elseif turtle.digDown() or turtle.attackDown() then
274
			collect()
275
		else
276
			sleep( 0.5 )
277
		end
278
	end
279
	
280
	while zDir ~= zd or xDir ~= xd do
281
		turnLeft()
282
	end
283
end
284
285
if not refuel() then
286
	print( "Out of Fuel" )
287
	return
288
end
289
290
print( "Excavating..." )
291
292
local reseal = false
293
turtle.select(1)
294
if turtle.digDown() then
295
	reseal = true
296
end
297
298
local alternate = 0
299
local done = false
300
while not done do
301
	for n=1,size do
302
		for m=1,size-1 do
303
			if not tryForwards() then
304
				done = true
305
				break
306
			end
307
		end
308
		if done then
309
			break
310
		end
311
		if n<size then
312
			if math.fmod(n + alternate,2) == 0 then
313
				turnLeft()
314
				if not tryForwards() then
315
					done = true
316
					break
317
				end
318
				turnLeft()
319
			else
320
				turnRight()
321
				if not tryForwards() then
322
					done = true
323
					break
324
				end
325
				turnRight()
326
			end
327
		end
328
	end
329
	if done then
330
		break
331
	end
332
	
333
	if size > 1 then
334
		if math.fmod(size,2) == 0 then
335
			turnRight()
336
		else
337
			if alternate == 0 then
338
				turnLeft()
339
			else
340
				turnRight()
341
			end
342
			alternate = 1 - alternate
343
		end
344
	end
345
	
346
	if not tryDown() then
347
		done = true
348
		break
349
	end
350
end
351
352
print( "Returning to surface..." )
353
354
-- Return to where we started
355
returnSupplies()
356
goTo( 0,0,0,0,-1 )
357
unload( false )
358
goTo( 0,0,0,0,1 )
359
360
-- Seal the hole
361
if reseal then
362
	turtle.placeDown()
363
end
364
365
print( "Mined "..(collected + unloaded).." items total." )