Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string.jsub(str, startIndex, endIndex)
- if (endIndex == nil) then
- endIndex = string.len(str) + 1
- end
- return string.sub(str, startIndex, endIndex - 1)
- end
- -- Checks if the given `str` starts with the given `value` (optionally start searching with the given start offset index. 1 by default). `string.startsWith("hello", "he", 1)` == `true`
- function string.startsWith(str, value, startIndex)
- if (startIndex == nil) then
- startIndex = 1
- end
- return string.jsub(str, startIndex, string.len(value) + startIndex) == value
- end
- -- Checks if the given `str` ends with the given value. string.endsWith("hello", "lo") == true
- function string.endsWith(str, value)
- local strLen = string.len(str)
- local valLen = string.len(value)
- return string.sub(str, strLen - valLen + 1, strLen) == value
- end
- function string.contains(str, value, startIndex)
- if (startIndex == nil) then
- startIndex = 1
- end
- return string.find(str, value) ~= nil
- end
- function string.indexOf(str, value, startIndex)
- if (startIndex == nil) then
- startIndex = 1
- end
- local s,e,c = string.find(str, value)
- if (s == nil) then
- return -1
- end
- return s
- end
- -- Clamps the given string to the given length. `string.len(str)` is smaller than or equal to `maxLen` then `str` is returned. Otherwise, a substring of `maxLen - 3` of characters + `"..."` is returned
- function string.clampString(str, maxLen)
- local strLen = string.len(str)
- if (strLen <= maxLen) then
- return str
- end
- return (string.sub(str, 1, maxLen - 3) .. "...")
- end
- utils = { }
- -- Calculates the center Y coordinate between `start` (inclusive) and `end` (inclusive)
- function utils.centerOf(start, ending)
- if (start == ending) then
- return start
- elseif (start < ending) then
- return math.floor((ending - start) / 2)
- else
- return math.floor((start - ending) / 2)
- end
- end
- function utils.thatOrNull(nullable, nonNull)
- if (nullable == nil) then
- return nonNull
- end
- return nullable
- end
- function utils.byteToMegaByte(bytes)
- return bytes / 1048576
- end
- function utils.byteToKiloByte(bytes)
- return bytes / 1024
- end
- function utils.byteToGigaByte(bytes)
- return bytes / 1073741824
- end
- GDI = {
- Handle = { }
- }
- -- returns a non null text colour
- function GDI.nonNullTextColour(text_colour)
- if (text_colour == nil) then
- return colours.white
- end
- return text_colour
- end
- -- returns a non null background colour
- function GDI.nonNullBackgroundColour(background_colour)
- if (background_colour == nil) then
- return colours.black
- end
- return background_colour
- end
- -- sets the monitor cursor pos
- function GDI.cpos(x, y)
- GDI.Handle.setCursorPos(x, y)
- end
- -- sets the monitor text colour
- function GDI.textColour(colour)
- GDI.Handle.setTextColor(colour)
- end
- -- sets the monitor background colour
- function GDI.backgroundColour(colour)
- GDI.Handle.setBackgroundColor(colour)
- end
- function GDI.clearBackground(foreground, background)
- GDI.textColour(GDI.nonNullTextColour(foreground))
- GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
- GDI.Handle.clear()
- end
- -- draws text on the monitor
- function GDI.drawText(x, y, text, foreground, background)
- GDI.textColour(GDI.nonNullTextColour(foreground))
- GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
- GDI.cpos(x, y)
- local txt = tostring(text)
- GDI.Handle.write(txt)
- return string.len(txt)
- end
- function GDI.drawLineH(x, y, w, background)
- GDI.drawText(x, y, string.rep(" ", w), background, background)
- end
- function GDI.drawLineV(x, y, h, background)
- GDI.textColour(GDI.nonNullTextColour(background))
- GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
- for i = y, h do
- GDI.Handle.write(" ")
- GDI.cpos(x, i)
- end
- end
- function GDI.drawSquare(x, y, w, h, background)
- local line = y
- local endLine = y + h
- while (true) do
- GDI.drawLineH(x, line, w, background)
- line = line + 1
- if (line >= endLine) then
- return
- end
- end
- end
- function GDI.drawProgressBar(x, y, w, h, min, max, val, foreground, background)
- GDI.drawSquare(x, y, w, h, background)
- GDI.drawSquare(x, y, math.floor((val / (max - min)) * w), h, foreground)
- end
- -- hello 5
- -- helo- 4
- -- -hie- 3
- -- -hi-- 2
- -- - h - 1
- -- - h -- 1
- -- - hh -- 2
- -- -- h -- 1
- function GDI.drawGroupBoxHeader(x, y, w, text, foreground, background)
- local strLen = string.len(text)
- if (strLen >= w) then
- GDI.drawText(x, y, string.sub(text, 1, w), foreground, background)
- elseif (strLen == (w - 1)) then
- GDI.drawText(x, y, text .. "-", foreground, background)
- elseif (strLen == (w - 2)) then
- GDI.drawText(x, y, "-" .. text .. "-", foreground, background)
- elseif (strLen == (w - 3)) then
- GDI.drawText(x, y, "-" .. text .. " -", foreground, background)
- else
- local leftOverSpace = strLen - w
- local halfLeftOver = leftOverSpace / 2
- if (leftOverSpace % 2 == 0) then -- even
- local glyphText = string.rep("-", halfLeftOver - 1)
- GDI.drawText(x, y, glyphText .. " " .. text .. " " .. glyphText)
- else -- odd
- local glyphTextA = string.rep("-", math.floor(halfLeftOver - 1))
- local glyphTextB = string.rep("-", math.ceil(halfLeftOver - 1))
- GDI.drawText(x, y, glyphTextA .. " " .. text .. " " .. glyphTextB)
- end
- end
- end
- function GDI.drawGroupBoxWall(x, y, h, foreground, background)
- GDI.textColour(GDI.nonNullTextColour(foreground))
- GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
- for i = y, h do
- GDI.Handle.write("-")
- GDI.cpos(x, i)
- end
- end
- function GDI.drawGroupBoxFooter(x, y, w, foreground, background)
- GDI.drawText(x, y, string.rep("-", w), foreground, background)
- end
- function GDI.drawGroupBox(x, y, w, h, header, border_foreground, border_background, background)
- GDI.drawSquare(x, y, w, h, background)
- GDI.drawGroupBoxHeader(x, y, w, header, border_foreground, border_background)
- GDI.drawGroupBoxWall(x, y, h, border_foreground, border_background)
- GDI.drawGroupBoxWall(x + w, y, h, border_foreground, border_background)
- GDI.drawGroupBoxFooter(x, y + h, w, border_foreground, border_background)
- end
- function GDI.drawWindowTitle(x, y, w, title, foreground, background)
- if (background == nil) then
- background = colours.grey
- end
- local len = GDI.drawText(x, y, string.clampString(title, w - 5), foreground, background)
- local endX = x + w
- GDI.drawLineH(x + len, y, w - len - 4, background)
- GDI.drawText(endX - 4, y, "-", colours.foreground, colours.lightGrey)
- GDI.drawText(endX - 3, y, "[]", colours.foreground, colours.lightGrey)
- GDI.drawText(endX - 1, y, "x", colours.foreground, colours.red)
- end
- function GDI.drawWindow(x, y, w, h, title, titleForeground, titleBackground, background)
- GDI.drawSquare(x, y, w, h, utils.thatOrNull(background, colours.white))
- GDI.drawWindowTitle(x, y, w, title, utils.thatOrNull(titleForeground, colours.lightGrey), utils.thatOrNull(titleBackground, colours.grey))
- end
- function GDI.clearMonitor()
- GDI.textColour(colours.white)
- GDI.backgroundColour(colours.black)
- GDI.Handle.clear()
- end
- totalMinEnergy = 0
- totalMaxEnergy = 0
- totalStoredEnergy = 0
- biggestId = 0
- peripherals = { }
- line = 1
- function getTypeAndId(name)
- local nameIndex = string.indexOf(name, "_name")
- if (nameIndex == -1) then
- return nil
- end
- local machineId = tonumber(string.sub(name, nameIndex + 5))
- if (machineId == nil) then
- return nil
- end
- return string.sub(name, 31, nameIndex - 1), machineId
- end
- Monitors = {
- Peripherals = {
- },
- Information = {
- Setup = function (me)
- -- local w,h = me.Handle.getSize()
- -- me.Width = w
- -- me.Height = h
- -- 25
- me.Handle.setTextScale(1)
- -- tile_thermalexpansion_machine_pulverizer_name_<number> = pulverizer
- -- tile_thermalexpansion_machine_smelter_name_<number> = induction
- -- tile_thermalexpansion_machine_furnace_name_<number> = furnace
- for i, name in pairs(peripheral.getNames()) do
- if (name ~= nil) then
- if (string.startsWith(name, "tile_thermalexpansion_machine_", 1)) then
- local name, id = getTypeAndId(name)
- if (name ~= nil and id ~= nil) then
- local cellHandle = peripheral.wrap(name)
- me.Peripherals[id] = {
- Handle = cellHandle,
- DeviceFullName = name,
- DeviceMinEnergy = 0,
- DeviceMaxEnergy = 0,
- DeviceEnergyStored = 0,
- CellID = id
- }
- end
- end
- end
- end
- end,
- Handle = peripheral.wrap("monitor_51"),
- Width = 82,
- Height = 33,
- IsInteractive = true,
- Update = function (me)
- end,
- Draw = function (me)
- end
- },
- PlayerInfo = {
- Setup = function (me)
- end,
- Handle = peripheral.wrap("monitor_56"),
- Width = 18,
- Height = 33,
- IsInteractive = false,
- Update = function (me)
- end,
- Draw = function (me)
- end
- },
- PowerInfo = {
- Setup = function (me)
- GDI.drawSquare(1, 1, 7, me.Height, colours.grey)
- GDI.drawSquare(1, me.Height - 3, me.Width, 4, colours.grey)
- end,
- Handle = peripheral.wrap("monitor_53"),
- Width = 29,
- Height = 40,
- IsInteractive = false,
- StartMaxIdLen = 1,
- TotalCellMin = 0,
- TotalCellMax = 0,
- TotalCellStored = 0,
- Cells = {
- Cell1 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_0"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell1.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell1.Handle.getEnergyStored("UNKNOWN") end },
- Cell2 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_1"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell2.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell2.Handle.getEnergyStored("UNKNOWN") end },
- Cell3 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_2"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell3.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell3.Handle.getEnergyStored("UNKNOWN") end },
- Cell4 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_3"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell4.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell4.Handle.getEnergyStored("UNKNOWN") end },
- Cell5 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_4"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell5.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell5.Handle.getEnergyStored("UNKNOWN") end },
- Cell6 = { Handle = peripheral.wrap("cofh_thermalexpansion_energycell_5"), Min = function() return 0 end, Max = function () return Monitors.PowerInfo.Cells.Cell6.Handle.getMaxEnergyStored("UNKNOWN") end, Stored = function () return Monitors.PowerInfo.Cells.Cell6.Handle.getEnergyStored("UNKNOWN") end }
- },
- LineIndex = 1,
- Update = function (me)
- end,
- Draw = function (me)
- local line = 2
- for k,v in pairs(me.Cells) do
- me.LocalFunctions.drawEnergyCell(me, line, v, tostring(k))
- line = line + 2
- end
- GDI.drawText(2, me.Height - 2, "Total energy", colours.white, colours.grey)
- GDI.drawProgressBar(2, me.Height - 1, me.Width - 2, 1, me.TotalCellMin, me.TotalCellMax, me.TotalCellStored, colours.cyan, colours.grey)
- end,
- LocalFunctions = {
- drawEnergyCell = function (me, y, cell, name)
- GDI.drawText(2, y, name, colours.white, colours.grey)
- local startX = string.len(name) + 4
- local min = cell.Min()
- local max = cell.Max()
- local val = cell.Stored()
- me.TotalCellMin = me.TotalCellMin + min
- me.TotalCellMax = me.TotalCellMax + max
- me.TotalCellStored = me.TotalCellStored + val
- GDI.drawProgressBar(startX, y, me.Width - startX, 1, min, max, val, colours.cyan, colours.grey)
- end
- }
- },
- InteractiveMain = {
- Setup = function (me)
- GDI.drawSquare(4, 15, 35, 11, colours.white)
- me.LoadCells()
- end,
- LoadCells = function()
- for i, name in pairs(peripheral.getNames()) do
- if (string.startsWith(name, "cofh_thermalexpansion_energycell_", 1)) then
- local cellId = tonumber(string.sub(tostring(name), 34))
- if (cellId ~= nil) then
- if (cellId > biggestId) then
- biggestId = cellId
- end
- local cellHandle = peripheral.wrap(name)
- peripherals[cellId] = {
- Handle = cellHandle,
- full_name = name,
- minEnergy = 0,
- maxEnergy = 0,
- stored = 0,
- id = cellId,
- update = function (cell_id)
- local cell = peripherals[cell_id]
- if (cell.Handle ~= nil) then
- cell.minEnergy = 0
- cell.maxEnergy = cell.Handle.getMaxEnergyStored("UNKNOWN")
- cell.stored = cell.Handle.getEnergyStored("UNKNOWN")
- end
- end
- }
- end
- end
- end
- end,
- LineNumber = 1,
- DrawCell = function (me, x, y, w, h, name, cell)
- local offset = string.len(tostring(biggestId))
- local startX = x + offset
- GDI.drawText(x + 1, y, name, colours.black, colours.white)
- GDI.drawProgressBar(startX + 3, y, w - offset - 4, 1, cell.minEnergy, cell.maxEnergy, cell.stored, colours.cyan, colours.lightGrey)
- end,
- TotalCellCount = 0,
- StaticCellCount = 0,
- Handle = peripheral.wrap("monitor_54"),
- Width = 71,
- Height = 26,
- IsInteractive = true,
- MESystem = {
- Handle = peripheral.wrap("appeng_me_tilecontroller_1"),
- GetTotalBytes = function () return Monitors.InteractiveMain.MESystem.Handle.getTotalBytes() end,
- GetFreeBytes = function () return Monitors.InteractiveMain.MESystem.Handle.getFreeBytes() end,
- GetUsedBytes = function () return Monitors.InteractiveMain.MESystem.Handle.getTotalBytes() - Monitors.InteractiveMain.MESystem.Handle.getFreeBytes() end
- },
- Update = function (me)
- TotalCellCount = 0
- for k,v in pairs(peripherals) do
- v.update(k)
- TotalCellCount = TotalCellCount + 1
- end
- if (me.StaticCellCount ~= me.TotalCellCount) then
- me.StaticCellCount = me.TotalCellCount
- me.Draw(me)
- end
- end,
- Draw = function (me)
- GDI.drawWindowTitle(4, 15, 35, "ME System Storage Monitor", colours.lightGrey, colours.grey)
- me.LocalFunctions.DrawMEPowerMon(me, 4, 16, 35, 11)
- local h = (TotalCellCount * 2) + 2
- GDI.drawWindow(40, 3, 24, h, "dynamic cells")
- local line = 2
- for k,v in pairs(peripherals) do
- me.DrawCell(me, 40, line + 3, 24, h, k, v)
- line = line + 2
- end
- end,
- LocalFunctions = {
- DrawMEPowerMon = function (me, x, y, w, h)
- local bytesTotal = me.MESystem.GetTotalBytes()
- local bytesFree = me.MESystem.GetFreeBytes()
- local bytesUsed = bytesTotal - bytesFree
- GDI.drawText(x + 1, y + 1, "Total Bytes", colours.black, colours.white)
- GDI.drawText(x + 1, y + 2, utils.byteToMegaByte(bytesTotal) .. " MB", colours.black, colours.white)
- GDI.drawText(x + 1, y + 4, "Free Bytes", colours.black, colours.white)
- GDI.drawText(x + 1, y + 5, utils.byteToMegaByte(bytesFree) .. " MB", colours.black, colours.white)
- GDI.drawText(x + 1, y + 7, "Bytes in Use", colours.black, colours.white)
- GDI.drawProgressBar(x + 1, y + 8, w - 2, 1, 0, bytesTotal, bytesUsed, colours.green, colours.grey)
- end
- }
- },
- NewsBoard = {
- Setup = function (me)
- local strLen = string.len(me.Text)
- me.StartOffset = 1 - strLen
- me.EndOffset = strLen
- end,
- Handle = peripheral.wrap("monitor_55"),
- Width = 71,
- Height = 5,
- Text = "ello ello ello this is the police",
- Offset = 1,
- StartOffset = 1,
- EndOffset = 1,
- IsInteractive = false,
- Update = function (me)
- if (me.Offset >= me.EndOffset) then
- me.Offset = me.StartOffset
- else
- me.Offset = me.Offset + 1
- end
- end,
- Draw = function (me)
- GDI.clearMonitor()
- GDI.drawText(me.Offset, 3, me.Text, colours.white, colours.grey)
- end
- }
- }
- function loadMonitor(mon)
- GDI.Handle = mon.Handle
- end
- function ProcessMonitor(mon)
- loadMonitor(mon)
- mon.Update(mon)
- mon.Draw(mon)
- end
- function ClearMonitor()
- GDI.backgroundColour(GDI.nonNullBackgroundColour(nil))
- GDI.textColour(GDI.nonNullTextColour(nil))
- GDI.Handle.clear()
- end
- function tryExec(func)
- pcall(func)
- end
- function UpdateMonitors()
- ProcessMonitor(Monitors.Information)
- ProcessMonitor(Monitors.InteractiveMain)
- ProcessMonitor(Monitors.NewsBoard)
- ProcessMonitor(Monitors.PlayerInfo)
- ProcessMonitor(Monitors.PowerInfo)
- end
- function main()
- loadMonitor(Monitors.Information)
- ClearMonitor()
- loadMonitor(Monitors.InteractiveMain)
- ClearMonitor()
- loadMonitor(Monitors.NewsBoard)
- ClearMonitor()
- loadMonitor(Monitors.PlayerInfo)
- ClearMonitor()
- loadMonitor(Monitors.PowerInfo)
- ClearMonitor()
- loadMonitor(Monitors.Information)
- Monitors.Information.Setup(Monitors.Information)
- loadMonitor(Monitors.InteractiveMain)
- Monitors.InteractiveMain.Setup(Monitors.InteractiveMain)
- loadMonitor(Monitors.NewsBoard)
- Monitors.NewsBoard.Setup(Monitors.NewsBoard)
- loadMonitor(Monitors.PlayerInfo)
- Monitors.PlayerInfo.Setup(Monitors.PlayerInfo)
- loadMonitor(Monitors.PowerInfo)
- Monitors.PowerInfo.Setup(Monitors.PowerInfo)
- while (true) do
- tryExec(UpdateMonitors)
- os.sleep(0.2)
- if (rs.getBundledInput("bottom") == colours.white) then
- return
- elseif (rs.getBundledInput("bottom") == colours.orange) then
- os.reboot()
- end
- end
- end
- main()
- print("Exit button clicked!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement