Advertisement
lemarwin

reactor

Jun 8th, 2025 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.55 KB | Gaming | 0 0
  1. if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
  2. if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
  3. local w = warpdriveCommons.w
  4.  
  5. local data
  6.  
  7. ----------- Reactor support
  8.  
  9. --  0000000001111111111222222222233333333334444444444
  10. --  1234567890123456789012345678901234567890123456789
  11. -- [Reactor stability %      Laser level k           ]
  12. -- [ 99.9 99.9 99.9 99.9     56 123456 123456 123456 ]
  13. -- [ 99.9 99.9 99.9 99.9     123.4 123.4 123.4 123.4 ]
  14. -- [ 99.9 99.9 99.9 99.9     4.5 x 456 123456 123456 ]
  15. -- [ 99.9 99.9 99.9 99.9 123456 123456 123456 123456 ]
  16.  
  17. local reactor
  18. local reactorlasers = {}
  19.  
  20. local reactor_outputMode = "off"
  21. local reactor_outputThreshold = 100
  22. local reactor_targetStability = 50
  23. local reactor_laserAmount = 10000
  24.  
  25. local reactor_output = 0
  26. local tArgs= { ... }
  27.  
  28. local signal_side = tArgs[1] or "right"
  29. local min_stability_str = tArgs[2] or "5.0"
  30. local min_stability = tonumber(min_stability_str)
  31.  
  32. function reactor_read(parData)
  33.   data = parData
  34. end
  35.  
  36. function reactor_boot()
  37.   if reactor ~= nil then
  38.     w.writeLn("Booting Reactor...")
  39.     reactor_outputMode, reactor_outputThreshold = reactor.outputMode()
  40.     reactor_targetStability = 100.0 - reactor.instabilityTarget()
  41.     reactor_laserAmount = reactor.stabilizerEnergy()
  42.   end
  43. end
  44.  
  45. function reactor_cycleOutputMode()
  46.   if reactor_outputMode == "off" then
  47.     reactor_outputMode = "above"
  48.   elseif reactor_outputMode == "above" then
  49.     reactor_outputMode = "at_rate"
  50.   else
  51.     reactor_outputMode = "off"
  52.   end
  53. end
  54.  
  55. local stage = "off"
  56. local preoutput_cycles = 0
  57.  
  58. function run_stage()
  59.     if stage == "off" then
  60.         reactor_stop()
  61.     elseif stage == "warmup" then
  62.         reactor_outputMode = "off"
  63.         reactor_targetStability = 7
  64.         reactor_start()
  65.     elseif stage == "preoutput" then
  66.         preoutput_cycles = 0
  67.         reactor_outputThreshold = 75000
  68.         reactor_outputMode = "above"
  69.         reactor_setMode()
  70.     elseif stage == "output" then
  71.         reactor_outputThreshold = 7500
  72.         reactor_outputMode = "above"
  73.         reactor_setMode()
  74.     end
  75. end
  76.  
  77. function reactor_key(character, keycode)
  78.   if character == 's' or character == 'S' then -- S
  79.     reactor_start()
  80.     return true
  81.   elseif character == 'p' or character == 'P' then -- P
  82.     reactor_stop()
  83.     return true
  84.   elseif character == 'l' or character == 'L' then -- L
  85.     reactor_laser()
  86.     return true
  87.   elseif character == 'o' or character == 'O' then -- O
  88.     reactor_cycleOutputMode()
  89.     reactor_setMode()
  90.     return true
  91.   elseif character == 'g' or character == 'G' then -- G
  92.     reactor_outputThreshold = reactor_outputThreshold - 1000
  93.     reactor_setMode()
  94.     return true
  95.   elseif character == 't' or character == 'T' then -- T
  96.     reactor_outputThreshold = reactor_outputThreshold + 1000
  97.     reactor_setMode()
  98.     return true
  99.   elseif character == 'j' or character == 'J' then -- J
  100.     reactor_laserAmount = reactor_laserAmount - 500
  101.     reactor_setLaser()
  102.     return true
  103.   elseif character == 'u' or character == 'U' then -- U
  104.     reactor_laserAmount = reactor_laserAmount + 500
  105.     reactor_setLaser()
  106.     return true
  107.   elseif character == '-' then -- -
  108.     reactor_targetStability = reactor_targetStability - 1
  109.     reactor_setTargetStability()
  110.     return true
  111.   elseif character == '+' then -- +
  112.     reactor_targetStability = reactor_targetStability + 1
  113.     reactor_setTargetStability()
  114.     return true
  115.   elseif character == 'c' or character == 'C' then -- C
  116.     reactor_config()
  117.     return true
  118.   elseif character == 'a' or character == 'C' then
  119.     if stage == "off" then
  120.         stage = "warmup"
  121.         run_stage()
  122.         return true
  123.     else
  124.         stage = "off"
  125.         run_stage()
  126.         return true
  127.     end
  128.   end
  129.   return false
  130. end
  131.  
  132. function reactor_page()
  133.   w.page_begin(w.data_getName() .. " - Reactor status")
  134.  
  135.   w.setCursorPos(1, 2)
  136.   if reactor == nil or reactor.isInterfaced() == nil then
  137.     w.setColorDisabled()
  138.     w.write("Reactor not detected")
  139.   else
  140.     w.setColorNormal()
  141.     w.write("Reactor stability (%)")
  142.     local instabilities = { reactor.getInstabilities() }
  143.     for key, instability in pairs(instabilities) do
  144.       local y = (key - 1) % 4
  145.       local x = (key - 1 - y) / 4
  146.       w.setCursorPos(2 + 6 * x, 3 + y)
  147.       local stability = math.floor((100.0 - instability) * 10) / 10.0
  148.       if stability >= reactor_targetStability then
  149.         w.setColorSuccess()
  150.       else
  151.         w.setColorWarning()
  152.       end
  153.       w.write(w.format_float(stability, 5))
  154.     end
  155.    
  156.     w.setColorNormal()
  157.     local energyStored, energyMax, energyUnits, _, energyOutputRate = reactor.getEnergyStatus()
  158.     w.setCursorPos(1, 7)
  159.     w.write("Energy   : ")
  160.     w.write(w.format_integer(energyStored, 10) .. " / " .. w.format_integer(energyMax, 10) .. " " .. energyUnits .. " +" .. w.format_integer(reactor_output, 6) .. " " .. energyUnits .. "/t")
  161.     w.setCursorPos(1, 8)
  162.     w.write("Outputing: ")
  163.     w.write(w.format_integer(energyOutputRate, 6) .. " " .. energyUnits .. "/t")
  164.    
  165.     w.setCursorPos(1, 9)
  166.     w.setColorNormal()
  167.     w.write("Activated: ")
  168.     local isEnabled = reactor.enable()
  169.     if isEnabled then w.setColorSuccess() else w.setColorNormal() end
  170.     w.write(w.format_boolean(isEnabled, "YES", "no"))
  171.     w.setColorNormal()
  172.     w.write("; Auto stage: ")
  173.     w.write(stage)
  174.   end
  175.  
  176.   w.setCursorPos(28, 2)
  177.   if #reactorlasers == 0 then
  178.     w.setColorDisabled()
  179.     w.write("Lasers not detected")
  180.   else
  181.     w.setColorNormal()
  182.     w.write("Lasers charge (k)")
  183.    
  184.     for _, reactorlaser in pairs(reactorlasers) do
  185.       if reactorlaser.wrap ~= nil and reactorlaser.side ~= nil then
  186.         local y = reactorlaser.side % 4
  187.         local x = (reactorlaser.side - y) / 4
  188.         w.setCursorPos(28 + 6 * x, 3 + y)
  189.         local energyStored, _, _ = reactorlaser.wrap.getEnergyStatus()
  190.         if energyStored == nil then
  191.           energyStored = -1
  192.         end
  193.         local energy_k = math.floor(energyStored / 100) / 10.0
  194.         if not reactorlaser.wrap.getAssemblyStatus() then
  195.           w.setColorDisabled()
  196.         elseif energyStored > 3 * reactor_laserAmount then
  197.           w.setColorSuccess()
  198.         else
  199.           w.setColorWarning()
  200.         end
  201.         w.write(w.format_integer(energy_k, 5))
  202.       end
  203.     end
  204.   end
  205.  
  206.   w.setCursorPos(1, 10)
  207.   w.setColorNormal()
  208.   w.write("  -----------------------------------------------")
  209.   w.setCursorPos(1, 11)
  210.   w.write("Output mode     : ")
  211.   if reactor_outputMode == "off" then
  212.     w.setColorDisabled()
  213.     w.write("hold")
  214.   elseif reactor_outputMode == "unlimited" then
  215.     w.write("manual/unlimited")
  216.   elseif reactor_outputMode == "above" then
  217.     w.write("surplus above " .. reactor_outputThreshold .. " RF")
  218.   else
  219.     w.write("rated at " .. reactor_outputThreshold .. " RF")
  220.   end
  221.   w.setCursorPos( 1, 12)
  222.   w.setColorNormal()
  223.   w.write("Target stability: " .. reactor_targetStability .. "%")
  224.   w.setCursorPos(30, 12)
  225.   w.write("Laser amount: " .. w.format_integer(reactor_laserAmount))
  226.  
  227.   w.setCursorPos(1, 14)
  228.   w.setColorControl()
  229.   w.writeFullLine(" Start/stoP reactor (S/P), Use lasers (L)")
  230.   w.writeFullLine(" Output mode (O), Configuration (C)")
  231.   w.writeFullLine(" Target stability (+/-), Laser amount (U/J)")
  232.   w.writeFullLine(" Output rate/threshold (T/G)")
  233. end
  234.  
  235. function reactor_setMode()
  236.   if reactor_outputThreshold < 1 then
  237.     reactor_outputThreshold = 1
  238.   elseif reactor_outputThreshold > 100000 then
  239.     reactor_outputThreshold = 100000
  240.   end
  241.   if reactor ~= nil then
  242.       reactor.outputMode(reactor_outputMode, reactor_outputThreshold)
  243.   end
  244. end
  245.  
  246. function reactor_setLaser()
  247.   if reactor_laserAmount < 1 then
  248.     reactor_laserAmount = 1
  249.   elseif reactor_laserAmount > 100000 then
  250.     reactor_laserAmount = 100000
  251.   end
  252.   if reactor ~= nil then
  253.     reactor_laserAmount = reactor.stabilizerEnergy(reactor_laserAmount)
  254.   end
  255. end
  256.  
  257. function reactor_setTargetStability()
  258.   if reactor_targetStability < 1.0 then
  259.     reactor_targetStability = 1.0
  260.   elseif reactor_targetStability > 100.0 then
  261.     reactor_targetStability = 100.0
  262.   end
  263.   if reactor ~= nil then
  264.     reactor_targetStability = 100.0 - reactor.instabilityTarget(100.0 - reactor_targetStability)
  265.   end
  266. end
  267.  
  268. function reactor_start()
  269.   if reactor ~= nil then
  270.     reactor_setMode()
  271.     reactor.enable(true)
  272.   end
  273. end
  274.  
  275. function reactor_stop()
  276.   if reactor ~= nil then
  277.     reactor.enable(false)
  278.   end
  279. end
  280.  
  281. function reactor_laser(side)
  282.   for key, reactorlaser in pairs(reactorlasers) do
  283.     if (side == nil) or (reactorlaser.side == side) then
  284.       reactorlaser.wrap.stabilize(reactor_laserAmount)
  285.     end
  286.   end
  287. end
  288.  
  289. local reactor_configPageLoaded = false
  290. function reactor_pulse(output)
  291.   reactor_output = output
  292.   if reactor == nil or reactor.isInterfaced() == nil then
  293.     w.reboot()
  294.   end
  295.   local instabilities = { reactor.getInstabilities() }
  296.   for key, instability in pairs(instabilities) do
  297.     local stability = math.floor((100.0 - instability) * 10) / 10
  298.     if stability <= min_stability then
  299.         redstone.setOutput("right", true)
  300.     end
  301.   end
  302.   local energyStored, energyMax, energyUnits, _, energyOutputRate = reactor.getEnergyStatus()
  303.   if stage == "warmup" and (energyStored == energyMax) then
  304.     stage = "preoutput"
  305.     run_stage()
  306.   elseif stage == "preoutput" then
  307.     if preoutput_cycles == 10 then
  308.         stage = "output"
  309.         run_stage()
  310.     else
  311.         preoutput_cycles = preoutput_cycles + 1
  312.     end
  313.   end
  314.   if w.page_getCallbackDisplay() == reactor_page and (not reactor_configPageLoaded) then
  315.     for key, instability in pairs(instabilities) do
  316.       local y = (key - 1) % 4
  317.       local x = (key - 1 - y) / 4
  318.       w.setCursorPos(2 + 6 * x, 3 + y)
  319.       local stability = math.floor((100.0 - instability) * 10) / 10
  320.       if stability >= reactor_targetStability then
  321.         w.setColorSuccess()
  322.       else
  323.         w.setColorWarning()
  324.       end
  325.       w.write(w.format_float(stability, 5))
  326.     end
  327.     w.setCursorPos(12, 7)
  328.     w.setColorNormal()
  329.     w.write(w.format_integer(energyStored, 10))
  330.     w.setCursorPos(40, 7)
  331.     w.write(w.format_integer(reactor_output, 6))
  332.     w.setCursorPos(12, 8)
  333.     w.write(w.format_integer(energyOutputRate, 6))
  334.    
  335.     if #reactorlasers ~= 0 then
  336.       for _, reactorlaser in pairs(reactorlasers) do
  337.         if reactorlaser.wrap ~= nil and reactorlaser.side ~= nil then
  338.           local y = reactorlaser.side % 4
  339.           local x = (reactorlaser.side - y) / 4
  340.           w.setCursorPos(28 + 6 * x, 3 + y)
  341.           local energyStored, _, _ = reactorlaser.wrap.getEnergyStatus()
  342.           if energyStored == nil then
  343.             energyStored = -1
  344.           end
  345.           local energy_k = math.floor(energyStored / 100) / 10.0
  346.           if not reactorlaser.wrap.getAssemblyStatus() then
  347.             w.setColorDisabled()
  348.           elseif energyStored > 3 * reactor_laserAmount then
  349.             w.setColorSuccess()
  350.           else
  351.             w.setColorWarning()
  352.           end
  353.           w.write(w.format_integer(energy_k, 5))
  354.         end
  355.       end
  356.     end
  357.   end
  358. end
  359.  
  360. function reactor_config()
  361.   reactor_configPageLoaded = true
  362.   w.page_begin(w.data_getName() .. " - Reactor configuration")
  363.  
  364.   w.setCursorPos(1, 2)
  365.   if reactor == nil or reactor.isInterfaced() == nil then
  366.     w.setColorDisabled()
  367.     w.write("Reactor not detected")
  368.   else
  369.     -- reactor output rate
  370.     w.setCursorPos(1, 6)
  371.     w.setColorHelp()
  372.     w.writeFullLine(" Enter a positive number.")
  373.    
  374.     w.setCursorPos(1, 4)
  375.     w.setColorNormal()
  376.     w.write("Reactor output rate (" .. w.format_integer(reactor_outputThreshold) .. " RF): ")
  377.     reactor_outputThreshold = w.input_readInteger(reactor_outputThreshold)
  378.     reactor_setMode()
  379.     w.setCursorPos(1, 5)
  380.     w.write("Reactor output rate set")
  381.     w.setCursorPos(1, 6)
  382.     w.writeFullLine(" ")
  383.    
  384.     -- laser amount
  385.     w.setCursorPos(1, 9)
  386.     w.setColorHelp()
  387.     w.writeFullLine(" Enter a positive number.")
  388.    
  389.     w.setCursorPos(1, 7)
  390.     w.setColorNormal()
  391.     w.write("Laser energy level (" .. w.format_integer(reactor_laserAmount) .. "): ")
  392.     reactor_laserAmount = w.input_readInteger(reactor_laserAmount)
  393.     reactor_setLaser()
  394.     w.setCursorPos(1, 8)
  395.     w.write("Laser energy level set")
  396.     w.setCursorPos(1, 9)
  397.     w.writeFullLine(" ")
  398.    
  399.     -- target stability
  400.     w.setCursorPos(1, 12)
  401.     w.setColorHelp()
  402.     w.writeFullLine(" Enter a positive number.")
  403.    
  404.     w.setCursorPos(1, 10)
  405.     w.setColorNormal()
  406.     w.write("Reactor target stability (" .. w.format_integer(reactor_targetStability) .. "%): ")
  407.     reactor_targetStability = w.input_readInteger(reactor_targetStability)
  408.     reactor_setTargetStability()
  409.     w.setCursorPos(1, 11)
  410.     w.write("Reactor target stability set")
  411.     w.setCursorPos(1, 12)
  412.     w.writeFullLine(" ")
  413.   end
  414.   reactor_configPageLoaded = false
  415. end
  416.  
  417. function reactor_register()
  418.   w.device_register("warpdriveEnanReactorCore",
  419.       function(deviceType, address, wrap) reactor = wrap end,
  420.       function() end)
  421.   w.device_register("warpdriveEnanReactorLaser",
  422.       function(deviceType, address, wrap) table.insert(reactorlasers, { side = wrap.side(), wrap = wrap }) end,
  423.       function() end)
  424.   w.event_register("reactorPulse"       , function(eventName, param) reactor_pulse(param)                        return false end )
  425.   w.event_register("reactorDeactivation", function(                ) w.status_showWarning("Reactor deactivated") return false end )
  426.   w.event_register("reactorActivation"  , function(                ) w.status_showWarning("Reactor activated")   return false end )
  427.   w.data_register("reactor", reactor_read, nil, nil)
  428. end
  429.  
  430. ----------- connections status
  431.  
  432. function connections_page(isBooting)
  433.   w.page_begin(w.data_getName() .. " - Connections")
  434.  
  435.   w.writeLn("")
  436.  
  437.   local monitors = w.device_getMonitors()
  438.   if #monitors == 0 then
  439.     w.setColorDisabled()
  440.     w.writeLn("No Monitor detected")
  441.   elseif #monitors == 1 then
  442.     w.setColorSuccess()
  443.     w.writeLn("1 monitor detected")
  444.   else
  445.     w.setColorSuccess()
  446.     w.writeLn(#monitors .. " Monitors detected")
  447.   end
  448.  
  449.   if reactor == nil or reactor.isInterfaced() == nil then
  450.     w.setColorDisabled()
  451.     w.writeLn("No Enantiomorphic reactor detected")
  452.   else
  453.     w.setColorSuccess()
  454.     w.writeLn("Enantiomorphic reactor detected")
  455.     if isBooting then
  456.       reactor_boot()
  457.     end
  458.   end
  459.  
  460.   if #reactorlasers == 0 then
  461.     w.setColorDisabled()
  462.     w.writeLn("No reactor stabilisation laser detected")
  463.   elseif #reactorlasers == 1 then
  464.     w.setColorSuccess()
  465.     w.writeLn("1 reactor stabilisation laser detected")
  466.   else
  467.     w.setColorSuccess()
  468.     w.writeLn(#reactorlasers .. " reactor stabilisation lasers detected")
  469.   end
  470.  
  471.   w.writeLn("")
  472.   w.setColorNormal()
  473.   w.writeLn("This is a keyboard controlled user interface.")
  474.   w.write("Key controls are written like so: ")
  475.   w.setColorControl()
  476.   w.write("Action (key)")
  477.   w.setColorNormal()
  478.   w.writeLn(".")
  479.   w.write("For example, typing ")
  480.   w.setColorControl()
  481.   w.write(" 1 ")
  482.   w.setColorNormal()
  483.   w.writeLn(" will open Reactor controls.")
  484. end
  485.  
  486. ----------- Boot sequence
  487.  
  488. w.page_setEndText(" Home (0), Reactor controls (1)")
  489. w.page_register('0', connections_page, nil)
  490. w.page_register('1', reactor_page, reactor_key)
  491. reactor_register()
  492.  
  493. w.boot()
  494. local success, message = pcall(w.run)
  495. if not success then
  496.   print("failed with message")
  497.   print(message)
  498.   w.sleep(3.0)
  499.   print("rebooting...")
  500.   w.reboot()
  501. else
  502.   w.close()
  503. end
  504.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement