Advertisement
joebodo

grinder.lua

Apr 24th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. local f, err = loadfile('apis.lua')
  2. if not f then
  3.   error(err)
  4. end
  5. setfenv(f, getfenv(1))
  6. f()
  7.  
  8. local alveary = Peripheral.wrap({ side = 'top' })
  9. Logger.filter('event')
  10. Peripheral.wrap("wireless_modem")
  11. --Message.enableWirelessLogging()
  12.  
  13. Relay.find()
  14.  
  15. local status
  16. local queenCount = 0
  17. local beeResources = {
  18.   { name = "Ender Pearl",
  19.     bee = 'Enderman',
  20.     level = 128,
  21.     qty = 128,
  22.     slot = 1 },
  23.   { name = "Bone",
  24.     bee = 'Skeleton',
  25.     level = 256,
  26.     qty = 256,
  27.     slot = 2 },
  28.   { name = "Blaze Rod",
  29.     bee = 'Blaze',
  30.     level = 128,
  31.     qty = 128,
  32.     slot = 3 },
  33.   { name = 'Slimeball',
  34.     bee = 'Slime',
  35.     level = 256,
  36.     qty = 256,
  37.     slot = 4 },
  38.   { name = 'Cooked Chicken',
  39.     bee = 'Chicken',
  40.     level = 128,
  41.     qty = 128,
  42.     slot = 5 },
  43.   { name = 'Ghast Tear',
  44.     bee = 'Ghast',
  45.     level = 32,
  46.     qty = 32,
  47.     slot = 6 },
  48. }
  49.  
  50. function insertQueen(res)
  51.   -- check if we are currently producing something
  52.   for _,v in pairs(beeResources) do
  53.     if turtle.getItemCount(v.slot) == 0 then
  54.        return
  55.     end
  56.   end
  57.  
  58.   Logger.debug("inserting %s from slot %d:",  res.bee, res.slot)
  59.   turtle.select(res.slot)
  60.   turtle.dropUp(1)
  61.   --alveary.pullItem("west", slot+1, 1)
  62. end
  63.  
  64. --Event.addHandler('heartbeat', function()
  65. function heartbeat()
  66.   while true do
  67.     updateStatus()
  68.     Relay.send('getResourceCounts', beeResources)
  69.     os.sleep(15)
  70.   end
  71. end
  72.  
  73. Message.addHandler('resourceCounts', function(h, id, msg)
  74.   local resources = msg.contents
  75. print('got counts')
  76.   for _, res in pairs(resources) do
  77.     for _,beeResource in pairs(beeResources) do
  78.       if res.name == beeResource.name then
  79.         beeResource.qty = res.qty
  80.         break
  81.       end
  82.     end
  83.   end
  84.  
  85.   local producing = false
  86.   status = nil
  87.   for _, res in ipairs(beeResources) do
  88.     if turtle.getItemCount(res.slot) == 0 then
  89.       if res.qty >= res.level then
  90.         turtle.select(res.slot)
  91.         turtle.suckUp()
  92.         Logger.debug('removing ' .. res.name)
  93.         status = ''
  94.       else
  95.         status = "Producing " .. res.name .. " " .. res.qty .. " of " .. res.level
  96.         producing = true
  97.       end
  98.       break
  99.     end
  100.   end
  101.  
  102.   if not producing then
  103.     for key, res in ipairs(beeResources) do
  104.       if res.qty < res.level then
  105.         status = "Producing " .. res.name .. " " .. res.qty .. " of " .. res.level
  106.         insertQueen(res)
  107.         Logger.debug(status)
  108.         producing = true
  109.         break
  110.       end
  111.     end
  112.   end
  113.   Relay.send('panelUpdate', {
  114.     uid = 'grinder',
  115.     text = 'Grinder',
  116.     type = 'radio',
  117.     active = producing,
  118.   })
  119.   local tankInfo = alveary.getTankInfo('unknown')[1]
  120.   tankInfo.amount = tankInfo.amount or 0
  121.   local enable = tankInfo.amount >= tankInfo.capacity
  122.   rs.setOutput('left', enable)
  123.   print('setting rs to ' .. tostring(enable))
  124. end)
  125.  
  126. function updateStatus()
  127.   if status then
  128.     Logger.debug(status)
  129.     Relay.send("status", status)
  130.     Relay.send('panelUpdate', {
  131.       uid = 'grinder',
  132.       type = 'message',
  133.       message = status,
  134.     })
  135.   end
  136. end
  137.  
  138. parallel.waitForAny(Event.pullEvents, heartbeat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement