
/* 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
*   "d" - display player stats at the end of map
*   "e" - display top15 at the end of map
*
*   client cmd:
*   say hp - displays all info about your killer
*   say stats - displays info about your stats
*   say top15 - displays first 15. players
*/

#include <amxmod>
#include <csstats>

new killers[33][4]
new klist[33]
new buffor[1280]
new body_part[8][] = {"whole body","head","chest","stomach","left arm","right arm","left leg","right leg"}

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

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

/* build list of hita for AV List */
get_hits(id,killer) {
	new temp[64], stats[8], body[8]
	buffor[0] = 0
	get_user_astats(id,killer,stats,body)
	for(new a = 1; a < 8; ++a){
		if(body[a]){
			format(temp,64,"%s: %d^n",body_part[a],body[a])
			add(buffor,1024,temp)
		}
	}
}

/*get top 15 */
get_top15(){
	new temp[128], stats[8], body[8], name[32]
	copy(buffor,1280,"#   nick                           kills/deaths          hits/shots/headshots^n")
	new imax = get_statsnum()
	if (imax > 15) imax = 15
	for(new a = 0; a < imax; ++a){
		get_stats(a,stats,body,name,32)
		format(temp,128,"%-2d.  %-32s    %d/%d                %d/%d/%d^n",a+1,name,stats[0],stats[1],stats[5],stats[4],stats[2])
		add(buffor,1280,temp)
	}
}

/* build list of hits for say hp */
get_myhits(id,killed) {
	new temp[64], stats[8], body[8], found = 0
	get_user_name(killed,temp,32)
	format(buffor,1024,"[AMX] You hit %s in:",temp)
	get_user_vstats(id,killed,stats,body)
	for(new a = 1; a < 8; ++a){
		if(body[a]){
			found = 1
			format(temp,64," %s: %d ",body_part[a],body[a])
			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) {
	if (killers[id][0]) {
		klist[id] = 0
		new avmode = get_avmode()
		if (avmode&4){
			new name[32], stats[8], body[8], wpn[32], mstats[8], mbody[8]
			get_user_name(killers[id][0],name,32)
			get_user_astats(id,killers[id][0],stats,body,wpn,32)
			get_user_vstats(id,killers[id][0],mstats,mbody)
			set_hudmessage(220,80,0,0.05,0.15,0, 6.0, 12.0, 1.0, 2.0, 1)
			get_hits(id,killers[id][0])
			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).^nHe hits you in:^n%s",
					name,wpn,float(killers[id][3]) * 0.0254,	stats[6],stats[5],		killers[id][1],killers[id][2],
						mstats[6],mstats[5],buffor )
		}
		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)
	}
}

/* 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)
		}
	}
	killers[id][0] = 0
	klist[id] = 1
}

/* display info on say hp */
public display_killer(id) {
	if (killers[id][0]) {
		new name[32], stats[8], body[8], wpn[32], mstats[8], mbody[8]
		get_user_name(killers[id][0],name,32)
		get_user_astats(id,killers[id][0],stats,body,wpn,32)
		get_user_vstats(id,killers[id][0],mstats,mbody)
		client_print(id,print_chat,"[AMX] %s killed you with %s from distance of %.2f meters.",	name,wpn,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.",
			stats[6],stats[5], killers[id][1],killers[id][2] )
		client_print(id,print_chat,"[AMX] You did %d damage to him with %d hit(s).",	mstats[5],mstats[6] )
		if (get_myhits(id,killers[id][0])) client_print(id,print_chat,buffor)
	}
	else {
		client_print(id,print_chat,"[AMX] You have no killer...")
	}
	return PLUGIN_CONTINUE
}

/* display stats on say stats */
public display_stats(id) {
		new name[32], stats[8], body[8], temp[256]
		get_user_wstats(id,0,stats,body)
		format(buffor,256,"Kills: %d^nDeaths: %d^nDamage: %d^nHits: %d^nShots: %d^n^n",
			stats[0],stats[1],stats[6],stats[5],stats[4])
		for(new a = 1; a < 31; ++a)	{
			if (get_user_wstats(id,a,stats,body)){
				get_weaponname(a,name,32)
				format(temp,256,"%s shots: %d  hits: %d  damage: %d  kills: %d  deaths: %d^n",
					name[7],stats[4],stats[5],stats[6],stats[0],stats[1])
				add(buffor,1024,temp)
			}
		}
		get_user_name(id,name,32)
		show_motd(id,buffor,name)
		return PLUGIN_CONTINUE
}

/* display stats on say top15 */
public display_top15(id) {
	get_top15()
	show_motd(id,buffor,"Top 15")
	return PLUGIN_CONTINUE
}

/* end map stats */
public end_game_stats(){
	new avmode = get_avmode()
	if (avmode&8){
		new players[32], inum
		get_players(players,inum)
		for(new i = 0; i < inum; ++i)
			display_stats(players[i])
	}	
	else if (avmode&16)	{
		new players[32], inum
		get_players(players,inum)
		get_top15()
		for(new i = 0; i < inum; ++i)
			show_motd(players[i],buffor,"Top 15")
	}
}

public intermission_event()
	set_task(1.0,"end_game_stats")

/* initialization */
public plugin_init() {
	register_plugin("Small Stats","0.8","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_event("30","intermission_event","a")
	register_cvar("amx_avlist_mode","abce")
	register_clcmd("say hp","display_killer")
	register_clcmd("say stats","display_stats")
	register_clcmd("say top15","display_top15")	
	return PLUGIN_CONTINUE
}

