ItsMods

Full Version: Show K/D Ratio
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This plugin shows a player's K/D under the minimap. It also provides an example on how to use HudElementTypes.Value and how to show a HudElement to a single entity.

Example how it will look like:
[Image: A2A302E3CF2ABC3E08C48074B7DF738BDA8DB4D0]

If the K/D is < 1 the text will be red, else it's green (as in the screenshot).

Thanks to @"jaydi", @Cyanide and GT.Under for testing.

Source code:
CSHARP Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Addon;
  5.  
  6. namespace KDRatio
  7. {
  8. public class Class1 : CPlugin
  9. {
  10. //Store ClientNum and HudElementNum in dictionary
  11. Dictionary<int, int> Client_HudElem = new Dictionary<int, int>();
  12.  
  13. public override void OnServerLoad()
  14. {
  15. ServerPrint("\n K/D Ratio Plugin loaded \n Author: zxz0O0 \n Thanks to Nukem, Jariz and Pozzuh\n");
  16. ServerPrint(" youtube.com/zxz0O0 \n itsmods.com\n");
  17. }
  18.  
  19. public override void OnPlayerConnect(ServerClient Client)
  20. {
  21. int HudElemNum = CreateKDHud(Client.ClientNum);
  22. //Add client and it's kd hudelement to the list
  23. if (Client_HudElem.ContainsKey(Client.ClientNum))
  24. Client_HudElem[Client.ClientNum] = HudElemNum;
  25. else
  26. Client_HudElem.Add(Client.ClientNum, HudElemNum);
  27. }
  28.  
  29. public override void OnPlayerDisconnect(ServerClient Client)
  30. {
  31. if(Client_HudElem.ContainsKey(Client.ClientNum))
  32. {
  33. //Get client's k/d hudelement
  34. HudElem kd = GetHudElement(Client_HudElem[Client.ClientNum]);
  35. //Setting hudelement's type to None 'deletes' it AFAIK
  36. kd.Type = HudElementTypes.None;
  37. //Remove entry from Dictionary
  38. Client_HudElem.Remove(Client.ClientNum);
  39. }
  40. }
  41.  
  42. public override int OnPlayerDamaged(ServerClient Attacker, ServerClient Victim, string Weapon, int Damage)
  43. {
  44. //If kill
  45. if (Damage >= Victim.Other.Health)
  46. {
  47. //Update victim's value
  48. if (Client_HudElem.ContainsKey(Victim.ClientNum))
  49. {
  50. HudElem victim = GetHudElement(Client_HudElem[Victim.ClientNum]);
  51. //victim.Value = (float)Victim.Stats.Kills / (Victim.Stats.Deaths + 1);
  52. victim.Value = (float)Math.Round((double)Victim.Stats.Kills / (Victim.Stats.Deaths + 1), 2);
  53. if (victim.Value < 1)
  54. {
  55. //Make it red
  56. victim.Color.R = 255;
  57. victim.Color.G = 0;
  58. victim.Color.B = 0;
  59. }
  60. else
  61. {
  62. //Make green
  63. victim.Color.R = 0;
  64. victim.Color.G = 255;
  65. victim.Color.B = 0;
  66. }
  67. }
  68.  
  69. //Don't do it when victim is attacker
  70. if (Victim.ClientNum != Attacker.ClientNum)
  71. {
  72. //Update attacker's value
  73. if (Client_HudElem.ContainsKey(Attacker.ClientNum))
  74. {
  75. HudElem attacker = GetHudElement(Client_HudElem[Attacker.ClientNum]);
  76. //Don't divide by zero :O
  77. if (Attacker.Stats.Deaths == 0)
  78. attacker.Value = Attacker.Stats.Kills + 1;
  79. else
  80. attacker.Value = (float)(Math.Round((double)(Attacker.Stats.Kills + 1) / Attacker.Stats.Deaths, 2));
  81.  
  82. if (attacker.Value < 1)
  83. {
  84. //Make it red
  85. attacker.Color.R = 255;
  86. attacker.Color.G = 0;
  87. attacker.Color.B = 0;
  88. }
  89. else
  90. {
  91. //Make green
  92. attacker.Color.R = 0;
  93. attacker.Color.G = 255;
  94. attacker.Color.B = 0;
  95. }
  96. }
  97. }
  98. }
  99. return Damage;
  100. }
  101.  
  102. private int CreateKDHud(int ClientNum)
  103. {
  104. //Create a simply 'empty' HudElement
  105. HudElem hud = CreateNewHudElem();
  106. //Make the HudElement Value
  107. hud.Type = HudElementTypes.Value;
  108. //Show the HudElement only to the Client
  109. hud.ShowToEnt = ClientNum;
  110. //HudElement hides when player is in menu
  111. hud.HideInMenu = true;
  112. //Set font and fontscale, default is the best looking in my opinion
  113. hud.Font = HudElementFonts.Default;
  114. hud.FontScale = 1.6f;
  115. /* The origin is relative from this point. I did not find out yet how the point type works correctly
  116.   * Just try a bit yourself and find the perfect point
  117.   * You can also let this value 0 then the 'start point' will be right to the radar */
  118. hud.PointType = 81;
  119. //Set the relative origin
  120. hud.OriginY = 70f;
  121. hud.OriginX = 10f;
  122. //Set the label to the HudElement
  123. hud.SetLabel("K/D Ratio: ");
  124. hud.Value = 0f;
  125. return hud.HudElementNum;
  126. }
  127. }
  128. }
1.33 is n00bie Troll
Hey @zxz0O0 are u german?
(07-13-2012, 15:22)Tomsen1410 Wrote: [ -> ]Hey @zxz0O0 are u german?

Swedish who speaks german.
(07-13-2012, 17:21)surtek Wrote: [ -> ]
(07-13-2012, 15:22)Tomsen1410 Wrote: [ -> ]Hey @zxz0O0 are u german?

Swedish who speaks german.
Swiss
(07-13-2012, 15:19)Yamato Wrote: [ -> ]1.33 is n00bie Troll

It's better than the average, so no, it's not noobie.

Also back on topic, everyone.
Great little plugin there ZXZ0O0, I really enjoyed testing, even if my ping was 500 lol. Fantastic little tool.
Also note that if you use a string to display numbers and change the string too often your server will crash eventually, with this method (HudElementTypes.Value) it will not crash.

(GSC coders will know)
Also an idea to show actual skill: Points per minute.
Thanks nice plugin!
And thx for the Hud example Wink
Pages: 1 2