tClear.lua
1012 lines · 29.2 KB
{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 | --------------------------------------- |
| 29 | local cVersion ="v1.10" |
| 30 | local cPrgName ="tClear" |
| 31 | local cMinFuel =110 |
| 32 | local blnAskForParameters =true |
| 33 | local blnShowUsage =false |
| 34 | local blnDebugPrint =false--true |
| 35 | local blnTurtle |
| 36 | local isComputer =false |
| 37 | local baseColor=colors.blue |
| 38 | |
| 39 | |
| 40 | --------------------------------------- |
| 41 | ---- VARIABLES: specific -------------- |
| 42 | --------------------------------------- |
| 43 | local slotFuel=16 |
| 44 | |
| 45 | local digDeep=3 -- all but 0 are okay |
| 46 | local digWide=3 -- all but -1,0,1 -- if wide <=-1 then depth needs to be >=2 |
| 47 | local digHeight=1 -- all but 0 |
| 48 | local digWideOrg |
| 49 | local digDeepOrg |
| 50 | local blnLayerByLayer=false |
| 51 | local blnStartWithin =false |
| 52 | local blnStripMine =false |
| 53 | |
| 54 | --------------------------------------- |
| 55 | ---- Early UI functions --------------- |
| 56 | --------------------------------------- |
| 57 | local function swapColors() |
| 58 | local backColor=term.getBackgroundColor() |
| 59 | local textColor=term.getTextColor() |
| 60 | |
| 61 | term.setBackgroundColor(textColor) |
| 62 | term.setTextColor(backColor) |
| 63 | end |
| 64 | |
| 65 | local function printUI(strInput) |
| 66 | if 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 |
| 80 | end |
| 81 | |
| 82 | local function coloredTextAt(inputText,outputRow,outputCol,textColor,backColor) |
| 83 | -- writes and colors text to coordinates |
| 84 | local oldRow, oldCol = term.getCursorPos() |
| 85 | local oldTextColor=term.getTextColor() |
| 86 | local oldBackColor=term.getBackgroundColor() |
| 87 | if textColor==nil then textColor=term.getTextColor() end |
| 88 | if 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) |
| 97 | end |
| 98 | |
| 99 | --------------------------------------- |
| 100 | ---- tArgs ---------------------------- |
| 101 | --------------------------------------- |
| 102 | local tArgs = {...} |
| 103 | term.clear() term.setCursorPos(1,1) |
| 104 | |
| 105 | local parameterShowSleep=1 |
| 106 | |
| 107 | if #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) |
| 171 | end |
| 172 | |
| 173 | if 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 |
| 184 | end |
| 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 | --------------------------------------- |
| 199 | local function gf(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.forward() do end end end |
| 200 | local function gb(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.back() do end end end |
| 201 | local function gu(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.up() do end end end |
| 202 | local function gd(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.down() do end end end |
| 203 | local function gl(n) if n==nil then n=1 end for i=1,n,1 do while not turtle.turnLeft() do end end end |
| 204 | local 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 | |
| 206 | local function df() turtle.dig() end |
| 207 | local function du() turtle.digUp() end |
| 208 | local function dd() turtle.digDown() end |
| 209 | |
| 210 | local function dfs() while turtle.dig() do end end |
| 211 | local function dus() while turtle.digUp() do end end |
| 212 | local function dds() while turtle.digDown() do end end |
| 213 | |
| 214 | local function pf() return turtle.place() end |
| 215 | local function pu() return turtle.placeUp() end |
| 216 | local function pd() return turtle.placeDown() end |
| 217 | |
| 218 | local function sf() turtle.suck() end |
| 219 | local function su() turtle.suckUp() end |
| 220 | local function sd() turtle.suckDown() end |
| 221 | local function Df() turtle.drop() end |
| 222 | local function Du(n) turtle.dropUp(n) end |
| 223 | local function Dd() turtle.dropDown() end |
| 224 | local function ss(s) turtle.select(s) end |
| 225 | local function gic(s) return turtle.getItemCount(s) end |
| 226 | |
| 227 | local 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 |
| 228 | local 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 |
| 229 | local 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 |
| 230 | local 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 |
| 231 | local function pfs() df() turtle.place() end |
| 232 | local function pus() du() turtle.placeUp() end |
| 233 | local function pds() dd() turtle.placeDown() end |
| 234 | |
| 235 | local function gfr() return turtle.forward() end |
| 236 | local function gbr() return turtle.back() end |
| 237 | local function gur() return turtle.up() end |
| 238 | local function gdr() return turtle.down() end |
| 239 | |
| 240 | local tPos={} |
| 241 | |
| 242 | local 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 |
| 243 | local 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 |
| 244 | local 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 |
| 245 | local 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 |
| 246 | local 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 |
| 247 | local 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 | |
| 250 | local 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 |
| 254 | end |
| 255 | local 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 |
| 259 | end |
| 260 | local 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 |
| 264 | end |
| 265 | |
| 266 | local function waitKey(strText) |
| 267 | local event, scancode |
| 268 | write(strText) |
| 269 | event, scancode = os.pullEvent("key") |
| 270 | print() |
| 271 | end |
| 272 | |
| 273 | local 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 |
| 282 | end |
| 283 | |
| 284 | local 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 |
| 290 | local blnReask=true |
| 291 | local returnNumber=nil |
| 292 | if minValue==nil then minValur=1 end |
| 293 | if maxValue==nil then maxValue=100 end |
| 294 | if 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 |
| 313 | end |
| 314 | |
| 315 | local function pressKeyNoSpecials(askText) |
| 316 | -- excludes ctrl / alt / shifts |
| 317 | -- catches windows |
| 318 | -- retruns the key number (if needed at all) |
| 319 | local tmpEvent, tmpKey |
| 320 | if 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 |
| 327 | end |
| 328 | |
| 329 | local function checkFuel() |
| 330 | local tmp=turtle.getFuelLevel() |
| 331 | return tmp |
| 332 | end |
| 333 | |
| 334 | local function checkTurtle(blnOnlyIdentify) |
| 335 | if 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 |
| 355 | end |
| 356 | |
| 357 | local function sleepDots(sec, duration) |
| 358 | if sec==nil then sec=10 end |
| 359 | if sec<1 then return end |
| 360 | if 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(".") |
| 369 | end |
| 370 | |
| 371 | local 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) |
| 377 | local oldSlot = turtle.getSelectedSlot() |
| 378 | if s==nil then s=turtle.getSelectedSlot() end |
| 379 | if nMin==nil then nMin=0 end |
| 380 | if nMax==nil then nMax=64 end |
| 381 | if blnExitOnFalse==nil then blnExitOnFalse=false end |
| 382 | if blnRepeat==nil then blnRepeat=false end |
| 383 | if 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 |
| 405 | end |
| 406 | |
| 407 | local function refuelFromSlot(s, n) -- slot, amount to consume |
| 408 | if s==nil then s=16 end |
| 409 | if n==nil then n=64 end |
| 410 | local currentSlot = turtle.getSelectedSlot() |
| 411 | local fuelItems = turtle.getItemCount(s) |
| 412 | local returnValue = false |
| 413 | |
| 414 | if fuelItems>0 then |
| 415 | ss(s) |
| 416 | returnValue=turtle.refuel(n) |
| 417 | ss(currentSlot) |
| 418 | end |
| 419 | return returnValue |
| 420 | end |
| 421 | |
| 422 | local 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) |
| 437 | end |
| 438 | |
| 439 | local function debugPrint(str) |
| 440 | if blnDebugPrint then print(str) end |
| 441 | end |
| 442 | |
| 443 | local function inspectFor(blockArray, strDirection) -- <<-- from TreeFarm |
| 444 | if strDirection==nil then strDirection="f" end |
| 445 | local 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 |
| 468 | end |
| 469 | |
| 470 | |
| 471 | local function getFirstEmptySlot(startAt) |
| 472 | if startAt==nil then startAt=1 end |
| 473 | if 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 |
| 482 | end |
| 483 | |
| 484 | local 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) |
| 488 | if toolSide==nil then toolSide="right" end |
| 489 | if toolSide~="right" and toolSide~="r" then toolSide="left" end |
| 490 | local slotNumber = getFirstEmptySlot() |
| 491 | local slotSelected=turtle.getSelectedSlot() |
| 492 | local 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 |
| 513 | end |
| 514 | |
| 515 | |
| 516 | local 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 |
| 521 | end |
| 522 | |
| 523 | |
| 524 | ------------------------------------------------------------------------------ |
| 525 | -- main: description --------------------------------------------------------- |
| 526 | ------------------------------------------------------------------------------ |
| 527 | checkTurtle() |
| 528 | |
| 529 | if blnAskForParameters then |
| 530 | term.clear() term.setCursorPos(1,1) |
| 531 | local iPage=0 |
| 532 | local iPageMax=5 |
| 533 | local event, key, isHeld |
| 534 | local blnLoop=true |
| 535 | |
| 536 | term.clear() term.setCursorPos(1,1) iPage=iPage+1 |
| 537 | printUI("header") |
| 538 | printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")") |
| 539 | printUI("line") |
| 540 | -- 1234567890123456789012345678901234567 |
| 541 | printUI("Program features:") |
| 542 | printUI("* Quick: mines 3 layers in 1 go") |
| 543 | printUI("* Precise: mines and moves only ") |
| 544 | printUI(" within the specified area") |
| 545 | printUI("* Versatile: place turtle at any") |
| 546 | printUI(" corner or within corner to start") |
| 547 | printUI("* Lava sparing: with layer by layer") |
| 548 | printUI("* Stripmine: may be misused for this") |
| 549 | printUI("footer") |
| 550 | |
| 551 | coloredTextAt(cPrgName,2,2,baseColor) |
| 552 | coloredTextAt("Quick",4,5,baseColor) |
| 553 | coloredTextAt("Precise",4,6,baseColor) |
| 554 | coloredTextAt("Versatile",4,8,baseColor) |
| 555 | coloredTextAt("Lava sparing",4,10,baseColor) |
| 556 | coloredTextAt("Stripmine",4,11,baseColor) |
| 557 | |
| 558 | pressKeyNoSpecials("Press key to START! (stop w/ ctrl+t) ") |
| 559 | --- |
| 560 | |
| 561 | term.clear() term.setCursorPos(1,1) iPage=iPage+1 |
| 562 | printUI("header") |
| 563 | printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")") |
| 564 | printUI("line") |
| 565 | -- 1234567890123456789012345678901234567 |
| 566 | printUI("Program mines all blocks of a") |
| 567 | printUI(" d * w * h cuboid. ") |
| 568 | printUI(" ") |
| 569 | printUI(" height="..digHeight) |
| 570 | printUI(" . ") |
| 571 | printUI(" | / depth="..digDeep) |
| 572 | printUI(" |/ ") |
| 573 | printUI(" +---- width="..digWide) |
| 574 | printUI("footer") |
| 575 | term.write("Key in depth and press Enter: ") |
| 576 | |
| 577 | coloredTextAt(cPrgName,2,2,baseColor) |
| 578 | coloredTextAt("<-- enter (d>=1)",18,9,baseColor) |
| 579 | event, key, isHeld = os.pullEvent("key") |
| 580 | digDeep=askForNumber("Key in depth and press Enter: ",1,999) |
| 581 | --- |
| 582 | |
| 583 | term.clear() term.setCursorPos(1,1) iPage=iPage+1 |
| 584 | printUI("header") |
| 585 | printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")") |
| 586 | printUI("line") |
| 587 | -- 1234567890123456789012345678901234567 |
| 588 | printUI("Program mines all blocks of a") |
| 589 | printUI(" d * w * h cuboid. ") |
| 590 | printUI(" ") |
| 591 | printUI(" height="..digHeight) |
| 592 | printUI(" . ") |
| 593 | printUI(" | / depth=") |
| 594 | printUI(" |/ ") |
| 595 | printUI(" +---- width="..digWide) |
| 596 | printUI("footer") |
| 597 | term.write("Key in width and press Enter: ") |
| 598 | |
| 599 | coloredTextAt(cPrgName,2,2,baseColor) |
| 600 | coloredTextAt( "(w>=2 or",31,10,baseColor) |
| 601 | coloredTextAt("<-- enter w<=-2)",21,11,baseColor) |
| 602 | coloredTextAt(digDeep,14,9,baseColor) |
| 603 | event, key, isHeld = os.pullEvent("key") |
| 604 | digWide=askForNumber("Key in width and press Enter: ",-999,999) |
| 605 | if digWide==-1 or digWide==0 or digWide==1 then digWide=2 end |
| 606 | --- |
| 607 | |
| 608 | term.clear() term.setCursorPos(1,1) iPage=iPage+1 |
| 609 | printUI("header") |
| 610 | printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")") |
| 611 | printUI("line") |
| 612 | -- 1234567890123456789012345678901234567 |
| 613 | printUI("Program mines all blocks of a") |
| 614 | printUI(" d * w * h cuboid. ") |
| 615 | printUI(" ") |
| 616 | printUI(" height="..digHeight) |
| 617 | printUI(" . ") |
| 618 | printUI(" | / depth=") |
| 619 | printUI(" |/ ") |
| 620 | printUI(" +---- width=") |
| 621 | printUI("footer") |
| 622 | term.write("Key in height and press Enter: ") |
| 623 | |
| 624 | coloredTextAt(cPrgName,2,2,baseColor) |
| 625 | coloredTextAt("<-- enter (h<>0)",15,7,baseColor) |
| 626 | coloredTextAt(digDeep,14,9,baseColor) |
| 627 | coloredTextAt(digWide,17,11,baseColor) |
| 628 | event, key, isHeld = os.pullEvent("key") |
| 629 | digHeight=askForNumber("Key in height and press Enter: ",-999,999) |
| 630 | if digHeight==0 then digHeight=1 end |
| 631 | --- |
| 632 | |
| 633 | term.clear() term.setCursorPos(1,1) iPage=iPage+1 |
| 634 | printUI("header") |
| 635 | printUI(""..cPrgName..", "..cVersion..", by Kaikaku ("..iPage.."/"..iPageMax..")") |
| 636 | printUI("line") |
| 637 | -- 1234567890123456789012345678901234567 |
| 638 | printUI("Program mines all blocks of a") |
| 639 | printUI(" d * w * h cuboid. ") |
| 640 | printUI(" ") |
| 641 | printUI(" height= Options:") |
| 642 | printUI(" . layer by layer: ") |
| 643 | printUI(" | / depth= start within: ") |
| 644 | printUI(' |/ "strip mine": ') |
| 645 | printUI(" +---- width=") |
| 646 | printUI("footer") |
| 647 | term.write("Toggle options or ENTER to start: ") |
| 648 | |
| 649 | coloredTextAt(cPrgName,2,2,baseColor) |
| 650 | coloredTextAt("Options:",18,7,baseColor) |
| 651 | coloredTextAt("l",20,8,baseColor) |
| 652 | coloredTextAt("w",26,9,baseColor) |
| 653 | coloredTextAt("m",27,10,baseColor) |
| 654 | coloredTextAt(digDeep,14,9,baseColor) |
| 655 | coloredTextAt(digWide,17,11,baseColor) |
| 656 | coloredTextAt(digHeight,11,7,baseColor) |
| 657 | |
| 658 | while 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 |
| 689 | end |
| 690 | |
| 691 | end |
| 692 | |
| 693 | |
| 694 | --------------------------------------- |
| 695 | -- additional functions - |
| 696 | --------------------------------------- |
| 697 | |
| 698 | local blnDigUp=false |
| 699 | local blnDigDown=false |
| 700 | |
| 701 | local function digUpDown() |
| 702 | if blnDigUp then dus() end |
| 703 | if blnDigDown then dds() end |
| 704 | end |
| 705 | |
| 706 | local function gfPosDig(n) |
| 707 | if n==nil then n=1 end |
| 708 | for i=1,n,1 do |
| 709 | gfPos() digUpDown() |
| 710 | end |
| 711 | end |
| 712 | |
| 713 | |
| 714 | ------------------------------------------------------------------------------ |
| 715 | -- main: pre-main options ---------------------------------------------------- |
| 716 | ------------------------------------------------------------------------------ |
| 717 | |
| 718 | local selfCall="" |
| 719 | local selfLayer="" |
| 720 | if 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 |
| 773 | end |
| 774 | |
| 775 | |
| 776 | ------------------------------------------------------------------------------ |
| 777 | -- main: program ------------------------------------------------------------- |
| 778 | ------------------------------------------------------------------------------ |
| 779 | term.clear() term.setCursorPos(1,1) |
| 780 | |
| 781 | ---- step 0: check fuel ---- |
| 782 | print("step 0: checking fuel...") |
| 783 | -- fuel |
| 784 | -- estimate consumption |
| 785 | if not blnLayerByLayer then |
| 786 | cMinFuel=math.floor(digHeight/3) |
| 787 | else |
| 788 | cMinFuel=math.floor(digHeight) |
| 789 | end |
| 790 | cMinFuel=cMinFuel*(math.abs(digDeep)+1)*(math.abs(digWide)+1)+(digHeight*2) |
| 791 | cMinFuel=math.floor(cMinFuel*1.1) + 20 -- extra |
| 792 | |
| 793 | refuelManager(cMinFuel,slotFuel,10) |
| 794 | print("... done") |
| 795 | |
| 796 | |
| 797 | ---- step 1: do deal with negative values ---- |
| 798 | term.write("step 1: deal with negative values...") |
| 799 | |
| 800 | -- first dig block is 1,1,1 |
| 801 | tPos[1]=1 tPos[2]=0 tPos[3]=1 tPos[4]=0 -- starting position |
| 802 | |
| 803 | |
| 804 | -- save inital values for end report |
| 805 | digWideOrg=digWide |
| 806 | digDeepOrg=digDeep |
| 807 | |
| 808 | --- check negative width |
| 809 | local blnNegativeWidth=false |
| 810 | if digWide<0 then |
| 811 | blnNegativeWidth=true |
| 812 | digWide=digDeepOrg |
| 813 | digDeep=-digWideOrg |
| 814 | end |
| 815 | |
| 816 | --- check negative height |
| 817 | local blnNegativeHeight=false |
| 818 | local remainingDigHeight=digHeight |
| 819 | if digHeight<0 then |
| 820 | blnNegativeHeight=true |
| 821 | remainingDigHeight=-digHeight |
| 822 | end |
| 823 | print(" done") |
| 824 | |
| 825 | |
| 826 | ---- step 2: enter and go up ---- |
| 827 | term.write("step 2: enter and go up...") |
| 828 | -- step inside area |
| 829 | if not blnStartWithin then |
| 830 | -- move into cuboid |
| 831 | gfPosDig() |
| 832 | else |
| 833 | -- I'm already there |
| 834 | tPos[2]=1 |
| 835 | end |
| 836 | |
| 837 | -- positive or negative inital width? |
| 838 | if not blnNegativeWidth then |
| 839 | grPos() -- turn to show progress |
| 840 | else |
| 841 | -- orientation is already okay, due to negative inital width |
| 842 | end |
| 843 | print(" done") |
| 844 | |
| 845 | -- step 3: starting height |
| 846 | term.write("step 3: starting height...") |
| 847 | if 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 |
| 856 | else |
| 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 |
| 864 | end |
| 865 | print(" done") |
| 866 | |
| 867 | |
| 868 | while 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") |
| 976 | end |
| 977 | |
| 978 | ---- step 7: return to floor ---- |
| 979 | term.write("step 7: return to floor...") |
| 980 | |
| 981 | -- return to floor |
| 982 | if not blnNegativeHeight then |
| 983 | gdPos(tPos[3]-1) |
| 984 | else |
| 985 | guPos(-tPos[3]+1) |
| 986 | end |
| 987 | |
| 988 | -- positive or negative inital width? |
| 989 | if not blnNegativeWidth then |
| 990 | glPos() -- turn to leave |
| 991 | else |
| 992 | -- orientation is already okay, due to negative inital width |
| 993 | end |
| 994 | |
| 995 | -- step out of area |
| 996 | if not blnStartWithin then |
| 997 | -- move out of cuboid |
| 998 | gbPos() |
| 999 | else |
| 1000 | -- I started there, so I'm already done |
| 1001 | end |
| 1002 | print(" done") |
| 1003 | |
| 1004 | |
| 1005 | ---- step 8: finishing stuff / report ---- |
| 1006 | print("Done with tClear "..digDeep.." "..digWide.." "..digHeight) |
| 1007 | print("That looks much cleaner now! !") ss(1) |
| 1008 | --sleep(0.4) |
| 1009 | printUI("header") |
| 1010 | printUI("Check out YouTube for more videos") |
| 1011 | printUI("and turtle programs by Kaikaku :)") |
| 1012 | printUI("footer") |