Advertisement
Samsterminator

Untitled

May 28th, 2023
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.27 KB | None | 0 0
  1. local tag = "reactorConfig"
  2. version = "0.43"
  3. --[[
  4. Program made by DrunkenKas
  5.     See github: https://github.com/Kasra-G/ReactorController/#readme
  6.  
  7. The MIT License (MIT)
  8.  
  9. Copyright (c) 2021 Kasra Ghaffari
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17.  
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. THE SOFTWARE.
  28. ]]
  29. local reactorType
  30. local mon, monSide, reactorSide
  31. local sizex, sizey, dim, oo, offy
  32. local btnOn, btnOff, invalidDim
  33. local minb, maxb, minrod
  34. local rod, rfLost
  35. local storedLastTick, storedThisTick, lastRFT, maxRFT = 0,0,0,1
  36. local fuelTemp, caseTemp, fuelUsage, waste, capacity = 0,0,0,0,0
  37. local t
  38. local displayingGraphMenu = false
  39. local calibrated = false
  40.  
  41. --table of which graphs to draw
  42. local graphsToDraw = {}
  43.  
  44. --table of all the graphs
  45. local graphs =
  46. {
  47.     "Energy Buffer",
  48.     "Control Level",
  49.     "Temperatures",
  50. }
  51.  
  52. --marks the offsets for each graph position
  53. local XOffs =
  54. {
  55.     { 4, true},
  56.     {27, true},
  57.     {50, true},
  58.     {73, true},
  59.     {96, true},
  60. }
  61.  
  62. --Draw a single point
  63. local function drawPoint(x, y, color)
  64.     if (monSide ~= nil) then
  65.         local ix,iy = mon.getCursorPos()
  66.         mon.setCursorPos(x,y)
  67.         mon.setBackgroundColor(color)
  68.         mon.write(" ")
  69.         mon.setBackgroundColor(colors.black)
  70.         mon.setCursorPos(ix,iy)
  71.     end
  72. end
  73.  
  74. --Draw a box with no fill
  75. local function drawBox(size, xoff, yoff, color)
  76.     if (monSide ~= nil) then
  77.         local x,y = mon.getCursorPos()
  78.         mon.setBackgroundColor(color)
  79.         for i=0,size[1] - 1 do
  80.             mon.setCursorPos(xoff + i + 1, yoff + 1)
  81.             mon.write(" ")
  82.             mon.setCursorPos(xoff + i + 1, yoff + size[2])
  83.             mon.write(" ")
  84.         end
  85.         for i=0, size[2] - 1 do
  86.             mon.setCursorPos(xoff + 1, yoff + i + 1)
  87.             mon.write(" ")
  88.             mon.setCursorPos(xoff + size[1], yoff + i +1)
  89.             mon.write(" ")
  90.         end
  91.         mon.setCursorPos(x,y)
  92.         mon.setBackgroundColor(colors.black)
  93.     end
  94. end
  95.  
  96. --Draw a filled box
  97. local function drawFilledBox(size, xoff, yoff, colorOut, colorIn)
  98.     if (monSide ~= nil) then
  99.         local horizLine = ""
  100.         for i=2, size[1] - 1 do
  101.             horizLine  = horizLine.." "
  102.         end
  103.         drawBox(size, xoff, yoff, colorOut)
  104.         local x,y = mon.getCursorPos()
  105.         mon.setBackgroundColor(colorIn)
  106.         for i=2, size[2] - 1 do
  107.             mon.setCursorPos(xoff + 2, yoff + i)
  108.             mon.write(horizLine)
  109.         end
  110.         mon.setBackgroundColor(colors.black)
  111.         mon.setCursorPos(x,y)
  112.     end
  113. end
  114.  
  115. --Draws text on the screen
  116. local function drawText(text, x1, y1, backColor, textColor)
  117.     if (monSide ~= nil) then
  118.         local x, y = mon.getCursorPos()
  119.         mon.setCursorPos(x1, y1)
  120.         mon.setBackgroundColor(backColor)
  121.         mon.setTextColor(textColor)
  122.         mon.write(text)
  123.         mon.setTextColor(colors.white)
  124.         mon.setBackgroundColor(colors.black)
  125.         mon.setCursorPos(x,y)
  126.     end
  127. end
  128.  
  129. --Helper method for adding buttons
  130. local function addButt(name, callBack, size, xoff, yoff, color1, color2)
  131.     if (monSide ~= nil) then
  132.         t:add(name, callBack,
  133.             xoff + 1, yoff + 1,
  134.             size[1] + xoff, size[2] + yoff,
  135.             color1, color2)
  136.     end
  137. end
  138.  
  139. --adds buttons
  140. local function addButtons()
  141.     if (sizey == 24) then
  142.         oo = 1
  143.     end
  144.     addButt("On", nil, {8, 3}, dim + 7, 3 + oo,
  145.         colors.red, colors.lime)
  146.     addButt("Off", nil, {8, 3}, dim + 19, 3 + oo,
  147.         colors.red, colors.lime)
  148.     if (btnOn) then
  149.         t:toggleButton("On")
  150.     else
  151.         t:toggleButton("Off")
  152.     end
  153.     if (sizey > 24) then
  154.         addButt("+ 10", nil, {8, 3}, dim + 7, 14 + oo,
  155.             colors.purple, colors.pink)
  156.         addButt(" + 10 ", nil, {8, 3}, dim + 19, 14 + oo,
  157.             colors.magenta, colors.pink)
  158.         addButt("- 10", nil, {8, 3}, dim + 7, 18 + oo,
  159.             colors.purple, colors.pink)
  160.         addButt(" - 10 ", nil, {8, 3}, dim + 19, 18 + oo,
  161.             colors.magenta, colors.pink)
  162.     end
  163. end
  164.  
  165. --Resets the monitor
  166. local function resetMon()
  167.     if (monSide ~= nil) then
  168.         mon.setBackgroundColor(colors.black)
  169.         mon.clear()
  170.         mon.setTextScale(0.5)
  171.         mon.setCursorPos(1,1)
  172.     end
  173. end
  174.  
  175. local function getPercPower()
  176.     return storedThisTick / capacity * 100
  177. end
  178.  
  179. local function rnd(num, dig)
  180.     return math.floor(10 ^ dig * num) / (10 ^ dig)
  181. end
  182.  
  183. local function getEfficiency()
  184.     return lastRFT / fuelUsage
  185. end
  186.  
  187. local function getGenRatio()
  188.     return lastRFT / capacity
  189. end
  190.  
  191. local function format(num)
  192.     if (num >= 1000000000) then
  193.         return string.format("%7.3f G", num / 1000000000)
  194.     elseif (num >= 1000000) then
  195.         return string.format("%7.3f M", num / 1000000)
  196.     elseif (num >= 1000) then
  197.         return string.format("%7.3f K", num / 1000)
  198.     elseif (num >= 1) then
  199.         return string.format("%7.3f ", num)
  200.     elseif (num >= .001) then
  201.         return string.format("%7.3f m", num * 1000)
  202.     elseif (num >= .000001) then
  203.         return string.format("%7.3f u", num * 1000000)
  204.     else
  205.         return string.format("%7.3f ", 0)
  206.     end
  207. end
  208.  
  209. local function getAvailableXOff()
  210.     for i,v in pairs(XOffs) do
  211.         if (v[2] and v[1] < dim) then
  212.             v[2] = false
  213.             return v[1]
  214.         end
  215.     end
  216.     return -1
  217. end
  218.  
  219. local function getXOff(num)
  220.     for i,v in pairs(XOffs) do
  221.         if (v[1] == num) then
  222.             return v
  223.         end
  224.     end
  225.     return nil
  226. end
  227.  
  228. local function disableGraph(name)
  229.     if (graphsToDraw[name] ~= nil) then
  230.         if (displayingGraphMenu) then
  231.             t:toggleButton(name)
  232.         end
  233.         getXOff(graphsToDraw[name])[2] = true
  234.         graphsToDraw[name] = nil
  235.     end
  236. end
  237.  
  238. local function addGraphButtons()
  239.     offy = oo - 14
  240.     for i,v in pairs(graphs) do
  241.         addButt(v, nil, {20, 3},
  242.             dim + 7, offy + i * 3 - 1,
  243.             colors.red, colors.lime)
  244.         if (graphsToDraw[v] ~= nil) then
  245.             t:toggleButton(v)
  246.         end
  247.     end
  248. end
  249.  
  250. local function drawGraphButtons()
  251.     drawBox({sizex - dim - 3, oo - offy - 1},
  252.         dim + 2, offy, colors.orange)
  253.     drawText(" Graph Controls ",
  254.         dim + 7, offy + 1,
  255.         colors.black, colors.orange)
  256. end
  257.  
  258. local function isGraph(name)
  259.     for i,v in pairs(graphs) do
  260.         if (v == name) then
  261.             return true
  262.         end
  263.     end
  264.     return false
  265. end
  266.  
  267. local function getGraph(num)
  268.     for i,v in pairs(graphsToDraw) do
  269.         if (v == num) then
  270.             return i
  271.         end
  272.     end
  273.     return nil
  274. end
  275.  
  276. local function enableGraph(name)
  277.     if (graphsToDraw[name] == nil) then
  278.         local e = getAvailableXOff()
  279.         if (e ~= -1) then
  280.             graphsToDraw[name] = e
  281.             if (displayingGraphMenu) then
  282.                 t:toggleButton(name)
  283.             end
  284.         end
  285.     end
  286. end
  287.  
  288. local function toggleGraph(name)
  289.     if (graphsToDraw[name] == nil) then
  290.         enableGraph(name)
  291.     else
  292.         disableGraph(name)
  293.     end
  294. end
  295.  
  296. local function drawEnergyBuffer(xoff)
  297.     local srf = sizey - 9
  298.     local off = xoff
  299.     local right = off + 19 < dim
  300.     local poff = right and off + 15 or off - 6
  301.  
  302.     drawBox({15, srf + 2}, off - 1, 4, colors.gray)
  303.     local pwr = math.floor(getPercPower() / 100
  304.         * (srf))
  305.     drawFilledBox({13, srf}, off, 5,
  306.         colors.red, colors.red)
  307.     local rndpw = rnd(getPercPower(), 2)
  308.     local color = (rndpw < maxb and rndpw > minb) and colors.green
  309.     or (rndpw >= maxb and colors.orange or colors.blue)
  310.     if (pwr > 0) then
  311.         drawFilledBox({13, pwr + 1}, off, srf + 4 - pwr,
  312.             color, color)
  313.     end
  314.     --drawPoint(off + 14, srf + 5 - pwr, pwr > 0 and color or colors.red)
  315.     drawText(string.format(right and "%.2f%%" or "%5.2f%%", rndpw), poff, srf + 5 - pwr,
  316.         colors.black, color)
  317.     drawText("Energy Buffer", off + 1, 4,
  318.         colors.black, colors.orange)
  319.     drawText(format(storedThisTick).."RF", off + 1, srf + 5 - pwr,
  320.         pwr > 0 and color or colors.red, colors.black)
  321. end
  322.  
  323. local function drawControlLevel(xoff)
  324.     local srf = sizey - 9
  325.     local off = xoff
  326.     drawBox({15, srf + 2}, off - 1, 4, colors.gray)
  327.     drawFilledBox({13, srf}, off, 5,
  328.         colors.red, colors.red)
  329.     local rodTr = math.floor(rod / 100
  330.         * (srf))
  331.     drawText("Control Level", off + 1, 4,
  332.         colors.black, colors.orange)
  333.     if (rodTr > 0) then
  334.         drawFilledBox({9, rodTr}, off + 2, 5,
  335.             colors.orange, colors.orange)
  336.     end
  337.     drawText(string.format("%6.2f%%", rod), off + 4, rodTr > 0 and rodTr + 5 or 6,
  338.         rodTr > 0 and colors.orange or colors.red, colors.black)
  339.  
  340. end
  341.  
  342. local function drawTemperatures(xoff)
  343.     local srf = sizey - 9
  344.     local off = xoff
  345.     drawBox({15, srf + 2}, off, 4, colors.gray)
  346.     --drawFilledBox({12, srf}, off, 5,
  347.     --  colors.red, colors.red)
  348.  
  349.     local fuelRnd = math.floor(fuelTemp - 273)
  350.     local caseRnd = math.floor(caseTemp - 273)
  351.     local fuelTr = math.floor(fuelRnd / 2000
  352.         * (srf))
  353.     local caseTr = math.floor(caseRnd / 2000
  354.         * (srf))
  355.     drawText(" Case ", off + 2, 5,
  356.         colors.gray, colors.lightBlue)
  357.     drawText(" Fuel ", off + 9, 5,
  358.         colors.gray, colors.magenta)
  359.     if (fuelTr > 0) then
  360.         fuelTr = math.min(fuelTr, srf)
  361.         drawFilledBox({6, fuelTr}, off + 8, srf + 5 - fuelTr,
  362.             colors.magenta, colors.magenta)
  363.  
  364.         drawText(string.format("%4sC", fuelRnd ..""),
  365.             off + 10, srf + 6 - fuelTr,
  366.             colors.magenta, colors.black)
  367.     else
  368.         drawText(string.format("%4sC", fuelRnd ..""),
  369.             off + 10, srf + 5,
  370.             colors.black, colors.magenta)
  371.     end
  372.  
  373.     if (caseTr > 0) then
  374.         caseTr = math.min(caseTr, srf)
  375.         drawFilledBox({6, caseTr}, off + 1, srf + 5 - caseTr,
  376.             colors.lightBlue, colors.lightBlue)
  377.         drawText(string.format("%4sC", caseRnd - 273..""),
  378.             off + 3, srf + 6 - caseTr,
  379.             colors.lightBlue, colors.black)
  380.     else
  381.         drawText(string.format("%4sC", caseRnd - 273..""),
  382.             off + 3, srf + 5,
  383.             colors.black, colors.lightBlue)
  384.     end
  385.  
  386.     drawText("Temperatures", off + 2, 4,
  387.         colors.black, colors.orange)
  388.     drawBox({1, srf}, off + 7, 5,
  389.         colors.gray)
  390. end
  391.  
  392. local beg
  393. local function drawGraph(name, offset)
  394.     if (name == "Energy Buffer") then
  395.         drawEnergyBuffer(offset)
  396.     elseif (name == "Control Level") then
  397.         drawControlLevel(offset)
  398.     elseif (name == "Temperatures") then
  399.         drawTemperatures(offset)
  400.     end
  401. end
  402.  
  403. local function drawGraphs()
  404.     for i,v in pairs(graphsToDraw) do
  405.         if (v + 15 < dim) then
  406.             drawGraph(i,v)
  407.         end
  408.     end
  409. end
  410.  
  411. local function drawStatus()
  412.     if (dim > -1) then
  413.         drawBox({dim, sizey - 2},
  414.             1, 1, colors.lightBlue)
  415.         drawText(" Reactor Graphs ", dim - 18, 2,
  416.             colors.black, colors.lightBlue)
  417.         drawGraphs()
  418.     end
  419. end
  420.  
  421. local function drawControls()
  422.     if (sizey == 24) then
  423.         drawBox({sizex - dim - 3, 9}, dim + 2, oo,
  424.             colors.cyan)
  425.         drawText(" Reactor Controls ", dim + 7, oo + 1,
  426.             colors.black, colors.cyan)
  427.         drawText("Reactor "..(btnOn and "Online" or "Offline"),
  428.             dim + 10, 3 + oo,
  429.             colors.black, btnOn and colors.green or colors.red)
  430.     else
  431.         drawBox({sizex - dim - 3, 23}, dim + 2, oo,
  432.             colors.cyan)
  433.         drawText(" Reactor Controls ", dim + 7, oo + 1,
  434.             colors.black, colors.cyan)
  435.         drawFilledBox({20, 3}, dim + 7, 8 + oo,
  436.             colors.red, colors.red)
  437.         drawFilledBox({(maxb - minb) / 5, 3},
  438.             dim + 7 + minb / 5, 8 + oo,
  439.             colors.green, colors.green)
  440.         drawText(string.format("%3s", minb.."%"), dim + 6 + minb / 5, 12 + oo,
  441.             colors.black, colors.purple)
  442.         drawText(maxb.."%", dim + 8 + maxb / 5, 12 + oo,
  443.             colors.black, colors.magenta)
  444.         drawText("Buffer Target Range", dim + 8, 8 + oo,
  445.             colors.black, colors.orange)
  446.         drawText("Min", dim + 10, 14 + oo,
  447.             colors.black, colors.purple)
  448.         drawText("Max", dim + 22, 14 + oo,
  449.             colors.black, colors.magenta)
  450.         drawText("Reactor ".. (btnOn and "Online" or "Offline"),
  451.             dim + 10, 3 + oo,
  452.             colors.black, btnOn and colors.green or colors.red)
  453.     end
  454. end
  455.  
  456. local function drawStatistics()
  457.     local oS = sizey - 13
  458.     drawBox({sizex - dim - 3, sizey - oS - 1}, dim + 2, oS,
  459.         colors.blue)
  460.     drawText(" Reactor Statistics ", dim + 7, oS + 1,
  461.         colors.black, colors.blue)
  462.  
  463.     --statistics
  464.     drawText("Generating : "
  465.         ..format(lastRFT).."RF/t", dim + 5, oS + 3,
  466.         colors.black, colors.green)
  467.     drawText("RF Drain   "
  468.         ..(storedThisTick <= lastRFT and "> " or ": ")
  469.         ..format(rfLost)
  470.         .."RF/t", dim + 5, oS + 5,
  471.         colors.black, colors.red)
  472.     drawText("Efficiency : "
  473.         ..format(getEfficiency()).."RF/B",
  474.         dim + 5, oS + 7,
  475.         colors.black, colors.green)
  476.     drawText("Fuel Usage : "
  477.         ..format(fuelUsage)
  478.         .."B/t", dim + 5, oS + 9,
  479.         colors.black, colors.green)
  480.     drawText("Waste      : "
  481.         ..string.format("%7d mB", waste),
  482.         dim + 5, oS + 11,
  483.         colors.black, colors.green)
  484. end
  485.  
  486. --Draw a scene
  487. local function drawScene()
  488.     if (monSide ~= nil) then
  489.         t:draw()
  490.     end
  491.     if (displayingGraphMenu) then
  492.         drawGraphButtons()
  493.     end
  494.     if (invalidDim) then
  495.         if (monSide ~= nil) then
  496.             mon.write("Invalid Monitor Dimensions")
  497.         end
  498.     else
  499.         drawControls()
  500.         drawStatus()
  501.         drawStatistics()
  502.     end
  503. end
  504.  
  505. --Redraws all the buttons
  506. --Updates the important values
  507. local function reDrawButtons()
  508.     if (monSide ~= nil) then
  509.         t = touchpoint.new(monSide)
  510.         sizex, sizey = mon.getSize()
  511.         oo = sizey - 37
  512.         dim = sizex - 33
  513.     end
  514.     --print(sizex, sizey)
  515.     if (sizex == 36) then
  516.         dim = -1
  517.     end
  518.     if (pcall(addGraphButtons)) then
  519.         drawGraphButtons()
  520.         displayingGraphMenu = true
  521.     else
  522.         if (monSide ~= nil) then
  523.             t = touchpoint.new(monSide)
  524.         end
  525.         displayingGraphMenu = false
  526.     end
  527.     local rtn = pcall(addButtons)
  528.     if (not rtn) then
  529.         if (monSide ~= nil) then
  530.             t = touchpoint.new(monSide)
  531.         end
  532.         invalidDim = true
  533.     else
  534.         invalidDim = false
  535.     end
  536.     --t:draw()
  537. end
  538.  
  539. local function setRods(level)
  540.     level = math.max(level, 0)
  541.     level = math.min(level, 100)
  542.     reactor.setAllControlRodLevels(level)
  543. end
  544.  
  545. --Turns off the reactor
  546. local function turnOff()
  547.     if (btnOn) then
  548.         t:toggleButton("Off")
  549.         t:toggleButton("On")
  550.         btnOff = true
  551.         btnOn = false
  552.         setRods(100)
  553.         reactor.setActive(false)
  554.     end
  555. end
  556.  
  557. --Turns on the reactor
  558. local function turnOn()
  559.     if (btnOff) then
  560.         t:toggleButton("Off")
  561.         t:toggleButton("On")
  562.         btnOff = false
  563.         btnOn = true
  564.         reactor.setActive(true)
  565.     end
  566. end
  567.  
  568. --adjusts the level of the rods
  569. local function adjustRods()
  570.     local currentRF = storedThisTick
  571.     local diffb = maxb - minb
  572.     maxRF = maxb / 100 * capacity
  573.     minRF = minb / 100 * capacity
  574.     diffRF = diffb / 100 * capacity
  575.     local diffr = diffb / 100
  576.     local targetRFT = rfLost
  577.     local currentRFT = lastRFT
  578.     local diffRFT = currentRFT/targetRFT
  579.     local targetRF = diffRF / 2 + minRF
  580.  
  581.     currentRF = math.min(currentRF, maxRF)
  582.     local equation1 = math.min((currentRF - minRF)/diffRF, 1)
  583.     equation1 = math.max(equation1, 0)
  584.    
  585.     local rodLevel = rod
  586.     if (storedThisTick < minRF) then
  587.         rodLevel = 0
  588.     elseif ((storedThisTick < maxRF and storedThisTick > minRF)) then
  589.         equation1 = equation1 * (currentRF / targetRF) --^ 2
  590.         equation1 = equation1 * diffRFT --^ 5
  591.         equation1 = equation1 * 100
  592.  
  593.         rodLevel = equation1
  594.     elseif (storedThisTick > maxRF) then
  595.         rodLevel = 100
  596.     end
  597.     setRods(rodLevel)
  598. end
  599.  
  600. --Saves the configuration of the reactor controller
  601. local function saveChanges()
  602.     local file = fs.open(tag..".txt", "w")
  603.     file.writeLine(calibrated)
  604.     if (calibrated) then
  605.         file.writeLine(capacity)
  606.         file.writeLine(maxRFT)
  607.     end
  608.     file.writeLine(maxb)
  609.     file.writeLine(minb)
  610.     file.writeLine(rod)
  611.     file.writeLine(btnOn)
  612.     for i,v in pairs(XOffs) do
  613.         local graph = getGraph(v[1])
  614.         graph = (graph == nil and "nil" or graph)
  615.         file.writeLine(graph)
  616.         file.writeLine(v[1])
  617.     end
  618.     file.close()
  619. end
  620.  
  621. --returns the side that a given peripheral type is connected to
  622. local function getPeripheral(name)
  623.     for i,v in pairs(peripheral.getNames()) do
  624.         if (peripheral.getType(v) == name) then
  625.             return v
  626.         end
  627.     end
  628.     return ""
  629. end
  630.  
  631. local function updateStats()
  632.     storedLastTick = storedThisTick
  633.     if (reactorVersion == "Big Reactors") then
  634.         storedThisTick = reactor.getEnergyStored()
  635.         lastRFT = reactor.getEnergyProducedLastTick()
  636.         rod = reactor.getControlRodLevel(0)
  637.         fuelUsage = reactor.getFuelConsumedLastTick() / 1000
  638.         waste = reactor.getWasteAmount()
  639.         fuelTemp = reactor.getFuelTemperature()
  640.         caseTemp = reactor.getCasingTemperature()
  641.     elseif (reactorVersion == "Extreme Reactors") then
  642.         local bat = reactor.getEnergyStats()
  643.         local fuel = reactor.getFuelStats()
  644.  
  645.         storedThisTick = bat.energyStored
  646.         lastRFT = bat.energyProducedLastTick
  647.         capacity = bat.energyCapacity
  648.         rod = reactor.getControlRodLevel(0)
  649.         fuelUsage = fuel.fuelConsumedLastTick / 1000
  650.         waste = reactor.getWasteAmount()
  651.         fuelTemp = reactor.getFuelTemperature()
  652.         caseTemp = reactor.getCasingTemperature()
  653.     elseif (reactorVersion == "Bigger Reactors") then
  654.         storedThisTick = reactor.battery().stored()
  655.         lastRFT = reactor.battery().producedLastTick()
  656.         capacity = reactor.battery().capacity()
  657.         rod = reactor.getControlRod(0).level()
  658.         fuelUsage = reactor.fuelTank().burnedLastTick() / 1000
  659.         waste = reactor.fuelTank().waste()
  660.         fuelTemp = reactor.fuelTemperature()
  661.         caseTemp = reactor.casingTemperature()
  662.     end
  663.     rfLost = lastRFT + storedLastTick - storedThisTick
  664. end
  665.  
  666. --Updates statistics and adjusts the rods
  667. local function compute()
  668.     updateStats()
  669.     if (btnOn) then
  670.         adjustRods()
  671.     end
  672. end
  673.  
  674. --The main routine that runs each tick
  675. function routine()
  676.     while (true) do
  677.         --[[
  678.         If the graphs are drawn every tick, everything
  679.         just breaks.
  680.         If the graphs are drawn every 2nd tick, my
  681.         RFLost calculation is wrong every time
  682.         If the graphs are drawn every 3rd tick, my
  683.         RFLost calculation is wrong sometimes
  684.  
  685.         THIS MAKES NO SENSE
  686.         ]]
  687.         for i = 1,4 do 
  688.             compute()
  689.             sleep(0.01)
  690.         end
  691.         resetMon()
  692.         drawScene()
  693.     end
  694. end
  695.  
  696. --Manages window resizing events
  697. function resizer()
  698.     while (true) do
  699.         local event = os.pullEvent("monitor_resize")
  700.         if (event == "monitor_resize") then
  701.             reDrawButtons()
  702.         end
  703.     end
  704. end
  705.  
  706. local function calibrate()
  707.     setRods(0)
  708.     reactor.setActive(true)
  709.     sleep(15)
  710.     updateStats()
  711.     setRods(100)
  712.     reactor.setActive(false)
  713.     if (reactorVersion == "Big Reactors") then
  714.         capacity = storedThisTick
  715.     end
  716.     maxRFT = lastRFT
  717. end
  718.  
  719. --Initialize variables from either a config file or the defaults
  720. local function initializeVars()
  721.     invalidDim = false
  722.     if (not fs.exists(tag..".txt")) then
  723.         print("Config file "..tag.." not found, generating a default one!")
  724.         repeat
  725.             print("The program can be optionally calibrated. Proceed? (y/n) ")
  726.             local response = read()
  727.             if (response == "n") then
  728.                 print("Calibration skipped. Some functions may be unavailable")
  729.                 calbrated = false
  730.             elseif (response == "y") then
  731.                 print("Beginning 15 second calibration, do not turn off the reactor!")
  732.                 calibrate()
  733.                 print("Calibrated!")
  734.                 calibrated = true
  735.             end
  736.         until response == "y" or response == "n"
  737.        
  738.         maxb = 70
  739.         minb = 30
  740.         rod = 80
  741.         btnOn = false
  742.         if (monSide == nil) then
  743.             btnOn = true
  744.         end
  745.         dim = sizex - 33
  746.         oo = sizey - 37
  747.         enableGraph("Energy Buffer")
  748.         enableGraph("Control Level")
  749.         enableGraph("Temperatures")
  750.     else
  751.         local file = fs.open(tag..".txt", "r")
  752.         print("Config file "..tag.." found! Using configurated settings")
  753.  
  754.         calibrated = file.readLine() == "true"
  755.        
  756.         --read calibration information
  757.         if (calibrated) then
  758.             capacity = tonumber(file.readLine())
  759.             maxRFT = tonumber(file.readLine())
  760.         end
  761.         maxb = tonumber(file.readLine())
  762.         minb = tonumber(file.readLine())
  763.         rod = tonumber(file.readLine())
  764.         btnOn = file.readLine() == "true"
  765.  
  766.         --read Graph data
  767.         for i in pairs(XOffs) do
  768.             local graph = file.readLine()
  769.             local v1 = tonumber(file.readLine())
  770.             local v2 = true
  771.             if (graph ~= "nil") then
  772.                 v2 = false
  773.                 graphsToDraw[graph] = v1
  774.             end
  775.  
  776.             XOffs[i] = {v1, v2}
  777.  
  778.         end
  779.         file.close()
  780.     end
  781.     btnOff = not btnOn
  782.     diffb = maxb - minb
  783.     reactor.setActive(btnOn)
  784. end
  785.  
  786. --Initialize program
  787. local function initialize()
  788.     term.setBackgroundColor(colors.black)
  789.     term.clear()
  790.     term.setCursorPos(1,1)
  791.     os.loadAPI("/usr/apis/touchpoint.lua")
  792.     reactorSide = getPeripheral("BiggerReactors_Reactor")
  793.     reactorVersion = "Bigger Reactors"
  794.     reactor = peripheral.wrap(reactorSide)
  795.     if (reactor == nil) then
  796.         reactorSide = getPeripheral("BigReactors-Reactor")
  797.         reactor = peripheral.wrap(reactorSide)
  798.         if (reactor.mbIsConnected ~= nil) then
  799.             reactorVersion = "Extreme Reactors"
  800.         else
  801.             reactorVersion = "Big Reactors"
  802.         end
  803.     end
  804.     if (reactor == nil) then
  805.         reactorSide = getPeripheral("bigger-reactor")
  806.         reactor = peripheral.wrap(reactorSide)
  807.         reactorVersion = "Big Reactors"
  808.     end
  809.     monSide = getPeripheral("monitor")
  810.     monSide = monSide == "" and nil or monSide
  811.     sizex, sizey = 36, 38
  812.     if (monSide ~= nil) then
  813.         mon = peripheral.wrap(monSide)
  814.         sizex, sizey = mon.getSize()
  815.         resetMon()
  816.     end
  817.     return reactor ~= nil
  818. end
  819.  
  820. --Entry point
  821. function threadMain()
  822.     repeat
  823.         local good = initialize()
  824.         if (not good) then
  825.             print("Reactor could not be detected! Trying again")
  826.             sleep(1)
  827.         else
  828.             print("Reactor detected! Proceeding with initialization: ")
  829.         end
  830.     until (good)
  831.     initializeVars()
  832.     reDrawButtons()
  833.     saveChanges()
  834.     print("Reactor initialization done!")
  835.     sleep(2)
  836.     term.clear()
  837.     term.setCursorPos(1,1)
  838.     os.startThread(resizer)
  839.     os.startThread(routine)
  840.     print("Reactor Controller Version "..version)
  841.     print("Reactor Mod: "..reactorVersion)
  842.     --main loop
  843.     --local lastTime = 0
  844.  
  845.     os.startTimer(0.01)
  846.     while (true) do
  847.         local event, p1
  848.         if (monSide ~= nil) then
  849.             event, p1 = t:handleEvents(os.pullEvent("monitor_touch"))
  850.         else
  851.             event = os.pullEvent("monitor_touch")
  852.         end
  853.         if (event == "button_click") then
  854.             if (p1 == "Off") then
  855.                 turnOff()
  856.             elseif (p1 == "On") then
  857.                 turnOn()
  858.             elseif (p1 == "+ 10") then
  859.                 minb = math.min(maxb - 10, minb + 10)
  860.             elseif (p1 == "- 10") then
  861.                 minb = math.max(0, minb - 10)
  862.             elseif (p1 == " + 10 ") then
  863.                 maxb = math.min(100, maxb + 10)
  864.             elseif (p1 == " - 10 ") then
  865.                 maxb = math.max(minb + 10, maxb - 10)
  866.             elseif (isGraph(p1)) then
  867.                 toggleGraph(p1)
  868.             end
  869.             saveChanges()
  870.         end
  871.     end
  872. end
  873.  
  874. --thread stuff below here
  875. local threads = {}
  876. local starting = {}
  877. local eventFilter = nil
  878.  
  879. rawset(os, "startThread", function(fn, blockTerminate)
  880.         table.insert(starting, {
  881.                 cr = coroutine.create(fn),
  882.                 blockTerminate = blockTerminate or false,
  883.                 error = nil,
  884.                 dead = false,
  885.                 filter = nil
  886.             })
  887.     end)
  888.  
  889. local function tick(t, evt, ...)
  890.     if t.dead then return end
  891.     if t.filter ~= nil and evt ~= t.filter then return end
  892.     if evt == "terminate" and t.blockTerminate then return end
  893.  
  894.     coroutine.resume(t.cr, evt, ...)
  895.     t.dead = (coroutine.status(t.cr) == "dead")
  896. end
  897.  
  898. local function tickAll()
  899.     if #starting > 0 then
  900.         local clone = starting
  901.         starting = {}
  902.         for _,v in ipairs(clone) do
  903.             tick(v)
  904.             table.insert(threads, v)
  905.         end
  906.     end
  907.     local e
  908.     if eventFilter then
  909.         e = {eventFilter(coroutine.yield())}
  910.     else
  911.         e = {coroutine.yield()}
  912.     end
  913.     local dead = nil
  914.     for k,v in ipairs(threads) do
  915.         tick(v, unpack(e))
  916.         if v.dead then
  917.             if dead == nil then dead = {} end
  918.             table.insert(dead, k - #dead)
  919.         end
  920.     end
  921.     if dead ~= nil then
  922.         for _,v in ipairs(dead) do
  923.             table.remove(threads, v)
  924.         end
  925.     end
  926. end
  927.  
  928. rawset(os, "setGlobalEventFilter", function(fn)
  929.         if eventFilter ~= nil then error("This can only be set once!") end
  930.         eventFilter = fn
  931.         rawset(os, "setGlobalEventFilter", nil)
  932.     end)
  933.  
  934. if type(threadMain) == "function" then
  935.     os.startThread(threadMain)
  936. else
  937.     os.startThread(function() shell.run("shell") end)
  938. end
  939.  
  940. while #threads > 0 or #starting > 0 do
  941.     tickAll()
  942. end
  943.  
  944. print("All threads terminated!")
  945. print("Exiting thread manager")
  946.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement