SHOW:
|
|
- or go back to the newest paste.
1 | --[[ | |
2 | ||
3 | Rectangular Quarry Program 1.1b | |
4 | by Adam Smith "shiphorns" | |
5 | January 2013 | |
6 | ||
7 | 1.0b - Original public release | |
8 | 1.1b - Fixes bug with turtle using the wrong axis order when trying to return home after hitting | |
9 | and undiggable block. I erroneously had it trying to do moveTo(0,0,0) instead of goHome() | |
10 | which would result in the turtle trying to move in the x-direction first, and possibly | |
11 | getting blocked by bedrock when trying to move home. | |
12 | ||
13 | --]] | |
14 | local tArgs = { ... } | |
15 | local sizeZ -- Quarry is this long in direction turtle is initially facing, including block turtle is on | |
16 | local sizeX -- Quarry is this wide to the right of where turtle is facing, including block turtle is on | |
17 | local sizeY -- Quarry removes this many layers of blocks including layer where turtle starts | |
18 | local bDebug= false | |
19 | ||
20 | local goUnload -- Forward declaration | |
21 | ||
22 | if (#tArgs == 1) then | |
23 | sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[1]),256 | |
24 | elseif (#tArgs == 2) then | |
25 | sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),256 | |
26 | elseif (#tArgs >= 3) then | |
27 | sizeZ,sizeX,sizeY = tonumber(tArgs[1]),tonumber(tArgs[2]),tonumber(tArgs[3]) | |
28 | if (#tArgs > 3) then | |
29 | bDebug = (tonumber(tArgs[4])==1) | |
30 | end | |
31 | else | |
32 | print( "Usage: quarry <sq. size> <optional width > <optional fixed depth> <optional 1 for debug mode>" ) | |
33 | return | |
34 | end | |
35 | ||
36 | -- Validate dimensions | |
37 | if (sizeX<2 or sizeZ<2 or sizeY<1) then | |
38 | print( "Dimensions given must be at least 2L x 2W x 1D. Efficiency is optimal if fixed depth is a multiple of 3." ) | |
39 | return | |
40 | end | |
41 | ||
42 | local minFuel = math.ceil((math.ceil(sizeY/3)*(sizeX*sizeY)+(2*sizeY))/1200) | |
43 | local maxFuel = "TBD" | |
44 | ||
45 | print("Place fuel reserves in slot 1 (upper left) if desired and hit any key to start.") | |
46 | os.pullEvent("char") | |
47 | ||
48 | local tX,tZ,tY = 0,0,0 -- Place the turtle starts is considered block 0,0,0 in the turtle's local coordinate system | |
49 | local xDir,zDir = 0,1 -- Turtle is considered as initially facing positive z direction, regardless of global world facing direction | |
50 | local refuelSlot = 1 -- Turtle can fuel from any slot, but it will never dump this slot's contents so this is where fuel should be placed | |
51 | ||
52 | -- Notice that all coordinates formated as 0,0,0 are in X,Z,Y order, NOT alphabetical X,Y,Z order, where Y is up/down axis | |
53 | -- Y axis is always the minecraft world Y-axis, but X and Z in this turtle's local coordinate system won't necessarily match his | |
54 | -- orientation in the global world system (turtle's system is relative to how he is initially facing, +Z is his facing direction) | |
55 | ||
56 | local function checkFuel(bMovingAwayFromOrigin) | |
57 | if bDebug then print("checkFuel()") end | |
58 | -- This function returns true only if there is enough fuel left to move 1 block in any direction, | |
59 | -- and still have enough left over for a return trip to 0,0,0 that might be needed for refuel or at | |
60 | -- the end of the quarrying. This ensures the turtle is never stranded in the quarry. | |
61 | local fuelLevel = turtle.getFuelLevel() | |
62 | ||
63 | if (fuelLevel == "unlimited") then | |
64 | -- Server has fuel requirement turned off in configs | |
65 | return true | |
66 | end | |
67 | ||
68 | -- If the turtle is attempting to move away from its starting location, it is going to | |
69 | -- consume the normal 1 fuel cost to move, but it will also add +1 to the cost of the | |
70 | -- trip to return home to dump/refuel/finish. If we know the turtle is moving closer to | |
71 | -- home, there is no extra cost since it is effectively part of the return trip. | |
72 | local fuelNeeded = math.abs(tX)+math.abs(tY)+math.abs(tZ) | |
73 | if (bMovingAwayFromOrigin == nil or bMovingAwayFromOrigin == true) then | |
74 | -- Turtle is moving away from 0,0,0 or direction is unspecified (assume worst case), add 2 fuel | |
75 | fuelNeeded = fuelNeeded + 2 | |
76 | end | |
77 | ||
78 | if (fuelLevel >= fuelNeeded) then | |
79 | -- Turtle has enough fuel to do the next 1-block movement, plus enough to | |
80 | -- return home from there. | |
81 | return true | |
82 | end | |
83 | ||
84 | -- If we get here, turtle does not have enough fuel for the move plus a return to base | |
85 | -- First we will try to refuel from anything we find in the turtle's inventory. Failing that | |
86 | -- We will return to home and prompt the user to add fuel | |
87 | ||
88 | local slot = 1 | |
89 | turtle.select(slot) | |
90 | ||
91 | if bDebug then print("Entering while true do in checkFuel") end | |
92 | while true do | |
93 | if turtle.refuel(1) then | |
94 | -- Found fuel in current slot, consume 1, see if it's enough, if not loop again | |
95 | if (turtle.getFuelLevel()>=fuelNeeded) then | |
96 | print("Refueled from inventory, resuming quarrying...") | |
97 | return true | |
98 | end | |
99 | else | |
100 | -- Couldn't refuel from currently-selected slot, try next slot. If there are no more slots, ask for player help. | |
101 | if (slot < 16) then | |
102 | slot = slot + 1 | |
103 | turtle.select(slot) | |
104 | else | |
105 | -- There are no more slots to look in, reset selection so that we're ready to loop over all slots again, and so that the | |
106 | -- player sees slot 1 highlighted (fastest for turtle to find fuel in). Return to 0,0,0 if we can (in case turtle is | |
107 | -- under lava or otherwise inaccessible), prompt player to add fuel. | |
108 | ||
109 | return goUnload(true) | |
110 | end | |
111 | end | |
112 | end | |
113 | end | |
114 | ||
115 | local function turnLeft() | |
116 | turtle.turnLeft() | |
117 | xDir,zDir = -zDir,xDir | |
118 | return true | |
119 | end | |
120 | ||
121 | local function turnRight() | |
122 | turtle.turnRight() | |
123 | xDir,zDir = zDir,-xDir | |
124 | return true | |
125 | end | |
126 | ||
127 | local function goForward(bCheckFuel) | |
128 | if bDebug then print("goForward()") end | |
129 | -- Can't move without fuel. checkFuel() will wait on player if necessary. | |
130 | if (bCheckFuel==true or bCheckFuel==nil) then | |
131 | checkFuel((xDir>0 and tX>=0) or (xDir<0 and tX<=0) or (zDir>0 and tZ>=0) or (zDir<0 and tZ<=0)) -- Passes boolean true if moving away from 0,0,0 | |
132 | end | |
133 | ||
134 | local tries = 3 | |
135 | while not turtle.forward() do | |
136 | if bDebug then print("goForward: while not turtle.forward() do tries="..tries) end | |
137 | if turtle.detect() then | |
138 | if bDebug then print("goForward: detect") end | |
139 | if not turtle.dig() then | |
140 | print("Undiggable block encountered. Will retry in 5 seconds.") | |
141 | -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure | |
142 | tries = tries - 1 | |
143 | if (tries <= 0) then | |
144 | return false | |
145 | else | |
146 | if bDebug then print("goForward: sleep(5)") end | |
147 | sleep(5) -- Wait 5 seconds, hope the problem resolves itself | |
148 | end | |
149 | end | |
150 | elseif turtle.attack() then | |
151 | if bDebug then print("goForward: attack") end | |
152 | -- Had to attack player or mob. You can add additional code here such | |
153 | -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program | |
154 | -- to collect ores, not rotten flesh and bones, so this block is empty. | |
155 | else | |
156 | -- Unknown obstruction, possibly a player in | |
157 | -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone. | |
158 | if bDebug then | |
159 | print("goForward: sleep(0.5) else block") | |
160 | print("Turtle fuel="..turtle.getFuelLevel()) | |
161 | end | |
162 | sleep(0.5) | |
163 | end | |
164 | end | |
165 | ||
166 | tX = tX + xDir -- If we're moving in the xDir, this will change tX by + or - 1 | |
167 | tZ = tZ + zDir -- If we're moving in the zDir, this will change tZ by + or - 1 | |
168 | ||
169 | return true -- Turtle moved successfully | |
170 | end | |
171 | ||
172 | local function goDown(bCheckFuel) | |
173 | if bDebug then print("goDown()") end | |
174 | -- Can't move without fuel. checkFuel() will wait on player if necessary. | |
175 | if (bCheckFuel==true or bCheckFuel==nil) then | |
176 | checkFuel(tY<=0) -- Passes boolean true if moving away from 0,0,0 | |
177 | end | |
178 | ||
179 | local tries = 3 | |
180 | while not turtle.down() do | |
181 | if bDebug then print("goDown: while not turtle.down() do tries="..tries) end | |
182 | if turtle.detectDown() then | |
183 | if bDebug then print("goDown: detectDown") end | |
184 | if not turtle.digDown() then | |
185 | print("Undiggable block encountered. Will retry in 5 seconds") | |
186 | -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure | |
187 | tries = tries - 1 | |
188 | if (tries <= 0) then | |
189 | return false | |
190 | else | |
191 | if bDebug then print("goDown: sleep(5)") end | |
192 | sleep(5) -- Wait 5 seconds, hope the problem resolves itself | |
193 | end | |
194 | end | |
195 | elseif turtle.attackDown() then | |
196 | if bDebug then print("goDown: attack") end | |
197 | -- Had to attack player or mob. You can add additional code here such | |
198 | -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program | |
199 | -- to collect ores, not rotten flesh and bones, so this block is empty. | |
200 | else | |
201 | -- Unknown obstruction, possibly a player in | |
202 | -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone. | |
203 | if bDebug then print("goDown: sleep(0.5)") end | |
204 | sleep(0.5) | |
205 | end | |
206 | end | |
207 | ||
208 | tY = tY - 1 | |
209 | return true -- Turtle moved successfully | |
210 | end | |
211 | ||
212 | local function goUp(bCheckFuel) | |
213 | if bDebug then print("goUp()") end | |
214 | ||
215 | -- Can't move without fuel. checkFuel() will wait on player if necessary. | |
216 | if (bCheckFuel==true or bCheckFuel==nil) then | |
217 | checkFuel(tY>=0) -- Passes boolean true if moving away from 0,0,0 | |
218 | end | |
219 | ||
220 | local tries = 3 | |
221 | while not turtle.up() do | |
222 | if bDebug then print("goUp: while not loop tries="..tries) end | |
223 | if turtle.detectUp() then | |
224 | if bDebug then print("goUp: detectUp") end | |
225 | if not turtle.digUp() then | |
226 | print("Undiggable block encountered. Will retry in 5 seconds.") | |
227 | -- Turtle is blocked. In case this is a temporary glitch, we try 3 times before conceding hard failure | |
228 | tries = tries - 1 | |
229 | if (tries <= 0) then | |
230 | return false | |
231 | else | |
232 | sleep(10) -- Wait 10 seconds, hope the problem resolves itself | |
233 | end | |
234 | end | |
235 | elseif turtle.attackUp() then | |
236 | if bDebug then print("goUp: attack") end | |
237 | -- Had to attack player or mob. You can add additional code here such | |
238 | -- as turtle.suck() if you want to collect killed mob loot. This is a quarry program | |
239 | -- to collect ores, not rotten flesh and bones, so this block is empty. | |
240 | else | |
241 | -- Unknown obstruction, possibly a player in | |
242 | -- peaceful or creative mode. Try again in 0.5 seconds and hope it's gone. | |
243 | if bDebug then print("goUp: sleep(0.5)") end | |
244 | sleep(0.5) | |
245 | end | |
246 | end | |
247 | ||
248 | tY = tY + 1 | |
249 | return true -- Turtle moved successfully | |
250 | end | |
251 | ||
252 | local function orient(targetXdir, targetZdir) | |
253 | -- One of the supplied directions should be -1 or +1, the other should be 0. | |
254 | if ((targetXdir ~= 0) and (targetZdir ~= 0)) or ((targetXdir==0) and (targetZdir==0)) then | |
255 | print("orient() given mutually exclusive values: "..targetXdir..", "..targetZdir) | |
256 | return false | |
257 | end | |
258 | ||
259 | if (((targetXdir ~= 0) and (math.abs(targetXdir) ~= 1)) or ((targetZdir ~= 0) and (math.abs(targetZdir) ~= 1))) then | |
260 | print("orient() given bad values: "..targetXdir..", "..targetZdir) | |
261 | return false | |
262 | end | |
263 | ||
264 | if (targetXdir ~= 0) and (targetXdir ~= xDir) then | |
265 | -- x axis alignment requested, and differs from current alignment | |
266 | if (xDir ~= 0) then | |
267 | -- Turtle is x-axis aligned 180 from target | |
268 | turnLeft() | |
269 | turnLeft() | |
270 | elseif (zDir == targetXdir) then | |
271 | turnRight() | |
272 | else | |
273 | turnLeft() | |
274 | end | |
275 | elseif (targetZdir ~= 0) and (targetZdir ~= zDir) then | |
276 | -- z axis alignment requested, and differs from current alignment | |
277 | if (zDir ~= 0) then | |
278 | -- Turtle is z-axis aligned 180 from target | |
279 | turnLeft() | |
280 | turnLeft() | |
281 | elseif (xDir == targetZdir) then | |
282 | turnLeft() | |
283 | else | |
284 | turnRight() | |
285 | end | |
286 | end | |
287 | ||
288 | return true | |
289 | end | |
290 | ||
291 | ||
292 | local function moveTo(targetX,targetZ,targetY) | |
293 | ||
294 | local fuelNeeded = math.abs(tX-targetX)+math.abs(tY-targetY)+math.abs(tZ-targetZ) | |
295 | if not (turtle.getFuelLevel()>=fuelNeeded) then | |
296 | print("Error: Turtle ended up in the unexpected state of not having enough fuel to return home.") | |
297 | return false | |
298 | end | |
299 | ||
300 | -- If not at tZ==targetZ, move the right direction until tZ==targetZ | |
301 | if (tZ>targetZ) then orient(0,-1) end | |
302 | if (tZ<targetZ) then orient(0,1) end | |
303 | while (tZ~=targetZ) do | |
304 | if bDebug then print("moveTo while tZ~=targetZ tZ="..tZ.." targetZ="..targetZ) end | |
305 | if not goForward(false) then | |
306 | -- Critical movement fail, bail | |
307 | return false | |
308 | end | |
309 | end | |
310 | ||
311 | -- If not at tX==targetX, move the right direction until tX==targetX | |
312 | if (tX>targetX) then orient(-1,0) end | |
313 | if (tX<targetX) then orient(1,0) end | |
314 | while (tX~=targetX) do | |
315 | if bDebug then print("moveTo while tX~=targetX tX="..tX.." targetX="..targetX) end | |
316 | if not goForward(false) then | |
317 | -- Critical movement fail, bail | |
318 | return false | |
319 | end | |
320 | end | |
321 | ||
322 | while (tY<targetY) do | |
323 | if bDebug then print("moveTo while tY<targetY tY="..tY.." targetY="..targetY) end | |
324 | if not goUp(false) then | |
325 | -- Critical movement fail, bail | |
326 | return false | |
327 | end | |
328 | end | |
329 | ||
330 | while (tY>targetY) do | |
331 | if bDebug then print("moveTo while tY>targetY tY="..tY.." targetY="..targetY) end | |
332 | if not goDown(false) then | |
333 | -- Critical movement fail, bail | |
334 | return false | |
335 | end | |
336 | end | |
337 | ||
338 | return true | |
339 | end | |
340 | ||
341 | --[[ | |
342 | ||
343 | START OF THE MAIN PROGRAM | |
344 | ||
345 | --]] | |
346 | ||
347 | - | checkFreeSlot() |
347 | + | |
348 | local traversal = 1 -- Counts x-z layers we're rasterizing. Used to determine turning directions at end of columns and layers | |
349 | local lowestY = 1-sizeY | |
350 | local bDigBelow, bDigAbove = false, false -- initially false | |
351 | while true do -- This loops digging layers | |
352 | print("Main loop traversal="..traversal.." tY="..tY.." lowestY="..lowestY) | |
353 | ||
354 | if (traversal==1) then --special case since turtle initially starts NOT on a layer that it just dug out. | |
355 | if ((tY - lowestY) == 0) then | |
356 | bDigBelow, bDigAbove = false, false | |
357 | elseif ((tY - lowestY) == 1) then | |
358 | bDigBelow, bDigAbove = true, false | |
359 | elseif ((tY - lowestY) >= 2) then | |
360 | bDigBelow, bDigAbove = true, true | |
361 | if not goDown() then | |
362 | -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned | |
363 | lowestY = tY - 1 | |
364 | end | |
365 | else | |
366 | -- Error: turtle is not in an expected place | |
367 | print("Error: Turtle vertical position is not what we expect on 1st traversal. Aborting, please debug.") | |
368 | abort = true | |
369 | break | |
370 | end | |
371 | else | |
372 | -- Not our first traversal, and turtle should now be on the last layer it dug out. | |
373 | if ((tY - lowestY) == 1) then | |
374 | bDigBelow, bDigAbove = true, false | |
375 | elseif ((tY - lowestY) == 2) then | |
376 | bDigBelow, bDigAbove = true, false | |
377 | if not goDown() then | |
378 | -- Turtle can't go down, adjust lowestY because we can't go as deep as planned | |
379 | lowestY = tY - 1 | |
380 | end | |
381 | elseif ((tY - lowestY) >= 3) then | |
382 | bDigBelow, bDigAbove = true, true | |
383 | -- Try to descend 2, if either fails, adjust lowestY to just below where turtle is able to get to, and | |
384 | -- cancel the need to digAbove | |
385 | for j=1,2 do | |
386 | if not goDown() then | |
387 | -- Turtle can't dig down, adjust lowestY because we can't go as deep as planned | |
388 | lowestY = tY - 1 | |
389 | bDigAbove = false | |
390 | end | |
391 | end | |
392 | else | |
393 | -- Error: turtle is not in an expected place | |
394 | print("Error: Turtle vertical position is not what we expect on traversal>1. Aborting, please debug.") | |
395 | abort = true | |
396 | break | |
397 | end | |
398 | end | |
399 | ||
400 | ||
401 | ||
402 | for column=1,sizeX do -- This loops sizeX times digging out columns | |
403 | for block=1,(sizeZ-1) do -- this loops (sizeZ-1) times doing digDown and goForward to do all but the end of each column | |
404 | ||
405 | -- Since we're about to do a potentially ore-digging move, check for free space in inventory. | |
406 | -- hasFreeSlot() calls goUnload if necessary | |
407 | if not checkFreeSlot() then | |
408 | print("Error: checkFreeSlot failure.") | |
409 | abort = true | |
410 | break | |
411 | end | |
412 | ||
413 | if bDigBelow and turtle.detectDown() then | |
414 | if not turtle.digDown() then | |
415 | -- Turtle can't dig down, but we're not moving down so this is not a fatal error. | |
416 | -- It might be bedrock below turtle, but that's not a concern until turtle is level with it. | |
417 | end | |
418 | end | |
419 | ||
420 | if bDigAbove and turtle.detectUp() then | |
421 | if not turtle.digUp() then | |
422 | -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock. | |
423 | -- Because of the danger of entrapment, we're ending our quarrying here. | |
424 | print("Turtle below undiggable block, backing out and returning home.") | |
425 | turnRight() | |
426 | turnRight() | |
427 | if not goForward() then | |
428 | -- This failure we care about, because there is something blocking us that we | |
429 | -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home | |
430 | print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY) | |
431 | end | |
432 | abort = true | |
433 | break | |
434 | end | |
435 | sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel) | |
436 | ||
437 | -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can | |
438 | -- safely loop trying to dig up as long as something is falling on us (gravel, sand) | |
439 | while turtle.detectUp() do | |
440 | if bDebug then print("in while turtrle.detectUp() loop.") end | |
441 | if not turtle.digUp() then | |
442 | -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on... | |
443 | break | |
444 | end | |
445 | sleep(0.5) | |
446 | end | |
447 | end | |
448 | ||
449 | if not goForward() then | |
450 | -- This failure we care about, because there is something blocking us that we | |
451 | -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home | |
452 | print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY) | |
453 | abort = true | |
454 | break | |
455 | end | |
456 | end -- end of block loop | |
457 | ||
458 | -- If movement failed while traversing column, escape out | |
459 | if abort then | |
460 | -- unwinding | |
461 | break | |
462 | end | |
463 | ||
464 | -- Dig out the last block of this column | |
465 | if bDigBelow and turtle.detectDown() then | |
466 | if not turtle.digDown() then | |
467 | -- Turtle can't dig down, but we're not moving down so this is not a fatal error. | |
468 | -- It might be bedrock below turtle, but that's not a concern until turtle is level with it. | |
469 | end | |
470 | end | |
471 | ||
472 | -- Do last digUp in this column, if required by bDigAbove | |
473 | if bDigAbove and turtle.detectUp() then | |
474 | if not turtle.digUp() then | |
475 | -- Turtle can't dig up. This is actually concerning since we don't want to get him trapped under bedrock. | |
476 | -- Because of the danger of entrapment, we're ending our quarrying here. | |
477 | print("Turtle below undiggable block, backing out and returning home.") | |
478 | turnRight() | |
479 | turnRight() | |
480 | if not goForward() then | |
481 | -- This failure we care about, because there is something blocking us that we | |
482 | -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home | |
483 | print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY) | |
484 | end | |
485 | abort = true | |
486 | break | |
487 | end | |
488 | sleep(0.5) -- wait to see if anything falls on turtle from above (sand, gravel) | |
489 | ||
490 | -- First dig up was successful, so we haven't got an undiggable block above, and undiggables can't fall, so we can | |
491 | -- safely loop trying to dig up as long as something is falling on us (gravel, sand) | |
492 | while turtle.detectUp() do | |
493 | if bDebug then print("in while turtrle.detectUp() loop.") end | |
494 | if not turtle.digUp() then | |
495 | -- whatever is up there, we couldn't dig it, but it's not bedrock. Just move on... | |
496 | break | |
497 | end | |
498 | sleep(0.5) | |
499 | end | |
500 | end | |
501 | ||
502 | -- Turtle just finished the column, figure out if we need to advance to a new column, or | |
503 | -- if we've finished the layer. If we need to turn to start a new column, we have to figure out which | |
504 | -- direction to turn | |
505 | if (column<sizeX) then | |
506 | -- Turtle is at the end of a z column, but not the end of the whole x-z layer traversal | |
507 | ||
508 | -- FYI: These odd/even values are based on 1-based block, column, traversal numbers not 0-based tX, tZ, tY | |
509 | -- sorry if that's confusing, but Lua convention is to start loop indicies at 1 | |
510 | local evenCol = ((column%2)==0) | |
511 | local evenWidth = ((sizeX%2)==0) | |
512 | local evenLayer = ((traversal%2)==0) | |
513 | local backtrackingLayer = (evenWidth and evenLayer) | |
514 | ||
515 | if ((not evenCol and not backtrackingLayer) or (evenCol and backtrackingLayer)) then | |
516 | turnRight() -- turn towards next row | |
517 | if not goForward() then | |
518 | print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY) | |
519 | abort = true | |
520 | break | |
521 | end | |
522 | ||
523 | -- Danger check to see if we've moved under an undiggable block | |
524 | if bDigAbove and turtle.detectUp() and not turtle.digUp() then | |
525 | print("Turtle below undiggable block, backing out 1 and returning home.") | |
526 | turnRight() | |
527 | turnRight() | |
528 | if not goForward() then | |
529 | -- This failure we care about, because there is something blocking us that we | |
530 | -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home | |
531 | print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY) | |
532 | end | |
533 | abort = true | |
534 | break | |
535 | end | |
536 | ||
537 | turnRight() | |
538 | else | |
539 | turnLeft() | |
540 | if not goForward() then | |
541 | print("Fatal Error during goForward from column "..column.." to column "..(column+1).." tX="..tX.." tZ="..tZ.." tY="..tY) | |
542 | abort = true | |
543 | break | |
544 | end | |
545 | ||
546 | -- Danger check to see if we've moved under an undiggable block | |
547 | if bDigAbove and turtle.detectUp() and not turtle.digUp() then | |
548 | print("Turtle below undiggable block, backing out 1 and returning home.") | |
549 | turnRight() | |
550 | turnRight() | |
551 | if not goForward() then | |
552 | -- This failure we care about, because there is something blocking us that we | |
553 | -- can't dig, attack or otherwise resolve. Bust out of our digging loops and attempt to return home | |
554 | print("Fatal Error during column goForward tX="..tX.." tZ="..tZ.." tY="..tY) | |
555 | end | |
556 | abort = true | |
557 | break | |
558 | end | |
559 | ||
560 | turnLeft() | |
561 | end | |
562 | -- Turtle is now ready to start the next column | |
563 | else | |
564 | -- Turtle is at the end of the layer, rotate 180 | |
565 | turnRight() | |
566 | turnRight() | |
567 | end | |
568 | end -- end of column loop | |
569 | ||
570 | if abort then | |
571 | print("Abort breaking out of while true loop.") | |
572 | -- abort in progress, unwinding out of loops | |
573 | break | |
574 | end | |
575 | ||
576 | -- See if we're done yet | |
577 | if ((tY - lowestY) == 0) or (((tY - lowestY) == 1) and (bDigBelow == true)) then | |
578 | -- We're done. We've finished digging our lowest layer either by digging forward or with digDown. | |
579 | done = true | |
580 | break | |
581 | end | |
582 | ||
583 | -- If we got past the last if-then, we are not done, so we need to descend in preparation for next layer traversal | |
584 | if bDigBelow then | |
585 | -- We were digging below us on the traversal we just finished, so we need to drop down 2 levels to be on an undug layer | |
586 | -- First we try to descend through the dug-down layer, but since that could have bedrock (we we skimming above it not caring) | |
587 | -- we need to test to see if we can descend into that layer at our current tX,tZ location | |
588 | if not goDown() then | |
589 | print("Turtle finished a traversal and was digging below. Turtle can't go further down, we're done quarrying.") | |
590 | abort = true | |
591 | break | |
592 | end | |
593 | end | |
594 | ||
595 | traversal = traversal + 1 | |
596 | end -- end of while not done loop | |
597 | ||
598 | -- Quarrying ended, either normally or because of encountering undiggable block. Try to return to 0,0,0 | |
599 | if not goHome(0,0,0) then | |
600 | -- Can't even get back home :-( Notify the user | |
601 | print("Turtle was not able to safely get back to starting location") | |
602 | abort = true | |
603 | else | |
604 | orient(0,-1) | |
605 | ||
606 | -- Drop everything | |
607 | -- Drop items. Turtle will not empty the slot designated as the refuel slot. | |
608 | orient(0,1) | |
609 | end | |
610 | ||
611 | if abort then | |
612 | print("Quarrying ended due to encounter with undiggable block.") | |
613 | else | |
614 | print("Quarrying complete to desired depth.") | |
615 | end |