Advertisement
Aguia_016

turbina2

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