Advertisement
theweridguy

mypaste1234

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