tClear.lua

1290 lines · 36.8 KB

Open raw

{program="tClear",version="1.10",date="2024-10-22"}

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tClear/tClear.lua
1--{program="tClear",version="1.10",date="2024-10-22"}
2---------------------------------------
3-- tClear by Kaikaku
4-- 2024-10-22, v1.10 UI fix
5-- 2021-03-28, v1.00 three-way digging
6-- 2016-03-13, v0.90 dig on way back
7-- 2014-09-27, v0.80 dig sand
8-- 2013-09-01, v0.10 initial
9---------------------------------------
10
11---------------------------------------
12---- DESCRIPTION ----------------------
13---------------------------------------
14-- Mining turtle digs specified cuboid
15-- (x,y,z) without leaving the area,
16-- using tripple dig :)
17-- Incl. option to missus as strip miner.
18
19
20---------------------------------------
21---- ASSUMPTIONS ----------------------
22---------------------------------------
23-- Requires a mininng turtle
24
25
26---------------------------------------
27---- VARIABLES: template --------------
28---------------------------------------
29local cVersion = "v1.10"
30local cPrgName = "tClear"
31local cMinFuel = 110
32local blnAskForParameters = true
33local blnShowUsage = false
34local blnDebugPrint = false --true
35local blnTurtle
36local isComputer = false
37local baseColor = colors.blue
38
39
40---------------------------------------
41---- VARIABLES: specific --------------
42---------------------------------------
43local slotFuel = 16
44
45local digDeep = 3 -- all but 0 are okay
46local digWide = 3 -- all but -1,0,1 -- if wide <=-1 then depth needs to be >=2
47local digHeight = 1 -- all but 0
48local digWideOrg
49local digDeepOrg
50local blnLayerByLayer = false
51local blnStartWithin = false
52local blnStripMine = false
53
54-- Chunky turtle variables
55local chunkyTurtleId = nil
56local blnUseChunky = false
57local chunkyPosition = { x = 0, y = 0, z = -1, facing = 0 } -- relative position of chunky turtle (to the left)
58
59---------------------------------------
60---- Early UI functions ---------------
61---------------------------------------
62local function swapColors()
63 local backColor = term.getBackgroundColor()
64 local textColor = term.getTextColor()
65
66 term.setBackgroundColor(textColor)
67 term.setTextColor(backColor)
68end
69
70local function printUI(strInput)
71 if strInput == ni then strInput = "" end
72
73 if strInput == "header" then
74 term.write("+-------------------------------------")
75 print("+")
76 elseif strInput == "line" then
77 term.write("+-------------------------------------")
78 print("+")
79 elseif strInput == "footer" then
80 term.write("+-------------------------------------")
81 print("+")
82 else
83 term.write("|")
84 strInput = strInput .. " "
85 term.write(string.sub(strInput, 1, 37))
86 print("|")
87 end
88end
89
90local function coloredTextAt(inputText, outputRow, outputCol, textColor, backColor)
91 -- writes and colors text to coordinates
92 local oldRow, oldCol = term.getCursorPos()
93 local oldTextColor = term.getTextColor()
94 local oldBackColor = term.getBackgroundColor()
95 if textColor == nil then textColor = term.getTextColor() end
96 if backColor == nil then backColor = term.getBackgroundColor() end
97
98 term.setTextColor(textColor)
99 term.setBackgroundColor(backColor)
100 term.setCursorPos(outputRow, outputCol)
101 term.write(inputText)
102 term.setCursorPos(oldRow, oldCol)
103 term.setTextColor(oldTextColor)
104 term.setBackgroundColor(oldBackColor)
105end
106
107---------------------------------------
108---- tArgs ----------------------------
109---------------------------------------
110local tArgs = { ... }
111term.clear()
112term.setCursorPos(1, 1)
113
114local parameterShowSleep = 1
115
116if #tArgs ~= 0 then
117 -- header
118 blnAskForParameters = false
119 term.clear()
120 term.setCursorPos(1, 1)
121 printUI("header")
122 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku. Enjoy!")
123 printUI("line")
124 coloredTextAt(cPrgName, 2, 2, baseColor)
125 print("Starting...")
126
127 if tArgs[1] ~= nil then
128 digDeep = math.floor(tonumber(tArgs[1]))
129 if digDeep <= 0 then
130 print("Parameter correction (depths must be >=1)!")
131 sleep(2)
132 parameterShowSleep = parameterShowSleep + 2
133 digDeep = 1
134 end
135 end
136 if tArgs[2] ~= nil then
137 digWide = math.floor(tonumber(tArgs[2]))
138 if digWide == 0 or digWide == 1 or digWide == -1 then
139 print("Parameter correction (width not 0 or 1)!")
140 sleep(2)
141 parameterShowSleep = parameterShowSleep + 2
142 digWide = 2
143 end
144 end
145 if tArgs[3] ~= nil then
146 digHeight = math.floor(tonumber(tArgs[3]))
147 if digHeight == 0 then
148 print("Parameter correction (height not 0)!")
149 sleep(2)
150 parameterShowSleep = parameterShowSleep + 2
151 digHeight = 1
152 end
153 end
154
155 --check combinations of min values
156 if digDeep == 1 and digWide < 0 then
157 error(
158 "Parameter combination not allowed: depth=1 with width<0! Hint: increase depths or move turtle to use positive width.")
159 end
160
161 -- further parameters 4+
162 for i = 4, #tArgs, 1 do
163 if string.lower(tArgs[i]) == "layerbylayer" or string.lower(tArgs[i]) == "layer" or string.lower(tArgs[i]) == "singlelayer" then
164 blnLayerByLayer = true
165 end
166 if string.lower(tArgs[i]) == "startwithin" or string.lower(tArgs[i]) == "within" or string.lower(tArgs[i]) == "in" then
167 blnStartWithin = true
168 end
169 if string.lower(tArgs[i]) == "stripmine" or string.lower(tArgs[i]) == "strip" or string.lower(tArgs[i]) == "mine" then
170 blnStripMine = true
171 end
172 end
173
174 -- show parameters
175 print("Clear depth = " .. digDeep)
176 print("Clear width = " .. digWide)
177 print("Clear height= " .. digHeight)
178 term.write("Option LayerByLayert= ")
179 if blnLayerByLayer then print("on") else print("off") end
180 term.write("Option StartWithin = ")
181 if blnStartWithin then print("on") else print("off") end
182 term.write("Option StripMine = ")
183 if blnStripMine then print("on") else print("off") end
184 sleep(parameterShowSleep)
185end
186
187if blnShowUsage then
188 term.clear()
189 term.setCursorPos(1, 1)
190 printUI("header")
191 printUI("" .. cPrgName .. ", by Kaikaku")
192 printUI("footer")
193 print("Usage: ", cPrgName, " depth, width [height ['LayerByLayer'] ['StartWithin'] ['StripMine']] ")
194 print()
195 print("If called with any parameter, then")
196 print(" there will be no info screen. Turtle")
197 print(" starts immediately.")
198 return
199end
200
201
202---------------------------------------
203-- basic functions for turtle control -
204---------------------------------------
205-- 2021-03-28 partly tPos (from tClear)
206-- 2021-03-20 arrangeArray, printArray (from tLoader)
207-- 2021-03-13 askForNumber, pressKeyNoSpecials, checkInventory(u), getFirstEmptySlot, identifyTool, findModem, ensureModem
208-- 2021-03-05 gfr -
209-- 2021-02-06 select+place -
210-- 2021-01-29 save turtle go func -
211-- checkInventory w/ exit -
212-- refuelManager -
213---------------------------------------
214local function gf(n)
215 if n == nil then n = 1 end
216 for i = 1, n, 1 do while not turtle.forward() do end end
217end
218local function gb(n)
219 if n == nil then n = 1 end
220 for i = 1, n, 1 do while not turtle.back() do end end
221end
222local function gu(n)
223 if n == nil then n = 1 end
224 for i = 1, n, 1 do while not turtle.up() do end end
225end
226local function gd(n)
227 if n == nil then n = 1 end
228 for i = 1, n, 1 do while not turtle.down() do end end
229end
230local function gl(n)
231 if n == nil then n = 1 end
232 for i = 1, n, 1 do while not turtle.turnLeft() do end end
233end
234local function gr(n)
235 if n == nil then n = 1 end
236 for i = 1, n, 1 do while not turtle.turnRight() do end end
237end
238
239local function df() turtle.dig() end
240local function du() turtle.digUp() end
241local function dd() turtle.digDown() end
242
243local function dfs() while turtle.dig() do end end
244local function dus() while turtle.digUp() do end end
245local function dds() while turtle.digDown() do end end
246
247local function pf() return turtle.place() end
248local function pu() return turtle.placeUp() end
249local function pd() return turtle.placeDown() end
250
251local function sf() turtle.suck() end
252local function su() turtle.suckUp() end
253local function sd() turtle.suckDown() end
254local function Df() turtle.drop() end
255local function Du(n) turtle.dropUp(n) end
256local function Dd() turtle.dropDown() end
257local function ss(s) turtle.select(s) end
258local function gic(s) return turtle.getItemCount(s) end
259
260local function gfs(n)
261 if n == nil then n = 1 end
262 for i = 1, n, 1 do while not turtle.forward() do df() end end
263end
264local function gbs(n)
265 if n == nil then n = 1 end
266 for i = 1, n, 1 do while not turtle.back() do
267 gl()
268 gl()
269 df()
270 gr()
271 gr()
272 end end
273end
274local function gus(n)
275 if n == nil then n = 1 end
276 for i = 1, n, 1 do while not turtle.up() do du() end end
277end
278local function gds(n)
279 if n == nil then n = 1 end
280 for i = 1, n, 1 do while not turtle.down() do dd() end end
281end
282local function pfs()
283 df()
284 turtle.place()
285end
286local function pus()
287 du()
288 turtle.placeUp()
289end
290local function pds()
291 dd()
292 turtle.placeDown()
293end
294
295local function gfr() return turtle.forward() end
296local function gbr() return turtle.back() end
297local function gur() return turtle.up() end
298local function gdr() return turtle.down() end
299
300local tPos = {}
301
302local function glPos(n)
303 if n == nil then n = 1 end
304 for i = 1, n, 1 do while not turtle.turnLeft() do end end
305end --tPos[4]=(tPos[4]-1)%4 end end
306local function grPos(n)
307 if n == nil then n = 1 end
308 for i = 1, n, 1 do while not turtle.turnRight() do end end
309end --tPos[4]=(tPos[4]+1)%4 end end
310local function gfPos(n)
311 if n == nil then n = 1 end
312 for i = 1, n, 1 do while not turtle.forward() do df() end end
313end --pCF(1) end end
314local function gbPos(n)
315 if n == nil then n = 1 end
316 for i = 1, n, 1 do while not turtle.back() do
317 gl()
318 gl()
319 df()
320 gr()
321 gr()
322 end end
323end --pCF(-1) end end
324local function guPos(n)
325 if n == nil then n = 1 end
326 for i = 1, n, 1 do
327 while not turtle.up() do du() end
328 tPos[3] = tPos[3] + 1
329 end
330end
331local function gdPos(n)
332 if n == nil then n = 1 end
333 for i = 1, n, 1 do
334 while not turtle.down() do dd() end
335 tPos[3] = tPos[3] - 1
336 end
337end
338
339
340local function spd(s, blnForward)
341 if s == nil then s = turtle.currentSlot() end
342 if blnForward == nil then blnForward = true end
343 ss(s)
344 pd()
345 if blnForward then gf() end
346end
347local function spu(s, blnForward)
348 if s == nil then s = turtle.currentSlot() end
349 if blnForward == nil then blnForward = true end
350 ss(s)
351 pu()
352 if blnForward then gf() end
353end
354local function spf(s, blnBack)
355 if s == nil then s = turtle.currentSlot() end
356 if blnBack == nil then blnBack = true end
357 ss(s)
358 pf()
359 if blnBack then gb() end
360end
361
362local function waitKey(strText)
363 local event, scancode
364 write(strText)
365 event, scancode = os.pullEvent("key")
366 print()
367end
368
369local function askForInputText(textt)
370 local at = ""
371 -- check prompting texts
372 if textt == nil then textt = "Enter text:" end
373
374 -- ask for input
375 write(textt)
376 at = read()
377 return at
378end
379
380local function askForNumber(askText, minValue, maxValue)
381 -- gets entered data, ensures it's a number and returns it
382 -- keeps asking if entry is not a number
383 -- adapts to min and max values
384 -- allways writes in screen line 13 (last for turtles)
385 -- calls askForInputText
386 local blnReask = true
387 local returnNumber = nil
388 if minValue == nil then minValur = 1 end
389 if maxValue == nil then maxValue = 100 end
390 if askText == nil then askText = "Key in number and press Enter: " end
391 while blnReask do
392 term.setCursorPos(1, 13)
393 returnNumber = askForInputText(askText)
394 if returnNumber == nil then
395 blnReask = true
396 else
397 returnNumber = tonumber(returnNumber)
398 if returnNumber == nil then
399 blnReask = true
400 else
401 returnNumber = math.floor(returnNumber)
402 if returnNumber > maxValue then returnNumber = maxValue end
403 if returnNumber < minValue then returnNumber = minValue end
404 blnReask = false
405 end
406 end
407 end
408 return returnNumber
409end
410
411local function pressKeyNoSpecials(askText)
412 -- excludes ctrl / alt / shifts
413 -- catches windows
414 -- retruns the key number (if needed at all)
415 local tmpEvent, tmpKey
416 if askText == nil then askText = "Press key to START! (stop w/ ctrl+t) " end
417 tmpKey = 341
418 while tmpKey >= 340 and tmpKey <= 346 do -- ctrls, alts, shifts
419 term.write(askText)
420 tmpEvent, tmpKey = os.pullEvent("key")
421 if tmpKey == nil then tmpKey = 341 end -- win
422 end
423 return tmpKey
424end
425
426local function checkFuel()
427 local tmp = turtle.getFuelLevel()
428 return tmp
429end
430
431local function checkTurtle(blnOnlyIdentify)
432 if blnOnlyIdentify == nil then blnOnlyIdentify = false end
433 -- turtle?
434 local turtleOk, turtleVal = pcall(checkFuel)
435 if not turtleOk then
436 blnTurtle = false
437 if not blnOnlyIdentify then
438 term.clear()
439 term.setCursorPos(1, 1)
440 printUI("header")
441 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
442 printUI("line")
443 printUI("This is a turtle program.")
444 printUI(" Please, execute it with a turtle!")
445 printUI("footer")
446
447 coloredTextAt(cPrgName, 2, 2, baseColor)
448 error()
449 end
450 else
451 blnTurtle = true
452 end
453end
454
455local function sleepDots(sec, duration)
456 if sec == nil then sec = 10 end
457 if sec < 1 then return end
458 if duration == nil then duration = 1 end -- shorten durtation for more dots
459
460 for i = 1, sec - 1 do
461 sleep(1 * duration)
462 term.write(".")
463 end
464
465 sleep(1)
466 print(".")
467end
468
469local function checkInventory(s, nMin, nMax, textt, blnExitOnFalse, blnRepeat)
470 -- checks if in slot s are not less than nMin and not more than nMax items
471 -- returns true if okay
472 -- if not displays textt
473 -- blnExitOnFalse=true raises an error
474 -- blnRepeat=true repeatedly sks to put in the right number of items (overrides blnExitOnFalse)
475 local oldSlot = turtle.getSelectedSlot()
476 if s == nil then s = turtle.getSelectedSlot() end
477 if nMin == nil then nMin = 0 end
478 if nMax == nil then nMax = 64 end
479 if blnExitOnFalse == nil then blnExitOnFalse = false end
480 if blnRepeat == nil then blnRepeat = false end
481 if blnRepeat ~= true then blnRepeat = false end
482
483 while true do
484 if turtle.getItemCount(s) < nMin or turtle.getItemCount(s) > nMax then
485 print(textt)
486 if not blnRepeat then
487 -- single check ends with this
488 if blnExitOnFalse then
489 error()
490 else
491 ss(oldSlot)
492 return false
493 end
494 end
495 -- repeated check
496 ss(s)
497 sleepDots(3)
498 else
499 -- everything is fine
500 ss(oldSlot)
501 return true
502 end
503 end
504end
505
506local function refuelFromSlot(s, n) -- slot, amount to consume
507 if s == nil then s = 16 end
508 if n == nil then n = 64 end
509 local currentSlot = turtle.getSelectedSlot()
510 local fuelItems = turtle.getItemCount(s)
511 local returnValue = false
512
513 if fuelItems > 0 then
514 ss(s)
515 returnValue = turtle.refuel(n)
516 ss(currentSlot)
517 end
518 return returnValue
519end
520
521local function refuelManager(setMinFuel, setSlotFuel, waitTime)
522 local currentSlotSelected = turtle.getSelectedSlot()
523 ss(setSlotFuel)
524 while turtle.getFuelLevel() < setMinFuel do
525 print("Need more fuel (" .. turtle.getFuelLevel() .. "/" .. setMinFuel .. ").")
526 if not refuelFromSlot(setSlotFuel) then
527 -- unsucessfull try
528 print(" Please, put fuel items in slot " .. setSlotFuel .. "!")
529 term.write(" Sleeping " .. waitTime .. " seconds")
530 sleepDots(waitTime)
531 else
532 print("Refueled...")
533 end
534 end
535 ss(currentSlotSelected)
536end
537
538local function debugPrint(str)
539 if blnDebugPrint then print(str) end
540end
541
542local function inspectFor(blockArray, strDirection) -- <<-- from TreeFarm
543 if strDirection == nil then strDirection = "f" end
544 local blnOk, data
545
546 -- inspect
547 if strDirection == "d" then
548 blnOk, data = turtle.inspectDown()
549 elseif strDirection == "u" then
550 blnOk, data = turtle.inspectUp()
551 elseif strDirection == "f" then
552 blnOk, data = turtle.inspect()
553 else
554 print("Warning: Unknown direction '", strDirection, "' in inspectFor, taking (f)orward instead.")
555 strDirection = "f"
556 blnOk, data = turtle.inspect()
557 end
558 if data.name ~= nil then debugPrint("Found:" .. string.lower(data.name)) end
559 -- compare
560 local i = 1
561 while blockArray[i] ~= nil do
562 debugPrint("Compare to:" .. string.lower(blockArray[i]))
563 if data.name ~= nil then
564 if string.lower(data.name) == string.lower(blockArray[i]) then return true end
565 end
566 i = i + 1
567 end
568
569 return false -- reached a nil value
570end
571
572
573local function getFirstEmptySlot(startAt)
574 if startAt == nil then startAt = 1 end
575 if startAt > 16 or startAt < 1 then return nil end
576
577 for i = startAt, 16, 1 do
578 if gic(i) == 0 then return i end
579 end
580 for i = 1, startAt, 1 do
581 if gic(i) == 0 then return i end
582 end
583 return nil
584end
585
586local function identifyTool(toolSide)
587 -- returns name of tool at side toolSide
588 -- returns no_tool if there is none
589 -- requires at least one empty slot for tool check (throws error)
590 if toolSide == nil then toolSide = "right" end
591 if toolSide ~= "right" and toolSide ~= "r" then toolSide = "left" end
592 local slotNumber = getFirstEmptySlot()
593 local slotSelected = turtle.getSelectedSlot()
594 local toolName = "no_tool"
595
596 if slotNumber == nil then error("Couldn't find empty slot to check tool on side '" .. toolSide .. "'!") end
597 ss(slotNumber)
598 -- step 1: get name
599 if toolSide == "right" or toolSide == "r" then
600 turtle.equipRight()
601 else
602 turtle.equipLeft()
603 end
604 -- step 2: get name
605 local data = turtle.getItemDetail()
606 if data ~= nil then toolName = data.name end
607 -- step 3: re-equipget name
608 if toolSide == "right" or toolSide == "r" then
609 turtle.equipRight()
610 else
611 turtle.equipLeft()
612 end
613 ss(slotSelected)
614 return toolName
615end
616
617
618local function findModem() -- <<-- delete if no modem used
619 for _, p in pairs(rs.getSides()) do
620 if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then return true, p end
621 end
622 return false, nil
623end
624
625-- Chunky turtle communication functions
626local function findChunkyTurtle()
627 -- Look for available chunky turtles
628 print("Broadcasting find_chunky message...")
629 local timer = os.startTimer(3) -- Wait 3 seconds for responses
630 local availableChunkies = {}
631
632 -- Send broadcast to find chunky turtles
633 rednet.broadcast({
634 type = "find_chunky",
635 masterId = os.getComputerID(),
636 timestamp = os.time()
637 }, "tclear-chunky")
638
639 print("Waiting for chunky turtle responses...")
640
641 -- Collect responses
642 local event, timerId
643 repeat
644 event, timerId = os.pullEvent()
645 if event == "rednet_message" then
646 local senderId, message, protocol = rednet.receive(0.1)
647 if senderId then
648 print("Received message from " .. senderId .. " on protocol '" .. (protocol or "none") .. "'")
649 if message and message.type == "chunky_available" then
650 table.insert(availableChunkies, senderId)
651 print("Found chunky turtle: " .. senderId)
652 else
653 print("Message type: " .. (message and message.type or "nil"))
654 end
655 end
656 end
657 until event == "timer" and timerId == timer
658
659 print("Discovery timeout - found " .. #availableChunkies .. " chunky turtles")
660
661 if #availableChunkies > 0 then
662 chunkyTurtleId = availableChunkies[1] -- Use first available
663 print("Attempting to pair with chunky turtle: " .. chunkyTurtleId)
664 -- Pair with the chunky turtle
665 rednet.send(chunkyTurtleId, {
666 type = "pair",
667 masterId = os.getComputerID(),
668 timestamp = os.time()
669 }, "tclear-chunky")
670 print("Paired with chunky turtle: " .. chunkyTurtleId)
671 blnUseChunky = true
672 return true
673 else
674 print("No chunky turtles found - check that chunky turtle is running and has wireless modem")
675 end
676 return false
677end
678
679local function moveChunkyTurtle(mainX, mainY, mainZ, mainFacing)
680 if blnUseChunky and chunkyTurtleId then
681 -- Calculate chunky turtle position relative to main turtle (to the left side)
682 -- Chunky turtle should be at same height, same Y level, but offset to the left
683 local chunkyX = mainX -- Same X position (depth)
684 local chunkyY = mainY -- Same height
685 local chunkyZ = mainZ - 1 -- One block to the left (negative Z)
686 local chunkyFacing = mainFacing -- Same facing direction
687
688 rednet.send(chunkyTurtleId, {
689 type = "move",
690 target = { x = chunkyX, y = chunkyY, z = chunkyZ, facing = chunkyFacing },
691 timestamp = os.time()
692 }, "tclear-chunky")
693 chunkyPosition = { x = chunkyX, y = chunkyY, z = chunkyZ, facing = chunkyFacing }
694 print("Sent chunky turtle to position: (" ..
695 chunkyX .. "," .. chunkyY .. "," .. chunkyZ .. ") facing=" .. chunkyFacing)
696 end
697end
698
699local function stopChunkyTurtle()
700 if blnUseChunky and chunkyTurtleId then
701 rednet.send(chunkyTurtleId, {
702 type = "stop",
703 timestamp = os.time()
704 }, "tclear-chunky")
705 end
706end
707
708
709------------------------------------------------------------------------------
710-- main: description ---------------------------------------------------------
711------------------------------------------------------------------------------
712checkTurtle()
713
714-- Initialize rednet for chunky turtle communication
715local hasModem, modemSide = findModem()
716if hasModem then
717 rednet.open(modemSide)
718 print("Rednet opened for chunky turtle communication")
719else
720 print("No modem found - chunky turtle pairing disabled")
721end
722
723if blnAskForParameters then
724 term.clear()
725 term.setCursorPos(1, 1)
726 local iPage = 0
727 local iPageMax = 5
728 local event, key, isHeld
729 local blnLoop = true
730
731 term.clear()
732 term.setCursorPos(1, 1)
733 iPage = iPage + 1
734 printUI("header")
735 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
736 printUI("line")
737 -- 1234567890123456789012345678901234567
738 printUI("Program features:")
739 printUI("* Quick: mines 3 layers in 1 go")
740 printUI("* Precise: mines and moves only ")
741 printUI(" within the specified area")
742 printUI("* Versatile: place turtle at any")
743 printUI(" corner or within corner to start")
744 printUI("* Lava sparing: with layer by layer")
745 printUI("* Stripmine: may be misused for this")
746 printUI("footer")
747
748 coloredTextAt(cPrgName, 2, 2, baseColor)
749 coloredTextAt("Quick", 4, 5, baseColor)
750 coloredTextAt("Precise", 4, 6, baseColor)
751 coloredTextAt("Versatile", 4, 8, baseColor)
752 coloredTextAt("Lava sparing", 4, 10, baseColor)
753 coloredTextAt("Stripmine", 4, 11, baseColor)
754
755 pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t) ")
756 ---
757
758 term.clear()
759 term.setCursorPos(1, 1)
760 iPage = iPage + 1
761 printUI("header")
762 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
763 printUI("line")
764 -- 1234567890123456789012345678901234567
765 printUI("Program mines all blocks of a")
766 printUI(" d * w * h cuboid. ")
767 printUI(" ")
768 printUI(" height=" .. digHeight)
769 printUI("  . ")
770 printUI(" | / depth=" .. digDeep)
771 printUI(" |/ ")
772 printUI(" +---- width=" .. digWide)
773 printUI("footer")
774 term.write("Key in depth and press Enter: ")
775
776 coloredTextAt(cPrgName, 2, 2, baseColor)
777 coloredTextAt("<-- enter (d>=1)", 18, 9, baseColor)
778 event, key, isHeld = os.pullEvent("key")
779 digDeep = askForNumber("Key in depth and press Enter: ", 1, 999)
780 ---
781
782 term.clear()
783 term.setCursorPos(1, 1)
784 iPage = iPage + 1
785 printUI("header")
786 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
787 printUI("line")
788 -- 1234567890123456789012345678901234567
789 printUI("Program mines all blocks of a")
790 printUI(" d * w * h cuboid. ")
791 printUI(" ")
792 printUI(" height=" .. digHeight)
793 printUI("  . ")
794 printUI(" | / depth=")
795 printUI(" |/ ")
796 printUI(" +---- width=" .. digWide)
797 printUI("footer")
798 term.write("Key in width and press Enter: ")
799
800 coloredTextAt(cPrgName, 2, 2, baseColor)
801 coloredTextAt("(w>=2 or", 31, 10, baseColor)
802 coloredTextAt("<-- enter w<=-2)", 21, 11, baseColor)
803 coloredTextAt(digDeep, 14, 9, baseColor)
804 event, key, isHeld = os.pullEvent("key")
805 digWide = askForNumber("Key in width and press Enter: ", -999, 999)
806 if digWide == -1 or digWide == 0 or digWide == 1 then digWide = 2 end
807 ---
808
809 term.clear()
810 term.setCursorPos(1, 1)
811 iPage = iPage + 1
812 printUI("header")
813 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
814 printUI("line")
815 -- 1234567890123456789012345678901234567
816 printUI("Program mines all blocks of a")
817 printUI(" d * w * h cuboid. ")
818 printUI(" ")
819 printUI(" height=" .. digHeight)
820 printUI("  . ")
821 printUI(" | / depth=")
822 printUI(" |/ ")
823 printUI(" +---- width=")
824 printUI("footer")
825 term.write("Key in height and press Enter: ")
826
827 coloredTextAt(cPrgName, 2, 2, baseColor)
828 coloredTextAt("<-- enter (h<>0)", 15, 7, baseColor)
829 coloredTextAt(digDeep, 14, 9, baseColor)
830 coloredTextAt(digWide, 17, 11, baseColor)
831 event, key, isHeld = os.pullEvent("key")
832 digHeight = askForNumber("Key in height and press Enter: ", -999, 999)
833 if digHeight == 0 then digHeight = 1 end
834 ---
835
836 term.clear()
837 term.setCursorPos(1, 1)
838 iPage = iPage + 1
839 printUI("header")
840 printUI("" .. cPrgName .. ", " .. cVersion .. ", by Kaikaku (" .. iPage .. "/" .. iPageMax .. ")")
841 printUI("line")
842 -- 1234567890123456789012345678901234567
843 printUI("Program mines all blocks of a")
844 printUI(" d * w * h cuboid. ")
845 printUI(" ")
846 printUI(" height= Options:")
847 printUI("  . layer by layer: ")
848 printUI(" | / depth= start within: ")
849 printUI(' |/ "strip mine": ')
850 printUI(" +---- width=")
851 printUI("footer")
852 term.write("Toggle options or ENTER to start: ")
853
854 coloredTextAt(cPrgName, 2, 2, baseColor)
855 coloredTextAt("Options:", 18, 7, baseColor)
856 coloredTextAt("l", 20, 8, baseColor)
857 coloredTextAt("w", 26, 9, baseColor)
858 coloredTextAt("m", 27, 10, baseColor)
859 coloredTextAt(digDeep, 14, 9, baseColor)
860 coloredTextAt(digWide, 17, 11, baseColor)
861 coloredTextAt(digHeight, 11, 7, baseColor)
862
863 while blnLoop do
864 -- color toggles
865 if blnLayerByLayer then
866 coloredTextAt("on ", 36, 8, baseColor)
867 else
868 coloredTextAt("off", 36, 8, colors.white)
869 end
870 if blnStartWithin then
871 coloredTextAt("on ", 36, 9, baseColor)
872 else
873 coloredTextAt("off", 36, 9, colors.white)
874 end
875 if blnStripMine then
876 coloredTextAt("on ", 36, 10, baseColor)
877 else
878 coloredTextAt("off", 36, 10, colors.white)
879 end
880 -- get key
881 event, key, isHeld = os.pullEvent("key")
882 -- evaluate key
883 if key ~= nil then
884 if keys.getName(key) == "l" then
885 blnLayerByLayer = not blnLayerByLayer
886 elseif keys.getName(key) == "m" then
887 blnStripMine = not blnStripMine
888 elseif keys.getName(key) == "w" then
889 blnStartWithin = not blnStartWithin
890 elseif keys.getName(key) == "enter" then
891 blnLoop = false
892 end
893 end
894 end
895end
896
897
898---------------------------------------
899-- additional functions -
900---------------------------------------
901
902local blnDigUp = false
903local blnDigDown = false
904
905local function digUpDown()
906 if blnDigUp then dus() end
907 if blnDigDown then dds() end
908end
909
910local function gfPosDig(n)
911 if n == nil then n = 1 end
912 for i = 1, n, 1 do
913 gfPos()
914 digUpDown()
915 end
916end
917
918-- Try to find and pair with chunky turtle
919if hasModem then
920 print("Looking for chunky turtle...")
921 if findChunkyTurtle() then
922 print("Chunky turtle paired successfully!")
923 -- Wait for chunky turtle to confirm pairing
924 print("Waiting for chunky turtle confirmation...")
925 sleep(2)
926 -- Send initial position to chunky turtle
927 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
928 print("Chunky turtle positioned and ready!")
929 else
930 print("No chunky turtle found - continuing without chunk loading")
931 end
932else
933 print("No modem found - chunky turtle pairing disabled")
934end
935
936------------------------------------------------------------------------------
937-- main: pre-main options ----------------------------------------------------
938------------------------------------------------------------------------------
939
940local selfCall = ""
941local selfLayer = ""
942if blnStripMine then
943 -- let's missuse this beautiful program for strip mining
944
945 -- srtip mine step 1: main corridor
946 if blnLayerByLayer then selfLayer = " LayerByLayer" end
947 selfCall = "tClear " .. digDeep .. " -2 " .. digHeight .. selfLayer
948
949 if blnStripMine then selfCall = selfCall .. " within" end
950
951 --print(selfCall)
952 shell.run(selfCall)
953
954 -- strip mine step 2: mining shafts right side
955 -- step inside
956 if not blnStartWithin then gfPos() end
957 -- mine shafts
958 for i = 1, digDeep, 4 do
959 -- get on position
960 if i > 1 then
961 gfPos(4)
962 end
963 -- mining shafts
964 selfCall = "tClear 1 " .. (digWide + 1) .. " " .. digHeight .. " within" .. selfLayer
965 print(selfCall)
966 --sleep(3)
967 shell.run(selfCall)
968 end
969
970 -- strip mine step 3: mining shafts left side
971 -- get on position
972 gl()
973 gfPos()
974 gl()
975 -- mine shafts
976 for i = 1, digDeep, 4 do
977 -- get on position
978 if i > 1 then
979 gfPos(4)
980 end
981
982 -- mining shafts
983 selfCall = "tClear 1 " .. (digWide + 1) .. " " .. digHeight .. " within" .. selfLayer
984 print(selfCall)
985 --sleep(3)
986 shell.run(selfCall)
987 end
988
989 -- strip mine step 4: return
990 -- to entrance
991 gl()
992 gfPos()
993 gl()
994 -- step outside
995 if not blnStartWithin then gbPos() end
996
997 -- done
998 return
999end
1000
1001
1002------------------------------------------------------------------------------
1003-- main: program -------------------------------------------------------------
1004------------------------------------------------------------------------------
1005term.clear()
1006term.setCursorPos(1, 1)
1007
1008---- step 0: check fuel ----
1009print("step 0: checking fuel...")
1010-- fuel
1011-- estimate consumption
1012if not blnLayerByLayer then
1013 cMinFuel = math.floor(digHeight / 3)
1014else
1015 cMinFuel = math.floor(digHeight)
1016end
1017cMinFuel = cMinFuel * (math.abs(digDeep) + 1) * (math.abs(digWide) + 1) + (digHeight * 2)
1018cMinFuel = math.floor(cMinFuel * 1.1) + 20 -- extra
1019
1020refuelManager(cMinFuel, slotFuel, 2)
1021print("... done")
1022
1023
1024---- step 1: do deal with negative values ----
1025term.write("step 1: deal with negative values...")
1026
1027-- first dig block is 1,1,1
1028tPos[1] = 1
1029tPos[2] = 0
1030tPos[3] = 1
1031tPos[4] = 0 -- starting position
1032
1033
1034-- save inital values for end report
1035digWideOrg = digWide
1036digDeepOrg = digDeep
1037
1038--- check negative width
1039local blnNegativeWidth = false
1040if digWide < 0 then
1041 blnNegativeWidth = true
1042 digWide = digDeepOrg
1043 digDeep = -digWideOrg
1044end
1045
1046--- check negative height
1047local blnNegativeHeight = false
1048local remainingDigHeight = digHeight
1049if digHeight < 0 then
1050 blnNegativeHeight = true
1051 remainingDigHeight = -digHeight
1052end
1053print(" done")
1054
1055
1056---- step 2: enter and go up ----
1057term.write("step 2: enter and go up...")
1058-- step inside area
1059if not blnStartWithin then
1060 -- move into cuboid
1061 gfPosDig()
1062 -- Move chunky turtle to follow
1063 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1064else
1065 -- I'm already there
1066 tPos[2] = 1
1067 -- Move chunky turtle to follow
1068 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1069end
1070
1071-- positive or negative inital width?
1072if not blnNegativeWidth then
1073 grPos() -- turn to show progress
1074else
1075 -- orientation is already okay, due to negative inital width
1076end
1077print(" done")
1078
1079-- step 3: starting height
1080term.write("step 3: starting height...")
1081if not blnLayerByLayer then
1082 -- normal 3 layer dig mode
1083 if digHeight > 2 then
1084 -- get to right inital digging height
1085 guPos(digHeight - 2)
1086 -- Move chunky turtle to follow
1087 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1088 elseif digHeight < -1 then
1089 -- get to right inital negative digging height
1090 gdPos(1)
1091 -- Move chunky turtle to follow
1092 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1093 end
1094else
1095 -- layer by layer
1096 if digHeight > 1 then
1097 -- go to very top
1098 guPos(digHeight - 1)
1099 -- Move chunky turtle to follow
1100 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1101 else
1102 -- just stay where you are
1103 end
1104end
1105print(" done")
1106
1107
1108while remainingDigHeight > 0 do
1109 -- step 4: set dig up/down
1110 term.write("step 4: set dig up/down...")
1111 if not blnLayerByLayer then
1112 -- normal 3 layer dig mode
1113 if not blnNegativeHeight then
1114 -- positive dig height
1115 if tPos[3] > 1 then
1116 -- gone up to tripple dig
1117 blnDigUp = true
1118 blnDigDown = true
1119 elseif remainingDigHeight == 2 then
1120 blnDigUp = true
1121 blnDigDown = false
1122 else
1123 blnDigUp = false
1124 blnDigDown = false
1125 end
1126 else
1127 -- negative dig hight
1128 if tPos[3] >= digHeight + 3 then
1129 -- gone down to tripple dig
1130 blnDigUp = true
1131 blnDigDown = true
1132 elseif remainingDigHeight == 2 then
1133 blnDigUp = true --false
1134 blnDigDown = false --true
1135 else
1136 blnDigUp = false
1137 blnDigDown = false
1138 end
1139 end
1140 else
1141 -- layer by layer mode
1142 blnDigUp = false
1143 blnDigDown = false
1144 end
1145 print(" done")
1146
1147
1148 ---- step 5: digging one level ----
1149 term.write("step 5: digging one level...")
1150
1151 for iy = 1, digDeep, 1 do
1152 if iy == 1 then
1153 gfPosDig() -- step inside track
1154 -- Move chunky turtle to follow
1155 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1156 elseif iy % 2 == 0 then
1157 -- u-turn left
1158 glPos()
1159 gfPosDig()
1160 glPos()
1161 -- Move chunky turtle to follow
1162 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1163 else
1164 -- u-turn right
1165 grPos()
1166 gfPosDig()
1167 grPos()
1168 -- Move chunky turtle to follow
1169 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1170 end
1171 -- lane
1172 gfPosDig(digWide - 2)
1173 -- Move chunky turtle to follow
1174 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1175
1176 if iy == digDeep then
1177 -- return
1178 if iy % 2 == 1 then
1179 -- uneven! lets return
1180 glPos(2)
1181 gfPosDig(digWide - 2)
1182 end
1183
1184 -- dig lane y=1 back
1185 gfPosDig()
1186 glPos()
1187 gfPosDig(digDeep - 1)
1188 glPos()
1189 -- Move chunky turtle to follow
1190 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1191 end
1192 end
1193 print(" done")
1194
1195
1196 ---- step 6: change level ----
1197 term.write("step 6: change level...")
1198
1199 -- adjuste remainingDigHeight
1200 remainingDigHeight = remainingDigHeight - 1
1201 if blnDigUp then remainingDigHeight = remainingDigHeight - 1 end
1202 if blnDigDown then remainingDigHeight = remainingDigHeight - 1 end
1203
1204 -- adjust layer if there's s.th. left to dig
1205 if remainingDigHeight > 0 then
1206 if not blnLayerByLayer then
1207 -- normal 3 layer dig mode
1208 -- inital dig height pos or neg?
1209 if not blnNegativeHeight then
1210 -- inital dig height positive
1211 -- get to next dig level
1212 if remainingDigHeight >= 2 then
1213 gdPos(3)
1214 -- Move chunky turtle to follow
1215 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1216 else
1217 gdPos(tPos[3] - 1)
1218 -- Move chunky turtle to follow
1219 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1220 end
1221 else
1222 -- inital dig height negative
1223 -- get to next dig level
1224 if remainingDigHeight >= 2 then
1225 gdPos(3)
1226 -- Move chunky turtle to follow
1227 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1228 else
1229 gdPos(-digHeight + tPos[3] - 2)
1230 -- Move chunky turtle to follow
1231 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1232 end
1233 end
1234 else
1235 -- layer by layer mode
1236 gdPos(1) -- just the next one
1237 -- Move chunky turtle to follow
1238 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1239 end
1240 end
1241 print(" done")
1242end
1243
1244---- step 7: return to floor ----
1245term.write("step 7: return to floor...")
1246
1247-- return to floor
1248if not blnNegativeHeight then
1249 gdPos(tPos[3] - 1)
1250else
1251 guPos(-tPos[3] + 1)
1252end
1253-- Move chunky turtle to follow
1254moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1255
1256-- positive or negative inital width?
1257if not blnNegativeWidth then
1258 glPos() -- turn to leave
1259else
1260 -- orientation is already okay, due to negative inital width
1261end
1262
1263-- step out of area
1264if not blnStartWithin then
1265 -- move out of cuboid
1266 gbPos()
1267 -- Move chunky turtle to follow
1268 moveChunkyTurtle(tPos[1], tPos[2], tPos[3], tPos[4])
1269else
1270 -- I started there, so I'm already done
1271end
1272print(" done")
1273
1274
1275---- step 8: finishing stuff / report ----
1276-- Stop chunky turtle
1277if blnUseChunky then
1278 stopChunkyTurtle()
1279 print("Chunky turtle stopped")
1280end
1281
1282print("Done with tClear " .. digDeep .. " " .. digWide .. " " .. digHeight)
1283print("That looks much cleaner now! !")
1284ss(1)
1285--sleep(0.4)
1286printUI("header")
1287printUI("Check out YouTube for more videos")
1288printUI("and turtle programs by Kaikaku :)")
1289printUI("footer")
1290