bobmarley12345

bigreactor

May 4th, 2021 (edited)
566
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Finds a peripheral on every side of the computer with the
  2. -- given peripheral type name. returns nil if one isnt found
  3. function findPeripheral(typeName)
  4.     local function contains(str, find)
  5.         if (str == nil or find == nil) then
  6.             return true
  7.         end
  8.         if (string.find(str, find, 1) == nil) then
  9.             return false
  10.         end
  11.         return true
  12.     end
  13.     local side
  14.     side = peripheral.wrap("left")
  15.     if (side ~= nil) then
  16.         if (contains(peripheral.getType("left"), typeName)) then
  17.             return side
  18.         end
  19.     end
  20.     side = peripheral.wrap("front")
  21.     if (side ~= nil) then
  22.         if (contains(peripheral.getType("front"), typeName)) then
  23.             return side
  24.         end
  25.     end
  26.  
  27.     side = peripheral.wrap("right")
  28.     if (side ~= nil) then
  29.         if (contains(peripheral.getType("right"), typeName)) then
  30.             return side
  31.         end
  32.     end
  33.  
  34.     side = peripheral.wrap("back")
  35.     if (side ~= nil) then
  36.         if (contains(peripheral.getType("back"), typeName)) then
  37.             return side
  38.         end
  39.     end
  40.  
  41.     side = peripheral.wrap("top")
  42.     if (side ~= nil) then
  43.         if (contains(peripheral.getType("top"), typeName)) then
  44.             return side
  45.         end
  46.     end
  47.  
  48.     side = peripheral.wrap("bottom")
  49.     if (side ~= nil) then
  50.         if (contains(peripheral.getType("bottom"), typeName)) then
  51.             return side
  52.         end
  53.     end
  54.  
  55.     return nil
  56. end
  57.  
  58. function getReactorApi(code)
  59.     return peripheral.wrap("BigReactors-Reactor_" .. code)
  60. end
  61.  
  62. -- maths
  63.  
  64. function min(a, b)
  65.     return math.min(a, b)
  66. end
  67.  
  68. function max(a, b)
  69.     return math.max(a, b)
  70. end
  71.  
  72. function clamp(value, min, max)
  73.     if (value < min) then
  74.         return min
  75.     end
  76.     if (value > max) then
  77.         return max
  78.     end
  79.     return value
  80. end
  81. -- monitors and stuff
  82.  
  83. function setCursorPos(x, y)
  84.     monitor.setCursorPos(x + 1, y + 1)
  85. end
  86.  
  87. monitor = findPeripheral("monitor")
  88.  
  89. function clearMonitor()
  90.     monitor.clear()
  91. end
  92.  
  93. function resetMonitor()
  94.     local colourss = colours.black
  95.     monitor.setTextColor(colourss)
  96.     monitor.setBackgroundColor(colours.black)
  97.     clearMonitor()
  98.     setCursorPos(0, 0)
  99. end
  100.  
  101. -- Helpers
  102.  
  103. function calculateCenter(monWidth, length, biasFloor)
  104.     if (biasFloor == nil or biasFloor == true) then
  105.         return math.floor((monWidth - length) / 2)
  106.     else
  107.         return math.ceil((monWidth - length) / 2)
  108.     end
  109. end
  110.  
  111. function calculateCenterText(monWidth, text, biasFloor)
  112.     return calculateCenter(monWidth, string.len(text), biasFloor)
  113. end
  114.  
  115. function calculateCentered(length, height, minX, minY, maxX, maxY, biasLeft, biasUp)
  116.     local centerX = calculateCenter(maxX - minX, length, biasLeft)
  117.     local centerY = calculateCenter(maxY - minY, height, biasUp)
  118.     return centerX, centerY
  119. end
  120.  
  121. function roundBias(value, biasFloor)
  122.     if (biasFloor) then
  123.         return math.floor(value)
  124.     else
  125.         return math.ceil(value)
  126.     end
  127. end
  128.  
  129. function twoStepColour(value, minValue, maxValue, colourA, colourB, colourSplitValue)
  130.     local range = (maxValue - minValue)
  131.     local percent = (value / range) * 100
  132.     if (percent < colourSplitValue) then
  133.         return colourA
  134.     else
  135.         return colourB
  136.     end
  137. end
  138.  
  139. function threeStepColour(value, minValue, maxValue, colourA, colourB, colourC, minColourA, minColourB)
  140.     local range = (maxValue - minValue)
  141.     local percent = (value / range) * 100
  142.     if (percent <= minColourA) then
  143.         return colourA
  144.     elseif (percent <= minColourB) then
  145.         return colourB
  146.     else
  147.         return colourC
  148.     end
  149. end
  150.  
  151. function fourStepColour(value, minValue, maxValue, colourA, colourB, colourC, colourD, minColourA, minColourB, minColourC)
  152.     local range = (maxValue - minValue)
  153.     local percent = (value / range) * 100
  154.     if (percent <= 25) then
  155.         return colourA
  156.     elseif (percent <= 50) then
  157.         return colourB
  158.     elseif (percent <= 75) then
  159.         return colourC
  160.     else
  161.         return colourD
  162.     end
  163. end
  164.  
  165. -- GUI
  166.  
  167. function drawText(text, x, y, foreground, background)
  168.     monitor.setBackgroundColor(background)
  169.     monitor.setTextColor(foreground)
  170.     setCursorPos(x, y)
  171.     monitor.write(text)
  172. end
  173.  
  174. function drawTextCenterX(text, y, minX, maxX, foreground, background)
  175.     local length = maxX - minX
  176.     local strlen = string.len(text)
  177.     local startX = (minX + (length / 2) - (strlen / 2))
  178.     drawText(text, startX, y, foreground, background)
  179. end
  180.  
  181. function drawTextCenterY(text, x, minY, maxY, foreground, background)
  182.     local length = maxY - minY
  183.     local strlen = string.len(text)
  184.     local startY = (minY + (length / 2) - (strlen / 2))
  185.     drawText(text, x, startY, foreground, background)
  186. end
  187.  
  188. function drawTextCenter(text, minX, minY,  maxX, maxY, biasLeft, biasUp, foreground, background)
  189.     local strlen = string.len(text)
  190.     local startX = roundBias(minX + ((maxX - minX) / 2) - (strlen / 2), biasLeft)
  191.     local startY = roundBias(minY + ((maxY - minY) / 2), biasUp)
  192.     drawText(text, startX, startY, foreground, background)
  193. end
  194.  
  195. function drawLineH(x, y, length, colour)
  196.     monitor.setBackgroundColor(colour)
  197.     monitor.setTextColor(colour)
  198.     setCursorPos(x, y)
  199.     monitor.write(string.rep(" ", length))
  200. end
  201.  
  202. function drawLineV(x, y, height, colour)
  203.     monitor.setBackgroundColor(colour)
  204.     monitor.setTextColor(colours.orange)
  205.     local amountY = height + y - 1
  206.     for i = y, amountY, 1 do
  207.         setCursorPos(x, i)
  208.         monitor.write("")
  209.     end
  210. end
  211.  
  212. function drawRect(x, y, length, height, colour)
  213.     monitor.setBackgroundColor(colour)
  214.     monitor.setTextColor(colours.orange)
  215.     local amountY = height + y - 1
  216.     for i = y, amountY, 1 do
  217.         setCursorPos(x, i)
  218.         monitor.write(string.rep(" ", length))
  219.     end
  220. end
  221.  
  222. function drawMinMax(minX, minY, maxX, maxY, colour)
  223.     monitor.setBackgroundColor(colour)
  224.     monitor.setTextColor(colours.orange)
  225.     local length = (maxX - minX)
  226.     local y = minY
  227.     while (y <= maxY) do
  228.         setCursorPos(minX, y)
  229.         monitor.write(string.rep(" ", length))
  230.         y = y + 1
  231.     end
  232. end
  233.  
  234. function drawProgressBarHorzRect(x, y, width, height, minValue, maxValue, value, progressColour, backgroundColour)
  235.     drawRect(x, y, width, height, backgroundColour)
  236.     drawRect(x, y, math.floor(value / (maxValue - minValue)), height, progressColour)
  237. end
  238.  
  239. function drawProgressBarHorz(x, y, width, minValue, maxValue, value, progressColour, backgroundColour)
  240.     drawLineH(x, y, width, backgroundColour)
  241.     drawLineH(x, y, math.floor((value / (maxValue - minValue)) * width), progressColour)
  242. end
  243.  
  244. function drawProgressBarVert(x, y, height, minValue, maxValue, value, progressColour, backgroundColour)
  245.     drawLineV(x, y, height, backgroundColour)
  246.     drawLineV(x, y, math.floor((value / (maxValue - minValue)) * height), progressColour)
  247. end
  248.  
  249. function drawTextProgress(text, gap, minValue, maxValue, value, totalLength, x, y, textColour, textBackground, progressColour, progressBackground)
  250.     local progressOffset = string.len(text) + gap
  251.     local progressStart = x + progressOffset
  252.     local progressLength = totalLength - progressOffset
  253.     drawText(text, x, y, textColour, textBackground)
  254.     drawProgressBarHorz(progressStart, y, progressLength, minValue, maxValue, value, progressBackground, progressColour)
  255. end
  256.  
  257. tileTopBackground = colours.lightGrey
  258. tileBtmBackground = colours.grey
  259. progressBarBackground = colours.black
  260.  
  261. function drawReactor(rect, ri)
  262.     local colour = colours.red
  263.     if (ri.isOnline) then
  264.         colour = colours.green
  265.     end
  266.  
  267.     --drawMinMax(rect.min.x, rect.min.y, rect.max.x, rect.max.y, tileTopBackground)
  268.  
  269.     local statsOffset = 7
  270.     local centerY = (rect.min.y + ((rect.max.y - rect.min.y - statsOffset) / 2)) - 1
  271.     drawMinMax(rect.min.x, rect.min.y, rect.max.x, rect.max.y - statsOffset - 2, tileTopBackground)
  272.     drawMinMax(rect.min.x, rect.max.y - statsOffset - 1, rect.max.x, rect.max.y, tileBtmBackground)
  273.     drawMinMax(rect.min.x + 1, centerY - 1, rect.max.x - 1, centerY + 2, colour)
  274.     --drawRect(rect.min.x, centerY - 1, rect.max.x - rect.min.x, 4, colour)
  275.     drawTextCenterX(ri.reactorName, centerY, rect.min.x, rect.max.x, colours.white, colour)
  276.     if (ri.isOnline) then
  277.         drawTextCenterX("Online", centerY + 1, rect.min.x, rect.max.x, colours.white, colour)
  278.     else
  279.         drawTextCenterX("Offline", centerY + 1, rect.min.x, rect.max.x, colours.white, colour)
  280.     end
  281.  
  282.     local y = rect.max.y - statsOffset
  283.     local x = rect.min.x + 1
  284.     local len = (rect.max.x - rect.min.x) - 2
  285.     local heatColour = threeStepColour(ri.heatLevel, ri.minHeatLevel, ri.maxHeatLevel, colours.cyan, colours.purple, colours.red, 33, 70)
  286.     drawTextProgress("Heat", 1, ri.minHeatLevel, ri.maxHeatLevel, ri.heatLevel, len, x, y, colours.white, tileBtmBackground, progressBarBackground, heatColour)
  287.     drawTextProgress("Fuel", 1, ri.minFuelLevel, ri.maxFuelLevel, ri.fuelLevel, len, x, y + 2, colours.white, tileBtmBackground, progressBarBackground, colours.yellow)
  288.     drawTextProgress("Power", 1, ri.minPwrLevel, ri.maxPwrLevel, ri.pwrLevel, len, x, y + 4, colours.white, tileBtmBackground, progressBarBackground, colours.red)
  289.     local radiationColour = threeStepColour(ri.radiation, ri.minRadLevel, ri.maxRadLevel, colours.black, colours.blue, colours.cyan, 20, 50)
  290.     drawTextProgress("Radiation", 1, ri.minRadLevel, ri.maxRadLevel, ri.radiation, len, x, y + 6, colours.white, tileBtmBackground, progressBarBackground, radiationColour)
  291. end
  292.  
  293. reactor1 = {
  294.     reactorName = "Reactor 1",
  295.     isOnline = false,
  296.     minHeatLevel = 0,
  297.     maxHeatLevel = 3000,
  298.     minFuelLevel = 0,
  299.     maxFuelLevel = 1000,
  300.     minPwrLevel = 0,
  301.     maxPwrLevel = 10000000,
  302.     minRadLevel = 0,
  303.     maxRadLevel = 1000,
  304.     heatLevel = 241,
  305.     fuelLevel = 29,
  306.     pwrLevel = 8284657,
  307.     radiation = 330,
  308.     api = getReactorApi(16)
  309. }
  310.  
  311. reactor2 = {
  312.     reactorName = "Reactor 2",
  313.     isOnline = true,
  314.     minHeatLevel = 0,
  315.     maxHeatLevel = 3000,
  316.     minFuelLevel = 0,
  317.     maxFuelLevel = 1000,
  318.     minPwrLevel = 0,
  319.     maxPwrLevel = 10000000,
  320.     minRadLevel = 0,
  321.     maxRadLevel = 1000,
  322.     heatLevel = 241,
  323.     fuelLevel = 29,
  324.     pwrLevel = 8284657,
  325.     radiation = 512,
  326.     api = getReactorApi(17)
  327. }
  328.  
  329. reactor3 = {
  330.     reactorName = "Reactor 3",
  331.     isOnline = false,
  332.     minHeatLevel = 0,
  333.     maxHeatLevel = 3000,
  334.     minFuelLevel = 0,
  335.     maxFuelLevel = 1000,
  336.     minPwrLevel = 0,
  337.     maxPwrLevel = 10000000,
  338.     minRadLevel = 0,
  339.     maxRadLevel = 1000,
  340.     heatLevel = 0,
  341.     fuelLevel = 0,
  342.     pwrLevel = 0,
  343.     radiation = 0,
  344.     api = getReactorApi(35)
  345. }
  346.  
  347. reactor4 = {
  348.     reactorName = "Reactor 4",
  349.     isOnline = false,
  350.     minHeatLevel = 0,
  351.     maxHeatLevel = 3000,
  352.     minFuelLevel = 0,
  353.     maxFuelLevel = 1000,
  354.     minPwrLevel = 0,
  355.     maxPwrLevel = 10000000,
  356.     minRadLevel = 0,
  357.     maxRadLevel = 100,
  358.     heatLevel = 0,
  359.     fuelLevel = 0,
  360.     pwrLevel = 0,
  361.     radiation = 0,
  362.     api = getReactorApi(1000)
  363. }
  364.  
  365. function render()
  366.     local fX, fY = monitor.getSize()
  367.     fX = fX - 1
  368.     fY = fY - 2
  369.     local g = {
  370.         minX = 1, minY = 1,
  371.         maxX = fX, maxY = fY,
  372.         halfX = math.floor(fX / 2), halfY = math.floor(fY / 2),
  373.         offset = {
  374.             left = 1, up = 0, right = 1, down = 1
  375.         }
  376.     }
  377.    
  378.     local s1 = {
  379.         min = { x = (g.minX), y = (g.minY) },
  380.         max = { x = math.floor(g.halfX - g.offset.left), y = math.floor(g.halfY - g.offset.up) }
  381.     }
  382.     local s2 = {
  383.         min = { x = math.floor(g.halfX + g.offset.right), y = (g.minY) },
  384.         max = { x = (g.maxX), y = math.floor(g.halfY - g.offset.up) }
  385.     }
  386.     local s3 = {
  387.         min = { x = (g.minX), y = math.floor(g.halfY + (g.offset.down) + 1) },
  388.         max = { x = math.floor(g.halfX - g.offset.left), y = (g.maxY) }
  389.     }
  390.     local s4 = {
  391.         min = { x = math.floor(g.halfX + g.offset.right), y = math.floor(g.halfY + (g.offset.down) + 1) },
  392.         max = { x = (g.maxX), y = (g.maxY) }
  393.     }
  394.  
  395.     monitor.setBackgroundColor(colours.black)
  396.     monitor.setTextColor(colours.black)
  397.     clearMonitor()
  398.     drawReactor(s1, reactor1)
  399.     drawReactor(s2, reactor2)
  400.     drawReactor(s3, reactor3)
  401.     drawReactor(s4, reactor4)
  402. end
  403.  
  404. function updateReactor(reactor)
  405.     if (reactor.api ~= nil) then
  406.         reactor.minPwrLevel = 0
  407.         reactor.maxPwrLevel = 10000000
  408.         reactor.minHeatLevel = 0
  409.         reactor.maxHeatLevel = 3000
  410.         reactor.minFuelLevel = 0
  411.         reactor.maxFuelLevel = reactor.api.getFuelAmountMax()
  412.         reactor.minRadLevel = 0
  413.         reactor.maxRadLevel = 700
  414.         reactor.isOnline = reactor.api.getActive()
  415.         reactor.heatLevel = reactor.api.getFuelTemperature()
  416.         reactor.fuelLevel = reactor.api.getFuelAmount()
  417.         reactor.radiation = reactor.api.getFuelReactivity()
  418.         reactor.pwrLevel = reactor.api.getEnergyStored()
  419.     end
  420. end
  421.  
  422. function update()
  423.     updateReactor(reactor1)
  424.     updateReactor(reactor2)
  425.     updateReactor(reactor3)
  426.     updateReactor(reactor4)
  427. end
  428.  
  429. function main()
  430.     if (monitor == false) then
  431.         print("Failed to find a monitor!")
  432.         return
  433.     else
  434.         print("Found a monitor!")
  435.     end
  436.  
  437.     while (true) do
  438.         update()
  439.         render()
  440.         os.sleep(0.2)
  441.     end
  442. end
  443.  
  444. main()
RAW Paste Data