View difference between Paste ID: C316Cyef and z6HYeDgD
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
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
	end
52
	
53
	return false
54
end
55
56
-- Go back to the base of the MiningTurtle
57
function MiningTurtle:toBase( )
58
59
end
60
	
61
-- Go to ground
62
function MiningTurtle:toGround( )
63
64
	while not turtle.detectDown() do
65
		MiningTurtle:down()
66
	end
67
	
68
end
69
70
-- forward
71
function MiningTurtle:forward()
72
	local ret
73
	
74
	ret = turtle.forward()
75
	
76
	if ret then
77
		
78
		
79
		if self.direction == 0 then
80
			
81
			self.position.x = self.position.x + 1
82
			
83
		elseif self.direction == 90 then
84
		
85
			self.position.y = self.position.y + 1
86
		
87
		elseif self.direction == 180 then
88
		
89
			self.position.x = self.position.x - 1
90
		
91
		elseif self.direction == 270 then
92
		
93
			self.position.y = self.position.y - 1
94
		
95
		end
96
		
97
	end
98
	
99
	return ret
100
end
101
102
-- back
103
function MiningTurtle:back()
104
	local ret
105
	
106
	ret = turtle.back()
107
	
108
	if ret then
109
		
110
		if self.direction == 0 then
111
			
112
			self.position.x = self.position.x - 1
113
			
114
		elseif self.direction == 90 then
115
		
116
			self.position.y = self.position.y - 1
117
		
118
		elseif self.direction == 180 then
119
		
120
			self.position.x = self.position.x + 1
121
		
122
		elseif self.direction == 270 then
123
		
124
			self.position.y = self.position.y + 1
125
		
126
		end
127
		
128
	end
129
	
130
	return ret
131
end
132
133
-- up
134
function MiningTurtle:up()
135
	local ret
136
	
137
	ret = turtle.up()
138
	
139
	if ret then
140
		self.position.z = self.position.z + 1
141
	end
142
	
143
	return ret
144
end
145
146
-- down
147
function MiningTurtle:down()
148
	local ret
149
	
150
	ret = turtle.down()
151
	
152
	if ret then
153
		self.position.z = self.position.z - 1
154
	end
155
	
156
	return ret
157
end
158
159
-- turn Left
160
function MiningTurtle:turnLeft()
161
	local ret
162
	
163
	ret = turtle.turnLeft()
164
	
165
	if ret then
166
		self.direction = self.direction + 90
167
		
168
		while self.direction >= 360 do
169
			self.direction = self.direction - 360
170
		end
171
	end
172
	
173
	return ret
174
end
175
176
-- turn Right
177
function MiningTurtle:turnRight()
178
	local ret
179
	
180
	ret = turtle.turnRight()
181
	
182
	if ret then
183
		self.direction = self.direction - 90
184
		
185
		while self.direction < 0 do
186
			self.direction = self.direction + 360
187
		end
188
	end
189
	
190
	return ret
191
end
192
193
-- Save forward
194
function MiningTurtle:saveForward()
195
196
	while not MiningTurtle:forward() do
197
		turtle.dig()
198
	end
199
end
200
201
-- Save up
202
function MiningTurtle:saveUp()
203
204
	while not MiningTurtle:up() do
205
		turtle.digUp()
206
	end
207
end
208
209
-- Save down
210
function MiningTurtle:saveDown()
211
212
	while not MiningTurtle:down() do
213
		turtle.digDown()
214
	end
215
end
216
217
-- 180 degree turn
218
function MiningTurtle:turn()
219
220
	MiningTurtle:turnRight()
221
	MiningTurtle:turnRight()
222
end
223
     
224
-- Make a tunnel
225
function MiningTurtle:tunnel( length, height, width, ret )
226
	
227
	if height == nil then
228
		height 	= 2
229
	end
230
	
231
	if height == nil then
232
		height 	= 2
233
	end
234
	
235
	if ret == nil then
236
		ret 	= false
237
	end
238
	
239
	-- Length loop
240
	
241
	for i = 1,length do
242
243
		MiningTurtle:saveForward()
244
	
245
		if i % 2 == 0 then
246
			-- even
247
			
248
			MiningTurtle:turnLeft()
249
			
250
			for x = 0,width do
251
				
252
				for y = 0,height do
253
				
254
					if x % 2 == 0 then
255
						MiningTurtle:saveUp()						
256
					else					
257
						MiningTurtle:saveDown()					
258
					end
259
					
260
				end
261
				
262
				if x < width then
263
					MiningTurtle:saveForward()
264
				end
265
			end
266
			
267
			MiningTurtle:turnRight()
268
		
269
		else
270
			-- odd
271
			
272
			MiningTurtle:turnRight()
273
			
274
			for x = 0,width do
275
				
276
				for y = 0,height do
277
				
278
					if x % 2 == 0 then
279
						MiningTurtle:saveUp()						
280
					else					
281
						MiningTurtle:saveDown()					
282
					end
283
					
284
				end
285
				
286
				if x < width then
287
					MiningTurtle:saveForward()
288
				end
289
			end
290
			
291
			MiningTurtle:turnLeft()
292
		
293
		end
294
	
295
	end
296
	
297
	if ret then
298
		
299
		MiningTurtle:turn()
300
	end
301
	
302
end
303
304
305
-- 
306
function MiningTurtle:matrix( rows, cols )
307
308
	
309
310
end
311
312
313
-- main
314
315
while not MiningTurtle:requiredRefuel() do
316
	print("Waiting for MiningTurtle to have enough fuel")
317
end
318
319
MiningTurtle:toGround()
320
MiningTurtle:tunnel(10)