Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Finds a peripheral on every side of the computer with the
- -- given peripheral type name. returns nil if one isnt found
- function findPeripheral(typeName)
- local function contains(str, find)
- if (str == nil or find == nil) then
- return true
- end
- if (string.find(str, find, 1) == nil) then
- return false
- end
- return true
- end
- local side
- side = peripheral.wrap("left")
- if (side ~= nil) then
- if (contains(peripheral.getType("left"), typeName)) then
- return side
- end
- end
- side = peripheral.wrap("front")
- if (side ~= nil) then
- if (contains(peripheral.getType("front"), typeName)) then
- return side
- end
- end
- side = peripheral.wrap("right")
- if (side ~= nil) then
- if (contains(peripheral.getType("right"), typeName)) then
- return side
- end
- end
- side = peripheral.wrap("back")
- if (side ~= nil) then
- if (contains(peripheral.getType("back"), typeName)) then
- return side
- end
- end
- side = peripheral.wrap("top")
- if (side ~= nil) then
- if (contains(peripheral.getType("top"), typeName)) then
- return side
- end
- end
- side = peripheral.wrap("bottom")
- if (side ~= nil) then
- if (contains(peripheral.getType("bottom"), typeName)) then
- return side
- end
- end
- return nil
- end
- function getReactorApi(code)
- return peripheral.wrap("BigReactors-Reactor_" .. code)
- end
- -- maths
- function min(a, b)
- return math.min(a, b)
- end
- function max(a, b)
- return math.max(a, b)
- end
- function clamp(value, min, max)
- if (value < min) then
- return min
- end
- if (value > max) then
- return max
- end
- return value
- end
- -- monitors and stuff
- function setCursorPos(x, y)
- monitor.setCursorPos(x + 1, y + 1)
- end
- monitor = findPeripheral("monitor")
- function clearMonitor()
- monitor.clear()
- end
- function resetMonitor()
- local colourss = colours.black
- monitor.setTextColor(colourss)
- monitor.setBackgroundColor(colours.black)
- clearMonitor()
- setCursorPos(0, 0)
- end
- -- Helpers
- function calculateCenter(monWidth, length, biasFloor)
- if (biasFloor == nil or biasFloor == true) then
- return math.floor((monWidth - length) / 2)
- else
- return math.ceil((monWidth - length) / 2)
- end
- end
- function calculateCenterText(monWidth, text, biasFloor)
- return calculateCenter(monWidth, string.len(text), biasFloor)
- end
- function calculateCentered(length, height, minX, minY, maxX, maxY, biasLeft, biasUp)
- local centerX = calculateCenter(maxX - minX, length, biasLeft)
- local centerY = calculateCenter(maxY - minY, height, biasUp)
- return centerX, centerY
- end
- function roundBias(value, biasFloor)
- if (biasFloor) then
- return math.floor(value)
- else
- return math.ceil(value)
- end
- end
- function twoStepColour(value, minValue, maxValue, colourA, colourB, colourSplitValue)
- local range = (maxValue - minValue)
- local percent = (value / range) * 100
- if (percent < colourSplitValue) then
- return colourA
- else
- return colourB
- end
- end
- function threeStepColour(value, minValue, maxValue, colourA, colourB, colourC, minColourA, minColourB)
- local range = (maxValue - minValue)
- local percent = (value / range) * 100
- if (percent <= minColourA) then
- return colourA
- elseif (percent <= minColourB) then
- return colourB
- else
- return colourC
- end
- end
- function fourStepColour(value, minValue, maxValue, colourA, colourB, colourC, colourD, minColourA, minColourB, minColourC)
- local range = (maxValue - minValue)
- local percent = (value / range) * 100
- if (percent <= 25) then
- return colourA
- elseif (percent <= 50) then
- return colourB
- elseif (percent <= 75) then
- return colourC
- else
- return colourD
- end
- end
- -- GUI
- function drawText(text, x, y, foreground, background)
- monitor.setBackgroundColor(background)
- monitor.setTextColor(foreground)
- setCursorPos(x, y)
- monitor.write(text)
- end
- function drawTextCenterX(text, y, minX, maxX, foreground, background)
- local length = maxX - minX
- local strlen = string.len(text)
- local startX = (minX + (length / 2) - (strlen / 2))
- drawText(text, startX, y, foreground, background)
- end
- function drawTextCenterY(text, x, minY, maxY, foreground, background)
- local length = maxY - minY
- local strlen = string.len(text)
- local startY = (minY + (length / 2) - (strlen / 2))
- drawText(text, x, startY, foreground, background)
- end
- function drawTextCenter(text, minX, minY, maxX, maxY, biasLeft, biasUp, foreground, background)
- local strlen = string.len(text)
- local startX = roundBias(minX + ((maxX - minX) / 2) - (strlen / 2), biasLeft)
- local startY = roundBias(minY + ((maxY - minY) / 2), biasUp)
- drawText(text, startX, startY, foreground, background)
- end
- function drawLineH(x, y, length, colour)
- monitor.setBackgroundColor(colour)
- monitor.setTextColor(colour)
- setCursorPos(x, y)
- monitor.write(string.rep(" ", length))
- end
- function drawLineV(x, y, height, colour)
- monitor.setBackgroundColor(colour)
- monitor.setTextColor(colours.orange)
- local amountY = height + y - 1
- for i = y, amountY, 1 do
- setCursorPos(x, i)
- monitor.write("")
- end
- end
- function drawRect(x, y, length, height, colour)
- monitor.setBackgroundColor(colour)
- monitor.setTextColor(colours.orange)
- local amountY = height + y - 1
- for i = y, amountY, 1 do
- setCursorPos(x, i)
- monitor.write(string.rep(" ", length))
- end
- end
- function drawMinMax(minX, minY, maxX, maxY, colour)
- monitor.setBackgroundColor(colour)
- monitor.setTextColor(colours.orange)
- local length = (maxX - minX)
- local y = minY
- while (y <= maxY) do
- setCursorPos(minX, y)
- monitor.write(string.rep(" ", length))
- y = y + 1
- end
- end
- function drawProgressBarHorzRect(x, y, width, height, minValue, maxValue, value, progressColour, backgroundColour)
- drawRect(x, y, width, height, backgroundColour)
- drawRect(x, y, math.floor(value / (maxValue - minValue)), height, progressColour)
- end
- function drawProgressBarHorz(x, y, width, minValue, maxValue, value, progressColour, backgroundColour)
- drawLineH(x, y, width, backgroundColour)
- drawLineH(x, y, math.floor((value / (maxValue - minValue)) * width), progressColour)
- end
- function drawProgressBarVert(x, y, height, minValue, maxValue, value, progressColour, backgroundColour)
- drawLineV(x, y, height, backgroundColour)
- drawLineV(x, y, math.floor((value / (maxValue - minValue)) * height), progressColour)
- end
- function drawTextProgress(text, gap, minValue, maxValue, value, totalLength, x, y, textColour, textBackground, progressColour, progressBackground)
- local progressOffset = string.len(text) + gap
- local progressStart = x + progressOffset
- local progressLength = totalLength - progressOffset
- drawText(text, x, y, textColour, textBackground)
- drawProgressBarHorz(progressStart, y, progressLength, minValue, maxValue, value, progressBackground, progressColour)
- end
- tileTopBackground = colours.lightGrey
- tileBtmBackground = colours.grey
- progressBarBackground = colours.black
- function drawReactor(rect, ri)
- local colour = colours.red
- if (ri.isOnline) then
- colour = colours.green
- end
- --drawMinMax(rect.min.x, rect.min.y, rect.max.x, rect.max.y, tileTopBackground)
- local statsOffset = 7
- local centerY = (rect.min.y + ((rect.max.y - rect.min.y - statsOffset) / 2)) - 1
- drawMinMax(rect.min.x, rect.min.y, rect.max.x, rect.max.y - statsOffset - 2, tileTopBackground)
- drawMinMax(rect.min.x, rect.max.y - statsOffset - 1, rect.max.x, rect.max.y, tileBtmBackground)
- drawMinMax(rect.min.x + 1, centerY - 1, rect.max.x - 1, centerY + 2, colour)
- --drawRect(rect.min.x, centerY - 1, rect.max.x - rect.min.x, 4, colour)
- drawTextCenterX(ri.reactorName, centerY, rect.min.x, rect.max.x, colours.white, colour)
- if (ri.isOnline) then
- drawTextCenterX("Online", centerY + 1, rect.min.x, rect.max.x, colours.white, colour)
- else
- drawTextCenterX("Offline", centerY + 1, rect.min.x, rect.max.x, colours.white, colour)
- end
- local y = rect.max.y - statsOffset
- local x = rect.min.x + 1
- local len = (rect.max.x - rect.min.x) - 2
- local heatColour = threeStepColour(ri.heatLevel, ri.minHeatLevel, ri.maxHeatLevel, colours.cyan, colours.purple, colours.red, 33, 70)
- drawTextProgress("Heat", 1, ri.minHeatLevel, ri.maxHeatLevel, ri.heatLevel, len, x, y, colours.white, tileBtmBackground, progressBarBackground, heatColour)
- drawTextProgress("Fuel", 1, ri.minFuelLevel, ri.maxFuelLevel, ri.fuelLevel, len, x, y + 2, colours.white, tileBtmBackground, progressBarBackground, colours.yellow)
- drawTextProgress("Power", 1, ri.minPwrLevel, ri.maxPwrLevel, ri.pwrLevel, len, x, y + 4, colours.white, tileBtmBackground, progressBarBackground, colours.red)
- local radiationColour = threeStepColour(ri.radiation, ri.minRadLevel, ri.maxRadLevel, colours.black, colours.blue, colours.cyan, 20, 50)
- drawTextProgress("Radiation", 1, ri.minRadLevel, ri.maxRadLevel, ri.radiation, len, x, y + 6, colours.white, tileBtmBackground, progressBarBackground, radiationColour)
- end
- reactor1 = {
- reactorName = "Reactor 1",
- isOnline = false,
- minHeatLevel = 0,
- maxHeatLevel = 3000,
- minFuelLevel = 0,
- maxFuelLevel = 1000,
- minPwrLevel = 0,
- maxPwrLevel = 10000000,
- minRadLevel = 0,
- maxRadLevel = 1000,
- heatLevel = 241,
- fuelLevel = 29,
- pwrLevel = 8284657,
- radiation = 330,
- api = getReactorApi(57)
- }
- reactor2 = {
- reactorName = "Reactor 2",
- isOnline = true,
- minHeatLevel = 0,
- maxHeatLevel = 3000,
- minFuelLevel = 0,
- maxFuelLevel = 1000,
- minPwrLevel = 0,
- maxPwrLevel = 10000000,
- minRadLevel = 0,
- maxRadLevel = 1000,
- heatLevel = 241,
- fuelLevel = 29,
- pwrLevel = 8284657,
- radiation = 512,
- api = getReactorApi(58)
- }
- reactor3 = {
- reactorName = "Reactor 3",
- isOnline = false,
- minHeatLevel = 0,
- maxHeatLevel = 3000,
- minFuelLevel = 0,
- maxFuelLevel = 1000,
- minPwrLevel = 0,
- maxPwrLevel = 10000000,
- minRadLevel = 0,
- maxRadLevel = 1000,
- heatLevel = 0,
- fuelLevel = 0,
- pwrLevel = 0,
- radiation = 0,
- api = getReactorApi(59)
- }
- reactor4 = {
- reactorName = "Reactor 4",
- isOnline = false,
- minHeatLevel = 0,
- maxHeatLevel = 3000,
- minFuelLevel = 0,
- maxFuelLevel = 1000,
- minPwrLevel = 0,
- maxPwrLevel = 10000000,
- minRadLevel = 0,
- maxRadLevel = 100,
- heatLevel = 0,
- fuelLevel = 0,
- pwrLevel = 0,
- radiation = 0,
- api = getReactorApi(60)
- }
- function render()
- local fX, fY = monitor.getSize()
- fX = fX - 1
- fY = fY - 2
- local g = {
- minX = 1, minY = 1,
- maxX = fX, maxY = fY,
- halfX = math.floor(fX / 2), halfY = math.floor(fY / 2),
- offset = {
- left = 1, up = 0, right = 1, down = 1
- }
- }
- local s1 = {
- min = { x = (g.minX), y = (g.minY) },
- max = { x = math.floor(g.halfX - g.offset.left), y = math.floor(g.halfY - g.offset.up) }
- }
- local s2 = {
- min = { x = math.floor(g.halfX + g.offset.right), y = (g.minY) },
- max = { x = (g.maxX), y = math.floor(g.halfY - g.offset.up) }
- }
- local s3 = {
- min = { x = (g.minX), y = math.floor(g.halfY + (g.offset.down) + 1) },
- max = { x = math.floor(g.halfX - g.offset.left), y = (g.maxY) }
- }
- local s4 = {
- min = { x = math.floor(g.halfX + g.offset.right), y = math.floor(g.halfY + (g.offset.down) + 1) },
- max = { x = (g.maxX), y = (g.maxY) }
- }
- monitor.setBackgroundColor(colours.black)
- monitor.setTextColor(colours.black)
- clearMonitor()
- drawReactor(s1, reactor1)
- drawReactor(s2, reactor2)
- drawReactor(s3, reactor3)
- drawReactor(s4, reactor4)
- end
- function updateReactor(reactor)
- if (reactor.api ~= nil) then
- reactor.minPwrLevel = 0
- reactor.maxPwrLevel = 10000000
- reactor.minHeatLevel = 0
- reactor.maxHeatLevel = 3000
- reactor.minFuelLevel = 0
- reactor.maxFuelLevel = reactor.api.getFuelAmountMax()
- reactor.minRadLevel = 0
- reactor.maxRadLevel = 700
- reactor.isOnline = reactor.api.getActive()
- reactor.heatLevel = reactor.api.getFuelTemperature()
- reactor.fuelLevel = reactor.api.getFuelAmount()
- reactor.radiation = reactor.api.getFuelReactivity()
- reactor.pwrLevel = reactor.api.getEnergyStored()
- end
- end
- function update()
- updateReactor(reactor1)
- updateReactor(reactor2)
- updateReactor(reactor3)
- updateReactor(reactor4)
- end
- function main()
- if (monitor == false) then
- print("Failed to find a monitor!")
- return
- else
- print("Found a monitor!")
- end
- while (true) do
- update()
- render()
- os.sleep(0.2)
- end
- end
- main()
Add Comment
Please, Sign In to add comment