Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ opencomputers Вывод графика энергии mfsu на монитор
- автор: lokin135
- http://computercraft.ru/topic/2345-vyvod-grafika-energii-na-monitor/#entry34503
- https://image.ibb.co/bRUYdJ/image_2018_05_27_20_19_24.png
- --]]
- local com = require('component')
- local term = require("term")
- local gpu = com.gpu
- local ResX, ResY = 100, 50 -- 80, 25
- local TimeSleep = 2
- local BarY = 3/1.5
- local lastX = -1
- local drawBar = true
- local nameComponent = "mfsu"
- local Output
- local color = {
- ["background"] = 0x161a1e,
- ["foreground"] = 0xf5f5f5,
- ["Schedule"] = {
- ["Up"] = {0x27a327,0x145214}, -- 1 значение - верхний пиксель графика, 2 - "тень"
- ["Down"] = {0xb32821,0x591410},
- ["WithoutChanges"] = {0x007dff,0x004a99}
- }
- }
- local function getAllComponents(nameComponent)
- local listCom = {}
- for address in com.list(nameComponent) do
- listCom[#listCom+1] = com.proxy(address)
- end
- return listCom
- end
- local function getAllCapacity(listComponents)
- local totalCapacity = 0
- for i=1,#listComponents do
- totalCapacity = totalCapacity + listComponents[i].getCapacity()
- end
- return totalCapacity
- end
- local function getAllEnergy(listComponents, Capacity)
- local AllEU = 0
- for i=1, #listComponents do
- AllEU = AllEU + listComponents[i].getStored()
- end
- return AllEU>Capacity and Capacity or AllEU -- да-да, при тестированнии произошло невозможное: энергии было больше, чем могло вместиться. 0_0
- end
- local function calculateSchedulePoint(lastPoint, energy, capacity)
- local differenceEU = energy-lastPoint.EU
- local Point = {}
- Point.EU = energy
- Point.Y = math.ceil((energy / capacity) * ((ResY-BarY) * 2))
- -- по сути, можно было бы это условие заменить логическим вырожением. но что-то пошло не так, и оно не работало как нужно. доконца не понимаю их, по этому юзаю не всегда где можно
- if math.abs(differenceEU) > Output then
- Point.color = differenceEU > Output and color.Schedule.Up or color.Schedule.Down
- else
- Point.color = color.Schedule.WithoutChanges
- end
- return Point
- end
- local function drawPixel(x,y)
- local posPixel = y*2 % 2 ~= 0
- if posPixel then gpu.setBackground(color.background) end
- gpu.set(x,y, posPixel and "▄" or "▀")
- end
- local function setBackAndForeGround(ColorBackground, ColorForeground)
- gpu.setBackground(ColorBackground)
- gpu.setForeground(ColorForeground)
- end
- local function drawSchedulePoint(Point,lastPoint)
- local differenceY = Point.Y - lastPoint.Y
- Point.Y = Point.Y / 2
- if lastX+2 > ResX then -- это условие нужно ставить в конец функции, но я перенес в начало из-за пустого столба в конце графика
- gpu.copy(1,BarY,ResX,ResY,-1,0)
- gpu.setBackground(color.background)
- gpu.fill(ResX,BarY,1,ResY," ")
- else
- lastX = lastX + 1
- end
- setBackAndForeGround(Point.color[2], Point.color[1])
- gpu.fill(lastX+1,ResY-Point.Y+1+BarY,1,Point.Y+1," ") -- рисуем тень
- drawPixel(lastX+1,ResY-Point.Y+BarY) -- рисую "шапку"
- end
- local function drawBar(energy, capacity, growth)
- local text = "Хранится всего: "..energy.." из "..capacity.." | Прирост энергии: "..growth
- local byText = "by lokin135"
- setBackAndForeGround(color.background, color.foreground)
- gpu.fill(1,3,ResX,1,"_")
- gpu.fill(1,2,ResX,1," ")
- gpu.set(ResX/2-#text/3,2,text)
- gpu.set(ResX-#byText+1,1,byText)
- end
- local function Init()
- gpu.setResolution(ResX,ResY)
- gpu.setBackground(color.background)
- gpu.setForeground(color.foreground)
- term.clear()
- end
- local function Main()
- local CompList = getAllComponents(nameComponent)
- local Capacity = getAllCapacity(CompList)
- local lastPoint = {}
- lastPoint.color, lastPoint.Y, lastPoint.EU = color.Schedule.WithoutChanges, 1, 0
- Output = CompList[1].getOutput()
- while true do
- local EU = getAllEnergy(CompList,Capacity)
- local schedulePoint = calculateSchedulePoint(lastPoint, EU, Capacity)
- if drawBar then drawBar(EU, Capacity, EU-lastPoint.EU) end
- drawSchedulePoint(schedulePoint,lastPoint)
- lastPoint = schedulePoint
- os.sleep(TimeSleep)
- end
- end
- Init()
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement