/* AMXMODX script.
*
* (c) Copyright 2002-2003, OLO
* Made by Shromilder
* Upgraded and converted to Amx Mod X by Infra
* 
*
* This file is provided as is (no warranties).
*
* -=[ AMX Match Deluxe ]=-
*
* Works with AMXX 0.16 or more only!!
* To use HLTV hability, you need udp module which can be found here: http://amxmod.net/amx-modules.php
* /!\ IF YOU WANT TO USE THAT SCRIPT WITH CS 1.5, YOU HAVE TO SEE amxconst.inc FILE AND UNCOMMENT "#define NO_Sf" AND THEN RECOMPILE ALL YOU PLUGINS (THIS ONE TOO OF COURSE).
* /!\ IF YOU DON'T WANT TO USE UDP MODULE (FOR THE HLTV), YOU HAVE TO COMMENT THE LINE "#define USE_HLTV_SECTION" AND RECOMPILE THE PLUGIN (read AMX readme.txt).
* You also need some config files which are here: http://shromy.free.fr/amx_match_cfgs.zip
* Need admin level "m" and works with CS mod only.
*
* This plug-in allow you to play matchs in MaxRound (warmup, CT, warmup2, Terrorist) or TimeLimit.
* It supports overtimes.
* It loads match server configs before the match and loads FFA server config when it's finished.
* It takes screenshots and can also record demos if you want it.
* You can even use a menu to load your matchs
*
*
* Usage:
* ------
* With commands:
* To load a match: amx_match <CT's clan tag> <T's clan tag> <mrXX or tlXX> <Config filename> [recdemo|rechltv|recboth]
*                  or amx_match2 <mrXX or tlXX> <Config filename> [recdemo|rechltv|recboth]
*                  for example: amx_match SK eoL mr12 ecup recboth
*                               amx_match2 mr12 ecup recboth
*                               These will load a match with MaxRound 12 rule,
*                               using ecup.cfg server config file, and will record demos, HLTV demos & take screenshots
*                               of every players.
*                               P.S: if your clan tag has a space, use "". ex: "SK swe"
* To restart a match: amx_matchrestart
* To stop a match: amx_matchstop or "say /stop"
* To force the start of a match: amx_matchstart or "say /start"
* To swap teams: amx_swapteams
* To tell to the server the HLTV adminpassword: amx_match_hltv <HLTV adminpassword>
*
* The Menu:
* ---------
* First you must configure the menu.
* You must use amx_match_tmenu <matchtype>... to add match type
* and amx_match_cmenu <config name> <config filename> to add match config file
* Concretely, put this into your admin.cfg file:
* 
* amx_match_tmenu "mr12" "mr15" "tl10" "tl15" "tl20" "tl30"
* amx_match_cmenu "EuroCup Config" "ecup"
* amx_match_cmenu "CAL config" "cal"
* 
* When it's done, just use amx_match_menu command and the menu will popup, allowing you to choose your match :)
*
* Cvars:
* -----
* amx_match_endtype: This cvar only works with maxround match type.
*                    0 -> the match is finished after all rounds are played. (set by default)
*                    1 -> the match is finished when a team has won mp_maxrounds+1 rounds.
*		     2 -> there is a vote to play out the match after a team has won maxrounds+1 rounds, unless rounds played is less than 1/5 of total rounds away from end of match).
* amx_match_hostname: 0 -> Does not change host name of server.
*                     1 -> Changes host name of server to "<CT tag> vs. <T tag> | <CFG file> CS Match In Progress" or (amx_match2) "<CFG file> CS Match In Progress". (set by default)
* amx_match_overtime: 0 -> there is no overtime if there's a draw (set by default).
*                     1 -> there is an overtime after a draw match.
* amx_match_overtimecfg: Only use if you set amx_match_overtime to 1
*			 0 -> there is no overtime config (set by default).
*			 1 -> there is an overtime config.
* amx_match_otrounds: only used if you've set amx_match_overtime to 1.
*                     These is the number of rounds which are played during the maxround overtime (3 by default).
* amx_match_password: 0 -> match plugin will not password server before match.
*		      1 -> match plugin will password server before match, use with amx_match_password2. (set by default)
* amx_match_password2: Only use this cvar if you set amx_match_password to 1.
*		       Use this cvar to set the value of password. (default value is "scrim")
* amx_match_playerneed: only used if you've set amx_match_readytype to 1.
*                       This is the number of players which have to be ready (10 by default).
*                       Set it to 4 if you want to play a 2 vs 2 match for example.
* amx_match_readytype: 0 -> only one player of each side must say ready for the match to begin.
*                      1 -> ALL players much say ready in order to start the match (set by default).
*                      2 -> only an AMX admin can start the match by saying /start or amx_matchstart
* amx_match_swaptype: 0 -> teams will not swap after the 1st half.
*                     1 -> teams will swap automatically after the 1st half (set by default).
* amx_match_screenshot: 0 -> no screenshot at all.
*                       1 -> screenshot of the score is taken at the end of each half (set by default).
*			2 -> screenshot of score, including userids, is taken at the end of each half.
* amx_match_screenshot2: 0 -> no screenshot if match is stopped using amx_match_stop or "say /stop" .
*                       1 -> screenshot of the score is taken at the end of each half, even if match is stopped using amx_match_stop or "say /stop" (set by default).
* amx_match_shield: This cvar only works with CS 1.6.
*                    0 -> you can't use the shield during the match (set by default).
*                    1 -> you can use the shield during the match.
* amx_match_shield2: This cvar only works with CS 1.6.
*                    0 -> unrestricts shield after match.
*                    1 -> (re)restricts shield after match (set by default).
* amx_match_showscore: 0 -> Score will not be show after each round .
*                      1 -> Score is shown after each round (set by default).
*
*
* For MaxRound:
* -------------
* When a match is loaded, warmup time begins and match server config is loaded.
* Then, when one player of each team says 'ready', the 1st half of the match starts.
* When the maxround limit is reached, it goes back to warmup.
* Then, when both teams say 'ready' again, the 2nd half starts.
* When the game is finished, it tells you which team has won the match and loads FFA server config file.
* It also records both side demos if you want it.
*
* For TimeLimit:
* --------------
* When a match is loaded, warmup time begins and match server config is loaded.
* Then, when one player of each team says 'ready', it starts the match, wait the end of the time limit, and reload the map.
*
* Config Files:
* -------------
* There are some special config files:
*    - default.cfg is a server config file which is executed before every games. 
*      It can be used to disable some plug-ins like stats or set some default cvars
*      which will erase your match config file cvars
*    - ffa.cfg is the server config file which is executed at the end of the match
*      in order that your server can be played by everyone =)
*    - cfglist.txt is the list of your available config file. 
*      It is shown when you type a wrong config file when you're trying to load a match.
*      So if you don't want to remember the name of all your server config file,
*      just add them into this file =)
*
* All config files need to be into addons/amxx/custom/leagues/ folder
* and have .cfg extention (/!\ case sensitive so .CFG will not work!!)
* 
* Note: for config files in AMX 0.20, see note below
* 
*
* HLTV help:
* ----------
* /!\ IF YOU DON'T WANT TO USE UDP MODULE, YOU HAVE TO COMMENT THE LINE "#define USE_HLTV_SECTION" AND RECOMPILE THE PLUGIN (read AMX readme.txt).
* To use HLTV hability, you need udp module. If you use AMX 0.9.4 or more, you can find it here: http://amxmod.net/amx-modules.php
* Then, you have to configure your HLTV:
* In order to be able to control the HLTV, you must know it rcon. HLTV rcon can be set using "adminpassword" command in the HLTV console
* So how to get all work?...
* Load your HLTV.
* Set the HLTV adminpassword by using adminpassword <rcon> command in the HLTV console. Then connect it to your server using "connect ip:port" in the console.
* Load your match on the server using amx_match or amx_match2
* Then use amx_match_hltv <adminpassword> command in the server in order to tell to the server what's the HLTV adminpassword.
* If your HLTV is already connected, you should see him say " HLTV correctly configured =)".
* If you don't see that, it means that the HLTV adminpassword hasn't been correctly configured.
* If your HLTV isn't connected, it will try the adminpassword that you've set when it will enter the server.
*
*
* 
* * Note for AMXX 0.20:
* * ------------------
* * All config files need to be into "addons/amxx/config/leagues/" instead of "addons/amxx/custom/leagues/" the folder
* * and have .cfg extention (/!\ case sensitive so .CFG will not work!!)
* *
* * You will also need to recompile the plugin using the AMXX 0.20 compiler.
* *
*
* Changelog:
* ----------
*
*  - 2.01: fixed string bug in back_to_ffa().
*          added match_restart to amx_match_menu.
*
* ----------
*
*  - 2.00: (major release)
*	   added support for restarting the match.
*	   added support for show activity, if on.
*	   added cvars that were not previously in the menu, to the menu.
*	    	---
*	   changed behavior of amx_match_password2 and amx_match_password for more fluid execution and easier to remember.
*	    	---
*	   fixed version number bug in register plugin command.
*	   fixed bug where hostname would not be changed back to original hostname after match, if changed by match plugin.
*	   fixed bug where the vote, for playout, would happen even if there were 3 or 4 (total-rounds divided by 5 minus 1) or (30/5) - 1 = 5) rounds left in the match. version 2  ^_^
*	   fixed bug where vote to play out would happen even if end_type = 0
* 	   fixed "un/passwording" of server after match, used same method as hostname fix to add/remove password at end of match.
*
* ----------
*
*  - 1.81: fixed readme file to reflect changes that will be in AMXX 0.20.
*          added amx_match_password and amx_match_password2 cvars
*          added support for "passwording" the server.
*  - 1.80: fixed bug where overtime config file was execed even in regular match time.
*  - 1.79: added support for overtime config files.
*	   added amx_match_overtimecfg to turn [on|off] overtime config files.
*  - 1.78: fixed typos.
*  - 1.77: better code.
*	   fixed bug where the vote, for playout, would happen even if there were 3 or 4 rounds left in the match.
*  - 1.76: added amx_match_shield2 cvar, unrestricts or (re)restricts shield after the match
*  - 1.75: added compatibility with AmxModX 0.16
*  - 1.74: added the ability to vote to play out the match. (Use amx_match_endtype)
*  - 1.73: added amx_match_screenshot2 to take end-half screenshots even if match is stopped using amx_match_stop or "say /stop"
*  - 1.72: added amx_match_hostname cvar to have the server change it's name based on who is playing.
*          added "say /stop" command in addition to amx_match_stop.
*  - 1.71: added the possibility to change match settings in the menu (it just change them, it doesn't save them anywhere so after a changelevel, all is lost).
*          added amx_match_shield cvar to allow or not the use of the shield during the match.
*
* ----------
*
*  - 1.70: trying to make it 1.6 compatible:
*             stoprecord changed to stoprecording if using 1.6
*             cancelselect used for the screenshots if using 1.6
*             changed the hud msgs for 1.6 (readylist not yet done)
*             score msg is a hud msg if using 1.6
*  - 1.69: better code.
*  - 1.68: if a player is already recording his own demo, then it's stopped to record a correct one.
*          changed the score display.
*          some bugs fixed & small changes.
*  - 1.67: the cvar amx_match_showids has been changed to amx_match_screenshot which allow you to choose if you want to take a screenshot or not.
*          added overtime support with amx_match_overtime & amx_match_otrounds cvars. To respect clantags, the team which must start the overtime as CT
*          must be the team which started the match as CT (if you don't use clantags, there's no problem).
*  - 1.66: changed showids. it now takes a screenshot of 'status' in the console
*  - 1.65: added clantag recognition in the menu
*  - 1.62: added the possibility to show score after each rounds using amx_match_showscore cvar (default is off)
*  - 1.61: added the option to see wonid using amx_match_showids cvar (default is off)
*  - 1.60: added the ability to use UDP module or not (so in fact, use hltv feature or not)
*  - 1.54: corrected demo not stopping when the match is stoped.
*          added amx_matchstart command which force the start of a match
*  - 1.52: modified the menu, added HLTV help
*  - 1.51: fixed HLTV record demo & some other bugs (thx to en3my for HLTV)
*  - 1.50: HLTV demos \o/ recdemo will record players demos. rechltv will record HLTV demos. recboth will record HLTV and players demos.
*  - 1.46: added amx_match_swaptype
*  - 1.45: added admin start with say & menu (amx_match_readytype 2)
*  - 1.44: some modifications. HLTV soon!
*  - 1.43: removed a possible bug (warmup msg during match)
*  - 1.42: corrected swapteam command: now it check if the user using it is an admin (stupid me :/)
*  - 1.41: removed an horrible bug (changing map before the match) >:[
*  - 1.40: added amx_match_menu which allow to load a match with a menu =]
*  - 1.30: added amx_match_readytype & amx_match_playerneed cvars which allow that everyone must say ready
*  - 1.21: added notready feature which allow a team or a player to remove their ready flag
*  - 1.20: added amx_match_endtype cvar & fixed some bugs like recording demos with voicecom on.
*  - 1.10: added amx_match2 and amx_swapteams command, added screenshots feature
*  - 1.02: other bugs removed, less warmup msg flood
*  - 1.01: added log commands, small bug removed
*  - 1.00: first release
*
* To do:
* - Fix bug in clan tags finder function or add a messagemode to enter clan tags.
* - Add redmist's hltv code for use with socket module not UDP module
* - Fix bugs in cointoss 
* - Support for infinite overtimes if match is tied 
* - Allow double maps match:
*    amx_match3 <CT's clan tag> <T's clan tag> <mrXX or tlXX> <Config filename> <second map> [recdemo|rechltv|recboth]
*    amx_match4 <mrXX or tlXX> <Config filename> <second map> [recdemo|rechltv|recboth]
*
*/

// COMMENT THIS LINE IF YOU DON'T WANT TO USE UDP MODULE (add // in front of it) AND RECOMPILE THE PLUGIN
//#define USE_HLTV_SECTION

#include <amxmodx>
#include <amxmisc>
#include <string>
#include <cstrike>


// PTB...sort teams
#define ACCESS_PTB ADMIN_RCON


#if defined(USE_HLTV_SECTION)
	#include <udp>
#endif

#define MAX_VARS 64 // maximum variables for the menu
#define MAX_TAGS 20 // Maximum of tags in the menu
#define MIN_TAG_LENGTH 2 // The minimum length of a tag (number of same letters)

//Custom directory for the config files
new customdir[64]

// Configs
new cmdtype // 1= amx_match 2= amx_match2
new match_inprogress = 0 // 0= no match, 1= warmup, 2= 1st half, 3= warmup2, 4= 2nd half
new teams_ready = 0 // 0=none, 4= CT ready, 8= T ready
new ready_type = 0 // 0= only one, 1= all players, 2= admin starts it
new swap_type = 0 // 1= teams are swapped after first half, 0= they don't
new players_ready[33] // list of userid of players which are ready
new ready_CT_names[256] //list of name of CT which are ready
new ready_T_names[256] //list of name of T which are ready
new end_type = 0// 0= match ends after all rounds played, 1 = match ends after mp_maxrounds+1 rounds won, 2 = there is a vote to play out the match after mp_maxrounds+1 rounds won.
new ct_score[2],t_score[2] // scores
new clanCT[32],clanT[32],cfgfile[32],matchtype1,matchtype2,recdemo // matchtype1= 1 -> mr, 2 -> tl. recdemo: 0: none, 1: player, 2: hltv, 3: both
new checktype[32], checktype2[32]
new in_overtime  = 0 // 1: we are playing overtime, 0: no
new in_overtime2 = 0 // 1: we are in overtime now, 0: no
new overtime_score = 0 // to remember the score of the match
new server_pass[32]
new logfilename[32]
new match_command[256]
#if defined(NO_STEAM)
new StatusTextid // used for the score msg
#else
new shield_type = 1  // 1 = shield allowed, 0 = shield not allowed
new shield_type2 = 1 // 1 = (re)restricts shield, 0 = unrestricts shield after match
#endif

// Reset server...
new old_serverpass[32]
new old_servername[64]

// Vote
new g_isVoting = 0 // this is the flag telling u if it's already voting now
new option[10]

new HLTV_id,HLTV_rcon[32], HLTV_rconid[16]
#if defined(USE_HLTV_SECTION)
new HLTV_iaddress = -1
#endif

// MENU
new typelist[MAX_VARS][32] // list of match types
new typelistpos = 0 // position of last type added
new confignamelist[MAX_VARS][32] // list of match configs names
new configfilelist[MAX_VARS][32] // list of match configs filenames
new configlistpos = 0 // position of last config added
new menu_position[33] // position in menu of each players
new menu_selections[33][5] // commands selected by users
new CT_tags[MAX_TAGS][32]
new T_tags[MAX_TAGS][32]
new CT_tagspos = 0, T_tagspos = 0
//new message_Tag[192]


public set_team(id,team) {
	user_kill(id,1)
	client_cmd(id,"setinfo _vgui_menus 0;wait;wait;wait;chooseteam;wait;wait;wait;menuselect %d;wait;wait;wait;menuselect 5;wait;wait;wait;menuselect 0;wait;wait;wait;menuselect 5",(team == 1) ? 1 : 2)
	client_cmd(id,"wait; wait; wait; wait; wait; wait; slot10; wait; wait; wait; wait; wait; slot10")

	return PLUGIN_CONTINUE
}

public swap()
{
	new playersCT[32]
	new playersT[32]
	new nbrCT,nbrT
	get_players(playersCT,nbrCT,"e","CT")
	get_players(playersT,nbrT,"e","TERRORIST")

	for(new i = 0; i < nbrCT; i++) {
			set_team(playersCT[i], 1)
	}

	for(new i = 0; i < nbrT; i++) {
			set_team(playersT[i], 2)
	}

	return PLUGIN_CONTINUE
}

public swap_teams(id,level,cid)
{
	if (!cmd_access(id,level,cid,1)) {
		return PLUGIN_HANDLED
	}
	
	new name[32]
	get_user_name(id,name,31)
	switch(get_cvar_num("amx_show_activity")) 
	{	
  		case 2: client_print(0,print_chat,"ADMIN %s: switched the teams",name)
  		case 1: client_print(0,print_chat,"ADMIN: switched the teams")
	}
	
	client_print(0,print_chat,"* [AMX] Switching teams!...")
	set_cvar_string("mp_limitteams","0")
	set_cvar_string("mp_autoteambalance","0")
	restart_round("1")
	swap()
	restart_round("1")
	return PLUGIN_CONTINUE
}

public restart_round(time[])
{
	server_cmd("sv_restartround %s",time)
	return PLUGIN_CONTINUE
}

public voice_enable(time[])
{
	server_cmd("sv_voiceenable %s",time)
	return PLUGIN_CONTINUE
}

#if defined(USE_HLTV_SECTION)
public hltv_help(idt[])
{
	new ident = str_to_num(idt)
	console_print(ident,"* In order to properly record HLTV demos, an HLTV must be connected on the server before each half and it must have an adminpassword (use adminpassword <adminpassword> in the HLTV console in order to set it).^n* Now you have to type the adminpassword of the HLTV here.^n* Use amx_match_hltv <adminpassword of the HLTV> to set it.^n* If you don't do it, HLTV demos will never be recorded.")
	return PLUGIN_CONTINUE
}

public set_hltv_rcon(id,level,cid)
{
	if (match_inprogress > 0) {
		if (recdemo > 1) {
			if (!cmd_access(id,level,cid,2)) {
				return PLUGIN_HANDLED
			}
			read_argv(1,HLTV_rcon,31)
			console_print(id,"* HLTV adminpassword set to ^"%s^"",HLTV_rcon)
			if (HLTV_id == 0) {
				console_print(id,"* No HLTV currently connected to the server")
				HLTV_iaddress = -1
				HLTV_rconid[0] = 0
			}
			else {
				new HLTV_ip[32]
				get_user_ip(HLTV_id,HLTV_ip,31)
				console_print(id,"* Testing HLTV (%s)...",HLTV_ip)
				hltv_rcon_cmd("say HLTV correctly configured =)",id)
				console_print(id,"* If the HLTV said something, it's ok. Otherwise, the adminpassword of the HLTV isn't correct :/")
			}
		}
		else
			console_print(id,"* HLTV demos are not recorded during this match.")
	}
	else
		console_print(id,"* A match must be loaded before setting HLTV adminpassword.")
	return PLUGIN_CONTINUE
}

public hltv_rcon_cmd(cmd[],id) {
	if (HLTV_id == 0) {
		console_print(id,"* No HLTV currently connected to the server")
		HLTV_iaddress = -1
		HLTV_rconid[0] = 0
	}
	else {
		new temp[32], temp2[64]
		setc(temp,4,0xff)
		copy(temp[4],31,"challenge rcon")
		setc(temp[18],1,'^n')
		new ok = query_udp_msg(HLTV_iaddress,temp2,63,temp)
		if (ok) {
			copy(HLTV_rconid,12,temp2[15])
			replace(HLTV_rconid,15,"^n","")
			new realcmd[1024]
			setc(realcmd,4,0xff)
			format(realcmd[4],1023,"rcon %s ^"%s^" %s^n",HLTV_rconid,HLTV_rcon,cmd)
			send_udp_msg(HLTV_iaddress,realcmd)
		}
	}
	return PLUGIN_CONTINUE
}
#endif

public rec_demos()
{
	new demoname[256],curmap[32],time_date[32]
	get_mapname(curmap,31)
	get_time("%Y.%m.%d-%H.%M.%S",time_date,31)
	#if defined(USE_HLTV_SECTION)
	new cmd[512]
	if (recdemo > 1) { // HLTV demos
		if (equal(HLTV_rconid,""))
			hltv_rcon_cmd("",0)
		if (!equal(HLTV_rconid,"")) {
			if (cmdtype == 1)
				format(demoname,255,"%s(%s)_%s(%s)_%s_HLTV_%s.dem",clanCT,(match_inprogress==1) ? "CT" : "T",clanT,(match_inprogress==1) ? "T" : "CT",curmap,time_date)
			else
				format(demoname,255,"%s_part%i_HLTV_%s.dem",curmap,(match_inprogress==1) ? 1 : 2,time_date)
			// Remove bad strings before recording
			while(replace(demoname,255,"/","-")) {}
			while(replace(demoname,255,"\","-")) {}
			while(replace(demoname,255,":","-")) {}
			while(replace(demoname,255,"*","-")) {}
			while(replace(demoname,255,"?","-")) {}
			while(replace(demoname,255,">","-")) {}
			while(replace(demoname,255,"<","-")) {}
			while(replace(demoname,255,"|","-")) {}
			format(cmd,511,"say [AMX MATCH] Recording a demo : %s",demoname)
			hltv_rcon_cmd(cmd,0)
			format(cmd,511,"record %s",demoname)
			hltv_rcon_cmd(cmd,0)
		}
		else
			client_print(0,print_chat,"* /!\ HLTV DEMO WILL NOT BE RECORDED. NO HLTV OR WRONG ADMINPASSWORD /!\ ")
	}
	#endif
	if ((recdemo == 1)||(recdemo == 3)) { // Player's demos
		new players[32],playername[32],playerteam[16]
		new nbr,i
		get_players(players,nbr)
		for(i=0; i<nbr; i++) {
			get_user_team(players[i],playerteam,15)
			if (equal(playerteam,"CT")) {
				get_user_name(players[i],playername,31)
				if (cmdtype == 1)
					format(demoname,255,"%s(CT)_%s(T)_%s_ineyes_%s_%s.dem",(match_inprogress==1) ? clanCT : clanT,(match_inprogress==1) ? clanT : clanCT,curmap,playername,time_date)
				else
					format(demoname,255,"%s_%s(CT)_%s.dem",playername,curmap,time_date)
			}
			else {
				if (equal(playerteam,"TERRORIST")) {
					get_user_name(players[i],playername,31)
					if (cmdtype == 1)
						format(demoname,255,"%s(T)_%s(CT)_%s_ineyes_%s_%s.dem",(match_inprogress==1) ? clanT : clanCT,(match_inprogress==1) ? clanCT : clanT,curmap,playername,time_date)
					else
						format(demoname,255,"%s_%s(T)_%s.dem",playername,curmap,time_date)
				}
			}
			if ((equal(playerteam,"CT"))||(equal(playerteam,"TERRORIST"))) {
				// Removes bad strings before recording
				while(replace(demoname,255,"/","-")) {}
				while(replace(demoname,255,"\","-")) {}
				while(replace(demoname,255,":","-")) {}
				while(replace(demoname,255,"*","-")) {}
				while(replace(demoname,255,"?","-")) {}
				while(replace(demoname,255,">","-")) {}
				while(replace(demoname,255,"<","-")) {}
				while(replace(demoname,255,"|","-")) {}
				client_cmd(players[i],"stop")
				#if defined(NO_STEAM)
				client_cmd(players[i],"record ^"%s^"",demoname)
				#else
				client_cmd(players[i],"record ^"%s.a^"",demoname)
				#endif
				client_print(players[i],print_chat,"* [AMX MATCH] Recording a demo on your PC: %s",demoname)
			}
		}
	}
	return PLUGIN_CONTINUE
}

public stop_demo()
{
	if ((recdemo == 1)||(recdemo == 3)) { // Player's demos
		new players[32],playerteam[16]
		new nbr,i
		get_players(players,nbr)
		for(i=0; i<nbr; i++) {
			get_user_team(players[i],playerteam,15)
			if ((equal(playerteam,"CT"))||(equal(playerteam,"TERRORIST"))) {
				client_cmd(players[i],"stop")
				client_print(players[i],print_chat,"* [AMX MATCH] Stopped recording the demo.")
			}
		}
	}
	#if defined(USE_HLTV_SECTION)
	if (recdemo > 1) { // HLTV demos
		if (equal(HLTV_rconid,""))
			hltv_rcon_cmd("",0)
		if (!equal(HLTV_rconid,"")) {
			#if defined(NO_STEAM)
			hltv_rcon_cmd("stoprecord",0)
			#else
			hltv_rcon_cmd("stoprecording",0)
			#endif
			hltv_rcon_cmd("say [AMX MATCH] Stopped recording the demo.",0)
		}
		else
			client_print(0,print_chat,"* /!\ HLTV DEMO NOT STOPPED. NO HLTV OR WRONG ADMINPASSWORD /!\ ")
	}
	#endif
	return PLUGIN_CONTINUE
}

public screenshot()
{
	new players[32]
	new nbr
	get_players(players,nbr)
	for(new i=0; i<nbr; i++) {
		if (players[i] != HLTV_id)
			client_cmd(players[i],"snapshot")
	}
	return PLUGIN_CONTINUE
}

public show_scoreboard()
{
	client_cmd(0,"+showscores")
	return PLUGIN_CONTINUE
}

public remove_scoreboard()
{
	client_cmd(0,"-showscores")
	return PLUGIN_CONTINUE
}

public toggle_console()
{
	new players[32]
	new nbr
	get_players(players,nbr)
	for(new i=0; i<nbr; i++) {
		if (players[i] != HLTV_id)
			client_cmd(players[i],"toggleconsole")
	}
	return PLUGIN_CONTINUE
}

public remove_console()
{
	new players[32]
	new nbr
	get_players(players,nbr)
	#if defined(NO_STEAM)
	for(new i=0; i<nbr; i++) {
		if (players[i] != HLTV_id)
			client_cmd(players[i],"toggleconsole")
	}
	#else
	for(new i=0; i<nbr; i++) {
		if (players[i] != HLTV_id)
			client_cmd(players[i],"cancelselect")
	}
	#endif
	return PLUGIN_CONTINUE
}

public take_screenshot()
{
	new show = get_cvar_num("amx_match_screenshot")
	if (show > 0) {
		if (show == 2) {
			client_print(0,print_chat,"* [AMX MATCH] Taking screenshots... Smile!")
			client_cmd(0,"status")
			set_task(0.5,"toggle_console")
			set_task(1.0,"screenshot")
			set_task(1.5,"remove_console")
			set_task(2.0,"show_scoreboard")
			set_task(2.5,"screenshot")
			set_task(5.0,"remove_scoreboard")
		}
		else {
			client_cmd(0,"+showscores")
			client_print(0,print_chat,"* [AMX MATCH] Taking a screenshot... Smile!")
			set_task(1.5,"screenshot")
			set_task(4.0,"remove_scoreboard")
		}
	}
	return PLUGIN_CONTINUE
}

public start_match(id,level,cid)
{
	new command[16]

	read_argv(0,command,15)
	if (equal(command,"amx_match"))
		cmdtype = 1
	else
		cmdtype = 2
	if (cmdtype == 1) {
		if (!cmd_access(id,level,cid,5)) {
			return PLUGIN_HANDLED
		}
	}
	else {
		if (!cmd_access(id,level,cid,3)) {
			return PLUGIN_HANDLED
		}
	}
	if (match_inprogress == 0) {
		// checks config file
		if (cmdtype == 1)
			read_argv(4,cfgfile,31)
		else
			read_argv(2,cfgfile,31)
		new cfgfile2[64]
		new cfgfile3[64]
		format(cfgfile2,63,"%s/leagues/%s.cfg",customdir, cfgfile)
		if (!(file_exists(cfgfile2))) {
			console_print(id,"* This config file doesn't exist.")
			
			format(cfgfile3,63,"%s/leagues/cfglist.txt",customdir)
			if (file_exists(cfgfile3)) {
				new line = 0, txtsize = 0, message[192]
				console_print(id,"* Here is the list of available config files:")
				while((line = read_file(cfgfile3,line,message,191,txtsize)) != 0)
					console_print(id,"*   %s",message)
			}
			return PLUGIN_HANDLED
		}
		
		// checks match type
		if (cmdtype == 1)
			read_argv(3,checktype,31)
		else
			read_argv(1,checktype,31)
		if (equali(checktype,"mr",2))
			matchtype1 = 1
		else {
			if (equali(checktype,"tl",2))
				matchtype1 = 2
		}
		
		checktype2 = checktype
		
		new i,length
		length=strlen(checktype)
		for(i=0;i<(length-2);i++)
			checktype[i] = checktype[i+2]
		checktype[i]= 0
		if (!( matchtype1 && str_to_num(checktype) )) {
			console_print(id,"* That type of match isn't supported :/^n* Type supported is maxround: mrXX (mr12 for example) or timelimit: tlXX (tl30 for example).")
			return PLUGIN_HANDLED
		}
		
		
		// starting the match
		match_inprogress = 1
		in_overtime2 = 0
		ready_type = get_cvar_num("amx_match_readytype")
		end_type = get_cvar_num("amx_match_endtype")
		swap_type = get_cvar_num("amx_match_swaptype")
		#if !defined(NO_STEAM)
		shield_type = get_cvar_num("amx_match_shield")
		if ( shield_type == 0) {
			server_cmd("amx_restrict on shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been disabled")
		}
		else
		{
			server_cmd("amx_restrict off shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been enabled")
		}
		#endif
		
		new pass
		pass = get_cvar_num("amx_match_password")
		if(pass == 1)
		{
			
			get_cvar_string("sv_password", old_serverpass, 31)
			
			get_cvar_string("amx_match_password2", server_pass, 31)

			server_cmd("sv_password ^"%s^"", server_pass)
			client_print(0,print_chat,"* [AMX MATCH] Server password has been set by the plugin")
			client_print(id,print_chat,"* [AMX MATCH] Server password has been set to: |  %s  |", server_pass)
		}

		ready_CT_names = "None."
		ready_T_names = "None."
		players_ready[0] = 0
		if (cmdtype == 1) {
			read_argv(1,clanCT,31)
			read_argv(2,clanT,31)
		}
		else {
			format(clanCT,31,"CT")
			format(clanT,31,"T")
		}
		
		new hostname_type
		new cfgfile_upr[32];
		hostname_type = get_cvar_num("amx_match_hostname")
		if(hostname_type == 1)
		{
			cfgfile_upr = cfgfile
			strtoupper(cfgfile_upr)
			
			get_cvar_string("hostname", old_servername, 63)
			
			if (cmdtype == 1)
			{
				server_cmd("hostname ^"%s vs. %s | %s CS Match In Progress^"", clanCT, clanT, cfgfile_upr)
			}
			else
			{
				server_cmd("hostname ^"%s CS Match In Progress^"", cfgfile_upr)	
			}
		}		
		
		new demo[8]
		if (cmdtype == 1)
			read_argv(5,demo,7)
		else
			read_argv(3,demo,7)
		new temp[16]
		temp[0] = 0
		if (equali(demo,"recdemo")) {
			recdemo = 1
			temp = "Players "
		}
		#if defined(USE_HLTV_SECTION)
		if (equali(demo,"rechltv")) {
			recdemo = 2
			num_to_str(id,temp,15)
			set_task(1.0,"hltv_help",0,temp,15)
			temp = "HLTV "
		}
		if (equali(demo,"recboth")) {
			recdemo = 3
			num_to_str(id,temp,15)
			set_task(1.0,"hltv_help",0,temp,15)
			temp = "Players & HLTV "
		}
		#endif
		
		matchtype2 = str_to_num(checktype)
		
		// log
		new name[32],authid[16], userid		
		new hudmsg[200]
		
		get_user_name(id,name,31)
		get_user_authid(id,authid,15)
		userid = get_user_userid(id)
		if (cmdtype == 1)  // For log and show activity
			format(hudmsg,199,"%s vs %s. %s%i. %s.cfg. recdemo: %i",clanCT,clanT,(matchtype1==1)?"mr":"tl",matchtype2,cfgfile,recdemo)
		else
			format(hudmsg,199,"%s%i. %s.cfg. recdemo: %i",(matchtype1==1)?"mr":"tl",matchtype2,cfgfile,recdemo)
		
		log_amx(logfilename,"^"%s<%d><%s><>^" started a match ^"%s^"",name,userid,authid,hudmsg)
		
		
		// Show activity, if on
		get_user_name(id,name,31)
		switch(get_cvar_num("amx_show_activity")) 
		{	
	  		case 2: client_print(0,print_chat,"ADMIN %s: started a match ^"%s^"", name, hudmsg)
	  		case 1: client_print(0,print_chat,"ADMIN: started a match ^"%s^"", hudmsg)
		}
		
		
		
		if (cmdtype == 1)
			format(hudmsg,199,"--[ Match loaded: %s(CT) vs %s(T) ]--^n^nMatch type is %s %i (%s config)",clanCT,clanT,(matchtype1==1)?"MaxRound":"TimeLimit",matchtype2,cfgfile)
		else
			format(hudmsg,199,"--[ Match loaded ]--^n^nMatch type is %s %i (%s config)",(matchtype1==1)?"MaxRound":"TimeLimit",matchtype2,cfgfile)
		set_hudmessage(0, 255, 255, -1.0, 0.30, 0, 2.0, 6.0, 0.8, 0.8, 2)
		show_hudmessage(0,hudmsg)	
		
		format(hudmsg,199,"%s%s^nEntering warmup session...",temp,(recdemo>0) ? "demos autorecorded" : "")
		set_hudmessage(0, 255, 255, -1.0, 0.38, 0, 2.0, 6.0, 0.8, 0.8, 5)
		show_hudmessage(0,hudmsg)
		
		start_warmup()
	}
	else
	{
		console_print(id,"* Match already in progress.")
		client_print(id,print_chat,"* [AMX MATCH] Match already in progress")
	}
	return PLUGIN_CONTINUE
}

public restart_match(id,level)
{
	if (!access(id,level)) {
		console_print(id,"[AMX] You have not access to that command.")
		console_print(id,"* You have no access to that command.")
		return PLUGIN_HANDLED
	}
	if (match_inprogress > 0){
		new name[32]
		
		get_user_name(id,name,31)
		
		set_hudmessage(208, 12, 2, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
		show_hudmessage(0,"--[ %s HAS RESTARTED THE MATCH ]--^n^nGreat fun, eh? ;-)",name)
		server_print("* Match stopped.")
		client_print(0,print_chat,"* [AMX MATCH] Executing FFA config file (ffa.cfg)")
		
		server_cmd("exec ^"%s/leagues/ffa.cfg^"", customdir)
		
		// Format string for match restart
		
		// recdemo = 0: none
		// recdemo = 1: player
		// recdemo = 2: hltv
		// recdemo = 3: both

		new record_str[32]

		if(recdemo == 0) // none
		{
			record_str = ""
		}
		else if (recdemo == 1) //player
		{
			record_str = "recdemo"
		}
		else if (recdemo == 2) //hltv
		{
			record_str = "rechltv"
		}
		else if (recdemo == 3) // both
		{
			record_str = "recboth"
		}
	
	
		new matchtype_str[32]
		
		if( matchtype1 == 1)
		{
			format(matchtype_str, 31, "mr%d", matchtype2)
		}
		else
		{
			format(matchtype_str, 31, "mr%d", matchtype2)
		}
		
				
		if(cmdtype == 1)
		{
			// amx_match <CT's clan tag> <T's clan tag> <mrXX or tlXX> <Config filename> [recdemo|rechltv|recboth]
			format(match_command, 255, "amx_match %s %s %s %s %s", clanCT, clanT, matchtype_str, cfgfile, record_str)
		}
		else
		{
			// amx_match <mrXX or tlXX> <Config filename> [recdemo|rechltv|recboth]
			format(match_command, 255, "amx_match2 %s %s %s", matchtype_str, cfgfile, record_str)
		}
		
		
		// Uninitialize
		if (recdemo > 0)
			stop_demo()
		match_inprogress = 0
		in_overtime = 0
		overtime_score = 0
		ready_type = 0
		end_type = 0
		swap_type = 0
		ready_CT_names = "None."
		ready_T_names = "None."
		players_ready[0] = 0
		teams_ready = 0
		clanCT[0] = 0
		clanT[0] = 0
		cfgfile[0] = 0
		matchtype1 = 0
		matchtype2 = 0
		recdemo = 0
		ct_score[0] = 0
		t_score[0] = 0
		ct_score[1] = 0
		t_score[1] = 0
		HLTV_rconid[0] = 0
		HLTV_rcon[0] = 0
		
		// (Re)set Hostname
		new hostname_type
		hostname_type = get_cvar_num("amx_match_hostname")
		if(hostname_type == 1)
		{
			set_cvar_string("hostname", old_servername)
		}
		
		// (Re)set Password
		new pass
		pass = get_cvar_num("amx_match_password")
		if(pass == 1)
		{
			set_cvar_string("sv_password", old_serverpass)
		}
		
		#if !defined(NO_STEAM)
		shield_type2 = get_cvar_num("amx_match_shield2")
		if ( shield_type2 == 0) {
			server_cmd("amx_restrict off shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been unrestricted")
		}
		else
		{
			server_cmd("amx_restrict on shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been restricted")
		}
		#endif
		remove_task(1337)
		// log
		new authid[16], userid
		get_user_authid(id,authid,15)
		userid = get_user_userid(id)
		log_amx(logfilename,"^"%s<%d><%s><>^" restarted a match",name,userid,authid)
		
		// Show activity, if on
		get_user_name(id,name,31)
		switch(get_cvar_num("amx_show_activity")) 
		{	
	  		case 2: client_print(0,print_chat,"ADMIN %s: restart the match", name)
	  		case 1: client_print(0,print_chat,"ADMIN: restarted the match")
		}			
		
		server_cmd("%s", match_command)
	}
	else
	{
		client_print(id,print_chat,"* [AMX MATCH] No match to restart")
		console_print(id,"* There's no match to restart.")
	}
	return PLUGIN_CONTINUE
}

public stop_match(id,level)
{
	if (!access(id,level)) {
		console_print(id,"[AMX] You have not access to that command.")
		console_print(id,"* You have no access to that command.")
		return PLUGIN_HANDLED
	}
	if (match_inprogress > 0){
		new name[32]
		new screenshot2
		
		get_user_name(id,name,31)
		
		screenshot2 = get_cvar_num("amx_match_screenshot2")
		if(screenshot2 == 1)
		{
			set_hudmessage(208, 12, 2, 0.05, -0.25, 0, 6.0, 12.0, 0.1, 0.2, 4)
			show_hudmessage(0,"--[ MATCH WAS ENDED EARLY BY %s ]--", name)
		
			show_the_score()
			take_screenshot()
		}
		
		set_hudmessage(208, 12, 2, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
		show_hudmessage(0,"--[ %s HAS STOPPED THE MATCH ]--^n^nLet's go back to FFA ;-)",name)
		server_print("* Match stopped.")
		client_print(0,print_chat,"* [AMX MATCH] Executing FFA config file (ffa.cfg)")
		
		server_cmd("exec ^"%s/leagues/ffa.cfg^"", customdir)
		if (recdemo > 0)
			stop_demo()
		match_inprogress = 0
		in_overtime = 0
		overtime_score = 0
		ready_type = 0
		end_type = 0
		swap_type = 0
		ready_CT_names = "None."
		ready_T_names = "None."
		players_ready[0] = 0
		teams_ready = 0
		clanCT[0] = 0
		clanT[0] = 0
		cfgfile[0] = 0
		matchtype1 = 0
		matchtype2 = 0
		recdemo = 0
		ct_score[0] = 0
		t_score[0] = 0
		ct_score[1] = 0
		t_score[1] = 0
		HLTV_rconid[0] = 0
		HLTV_rcon[0] = 0
		
		// (Re)set Hostname
		new hostname_type
		hostname_type = get_cvar_num("amx_match_hostname")
		if(hostname_type == 1)
		{
			set_cvar_string("hostname", old_servername)
		}
		
		// (Re)set Password
		new pass
		pass = get_cvar_num("amx_match_password")
		if(pass == 1)
		{
			set_cvar_string("sv_password", old_serverpass)
		}
		
		#if !defined(NO_STEAM)
		shield_type2 = get_cvar_num("amx_match_shield2")
		if ( shield_type2 == 0) {
			server_cmd("amx_restrict off shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been unrestricted")
		}
		else
		{
			server_cmd("amx_restrict on shield")
			client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been restricted")
		}
		#endif
		remove_task(1337)
		// log
		new authid[16], userid
		get_user_authid(id,authid,15)
		userid = get_user_userid(id)
		log_amx(logfilename,"^"%s<%d><%s><>^" stopped a match",name,userid,authid)
		
		
		// Show activity, if on
		get_user_name(id,name,31)
		switch(get_cvar_num("amx_show_activity")) 
		{	
	  		case 2: client_print(0,print_chat,"ADMIN %s: stopped the match", name)
	  		case 1: client_print(0,print_chat,"ADMIN: stopped the match")
		}
		
	}
	else
	{
		client_print(id,print_chat,"* [AMX MATCH] No match to stop")
		console_print(id,"* There's no match to stop.")
	}
	return PLUGIN_CONTINUE
}

public back_to_ffa()
{
	//restart_round("8")
	match_inprogress = 0
	in_overtime = 0
	overtime_score = 0
	ready_type = 0
	end_type = 0
	swap_type = 0
	ready_CT_names = "None."
	ready_T_names = "None."
	players_ready[0] = 0
	teams_ready = 0
	clanCT[0] = 0
	clanT[0] = 0
	cfgfile[0] = 0
	matchtype1 = 0
	matchtype2 = 0
	recdemo = 0
	ct_score[0] = 0
	t_score[0] = 0
	ct_score[1] = 0
	t_score[1] = 0
	HLTV_rconid[0] = 0
	HLTV_rcon[0] = 0
	
	// (Re)set Hostname
	new hostname_type
	hostname_type = get_cvar_num("amx_match_hostname")
	if(hostname_type == 1)
	{
		set_cvar_string("hostname", old_servername)
	}
	
	// (Re)set Password
	new pass
	pass = get_cvar_num("amx_match_password")
	if(pass == 1)
	{
		set_cvar_string("sv_password", old_serverpass)
	}
	
	#if !defined(NO_STEAM)
	shield_type2 = get_cvar_num("amx_match_shield2")
	if ( shield_type2 == 0) {
		server_cmd("amx_restrict off shield")
		client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been unrestricted")
	}
	else
	{
		server_cmd("amx_restrict onn shield")
		client_print(0,print_chat,"* [AMX MATCH] Tactical shield has been restricted")
	}
	#endif
	client_print(0,print_chat,"* [AMX MATCH] Executing FFA config file (ffa.cfg)")
	server_cmd("exec ^"%s/leagues/ffa.cfg^"", customdir)
	return PLUGIN_CONTINUE
}

public warmup_msg()
{
	if ((match_inprogress == 1)||(match_inprogress == 3)) {
		set_hudmessage(255, 255, 255, -1.0, 0.01, 0, 2.0, 10.0, 0.8, 0.8, 6)
		new show = get_cvar_num("amx_match_screenshot")
		if ((recdemo==1)||(recdemo==3))
			show_hudmessage(0,"--[ Warmup Time ]--^nDemos autorecorded on your PC (If you're not spectating..)")
		else
			show_hudmessage(0,"--[ Warmup Time ]--^nRemember to record demos if you want...")
		set_hudmessage(255, 255, 255, -1.0, 0.08, 0, 2.0, 10.0, 0.8, 0.8, 5)
		show_hudmessage(0,"%s %s",(show > 0) ? "Screenshots are also taken" : "", (swap_type==1) ? "^nTeams will automatically swap after the 1st half" : "")
		if (cmdtype == 1)
			client_print(0,print_chat,"* [AMX MATCH] %s(CT) vs %s(T)",(match_inprogress == 1) ? clanCT : clanT,(match_inprogress == 1) ? clanT : clanCT)
		client_print(0,print_chat,"* [AMX MATCH] Warmup Time...")
		if (ready_type == 0) {
			if (teams_ready == 0)
				client_print(0,print_chat,"* [AMX MATCH] Both teams aren't ready.")
			if (teams_ready == 4) {
				if (cmdtype == 1)
					client_print(0,print_chat,"* [AMX MATCH] %s are ready. Waiting for %s to be ready.",(match_inprogress==1) ? clanCT : clanT,(match_inprogress==1) ? clanT : clanCT)
				else
					client_print(0,print_chat,"* [AMX MATCH] CT are ready. Waiting for T to be ready.")
			}
			if (teams_ready == 8) {
				if (cmdtype == 1)
					client_print(0,print_chat,"* [AMX MATCH] %s are ready. Waiting for %s to be ready.",(match_inprogress==1) ? clanT : clanCT,(match_inprogress==1) ? clanCT : clanT)
				else
					client_print(0,print_chat,"* [AMX MATCH] T are ready. Waiting for CT to be ready.")
			}
			client_print(0,print_chat,"* [AMX MATCH] When your team is ready: say ready")
			client_print(0,print_chat,"* [AMX MATCH] If your team isn't ready anymore: say notready")
		}
		else {
			if (ready_type == 1) {
				client_print(0,print_chat,"* [AMX MATCH] When you are ready: say ready")
				client_print(0,print_chat,"* [AMX MATCH] If you're not ready anymore: say notready")
				client_print(0,print_chat,"* [AMX MATCH] The match starts when everybody is ready")
			}
			else
				client_print(0,print_chat,"* [AMX MATCH] The admin will start the match when needed (by saying ^"/start^")")
		}
		set_task(90.0,"warmup_msg",1338)
	}
	return PLUGIN_CONTINUE
}

public start_warmup()
{
	restart_round("8")

	if (in_overtime2 == 0) 
	{	
		client_print(0,print_chat,"* [AMX MATCH] Executing match config file (%s.cfg + default.cfg)",cfgfile)
		server_cmd("exec ^"%s/leagues/%s.cfg^"", customdir, cfgfile)
		server_cmd("exec ^"%s/leagues/default.cfg^"", customdir)
	}
	else
	{
		new overtime_config = get_cvar_num("amx_match_overtimecfg")
		if(overtime_config == 0)
		{
			client_print(0,print_chat,"* [AMX MATCH] Executing match config file (%s.cfg + default.cfg)",cfgfile)
			server_cmd("exec ^"%s/leagues/%s.cfg^"", customdir, cfgfile)
			server_cmd("exec ^"%s/leagues/default.cfg^"", customdir)
		}
		else
		{
			client_print(0,print_chat,"* [AMX MATCH] Executing match overtime config file (%sot.cfg + default.cfg)",cfgfile)
			server_cmd("exec ^"%s/leagues/%sot.cfg^"", customdir, cfgfile)
			server_cmd("exec ^"%s/leagues/default.cfg^"", customdir)
		}
	}
	
	if (matchtype1 == 2) {
		new currentmap[64]
		get_mapname(currentmap,63)
		set_cvar_string("amx_nextmap",currentmap)
	}
	ready_CT_names = "None."
	ready_T_names = "None."
	players_ready[0] = 0
	server_cmd("mp_timelimit ^"0^"")
	server_cmd("mp_winlimit ^"0^"")
	server_cmd("mp_maxrounds ^"0^"")
	server_cmd("mp_limitteams ^"0^"")
	server_cmd("mp_autoteambalance ^"0^"")
	set_task(10.0,"warmup_msg")
	if (ready_type == 1)
		set_task(9.0,"print_readylist")
	return PLUGIN_CONTINUE
}

public live_msg()
{
	match_inprogress++
	set_hudmessage(0, 255, 255, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
	show_hudmessage(0,"--[ LIVE !!! GL & HF ]--^n--[ LIVE !!! GL & HF ]--^n--[ LIVE !!! GL & HF ]--")
	client_print(0,print_chat,"* [AMX MATCH] LIVE LIVE LIVE  !!!")
	client_print(0,print_chat,"* [AMX MATCH] LIVE LIVE LIVE  !!!")
	client_print(0,print_chat,"* [AMX MATCH] LIVE LIVE LIVE  !!!")
	return PLUGIN_CONTINUE
}

public set_rules()
{
	if (matchtype1 == 1)
		server_cmd("mp_maxrounds ^"%i^"",matchtype2+2)
	else
		server_cmd("mp_timelimit ^"%i^"",matchtype2)
	return PLUGIN_CONTINUE
}

public start_half()
{
	teams_ready = 0
	ct_score[1] = 0
	t_score[1] = 0
	if (recdemo > 0) {
		server_cmd("sv_voiceenable 0")
		set_task(2.0,"rec_demos")
		set_task(4.0,"voice_enable",0,"1",1)
	}
	if (match_inprogress == 1) {
		client_print(0,print_chat,"* [AMX MATCH] 1st half starting")
	}
	else {
		client_print(0,print_chat,"* [AMX MATCH] 2nd half starting")
	}
	client_print(0,print_chat,"* [AMX MATCH] GOING LIVE !!! GET READY !!!")
	client_print(0,print_chat,"* [AMX MATCH] GOING LIVE !!! GET READY !!!")
	set_task(2.0,"restart_round",0,"1",1)
	set_task(4.0,"set_rules")
	set_task(4.0,"restart_round",0,"1",1)
	set_task(6.0,"restart_round",0,"3",1)
	set_task(11.0,"live_msg")
	return PLUGIN_CONTINUE
}

public print_readylist()
{
	if ((match_inprogress == 1)||(match_inprogress == 3)) {
		new hudmessage[550]
		format(hudmessage,549,"CT ready: %s^nT ready: %s",ready_CT_names,ready_T_names)
		set_hudmessage(0, 255, 0, 0.01, 0.17, 0, 0.5, 5.0, 0.8, 0.8, 4)
		show_hudmessage(0,hudmessage)
		set_task(6.0,"print_readylist",1339)
	}
	return PLUGIN_CONTINUE
}

public add_readylist(ident)
{
	new longueur = strlen(players_ready)
	new playername[32],playerteam[16]
	new i=0
	while((i<longueur)&&(!(players_ready[i] == ident)))
		i++
	if (i == longueur) {
		players_ready[longueur] = ident
		players_ready[longueur+1] = 0
		get_user_name(ident,playername,31)
		get_user_team(ident,playerteam,15)
		if (equal(playerteam,"CT")) {
			if (equal(ready_CT_names,"None."))
				format(ready_CT_names,255,"%s",playername)
			else {
				new temp[40]
				format(temp,39,", %s",playername)
				add(ready_CT_names,255,temp)
			}
		}
		else {
			if (equal(ready_T_names,"None."))
				format(ready_T_names,255,"%s",playername)
			else {
				new temp[40]
				format(temp,39,", %s",playername)
				add(ready_T_names,255,temp)
			}
		}
		return 1
	}
	else
		return 0
	return PLUGIN_CONTINUE
}

public remove_readylist(ident)
{
	new longueur = strlen(players_ready)
	new i=0
	while((i<longueur)&&(!(players_ready[i] == ident)))
		i++
	if (i == longueur)
		return 0
	else {
		while(i<(longueur-1)) {
			players_ready[i] = players_ready[i+1]
			i++
		}
		players_ready[i] = 0
		new playername[32],playerteam[16],temp[40]
		get_user_name(ident,playername,31)
		get_user_team(ident,playerteam,15)
		if (equal(playerteam,"CT")) {
			format(temp,39,", %s",playername) // not first
			replace(ready_CT_names,255,temp,"")
			format(temp,39,"%s, ",playername) // first with next
			replace(ready_CT_names,255,temp,"")
			format(temp,39,"%s",playername) // alone
			replace(ready_CT_names,255,temp,"None.")
		}
		else {
			format(temp,39,", %s",playername) // not first
			replace(ready_T_names,255,temp,"")
			format(temp,39,"%s, ",playername) // first with next
			replace(ready_T_names,255,temp,"")
			format(temp,39,"%s",playername) // alone
			replace(ready_T_names,255,temp,"None.")
		}
		return 1
	}
	return PLUGIN_CONTINUE
}

public check_ready(id)
{
	if (ready_type == 2)
		return PLUGIN_CONTINUE
	new team[16]
	get_user_team(id, team, 15)
	if ((match_inprogress == 1)||(match_inprogress == 3)) {
		if (ready_type == 0) { // only one ready needed
			if (((equal(team,"CT"))&&(teams_ready == 8))||((equal(team,"TERRORIST"))&&(teams_ready == 4))) { // both are now ready
				set_hudmessage(255, 0, 0, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
				show_hudmessage(0,"--[ BOTH TEAMS ARE READY !!! ]--")
				set_hudmessage(255, 0, 0, -1.0, 0.35, 0, 2.0, 6.0, 0.8, 0.8, 5)
				if (cmdtype == 1)
					show_hudmessage(0,"%s vs %s can now begin :)^n3 restarts and GO!",clanCT,clanT)
				else
					show_hudmessage(0,"The match can now begin :)^n3 restarts and GO!")
				remove_task(1337)
				set_task(5.0,"start_half",1337)
				remove_task(1338)
			}
			else {
				if ((equal(team,"CT"))&&(teams_ready == 0)) { // CT wasn't ready
					teams_ready = 4
					set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
					if (cmdtype == 1)
						show_hudmessage(0,"--[ %s are now ready ]--^nWaiting for %s to be ready",(match_inprogress==1) ? clanCT : clanT,(match_inprogress==1) ? clanT : clanCT)
					else
						show_hudmessage(0,"--[ CT are now ready ]--^nWaiting for T to be ready")
				}
				else {
					if ((equal(team,"TERRORIST"))&&(teams_ready == 0)) { // T are now ready
						teams_ready = 8
						set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
						if (cmdtype == 1)
							show_hudmessage(0,"--[ %s are now ready ]--^nWaiting for %s to be ready",(match_inprogress==1) ? clanT : clanCT,(match_inprogress==1) ? clanCT : clanT)
						else
							show_hudmessage(0,"--[ T are now ready ]--^nWaiting for CT to be ready")
					}
					else // Team is already ready
						client_print(id,print_chat,"* [AMX MATCH] Your team is already ready. Get ready to play! :-p")
				}
			}
		}
		else { // every players must say ready
			if ((equal(team,"CT"))||(equal(team,"TERRORIST"))) {
				if (add_readylist(id)) { // user wasn't ready
					new playername[32]
					get_user_name(id,playername,31)
					set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
					show_hudmessage(0,"%s is now ready!",playername)
					client_print(id,print_chat,"* [AMX MATCH] You're set as ready. Get ready to play! :-p")
					new players_need = get_cvar_num("amx_match_playerneed")
					if (players_need == strlen(players_ready)) { // all players are ready
						set_hudmessage(255, 0, 0, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
						show_hudmessage(0,"--[ BOTH TEAMS ARE READY !!! ]--")
						set_hudmessage(255, 0, 0, -1.0, 0.35, 0, 2.0, 6.0, 0.8, 0.8, 5)
						if (cmdtype == 1)
							show_hudmessage(0,"%s vs %s can now begin :)^n3 restarts and GO!",clanCT,clanT)
						else
							show_hudmessage(0,"The match can now begin :)^n3 restarts and GO!")
						remove_task(1337)
						set_task(5.0,"start_half",1337)
						remove_task(1338)
						remove_task(1339)
					}
				}
				else // user was already ready
					client_print(id,print_chat,"* [AMX MATCH] You were already ready. Get ready to play !")
			}
		}
	}
	else {
		if (((match_inprogress == 2)||(match_inprogress == 4))&&((equal(team,"CT"))||(equal(team,"TERRORIST"))))
			client_print(id,print_chat,"* [AMX MATCH] The match has already started! O_o")
	}
	return PLUGIN_CONTINUE
}

public check_notready(id)
{
	if (ready_type == 2)
		return PLUGIN_CONTINUE
	new team[16]
	get_user_team(id, team, 15)
	if ((match_inprogress == 1)||(match_inprogress == 3)) {
		if (ready_type == 0) { // only one ready needed
			if ((equal(team,"CT"))&&(teams_ready == 4)) { // CT was ready
				set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
				if (cmdtype == 1)
					show_hudmessage(0,"--[ %s aren't ready anymore! ]--^nWaiting for both teams to be ready",(match_inprogress==1) ? clanCT : clanT)
				else
					show_hudmessage(0,"--[ CT aren't ready anymore! ]--^nWaiting for both teams to be ready")
				teams_ready = 0
			}
			else {
				if ((equal(team,"TERRORIST"))&&(teams_ready == 8)) { // T was ready
					set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
					if (cmdtype == 1)
						show_hudmessage(0,"--[ %s aren't ready anymore! ]--^nWaiting for both teams to be ready",(match_inprogress==1) ? clanT : clanCT)
					else
						show_hudmessage(0,"--[ T aren't ready anymore! ]--^nWaiting for both teams to be ready")
					teams_ready = 0
				}
				else {
					client_print(id,print_chat,"* [AMX MATCH] Your team wasn't ready. Don't be too long to prepare yourself ;-)")
				}
			}
		}
		else { // all players must say ready
			if ((equal(team,"CT"))||(equal(team,"TERRORIST"))) {
				if (remove_readylist(id)) { // user was ready
					new playername[32]
					get_user_name(id,playername,31)
					set_hudmessage(0, 255, 255, -1.0, 0.35, 1, 2.0, 6.0, 0.8, 0.8, 2)
					show_hudmessage(0,"%s isn't ready anymore",playername)
					client_print(id,print_chat,"* [AMX MATCH] You're no more ready. Don't be too long to prepare yourself ;-)")
				}
				else // user wasn't ready
					client_print(id,print_chat,"* [AMX MATCH] You wasn't ready. Don't be too long to prepare yourself ;-)")
			}
		}
	}
	else {
		if (((match_inprogress == 2)||(match_inprogress == 4))&&((equal(team,"CT"))||(equal(team,"TERRORIST"))))
			client_print(id,print_chat,"* [AMX MATCH] The match has already started! O_o")
	}
	return PLUGIN_CONTINUE
}

public force_start(id)
{
	new adminname[32]
	get_user_name(id,adminname,31)
	set_hudmessage(255, 0, 0, -1.0, 0.32, 0, 2.0, 6.0, 0.8, 0.8, 2)
	show_hudmessage(0,"--[ %s HAS STARTED THE HALF !!! ]--",adminname)
	set_hudmessage(255, 0, 0, -1.0, 0.35, 0, 2.0, 6.0, 0.8, 0.8, 5)
	show_hudmessage(0,"The match can now begin :)^n3 restarts and GO!")
	remove_task(1337)
	set_task(5.0,"start_half",1337)
	remove_task(1338)
	if (ready_type == 1)
		remove_task(1339)
	return PLUGIN_CONTINUE
}

public check_start(id)
{
	if ((match_inprogress == 1)||(match_inprogress == 3)) {
		if (!(get_user_flags(id) & ADMIN_LEVEL_A))
			client_print(id,print_console,"* [AMX MATCH] You need to be an admin in order to start the match")
		else
		{		
			// Show activity, if on
			new name[32]
			
			get_user_name(id,name,31)
			switch(get_cvar_num("amx_show_activity")) 
			{	
		  		case 2: client_print(0,print_chat,"ADMIN %s: forced the start of a half", name)
		  		case 1: client_print(0,print_chat,"ADMIN: forced the start of a half")
			}
		
			force_start(id)
		}
	}
	else {
		if ((match_inprogress == 2)||(match_inprogress == 4)) {
			console_print(id,"* The match has already started! O_o")
			client_print(id,print_chat,"* [AMX MATCH] The match has already started! O_o")
		}
		else {
			console_print(id,"* No match loaded")
			client_print(id,print_chat,"* [AMX MATCH] No match loaded")
		}
	}
	return PLUGIN_CONTINUE
}

public show_the_score()
{
	if (match_inprogress > 0) {
		new show_score = get_cvar_num("amx_match_showscore")
		if (show_score) {
			new the_score[1024]
			the_score[0] = 0
			if (match_inprogress == 1)
				format(the_score,1023,"* [AMX MATCH] Both teams have won %d rounds",overtime_score)
			if (match_inprogress == 2) {
				if (ct_score[1] > t_score[1])
					format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",clanCT,(ct_score[1] + overtime_score),(t_score[1] + overtime_score))
				else {
					if (ct_score[1] < t_score[1])
						format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",clanT,(t_score[1] + overtime_score),(ct_score[1] + overtime_score))
					else
						format(the_score,1023,"* [AMX MATCH] Both teams have won %d rounds",(ct_score[1] + overtime_score))
				}
			}
			if (match_inprogress == 3) {
				if (ct_score[0] > t_score[0])
					format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",clanCT,(ct_score[0] + overtime_score),(t_score[0] + overtime_score))
				else {
					if (ct_score[0] < t_score[0])
						format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",clanT,(t_score[0] + overtime_score),(ct_score[0] + overtime_score))
					else
						format(the_score,1023,"* [AMX MATCH] Both teams have won %d rounds",(ct_score[0] + overtime_score))
				}
			}
			if (match_inprogress == 4) {
				new total[2]
				total[0] = ct_score[0] + t_score[1]
				total[1] = t_score[0] + ct_score[1]
				if (total[0] > total[1])
					format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",(cmdtype==1) ? clanCT : "T",(total[0] + overtime_score),(total[1] + overtime_score))
				else {
					if (total[0] < total[1])
						format(the_score,1023,"* [AMX MATCH] %s are winning %d to %d",(cmdtype==1) ? clanT : "CT",(total[1] + overtime_score),(total[0] + overtime_score))
					else
						format(the_score,1023,"* [AMX MATCH] Both teams have won %d rounds",(total[0] + overtime_score))
				}
			}
			#if defined(NO_STEAM)
			message_begin(MSG_ALL,StatusTextid,{0,0,0})
			write_byte(0)
			write_string(the_score)
			message_end()
			#else
			set_hudmessage(255, 255, 0, 0.0, 0.90, 0, 2.0, 8.0, 0.8, 0.8, 3)
			show_hudmessage(0,the_score)
			#endif
		}
	}
	return PLUGIN_CONTINUE
}

public vote_playout(){

	g_isVoting=1;
	new menu_msg[256]

	format(menu_msg,255,"\y[AMX Match] Play Out? \w^n^n1. Yes^n2. No")
	
	new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0
	set_cvar_float("amx_last_voting",  get_gametime() + vote_time )
	show_menu(0,(1<<0)|(1<<1),menu_msg,floatround(vote_time))
 
	set_task(vote_time,"check_votes")
	client_print(0,print_chat,"* Voting has started...")
	option[0]=option[1]=0
	return PLUGIN_HANDLED
}

public vote_count(id,key){
   if ( get_cvar_float("amx_vote_answers") ) {
      new name[32]
      get_user_name(id,name,31)
      client_print(0,print_chat,"* %s voted %s", name, key ? "against" : "for" )
   }
   ++option[key]
   return PLUGIN_HANDLED
}

public check_votes(id){
   if (option[0] > option[1])
   {
		end_type=0;
		client_print(0,print_chat,"* Voting outcome: Playout (yes ^"%d^") (no ^"%d^").",option[0],option[1])
   }
   else
   {
		new_score2();
		client_print(0,print_chat,"* Voting outcome: Do NOT Playout (yes ^"%d^") (no ^"%d^").",option[0],option[1])
   }
   return PLUGIN_CONTINUE
}

public new_score()
{
	if ((match_inprogress == 2)||(match_inprogress == 4)) { // Playing a half
		new team[32]
		read_data(1,team,31)
		if (equal(team,"CT"))
			ct_score[1] = read_data(2)
		else {
			if (equal(team,"TERRORIST"))
				t_score[1] = read_data(2)
		}
	}
	show_the_score()
	if (matchtype1 == 1) { //Playing mr
		if  ( (match_inprogress == 2) && ((ct_score[1] + t_score[1]) == matchtype2) ) { // Playing 1st half and it's finished
			ct_score[0] = ct_score[1]
			t_score[0] = t_score[1]
			ct_score[1] = 0
			t_score[1] = 0
			new hudmsg[256]
			format(hudmsg,255,"--[ 1st Half Finished ]--^n^nScore is: %s %i / %i %s",clanCT,(in_overtime==1) ? (ct_score[0] + overtime_score) : ct_score[0],(in_overtime==1) ? (t_score[0] + overtime_score) : t_score[0],clanT)
			new show = get_cvar_num("amx_match_screenshot")
			if (show == 2)
				set_hudmessage(255, 255, 255, -1.0, 0.52, 0, 2.0, 8.0, 0.8, 0.8, 2)
			else
				set_hudmessage(255, 255, 255, -1.0, 0.32, 0, 2.0, 8.0, 0.8, 0.8, 2)
			show_hudmessage(0,hudmsg)
			format(hudmsg,255,"%s^nWe are going back in warmup.",(swap_type==1) ? "DON'T CHANGE YOUR TEAM!" : "")
			if (show == 2)
				set_hudmessage(255, 255, 255, -1.0, 0.63, 0, 2.0, 8.0, 0.8, 0.8, 5)
			else
				set_hudmessage(255, 255, 255, -1.0, 0.43, 0, 2.0, 8.0, 0.8, 0.8, 5)
			show_hudmessage(0,hudmsg)
			match_inprogress = 3
			take_screenshot()
			set_task(2.0,"start_warmup")
			if (recdemo > 0)
				set_task(3.0,"stop_demo")
			if (swap_type == 1)
				set_task(5.0,"swap")
				
			g_isVoting = 0
		}
		else {
			if (match_inprogress == 4) { // Playing 2nd half
				if ( ((ct_score[1] + t_score[1]) == matchtype2) || (((end_type == 1) || (end_type == 2)) && ( ((ct_score[0] + t_score[1]) == (matchtype2 + 1)) || ((t_score[0] + ct_score[1]) == (matchtype2 + 1)) )) ) // 2nd half is finished
				{
					new roundsPlayed = (ct_score[0] + t_score[1] + ct_score[1] + t_score[0])
							
					if(end_type == 2 && (((matchtype2 * 2) - roundsPlayed) > (((matchtype2 * 2) / 5) - 1))) //Check if end type is 2 and check if rounds played is more than a (1/5) - 1 of the rounds away from end of match	
					{			
						// Vote...	
						if (g_isVoting == 0)//if it's not voting			
							vote_playout()
						
					}
					else
					{
						new_score2()
					}
				}
			}
		}
	}
	return PLUGIN_CONTINUE
}


public new_score2()
{
	ct_score[0] += t_score[1]
	t_score[0] += ct_score[1]
	new hudmsg[200]
	format(hudmsg,199,"--[ Match Finished. GG all ]--^n^nAnd the winner of the map is...")
	new show = get_cvar_num("amx_match_screenshot")
	if (show == 2)
		set_hudmessage(255, 255, 255, -1.0, 0.50, 0, 2.0, 8.0, 0.8, 0.8, 2)
	else
		set_hudmessage(255, 255, 255, -1.0, 0.30, 0, 2.0, 8.0, 0.8, 0.8, 2)
	show_hudmessage(0,hudmsg)
	if (ct_score[0] > t_score[0]) {
		format(hudmsg,199,"%s with the score of %i/%i",(cmdtype==1) ? clanCT : "Current T team",(in_overtime==1) ? (ct_score[0] + overtime_score) : ct_score[0],(in_overtime==1) ? (t_score[0] + overtime_score) : t_score[0])
		in_overtime = 0
	}
	else {
		if (t_score[0] > ct_score[0]) {
			format(hudmsg,199,"%s with the score of %i/%i",(cmdtype==1) ? clanT : "Current CT team",(in_overtime==1) ? (t_score[0] + overtime_score) : t_score[0],(in_overtime==1) ? (ct_score[0] + overtime_score) : ct_score[0])
			in_overtime = 0
		}
		else {
			in_overtime = get_cvar_num("amx_match_overtime")
			if (in_overtime) {
				match_inprogress = 1
				overtime_score += matchtype2
				matchtype2 = get_cvar_num("amx_match_otrounds")
				format(hudmsg,199,"NO ONE!!!^nDraw match with the score of %i/%i^n^nLet's play OVERTIME (mr%i)",(ct_score[0] + overtime_score),(t_score[0] + overtime_score),matchtype2)
			}
			else
				format(hudmsg,199,"NO ONE!!!^nDraw match with the score of %i/%i",ct_score[0],t_score[0])
		}
	}
	
	if (show == 2)
		set_hudmessage(255, 255, 255, -1.0, 0.60, 0, 2.0, 8.0, 0.8, 0.8, 5)
	else
		set_hudmessage(255, 255, 255, -1.0, 0.40, 0, 2.0, 8.0, 0.8, 0.8, 5)
	show_hudmessage(0,hudmsg)
	take_screenshot()
	if (recdemo > 0)
		set_task(3.0,"stop_demo")
	if (in_overtime) {
		in_overtime2 = 1
		set_task(2.0,"start_warmup")
		if (swap_type == 1)
			set_task(5.0,"swap")
	}
	else {
		match_inprogress = 0
		set_task(2.0,"back_to_ffa")
	}
	
	return PLUGIN_CONTINUE
}



/**********************************/
/*         AMX Match Menu         */
/**********************************/
public addtypetolist(id,level,cid)
{
	if (!cmd_access(id,level,cid,2)) 
		return PLUGIN_HANDLED 
	new argc = read_argc()
	for(new i=1; i<argc; i++) {
		if (typelistpos == MAX_VARS) {
			console_print(id,"* Too many match type added (amx_match_deluxe plugin)")
			i=argc
		}
		else {
			new arg[32]
			read_argv(i,arg,31)
			typelist[typelistpos] = arg
			typelistpos++
		}
	}
	return PLUGIN_CONTINUE
}

public addconfigtolist(id,level,cid)
{
	if (!cmd_access(id,level,cid,3)) 
		return PLUGIN_HANDLED
	if (configlistpos < MAX_VARS) {
		new temp[32]
		read_argv(1,temp,31)
		confignamelist[configlistpos] = temp
		read_argv(2,temp,31)
		configfilelist[configlistpos] = temp
		configlistpos++
	}
	else
		console_print(id,"* Too many match config files added (amx_match_deluxe plugin).")
	return PLUGIN_CONTINUE
}

public get_CTclantags(id)
{
	CT_tags[0] = "Don't use Tags"
	CT_tagspos = 1
	new players[32], nbr
	new player_name1[32], player_name2[32]
	get_players(players,nbr,"e","CT")
	for(new i=0;i<nbr;i++) {
		get_user_name(players[i],player_name1,31)
		for(new j=i+1;j<nbr;j++) {
			if (CT_tagspos < MAX_TAGS) {
				get_user_name(players[j],player_name2,31)
				new k=MIN_TAG_LENGTH, l_max = min(strlen(player_name1),strlen(player_name2))
				while(k < l_max) {
					if (equal(player_name1,player_name2,k))
						k++
					else {
						if (k > MIN_TAG_LENGTH) { // it's added to the Tags table after checking doubles
							new l = 0
							while(l < CT_tagspos) {
								if (!equal(CT_tags[l],player_name1,max(strlen(CT_tags[l]),k-1)))
									l++
								else {
									l = CT_tagspos+1
								}
							}
							if (l == CT_tagspos) {
								copy(CT_tags[CT_tagspos],k-1,player_name1)
								CT_tagspos++
							}
						}
						k = l_max
					}
				}
			}
		}
	}
	if (CT_tagspos == 1)
		client_print(id,print_chat,"* [AMX MATCH] No CT clantag found :/")
	return PLUGIN_CONTINUE
}

public get_Tclantags(id)
{
	T_tagspos = 0
	new players[32], nbr
	new player_name1[32], player_name2[32]
	get_players(players,nbr,"e","TERRORIST")
	for(new i=0;i < nbr;i++) {
		get_user_name(players[i],player_name1,31)
		for(new j=i+1;j < nbr;j++) {
			if (T_tagspos < MAX_TAGS) {
				get_user_name(players[j],player_name2,31)
				new k=MIN_TAG_LENGTH, l_max = min(strlen(player_name1),strlen(player_name2))
				while(k < l_max) {
					if (equal(player_name1,player_name2,k))
						k++
					else {
						if (k > MIN_TAG_LENGTH) { // it's added to the Tags table after checking doubles
							new l = 0
							while(l < T_tagspos) {
								if (!equal(T_tags[l],player_name1,max(strlen(T_tags[l]),k-1)))
									l++
								else {
									l = T_tagspos+1
								}
							}
							if (l == T_tagspos) {
								copy(T_tags[T_tagspos],k-1,player_name1)
								T_tagspos++
							}
						}
						k = l_max
					}
				}
			}
		}
	}
	if (T_tagspos == 0)
	{
		client_print(id,print_chat,"* [AMX MATCH] No Terrorist clantag found :/")
/*
		new enter_tags
			
		enter_tags = get_cvar_num("amx_match_entertags")
		if(enter_tags == 1)
		{
			copy (message_Tag, 191, "")
			
			client_cmd(id, "messagemode clanTag")

			set_task(2.0, "clantag_loop")		
		}
*/		
	}
				
	return PLUGIN_CONTINUE
}

/*
public clantag_Tloop()
{
	if(strlen(message_Tag) != 0)
	{
		clantag_enter(1)
		
		client_print(id,print_chat,"* [AMX MATCH] %s has been added as the T clan tag", message_Tag)	
	}
	else
	{
		set_task(2.0, "clantag_loop", 1340)
	}	
}

public clantag_enter(team) //team = 1 - Ts, team = 2 - CTs
{
	if(team == 1)
	{	
		client_print(0,print_chat,"DEBUG_CLANTAG | %d |", team)			
		
		copy (clanT, 31, message_Tag)
	}
	else
	{
		copy (clanCT, 31, message_Tag)
	}

	
	return PLUGIN_CONTINUE
}

public clantag_messagemode(id,level,cid)
{
	read_args(message_Tag, 191)
	
	remove_quotes(message_Tag)
	
	client_print(id,print_chat,"DEBUG_MESSAGEMODE | %s |", message_Tag)
	
	return PLUGIN_CONTINUE
}
*/

public match_menu(id,level,cid)
{
	if (!cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED
	show_main_menu(id)
	return PLUGIN_CONTINUE
}

// Main Menu
show_main_menu(id)
{
	menu_position[id] = 0
	menu_selections[id][0] =  0
	menu_selections[id][1] =  0
	menu_selections[id][2] =  0
	menu_selections[id][3] =  0
	menu_selections[id][4] =  0
	if ((match_inprogress == 1)||(match_inprogress == 3))
		show_menu(id,(1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<9),"\yAMX Match Menu:\w^n^n1. Stop the match^n2. Force the start of the half^n3. Restart match^n4. Swap Teams^n5. Plugin settings^n^n0. Exit")
	else {
		if ((match_inprogress == 2)||(match_inprogress == 4))
			show_menu(id,(1<<0)|(1<<1)|(1<<2)|(1<<9),"\yAMX Match Menu:\w^n^n1. Stop the match^n2. Restart match^n3. Plugin settings^n^n0. Exit")
		else
			show_menu(id,(1<<0)|(1<<1)|(1<<2)|(1<<9),"\yAMX Match Menu:\w^n^n1. Start a match^n2. Swap Teams^n3. Plugin settings^n^n0. Exit")
	}
	return PLUGIN_CONTINUE
}

public action_main_menu(id,key)
{
	switch(key) {
		case 0: {
			if ((match_inprogress > 0)) // stops the match
				server_cmd("amx_matchstop")
			else { // start choosing a match
				get_CTclantags(id)
				get_Tclantags(id)
				if ((CT_tagspos > 0) && (T_tagspos > 0))
					show_CTtags_menu(id,0) // show CT clantags
				else
					show_type_menu(id,0) // show type choices
			}
		}
		case 1: {
			if (match_inprogress == 0) // swap teams
				server_cmd("amx_swapteams")
			else { // forces the start of the half
				if ((match_inprogress == 1)||(match_inprogress == 3))
					force_start(id)
				else
					restart_match(id, ADMIN_LEVEL_A)
			}
		}
		case 2: {
			if (match_inprogress == 0)
				show_settings_menu(id)
			else {
				if ((match_inprogress == 1)||(match_inprogress == 3))
					restart_match(id, ADMIN_LEVEL_A)
				else
					show_settings_menu(id)					
			}
		}
		case 3: {
			if ((match_inprogress == 1)||(match_inprogress == 3))
				server_cmd("amx_swapteams")	
		}
		case 4: {
			if ((match_inprogress == 1)||(match_inprogress == 3))
				show_settings_menu(id)
		}
	}
	return PLUGIN_CONTINUE
}

// CT Tags Menu
show_CTtags_menu(id,pos)
{
	new menu_body[512], pos2
	new keys = (1<<9)
	pos2 = format(menu_body,511,"\yAMX Match CT ClanTag:\R%d/%d^n\w^n",pos+1,(CT_tagspos/8)+((CT_tagspos%8)?1:0))
	new start = pos * 8
	new end = start + 8
	if (end>CT_tagspos)
		end = CT_tagspos
	new j = 1
	for(new i=start; i<end; i++) {
		pos2 += format(menu_body[pos2],511-pos2,"%d. %s^n",j,CT_tags[i])
		keys |=(1<<(j-1))
		j++
	}
	if (end != CT_tagspos) {
		format(menu_body[pos2],511-pos2,"^n9. More...^n0. Back")
		keys |= (1<<8)
	}
	else
		format(menu_body[pos2],511-pos2,"^n0. Back")
	show_menu(id,keys,menu_body)
	return PLUGIN_CONTINUE
}

public action_CTtags_menu(id,key)
{
	if (key == 9) { // goes back
		if (menu_position[id] == 0)
			show_main_menu(id)
		else {
			(menu_position[id])--
			show_CTtags_menu(id,menu_position[id])
		}
	}
	else {
		if ((key == 0)&&(menu_position[id] == 0))
			show_type_menu(id,0)
		else {
			if (key == 8) { // show next clantags
				(menu_position[id])++
				show_CTtags_menu(id,menu_position[id])
			}
			else {
				client_print(id,print_chat,"* Tag selected = %s",CT_tags[key + (menu_position[id] * 8)])
				menu_selections[id][0] = key + (menu_position[id] * 8)
				menu_position[id] = 0
				show_Ttags_menu(id,0)
			}
		}
	}	
	return PLUGIN_CONTINUE
}

// T Tags Menu
show_Ttags_menu(id,pos)
{
	new menu_body[512], pos2
	new keys = (1<<9)
	pos2 = format(menu_body,511,"\yAMX Match T ClanTag:\R%d/%d^n\w^n",pos+1,(T_tagspos/8)+((T_tagspos%8)?1:0))
	new start = pos * 8
	new end = start + 8
	if (end>T_tagspos)
		end = T_tagspos
	new j = 1
	for(new i=start; i<end; i++) {
		pos2 += format(menu_body[pos2],511-pos2,"%d. %s^n",j,T_tags[i])
		keys |=(1<<(j-1))
		j++
	}
	if (end != T_tagspos) {
		format(menu_body[pos2],511-pos2,"^n9. More...^n0. Back")
		keys |= (1<<8)
	}
	else
		format(menu_body[pos2],511-pos2,"^n0. Back")
	show_menu(id,keys,menu_body)
	return PLUGIN_CONTINUE
}

public action_Ttags_menu(id,key)
{
	if (key == 9) { // goes back
		if (menu_position[id] == 0)
			show_CTtags_menu(id,0)
		else {
			(menu_position[id])--
			show_Ttags_menu(id,menu_position[id])
		}
	}
	else {
		if (key == 8) { // show next clantags
			(menu_position[id])++
			show_Ttags_menu(id,menu_position[id])
		}
		else {
			client_print(id,print_chat,"* Tag selected = %s",T_tags[key + (menu_position[id] * 8)])
			menu_selections[id][1] = key + (menu_position[id] * 8)
			menu_position[id] = 0
			show_type_menu(id,0)
		}
	}
	return PLUGIN_CONTINUE
}

//Plugin Settings
show_settings_menu(id)
{
/*
* - amx_match_endtype
* - amx_match_hostname
* - amx_match_playerneed
* - amx_match_readytype
* - amx_match_swaptype
* - amx_match_screenshot
* - amx_match_screenshot2
*/
	new menubody[1024], keys , pos
	new rdy_type = get_cvar_num("amx_match_readytype")
	new screen_type = get_cvar_num("amx_match_screenshot")
	
	get_cvar_string("amx_match_password2", server_pass, 31)
	
	pos = format(menubody,1023,"\yAMX Match Settings (main):\w^n^n1. End type: \y%s\w^n2. Ready type: \y%s\w^n3. Ready players needed: \y%d\w^n", (get_cvar_num("amx_match_endtype") == 1) ? "mp_maxround+1" : ((get_cvar_num("amx_match_endtype") == 2) ? "Vote after mp_maxround+1" : "After all rounds"), (rdy_type == 0) ? "1 per team" : ((rdy_type == 1) ? "All players" : "An admin"), get_cvar_num("amx_match_playerneed"))
	pos += format(menubody[pos],1023 - pos,"4. Auto swap: \y%s\w^n5. Screenshot: \y%s\w^n6. Always Screenshot: \y%s\w^n", (get_cvar_num("amx_match_swaptype") == 0) ? "No" : "Yes", (screen_type == 0) ? "None" : ((screen_type == 1) ? "Scores" : "Scores & WonID/SteamID"), (get_cvar_num("amx_match_screenshot2") == 0) ? "No" : "Yes")
	pos += format(menubody[pos],1023 - pos,"7. Change hostname: \y%s\w^n^n9. More...^n0. Back", get_cvar_num("amx_match_hostname") ? "Yes" : "No")

	keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<8)|(1<<9)
	
	show_menu(id,keys,menubody)

	return PLUGIN_CONTINUE
}

public action_setting_menu(id,key)
{
	switch(key) {
		case 0: {
			if (get_cvar_num("amx_match_endtype") == 2)
				set_cvar_num("amx_match_endtype",0)
			else
				set_cvar_num("amx_match_endtype", get_cvar_num("amx_match_endtype") + 1)
			show_settings_menu(id)
		}
		case 1: {
			if (get_cvar_num("amx_match_readytype") == 2)
				set_cvar_num("amx_match_readytype",0)
			else
				set_cvar_num("amx_match_readytype",get_cvar_num("amx_match_readytype") + 1)
			show_settings_menu(id)
		}
		case 2: {
			if (get_cvar_num("amx_match_playerneed") == 12)
				set_cvar_num("amx_match_playerneed",2)
			else
				set_cvar_num("amx_match_playerneed",get_cvar_num("amx_match_playerneed") + 2)
			show_settings_menu(id)
		}
		case 3: {
			set_cvar_num("amx_match_swaptype",!(get_cvar_num("amx_match_swaptype")))
			show_settings_menu(id)
		}
		case 4: {
			if (get_cvar_num("amx_match_screenshot") == 2)
				set_cvar_num("amx_match_screenshot",0)
			else
				set_cvar_num("amx_match_screenshot",get_cvar_num("amx_match_screenshot") + 1)
			show_settings_menu(id)
		}
		case 5: {
			set_cvar_num("amx_match_screenshot2",!(get_cvar_num("amx_match_screenshot2")))
			show_settings_menu(id)
		}
		case 6: {
			set_cvar_num("amx_match_hostname",!(get_cvar_num("amx_match_hostname")))
			show_settings_menu(id)
		}
		case 8: { // get more
			show_settings_menu2(id)
		}
		case 9: { // goes back
			show_main_menu(id)
		}
	}
	return PLUGIN_CONTINUE
}

show_settings_menu2(id)
{
/*
* - amx_match_overtime
* - amx_match_overtimecfg
* - amx_match_otrounds
* - amx_match_password
* - amx_match_password2
* - amx_match_shield
* - amx_match_shield2
* - amx_match_showscore
*/
	new menubody[1024], keys , pos
	
	get_cvar_string("amx_match_password2", server_pass, 31)
	
	pos = format(menubody,1023,"\yAMX Match Settings (cont.):\w^n^n")
	pos += format(menubody[pos],1023 - pos,"1. Password: \y%s\w^n2. Password: ^"\y%s\w^"^n3. Overtime configs: \y%s\w^n", get_cvar_num("amx_match_password") ? "Yes" : "No", server_pass, get_cvar_num("amx_match_overtimecfg") ? "Yes" : "No")
	#if defined(NO_STEAM)
	format(menubody[pos],1023 - pos,"4. Show score: \y%s\w^n5. Allow overtime: \y%s\w^n6. Overtime rounds: \y%d\w^n^n0. Back",(get_cvar_num("amx_match_showscore") == 0) ? "No" : "Yes", (get_cvar_num("amx_match_overtime") == 0) ? "No" : "Yes", get_cvar_num("amx_match_otrounds"))
	keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)||(1<<5)|(1<<9)
	#else
	format(menubody[pos],1023 - pos,"4. Show score: \y%s\w^n5. Allow overtime: \y%s\w^n6. Overtime rounds: \y%d\w^n7. Shield allowed: \y%s\w^n8. (Re)allow Shield: \y%s\w^n^n0. Back",(get_cvar_num("amx_match_showscore") == 0) ? "No" : "Yes", (get_cvar_num("amx_match_overtime") == 0) ? "No" : "Yes", get_cvar_num("amx_match_otrounds"), (get_cvar_num("amx_match_shield") == 0) ? "No" : "Yes", (get_cvar_num("amx_match_shield2") == 0) ? "No" : "Yes")
	keys = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<9)
	#endif
	
	show_menu(id,keys,menubody)

	return PLUGIN_CONTINUE
}

public action_settingmenu2(id,key)
{
	switch(key) {
		case 0: {
			set_cvar_num("amx_match_password",!(get_cvar_num("amx_match_password")))
			show_settings_menu2(id)
		}		
		case 1: {
			get_cvar_string("amx_match_password2", server_pass, 31)
			
			if (equal(server_pass, "scrim"))
				set_cvar_string("amx_match_password2", "scrimmage")
			else if (equal(server_pass, "scrimmage"))
				set_cvar_string("amx_match_password2", "calmatch")
			else if (equal(server_pass, "calmatch"))
				set_cvar_string("amx_match_password2", "ecupmatch")
			else if (equal(server_pass, "ecupmatch"))
				set_cvar_string("amx_match_password2", "julmatch")
			else if (equal(server_pass, "julmatch"))
				set_cvar_string("amx_match_password2", "practice")
			else
				set_cvar_string("amx_match_password2", "scrim")
								
			show_settings_menu2(id)
		}
		case 2: {
			set_cvar_num("amx_match_overtimecfg",!(get_cvar_num("amx_match_overtimecfg")))
			show_settings_menu2(id)
		}	
		case 3: {
			set_cvar_num("amx_match_showscore",!(get_cvar_num("amx_match_showscore")))
			show_settings_menu2(id)
		}		
		
		
		case 4: {
			set_cvar_num("amx_match_overtime",!(get_cvar_num("amx_match_overtime")))
			show_settings_menu2(id)
		}
		case 5: {
			if (get_cvar_num("amx_match_otrounds") == 12)
				set_cvar_num("amx_match_otrounds",1)
			else
				set_cvar_num("amx_match_otrounds",get_cvar_num("amx_match_otrounds") + 1)
			show_settings_menu2(id)
		}
		#if !defined(NO_STEAM)
		case 6: {
			set_cvar_num("amx_match_shield",!(get_cvar_num("amx_match_shield")))
			show_settings_menu2(id)
		}
		case 7: {
			set_cvar_num("amx_match_shield2",!(get_cvar_num("amx_match_shield2")))
			show_settings_menu2(id)
		}
		#endif
		case 9: { // goes back
			show_settings_menu(id)
		}
	}
	return PLUGIN_CONTINUE
}

// Match Type Menu
show_type_menu(id,pos)
{
	new menu_body[512], pos2
	new keys = (1<<9)
	pos2 = format(menu_body,511,"\yAMX Match Type:\R%d/%d^n\w^n",pos+1,(typelistpos/8)+((typelistpos%8)?1:0))
	new start = pos * 8
	new end = start + 8
	if (end>typelistpos)
		end = typelistpos
	new j = 1
	for(new i=start; i<end; i++) {
		pos2 += format(menu_body[pos2],511-pos2,"%d. %s^n",j,typelist[i])
		keys |=(1<<(j-1))
		j++
	}
	if (end != typelistpos) {
		format(menu_body[pos2],511-pos2,"^n9. More...^n0. Back")
		keys |= (1<<8)
	}
	else
		format(menu_body[pos2],511-pos2,"^n0. Back")
	show_menu(id,keys,menu_body)
	return PLUGIN_CONTINUE
}

public action_type_menu(id,key)
{
	if (key == 9) { // goes back
		if (menu_position[id] == 0)
			show_main_menu(id)
		else {
			(menu_position[id])--
			show_type_menu(id,menu_position[id])
		}
	}
	else {
		if (key == 8) { // shows next type
			(menu_position[id])++
			show_type_menu(id,menu_position[id])
		}
		else {
			menu_selections[id][2] = key + (menu_position[id] * 8)
			menu_position[id] = 0
			show_config_menu(id,0)
		}
	}	
	return PLUGIN_CONTINUE
}

// Config File Menu
show_config_menu(id,pos)
{
	new menu_body[512], pos2
	new keys = (1<<9)
	pos2 = format(menu_body,511,"\yAMX Match Config File:\R%d/%d^n\w^n",pos+1,(configlistpos/8)+((configlistpos%8)?1:0))
	new start = pos * 8
	new end = start + 8
	if (end>configlistpos)
		end = configlistpos
	new j = 1
	for(new i=start; i<end; i++) {
		pos2 += format(menu_body[pos2],511-pos2,"%d. %s^n",j,confignamelist[i])
		keys |=(1<<(j-1))
		j++
	}
	if (end != configlistpos) {
		format(menu_body[pos2],511-pos2,"^n9. More...^n0. Back")
		keys |= (1<<8)
	}
	else
		format(menu_body[pos2],511-pos2,"^n0. Back")
	show_menu(id,keys,menu_body)
	return PLUGIN_CONTINUE
}

public action_config_menu(id,key)
{
	if (key == 9) { // goes back
		if (menu_position[id] == 0)
			show_type_menu(id,0)
		else {
			(menu_position[id])--
			show_config_menu(id,menu_position[id])
		}
	}
	else {
		if (key == 8) { // shows next type
			(menu_position[id])++
			show_config_menu(id,menu_position[id])
		}
		else {
			menu_selections[id][3] = key + (menu_position[id] * 8)
			menu_position[id] = 0
			show_demo_menu(id)
		}
	}	
	return PLUGIN_CONTINUE
}

// Demo Menu
show_demo_menu(id)
{
	#if defined(USE_HLTV_SECTION)
	show_menu(id,(1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<9),"\yAMX Match Record demo:\w^n^n1. In-eyes^n2. HLTV^n3. In-eyes & HLTV^n4. No^n^n0. Back")
	#else
	show_menu(id,(1<<0)|(1<<1)|(1<<9),"\yAMX Match Record demo:\w^n^n1. Yes^n2. No^n^n0. Back")
	#endif
	return PLUGIN_CONTINUE
}

public action_demo_menu(id,key)
{
	if (key == 9) { // goes back
		show_config_menu(id,0)
	}
	else { // selects last variable and executes the match
		menu_selections[id][4] = key
		new command[128]
		new demotype[16]
		switch(key) {
			case 0: format(demotype,15," recdemo")
			case 1: format(demotype,15," rechltv")
			case 2: format(demotype,15," recboth")
			case 3: format(demotype,15,"")
		}
		#if defined(USE_HLTV_SECTION)
		if (menu_selections[id][0] == 0)
			format(command,127,"amx_match2 %s %s%s",typelist[menu_selections[id][2]],configfilelist[menu_selections[id][3]],demotype)
		else
			format(command,127,"amx_match ^"%s^" ^"%s^" %s %s%s",CT_tags[menu_selections[id][0]],T_tags[menu_selections[id][1]],typelist[menu_selections[id][2]],configfilelist[menu_selections[id][3]],demotype)
		#else
		if (menu_selections[id][0] == 0)
			format(command,127,"amx_match2 %s %s%s",typelist[menu_selections[id][2]],configfilelist[menu_selections[id][3]],(menu_selections[id][4]) ? "" : " recdemo")
		else
			format(command,127,"amx_match ^"%s^" ^"%s^" %s %s%s",CT_tags[menu_selections[id][0]],T_tags[menu_selections[id][1]],typelist[menu_selections[id][2]],configfilelist[menu_selections[id][3]],(menu_selections[id][4]) ? "" : " recdemo")
		#endif
		server_cmd(command)
	}
	return PLUGIN_CONTINUE
}

public client_putinserver(id)
{
	if (is_user_hltv(id)) {
		HLTV_id = id
		new HLTV_ip[32]
		get_user_ip(HLTV_id,HLTV_ip,31)
		#if defined(USE_HLTV_SECTION)
		new pos = contain(HLTV_ip,":")
		new HLTV_port = strtonum(HLTV_ip[pos+1])
		HLTV_ip[pos] = 0
		HLTV_iaddress = set_iaddress(HLTV_ip,HLTV_port)
		hltv_rcon_cmd("say HLTV correctly configured =)",0)
		server_print("* HLTV entered the game (%s:%i)",HLTV_ip,HLTV_port)
		log_amx(logfilename,"* HLTV entered the game (%s:%i)",HLTV_ip,HLTV_port)
		#else
		server_print("* HLTV entered the game (%s)",HLTV_ip)
		log_amx(logfilename,"* HLTV entered the game (%s)",HLTV_ip)
		#endif
	}
	return PLUGIN_CONTINUE
}

public client_disconnect(id)
{
	if (HLTV_id == id) {
		new HLTV_ip[32]
		get_user_ip(HLTV_id,HLTV_ip,31)
		server_print("* HLTV left the game (%s)",HLTV_ip)
		log_amx(logfilename,"* HLTV left the game (%s)",HLTV_ip)
		HLTV_id = 0
		HLTV_rconid[0] = 0
		#if defined(USE_HLTV_SECTION)
		HLTV_iaddress = -1
		#endif
	}
	if ((ready_type == 1)&&((match_inprogress == 1)||(match_inprogress == 3))) {
		remove_readylist(id)
	}
	return PLUGIN_CONTINUE
}

public plugin_init()
{
	register_plugin("AMX Match Deluxe","2.01","Infra and Shromilder")
	register_cvar("amx_match_deluxe", "2.01",FCVAR_SERVER)
	#if defined(USE_HLTV_SECTION)
	
	// Console commands
	register_concmd("amx_match","start_match",ADMIN_LEVEL_A,"<CT's clan tag> <T's clan tag> <mrXX or clXX> <Config filename> [recdemo|rechltv|recboth]")
	register_concmd("amx_match2","start_match",ADMIN_LEVEL_A,"<mrXX or clXX> <Config filename> [recdemo|rechltv|recboth]")
	register_concmd("amx_match_hltv","set_hltv_rcon",ADMIN_LEVEL_A,"<HLTV adminpassword>")
	#else
	register_concmd("amx_match","start_match",ADMIN_LEVEL_A,"<CT's clan tag> <T's clan tag> <mrXX or clXX> <Config filename> [recdemo]")
	register_concmd("amx_match2","start_match",ADMIN_LEVEL_A,"<mrXX or clXX> <Config filename> [recdemo]")
	#endif
	
	// Start...Stop...Restart
	register_concmd("amx_matchrestart","restart_match",ADMIN_LEVEL_A,"Restart a match")
	register_concmd("amx_matchstart","check_start",ADMIN_LEVEL_A,"Force a match to start")
	register_concmd("amx_matchstop","stop_match",ADMIN_LEVEL_A,"Stop a match")
	register_concmd("amx_swapteams","swap_teams",ADMIN_LEVEL_A,"Swap teams")
	
	// Client commands
	register_clcmd("say ready","check_ready")
	register_clcmd("say notready","check_notready")
	
	//register_clcmd("say /ct","check_ct")
	//register_clcmd("say /t","check_t")

	//register_clcmd("say /heads","check_heads")
	//register_clcmd("say /tails","check_tails")
	
	register_clcmd("say /start","check_start", ADMIN_LEVEL_A, "check_start")
	register_clcmd("say /stop","stop_match", ADMIN_LEVEL_A, "stop_match")
	
	// Menu
	register_clcmd("amx_match_menu","match_menu",ADMIN_LEVEL_A,"match menu")
	
	// Server commands
	register_srvcmd("amx_match_tmenu","addtypetolist")
	register_srvcmd("amx_match_cmenu","addconfigtolist")
    	
    	// Events
	register_event("TeamScore","new_score","a")
	register_event("ResetHUD", "show_the_score", "b")
	
	// Cvars
	//register_cvar("amx_match_cointoss","0")
	register_cvar("amx_match_endtype","0")
	register_cvar("amx_match_entertags","1")
	register_cvar("amx_match_hostname","1")
	register_cvar("amx_match_overtime","0")
	register_cvar("amx_match_overtimecfg","0")
	register_cvar("amx_match_otrounds","3")
	register_cvar("amx_match_password", "1")
	register_cvar("amx_match_password2", "scrim")
	register_cvar("amx_match_playerneed", "10")
	register_cvar("amx_match_readytype", "1")
	register_cvar("amx_match_swaptype","1")
	register_cvar("amx_match_screenshot","1")
	register_cvar("amx_match_screenshot2","1")
	register_cvar("amx_match_showscore","1")
	
	#if !defined(NO_STEAM)
	register_cvar("amx_match_shield","0")
	register_cvar("amx_match_shield2","1")
	#endif
	
	// Playout vote
	register_menucmd(register_menuid("[AMX Match] Play Out?") ,(1<<0)|(1<<1),"vote_count") 
	
	// Init Custom Directory string
	get_customdir(customdir, 63)

	// Enter clan tags
	//register_concmd("clanTag","messagemode_clantag",ADMIN_LEVEL_A,"< message >")

	// MENU
	register_menucmd(register_menuid("AMX Match Menu"),(1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<9),"action_main_menu")
	register_menucmd(register_menuid("AMX Match CT ClanTag"),1023,"action_CTtags_menu")
	register_menucmd(register_menuid("AMX Match T ClanTag"),1023,"action_Ttags_menu")
	register_menucmd(register_menuid("AMX Match Settings (main)"),1023,"action_setting_menu")
	register_menucmd(register_menuid("AMX Match Settings (cont.)"),1023,"action_settingmenu2")
	register_menucmd(register_menuid("AMX Match Type"),1023,"action_type_menu")
	register_menucmd(register_menuid("AMX Match Config File"),1023,"action_config_menu")
	#if defined(USE_HLTV_SECTION)
	register_menucmd(register_menuid("AMX Match Record demo"),(1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<9),"action_demo_menu")
	#else
	register_menucmd(register_menuid("AMX Match Record demo"),(1<<0)|(1<<1)|(1<<9),"action_demo_menu")
	#endif
	#if defined(NO_STEAM)
	StatusTextid = get_user_msgid("StatusText")
	#endif

	return PLUGIN_CONTINUE
}