Advertisement
SG_bun

Modified Kraken's Reactor Control

Oct 2nd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.60 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.  
  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 minPower = 0
  46. local maxPower = 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 = minPower + number
  221.         if tempLevel <= 0 then
  222.             minPower = 0
  223.         end
  224.  
  225.         if tempLevel >= maxPower then
  226.             minPower = maxPower -10
  227.         end
  228.  
  229.         if tempLevel < maxPower and tempLevel > 0 then
  230.             minPower = tempLevel
  231.         end
  232.     else
  233.         tempLevel = maxPower + number
  234.         if tempLevel <= minPower then
  235.             maxPower = minPower +10
  236.         end
  237.  
  238.         if tempLevel >= 100 then
  239.             maxPower = 100
  240.         end
  241.  
  242.         if tempLevel > minPower and tempLevel < 100 then
  243.             maxPower = 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, minPower .. '%', 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, maxPower .. '%', 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 = maxPower - minPower
  265.  
  266.     maxPower = (rfTotalMax/100) * maxPower
  267.     minPower = (rfTotalMax/100) * minPower
  268.  
  269.     if currentRfTotal >= maxPower then
  270.         reactor.setActive(false)
  271.     end
  272.  
  273.     if currentRfTotal <= minPower then
  274.         reactor.setActive(true)
  275.     end
  276.    
  277.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  278.     local rodLevel = math.floor((currentRfTotal/rfInBetween)*100)
  279.  
  280. end
  281.  
  282. -- Creates the frame and the basic of the visual
  283. -- Also adds the variables informations for placement of stuff and things
  284.  
  285. function addDrawBoxesSingleReactor()
  286.     local w, h = mon.getSize()
  287.     local margin = math.floor((w/100)*2)
  288.  
  289.     infosSize['startX'] = margin + 1
  290.     infosSize['startY'] =  margin + 1
  291.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  292.     infosSize['endY'] = h - margin
  293.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  294.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  295.     infosSize['inX'] = infosSize['startX'] + margin +1
  296.     infosSize['inY'] = infosSize['startY'] + margin +1
  297.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  298.  
  299.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  300.     local name = "INFOS"
  301.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  302.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  303.  
  304.     local names = {}
  305.     names[1] = 'ENERGY LAST TICK'
  306.     names[2] = 'ENERGY STORED'
  307.     names[3] = 'CONTROL ROD LEVEL'
  308.  
  309.     for i=0,2,1 do
  310.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  311.         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})
  312.     end
  313.  
  314.  
  315.     -- Controls
  316.  
  317.     contorlsSize['startX'] = infosSize['endX'] + margin + 1
  318.     contorlsSize['startY'] =  margin + 1
  319.     contorlsSize['endX'] = w-margin
  320.     contorlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  321.     contorlsSize['height'] = contorlsSize['endY']-contorlsSize['startY']-(margin)-1
  322.     contorlsSize['width'] = contorlsSize['endX']-contorlsSize['startX']-(margin*2)-2
  323.     contorlsSize['inX'] = contorlsSize['startX'] + margin +1
  324.     contorlsSize['inY'] = contorlsSize['startY'] + margin
  325.  
  326.     table.insert(boxes, {contorlsSize['startX'] , contorlsSize['startY'], contorlsSize['endX'], contorlsSize['endY'], colors.gray})
  327.     name = "CONTROLS"
  328.     table.insert(lines, {contorlsSize['startX'] + margin , contorlsSize['startY'], contorlsSize['startX'] + (margin*2) + #name+1, contorlsSize['startY'], colors.black})
  329.     table.insert(texts, {contorlsSize['startX'] + (margin*2), contorlsSize['startY'], name, colors.white, colors.black})
  330.    
  331.     contorlsSize['sectionHeight'] = math.floor(contorlsSize['height']/4)
  332.  
  333.     reactor = reactors[1]
  334.  
  335.     mon.setTextColor(colors.white)
  336.     setButton("ON","ON", powerUp, contorlsSize['inX'], contorlsSize['inY'], contorlsSize['inX'] + math.floor(contorlsSize['width']/2) -1, contorlsSize['inY'] +2, 0, 0, colors.green)
  337.     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)
  338.  
  339.     table.insert(texts, {contorlsSize['inX']+8, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  340.  
  341.     table.insert(texts, {contorlsSize['inX']+5, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  342.     table.insert(texts, {contorlsSize['inX']+5, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, minPower..'%', colors.lightBlue, colors.black})
  343.  
  344.     table.insert(texts, {contorlsSize['inX']+13, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  345.     table.insert(texts, {contorlsSize['inX']+20, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  346.     table.insert(texts, {contorlsSize['inX']+20, contorlsSize['inY'] +(contorlsSize['sectionHeight']*1)+4, maxPower..'%', colors.purple, colors.black})
  347.     mon.setTextColor(colors.white)
  348.  
  349.     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)
  350.     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)   
  351.  
  352.     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)
  353.     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)
  354.  
  355.     -- Numbers
  356.  
  357.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  358.     numbersSize['startY'] = contorlsSize['endY'] + margin + 1
  359.     numbersSize['endX'] = w-margin
  360.     numbersSize['endY'] = h-margin
  361.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  362.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  363.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  364.     numbersSize['inY'] = numbersSize['startY'] + margin
  365.  
  366.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  367.     name = "NUMBERS"
  368.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  369.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  370.  
  371.     refresh = true
  372.     while refresh do
  373.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  374.     end
  375. end
  376.  
  377. -- Makes and Handles the draw function for less lag in the visual
  378.  
  379. function refreshSingleReactor()
  380.     local rfPerTick = 0
  381.     local rfTotal = 0
  382.     local rfTotalMax = 10000000
  383.     local reactor = reactors[1]
  384.  
  385.     rfTotal = reactor.getEnergyStored()
  386.     rfPerTick = math.floor(reactor.getEnergyProducedLastTick())
  387.     rodLevel = math.floor(reactor.getControlRodLevel(0))
  388.     fuelPerTick = reactor.getFuelConsumedLastTick();
  389.  
  390.     local i = 0
  391.     local infotoAdd = 'RF PER TICK : '
  392.  
  393.     if currentRfTick ~= rfPerTick then
  394.         currentRfTick = rfPerTick
  395.         if rfPerTick > rfPerTickMax then
  396.             rfPerTickMax = rfPerTick
  397.         end
  398.  
  399.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  400.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  401.         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})
  402.    
  403.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  404.         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})
  405.  
  406.     end
  407.  
  408.     i = 1
  409.     infotoAdd = 'ENERGY STORED : '
  410.     if currentRfTotal ~= rfTotal then
  411.         currentRfTotal = rfTotal
  412.  
  413.         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})
  414.    
  415.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  416.         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})
  417.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  418.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  419.     end
  420.  
  421.     i = 2
  422.     infotoAdd = 'CONTROL ROD LEVEL : '
  423.     if currentRodLevel ~= rodLevel then
  424.         currentRodLevel = rodLevel
  425.  
  426.         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})
  427.    
  428.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  429.         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})
  430.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  431.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  432.     end
  433.  
  434.     i = 3
  435.     infotoAdd = 'FUEL USAGE : '
  436.     if currentFuelConsumedLastTick ~= fuelPerTick then
  437.         currentFuelConsumedLastTick = fuelPerTick
  438.  
  439.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  440.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})  
  441.     end
  442.    
  443.     mon.setTextColor(colors.white)
  444.     adjustRodsLevel()
  445.    
  446.     draw()
  447.  
  448.     sleep(2)
  449. end
  450.  
  451. --
  452. -- ** Get the informations from the index file
  453. -- line 1 = min ROD
  454. -- line 2 = max ROD
  455. --
  456.  
  457. function getInfoFromFile()
  458.  
  459.      if (fs.exists(index..".txt") == false) then
  460.         file = io.open(index..".txt","w")
  461.         file:write("0")
  462.         file:write("\n")
  463.         file:write("100")
  464.         file:close()
  465.     else
  466.         file = fs.open(index..".txt","r")
  467.         minPower = tonumber(file.readLine())
  468.         maxPower = tonumber(file.readLine())
  469.         file.close()
  470.     end
  471. end
  472.  
  473. -- Save informations to the index file
  474.  
  475. function setInfoToFile()
  476.     file = io.open(index..".txt","w")
  477.     file:write(minPower .. "\n" .. maxPower)
  478.     file:flush()
  479.     file:close()
  480. end
  481.  
  482. ---============================================================
  483. -- add comma to separate thousands
  484. -- From Lua-users.org/wiki/FormattingNumbers
  485. --
  486. --
  487. function comma_value(amount)
  488.   local formatted = amount
  489.   while true do  
  490.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  491.     if (k==0) then
  492.       break
  493.     end
  494.   end
  495.   return formatted
  496. end
  497.  
  498. ---============================================================
  499. -- rounds a number to the nearest decimal places
  500. -- From Lua-users.org/wiki/FormattingNumbers
  501. --
  502. --
  503. function round(val, decimal)
  504.   if (decimal) then
  505.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  506.   else
  507.     return math.floor(val+0.5)
  508.   end
  509. end
  510.  
  511. --===================================================================
  512. -- given a numeric value formats output with comma to separate thousands
  513. -- and rounded to given decimal places
  514. -- From Lua-users.org/wiki/FormattingNumbers
  515. --
  516. function format_num(amount, decimal, prefix, neg_prefix)
  517.   local str_amount,  formatted, famount, remain
  518.  
  519.   decimal = decimal or 2  -- default 2 decimal places
  520.   neg_prefix = neg_prefix or "-" -- default negative sign
  521.  
  522.   famount = math.abs(round(amount,decimal))
  523.   famount = math.floor(famount)
  524.  
  525.   remain = round(math.abs(amount) - famount, decimal)
  526.  
  527.         -- comma to separate the thousands
  528.   formatted = comma_value(famount)
  529.  
  530.         -- attach the decimal portion
  531.   if (decimal > 0) then
  532.     remain = string.sub(tostring(remain),3)
  533.     formatted = formatted .. "." .. remain ..
  534.                 string.rep("0", decimal - string.len(remain))
  535.   end
  536.  
  537.         -- attach prefix string e.g '$'
  538.   formatted = (prefix or "") .. formatted
  539.  
  540.         -- if value is negative then format accordingly
  541.   if (amount<0) then
  542.     if (neg_prefix=="()") then
  543.       formatted = "("..formatted ..")"
  544.     else
  545.       formatted = neg_prefix .. formatted
  546.     end
  547.   end
  548.  
  549.   return formatted
  550. end
  551.  
  552. -- Clear and make the pixel smaller because we are not blind
  553.  
  554. mon.setBackgroundColor(colors.black)
  555. mon.clear()
  556. mon.setTextScale(0.5)
  557.  
  558. -- Get the information from the index file
  559. getInfoFromFile()
  560.  
  561.  
  562. -- Add's the visual and starts the Loop
  563. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement