neuroticfox

Draconic Control v16.0a [xSMT] [Lua 5.3 / 5.2]

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