#include extreme\_ex_punishments;
#include extreme\_ex_lin;

init()
{
	//Main Function that loops looking for admin requests

	// Clears command cvars, just incase
	setcvar("team", "");
	setcvar("disableweapon", "");
	setcvar("enableweapon", "");
	setcvar("funmode", "");
	setcvar("mattress", "");
	setcvar("barrel", "");
	setcvar("bathtub", "");
	setcvar("cheater", "");
	setcvar("toilet", "");
	setcvar("tree", "");
	setcvar("tomb", "");
	setcvar("original", "");
	setcvar("warp", "");
	setcvar("lock", "");
	setcvar("unlock", "");
	setcvar("suicide", "");
	setcvar("smite", "");
	setcvar("torch", "");
	setcvar("fire", "");
	setcvar("spank", "");
	setcvar("arty", "");
	setcvar("endmap", "");
	setcvar("sayall", "");
	setcvar("sayallcenter", "");
	setcvar("switchplayerallies", "");
	setcvar("switchplayeraxis", "");
	setcvar("switchplayerspec", "");
	setcvar("switchsidesallplayers", "");

	v_FunmodePlayer = undefined;
	v_MattressPlayer = undefined;
	v_BarrelPlayer = undefined;
	v_BathtubPlayer = undefined;
	v_ToiletPlayer = undefined;
	v_TreePlayer = undefined;
	v_TombPlayer = undefined;
	v_OriginalPlayer = undefined;

	//make sure only running once
	if(isDefined(level.ex_cmdmonon)) return;

	level.ex_cmdmonon = true;

	while(!level.ex_gameover)
	{
		v_DisableWeaponPlayer = getcvar("disableweapon");
		v_EnableWeaponPlayer = getcvar("enableweapon");

		if(level.ex_cmdmonitor_models)
		{
			v_FunmodePlayer = getcvar("funmode");
			v_MattressPlayer = getcvar("mattress");
			v_BarrelPlayer = getcvar("barrel");
			v_BathtubPlayer = getcvar("bathtub");
			v_ToiletPlayer = getcvar("toilet");
			v_TreePlayer = getcvar("tree");
			v_TombPlayer = getcvar("tombstone");
			v_OriginalPlayer = getcvar("original");
		}

		v_WarpPlayer = getcvar("warp");
		v_LockPlayer = getcvar("lock");
		v_CheaterPlayer = getcvar("cheater");
		v_UnLockPlayer = getcvar("unlock");
		v_SuicidePlayer = getcvar("suicide");
		v_SmitePlayer = getcvar("smite");
		v_TorchPlayer = getcvar("torch");
		v_FirePlayer = getcvar("fire");
		v_SpankPlayer = getcvar("spank");
		v_ArtyPlayer = getcvar("arty");
		v_EndMap = getcvar("endmap");
		v_SayAll = getcvar("sayall");
		v_SayAllCenter = getcvar("sayallcenter");
		v_SwitchPlayerAllies = getcvar("switchplayerallies");
		v_SwitchPlayerAxis = getcvar("switchplayeraxis");
		v_SwitchPlayerSpectator = getcvar("switchplayerspec");
		v_SwitchSidesAllPlayers = getcvar("switchsidesallplayers");

		if(v_DisableWeaponPlayer != "") thread setStatusweaponPlayer(v_DisableWeaponPlayer, true);
		if(v_EnableWeaponPlayer != "") thread setStatusweaponPlayer(v_EnableWeaponPlayer, false);

		if(level.ex_cmdmonitor_models)
		{
			if(v_FunmodePlayer != "") thread changePlayerModel(v_FunmodePlayer, "funmode");
			if(v_MattressPlayer != "") thread changePlayerModel(v_MattressPlayer, "mattress");
			if(v_BarrelPlayer != "") thread changePlayerModel(v_BarrelPlayer, "barrel");
			if(v_BathtubPlayer != "") thread changePlayerModel(v_BathtubPlayer, "bathtub");
			if(v_ToiletPlayer != "") thread changePlayerModel(v_ToiletPlayer, "toilet");
			if(v_TreePlayer != "") thread changePlayerModel(v_TreePlayer, "tree");
			if(v_TombPlayer != "") thread changePlayerModel(v_TombPlayer, "tombstone");
			if(v_OriginalPlayer != "") thread changePlayerModel(v_OriginalPlayer, "original");
		}

		if(v_WarpPlayer != "") thread messWithPlayer(v_WarpPlayer, "warp");
		if(v_LockPlayer != "") thread messWithPlayer(v_LockPlayer, "lock");
		if(v_CheaterPlayer != "") thread messWithPlayer(v_CheaterPlayer, "cheater");
		if(v_UnLockPlayer != "") thread messWithPlayer(v_UnLockPlayer, "unlock");
		if(v_SuicidePlayer != "") thread messWithPlayer(v_SuicidePlayer, "suicide");
		if(v_SmitePlayer != "") thread messWithPlayer(v_SmitePlayer, "smite");
		if(v_TorchPlayer != "") thread messWithPlayer(v_TorchPlayer, "torch");
		if(v_FirePlayer != "") thread messWithPlayer(v_FirePlayer, "fire");
		if(v_SpankPlayer != "") thread messWithPlayer(v_SpankPlayer, "spank");
		if(v_ArtyPlayer != "") thread messWithPlayer(v_ArtyPlayer, "arty");
		if(v_EndMap != "") thread endMap();

		if(v_SayAll != "") thread sayAll(v_SayAll, 0);
		if(v_SayAllCenter != "") thread sayAll(v_SayAllCenter, 1);
		if(v_SwitchPlayerAllies != "") thread switchSide(v_SwitchPlayerAllies, "allies", 1, true);
		if(v_SwitchPlayerAxis != "") thread switchSide(v_SwitchPlayerAxis, "axis", 1, true);
		if(v_SwitchPlayerSpectator != "") thread switchSide(v_SwitchPlayerSpectator, "spectator", 1, true);
		if(v_switchSidesAllPlayers != "") thread switchSides();

		wait 5;
	}
}

setStatusweaponPlayer(PlayerEntID, lever)
{
	//first clear the buffer
	if(lever) setcvar("disableweapon", "");
		else setcvar("enableweapon", "");

	// Do we want to set teams
	v_team = getcvar("team");
	setcvar("team", "");

	playerEntID = int(PlayerEntID);
	players = getentarray("player", "classname");
	for (i = 0; i < players.size; i++)
	{
		player = players[i];
		entID = player getEntityNumber();

		if(player.sessionstate == "playing" && ((entID == playerEntID) || (playerEntID == -1) || (v_team != "" && player.pers["team"] == v_team)) )
		{
			if(lever)
			{
				if(isAlive(player))
				{
					player thread setWeaponStatus(lever);
					player iprintlnbold(&"CMDMONITOR_DISABLEWEAPONS");
					iprintlnFIXED(&"CMDMONITOR_DISABLEWEAPONSB", player);
					wait 1;
				}
			}
			else
			{
				if(isAlive(player))
				{
					player thread setWeaponStatus(lever);
					player iprintlnbold(&"CMDMONITOR_ENABLEWEAPONS");
					iprintlnFIXED(&"CMDMONITOR_ENABLEWEAPONSB", player);
					wait 1;
				}
			}
		}
	}
}

changePlayerModel(PlayerEntID, mode)
{
	//first clear the buffer
	setcvar(mode, "");

	// Do we want to set teams
	v_team = getcvar("team");
	setcvar("team", "");

	models = [];
	pmsg = undefined;
	amsg = undefined;

	// Setup model's
	switch(mode)
	{
		case "funmode":
		models[0] = "xmodel/furniture_piano";
		models[1] = "xmodel/prop_bike";
		models[2] = "xmodel/prop_haybale";
		models[3] = "xmodel/prop_hydrant";
		models[4] = "xmodel/prop_tractor";
		pmsg = &"CMDMONITOR_FUNMODE";
		amsg = &"CMDMONITOR_FUNMODEB";
		break;

		case "mattress":
		models[0] = "xmodel/furniture_bedmattress1";
		models[1] = "xmodel/furniture_bedmattress_bunker";
		models[2] = "xmodel/furniture_bedmattress2";
		pmsg = &"CMDMONITOR_MATTRESS";
		amsg = &"CMDMONITOR_MATTRESSB";
		break;

		case "barrel":
		models[0] = "xmodel/prop_barrel_benzin";
		models[1] = "xmodel/prop_barrel_black";
		models[2] = "xmodel/prop_barrel_green";
		models[3] = "xmodel/prop_barrel_silver";
		models[4] = "xmodel/prop_barrel_tan";
		models[5] = "xmodel/prop_barrel_white";
		pmsg = &"CMDMONITOR_BARREL";
		amsg = &"CMDMONITOR_BARRELB";
		break;

		case "bathtub":
		models[0] = "xmodel/furniture_bathtub";
		pmsg = &"CMDMONITOR_BATHTUB";
		amsg = &"CMDMONITOR_BATHTUBB";
		break;

		case "toilet":
		models[0] = "xmodel/furniture_toilet";
		pmsg = &"CMDMONITOR_TOILET";
		amsg = &"CMDMONITOR_TOILETB";
		break;

		case "tree":
		models[0] = "xmodel/tree_grey_oak_sm_a";
		models[1] = "xmodel/tree_pine_sm_b";
		pmsg = &"CMDMONITOR_TREE";
		amsg = &"CMDMONITOR_TREEB";
		break;

		case "tombstone":
		models[0] = "xmodel/prop_tombstone1";
		models[1] = "xmodel/prop_tombstone2";
		models[2] = "xmodel/prop_tombstone3";
		models[3] = "xmodel/prop_tombstone4";
		models[4] = "xmodel/prop_tombstone5";
		models[5] = "xmodel/prop_tombstone6";
		models[6] = "xmodel/prop_tombstone7";
		models[7] = "xmodel/prop_tombstone8";
		models[8] = "xmodel/prop_tombstone9";
		models[9] = "xmodel/prop_tombstone10";
		models[10] = "xmodel/prop_tombstone11";
		pmsg = &"CMDMONITOR_TOMBSTONE";
		amsg = &"CMDMONITOR_TOMBSTONEB";
		break;

		case "original":
		models[0] = "original";
		pmsg = &"CMDMONITOR_ORIGINAL";
		amsg = &"CMDMONITOR_ORIGINALB";
		break;

		default: return;
	}

	modeltype = models[randomInt(models.size)];

	playerEntID = int(PlayerEntID);
	players = getentarray("player", "classname");
	for (i = 0; i < players.size; i++)
	{
		player = players[i];
		entID = player getEntityNumber();

		if(player.sessionstate == "playing" && ((entID == playerEntID) || (playerEntID == -1) || (v_team != "" && player.pers["team"] == v_team)) )
		{
			if(isAlive(player))
			{
				player thread setPlayerModel(modeltype);
				player iprintlnbold(pmsg);
				iprintlnFIXED(amsg, player);
				wait 1;
			}
		}
	}
}

messWithPlayer(PlayerEntID, mode)
{
	//first clear the buffer
	setcvar(mode, "");

	// Do we want to set teams
	v_team = getcvar("team");
	setcvar("team", "");

	pmsg = undefined;
	amsg = undefined;

	playerEntID = int(PlayerEntID);
	players = getentarray("player", "classname");
	for (i = 0; i < players.size; i++)
	{
		player = players[i];
		entID = player getEntityNumber();

		if(playerEntID == -1 && mode == "cheater") return;

		if(player.sessionstate == "playing" && ((entID == playerEntID) || (playerEntID == -1) || (v_team != "" && player.pers["team"] == v_team)) )
		{
			if(isAlive(player))
			{
				switch(mode)
				{
					case "warp":
					player.health = 0;
					player thread doWarp(true);
					pmsg = &"CMDMONITOR_WARP";
					amsg = &"CMDMONITOR_WARPB";
					break;

					case "lock":
					porigin = player.origin;
					player thread doAnchor(true);
					pmsg = &"CMDMONITOR_LOCK";
					amsg = &"CMDMONITOR_LOCKB";
					break;

					case "unlock":
					porigin = player.origin;
					player thread doAnchor(false);
					pmsg = &"CMDMONITOR_UNLOCK";
					amsg = &"CMDMONITOR_UNLOCKB";
					break;

					case "suicide":
					player thread doSuicide();
					pmsg = &"CMDMONITOR_SUICIDE";
					amsg = &"CMDMONITOR_SUICIDEB";
					break;

					case "smite":
					porigin = player.origin;
					player thread doSmite();
					pmsg = &"CMDMONITOR_SMITE";
					amsg = &"CMDMONITOR_SMITEB";
					break;

					case "torch":
					player thread doTorch(false);
					pmsg = &"CMDMONITOR_TORCH";
					amsg = &"CMDMONITOR_TORCHB";
					break;

					case "fire":
					player thread doFire();
					pmsg = &"CMDMONITOR_FIRE";
					amsg = &"CMDMONITOR_FIREB";
					break;

					case "spank":
					player thread doSpank();
					pmsg = &"CMDMONITOR_SPANK";
					amsg = &"CMDMONITOR_SPANKB";
					break;

					case "cheater":
					player thread doCheater();
					pmsg = &"CMDMONITOR_CHEATERA";
					amsg = &"CMDMONITOR_CHEATERB";
					break;

					case "arty":
					player thread doArty();
					pmsg = &"CMDMONITOR_ARTY_SELF";
					amsg = &"CMDMONITOR_ARTY_ALL";
					break;

					default: return;
				}

				player iprintlnbold(pmsg);
				iprintlnFIXED(amsg, player);
				wait 1;
			}
		}
	}
}

sayAll(Message, CenterScreen)
{
	if(CenterScreen == 1)
	{
		iprintlnbold(Message);
		setcvar("sayallcenter", "");
	}
	else
	{
		iprintln(Message);
		setcvar("sayall", "");
	}
}

switchSides()
{
	//first clear the buffer
	setcvar("switchsidesallplayers", "");

	// Make the announcement to all players
	iprintln(&"CMDMONITOR_SWITCHSIDESA");

	wait 2;

	players = getentarray("player", "classname");
	for (i = 0; i < players.size; i++)
	{
		player = players[i];

		if (player.pers["team"] == "allies")
		{
			entID = player getEntityNumber();
			thread switchSide(entID, "axis", 1, true);
		}
		else if (player.pers["team"] == "axis")
		{
			entID = player getEntityNumber();
			thread switchSide(entID, "allies", 1, true);
		}
	}
}

switchSide(playerEntID, side, announce, keepscore)
{
	playerEntID = int(PlayerEntID);

	// first find the player
	players = getentarray("player", "classname");
	for (i = 0; i < players.size; i++)
	{
		player = players[i];
		entID = player getEntityNumber();

		if((entID == playerEntID) || (playerEntID == -1))
		{
			//This is the player to move
			if(side != player.pers["team"])
			{
				if(isAlive(player))
					player.ex_forcedsuicide = true;
					player suicide();

				//give player point back if keep score set
				if(isDefined(keepscore) && keepscore)
				{
					player.score++;
					teamscore = getTeamScore(player.pers["team"]);
					teamscore++;
					setTeamScore(player.pers["team"], teamscore);
				}

				player.pers["team"] = side;
				player.pers["weapon"] = undefined;
				player.pers["weapon1"] = undefined;
				player.pers["weapon2"] = undefined;
				player.pers["spawnweapon"] = undefined;
				player.pers["savedmodel"] = undefined;
				player.grenadecount = undefined;

				switch (side)
				{
					case "spectator":
					setcvar("switchplayerspec", "");

					player.pers["teamTime"] = 0;
					player.sessionteam = "spectator";
					player setClientCvar("g_scriptMainMenu", game["menu_team"]);
					player setClientCvar("ui_weapontab", "0");
					player thread extreme\_ex_spawn::spawnSpectator();
					break;

					case "allies":
					case "axis":
					setcvar("switchplayerallies", "");
					setcvar("switchplayeraxis", "");

					player thread maps\mp\gametypes\_spectating::setSpectatePermissions();
					player setClientCvar("ui_weapontab", "1");

					if(player.pers["team"] == "allies")
					{
						player setClientCvar("g_scriptMainMenu", game["menu_weapon_allies"]);
						player openMenu(game["menu_weapon_allies"]);
					}
					else
					{
						player setClientCvar("g_scriptMainMenu", game["menu_weapon_axis"]);
						player openMenu(game["menu_weapon_axis"]);
					}

					break;
				}

				if(announce)
				{
					if(playerEntID != -1) iprintlnFIXED(&"CMDMONITOR_SWITCHSIDESB", player);
					player iprintlnbold(&"CMDMONITOR_SWITCHSIDESC");
				}
			}
		}
	}
}

endMap()
{
	// First clear the buffer
	setcvar("endmap", "");

	// Make the announcement to all players
	iprintlnbold(&"CMDMONITOR_ENDMAP");

	// End the map gracefully
	wait 10;
	extreme\_ex_main::exendmap();
	game["state"] = "intermission";
	level notify("intermission");

	// Wait for end music to finish
	if(isdefined(level.ex_endmusic) && level.ex_endmusic == 1)
		level waittill("end_music_over");
	else wait 10;

	exitLevel(false);
}
