Advertisement
costantino03

reactor3

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