neuroticfox

HDP

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