/*########################################################################## 
## 
## -- www.SteamTools.net 
##      ___       ___  ___  __    __       _____   _____       ___       ___  ___   _____  
##     /   |     /   |/   | \ \  / /      |_   _| | ____|     /   |     /   |/   | /  ___/ 
##    / /| |    / /|   /| |  \ \/ /         | |   | |__      / /| |    / /|   /| | | |___  
##   / / | |   / / |__/ | |   }  {          | |   |  __|    / / | |   / / |__/ | | \___  \ 
##  / /  | |  / /       | |  / /\ \         | |   | |___   / /  | |  / /       | |  ___| | 
## /_/   |_| /_/        |_| /_/  \_\        |_|   |_____| /_/   |_| /_/        |_| /_____/ 
##                                                        
##          |__                   |__  o _|_   ___   __ __  o |__,  ___  
##      --  |__) (__|     (__(__( |  ) |  |_, (__/_ |  )  ) | |  \ (__/_ 
##                  |                                                    
## 
##   I just got so sick of all these team switchers that either didn't work
## at all, or didn't work properly. Some of them didnt wait for the user
## to die. Some of them didn't let you switch entire teams, so I decided
## ok fine, i'll just make my own. Two cokes, one sandwich, and 2 hours
## later I was finished!
## 
##   Enjoy! 
## 
## 
## CHANGELOG 
##------------------------------------------------------------------------ 
## 1) This is the first version -- no changes
## 
## 
## INSTALLATION 
##------------------------------------------------------------------------ 
## 1) Unzip (which you may have done already) 
## 2) Place 'amx_teams.amxx' in 'cstrike/addons/amxmodx/plugins' 
## 3) Add a line in 'configs/plugins.ini' containing 'amx_teams.amxx' 
## 4) Add the optional CVARs to your 'server.cfg'
## 5) -- Visit www.SteamTools.net and enjoy your new plugin! 
## 
## 
## THE COMMANDS
##------------------------------------------------------------------------ 
## amx_team <userid, nick, authid, @team> <CT, TE, SPEC>
## - - -
## Example #1:
##    amx_team mike te
##    // Will switch 'whitemike' to the 'terrorist' team
##
## Example #2:
##    amx_team @CT TE
##    // Will switch all clients on CT team to TE team
##
## 
## THE CVARs 
##------------------------------------------------------------------------ 
## 
## amx_team_spawn
##   - Respawn or wait until the player dies when they switch?
##
##     - When set to 1 even if the player is alive they will respawn
##        at their designated spawn point. When set to 0 and the player
##        is alive it will just wait until they die to switch them.
##
##     + Default is 0
## 
## 
##########################################################################*/


#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fun>

new g_toSwitchIndex[32]
new CsTeams:g_toSwitchTeam[32]
new CsInternalModel:g_toSwitchClass[32]
new g_playersCount = 0

public plugin_init() {
	register_plugin("AMX Team Control", "1.0", "whitemike")
	register_clcmd("amx_team", "switchTeam", ADMIN_KICK, "<userid, nick, authid, @team> <CT, TE, SPEC>")
	register_event("DeathMsg", "userKilled", "a", "1>0") 
	register_cvar("amx_team_spawn", "0");
	// Respawn or wait until the player dies when you switch them?
	// 0 = Wait for them to die
	// 1 = Respawn them immediately
}

public switchTeam(id, level, cid) {
	if(!access(id, ADMIN_KICK)) return PLUGIN_HANDLED

	new argNum = read_argc()

	if(argNum != 3) {
		console_print(id, "[AMXX] Invalid number of arguments (must be 2)^n[AMXX] amx_team <userid, nick, authid, @team> <CT, TE, SPEC>")
		return PLUGIN_HANDLED
	}

	new argWho[32]
	new argWhere[32]

	read_argv(1, argWho, 31)
	read_argv(2, argWhere, 31)

	new CsInternalModel:plrClassCT[4]
	new CsInternalModel:plrClassTE[4]
	new CsInternalModel:plrClass
	new CsTeams:plrGoTo
	new teamMatch = 1

	plrClassCT[0] = CS_CT_URBAN
	plrClassCT[1] = CS_CT_GSG9
	plrClassCT[2] = CS_CT_GIGN
	plrClassCT[3] = CS_CT_SAS

	plrClassTE[0] = CS_T_TERROR
	plrClassTE[1] = CS_T_LEET
	plrClassTE[2] = CS_T_ARCTIC
	plrClassTE[3] = CS_T_GUERILLA

	if(equali(argWhere, "CT")) plrGoTo = CS_TEAM_CT
	else if(equali(argWhere, "TE")) plrGoTo = CS_TEAM_T
	else if(equali(argWhere, "SPEC")) plrGoTo = CS_TEAM_SPECTATOR
	else teamMatch = 0

	//console_print(id, "[AMXX][DEBUG] %s", argWhere[1])

	if(plrGoTo == CS_TEAM_CT)	plrClass = plrClassCT[random(3)]
	else if(plrGoTo == CS_TEAM_T) plrClass = plrClassTE[random(3)]
	else 					plrClass = CS_DONTCHANGE

	if(argWho[0] == '@') {
		new plrList[32]
		new plrCount

		get_players(plrList, plrCount, "e", argWho[1])
		if(plrCount == 0) {
			console_print(id, "[AMXX] There are no players in that team")
			return PLUGIN_HANDLED
		}

		if(teamMatch == 0) {
			console_print(id, "[AMXX] Couldn't find a matching team for your destination")
			return PLUGIN_HANDLED
		}

		for(new i=0; i<plrCount; i++) {
			handleSwitch(plrList[i], plrGoTo, plrClass)
		}

		new pluralText[7]
		if(plrCount == 1) pluralText = " has"
		else 			pluralText = "s have"

		console_print(id, "[AMXX] %i client%s been switched", plrCount, pluralText)
	}
	else {
		new plrIndex
		new plrName[33]

		plrIndex = find_player("bl", argWho)
		if(!plrIndex) plrIndex = find_player("c", argWho)
		if(!plrIndex) plrIndex = find_player("k", argWho)
		if(!plrIndex) plrIndex = find_player("d", argWho)


		if(!plrIndex) {
			console_print(id, "[AMXX] Couldn't find a player matching your query")
			return PLUGIN_HANDLED
		}

		if(teamMatch == 0) {
			console_print(id, "[AMXX] Couldn't find a matching team for your destination")
			return PLUGIN_HANDLED
		}		

		handleSwitch(plrIndex, plrGoTo, plrClass)
		get_user_name(plrIndex, plrName, 32)

		console_print(id, "[AMXX] Client '%s' has been queued for switch", plrName)
	}

	return PLUGIN_HANDLED
}

public handleSwitch(Index, CsTeams:Team, CsInternalModel:Class) {
	new plrIndex = Index
	new CsTeams:plrTeam = Team
	new CsInternalModel:plrClass = Class

	if(get_cvar_num("amx_team_spawn") == 1) {
		cs_set_user_team(plrIndex, plrTeam, plrClass)

		if(is_user_alive(plrIndex)) {
			spawn(plrIndex);
		}
	}
	else {
		if(is_user_alive(plrIndex)) {
			g_toSwitchIndex[g_playersCount] = plrIndex
			g_toSwitchTeam[g_playersCount] = plrTeam
			g_toSwitchClass[g_playersCount] = plrClass
			g_playersCount++
		}
		else {
			cs_set_user_team(plrIndex, plrTeam, plrClass)
		}
	}
}


public userKilled() {
	new plrVictim = read_data(2)
	new plrSwitch = -1

	for(new i=0; i<g_playersCount; i++) {
		if(g_toSwitchIndex[i] == plrVictim) {
			plrSwitch = i
		}
	}

	if(plrSwitch > -1) {
		cs_set_user_team(g_toSwitchIndex[plrSwitch], g_toSwitchTeam[plrSwitch], g_toSwitchClass[plrSwitch])
		g_toSwitchIndex[plrSwitch] = false
		g_playersCount--
	}
}




/*
enum CsInternalModel {
	CS_DONTCHANGE = 0,
	CS_CT_URBAN = 1,
	CS_T_TERROR = 2,
	CS_T_LEET = 3,
	CS_T_ARCTIC = 4,
	CS_CT_GSG9 = 5,
	CS_CT_GIGN = 6,
	CS_CT_SAS = 7,
	CS_T_GUERILLA = 8,
	CS_CT_VIP = 9
};
enum CsTeams {
	CS_TEAM_T = 1,
	CS_TEAM_CT = 2,
	CS_TEAM_SPECTATOR = 3
};
Team values are CS_TEAM_T, CS_TEAM_CT, or CS_TEAM_SPECTATOR. 
cs_set_user_team ( index, CsTeams:team, [ CsInternalModel: model = CS_DONTCHANGE ] ) 
*/