Advertisement
paulbatum

pbrobot.lua

Jan 29th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. local os = require("os")
  2. local component = require("component")
  3. local sides = require("sides")
  4. local computer = require("computer")
  5. local robot = require("robot")
  6. --local _ = require('moses')
  7.  
  8. -------------------------------------
  9. local lastRobotSlot = 16
  10. local rangeThreshold = 0.9
  11. local minY = 18
  12. local homeY = 50
  13. local lowEnergy = 5000
  14. local robotSide = sides.back
  15. local enderChestName = "EnderStorage:enderChest"
  16.  
  17. local inv = component.inventory_controller
  18. local gen = component.generator
  19. local nav = component.navigation
  20.  
  21. if not inv then
  22.     error("no inventory controller found")
  23. end
  24.  
  25. if not gen then
  26.     error("no generator found")
  27. end
  28.  
  29. if not nav then
  30.     error("no navigation controller found")    
  31. end
  32.  
  33. local pbrobot = {}
  34. local workQueue = {}
  35. local commands = {
  36.     mine = robot.swing,
  37.     mineForward = function()
  38.         robot.swing()
  39.         return robot.forward()
  40.     end,
  41.     mineDown = function()
  42.         robot.swingDown()
  43.         return robot.down()
  44.     end,
  45.     turnLeft = robot.turnLeft,
  46.     turnRight = robot.turnRight,
  47. }
  48.  
  49. function pbrobot.queueLayers(layerSize, count)
  50.     for i=1,count do
  51.         pbrobot.queueMineLayerWork(layerSize)
  52.         pbrobot.queueWork("mineDown", 3)        
  53.     end    
  54. end
  55.  
  56. function pbrobot.queueLayersFine(layerSize, count)
  57.     for i=1,count do
  58.         pbrobot.queueMineLayerWorkFine(layerSize)
  59.         pbrobot.queueWork("mineDown", 1)        
  60.     end    
  61. end
  62.    
  63.  
  64. function pbrobot.queueMineLayerWork(layerSize)    
  65.  
  66.     iterationRight = function()
  67.         pbrobot.queueWork("mineForward", layerSize-1)
  68.         pbrobot.queueWork("turnRight")
  69.         pbrobot.queueWork("mineForward", 3)
  70.         pbrobot.queueWork("turnRight")
  71.     end
  72.  
  73.     iterationLeft = function()
  74.         pbrobot.queueWork("mineForward", layerSize-1)
  75.         pbrobot.queueWork("turnLeft")
  76.         pbrobot.queueWork("mineForward", 3)
  77.         pbrobot.queueWork("turnLeft")
  78.     end
  79.  
  80.     for i=1,math.floor(layerSize/8) + 1 do
  81.         iterationRight()
  82.         iterationLeft()    
  83.     end    
  84.    
  85.     pbrobot.queueWork("turnLeft")
  86.     pbrobot.queueWork("mineForward", layerSize+2)
  87.     pbrobot.queueWork("turnRight")
  88. end
  89.  
  90. function pbrobot.queueMineLayerWorkFine(layerSize)    
  91.  
  92.     iterationRight = function()
  93.         pbrobot.queueWork("mineForward", layerSize-1)
  94.         pbrobot.queueWork("turnRight")
  95.         pbrobot.queueWork("mineForward")
  96.         pbrobot.queueWork("turnRight")
  97.     end
  98.  
  99.     iterationLeft = function()
  100.         pbrobot.queueWork("mineForward", layerSize-1)
  101.         pbrobot.queueWork("turnLeft")
  102.         pbrobot.queueWork("mineForward", 1)
  103.         pbrobot.queueWork("turnLeft")
  104.     end
  105.  
  106.     for i=1,math.floor(layerSize/2) + 1 do
  107.         iterationRight()
  108.         iterationLeft()    
  109.     end    
  110.    
  111.     pbrobot.queueWork("turnLeft")
  112.     pbrobot.queueWork("mineForward", layerSize)
  113.     pbrobot.queueWork("turnRight")
  114. end
  115.  
  116. function pbrobot.queueWork(command, iterations)
  117.     local work = {
  118.         command = command,
  119.         iterations = iterations or 1,  
  120.         retry = 10
  121.     }
  122.     work.execute = function()
  123.         if commands[work.command]() then
  124.             work.iterations = work.iterations - 1
  125.         else
  126.             work.retry = work.retry - 1
  127.             if work.retry == 0 then
  128.                 work.iterations = 0
  129.             end
  130.         end
  131.     end
  132.     table.insert(workQueue, work)
  133. end
  134.  
  135. function pbrobot.getWork()
  136.     return function()
  137.         local work = workQueue[1]
  138.         while work do
  139.             if work.iterations > 0 then    
  140.                 return work.execute
  141.             else
  142.                 table.remove(workQueue, 1)    
  143.                 work = workQueue[1]
  144.             end
  145.         end
  146.         return nil
  147.     end
  148. end
  149.  
  150. function pbrobot.doWork()
  151.     local cycle = 0
  152.     for work in pbrobot.getWork() do        
  153.         work()
  154.         cycle = cycle + 1
  155.         if cycle == 10 then
  156.             print('maintenance check..')
  157.             cycle = 0
  158.             if pbrobot.isFull() or pbrobot.isGeneratorEmpty() then                
  159.                 pbrobot.unloadAndRefuel()
  160.             end
  161.             if pbrobot.isEnergyLow() or pbrobot.tooFarFromHome() then
  162.                 pbrobot.goHome()
  163.                 break
  164.             end
  165.         end        
  166.         os.sleep(0.1)
  167.     end
  168. end
  169.  
  170. function pbrobot.goHome()
  171.     print("going home")    
  172.     local x,y,z = nav.getPosition()
  173.     while y < homeY do
  174.     x,y,z = nav.getPosition()
  175.         robot.swingUp()
  176.         robot.up()
  177.     end
  178. end
  179.  
  180. function pbrobot.tooFarFromHome()
  181.     local range = nav.getRange()
  182.     local x,y,z = nav.getPosition()
  183.     local max = math.max(math.abs(x), math.abs(z))
  184.     return (max > range * rangeThreshold) or (y < minY)
  185. end
  186.  
  187. function pbrobot.isEnergyLow()
  188.     return computer.energy() < lowEnergy
  189. end
  190.  
  191. function pbrobot.isGeneratorEmpty()
  192.     return gen.count() < 1
  193. end
  194.  
  195. function pbrobot.isFull()
  196.     return pbrobot.findEmptySlot(robotSide) == -1
  197. end
  198.  
  199. function pbrobot.findEnder()
  200.     for slot in pbrobot.ownSlots() do
  201.         if slot.name == enderChestName then
  202.             return slot.index
  203.         end
  204.     end
  205. end
  206.  
  207. function pbrobot.placeEnder()
  208.     local enderSlot = pbrobot.findEnder()
  209.     if enderSlot then
  210.         print("found ender chest in slot "..enderSlot)
  211.         robot.select(enderSlot)
  212.         return robot.place()
  213.     else
  214.         error("no ender chest found")
  215.     end
  216. end
  217.  
  218. function pbrobot.unloadAndRefuel()
  219.     robot.swing()
  220.     if pbrobot.placeEnder() then        
  221.         pbrobot.fillInventory(sides.front)    
  222.         pbrobot.refuel(sides.front)
  223.         robot.select(1)    
  224.         robot.swing()
  225.     end    
  226.     os.sleep(1)
  227. end
  228.  
  229. function pbrobot.refuel(side)    
  230.     local emptySlot = pbrobot.findEmptySlot(robotSide)
  231.     robot.select(emptySlot)
  232.  
  233.     local coalIndex = pbrobot.findItem(side, "minecraft:coal")
  234.     if coalIndex >= 1 then
  235.         inv.suckFromSlot(side, coalIndex)
  236.         gen.insert()
  237.     else
  238.         print("no coal found")
  239.     end
  240. end
  241.  
  242. function pbrobot.fillInventory(side)    
  243.     for robotSlot in pbrobot.ownSlots() do
  244.         if not robotSlot.empty then            
  245.             local targetSlot = pbrobot.findEmptySlot(side)
  246.             if targetSlot >= 1 then
  247.                 robot.select(robotSlot.index)
  248.                 inv.dropIntoSlot(side, targetSlot)
  249.             end
  250.         end
  251.     end
  252. end
  253.  
  254. function pbrobot.findItem(side, name)
  255.     for slot in pbrobot.slots(side) do
  256.         if slot.name == name then
  257.             return slot.index
  258.         end
  259.     end
  260.     return -1
  261. end
  262.  
  263. function pbrobot.findEmptySlot(side)
  264.     for slot in pbrobot.slots(side) do
  265.         if slot.empty then
  266.             return slot.index
  267.         end
  268.     end
  269.     return -1
  270. end
  271.  
  272. function pbrobot.ownSlots()
  273.     return pbrobot.slots(robotSide)
  274. end
  275.  
  276. function pbrobot.slots(side)
  277.     local currentSlot = 0
  278.     local lastSlot = 0
  279.    
  280.     if side == robotSide then
  281.         lastSlot = lastRobotSlot
  282.     else
  283.         lastSlot = inv.getInventorySize(side)
  284.     end
  285.  
  286.     return function()
  287.         currentSlot = currentSlot + 1
  288.         if currentSlot > lastSlot then
  289.             return nil
  290.         end
  291.  
  292.         local result = {
  293.             index = currentSlot,
  294.             contents = inv.getStackInSlot(side, currentSlot)
  295.         }        
  296.         result.empty = result.contents == nil
  297.        
  298.         if result.contents then
  299.             result.name = result.contents.name
  300.         end
  301.        
  302.         return result
  303.     end
  304. end
  305.  
  306.  
  307. --pbrobot.queueLayers(2)
  308. --pbrobot.doWork()
  309.  
  310. return pbrobot
  311. -- pastebin run XNAQz7Ee
  312. -- pastebin get -f XNAQz7Ee pbrobot.lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement