/*
				[ZP] Extra Server Addon : zTele
				   (For Humans and Zombies)
					    by Fry!
					
					
	Description :
	
			Does anyone remember ztele from cs:source zm? Well yea now it's made for cs 1.6 to zombie plague mode.
			If you can't escape from zombies or from survivors just say /ztele or !ztele and you will be teleported
			to your spawn.
			
			
	Cvars :
	
			zp_extra_ztele_mode "0"	- 0 - for both teams, 1 - for zombies, 2 - survivors.
			zp_extra_ztele_max "3"	- How many times you can use ztele, default is 3 times.
			
	
	To Do :
	
			Make it purchaseable too (maybe).
	
	
	Changelog :
	
			29/07/2009 - v1.0 - Public release
*/

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <zombieplague>

#define PLUGIN "[ZP] Extra Server Addon : zTele"
#define VERSION "1.0"
#define AUTHOR "Fry!"

new const Teleport_Sound[] = "zombie_plague/teleport.wav"

new g_ztele_mode, g_ztele_max
new bool:g_canztele[33], g_used[33]
new Float:g_spawns[33][3], Float:g_angles[33][3]

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_cvar("zp_extra_zTele",VERSION,FCVAR_SERVER|FCVAR_EXTDLL|FCVAR_UNLOGGED|FCVAR_SPONLY)
	
	g_ztele_mode = register_cvar("zp_extra_ztele_mode", "0") // 0 - both teams, 1 - humans, 2 - zombies
	g_ztele_max = register_cvar("zp_extra_ztele_max", "3") // how many times you can use ztele
	
	register_clcmd("say /ztele", "teleportme")
	register_clcmd("say !ztele", "teleportme")
	
	RegisterHam(Ham_Spawn, "player", "fw_PlayerSpawn", 1)
}

public plugin_precache()
{
	engfunc(EngFunc_PrecacheSound, Teleport_Sound)
}

public client_connect(id)
{
	g_used[id] = 0
	g_canztele[id] = false
}

public client_disconnect(id)
{
	g_used[id] = 0
	g_canztele[id] = false
}

public fw_PlayerSpawn(id)
{
	if (!is_user_alive(id))
		return HAM_IGNORED
	
	g_canztele[id] = false
	
	pev(id, pev_origin, g_spawns[id])
	pev(id, pev_angles, g_angles[id])
	
	client_print(id, print_chat, "[ZP] This server is running Teleport to use it say !ztele")
	
	return HAM_IGNORED
}

public teleportme(id)
{
	if (!is_user_alive(id))
	{
		g_used[id] = 0
		g_canztele[id] = false
		
		client_print(id, print_chat, "[ZP] You must be alive to use ztele!")
		
		return PLUGIN_HANDLED
	}
	
	switch (get_pcvar_num(g_ztele_mode))
	{
		case 0:
		{
			teleport_player(id)
			client_print(id, print_chat, "[ZP] You are now teleported to your spawn point.")
			client_print(id, print_chat, "[ZP] You have teleported %d of %d times", get_pcvar_num(g_ztele_max))
		}
		
		case 1:
		{
			if (zp_get_user_zombie(id))
			{
				teleport_player(id)
				client_print(id, print_chat, "[ZP] You are now teleported away as zombie.")
				client_print(id, print_chat, "[ZP] You have teleported %d of %d times", get_pcvar_num(g_ztele_max))
			}
			
			else if (zp_get_user_nemesis(id))
			{
				g_used[id] = 0
				g_canztele[id] = false
				
				client_print(id, print_chat, "[ZP] Sorry but nemesis can't teleport")
				
				return PLUGIN_HANDLED
			}
		}
			
		case 2:
		{
			if (!zp_get_user_zombie(id))
			{
				teleport_player(id)
				client_print(id, print_chat, "[ZP] You are now teleported away as survivor, to your spawn point.")
				client_print(id, print_chat, "[ZP] You have teleported %d of %d times", get_pcvar_num(g_ztele_max))
			}
		}
	}
	
	if (get_pcvar_num(g_ztele_max) == g_used[id])
	{
		g_canztele[id] = false
		client_print(id, print_chat, "[ZP] Sorry but you can't ztele anymore you already did %d of %d times", get_pcvar_num(g_ztele_max))
		
		return PLUGIN_HANDLED
	}
	
	return PLUGIN_CONTINUE
}

stock teleport_player(id)
{
	g_used[id]++
	
	set_pev(id,pev_origin, g_spawns[id])
	set_pev(id, pev_angles, g_angles[id])
	emit_sound(id, CHAN_VOICE, Teleport_Sound, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)

	message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
	write_byte(TE_TELEPORT)
	engfunc(EngFunc_WriteCoord, g_spawns[id][0])
	engfunc(EngFunc_WriteCoord, g_spawns[id][1])
	engfunc(EngFunc_WriteCoord, g_spawns[id][2])
	message_end()
}
