/* AMXMOD script
*
* Automatically changes map after a specifed time if server is empty
*  by JustinHoMi
*  email: justin@justinmitchell.net
*  web: www.modkillers.com
*  irc: #modkillers @ irc.gamesnet.net
*
* Usage:
*  Edit variables below for options
*
* Changelog:
*  0.6 - Updated set_task for amx 0.6
*          Removed misc debugging info
*  0.51 - now ignores bots in player count
*  0.5 - allows switching to random map from list
*  0.1 - initial release
*/

#include <amxmod>

// 1 - Change to next map in cycle (value of amx_nextmap)
// 2 - Change to a specified map from a predefined list
new next_or_specific = 1

// If you chose to change to a specific map, set the list up here
//  You must set MAPS to the number of maps in the list
#define MAPS 4
new map[MAPS][32] = {
"de_dust2",
"de_aztec",
"cs_italy",
"de_inferno"
}

// How long in seconds to wait before changing maps
new Float:time_delay = 600.0

// 1 - Check curplayers only after time_delay
// 2 - Check curplayers several times before changing map
// If you check it several times then it will ensure that players were never on the server
//  rather than just checking right at time_delay
new once_or_frequently = 2

// If you set once_or_frequently to 2, then this is the time in seconds
//  between each check
new Float:check_time = 60.0	// how frequently to check to see if server is empty

/*** Nothing to be changed below this line ***/

new curplayers

public change_map()
{
	new curmap[32]
	get_mapname(curmap,32)
	curplayers = get_playersnum2()

	remove_task(2)
	
	if (curplayers == 0)
	{
		if (next_or_specific == 1)
		{
			get_cvar_string("amx_nextmap",map[0],32)
			server_cmd("changelevel %s",map[0])
		}
		else
		{
			new rand = random_num(0,MAPS-1)
			while(equal(curmap,map[rand]))
				rand = random_num(0,MAPS-1)
			server_cmd("changelevel %s",map[rand])
		}
	}
}

public check_players()
{
	curplayers = get_playersnum2()

	if ((curplayers > 0) && (once_or_frequently == 2))
	{
		remove_task(1)
		remove_task(2)
	}
}

public set_tasks()
{
	set_task(time_delay,"change_map",1)
	if (once_or_frequently == 2)
		set_task(check_time,"check_players",2,"",1,"b")
}

public get_playersnum2()
{
	new player_list[32],player_num
	get_players(player_list,player_num,"c")
	return player_num
}

public plugin_init()
{
	register_plugin("Empty Server","0.6","JustinHoMi")
	set_tasks()
	return PLUGIN_CONTINUE
}
