Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PROGRAM_VERSION = "2.5"
- -- TODO: add more args to configure
- -- allow for negative vertical layers, need to end safely if run into bedrock
- -- 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: autostart in startup, wait for user input, either start or args for cycles, distance, refuel (ability ro receive either directly or over rednet)
- -- and then update logic so turtle can handle using either as needed (e.g. only need to export at end of cycle with ender if inventory is actually full, less exporting = faster mining)
- --TODO: if checks to ensure export/import inventory is actually exists (only check expected slot) before using the function
- --TODO: if using non-ender chests they should be stored in a selected spot, same as ender inventories, that way extra chests picked up during operation can be dropped
- --TODO: add help arg, completion for inputs, better readability on arg display when starting
- local PROGRAM_NAME = "STRIP_MINE"
- local completion = require "cc.completion"
- -- expected args
- local cycles = 1
- local verticalCycles = 1
- local mineDistance = 96
- local argCount = 3 -- #of expected args
- local initialCycles
- local vertDistanceUp = 3
- 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 verticalCycles = tonumber(arg[argIndex]) end
- if(argIndex==3) then mineDistance = 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
- initialCycles = cycles
- print("Cycles= "..cycles..", VertCycles= " ..verticalCycles.. ", Mine Distance= "..mineDistance.." blocks")
- print("waiting a few seconds so you can verify the args")
- sleep(1)
- --make a dependencies table with pastebin download links
- local DEPENDENCIES = {
- [1] = {
- ["name"] = "wLibMining",
- ["link"] = "rJjmGPwk",
- ["path"] = "wisher/"
- },
- [2] = {
- ["name"] = "wLibVariables",
- ["link"] = "P1xXXYKC",
- ["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
- -- Ender Chest
- local enderChests = 0 -- is updated when configuring enderchests and utilised in a few places for checks
- local exportChest = {
- ["name"] = wLibVariables.ENDER_INVENTORIES[2],
- ["slot"] = nil,
- ["exists"] = false
- }
- local importChest = {
- ["name"] = wLibVariables.ENDER_INVENTORIES[1],
- ["slot"] = nil,
- ["exists"] = false
- }
- --TODO: function to help ensure chests are returned to correct slots, maybe make generic and just provide the slot
- function configureEnderChests()
- --TODO: once chests are stored in specific spots we need to get rid of new chests that are mined, export them? place them behind if no export chest?
- -- only mine if missing import or export chest is missing, should generate files that get deleted once finished mining to indicate which chests exist
- -- only need this in GPS and persistent since normal program should never have to retrieve a chest due to failed operations
- -- in the event we have all our chests and detect one we should leave it above/below us, if in front need to either leave it there end maneuver around
- -- or break/place it somewhere. Current thoughts is place it behind us until a maneuver function is made to get around unbreakable blocks
- -- local var1, block = turtle.inspect()
- -- local var2, blockUp = turtle.inspectUp()
- -- local var3, blockDown = turtle.inspectDown()
- -- if ((block.name == exportChest.name) or (block.name == importChest.name)) then
- -- if (not wLibMining.emptySlots(1)) then
- -- wLibMining.dropItems()
- -- end
- -- turtle.dig()
- -- elseif ((blockUp.name == exportChest.name) or (blockUp.name == importChest.name)) then
- -- if (not wLibMining.emptySlots(1)) then
- -- wLibMining.dropItems()
- -- end
- -- turtle.digUp()
- -- elseif ((blockDown.name == exportChest.name) or (blockDown.name == importChest.name )) then
- -- if (not wLibMining.emptySlots(1)) then
- -- wLibMining.dropItems()
- -- end
- -- turtle.digDown()
- -- end
- for i, v in ipairs(wLibVariables.ENDER_INVENTORIES) do
- if (turtle.getItemCount(15) > 0) then
- local item = turtle.getItemDetail(15)
- if ((item ~= nil) and (item.name == v)) then
- importChest.slot = 15
- importChest.exists = true
- enderChests = enderChests + 1
- end
- end
- if (turtle.getItemCount(16) > 0) then
- local item = turtle.getItemDetail(16)
- if ((item ~= nil) and (item.name == v)) then
- exportChest.slot = 16
- exportChest.exists = true
- enderChests = enderChests + 1
- end
- end
- end
- if (enderChests == 1) then
- if (importChest.exists) then
- exportChest.name = importChest.name
- exportChest.slot = importChest.slot
- exportChest.exists = importChest.exists
- elseif (exportChest.exists) then
- importChest.name = exportChest.name
- importChest.slot = exportChest.slot
- importChest.exists = exportChest.exists
- end
- end
- end
- configureEnderChests()
- --TODO: in general need logic to handle more than 2 stacks of enderChests in the event turtles retrieve another one somehow, sortInventory could also mess that up?
- function setEnderChests(exists, slot, type)
- type = type or nil -- 0 = import, 1 = export, nil if it doesnt matter e.g. 1 chest
- if (enderChests == 1) then
- importChest.slot, exportChest.slot = slot, slot
- importChest.exists, exportChest.exists = exists, exists
- elseif (type ~= nil) then
- if (type == 0) then
- importChest.slot = slot
- importChest.exists = exists
- elseif (type == 1) then
- exportChest.slot = slot
- exportChest.exists = exists
- end
- end
- end
- -- 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()
- -- 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
- if (exportChest.exists) then
- --TODO: edit exportInvMining to handle ender chests and use that here
- setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
- elseif (not exportChest.exists) then
- wLibMining.dropItems()
- end
- end
- setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
- wLibMining.detectAndDigStrip()
- turtle.forward()
- end
- --to return from far end of a strip
- function turnReturnStrip()
- turtle.turnRight()
- forward()
- turtle.turnRight()
- end
- --to begin a new strip, use if there is more than 1 cycle
- function turnNewStrip()
- turtle.turnLeft()
- forward()
- turtle.turnLeft()
- end
- function newVertCycleUp()
- if (not wLibMining.emptySlots(3)) then
- if (exportChest.exists) then
- --TODO: edit exportInvMining to handle ender chests and use that here
- setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
- elseif (not exportChest.exists) then
- wLibMining.dropItems()
- end
- end
- setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
- wLibMining.detectAndDigUp()
- turtle.up()
- end
- function newVertCycleRepos()
- turtle.turnRight()
- local resetDistance = ((initialCycles * 2) - 1)
- repeat
- forward()
- resetDistance = resetDistance - 1
- until resetDistance == 0
- turtle.turnRight()
- cycles = initialCycles
- end
- -- 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 dropItems to calculate when to do that based on mineDistance
- print("Initiating: " ..PROGRAM_NAME.." "..PROGRAM_VERSION)
- --setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
- repeat
- repeat --TODO: change into for loop that counts down cycles from max to 0
- print("1st half of Cycle "..cycles)
- for i=1,mineDistance,1 do
- forward()
- end
- print("Halfway through STRIP MINE Cycle")
- print("Turning!")
- turnReturnStrip()
- print("Finished Turning!")
- print("2nd half of STRIP MINE Cycle "..cycles)
- for i=1,mineDistance,1 do
- forward()
- if( ((verticalCycles > 1) or (cycles>1)) and (i==mineDistance)) then
- if (not exportChest.exists) then
- wLibMining.placeInventoryLocal()
- if(wLibMining.depositInventory()) then
- wLibMining.exportInventoryMining()
- end
- if(not wLibMining.depositInventory()) then
- cycles = 1
- verticalCycles = 1
- end
- if(not wLibMining.emptySlots(3)) then -- if unable to clear inventory into non-ender export too acceptable level then stop
- cycles = 1
- verticalCycles = 1
- end
- end
- -- dont need to use exportInventoryEnder() here as that is called when we move if inventory is full, should speed up mining
- if(cycles>1) then turnNewStrip() end
- elseif ( ((verticalCycles == 1) and (cycles == 1)) and (i==mineDistance)) then -- if at end of final cycle on final verticle layer export inventory
- if (exportChest.exists) then
- setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
- end
- end
- end
- cycles = cycles-1
- until cycles==0
- if (verticalCycles > 1) then
- print("moving up to new Verticle Cycle")
- for i=1,vertDistanceUp,1 do
- newVertCycleUp()
- end
- print("moving to start of horizontal Cycle")
- newVertCycleRepos()
- print("Finished Verticle Cycle!")
- end
- verticalCycles = verticalCycles - 1
- print("Remaining Verticle Cycles: "..verticalCycles)
- until verticalCycles==0
- print("Completed STRIP MINE")
- end
- start()
Add Comment
Please, Sign In to add comment