View difference between Paste ID: VybYFxXS and xUKzcePs
SHOW: | | - or go back to the newest paste.
1
MiningTurtle = { 
2
	position = { 
3
		x = 0, 
4
		y = 0,
5
		z = 0
6
	},
7
	direction = 0
8
}
9
10
-- Check if the Turtle needs to be refueled ( <= refuelLevel )
11
-- if yes, try to refuel it taking the contents of the inventory
12
-- if this is not possible or not enough, go back to base
13
function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel )
14
15
	if refuelLevel == nil then
16
		refuelLevel 		= 25
17
	end
18
	
19
	if requiredFuelLevel == nil then
20
		requiredFuelLevel 	= 500
21
	end
22
	
23
	-- should we refuel?
24
	if turtle.getFuelLevel() <= refuelLevel then
25
		
26
		-- iterate through our inventory
27
		for slot = 1,16 do
28
			
29
			-- select slot
30
			turtle.select(slot)
31
			
32
			-- refuel from active slot...
33
			if turtle.refuel( turtle.getItemCount( slot ) ) then
34
				
35
			end
36
			
37
			-- ...and check again
38
			if turtle.getFuelLevel() >= requiredFuelLevel then
39
				return true
40
			end
41
			
42
		end
43
		
44
		-- check if we did not refueled enough alreay
45
		if not (turtle.getFuelLevel() >= requiredFuelLevel) then
46
			-- if yes, go back to base
47
			
48
			MiningTurtle:toBase()
49
			-- refuel: to be implemented!!!!
50
		end
51
		
52
		return false
53-
	return false
53+
54
	
55
	return true
56
end
57
58
-- Go back to the base of the MiningTurtle
59
function MiningTurtle:toBase( )
60
61
end
62
	
63
-- Go to ground
64
function MiningTurtle:toGround( )
65
66
	while not turtle.detectDown() do
67
		MiningTurtle:down()
68
	end
69
	
70
end
71
72
-- forward
73
function MiningTurtle:forward()
74
	local ret
75
	
76
	ret = turtle.forward()
77
	
78
	if ret then
79
		
80
		
81
		if self.direction == 0 then
82
			
83
			self.position.x = self.position.x + 1
84
			
85
		elseif self.direction == 90 then
86
		
87
			self.position.y = self.position.y + 1
88
		
89
		elseif self.direction == 180 then
90
		
91
			self.position.x = self.position.x - 1
92
		
93
		elseif self.direction == 270 then
94
		
95
			self.position.y = self.position.y - 1
96
		
97
		end
98
		
99
	end
100
	
101
	return ret
102
end
103
104
-- back
105
function MiningTurtle:back()
106
	local ret
107
	
108
	ret = turtle.back()
109
	
110
	if ret then
111
		
112
		if self.direction == 0 then
113
			
114
			self.position.x = self.position.x - 1
115
			
116
		elseif self.direction == 90 then
117
		
118
			self.position.y = self.position.y - 1
119
		
120
		elseif self.direction == 180 then
121
		
122
			self.position.x = self.position.x + 1
123
		
124
		elseif self.direction == 270 then
125
		
126
			self.position.y = self.position.y + 1
127
		
128
		end
129
		
130
	end
131
	
132
	return ret
133
end
134
135
-- up
136
function MiningTurtle:up()
137
	local ret
138
	
139
	ret = turtle.up()
140
	
141
	if ret then
142
		self.position.z = self.position.z + 1
143
	end
144
	
145
	return ret
146
end
147
148
-- down
149
function MiningTurtle:down()
150
	local ret
151
	
152
	ret = turtle.down()
153
	
154
	if ret then
155
		self.position.z = self.position.z - 1
156
	end
157
	
158
	return ret
159
end
160
161
-- turn Left
162
function MiningTurtle:turnLeft()
163
	local ret
164
	
165
	ret = turtle.turnLeft()
166
	
167
	if ret then
168
		self.direction = self.direction + 90
169
		
170
		while self.direction >= 360 do
171
			self.direction = self.direction - 360
172
		end
173
	end
174
	
175
	return ret
176
end
177
178
-- turn Right
179
function MiningTurtle:turnRight()
180
	local ret
181
	
182
	ret = turtle.turnRight()
183
	
184
	if ret then
185
		self.direction = self.direction - 90
186
		
187
		while self.direction < 0 do
188
			self.direction = self.direction + 360
189
		end
190
	end
191
	
192
	return ret
193
end
194
195
-- Save forward
196
function MiningTurtle:saveForward()
197
198
	while not MiningTurtle:forward() do
199
		turtle.dig()
200
	end
201
end
202
203
-- Save up
204
function MiningTurtle:saveUp()
205
206
	while not MiningTurtle:up() do
207
		turtle.digUp()
208
	end
209
end
210
211
-- Save down
212
function MiningTurtle:saveDown()
213
214
	while not MiningTurtle:down() do
215
		turtle.digDown()
216
	end
217
end
218
219
-- 180 degree turn
220
function MiningTurtle:turn()
221
222
	MiningTurtle:turnRight()
223
	MiningTurtle:turnRight()
224
end
225
     
226
-- Make a tunnel
227
function MiningTurtle:tunnel( length, height, width, ret )
228
	
229
	if height == nil then
230
		height 	= 2
231
	end
232
	
233
	if width == nil then
234
		width 	= 1
235
	end
236
	
237
	if ret == nil then
238
		ret 	= false
239
	end
240
	
241
	-- Length loop
242
	
243
	for i = 1,length do
244
245
		MiningTurtle:saveForward()
246
	
247
		if i % 2 == 0 then
248
			-- even
249
			
250
			MiningTurtle:turnLeft()
251
			
252
			for x = 0,width do
253
				
254
				for y = 0,height do
255
				
256
					if x % 2 == 0 then
257
						MiningTurtle:saveUp()						
258
					else					
259
						MiningTurtle:saveDown()					
260
					end
261
					
262
				end
263
				
264
				if x < width then
265
					MiningTurtle:saveForward()
266
				end
267
			end
268
			
269
			MiningTurtle:turnRight()
270
		
271
		else
272
			-- odd
273
			
274
			MiningTurtle:turnRight()
275
			
276
			for x = 0,width do
277
				
278
				for y = 0,height do
279
				
280
					if x % 2 == 0 then
281
						MiningTurtle:saveUp()						
282
					else					
283
						MiningTurtle:saveDown()					
284
					end
285
					
286
				end
287
				
288
				if x < width then
289
					MiningTurtle:saveForward()
290
				end
291
			end
292
			
293
			MiningTurtle:turnLeft()
294
		
295
		end
296
	
297
	end
298
	
299
	if ret then
300
		
301
		MiningTurtle:turn()
302
	end
303
	
304
end
305
306
307
-- 
308
function MiningTurtle:matrix( rows, cols )
309
310
	
311
312
end
313
314
315
-- main
316
317
while not MiningTurtle:requiredRefuel() do
318
	print("Waiting for MiningTurtle to have enough fuel")
319
end
320
321
MiningTurtle:toGround()
322
MiningTurtle:tunnel(10)