77wisher77

stripMine ender test

Jun 17th, 2021 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.96 KB | None | 0 0
  1. local PROGRAM_VERSION = "2.5"
  2. -- TODO: add more args to configure
  3. --    allow for negative vertical layers, need to end safely if run into bedrock
  4. -- TODO: remove dependency downloads and checks, should be handled by startup, reduces issues with different modpacks needing different files
  5. -- TODO: logic for wireless command activation
  6. -- TODO: autostart in startup, wait for user input, either start or args for cycles, distance, refuel (ability ro receive either directly or over rednet)
  7. --    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)
  8. --TODO: if checks to ensure export/import inventory is actually exists (only check expected slot) before using the function
  9. --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
  10. --TODO: add help arg, completion for inputs, better readability on arg display when starting
  11.  
  12. local PROGRAM_NAME = "STRIP_MINE"
  13.  
  14. local completion = require "cc.completion"
  15.  
  16.  
  17.  
  18. -- expected args
  19. local cycles = 1
  20. local verticalCycles = 1
  21. local mineDistance = 96
  22. local argCount = 3 -- #of expected args
  23.  
  24. local initialCycles
  25. local vertDistanceUp = 3
  26.  
  27.  
  28. for argIndex=1,#arg,1 do
  29.   if(argIndex>argCount) then
  30.     print("Incorrect number of args")
  31.     print("Defaulting to:")
  32.     break
  33.   end
  34.   if(argIndex==1) then cycles = tonumber(arg[argIndex]) end
  35.   if(argIndex==2) then verticalCycles = tonumber(arg[argIndex]) end
  36.   if(argIndex==3) then mineDistance = tonumber(arg[argIndex]) end
  37.   if(argIndex==#arg) then print("Args received:") end
  38.   --if(argIndex>argCount) then print("Discarding unknown arg "..arg[argIndex]) -- could be used but will leave disabled, safer to just use defaults
  39. end
  40. if(#arg==0) then
  41.   print("No args")
  42.   print("Defaulting to:")
  43. end
  44. initialCycles = cycles
  45. print("Cycles= "..cycles..", VertCycles= " ..verticalCycles.. ", Mine Distance= "..mineDistance.." blocks")
  46. print("waiting a few seconds so you can verify the args")
  47. sleep(1)
  48.  
  49. --make a dependencies table with pastebin download links
  50. local DEPENDENCIES = {
  51.   [1] = {
  52.     ["name"] = "wLibMining",
  53.     ["link"] = "rJjmGPwk",
  54.     ["path"] = "wisher/"
  55.   },
  56.   [2] = {
  57.     ["name"] = "wLibVariables",
  58.     ["link"] = "P1xXXYKC",
  59.     ["path"] = "wisher/"
  60.   }
  61. }
  62.  
  63. -- Ensures all Libraries/Apis are downloaded
  64. function checkDependencies()
  65.   local libList = {} -- for libs physically located on the robot
  66.   local apiExist = false
  67.   for i, v in ipairs(DEPENDENCIES) do
  68.     local libName = DEPENDENCIES[i]["path"]..DEPENDENCIES[i]["name"]
  69.     libList[i] = shell.resolveProgram(libName) -- attempts to add the filename of dependency[i] to libList to prove it exists
  70.     if(libList[i]==libName) then --if the lib exists then set true so file is not downloaded
  71.       print("Found file: "..DEPENDENCIES[i]["name"])
  72.       apiExist = true
  73.       --sleep(0.15) -- for readability
  74.     end
  75.     if(apiExist==false) then -- if the lib doesnt exist, restart to have startup grab it or cancel out manually
  76.       --local libLink = DEPENDENCIES[i]["link"]
  77.       print("Dependencies not found")
  78.       print("press'CTRL+T' to exit and manually fix, restarting automatically")
  79.       sleep(2)
  80.       os.reboot() -- TODO: launch startup with the command to force download files (if still using pastebin or if gitlab doesnt like tonnes of downloads)
  81.     end -- at this point the file should be downloaded and we SHOULD be able to continue the for loop normally?
  82.     apiExist = false
  83.   end
  84. end
  85. checkDependencies()
  86. -- Dynamically initializes DEPENDENCIES globally, only if one with the same name doesnt already exist
  87. for keyLibIndex, valueLibName in ipairs(DEPENDENCIES) do
  88.   local libraryName = DEPENDENCIES[keyLibIndex]["name"]
  89.   local libraryPath = DEPENDENCIES[keyLibIndex]["path"]..DEPENDENCIES[keyLibIndex]["name"]
  90.   if(_G[libraryName]) then
  91.     print("Already Loaded: "..libraryName)
  92.   end
  93.   if(not _G[libraryName]) then
  94.     _G[libraryName] = require(libraryPath)
  95.     print("Loaded: "..libraryName)
  96.   end
  97. end
  98. sleep(2) -- for readability
  99.  
  100.  
  101. -- Ender Chest
  102. local enderChests = 0 -- is updated when configuring enderchests and utilised in a few places for checks
  103. local exportChest = {
  104.   ["name"] = wLibVariables.ENDER_INVENTORIES[2],
  105.   ["slot"] = nil,
  106.   ["exists"] = false
  107. }
  108. local importChest = {
  109.   ["name"] = wLibVariables.ENDER_INVENTORIES[1],
  110.   ["slot"] = nil,
  111.   ["exists"] = false
  112. }
  113.  
  114.  
  115. --TODO: function to help ensure chests are returned to correct slots, maybe make generic and just provide the slot
  116. function configureEnderChests()
  117.  
  118.   --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?
  119.   -- only mine if missing import or export chest is missing, should generate files that get deleted once finished mining to indicate which chests exist
  120.   -- only need this in GPS and persistent since normal program should never have to retrieve a chest due to failed operations
  121.   -- 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
  122.   --    or break/place it somewhere. Current thoughts is place it behind us until a maneuver function is made to get around unbreakable blocks
  123.   -- local var1, block = turtle.inspect()
  124.   -- local var2, blockUp = turtle.inspectUp()
  125.   -- local var3, blockDown = turtle.inspectDown()
  126.   -- if ((block.name == exportChest.name) or (block.name == importChest.name)) then
  127.   --   if (not wLibMining.emptySlots(1)) then
  128.   --     wLibMining.dropItems()
  129.   --   end
  130.   --   turtle.dig()
  131.   -- elseif ((blockUp.name == exportChest.name) or (blockUp.name == importChest.name)) then
  132.   --   if (not wLibMining.emptySlots(1)) then
  133.   --     wLibMining.dropItems()
  134.   --   end
  135.   --   turtle.digUp()
  136.   -- elseif ((blockDown.name == exportChest.name) or (blockDown.name == importChest.name )) then
  137.   --   if (not wLibMining.emptySlots(1)) then
  138.   --     wLibMining.dropItems()
  139.   --   end
  140.   --   turtle.digDown()
  141.   -- end
  142.  
  143.   for i, v in ipairs(wLibVariables.ENDER_INVENTORIES) do
  144.     if (turtle.getItemCount(15) > 0) then
  145.       local item = turtle.getItemDetail(15)
  146.       if ((item ~= nil) and (item.name == v)) then
  147.         importChest.slot = 15
  148.         importChest.exists = true
  149.         enderChests = enderChests + 1
  150.       end
  151.     end
  152.     if (turtle.getItemCount(16) > 0) then
  153.       local item = turtle.getItemDetail(16)
  154.       if ((item ~= nil) and (item.name == v)) then
  155.         exportChest.slot = 16
  156.         exportChest.exists = true
  157.         enderChests = enderChests + 1
  158.       end
  159.     end
  160.   end
  161.   if (enderChests == 1) then
  162.     if (importChest.exists) then
  163.       exportChest.name = importChest.name
  164.       exportChest.slot = importChest.slot
  165.       exportChest.exists = importChest.exists
  166.     elseif (exportChest.exists) then
  167.       importChest.name = exportChest.name
  168.       importChest.slot = exportChest.slot
  169.       importChest.exists = exportChest.exists
  170.     end
  171.   end
  172. end
  173. configureEnderChests()
  174.  
  175. --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?
  176. function setEnderChests(exists, slot, type)
  177.   type = type or nil -- 0 = import, 1 = export, nil if it doesnt matter e.g. 1 chest
  178.   if (enderChests == 1) then
  179.     importChest.slot, exportChest.slot = slot, slot
  180.     importChest.exists, exportChest.exists = exists, exists
  181.   elseif (type ~= nil) then
  182.     if (type == 0) then
  183.       importChest.slot = slot
  184.       importChest.exists = exists
  185.     elseif (type == 1) then
  186.       exportChest.slot = slot
  187.       exportChest.exists = exists
  188.     end
  189.   end
  190.  
  191. end
  192.  
  193. -- to move forward and dig, iterate this in a for loop to move needed number of times
  194. -- when coding is more advanced we will use this better
  195. function forward()
  196.   -- check/ensure atleast 3 slots free to store mined items, stripmine may mine up to 3 blocks per movement
  197.   if (not wLibMining.emptySlots(3)) then
  198.     if (exportChest.exists) then
  199.       --TODO: edit exportInvMining to handle ender chests and use that here
  200.       setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
  201.     elseif (not exportChest.exists) then
  202.     wLibMining.dropItems()
  203.     end
  204.   end
  205.   setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
  206.   wLibMining.detectAndDigStrip()
  207.   turtle.forward()
  208. end
  209.  
  210. --to return from far end of a strip
  211. function turnReturnStrip()
  212.   turtle.turnRight()
  213.   forward()
  214.   turtle.turnRight()
  215. end
  216.  
  217. --to begin a new strip, use if there is more than 1 cycle
  218. function turnNewStrip()
  219.   turtle.turnLeft()
  220.   forward()
  221.   turtle.turnLeft()
  222. end
  223.  
  224. function newVertCycleUp()
  225.   if (not wLibMining.emptySlots(3)) then
  226.     if (exportChest.exists) then
  227.       --TODO: edit exportInvMining to handle ender chests and use that here
  228.       setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
  229.     elseif (not exportChest.exists) then
  230.     wLibMining.dropItems()
  231.     end
  232.   end
  233.   setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
  234.   wLibMining.detectAndDigUp()
  235.   turtle.up()
  236. end
  237.  
  238. function newVertCycleRepos()
  239.   turtle.turnRight()
  240.   local resetDistance = ((initialCycles * 2) - 1)
  241.   repeat
  242.     forward()
  243.     resetDistance = resetDistance - 1
  244.   until resetDistance == 0
  245.   turtle.turnRight()
  246.   cycles = initialCycles
  247. end
  248.  
  249. -- Main Function
  250. function start()
  251.   -- split into halves so robot finished beside where it started for strip mines
  252.   -- bear in mind if more than 1 cycle is present the turtle needs an inventory to deploy
  253.   -- TODO: adjust dropItems to calculate when to do that based on mineDistance
  254.   print("Initiating: " ..PROGRAM_NAME.." "..PROGRAM_VERSION)
  255.   --setEnderChests(wLibMining.checkFuel(importChest.exists, importChest.slot))
  256.   repeat
  257.     repeat --TODO: change into for loop that counts down cycles from max to 0
  258.       print("1st half of Cycle "..cycles)
  259.       for i=1,mineDistance,1 do
  260.         forward()
  261.       end
  262.       print("Halfway through STRIP MINE Cycle")
  263.       print("Turning!")
  264.       turnReturnStrip()
  265.       print("Finished Turning!")
  266.       print("2nd half of STRIP MINE Cycle "..cycles)
  267.       for i=1,mineDistance,1 do
  268.         forward()
  269.         if( ((verticalCycles > 1) or (cycles>1)) and (i==mineDistance)) then
  270.           if (not exportChest.exists) then
  271.             wLibMining.placeInventoryLocal()
  272.             if(wLibMining.depositInventory()) then
  273.               wLibMining.exportInventoryMining()
  274.             end
  275.             if(not wLibMining.depositInventory()) then
  276.               cycles = 1
  277.               verticalCycles = 1
  278.             end
  279.             if(not wLibMining.emptySlots(3)) then -- if unable to clear inventory into non-ender export too acceptable level then stop
  280.               cycles = 1
  281.               verticalCycles = 1
  282.             end
  283.           end
  284.           -- dont need to use exportInventoryEnder() here as that is called when we move if inventory is full, should speed up mining
  285.           if(cycles>1) then turnNewStrip() end
  286.         elseif ( ((verticalCycles == 1) and (cycles == 1)) and (i==mineDistance)) then -- if at end of final cycle on final verticle layer export inventory
  287.           if (exportChest.exists) then
  288.             setEnderChests(wLibMining.exportInventoryEnder(exportChest.exists, exportChest.slot))
  289.           end
  290.         end
  291.       end
  292.       cycles = cycles-1
  293.     until cycles==0
  294.  
  295.     if (verticalCycles > 1) then
  296.       print("moving up to new Verticle Cycle")
  297.       for i=1,vertDistanceUp,1 do
  298.         newVertCycleUp()
  299.       end
  300.       print("moving to start of horizontal Cycle")
  301.       newVertCycleRepos()
  302.       print("Finished Verticle Cycle!")
  303.     end
  304.     verticalCycles = verticalCycles - 1
  305.     print("Remaining Verticle Cycles: "..verticalCycles)
  306.   until verticalCycles==0
  307.   print("Completed STRIP MINE")
  308. end
  309. start()
Add Comment
Please, Sign In to add comment