Advertisement
Guest User

Untitled

a guest
May 7th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. --- Find any connected redstone energy cells.
  2. -- @return the number of newly found energy cells
  3. --
  4. function updateConnectedRedstoneEnergyCells(cells)
  5.     local count = 0
  6.     local found = {}
  7.  
  8.     for _, name in pairs(peripheral.getNames()) do
  9.         if peripheral.getType(name) == "redstone_energy_cell" then
  10.             local handle = peripheral.wrap(name)
  11.             found[name] = true
  12.  
  13.             if cells[name] == nil then
  14.                 cells[name] = handle
  15.                 count = count + 1
  16.             end
  17.         end
  18.     end
  19.  
  20.     -- remove cells that have been removed from the system
  21.     for name, _ in pairs(cells) do
  22.         if found[name] == nil then
  23.             cells[name] = nil
  24.         end
  25.     end
  26.  
  27.     return count
  28. end
  29.  
  30. --- Get the number of connected redstone energy cells
  31. --
  32. function numConnectedRedstoneEnergyCells(cells)
  33.     local count = 0
  34.  
  35.     for _, cell in pairs(cells) do
  36.         count = count + 1
  37.     end
  38.  
  39.     return count
  40. end
  41.  
  42. --- Get the total amount of energy stored in the system
  43. -- @return int the amount of energy stored
  44. function getEnergyStored(cells)
  45.     local energy = 0
  46.  
  47.     for _, cell in pairs(cells) do
  48.         energy = energy + cell.getEnergyStored()
  49.     end
  50.  
  51.     return energy
  52. end
  53.  
  54. --- Get the max amount of energy that can be stored in the system
  55. -- @return int the max amount of energy stored
  56. function getMaxEnergyStored(cells)
  57.     local energy = 0
  58.  
  59.     for _, cell in pairs(cells) do
  60.         energy = energy + cell.getMaxEnergyStored()
  61.     end
  62.  
  63.     return energy
  64. end
  65.  
  66. --- Get the percentage of energy stored in the system as a float ( 0 <= x <= 100 )
  67. -- @return float the percentage of energy stored in the system ( 0 <= x <= 100 )
  68. function getPercentEnergyStored(cells)
  69.     local energy = getEnergyStored(cells)
  70.     local maxEnergy = getMaxEnergyStored(cells)
  71.  
  72.     if energy == 0 then
  73.         return 0
  74.     end
  75.  
  76.     return round((energy / maxEnergy) * 100, 2)
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement