kaibochan

ReactorStatus.lua

Feb 21st, 2025 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. local gui = require("/apis/gui")
  2. local Reactor = require("Reactor")
  3.  
  4. require("Instrumentation")
  5. require("Control")
  6.  
  7. function main()
  8.     local monitor_0, monitor_5 = getMonitors()
  9.     local main_display = gui.initializeDisplay(monitor_0)
  10.     local debug_display = gui.initializeDisplay(monitor_5)
  11.    
  12.     local active_element = makeActivityElement(main_display)
  13.     local debug_log = makeDebugLog(debug_display)
  14.  
  15.     local reactor = Reactor.Reactor {
  16.         battery = Reactor.Battery {
  17.             capacity = 10000,
  18.         },
  19.     }
  20.  
  21.     while true do
  22.         local status = CreateReactorStatus(reactor)
  23.         ControlReactor(reactor, status)
  24.         updateActivityElement(status, active_element)
  25.         log(status, debug_log)
  26.  
  27.         main_display:render()
  28.         debug_display:render()
  29.         parallel.waitForAny(delta, gui.doEvents)
  30.     end
  31. end
  32.  
  33. function delta()
  34.     sleep(1)
  35. end
  36.  
  37. function getMonitors()
  38.     local monitor_0 = peripheral.wrap("monitor_0")
  39.     monitor_0.setTextScale(1)
  40.     monitor_0.clear()
  41.     monitor_0.setCursorPos(0, 0)
  42.  
  43.     local monitor_5 = peripheral.wrap("monitor_5")
  44.     monitor_5.setTextScale(0.5)
  45.     monitor_5.clear()
  46.     monitor_5.setCursorPos(0, 0)
  47.  
  48.     return monitor_0, monitor_5
  49. end
  50.  
  51. function makeActivityElement(display)
  52.     local w, h = display.window:getSize()
  53.  
  54.     local active_element = gui.Text:new {
  55.         x = w - 9,
  56.         y = 1,
  57.         width = 8,
  58.         height = 1,
  59.         bg_color = colors.red,
  60.         text_color = colors.white,
  61.     }
  62.     display.window:addElement(active_element)
  63.  
  64.     return active_element
  65. end
  66.  
  67. function makeDebugLog(display)
  68.     local w, h = display.window:getSize()
  69.  
  70.     local debug_log = gui.Text:new {
  71.         width = w,
  72.         height = h,
  73.         auto_scroll = true,
  74.     }
  75.     display.window:addElement(debug_log)
  76.  
  77.     return debug_log
  78. end
  79.  
  80. function updateActivityElement(status, active_element)
  81.     if status.activity then
  82.         active_element:setText(" active ")
  83.         active_element:setBGColor(colors.green)
  84.     else
  85.         active_element:setText("inactive")
  86.         active_element:setBGColor(colors.red)
  87.     end
  88. end
  89.  
  90. function log(status, debug_log)
  91.     debug_log:write("[" .. os.clock() .. "] "
  92.         .. "Reactor Status: " .. (status.activity and "Active" or "Not Active")
  93.         .. ", " .. status.fuel .. " mB"
  94.         .. ", " .. status.waste .. " mB"
  95.         .. ", " .. status.power_stored .. " RF"
  96.         .. "\n")
  97. end
  98.  
  99. main()
Advertisement
Add Comment
Please, Sign In to add comment