neuroticfox

DC 9.5b

Apr 27th, 2022 (edited)
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.67 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local gpu = component.gpu
  5. local screen = component.screen
  6. local unicode= require("unicode")
  7.  
  8.  -- DynamicRes
  9.  
  10. local ratioX, ratioY = screen.getAspectRatio()
  11. local maxX, maxY = gpu.maxResolution()
  12. gpu.setResolution(math.min(ratioX*55, maxX), math.min(ratioY*25,maxY))
  13.  
  14.  -- Safety Checks
  15.  
  16. if not component.isAvailable("draconic_reactor") then
  17.   print("Reactor not connected. Please connect computer to reactor with an Adapter block.")
  18.   os.exit()
  19. end
  20. reactor = component.draconic_reactor
  21. local flux_gates = {}
  22. for x,y in pairs(component.list("flux_gate")) do
  23.   flux_gates[#flux_gates+1] = x
  24. end
  25. if #flux_gates < 2 then
  26.   print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  27.   os.exit()
  28. end
  29. flux_in = component.proxy(flux_gates[1])
  30. flux_out = component.proxy(flux_gates[2])
  31. if not flux_in or not flux_out then
  32.   print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  33.   os.exit()
  34. end
  35.  
  36.  -- Functions
  37.  
  38. function exit_msg(msg)
  39.   term.clear()
  40.   print(msg)
  41.   os.exit()
  42. end
  43.  
  44. function modify_temp(offset)
  45.   local new_temp = ideal_temp + offset
  46.   if new_temp > 8000 then
  47.     new_temp = 8000
  48.   elseif new_temp < 2000 then
  49.     new_temp = 2000
  50.   end
  51.   ideal_temp = new_temp
  52. end
  53.  
  54.  
  55.  
  56. local bypassfield = 0
  57. local chaosmode = 0
  58.  
  59. function modify_field(offset)
  60.   local new_strength = ideal_strength + offset
  61.   if new_strength > 99 then
  62.     new_strength = 99
  63.   elseif new_strength < 75 and chaosmode == 1 then
  64.     new_strength = 75
  65.   elseif new_strength < 0.5 then
  66.     new_strength = 0.5
  67.   end
  68.   ideal_strength = new_strength
  69. end
  70.  
  71.  -- Buttons
  72.  
  73. local adj_button_width = 19
  74. local temp_adjust_x_offset = 62
  75. local temp_adjust_y_offset = 2
  76. local field_adjust_x_offset = temp_adjust_x_offset + adj_button_width + 2
  77. local field_adjust_y_offset = 2
  78. local status = "PeFi"
  79. local lowest_field = 100
  80. local lowest_fuel = 100
  81. local highest_temp = 0
  82. local highest_sat = 0
  83. local highest_outflow = 0
  84. local cutoff_field = 0.75
  85. local highest_use = 0.1
  86.  
  87.       -- Inflow PID
  88. local proportional_field_error = 0
  89. local inflow_I_sum = 0
  90. local integral_field_error = 0
  91. local derivative_field_error = 0
  92. local inflow_D_last = 0
  93. local inflow_correction = 0
  94.  
  95.     -- Outflow PID
  96. local proportional_temp_error = 0
  97. local outflow_I_sum = 0
  98. local integral_temp_error = 0
  99. local derivative_temp_error = 0
  100. local outflow_D_last = 0
  101. local outflow_correction = 0
  102.  
  103. local buttons = {
  104.   start={
  105.     x=2,
  106.     y=30,
  107.     width=18,
  108.     height=1,
  109.     text="Start",
  110.     action=function()
  111.       if safe then
  112.             chaosmode = 0
  113.             shutdownE = 0
  114.             ideal_strength = 15
  115.             cutoff_field = 0.4
  116.             ideal_temp = 8000
  117.             lowest_field = 99
  118.         state = "CHRG"
  119.         reactor.chargeReactor()
  120.         shutdownE = 0
  121.       elseif shutting_down then
  122.             chaosmode = 0
  123.             shutdownE = 0
  124.             ideal_strength = 15
  125.             cutoff_field = 0.4
  126.             ideal_temp = 8000
  127.             lowest_field = 99
  128.         state = "ACTV"
  129.         reactor.activateReactor()
  130.       end
  131.     end,
  132.     condition=function() return safe or shutting_down end
  133.   },
  134.   shutdown={
  135.     x=2,
  136.     y=30,
  137.     width=18,
  138.     height=1,
  139.     text="Shutdown",
  140.     action=function()
  141.     cutoff_temp = 8001
  142.     ideal_temp = 8000
  143.     ideal_strength = 15
  144.     cutoff_field = 0.75
  145.     chaosmode = 0
  146.     shutdownE = 1
  147.       state = "MASD"
  148.       reactor.stopReactor()
  149.     end,
  150.     condition=function() return running end
  151.   },
  152.   chaosmode={
  153.     x=2,
  154.     y=32,
  155.     width=18,
  156.     height=1,
  157.     text=" Chaos Mode",
  158.     action=function()
  159.         if chaosmode == 0 then
  160.             chaosmode = 1
  161.             shutdownE = 0
  162.             cutoff_temp = 19750
  163.             ideal_strength = 99
  164.             cutoff_field = 20
  165.             ideal_temp = 55537
  166.             lowest_field = 99
  167.         elseif chaosmode == 1 then
  168.             chaosmode = 0
  169.             shutdownE = 0
  170.             ideal_strength = 15
  171.             cutoff_field = 0.4
  172.             ideal_temp = 8000
  173.             lowest_field = 99
  174.         end
  175.     end,
  176.     condition=function() return running end
  177.   },
  178.     analytics={
  179.     x=42,
  180.     y=30,
  181.     width=18,
  182.     height=1,
  183.     text="Reset Analytics",
  184.     action=function()
  185.       highest_temp = 0
  186.       lowest_field = 200
  187.       highest_outflow = 0
  188.       highest_sat = 0
  189.       status = "PeFi"
  190.     end,
  191.   },
  192.     force_exit={
  193.     x=22,
  194.     y=30,
  195.     width=18,
  196.     height=1,
  197.     text="Force Exit",
  198.     action=function()
  199.         chaosmode = 0
  200.         shutdownE = 0
  201.         ideal_strength = 99
  202.         cutoff_field = 0.4
  203.         ideal_temp = 8000
  204.         lowest_field = 99
  205.       state = "MASD"
  206.       reactor.stopReactor()
  207.       gpu.setResolution(gpu.maxResolution())
  208.       event_loop = false
  209.     end,
  210.     condition=function() return running or shutting_down end
  211.   },
  212.     update={
  213.     x=22,
  214.     y=32,
  215.     width=18,
  216.     height=1,
  217.     text="Update",
  218.     action=function()
  219.         reactor.stopReactor()
  220.         os.execute("pastebin get -f 8gYugLnR dc10; cls; rm dc9; dc10")
  221.     end,
  222.   },
  223.     switch_gates={
  224.     x=2,
  225.     y=32,
  226.     width=18,
  227.     height=1,
  228.     text="Swap flux gates",
  229.     action=function()
  230.       cutoff_temp = 10500
  231.       local old_addr = flux_in.address
  232.       flux_in = component.proxy(flux_out.address)
  233.       flux_out = component.proxy(old_addr)
  234.     end,
  235.     condition=function() return safe end
  236.   },
  237.     exit={
  238.     x=22,
  239.     y=30,
  240.     width=18,
  241.     height=1,
  242.     text="Exit",
  243.     action=function()
  244.       event_loop = false
  245.     end,
  246.     condition=function() return safe end
  247.   },
  248.   temp_up_max={
  249.     x=temp_adjust_x_offset,
  250.     y=temp_adjust_y_offset,
  251.     width=adj_button_width,
  252.     height=1,
  253.     text="Maximum",
  254.     action=function()
  255.       ideal_temp = 8000
  256.     end
  257.   },
  258.   temp_up_thousand={
  259.     x=temp_adjust_x_offset,
  260.     y=temp_adjust_y_offset+2,
  261.     width=adj_button_width,
  262.     height=1,
  263.     text="+1000",
  264.     action=function() modify_temp(1000) end
  265.   },
  266.   temp_up_hundred={
  267.     x=temp_adjust_x_offset,
  268.     y=temp_adjust_y_offset+4,
  269.     width=adj_button_width,
  270.     height=1,
  271.     text="+100",
  272.     action=function() modify_temp(100) end
  273.   },
  274.   temp_up_ten={
  275.     x=temp_adjust_x_offset,
  276.     y=temp_adjust_y_offset+6,
  277.     width=adj_button_width,
  278.     height=1,
  279.     text="+10",
  280.     action=function() modify_temp(10) end
  281.   },
  282.   temp_up_one={
  283.     x=temp_adjust_x_offset,
  284.     y=temp_adjust_y_offset+8,
  285.     width=adj_button_width,
  286.     height=1,
  287.     text="+1",
  288.     action=function() modify_temp(1) end
  289.   },
  290.   temp_down_thousand={
  291.     x=temp_adjust_x_offset,
  292.     y=temp_adjust_y_offset+18,
  293.     width=adj_button_width,
  294.     height=1,
  295.     text="-1000",
  296.     action=function() modify_temp(-1000) end
  297.   },
  298.     temp_down_max={
  299.     x=temp_adjust_x_offset,
  300.     y=temp_adjust_y_offset+20,
  301.     width=adj_button_width,
  302.     height=1,
  303.     text="Minimum",
  304.     action=function() modify_temp(-20000) end
  305.   },
  306.   temp_down_hundred={
  307.     x=temp_adjust_x_offset,
  308.     y=temp_adjust_y_offset+16,
  309.     width=adj_button_width,
  310.     height=1,
  311.     text="-100",
  312.     action=function() modify_temp(-100) end
  313.   },
  314.   temp_down_ten={
  315.     x=temp_adjust_x_offset,
  316.     y=temp_adjust_y_offset+14,
  317.     width=adj_button_width,
  318.     height=1,
  319.     text="-10",
  320.     action=function() modify_temp(-10) end
  321.   },
  322.   temp_down_one={
  323.     x=temp_adjust_x_offset,
  324.     y=temp_adjust_y_offset+12,
  325.     width=adj_button_width,
  326.     height=1,
  327.     text="-1",
  328.     action=function() modify_temp(-1) end
  329.   },
  330.   field_up_ten={
  331.     x=field_adjust_x_offset,
  332.     y=field_adjust_y_offset+3,
  333.     width=adj_button_width,
  334.     height=1,
  335.     text="+10",
  336.     action=function() modify_field(10) end
  337.   },
  338.     field_up_one={
  339.     x=field_adjust_x_offset,
  340.     y=field_adjust_y_offset+5,
  341.     width=adj_button_width,
  342.     height=1,
  343.     text="+1",
  344.     action=function() modify_field(1) end
  345.   },
  346.   field_up_tenth={
  347.     x=field_adjust_x_offset,
  348.     y=field_adjust_y_offset+7,
  349.     width=adj_button_width,
  350.     height=1,
  351.     text="+0.1",
  352.     action=function() modify_field(0.1) end
  353.   },
  354.   field_down_ten={
  355.     x=field_adjust_x_offset,
  356.     y=field_adjust_y_offset+17,
  357.     width=adj_button_width,
  358.     height=1,
  359.     text="-10",
  360.     action=function() modify_field(-10) end
  361.   },
  362.     field_down_one={
  363.     x=field_adjust_x_offset,
  364.     y=field_adjust_y_offset+15,
  365.     width=adj_button_width,
  366.     height=1,
  367.     text="-1",
  368.     action=function() modify_field(-1) end
  369.   },
  370.   field_down_tenth={
  371.     x=field_adjust_x_offset,
  372.     y=field_adjust_y_offset+13,
  373.     width=adj_button_width,
  374.     height=1,
  375.     text="-0.1",
  376.     action=function() modify_field(-0.1) end
  377.   }
  378. }
  379.  
  380.  -- main code
  381.  
  382. flux_in.setFlowOverride(0)
  383. flux_out.setFlowOverride(0)
  384. flux_in.setOverrideEnabled(true)
  385. flux_out.setOverrideEnabled(true)
  386.  
  387. local condition = reactor.getReactorInfo()
  388. if not condition then
  389.   print("Reactor not initialized, please ensure the stabilizers are properly laid out.")
  390.   os.exit()
  391. end
  392.  
  393. ideal_strength = 15
  394.  
  395. ideal_temp = 8000
  396. cutoff_temp = 8001
  397.  
  398.  -- tweakable pid gains
  399.  
  400. inflow_P_gain = 1
  401. inflow_I_gain = 0.04
  402. inflow_D_gain = 0.05
  403.  
  404. outflow_P_gain = 500
  405. outflow_I_gain = 0.10
  406. outflow_II_gain = 0.0000003
  407. outflow_D_gain = 30000
  408.  
  409.  -- initialize main loop
  410.  
  411. inflow_I_sum = 0
  412. inflow_D_last = 0
  413.  
  414. outflow_I_sum = 0
  415. outflow_II_sum = 0
  416. outflow_D_last = 0
  417.  
  418. state = "STBY"
  419. shutting_down = false
  420.  
  421. if condition.temperature > 25 then
  422.   state = "CLNG"
  423. end
  424. if condition.temperature > 2000 then
  425.   state = "ACTV"
  426. end
  427.  
  428.  -- Possible states:
  429.   --Standby
  430.   --Charging
  431.   --Active
  432.   --Manual Shutdown
  433.   --Emergency Shutdown
  434.   --Cooling
  435.  
  436. event_loop = true
  437. while event_loop do
  438.  
  439.   if not component.isAvailable("draconic_reactor") then
  440.     exit_msg("Reactor disconnected, exiting")
  441.   end
  442.  
  443.   if not component.isAvailable("flux_gate") then
  444.     exit_msg("Flux gates disconnected, exiting")
  445.   end
  446.  
  447.     local info = reactor.getReactorInfo()
  448.  
  449.  -- Highest Heat Value
  450.  
  451. if info.temperature > highest_temp then
  452.   highest_temp = info.temperature
  453. end
  454.  
  455.  -- Highest Sat Value
  456.  
  457. if ((info.energySaturation / info.maxEnergySaturation) * 100) > highest_sat then
  458.   highest_sat = ((info.energySaturation / info.maxEnergySaturation) * 100)
  459. end
  460.  
  461.  -- Lowest Field Value ((1 - info.fuelConversion / info.maxFuelConversion) * 100)
  462.  
  463. if ((info.fieldStrength / info.maxFieldStrength) * 100) < lowest_field then
  464.   lowest_field = ((info.fieldStrength / info.maxFieldStrength) * 100)
  465. end
  466.  
  467.  -- Lowest Fuel Value
  468.  
  469. if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < lowest_fuel then
  470.   lowest_fuel = ((1 - info.fuelConversion / info.maxFuelConversion) * 100)
  471. end
  472.  
  473.  -- Lowest Fuel Use Rate
  474. if info.fuelConversionRate > highest_use then
  475.     highest_use = info.fuelConversionRate
  476. end
  477.  
  478.   local inflow = 0
  479.   local outflow = 0
  480.  
  481.   shutting_down = state == "MASD" or state == "ESHD"
  482.   running = state == "CHRG" or state == "ACTV"
  483.   safe = state == "STBY" or state == "CLNG"
  484.  
  485.   if state == "CHRG" then
  486.     inflow = 5000000
  487.  
  488.     if info.temperature > 2000 then
  489.       reactor.activateReactor()
  490.       state = "ACTV"
  491.     end
  492.   elseif state == "CLNG" then
  493.     if info.temperature < 25 then
  494.       state = "STBY"
  495.     end
  496.     inflow = 10
  497.     outflow = 20
  498.   elseif state == "STBY" then
  499.     inflow = 10
  500.     outflow = 20
  501.   else
  502.     -- adjust inflow rate based on field strength
  503.  
  504.     field_error = (info.maxFieldStrength * (ideal_strength / 100)) - info.fieldStrength
  505.     proportional_field_error = field_error * inflow_P_gain
  506.     inflow_I_sum = inflow_I_sum + field_error
  507.     integral_field_error = inflow_I_sum * inflow_I_gain
  508.     derivative_field_error = (field_error - inflow_D_last) * inflow_D_gain
  509.     inflow_D_last = field_error
  510.     inflow_correction = proportional_field_error + integral_field_error + derivative_field_error
  511.     if inflow_correction < 0 then
  512.       inflow_I_sum = inflow_I_sum - field_error
  513.     end
  514.     inflow = inflow_correction
  515.  
  516.     if not shutting_down then
  517.  
  518.       -- adjust outflow rate based on core temperature
  519.  
  520.       temp_error = ideal_temp - info.temperature
  521.       proportional_temp_error = temp_error * outflow_P_gain
  522.       outflow_I_sum = outflow_I_sum + temp_error
  523.       integral_temp_error = outflow_I_sum * outflow_I_gain
  524.       if math.abs(temp_error) < 100 then
  525.         outflow_II_sum = outflow_II_sum + integral_temp_error
  526.       else
  527.         outflow_II_sum = 0
  528.       end
  529.       second_integral_temp_error = outflow_II_sum * outflow_II_gain
  530.       derivative_temp_error = (temp_error - outflow_D_last) * outflow_D_gain
  531.       outflow_D_last = temp_error
  532.       outflow_correction = proportional_temp_error + integral_temp_error + second_integral_temp_error + derivative_temp_error
  533.       if outflow_correction < 0 then
  534.         outflow_I_sum = outflow_I_sum - temp_error
  535.       end
  536.       outflow = outflow_correction
  537.  
  538.       -- cut off reactor in case of emergency
  539.  
  540.       if info.temperature > cutoff_temp then
  541.         print("Reactor Too Hot, shutting down")
  542.         state = "ESHD"
  543.         status = "HiTe"
  544.         reactor.stopReactor()
  545.       end
  546.       if ((info.fieldStrength / info.maxFieldStrength) * 100) < cutoff_field then
  547.         print("Reactor Field Has Failed, Failsafe Activated, Shutting Down")
  548.         state = "ESHD"
  549.         status = "LoFi"
  550.         reactor.stopReactor()
  551.       end
  552.       if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < 1.25 and chaosmode == 0 then
  553.         print("Reactor Fuel Low, Shutting Down")
  554.       state = "ESHD"
  555.       status = "LoFu"
  556.       reactor.stopReactor()
  557.       elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < 5 and chaosmode == 1 then
  558.         print("Reactor Fuel Low, Shutting Down")
  559.       state = "ESHDn"
  560.       status = "LoFu"
  561.       reactor.stopReactor()
  562.       end
  563.     else
  564.       if info.temperature < 2000 then
  565.         state = "CLNG"
  566.       end
  567.     end
  568.   end
  569.  
  570.   if state ~= "ACTV" and not shutting_down then
  571.     inflow_I_sum = 0
  572.     inflow_D_last = 0
  573.     outflow_I_sum = 0
  574.     outflow_II_sum = 0
  575.     outflow_D_last = 0
  576.   end
  577.  
  578.   if inflow < 0 then
  579.     inflow = 0
  580.   end
  581.   if outflow < 0 then
  582.     outflow = 0
  583.   end
  584.  
  585.   inflow = math.floor(inflow)
  586.   outflow = math.floor(outflow)
  587.  
  588.   flux_in.setFlowOverride(inflow)
  589.   flux_out.setFlowOverride(outflow)
  590.  
  591.   -- Draw screen
  592.  
  593.   if term.isAvailable() then
  594.  
  595.     -- Draw Values
  596.  
  597. function modify_eff(offset)
  598.   local eff = ((outflow / inflow) * 100)
  599.   if eff > 100000 then
  600.     eff = 1
  601.   end
  602. end
  603.  
  604.     -- Get Temp Rise
  605.  
  606. oldTemp = currentTemp or info.temperature
  607. currentTemp = info.temperature
  608. oldTempRate = tempChangeRate or currentTemp - oldTemp
  609. tempChangeRate = currentTemp - oldTemp
  610. tempAccel = tempChangeRate - oldTempRate
  611. if tempAccel == 0 then
  612.     tempAccel = 0.001
  613. end
  614.    
  615.     -- Get Fuel Use Rate
  616.  
  617. oldFuel = currentFuel or (info.maxFuelConversion - info.fuelConversion)
  618. currentFuel = (info.maxFuelConversion - info.fuelConversion)
  619. oldUseRate = fuelUseRate or math.max(info.fuelConversionRate*20, 0.1)
  620. fuelUseRate = math.max(info.fuelConversionRate*20, 0.1)
  621. fuelAccel = math.max(fuelUseRate - oldUseRate, 0.1)
  622.  
  623.  -- Fuel Conversion Rate
  624.  
  625. if info.fuelConversionRate > 999 then
  626.     fuelConversionRate = (info.fuelConversionRate / 1000)
  627.     fuelMeasure = (unicode.char(956)).." b/t"
  628.     elseif info.fuelConversionRate > 999999 then
  629.     fuelConversionRate = (info.fuelConversionRate / 1000000)
  630.     fuelMeasure = " mb/t"
  631.     else
  632.     fuelConversionRate = info.fuelConversionRate
  633.     fuelMeasure = " nb/t"
  634. end
  635.  
  636.  -- Inflow Modification
  637. if chaosmode == 1 or info.temperature > 10000 then
  638. inflowMod = -1
  639. else inflowMod = inflow
  640. end
  641.  
  642.  -- Outflow Modification
  643. if chaosmode == 1 then
  644. outflowMod = -1
  645. else outflowMod = outflow
  646. end
  647.  
  648. if chaosmode == 1 then
  649. profitMod = -1
  650. else profitMod = (outflow - inflow)
  651. end
  652.  
  653.  -- Efficiency Modification
  654.  
  655. if chaosmode == 1 then
  656. efficiencyMod = -1
  657. else efficiencyMod = ((outflow / inflow) * 100)
  658. end
  659.  
  660.     -- DrawData
  661.    
  662.     --local secondsToExpire = (fuelUseRate + math.abs(fuelUseRate^2 - 2*fuelAccel*currentFuel)^0.5)/fuelAccel
  663.     local secondsToExpire = (info.maxFuelConversion - info.fuelConversion) / math.max(info.fuelConversionRate*0.00002, 0.00001)
  664.  
  665.     local left_margin = 2
  666.     local spacing = 1
  667.     local values = {
  668.               "- v9.5b [Y8gPvV48]",
  669.               " ",
  670.               "                   Reactor Statistics",
  671.               "┌───────────────────────────┬────────────────────────────┐",
  672. string.format("│Time Until Refuel:         │  %5dd, %2dh, %2dm, %2ds     │", secondsToExpire/86400, secondsToExpire   /3600 % 24, secondsToExpire/60 % 60, secondsToExpire % 60),
  673. string.format("│Ideal Field:               │           %5.1f%%           │", ideal_strength),
  674. string.format("│Current Field:             │         %5.1f%%             │", ((info.fieldStrength / info.maxFieldStrength) * 100)),
  675.               "├───────────────────────────┼────────────────────────────┤",
  676. string.format("│Fuel Remaining:            │           %7.3f%%         │", ((1 - info.fuelConversion / info.maxFuelConversion) * 100)),
  677. string.format("│Fuel Use Rate:             │           %7.3f" .. fuelMeasure .. "     │", fuelConversionRate),
  678.               "├───────────────────────────┼────────────────────────────┤",
  679. string.format("│Temperature                │   %7.1f°c [%7.1f°f]    │", info.temperature, ((info.temperature * 1.8) + 32)),
  680. string.format("│Ideal Temperature:         │   %7.1f°c [%7.1f°f]    │", ideal_temp, ((ideal_temp * 1.8) + 32)),
  681.               "├───────────────────────────┼────────────────────────────┤",
  682. string.format("│Energy Input:              │   %12.1f RF/t        │", inflowMod),
  683. string.format("│Energy Output:             │   %12.1f RF/t        │", outflowMod),
  684. string.format("│Energy Efficiency:         │   %12.1f%%            │", efficiencyMod),
  685. string.format("│Energy Profit:             │  %13.1f RF/t        │", profitMod),
  686.               "└───────────────────────────┴────────────────────────────┘",
  687.               "                    Debug Information                    ",
  688.               "┌───────────────────────────┬────────────────────────────┐",
  689.               "│Status:                    │        " .. state .. "-" .. status .. "           │",
  690. string.format("│Max Field Drop:            │        %6.2f%%             │", lowest_field),
  691. string.format("│Lowest Recorded Fuel:      │        %6.2f%%             │", lowest_fuel),
  692. string.format("│Max Temp Spike:            │      %8.2f              │", highest_temp),
  693. string.format("│Max Saturation:            │        %6.2f%%             │", highest_sat),
  694. string.format("│Max Fuel Use:              │     %9.2f nb/t         │", highest_use),
  695.               "└───────────────────────────┴────────────────────────────┘",
  696.               " ",
  697.               " ",
  698.               " ",
  699.               " ",
  700.               " ",
  701.               " ",
  702.               " ",
  703.               " ",
  704.               " ",
  705.               " ",
  706.               " ",
  707.               " ",
  708.               " ",
  709. string.format("Max Fuel [nb]:       %4.3f", (info.maxFuelConversion * 1000000)),
  710. string.format("Fuel Remaining [nb]: %4.3f", ((info.maxFuelConversion - info.fuelConversion) * 1000000)),
  711. string.format("Temperature Rise: %4.3f", tempAccel),
  712. "                                         PID Values",
  713.               " ",
  714. string.format("I: P%12.1f, IS%12.1f, I%12.1f, D%12.1f, DL%12.1f, C%12.1f", proportional_field_error, inflow_I_sum, integral_field_error, derivative_field_error, inflow_D_last, inflow_correction),
  715. string.format("O: P%12.1f, IS%12.1f, I%12.1f, D%12.1f, DL%12.1f, C%12.1f", proportional_temp_error, outflow_I_sum, integral_temp_error, derivative_temp_error, outflow_D_last, outflow_correction),
  716. }
  717.  
  718.  
  719.     term.clear()
  720.  
  721.     for i, v in ipairs(values) do
  722.       term.setCursor(left_margin, i * spacing)
  723.       term.write(v)
  724.     end
  725.  
  726.     -- Draw button values
  727.  
  728.     term.setCursor(temp_adjust_x_offset, temp_adjust_y_offset+10)
  729.     term.write("Reactor Temperature")
  730.     term.setCursor(field_adjust_x_offset+1, field_adjust_y_offset+10)
  731.     term.write("Field Strength")
  732.  
  733.     -- Draw Buttons
  734.  
  735.     gpu.setForeground(0x000000)
  736.  
  737.     for bname, button in pairs(buttons) do
  738.       if button.depressed then
  739.  
  740.         button.depressed = button.depressed - 1
  741.         if button.depressed == 0 then
  742.           button.depressed = nil
  743.         end
  744.       end
  745.       if button.condition == nil or button.condition() then
  746.         local center_color = 0xAAAAAA
  747.         local highlight_color = 0xCCCCCC
  748.         local lowlight_color = 0x808080
  749.         if button.depressed then
  750.           center_color = 0x999999
  751.           highlight_color = 0x707070
  752.           lowlight_color = 0xBBBBBB
  753.         end
  754.         gpu.setBackground(center_color)
  755.         gpu.fill(button.x, button.y, button.width, button.height, " ")
  756.         if button.width > 1 and button.height > 1 then
  757.           gpu.setBackground(lowlight_color)
  758.           gpu.fill(button.x+1, button.y+button.height-1, button.width-1, 1, " ")
  759.           gpu.fill(button.x+button.width-1, button.y, 1, button.height, " ")
  760.           gpu.setBackground(highlight_color)
  761.           gpu.fill(button.x, button.y, 1, button.height, " ")
  762.           gpu.fill(button.x, button.y, button.width, 1, " ")
  763.         end
  764.         gpu.setBackground(center_color)
  765.         term.setCursor(button.x + math.floor(button.width / 2 - #button.text / 2), button.y + math.floor(button.height / 2))
  766.         term.write(button.text)
  767.       end
  768.     end
  769.  
  770.     gpu.setBackground(0x000000)
  771.     gpu.setForeground(0xFFFFFF)
  772.   end
  773.  
  774.   -- Wait for next tick, or manual shutdown
  775.  
  776.   local event, id, op1, op2 = event.pull(0.05)
  777.   if event == "interrupted" then
  778.     if safe then
  779.       break
  780.     end
  781.   elseif event == "touch" then
  782.  
  783.     -- Handle Button Presses
  784.  
  785.     local x = op1
  786.     local y = op2
  787.  
  788.     for bname, button in pairs(buttons) do
  789.       if (button.condition == nil or button.condition()) and x >= button.x and x <= button.x + button.width and y >= button.y and y <= button.y + button.height then
  790.         button.action()
  791.         button.depressed = 3
  792.       end
  793.     end
  794.   end
  795.   os.sleep()
  796. end
  797.  
  798. term.clear()
Add Comment
Please, Sign In to add comment