lokin135

Reactor_Status

May 19th, 2018
443
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local com = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local gpu = com.gpu
  5.  
  6. local proxyReactors
  7.  
  8. local ResX, ResY = gpu.getResolution()
  9. local color = {
  10.  ["StartForeground"] = gpu.getForeground(), -- цвета для завершения проги
  11.  ["StartBackground"] = gpu.getBackground(), -- цвета для завершения проги
  12.  ["background"] = 0x161a1e, -- цвет фона
  13.  ["UpBar"] = 0x2a323b,      -- цвет фона вверхней полосы (бар)
  14.  ["foreground"] = 0xf5f5f5, -- цвет текста
  15.  ["reactorON"] = 0x00DD00,  -- цвет текста состояния реактора.
  16.  ["reactorOFF"] = 0xFF0000, -- цвет текста состояния реактора.
  17.  ["reactorNIL"] = 0x5c5c5c, -- цвет состояния, если реактор не найден
  18.  ["lastColor"] = 0x000000,  -- штука для хоть какойнить оптимизации
  19.  ["warningHeat"] = 0xffba00,
  20.  ["normalHeat"] = 0x66cc00,
  21.  ["backgroundReactorIcon"] = { -- таакс... а это цвета для выделения блоков. что бы не сливались
  22.   ["background_1"] = 0x777777,
  23.   ["background_2"] = 0x878787,
  24.   ["background_nil_1"] = 0x393939,
  25.   ["background_nil_2"] = 0x262626
  26.  },
  27.  ["ByColors"] = {0xffffff,0x66ff00,0xe0e0e0,0x0060ff,0xc2c2c2,0xff0000,0x999999}
  28. }
  29. local addresses = {
  30.  --1      2      3      4      5      6      7      8      9         реактор
  31.  "2e6"
  32. }
  33.  
  34. local statusReactors = {}
  35.  
  36. local IDColorBy = 1
  37.  
  38. local ExitStatus = false
  39.  
  40. local function DrawBy()
  41.  local by = "by lokin135"
  42.  gpu.setBackground(color.UpBar)
  43.  gpu.setForeground(color.ByColors[IDColorBy])
  44.  lastColor = color.ByColors[IDColorBy]
  45.  gpu.set(ResX-#by+1, 1, by)
  46.  if IDColorBy+1 <= #color.ByColors then IDColorBy = IDColorBy + 1 else IDColorBy = 1 end
  47. end
  48.  
  49. local function GetComponentsReactors(addressesComponents)
  50.  local proxys = {}
  51.  for i = 1, #addressesComponents do
  52.   proxys[i] = com.proxy(addressesComponents[i]) or "?"
  53.  end
  54.  return proxys
  55. end
  56.  
  57. local function GetStatusReactors(reactors)
  58.  local result = {}
  59.  for i = 1, #reactors do
  60.   if com.get(reactors[i].address) ~= nil then
  61.     result[i] = {
  62.      math.floor(reactors[i].getReactorEUOutput()),
  63.      reactors[i].getHeat()/reactors[i].getMaxHeat()*100
  64.     }
  65.   end
  66.  end
  67.  return result
  68. end
  69.  
  70. local function GetColorBackgroundReactor(ID,color1,color2)
  71.  return ID%2==0 and color1 or color2
  72. end
  73.  
  74. local function GetColorStatusReactor(ID,color1,color2)
  75.  return statusReactors[ID][1]>0 and color1 or color2
  76. end
  77.  
  78. local function ChangeForegroundColor(color)
  79.  if lastColor ~= color then
  80.   gpu.setForeground(color); lastColor = color
  81.  end
  82. end
  83.  
  84. local function DrawBarsAndOther()
  85.  gpu.setBackground(color.background)
  86.  gpu.setForeground(color.foreground)
  87.  term.clear()
  88.  
  89.  gpu.setBackground(color.UpBar)
  90.  gpu.fill(1,1,160,3," ")
  91.  gpu.fill(5,1,1,50," ")
  92.  gpu.set(1,1,"X")
  93.  for i=1, 6 do
  94.   local XPos = 4+(i-1)*24+i
  95.   gpu.set((XPos*2+26)/2-3,2, "Блок "..i)
  96.   gpu.fill(30+((i-1)*25),3,1,48," ")
  97.  end
  98. end
  99.  
  100. local function DrawStatusReactors(STS)
  101.  local IDReactor = 1
  102.  local IBlocks = 1
  103.  for address=1, #addresses do
  104.   local EUOutput, HeatReactor = "?"
  105.   local StartX = 6+(IBlocks-1)*24+IBlocks
  106.   local StartY = 5+(IDReactor-1)*5
  107.   local EndX = 22
  108.   local EndY = 5
  109.   local positionIcon = {StartX, StartY, EndX, EndY}
  110.   local PositionText = {
  111.    (positionIcon[1]*2+positionIcon[3])/2,
  112.    (positionIcon[2]*2+positionIcon[4])/2
  113.   }
  114.  
  115.   if STS[address] ~= nil then
  116.    EUOutput =    tostring(STS[address][1]).." EU/t"
  117.    HeatReactor = tostring(STS[address][2]).."% heat"
  118.    gpu.setBackground(GetColorBackgroundReactor(IDReactor,color.backgroundReactorIcon.background_1,color.backgroundReactorIcon.background_2))
  119.    ChangeForegroundColor(GetColorStatusReactor(address,color.reactorON,color.reactorOFF))
  120.   else
  121.    ChangeForegroundColor(color.reactorNIL)
  122.    gpu.setBackground(GetColorBackgroundReactor(IDReactor,color.backgroundReactorIcon.background_nil_1,color.backgroundReactorIcon.background_nil_2))
  123.   end
  124.   gpu.fill(positionIcon[1],positionIcon[2],positionIcon[3],positionIcon[4]," ")
  125.   gpu.set(PositionText[1]-5,PositionText[2],"Реактор N"..IDReactor)
  126.   gpu.set(StartX,PositionText[2]+2,EUOutput)
  127.  
  128.   ChangeForegroundColor(STS[address][2]>=50 and color.warningHeat or color.normalHeat)
  129.   gpu.set(StartX+EndX-#HeatReactor,PositionText[2]-2,HeatReactor)
  130.   if IDReactor+1 <= 9 then IDReactor = IDReactor + 1 else IDReactor = 1; IBlocks = IBlocks + 1 end
  131.  end
  132. end
  133.  
  134. local function HandlerTouch()
  135.  local _, _, x, y = event.pull(1,"touch")
  136.  ExitStatus = x == 1 and y == 1
  137. end
  138.  
  139. local function Init()
  140.  for i=1, #addresses do addresses[i] = com.get(addresses[i]) or "?" end
  141.  proxyReactors = GetComponentsReactors(addresses)
  142.  DrawBarsAndOther()
  143. end
  144.  
  145. local function Main()
  146.  while not ExitStatus do
  147.   statusReactors = GetStatusReactors(proxyReactors)
  148.   DrawStatusReactors(statusReactors)
  149.   DrawBy() -- и это тоже не трогай, иначе будет плохо...
  150.   HandlerTouch()
  151.  end
  152.  gpu.setBackground(color.StartBackground)
  153.  gpu.setForeground(color.StartForeground)
  154.  term.clear()
  155. end
  156.  
  157. Init()
  158. Main()
RAW Paste Data