Advertisement
neuroticfox

DC 10.0

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