Advertisement
neuroticfox

Draconic Control v14.0 [xPID] [Lua 5.3 / 5.2]

Jun 28th, 2023 (edited)
722
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.88 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 unicode= require("unicode")
  6. chaosMode = 0
  7. Mode = 2
  8.       -- Inflow PID
  9. local proportional_field_error = 0
  10. local inflow_I_sum = 0
  11. local integral_field_error = 0
  12. local derivative_field_error = 0
  13. local inflow_D_last = 0
  14. local inflow_correction = 0
  15.  
  16.     -- Outflow PID
  17. local proportional_temp_error = 0
  18. local outflow_I_sum = 0
  19. local integral_temp_error = 0
  20. local derivative_temp_error = 0
  21. local outflow_D_last = 0
  22. local outflow_correction = 0
  23.  
  24.  -- Safety Checks
  25.  
  26. if not component.isAvailable("draconic_reactor") then
  27.   print("Reactor not connected. Please connect computer to reactor with an Adapter block.")
  28.   os.exit()
  29. end
  30. reactor = component.draconic_reactor
  31. local fluxGates = {}
  32. for x,y in pairs(component.list("flux_gate")) do
  33.   fluxGates[#fluxGates+1] = x
  34. end
  35. if #fluxGates < 2 then
  36.   print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  37.   os.exit()
  38. end
  39. input_flux = component.proxy(fluxGates[1])
  40. output_flux = component.proxy(fluxGates[2])
  41. if not input_flux or not output_flux then
  42.   print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  43.   os.exit()
  44. end
  45.  
  46.   local info = reactor.getReactorInfo()
  47.  
  48.  -- AutoSet Gates
  49.         reactor.chargeReactor()
  50.         satOne = info.energySaturation
  51.         fieldOne = info.fieldStrength
  52.         input_flux.setFlowOverride(1.0)
  53.         os.sleep(0.5)
  54.         satTwo = info.energySaturation
  55.         fieldTwo = info.fieldStrength
  56.         input_flux.setFlowOverride(0.0)
  57.         reactor.stopReactor()
  58.     if satTwo == satOne or fieldTwo == fieldOne then
  59.     local oldAddr = input_flux.address
  60.       input_flux = component.proxy(output_flux.address)
  61.       output_flux = component.proxy(oldAddr)
  62.     end
  63.  
  64.  -- Functions
  65.  
  66. function exit_msg(msg)
  67.   term.clear()
  68.   print(msg)
  69.   os.exit()
  70. end
  71.  
  72. function modify_temp(offset)
  73.   local new_temp = idealTemp + offset
  74.   if new_temp > 10000 then
  75.     new_temp = 10000
  76.   elseif new_temp < 2000 then
  77.     new_temp = 2000
  78.   end
  79.   idealTemp = new_temp
  80. end
  81.  
  82. function modify_field(offset)
  83.   local new_strength = idealField + offset
  84.   if new_strength > 100 then
  85.     new_strength = 100
  86.   elseif new_strength < 1 then
  87.     new_strength = 1
  88.   end
  89.   idealField = new_strength
  90. end
  91.  
  92.  -- Buttons
  93.  
  94. local adj_button_width = 19
  95. local temp_adjust_x_offset = 62
  96. local temp_adjust_y_offset = 2
  97. local field_adjust_x_offset = temp_adjust_x_offset + adj_button_width + 2
  98. local field_adjust_y_offset = 2
  99.  
  100. local buttons = {
  101.   start={
  102.     x=2,
  103.     y=20,
  104.     width=18,
  105.     height=1,
  106.     text="Start",
  107.     action=function()
  108.       if safe then
  109.         state = "Charging"
  110.         reactor.chargeReactor()
  111.       elseif shutting_down then
  112.         state = "Active"
  113.         reactor.activateReactor()
  114.       end
  115.     end,
  116.     condition=function() return safe or shutting_down end
  117.   },
  118.   shutdown={
  119.     x=2,
  120.     y=20,
  121.     width=18,
  122.     height=1,
  123.     text="Shutdown",
  124.     action=function()
  125.     cutoff_temp = 10001
  126.       state = "Manual Shutdown"
  127.       reactor.stopReactor()
  128.     end,
  129.     condition=function() return running end
  130.   },
  131.       chaosmode={
  132.     x=2,
  133.     y=22,
  134.     width=18,
  135.     height=1,
  136.     text=" Chaos Mode",
  137.     action=function()
  138.       if chaosMode == 0 then
  139.             chaosMode = 1
  140.             cutoff_temp = 19750
  141.             idealField = 100
  142.             idealTemp = 40000
  143.         elseif chaosMode == 1 then
  144.             chaosMode = 0
  145.             idealField = 25
  146.             idealTemp = 8000
  147.         end
  148.     end,
  149.     condition=function() return running end
  150.   },
  151.     Update={
  152.     x=22,
  153.     y=22,
  154.     width=18,
  155.     height=1,
  156.     text="Update",
  157.     action=function()
  158.         reactor.stopReactor()
  159.         os.execute("cd /home; pastebin get -f KiSBN88r dc14; cls; dc14")
  160.     end,
  161.     condition=function() return info.status ~= "running" and info.status ~= "warming_up" end
  162.   },
  163.     switch_gates={
  164.     x=2,
  165.     y=22,
  166.     width=18,
  167.     height=1,
  168.     text="Swap Flux Gates",
  169.     action=function()
  170.       cutoff_temp = 10001
  171.       oldAddr = input_flux.address
  172.       input_flux = component.proxy(output_flux.address)
  173.       output_flux = component.proxy(oldAddr)
  174.     end,
  175.     condition=function() return safe end
  176.   },
  177.   exit={
  178.     x=158,
  179.     y=1,
  180.     width=3,
  181.     height=1,
  182.     text="X",
  183.     action=function()
  184.       event_loop = false
  185.       os.execute("cls")
  186.     end,
  187.   },
  188.   temp_up_max={
  189.     x=temp_adjust_x_offset,
  190.     y=temp_adjust_y_offset,
  191.     width=adj_button_width,
  192.     height=1,
  193.     text="Maximum",
  194.     action=function() modify_temp(20000) end
  195.   },
  196.   temp_up_thousand={
  197.     x=temp_adjust_x_offset,
  198.     y=temp_adjust_y_offset+2,
  199.     width=adj_button_width,
  200.     height=1,
  201.     text="+1000",
  202.     action=function() modify_temp(1000) end
  203.   },
  204.   temp_up_hundred={
  205.     x=temp_adjust_x_offset,
  206.     y=temp_adjust_y_offset+4,
  207.     width=adj_button_width,
  208.     height=1,
  209.     text="+100",
  210.     action=function() modify_temp(100) end
  211.   },
  212.   temp_up_ten={
  213.     x=temp_adjust_x_offset,
  214.     y=temp_adjust_y_offset+6,
  215.     width=adj_button_width,
  216.     height=1,
  217.     text="+10",
  218.     action=function() modify_temp(10) end
  219.   },
  220.   temp_up_one={
  221.     x=temp_adjust_x_offset,
  222.     y=temp_adjust_y_offset+8,
  223.     width=adj_button_width,
  224.     height=1,
  225.     text="+1",
  226.     action=function() modify_temp(1) end
  227.   },
  228.   temp_down_thousand={
  229.     x=temp_adjust_x_offset,
  230.     y=temp_adjust_y_offset+18,
  231.     width=adj_button_width,
  232.     height=1,
  233.     text="-1000",
  234.     action=function() modify_temp(-1000) end
  235.   },
  236.     temp_down_max={
  237.     x=temp_adjust_x_offset,
  238.     y=temp_adjust_y_offset+20,
  239.     width=adj_button_width,
  240.     height=1,
  241.     text="Minimum",
  242.     action=function() modify_temp(-20000) end
  243.   },
  244.   temp_down_hundred={
  245.     x=temp_adjust_x_offset,
  246.     y=temp_adjust_y_offset+16,
  247.     width=adj_button_width,
  248.     height=1,
  249.     text="-100",
  250.     action=function() modify_temp(-100) end
  251.   },
  252.   temp_down_ten={
  253.     x=temp_adjust_x_offset,
  254.     y=temp_adjust_y_offset+14,
  255.     width=adj_button_width,
  256.     height=1,
  257.     text="-10",
  258.     action=function() modify_temp(-10) end
  259.   },
  260.   temp_down_one={
  261.     x=temp_adjust_x_offset,
  262.     y=temp_adjust_y_offset+12,
  263.     width=adj_button_width,
  264.     height=1,
  265.     text="-1",
  266.     action=function() modify_temp(-1) end
  267.   },
  268.   field_up_ten={
  269.     x=field_adjust_x_offset,
  270.     y=field_adjust_y_offset+3,
  271.     width=adj_button_width,
  272.     height=1,
  273.     text="+10",
  274.     action=function() modify_field(10) end
  275.   },
  276.     field_up_one={
  277.     x=field_adjust_x_offset,
  278.     y=field_adjust_y_offset+5,
  279.     width=adj_button_width,
  280.     height=1,
  281.     text="+1",
  282.     action=function() modify_field(1) end
  283.   },
  284.   field_up_tenth={
  285.     x=field_adjust_x_offset,
  286.     y=field_adjust_y_offset+7,
  287.     width=adj_button_width,
  288.     height=1,
  289.     text="+0.1",
  290.     action=function() modify_field(0.1) end
  291.   },
  292.   field_down_ten={
  293.     x=field_adjust_x_offset,
  294.     y=field_adjust_y_offset+17,
  295.     width=adj_button_width,
  296.     height=1,
  297.     text="-10",
  298.     action=function() modify_field(-10) end
  299.   },
  300.     field_down_one={
  301.     x=field_adjust_x_offset,
  302.     y=field_adjust_y_offset+15,
  303.     width=adj_button_width,
  304.     height=1,
  305.     text="-1",
  306.     action=function() modify_field(-1) end
  307.   },
  308.   field_down_tenth={
  309.     x=field_adjust_x_offset,
  310.     y=field_adjust_y_offset+13,
  311.     width=adj_button_width,
  312.     height=1,
  313.     text="-0.1",
  314.     action=function() modify_field(-0.1) end
  315.   }
  316. }
  317.  
  318.  -- main code
  319.  
  320. input_flux.setFlowOverride(0.0)
  321. output_flux.setFlowOverride(0.0)
  322. input_flux.setOverrideEnabled(true)
  323. output_flux.setOverrideEnabled(true)
  324.  
  325. local condition = reactor.getReactorInfo()
  326. if not condition then
  327.   print("Reactor not initialized, please ensure the stabilizers are properly laid out.")
  328.   os.exit()
  329. end
  330.  
  331. idealField = 15
  332.  
  333. idealTemp = 8000
  334. cutoff_temp = 10001
  335.  
  336.  -- tweakable pid gains
  337.  
  338. inflow_P_gain = 1
  339. inflow_I_gain = 0.04
  340. inflow_D_gain = 0.1
  341.  
  342. outflow_P_gain = 500
  343. outflow_I_gain = 0.5
  344. outflow_II_gain = 0.0000003
  345. outflow_D_gain = 60000
  346.  
  347.  -- initialize main loop
  348.  
  349. inflow_I_sum = 0
  350. inflow_D_last = 0
  351.  
  352. outflow_I_sum = 0
  353. outflow_II_sum = 0
  354. outflow_D_last = 0
  355.  
  356. state = "Standby"
  357. shutting_down = false
  358.  
  359. if condition.temperature > 25 then
  360.   state = "Cooling"
  361. end
  362. if condition.temperature > 2000 then
  363.   state = "Active"
  364. end
  365.  
  366.  -- Possible states:
  367.   --Standby
  368.   --Charging
  369.   --Active
  370.   --Manual Shutdown
  371.   --Emergency Shutdown
  372.   --Cooling
  373.  
  374. event_loop = true
  375. while event_loop do
  376. --gpu.setBackground(0x000000)
  377. --gpu.setForeground(0xFF0000)
  378. local info = reactor.getReactorInfo()
  379.  
  380.   if not component.isAvailable("draconic_reactor") then
  381.     exit_msg("Reactor disconnected, exiting")
  382.   end
  383.  
  384.   if not component.isAvailable("flux_gate") then
  385.     exit_msg("Flux gates disconnected, exiting")
  386.   end
  387.  
  388.   local inflow = 0.0
  389.   local outflow = 0.0
  390.  
  391.   shutting_down = state == "Manual Shutdown" or state == "Emergency Shutdown"
  392.   running = state == "Charging" or state == "Active"
  393.   safe = state == "Standby" or state == "Cooling"
  394.  
  395.   if state == "Charging" then
  396.     inflow = 200000.0
  397.  
  398.     if info.temperature > 2000 then
  399.       reactor.activateReactor()
  400.       state = "Active"
  401.     end
  402.   elseif state == "Cooling" then
  403.     if info.temperature < 25 then
  404.       state = "Standby"
  405.     end
  406.     inflow = 10.0
  407.     outflow = 20.0
  408.   elseif state == "Standby" then
  409.     inflow = 10.0
  410.     outflow = 20.0
  411.   else
  412.     -- adjust inflow rate based on field strength
  413.    
  414.     local field_error = (info.maxFieldStrength * (idealField / 100)) - info.fieldStrength
  415.     local proportional_field_error = field_error * inflow_P_gain
  416.     inflow_I_sum = inflow_I_sum + field_error
  417.     local integral_field_error = inflow_I_sum * inflow_I_gain
  418.     local derivative_field_error = (field_error - inflow_D_last) * inflow_D_gain
  419.     inflow_D_last = field_error
  420.     local inflow_correction = proportional_field_error + integral_field_error + derivative_field_error
  421.     if inflow_correction < 0 then
  422.       inflow_I_sum = inflow_I_sum - field_error
  423.     end
  424.     inflow = inflow_correction
  425.  
  426.     if not shutting_down then
  427.  
  428.       -- adjust outflow rate based on core temperature
  429.  
  430.       local temp_error = idealTemp - info.temperature
  431.       local proportional_temp_error = temp_error * outflow_P_gain
  432.       outflow_I_sum = outflow_I_sum + temp_error
  433.       local integral_temp_error = outflow_I_sum * outflow_I_gain
  434.       if math.abs(temp_error) < 100 then
  435.         outflow_II_sum = outflow_II_sum + integral_temp_error
  436.       else
  437.         outflow_II_sum = 0
  438.       end
  439.       local second_integral_temp_error = outflow_II_sum * outflow_II_gain
  440.       local derivative_temp_error = (temp_error - outflow_D_last) * outflow_D_gain
  441.       outflow_D_last = temp_error
  442.       local outflow_correction = proportional_temp_error + integral_temp_error + second_integral_temp_error + derivative_temp_error
  443.       if outflow_correction < 0 then
  444.         outflow_I_sum = outflow_I_sum - temp_error
  445.       end
  446.       outflow = outflow_correction
  447.  
  448.         if info.temperature > idealTemp then
  449.           state = "Manual Shutdown"
  450.           reactor.stopReactor()
  451.           os.sleep(0.10)
  452.           state = "Active"
  453.           reactor.activateReactor()
  454.         end
  455.  
  456.       -- cut off reactor in case of emergency
  457.  
  458.       if info.temperature > cutoff_temp then
  459.         print("Reactor Too Hot, shutting down")
  460.         outflow = 0.0
  461.         new_strength = 50
  462.         cutoff_temp = 10001
  463.         state = "Emergency Shutdown"
  464.         reactor.stopReactor()
  465.       end
  466.       if ((info.fieldStrength / info.maxFieldStrength) * 100) < 0.75 and chaosMode == 0 then
  467.         print("Reactor Field Has Failed, Failsafe Activated, Shutting Down")
  468.         outflow = 0.0
  469.         new_strength = 50
  470.         cutoff_temp = 10001
  471.         state = "Emergency Shutdown"
  472.         reactor.stopReactor()
  473.       end
  474.       if ((info.fieldStrength / info.maxFieldStrength) * 100) < 5 and chaosMode == 1 then
  475.         print("Reactor Field Has Failed, Failsafe Activated, Shutting Down")
  476.         outflow = 0.0
  477.         new_strength = 50
  478.         cutoff_temp = 10001
  479.         state = "Emergency Shutdown"
  480.         reactor.stopReactor()
  481.       end
  482.       if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < 12 then
  483.         print("Reactor Fuel Low, Shutting Down")
  484.         outflow = 0.0
  485.         new_strength = 1
  486.         cutoff_temp = 10001
  487.         state = "Emergency Shutdown"
  488.         reactor.stopReactor()
  489.       end
  490.     else
  491.       if info.temperature < 2000 then
  492.         state = "Cooling"
  493.       end
  494.     end
  495.   end
  496.  
  497.   if state ~= "Active" and not shutting_down then
  498.     inflow_I_sum = 0
  499.     inflow_D_last = 0
  500.     outflow_I_sum = 0
  501.     outflow_II_sum = 0
  502.     outflow_D_last = 0
  503.   end
  504.  
  505.   if inflow < 0 then
  506.     inflow = 0.0
  507.   end
  508.   if outflow < 0 then
  509.     outflow = 0.0
  510.   end
  511.  
  512.  
  513.   input_flux.setFlowOverride(inflow)
  514.   output_flux.setFlowOverride(outflow)
  515.  -- displayVariables
  516.     local deviation = idealField - ((info.fieldStrength / info.maxFieldStrength) * 100)
  517.     local secondsToExpire = (info.maxFuelConversion - info.fuelConversion) / math.max(info.fuelConversionRate*0.00002, 0.00001)
  518.     if info.fuelConversionRate > 249999 then
  519.         fuelConversionRate = ((info.fuelConversionRate / (info.maxFuelConversion * 1000000)) * 2000)
  520.         fuelMeasure = "  %%/s"
  521.     elseif info.fuelConversionRate > 999 then
  522.         fuelConversionRate = (info.fuelConversionRate / 1000)
  523.         fuelMeasure = " "..(unicode.char(956)).."b/t"
  524.     elseif info.fuelConversionRate > 999999 then
  525.         fuelConversionRate = (info.fuelConversionRate / 1000000)
  526.         fuelMeasure = " mb/t"
  527.     else
  528.         fuelConversionRate = info.fuelConversionRate
  529.         fuelMeasure = " nb/t"
  530.     end
  531.    
  532.   -- Draw screen
  533.  
  534. if term.isAvailable() then
  535.         function modify_eff(offset)
  536.             local eff = ((output_flux / input_flux) * 100)
  537.             if eff > 100000 then
  538.             eff = 1
  539.         end
  540.     end
  541.     local left_margin = 2
  542.     local spacing = 1
  543.     local values = {
  544.                 "Draconic Control™  [v14-xPID | KiSBN88r]",
  545.                 " ",
  546.                 "                   Reactor Statistics",
  547.                 "┌───────────────────────────┬────────────────────────────┐",
  548. string.format("│Time Until Refuel:         │  %5.0fd, %2.0fh, %2.0fm, %2.0fs     │", secondsToExpire/86400, secondsToExpire   /3600 % 24, secondsToExpire/60 % 60, secondsToExpire % 60),
  549. string.format("│Ideal Field:               │           %7.3f%%         │", idealField),
  550. string.format("│Current Field:             │           %7.3f%%         │", ((info.fieldStrength / info.maxFieldStrength) * 100)),
  551.                 "├───────────────────────────┼────────────────────────────┤",
  552. string.format("│Fuel Remaining:            │           %7.3f%%         │", ((1 - info.fuelConversion / info.maxFuelConversion) * 100)),
  553. string.format("│Fuel Use Rate:             │           %7.3f" .. fuelMeasure .. "     │", fuelConversionRate),
  554.                 "├───────────────────────────┼────────────────────────────┤",
  555. string.format("│Temperature                │   %7.1f°c [%8.1f°f]   │", info.temperature, ((info.temperature * 1.8) + 32)),
  556. string.format("│Ideal Temperature:         │   %7.1f°c [%8.1f°f]   │", idealTemp, ((idealTemp * 1.8) + 32)),
  557.                 "├───────────────────────────┼────────────────────────────┤",
  558. string.format("│Energy Input:              │   %13.1f RF/t       │", inflow),
  559. string.format("│Energy Output:             │   %12.1f RF/t        │", outflow),
  560.                 "└───────────────────────────┴────────────────────────────┘",
  561.                 " ",
  562.                 " "
  563. }
  564.     local values2 = {
  565. " ",
  566. " ",
  567. " ",
  568. "                                                                                                                          [Reference Table]",
  569. "                                                                                                             ┌─────────────┬─────────────┬─────────────┐",
  570. "                                                                                                             │ Temperature │  Remaining  │ Consumption │",
  571. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  572. "                                                                                                             │    14000    │    93.27    │    91.90    │",
  573. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  574. "                                                                                                             │    15000    │    59.00    │    123.5    │",
  575. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  576. "                                                                                                             │    16000    │    36.45    │     161     │",
  577. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  578. "                                                                                                             │    17000    │    21.40    │     204     │",
  579. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  580. "                                                                                                             │    18000    │    11.80    │     251     │",
  581. "                                                                                                             ├─────────────┼─────────────┼─────────────┤",
  582. "                                                                                                             │    19000    │    03.89    │     303     │",
  583. "                                                                                                             └─────────────┴─────────────┴─────────────┘",
  584. " ",
  585. " ",
  586. " ",
  587. " ",
  588. " ",
  589. " ",
  590. " ",
  591. " ",
  592. " ",
  593. " ",
  594. " ",
  595. " ",
  596. " ",
  597. " ",
  598. " ",
  599. " ",
  600. " ",
  601. " ",
  602. string.format("Energy Saturation:       %4.3f", info.energySaturation),
  603. string.format("Max Fuel [nb]:           %4.3f", (info.maxFuelConversion * 1000000)),
  604. string.format("Fuel Remaining [nb]:     %4.3f", ((info.maxFuelConversion - info.fuelConversion) * 1000000)),
  605. string.format("Current field drain is   %g RF/t", info.fieldDrainRate),
  606. string.format("Field Deviation: " .. unicode.char(8776) .. deviation .. "%%"),
  607. string.format("Input Flux Gate:  [" .. input_flux.address .. "] Set To: " .. inflow),
  608. string.format("Output Flux Gate: [" .. output_flux.address .. "] Set To: " .. outflow),
  609. " ",
  610.                 "                                         PID Values",
  611.                 " ",
  612. 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),
  613. 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),
  614. }
  615. term.clear()
  616.     if Mode == 2 and Mode ~= 3 then
  617.     for i, v in ipairs(values2) do
  618.         term.setCursor(left_margin, i * spacing)
  619.         term.write(v)
  620.         end
  621.     end
  622.     if Mode ~= 3 then
  623.     for i, v in ipairs(values) do
  624.         term.setCursor(left_margin, i * spacing)
  625.         term.write(v)
  626.         end
  627.     end
  628.     -- Draw button values
  629.  
  630.     term.setCursor(temp_adjust_x_offset, temp_adjust_y_offset+10)
  631.     term.write("Reactor Temperature")
  632.     term.setCursor(field_adjust_x_offset+1, field_adjust_y_offset+10)
  633.     term.write("Field Strength")
  634.  
  635.     -- Draw Buttons
  636.  
  637.     gpu.setForeground(0x000000)
  638.  
  639.     for bname, button in pairs(buttons) do
  640.       if button.depressed then
  641.  
  642.         button.depressed = button.depressed - 1
  643.         if button.depressed == 0 then
  644.           button.depressed = nil
  645.         end
  646.       end
  647.       if button.condition == nil or button.condition() then
  648.         local center_color = 0xBBBBBB
  649.         local highlight_color = 0xCCCCCC
  650.         local lowlight_color = 0x808080
  651.         if button.depressed then
  652.           center_color = 0xAAAAAA
  653.           highlight_color = 0x707070
  654.           lowlight_color = 0xBBBBBB
  655.         end
  656.         gpu.setBackground(center_color)
  657.         gpu.fill(button.x, button.y, button.width, button.height, " ")
  658.         if button.width > 1 and button.height > 1 then
  659.           gpu.setBackground(lowlight_color)
  660.           gpu.fill(button.x+1, button.y+button.height-1, button.width-1, 1, " ")
  661.           gpu.fill(button.x+button.width-1, button.y, 1, button.height, " ")
  662.           gpu.setBackground(highlight_color)
  663.           gpu.fill(button.x, button.y, 1, button.height, " ")
  664.           gpu.fill(button.x, button.y, button.width, 1, " ")
  665.         end
  666.         gpu.setBackground(center_color)
  667.         term.setCursor(button.x + math.floor(button.width / 2 - #button.text / 2), button.y + math.floor(button.height / 2))
  668.         term.write(button.text)
  669.       end
  670.     end
  671.  
  672.     gpu.setBackground(0x676767)
  673.     gpu.setForeground(0x220000)
  674.   end  
  675.  
  676.   -- Wait for next tick, or manual shutdown
  677.  
  678.   local event, id, op1, op2 = event.pull(0.05)
  679.   if event == "interrupted" then
  680.     if safe then
  681.       break
  682.     end
  683.   elseif event == "touch" then
  684.    
  685.     -- Handle Button Presses
  686.  
  687.     local x = op1
  688.     local y = op2
  689.  
  690.     for bname, button in pairs(buttons) do
  691.       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
  692.         button.action()
  693.         button.depressed = 3
  694.       end
  695.     end
  696.   end
  697. end
  698.  
  699. term.clear()
Advertisement
Comments
  • neuroticfox
    331 days
    # text 0.26 KB | 0 0
    1. !!WARNING!!
    2. This program has a hard time compensating for modified power
    3. output values in your config. No Refunds. This is to be used
    4. on a world with stock configs, and is only functionally better
    5. than sMAT when using the chaos shard generation mode.
    6. !!WARNING!!
Add Comment
Please, Sign In to add comment
Advertisement