Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PROGRAM_VERSION = "1.0"
- -- TODO: add more args to configure
- -- preffered fuel?, height Layers?, height tier up/down
- -- TODO: remove dependency downloads and checks, should be handled by startup, reduces issues with different modpacks needing different files
- -- TODO: logic for wireless command activation
- -- TODO: vertical strip logic/cycles
- -- TODO: autostart in startup, wait for wireless input if no active file/=true, and inform user to CTRL+T and relaunch with args to enter manually
- -- TODO: enderchest stuff
- -- TODO: if out of fuel turtle doesnt know and keeps looping, need to stop and pause, need checks for fuel levels, new fuel logic will probs handle it
- local PROGRAM_NAME = "STRIP_MINE_PERSISTENT"
- --make a dependencies table with pastebin download links
- local DEPENDENCIES = {
- [1] = {
- ["name"] = "wLibMining",
- ["link"] = "rJjmGPwk",
- ["path"] = "wisher/"
- },
- [2] = {
- ["name"] = "wLibVariables",
- ["link"] = "P1xXXYKC",
- ["path"] = "wisher/"
- },
- [3] = {
- ["name"] = "wLibFs",
- ["link"] = "", --TODO: add link
- ["path"] = "wisher/"
- }
- }
- -- Ensures all Libraries/Apis are downloaded
- function checkDependencies()
- local libList = {} -- for libs physically located on the robot
- local apiExist = false
- for i, v in ipairs(DEPENDENCIES) do
- local libName = DEPENDENCIES[i]["path"]..DEPENDENCIES[i]["name"]
- libList[i] = shell.resolveProgram(libName) -- attempts to add the filename of dependency[i] to libList to prove it exists
- if(libList[i]==libName) then --if the lib exists then set true so file is not downloaded
- print("Found file: "..DEPENDENCIES[i]["name"])
- apiExist = true
- --sleep(0.15) -- for readability
- end
- if(apiExist==false) then -- if the lib doesnt exist, restart to have startup grab it or cancel out manually
- --local libLink = DEPENDENCIES[i]["link"]
- print("Dependencies not found")
- print("press'CTRL+T' to exit and manually fix, restarting automatically")
- sleep(2)
- os.reboot() -- TODO: launch startup with the command to force download files (if still using pastebin or if gitlab doesnt like tonnes of downloads)
- end -- at this point the file should be downloaded and we SHOULD be able to continue the for loop normally?
- apiExist = false
- end
- end
- checkDependencies()
- -- Dynamically initializes DEPENDENCIES globally, only if one with the same name doesnt already exist
- for keyLibIndex, valueLibName in ipairs(DEPENDENCIES) do
- local libraryName = DEPENDENCIES[keyLibIndex]["name"]
- local libraryPath = DEPENDENCIES[keyLibIndex]["path"]..DEPENDENCIES[keyLibIndex]["name"]
- if(_G[libraryName]) then
- print("Already Loaded: "..libraryName)
- end
- if(not _G[libraryName]) then
- _G[libraryName] = require(libraryPath)
- print("Loaded: "..libraryName)
- end
- end
- sleep(2) -- for readability
- -- expected args
- local cycles = 1
- local mineDistance = 96
- local refuelCount = 3
- local argCount = 3 -- #of expected args
- -- persistence variables
- local isActive -- was running before current boot
- local r_mineDistance -- mineDistance left on current cycle
- local strips -- total strips left --TODO: possibly dont need this
- local r_turnReturnStrip --total return strips left
- local r_turnReturnStripStatus -- if and how the turtle is positioned in a turn
- local r_turnNewStrip -- total new strips left
- local r_turnNewStripStatus -- if and how the turtle is positioned in a turn
- local r_exportInvStatus -- If and where the turtle is at with exporting inventory
- -- persistence files
- local stateDir = "wisher/_states/stripMine/"
- local completeFile = stateDir.."isComplete"
- local activeFile = stateDir.."isActive"
- local conf_mineDistance = stateDir.."_mineDistance"
- local pers_mineDistance = stateDir.."mineDistance"
- local pers_strips = stateDir.."stripsRemaining" --cycles x2 at start, number of strips left to do
- local pers_turnReturn = stateDir.."turnReturn" -- number of remaining return strips
- local pers_turnReturnStatus = stateDir.."turnReturnStatus" -- 0 if not turning, 1 if turned, 2 if moved forward, (then 0 if finished turning?)
- local pers_turnNewStrip = stateDir.."turnNewStrip" --number of new strips remaining
- local pers_turnNewStripStatus = stateDir.."turnNewStripStatus" -- 0 if not turning, 1 if turned, 2 if moved forward, (then 0 if finished turning?)
- local pers_inventoryExportStatus = stateDir.."invExpStatus" -- 0 if not exporting, 1 placed inventory, 2 can deposite & exported, 3 ERROR can't deposit (no chest? or access problem), 4 ERROR still too many full slots in turtle (chest full?)
- local pers_files = {
- [1] = activeFile,
- [2] = conf_mineDistance,
- [3] = pers_mineDistance,
- [4] = pers_strips,
- [5] = pers_turnReturn,
- [6] = pers_turnReturnStatus,
- [7] = pers_turnNewStrip,
- [8] = pers_turnNewStripStatus
- }
- if (#arg > 0) then
- isActive = false
- elseif (wLibFs.exists(activeFile)) then
- isActive = wLibFs.read(nil, activeFile, 2, nil, true)
- if (isActive == "") then
- isActive = false
- elseif (isActive == "true") then
- isActive = true
- elseif (isActive == "false") then
- isActive = false
- end
- else
- isActive = false
- end
- -- To stop program from auto-contiuing when turtle should remain still
- if wLibFs.exists(completeFile) then
- print("Strip mine previously completed")
- print("press key to clear configs and continue")
- os.pullEvent("key_up")
- fs.delete(completeFile)
- print("Cleared Configs!")
- sleep(2)
- shell.run("clear")
- end
- if not (isActive) then
- if wLibFs.exists(stateDir.."_errorLog") then
- fs.delete(stateDir.."_errorLog") -- if not currently meant to be running then delete error log, error log shouldnt exist if the entire config was deleted
- end
- --TODO: print out a message telling user to cancel program and launch manually with args or to enter args wirelessly
- for argIndex=1,#arg,1 do
- if(argIndex>argCount) then
- print("Incorrect number of args")
- print("Defaulting to:")
- break
- end
- if(argIndex==1) then cycles = tonumber(arg[argIndex]) end
- if(argIndex==2) then mineDistance = tonumber(arg[argIndex]) end
- if(argIndex==3) then refuelCount = tonumber(arg[argIndex]) end
- if(argIndex==#arg) then print("Args received:") end
- --if(argIndex>argCount) then print("Discarding unknown arg "..arg[argIndex]) -- could be used but will leave disabled, safer to just use defaults
- end
- if(#arg==0) then
- print("No args")
- print("Defaulting to:")
- end
- print("Cycles= "..cycles..", Mine Distance= "..mineDistance.." blocks"..", fuelConsume#= "..refuelCount)
- print("waiting a few seconds so you can verify the args")
- sleep(1)
- print("saving settings, please wait")
- wLibFs.write(nil, conf_mineDistance, tostring(mineDistance), 2, true)
- wLibFs.write(nil, pers_mineDistance, tostring(mineDistance), 2, true)
- r_mineDistance = mineDistance
- wLibFs.write(nil, pers_strips, tostring(cycles * 2), 2, true)
- strips = (cycles * 2)
- wLibFs.write(nil, pers_turnReturn, tostring(cycles), 2, true)
- r_turnReturnStrip = cycles
- wLibFs.write(nil, pers_turnReturnStatus, tostring(0), 2, true)
- r_turnReturnStripStatus = 0
- wLibFs.write(nil, pers_turnNewStrip, tostring(cycles - 1), 2, true)
- r_turnNewStrip = (cycles - 1)
- wLibFs.write(nil, pers_turnNewStripStatus, tostring(0), 2, true)
- r_turnNewStripStatus = 0
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(0), 2, true)
- r_exportInvStatus = 0
- wLibFs.write(nil, activeFile, tostring(true), 2, true)
- sleep(1) -- give files some time
- print("Saved!")
- elseif isActive then
- local isFullyConfigured = true
- for i, file in ipairs(pers_files) do
- if not (wLibFs.exists(file)) then
- isFullyConfigured = false
- end
- if (wLibFs.read(nil, file, 2, nil, true) == "") then
- isFullyConfigured = false
- end
- end
- if (isFullyConfigured) then
- mineDistance = tonumber(wLibFs.read(nil, conf_mineDistance, 2, nil, true))
- r_mineDistance = tonumber(wLibFs.read(nil, pers_mineDistance, 2, nil, true))
- strips = tonumber(wLibFs.read(nil, pers_strips, 2, nil, true))
- r_turnReturnStrip = tonumber(wLibFs.read(nil, pers_turnReturn, 2, nil, true))
- r_turnReturnStripStatus = tonumber(wLibFs.read(nil, pers_turnReturnStatus, 2, nil, true))
- r_turnNewStrip = tonumber(wLibFs.read(nil, pers_turnNewStrip, 2, nil, true))
- r_turnNewStripStatus = tonumber(wLibFs.read(nil, pers_turnNewStripStatus, 2, nil, true))
- r_exportInvStatus = tonumber(wLibFs.read(nil, pers_inventoryExportStatus, 2, nil, true))
- cycles = math.ceil(strips / 2)
- elseif not isFullyConfigured then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Config Corrupted, unable to safely continue", 2, true)
- print("Config corrupted, unable to continue safely")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up") -- stops program from contiuing, turtle may get lost
- end
- end
- -- if adjusting r_mineDistance here will need to check its not 0, leave at 0 until end of turn then reset to full for forward() to deplete
- -- add optional parameter for turning manuevers, e.g. isOverride = isOverride or false so we can use the function to move forward while r_mineDistance == 0
- -- to move forward and dig, iterate this in a for loop to move needed number of times
- -- when coding is more advanced we will use this better
- function forward(isOverride, isReduceMineDistance)
- isOverride = isOverride or false
- isReduceMineDistance = isReduceMineDistance or true
- -- check/ensure atleast 3 slots free to store mined items, stripmine may mine up to 3 blocks per movement
- if (not wLibMining.emptySlots(3)) then
- wLibMining.dropItems()
- end
- if ((isOverride) or (r_mineDistance > 0)) then
- wLibMining.detectAndDigStrip()
- turtle.forward()
- if isReduceMineDistance then
- wLibFs.write(nil, pers_mineDistance, tostring(r_mineDistance - 1), 2, true)
- r_mineDistance = r_mineDistance - 1
- end
- elseif ((r_mineDistance == 0) and (not (isOverride))) then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Tried to move forward while mineDistance = 0", 2, true)
- print("mineDistance forward() error")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up")
- end
- end
- -- reduces return strip count and updates turnStripStatus at each interval. Once finished should reset mineDistance to full
- -- and set turnStripStatus to the starting value
- --to return from far end of a strip
- function turnReturnStrip(isOverride, isResetMineDistance)
- isOverride = isOverride or false
- isResetMineDistance = isResetMineDistance or true
- if ((isOverride) or ((r_mineDistance == 0) and (r_turnReturnStrip > 0))) then
- if (r_turnReturnStripStatus == 0) then
- turtle.turnRight()
- wLibFs.write(nil, pers_turnReturnStatus, tostring(1), 2, true)
- r_turnReturnStripStatus = 1
- end
- if (r_turnReturnStripStatus == 1) then
- forward(true, false)
- wLibFs.write(nil, pers_turnReturnStatus, tostring(2), 2, true)
- r_turnReturnStripStatus = 2
- end
- if (r_turnReturnStripStatus == 2) then
- turtle.turnRight()
- wLibFs.write(nil, pers_turnReturnStatus, tostring(0), 2, true)
- r_turnReturnStripStatus = 0
- if (not isOverride) then
- wLibFs.write(nil, pers_turnReturn, tostring(r_turnReturnStrip - 1), 2, true)
- r_turnReturnStrip = r_turnReturnStrip - 1
- end
- if isResetMineDistance then
- wLibFs.write(nil, pers_mineDistance, (tostring(tonumber(wLibFs.read(nil, conf_mineDistance, 2, nil, true)))), 2, true)
- r_mineDistance = mineDistance
- end
- end
- elseif ((r_turnReturnStrip == 0) and (not isOverride)) then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Tried to turnReturnStrip() when turnReturn = 0", 2, true)
- print("Return Strip# turnReturnStrip() error")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up")
- elseif ((r_mineDistance > 0) and (not isOverride)) then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Tried to turnReturnStrip() when mineDistance > 0", 2, true)
- print("mineDistance turnReturnStrip() error")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up")
- end
- end
- -- persistence should work the same as turnReturnStrip
- --to begin a new strip, use if there is more than 1 cycle
- function turnNewStrip(isOverride, isResetMineDistance)
- isOverride = isOverride or false
- isResetMineDistance = isResetMineDistance or true
- if ((isOverride) or ((r_mineDistance == 0) and (r_turnNewStrip > 0))) then
- if (r_turnNewStripStatus == 0) then
- turtle.turnLeft()
- wLibFs.write(nil, pers_turnNewStripStatus, tostring(1), 2, true)
- r_turnNewStripStatus = 1
- end
- if (r_turnNewStripStatus == 1) then
- forward(true, false)
- wLibFs.write(nil, pers_turnNewStripStatus, tostring(2), 2, true)
- r_turnNewStripStatus = 2
- end
- if (r_turnNewStripStatus == 2) then
- turtle.turnLeft()
- wLibFs.write(nil, pers_turnNewStripStatus, tostring(0), 2, true)
- r_turnNewStripStatus = 0
- if (not isOverride) then
- wLibFs.write(nil, pers_turnNewStrip, tostring(r_turnNewStrip - 1), 2, true)
- r_turnNewStrip = r_turnNewStrip - 1
- end
- if isResetMineDistance then
- wLibFs.write(nil, pers_mineDistance, (tostring(tonumber(wLibFs.read(nil, conf_mineDistance, 2, nil, true)))), 2, true)
- r_mineDistance = mineDistance
- end
- end
- elseif ((r_turnNewStrip == 0) and (not isOverride)) then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Tried to turnNewStrip() when turnNew = 0", 2, true)
- print("NewStrip# turnNewStrip() error")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up")
- elseif ((r_mineDistance > 0) and (not isOverride)) then
- local handle = wLibFs.write(nil, stateDir.."_errorLog", os.date(), 2, false)
- wLibFs.write(handle, nil, "Tried to turnNewStrip() when mineDistance > 0", 2, true)
- print("MineDistance turnNewStrip() error")
- print("please press CTRL+T and refer to the log")
- os.pullEvent("key_up")
- end
- end
- function exportInventory(isOverride)
- isOverride = isOverride or false
- local handle = wLibFs.open(stateDir.."_errorLog", "w")
- if ((isOverride) or ((r_mineDistance == 0) and ((r_turnReturnStrip == r_turnNewStrip) and (r_turnNewStrip > 0)) )) then
- if (r_exportInvStatus == 0) then
- wLibMining.placeInventoryLocal()
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(1), 2, true)
- r_exportInvStatus = 1
- end
- if (r_exportInvStatus == 1) then
- if(wLibMining.depositInventory()) then
- wLibMining.exportInventoryMining()
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(2), 2, true)
- r_exportInvStatus = 2
- end
- if(not wLibMining.depositInventory()) then
- wLibFs.write(nil, pers_strips, tostring(1))
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(3), 2, true)
- strips = 1
- r_exportInvStatus = 3
- wLibFs.write(handle, nil, os.date(), 2, false)
- wLibFs.write(handle, nil, "DepositInventory() returned false, did chest get placed?", 2, false)
- print("Unable deposit inventory, did the chest get placed?")
- end
- end
- if ((r_exportInvStatus == 2) or (r_exportInvStatus == 3)) then
- if(not wLibMining.emptySlots(3)) then -- if unable to clear inventory into export too acceptable level then stop
- wLibFs.write(nil, pers_strips, tostring(1))
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(4), 2, true)
- strips = 1
- r_exportInvStatus = 4
- wLibFs.write(handle, nil, os.date(), 2, false)
- wLibFs.write(handle, nil, "emptySlots() failed, is the chest full?", 2, false)
- print("Unable export inventory, is the chest full?")
- end
- end
- if ((r_exportInvStatus == 3) or (r_exportInvStatus == 4)) then
- handle.close()
- print("please press CTRL+T and check logs")
- os.pullEvent("key_up")
- end
- if (r_exportInvStatus == 2) then
- handle.close()
- fs.delete(stateDir.."_errorLog")
- wLibFs.write(nil, pers_inventoryExportStatus, tostring(0), 2, true)
- r_exportInvStatus = 0
- end
- end
- end
- -- add logic to check whats happening in persistence files, should read the status of turns first
- -- need a startup bool that can be used to set the current minedistance
- -- Main Function
- function start()
- -- split into halves so robot finished beside where it started for strip mines
- -- bear in mind if more than 1 cycle is present the turtle needs an inventory to deploy
- -- TODO: adjust checkFuel, dropItems to calculate when to do that based on mineDistance
- -- ensure the first run after booting mines the correct distance, then set so all other runs do the expected distance
- local firstRun = true -- first run after reboot, should be used to set mineDistance to r_mineDistance
- local fuelThreshold = 80 * refuelCount - 70 -- assumes the smallest fuel source, larger ones shouldnt be problematic
- print("Initiating: " ..PROGRAM_NAME.." "..PROGRAM_VERSION)
- wLibMining.checkFuel(refuelCount, fuelThreshold)
- repeat
- if (firstRun) then
- if (r_exportInvStatus > 0) then
- exportInventory()
- end
- if (r_turnReturnStripStatus > 0) then
- turnReturnStrip()
- print("2nd half of STRIP MINE Cycle"..cycles)
- end
- if (r_turnNewStripStatus > 0) then
- turnNewStrip()
- print("1st half of Cycle "..cycles)
- end
- if ((r_mineDistance ~= mineDistance) and (not (r_mineDistance == 0))) then
- mineDistance = r_mineDistance
- end
- end
- for i=1,mineDistance,1 do
- if((turtle.getFuelLevel()) / (fuelThreshold - 1) == 1) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
- --if(i%47==0) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
- -- if(i%10==0) then wLibMining.dropItems() end -- not needed now as checked on each forward()
- forward()
- end
- print("Turning!")
- if ((firstRun) and (mineDistance ~= ( tonumber(wLibFs.read(nil, conf_mineDistance, 2, nil, true)) ) )) then
- mineDistance = tonumber(wLibFs.read(nil, conf_mineDistance, 2, nil, true))
- end
- if firstRun then
- firstRun = false
- end
- if(strips>1) then
- --TODO: persistence breaks here, need persistence for exporting inv
- exportInventory()
- if (strips > 1) then
- if((turtle.getFuelLevel()) / (fuelThreshold - 1) == 1) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
- if (r_turnReturnStrip > r_turnNewStrip) then
- turnReturnStrip()
- print("Finished Turning!")
- print("1st half of Cycle "..cycles)
- elseif (r_turnReturnStrip == r_turnNewStrip) then
- turnNewStrip()
- print("Finished Turning!")
- print("2nd half of STRIP MINE Cycle"..cycles)
- end
- end
- end
- wLibFs.write(nil, pers_strips, tostring(strips - 1), 2, true)
- strips = strips-1
- until strips==0
- fs.delete(stateDir)
- wLibFs.newFileBlank(completeFile, true)
- print("Completed STRIP MINE")
- print("press key to clear configs and reboot")
- os.pullEvent("key_up")
- fs.delete(completeFile)
- os.reboot()
- end
- start()
Add Comment
Please, Sign In to add comment