/***************************************************************************
 * amx_ejl_playerghosts.sma     version 1.3                  Date: 4/29/2003
 *  Author: Eric Lidman      ejlmozart@hotmail.com
 *  Alias: Ludwig van        Upgrade: http://lidmanmusic.com/cs/plugins.html
 *
 *  Another dumb plugin by me. I wasn't going to post it, but some people
 *   said they wanted it when they saw it on my server so why not? Anyway,
 *   if the command amx_ghosts is issued, dead players will become ghosts
 *   and have the appearance of a strange blue cloud hovering about. 
 *   New: admins appear as a different type of ghost than players
 *   New: different modes where dead only can see ghosts or all players can
 *   see ghosts
 *
 *  COMMANDS:
 *
 *   amx_ghosts             --enables/disables ghosts
 *   amx_ghosts_mode        --toggles ghosts visability to dead only, or
 *                            ghosts visable to all
 *
 *  CVARS:
 *
 *   amx_ghosts_default 0   --set this to 1 in admin.cfg for ghost mode
 *                            default on, 0 for off. 0 is plugin default 
 *   amx_ghosts_alivesee 0  --set this to 1 in admin.cfg for ghost mode
 *                            so that all players see ghosts, not just the
 *                            dead players seeing other dead player ghosts
 *                            0 for dead only see ghosts. 0 is default 
 *   amx_ghosts_admin 0     --1 sets admins with different ghosts than
 *                            players. 0 sets everyones ghost to the same
 *                            type                             
 *
 ***************************************************************************/

#include <amxmod>
#include <amxmisc>

new modname[32]
new glow_spr
new glow_spr2
new bool:pl_ghosts = false

public admin_plghdo(id,level,cid){
	if (!cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED

	if(get_cvar_num("amx_ghosts_alivesee") == 1){
		set_cvar_string("amx_ghosts_alivesee","0")
		console_print(id,"[AMX]  Ghosts plugin set so only the dead can see ghosts")
	}else{
		set_cvar_string("amx_ghosts_alivesee","1")
		console_print(id,"[AMX]  Ghosts plugin set so now even the living can see the ghosts")
	}

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)
	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" ghosts_mode", name,get_user_userid(id),authid)
	return PLUGIN_HANDLED
}


public admin_pl_ghosts(id,level,cid){
	if (!cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED

	if(pl_ghosts == false){
		pl_ghosts = true
		client_print(0,print_chat,"[AMX]  Admin has enabled player ghosts") 
		console_print(id,"[AMX]  You have enabled player ghosts")
		new maxplayers = get_maxplayers()+1
		for (new i=1; i<=maxplayers; i++){
			if( (is_user_connected(i) == 1) && (is_user_alive(i) == 0) ){
				new sid[2]
				sid[0] = i
				if( (get_user_flags(i) & ADMIN_MAP) && (get_cvar_num("amx_ghosts_admin") == 1) ){
					deadadmin_sprite(sid[0])
					set_task(0.9,"deadadmin_sprite",65123+i,sid,2,"b")
				}else{
					deadplayer_sprite(sid[0])
					set_task(0.7,"deadplayer_sprite",65123+i,sid,2,"b")	
				}
			}
		}		
	}else {
		round_end()
		client_print(0,print_chat,"[AMX]  Admin has disabled player ghosts") 
		console_print(id,"[AMX]  You have disabled player ghosts")
		pl_ghosts = false
	}	

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)
	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" ghosts",name,get_user_userid(id),authid)
	return PLUGIN_HANDLED
}

public death_ghosts(){
	if(pl_ghosts == false)
		return PLUGIN_CONTINUE
	new victim[2]
	victim[0] = read_data(2)
	if( (get_user_flags(victim[0]) & ADMIN_MAP) && (get_cvar_num("amx_ghosts_admin") == 1) ){
		deadadmin_sprite(victim[0])
		set_task(0.9,"deadadmin_sprite",65123+victim[0],victim,2,"a",99999)
	}else{
		deadplayer_sprite(victim[0])
		set_task(0.7,"deadplayer_sprite",65123+victim[0],victim,2,"a",99999)
	}
	return PLUGIN_CONTINUE
}

public deadplayer_sprite(id[]){
	new origin[3]
	get_user_origin(id[0],origin)
	new players[32], inum
	if(get_cvar_num("amx_ghosts_alivesee"))
		get_players(players,inum,"c")
	else
		get_players(players,inum,"bc")
	for(new i = 0 ;i < inum; ++i){
		//TE_SPRITE
		message_begin(MSG_ONE,SVC_TEMPENTITY,{0,0,0},players[i])
		write_byte(17) // additive sprite, plays 1 cycle
		write_coord(origin[0]) // pos
		write_coord(origin[1]) // pos
		write_coord(origin[2]+20) // pos
		write_short (glow_spr) // spr index	
		write_byte(8) // (scale in 0.1's) 
		write_byte (200) //(brightness)
		message_end()
	}
	return PLUGIN_CONTINUE
}

public deadadmin_sprite(id[]){
	new origin[3]
	get_user_origin(id[0],origin)
	new players[32], inum
	if(get_cvar_num("amx_ghosts_alivesee"))
		get_players(players,inum,"c")
	else
		get_players(players,inum,"bc")
	for(new i = 0 ;i < inum; ++i){
		//TE_SPRITE
		message_begin(MSG_ONE,SVC_TEMPENTITY,{0,0,0},players[i])
		write_byte(17) // additive sprite, plays 1 cycle
		write_coord(origin[0]) // pos
		write_coord(origin[1]) // pos
		write_coord(origin[2]+20) // pos
		write_short (glow_spr2) // spr index	
		write_byte(8) // (scale in 0.1's) 
		write_byte (75) //(brightness)
		message_end()
	}
	return PLUGIN_CONTINUE
}

public round_end(){
	if(pl_ghosts == false)
		return PLUGIN_CONTINUE
	new maxplayers = get_maxplayers()+1
	for (new i=1; i<=maxplayers; i++){
		remove_task(65123+i)
	}
	return PLUGIN_CONTINUE
}

public chk_player(){
	new maxplayers = get_maxplayers()+1
	for (new i=1; i<=maxplayers; i++){
		if(is_user_alive(i) == 1){
			remove_task(65123+i)
		}
	}
	return PLUGIN_CONTINUE
}

public check_ghost_def(){
	if(get_cvar_num("amx_ghosts_default"))
		server_cmd("amx_ghosts")
	return PLUGIN_CONTINUE	
}

public plugin_precache(){ 
	glow_spr = precache_model("sprites/poison.spr")
	glow_spr2 = precache_model("sprites/nhth1.spr")
	return PLUGIN_CONTINUE 
}

public plugin_init(){
	register_plugin("PLAYER GHOSTS","1.3","EJL")
	register_concmd("amx_ghosts","admin_pl_ghosts",ADMIN_MAP,": turns visable player ghost on while player is dead")
	register_concmd("amx_ghosts_mode","admin_plghdo",ADMIN_MAP,": toggles ghosts visable to dead only")
	register_event("DeathMsg", "death_ghosts", "a")
	register_cvar("amx_ghosts_default","0")
	register_cvar("amx_ghosts_admin","0")
	register_cvar("amx_ghosts_alivesee","0")
	set_cvar_string("amx_ghosts_default","0")
	set_cvar_string("amx_ghosts_admin","0")
	set_cvar_string("amx_ghosts_alivesee","0")

	get_modname(modname,31)
	if(equal(modname,"cstrike")){	
		register_event("SendAudio","round_end","a","2=%!MRAD_terwin","2=%!MRAD_ctwin","2=%!MRAD_rounddraw")
		register_event("TextMsg","round_end","a","2&#Game_C","2&#Game_w")
	}else{
		set_task(1.5,"chk_player",14432,"",0,"b")
	}
	set_task(2.0,"check_ghost_def")
	return PLUGIN_CONTINUE
}

