Advertisement
77wisher77

robotTest

Nov 30th, 2020 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.65 KB | None | 0 0
  1. -- TODO: add more args to configure
  2. --    preffered fuel?, height Layers?
  3.  
  4. local PROGRAM_NAME = "STRIP_MINE"
  5. local PROGRAM_VERSION = "v2.0.1"
  6.  
  7. -- expected args
  8. local cycles = 1
  9. local mineDistance = 96
  10. local refuelCount = 3
  11. local argCount = 3 -- #of expected args
  12.  
  13. for argIndex=1,#arg,1 do
  14.   if(argIndex>argCount) then
  15.     print("Incorrect number of args")
  16.     print("Defaulting to:")
  17.     break
  18.   end
  19.   if(argIndex==1) then cycles = tonumber(arg[argIndex]) end
  20.   if(argIndex==2) then mineDistance = tonumber(arg[argIndex]) end
  21.   if(argIndex==3) then refuelCount = tonumber(arg[argIndex]) end
  22.   if(argIndex==#arg) then print("Args received:") end
  23.   --if(argIndex>argCount) then print("Discarding unknown arg "..arg[argIndex]) -- could be used but will leave disabled, safer to just use defaults
  24. end
  25. if(#arg==0) then
  26.   print("No args")
  27.   print("Defaulting to:")
  28. end
  29. print("Cycles= "..cycles..", Mine Distance= "..mineDistance.." blocks"..", fuelConsume#= "..refuelCount)
  30. print("waiting a few seconds so you can verify the args")
  31. sleep(2)
  32.  
  33. --make a dependencies table with pastebin download links
  34. local DEPENDENCIES = {
  35.   [1] = {
  36.     ["name"] = "wLibMining",
  37.     ["link"] = "rJjmGPwk",
  38.     ["path"] = "wisher/"
  39.   },
  40.   [2] = {
  41.     ["name"] = "wLibVariables",
  42.     ["link"] = "P1xXXYKC",
  43.     ["path"] = "wisher/"
  44.   }
  45. }
  46.  
  47. -- Ensures all Libraries/Apis are downloaded
  48. function checkDependencies()
  49.   local libList = {} -- for libs physically located on the robot
  50.   local apiExist = false
  51.   for i, v in ipairs(DEPENDENCIES) do
  52.     local libName = DEPENDENCIES[i]["path"]..DEPENDENCIES[i]["name"]
  53.     libList[i] = shell.resolveProgram(libName) -- attempts to add the filename of dependency[i] to libList to prove it exists
  54.     if(libList[i]==libName) then --if the lib exists then set true so file is not downloaded
  55.       print("Found file: "..DEPENDENCIES[i]["name"])
  56.       apiExist = true
  57.       sleep(0.2) -- for readability
  58.     end
  59.     if(apiExist==false) then -- if the lib doesnt exist, download it by using the link in dependancy[i]
  60.       local libLink = DEPENDENCIES[i]["link"]
  61.       shell.run("pastebin", "get", libLink, libName)
  62.       sleep(0.2) -- for readability
  63.     end -- at this point the file should be downloaded and we SHOULD be able to continue the for loop normally?
  64.   end
  65. end
  66. checkDependencies()
  67. -- Dynamically initializes DEPENDENCIES globally, only if one with the same name doesnt already exist
  68. for keyLibIndex, valueLibName in ipairs(DEPENDENCIES) do
  69.   local libraryName = DEPENDENCIES[keyLibIndex]["name"]
  70.   local libraryPath = DEPENDENCIES[keyLibIndex]["path"]..DEPENDENCIES[keyLibIndex]["name"]
  71.   if(_G[libraryName]) then
  72.     print("Already Loaded: "..libraryName)
  73.   end
  74.   if(not _G[libraryName]) then
  75.     _G[libraryName] = require(libraryPath)
  76.     print("Loaded: "..libraryName)
  77.   end
  78. end
  79. sleep(3) -- for readability
  80.  
  81. -- to move forward and dig, iterate this in a for loop to move needed number of times
  82. -- when coding is more advanced we will use this better
  83. function forward()
  84.   -- check/ensure atleast 3 slots free to store mined items, stripmine may mine up to 3 blocks per movement
  85.   if (not wLibMining.emptySlots(3)) then
  86.     wLibMining.dropItems()
  87.   end
  88.   wLibMining.detectAndDigStrip()
  89.   turtle.forward()
  90.   fuelLog = fuelLog + 1
  91. end
  92.  
  93. --to return from far end of a strip
  94. function turnReturnStrip()
  95.   turtle.turnRight()
  96.   forward()
  97.   turtle.turnRight()
  98. end
  99.  
  100. --to begin a new strip, use if there is more than 1 cycle
  101. function turnNewStrip()
  102.     turtle.turnLeft()
  103.     forward()
  104.     turtle.turnLeft()
  105. end
  106.  
  107. -- Main Function
  108. function start()
  109.   -- split into halves so robot finished beside where it started for strip mines
  110.   -- bear in mind if more than 1 cycle is present the turtle needs an inventory to deploy
  111.   -- TODO: adjust checkFuel, dropItems to calculate when to do that based on mineDistance
  112.   local fuelThreshold = 80 * refuelCount - 70 -- assumes the smallest fuel source, larger ones shouldnt be problematic
  113.   print("Initiating: " ..PROGRAM_NAME.." "..PROGRAM_VERSION)
  114.   wLibMining.checkFuel(refuelCount, fuelThreshold)
  115.   repeat
  116.     print("1st half of Cycle "..cycles)
  117.     for i=1,mineDistance,1 do
  118.       if((turtle.getFuelLevel()) / (fuelThreshold - 1) == 1) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
  119.       --if(i%47==0) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
  120.       -- if(i%10==0) then wLibMining.dropItems() end -- not needed now as checked on each forward()
  121.       forward()
  122.     end
  123.     print("Halfway through STRIP MINE Cycle")
  124.     print("Turning!")
  125.     turnReturnStrip()
  126.     print("Finished Turning!")
  127.     if((turtle.getFuelLevel()) / (fuelThreshold - 1) == 1) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
  128.     print("2nd half of STRIP MINE Cycle"..cycles)
  129.     for i=1,mineDistance,1 do
  130.       if((turtle.getFuelLevel()) / (fuelThreshold - 1) == 1) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
  131.       -- if(i%47==0) then wLibMining.checkFuel(refuelCount, fuelThreshold) end
  132.       -- if(i%10==0) then wLibMining.dropItems() end -- not needed now as checked on each forward()
  133.       forward()
  134.       if(cycles>1) and (i==mineDistance) then
  135.         wLibMining.placeInventoryLocal()
  136.         if(wLibMining.depositInventory()) then
  137.           wLibMining.exportInventoryMining()
  138.         end
  139.         if(not wLibMining.depositInventory()) then
  140.           cycles = 1
  141.         end
  142.         if(not wLibMining.emptySlots(3)) then cycles = 1 end -- if unable to clear inventory into export too acceptable level then stop
  143.         if(cycles>1) then turnNewStrip() end
  144.       end
  145.     end
  146.     cycles = cycles-1
  147.   until cycles==0
  148.   print("Completed STRIP MINE")
  149. end
  150. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement