plugins/mapchooser.sp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 06:51:21 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2192 7602d7fb60d9
parent 2168 c24a6fa957db
permissions -rw-r--r--
fixed a compat regression, forgot msvc does funky things with overloaded virtual calls
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Mapchooser Plugin
     5  * Creates a map vote at appropriate times, setting sm_nextmap to the winning
     6  * vote
     7  *
     8  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     9  * =============================================================================
    10  *
    11  * This program is free software; you can redistribute it and/or modify it under
    12  * the terms of the GNU General Public License, version 3.0, as published by the
    13  * Free Software Foundation.
    14  * 
    15  * This program is distributed in the hope that it will be useful, but WITHOUT
    16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    18  * details.
    19  *
    20  * You should have received a copy of the GNU General Public License along with
    21  * this program.  If not, see <http://www.gnu.org/licenses/>.
    22  *
    23  * As a special exception, AlliedModders LLC gives you permission to link the
    24  * code of this program (as well as its derivative works) to "Half-Life 2," the
    25  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    26  * by the Valve Corporation.  You must obey the GNU General Public License in
    27  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    28  * this exception to all derivative works.  AlliedModders LLC defines further
    29  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    30  * or <http://www.sourcemod.net/license.php>.
    31  *
    32  * Version: $Id$
    33  */
    34 
    35 #pragma semicolon 1
    36 #include <sourcemod>
    37 #include <mapchooser>
    38 #include <nextmap>
    39 #undef REQUIRE_EXTENSIONS
    40 #include <tf2>
    41 #define REQUIRE_EXTENSIONS
    42 
    43 public Plugin:myinfo =
    44 {
    45 	name = "MapChooser",
    46 	author = "AlliedModders LLC",
    47 	description = "Automated Map Voting",
    48 	version = SOURCEMOD_VERSION,
    49 	url = "http://www.sourcemod.net/"
    50 };
    51 
    52 /* Valve ConVars */
    53 new Handle:g_Cvar_Winlimit = INVALID_HANDLE;
    54 new Handle:g_Cvar_Maxrounds = INVALID_HANDLE;
    55 new Handle:g_Cvar_Fraglimit = INVALID_HANDLE;
    56 new Handle:g_Cvar_Bonusroundtime = INVALID_HANDLE;
    57 
    58 /* Plugin ConVars */
    59 new Handle:g_Cvar_StartTime = INVALID_HANDLE;
    60 new Handle:g_Cvar_StartRounds = INVALID_HANDLE;
    61 new Handle:g_Cvar_StartFrags = INVALID_HANDLE;
    62 new Handle:g_Cvar_ExtendTimeStep = INVALID_HANDLE;
    63 new Handle:g_Cvar_ExtendRoundStep = INVALID_HANDLE;
    64 new Handle:g_Cvar_ExtendFragStep = INVALID_HANDLE;
    65 new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
    66 new Handle:g_Cvar_IncludeMaps = INVALID_HANDLE;
    67 new Handle:g_Cvar_NoVoteMode = INVALID_HANDLE;
    68 new Handle:g_Cvar_Extend = INVALID_HANDLE;
    69 new Handle:g_Cvar_DontChange = INVALID_HANDLE;
    70 new Handle:g_Cvar_EndOfMapVote = INVALID_HANDLE;
    71 new Handle:g_Cvar_VoteDuration = INVALID_HANDLE;
    72 new Handle:g_VoteTimer = INVALID_HANDLE;
    73 new Handle:g_RetryTimer = INVALID_HANDLE;
    74 
    75 /* Data Handles */
    76 new Handle:g_MapList = INVALID_HANDLE;
    77 new Handle:g_NominateList = INVALID_HANDLE;
    78 new Handle:g_NominateOwners = INVALID_HANDLE;
    79 new Handle:g_OldMapList = INVALID_HANDLE;
    80 new Handle:g_NextMapList = INVALID_HANDLE;
    81 new Handle:g_VoteMenu = INVALID_HANDLE;
    82 
    83 new g_Extends;
    84 new g_TotalRounds;
    85 new bool:g_HasVoteStarted;
    86 new bool:g_WaitingForVote;
    87 new bool:g_MapVoteCompleted;
    88 new bool:g_ChangeMapAtRoundEnd;
    89 new g_mapFileSerial = -1;
    90 
    91 new g_NominateCount = 0;
    92 new MapChange:g_ChangeTime;
    93 
    94 new Handle:g_NominationsResetForward = INVALID_HANDLE;
    95 
    96 /* Upper bound of how many team there could be */
    97 #define MAXTEAMS 10
    98 new g_winCount[MAXTEAMS];
    99 
   100 #define VOTE_EXTEND "##extend##"
   101 #define VOTE_DONTCHANGE "##dontchange##"
   102 
   103 public OnPluginStart()
   104 {
   105 	LoadTranslations("mapchooser.phrases");
   106 	
   107 	new arraySize = ByteCountToCells(33);
   108 	g_MapList = CreateArray(arraySize);
   109 	g_NominateList = CreateArray(arraySize);
   110 	g_NominateOwners = CreateArray(1);
   111 	g_OldMapList = CreateArray(arraySize);
   112 	g_NextMapList = CreateArray(arraySize);
   113 	
   114 	g_Cvar_EndOfMapVote = CreateConVar("sm_mapvote_endvote", "1", "Specifies if MapChooser should run an end of map vote", _, true, 0.0, true, 1.0);
   115 	HookConVarChange(g_Cvar_EndOfMapVote, EndVoteChanged);
   116 
   117 	g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
   118 	g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining. Use 0 on TF2 to start vote during bonus round time", _, true, 0.0);
   119 	g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
   120 	g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
   121 	g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 1.0);
   122 	g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);	
   123 	g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
   124 	g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
   125 	g_Cvar_NoVoteMode = CreateConVar("sm_mapvote_novote", "1", "Specifies whether or not MapChooser should pick a map if no votes are received.", _, true, 0.0, true, 1.0);
   126 	g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "0", "Number of extensions allowed each map.", _, true, 0.0);
   127 	g_Cvar_DontChange = CreateConVar("sm_mapvote_dontchange", "1", "Specifies if a 'Don't Change' option should be added to early votes", _, true, 0.0);
   128 	g_Cvar_VoteDuration = CreateConVar("sm_mapvote_voteduration", "20", "Specifies how long the mapvote should be available for.", _, true, 5.0, true, 25.0);
   129 	RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
   130 
   131 	g_Cvar_Winlimit = FindConVar("mp_winlimit");
   132 	g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
   133 	g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
   134 	g_Cvar_Bonusroundtime = FindConVar("mp_bonusroundtime");
   135 	
   136 	if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
   137 	{
   138 		HookEvent("round_end", Event_RoundEnd);
   139 		HookEventEx("teamplay_win_panel", Event_TeamPlayWinPanel);
   140 		HookEventEx("teamplay_restart_round", Event_TFRestartRound);
   141 	}
   142 	
   143 	if (g_Cvar_Fraglimit != INVALID_HANDLE)
   144 	{
   145 		HookEvent("player_death", Event_PlayerDeath);		
   146 	}
   147 	
   148 	AutoExecConfig(true, "mapchooser");
   149 	
   150 	//Change the mp_bonusroundtime max so that we have time to display the vote
   151 	//If you display a vote during bonus time good defaults are 17 vote duration and 19 mp_bonustime
   152 	if (g_Cvar_Bonusroundtime != INVALID_HANDLE)
   153 	{
   154 		SetConVarBounds(g_Cvar_Bonusroundtime, ConVarBound_Upper, true, 30.0);		
   155 	}
   156 	
   157 
   158 	
   159 	g_NominationsResetForward = CreateGlobalForward("OnNominationRemoved", ET_Ignore, Param_String, Param_Cell);
   160 }
   161 
   162 public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
   163 {
   164 	RegPluginLibrary("mapchooser");	
   165 	
   166 	CreateNative("NominateMap", Native_NominateMap);
   167 	CreateNative("InitiateMapChooserVote", Native_InitiateVote);
   168 	CreateNative("CanMapChooserStartVote", Native_CanVoteStart);
   169 	CreateNative("HasEndOfMapVoteFinished", Native_CheckVoteDone);
   170 	CreateNative("GetExcludeMapList", Native_GetExcludeMapList);
   171 
   172 	return true;
   173 }
   174 
   175 public OnConfigsExecuted()
   176 {
   177 	if (ReadMapList(g_MapList,
   178 					 g_mapFileSerial, 
   179 					 "mapchooser",
   180 					 MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
   181 		!= INVALID_HANDLE)
   182 		
   183 	{
   184 		if (g_mapFileSerial == -1)
   185 		{
   186 			LogError("Unable to create a valid map list.");
   187 		}
   188 	}
   189 	
   190 	CreateNextVote();
   191 	SetupTimeleftTimer();
   192 	
   193 	g_TotalRounds = 0;
   194 	
   195 	g_Extends = 0;
   196 	
   197 	g_MapVoteCompleted = false;
   198 	
   199 	g_NominateCount = 0;
   200 	ClearArray(g_NominateList);
   201 	ClearArray(g_NominateOwners);
   202 	
   203 	for (new i=0; i<MAXTEAMS; i++)
   204 	{
   205 		g_winCount[i] = 0;	
   206 	}
   207 	
   208 
   209 	/* Check if mapchooser will attempt to start mapvote during bonus round time - TF2 Only */
   210 	if ((g_Cvar_Bonusroundtime != INVALID_HANDLE) && !GetConVarInt(g_Cvar_StartRounds))
   211 	{
   212 		if (GetConVarFloat(g_Cvar_Bonusroundtime) <= GetConVarFloat(g_Cvar_VoteDuration))
   213 		{
   214 			LogError("Warning - Bonus Round Time shorter than Vote Time. Votes during bonus round may not have time to complete");
   215 		}
   216 	}
   217 }
   218 
   219 public OnMapEnd()
   220 {
   221 	g_HasVoteStarted = false;
   222 	g_WaitingForVote = false;
   223 	
   224 	if (g_VoteTimer != INVALID_HANDLE)
   225 	{
   226 		KillTimer(g_VoteTimer);
   227 		g_VoteTimer = INVALID_HANDLE;
   228 	}
   229 	
   230 	if (g_RetryTimer != INVALID_HANDLE)
   231 	{
   232 		KillTimer(g_RetryTimer);
   233 		g_RetryTimer = INVALID_HANDLE;
   234 	}
   235 	
   236 	decl String:map[32];
   237 	GetCurrentMap(map, sizeof(map));
   238 	PushArrayString(g_OldMapList, map);
   239 				
   240 	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
   241 	{
   242 		RemoveFromArray(g_OldMapList, 0);
   243 	}	
   244 }
   245 
   246 public OnClientDisconnect(client)
   247 {
   248 	new index = FindValueInArray(g_NominateOwners, client);
   249 	
   250 	if (index == -1)
   251 	{
   252 		return;
   253 	}
   254 	
   255 	new String:oldmap[33];
   256 	GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
   257 	Call_StartForward(g_NominationsResetForward);
   258 	Call_PushString(oldmap);
   259 	Call_PushCell(GetArrayCell(g_NominateOwners, index));
   260 	Call_Finish();
   261 	
   262 	RemoveFromArray(g_NominateOwners, index);
   263 	RemoveFromArray(g_NominateList, index);
   264 	g_NominateCount--;
   265 }
   266 
   267 public EndVoteChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   268 {
   269 	if (newValue[0] == '1')
   270 	{
   271 		SetNextMap("Pending Vote");
   272 	}
   273 	else
   274 	{
   275 		SetNextMap("");	
   276 	}
   277 }
   278 
   279 public OnMapTimeLeftChanged()
   280 {
   281 	if (GetArraySize(g_MapList))
   282 	{
   283 		SetupTimeleftTimer();
   284 	}
   285 }
   286 
   287 SetupTimeleftTimer()
   288 {
   289 	new time;
   290 	if (GetMapTimeLeft(time) && time > 0)
   291 	{
   292 		new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
   293 		if (time - startTime < 0 && GetConVarBool(g_Cvar_EndOfMapVote) && !g_MapVoteCompleted && !g_HasVoteStarted)
   294 		{
   295 			InitiateVote(MapChange_MapEnd, INVALID_HANDLE);		
   296 		}
   297 		else
   298 		{
   299 			if (g_VoteTimer != INVALID_HANDLE)
   300 			{
   301 				KillTimer(g_VoteTimer);
   302 				g_VoteTimer = INVALID_HANDLE;
   303 			}	
   304 			
   305 			g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote);
   306 		}		
   307 	}
   308 }
   309 
   310 public Action:Timer_StartMapVote(Handle:timer)
   311 {
   312 	if (!GetArraySize(g_MapList) || !GetConVarBool(g_Cvar_EndOfMapVote) || g_MapVoteCompleted || g_HasVoteStarted)
   313 	{
   314 		return Plugin_Stop;
   315 	}		
   316 	
   317 	if (timer == g_RetryTimer)
   318 	{
   319 		g_RetryTimer = INVALID_HANDLE;
   320 	}
   321 	else
   322 	{
   323 		g_VoteTimer = INVALID_HANDLE;
   324 	}
   325 	
   326 	InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   327 
   328 	return Plugin_Stop;
   329 }
   330 
   331 public Event_TFRestartRound(Handle:event, const String:name[], bool:dontBroadcast)
   332 {
   333 	/* Game got restarted - reset our round count tracking */
   334 	g_TotalRounds = 0;	
   335 }
   336 
   337 public Event_TeamPlayWinPanel(Handle:event, const String:name[], bool:dontBroadcast)
   338 {
   339 	if (g_ChangeMapAtRoundEnd)
   340 	{
   341 		g_ChangeMapAtRoundEnd = false;
   342 		CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE);
   343 	}
   344 	
   345 	if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted || !GetConVarBool(g_Cvar_EndOfMapVote))
   346 	{
   347 		return;
   348 	}
   349 	
   350 	new bluescore = GetEventInt(event, "blue_score");
   351 	new redscore = GetEventInt(event, "red_score");
   352 	
   353 	if(GetEventInt(event, "round_complete") == 1)
   354 	{	
   355 		switch(GetEventInt(event, "winning_team"))
   356 		{
   357 			case TFTeam_Blue:
   358 			{
   359 				CheckWinLimit(bluescore);
   360 			}
   361 			case TFTeam_Red:
   362 			{
   363 				CheckWinLimit(redscore);				
   364 			}			
   365 			//We need to do nothing on winning_team == 0 this indicates stalemate.
   366 			default:
   367 			{
   368 				return;
   369 			}			
   370 		}
   371 		
   372 		g_TotalRounds++;
   373 		CheckMaxRounds(g_TotalRounds);
   374 	}
   375 }
   376 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
   377 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   378 {
   379 	if (g_ChangeMapAtRoundEnd)
   380 	{
   381 		g_ChangeMapAtRoundEnd = false;
   382 		CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE);
   383 	}
   384 	
   385 	if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted)
   386 	{
   387 		return;
   388 	}
   389 
   390 	new winner = GetEventInt(event, "winner");
   391 	
   392 	if (winner == 0 || winner == 1 || !GetConVarBool(g_Cvar_EndOfMapVote))
   393 	{
   394 		return;
   395 	}
   396 	
   397 	if (winner >= MAXTEAMS)
   398 	{
   399 		SetFailState("Mod exceed maximum team count - Please file a bug report.");	
   400 	}
   401 
   402 	g_TotalRounds++;
   403 	
   404 	g_winCount[winner]++;
   405 	
   406 	CheckWinLimit(g_winCount[winner]);
   407 	CheckMaxRounds(g_TotalRounds);
   408 }
   409 
   410 public CheckWinLimit(winner_score)
   411 {	
   412 	if (g_Cvar_Winlimit != INVALID_HANDLE)
   413 	{
   414 		new winlimit = GetConVarInt(g_Cvar_Winlimit);
   415 		if (winlimit)
   416 		{			
   417 			if (winner_score >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
   418 			{
   419 				InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   420 			}
   421 		}
   422 	}
   423 }
   424 
   425 public CheckMaxRounds(roundcount)
   426 {		
   427 	if (g_Cvar_Maxrounds != INVALID_HANDLE)
   428 	{
   429 		new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   430 		if (maxrounds)
   431 		{
   432 			if (roundcount >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
   433 			{
   434 				InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   435 			}			
   436 		}
   437 	}
   438 }
   439 
   440 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
   441 {
   442 	if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
   443 	{
   444 		return;
   445 	}
   446 	
   447 	if (!GetConVarInt(g_Cvar_Fraglimit) || !GetConVarBool(g_Cvar_EndOfMapVote))
   448 	{
   449 		return;
   450 	}
   451 
   452 	new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
   453 	if (fragger && GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
   454 	{
   455 		InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   456 	}
   457 }
   458 
   459 public Action:Command_Mapvote(client, args)
   460 {
   461 	InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   462 
   463 	return Plugin_Handled;	
   464 }
   465 
   466 InitiateVote(MapChange:when, Handle:inputlist)
   467 {
   468 	if (!CanVoteStart())
   469 	{
   470 		return;
   471 	}
   472 	
   473 	g_WaitingForVote = true;
   474 	
   475 	if (IsVoteInProgress())
   476 	{
   477 		// Can't start a vote, try again in 5 seconds.
   478 		g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote);
   479 		return;
   480 	}
   481 	
   482 	g_ChangeTime = when;
   483 	
   484 	g_WaitingForVote = false;
   485 		
   486 	g_HasVoteStarted = true;
   487 	g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
   488 	SetMenuTitle(g_VoteMenu, "Vote Nextmap");
   489 	SetVoteResultCallback(g_VoteMenu, Handler_MapVoteFinished);
   490 	
   491 	/**
   492 	 * TODO: Make a proper decision on when to clear the nominations list.
   493 	 * Currently it clears when used, and stays if an external list is provided.
   494 	 * Is this the right thing to do? External lists will probably come from places
   495 	 * like sm_mapvote from the adminmenu in the future.
   496 	 */
   497 	 
   498 	decl String:map[32];
   499 	
   500 	/* No input given - User our internal nominations and maplist */
   501 	if (inputlist == INVALID_HANDLE)
   502 	{
   503 		new nominateCount = GetArraySize(g_NominateList);
   504 		new voteSize = GetConVarInt(g_Cvar_IncludeMaps);
   505 		
   506 		/* Smaller of the two - It should be impossible for nominations to exceed the size though (cvar changed mid-map?) */
   507 		new nominationsToAdd = nominateCount >= voteSize ? voteSize : nominateCount;
   508 		
   509 		
   510 		for (new i=0; i<nominationsToAdd; i++)
   511 		{
   512 			GetArrayString(g_NominateList, i, map, sizeof(map));
   513 			AddMenuItem(g_VoteMenu, map, map);
   514 			
   515 			/* Notify Nominations that this map is now free */
   516 			Call_StartForward(g_NominationsResetForward);
   517 			Call_PushString(map);
   518 			Call_PushCell(GetArrayCell(g_NominateOwners, i));
   519 			Call_Finish();
   520 		}
   521 		
   522 		/* Clear out the rest of the nominations array */
   523 		for (new i=nominationsToAdd; i<nominateCount; i++)
   524 		{
   525 			GetArrayString(g_NominateList, i, map, sizeof(map));
   526 			
   527 			/* Notify Nominations that this map is now free */
   528 			Call_StartForward(g_NominationsResetForward);
   529 			Call_PushString(map);
   530 			Call_PushCell(GetArrayCell(g_NominateOwners, i));
   531 			Call_Finish();			
   532 		}
   533 		
   534 		/* There should currently be 'nominationsToAdd' unique maps in the vote */
   535 		
   536 		new i = nominationsToAdd;
   537 		new count = 0;
   538 		new availableMaps = GetArraySize(g_NextMapList);
   539 		
   540 		while (i < voteSize)
   541 		{
   542 			GetArrayString(g_NextMapList, count, map, sizeof(map));
   543 			count++;
   544 			
   545 			//Check if this map is in the nominate list (and thus already in the vote) */
   546 			if (FindStringInArray(g_NominateList, map) == -1)
   547 			{
   548 				/* Insert the map and increment our count */
   549 				AddMenuItem(g_VoteMenu, map, map);
   550 				i++;
   551 			}
   552 			
   553 			if (count >= availableMaps)
   554 			{
   555 				//Run out of maps, this will have to do.
   556 				break;	
   557 			}
   558 		}
   559 		
   560 		/* Wipe out our nominations list - Nominations have already been informed of this */
   561 		ClearArray(g_NominateOwners);
   562 		ClearArray(g_NominateList);
   563 	}
   564 	else //We were given a list of maps to start the vote with
   565 	{
   566 		new size = GetArraySize(inputlist);
   567 		
   568 		for (new i=0; i<size; i++)
   569 		{
   570 			GetArrayString(inputlist, i, map, sizeof(map));
   571 			
   572 			if (IsMapValid(map))
   573 			{
   574 				AddMenuItem(g_VoteMenu, map, map);
   575 			}	
   576 		}
   577 	}
   578 	
   579 	/* Do we add any special items? */
   580 	if ((when == MapChange_Instant || when == MapChange_RoundEnd) && GetConVarBool(g_Cvar_DontChange))
   581 	{
   582 		AddMenuItem(g_VoteMenu, VOTE_DONTCHANGE, "Don't Change");
   583 	}
   584 	else if (GetConVarBool(g_Cvar_Extend) && g_Extends < GetConVarInt(g_Cvar_Extend))
   585 	{
   586 		AddMenuItem(g_VoteMenu, VOTE_EXTEND, "Extend Map");
   587 	}
   588 	
   589 	new voteDuration = GetConVarInt(g_Cvar_VoteDuration);
   590 
   591 	SetMenuExitButton(g_VoteMenu, false);
   592 	VoteMenuToAll(g_VoteMenu, voteDuration);
   593 
   594 	LogMessage("Voting for next map has started.");
   595 	PrintToChatAll("[SM] %t", "Nextmap Voting Started");
   596 }
   597 
   598 public Handler_MapVoteFinished(Handle:menu,
   599 						   num_votes, 
   600 						   num_clients,
   601 						   const client_info[][2], 
   602 						   num_items,
   603 						   const item_info[][2])
   604 {
   605 	if (num_votes == 0)
   606 	{
   607 		LogError("No Votes recorded yet Advanced callback fired - Tell pRED* to fix this");
   608 		return;	
   609 	}
   610 	
   611 	decl String:map[32];
   612 	GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map));
   613 
   614 	if (strcmp(map, VOTE_EXTEND, false) == 0)
   615 	{
   616 		g_Extends++;
   617 		
   618 		new time;
   619 		if (GetMapTimeLimit(time))
   620 		{
   621 			if (time > 0)
   622 			{
   623 				ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);						
   624 			}
   625 		}
   626 		
   627 		if (g_Cvar_Winlimit != INVALID_HANDLE)
   628 		{
   629 			new winlimit = GetConVarInt(g_Cvar_Winlimit);
   630 			if (winlimit)
   631 			{
   632 				SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
   633 			}					
   634 		}
   635 		
   636 		if (g_Cvar_Maxrounds != INVALID_HANDLE)
   637 		{
   638 			new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   639 			if (maxrounds)
   640 			{
   641 				SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
   642 			}
   643 		}
   644 		
   645 		if (g_Cvar_Fraglimit != INVALID_HANDLE)
   646 		{
   647 			new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
   648 			if (fraglimit)
   649 			{
   650 				SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));						
   651 			}
   652 		}
   653 
   654 		PrintToChatAll("[SM] %t", "Current Map Extended", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   655 		LogMessage("Voting for next map has finished. The current map has been extended.");
   656 		
   657 		// We extended, so we'll have to vote again.
   658 		g_HasVoteStarted = false;
   659 		CreateNextVote();
   660 		SetupTimeleftTimer();
   661 		
   662 	}
   663 	else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
   664 	{
   665 		PrintToChatAll("[SM] %t", "Current Map Stays", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   666 		LogMessage("Voting for next map has finished. 'No Change' was the winner");
   667 		
   668 		g_HasVoteStarted = false;
   669 		CreateNextVote();
   670 		SetupTimeleftTimer();
   671 	}
   672 	else
   673 	{
   674 		if (g_ChangeTime == MapChange_MapEnd)
   675 		{
   676 			SetNextMap(map);
   677 		}
   678 		else if (g_ChangeTime == MapChange_Instant)
   679 		{
   680 			new Handle:data;
   681 			CreateDataTimer(2.0, Timer_ChangeMap, data);
   682 			WritePackString(data, map);
   683 		}
   684 		else // MapChange_RoundEnd
   685 		{
   686 			SetNextMap(map);
   687 			g_ChangeMapAtRoundEnd = true;
   688 		}
   689 		
   690 		g_HasVoteStarted = false;
   691 		g_MapVoteCompleted = true;
   692 		
   693 		PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map, RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   694 		LogMessage("Voting for next map has finished. Nextmap: %s.", map);
   695 	}	
   696 }
   697 
   698 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
   699 {
   700 	switch (action)
   701 	{
   702 		case MenuAction_End:
   703 		{
   704 			g_VoteMenu = INVALID_HANDLE;
   705 			CloseHandle(menu);
   706 		}
   707 		
   708 		case MenuAction_Display:
   709 		{
   710 	 		decl String:buffer[255];
   711 			Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
   712 
   713 			new Handle:panel = Handle:param2;
   714 			SetPanelTitle(panel, buffer);
   715 		}		
   716 		
   717 		case MenuAction_DisplayItem:
   718 		{
   719 			if (GetMenuItemCount(menu) - 1 == param2)
   720 			{
   721 				decl String:map[64], String:buffer[255];
   722 				GetMenuItem(menu, param2, map, sizeof(map));
   723 				if (strcmp(map, VOTE_EXTEND, false) == 0)
   724 				{
   725 					Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
   726 					return RedrawMenuItem(buffer);
   727 				}
   728 				else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
   729 				{
   730 					Format(buffer, sizeof(buffer), "%T", "Dont Change", param1);
   731 					return RedrawMenuItem(buffer);					
   732 				}
   733 			}
   734 		}		
   735 	
   736 		case MenuAction_VoteCancel:
   737 		{
   738 			// If we receive 0 votes, pick at random.
   739 			if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
   740 			{
   741 				new count = GetMenuItemCount(menu);
   742 				new item = GetRandomInt(0, count - 1);
   743 				decl String:map[32];
   744 				GetMenuItem(menu, item, map, sizeof(map));
   745 				
   746 				while (strcmp(map, VOTE_EXTEND, false) == 0)
   747 				{
   748 					item = GetRandomInt(0, count - 1);
   749 					GetMenuItem(menu, item, map, sizeof(map));
   750 				}
   751 				
   752 				SetNextMap(map);			
   753 			}
   754 			else
   755 			{
   756 				// We were actually cancelled. What should we do?
   757 				g_HasVoteStarted = false;
   758 				CreateNextVote();
   759 				SetupTimeleftTimer();
   760 			}
   761 		}
   762 	}
   763 	
   764 	return 0;
   765 }
   766 
   767 public Action:Timer_ChangeMap(Handle:hTimer, Handle:dp)
   768 {
   769 	new String:map[65];
   770 	
   771 	if (dp == INVALID_HANDLE)
   772 	{
   773 		if (!GetNextMap(map, sizeof(map)))
   774 		{
   775 			//No passed map and no set nextmap. fail!
   776 			return Plugin_Stop;	
   777 		}
   778 	}
   779 	else
   780 	{
   781 		ResetPack(dp);
   782 		ReadPackString(dp, map, sizeof(map));		
   783 	}
   784 	
   785 	ServerCommand("changelevel \"%s\"", map);
   786 	
   787 	return Plugin_Stop;
   788 }
   789 
   790 CreateNextVote()
   791 {
   792 	if(g_NextMapList != INVALID_HANDLE)
   793 	{
   794 		ClearArray(g_NextMapList);
   795 	}
   796 	
   797 	decl String:map[32];
   798 	new index, Handle:tempMaps  = CloneArray(g_MapList);
   799 	
   800 	GetCurrentMap(map, sizeof(map));
   801 	index = FindStringInArray(tempMaps, map);
   802 	if (index != -1)
   803 	{
   804 		RemoveFromArray(tempMaps, index);
   805 	}	
   806 	
   807 	if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
   808 	{
   809 		for (new i = 0; i < GetArraySize(g_OldMapList); i++)
   810 		{
   811 			GetArrayString(g_OldMapList, i, map, sizeof(map));
   812 			index = FindStringInArray(tempMaps, map);
   813 			if (index != -1)
   814 			{
   815 				RemoveFromArray(tempMaps, index);
   816 			}
   817 		}	
   818 	}
   819 
   820 	new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
   821 	for (new i = 0; i < limit; i++)
   822 	{
   823 		new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
   824 		GetArrayString(tempMaps, b, map, sizeof(map));		
   825 		PushArrayString(g_NextMapList, map);
   826 		RemoveFromArray(tempMaps, b);
   827 	}
   828 	
   829 	CloseHandle(tempMaps);
   830 }
   831 
   832 bool:CanVoteStart()
   833 {
   834 	if (g_WaitingForVote || g_HasVoteStarted)
   835 	{
   836 		return false;	
   837 	}
   838 	
   839 	return true;
   840 }
   841 
   842 NominateResult:InternalNominateMap(String:map[], bool:force, owner)
   843 {
   844 	if (!IsMapValid(map))
   845 	{
   846 		return Nominate_InvalidMap;
   847 	}
   848 	
   849 	new index;
   850 
   851 	if ((index = FindValueInArray(g_NominateOwners, owner)) != -1)
   852 	{
   853 		new String:oldmap[33];
   854 		GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
   855 		Call_StartForward(g_NominationsResetForward);
   856 		Call_PushString(oldmap);
   857 		Call_PushCell(owner);
   858 		Call_Finish();
   859 		
   860 		
   861 		SetArrayString(g_NominateList, index, map);
   862 		return Nominate_Replaced;
   863 	}
   864 	
   865 	/* Too many nominated maps. */
   866 	if (g_NominateCount >= GetConVarInt(g_Cvar_IncludeMaps) && !force)
   867 	{
   868 		return Nominate_VoteFull;
   869 	}
   870 	
   871 	/* Map already in the vote */
   872 	if (FindStringInArray(g_NominateList, map) != -1)
   873 	{
   874 		return Nominate_AlreadyInVote;	
   875 	}
   876 	
   877 	
   878 	PushArrayString(g_NominateList, map);
   879 	PushArrayCell(g_NominateOwners, owner);
   880 	g_NominateCount++;
   881 	
   882 	while (GetArraySize(g_NominateList) > GetConVarInt(g_Cvar_IncludeMaps))
   883 	{
   884 		new String:oldmap[33];
   885 		GetArrayString(g_NominateList, 0, oldmap, sizeof(oldmap));
   886 		Call_StartForward(g_NominationsResetForward);
   887 		Call_PushString(oldmap);
   888 		Call_PushCell(GetArrayCell(g_NominateOwners, 0));
   889 		Call_Finish();
   890 		
   891 		RemoveFromArray(g_NominateList, 0);
   892 		RemoveFromArray(g_NominateOwners, 0);
   893 	}
   894 	
   895 	return Nominate_Added;
   896 }
   897 
   898 /* Add natives to allow nominate and initiate vote to be call */
   899 
   900 /* native  bool:NominateMap(const String:map[], bool:force, &NominateError:error); */
   901 public Native_NominateMap(Handle:plugin, numParams)
   902 {
   903 	new len;
   904 	GetNativeStringLength(1, len);
   905 	
   906 	if (len <= 0)
   907 	{
   908 	  return false;
   909 	}
   910 	
   911 	new String:map[len+1];
   912 	GetNativeString(1, map, len+1);
   913 	
   914 	return _:InternalNominateMap(map, GetNativeCell(2), GetNativeCell(3));
   915 }
   916 
   917 /* native InitiateMapChooserVote(); */
   918 public Native_InitiateVote(Handle:plugin, numParams)
   919 {
   920 	new MapChange:when = MapChange:GetNativeCell(1);
   921 	new Handle:inputarray = Handle:GetNativeCell(2);
   922 	
   923 	LogMessage("Starting map vote because outside request");
   924 	InitiateVote(when, inputarray);
   925 }
   926 
   927 public Native_CanVoteStart(Handle:plugin, numParams)
   928 {
   929 	return CanVoteStart();	
   930 }
   931 
   932 public Native_CheckVoteDone(Handle:plugin, numParams)
   933 {
   934 	return g_MapVoteCompleted;
   935 }
   936 
   937 public Native_GetExcludeMapList(Handle:plugin, numParams)
   938 {
   939 	new Handle:array = Handle:GetNativeCell(1);
   940 	
   941 	if (array == INVALID_HANDLE)
   942 	{
   943 		return;	
   944 	}
   945 	
   946 	
   947 	new size = GetArraySize(g_OldMapList);
   948 	decl String:map[33];
   949 	
   950 	for (new i=0; i<size; i++)
   951 	{
   952 		GetArrayString(g_OldMapList, i, map, sizeof(map));
   953 		PushArrayString(array, map);	
   954 	}
   955 	
   956 	return;
   957 }