krakaen

[Krakaen] Reactors Madness

Feb 13th, 2016 (edited)
1,721,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.84 KB | None | 0 0
  1. -- pay attention to the fact that things return error now instead of origin values
  2. -- especially on reassembling on restart of server and things of the sort
  3.  
  4.  
  5.  
  6. -- ****************************************************** --
  7. -- ****************************************************** --
  8. -- **             Reactor Madness Program              ** --
  9. -- **                 for Big Reactor                  ** --
  10. -- **                Written by krakaen                ** --
  11. -- **                  Video Tutorial                  ** --
  12. -- **   https://www.youtube.com/watch?v=SbbT7ncyS2M    ** --
  13. -- ****************************************************** --
  14. -- ****************************************************** --
  15.  
  16. -- ******** Version 0.1 Changelog (02/16/2016) ********
  17. -- Changed currentRFTotal to 1 (makes power show at start)
  18.  
  19. -- ******** Version 0.2 Changelog (02/16/2016) ********
  20. -- Added fuel usage (mb/t)
  21. -- Added function round
  22. -- Added function comma_value
  23. -- Added function format_num
  24.  
  25. -- ******** Version 0.3 Changelog (02/17/2016) ********
  26. -- Change Rod looking for 0 instead of 1
  27.  
  28. -- ******** Version 0.4 Changelog (10/18/2016) ********
  29. -- Change rodLevel to do a Math.ceil instead of Math.floor
  30.  
  31. -- ******** Version 0.5 Changelog (03/01/2017) ********
  32. -- Change drawBoxed and drawFilledBox for drawLines with for loop
  33. -- to get compatibility with 1.60+
  34.  
  35. -- ******** Version 0.6 Changelog (03/22/2017) ********
  36. -- Added Custom error handling
  37. -- fixed typo in controlsSize for controlsSize
  38.  
  39. -- ******** Version 0.7 Changelog (05/15/2018) ********
  40. -- Added new functions for extreme reactor handling. Will work on both new and old versions
  41.  
  42. -- ******** Version 0.8 Changelog (31/10/2020) ********
  43. -- Added new functions for Bigger reactor handling. Will work on both new, newer and old versions
  44.  
  45. -- ******** Version 0.9 Changelog (24/04/2021) ********
  46. -- Added new functions for newer Bigger reactor handling. Will work on both new, newer and old versions.
  47.  
  48. -- ******** Version 0.10 Changelog (19/09/2021) ********
  49. -- Added fix to calculate size of power tank on Extreme Reactor and fixed the total RB decimal issue.
  50.  
  51. -- you need to give the index to be able to use the program
  52. -- ex : NameOfProgram Index   (reactor Krakaen)
  53.  
  54. local args = { ... }
  55.  
  56. local button = {}
  57. local filleds = {}
  58. local boxes = {}
  59. local lines = {}
  60. local texts = {}
  61.  
  62. local refresh = true
  63.  
  64. local infosSize = {}
  65. local controlsSize = {}
  66. local numbersSize = {}
  67. local currentRfTotal = 1
  68. local currentRfTick = 1
  69. local currentFuelConsumedLastTick = 0.00001
  70.  
  71.  
  72. local rfPerTickMax = 1
  73. local rfTotalMax = 10000000
  74. local minPowerRod = 0
  75. local maxPowerRod = 100
  76. local currentRodLevel = 1
  77.  
  78. local index = ""
  79.  
  80. local reactors = {}
  81. local monitors = {}
  82.  
  83. local VERSION = "NONE"
  84.  
  85. function checkVersionTooOld()
  86.     local uselessMonitors = {peripheral.find("monitor")}
  87. end
  88.  
  89. function checkMBFunctionAvailability()
  90.     local uselessMbConnected = reactors[1].mbIsConnected()
  91.     local uselessMbAssembled = reactors[1].mbIsAssembled()
  92. end
  93.  
  94. function checkEnergyCapacityFunction()
  95.     local uselessEnergyCapacity = reactors[1].getEnergyStats().energyCapacity
  96. end
  97.  
  98. -- VERSIONS - BIG, EXTREME, BIGGERv1, BIGGERv2
  99. function initReactor()
  100.     if pcall(checkVersionTooOld) then
  101.         monitors = {peripheral.find("monitor")}
  102.  
  103.         -- allow 5 seconds to detect if the reactor is on the network
  104.         print("Detecting reactor. This may take up to 5 seconds.")
  105.         local looptyloop = 0
  106.         while looptyloop ~= 5 do
  107.             if peripheral.find("BigReactors-Reactor") ~= nil then
  108.                 reactors = {peripheral.find("BigReactors-Reactor")}
  109.                 if pcall(checkEnergyCapacityFunction) then
  110.                     rfTotalMax = reactors[1].getEnergyStats().energyCapacity
  111.                 end
  112.                 if pcall(checkMBFunctionAvailability) then
  113.                     VERSION = "EXTREME"
  114.                 else
  115.                     VERSION = "BIG"
  116.                 end
  117.                 break
  118.             elseif peripheral.find("bigger-reactor") ~= nil then
  119.                 reactors = {peripheral.find("bigger-reactor")}
  120.                 VERSION = "BIGGERv1"
  121.                 break
  122.             elseif peripheral.find("BiggerReactors_Reactor") ~= nil then
  123.                 reactors = {peripheral.find("BiggerReactors_Reactor")}
  124.                 VERSION = "BIGGERv2"
  125.                 rfTotalMax = reactors[1].battery().capacity()
  126.                 break
  127.             end
  128.  
  129.             sleep(1)
  130.             looptyloop = looptyloop + 1
  131.         end
  132.  
  133.         if monitors[1] == nil then
  134.             error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  135.         end
  136.  
  137.         if reactors[1] == nil then
  138.             error("The Reactor is not being detected, make sure the connections(modem) are activated. The problem could also be related to chunk protection on some public servers, ask an admin about it.", 0)
  139.         end
  140.     else
  141.         error("The version of ComputerCraft is too old to use this program, sorry", 0)
  142.     end
  143.     print("Reactor detected. Program Starting.")
  144. end
  145.  
  146. if (#args == 0) then
  147.     error("No index Given, Make sure to start the 'start' program and not the 'reactor' program", 0)
  148. end
  149.  
  150. if (#args == 1) then
  151.     index = args[1]
  152. end
  153.  
  154. initReactor() -- Init and Verify that everything is okay to start the program
  155.  
  156. local mon = monitors[1]
  157.  
  158. -- Use the black thingy sponge to clear the chalkboard
  159.  
  160. term.redirect(mon)
  161. mon.clear()
  162. mon.setTextColor(colors.white)
  163. mon.setBackgroundColor(colors.black)
  164.  
  165. function clearTable()
  166.     button = {}
  167. end
  168.  
  169.  
  170. -- All the things that make my buttons work
  171.  
  172. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  173.     button[name] = {}
  174.     button[name]["title"] = title
  175.     button[name]["func"] = func
  176.     button[name]["active"] = false
  177.     button[name]["xmin"] = xmin
  178.     button[name]["ymin"] = ymin
  179.     button[name]["xmax"] = xmax
  180.     button[name]["ymax"] = ymax
  181.     button[name]["color"] = color
  182.     button[name]["elem"] = elem
  183.     button[name]["elem2"] = elem2
  184. end
  185.  
  186. -- stuff and things for buttons
  187.  
  188. function fill(text, color, bData)
  189.    mon.setBackgroundColor(color)
  190.    mon.setTextColor(colors.white)
  191.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  192.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  193.    for j = bData["ymin"], bData["ymax"] do
  194.       mon.setCursorPos(bData["xmin"], j)
  195.       if j == yspot then
  196.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  197.             if k == xspot then
  198.                mon.write(bData["title"])
  199.             else
  200.                mon.write(" ")
  201.             end
  202.          end
  203.       else
  204.          for i = bData["xmin"], bData["xmax"] do
  205.             mon.write(" ")
  206.          end
  207.       end
  208.    end
  209.    mon.setBackgroundColor(colors.black)
  210. end
  211.  
  212. -- stuff and things for buttons
  213.  
  214. function screen()
  215.    local currColor
  216.    for name,data in pairs(button) do
  217.       local on = data["active"]
  218.       currColor = data["color"]
  219.       fill(name, currColor, data)
  220.    end
  221. end
  222.  
  223. -- stuff and things for buttons
  224.  
  225. function flash(name)
  226.     screen()
  227. end
  228.  
  229. -- magical handler for clicky clicks
  230.  
  231. function checkxy(x, y)
  232.    for name, data in pairs(button) do
  233.       if y>=data["ymin"] and  y <= data["ymax"] then
  234.          if x>=data["xmin"] and x<= data["xmax"] then
  235.             data["func"](data["elem"], data["elem2"])
  236.             flash(data['name'])
  237.             return true
  238.             --data["active"] = not data["active"]
  239.             --print(name)
  240.          end
  241.       end
  242.    end
  243.    return false
  244. end
  245.  
  246. -- Don't question my code, it works on my machine...
  247.  
  248. function label(w, h, text)
  249.    mon.setCursorPos(w, h)
  250.    mon.write(text)
  251. end
  252.  
  253. -- Draw function : put's all the beautiful magic in the screen
  254.  
  255. function draw()
  256.     for key,value in pairs(filleds) do
  257.         local counter = 1
  258.         for i=value[2],value[4],1 do
  259.             paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  260.             counter = counter + 1
  261.         end
  262.     end
  263.  
  264.     for key,value in pairs(boxes) do
  265.         paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  266.         paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  267.         paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  268.         paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  269.     end
  270.  
  271.     for key,value in pairs(lines) do
  272.         paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  273.     end
  274.  
  275.     for key,value in pairs(texts) do
  276.         mon.setCursorPos(value[1], value[2])
  277.         mon.setTextColor(value[4])
  278.         mon.setBackgroundColor(value[5])
  279.         mon.write(value[3])
  280.     end
  281.     screen()
  282.     resetDraw()
  283. end
  284.  
  285. -- Resets the elements to draw to only draw the neccessity
  286.  
  287. function resetDraw()
  288.     filleds = {}
  289.     boxes = {}
  290.     lines = {}
  291.     texts = {}
  292. end
  293.  
  294. -- Handles all the clicks for the buttons
  295.  
  296. function clickEvent()
  297.     local myEvent={os.pullEvent("monitor_touch")}
  298.     checkxy(myEvent[3], myEvent[4])
  299. end
  300.  
  301. -- Power up the reactor (M&N are a good source of food right?)
  302.  
  303. function powerUp(m,n)
  304.     local reactor = reactors[1]
  305.     reactor.setActive(true)
  306. end
  307.  
  308. -- Power down the reactor (M&N are a good source of food right?)
  309.  
  310. function powerDown(m,n)
  311.     local reactor = reactors[1]
  312.     reactor.setActive(false)
  313. end
  314.  
  315. -- Handles and calculate the Min and Max of the power limitation
  316.  
  317. function modifyRods(limit, number)
  318.     local tempLevel = 0
  319.  
  320.     if limit == "min" then
  321.         tempLevel = minPowerRod + number
  322.         if tempLevel <= 0 then
  323.             minPowerRod = 0
  324.         end
  325.  
  326.         if tempLevel >= maxPowerRod then
  327.             minPowerRod = maxPowerRod -10
  328.         end
  329.  
  330.         if tempLevel < maxPowerRod and tempLevel > 0 then
  331.             minPowerRod = tempLevel
  332.         end
  333.     else
  334.         tempLevel = maxPowerRod + number
  335.         if tempLevel <= minPowerRod then
  336.             maxPowerRod = minPowerRod +10
  337.         end
  338.  
  339.         if tempLevel >= 100 then
  340.             maxPowerRod = 100
  341.         end
  342.  
  343.         if tempLevel > minPowerRod and tempLevel < 100 then
  344.             maxPowerRod = tempLevel
  345.         end
  346.     end
  347.  
  348.     table.insert(lines, {controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, controlsSize['inX'] + controlsSize['width'], controlsSize['inY']+(controlsSize['sectionHeight']*1)+4, colors.black})
  349.  
  350.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  351.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  352.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  353.  
  354.     setInfoToFile()
  355.     adjustRodsLevel()
  356. end
  357.  
  358. -- Calculate and adjusts the level of the rods
  359. -- TOMODIFY
  360. function adjustRodsLevel()
  361.     local reactor = reactors[1]
  362.  
  363.     local allStats = getAllStats()
  364.     local currentRfTotal = allStats["rfTotal"]
  365.     local reactorRodsLevel = allStats["reactorRodsLevel"]
  366.  
  367.     differenceMinMax = maxPowerRod - minPowerRod
  368.  
  369.     maxPower = (rfTotalMax/100) * maxPowerRod
  370.     minPower = (rfTotalMax/100) * minPowerRod
  371.  
  372.     if currentRfTotal >= maxPower then
  373.         currentRfTotal = maxPower
  374.     end
  375.  
  376.     if currentRfTotal <= minPower then
  377.         currentRfTotal = minPower
  378.     end
  379.  
  380.     currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  381.  
  382.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  383.     local rodLevel = math.ceil((currentRfTotal/rfInBetween)*100)
  384.     if VERSION == "EXTREME" then
  385.         for key,value in pairs(reactorRodsLevel) do
  386.             reactorRodsLevel[key] = rodLevel
  387.         end
  388.         reactor.setControlRodsLevels(reactorRodsLevel)
  389.     else
  390.         reactor.setAllControlRodLevels(rodLevel)
  391.     end
  392. end
  393.  
  394. -- Creates the frame and the basic of the visual
  395. -- Also adds the variables informations for placement of stuff and things
  396.  
  397. function addDrawBoxesSingleReactor()
  398.     local w, h = mon.getSize()
  399.     local margin = math.floor((w/100)*2)
  400.  
  401.     infosSize['startX'] = margin + 1
  402.     infosSize['startY'] =  margin + 1
  403.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  404.     infosSize['endY'] = h - margin
  405.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  406.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  407.     infosSize['inX'] = infosSize['startX'] + margin +1
  408.     infosSize['inY'] = infosSize['startY'] + margin +1
  409.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  410.  
  411.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  412.     local name = "INFOS"
  413.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  414.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  415.  
  416.     local names = {}
  417.     names[1] = 'ENERGY LAST TICK'
  418.     names[2] = 'ENERGY STORED'
  419.     names[3] = 'CONTROL ROD LEVEL'
  420.  
  421.     for i=0,2,1 do
  422.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  423.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 2 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  424.     end
  425.  
  426.  
  427.     -- Controls
  428.  
  429.     controlsSize['startX'] = infosSize['endX'] + margin + 1
  430.     controlsSize['startY'] =  margin + 1
  431.     controlsSize['endX'] = w-margin
  432.     controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  433.     controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  434.     controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  435.     controlsSize['inX'] = controlsSize['startX'] + margin +1
  436.     controlsSize['inY'] = controlsSize['startY'] + margin
  437.  
  438.     table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.gray})
  439.     name = "CONTROLS"
  440.     table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  441.     table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  442.  
  443.     controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  444.  
  445.     reactor = reactors[1]
  446.  
  447.     mon.setTextColor(colors.white)
  448.     setButton("ON","ON", powerUp, controlsSize['inX'], controlsSize['inY'], controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +2, 0, 0, colors.green)
  449.     setButton("OFF","OFF", powerDown, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'], controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +2,0, 0, colors.red)
  450.  
  451.     table.insert(texts, {controlsSize['inX']+8, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  452.  
  453.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  454.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  455.  
  456.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  457.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  458.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  459.     mon.setTextColor(colors.white)
  460.  
  461.     setButton("low-10","-10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "min", -10, colors.lightBlue)
  462.     setButton("high-10","-10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "max", -10, colors.purple)
  463.  
  464.     setButton("low+10","+10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "min", 10, colors.lightBlue)
  465.     setButton("high+10","+10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "max", 10, colors.purple)
  466.  
  467.     -- Numbers
  468.  
  469.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  470.     numbersSize['startY'] = controlsSize['endY'] + margin + 1
  471.     numbersSize['endX'] = w-margin
  472.     numbersSize['endY'] = h-margin
  473.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  474.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  475.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  476.     numbersSize['inY'] = numbersSize['startY'] + margin
  477.  
  478.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  479.     name = "NUMBERS"
  480.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  481.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  482.  
  483.     refresh = true
  484.     while refresh do
  485.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  486.     end
  487. end
  488.  
  489. -- Gets the stats needed for the program to function based on the version of the reactor
  490. -- TOMODIFY
  491. function getAllStats()
  492.     local stats = {}
  493.     local reactor = reactors[1]
  494.  
  495.     if VERSION == "EXTREME" then
  496.         local reactorEnergyStats = reactor.getEnergyStats()
  497.         local reactorFuelStats = reactor.getFuelStats()
  498.  
  499.         stats["reactorRodsLevel"] = reactor.getControlRodsLevels()
  500.         stats["rfTotal"] = math.floor(reactorEnergyStats["energyStored"])
  501.         stats["rfPerTick"] = math.floor(reactorEnergyStats["energyProducedLastTick"])
  502.         stats["rodLevel"] = stats["reactorRodsLevel"][0]
  503.         stats["fuelPerTick"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  504.     elseif VERSION == "BIG" or VERSION == "BIGGERv1" then  
  505.         stats["rfTotal"] = math.floor(reactor.getEnergyStored())
  506.         stats["rfPerTick"] = math.floor(reactor.getEnergyProducedLastTick())
  507.         stats["rodLevel"] = math.floor(reactor.getControlRodLevel(0))
  508.         stats["fuelPerTick"] = reactor.getFuelConsumedLastTick()
  509.     elseif VERSION == "BIGGERv2" then
  510.         stats["rfTotal"] = reactor.battery().stored()
  511.         stats["rfPerTick"] = math.floor(reactor.battery().producedLastTick())
  512.         stats["rodLevel"] = math.floor(reactor.getControlRod(0).level())
  513.         stats["fuelPerTick"] = reactor.fuelTank().burnedLastTick()
  514.     end
  515.    
  516.     return stats
  517. end
  518.  
  519. -- Makes and Handles the draw function for less lag in the visual
  520. function refreshSingleReactor()
  521.     local rfPerTick = 0
  522.     local rfTotal = 0
  523.     local reactor = reactors[1]
  524.  
  525.     local allStats = getAllStats()
  526.     rfTotal = allStats["rfTotal"]
  527.     rfPerTick = allStats["rfPerTick"]
  528.     rodLevel = allStats["rodLevel"]
  529.     fuelPerTick = allStats["fuelPerTick"]
  530.  
  531.     local i = 0
  532.     local infotoAdd = 'RF PER TICK : '
  533.  
  534.     if currentRfTick ~= rfPerTick then
  535.         currentRfTick = rfPerTick
  536.         if rfPerTick > rfPerTickMax then
  537.             rfPerTickMax = rfPerTick
  538.         end
  539.  
  540.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  541.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  542.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  543.  
  544.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  545.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  546.  
  547.     end
  548.  
  549.     i = 1
  550.     infotoAdd = 'ENERGY STORED : '
  551.     if currentRfTotal ~= rfTotal then
  552.         currentRfTotal = rfTotal
  553.  
  554.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  555.  
  556.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  557.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  558.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  559.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  560.     end
  561.  
  562.     i = 2
  563.     infotoAdd = 'CONTROL ROD LEVEL : '
  564.     if currentRodLevel ~= rodLevel then
  565.         currentRodLevel = rodLevel
  566.  
  567.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  568.  
  569.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  570.         table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  571.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  572.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  573.     end
  574.  
  575.     i = 3
  576.     infotoAdd = 'FUEL USAGE : '
  577.     if currentFuelConsumedLastTick ~= fuelPerTick then
  578.         currentFuelConsumedLastTick = fuelPerTick
  579.  
  580.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  581.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  582.     end
  583.  
  584.     mon.setTextColor(colors.white)
  585.     adjustRodsLevel()
  586.  
  587.     draw()
  588.  
  589.     sleep(2)
  590. end
  591.  
  592. --
  593. -- ** Get the informations from the index file
  594. -- line 1 = min ROD
  595. -- line 2 = max ROD
  596. --
  597.  
  598. function getInfoFromFile()
  599.  
  600.      if (fs.exists(index..".txt") == false) then
  601.         file = io.open(index..".txt","w")
  602.         file:write("0")
  603.         file:write("\n")
  604.         file:write("100")
  605.         file:close()
  606.     else
  607.         file = fs.open(index..".txt","r")
  608.         minPowerRod = tonumber(file.readLine())
  609.         maxPowerRod = tonumber(file.readLine())
  610.         file.close()
  611.     end
  612. end
  613.  
  614. -- Save informations to the index file
  615.  
  616. function setInfoToFile()
  617.     file = io.open(index..".txt","w")
  618.     file:write(minPowerRod .. "\n" .. maxPowerRod)
  619.     file:flush()
  620.     file:close()
  621. end
  622.  
  623. ---============================================================
  624. -- add comma to separate thousands
  625. -- From Lua-users.org/wiki/FormattingNumbers
  626. --
  627. --
  628. function comma_value(amount)
  629.   local formatted = amount
  630.   while true do
  631.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  632.     if (k==0) then
  633.       break
  634.     end
  635.   end
  636.   return formatted
  637. end
  638.  
  639. ---============================================================
  640. -- rounds a number to the nearest decimal places
  641. -- From Lua-users.org/wiki/FormattingNumbers
  642. --
  643. --
  644. function round(val, decimal)
  645.   if (decimal) then
  646.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  647.   else
  648.     return math.floor(val+0.5)
  649.   end
  650. end
  651.  
  652. --===================================================================
  653. -- given a numeric value formats output with comma to separate thousands
  654. -- and rounded to given decimal places
  655. -- From Lua-users.org/wiki/FormattingNumbers
  656. --
  657. function format_num(amount, decimal, prefix, neg_prefix)
  658.   local str_amount,  formatted, famount, remain
  659.  
  660.   decimal = decimal or 2  -- default 2 decimal places
  661.   neg_prefix = neg_prefix or "-" -- default negative sign
  662.  
  663.   famount = math.abs(round(amount,decimal))
  664.   famount = math.floor(famount)
  665.  
  666.   remain = round(math.abs(amount) - famount, decimal)
  667.  
  668.         -- comma to separate the thousands
  669.   formatted = comma_value(famount)
  670.  
  671.         -- attach the decimal portion
  672.   if (decimal > 0) then
  673.     remain = string.sub(tostring(remain),3)
  674.     formatted = formatted .. "." .. remain ..
  675.                 string.rep("0", decimal - string.len(remain))
  676.   end
  677.  
  678.         -- attach prefix string e.g '$'
  679.   formatted = (prefix or "") .. formatted
  680.  
  681.         -- if value is negative then format accordingly
  682.   if (amount<0) then
  683.     if (neg_prefix=="()") then
  684.       formatted = "("..formatted ..")"
  685.     else
  686.       formatted = neg_prefix .. formatted
  687.     end
  688.   end
  689.  
  690.   return formatted
  691. end
  692.  
  693. -- Clear and make the pixel smaller because we are not blind
  694.  
  695. mon.setBackgroundColor(colors.black)
  696. mon.clear()
  697. mon.setTextScale(0.5)
  698.  
  699. -- Get the information from the index file
  700. getInfoFromFile()
  701.  
  702.  
  703. -- Add's the visual and starts the Loop
  704. addDrawBoxesSingleReactor()
Add Comment
Please, Sign In to add comment