Advertisement
NanoBob

glasses power

Sep 26th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local glasses = peripheral.wrap("openperipheral_bridge_0")
  2. local lastPower = 0
  3.  
  4. local bounds = {
  5.     low = 0.1,
  6.     high = 0.9,
  7. }
  8.  
  9. local cells = {
  10.     peripheral.wrap("powered_tile_0"),
  11.     peripheral.wrap("powered_tile_1"),
  12.     peripheral.wrap("powered_tile_2"),
  13.     peripheral.wrap("powered_tile_3"),
  14.     peripheral.wrap("powered_tile_4"),
  15.     peripheral.wrap("powered_tile_5"),
  16.     peripheral.wrap("powered_tile_6"),
  17.     peripheral.wrap("powered_tile_7"),
  18.     peripheral.wrap("powered_tile_8"),
  19.     peripheral.wrap("powered_tile_9"),
  20. }
  21.  
  22. local tesseracts = {
  23.     peripheral.wrap("powered_tile_10"),
  24. }
  25.  
  26. function setGenerationEnabled(bool)
  27.     for _,tesseract in ipairs(tesseracts) do
  28.         if bool == true then
  29.             tesseract.setRedstoneControl("low")
  30.         else
  31.             tesseract.setRedstoneControl("high")
  32.         end
  33.     end
  34. end
  35.  
  36. function getGenerationEnabled()
  37.     return tesseracts[1].getRedstoneControl() == "low"
  38. end
  39.  
  40. function getPowerStored()
  41.     local stored = 0
  42.     for _,cell in ipairs(cells) do
  43.         stored = stored + cell.getEnergyStored()
  44.     end
  45.     return stored
  46. end
  47.  
  48. function getMaxPowerStored()
  49.     local max = 0
  50.     for _,cell in ipairs(cells) do
  51.         max = max + cell.getMaxEnergyStored()
  52.     end
  53.     return max
  54. end
  55.  
  56. function getMinPowerStored()
  57.     local min = 1e309
  58.     local minxMax
  59.     for _,cell in ipairs(cells) do
  60.         local stored = cell.getEnergyStored()
  61.         if stored < min then
  62.             min = stored
  63.             minMax = cell.getMaxEnergyStored()
  64.         end
  65.     end
  66.     return min / minxMax
  67. end
  68.  
  69. function handleGeneration()
  70.     local power = getPowerStored()
  71.     lastPower = power
  72.     local maxPower = getMaxPowerStored()
  73.     local filled = getMinPowerStored()
  74.     if filled < bounds.low then
  75.         setGenerationEnabled(true)
  76.     elseif filled > bounds.high then
  77.         setGenerationEnabled(false)
  78.     end
  79. end
  80.  
  81. function handleDisplay()
  82.     local power = getPowerStored()
  83.     local maxPower = getMaxPowerStored()
  84.     local filled = power / maxPower
  85.     glasses.clear()
  86.     local g = filled * 255
  87.     local r = 255 - g
  88.     local color = tonumber("0x" .. num2hex(r) .. num2hex(g) .. "00")
  89.     glasses.addBox(700,0,250,100,0xAAAAAA,0.2)
  90.     glasses.addBox(710,10,230,80,0xBBBBBB,0.2)
  91.     glasses.addBox(710,10,230 * filled,80,color,0.2)
  92.     glasses.addText(715,15,math.floor(power / 1e6) .." / " .. math.floor(maxPower / 1e6) .." MRF", 0xFFFFFF)
  93.     glasses.addText(715,25,( math.floor(power - lastPower) / 20 ) .." RF / tick", 0xFFFFFF)
  94.     glasses.addText(715,35,"Generating: ", 0xFFFFFF)
  95.     local color = 0xff0000
  96.     local generating = getGenerationEnabled()
  97.     if generating then
  98.         color = 0x00ff00
  99.     end
  100.     glasses.addText(715,45,tostring( generating ), color)
  101.     glasses.sync()
  102. end
  103.  
  104. function num2hex(num)
  105.     local hexstr = '0123456789abcdef'
  106.     local s = ''
  107.     while num > 0 do
  108.         local mod = math.fmod(num, 16)
  109.         s = string.sub(hexstr, mod+1, mod+1) .. s
  110.         num = math.floor(num / 16)
  111.     end
  112.     if s == '' then s = '0' end
  113.     return s
  114. end
  115.  
  116. while true do
  117.     handleDisplay()
  118.     handleGeneration()
  119.     sleep(1)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement