/*
	 Copyright © 2011, Game over

	Zp_zspawn_system plugin is free software;
	you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Dispenser Plugin; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.
	
	Источник http://best-kill.ru/
*/

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

#define PLUGIN "[ZP] Server addon: Zspawn_System"
#define VERSION "5.1"
#define AUTHOR "Game over"

new bool:g_IsZspawned[33]
new z_spawn_mode, z_spawn_infection, z_spawn_team_infection, z_spawn_swarm, z_spawn_team_swarm, z_spawn_plague, z_spawn_team_plague, z_spawn_nemesis, z_spawn_team_nemesis, z_spawn_survivor, z_spawn_team_survivor, z_spawn_client_settings, z_spawn_client_rate, z_spawn_client_cmdrate, z_spawn_client_updaterate

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	z_spawn_mode = register_cvar("zp_spawn_mode", "2")
	z_spawn_infection = register_cvar("zp_spawn_infection", "1")
	z_spawn_team_infection = register_cvar("zp_spawn_team_infection", "0")
	z_spawn_swarm = register_cvar("zp_spawn_swarm", "1")
	z_spawn_team_swarm = register_cvar("zp_spawn_team_swarm", "0")
	z_spawn_plague = register_cvar("zp_spawn_plague", "1")
	z_spawn_team_plague = register_cvar("zp_spawn_team_plague", "0")
	z_spawn_nemesis = register_cvar("zp_spawn_nemesis", "1")
	z_spawn_team_nemesis = register_cvar("zp_spawn_team_nemesis", "0")
	z_spawn_survivor = register_cvar("zp_spawn_survivor", "1")
	z_spawn_team_survivor = register_cvar("zp_spawn_team_survivor", "0")
	z_spawn_client_settings = register_cvar("zp_spawn_client_settings", "1")
	z_spawn_client_rate = register_cvar("zp_spawn_client_rate", "15000")
	z_spawn_client_cmdrate = register_cvar("zp_spawn_client_cmdrate", "71")
	z_spawn_client_updaterate = register_cvar("zp_spawn_client_updaterate", "71")

	register_event("TeamInfo", "event_TeamInfo", "a", "2=TERRORIST", "2=CT")
}
public plugin_cfg()
{
	server_cmd("exec addons/amxmodx/configs/zp_zspawn_system.cfg")
}
public client_putinserver(id)
{
	g_IsZspawned[id] = false
	set_task(0.1, "ClientSettings", id)
}
public ClientSettings(id)
{
	if (1 == get_pcvar_num(z_spawn_client_settings))
{
	client_cmd(id, "fakelag 0")
	client_cmd(id, "fakeloss 0")
	client_cmd(id, "rate %d", get_pcvar_num(z_spawn_client_rate))
	client_cmd(id, "cl_cmdrate %d", get_pcvar_num(z_spawn_client_cmdrate))
	client_cmd(id, "cl_updaterate %d", get_pcvar_num(z_spawn_client_updaterate))
	client_cmd(id, "cl_allowupload 1")
	client_cmd(id, "cl_allowdownload 1")
	client_cmd(id, "cl_corpsestay 60")
}
}
public event_TeamInfo()
{
	static id
	id = read_data(1)
	
	if (!g_IsZspawned[id])
	{
		set_task(5.0, "ZSpawnMode", id)
		g_IsZspawned[id] = true
	}
}
public ZSpawnMode(id)
{
if (0 < get_pcvar_num(z_spawn_mode))
switch (get_pcvar_num(z_spawn_mode))
{
    case 1:
    {
set_task(0.1, "ZSpawn", id)
    }
	case 2:
    {
set_task(0.1, "ZSpawn", id)
if (!is_user_alive(id))
set_task(0.5, "FuncTeleport", id)
    }
}
}
public ZSpawn(id) {
	if (zp_is_nemesis_round() && get_pcvar_num(z_spawn_nemesis)) {
	switch (get_pcvar_num(z_spawn_team_nemesis)) {
    case 0:
    {
    zp_respawn_user(id, ZP_TEAM_ZOMBIE)
    }
    case 1:
    {
    zp_respawn_user(id, ZP_TEAM_HUMAN)
    }
}
	} else if (zp_is_survivor_round() && get_pcvar_num(z_spawn_survivor)) {
	switch (get_pcvar_num(z_spawn_team_survivor)) {
    case 0:
    {
    zp_respawn_user(id, ZP_TEAM_ZOMBIE)
    }
    case 1:
    {
    zp_respawn_user(id, ZP_TEAM_HUMAN)
    }
}
	} else if (zp_is_swarm_round() && get_pcvar_num(z_spawn_swarm)) {
	switch (get_pcvar_num(z_spawn_team_swarm)) {
    case 0:
    {
    zp_respawn_user(id, ZP_TEAM_ZOMBIE)
    }
    case 1:
    {
    zp_respawn_user(id, ZP_TEAM_HUMAN)
    }
}
	} else if (zp_is_plague_round() && get_pcvar_num(z_spawn_plague)) {
	switch (get_pcvar_num(z_spawn_team_plague)) {
    case 0:
    {
    zp_respawn_user(id, ZP_TEAM_ZOMBIE)
    }
    case 1:
    {
    zp_respawn_user(id, ZP_TEAM_HUMAN)
    }
}
	} else if (zp_has_round_started() && get_pcvar_num(z_spawn_infection) && zp_is_nemesis_round() == 0 && zp_is_survivor_round() == 0 && zp_is_swarm_round() == 0 && zp_is_plague_round() == 0) {
	switch (get_pcvar_num(z_spawn_team_infection)) {
    case 0:
    {
    zp_respawn_user(id, ZP_TEAM_ZOMBIE)
    }
    case 1:
    {
    zp_respawn_user(id, ZP_TEAM_HUMAN)
    }
}
	}
}
public FuncTeleport(id)
{
	static Float:iOrigin[3]
	pev(id, pev_origin, iOrigin)
	
	engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, iOrigin, 0)
	write_byte(TE_TELEPORT)
	engfunc(EngFunc_WriteCoord, iOrigin[0])
	engfunc(EngFunc_WriteCoord, iOrigin[1])
	engfunc(EngFunc_WriteCoord, iOrigin[2])
	message_end()
}