Advertisement
hyndgrinder

gridWork

Sep 18th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.05 KB | None | 0 0
  1. local robot = require('robot')
  2. local computer = require('computer')
  3. local c = require('component')
  4. local ic = c.inventory_controller
  5. local event = require('event')
  6. local sides = require('sides')
  7. local r = require('refuel')
  8.  
  9. local tArgs = { ... }
  10. if #tArgs ~=2 then
  11.     print( "Usage: excavate <lenght>  <width>" )
  12.     return
  13. end
  14.  
  15. -- Mine in a quarry pattern until we hit something we can't dig
  16. local size = tonumber( tArgs[1] )
  17. local tunnelWidth = tonumber( tArgs[2] )
  18. if size < 1 or tunnelWidth < 1 then
  19.     print( "Tunnel length and width must be positive." )
  20.     return
  21. end
  22.  
  23. ------------------Configurations-------------------------  
  24. local usableTool = "Pickaxe"
  25. local minToolDamage = 100 -- No point going out with a tool that is just going to break on the nth swing
  26. local minEnergyReserve = 1000
  27. local wallTile = {['Black Stained Glass']=true}
  28. local ceilingTile = {['Stone']=true}
  29. local floorTile = {['Granite']=true}
  30. -------------------Inits-----------------------------
  31. local depth = 0
  32. local unloaded = 0
  33. local collected = 0
  34. local xPos,zPos = 0,0
  35. local xDir,zDir = 0,1
  36.  
  37. local goTo -- Filled in further down
  38.  
  39. local generator = c.isAvailable('generator')
  40. local invSize = robot.inventorySize()
  41. local isChest = false
  42.  
  43. local consumptionRates = {
  44. running = 0.25*1,
  45. break_block = 0.025*10,
  46. move = 10*1+8.035555556*0.25,
  47. turn = 2.5*1+8.0194444*0.25,
  48. }
  49.  
  50. ----------------------Functions-----------------------
  51.  
  52. local function unload( --[[TODO _bKeepOneFuelStack ]])
  53.     print( "Unloading items..." )
  54.     for n=1,invSize do
  55.         local nCount = robot.count(n)
  56.         if nCount > 0 then
  57.             robot.select(n)        
  58.             local bDrop = true
  59.             if bDrop then
  60.                 robot.drop()
  61.                 unloaded = unloaded + nCount
  62.             end
  63.         end
  64.     end
  65.     collected = 0
  66.     robot.select(1)
  67. end
  68.  
  69. local function requiredEnergy(nTrips)
  70. --[[
  71.     Determine if there is a generator upgrade
  72.     if there is, make sure there are items in generator
  73.     else check Energy Levels and return to charger
  74.     if enough fuel return true
  75. ]]
  76.     nTrips = nTrips or 1
  77.     local moves = math.abs(xPos) + math.abs(zPos) + math.abs(depth)
  78.     local turns = math.abs(1-xDir+zDir)
  79.     local requirements = nTrips * (moves * consumptionRates.move) + (turns * consumptionRates.turn) + consumptionRates.move + minEnergyReserve
  80.     local energy = computer.energy()
  81.     -- if there is enough fuel to cover the return trip return true
  82.     if requirements > energy then
  83.         local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  84.         print( "Not enough Fuel" )
  85.         goTo(0,0,0,0,-1)
  86.         unload()
  87.         r.refuel()
  88.         os.sleep(60)
  89.         goTo(x, y, z, xd, zd)
  90.     end
  91. end
  92.  
  93. local function returnSupplies()
  94.     local x, y, z, xd, zd = xPos,depth,zPos,xDir,zDir
  95.     print( "Returning to surface..." )
  96.     goTo( 0,0,0,0,-1 )
  97.     unload()
  98.     if not done then getSupplies() end
  99.     print( "Resuming mining..." )
  100.     goTo(x, y, z, xd, zd)
  101. end
  102.  
  103. local function collect()   
  104.     local bFull = true
  105.     local nTotalItems = 0
  106.     local nCount = 0
  107.     for n=1,invSize do
  108.         local stack = ic.getStackInInternalSlot(n)
  109.         if not stack then
  110.             -- get Item count (nCount)
  111.             bFull = false
  112.         else
  113.             local nCount = stack.size
  114.         end
  115.     nTotalItems = nTotalItems + nCount
  116.     end
  117.    
  118.     if nTotalItems > collected then
  119.         collected = nTotalItems
  120.         if math.fmod(collected + unloaded, 50) == 0 then
  121.             print( "Mined "..(collected + unloaded).." items." )
  122.         end
  123.     end
  124.    
  125.     if bFull then
  126.         print( "No empty slots left." )
  127.         return false
  128.     end
  129.     return true
  130. end
  131.  
  132. local function reTool()
  133.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  134.     --print("Pickaxe is broken, looking for replacement")
  135.     local dur, msg = robot.durability()
  136.     if not dur then dur =0 end
  137.     local retool = false
  138.     if dur>=0.1then retool = true end
  139.     --cycle through local inventory to find a pickaxe
  140.     --print("Looking internally:  ")
  141.     if not retool then
  142.         for i = 1, invSize do
  143.             local stack = ic.getStackInInternalSlot(i)
  144.             if stack and not retool then
  145.                 if string.find(stack.label, usableTool) and
  146.                 stack.maxDamage-stack.damage>=minToolDamage then
  147.                     robot.select(i)
  148.                     retool, msg = ic.equip()
  149.                     robot.select(1)
  150.                     dur, msg = robot.durability()
  151.                     retool = true
  152.                     return
  153.                 end
  154.             end
  155.         end
  156.    
  157.     --Go back to origin point and look in the chest
  158.         goTo(0,0,0,0,-1)
  159.         unload()
  160.         local chestSize = ic.getInventorySize(sides.front)
  161.         if chestSize then
  162.             for i = 1, chestSize do
  163.                 local stack  = ic.getStackInSlot(sides.front,i)
  164.                 if stack and not retool then
  165.                     if string.find(stack.label, usableTool) and
  166.                     stack.maxDamage-stack.damage>=minToolDamage then
  167.                         ic.suckFromSlot(sides.front,i)
  168.                         retool, msg = ic.equip()
  169.                         dur, msg = robot.durability()
  170.                     end
  171.                 end
  172.             end
  173.         end
  174.     --Wait for a tool to be added to the internal inventory
  175.         while dur<0.1 and not retool do
  176.             --print("require a new tool")
  177.             local bTool, toolSlot = event.pull(1, "inventory_change")
  178.             if toolSlot then ic.equip(toolSlot) end
  179.                 dur, msg = robot.durability()
  180.             if not dur then dur = 0 end
  181.             if dur>=0.1 then
  182.                 retool = true
  183.                 goTo(x, y, z, xd, zd)
  184.                 return
  185.             end
  186.             os.sleep(1)
  187.         end
  188.     end
  189.     --print('All equipped up and ready to go')
  190.  
  191. end
  192.  
  193. local function clearUp()
  194.     local dtctU, msgU = robot.detectUp()
  195.     while dtctU do
  196.         if robot.swingUp() then
  197.             if not collect() then
  198.                 returnSupplies()
  199.             end
  200.         end            
  201.         dtctU, msgU = robot.detectUp()
  202.     end
  203. end
  204.  
  205. local function clearDown()
  206.     local dtctD, msgD = robot.detectDown()
  207.     while dtctD do
  208.         if robot.swingDown() then
  209.             if not collect() then
  210.                 returnSupplies()
  211.             end
  212.         end        
  213.         dtctD, msg = robot.detectDown()
  214.     end
  215. end
  216.  
  217.  local function tryForwards()
  218.     requiredEnergy()
  219.    
  220.     if not robot.durability() or robot.durability()<0.1 then
  221.         reTool()
  222.     end
  223.     local dtct, msg = robot.detect()
  224.     while dtct do
  225.         print("detect forward")
  226.         if robot.swing() then
  227.             if not collect() then
  228.                 returnSupplies()
  229.             end
  230.         else
  231.             return false
  232.         end            
  233.         dtct, msg = robot.detect()
  234.     end
  235.    
  236.     if robot.forward() then
  237.         xPos = xPos + xDir
  238.         zPos = zPos + zDir
  239.     end
  240.     clearUp()
  241.     clearDown()
  242.     return true
  243. end
  244.  
  245. local function turnLeft()
  246.     robot.turnLeft()
  247.     xDir, zDir = -zDir, xDir
  248. end
  249.  
  250. local function turnRight()
  251.     robot.turnRight()
  252.     xDir, zDir = zDir, -xDir
  253. end
  254.  
  255. function goTo( x, y, z, xd, zd )
  256.     if depth>y then
  257.         if robot.up() then
  258.             depth = depth - 1
  259.         elseif robot.swingUp() then
  260.             collect()
  261.         else
  262.             sleep( 0.5 )
  263.         end
  264.     end
  265.    
  266.     while depth < y - 1 do
  267.         if robot.down() then
  268.             depth = depth + 1
  269.         elseif robot.swing() then
  270.             collect()
  271.         else
  272.             os.sleep( 0.5 )
  273.         end
  274.     end
  275.    
  276.     if xPos > x then
  277.         while xDir ~= -1 do
  278.             turnLeft()
  279.         end
  280.         while xPos > x do
  281.             if robot.forward() then
  282.                 xPos = xPos - 1
  283.             elseif robot.swing() then
  284.                 collect()
  285.             else
  286.                 os.sleep( 0.5 )
  287.             end
  288.         end
  289.     elseif xPos < x then
  290.         while xDir ~= 1 do
  291.             turnLeft()
  292.         end
  293.         while xPos < x do
  294.             if robot.forward() then
  295.                 xPos = xPos + 1
  296.             elseif robot.swing() then
  297.                 collect()
  298.             else
  299.                 os.sleep( 0.5 )
  300.             end
  301.         end
  302.     end
  303.    
  304.     if zPos > z then
  305.         while zDir ~= -1 do
  306.             turnLeft()
  307.         end
  308.         while zPos > z do
  309.             if robot.forward() then
  310.                 zPos = zPos - 1
  311.             elseif robot.swing() then
  312.                 collect()
  313.             else
  314.                 os.sleep( 0.5 )
  315.             end
  316.         end
  317.     elseif zPos < z then
  318.         while zDir ~= 1 do
  319.             turnLeft()
  320.         end
  321.         while zPos < z do
  322.             if robot.forward() then
  323.                 zPos = zPos + 1
  324.             elseif robot.swing() then
  325.                 collect()
  326.             else
  327.                 os.sleep( 0.5 )
  328.             end
  329.         end
  330.     end
  331.    
  332.     if depth<y then
  333.         if robot.down() then
  334.             depth = depth + 1
  335.         elseif robot.swingDown() then
  336.             collect()
  337.         else
  338.             sleep( 0.5 )
  339.         end
  340.     end
  341.    
  342.    
  343.     while depth > y do
  344.         if robot.up() then
  345.             depth = depth - 1
  346.         elseif robot.swingUp() then
  347.             collect()
  348.         else
  349.             sleep( 0.5 )
  350.         end
  351.     end
  352.    
  353.     while zDir ~= zd or xDir ~= xd do
  354.         turnLeft()
  355.     end
  356. end
  357.  
  358. ----------------------MAIN-----------------------------
  359.  
  360. robot.select(1)
  361.  
  362. --Check for adequate energy, tools, and supplies
  363.  
  364. requiredEnergy(2)
  365. reTool()
  366.  
  367. print( "Tunneling..." )
  368.  
  369. local alternate = 0
  370. local done = false
  371. clearUp()
  372. clearDown()
  373. while not done do
  374.     for n=1,tunnelWidth do
  375.         for m=1,size-1 do
  376.             --print('n: '..n, 'm: '..m)
  377.             if n==tunnelWidth and m==size-1 then done = true end
  378.             if not tryForwards() then
  379.                 done = true
  380.                 break
  381.             end
  382.        
  383.         end
  384.         if done then
  385.             break
  386.         end
  387.  
  388.         if n<tunnelWidth then
  389.             if math.fmod(n + alternate,2) == 0 then
  390.                 turnLeft()
  391.                 if not tryForwards() then
  392.                     done = true
  393.                     break
  394.                 end
  395.                 turnLeft()
  396.             else
  397.                 turnRight()
  398.                 if not tryForwards() then
  399.                     done = true
  400.                     break
  401.                 end
  402.                 turnRight()
  403.             end
  404.         end
  405.         if done then
  406.             break
  407.         end
  408.    
  409. --[[
  410.     if size > 1 then
  411.         if math.fmod(size,2) == 0 then
  412.             turnRight()
  413.         else
  414.             if alternate == 0 then
  415.                 turnLeft()
  416.             else
  417.                 turnRight()
  418.             end
  419.             alternate = 1 - alternate
  420.         end
  421.     end
  422. ]]--
  423.         print(n,m)
  424.     end
  425.    
  426. end
  427.  
  428. print( "Returning to start... from "..xPos, depth, zPos, xDir, zDir )
  429.  
  430. -- Return to where we started
  431. goTo( 0,0,0,0,-1 )
  432. unload( false )
  433. goTo( 0,0,0,0,1 )
  434.  
  435. print( "Mined "..(collected + unloaded).." items total." )
  436. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement