07-13-2012, 15:08
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]](http://cloud-2.steampowered.com/ugc/578961482036032857/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:
Example how it will look like:
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:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Addon;
namespace KDRatio
{
public class Class1 : CPlugin
{
//Store ClientNum and HudElementNum in dictionary
Dictionary<int, int> Client_HudElem = new Dictionary<int, int>();
public override void OnServerLoad()
{
ServerPrint("\n K/D Ratio Plugin loaded \n Author: zxz0O0 \n Thanks to Nukem, Jariz and Pozzuh\n");
ServerPrint(" youtube.com/zxz0O0 \n itsmods.com\n");
}
public override void OnPlayerConnect(ServerClient Client)
{
int HudElemNum = CreateKDHud(Client.ClientNum);
//Add client and it's kd hudelement to the list
if (Client_HudElem.ContainsKey(Client.ClientNum))
Client_HudElem[Client.ClientNum] = HudElemNum;
else
Client_HudElem.Add(Client.ClientNum, HudElemNum);
}
public override void OnPlayerDisconnect(ServerClient Client)
{
if(Client_HudElem.ContainsKey(Client.ClientNum))
{
//Get client's k/d hudelement
HudElem kd = GetHudElement(Client_HudElem[Client.ClientNum]);
//Setting hudelement's type to None 'deletes' it AFAIK
kd.Type = HudElementTypes.None;
//Remove entry from Dictionary
Client_HudElem.Remove(Client.ClientNum);
}
}
public override int OnPlayerDamaged(ServerClient Attacker, ServerClient Victim, string Weapon, int Damage)
{
//If kill
if (Damage >= Victim.Other.Health)
{
//Update victim's value
if (Client_HudElem.ContainsKey(Victim.ClientNum))
{
HudElem victim = GetHudElement(Client_HudElem[Victim.ClientNum]);
//victim.Value = (float)Victim.Stats.Kills / (Victim.Stats.Deaths + 1);
victim.Value = (float)Math.Round((double)Victim.Stats.Kills / (Victim.Stats.Deaths + 1), 2);
if (victim.Value < 1)
{
//Make it red
victim.Color.R = 255;
victim.Color.G = 0;
victim.Color.B = 0;
}
else
{
//Make green
victim.Color.R = 0;
victim.Color.G = 255;
victim.Color.B = 0;
}
}
//Don't do it when victim is attacker
if (Victim.ClientNum != Attacker.ClientNum)
{
//Update attacker's value
if (Client_HudElem.ContainsKey(Attacker.ClientNum))
{
HudElem attacker = GetHudElement(Client_HudElem[Attacker.ClientNum]);
//Don't divide by zero :O
if (Attacker.Stats.Deaths == 0)
attacker.Value = Attacker.Stats.Kills + 1;
else
attacker.Value = (float)(Math.Round((double)(Attacker.Stats.Kills + 1) / Attacker.Stats.Deaths, 2));
if (attacker.Value < 1)
{
//Make it red
attacker.Color.R = 255;
attacker.Color.G = 0;
attacker.Color.B = 0;
}
else
{
//Make green
attacker.Color.R = 0;
attacker.Color.G = 255;
attacker.Color.B = 0;
}
}
}
}
return Damage;
}
private int CreateKDHud(int ClientNum)
{
//Create a simply 'empty' HudElement
HudElem hud = CreateNewHudElem();
//Make the HudElement Value
hud.Type = HudElementTypes.Value;
//Show the HudElement only to the Client
hud.ShowToEnt = ClientNum;
//HudElement hides when player is in menu
hud.HideInMenu = true;
//Set font and fontscale, default is the best looking in my opinion
hud.Font = HudElementFonts.Default;
hud.FontScale = 1.6f;
/* The origin is relative from this point. I did not find out yet how the point type works correctly
* Just try a bit yourself and find the perfect point
* You can also let this value 0 then the 'start point' will be right to the radar */
hud.PointType = 81;
//Set the relative origin
hud.OriginY = 70f;
hud.OriginX = 10f;
//Set the label to the HudElement
hud.SetLabel("K/D Ratio: ");
hud.Value = 0f;
return hud.HudElementNum;
}
}
}



![[Image: lQDUjba.jpg]](http://i.imgur.com/lQDUjba.jpg)