View difference between Paste ID: As4JdY1j and 2hu68DHt
SHOW: | | - or go back to the newest paste.
1
--[[
2
 Expected slot assignment/content
3
 
4
 Slot 1: 64 Torches
5
 Slot 2: 1 Cobble (reserved for cobble compare)
6-
 Slot 3: Enderchest
6+
 Slot 3: Fuel
7
 Slot 4: Enderchest
8
--]]
9
10
-- Parameters
11
12
local torchSlot = 1  -- Inventory slot of torches
13-
local enderChestSlot = 3  -- Inventory slot of enderchest to be used for offloading
13+
14
local enderChestSlot = 4  -- Inventory slot of enderchest to be used for offloading
15
local frequencyOfCobbleRemoval = 30  -- Remove cobble every n blocks mined
16
local enderDumpInterval = 100  -- Dump content into an enderchest every 100 blocks mined
17
18
-- Variables
19
20
local firstProcessSlot = 5
21
local doProcessContent = false
22
local depth = 0
23
local collected = 0
24
local itemsToEnder = 0
25
local cobbleDumped = 0
26
27
-- Process commandline
28
29
local tArgs = { ... }
30
if #tArgs < 2 then
31
	print( "Usage: Area <length> <width> [Process]" )	
32
	print( "======================================" )
33
	print( "Process If true, content -> Enderchest")
34
	print( "")
35
	print( "Expected content in inventory of turtle:" )
36
	print( "Slot 1: Torches" )
37
	print( "Slot 2: Cobble (at least 1)")
38
	print( "Slot 3: Enderchest (only if Process=true)")
39
	return
40
end
41
42
local length = tonumber( tArgs[1] )
43
if length < 1 then
44
	print( "Area length must be 1+" )
45
	return
46
end
47
48
local width = 1
49
50
if #tArgs > 1 then 
51
    width = tonumber( tArgs[2] )
52
    if width < 1 then	
53
		print( "Area width must be 1+" )
54
		return
55
	end
56
	if #tArgs > 2 then
57
		doProcessContent = tArgs[3]:lower()=="true" 
58
	end
59
end
60
61
local function checkSlot(slotNr,description)
62
   if turtle.getItemCount(slotNr)==0 then
63
      print("Expected item '"..description.."' in slot "..slotNr..".")
64
	  return false
65
   end
66
   return true
67
end
68
69
-- Check slots
70
71
if not checkSlot(torchSlot,"torches") then return end
72
if not checkSlot(cobbleSlot,"cobbleStone (atleast 1)") then return end
73
if doProcessContent then 
74
   if not checkSlot(enderChestSlot,"enderChest") then return end
75
end
76
77
-- Main Code
78
79-
	if math.fmod(collected, 25) == 0 then
79+
80
	collected = collected + 1
81
	if math.fmod(collected, 100) == 0 then
82
		print( "Mined "..collected.." items." )
83
	end
84
end
85
local function refuel()
86
	local fuelLevel = turtle.getFuelLevel()
87
	if fuelLevel == "unlimited" or fuelLevel > 0 then
88
		return
89
	end
90
	
91
	local function tryRefuel()
92
		for n=1,16 do
93
			if turtle.getItemCount(n) > 0 then
94
				turtle.select(n)
95
				if turtle.refuel(1) then
96
					turtle.select(1)
97
					return true
98
				end
99
			end
100
		end
101
		turtle.select(1)
102
		return false
103
	end
104
	
105
	if not tryRefuel() then
106
		print( "Add more fuel to continue." )
107
		while not tryRefuel() do
108
			sleep(1)
109
		end
110
		print( "Resuming Tunnel." )
111
	end
112
end
113
local function tryDig()
114
	while turtle.detect() do
115
		if turtle.dig() then
116
			collect()
117
			sleep(0.5)
118
		else
119
			return false
120
		end
121
	end
122
	return true
123
end
124
local function tryDigUp()
125
	while turtle.detectUp() do
126
		if turtle.digUp() then
127
			collect()
128
			sleep(0.5)
129
		else
130
			return false
131
		end
132
	end
133
	return true
134
end
135
local function tryDigDown()
136
 while turtle.detectDown() do
137
  if turtle.digDown() then 
138
    collect()
139
    sleep(0.5)
140
 else
141
   return false
142
  end
143
 end
144
 return true
145
end
146
local function tryUp()
147
	refuel()
148
	while not turtle.up() do
149
		if turtle.detectUp() then
150
			if not tryDigUp() then
151
				return false
152
			end
153
		elseif turtle.attackUp() then
154
			collect()
155
		else
156
			sleep( 0.5 )
157
		end
158
	end
159
	return true
160
end
161
local function tryDown()
162
	refuel()
163
	while not turtle.down() do
164
		if turtle.detectDown() then
165
			if not tryDigDown() then
166
				return false
167
			end
168
		elseif turtle.attackDown() then
169
			collect()
170
		else
171
			sleep( 0.5 )
172
		end
173
	end
174
	return true
175
end
176
local function tryForward()
177
	refuel()
178
	while not turtle.forward() do
179
		if turtle.detect() then
180
			if not tryDig() then
181
				return false
182
			end
183
		elseif turtle.attack() then
184
			collect()
185
		else
186
			sleep( 0.5 )
187
		end
188
	end
189
	return true
190-
	for slot=3,16 do
190+
191
local function dumpCobble()
192
	for slot=firstProcessSlot,16 do
193
		turtle.select(slot)
194
		local slotQuantity=turtle.getItemCount(slot)
195
		if slotQuantity > 0 then 
196
			if turtle.compareTo(cobbleSlot) then
197
				cobbleDumped = cobbleDumped + slotQuantity
198
				turtle.drop()
199
			end
200
		end
201
	end
202
end
203
local function dropCobbleWhenNecessary()
204
	if math.fmod(collected,frequencyOfCobbleRemoval)==0 then
205
	   dumpCobble()
206
	end
207
end
208
local function placeTorchWhenNecessary(x,y)
209
	if math.fmod(x,8)==1 and math.fmod(y,8)==1 then
210
		turtle.select(1)
211
		if (turtle.getItemCount(torchSlot)==0) then
212
			print("Out of torches, please resupply!")
213
			return false
214
		else
215
			turtle.placeDown()
216
		end
217
	end	
218
	return true 
219
end
220
local function placeEnderChest()
221
	turtle.select(enderChestSlot)
222
	turtle.placeDown()
223
end
224
local function retrieveEnderChest()
225
	turtle.select(enderChestSlot)
226
	turtle.digDown()	
227
end
228
local function processContent()
229
	if (turtle.getItemCount(enderChestSlot)==0) then
230
		print("No enderchest found in slot"..enderChestSlot.."!")
231
		return false
232
	end
233-
	for slot=3,16 do
233+
234
	placeEnderChest()
235
	for slot=firstProcessSlot,16 do
236
	   	turtle.select(slot)
237
		local slotQuantity=turtle.getItemCount(slot)
238
		itemsToEnder = itemsToEnder + slotQuantity
239
		turtle.dropDown()
240
	end	
241
	retrieveEnderChest()
242
end
243
local function DumpStatistics()
244
	print( "Statistics:")
245
	print( string.rep("=",20))
246
	print( "Mined "..collected.." blocks total." )
247
	print( "Discarded "..cobbleDumped.." pieces of cobble.")
248
	print( itemsToEnder.." items send to storage.")
249
	print( string.rep("=",20))
250
end
251
local function mine()
252
	print( "Tunneling..." )
253
	local rotation=0
254
	for m=1,width do
255
		for n=1,length-1 do
256
			tryDigUp()
257
			tryDigDown()				
258
			if  doProcessContent and (math.fmod(collected+1,enderDumpInterval)==0) then
259
				processContent()
260
			end
261
			dropCobbleWhenNecessary(n)
262
			placeTorchWhenNecessary(n,m)			
263
			tryDig()
264
			if not tryForward() then
265
				return false
266
			end
267
		end		
268
		tryDigUp()
269
		tryDigDown()				
270
		if m < width then 
271
			if rotation==0 then
272
				turtle.turnRight()
273
			else
274
				turtle.turnLeft()
275
			end
276
			tryDig()
277
			if not tryForward() then
278
				return false
279
			end
280
			if rotation==0 then
281
				turtle.turnRight()
282
			else
283
				turtle.turnLeft()
284
			end
285
			rotation = 1 - rotation
286
		end
287
	end
288
	processContent()
289
	return true
290
end
291
292
if mine() then 
293
	print( "Tunnel complete." )
294
else
295
	print( "Tunnel aborted prematurely.")
296
end
297
DumpStatistics()