neuroticfox

Draconic Control v15.2 [SMT] [Lua 5.3 / 5.2]

Apr 6th, 2025 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.16 KB | None | 0 0
  1. -- Draconic Control v16.0f [xSMT] [T3 autoflux | slice probe]
  2. -- Based on vpLUH33Y / v16.0a and the v16.0b test build.
  3. -- This build controls against the same shield-charge basis shown by the DE GUI.
  4. --
  5. -- Changes:
  6. --   * Full DE-required field input; no "- info.fieldDrainRate" on input command.
  7. --   * Current Field is calculated on the same shield-charge basis used by the DE GUI.
  8. --   * Flux gate autodetect is restored to the T3Zucygs/v15.2 startup behavior.
  9. --   * Control loop is read -> calculate -> write gates -> optional slice record -> short yield.
  10. --   * Grab Slice no longer calls getReactorInfo() or sleeps from the button thread.
  11. --     It asks the primary control loop to record four consecutive control-pass samples.
  12. --   * Main control buttons are on the right side so they do not overlap the table.
  13.  
  14. local component = require("component")
  15. local event = require("event")
  16. local term = require("term")
  17. local thread = require("thread")
  18. local unicode = require("unicode")
  19.  
  20. local gpu = component.gpu
  21. local screen = component.screen
  22.  
  23. local function clamp(v, lo, hi)
  24.   if v ~= v then return lo end
  25.   if v < lo then return lo end
  26.   if v > hi then return hi end
  27.   return v
  28. end
  29.  
  30. local function pct_to_frac(pct)
  31.   return (tonumber(pct) or 0) / 100
  32. end
  33.  
  34. local function safe_div(a, b, fallback)
  35.   if not b or b == 0 then return fallback or 0 end
  36.   return a / b
  37. end
  38.  
  39. local function fmt_rf(v)
  40.   v = tonumber(v) or 0
  41.   if math.abs(v) >= 1000000000 then
  42.     return string.format("%.3f GRF/t", v / 1000000000)
  43.   elseif math.abs(v) >= 1000000 then
  44.     return string.format("%.3f MRF/t", v / 1000000)
  45.   elseif math.abs(v) >= 1000 then
  46.     return string.format("%.3f kRF/t", v / 1000)
  47.   else
  48.     return string.format("%.1f RF/t", v)
  49.   end
  50. end
  51.  
  52. local function clear()
  53.   term.clear()
  54.   term.setCursor(1, 1)
  55. end
  56.  
  57. local tArgs = {...}
  58.  
  59. if tostring(tArgs[1]) == "-h" then
  60.   clear()
  61.   print("Draconic Control v16.0f [xSMT | T3 autoflux | slice probe]")
  62.   print("dc16d [O] [T] [F] [M]")
  63.   print()
  64.   print("[O] Reactor Output Multiplier, usually 1")
  65.   print("[T] Desired Temperature, usually 8000")
  66.   print("[F] Desired Field Strength as displayed percent")
  67.   print("    Example: 0.15 means 0.15%, internally 0.0015")
  68.   print("[M] 1 = Simple GUI, 2 = Developer GUI, 3 = Text only")
  69.   os.exit()
  70. end
  71.  
  72. local reactorOutputMultiplier = tonumber(tArgs[1]) or 1
  73. local idealTemp = tonumber(tArgs[2]) or 8000
  74. local idealFieldPercent = tonumber(tArgs[3]) or 15
  75. local Mode = tonumber(tArgs[4]) or 1
  76.  
  77. local chaosMode = 0
  78. local eventLoop = true
  79. local info = nil
  80. local statusLine = "T3-style flux autodetect active; use Swap Flux Gates if reversed."
  81.  
  82. local coreSat = 0
  83. local convLVL = 0
  84. local targetTemp50 = 0
  85. local targetTempResist = 0
  86. local targetTempExpo = 0
  87. local targetNegCSat = 0
  88. local targetCoreSat = 0
  89. local targetSat = 0
  90. local saturationError = 0
  91. local outputNeeded = 0
  92.  
  93. local tempDrainFactor = 0
  94. local baseMaxRFt = 0
  95. local calculatedFieldDrain = 0
  96. local fieldDrainUsed = 0
  97. local fieldNegPercent = 1
  98. local requiredInput = 0
  99. local fieldStrengthError = 0
  100. local inputCommand = 0
  101. local drainPercentThisTick = 0
  102. local currentFieldPercent = 0
  103. local postDrainFieldPercent = 0
  104. local deviation = 0
  105.  
  106. local fuelConversionRate = 0
  107. local fuelMeasure = " nb/t"
  108. local fuelUseRate = 0
  109. local fuelAccel = 0
  110. local tempChangeRate = 0
  111. local tempAccel = 0
  112. local secondsToExpire = 0
  113. local tempDrain = 0
  114. local burnStage = "?"
  115.  
  116. local cutoffTemp = 19500
  117. local cutoffFieldPercent = math.max(0.05, idealFieldPercent * 0.50)
  118.  
  119. local lastSlice = {
  120.   label = "none",
  121.   a = nil, b = nil, c = nil, d = nil,
  122.   drain = nil,
  123.   max = nil,
  124.   msg = "Slice: not grabbed yet"
  125. }
  126.  
  127. -- Slice probe state. The button only sets sliceRequested.
  128. -- The primary control loop records samples immediately after gate writes, so the probe
  129. -- does not sleep/yield between reactor read and gate update.
  130. local sliceRequested = false
  131. local sliceActive = false
  132. local sliceIndex = 0
  133. local sliceSamples = {}
  134.  
  135. local ratioX, ratioY = screen.getAspectRatio()
  136. local maxX, maxY = gpu.maxResolution()
  137. gpu.setResolution(math.min(ratioX * 55, maxX), math.min(ratioY * 25, maxY))
  138. clear()
  139.  
  140. if not component.isAvailable("draconic_reactor") then
  141.   print("Reactor not connected. Please connect computer to reactor with an Adapter block.")
  142.   os.exit()
  143. end
  144.  
  145. local reactor = component.draconic_reactor
  146. info = reactor.getReactorInfo()
  147.  
  148. local fluxGates = {}
  149. for address in pairs(component.list("flux_gate")) do
  150.   fluxGates[#fluxGates + 1] = address
  151. end
  152.  
  153. if #fluxGates < 2 then
  154.   print("Not enough flux gates connected; connect inflow and outflow flux gates with Adapter blocks.")
  155.   os.exit()
  156. end
  157.  
  158. local inputFlux = component.proxy(fluxGates[1])
  159. local outputFlux = component.proxy(fluxGates[2])
  160.  
  161. if not inputFlux or not outputFlux then
  162.   print("Could not proxy both flux gates.")
  163.   os.exit()
  164. end
  165.  
  166. inputFlux.setOverrideEnabled(true)
  167. outputFlux.setOverrideEnabled(true)
  168.  
  169. print("Input and Output Flux Gates Available")
  170.  
  171. local function autodetectFluxGatesT3()
  172.   -- T3Zucygs/v15.2 style auto gate setup.
  173.   -- This intentionally mirrors the older startup behavior instead of the
  174.   -- inverted pulse detector from v16.0e.
  175.   reactor.chargeReactor()
  176.  
  177.   local satOne = info.energySaturation
  178.   local fieldOne = info.fieldStrength
  179.  
  180.   inputFlux.setFlowOverride(1.0)
  181.   os.sleep(0.1)
  182.  
  183.   local satTwo = info.energySaturation
  184.   local fieldTwo = info.fieldStrength
  185.  
  186.   inputFlux.setFlowOverride(0.0)
  187.   reactor.stopReactor()
  188.  
  189.   if satTwo <= satOne or fieldTwo <= fieldOne then
  190.     local oldAddr = inputFlux.address
  191.     inputFlux = component.proxy(outputFlux.address)
  192.     outputFlux = component.proxy(oldAddr)
  193.     inputFlux.setOverrideEnabled(true)
  194.     outputFlux.setOverrideEnabled(true)
  195.     statusLine = "T3 autoflux swapped gates; use Swap Flux Gates if reversed."
  196.   else
  197.     statusLine = "T3 autoflux kept gates; use Swap Flux Gates if reversed."
  198.   end
  199. end
  200.  
  201. autodetectFluxGatesT3()
  202. print("Input and Output Flux Gates Set")
  203.  
  204. local function resetNormalTargets()
  205.   chaosMode = 0
  206.   idealFieldPercent = tonumber(tArgs[3]) or idealFieldPercent
  207.   idealTemp = tonumber(tArgs[2]) or idealTemp
  208.   cutoffTemp = 19500
  209.   cutoffFieldPercent = math.max(0.05, idealFieldPercent * 0.50)
  210.   statusLine = "Normal raw OC field control active."
  211. end
  212.  
  213. local function modifyTemp(offset)
  214.   local newTemp = idealTemp + offset
  215.   if newTemp > 8000 and Mode == 1 then
  216.     newTemp = 8000
  217.   elseif newTemp > 15000 then
  218.     newTemp = 15000
  219.   elseif newTemp < 2000 then
  220.     newTemp = 2000
  221.   end
  222.   idealTemp = newTemp
  223. end
  224.  
  225. local function modifyField(offset)
  226.   local newField = idealFieldPercent + offset
  227.  
  228.   if chaosMode == 1 and newField < 99 then
  229.     newField = 99
  230.   elseif newField > 99 then
  231.     newField = 99
  232.   elseif Mode == 2 then
  233.     newField = math.max(0.005, newField)
  234.   else
  235.     newField = math.max(0.05, newField)
  236.   end
  237.  
  238.   idealFieldPercent = newField
  239.   cutoffFieldPercent = math.max(0.025, idealFieldPercent * 0.50)
  240. end
  241.  
  242. local function getIdealFieldFraction()
  243.   return clamp(pct_to_frac(idealFieldPercent), 0.0000001, 0.99)
  244. end
  245.  
  246. local function pctFromSnapshot(s)
  247.   if not s then return 0 end
  248.   return safe_div(s.fieldStrength or 0, s.maxFieldStrength or 0, 0) * 100
  249. end
  250.  
  251. local function grabSlice()
  252.   -- Do not call reactor.getReactorInfo() here. Do not sleep here.
  253.   -- The primary control loop will record four consecutive post-write control-pass samples.
  254.   sliceRequested = true
  255.   statusLine = "Slice requested; primary loop will record A/B/C/D without blocking control."
  256. end
  257.  
  258. local function recordSliceSample()
  259.   if sliceRequested and not sliceActive then
  260.     sliceRequested = false
  261.     sliceActive = true
  262.     sliceIndex = 0
  263.     sliceSamples = {}
  264.     statusLine = "Slice capture active; recording consecutive control-pass samples."
  265.   end
  266.  
  267.   if sliceActive and info then
  268.     sliceIndex = sliceIndex + 1
  269.     sliceSamples[sliceIndex] = {
  270.       raw = postDrainFieldPercent,
  271.       drain = drainPercentThisTick,
  272.       input = inputCommand,
  273.       output = outputNeeded
  274.     }
  275.  
  276.     if sliceIndex >= 4 then
  277.       sliceActive = false
  278.       lastSlice.a = sliceSamples[1] and sliceSamples[1].raw or 0
  279.       lastSlice.b = sliceSamples[2] and sliceSamples[2].raw or 0
  280.       lastSlice.c = sliceSamples[3] and sliceSamples[3].raw or 0
  281.       lastSlice.d = sliceSamples[4] and sliceSamples[4].raw or 0
  282.       lastSlice.drain = drainPercentThisTick
  283.       lastSlice.max = info.maxFieldStrength or 0
  284.       lastSlice.label = string.format("A %.6f B %.6f C %.6f D %.6f", lastSlice.a, lastSlice.b, lastSlice.c, lastSlice.d)
  285.       lastSlice.msg = string.format("Slice A/B/C/D raw: %.6f / %.6f / %.6f / %.6f | drain %.6f%%", lastSlice.a, lastSlice.b, lastSlice.c, lastSlice.d, lastSlice.drain)
  286.       statusLine = lastSlice.msg
  287.     else
  288.       statusLine = string.format("Slice capture active: %d/4 samples recorded.", sliceIndex)
  289.     end
  290.   end
  291. end
  292. local function updateReactorMath()
  293.   info = reactor.getReactorInfo()
  294.  
  295.   targetTemp50 = math.min((idealTemp / 10000) * 50, 99)
  296.   coreSat = safe_div(info.energySaturation, info.maxEnergySaturation, 0)
  297.   convLVL = safe_div(info.fuelConversion, info.maxFuelConversion, 0) * 1.3 - 0.3
  298.  
  299.   targetTempResist = ((targetTemp50 ^ 4) / (100 - targetTemp50))
  300.   targetTempExpo = -(targetTempResist * convLVL) - 1000 * convLVL + targetTempResist
  301.  
  302.   local term1 = 1334.1 - (3 * targetTempExpo)
  303.   local term2 = (1200690 - (2700 * targetTempExpo)) ^ 2
  304.   local term3Inner = (-1350 * targetTempExpo) + (((-4 * term1 ^ 3 + term2) ^ 0.5) / 2) + 600345
  305.  
  306.   if term3Inner < 0 then
  307.     targetNegCSat = 1
  308.     targetCoreSat = 0.99
  309.   else
  310.     local term3 = term3Inner ^ (1 / 3)
  311.     if term3 == 0 then
  312.       targetNegCSat = 1
  313.     else
  314.       targetNegCSat = -(term1 / (3 * term3)) - (term3 / 3)
  315.     end
  316.     targetCoreSat = clamp(1 - (targetNegCSat / 99), 0.01, 0.99)
  317.   end
  318.  
  319.   targetSat = targetCoreSat * info.maxEnergySaturation
  320.   saturationError = info.energySaturation - targetSat
  321.  
  322.   if info.temperature > 8000 then
  323.     tempDrainFactor = 1 + ((info.temperature - 8000) ^ 2 * 0.0000025)
  324.   elseif info.temperature > 2000 then
  325.     tempDrainFactor = 1
  326.   elseif info.temperature > 1000 then
  327.     tempDrainFactor = (info.temperature - 1000) / 1000
  328.   else
  329.     tempDrainFactor = 0
  330.   end
  331.  
  332.   baseMaxRFt = (info.maxEnergySaturation / 1000) * reactorOutputMultiplier * 1.5
  333.   calculatedFieldDrain = math.min(
  334.     tempDrainFactor * math.max(0.01, (1 - coreSat)) * (baseMaxRFt / 10.923556),
  335.     2147000000
  336.   )
  337.  
  338.   fieldDrainUsed = tonumber(info.fieldDrainRate) or calculatedFieldDrain
  339.  
  340.   local idealField = getIdealFieldFraction()
  341.   fieldNegPercent = math.max(0.0000001, 1 - idealField)
  342.   requiredInput = math.min(fieldDrainUsed / fieldNegPercent, info.maxFieldStrength)
  343.  
  344.   drainPercentThisTick = safe_div(fieldDrainUsed, info.maxFieldStrength, 0) * 100
  345.   postDrainFieldPercent = safe_div(info.fieldStrength, info.maxFieldStrength, 0) * 100
  346.   currentFieldPercent = clamp(postDrainFieldPercent + drainPercentThisTick, 0, 100)
  347.  
  348.   local targetFieldStrength = math.max(0, (info.maxFieldStrength * idealField) - fieldDrainUsed)
  349.   fieldStrengthError = targetFieldStrength - info.fieldStrength
  350.  
  351.   deviation = math.abs(currentFieldPercent - idealFieldPercent)
  352.  
  353.   outputNeeded = clamp(math.min(saturationError, info.maxEnergySaturation / 40), 0, info.maxEnergySaturation / 40)
  354.  
  355.   -- Full required input plus correction. Do not subtract fieldDrainRate.
  356.   inputCommand = clamp(requiredInput + fieldStrengthError + 1, 0, info.maxFieldStrength)
  357. end
  358.  
  359. local function primaryCalculations()
  360.   while eventLoop do
  361.     -- Critical path: read -> calculate -> write gates.
  362.     -- No sleep/yield between getReactorInfo() and setFlowOverride().
  363.     updateReactorMath()
  364.  
  365.     if info.status == "running" then
  366.       outputFlux.setFlowOverride(outputNeeded)
  367.       inputFlux.setFlowOverride(inputCommand)
  368.     elseif info.status == "warming_up" then
  369.       outputFlux.setFlowOverride(0)
  370.       inputFlux.setFlowOverride(550000000)
  371.     elseif info.status == "stopping" then
  372.       outputFlux.setFlowOverride(0)
  373.       inputFlux.setFlowOverride(requiredInput)
  374.     else
  375.       outputFlux.setFlowOverride(0)
  376.     end
  377.  
  378.     -- Probe records after gate writes, from cached values, without extra reactor calls.
  379.     recordSliceSample()
  380.  
  381.     -- Cooperative yield only after gates are already updated.
  382.     -- Keep this small but nonzero so GUI/input threads do not get starved.
  383.     os.sleep(0.05)
  384.   end
  385. end
  386.  
  387. local function secondaryCalculations()
  388.   local oldTemp = nil
  389.   local oldTempRate = nil
  390.   local oldFuel = nil
  391.   local oldUseRate = nil
  392.  
  393.   while eventLoop do
  394.     if info and info.status == "running" then
  395.       if info.temperature > cutoffTemp then
  396.         print("Reactor Too Hot, shutting down")
  397.         reactor.stopReactor()
  398.       end
  399.  
  400.       if currentFieldPercent < cutoffFieldPercent then
  401.         print("Reactor Field Below Cutoff, failsafe activated, shutting down")
  402.         reactor.stopReactor()
  403.       end
  404.  
  405.       if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < 0.2 then
  406.         print("Reactor Fuel Low, Shutting Down")
  407.         reactor.stopReactor()
  408.       end
  409.     end
  410.  
  411.     if info then
  412.       oldTemp = oldTemp or info.temperature
  413.       local currentTemp = info.temperature
  414.       oldTempRate = oldTempRate or (currentTemp - oldTemp)
  415.       tempChangeRate = currentTemp - oldTemp
  416.       tempAccel = tempChangeRate - oldTempRate
  417.       if tempAccel == 0 then tempAccel = 0.001 end
  418.       oldTemp = currentTemp
  419.       oldTempRate = tempChangeRate
  420.  
  421.       oldFuel = oldFuel or (info.maxFuelConversion - info.fuelConversion)
  422.       local currentFuel = (info.maxFuelConversion - info.fuelConversion)
  423.       oldUseRate = oldUseRate or math.max(info.fuelConversionRate * 20, 0.1)
  424.       fuelUseRate = math.max(info.fuelConversionRate * 20, 0.1)
  425.       fuelAccel = math.max(fuelUseRate - oldUseRate, 0.1)
  426.       oldFuel = currentFuel
  427.       oldUseRate = fuelUseRate
  428.  
  429.       if info.fuelConversionRate > 999999 then
  430.         fuelConversionRate = info.fuelConversionRate / 1000000
  431.         fuelMeasure = " mb/t"
  432.       elseif info.fuelConversionRate > 249999 then
  433.         fuelConversionRate = ((info.fuelConversionRate / (info.maxFuelConversion * 1000000)) * 2000)
  434.         fuelMeasure = " %%/s"
  435.       elseif info.fuelConversionRate > 999 then
  436.         fuelConversionRate = info.fuelConversionRate / 1000
  437.         fuelMeasure = " " .. unicode.char(956) .. "b/t"
  438.       else
  439.         fuelConversionRate = info.fuelConversionRate
  440.         fuelMeasure = " nb/t"
  441.       end
  442.  
  443.       if info.fuelConversionRate < 1 then
  444.         tempDrain = math.max((info.temperature - 2000) / 0.4, 0)
  445.       else
  446.         tempDrain = 1
  447.       end
  448.  
  449.       local fuelRemainPct = (1 - info.fuelConversion / info.maxFuelConversion) * 100
  450.       if fuelRemainPct > 20 then burnStage = "H"
  451.       elseif fuelRemainPct > 15 then burnStage = "He"
  452.       elseif fuelRemainPct > 10 then burnStage = "C"
  453.       elseif fuelRemainPct > 8 then burnStage = "Ne"
  454.       elseif fuelRemainPct > 5.5 then burnStage = "O"
  455.       elseif fuelRemainPct > 2.5 then burnStage = "Si"
  456.       elseif fuelRemainPct > 1 then burnStage = "Fe"
  457.       else burnStage = "Spent" end
  458.  
  459.       secondsToExpire = (info.maxFuelConversion - info.fuelConversion) / math.max(info.fuelConversionRate * 0.00002, 0.00001)
  460.     end
  461.  
  462.     os.sleep(0.1)
  463.   end
  464. end
  465.  
  466. local adj_button_width = 19
  467. local tempOffsetX = 62
  468. local tempOffsetY = 2
  469. local fieldOffsetX = tempOffsetX + adj_button_width + 2
  470. local fieldOffsetY = 2
  471. local controlX = fieldOffsetX + adj_button_width + 3
  472.  
  473. local buttons = {}
  474.  
  475. local function buildButtons()
  476.   buttons = {
  477.     startButton = {
  478.       x = controlX, y = 13, width = 18, height = 1, text = "Start", textcolor = 0x0000AA,
  479.       action = function()
  480.         if info.status == "cooling" or info.status == "cold" then
  481.           resetNormalTargets()
  482.           reactor.chargeReactor()
  483.         elseif info.status == "stopping" then
  484.           resetNormalTargets()
  485.           reactor.activateReactor()
  486.         end
  487.       end,
  488.       condition = function() return info.status ~= "running" and info.status ~= "warming_up" end
  489.     },
  490.     shutdownButton = {
  491.       x = controlX, y = 13, width = 18, height = 1, text = "Shutdown", textcolor = 0xAA0000,
  492.       action = function()
  493.         resetNormalTargets()
  494.         reactor.stopReactor()
  495.       end,
  496.       condition = function() return info.status == "running" or info.status == "warming_up" end
  497.     },
  498.     sliceButton = {
  499.       x = controlX, y = 15, width = 18, height = 1, text = "Grab Slice", textcolor = 0x005555,
  500.       action = function() grabSlice() end,
  501.       condition = function() return info.status == "running" or info.status == "warming_up" or info.status == "stopping" end
  502.     },
  503.     switchGates = {
  504.       x = controlX, y = 17, width = 18, height = 1, text = "Swap Flux Gates",
  505.       action = function()
  506.         local oldAddr = inputFlux.address
  507.         inputFlux = component.proxy(outputFlux.address)
  508.         outputFlux = component.proxy(oldAddr)
  509.         inputFlux.setOverrideEnabled(true)
  510.         outputFlux.setOverrideEnabled(true)
  511.         statusLine = "Flux gates swapped manually."
  512.       end
  513.     },
  514.     chaosMode = {
  515.       x = controlX, y = 19, width = 18, height = 1, text = " Chaos Mode", textcolor = 0x800080,
  516.       action = function()
  517.         if chaosMode == 0 then
  518.           chaosMode = 1
  519.           cutoffTemp = 19750
  520.           idealFieldPercent = 99
  521.           cutoffFieldPercent = 5
  522.           idealTemp = 55537.78
  523.           statusLine = "Chaos Mode active. This is supposed to be cursed."
  524.         else
  525.           resetNormalTargets()
  526.         end
  527.       end,
  528.       condition = function() return Mode == 2 and info.status == "running" end
  529.     },
  530.     forceExit = {
  531.       x = 158, y = 1, width = 3, height = 1, text = " X ", textcolor = 0xB00000,
  532.       action = function()
  533.         inputFlux.setFlowOverride(250000)
  534.         outputFlux.setFlowOverride(0)
  535.         resetNormalTargets()
  536.         reactor.stopReactor()
  537.         eventLoop = false
  538.       end
  539.     },
  540.  
  541.     tempMax = {x=tempOffsetX, y=tempOffsetY, width=adj_button_width, height=1, text="Maximum", textcolor=0x552222, action=function() idealTemp = 8000 end},
  542.     tempPThousand = {x=tempOffsetX, y=tempOffsetY+2, width=adj_button_width, height=1, text="+1000", textcolor=0x552222, action=function() modifyTemp(1000) end},
  543.     tempPHundred = {x=tempOffsetX, y=tempOffsetY+4, width=adj_button_width, height=1, text="+100", textcolor=0x552222, action=function() modifyTemp(100) end},
  544.     tempPTen = {x=tempOffsetX, y=tempOffsetY+6, width=adj_button_width, height=1, text="+10", textcolor=0x552222, action=function() modifyTemp(10) end},
  545.     tempPOne = {x=tempOffsetX, y=tempOffsetY+8, width=adj_button_width, height=1, text="+1", textcolor=0x552222, action=function() modifyTemp(1) end},
  546.     tempMOne = {x=tempOffsetX, y=tempOffsetY+12, width=adj_button_width, height=1, text="-1", textcolor=0x552222, action=function() modifyTemp(-1) end},
  547.     tempMTen = {x=tempOffsetX, y=tempOffsetY+14, width=adj_button_width, height=1, text="-10", textcolor=0x552222, action=function() modifyTemp(-10) end},
  548.     tempMHundred = {x=tempOffsetX, y=tempOffsetY+16, width=adj_button_width, height=1, text="-100", textcolor=0x552222, action=function() modifyTemp(-100) end},
  549.     tempMThousand = {x=tempOffsetX, y=tempOffsetY+18, width=adj_button_width, height=1, text="-1000", textcolor=0x552222, action=function() modifyTemp(-1000) end},
  550.     tempMin = {x=tempOffsetX, y=tempOffsetY+20, width=adj_button_width, height=1, text="Minimum", textcolor=0x552222, action=function() modifyTemp(-20000) end},
  551.  
  552.     fieldPTen = {x=fieldOffsetX, y=fieldOffsetY+1, width=adj_button_width, height=1, text="+10", textcolor=0x222299, action=function() modifyField(10) end},
  553.     fieldPOne = {x=fieldOffsetX, y=fieldOffsetY+3, width=adj_button_width, height=1, text="+1", textcolor=0x222299, action=function() modifyField(1) end},
  554.     fieldPTenth = {x=fieldOffsetX, y=fieldOffsetY+5, width=adj_button_width, height=1, text="+0.1", textcolor=0x222299, action=function() modifyField(0.1) end},
  555.     fieldPThou = {x=fieldOffsetX, y=fieldOffsetY+7, width=adj_button_width, height=1, text="+0.005", textcolor=0x222299, action=function() modifyField(0.005) end},
  556.     fieldMThou = {x=fieldOffsetX, y=fieldOffsetY+13, width=adj_button_width, height=1, text="-0.005", textcolor=0x222299, action=function() modifyField(-0.005) end},
  557.     fieldMTenth = {x=fieldOffsetX, y=fieldOffsetY+15, width=adj_button_width, height=1, text="-0.1", textcolor=0x222299, action=function() modifyField(-0.1) end},
  558.     fieldMOne = {x=fieldOffsetX, y=fieldOffsetY+17, width=adj_button_width, height=1, text="-1", textcolor=0x222299, action=function() modifyField(-1) end},
  559.     fieldMTen = {x=fieldOffsetX, y=fieldOffsetY+19, width=adj_button_width, height=1, text="-10", textcolor=0x222299, action=function() modifyField(-10) end}
  560.   }
  561. end
  562.  
  563. buildButtons()
  564.  
  565. local function writeRows(rows, left, spacing)
  566.   for i, v in ipairs(rows) do
  567.     term.setCursor(left, i * spacing)
  568.     term.write(v)
  569.   end
  570. end
  571.  
  572. local function drawButton(button)
  573.   if button.condition and not button.condition() then return end
  574.  
  575.   local centerColor = 0xBBBBBB
  576.   local highlightColor = 0xCCCCCC
  577.   local lowlightColor = 0x808080
  578.  
  579.   if button.depressed then
  580.     centerColor = 0xAAAAAA
  581.     highlightColor = 0x707070
  582.     lowlightColor = 0xBBBBBB
  583.     button.depressed = button.depressed - 1
  584.     if button.depressed <= 0 then button.depressed = nil end
  585.   end
  586.  
  587.   gpu.setForeground(0x000000)
  588.   gpu.setBackground(centerColor)
  589.   gpu.fill(button.x, button.y, button.width, button.height, " ")
  590.  
  591.   if button.width > 1 and button.height > 1 then
  592.     gpu.setBackground(lowlightColor)
  593.     gpu.fill(button.x + 1, button.y + button.height - 1, button.width - 1, 1, " ")
  594.     gpu.fill(button.x + button.width - 1, button.y, 1, button.height, " ")
  595.     gpu.setBackground(highlightColor)
  596.     gpu.fill(button.x, button.y, 1, button.height, " ")
  597.     gpu.fill(button.x, button.y, button.width, 1, " ")
  598.   end
  599.  
  600.   gpu.setBackground(centerColor)
  601.   if button.textcolor then gpu.setForeground(button.textcolor) end
  602.   term.setCursor(button.x + math.floor(button.width / 2 - #button.text / 2), button.y + math.floor(button.height / 2))
  603.   term.write(button.text)
  604. end
  605.  
  606. local function drawGUI()
  607.   os.sleep(1)
  608.  
  609.   while eventLoop do
  610.     if term.isAvailable() and Mode ~= 3 and info then
  611.       local left_margin = 2
  612.       local spacing = 1
  613.  
  614.       local values = {
  615.         "Draconic Control™ [v16.0f xSMT | gui-field-sync]",
  616.         " ",
  617.         "┌──────────────────Reactor Statistics────────────────────┐",
  618.         string.format("│Status:           │ %32s │", tostring(info.status)),
  619.         string.format("│Time Until Refuel:│ %5.0fd, %2.0fh, %2.0fm, %2.0fs │", secondsToExpire/86400, secondsToExpire/3600 % 24, secondsToExpire/60 % 60, secondsToExpire % 60),
  620.         string.format("│Time Until Cool:  │ %5.0fd, %2.0fh, %2.0fm, %2.0fs │", tempDrain/86400, tempDrain/3600 % 24, tempDrain/60 % 60, tempDrain % 60),
  621.         string.format("│Ideal Field:      │ %26.2f%% │", idealFieldPercent),
  622.         string.format("│Current Field:    │ %26.2f%% │", currentFieldPercent),
  623.         string.format("│Field Deviation:  │ %26.3f%% │", deviation),
  624.         "├──────────────────┼──────────────────────────────────┤",
  625.         string.format("│Slice A Raw:      │ %26.6f%% │", lastSlice.a or 0),
  626.         string.format("│Slice B/C/D Raw:  │ %8.6f / %8.6f / %8.6f │", lastSlice.b or 0, lastSlice.c or 0, lastSlice.d or 0),
  627.         "├──────────────────┼──────────────────────────────────┤",
  628.         string.format("│Fuel Remaining:   │ %26.3f%% │", ((1 - info.fuelConversion / info.maxFuelConversion) * 100)),
  629.         string.format("│Fuel Use Rate:    │ %22.3f%s │", fuelConversionRate, fuelMeasure),
  630.         "├──────────────────┼──────────────────────────────────┤",
  631.         string.format("│Temperature:      │ %10.1f°c [%10.1f°f] │", info.temperature, ((info.temperature * 1.8) + 32)),
  632.         string.format("│Ideal Temp:       │ %10.1f°c [%10.1f°f] │", idealTemp, ((idealTemp * 1.8) + 32)),
  633.         string.format("│Cutoff Field:     │ %26.3f%% │", cutoffFieldPercent),
  634.         "├──────────────────┼──────────────────────────────────┤",
  635.         string.format("│Field Input Need: │ %32s │", fmt_rf(requiredInput)),
  636.         string.format("│Input Gate Set:   │ %32s │", fmt_rf(inputCommand)),
  637.         string.format("│Energy Output:    │ %32s │", fmt_rf(outputFlux.getFlow())),
  638.         "└──────────────────┴──────────────────────────────────┘",
  639.         " " .. statusLine,
  640.         " "
  641.       }
  642.  
  643.       local values2 = {
  644.         " ", " ", " ", " [Developer]",
  645.         string.format("CoreSat: %.6f", coreSat),
  646.         string.format("ConvLVL: %.6f", convLVL),
  647.         string.format("TargetCoreSat: %.6f", targetCoreSat),
  648.         string.format("TargetSat: %.0f", targetSat),
  649.         string.format("SaturationError: %.0f", saturationError),
  650.         string.format("FieldDrainReported: %.0f RF/t", tonumber(info.fieldDrainRate) or 0),
  651.         string.format("FieldDrainCalculated: %.0f RF/t", calculatedFieldDrain),
  652.         string.format("FieldDrainUsed: %.0f RF/t", fieldDrainUsed),
  653.         string.format("TempDrainFactor: %.6f", tempDrainFactor),
  654.         string.format("FieldNegPercent(gui target): %.9f", fieldNegPercent),
  655.         string.format("RequiredInput(total): %.0f RF/t", requiredInput),
  656.         string.format("FieldStrengthError: %.0f RF", fieldStrengthError),
  657.         string.format("InputCommand: %.0f RF/t", inputCommand),
  658.         string.format("PostDrainField: %.6f%%", postDrainFieldPercent),
  659.         string.format("DrainThisTick: %.6f%%", drainPercentThisTick),
  660.         string.format("Slice: %s", lastSlice.label or "none"),
  661.         string.format("Burn Stage: %s", burnStage),
  662.         "Input Gate:  " .. tostring(inputFlux.address),
  663.         "Output Gate: " .. tostring(outputFlux.address)
  664.       }
  665.  
  666.       term.clear()
  667.       if Mode == 2 then writeRows(values2, 100, spacing) end
  668.       writeRows(values, left_margin, spacing)
  669.  
  670.       term.setCursor(tempOffsetX, tempOffsetY + 10)
  671.       term.write("Reactor Temperature")
  672.       term.setCursor(fieldOffsetX + 1, fieldOffsetY + 10)
  673.       term.write(" Field Strength")
  674.  
  675.       for _, button in pairs(buttons) do drawButton(button) end
  676.  
  677.       gpu.setBackground(0x000000)
  678.       gpu.setForeground(0xFFFFFF)
  679.     elseif term.isAvailable() and Mode == 3 and info then
  680.       local rows = {
  681.         string.format("Status: %s", tostring(info.status)),
  682.         string.format("Ideal Field: %.2f%%", idealFieldPercent),
  683.         string.format("Current Field: %.2f%%", currentFieldPercent),
  684.         string.format("PostDrainField: %.6f%%", postDrainFieldPercent),
  685.         string.format("DrainThisTick: %.6f%%", drainPercentThisTick),
  686.         string.format("Slice A/B/C/D: %.6f / %.6f / %.6f / %.6f", lastSlice.a or 0, lastSlice.b or 0, lastSlice.c or 0, lastSlice.d or 0),
  687.         string.format("Temp: %.1fC / Target %.1fC", info.temperature, idealTemp),
  688.         string.format("FieldDrainUsed: %.0f RF/t", fieldDrainUsed),
  689.         string.format("RequiredInput: %.0f RF/t", requiredInput),
  690.         string.format("InputCommand: %.0f RF/t", inputCommand),
  691.         string.format("OutputCommand: %.0f RF/t", outputNeeded),
  692.         string.format("Fuel Remaining: %.3f%%", ((1 - info.fuelConversion / info.maxFuelConversion) * 100)),
  693.         string.format("Eq. Fusion Stage: %s", burnStage)
  694.       }
  695.       term.clear()
  696.       writeRows(rows, 2, 1)
  697.     end
  698.  
  699.     os.sleep(0.1)
  700.   end
  701. end
  702.  
  703. local function parseInput()
  704.   while eventLoop do
  705.     local ev, _, x, y = event.pull()
  706.  
  707.     if ev == "interrupted" then
  708.       eventLoop = false
  709.       break
  710.     elseif ev == "touch" then
  711.       for _, button in pairs(buttons) do
  712.         if (not button.condition or button.condition())
  713.           and x >= button.x and x <= button.x + button.width
  714.           and y >= button.y and y <= button.y + button.height then
  715.           button.action()
  716.           button.depressed = 3
  717.         end
  718.       end
  719.     end
  720.   end
  721. end
  722.  
  723. local primaryThread = thread.create(primaryCalculations)
  724. local secondaryThread = thread.create(secondaryCalculations)
  725. local guiThread = thread.create(drawGUI)
  726. local inputThread = thread.create(parseInput)
  727.  
  728. thread.waitForAny({primaryThread, secondaryThread, guiThread, inputThread})
  729.  
  730. eventLoop = false
  731. pcall(function() outputFlux.setFlowOverride(0) end)
  732. pcall(function() inputFlux.setFlowOverride(250000) end)
  733. pcall(function() reactor.stopReactor() end)
  734.  
  735. term.clear()
  736. term.setCursor(1, 1)
  737. local sx, sy = gpu.maxResolution()
  738. gpu.setResolution(sx, sy)
  739. print("Exited Draconic Control v16.0f T3 autoflux slice probe")
  740.  
Advertisement
Add Comment
Please, Sign In to add comment