DerReuter

BigReactor2021

Aug 4th, 2021 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.13 KB | None | 0 0
  1. --[[
  2.    /---------------------------------\
  3.    | Big Reactors Status Display     |
  4.    \---------------------------------/
  5. ]]
  6.  
  7. -- Config
  8.  
  9. -- This is the side on which your reactor is connected
  10. local reactor_side = "BigReactors-Reactor_0"
  11. -- This is the side on which your monitor is connected
  12. local monitor_side = "right"
  13.  
  14. -- Target Core Temperature
  15. local target_temp = 900
  16. -- Hysteresis +/- temperature offset before controlling rods
  17. local hysteresis = 30
  18.  
  19.  
  20. -- If the reactor is not connected, exit the program
  21. if not peripheral.isPresent(reactor_side) then
  22.   print("Please check, that the reactor is connected to the same side as in your config!")
  23.   return
  24. end
  25. -- If the monitor is not connected, exit
  26. if not peripheral.isPresent(monitor_side) then
  27.   print("Please check, that the monitor is connected to the same side as in your config!")
  28.   return
  29. end
  30.  
  31. -- Wrap the peripherals
  32. local reactor = peripheral.wrap(reactor_side)
  33. while not reactor.getConnected() do sleep(10) end
  34. local monitor = peripheral.wrap(monitor_side)
  35. monitor.setCursorPos(1,9)
  36.  
  37. print("The program is now running on the monitor!")
  38.  
  39. -- BEGIN FUNCTIONS PART
  40. function clear()
  41.   monitor.clear()
  42.   monitor.setTextColor(colors.white)
  43.   monitor.setBackgroundColor(colors.black)
  44.   monitor.setCursorPos(1, 1)
  45. end
  46.  
  47. function resetColors()
  48.   monitor.setTextColor(colors.white)
  49.   monitor.setBackgroundColor(colors.black)
  50. end
  51.  
  52. function reactortoggle()
  53.  if reactor.getActive() then
  54.  reactor.setActive(false)
  55.  else reactor.setActive(true)
  56.  end
  57. end
  58.  
  59. function newLine()
  60.   local x, y = monitor.getCursorPos()
  61.   monitor.setCursorPos(1, y+1)
  62. end
  63.  
  64. function drawUI()
  65.   monitor.write("/-------------------------------------\\")
  66.  
  67.   for x = 2, 11, 1 do
  68.         monitor.setCursorPos(1, x)
  69.         monitor.write("|")
  70.         monitor.setCursorPos(29, x)
  71.         monitor.write("|")
  72.         monitor.setCursorPos(39, x)
  73.         monitor.write("|")
  74.   end
  75.  
  76.   monitor.setCursorPos(2, 3)
  77.   monitor.write("---------------------------")
  78.  
  79.   monitor.setCursorPos(2, 5)
  80.   monitor.write("---------------------------")
  81.   monitor.setCursorPos(2, 8)
  82.   monitor.write("---------------------------")
  83.   monitor.setCursorPos(30, 8)
  84.   monitor.write("---------")
  85.  
  86.   monitor.setCursorPos(1, 12)
  87.   monitor.write("\\-------------------------------------/")
  88. end
  89.  
  90. local function Button(
  91.                                 width,
  92.                                 height,
  93.                                 label,
  94.                                 backgroundColorNormal,                                
  95.                                 backgroundColorPressed,
  96.                                 textColorNormal,
  97.                                 textColorPressed,
  98.                                 hasBorder,
  99.                                 borderColorNormal,
  100.                                 borderColorPressed,
  101.                                 startColumn,
  102.                                 startRow,
  103.                                 isPressed,
  104.                                 defaultBackgroundColor,
  105.                                 defaultTextColor
  106.                             )
  107.     local button = {}
  108.     button.height=height or 1
  109.     button.width=width or 1
  110.     button.label=label or ""
  111.     button.backgroundColorNormal=backgroundColorNormal or colors.black
  112.     button.backgroundColorPressed=backgroundColorPressed or colors.white
  113.     button.textColorNormal=textColorNormal or colors.white
  114.     button.textColorPressed=textColorPressed or colors.black
  115.     button.hasBorder = hasBorder or false
  116.     button.borderColorNormal = borderColorNormal or backGroundColorNormal
  117.     button.borderColorPressed = borderColorPressed or backGroundColorPressed
  118.     button.defaultBackgroundColor = defaultBackgroundColor or colors.black
  119.     button.defaultTextColor = defaultTextColor or colors.white
  120.     button.startColumn = startColumn or 1
  121.     button.startRow = startRow or 1
  122.     button.isPressed=isPressed or false
  123.     function button.press()
  124.         button.isPressed = not button.isPressed
  125.     end
  126.    
  127.     function button.clicked(column,row)
  128.     return (column >= button.startColumn and column < button.startColumn + button.width and row >= button.startRow and row < button.startRow + button.height)
  129.     end
  130.  
  131.     function button.draw(display,isPressed,startColumn,startRow)
  132.  
  133.         button.startColumn = startColumn or button.startColumn
  134.         button.startRow = startRow or button.startRow
  135.         display = display or term
  136.         if isPressed == false or isPressed then
  137.             button.isPressed = isPressed
  138.         else isPressed = button.isPressed
  139.         end
  140.         local width = button.width
  141.         local height = button.height
  142.         startRow = button.startRow
  143.         startColumn = button.startColumn
  144.  
  145.         local label = button.label
  146.         local labelPad = 2
  147.  
  148.         -- set border params and draw border if hasBorder
  149.         if button.hasBorder == true then
  150.             -- button must be at least 3x3, if not, make it so
  151.             if width < 3 then
  152.                 width = 3
  153.             end
  154.             if height < 3 then
  155.                 height = 3
  156.             end
  157.  
  158.             -- set border colors
  159.             if not isPressed then
  160.                 if not display.isColor() then
  161.                     display.setBackgroundColor(colors.white)
  162.                 else
  163.                     display.setBackgroundColor(button.borderColorNormal)
  164.                 end
  165.             else
  166.                 if not display.isColor() then
  167.                     display.setBackgroundColor(colors.white)
  168.                 else
  169.                     display.setBackgroundColor(button.borderColorPressed)
  170.                 end
  171.             end
  172.  
  173.             -- draw button border (inset)
  174.             display.setCursorPos(startColumn,startRow)
  175.             display.write(string.rep(" ",width))
  176.             for row = 2,height-1 do
  177.                 display.setCursorPos(startColumn,button.startRow+row -1)
  178.                 display.write(" ")
  179.                 display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  180.                 display.write(" ")
  181.             end
  182.             display.setCursorPos(startColumn,startRow+height-1)
  183.             display.write(string.rep(" ",width))
  184.  
  185.             -- reset startColumn,startRow,width,column to inset button and label
  186.             startColumn=startColumn+1
  187.             startRow = startRow +1
  188.             width = width - 2
  189.             height = height - 2
  190.         end
  191.  
  192.         --set button background and text colors
  193.         if not isPressed then
  194.             if not display.isColor() then
  195.                 display.setBackgroundColor(colors.black)
  196.                 display.setTextColor(colors.white)
  197.             else
  198.                 display.setBackgroundColor(button.backgroundColorNormal)
  199.                 display.setTextColor(button.textColorNormal)
  200.             end
  201.         else
  202.             if not display.isColor() then
  203.                 display.setBackgroundColor(colors.white)
  204.                 display.setTextColor(colors.black)
  205.             else
  206.                 display.setBackgroundColor(button.backgroundColorPressed)
  207.                 display.setTextColor(button.textColorPressed)
  208.             end
  209.         end
  210.  
  211.         -- draw button background (will be inside border if there is one)
  212.         for row = 1,height do
  213.             --print(tostring(startColumn)..","..tostring(startRow-row))
  214.             display.setCursorPos(startColumn,startRow + row -1)
  215.             display.write(string.rep(" ",width))
  216.         end
  217.  
  218.         -- prepare label, truncate label if necessary
  219.  
  220.         -- prepare label, truncate label if necessary
  221.         if width < 3 then
  222.             labelPad = 0
  223.         end
  224.         if #label > width - labelPad then
  225.             label = label:sub(1,width - labelPad)
  226.         end
  227.  
  228.         -- draw label
  229.         display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  230.         display.write(label)
  231.         display.setBackgroundColor(button.defaultBackgroundColor)
  232.         display.setTextColor(button.defaultTextColor)
  233.     end
  234.     button.toggle = function ()
  235.             button.isPressed = not button.isPressed
  236.             return button.isPressed
  237.         end            
  238.     return button
  239. end
  240.  
  241.  
  242.  ----------------------------------------------------------------------
  243. buttons = {}
  244. buttons.on= Button(9,3," ON/OFF",colors.red,colors.lime,colors.black,colors.white,false,colors.green,colors.pink,30,3,false,nil,nil)
  245.  
  246.  
  247.  
  248.  
  249. ----------------------------------------------------------------------
  250. -- END FUNCTIONS PART
  251.  
  252.  
  253.  
  254. -- Repeat forever
  255. function update()
  256. while true do
  257.  
  258.    
  259.   -- Clear the terminal---------------------------------------------------------------------
  260.   clear()
  261.   -- Draw the basic UI layout
  262.   drawUI()
  263.  
  264.   -- Reactor status
  265.   active = reactor.getActive()
  266.   monitor.setCursorPos(3, 2)
  267.  
  268.   monitor.write("Status: ")
  269.   if active then
  270.     monitor.setBackgroundColor(colors.green)
  271.     monitor.setTextColor(colors.white)
  272.     monitor.write("ACTIVE (Auto)")
  273.   else
  274.     monitor.setBackgroundColor(colors.red)
  275.     monitor.setTextColor(colors.white)
  276.     monitor.write("INACTIVE")
  277.   end
  278.   -- End Reactor status
  279.  
  280.  
  281.   resetColors()  
  282.  
  283.  
  284.  
  285.   monitor.setCursorPos(3, 4)  
  286.   -- Steam Output
  287.   energy = reactor.getHotFluidProducedLastTick()
  288.  
  289.    
  290.   -- Print out the reactor status
  291.   monitor.setTextColor(colors.white)
  292.   monitor.write("Steam Output: ")
  293.   monitor.setTextColor(colors.yellow)
  294.   monitor.write(energy)
  295.   monitor.write(" mB/T")
  296.   -- END reactor status
  297.  
  298.   -- Temperatures
  299.   monitor.setCursorPos(3, 6)
  300.   monitor.setTextColor(colors.red)
  301.   monitor.write("Core Temp:")
  302.   monitor.setCursorPos(15, 6)
  303.   monitor.setTextColor(colors.white)
  304.   monitor.write("| Power Level:")
  305.   monitor.setCursorPos(15, 7)
  306.   monitor.write("|")
  307.  
  308.   local coreTemp = math.floor(reactor.getFuelTemperature())
  309.   local powerLevel = 100 - math.floor(reactor.getControlRodLevel(1))
  310.   local controlRods = reactor.getControlRodLevel(1)
  311.  
  312.   monitor.setCursorPos(3, 7)
  313.   monitor.setTextColor(colors.cyan)
  314.   monitor.write(coreTemp .. " C")
  315.   monitor.setCursorPos(17, 7)
  316.   monitor.write(powerLevel .. " %")
  317.  
  318.   if active then --Only move control rods when reactor is active
  319.     -- If core temp rises above the target_temp + hysteresis value, lower control rods
  320.     if coreTemp >= (target_temp + hysteresis) then
  321.     reactor.setAllControlRodLevels(controlRods  + 1)
  322.     end
  323.     -- If core temp falls below the target_temp - hysteresis value, raise control rods
  324.     if coreTemp <= (target_temp - hysteresis) then
  325.     reactor.setAllControlRodLevels(controlRods  - 1)
  326.     end
  327.     -- Avoid critial core temp
  328.     if coreTemp >= 1500 then
  329.     reactor.setAllControlRodLevels(100)
  330.     end
  331.   end
  332.  
  333.     -- END temperature control
  334.  
  335.   -- Waste, Water and fuel status
  336.   local waste = math.floor(reactor.getFuelConsumedLastTick()*1000+0.5)/1000
  337.   local fuel = math.floor((100/reactor.getFuelAmountMax()) * reactor.getFuelAmount())
  338.   local water = math.floor((100/50000) * reactor.getCoolantAmount())
  339.  
  340.   monitor.setTextColor(colors.white)
  341.   monitor.setCursorPos(3, 9)
  342.   monitor.write("Burnup Rate:")
  343.   monitor.setCursorPos(15, 9)
  344.   monitor.write("| Fuel Left:")
  345.   monitor.setCursorPos(3, 10)
  346.   monitor.setTextColor(colors.yellow)
  347.   monitor.write(waste)
  348.   monitor.setTextColor(colors.white)
  349.   monitor.setCursorPos(15, 10)
  350.   monitor.write("|")
  351.   monitor.setCursorPos(17, 10)
  352.   monitor.setTextColor(colors.red)
  353.   monitor.write(fuel .. " %")
  354.   monitor.setCursorPos(31, 9)
  355.   monitor.setTextColor(colors.cyan)
  356.   monitor.write("Water %")
  357.   monitor.setCursorPos(32, 10)
  358.   monitor.setTextColor(colors.lightBlue)
  359.   monitor.write(water)
  360.   -- END Waste and Fuel Status
  361.  
  362.  
  363.     for key,button in pairs(buttons) do
  364.         button.draw(monitor)
  365.     end
  366.     sleep(0.5)
  367. end
  368. end
  369.  
  370. function interrupt()
  371. while true do
  372. event ={os.pullEvent()}
  373.   if event[1] =="monitor_touch" then
  374.     for key,button in pairs(buttons) do
  375.       if button.clicked(event[3],event[4]) then -- column,row
  376.           reactortoggle()
  377.           button.toggle()
  378.           break -- we found one, so we don't need to keep looking
  379.       end
  380.     end
  381.     end
  382. end
  383. end
  384.  
  385. parallel.waitForAny(update, interrupt)
  386.  
  387.  
  388.  
  389.  
  390.  
  391.   -- Sleep for 1 second
  392.  
  393. --end
Add Comment
Please, Sign In to add comment