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 | -- 0 = SOUTH -> Z++ | |
9 | -- 1 = WEST -> X-- | |
10 | -- 2 = NORTH -> Z-- | |
11 | -- 3 = EAST -> X++ | |
12 | } | |
13 | ||
14 | -- Constructor | |
15 | ||
16 | function MiningTurtle:new() | |
17 | ||
18 | local object = { } | |
19 | ||
20 | setmetatable(object, { __index = MiningTurtle }) | |
21 | return object | |
22 | end | |
23 | ||
24 | -- Check if the Turtle needs to be refueled ( <= refuelLevel ) | |
25 | -- if yes, try to refuel it taking the contents of the inventory | |
26 | -- if this is not possible or not enough, go back to base | |
27 | function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel ) | |
28 | local slot | |
29 | ||
30 | if refuelLevel == nil then | |
31 | refuelLevel = 25 | |
32 | end | |
33 | ||
34 | if requiredFuelLevel == nil then | |
35 | requiredFuelLevel = 500 | |
36 | end | |
37 | ||
38 | -- should we refuel? | |
39 | if turtle.getFuelLevel() <= refuelLevel then | |
40 | ||
41 | -- iterate through our inventory | |
42 | for slot = 1,16 do | |
43 | ||
44 | -- select slot | |
45 | turtle.select(slot) | |
46 | ||
47 | -- refuel from active slot... | |
48 | if turtle.refuel( turtle.getItemCount( slot ) ) then | |
49 | ||
50 | end | |
51 | ||
52 | -- ...and check again | |
53 | if turtle.getFuelLevel() >= requiredFuelLevel then | |
54 | return true | |
55 | end | |
56 | ||
57 | end | |
58 | ||
59 | -- check if we did not refueled enough alreay | |
60 | if not (turtle.getFuelLevel() >= requiredFuelLevel) then | |
61 | -- if yes, go back to base | |
62 | ||
63 | MiningTurtle:toBase() | |
64 | -- refuel: to be implemented!!!! | |
65 | end | |
66 | ||
67 | return false | |
68 | end | |
69 | ||
70 | return true | |
71 | end | |
72 | ||
73 | -- Go back to the base of the MiningTurtle | |
74 | function MiningTurtle:toBase( ) | |
75 | ||
76 | end | |
77 | ||
78 | -- Go to ground | |
79 | function MiningTurtle:toGround( ) | |
80 | ||
81 | while not turtle.detectDown() do | |
82 | MiningTurtle:down() | |
83 | end | |
84 | ||
85 | end | |
86 | ||
87 | -- Go the our "base" Ground (z = 0) | |
88 | function MiningTurtle:toBaseLevel( ) | |
89 | ||
90 | while self.position.y > 0 do | |
91 | MiningTurtle:saveDown() | |
92 | end | |
93 | ||
94 | end | |
95 | ||
96 | -- forward | |
97 | function MiningTurtle:forward() | |
98 | local ret | |
99 | ||
100 | ret = turtle.forward() | |
101 | ||
102 | if ret then | |
103 | ||
104 | ||
105 | if self.direction == 0 then | |
106 | -- 0 = SOUTH -> Z++ | |
107 | self.position.z = self.position.z + 1 | |
108 | ||
109 | elseif self.direction == 1 then | |
110 | -- 1 = WEST -> X-- | |
111 | self.position.x = self.position.x - 1 | |
112 | ||
113 | elseif self.direction == 2 then | |
114 | -- 2 = NORTH -> Z-- | |
115 | self.position.z = self.position.z - 1 | |
116 | ||
117 | elseif self.direction == 3 then | |
118 | -- 3 = EAST -> X++ | |
119 | self.position.x = self.position.x + 1 | |
120 | ||
121 | end | |
122 | ||
123 | end | |
124 | ||
125 | MiningTurtle:outPos() | |
126 | ||
127 | return ret | |
128 | end | |
129 | ||
130 | -- back | |
131 | function MiningTurtle:back() | |
132 | local ret | |
133 | ||
134 | ret = turtle.back() | |
135 | ||
136 | if ret then | |
137 | ||
138 | -- all directions reverse!!! (moving back) | |
139 | ||
140 | if self.direction == 0 then | |
141 | -- 0 = SOUTH -> Z++ | |
142 | self.position.z = self.position.z - 1 | |
143 | ||
144 | elseif self.direction == 1 then | |
145 | -- 1 = WEST -> X-- | |
146 | self.position.x = self.position.x + 1 | |
147 | ||
148 | elseif self.direction == 2 then | |
149 | -- 2 = NORTH -> Z-- | |
150 | self.position.z = self.position.z + 1 | |
151 | ||
152 | elseif self.direction == 3 then | |
153 | -- 3 = EAST -> X++ | |
154 | self.position.x = self.position.x - 1 | |
155 | ||
156 | end | |
157 | ||
158 | end | |
159 | ||
160 | MiningTurtle:outPos() | |
161 | ||
162 | return ret | |
163 | end | |
164 | ||
165 | -- up | |
166 | function MiningTurtle:up() | |
167 | local ret | |
168 | ||
169 | ret = turtle.up() | |
170 | ||
171 | if ret then | |
172 | self.position.y = self.position.y + 1 | |
173 | end | |
174 | ||
175 | MiningTurtle:outPos() | |
176 | ||
177 | return ret | |
178 | end | |
179 | ||
180 | -- down | |
181 | function MiningTurtle:down() | |
182 | local ret | |
183 | ||
184 | ret = turtle.down() | |
185 | ||
186 | if ret then | |
187 | self.position.y = self.position.y - 1 | |
188 | end | |
189 | ||
190 | MiningTurtle:outPos() | |
191 | ||
192 | return ret | |
193 | end | |
194 | ||
195 | -- turn Left | |
196 | function MiningTurtle:turnLeft() | |
197 | local ret | |
198 | ||
199 | ret = turtle.turnLeft() | |
200 | ||
201 | if ret then | |
202 | self.direction = self.direction + 1 | |
203 | ||
204 | while self.direction >= 4 do | |
205 | self.direction = self.direction - 4 | |
206 | end | |
207 | end | |
208 | ||
209 | MiningTurtle:outPos() | |
210 | ||
211 | return ret | |
212 | end | |
213 | ||
214 | -- turn Right | |
215 | function MiningTurtle:turnRight() | |
216 | local ret | |
217 | ||
218 | ret = turtle.turnRight() | |
219 | ||
220 | if ret then | |
221 | self.direction = self.direction - 1 | |
222 | ||
223 | while self.direction < 0 do | |
224 | self.direction = self.direction - 4 | |
225 | end | |
226 | end | |
227 | ||
228 | MiningTurtle:outPos() | |
229 | ||
230 | return ret | |
231 | end | |
232 | ||
233 | -- Save forward | |
234 | function MiningTurtle:saveForward() | |
235 | ||
236 | while not MiningTurtle:forward() do | |
237 | turtle.dig() | |
238 | end | |
239 | end | |
240 | ||
241 | -- Save up | |
242 | function MiningTurtle:saveUp() | |
243 | ||
244 | while not MiningTurtle:up() do | |
245 | turtle.digUp() | |
246 | end | |
247 | end | |
248 | ||
249 | -- Save down | |
250 | function MiningTurtle:saveDown() | |
251 | ||
252 | while not MiningTurtle:down() do | |
253 | turtle.digDown() | |
254 | end | |
255 | end | |
256 | ||
257 | -- 180 degree turn | |
258 | function MiningTurtle:turn() | |
259 | ||
260 | MiningTurtle:turnRight() | |
261 | MiningTurtle:turnRight() | |
262 | end | |
263 | ||
264 | -- output pos data | |
265 | function MiningTurtle:outPos() | |
266 | ||
267 | print("Pos: " .. tostring(self.position.x) .. "|" .. tostring(self.position.z) .. "|" .. tostring(self.position.y) .. " o: " .. tostring(self.direction) .. " f: " .. tostring(turtle.getFuelLevel()) ) | |
268 | ||
269 | end | |
270 | ||
271 | -- Turn to direction | |
272 | function MiningTurtle:turnTo( newOrientation ) | |
273 | while self.direction ~= newOrientation do | |
274 | if math.abs(self.direction - newOrientation) < 180 then | |
275 | MiningTurtle:turnRight() | |
276 | else | |
277 | MiningTurtle:turnLeft() | |
278 | end | |
279 | end | |
280 | end | |
281 | ||
282 | -- Go to | |
283 | function MiningTurtle:goTo( newPosition ) | |
284 | print("GoTo: " .. newPosition.x .. "|" .. newPosition.z .. "|" .. newPosition.y) | |
285 | ||
286 | -- 0 = SOUTH -> Z++ | |
287 | -- 1 = WEST -> X-- | |
288 | -- 2 = NORTH -> Z-- | |
289 | -- 3 = EAST -> X++ | |
290 | ||
291 | while not ( (newPosition.x - self.position.x) == 0 and (newPosition.z - self.position.z) == 0 and (newPosition.y - self.position.y) == 0 ) do | |
292 | print("Was: " .. self.position.x .. "|" .. self.position.z .. "|" .. self.position.y) | |
293 | ||
294 | if (newPosition.x - self.position.x) < 0 then | |
295 | MiningTurtle:turnTo(1) | |
296 | MiningTurtle:saveForward() | |
297 | elseif (newPosition.x - self.position.x) > 0 then | |
298 | MiningTurtle:turnTo(3) | |
299 | MiningTurtle:saveForward() | |
300 | end | |
301 | ||
302 | if (newPosition.z - self.position.z) < 0 then | |
303 | MiningTurtle:turnTo(2) | |
304 | MiningTurtle:saveForward() | |
305 | elseif (newPosition.z - self.position.z) > 0 then | |
306 | MiningTurtle:turnTo(0) | |
307 | MiningTurtle:saveForward() | |
308 | end | |
309 | ||
310 | if (newPosition.y - self.position.y) < 0 then | |
311 | MiningTurtle:saveDown() | |
312 | elseif (newPosition.y - self.position.y) > 0 then | |
313 | MiningTurtle:saveUp() | |
314 | end | |
315 | print("Is : " .. self.position.x .. "|" .. self.position.z .. "|" .. self.position.y) | |
316 | ||
317 | end | |
318 | ||
319 | end | |
320 | ||
321 | -- Make a tunnel | |
322 | function MiningTurtle:tunnel( length, height, width, ret ) | |
323 | ||
324 | -- default values for parameters, only length is required (!) | |
325 | if height == nil then | |
326 | height = 2 | |
327 | end | |
328 | ||
329 | if width == nil then | |
330 | width = 1 | |
331 | end | |
332 | ||
333 | if ret == nil then | |
334 | ret = false | |
335 | end | |
336 | ||
337 | -- Save our current positon back for later return | |
338 | ||
339 | returnPos = {} | |
340 | returnPos.x = self.position.x | |
341 | returnPos.z = self.position.z | |
342 | returnPos.y = self.position.y | |
343 | returnDirection = self.direction | |
344 | ||
345 | -- Place the turtle at the lower left corner of the new tunnel (so that we were starting from the middle of the new tunnel) | |
346 | -- We only need this, if we have a tonnel with a width > 2 (<=2 means we are already on the right position) | |
347 | ||
348 | if width > 2 then | |
349 | ||
350 | MiningTurtle:turnLeft() | |
351 | ||
352 | for i = 0,math.floor( width / 2 ) do | |
353 | MiningTurtle:saveForward() | |
354 | end | |
355 | ||
356 | MiningTurtle:turnRight() | |
357 | end | |
358 | ||
359 | -- Length loop | |
360 | ||
361 | for i = 0,length-1 do | |
362 | ||
363 | MiningTurtle:saveForward() | |
364 | ||
365 | if i % 2 == 0 then | |
366 | -- even | |
367 | ||
368 | -- only change direction if we mine a tunnel wider than 1 block | |
369 | if width > 1 then | |
370 | MiningTurtle:turnRight() | |
371 | end | |
372 | ||
373 | for x = 0,width-1 do | |
374 | ||
375 | MiningTurtle:tick() | |
376 | ||
377 | for y = 0,height-3 do | |
378 | ||
379 | if x % 2 == 0 then | |
380 | MiningTurtle:saveUp() | |
381 | else | |
382 | MiningTurtle:saveDown() | |
383 | end | |
384 | ||
385 | end | |
386 | ||
387 | if height > 1 then | |
388 | turtle.digUp() | |
389 | end | |
390 | ||
391 | if x < width-1 then | |
392 | MiningTurtle:saveForward() | |
393 | end | |
394 | end | |
395 | ||
396 | -- same as above | |
397 | if width > 1 then | |
398 | MiningTurtle:turnLeft() | |
399 | end | |
400 | ||
401 | else | |
402 | -- odd | |
403 | ||
404 | if width > 1 then | |
405 | MiningTurtle:turnLeft() | |
406 | end | |
407 | ||
408 | for x = 0,width-1 do | |
409 | ||
410 | MiningTurtle:tick() | |
411 | ||
412 | for y = 0,height-3 do | |
413 | ||
414 | if x % 2 == 1 or width % 2 == 0 then | |
415 | MiningTurtle:saveUp() | |
416 | else | |
417 | MiningTurtle:saveDown() | |
418 | end | |
419 | ||
420 | end | |
421 | ||
422 | if height > 1 then | |
423 | turtle.digUp() | |
424 | end | |
425 | ||
426 | if x < width-1 then | |
427 | MiningTurtle:saveForward() | |
428 | end | |
429 | end | |
430 | ||
431 | if width > 1 then | |
432 | MiningTurtle:turnRight() | |
433 | end | |
434 | end | |
435 | ||
436 | end | |
437 | ||
438 | if ret then | |
439 | ||
440 | -- Go up to not remove torches on the floow | |
441 | MiningTurtle:saveUp() | |
442 | ||
443 | MiningTurtle:goTo( returnPos ) | |
444 | ||
445 | MiningTurtle:turnTo( returnDirection ) | |
446 | ||
447 | end | |
448 | ||
449 | end | |
450 | ||
451 | function MiningTurtle:tick() | |
452 | ||
453 | -- refuel loop | |
454 | while not MiningTurtle:requiredRefuel( 20, 50 ) do | |
455 | sleep(1) | |
456 | end | |
457 | end | |
458 | ||
459 | -- | |
460 | function MiningTurtle:matrix( rows, cols ) | |
461 | ||
462 | MiningTurtle:tick() | |
463 | ||
464 | --MiningTurtle:toBaseLevel() | |
465 | MiningTurtle:toGround() | |
466 | ||
467 | MiningTurtle:tunnel(5, 2, 1, true) | |
468 | ||
469 | ||
470 | end | |
471 | ||
472 | ||
473 | -- main program | |
474 | ||
475 | baby = MiningTurtle:new() | |
476 | ||
477 | baby:matrix( 10, 10 ) |