Advertisement
serafim7

вывод графика энергии mfsu на монитор [OpenComputers]

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