Guest User

Untitled

a guest
Mar 26th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.70 KB | None | 0 0
  1. if teamStats == nil then teamStats = {} end
  2. teamStats.author = "Nighthawk"
  3. teamStats.version = "0.2"
  4. function teamStats.initArray(table,index) local arr = table if type(table) == "table" then arr = {} end for i=1,index do arr[i] = 0 end return arr end
  5.  
  6. --[[
  7.     Configuration
  8. ]]
  9. teamStats.config = {
  10.     --[[
  11.         You can turn the visual bar on/off here.
  12.             render = true -- it will show
  13.             render = false -- it won't show
  14.         This will NOT turn it off!
  15.     ]]
  16.     render = true,
  17.     --[[
  18.         showTime = x
  19.         In Seconds. The visual bar will disappear after x seconds.
  20.         If you don't want it to disappear and to stay for the rest of the round then set x as 0
  21.             showTime = 0
  22.     ]]
  23.     showTime = 30, -- seconds
  24.     --[[
  25.         freeTime = x
  26.         In Seconds. The visual bar will be freed up as will the variables included with the images after x seconds AFTER showTime. This helps prevent lag
  27.         So if the showTime of the visual bar is 30 seconds and the freeTime is 3 seconds then the visual bar will be free its memory after 33 seconds.
  28.         This is useful is you extend the fade out timings of the HUD elements. If your background is set to fade out after 5 seconds then you will have to increase 'freeTime' to 5 or 6 seconds so the HUD doesn't free up before the transition finishes.
  29.         If you don't know what this means then don't touch it
  30.     ]]
  31.     freeTime = 3, -- seconds
  32.     --[[
  33.         roundLimit = x
  34.         The team win stats will reset count after x rounds. If you don't want it to reset then set it as 0;
  35.             roundLimit = 0
  36.     ]]
  37.     roundLimit = 3, --rounds
  38.     --[[
  39.         The positions are completely customizable.
  40.     ]]
  41.     UI = {
  42.         bg = {
  43.             src = "gfx/teamwinhud/bg.png",
  44.             x = 330,
  45.             y = 75,
  46.             alpha = 0.8, -- 0.0 - 1.0
  47.         },
  48.         shade = {
  49.             show = true,
  50.             src = "gfx/teamwinhud/shade.png",
  51.             x = 330,
  52.             y = 75,
  53.             alpha = 0.8, -- 0.0 - 1.0
  54.         },
  55.         ct_bar = {
  56.             src = "gfx/teamwinhud/ct.png",
  57.             alpha = 0.9, -- 0.0 - 1.0
  58.             --[[
  59.                 Do not tweak this unless you know what you're doing
  60.                 start_x is where the FIRST rendered block lies
  61.             ]]
  62.             start_x = 130,
  63.             y = 75,
  64.             --[[
  65.                 Do not tweak this unless you know what you're doing
  66.                 The width here is the image's width of 1 block in pixels.  (i.e; ct.png)
  67.             ]]
  68.             width = 4,
  69.         },
  70.         tt_bar = {
  71.             src = "gfx/teamwinhud/tt.png",
  72.             alpha = 0.9, -- 0.0 - 1.0
  73.             --[[
  74.                 Do not tweak this unless you know what you're doing
  75.                 start_x is where the FIRST rendered block lies
  76.             ]]
  77.             start_x = 525,
  78.             y = 75,
  79.             --[[
  80.                 Do not tweak this unless you know what you're doing
  81.                 The width here is the image's width of 1 block in pixels.  (i.e; tt.png)
  82.             ]]
  83.             width = 4,
  84.         },
  85.         ct_hudtxt = {
  86.             show = true,
  87.             id = 44, -- hudtxt ID
  88.             text = "CT",
  89.             color = "\169230230230", -- \169rrggbb
  90.             x = 130,
  91.             y = 62,
  92.             alpha = 1.0,
  93.         },
  94.         tt_hudtxt = {
  95.             show = true,
  96.             id = 45, -- hudtxt ID
  97.             text = "TT",
  98.             color = "\169230230230", -- rrggbb
  99.             x = 435,
  100.             y = 62,
  101.             alpha = 1.0,
  102.         },
  103.         percentage_hudtxt = {
  104.             show = true,
  105.             color = "\169255255255", -- rrggbb
  106.         },
  107.     },
  108.     --[[
  109.         Choose whether you want a nice fade-in/out transition.
  110.     ]]
  111.     animate = true,
  112.     --[[
  113.         Animation Settings
  114.     ]]
  115.     animation = {
  116.         bg = {
  117.             fadeIn = true,
  118.             fadeOut = true,
  119.             fadeInTime = 1000, -- milliseconds
  120.             fadeOutTime = 1000, -- milliseconds
  121.         },
  122.         shade = {
  123.             fadeIn = true,
  124.             fadeOut = true,
  125.             fadeInTime = 1500, -- milliseconds
  126.             fadeOutTime = 1500, -- milliseconds
  127.         },
  128.         hudtxt = {
  129.             fadeOut = true,
  130.             fadeOutTime = 1500, -- milliseconds
  131.             slideIn = true,
  132.             slideOut = false,
  133.             slideInTime = 1000, -- milliseconds
  134.             slideOutTime = 1000, -- milliseconds
  135.         },
  136.         bar = {
  137.             fadeIn = true,
  138.             fadeOut = true,
  139.             slideOut = true,
  140.             --[[
  141.                 !!!WARNING!!!
  142.                 Don't touch these if you don't know what you're doing.
  143.             ]]
  144.             fadeOutTime = 500, -- milliseconds
  145.             fadeInTime = 5000, -- milliseconds
  146.             --[[
  147.                 1 block is 1 block image that's equal to 1% of what completes the bar in the HUD, so in the render process we'll be negating the tween_alpha time taken
  148.                 i.e fadeInTime - fadeInTimeDecrementPerBlock
  149.                 A total of 100 bars are called in the process. Each bar will have it's own tween_alpha on the transition process so the effect would be more like the starting blocks will take longer to fade in then the ones further in. Damn I suck at explaining so there you go, top notch explanation.
  150.                 Imagine this as a bar, the numbers inside are the tween_alpha time taken
  151.                 [] = 1 block (there are a total of 100 blocks)
  152.                 [5000][5900][5800][5700][5600][5500], [fadeInTime-fadeInTimeDecrementPerBlock], ...
  153.  
  154.                 Tweak these settings at your own discretion
  155.             ]]
  156.             fadeInTimeDecrementPerBlock = 100, -- milliseconds
  157.             fadeOutTimeDecrementPerBlock = 200, -- milliseconds
  158.             slideOutTimeDecrementPerBlock = 250, -- milliseconds
  159.         },
  160.     },
  161. }
  162. --[[
  163.     Constants/CS2D Variables
  164. ]]
  165. teamStats.const = {
  166.     gameModes = {
  167.         ct = {30, 2, 11, 21, 22, 31, 41, 61}, -- cts win these modes
  168.         tt = {1, 10, 12, 20, 40, 50, 60}, -- tts win these modes
  169.     },
  170. }
  171.  
  172. --[[
  173.     Initialization
  174. ]]
  175. teamStats.UI = {
  176.     showing = false,
  177.     bg = nil,
  178.     shade = nil,
  179.     ct = {},
  180.     tt = {},
  181. }
  182. teamStats.UI.ct = teamStats.initArray(teamStats.UI.ct, 100)
  183. teamStats.UI.tt = teamStats.initArray(teamStats.UI.tt, 100)
  184.  
  185. teamStats.vars = {
  186.     tt = 0,
  187.     ct = 0,
  188.     ct_percentile = 0,
  189.     tt_percentile = 0,
  190.     round = 0,
  191.     showTimeCount = 0,
  192.     resetCall = false,
  193. }
  194.  
  195.  
  196. --[[
  197.     Hook Calls
  198. ]]
  199. addhook("startround", "teamStats.render")
  200. addhook("endround", "teamStats.roundend")
  201. addhook("second", "teamStats.second")
  202.  
  203. --[[
  204.     Hook Functions
  205. ]]
  206. function teamStats.second()
  207.     if teamStats.config.showTime > 0 then
  208.         if teamStats.UI.showing then
  209.             teamStats.vars.showTimeCount = teamStats.vars.showTimeCount + 1
  210.             if teamStats.vars.showTimeCount > teamStats.config.showTime then
  211.                 if teamStats.config.animation.bg.fadeOut then
  212.                     teamStats.animate(teamStats.UI.bg, "fadeOut", teamStats.config.animation.bg.fadeOutTime, 0)
  213.                 end
  214.                 if teamStats.config.animation.shade.fadeOut then
  215.                     teamStats.animate(teamStats.UI.shade, "fadeOut", teamStats.config.animation.shade.fadeOutTime, 0)
  216.                 end
  217.                 for i=1,teamStats.vars.ct_percentile do
  218.                     if teamStats.config.animation.bar.fadeOut then
  219.                         teamStats.animate(teamStats.UI.ct[i], "fadeOut", teamStats.config.animation.bar.fadeOutTime, 0)
  220.                     end
  221.                     if teamStats.config.animation.bar.slideOut then
  222.                         teamStats.animate(teamStats.UI.ct[i], "slideOut", teamStats.config.animation.bar.slideOutTimeDecrementPerBlock + (i*10), 0, {teamStats.config.UI.ct_bar.start_x, teamStats.config.UI.ct_bar.y})
  223.                     end
  224.                 end
  225.                 for i=1,teamStats.vars.tt_percentile do
  226.                     if teamStats.config.animation.bar.fadeOut then
  227.                         teamStats.animate(teamStats.UI.tt[i], "fadeOut", teamStats.config.animation.bar.fadeOutTime, 0)
  228.                     end
  229.                     if teamStats.config.animation.bar.slideOut then
  230.                         teamStats.animate(teamStats.UI.tt[i], "slideOut", teamStats.config.animation.bar.slideOutTimeDecrementPerBlock + (i*10), 0, {teamStats.config.UI.tt_bar.start_x, teamStats.config.UI.tt_bar.y})
  231.                     end
  232.                 end
  233.                 if teamStats.config.animate then
  234.                     if teamStats.config.animation.hudtxt.slideOut then
  235.                         parse('hudtxtmove 0 '..teamStats.config.UI.ct_hudtxt.id..' '..teamStats.config.animation.hudtxt.slideOutTime..' -100 '..teamStats.config.UI.ct_hudtxt.y)
  236.                         parse('hudtxtmove 0 '..teamStats.config.UI.tt_hudtxt.id..' '..teamStats.config.animation.hudtxt.slideOutTime..' 600 '..teamStats.config.UI.tt_hudtxt.y)
  237.                     end
  238.                     if teamStats.config.animation.hudtxt.fadeOut then
  239.                         parse('hudtxtalphafade 0 '..teamStats.config.UI.ct_hudtxt.id..' '..teamStats.config.animation.hudtxt.fadeOutTime..' 0.0')
  240.                         parse('hudtxtalphafade 0 '..teamStats.config.UI.tt_hudtxt.id..' '..teamStats.config.animation.hudtxt.fadeOutTime..' 0.0')
  241.                     end
  242.                 end
  243.             end
  244.             if teamStats.vars.showTimeCount > teamStats.config.showTime + teamStats.config.freeTime then
  245.                 teamStats.free()
  246.             end
  247.         end
  248.     end
  249. end
  250.  
  251. function teamStats.render()
  252.     if teamStats.vars.resetCall then
  253.         teamStats.reset()
  254.         teamStats.free()
  255.         teamStats.vars.resetCall = false
  256.     end
  257.     if teamStats.config.render then
  258.         if teamStats.vars.ct > 0 or teamStats.vars.tt > 0 then
  259.             teamStats.UI.showing = true
  260.             local ct_offset = 0
  261.             local tt_offset = 0
  262.             local ct_hudtxt = teamStats.config.UI.ct_hudtxt.text
  263.             local tt_hudtxt = teamStats.config.UI.tt_hudtxt.text
  264.             if teamStats.config.UI.percentage_hudtxt.show then
  265.                 ct_hudtxt = ct_hudtxt..': '..teamStats.config.UI.percentage_hudtxt.color..teamStats.vars.ct_percentile..' points'..teamStats.config.UI.ct_hudtxt.color..''
  266.                 tt_hudtxt = tt_hudtxt..': '..teamStats.config.UI.percentage_hudtxt.color..teamStats.vars.tt_percentile..' points'..teamStats.config.UI.tt_hudtxt.color..''
  267.             end
  268.             if teamStats.config.UI.ct_hudtxt.show then
  269.                 if teamStats.config.animate then
  270.                     if teamStats.config.animation.hudtxt.slideIn then
  271.                         teamStats.hudtxt(teamStats.config.UI.ct_hudtxt.id, ct_hudtxt, teamStats.config.UI.ct_hudtxt.color, (teamStats.config.UI.ct_hudtxt.x-50), teamStats.config.UI.ct_hudtxt.y, 0)
  272.                         parse('hudtxtmove 0 '..teamStats.config.UI.ct_hudtxt.id..' '..teamStats.config.animation.hudtxt.slideInTime..' '..teamStats.config.UI.ct_hudtxt.x..' '..teamStats.config.UI.ct_hudtxt.y)
  273.                     end
  274.                 else
  275.                     teamStats.hudtxt(teamStats.config.UI.ct_hudtxt.id, ct_hudtxt, teamStats.config.UI.ct_hudtxt.color, teamStats.config.UI.ct_hudtxt.x, teamStats.config.UI.ct_hudtxt.y, 0)
  276.                 end
  277.             end
  278.             if teamStats.config.UI.tt_hudtxt.show then
  279.                 if teamStats.config.animate then
  280.                     if teamStats.config.animation.hudtxt.slideIn then
  281.                         teamStats.hudtxt(teamStats.config.UI.tt_hudtxt.id, tt_hudtxt, teamStats.config.UI.tt_hudtxt.color, (teamStats.config.UI.tt_hudtxt.x+50), teamStats.config.UI.tt_hudtxt.y, 0)
  282.                         parse('hudtxtmove 0 '..teamStats.config.UI.tt_hudtxt.id..' '..teamStats.config.animation.hudtxt.slideInTime..' '..teamStats.config.UI.tt_hudtxt.x..' '..teamStats.config.UI.tt_hudtxt.y)
  283.                     end
  284.                 else
  285.                     teamStats.hudtxt(teamStats.config.UI.tt_hudtxt.id, tt_hudtxt, teamStats.config.UI.tt_hudtxt.color, teamStats.config.UI.tt_hudtxt.x, teamStats.config.UI.tt_hudtxt.y, 0)
  286.                 end
  287.             end
  288.             teamStats.UI.bg = image(teamStats.config.UI.bg.src, teamStats.config.UI.bg.x, teamStats.config.UI.bg.y, 2)
  289.             if teamStats.config.animation.bg.fadeIn then
  290.                 teamStats.animate(teamStats.UI.bg, "fadeIn", teamStats.config.animation.bg.fadeInTime, teamStats.config.UI.bg.alpha)
  291.             end
  292.             for i=1,teamStats.vars.ct_percentile do
  293.                 teamStats.UI.ct[i] = image(teamStats.config.UI.ct_bar.src, teamStats.config.UI.ct_bar.start_x+ct_offset, teamStats.config.UI.ct_bar.y, 2)
  294.                 imagealpha(teamStats.UI.ct[i], teamStats.config.UI.ct_bar.alpha)
  295.                 ct_offset = ct_offset + teamStats.config.UI.ct_bar.width
  296.                 if teamStats.config.animation.bar.fadeIn then
  297.                     local fade_in_time = teamStats.config.animation.bar.fadeInTime - (teamStats.config.animation.bar.fadeInTimeDecrementPerBlock*i)
  298.                     teamStats.animate(teamStats.UI.ct[i], "fadeIn", fade_in_time, teamStats.config.UI.ct_bar.alpha)
  299.                 end
  300.             end
  301.             for i=1,teamStats.vars.tt_percentile do
  302.                 teamStats.UI.tt[i] = image(teamStats.config.UI.tt_bar.src, teamStats.config.UI.tt_bar.start_x-tt_offset, teamStats.config.UI.tt_bar.y, 2)
  303.                 imagealpha(teamStats.UI.tt[i], teamStats.config.UI.tt_bar.alpha)
  304.                 tt_offset = tt_offset + teamStats.config.UI.tt_bar.width
  305.                 if teamStats.config.animation.bar.fadeIn then
  306.                     local fade_in_time = teamStats.config.animation.bar.fadeInTime - (teamStats.config.animation.bar.fadeInTimeDecrementPerBlock*i)
  307.                     teamStats.animate(teamStats.UI.tt[i], "fadeIn", fade_in_time, teamStats.config.UI.tt_bar.alpha)
  308.                 end
  309.             end
  310.             if teamStats.config.UI.shade.show then
  311.                 teamStats.UI.shade = image(teamStats.config.UI.shade.src, teamStats.config.UI.shade.x, teamStats.config.UI.shade.y, 2)
  312.                 if teamStats.config.animation.shade.fadeIn then
  313.                     teamStats.animate(teamStats.UI.shade, "fadeIn", teamStats.config.animation.shade.fadeInTime, teamStats.config.UI.shade.alpha)
  314.                 end
  315.             end
  316.         end
  317.     end
  318. end
  319.  
  320. function teamStats.roundend(mode)
  321.     if teamStats.config.roundLimit > 0 then
  322.         teamStats.vars.round = teamStats.vars.round + 1
  323.         if teamStats.vars.round <= teamStats.config.roundLimit then
  324.             for _,gm in pairs(teamStats.const.gameModes.ct) do
  325.                 if gm == mode then
  326.                     teamStats.vars.ct = teamStats.vars.ct + 1
  327.                     break
  328.                 end
  329.             end
  330.             for _,gm in pairs(teamStats.const.gameModes.tt) do
  331.                 if gm == mode then
  332.                     teamStats.vars.tt = teamStats.vars.tt + 1
  333.                     break
  334.                 end
  335.             end
  336.         else teamStats.vars.resetCall = true end
  337.     else
  338.         for _,gm in pairs(teamStats.const.gameModes.ct) do
  339.             if gm == mode then
  340.                 teamStats.vars.ct = teamStats.vars.ct + 1
  341.                 break
  342.             end
  343.         end
  344.         for _,gm in pairs(teamStats.const.gameModes.tt) do
  345.             if gm == mode then
  346.                 teamStats.vars.tt = teamStats.vars.tt + 1
  347.                 break
  348.             end
  349.         end
  350.     end
  351.     -- Round End Calls/Updates
  352.     teamStats.calculatePercentile()
  353.     teamStats.vars.showTimeCount = 0
  354.     teamStats.UI.showing = false
  355. end
  356.  
  357. --[[
  358.     Module Functions
  359. ]]
  360. function teamStats.reset()
  361.     teamStats.vars.ct = 0
  362.     teamStats.vars.tt = 0
  363.     teamStats.vars.ct_percentile = 0
  364.     teamStats.vars.tt_percentile = 0
  365.     teamStats.vars.round = 0
  366. end
  367. function teamStats.calculatePercentile()
  368.     local total = teamStats.vars.ct + teamStats.vars.tt
  369.     if total ~= 0 then
  370.         teamStats.vars.ct_percentile = math.floor(((teamStats.vars.ct/total)*100)+0.5)
  371.         teamStats.vars.tt_percentile = math.floor(((teamStats.vars.tt/total)*100)+0.5)
  372.     end
  373. end
  374. function teamStats.free()
  375.     if teamStats.UI.bg ~= nil then
  376.         freeimage(teamStats.UI.bg)
  377.         teamStats.UI.bg = nil
  378.     end
  379.     if teamStats.UI.shade ~= nil then
  380.         freeimage(teamStats.UI.shade)
  381.         teamStats.UI.shade = nil
  382.     end
  383.     for i=1,teamStats.vars.ct_percentile do
  384.         if teamStats.UI.ct[i] ~= nil then
  385.             freeimage(teamStats.UI.ct[i])
  386.             teamStats.UI.ct[i] = nil
  387.         end
  388.     end
  389.     for i=1,teamStats.vars.tt_percentile do
  390.         if teamStats.UI.tt[i] ~= nil then
  391.             freeimage(teamStats.UI.tt[i])
  392.             teamStats.UI.tt[i] = nil
  393.         end
  394.     end
  395.     parse('hudtxtalphafade 0 '..teamStats.config.UI.ct_hudtxt.id..' 0 0.0')
  396.     parse('hudtxtalphafade 0 '..teamStats.config.UI.tt_hudtxt.id..' 0 0.0')
  397.     teamStats.UI.showing = false
  398. end
  399. function teamStats.animate(image_id, animation, time, alpha, ...)
  400.     if teamStats.config.animate then
  401.         if time < 0 then time = 100 end
  402.         if animation == "fadeIn" then
  403.             imagealpha(image_id, 0)
  404.             tween_alpha(image_id, time, alpha)
  405.         elseif animation == "fadeOut" then
  406.             tween_alpha(image_id, time, alpha)
  407.         elseif animation == "slideOut" then
  408.             tween_move(image_id, time, unpack(...))
  409.         end
  410.     end
  411. end
  412. function teamStats.hudtxt(id,msg,color,x,y,align)
  413.     parse('hudtxt '..id..' "'..color..''..msg..' " '..x..' '..y..' '..align)
  414. end
Add Comment
Please, Sign In to add comment