Advertisement
cc-editor

reactor_display

Jan 19th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.24 KB | None | 0 0
  1. settings.load()
  2.  
  3. -- #region Setup
  4. local monitor = peripheral.wrap(settings.get("monitorSide"))
  5. local modem_side = settings.get("rednet.modemSide")
  6. local protocol = settings.get("rednet.protocol")
  7. -- #endregion
  8.  
  9. -- #region Functions
  10. local function init_reactor_display(x_offset, y_offset, reactor)
  11.     print("Initializing reactor display for block " .. reactor.name)
  12.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  13.     monitor.write("Block " .. reactor.name)
  14.  
  15.     if reactor.rods ~= nil then
  16.         for i, _ in ipairs(reactor.rods) do
  17.             monitor.setCursorPos(1 + x_offset + 1, 1 + y_offset + 1 + i)
  18.             monitor.write("Stab " .. i)
  19.         end
  20.     end
  21. end
  22.  
  23. local function init_powerline_display(x_offset, y_offset)
  24.     print("Initializing powerline display")
  25.     monitor.setCursorPos(1+ x_offset, 1 + y_offset)
  26.     monitor.write("Energieabnahme")
  27.     for i = 1, 6 do
  28.         monitor.setCursorPos(1 + x_offset + 1, 1 + y_offset + 1 + i)
  29.         monitor.write("Leitung " .. i)
  30.     end
  31. end
  32.  
  33. local function init_fuel_display(x_offset, y_offset)
  34.     print("Initializing fuel display")
  35.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  36.     monitor.write("Treibstoff")
  37. end
  38.  
  39. local function init_clock_display(x_offset, y_offset)
  40.     print("Initializing clock display")
  41.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  42.     monitor.write("Datenstand")
  43. end
  44.  
  45. local function update_reactor_display(x_offset, y_offset, reactor)
  46.     print("Updating reactor display for block " .. reactor.name)
  47.     if reactor.rods ~= nil then
  48.         for i, rod in ipairs(reactor.rods) do
  49.  
  50.             local message = "unknown"
  51.             if rod.state == nil then
  52.                 monitor.setTextColor(colors.orange)
  53.             elseif rod.state == "active OK" then
  54.                 monitor.setTextColor(colors.green)
  55.                 message = "aktiv OK"
  56.             elseif rod.state == "active ERROR" then
  57.                 monitor.setTextColor(colors.red)
  58.                 message = "aktiv ER"
  59.             elseif rod.state == "inactive OK" then
  60.                 monitor.setTextColor(colors.lightGray)
  61.                 message = "inaktiv OK"
  62.             elseif rod.state == "deactived ERROR" then
  63.                 monitor.setTextColor(colors.red)
  64.                 message = "deaktiv ER"
  65.             elseif rod.state == "starting" then
  66.                 monitor.setTextColor(colors.yellow)
  67.                 message = "startet   "
  68.             else
  69.                 monitor.setTextColor(colors.orange)
  70.             end
  71.  
  72.             monitor.setCursorPos(1 + x_offset + 8, 1 + y_offset + 1 + i)
  73.             monitor.write(message)
  74.             monitor.setTextColor(colors.white)
  75.         end
  76.     end
  77. end
  78.  
  79. local function update_powerline_display(x_offset, y_offset, power_lines)
  80.     print("Updating powerline display")
  81.     for i, value in ipairs(power_lines) do
  82.         local power = "0"
  83.         if value.power ~= nil then
  84.             power = tostring(value.power)
  85.         else
  86.             monitor.setTextColor(colors.red)
  87.             power = "ERR"
  88.         end
  89.         if power == "0" then
  90.             monitor.setTextColor(colors.lightGray)
  91.         end
  92.         for _ = 1, 5 - #power do
  93.             power = " " .. power
  94.         end
  95.         monitor.setCursorPos(1 + x_offset + 11, 1 + y_offset + 1 + i)
  96.         monitor.write(power)
  97.         monitor.setTextColor(colors.white)
  98.     end
  99. end
  100.  
  101. local function update_clock_display(x_offset, y_offset)
  102.     print("Updating clock display")
  103.     monitor.setCursorPos(1 + x_offset + 11, 1 + y_offset)
  104.     local time = textutils.formatTime(os.time(), true)
  105.     if #time == 4 then
  106.         time = "0" .. time
  107.     end
  108.     monitor.write(time)
  109. end
  110.  
  111. local function update_fuel_display(x_offset, y_offset, fuel_amount)
  112.     print("Updating fuel display")
  113.     local amount = "0"
  114.     amount = tostring(fuel_amount)
  115.     for i = 1, 5 - #amount do
  116.         amount = " " .. amount
  117.     end
  118.     if fuel_amount < 64 then
  119.         monitor.setTextColor(colors.red)
  120.     elseif fuel_amount < 256 then
  121.         monitor.setTextColor(colors.yellow)
  122.     else
  123.         monitor.setTextColor(colors.green)
  124.     end
  125.  
  126.     monitor.setCursorPos(1 + x_offset + 11, 1 + y_offset)
  127.     monitor.write(amount)
  128.     monitor.setTextColor(colors.white)
  129. end
  130. -- #endregion
  131.  
  132. -- #region Init
  133. monitor.setTextScale(1)
  134. monitor.setBackgroundColor(colors.black)
  135. monitor.setCursorBlink(false)
  136. monitor.clear()
  137.  
  138. local offsets = {}
  139. for _, id in ipairs({"A", "B", "C", "D"}) do
  140.     offsets[id] = {}
  141. end
  142. offsets["A"].x_offset = 1
  143. offsets["A"].y_offset = 1
  144. offsets["B"].x_offset = 21
  145. offsets["B"].y_offset = 1
  146. offsets["C"].x_offset = 1
  147. offsets["C"].y_offset = 9
  148. offsets["D"].x_offset = 21
  149. offsets["D"].y_offset = 9
  150.  
  151. local power_lines_x_offset = 43
  152. local power_lines_y_offset = 9
  153. local clock_x_offset = 43
  154. local clock_y_offset = 1
  155. local fuel_x_offset = 43
  156. local fuel_y_offset = 3
  157.  
  158. rednet.open(modem_side)
  159. -- #endregion
  160.  
  161. local function update_display()
  162.     local id, msg = rednet.receive(protocol, 12)
  163.  
  164.     if msg == nil then
  165.         print("No updates recieved")
  166.         monitor.setBackgroundColor(colors.red)
  167.         monitor.clear()
  168.         monitor.setTextScale(3)
  169.         monitor.setCursorPos(6, 4)
  170.         monitor.write("NO DATA ...")
  171.         monitor.setBackgroundColor(colors.black)
  172.         return
  173.     end
  174.  
  175.     monitor.clear()
  176.     monitor.setTextScale(1)
  177.     init_clock_display(clock_x_offset, clock_y_offset)
  178.     update_clock_display(clock_x_offset, clock_y_offset)
  179.  
  180.     if msg.fuel ~= nil then
  181.         init_fuel_display(fuel_x_offset, fuel_y_offset)
  182.         update_fuel_display(fuel_x_offset, fuel_y_offset, msg.fuel)
  183.     end
  184.  
  185.     if msg.power_lines ~= nil then
  186.         init_powerline_display(power_lines_x_offset, power_lines_y_offset)
  187.         update_powerline_display(power_lines_x_offset, power_lines_y_offset, msg.power_lines)
  188.     end
  189.  
  190.     if msg.reactors ~= nil then
  191.         for reactor_id, reactor in pairs(msg.reactors) do
  192.             init_reactor_display(offsets[reactor_id].x_offset, offsets[reactor_id].y_offset, reactor)
  193.             update_reactor_display(offsets[reactor_id].x_offset, offsets[reactor_id].y_offset, reactor)
  194.         end
  195.     end
  196. end
  197.  
  198. print("Starting reactor monitoring")
  199. while true do
  200.     update_display()
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement