Advertisement
neuroticfox

DC10b

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