
/* AMX Mod script.
*
* (c) Copyright 2002, OLO
* This file is provided as is (no warranties).
*
*/

/*  cvar:
*   amx_avlist_mode < flags >
*   "a" - show attackers
*   "b" - show victims
*   "c" - show killer
*
*   client cmd:
*   say hp
*   displays info about your killer
*/

#include <amxmod>

new killers[33][4]
new khits[33][33]
new kdmg[33][33]
new kweapons[33][16]
new klist[33]
new buffor[1024]

/* build list of attackers */
get_attackers(id) {
	new temp[64],name[32],wpn[32],found = 0
	copy(buffor,1024,"Attackers:^n")
	for(new a = 1; a < 33; ++a){
		if(khits[id][a]){
			found = 1
			if (killers[id][0] == a)	
				format(wpn,32," -- %s",kweapons[id])
			else
				wpn[0] = 0
			get_user_name(a,name,32)
			format(temp,64,"%s -- %d dmg / %d hit(s)%s^n",name,kdmg[id][a],khits[id][a],wpn)
			add(buffor,1024,temp)
		}
	}
	return found
}

/* build list of victims */
get_victims(id) {
	new temp[64],name[32],wpn[32],found = 0
	copy(buffor,1024,"Victims:^n")
	for(new a = 1; a < 33; ++a){
		if(khits[a][id]){
			found = 1
			if (killers[a][0] == id)
				format(wpn,32," -- %s",kweapons[a])
			else
				wpn[0] = 0
			get_user_name(a,name,32)
			format(temp,64,"%s -- %d dmg / %d hit(s)%s^n",name,kdmg[a][id],khits[a][id],wpn)
			add(buffor,1024,temp)
		}
	}
	return found
}

/* amx_avlist_mode */
get_avmode() {
		new avmode[8]
		get_cvar_string("amx_avlist_mode",avmode,8)
		return read_flags(avmode)
}

/* save hits and damage */
public damage_event(id) {
	new weapon, attacker = get_user_attacker(id,weapon)
	khits[id][attacker]++
	kdmg[id][attacker] += read_data(2)
	if (killers[id][0]) {
		klist[id] = 0
		new avmode = get_avmode()
		if (avmode&4){
			new name[32]
			get_user_name(killers[id][0],name,32)
			set_hudmessage(220,80,0,0.05,0.15,0, 6.0, 12.0, 1.0, 2.0, 1)
			show_hudmessage(id,"%s killed you with %s^nfrom distance of %.2f meters.^nHe did %d damage to you with %d hit(s)^nand still has %dhp and %dap.^nYou did %d damage to him with %d hit(s).",
					name,kweapons[id],float(killers[id][3]) * 0.0254,	kdmg[id][killers[id][0]],	khits[id][killers[id][0]],
					killers[id][1],killers[id][2],kdmg[killers[id][0]][id],khits[killers[id][0]][id] )
		}
		if ((avmode&2)&&get_victims(id)){
			set_hudmessage(0,80,220,0.55,0.60,0, 6.0, 12.0, 1.0, 2.0, 4)
			show_hudmessage(id,buffor)
		}
		if ((avmode&1)&&get_attackers(id)){
			set_hudmessage(220,80,0,0.55,0.35,0, 6.0, 12.0, 1.0, 2.0, 3)
			show_hudmessage(id,buffor)
		}
	}
}

/* save state at death */
public deathmsg_event() {
	new killer = read_data(1)
	new victim = read_data(2)
	if (killer != victim && killer)	{
		new vorigin[3], korigin[3]
		get_user_origin(victim,vorigin)
		get_user_origin(killer,korigin)
		killers[victim][0] = killer
		killers[victim][1] = get_user_health(killer)
		killers[victim][2] = get_user_armor(killer)
		killers[victim][3] = get_distance(vorigin,korigin)
		read_data(4,kweapons[victim],16)
	}
}

/* reset all data about your killer and attackers */
public clear_stats(param[]){
	new id = str_to_num(param)
	kweapons[id][0] = 0
	killers[id][0] = 0
	for(new a = 0; a < 33; ++a){
		kdmg[id][a] = 0
		khits[id][a] = 0
	}
}

/* clear all on respawn */
public resethud_event(id) {
	if(klist[id]){
		new avmode = get_avmode()
		if ((avmode&2)&&get_victims(id)){
			set_hudmessage(0,80,220,0.55,0.60,0, 6.0, 12.0, 1.0, 2.0, 4)
			show_hudmessage(id,buffor)
		}
		if ((avmode&1)&&get_attackers(id)){
			set_hudmessage(220,80,0,0.55,0.35,0, 6.0, 12.0, 1.0, 2.0, 3)
			show_hudmessage(id,buffor)
		}
	}
	klist[id] = 1
	new param[4]
	num_to_str(id,param,4)
	set_task(0.2,"clear_stats",0,param) /* so other respawning can still read from your list */
}

/* display info on say hp request */
public display_killer(id) {
	if (killers[id][0]) {
		new name[32]
		get_user_name(killers[id][0],name,32)
		client_print(id,print_chat,"[AMX] %s killed you with %s from distance of %.2f meters.",
			name,kweapons[id],float(killers[id][3]) * 0.0254 )
		client_print(id,print_chat,"[AMX] He did %d damage to you with %d hit(s) and still had %dhp and %dap.",
			kdmg[id][killers[id][0]],khits[id][killers[id][0]], killers[id][1],killers[id][2] )
		client_print(id,print_chat,"[AMX] You did %d damage to him with %d hit(s).",
			kdmg[killers[id][0]][id],khits[killers[id][0]][id] )
	}
	else {
		client_print(id,print_chat,"[AMX] You have no killer...")
	}
	return PLUGIN_HANDLED
}

/* initialization */
public plugin_init() {
	register_plugin("AV List","0.6","default")
	register_event("Damage","damage_event","b","2!0","3=0","4!0")
	register_event("DeathMsg","deathmsg_event","a")
	register_event("ResetHUD","resethud_event","be")
	register_cvar("amx_avlist_mode","abc")
	register_clcmd("say hp","display_killer")
	return PLUGIN_CONTINUE
}


