Advertisement
jackdell

Reactor Manager

Jan 27th, 2025 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | Source Code | 0 0
  1. -- Author: Jack Dell
  2. -- Website: jackdell.ca
  3. -- Date: 2025-01-27
  4.  
  5. -- Config variables
  6. local LOW_THRESHOLD_PERCENT = 0.3
  7. local HIGH_THRESHOLD_PERCENT = 0.85
  8. local LISTEN_CHANNEL = 1
  9. local RELAY_CHANNEL = 2
  10. local ACTION_RATE = 1
  11.  
  12. -- When false, auto-manage the reactor otherwise run in manual mode (on/off)
  13. local system_override = false
  14.  
  15. -- Connect to the reactor on the back, and modem on the top
  16. local reactor = peripheral.wrap('back') or error('Reactor not found on the back of computer')
  17. local modem = peripheral.wrap('top') or error('Modem not found on the top of computer')
  18.  
  19. -- Bundles information about the reactor into a table and returns it
  20. function getReactorStats()
  21.     local stats = {
  22.         dynamic = not system_override,
  23.         active = reactor.active(),
  24.         temperature = reactor.fuelTemperature(),
  25.         rf_output = reactor.battery().producedLastTick(),
  26.         rf_stored = reactor.battery().stored(),
  27.         rf_capacity = reactor.battery().capacity(),
  28.         fuel_rate = reactor.fuelTank().burnedLastTick(),
  29.         fuel_ammount = reactor.fuelTank().fuel(),
  30.         fuel_capacity = reactor.fuelTank().capacity()
  31.     }
  32.     return stats
  33. end
  34.  
  35. -- Handles the control of the reactor, turning it on if its below the rf stored threshold, off otherwise
  36. function manageReactor()
  37.     while true do
  38.         if system_override then
  39.             os.sleep(ACTION_RATE)
  40.         else
  41.             local s = getReactorStats()
  42.             if s.rf_stored < s.rf_capacity * LOW_THRESHOLD_PERCENT and not s.active then
  43.                 print('RF below threshold, enabling reactor')
  44.                 reactor.setActive(true)
  45.             elseif s.rf_stored > s.rf_capacity * HIGH_THRESHOLD_PERCENT and s.active then
  46.                 print('RF threshold met, disabling reaction')
  47.                 reactor.setActive(false)
  48.             end
  49.         end
  50.         os.sleep(ACTION_RATE)
  51.     end
  52. end
  53.  
  54. -- Gets the reactors information and relays it using a modem
  55. function relayStats()
  56.     while true do
  57.         local stats = getReactorStats()
  58.         modem.transmit(RELAY_CHANNEL, LISTEN_CHANNEL, stats)
  59.         os.sleep(ACTION_RATE)
  60.     end
  61. end
  62.  
  63. -- Updates the system_override variable and sets the reactor state
  64. function setSystemOverride(override, state)
  65.     system_override = override
  66.     reactor.setActive(state)
  67.     print(string.format("System override: %s, Reactor active: %s", tostring(override), tostring(state)))
  68. end
  69.  
  70. -- Handles processing incoming messages from the modem
  71. function handleMessage()
  72.     while true do
  73.         local _, _, recvChannel, _, message = os.pullEvent("modem_message")
  74.         if recvChannel == LISTEN_CHANNEL
  75.             and type(message) == 'table'
  76.             and message.override ~= nil
  77.             and message.state ~= nil
  78.         then
  79.             setSystemOverride(message.override, message.state)
  80.         end
  81.     end
  82. end
  83.  
  84. -- Initalizer
  85. function init()
  86.     modem.open(LISTEN_CHANNEL)
  87.     parallel.waitForAll(handleMessage, relayStats, manageReactor)
  88. end
  89.  
  90. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement