toffe_mcmuffin

Untitled

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