Advertisement
karelvysinka

Big Reactors Automatic Control Program

Oct 6th, 2016
194
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.  
  24. local monitors = {peripheral.find("monitor")}
  25. local mon = monitors[1]
  26. local reactors = {peripheral.find("BigReactors-Reactor")}
  27.  
  28. local button={}
  29. local filleds = {}
  30. local boxes = {}
  31. local lines = {}
  32. local texts = {}
  33.  
  34. local refresh = true
  35.  
  36. local infosSize = {}
  37. local contorlsSize = {}
  38. local numbersSize = {}
  39. local currentRfTotal = 1
  40. local currentRfTick = 1
  41. local currentFuelConsumedLastTick = 0.00001
  42.  
  43.  
  44. local rfPerTickMax = 1
  45. local minPowerRod = 0
  46. local maxPowerRod = 100
  47. local currentRodLevel = 1
  48.  
  49. -- you need to give the index to be able to use the program  
  50. -- ex : NameOfProgram Index   (reactor Krakaen)
  51.  
  52. local args = { ... }
  53. local index = ""
  54.  
  55. if (#args == 0) then
  56.     error("No index Given")
  57. end
  58.  
  59. if (#args == 1) then
  60.     index = args[1]
  61. end
  62.  
  63. -- use the black thingy sponge to clear the chalkboard
  64.  
  65. term.redirect(mon)
  66. mon.clear()
  67. mon.setTextColor(colors.white)
  68. mon.setBackgroundColor(colors.black)
  69.  
  70. function clearTable()
  71.     button = {}
  72. end
  73.  
  74. -- All the things that make my buttons work
  75.  
  76. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  77.     button[name] = {}
  78.     button[name]["title"] = title
  79.     button[name]["func"] = func
  80.     button[name]["active"] = false
  81.     button[name]["xmin"] = xmin
  82.     button[name]["ymin"] = ymin
  83.     button[name]["xmax"] = xmax
  84.     button[name]["ymax"] = ymax
  85.     button[name]["color"] = color
  86.     button[name]["elem"] = elem
  87.     button[name]["elem2"] = elem2
  88. end
  89.  
  90. -- stuff and things for buttons
  91.  
  92. function fill(text, color, bData)
  93.    mon.setBackgroundColor(color)
  94.    mon.setTextColor(colors.white)
  95.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  96.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  97.    for j = bData["ymin"], bData["ymax"] do
  98.       mon.setCursorPos(bData["xmin"], j)
  99.       if j == yspot then
  100.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  101.             if k == xspot then
  102.                mon.write(bData["title"])
  103.             else
  104.                mon.write(" ")
  105.             end
  106.          end
  107.       else
  108.          for i = bData["xmin"], bData["xmax"] do
  109.             mon.write(" ")
  110.          end
  111.       end
  112.    end
  113.    mon.setBackgroundColor(colors.black)
  114. end
  115.  
  116. -- stuff and things for buttons
  117.  
  118. function screen()
  119.    local currColor
  120.    for name,data in pairs(button) do
  121.       local on = data["active"]
  122.       currColor = data["color"]
  123.       fill(name, currColor, data)
  124.    end
  125. end
  126.  
  127. -- stuff and things for buttons
  128.  
  129. function flash(name)
  130.  
  131.     screen()
  132. end
  133.  
  134. -- magical handler for clicky clicks
  135.  
  136. function checkxy(x, y)
  137.    for name, data in pairs(button) do
  138.       if y>=data["ymin"] and  y <= data["ymax"] then
  139.          if x>=data["xmin"] and x<= data["xmax"] then
  140.             data["func"](data["elem"], data["elem2"])
  141.             flash(data['name'])
  142.             return true
  143.             --data["active"] = not data["active"]
  144.             --print(name)
  145.          end
  146.       end
  147.    end
  148.    return false
  149. end
  150.  
  151. -- Don't question my code, it works on my machine...
  152.  
  153. function label(w, h, text)
  154.    mon.setCursorPos(w, h)
  155.    mon.write(text)
  156. end
  157.  
  158. -- Draw function : put's all the beautiful magic in the screen
  159.  
  160. function draw()
  161.    
  162.     for key,value in pairs(filleds) do
  163.         paintutils.drawFilledBox(value[1] , value[2], value[3], value[4], value[5])
  164.     end
  165.  
  166.     for key,value in pairs(boxes) do
  167.         paintutils.drawBox(value[1] , value[2], value[3], value[4], value[5])
  168.     end
  169.  
  170.     for key,value in pairs(lines) do
  171.         paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  172.     end
  173.  
  174.     for key,value in pairs(texts) do
  175.         mon.setCursorPos(value[1], value[2])
  176.         mon.setTextColor(value[4])
  177.         mon.setBackgroundColor(value[5])
  178.         mon.write(value[3])
  179.     end
  180.     screen()
  181.     resetDraw()
  182. end
  183.  
  184. -- Resets the elements to draw to only draw the neccessity
  185.  
  186. function resetDraw()
  187.     filleds = {}
  188.     boxes = {}
  189.     lines = {}
  190.     texts = {}
  191. end
  192.  
  193. -- Handles all the clicks for the buttons
  194.  
  195. function clickEvent()
  196.     local myEvent={os.pullEvent("monitor_touch")}
  197.     checkxy(myEvent[3], myEvent[4])
  198. end
  199.  
  200. -- Power up the reactor (M&N are a good source of food right?)
  201.  
  202. function powerUp(m,n)
  203.     local reactor = reactors[1]
  204.     reactor.setActive(true)
  205. end
  206.  
  207. -- Power down the reactor (M&N are a good source of food right?)
  208.  
  209. function powerDown(m,n)
  210.     local reactor = reactors[1]
  211.     reactor.setActive(false)
  212. end
  213.  
  214. -- Handles and calculate the Min and Max of the power limitation
  215.  
  216. function modifyRods(limit, number)
  217.     local tempLevel = 0
  218.  
  219.     if limit == "min" then
  220.         tempLevel = minPowerRod + number
  221.         if tempLevel <= 0 then
  222.             minPowerRod = 0
  223.         end
  224.  
  225.         if tempLevel >= maxPowerRod then
  226.             minPowerRod = maxPowerRod -10
  227.         end
  228.  
  229.         if tempLevel < maxPowerRod and tempLevel > 0 then
  230.             minPowerRod = tempLevel
  231.         end
  232.     else
  233.         tempLevel = maxPowerRod + number
  234.         if tempLevel <= minPowerRod then
  235.             maxPowerRod = minPowerRod +10
  236.         end
  237.  
  238.         if tempLevel >= 100 then
  239.             maxPowerRod = 100
  240.         end
  241.  
  242.         if tempLevel > minPowerRod and tempLevel < 100 then
  243.             maxPowerRod = tempLevel
  244.         end
  245.     end
  246.  
  247.     table.insert(lines, {contorlsSize['inX'], contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, contorlsSize['inX'] + contorlsSize['width'], contorlsSize['inY']+(contorlsSize['sectionHeight']*1)+4, colors.black})
  248.    
  249.     table.insert(texts, {contorlsSize['inX']+5, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  250.     table.insert(texts, {contorlsSize['inX']+13, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  251.     table.insert(texts, {contorlsSize['inX']+20, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  252.  
  253.     setInfoToFile()
  254.     adjustRodsLevel()
  255. end
  256.  
  257. -- Calculate and adjusts the level of the rods
  258.  
  259. function adjustRodsLevel()
  260.     local reactor = reactors[1]
  261.     local rfTotalMax = 10000000
  262.     local currentRfTotal = reactor.getEnergyStored()
  263.  
  264.     differenceMinMax = maxPowerRod - minPowerRod
  265.  
  266.     maxPower = (rfTotalMax/100) * maxPowerRod
  267.     minPower = (rfTotalMax/100) * minPowerRod
  268.  
  269.     if currentRfTotal >= maxPower then
  270.         currentRfTotal = maxPower
  271.     end
  272.  
  273.     if currentRfTotal <= minPower then
  274.         currentRfTotal = minPower
  275.     end
  276.        
  277.     currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  278.    
  279.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  280.     local rodLevel = math.floor((currentRfTotal/rfInBetween)*100)
  281.  
  282.     reactor.setAllControlRodLevels(rodLevel)
  283. end
  284.  
  285. -- Creates the frame and the basic of the visual
  286. -- Also adds the variables informations for placement of stuff and things
  287.  
  288. function addDrawBoxesSingleReactor()
  289.     local w, h = mon.getSize()
  290.     local margin = math.floor((w/100)*2)
  291.  
  292.     infosSize['startX'] = margin + 1
  293.     infosSize['startY'] =  margin + 1
  294.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  295.     infosSize['endY'] = h - margin
  296.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  297.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  298.     infosSize['inX'] = infosSize['startX'] + margin +1
  299.     infosSize['inY'] = infosSize['startY'] + margin +1
  300.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  301.  
  302.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  303.     local name = "INFOS"
  304.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  305.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  306.  
  307.     local names = {}
  308.     names[1] = 'ENERGY LAST TICK'
  309.     names[2] = 'ENERGY STORED'
  310.     names[3] = 'CONTROL ROD LEVEL'
  311.  
  312.     for i=0,2,1 do
  313.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  314.         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})
  315.     end
  316.  
  317.  
  318.     -- Controls
  319.  
  320.     contorlsSize['startX'] = infosSize['endX'] + margin + 1
  321.     contorlsSize['startY'] =  margin + 1
  322.     contorlsSize['endX'] = w-margin
  323.     contorlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  324.     contorlsSize['height'] = contorlsSize['endY']-contorlsSize['startY']-(margin)-1
  325.     contorlsSize['width'] = contorlsSize['endX']-contorlsSize['startX']-(margin*2)-2
  326.     contorlsSize['inX'] = contorlsSize['startX'] + margin +1
  327.     contorlsSize['inY'] = contorlsSize['startY'] + margin
  328.  
  329.     table.insert(boxes, {contorlsSize['startX'] , contorlsSize['startY'], contorlsSize['endX'], contorlsSize['endY'], colors.gray})
  330.     name = "CONTROLS"
  331.     table.insert(lines, {contorlsSize['startX'] + margin , contorlsSize['startY'], contorlsSize['startX'] + (margin*2) + #name+1, contorlsSize['startY'], colors.black})
  332.     table.insert(texts, {contorlsSize['startX'] + (margin*2), contorlsSize['startY'], name, colors.white, colors.black})
  333.    
  334.     contorlsSize['sectionHeight'] = math.floor(contorlsSize['height']/4)
  335.  
  336.     reactor = reactors[1]
  337.  
  338.     mon.setTextColor(colors.white)
  339.     setButton("ON","ON", powerUp, contorlsSize['inX'], contorlsSize['inY'], contorlsSize['inX'] + math.floor(contorlsSize['width']/2) -1, contorlsSize['inY'] +2, 0, 0, colors.green)
  340.     setButton("OFF","OFF", powerDown, contorlsSize['inX'] + math.floor(contorlsSize['width']/2) +2, contorlsSize['inY'], contorlsSize['inX'] + contorlsSize['width'], contorlsSize['inY'] +2,0, 0, colors.red)
  341.  
  342.     table.insert(texts, {contorlsSize['inX']+8, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  343.  
  344.     table.insert(texts, {contorlsSize['inX']+5, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  345.     table.insert(texts, {contorlsSize['inX']+5, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  346.  
  347.     table.insert(texts, {contorlsSize['inX']+13, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  348.     table.insert(texts, {contorlsSize['inX']+20, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  349.     table.insert(texts, {contorlsSize['inX']+20, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  350.     mon.setTextColor(colors.white)
  351.  
  352.     setButton("low-10","-10", modifyRods, contorlsSize['inX'], contorlsSize['inY'] +(contorlsSize['sectionHeight']*2)+2, contorlsSize['inX'] + math.floor(contorlsSize['width']/2) -1, contorlsSize['inY'] +(contorlsSize['sectionHeight']*2)+4, "min", -10, colors.lightBlue)
  353.     setButton("high-10","-10", modifyRods, contorlsSize['inX'] + math.floor(contorlsSize['width']/2) +2, contorlsSize['inY'] +(contorlsSize['sectionHeight']*2)+2, contorlsSize['inX'] + contorlsSize['width'], contorlsSize['inY'] +(contorlsSize['sectionHeight']*2)+4, "max", -10, colors.purple)   
  354.  
  355.     setButton("low+10","+10", modifyRods, contorlsSize['inX'], contorlsSize['inY'] +(contorlsSize['sectionHeight']*3)+2, contorlsSize['inX'] + math.floor(contorlsSize['width']/2) -1, contorlsSize['inY'] +(contorlsSize['sectionHeight']*3)+4, "min", 10, colors.lightBlue)
  356.     setButton("high+10","+10", modifyRods, contorlsSize['inX'] + math.floor(contorlsSize['width']/2) +2, contorlsSize['inY'] +(contorlsSize['sectionHeight']*3)+2, contorlsSize['inX'] + contorlsSize['width'], contorlsSize['inY'] +(contorlsSize['sectionHeight']*3)+4, "max", 10, colors.purple)
  357.  
  358.     -- Numbers
  359.  
  360.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  361.     numbersSize['startY'] = contorlsSize['endY'] + margin + 1
  362.     numbersSize['endX'] = w-margin
  363.     numbersSize['endY'] = h-margin
  364.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  365.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  366.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  367.     numbersSize['inY'] = numbersSize['startY'] + margin
  368.  
  369.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  370.     name = "NUMBERS"
  371.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  372.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  373.  
  374.     refresh = true
  375.     while refresh do
  376.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  377.     end
  378. end
  379.  
  380. -- Makes and Handles the draw function for less lag in the visual
  381.  
  382. function refreshSingleReactor()
  383.     local rfPerTick = 0
  384.     local rfTotal = 0
  385.     local rfTotalMax = 10000000
  386.     local reactor = reactors[1]
  387.  
  388.     rfTotal = reactor.getEnergyStored()
  389.     rfPerTick = math.floor(reactor.getEnergyProducedLastTick())
  390.     rodLevel = math.floor(reactor.getControlRodLevel(0))
  391.     fuelPerTick = reactor.getFuelConsumedLastTick();
  392.  
  393.     local i = 0
  394.     local infotoAdd = 'RF PER TICK : '
  395.  
  396.     if currentRfTick ~= rfPerTick then
  397.         currentRfTick = rfPerTick
  398.         if rfPerTick > rfPerTickMax then
  399.             rfPerTickMax = rfPerTick
  400.         end
  401.  
  402.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  403.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  404.         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})
  405.    
  406.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  407.         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})
  408.  
  409.     end
  410.  
  411.     i = 1
  412.     infotoAdd = 'ENERGY STORED : '
  413.     if currentRfTotal ~= rfTotal then
  414.         currentRfTotal = rfTotal
  415.  
  416.         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})
  417.    
  418.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  419.         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})
  420.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  421.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  422.     end
  423.  
  424.     i = 2
  425.     infotoAdd = 'CONTROL ROD LEVEL : '
  426.     if currentRodLevel ~= rodLevel then
  427.         currentRodLevel = rodLevel
  428.  
  429.         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})
  430.    
  431.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  432.         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})
  433.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  434.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  435.     end
  436.  
  437.     i = 3
  438.     infotoAdd = 'FUEL USAGE : '
  439.     if currentFuelConsumedLastTick ~= fuelPerTick then
  440.         currentFuelConsumedLastTick = fuelPerTick
  441.  
  442.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  443.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})  
  444.     end
  445.    
  446.     mon.setTextColor(colors.white)
  447.     adjustRodsLevel()
  448.    
  449.     draw()
  450.  
  451.     sleep(2)
  452. end
  453.  
  454. --
  455. -- ** Get the informations from the index file
  456. -- line 1 = min ROD
  457. -- line 2 = max ROD
  458. --
  459.  
  460. function getInfoFromFile()
  461.  
  462.      if (fs.exists(index..".txt") == false) then
  463.         file = io.open(index..".txt","w")
  464.         file:write("0")
  465.         file:write("\n")
  466.         file:write("100")
  467.         file:close()
  468.     else
  469.         file = fs.open(index..".txt","r")
  470.         minPowerRod = tonumber(file.readLine())
  471.         maxPowerRod = tonumber(file.readLine())
  472.         file.close()
  473.     end
  474. end
  475.  
  476. -- Save informations to the index file
  477.  
  478. function setInfoToFile()
  479.     file = io.open(index..".txt","w")
  480.     file:write(minPowerRod .. "\n" .. maxPowerRod)
  481.     file:flush()
  482.     file:close()
  483. end
  484.  
  485. ---============================================================
  486. -- add comma to separate thousands
  487. -- From Lua-users.org/wiki/FormattingNumbers
  488. --
  489. --
  490. function comma_value(amount)
  491.   local formatted = amount
  492.   while true do  
  493.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  494.     if (k==0) then
  495.       break
  496.     end
  497.   end
  498.   return formatted
  499. end
  500.  
  501. ---============================================================
  502. -- rounds a number to the nearest decimal places
  503. -- From Lua-users.org/wiki/FormattingNumbers
  504. --
  505. --
  506. function round(val, decimal)
  507.   if (decimal) then
  508.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  509.   else
  510.     return math.floor(val+0.5)
  511.   end
  512. end
  513.  
  514. --===================================================================
  515. -- given a numeric value formats output with comma to separate thousands
  516. -- and rounded to given decimal places
  517. -- From Lua-users.org/wiki/FormattingNumbers
  518. --
  519. function format_num(amount, decimal, prefix, neg_prefix)
  520.   local str_amount,  formatted, famount, remain
  521.  
  522.   decimal = decimal or 2  -- default 2 decimal places
  523.   neg_prefix = neg_prefix or "-" -- default negative sign
  524.  
  525.   famount = math.abs(round(amount,decimal))
  526.   famount = math.floor(famount)
  527.  
  528.   remain = round(math.abs(amount) - famount, decimal)
  529.  
  530.         -- comma to separate the thousands
  531.   formatted = comma_value(famount)
  532.  
  533.         -- attach the decimal portion
  534.   if (decimal > 0) then
  535.     remain = string.sub(tostring(remain),3)
  536.     formatted = formatted .. "." .. remain ..
  537.                 string.rep("0", decimal - string.len(remain))
  538.   end
  539.  
  540.         -- attach prefix string e.g '$'
  541.   formatted = (prefix or "") .. formatted
  542.  
  543.         -- if value is negative then format accordingly
  544.   if (amount<0) then
  545.     if (neg_prefix=="()") then
  546.       formatted = "("..formatted ..")"
  547.     else
  548.       formatted = neg_prefix .. formatted
  549.     end
  550.   end
  551.  
  552.   return formatted
  553. end
  554.  
  555. -- Clear and make the pixel smaller because we are not blind
  556.  
  557. mon.setBackgroundColor(colors.black)
  558. mon.clear()
  559. mon.setTextScale(0.5)
  560.  
  561. -- Get the information from the index file
  562. getInfoFromFile()
  563.  
  564.  
  565. -- Add's the visual and starts the Loop
  566. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement