Advertisement
lokin135

mfsu4M

May 27th, 2018
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. local com = require('component')
  2. local term = require("term")
  3.  
  4. local gpu = com.gpu
  5.  
  6. local ResX, ResY = 100, 50
  7. local TimeSleep = 2
  8.  
  9. local BarY = 3/1.5
  10. local lastX = -1
  11.  
  12. local drawBar = true
  13. local nameComponent = "mfsu"
  14.  
  15. local Output
  16.  
  17. local color = {
  18.  ["background"] = 0x161a1e,
  19.  ["foreground"] = 0xf5f5f5,
  20.  ["Schedule"] = {
  21.   ["Up"] = {0x27a327,0x145214}, -- 1 значение - верхний пиксель графика, 2 - "тень"
  22.   ["Down"] = {0xb32821,0x591410},
  23.   ["WithoutChanges"] = {0x007dff,0x004a99}
  24.  }
  25. }
  26.  
  27. local function getAllComponents(nameComponent)
  28.  local listCom = {}
  29.  for address in com.list(nameComponent) do
  30.   listCom[#listCom+1] = com.proxy(address)
  31.  end
  32.  return listCom
  33. end
  34.  
  35. local function getAllCapacity(listComponents)
  36.  local totalCapacity = 0
  37.  for i=1,#listComponents do
  38.   totalCapacity = totalCapacity + listComponents[i].getCapacity()
  39.  end
  40.  return totalCapacity
  41. end
  42.  
  43. local function getAllEnergy(listComponents, Capacity)
  44.  local AllEU = 0
  45.  for i=1, #listComponents do
  46.   AllEU = AllEU + listComponents[i].getStored()
  47.  end
  48.  return AllEU>Capacity and Capacity or AllEU -- да-да, при тестированнии произошло невозможное: энергии было больше, чем могло вместиться. 0_0
  49. end
  50.  
  51. local function calculateSchedulePoint(lastPoint, energy, capacity)
  52.  local differenceEU = energy-lastPoint.EU
  53.  local Point = {}
  54.  Point.EU = energy
  55.  Point.Y = math.ceil((energy / capacity) * ((ResY-BarY) * 2))
  56.  -- по сути, можно было бы это условие заменить логическим вырожением. но что-то пошло не так, и оно не работало как нужно. доконца не понимаю их, по этому юзаю не всегда где можно
  57.  if math.abs(differenceEU) > Output then
  58.   Point.color = differenceEU > Output and color.Schedule.Up or color.Schedule.Down
  59.  else
  60.   Point.color = color.Schedule.WithoutChanges
  61.  end
  62.  return Point
  63. end
  64.  
  65. local function drawPixel(x,y)
  66.  local posPixel = y*2 % 2 ~= 0
  67.  if posPixel then gpu.setBackground(color.background) end
  68.  gpu.set(x,y, posPixel and "▄" or "▀")
  69. end
  70.  
  71. local function setBackAndForeGround(ColorBackground, ColorForeground)
  72.  gpu.setBackground(ColorBackground)
  73.  gpu.setForeground(ColorForeground)
  74. end
  75.  
  76. local function drawSchedulePoint(Point,lastPoint)
  77.  local differenceY = Point.Y - lastPoint.Y
  78.  Point.Y = Point.Y / 2
  79.  if lastX+2 > ResX then -- это условие нужно ставить в конец функции, но я перенес в начало из-за пустого столба в конце графика
  80.   gpu.copy(1,BarY,ResX,ResY,-1,0)
  81.   gpu.setBackground(color.background)
  82.   gpu.fill(ResX,BarY,1,ResY," ")
  83.  else
  84.   lastX = lastX + 1
  85.  end
  86.  setBackAndForeGround(Point.color[2], Point.color[1])
  87.  gpu.fill(lastX+1,ResY-Point.Y+1+BarY,1,Point.Y+1," ") -- рисуем тень
  88.  drawPixel(lastX+1,ResY-Point.Y+BarY) -- рисую "шапку"
  89. end
  90.  
  91. local function drawBar(energy, capacity, growth)
  92.  local text = "Хранится всего: "..energy.." из "..capacity.." | Прирост энергии: "..growth
  93.  local byText = "by lokin135"
  94.  setBackAndForeGround(color.background, color.foreground)
  95.  gpu.fill(1,3,ResX,1,"_")
  96.  gpu.fill(1,2,ResX,1," ")
  97.  gpu.set(ResX/2-#text/3,2,text)
  98.  gpu.set(ResX-#byText+1,1,byText)
  99. end
  100.  
  101. local function Init()
  102.  gpu.setResolution(ResX,ResY)
  103.  gpu.setBackground(color.background)
  104.  gpu.setForeground(color.foreground)
  105.  term.clear()
  106. end
  107.  
  108. local function Main()
  109.  local CompList = getAllComponents(nameComponent)
  110.  local Capacity = getAllCapacity(CompList)
  111.  local lastPoint = {}
  112.  lastPoint.color, lastPoint.Y, lastPoint.EU = color.Schedule.WithoutChanges, 1, 0
  113.  Output = CompList[1].getOutput()
  114.  while true do
  115.   local EU = getAllEnergy(CompList,Capacity)
  116.   local schedulePoint = calculateSchedulePoint(lastPoint, EU, Capacity)
  117.   if drawBar then drawBar(EU, Capacity, EU-lastPoint.EU) end
  118.   drawSchedulePoint(schedulePoint,lastPoint)
  119.   lastPoint = schedulePoint
  120.   os.sleep(TimeSleep)
  121.  end
  122. end
  123.  
  124. Init()
  125. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement