Advertisement
kaibochan

Control.lua

Feb 22nd, 2025
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -- Control.lua
  2. ---------------------------------------------------------------
  3. --- Written 2025
  4. --- This file is part of the Nuclear Reactor Control System I&C
  5. --- For Bigger Reactors on the ATM9TTS 1.1.3 Server
  6. --- Created by NP
  7. ---
  8. --- *scope edits by KB
  9. ---------------------------------------------------------------
  10.  
  11. local Controller = {}
  12.  
  13. function Controller.control(reactor, status, log)
  14.     -- control reactor based on passed status
  15.     -- runs at interval control_delta, make minor control adjustments
  16.    
  17.     -- if power is too high, deactivate reactor
  18.     local power_capacity = reactor.battery().capacity()
  19.     local power_upper_threshold = power_capacity * 0.95
  20.     local power_lower_threshold = power_capacity * 0.05
  21.     if status.power_stored >= power_upper_threshold then
  22.         reactor.setActive(false)
  23.         return
  24.     end
  25.  
  26.     -- if power is too low, activate reactor
  27.     if status.power_stored <= power_lower_threshold then
  28.         reactor.setActive(true)
  29.         return
  30.     end
  31.  
  32.     -- if reactor is active, control rods
  33.     if status.activity then
  34.         -- if heat is too high, step the rods in
  35.         if status.fuel_heat >= 1500 then
  36.             Controller.stepRodsInsert(reactor)
  37.             return
  38.         end
  39.  
  40.         -- if heat is too low, step the rods out
  41.         if status.fuel_heat <= 1000 then
  42.             Controller.stepRodsExtract(reactor)
  43.             return
  44.         end        
  45.     end
  46. end
  47.  
  48. function Controller.stepRodsInsert(reactor)
  49.     -- step rods
  50.     num_rods = reactor.controlRodCount()
  51.  
  52.     for i = 0, num_rods - 1 do
  53.         rod_depth = reactor.getControlRod(i).level()
  54.         if rod_depth < 100 then
  55.             reactor.getControlRod(i).setLevel(rod_depth + 1)
  56.             print("Stepped rod " .. i .. " to depth " .. (rod_depth + 1))
  57.         end
  58.     end
  59. end
  60.  
  61. function Controller.stepRodsExtract(reactor)
  62.     -- step rods
  63.     num_rods = reactor.controlRodCount()
  64.  
  65.     for i = 0, num_rods - 1 do
  66.         rod_depth = reactor.getControlRod(i).level()
  67.         if rod_depth > 0 then
  68.             reactor.getControlRod(i).setLevel(rod_depth - 1)
  69.             print("Stepped rod " .. i .. " to depth " .. (rod_depth - 1))
  70.         end
  71.     end
  72. end
  73.  
  74. return Controller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement