Advertisement
neuroticfox

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

Apr 6th, 2025 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.88 KB | None | 0 0
  1. ::start::
  2.  
  3. -- Set Requirements & Local Variables
  4.  
  5. local component = require("component")
  6. local event = require("event")
  7. local term = require("term")
  8. local gpu = component.gpu
  9. local screen = component.screen
  10. local unicode= require("unicode")
  11. local reactorOutputMultiplier = 1
  12. local cutoffTemp = 9005
  13. local chaosMode = 0
  14. local tempDrop = 0
  15. local devMode = 0
  16. local deviation = 0
  17. local cVar = "Do not use Chaos Mode with less than one block of fuel"
  18. local ratioX, ratioY = screen.getAspectRatio()
  19. local maxX, maxY = gpu.maxResolution()
  20. local tArgs = {...}
  21. local adj_button_width = 19
  22. local tempOffsetX = 62
  23. local tempOffsetY = 2
  24. local fieldOffsetX = tempOffsetX + adj_button_width + 2
  25. local fieldOffsetY = 2
  26. local cutoffField = 0.75
  27. local highest_use = 0.1
  28.  
  29. -- Set Resolution
  30.  
  31. gpu.setResolution(math.min(ratioX*55, maxX), math.min(ratioY*25,maxY))
  32. term.clear()
  33. term.setCursor(0, 0)
  34.  
  35. -- Program Help & Credits
  36.  
  37. if tostring(tArgs[1]) == "-h" then
  38.     os.execute(cls)
  39.     print("Draconic Control 15.2")
  40.     print("[1.12 and 1.16 Compatible]")
  41.     print()
  42.     print("dc15 [O] [T] [F] [M]")
  43.     print()
  44.     print("Start Arguments")
  45.     print()
  46.     print("[O] - Reactor Output Multiplier")
  47.     print("This is set in your Draconic Evolution")
  48.     print("configs, by default 1.12.2 uses 1, and")
  49.     print("1.16.5 uses 10.")
  50.     print()
  51.     print("[T] - Desired Reactor Temperature")
  52.     print("Recommended [8000]")
  53.     print("Range [2500-8000]")
  54.     print()
  55.     print("[F] - Desired Field Strength %")
  56.     print("Recommended [10]")
  57.     print("Range [0.5-99]")
  58.     print()
  59.     print("[M] - User Interface Selection")
  60.     print("Default [1]")
  61.     print("Range [1-3]")
  62.     print("Simple, Developer, Text Only")
  63.     print()
  64.     print()
  65.     print()
  66.     print("Compiled by BrokenSynapse & AwesomeAlec1")
  67.     print()
  68.     print("Thanks to Maurdekye for creating the base of this program:")
  69.     print("https://youtu.be/iLvkk41K84E")
  70.     print()
  71.     print("Thanks to AwesomeAlec1 for creating the control script:")
  72.     print("https://youtu.be/CLku1ckXpeU")
  73.     print()
  74.     print("Thanks to MightyPirates for being so gracious as to")
  75.     print("let me fix what they broke by never using Lua 5.3")
  76.     print()
  77.     print("And a very special thanks to ZanBorg for breaking")
  78.     print("this script until it doesn't break anymore.")
  79.     goto fin
  80. end
  81.  
  82. -- Set Control Variables
  83.  
  84. local reactorOutputMultiplier = tArgs[1] or 1
  85. local idealTemp = tArgs[2] or 8000
  86. local idealField = tArgs[3] or 15
  87. local Mode = tArgs[4] or 1
  88.  
  89. -- Register Components
  90.  
  91.     -- Check For Reactor
  92. if not component.isAvailable("draconic_reactor") then
  93.     print("Reactor not connected. Please connect computer to reactor with an Adapter block.")
  94.     os.exit()
  95. end
  96.  
  97. local reactor = component.draconic_reactor
  98. local info = reactor.getReactorInfo()
  99.  
  100.     -- Check For Flux Gates
  101. local fluxGates = {}
  102.  
  103. for x,y in pairs(component.list("flux_gate")) do
  104.     fluxGates[#fluxGates+1] = x
  105. end
  106.  
  107. if #fluxGates < 2 then
  108.     print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  109.     os.exit()
  110. end
  111.  
  112. local inputFlux = component.proxy(fluxGates[1])
  113. local outputFlux = component.proxy(fluxGates[2])
  114.  
  115. outputFlux.setOverrideEnabled(true)
  116. inputFlux.setOverrideEnabled(true)
  117.  
  118. if not inputFlux or not outputFlux then
  119.     print("Not enough flux gates connected; please connect inflow and outflow flux gates with Adapter blocks.")
  120.     os.exit()
  121. end
  122.  
  123. -- Automatically Set Input & Output Flux Gates
  124.  
  125. reactor.chargeReactor()
  126. satOne = info.energySaturation
  127. fieldOne = info.fieldStrength
  128. inputFlux.setFlowOverride(1.0)
  129.  
  130. os.sleep(0.1)
  131.  
  132. satTwo = info.energySaturation
  133. fieldTwo = info.fieldStrength
  134. inputFlux.setFlowOverride(0.0)
  135. reactor.stopReactor()
  136.  
  137. if satTwo <= satOne or fieldTwo <= fieldOne then
  138.     local oldAddr = inputFlux.address
  139.     inputFlux = component.proxy(outputFlux.address)
  140.     outputFlux = component.proxy(oldAddr)
  141. end
  142.  
  143. -- Functions & Arrays
  144.  
  145.     -- Exit Message
  146. function exit_msg(msg)
  147.     term.clear()
  148.     print(msg)
  149.     os.exit()
  150. end
  151.  
  152.     -- Temperature Controls
  153. function modifyTemp(offset)
  154.     local newTemp = idealTemp + offset
  155.     if newTemp > 8000 and Mode == 1 then
  156.         newTemp = 8000
  157.     elseif newTemp > 15000 then
  158.         newTemp = 15000
  159.     elseif newTemp < 2000 then
  160.         newTemp = 2000
  161.     end
  162.         idealTemp = newTemp
  163.     end
  164.  
  165.     -- Field Strength Controls
  166. function modifyField(offset)
  167.     local newField = idealField + offset
  168.     if newField > 99 then
  169.         newField = 99
  170.     elseif newField < 99 and chaosMode == 1 then
  171.         newField = 99
  172.     elseif newField < 1.0 and Mode == 1 or Mode == 3 then
  173.         newField = 1.0
  174.     elseif newField < 0.5 and Mode == 2 then
  175.         newField = 0.5
  176.     end
  177.         idealField = newField
  178.     end
  179.  
  180. local eventLoop = true
  181.  
  182.     -- Reactor Management Calculations
  183. function primaryCalculations()
  184.     while eventLoop do
  185.         info = reactor.getReactorInfo()
  186.         -- Reactor equation variables
  187.     local targetTemp50  = math.min((targetTemp / 10000) * 50, 99)
  188.     local convLVL       = (info.fuelConversion / info.maxFuelConversion * 1.3) - 0.3
  189.     -- Calculate the temperature rise resistance for the reactor at the desired temperature.
  190.     local targetTempResist = ((targetTemp50^4) / (100 - targetTemp50))
  191.     -- Calculate the temperature rise exponential needed to reach the desired temperature
  192.     local targetTempExpo = -(targetTempResist*convLVL) - 1000*convLVL + targetTempResist
  193.     -- Calculate the saturation level needed to produce the required tempRiseExpo
  194.     local term1 = 1334.1-(3*targetTempExpo)
  195.     local term2 = (1200690-(2700*targetTempExpo))^2
  196.     local term3 = ((-1350*targetTempExpo)+(((-4*term1^3+term2)^(1/2))/2)+600345)^(1/3)
  197.     local targetNegCSat = -(term1/(3*term3))-(term3/3)
  198.     -- Saturation received from above equation needs to be reformatted to a more useful form
  199.     local targetCoreSat = 1 - (targetNegCSat/99)
  200.     local targetSat = targetCoreSat * info.maxEnergySaturation
  201.     -- Calculate the difference between where saturation is and where it needs to be
  202.     local saturationError = info.energySaturation - targetSat
  203.     local requiredOutput = math.min(saturationError, info.maxEnergySaturation / 40) --+ info.generationRate
  204.     -- Calculate field input
  205.     local fieldNegPercent = 1 - targetField
  206.     local fieldStrengthError = (info.maxFieldStrength * targetField) - info.fieldStrength
  207.     local requiredInput = math.min(fieldStrengthError + (info.maxFieldStrength * info.fieldDrainRate) / (info.maxFieldStrength - info.fieldStrength) - info.fieldDrainRate + 1, info.maxFieldStrength - info.fieldStrength)
  208.     if info.status == "warming_up" then
  209.         if info.fuelConversion / info.maxFuelConversion > targetFuel then
  210.             reactor.stopReactor()
  211.             else
  212.                 if info.temperature >= 2000 then
  213.                 reactor.activateReactor()
  214.             else
  215.                 setReactorOutput(0)
  216.                 setReactorInput(5000000)
  217.                 end
  218.         end
  219.  
  220.     elseif info.status == "running" then
  221.         if info.fuelConversion / info.maxFuelConversion > targetFuel then
  222.             reactor.stopReactor()
  223.         end
  224.         setReactorOutput(requiredOutput)
  225.         setReactorInput(requiredInput)
  226.     elseif info.status == "stopping" then
  227.         setReactorOutput(0)
  228.         setReactorInput(requiredInput)
  229.     end
  230.     --print("primaryCalculations okay")
  231.     os.sleep(0.01)
  232. end
  233. print("primaryCalculations thread exited")
  234. end
  235.  
  236.     -- UI Related Calculations
  237. function secondaryCalculations()
  238.     while eventLoop do
  239.  
  240. -- Safety Measures
  241.  
  242.     if info.status == "running" then
  243.     if info.temperature > cutoffTemp then
  244.         print("Reactor Too Hot, shutting down")
  245.         reactor.stopReactor()
  246.     end
  247.     if ((info.fieldStrength / info.maxFieldStrength) * 100) < cutoffField then
  248.         print("Reactor Field Has Failed, Failsafe Activated, Shutting Down")
  249.         reactor.stopReactor()
  250.     end
  251.     if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) < 0.2 then
  252.         print("Reactor Fuel Low, Shutting Down")
  253.         reactor.stopReactor()
  254.     end
  255.    end
  256.  
  257. -- Set Secondary Calculations
  258.  
  259.     -- Get Temp Rise
  260. oldTemp = currentTemp or info.temperature
  261. currentTemp = info.temperature
  262. oldTempRate = tempChangeRate or currentTemp - oldTemp
  263. tempChangeRate = currentTemp - oldTemp
  264. tempAccel = tempChangeRate - oldTempRate
  265. if tempAccel == 0 then
  266.     tempAccel = 0.001
  267. end
  268.  
  269.     -- Get Fuel Use Rate
  270. oldFuel = currentFuel or (info.maxFuelConversion - info.fuelConversion)
  271. currentFuel = (info.maxFuelConversion - info.fuelConversion)
  272. oldUseRate = fuelUseRate or math.max(info.fuelConversionRate*20, 0.1)
  273. fuelUseRate = math.max(info.fuelConversionRate*20, 0.1)
  274. fuelAccel = math.max(fuelUseRate - oldUseRate, 0.1)
  275.  
  276.     -- Change Fuel Conversion Units
  277. if info.fuelConversionRate > 249999 then
  278.     fuelConversionRate = ((info.fuelConversionRate / (info.maxFuelConversion * 1000000)) * 2000)
  279.     fuelMeasure = "  %%/s"
  280. elseif info.fuelConversionRate > 999 then
  281.     fuelConversionRate = (info.fuelConversionRate / 1000)
  282.     fuelMeasure = " "..(unicode.char(956)).."b/t"
  283. elseif info.fuelConversionRate > 999999 then
  284.     fuelConversionRate = (info.fuelConversionRate / 1000000)
  285.     fuelMeasure = " mb/t"
  286. else
  287.     fuelConversionRate = info.fuelConversionRate
  288.     fuelMeasure = " nb/t"
  289. end
  290.  
  291.     -- Get Time Until Cool
  292. if info.fuelConversionRate < 1 then
  293.     tempDrain = ((info.temperature - 2000) / 0.4)
  294. else
  295.     tempDrain = 1
  296. end
  297.  
  298.     -- Get Equivalent Solar Fusion Stage
  299. if ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 20 then burnStage = "H"
  300. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 15 then burnStage = "He"
  301. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 10 then burnStage = "C"
  302. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 8 then burnStage = "Ne"
  303. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 5.5 then burnStage = "O"
  304. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 2.5 then burnStage = "Si"
  305. elseif ((1 - info.fuelConversion / info.maxFuelConversion) * 100) > 1 then burnStage = "Fe"
  306. end
  307.  
  308.     -- Get Field Deviation
  309. if ((info.fieldStrength / info.maxFieldStrength) * 100) > idealField then
  310.     deviation = (((info.fieldStrength / info.maxFieldStrength) * 100)) - idealField
  311. elseif ((info.fieldStrength / info.maxFieldStrength) * 100) < idealField then
  312.     deviation = idealField - (((info.fieldStrength / info.maxFieldStrength) * 100))
  313. end
  314.  
  315.     -- Get Time Until Refuel
  316.     local secondsToExpire = (info.maxFuelConversion - info.fuelConversion) / math.max(info.fuelConversionRate*0.00002, 0.00001)
  317. end
  318. print("secondaryCalculations thread exited")
  319. end
  320.  
  321.     -- Draw GUI
  322. function drawGUI()
  323.  
  324.     -- Button Array
  325. local buttons = {
  326.     startButton={
  327.     x=2,
  328.     y=20,
  329.     width=18,
  330.     height=1,
  331.     text="Start",
  332.     textcolor = 0x0000AA,
  333.     action=function()
  334.       if info.status == "cooling" or info.status == "cold" then
  335.             chaosMode = 0
  336.             idealField = Arg2
  337.             cutoffField = 0.4
  338.             idealTemp = Arg1
  339.             cVar = "Do not use Chaos Mode with less than one block of fuel"
  340.         reactor.chargeReactor()
  341.       elseif info.status == "stopping" then
  342.             chaosMode = 0
  343.             idealField = Arg2
  344.             cutoffField = 0.4
  345.             idealTemp = Arg1
  346.             cVar = "Do not use Chaos Mode with less than one block of fuel"
  347.         reactor.activateReactor()
  348.       end
  349.     end,
  350.     condition=function() return info.status ~= "running" and info.status ~= "warming_up" end
  351.   },
  352.   shutdownButton={
  353.     x=2,
  354.     y=20,
  355.     width=18,
  356.     height=1,
  357.     text="Shutdown",
  358.     textcolor = 0xAA0000,
  359.     action=function()
  360.     cutoffTemp = 9000
  361.     idealTemp = Arg1
  362.     idealField = Arg2
  363.     cutoffField = 0.4
  364.     chaosMode = 0
  365.     cVar = "Do not use Chaos Mode with less than one block of fuel"
  366.       state = "MASD"
  367.       reactor.stopReactor()
  368.     end,
  369.     condition=function() return info.status == "running" or info.status == "warming_up" end
  370.   },
  371.   chaosMode={
  372.     x=2,
  373.     y=22,
  374.     width=18,
  375.     height=1,
  376.     text=" Chaos Mode",
  377.     textcolor = 0x800080,
  378.     action=function()
  379.         if chaosMode == 0 then
  380.             chaosMode = 1
  381.             cutoffTemp = 19750
  382.             idealField = 99
  383.             cutoffField = 5
  384.             idealTemp = 55537.78
  385.         elseif chaosMode == 1 then
  386.             chaosMode = 0
  387.             idealField = Arg2
  388.             cutoffField = 0.4
  389.             idealTemp = Arg1
  390.         end
  391.     end,
  392.     condition=function() return Mode == 2 and info.status == "running" end
  393.   },
  394.   forceExit={
  395.     x=158,
  396.     y=1,
  397.     width=3,
  398.     height=1,
  399.     text=" X ",
  400.     textcolor = 0xB00000,
  401.     action=function()
  402.         inputFlux.setFlowOverride(250000)
  403.         chaosMode = 0
  404.         idealField = 99
  405.         cutoffField = 0.4
  406.         idealTemp = Arg1
  407.       reactor.stopReactor()
  408.       gpu.setResolution(gpu.maxResolution())
  409.       event_loop = false
  410.       os.execute("cls")
  411.     end,
  412. --    condition=function() return running or shutting_down end
  413.   },
  414.   Update={
  415.     x=22,
  416.     y=22,
  417.     width=18,
  418.     height=1,
  419.     text="Update",
  420.     action=function()
  421.         reactor.stopReactor()
  422.         os.execute("cd /home; pastebin get -f hh14Sxhi dc15; cls; dc15")
  423.     end,
  424.     condition=function() return info.status ~= "running" and info.status ~= "warming_up" end
  425.   },
  426.   switchGates={
  427.     x=2,
  428.     y=22,
  429.     width=18,
  430.     height=1,
  431.     text="Swap Flux Gates",
  432.     action=function()
  433.       cutoffTemp = 10500
  434.       local old_addr = inputFlux.address
  435.       inputFlux = component.proxy(outputFlux.address)
  436.       outputFlux = component.proxy(old_addr)
  437.     end,
  438.     condition=function() return info.status == "cooling" or info.status == "cold" or info.status == "stopping" end
  439.   },
  440.   tempMax={
  441.     x=tempOffsetX,
  442.     y=tempOffsetY,
  443.     width=adj_button_width,
  444.     height=1,
  445.     text="Maximum",
  446.     textcolor = 0x552222,
  447.     action=function()
  448.     idealTemp = 8000 end
  449.   },
  450.   tempPThousand={
  451.     x=tempOffsetX,
  452.     y=tempOffsetY+2,
  453.     width=adj_button_width,
  454.     height=1,
  455.     text="+1000",
  456.     textcolor = 0x552222,
  457.     action=function() modifyTemp(1000) end
  458.   },
  459.   tempPHundred={
  460.     x=tempOffsetX,
  461.     y=tempOffsetY+4,
  462.     width=adj_button_width,
  463.     height=1,
  464.     text="+100",
  465.     textcolor = 0x552222,
  466.     action=function() modifyTemp(100) end
  467.   },
  468.   tempPTen={
  469.     x=tempOffsetX,
  470.     y=tempOffsetY+6,
  471.     width=adj_button_width,
  472.     height=1,
  473.     text="+10",
  474.     textcolor = 0x552222,
  475.     action=function() modifyTemp(10) end
  476.   },
  477.   tempPOne={
  478.     x=tempOffsetX,
  479.     y=tempOffsetY+8,
  480.     width=adj_button_width,
  481.     height=1,
  482.     text="+1",
  483.     textcolor = 0x552222,
  484.     action=function() modifyTemp(1) end
  485.   },
  486.   tempMin={
  487.     x=tempOffsetX,
  488.     y=tempOffsetY+20,
  489.     width=adj_button_width,
  490.     height=1,
  491.     text="Minimum",
  492.     textcolor = 0x552222,
  493.     action=function() modifyTemp(-20000) end
  494.   },
  495.   tempMThousand={
  496.     x=tempOffsetX,
  497.     y=tempOffsetY+18,
  498.     width=adj_button_width,
  499.     height=1,
  500.     text="-1000",
  501.     textcolor = 0x552222,
  502.     action=function() modifyTemp(-1000) end
  503.   },
  504.   tempMHundred={
  505.     x=tempOffsetX,
  506.     y=tempOffsetY+16,
  507.     width=adj_button_width,
  508.     height=1,
  509.     text="-100",
  510.     textcolor = 0x552222,
  511.     action=function() modifyTemp(-100) end
  512.   },
  513.   tempMTen={
  514.     x=tempOffsetX,
  515.     y=tempOffsetY+14,
  516.     width=adj_button_width,
  517.     height=1,
  518.     text="-10",
  519.     textcolor = 0x552222,
  520.     action=function() modifyTemp(-10) end
  521.   },
  522.   tempMOne={
  523.     x=tempOffsetX,
  524.     y=tempOffsetY+12,
  525.     width=adj_button_width,
  526.     height=1,
  527.     text="-1",
  528.     textcolor = 0x552222,
  529.     action=function() modifyTemp(-1) end
  530.   },
  531.   fieldPTen={
  532.     x=fieldOffsetX,
  533.     y=fieldOffsetY+1,
  534.     width=adj_button_width,
  535.     height=1,
  536.     text="+10",
  537.     textcolor = 0x222299,
  538.     action=function() modifyField(10) end
  539.   },
  540.     fieldPOne={
  541.     x=fieldOffsetX,
  542.     y=fieldOffsetY+3,
  543.     width=adj_button_width,
  544.     height=1,
  545.     text="+1",
  546.     textcolor = 0x222299,
  547.     action=function() modifyField(1) end
  548.   },
  549.   fieldPTenth={
  550.     x=fieldOffsetX,
  551.     y=fieldOffsetY+5,
  552.     width=adj_button_width,
  553.     height=1,
  554.     text="+0.1",
  555.     textcolor = 0x222299,
  556.     action=function() modifyField(0.1) end
  557.   },
  558.     fieldPThou={
  559.     x=fieldOffsetX,
  560.     y=fieldOffsetY+7,
  561.     width=adj_button_width,
  562.     height=1,
  563.     text="+0.005",
  564.     textcolor = 0x222299,
  565.     action=function() modifyField(0.005) end
  566.   },
  567.   fieldMTen={
  568.     x=fieldOffsetX,
  569.     y=fieldOffsetY+19,
  570.     width=adj_button_width,
  571.     height=1,
  572.     text="-10",
  573.     textcolor = 0x222299,
  574.     action=function() modifyField(-10) end
  575.   },
  576.     fieldMOne={
  577.     x=fieldOffsetX,
  578.     y=fieldOffsetY+17,
  579.     width=adj_button_width,
  580.     height=1,
  581.     text="-1",
  582.     textcolor = 0x222299,
  583.     action=function() modifyField(-1) end
  584.   },
  585.   fieldMTenth={
  586.     x=fieldOffsetX,
  587.     y=fieldOffsetY+15,
  588.     width=adj_button_width,
  589.     height=1,
  590.     text="-0.1",
  591.     textcolor = 0x222299,
  592.     action=function() modifyField(-0.1) end
  593.    },
  594.      fieldMThou={
  595.     x=fieldOffsetX,
  596.     y=fieldOffsetY+13,
  597.     width=adj_button_width,
  598.     height=1,
  599.     text="-0.005",
  600.     textcolor = 0x222299,
  601.     action=function() modifyField(-0.005) end
  602.    }
  603. }
  604.  
  605.     -- Draw Relevant Data For GUI
  606. while eventLoop do
  607.     if term.isAvailable() and Mode ~= 3 then
  608.             function modify_eff(offset)
  609.                 local eff = ((outputFlux / inputFlux) * 100)
  610.                 if eff > 100000 then
  611.                 eff = 1
  612.             end
  613.         end
  614.         local left_margin = 2
  615.         local spacing = 1
  616.         local values = {
  617.                     "Draconic Control™  [v15.1 sMAT | hh14Sxhi]",
  618.                     " ",
  619.                     "┌──────────────────Reactor Statistics────────────────────┐",
  620.     string.format("│Time Until Refuel:         │  %5.0fd, %2.0fh, %2.0fm, %2.0fs     │", secondsToExpire/86400, secondsToExpire   /3600 % 24, secondsToExpire/60 % 60, secondsToExpire % 60),
  621.     string.format("│Time Until Cool:           │  %5.0fd, %2.0fh, %2.0fm, %2.0fs     │", tempDrain/86400, tempDrain   /3600 % 24, tempDrain/60 % 60, tempDrain % 60),
  622.     string.format("│Ideal Field:               │           %8.3f%%        │", idealField),
  623.     string.format("│Current Field:             │           %8.3f%%        │", ((info.fieldStrength / info.maxFieldStrength) * 100) + 0.122),
  624.                     "├───────────────────────────┼────────────────────────────┤",
  625.     string.format("│Fuel Remaining:            │           %7.3f%%         │", ((1 - info.fuelConversion / info.maxFuelConversion) * 100)),
  626.     string.format("│Fuel Use Rate:             │           %7.3f" .. fuelMeasure .. "     │", fuelConversionRate),
  627.                     "├───────────────────────────┼────────────────────────────┤",
  628.     string.format("│Temperature                │   %7.1f°c [%8.1f°f]   │", info.temperature, ((info.temperature * 1.8) + 32)),
  629.     string.format("│Ideal Temperature:         │   %7.1f°c [%8.1f°f]   │", idealTemp, ((idealTemp * 1.8) + 32)),
  630.                     "├───────────────────────────┼────────────────────────────┤",
  631.     string.format("│Energy Input:              │   %12.1f RF/t        │", requiredInput),
  632.     string.format("│Energy Output:             │   %12.1f RF/t        │", outputFlux.getFlow()),
  633.                     "└───────────────────────────┴────────────────────────────┘",
  634.                     " " .. cVar,
  635.                     " "
  636.     }
  637.         local values2 = {
  638.                 "Eq. Fusion Stage     " .. burnStage,
  639.     string.format("Max Fuel [nb]:       %4.3f", (info.maxFuelConversion * 1000000)),
  640.     string.format("Fuel Remaining [nb]: %4.3f", ((info.maxFuelConversion - info.fuelConversion) * 1000000)),
  641.     string.format("Temperature Rise: %4.3f", tempAccel),
  642.     string.format("Temp Resist for target temp %d (%d): %.2f", idealTemp, targetTemp50, targetTempResist),
  643.     string.format("Temp expo for convLVL %.2f: %.2f", convLVL, targetTempExpo),
  644.     string.format("Saturation needed for zero rise: %d (%3.2f%%)", targetSat, targetCoreSat*100),
  645.     string.format("Error between current saturation and target saturation: %d\n", saturationError),
  646.     string.format("Current field drain is %d RF/t", info.fieldDrainRate),
  647.     string.format("Current temp drain factor for temp %d is %1.2f", info.temperature, tempDrainFactor),
  648.     string.format("fieldNegPercent is %d", fieldNegPercent),
  649.     string.format("Required input to counter field drain: %d RF/t\n", requiredInput),
  650.     string.format("Field Deviation: " .. unicode.char(8776) .. deviation .. "%%"),
  651.     string.format("Input Flux Gate:  [" .. inputFlux.address .. "] Set To: " .. inputFlux.getFlow()),
  652.     string.format("Output Flux Gate: [" .. outputFlux.address .. "] Set To: " .. outputFlux.getFlow())
  653.     }
  654.    
  655.     term.clear()
  656.    
  657.     if Mode == 2 and Mode ~= 3 then
  658.         for i, v in ipairs(values2) do
  659.             term.setCursor(left_margin, i * spacing)
  660.             term.write(v)
  661.         end
  662.     end
  663.     if Mode ~= 3 then
  664.         for i, v in ipairs(values) do
  665.             term.setCursor(left_margin, i * spacing)
  666.             term.write(v)
  667.         end
  668.     end
  669.    
  670.     -- Draw Buttons For GUI
  671.     term.setCursor(tempOffsetX, tempOffsetY+10)
  672.     term.write("Reactor Temperature")
  673.     term.setCursor(fieldOffsetX+1, fieldOffsetY+10)
  674.     term.write("  Field Strength")
  675.     gpu.setForeground(0xFFFFFF)
  676.     for bname, button in pairs(buttons) do
  677.         gpu.setForeground(0x000000)
  678.         if button.depressed then
  679.             button.depressed = button.depressed - 1
  680.             if button.depressed == 0 then
  681.                 button.depressed = nil
  682.             end
  683.         end
  684.         if button.condition == nil or button.condition() then
  685.             local centerColor = 0xBBBBBB
  686.             local highlightColor = 0xCCCCCC
  687.             local lowlightColor = 0x808080
  688.             if button.depressed then
  689.                 centerColor = 0xAAAAAA
  690.                 highlightColor = 0x707070
  691.                 lowlightColor = 0xBBBBBB
  692.             end
  693.             gpu.setBackground(centerColor)
  694.             gpu.fill(button.x, button.y, button.width, button.height, " ")
  695.             if button.width > 1 and button.height > 1 then
  696.                 gpu.setBackground(lowlightColor)
  697.                 gpu.fill(button.x+1, button.y+button.height-1, button.width-1, 1, " ")
  698.                 gpu.fill(button.x+button.width-1, button.y, 1, button.height, " ")
  699.                 gpu.setBackground(highlightColor)
  700.                 gpu.fill(button.x, button.y, 1, button.height, " ")
  701.                 gpu.fill(button.x, button.y, button.width, 1, " ")
  702.             end
  703.             gpu.setBackground(centerColor)
  704.             if button.textcolor then
  705.                 gpu.setForeground(button.textcolor)
  706.             end
  707.             term.setCursor(button.x + math.floor(button.width / 2 - #button.text / 2), button.y + math.floor(button.height / 2))
  708.             term.write(button.text)
  709.         end
  710.     end
  711.     gpu.setBackground(0x777777)
  712.     gpu.setForeground(0x000000)
  713.     end
  714.    
  715.     -- Write Relevant Values For CLI
  716.     if term.isAvailable() and Mode == 3 then
  717.         local left_margin = 2
  718.         local spacing = 1
  719.         local values3 = {
  720.         "Eq. Fusion Stage     " .. burnStage,
  721.         string.format("Max Fuel [nb]:       %4.3f", (info.maxFuelConversion * 1000000)),
  722.         string.format("Fuel Remaining [nb]: %4.3f", ((info.maxFuelConversion - info.fuelConversion) * 1000000)),
  723.         string.format("Temperature Rise: %4.3f", tempAccel),
  724.         string.format("Temp Resist for target temp %d (%d): %.2f", idealTemp, targetTemp50, targetTempResist),
  725.         string.format("Temp expo for convLVL %.2f: %.2f", convLVL, targetTempExpo),
  726.         string.format("Saturation needed for zero rise: %d (%3.2f%%)", targetSat, targetCoreSat*100),
  727.         string.format("Error between current saturation and target saturation: %d\n", saturationError),
  728.         string.format("Current field drain is %d RF/t", info.fieldDrainRate),
  729.         string.format("Current temp drain factor for temp %d is %1.2f", info.temperature, tempDrainFactor),
  730.         string.format("fieldNegPercent is %d", fieldNegPercent),
  731.         string.format("Required input to counter field drain: %d RF/t\n", requiredInput),
  732.         string.format("Field Deviation: " .. unicode.char(8776) .. deviation .. "%%"),
  733.         string.format("Input Flux Gate:  [" .. inputFlux.address .. "] Set To: " .. inputFlux.getFlow()),
  734.         string.format("Output Flux Gate: [" .. outputFlux.address .. "] Set To: " .. outputFlux.getFlow())
  735.     }
  736.         for i, v in ipairs(values3) do
  737.             term.setCursor(left_margin, i * spacing)
  738.             term.write(v)
  739.         end
  740.     end
  741. -- print(drawGUI okay)
  742. end
  743. os.sleep(0.1)
  744. print("drawGUI thread exited")
  745. end
  746.  
  747.     -- Parse User Input
  748. function parseInput()
  749.     while eventLoop do
  750.         -- Wait for next tick, or manual shutdown
  751.    
  752.         local event, id, op1, op2 = event.pull(0.01)
  753.         if event == "interrupted" then
  754.         eventLoop = false
  755.         thread.current():join()
  756.         elseif event == "touch" then
  757.  
  758.         -- Handle Button Presses
  759.         local x = op1
  760.         local y = op2
  761.         for bname, button in pairs(buttons) do
  762.             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
  763.             button.action()
  764.             button.depressed = 3
  765.             end
  766.         end
  767.         end
  768.        
  769.         --print("parseInput okay")
  770.     end
  771.     print("parseInputThread exited")
  772. end
  773.  
  774. local primaryCalculationsThread = thread.create(primaryCalculations)
  775. local secondaryCalculationsThread = thread.create(secondaryCalculations)
  776. local drawGUIThread = thread.create(drawGUI)
  777. local parseInputThread = thread.create(parseInput)
  778. thread.waitForAny({primaryCalculationsThread, secondaryCalculations, drawGUIThread, parseInputThread})
  779. --drawUI()
  780. eventLoop = false
  781. reactor.stopReactor()
  782. term.clear()
  783. term.setCursor(1,1)
  784. local maxX, maxY = gpu.maxResolution()
  785. gpu.setResolution(maxX,maxY)
  786. print("Exited")
  787. ::fin::
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement