Advertisement
omr__

Bigger Reactor

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