Advertisement
joebodo

beeResources.lua

May 3rd, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. vos.loadApi('ui.api')
  2.  
  3. local alveary = Peripheral.wrap("alveary_0")
  4. Peripheral.wrap("wireless_modem")
  5.  
  6. Relay.find()
  7.  
  8. local queenCount = 0
  9. local beeResources = Util.readTable('.beeResources')
  10.  
  11. if not beeResources then
  12.   error('could not read .beeResources')
  13. end
  14.  
  15. function insertQueen(res)
  16.   -- check if we are currently producing something
  17.   for _,v in pairs(beeResources) do
  18.     if turtle.getItemCount(v.slot) == 0 or
  19.        turtle.getItemCount(v.slot + 1) == 0 then
  20.        return
  21.     end
  22.   end
  23. sleep(10)
  24.  
  25.   Logger.debug("inserting %s from slot %d:",  res.bee, res.slot)
  26.   turtle.select(res.slot)
  27.   turtle.drop(1)
  28.   --alveary.pullItem("west", slot, 1)
  29.   turtle.select(res.slot+1)
  30.   turtle.drop(1)
  31.   --alveary.pullItem("west", slot+1, 1)
  32. end
  33.  
  34. Message.addHandler('resourceCounts', function(h, id, msg)
  35.   local resources = msg.contents
  36.  
  37.   for _, res in pairs(resources) do
  38.     for _,beeResource in pairs(beeResources) do
  39.       if res.name == beeResource.name then
  40.         beeResource.qty = res.qty
  41.         break
  42.       end
  43.     end
  44.   end
  45.  
  46.   local producing
  47.   local status
  48.  
  49.   for _, res in ipairs(beeResources) do
  50.     if turtle.getItemCount(res.slot) == 0 then
  51.       status = "Producing " .. res.name .. " " .. res.qty .. " of " .. res.level
  52.       producing = true
  53.       break
  54.     end
  55.   end
  56.  
  57.   if not producing then
  58.     for key, res in ipairs(beeResources) do
  59.       if res.qty < res.level then
  60.         status = "Producing " .. res.name .. " " .. res.qty .. " of " .. res.level
  61.         insertQueen(res)
  62.         Logger.debug(status)
  63.         producing = true
  64.         break
  65.       end
  66.     end
  67.   end
  68.  
  69.   if status then
  70.     Logger.debug(status)
  71.     Relay.send('panelUpdate', {
  72.       uid = 'bees',
  73.       type = 'message',
  74.       message = status,
  75.     })
  76.   end
  77.  
  78.   Relay.send('panelUpdate', {
  79.     uid = 'alveary',
  80.     type = 'radio',
  81.     text = 'Alveary 1',
  82.     active = producing
  83.   })
  84. end)
  85.  
  86. Event.addHandler('heartbeat', function()
  87.   Relay.send('getResourceCounts', beeResources)
  88. end)
  89.  
  90. os.queueEvent('heartbeat')
  91. Event.heartbeat(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement