
/* AMX Mod script.
*
* (c) Copyright 2002, OLO
* This file is provided as is (no warranties).
*
*/

#include <amxmod>

/* cvar:
*  set nextmap when timelimit is reached
*  amx_nextmap < mapname >
*
*  commands:
*  check next map (available for all clients)
*  say nextmap
*/

new nextmap[64]
public delay_sayinfo(text[]) 
{
	client_print(0,print_chat,text)
}
public admin_nextmap()
{
	new cnext[64]
	get_cvar_string("amx_nextmap",cnext,64)
	if (is_map_valid(cnext)==0)	{
		copy(cnext,64,nextmap)
		set_cvar_string("amx_nextmap",nextmap)
	}
	new arg[96]
	new len = format(arg,96,"[AMX] Next Map:   %s",cnext)
	set_task(0.1,"delay_sayinfo",0,arg,len + 1)
	return PLUGIN_CONTINUE
}
public delay_mapchange() 
{
	new cnext[64]
	get_cvar_string("amx_nextmap",cnext,64)
	if (is_map_valid(cnext)==0){
		copy(cnext,64,nextmap)
		set_cvar_string("amx_nextmap",nextmap)
	}
	server_cmd("changelevel %s",cnext)
}
public admin_forcemap()
{
	set_task(1.5,"delay_mapchange")
}

bool:is_alpha(c)
	return ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) ? true : false

find_nextmap() {
	new filename[64], currentmap[64], buffor[64]
	new firstmap[64], state = 0, index = 0, len
	get_cvar_string("mapcyclefile",filename,64)
	get_mapname(currentmap,64)
	if (file_exists(filename)){
		while(read_file(filename,index++,buffor,64,len)){
			if (is_alpha(buffor[0])&&is_map_valid(buffor)){
				if (state == 0){
					state = 1
					copy(firstmap,64,buffor)
				}
				if (equal(currentmap,buffor)){
					state = 2
					continue
				}
				if (state == 2) {
					copy(nextmap,64,buffor)
					state = 3
					break
				}
			}
		}
	}
	if (state == 0)
		copy(nextmap,64,currentmap)
	else if (state < 3)
		copy(nextmap,64,firstmap)
	set_cvar_string("amx_nextmap",nextmap)
}
public plugin_init() 
{
	register_plugin("Next Map","0.7","default")
	register_event("30", "admin_forcemap", "a")
	register_clcmd("say nextmap","admin_nextmap")
	register_cvar("amx_nextmap","")
	find_nextmap()
	return PLUGIN_CONTINUE
}

