tClear.lua

1012 lines · 29.2 KB

Open raw

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

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Kaikaku/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---------------------------------------
55---- Early UI functions ---------------
56---------------------------------------
57local function swapColors()
58local backColor=term.getBackgroundColor()
59local textColor=term.getTextColor()
60
61term.setBackgroundColor(textColor)
62term.setTextColor(backColor)
63end
64
65local function printUI(strInput)
66if strInput==ni then strInput="" end
67
68 if strInput=="header" then
69 term.write("+-------------------------------------") print("+")
70 elseif strInput=="line" then
71 term.write("+-------------------------------------") print("+")
72 elseif strInput=="footer" then
73 term.write("+-------------------------------------") print("+")
74 else
75 term.write("|")
76 strInput=strInput.." "
77 term.write(string.sub(strInput,1,37))
78 print("|")
79 end
80end
81
82local function coloredTextAt(inputText,outputRow,outputCol,textColor,backColor)
83-- writes and colors text to coordinates
84local oldRow, oldCol = term.getCursorPos()
85local oldTextColor=term.getTextColor()
86local oldBackColor=term.getBackgroundColor()
87if textColor==nil then textColor=term.getTextColor() end
88if backColor==nil then backColor=term.getBackgroundColor() end
89
90 term.setTextColor(textColor)
91 term.setBackgroundColor(backColor)
92 term.setCursorPos(outputRow,outputCol)
93 term.write(inputText)
94 term.setCursorPos(oldRow, oldCol)
95 term.setTextColor(oldTextColor)
96 term.setBackgroundColor(oldBackColor)
97end
98
99---------------------------------------
100---- tArgs ----------------------------
101---------------------------------------
102local tArgs = {...}
103term.clear() term.setCursorPos(1,1)
104
105local parameterShowSleep=1
106
107if #tArgs~=0 then
108 -- header
109 blnAskForParameters=false
110 term.clear() term.setCursorPos(1,1)
111 printUI("header")
112 printUI(""..cPrgName..", "..cVersion..", by Kaikaku. Enjoy!")
113 printUI("line")
114 coloredTextAt(cPrgName,2,2,baseColor)
115 print("Starting...")
116
117 if tArgs[1]~=nil then
118 digDeep=math.floor(tonumber(tArgs[1]))
119 if digDeep<=0 then
120 print("Parameter correction (depths must be >=1)!")
121 sleep(2)
122 parameterShowSleep=parameterShowSleep+2
123 digDeep=1
124 end
125 end
126 if tArgs[2]~=nil then
127 digWide=math.floor(tonumber(tArgs[2]))
128 if digWide==0 or digWide==1 or digWide==-1 then
129 print("Parameter correction (width not 0 or 1)!")
130 sleep(2)
131 parameterShowSleep=parameterShowSleep+2
132 digWide=2
133 end
134 end
135 if tArgs[3]~=nil then
136 digHeight=math.floor(tonumber(tArgs[3]))
137 if digHeight==0 then
138 print("Parameter correction (height not 0)!")
139 sleep(2)
140 parameterShowSleep=parameterShowSleep+2
141 digHeight=1
142 end
143 end
144
145 --check combinations of min values
146 if digDeep==1 and digWide<0 then
147 error("Parameter combination not allowed: depth=1 with width<0! Hint: increase depths or move turtle to use positive width.")
148 end
149
150 -- further parameters 4+
151 for i=4,#tArgs,1 do
152 if string.lower(tArgs[i])=="layerbylayer" or string.lower(tArgs[i])=="layer" or string.lower(tArgs[i])=="singlelayer" then
153 blnLayerByLayer=true
154 end
155 if string.lower(tArgs[i])=="startwithin" or string.lower(tArgs[i])=="within" or string.lower(tArgs[i])=="in" then
156 blnStartWithin=true
157 end
158 if string.lower(tArgs[i])=="stripmine" or string.lower(tArgs[i])=="strip" or string.lower(tArgs[i])=="mine" then
159 blnStripMine=true
160 end
161 end
162
163 -- show parameters
164 print("Clear depth = "..digDeep)
165 print("Clear width = "..digWide)
166 print("Clear height= "..digHeight)
167 term.write("Option LayerByLayert= ") if blnLayerByLayer then print("on") else print("off") end
168 term.write("Option StartWithin = ") if blnStartWithin then print("on") else print("off") end
169 term.write("Option StripMine = ") if blnStripMine then print("on") else print("off") end
170 sleep(parameterShowSleep)
171end
172
173if blnShowUsage then
174 term.clear() term.setCursorPos(1,1)
175 printUI("header")
176 printUI(""..cPrgName..", by Kaikaku")
177 printUI("footer")
178 print("Usage: ",cPrgName," depth, width [height ['LayerByLayer'] ['StartWithin'] ['StripMine']] ")
179 print()
180 print("If called with any parameter, then")
181 print(" there will be no info screen. Turtle")
182 print(" starts immediately.")
183 return
184end
185
186
187---------------------------------------
188-- basic functions for turtle control -
189---------------------------------------
190-- 2021-03-28 partly tPos (from tClear)
191-- 2021-03-20 arrangeArray, printArray (from tLoader)
192-- 2021-03-13 askForNumber, pressKeyNoSpecials, checkInventory(u), getFirstEmptySlot, identifyTool, findModem, ensureModem
193-- 2021-03-05 gfr -
194-- 2021-02-06 select+place -
195-- 2021-01-29 save turtle go func -
196-- checkInventory w/ exit -
197-- refuelManager -
198---------------------------------------
199local function gf(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.forward() do end end end
200local function gb(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.back() do end end end
201local function gu(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.up() do end end end
202local function gd(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.down() do end end end
203local function gl(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.turnLeft() do end end end
204local function gr(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.turnRight() do end end end
205
206local function df() turtle.dig() end
207local function du() turtle.digUp() end
208local function dd() turtle.digDown() end
209
210local function dfs() while turtle.dig() do end end
211local function dus() while turtle.digUp() do end end
212local function dds() while turtle.digDown() do end end
213
214local function pf() return turtle.place() end
215local function pu() return turtle.placeUp() end
216local function pd() return turtle.placeDown() end
217
218local function sf() turtle.suck() end
219local function su() turtle.suckUp() end
220local function sd() turtle.suckDown() end
221local function Df() turtle.drop() end
222local function Du(n) turtle.dropUp(n) end
223local function Dd() turtle.dropDown() end
224local function ss(s) turtle.select(s) end
225local function gic(s) return turtle.getItemCount(s) end
226
227local function gfs(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.forward() do df() end end end
228local function gbs(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.back() do gl() gl() df() gr() gr() end end end
229local function gus(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.up() do du() end end end
230local function gds(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.down() do dd() end end end
231local function pfs() df() turtle.place() end
232local function pus() du() turtle.placeUp() end
233local function pds() dd() turtle.placeDown() end
234
235local function gfr() return turtle.forward() end
236local function gbr() return turtle.back() end
237local function gur() return turtle.up() end
238local function gdr() return turtle.down() end
239
240local tPos={}
241
242local function glPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.turnLeft() do end end end --tPos[4]=(tPos[4]-1)%4 end end
243local function grPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.turnRight() do end end end --tPos[4]=(tPos[4]+1)%4 end end
244local function gfPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.forward() do df() end end end --pCF(1) end end
245local function gbPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.back() do gl() gl() df() gr() gr() end end end--pCF(-1) end end
246local function guPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.up() do du() end tPos[3]=tPos[3]+1 end end
247local function gdPos(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.down() do dd() end tPos[3]=tPos[3]-1 end end
248
249
250local function spd(s,blnForward)
251 if s==nil then s=turtle.currentSlot() end
252 if blnForward==nil then blnForward=true end
253 ss(s) pd() if blnForward then gf() end
254end
255local function spu(s,blnForward)
256 if s==nil then s=turtle.currentSlot() end
257 if blnForward==nil then blnForward=true end
258 ss(s) pu() if blnForward then gf() end
259end
260local function spf(s,blnBack)
261 if s==nil then s=turtle.currentSlot() end
262 if blnBack==nil then blnBack=true end
263 ss(s) pf() if blnBack then gb() end
264end
265
266local function waitKey(strText)
267 local event, scancode
268 write(strText)
269 event, scancode = os.pullEvent("key")
270 print()
271end
272
273local function askForInputText(textt)
274 local at=""
275 -- check prompting texts
276 if textt==nil then textt="Enter text:" end
277
278 -- ask for input
279 write(textt)
280 at=read()
281 return at
282end
283
284local function askForNumber(askText, minValue, maxValue)
285-- gets entered data, ensures it's a number and returns it
286-- keeps asking if entry is not a number
287-- adapts to min and max values
288-- allways writes in screen line 13 (last for turtles)
289-- calls askForInputText
290local blnReask=true
291local returnNumber=nil
292if minValue==nil then minValur=1 end
293if maxValue==nil then maxValue=100 end
294if askText==nil then askText="Key in number and press Enter: " end
295 while blnReask do
296 term.setCursorPos(1,13)
297 returnNumber=askForInputText(askText)
298 if returnNumber==nil then
299 blnReask=true
300 else
301 returnNumber=tonumber(returnNumber)
302 if returnNumber==nil then
303 blnReask=true
304 else
305 returnNumber=math.floor(returnNumber)
306 if returnNumber>maxValue then returnNumber=maxValue end
307 if returnNumber<minValue then returnNumber=minValue end
308 blnReask=false
309 end
310 end
311 end
312 return returnNumber
313end
314
315local function pressKeyNoSpecials(askText)
316-- excludes ctrl / alt / shifts
317-- catches windows
318-- retruns the key number (if needed at all)
319local tmpEvent, tmpKey
320if askText==nil then askText="Press key to START! (stop w/ ctrl+t) " end
321 tmpKey=341
322 while tmpKey>=340 and tmpKey<=346 do -- ctrls, alts, shifts
323 term.write(askText) tmpEvent, tmpKey = os.pullEvent("key")
324 if tmpKey==nil then tmpKey=341 end -- win
325 end
326 return tmpKey
327end
328
329local function checkFuel()
330 local tmp=turtle.getFuelLevel()
331 return tmp
332end
333
334local function checkTurtle(blnOnlyIdentify)
335if blnOnlyIdentify==nil then blnOnlyIdentify=false end
336-- turtle?
337 local turtleOk, turtleVal = pcall(checkFuel)
338 if not turtleOk then
339 blnTurtle=false
340 if not blnOnlyIdentify then
341 term.clear() term.setCursorPos(1,1)
342 printUI("header")
343 printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
344 printUI("line")
345 printUI("This is a turtle program.")
346 printUI(" Please, execute it with a turtle!")
347 printUI("footer")
348
349 coloredTextAt(cPrgName,2,2,baseColor)
350 error()
351 end
352 else
353 blnTurtle=true
354 end
355end
356
357local function sleepDots(sec, duration)
358if sec==nil then sec=10 end
359if sec<1 then return end
360if duration==nil then duration=1 end -- shorten durtation for more dots
361
362 for i=1,sec-1 do
363 sleep(1*duration)
364 term.write(".")
365 end
366
367 sleep(1)
368 print(".")
369end
370
371local function checkInventory(s, nMin, nMax, textt, blnExitOnFalse, blnRepeat)
372-- checks if in slot s are not less than nMin and not more than nMax items
373-- returns true if okay
374-- if not displays textt
375-- blnExitOnFalse=true raises an error
376-- blnRepeat=true repeatedly sks to put in the right number of items (overrides blnExitOnFalse)
377local oldSlot = turtle.getSelectedSlot()
378if s==nil then s=turtle.getSelectedSlot() end
379if nMin==nil then nMin=0 end
380if nMax==nil then nMax=64 end
381if blnExitOnFalse==nil then blnExitOnFalse=false end
382if blnRepeat==nil then blnRepeat=false end
383if blnRepeat~=true then blnRepeat=false end
384
385 while true do
386
387 if turtle.getItemCount(s)<nMin or turtle.getItemCount(s)>nMax then
388 print(textt)
389 if not blnRepeat then
390 -- single check ends with this
391 if blnExitOnFalse then
392 error()
393 else
394 ss(oldSlot) return false
395 end
396 end
397 -- repeated check
398 ss(s)
399 sleepDots(3)
400 else
401 -- everything is fine
402 ss(oldSlot) return true
403 end
404 end
405end
406
407local function refuelFromSlot(s, n) -- slot, amount to consume
408if s==nil then s=16 end
409if n==nil then n=64 end
410local currentSlot = turtle.getSelectedSlot()
411local fuelItems = turtle.getItemCount(s)
412local returnValue = false
413
414 if fuelItems>0 then
415 ss(s)
416 returnValue=turtle.refuel(n)
417 ss(currentSlot)
418 end
419 return returnValue
420end
421
422local function refuelManager(setMinFuel,setSlotFuel,waitTime)
423 local currentSlotSelected=turtle.getSelectedSlot()
424 ss(setSlotFuel)
425 while turtle.getFuelLevel()<setMinFuel do
426 print("Need more fuel ("..turtle.getFuelLevel().."/"..setMinFuel..").")
427 if not refuelFromSlot(setSlotFuel) then
428 -- unsucessfull try
429 print(" Please, put fuel items in slot "..setSlotFuel.."!")
430 term.write(" Sleeping "..waitTime.." seconds")
431 sleepDots(waitTime)
432 else
433 print("Refueled...")
434 end
435 end
436 ss(currentSlotSelected)
437end
438
439local function debugPrint(str)
440 if blnDebugPrint then print(str) end
441end
442
443local function inspectFor(blockArray, strDirection) -- <<-- from TreeFarm
444if strDirection==nil then strDirection="f" end
445local blnOk, data
446
447 -- inspect
448 if strDirection=="d" then blnOk, data = turtle.inspectDown()
449 elseif strDirection=="u" then blnOk, data = turtle.inspectUp()
450 elseif strDirection=="f" then blnOk, data = turtle.inspect()
451 else
452 print("Warning: Unknown direction '",strDirection,"' in inspectFor, taking (f)orward instead.")
453 strDirection="f"
454 blnOk, data = turtle.inspect()
455 end
456 if data.name~=nil then debugPrint("Found:"..string.lower(data.name)) end
457 -- compare
458 local i=1
459 while blockArray[i]~=nil do
460 debugPrint("Compare to:"..string.lower(blockArray[i]))
461 if data.name~=nil then
462 if string.lower(data.name) == string.lower(blockArray[i]) then return true end
463 end
464 i=i+1
465 end
466
467 return false -- reached a nil value
468end
469
470
471local function getFirstEmptySlot(startAt)
472if startAt==nil then startAt=1 end
473if startAt>16 or startAt<1 then return nil end
474
475 for i=startAt,16,1 do
476 if gic(i)==0 then return i end
477 end
478 for i=1,startAt,1 do
479 if gic(i)==0 then return i end
480 end
481 return nil
482end
483
484local function identifyTool(toolSide)
485-- returns name of tool at side toolSide
486-- returns no_tool if there is none
487-- requires at least one empty slot for tool check (throws error)
488if toolSide==nil then toolSide="right" end
489if toolSide~="right" and toolSide~="r" then toolSide="left" end
490local slotNumber = getFirstEmptySlot()
491local slotSelected=turtle.getSelectedSlot()
492local toolName="no_tool"
493
494 if slotNumber==nil then error("Couldn't find empty slot to check tool on side '"..toolSide.."'!") end
495 ss(slotNumber)
496 -- step 1: get name
497 if toolSide=="right" or toolSide=="r" then
498 turtle.equipRight()
499 else
500 turtle.equipLeft()
501 end
502 -- step 2: get name
503 local data = turtle.getItemDetail()
504 if data~=nil then toolName=data.name end
505 -- step 3: re-equipget name
506 if toolSide=="right" or toolSide=="r" then
507 turtle.equipRight()
508 else
509 turtle.equipLeft()
510 end
511 ss(slotSelected)
512 return toolName
513end
514
515
516local function findModem() -- <<-- delete if no modem used
517 for _, p in pairs( rs.getSides() ) do
518 if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then return true,p end
519 end
520 return false,nil
521end
522
523
524------------------------------------------------------------------------------
525-- main: description ---------------------------------------------------------
526------------------------------------------------------------------------------
527checkTurtle()
528
529if blnAskForParameters then
530term.clear() term.setCursorPos(1,1)
531local iPage=0
532local iPageMax=5
533local event, key, isHeld
534local blnLoop=true
535
536term.clear() term.setCursorPos(1,1) iPage=iPage+1
537printUI("header")
538printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
539printUI("line")
540-- 1234567890123456789012345678901234567
541printUI("Program features:")
542printUI("* Quick: mines 3 layers in 1 go")
543printUI("* Precise: mines and moves only ")
544printUI(" within the specified area")
545printUI("* Versatile: place turtle at any")
546printUI(" corner or within corner to start")
547printUI("* Lava sparing: with layer by layer")
548printUI("* Stripmine: may be misused for this")
549printUI("footer")
550
551coloredTextAt(cPrgName,2,2,baseColor)
552coloredTextAt("Quick",4,5,baseColor)
553coloredTextAt("Precise",4,6,baseColor)
554coloredTextAt("Versatile",4,8,baseColor)
555coloredTextAt("Lava sparing",4,10,baseColor)
556coloredTextAt("Stripmine",4,11,baseColor)
557
558pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t) ")
559---
560
561term.clear() term.setCursorPos(1,1) iPage=iPage+1
562printUI("header")
563printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
564printUI("line")
565-- 1234567890123456789012345678901234567
566printUI("Program mines all blocks of a")
567printUI(" d * w * h cuboid. ")
568printUI(" ")
569printUI(" height="..digHeight)
570printUI("  . ")
571printUI(" | / depth="..digDeep)
572printUI(" |/ ")
573printUI(" +---- width="..digWide)
574printUI("footer")
575term.write("Key in depth and press Enter: ")
576
577coloredTextAt(cPrgName,2,2,baseColor)
578coloredTextAt("<-- enter (d>=1)",18,9,baseColor)
579event, key, isHeld = os.pullEvent("key")
580digDeep=askForNumber("Key in depth and press Enter: ",1,999)
581---
582
583term.clear() term.setCursorPos(1,1) iPage=iPage+1
584printUI("header")
585printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
586printUI("line")
587-- 1234567890123456789012345678901234567
588printUI("Program mines all blocks of a")
589printUI(" d * w * h cuboid. ")
590printUI(" ")
591printUI(" height="..digHeight)
592printUI("  . ")
593printUI(" | / depth=")
594printUI(" |/ ")
595printUI(" +---- width="..digWide)
596printUI("footer")
597term.write("Key in width and press Enter: ")
598
599coloredTextAt(cPrgName,2,2,baseColor)
600coloredTextAt( "(w>=2 or",31,10,baseColor)
601coloredTextAt("<-- enter w<=-2)",21,11,baseColor)
602coloredTextAt(digDeep,14,9,baseColor)
603event, key, isHeld = os.pullEvent("key")
604digWide=askForNumber("Key in width and press Enter: ",-999,999)
605if digWide==-1 or digWide==0 or digWide==1 then digWide=2 end
606---
607
608term.clear() term.setCursorPos(1,1) iPage=iPage+1
609printUI("header")
610printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
611printUI("line")
612-- 1234567890123456789012345678901234567
613printUI("Program mines all blocks of a")
614printUI(" d * w * h cuboid. ")
615printUI(" ")
616printUI(" height="..digHeight)
617printUI("  . ")
618printUI(" | / depth=")
619printUI(" |/ ")
620printUI(" +---- width=")
621printUI("footer")
622term.write("Key in height and press Enter: ")
623
624coloredTextAt(cPrgName,2,2,baseColor)
625coloredTextAt("<-- enter (h<>0)",15,7,baseColor)
626coloredTextAt(digDeep,14,9,baseColor)
627coloredTextAt(digWide,17,11,baseColor)
628event, key, isHeld = os.pullEvent("key")
629digHeight=askForNumber("Key in height and press Enter: ",-999,999)
630if digHeight==0 then digHeight=1 end
631---
632
633term.clear() term.setCursorPos(1,1) iPage=iPage+1
634printUI("header")
635printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")")
636printUI("line")
637-- 1234567890123456789012345678901234567
638printUI("Program mines all blocks of a")
639printUI(" d * w * h cuboid. ")
640printUI(" ")
641printUI(" height= Options:")
642printUI("  . layer by layer: ")
643printUI(" | / depth= start within: ")
644printUI(' |/ "strip mine": ')
645printUI(" +---- width=")
646printUI("footer")
647term.write("Toggle options or ENTER to start: ")
648
649coloredTextAt(cPrgName,2,2,baseColor)
650coloredTextAt("Options:",18,7,baseColor)
651coloredTextAt("l",20,8,baseColor)
652coloredTextAt("w",26,9,baseColor)
653coloredTextAt("m",27,10,baseColor)
654coloredTextAt(digDeep,14,9,baseColor)
655coloredTextAt(digWide,17,11,baseColor)
656coloredTextAt(digHeight,11,7,baseColor)
657
658while blnLoop do
659 -- color toggles
660 if blnLayerByLayer then
661 coloredTextAt("on ",36,8,baseColor)
662 else
663 coloredTextAt("off",36,8,colors.white)
664 end
665 if blnStartWithin then
666 coloredTextAt("on ",36,9,baseColor)
667 else
668 coloredTextAt("off",36,9,colors.white)
669 end
670 if blnStripMine then
671 coloredTextAt("on ",36,10,baseColor)
672 else
673 coloredTextAt("off",36,10,colors.white)
674 end
675 -- get key
676 event, key, isHeld = os.pullEvent("key")
677 -- evaluate key
678 if key~=nil then
679 if keys.getName(key)=="l" then
680 blnLayerByLayer= not blnLayerByLayer
681 elseif keys.getName(key)=="m" then
682 blnStripMine= not blnStripMine
683 elseif keys.getName(key)=="w" then
684 blnStartWithin = not blnStartWithin
685 elseif keys.getName(key)=="enter" then
686 blnLoop=false
687 end
688 end
689end
690
691end
692
693
694---------------------------------------
695-- additional functions -
696---------------------------------------
697
698local blnDigUp=false
699local blnDigDown=false
700
701local function digUpDown()
702 if blnDigUp then dus() end
703 if blnDigDown then dds() end
704end
705
706local function gfPosDig(n)
707if n==nil then n=1 end
708 for i=1,n,1 do
709 gfPos() digUpDown()
710 end
711end
712
713
714------------------------------------------------------------------------------
715-- main: pre-main options ----------------------------------------------------
716------------------------------------------------------------------------------
717
718local selfCall=""
719local selfLayer=""
720if blnStripMine then
721 -- let's missuse this beautiful program for strip mining
722
723 -- srtip mine step 1: main corridor
724 if blnLayerByLayer then selfLayer=" LayerByLayer" end
725 selfCall="tClear "..digDeep.." -2 "..digHeight..selfLayer
726
727 if blnStripMine then selfCall=selfCall.." within" end
728
729 --print(selfCall)
730 shell.run(selfCall)
731
732 -- strip mine step 2: mining shafts right side
733 -- step inside
734 if not blnStartWithin then gfPos() end
735 -- mine shafts
736 for i=1,digDeep,4 do
737 -- get on position
738 if i>1 then
739 gfPos(4)
740 end
741 -- mining shafts
742 selfCall="tClear 1 "..(digWide+1).." "..digHeight.." within"..selfLayer
743 print(selfCall)
744 --sleep(3)
745 shell.run(selfCall)
746 end
747
748 -- strip mine step 3: mining shafts left side
749 -- get on position
750 gl() gfPos() gl()
751 -- mine shafts
752 for i=1,digDeep,4 do
753 -- get on position
754 if i>1 then
755 gfPos(4)
756 end
757
758 -- mining shafts
759 selfCall="tClear 1 "..(digWide+1).." "..digHeight.." within"..selfLayer
760 print(selfCall)
761 --sleep(3)
762 shell.run(selfCall)
763 end
764
765 -- strip mine step 4: return
766 -- to entrance
767 gl() gfPos() gl()
768 -- step outside
769 if not blnStartWithin then gbPos() end
770
771 -- done
772 return
773end
774
775
776------------------------------------------------------------------------------
777-- main: program -------------------------------------------------------------
778------------------------------------------------------------------------------
779term.clear() term.setCursorPos(1,1)
780
781---- step 0: check fuel ----
782print("step 0: checking fuel...")
783-- fuel
784-- estimate consumption
785if not blnLayerByLayer then
786 cMinFuel=math.floor(digHeight/3)
787else
788 cMinFuel=math.floor(digHeight)
789end
790cMinFuel=cMinFuel*(math.abs(digDeep)+1)*(math.abs(digWide)+1)+(digHeight*2)
791cMinFuel=math.floor(cMinFuel*1.1) + 20 -- extra
792
793refuelManager(cMinFuel,slotFuel,10)
794print("... done")
795
796
797---- step 1: do deal with negative values ----
798term.write("step 1: deal with negative values...")
799
800-- first dig block is 1,1,1
801tPos[1]=1 tPos[2]=0 tPos[3]=1 tPos[4]=0 -- starting position
802
803
804-- save inital values for end report
805digWideOrg=digWide
806digDeepOrg=digDeep
807
808--- check negative width
809local blnNegativeWidth=false
810if digWide<0 then
811 blnNegativeWidth=true
812 digWide=digDeepOrg
813 digDeep=-digWideOrg
814end
815
816--- check negative height
817local blnNegativeHeight=false
818local remainingDigHeight=digHeight
819if digHeight<0 then
820 blnNegativeHeight=true
821 remainingDigHeight=-digHeight
822end
823print(" done")
824
825
826---- step 2: enter and go up ----
827term.write("step 2: enter and go up...")
828-- step inside area
829if not blnStartWithin then
830 -- move into cuboid
831 gfPosDig()
832else
833 -- I'm already there
834 tPos[2]=1
835end
836
837-- positive or negative inital width?
838if not blnNegativeWidth then
839 grPos() -- turn to show progress
840else
841 -- orientation is already okay, due to negative inital width
842end
843print(" done")
844
845-- step 3: starting height
846term.write("step 3: starting height...")
847if not blnLayerByLayer then
848 -- normal 3 layer dig mode
849 if digHeight>2 then
850 -- get to right inital digging height
851 guPos(digHeight-2)
852 elseif digHeight<-1 then
853 -- get to right inital negative digging height
854 gdPos(1)
855 end
856else
857 -- layer by layer
858 if digHeight>1 then
859 -- go to very top
860 guPos(digHeight-1)
861 else
862 -- just stay where you are
863 end
864end
865print(" done")
866
867
868while remainingDigHeight>0 do
869
870 -- step 4: set dig up/down
871 term.write("step 4: set dig up/down...")
872 if not blnLayerByLayer then
873 -- normal 3 layer dig mode
874 if not blnNegativeHeight then
875 -- positive dig height
876 if tPos[3]>1 then
877 -- gone up to tripple dig
878 blnDigUp=true
879 blnDigDown=true
880 elseif remainingDigHeight==2 then
881 blnDigUp=true
882 blnDigDown=false
883 else
884 blnDigUp=false
885 blnDigDown=false
886 end
887 else
888 -- negative dig hight
889 if tPos[3]>=digHeight+3 then
890 -- gone down to tripple dig
891 blnDigUp=true
892 blnDigDown=true
893 elseif remainingDigHeight==2 then
894 blnDigUp=true--false
895 blnDigDown=false--true
896 else
897 blnDigUp=false
898 blnDigDown=false
899 end
900 end
901 else
902 -- layer by layer mode
903 blnDigUp=false
904 blnDigDown=false
905 end
906 print(" done")
907
908
909 ---- step 5: digging one level ----
910 term.write("step 5: digging one level...")
911
912 for iy=1,digDeep,1 do
913
914 if iy==1 then
915 gfPosDig() -- step inside track
916 elseif iy%2==0 then
917 -- u-turn left
918 glPos() gfPosDig() glPos()
919 else
920 -- u-turn right
921 grPos() gfPosDig() grPos()
922 end
923 -- lane
924 gfPosDig(digWide-2)
925
926 if iy==digDeep then
927 -- return
928 if iy%2==1 then
929 -- uneven! lets return
930 glPos(2) gfPosDig(digWide-2)
931 end
932
933 -- dig lane y=1 back
934 gfPosDig() glPos() gfPosDig(digDeep-1) glPos()
935 end
936 end
937 print(" done")
938
939
940 ---- step 6: change level ----
941 term.write("step 6: change level...")
942
943 -- adjuste remainingDigHeight
944 remainingDigHeight=remainingDigHeight-1
945 if blnDigUp then remainingDigHeight=remainingDigHeight-1 end
946 if blnDigDown then remainingDigHeight=remainingDigHeight-1 end
947
948 -- adjust layer if there's s.th. left to dig
949 if remainingDigHeight>0 then
950 if not blnLayerByLayer then
951 -- normal 3 layer dig mode
952 -- inital dig height pos or neg?
953 if not blnNegativeHeight then
954 -- inital dig height positive
955 -- get to next dig level
956 if remainingDigHeight>=2 then
957 gdPos(3)
958 else
959 gdPos(tPos[3]-1)
960 end
961 else
962 -- inital dig height negative
963 -- get to next dig level
964 if remainingDigHeight>=2 then
965 gdPos(3)
966 else
967 gdPos(-digHeight+tPos[3]-2)
968 end
969 end
970 else
971 -- layer by layer mode
972 gdPos(1) -- just the next one
973 end
974 end
975 print(" done")
976end
977
978---- step 7: return to floor ----
979term.write("step 7: return to floor...")
980
981-- return to floor
982if not blnNegativeHeight then
983 gdPos(tPos[3]-1)
984else
985 guPos(-tPos[3]+1)
986end
987
988-- positive or negative inital width?
989if not blnNegativeWidth then
990 glPos() -- turn to leave
991else
992 -- orientation is already okay, due to negative inital width
993end
994
995-- step out of area
996if not blnStartWithin then
997 -- move out of cuboid
998 gbPos()
999else
1000 -- I started there, so I'm already done
1001end
1002print(" done")
1003
1004
1005---- step 8: finishing stuff / report ----
1006print("Done with tClear "..digDeep.." "..digWide.." "..digHeight)
1007print("That looks much cleaner now! !") ss(1)
1008--sleep(0.4)
1009printUI("header")
1010printUI("Check out YouTube for more videos")
1011printUI("and turtle programs by Kaikaku :)")
1012printUI("footer")