Advertisement
hyndgrinder

/Woodhouse/bin/excavate.lua

Sep 18th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.83 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 ~= 1 and #tArgs~=6 then
  11.     print( "Usage: excavate <diameter> " )
  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. if size < 1 then
  18.     print( "Excavate diameter must be positive" )
  19.     return
  20. end
  21.  
  22. ------------------Configurations-------------------------  
  23. local usableTool = "Pickaxe"
  24. local minToolDamage = 100 -- No point going out with a tool that is just going to break on the nth swing
  25. local minEnergyReserve = 1000
  26. local wallTile = {['Black Stained Glass']=true}
  27. local ceilingTile = {['Stone']=true}
  28. local floorTile = {['Granite']=true}
  29. -------------------Inits-----------------------------
  30. local depth = 0
  31. local unloaded = 0
  32. local collected = 0
  33. local xPos,zPos = 0,0
  34. local xDir,zDir = 0,1
  35.  
  36. local goTo -- Filled in further down
  37.  
  38. local generator = c.isAvailable('generator')
  39. local invSize = robot.inventorySize()
  40. local isChest = false
  41.  
  42. local consumptionRates = {
  43. running = 0.25*1,
  44. break_block = 0.025*10,
  45. move = 10*1+8.035555556*0.25,
  46. turn = 2.5*1+8.0194444*0.25,
  47. }
  48.  
  49. ----------------------Functions-----------------------
  50.  
  51. local function unload( --[[TODO _bKeepOneFuelStack ]])
  52.     print( "Unloading items..." )
  53.     for n=1,invSize do
  54.         local nCount = robot.count(n)
  55.         if nCount > 0 then
  56.             robot.select(n)        
  57.             local bDrop = true
  58.             if bDrop then
  59.                 robot.drop()
  60.                 unloaded = unloaded + nCount
  61.             end
  62.         end
  63.     end
  64.     collected = 0
  65.     robot.select(1)
  66. end
  67.  
  68. local function requiredEnergy(nTrips)
  69. --[[
  70.     Determine if there is a generator upgrade
  71.     if there is, make sure there are items in generator
  72.     else check Energy Levels and return to charger
  73.     if enough fuel return true
  74. ]]
  75.     nTrips = nTrips or 1
  76.     local moves = math.abs(xPos) + math.abs(zPos) + math.abs(depth)
  77.     local turns = math.abs(1-xDir+zDir)
  78.     local requirements = nTrips * (moves * consumptionRates.move) + (turns * consumptionRates.turn) + consumptionRates.move + minEnergyReserve
  79.     local energy = computer.energy()
  80.     -- if there is enough fuel to cover the return trip return true
  81.     if requirements > energy then
  82.         local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  83.         print( "Not enough Fuel" )
  84.         goTo(0,0,0,0,-1)
  85.         unload()
  86.         r.refuel()
  87.         os.sleep(60)
  88.         goTo(x, y, z, xd, zd)
  89.     end
  90. end
  91.  
  92. local function getSupplies()
  93.     local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  94.     print("Getting Supplies. \nIm at "..x, y, z, xd, zd)
  95.     goTo(0,0,0,0,-1)
  96.     if ic.getInventorySize(sides.front) then isChest = true end
  97.     --Combine the Tiles tables
  98.     if isChest then
  99.         print("dump chest found")
  100.         local tileLists = {wallTile, ceilingTile, floorTile}
  101.         local needsList = {}
  102.         local chestSize = ic.getInventorySize(sides.front)
  103.    
  104.         for k,v in pairs(tileLists) do
  105.             for a,b in pairs(v) do
  106.                 needsList[a] = true
  107.             end
  108.         end
  109.         print("needslist",needsList)
  110.         --for each unique type of block get 1 stack or maxAvailable
  111.         repeat
  112.         print("cycling needed items")
  113.             for a,b in pairs(needsList) do
  114.                 print("item: ".. tostring(a))
  115.                 local n =1
  116.                 for i=1,chestSize do
  117.                     local stack = ic.getStackInSlot(sides.front, i)
  118.                     if stack then
  119.                         if stack.label == a then
  120.                             ic.suckFromSlot(sides.front, i)
  121.                             table.remove(needsList, n)
  122.                             break
  123.                         end
  124.                     end
  125.                 end
  126.                 n = n+1
  127.             end
  128.             os.sleep(5)
  129.         until (#needsList==0)  
  130.         print("finished gathering supplies.  Headed back to "..x,y,z,xd,zd)
  131.         goTo(x,y,z,xd,zd)
  132.     else
  133.         print("no dump chest found")
  134.         goTo(x,y,z,xd,zd)
  135.         return false
  136.     end
  137.    
  138. end
  139.  
  140. local function returnSupplies()
  141.     local x, y, z, xd, zd = xPos,depth,zPos,xDir,zDir
  142.     print( "Returning to surface..." )
  143.     goTo( 0,0,0,0,-1 )
  144.     unload()
  145.     if not done then getSupplies() end
  146.     print( "Resuming mining..." )
  147.     goTo(x, y, z, xd, zd)
  148. end
  149.  
  150. local function collect()   
  151.     local bFull = true
  152.     local nTotalItems = 0
  153.     local nCount = 0
  154.     for n=1,invSize do
  155.         local stack = ic.getStackInInternalSlot(n)
  156.         if not stack then
  157.             -- get Item count (nCount)
  158.             bFull = false
  159.         else
  160.             local nCount = stack.size
  161.         end
  162.     nTotalItems = nTotalItems + nCount
  163.     end
  164.    
  165.     if nTotalItems > collected then
  166.         collected = nTotalItems
  167.         if math.fmod(collected + unloaded, 50) == 0 then
  168.             print( "Mined "..(collected + unloaded).." items." )
  169.         end
  170.     end
  171.    
  172.     if bFull then
  173.         print( "No empty slots left." )
  174.         return false
  175.     end
  176.     return true
  177. end
  178.  
  179. local function reTool()
  180.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  181.     print("Pickaxe is broken, looking for replacement")
  182.     local dur, msg = robot.durability()
  183.     if not dur then dur =0 end
  184.     local retool = false
  185.     if dur>=0.1then retool = true end
  186.     --cycle through local inventory to find a pickaxe
  187.     print("Looking internally:  ")
  188.     if not retool then
  189.         for i = 1, invSize do
  190.             local stack = ic.getStackInInternalSlot(i)
  191.             if stack and not retool then
  192.                 if string.find(stack.label, usableTool) and
  193.                 stack.maxDamage-stack.damage>=minToolDamage then
  194.                     robot.select(i)
  195.                     retool, msg = ic.equip()
  196.                     robot.select(1)
  197.                     dur, msg = robot.durability()
  198.                     retool = true
  199.                     return
  200.                 end
  201.             end
  202.         end
  203.    
  204.     --Go back to origin point and look in the chest
  205.         goTo(0,0,0,0,-1)
  206.         unload()
  207.         local chestSize = ic.getInventorySize(sides.front)
  208.         if chestSize then
  209.             for i = 1, chestSize do
  210.                 local stack  = ic.getStackInSlot(sides.front,i)
  211.                 if stack and not retool then
  212.                     if string.find(stack.label, usableTool) and
  213.                     stack.maxDamage-stack.damage>=minToolDamage then
  214.                         ic.suckFromSlot(sides.front,i)
  215.                         retool, msg = ic.equip()
  216.                         dur, msg = robot.durability()
  217.                     end
  218.                 end
  219.             end
  220.         end
  221.     --Wait for a tool to be added to the internal inventory
  222.         while dur<0.1 and not retool do
  223.             print("require a new tool")
  224.             local bTool, toolSlot = event.pull(1, "inventory_change")
  225.             if toolSlot then ic.equip(toolSlot) end
  226.                 dur, msg = robot.durability()
  227.             if not dur then dur = 0 end
  228.             if dur>=0.1 then
  229.                 retool = true
  230.                 goTo(x, y, z, xd, zd)
  231.                 return
  232.             end
  233.             os.sleep(1)
  234.         end
  235.     end
  236.     print('All equipped up and ready to go')
  237.  
  238. end
  239.  
  240. local function checkPlace(dir)
  241.     local dir = dir or 'f'
  242.     local dirActions = {
  243.             ['u']={ceilingTile, robot.detectUp, robot.placeUp},
  244.             ['d']={floorTile, robot.detectDown, robot.placeDown},
  245.             ['f']={wallTile, robot.detect, robot.place},       
  246.         }
  247.     for i = 1, invSize do
  248.         stack = ic.getStackInInternalSlot(i)
  249.         if stack then
  250.             if dirActions[dir][1][stack.label] then
  251.                 robot.select(i)
  252.                 dirActions[dir][3]()
  253.                 robot.select(1)
  254.                 return true
  255.             end
  256.         end
  257.     end
  258.     return false
  259. end
  260.  
  261.  local function tryForwards()
  262.     requiredEnergy()
  263.    
  264.     if not robot.durability() or robot.durability()<0.1 then
  265.         reTool()
  266.     end
  267.     local dtct, msg = robot.detect()
  268.     while dtct do
  269.         if robot.swing() then
  270.             if not collect() then
  271.                 returnSupplies()
  272.             end
  273.         else
  274.             return false
  275.         end            
  276.         dtct, msg = robot.detect()
  277.     end
  278.     if robot.forward() then    
  279.         xPos = xPos + xDir
  280.         zPos = zPos + zDir
  281.     end
  282.     return true
  283. end
  284.  
  285. local function tryDown()
  286.     requiredEnergy()
  287.    
  288.     if not robot.durability() or robot.durability()<0.1 then
  289.         reTool()
  290.     end
  291.    
  292.     while not robot.down() do
  293.         local block, dMsg = robot.detectDown()
  294.         if block then
  295.             if dMsg=="solid" then
  296.                 local action, aMsg = robot.swingDown()
  297.                 if action then
  298.                 else
  299.                     if not collect() then returnSupplies() end
  300.                     if aMsg == "block" then return false    end
  301.                 end
  302.             elseif dMsg == 'entity' then
  303.                 robot.swingDown()
  304.                 if not collect() then
  305.                     returnSupplies()
  306.                 end
  307.             end
  308.         else
  309.             os.sleep( 0.5 )
  310.         end
  311.     end
  312.  
  313.     depth = depth + 1
  314.     if math.fmod( depth, 10 ) == 0 then
  315.         print( "Descended "..depth.." metres." )
  316.     end
  317.     return true
  318. end
  319.  
  320. local function turnLeft()
  321.     robot.turnLeft()
  322.     xDir, zDir = -zDir, xDir
  323. end
  324.  
  325. local function turnRight()
  326.     robot.turnRight()
  327.     xDir, zDir = zDir, -xDir
  328. end
  329.  
  330. local function sealOutside(dir)
  331.     if dir=="left" then
  332.         turnLeft()
  333.         print("place wallTile: ".. tostring(checkPlace("f")))
  334.         turnRight()
  335.     elseif dir=="right" then
  336.         turnLeft()
  337.         print("place wallTile: ".. tostring(checkPlace("f")))
  338.         turnRight()
  339.     end
  340. end
  341.  
  342. function goTo( x, y, z, xd, zd )
  343.     if depth>y then
  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 depth < y - 1 do
  354.         if robot.down() then
  355.             depth = depth + 1
  356.         elseif robot.swing() then
  357.             collect()
  358.         else
  359.             os.sleep( 0.5 )
  360.         end
  361.     end
  362.    
  363.     if xPos > x then
  364.         while xDir ~= -1 do
  365.             turnLeft()
  366.         end
  367.         while xPos > x do
  368.             if robot.forward() then
  369.                 xPos = xPos - 1
  370.             elseif robot.swing() then
  371.                 collect()
  372.             else
  373.                 os.sleep( 0.5 )
  374.             end
  375.         end
  376.     elseif xPos < x then
  377.         while xDir ~= 1 do
  378.             turnLeft()
  379.         end
  380.         while xPos < x do
  381.             if robot.forward() then
  382.                 xPos = xPos + 1
  383.             elseif robot.swing() then
  384.                 collect()
  385.             else
  386.                 os.sleep( 0.5 )
  387.             end
  388.         end
  389.     end
  390.    
  391.     if zPos > z then
  392.         while zDir ~= -1 do
  393.             turnLeft()
  394.         end
  395.         while zPos > z do
  396.             if robot.forward() then
  397.                 zPos = zPos - 1
  398.             elseif robot.swing() then
  399.                 collect()
  400.             else
  401.                 os.sleep( 0.5 )
  402.             end
  403.         end
  404.     elseif zPos < z then
  405.         while zDir ~= 1 do
  406.             turnLeft()
  407.         end
  408.         while zPos < z do
  409.             if robot.forward() then
  410.                 zPos = zPos + 1
  411.             elseif robot.swing() then
  412.                 collect()
  413.             else
  414.                 os.sleep( 0.5 )
  415.             end
  416.         end
  417.     end
  418.    
  419.     if depth<y then
  420.         if robot.down() then
  421.             depth = depth + 1
  422.         elseif robot.swingDown() then
  423.             collect()
  424.         else
  425.             sleep( 0.5 )
  426.         end
  427.     end
  428.    
  429.    
  430.     while depth > y do
  431.         if robot.up() then
  432.             depth = depth - 1
  433.         elseif robot.swingUp() then
  434.             collect()
  435.         else
  436.             sleep( 0.5 )
  437.         end
  438.     end
  439.    
  440.     while zDir ~= zd or xDir ~= xd do
  441.         turnLeft()
  442.     end
  443. end
  444.  
  445. ----------------------MAIN-----------------------------
  446.  
  447. robot.select(1)
  448.  
  449. --Check for adequate energy, tools, and supplies
  450.  
  451. getSupplies()
  452. requiredEnergy(2)
  453. print(reTool())
  454.  
  455. ---Use and optional starting position
  456. if #tArgs==6 then
  457.     print("6 arguments preparing to move")
  458.     local x, y, z, xd, zd = tonumber(tArgs[2]), tonumber(tArgs[3]), tonumber(tArgs[4]), tonumber(tArgs[5]), tonumber(tArgs[6])
  459.     goTo(x, y, z, xd, zd)
  460.     xPos, depth, zPos, xDir, zDir = x, y, z, xd, zd
  461. end
  462.  
  463. print( "Excavating..." )
  464.  
  465. local alternate = 0
  466. local done = false
  467. while not done do
  468.     for n=1,size do
  469.         for m=1,size-1 do
  470.             print('n: '..n, 'm: '..m)
  471.             if (n==1 or n==size) then
  472.                 print("Im in an outside row..."..n)
  473.                 --- turn to the outside and detect and place
  474.                 --if n is odd turn left
  475.                 --else turn right
  476.                 if n ==1 then
  477.                     sealOutside("left")
  478.                 elseif math.fmod(n + alternate,2) == 0 then
  479.                     sealOutside("left")
  480.                 else
  481.                     sealOutside("right")
  482.                 end
  483.             end
  484.             if not tryForwards() then
  485.                 done = true
  486.                 break
  487.             end
  488.             if m==size-1  then
  489.                 checkPlace('f')
  490.             end
  491.         end
  492.         if done then
  493.             break
  494.         end
  495.  
  496.         if n<size then
  497.             if math.fmod(n + alternate,2) == 0 then
  498.                 turnLeft()
  499.                 if not tryForwards() then
  500.                     done = true
  501.                     break
  502.                 end
  503.                 turnLeft()
  504.             else
  505.                 turnRight()
  506.                 if not tryForwards() then
  507.                     done = true
  508.                     break
  509.                 end
  510.                 turnRight()
  511.             end
  512.         end
  513.     end
  514.     if done then
  515.         break
  516.     end
  517.    
  518.     if size > 1 then
  519.         if math.fmod(size,2) == 0 then
  520.             turnRight()
  521.         else
  522.             if alternate == 0 then
  523.                 turnLeft()
  524.             else
  525.                 turnRight()
  526.             end
  527.             alternate = 1 - alternate
  528.         end
  529.     end
  530.    
  531.     if not tryDown() then
  532.         done = true
  533.         break
  534.     end
  535. end
  536.  
  537. print( "Returning to surface... from "..xPos, depth, zPos, xDir, zDir )
  538.  
  539. -- Return to where we started
  540. goTo( 0,0,0,0,-1 )
  541. unload( false )
  542. goTo( 0,0,0,0,1 )
  543.  
  544. print( "Mined "..(collected + unloaded).." items total." )
  545. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement