plugins/mapchooser.sp
author Matt Woodrow <pred@alliedmods.net>
Fri Jul 04 22:54:14 2008 +0000 (2008-07-04)
changeset 2156 186f2f16564c
parent 2133 cad0a5ea6742
child 2165 ca3757e676f6
permissions -rw-r--r--
Nominations not blocking current map and excluded maps when used from console.
Removed error causing debug stuff from mapchooser.
     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 	RegPluginLibrary("mapchooser");
   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 	CreateNative("NominateMap", Native_NominateMap);
   165 	CreateNative("InitiateMapChooserVote", Native_InitiateVote);
   166 	CreateNative("CanMapChooserStartVote", Native_CanVoteStart);
   167 	CreateNative("HasEndOfMapVoteFinished", Native_CheckVoteDone);
   168 	CreateNative("GetExcludeMapList", Native_GetExcludeMapList);
   169 
   170 	return true;
   171 }
   172 
   173 public OnConfigsExecuted()
   174 {
   175 	if (ReadMapList(g_MapList,
   176 					 g_mapFileSerial, 
   177 					 "mapchooser",
   178 					 MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
   179 		!= INVALID_HANDLE)
   180 		
   181 	{
   182 		if (g_mapFileSerial == -1)
   183 		{
   184 			LogError("Unable to create a valid map list.");
   185 		}
   186 	}
   187 	
   188 	CreateNextVote();
   189 	SetupTimeleftTimer();
   190 	
   191 	g_TotalRounds = 0;
   192 	
   193 	g_Extends = 0;
   194 	
   195 	g_MapVoteCompleted = false;
   196 	
   197 	g_NominateCount = 0;
   198 	ClearArray(g_NominateList);
   199 	ClearArray(g_NominateOwners);
   200 	
   201 	for (new i=0; i<MAXTEAMS; i++)
   202 	{
   203 		g_winCount[i] = 0;	
   204 	}
   205 	
   206 
   207 	/* Check if mapchooser will attempt to start mapvote during bonus round time - TF2 Only */
   208 	if ((g_Cvar_Bonusroundtime != INVALID_HANDLE) && !GetConVarInt(g_Cvar_StartRounds))
   209 	{
   210 		if (GetConVarFloat(g_Cvar_Bonusroundtime) <= GetConVarFloat(g_Cvar_VoteDuration))
   211 		{
   212 			LogError("Warning - Bonus Round Time shorter than Vote Time. Votes during bonus round may not have time to complete");
   213 		}
   214 	}
   215 }
   216 
   217 public OnMapEnd()
   218 {
   219 	g_HasVoteStarted = false;
   220 	g_WaitingForVote = false;
   221 	
   222 	if (g_VoteTimer != INVALID_HANDLE)
   223 	{
   224 		KillTimer(g_VoteTimer);
   225 		g_VoteTimer = INVALID_HANDLE;
   226 	}
   227 	
   228 	if (g_RetryTimer != INVALID_HANDLE)
   229 	{
   230 		KillTimer(g_RetryTimer);
   231 		g_RetryTimer = INVALID_HANDLE;
   232 	}
   233 	
   234 	decl String:map[32];
   235 	GetCurrentMap(map, sizeof(map));
   236 	PushArrayString(g_OldMapList, map);
   237 				
   238 	if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
   239 	{
   240 		RemoveFromArray(g_OldMapList, 0);
   241 	}	
   242 }
   243 
   244 public OnClientDisconnect(client)
   245 {
   246 	new index = FindValueInArray(g_NominateOwners, client);
   247 	
   248 	if (index == -1)
   249 	{
   250 		return;
   251 	}
   252 	
   253 	new String:oldmap[33];
   254 	GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
   255 	Call_StartForward(g_NominationsResetForward);
   256 	Call_PushString(oldmap);
   257 	Call_PushCell(GetArrayCell(g_NominateOwners, index));
   258 	Call_Finish();
   259 	
   260 	RemoveFromArray(g_NominateOwners, index);
   261 	RemoveFromArray(g_NominateList, index);
   262 	g_NominateCount--;
   263 }
   264 
   265 public EndVoteChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   266 {
   267 	if (newValue[0] == '1')
   268 	{
   269 		SetNextMap("Pending Vote");
   270 	}
   271 	else
   272 	{
   273 		SetNextMap("");	
   274 	}
   275 }
   276 
   277 public OnMapTimeLeftChanged()
   278 {
   279 	if (GetArraySize(g_MapList))
   280 	{
   281 		SetupTimeleftTimer();
   282 	}
   283 }
   284 
   285 SetupTimeleftTimer()
   286 {
   287 	new time;
   288 	if (GetMapTimeLeft(time) && time > 0)
   289 	{
   290 		new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
   291 		if (time - startTime < 0 && GetConVarBool(g_Cvar_EndOfMapVote) && !g_MapVoteCompleted && !g_HasVoteStarted)
   292 		{
   293 			InitiateVote(MapChange_MapEnd, INVALID_HANDLE);		
   294 		}
   295 		else
   296 		{
   297 			if (g_VoteTimer != INVALID_HANDLE)
   298 			{
   299 				KillTimer(g_VoteTimer);
   300 				g_VoteTimer = INVALID_HANDLE;
   301 			}	
   302 			
   303 			g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote);
   304 		}		
   305 	}
   306 }
   307 
   308 public Action:Timer_StartMapVote(Handle:timer)
   309 {
   310 	if (!GetArraySize(g_MapList) || !GetConVarBool(g_Cvar_EndOfMapVote) || g_MapVoteCompleted || g_HasVoteStarted)
   311 	{
   312 		return Plugin_Stop;
   313 	}		
   314 	
   315 	if (timer == g_RetryTimer)
   316 	{
   317 		g_RetryTimer = INVALID_HANDLE;
   318 	}
   319 	else
   320 	{
   321 		g_VoteTimer = INVALID_HANDLE;
   322 	}
   323 	
   324 	InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   325 
   326 	return Plugin_Stop;
   327 }
   328 
   329 public Event_TFRestartRound(Handle:event, const String:name[], bool:dontBroadcast)
   330 {
   331 	/* Game got restarted - reset our round count tracking */
   332 	g_TotalRounds = 0;	
   333 }
   334 
   335 public Event_TeamPlayWinPanel(Handle:event, const String:name[], bool:dontBroadcast)
   336 {
   337 	if (g_ChangeMapAtRoundEnd)
   338 	{
   339 		g_ChangeMapAtRoundEnd = false;
   340 		CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE);
   341 	}
   342 	
   343 	if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted || !GetConVarBool(g_Cvar_EndOfMapVote))
   344 	{
   345 		return;
   346 	}
   347 	
   348 	new bluescore = GetEventInt(event, "blue_score");
   349 	new redscore = GetEventInt(event, "red_score");
   350 	
   351 	if(GetEventInt(event, "round_complete") == 1)
   352 	{	
   353 		switch(GetEventInt(event, "winning_team"))
   354 		{
   355 			case TFTeam_Blue:
   356 			{
   357 				CheckWinLimit(bluescore);
   358 			}
   359 			case TFTeam_Red:
   360 			{
   361 				CheckWinLimit(redscore);				
   362 			}			
   363 			//We need to do nothing on winning_team == 0 this indicates stalemate.
   364 			default:
   365 			{
   366 				return;
   367 			}			
   368 		}
   369 		
   370 		g_TotalRounds++;
   371 		CheckMaxRounds(g_TotalRounds);
   372 	}
   373 }
   374 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
   375 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   376 {
   377 	if (g_ChangeMapAtRoundEnd)
   378 	{
   379 		g_ChangeMapAtRoundEnd = false;
   380 		CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE);
   381 	}
   382 	
   383 	if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted)
   384 	{
   385 		return;
   386 	}
   387 
   388 	new winner = GetEventInt(event, "winner");
   389 	
   390 	if (winner == 0 || winner == 1 || !GetConVarBool(g_Cvar_EndOfMapVote))
   391 	{
   392 		return;
   393 	}
   394 	
   395 	if (winner >= MAXTEAMS)
   396 	{
   397 		SetFailState("Mod exceed maximum team count - Please file a bug report.");	
   398 	}
   399 
   400 	g_TotalRounds++;
   401 	
   402 	g_winCount[winner]++;
   403 	
   404 	CheckWinLimit(g_winCount[winner]);
   405 	CheckMaxRounds(g_TotalRounds);
   406 }
   407 
   408 public CheckWinLimit(winner_score)
   409 {	
   410 	if (g_Cvar_Winlimit != INVALID_HANDLE)
   411 	{
   412 		new winlimit = GetConVarInt(g_Cvar_Winlimit);
   413 		if (winlimit)
   414 		{			
   415 			if (winner_score >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
   416 			{
   417 				InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   418 			}
   419 		}
   420 	}
   421 }
   422 
   423 public CheckMaxRounds(roundcount)
   424 {		
   425 	if (g_Cvar_Maxrounds != INVALID_HANDLE)
   426 	{
   427 		new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   428 		if (maxrounds)
   429 		{
   430 			if (roundcount >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
   431 			{
   432 				InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   433 			}			
   434 		}
   435 	}
   436 }
   437 
   438 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
   439 {
   440 	if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
   441 	{
   442 		return;
   443 	}
   444 	
   445 	if (!GetConVarInt(g_Cvar_Fraglimit) || !GetConVarBool(g_Cvar_EndOfMapVote))
   446 	{
   447 		return;
   448 	}
   449 
   450 	new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
   451 	if (fragger && GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
   452 	{
   453 		InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   454 	}
   455 }
   456 
   457 public Action:Command_Mapvote(client, args)
   458 {
   459 	InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
   460 
   461 	return Plugin_Handled;	
   462 }
   463 
   464 InitiateVote(MapChange:when, Handle:inputlist)
   465 {
   466 	if (!CanVoteStart())
   467 	{
   468 		return;
   469 	}
   470 	
   471 	g_WaitingForVote = true;
   472 	
   473 	if (IsVoteInProgress())
   474 	{
   475 		// Can't start a vote, try again in 5 seconds.
   476 		g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote);
   477 		return;
   478 	}
   479 	
   480 	g_ChangeTime = when;
   481 	
   482 	g_WaitingForVote = false;
   483 		
   484 	g_HasVoteStarted = true;
   485 	g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
   486 	SetMenuTitle(g_VoteMenu, "Vote Nextmap");
   487 	SetVoteResultCallback(g_VoteMenu, Handler_MapVoteFinished);
   488 	
   489 	/**
   490 	 * TODO: Make a proper decision on when to clear the nominations list.
   491 	 * Currently it clears when used, and stays if an external list is provided.
   492 	 * Is this the right thing to do? External lists will probably come from places
   493 	 * like sm_mapvote from the adminmenu in the future.
   494 	 */
   495 	 
   496 	decl String:map[32];
   497 	
   498 	/* No input given - User our internal nominations and maplist */
   499 	if (inputlist == INVALID_HANDLE)
   500 	{
   501 		new nominateCount = GetArraySize(g_NominateList);
   502 		new voteSize = GetConVarInt(g_Cvar_IncludeMaps);
   503 		
   504 		/* Smaller of the two - It should be impossible for nominations to exceed the size though (cvar changed mid-map?) */
   505 		new nominationsToAdd = nominateCount >= voteSize ? voteSize : nominateCount;
   506 		
   507 		
   508 		for (new i=0; i<nominationsToAdd; i++)
   509 		{
   510 			GetArrayString(g_NominateList, i, map, sizeof(map));
   511 			AddMenuItem(g_VoteMenu, map, map);
   512 			
   513 			/* Notify Nominations that this map is now free */
   514 			Call_StartForward(g_NominationsResetForward);
   515 			Call_PushString(map);
   516 			Call_PushCell(GetArrayCell(g_NominateOwners, i));
   517 			Call_Finish();
   518 		}
   519 		
   520 		/* Clear out the rest of the nominations array */
   521 		for (new i=nominationsToAdd; i<nominateCount; i++)
   522 		{
   523 			GetArrayString(g_NominateList, i, map, sizeof(map));
   524 			
   525 			/* Notify Nominations that this map is now free */
   526 			Call_StartForward(g_NominationsResetForward);
   527 			Call_PushString(map);
   528 			Call_PushCell(GetArrayCell(g_NominateOwners, i));
   529 			Call_Finish();			
   530 		}
   531 		
   532 		/* There should currently be 'nominationsToAdd' unique maps in the vote */
   533 		
   534 		new i = nominationsToAdd;
   535 		new count = 0;
   536 		new availableMaps = GetArraySize(g_NextMapList);
   537 		
   538 		while (i < voteSize)
   539 		{
   540 			GetArrayString(g_NextMapList, count, map, sizeof(map));
   541 			count++;
   542 			
   543 			//Check if this map is in the nominate list (and thus already in the vote) */
   544 			if (FindStringInArray(g_NominateList, map) == -1)
   545 			{
   546 				/* Insert the map and increment our count */
   547 				AddMenuItem(g_VoteMenu, map, map);
   548 				i++;
   549 			}
   550 			
   551 			if (count >= availableMaps)
   552 			{
   553 				//Run out of maps, this will have to do.
   554 				break;	
   555 			}
   556 		}
   557 		
   558 		/* Wipe out our nominations list - Nominations have already been informed of this */
   559 		ClearArray(g_NominateOwners);
   560 		ClearArray(g_NominateList);
   561 	}
   562 	else //We were given a list of maps to start the vote with
   563 	{
   564 		new size = GetArraySize(inputlist);
   565 		
   566 		for (new i=0; i<size; i++)
   567 		{
   568 			GetArrayString(inputlist, i, map, sizeof(map));
   569 			
   570 			if (IsMapValid(map))
   571 			{
   572 				AddMenuItem(g_VoteMenu, map, map);
   573 			}	
   574 		}
   575 	}
   576 	
   577 	/* Do we add any special items? */
   578 	if ((when == MapChange_Instant || when == MapChange_RoundEnd) && GetConVarBool(g_Cvar_DontChange))
   579 	{
   580 		AddMenuItem(g_VoteMenu, VOTE_DONTCHANGE, "Don't Change");
   581 	}
   582 	else if (GetConVarBool(g_Cvar_Extend) && g_Extends < GetConVarInt(g_Cvar_Extend))
   583 	{
   584 		AddMenuItem(g_VoteMenu, VOTE_EXTEND, "Extend Map");
   585 	}
   586 	
   587 	new voteDuration = GetConVarInt(g_Cvar_VoteDuration);
   588 
   589 	SetMenuExitButton(g_VoteMenu, false);
   590 	VoteMenuToAll(g_VoteMenu, voteDuration);
   591 
   592 	LogMessage("Voting for next map has started.");
   593 	PrintToChatAll("[SM] %t", "Nextmap Voting Started");
   594 }
   595 
   596 public Handler_MapVoteFinished(Handle:menu,
   597 						   num_votes, 
   598 						   num_clients,
   599 						   const client_info[][2], 
   600 						   num_items,
   601 						   const item_info[][2])
   602 {
   603 	if (num_votes == 0)
   604 	{
   605 		LogError("No Votes recorded yet Advanced callback fired - Tell pRED* to fix this");
   606 		return;	
   607 	}
   608 	
   609 	decl String:map[32];
   610 	GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map));
   611 
   612 	if (strcmp(map, VOTE_EXTEND, false) == 0)
   613 	{
   614 		g_Extends++;
   615 		
   616 		new time;
   617 		if (GetMapTimeLimit(time))
   618 		{
   619 			if (time > 0)
   620 			{
   621 				ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);						
   622 			}
   623 		}
   624 		
   625 		if (g_Cvar_Winlimit != INVALID_HANDLE)
   626 		{
   627 			new winlimit = GetConVarInt(g_Cvar_Winlimit);
   628 			if (winlimit)
   629 			{
   630 				SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
   631 			}					
   632 		}
   633 		
   634 		if (g_Cvar_Maxrounds != INVALID_HANDLE)
   635 		{
   636 			new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
   637 			if (maxrounds)
   638 			{
   639 				SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
   640 			}
   641 		}
   642 		
   643 		if (g_Cvar_Fraglimit != INVALID_HANDLE)
   644 		{
   645 			new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
   646 			if (fraglimit)
   647 			{
   648 				SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));						
   649 			}
   650 		}
   651 
   652 		PrintToChatAll("[SM] %t", "Current Map Extended", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   653 		LogMessage("Voting for next map has finished. The current map has been extended.");
   654 		
   655 		// We extended, so we'll have to vote again.
   656 		g_HasVoteStarted = false;
   657 		CreateNextVote();
   658 		SetupTimeleftTimer();
   659 		
   660 	}
   661 	else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
   662 	{
   663 		PrintToChatAll("[SM] %t", "Current Map Stays", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   664 		LogMessage("Voting for next map has finished. 'No Change' was the winner");
   665 		
   666 		g_HasVoteStarted = false;
   667 		CreateNextVote();
   668 		SetupTimeleftTimer();
   669 	}
   670 	else
   671 	{
   672 		if (g_ChangeTime == MapChange_MapEnd)
   673 		{
   674 			SetNextMap(map);
   675 		}
   676 		else if (g_ChangeTime == MapChange_Instant)
   677 		{
   678 			new Handle:data;
   679 			CreateDataTimer(2.0, Timer_ChangeMap, data);
   680 			WritePackString(data, map);
   681 		}
   682 		else // MapChange_RoundEnd
   683 		{
   684 			SetNextMap(map);
   685 			g_ChangeMapAtRoundEnd = true;
   686 		}
   687 		
   688 		g_HasVoteStarted = false;
   689 		g_MapVoteCompleted = true;
   690 		
   691 		PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map, RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
   692 		LogMessage("Voting for next map has finished. Nextmap: %s.", map);
   693 	}	
   694 }
   695 
   696 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
   697 {
   698 	switch (action)
   699 	{
   700 		case MenuAction_End:
   701 		{
   702 			g_VoteMenu = INVALID_HANDLE;
   703 			CloseHandle(menu);
   704 		}
   705 		
   706 		case MenuAction_Display:
   707 		{
   708 	 		decl String:buffer[255];
   709 			Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
   710 
   711 			new Handle:panel = Handle:param2;
   712 			SetPanelTitle(panel, buffer);
   713 		}		
   714 		
   715 		case MenuAction_DisplayItem:
   716 		{
   717 			if (GetMenuItemCount(menu) - 1 == param2)
   718 			{
   719 				decl String:map[64], String:buffer[255];
   720 				GetMenuItem(menu, param2, map, sizeof(map));
   721 				if (strcmp(map, VOTE_EXTEND, false) == 0)
   722 				{
   723 					Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
   724 					return RedrawMenuItem(buffer);
   725 				}
   726 				else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
   727 				{
   728 					Format(buffer, sizeof(buffer), "%T", "Dont Change", param1);
   729 					return RedrawMenuItem(buffer);					
   730 				}
   731 			}
   732 		}		
   733 	
   734 		case MenuAction_VoteCancel:
   735 		{
   736 			// If we receive 0 votes, pick at random.
   737 			if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
   738 			{
   739 				new count = GetMenuItemCount(menu);
   740 				new item = GetRandomInt(0, count - 1);
   741 				decl String:map[32];
   742 				GetMenuItem(menu, item, map, sizeof(map));
   743 				
   744 				while (strcmp(map, VOTE_EXTEND, false) == 0)
   745 				{
   746 					item = GetRandomInt(0, count - 1);
   747 					GetMenuItem(menu, item, map, sizeof(map));
   748 				}
   749 				
   750 				SetNextMap(map);			
   751 			}
   752 			else
   753 			{
   754 				// We were actually cancelled. What should we do?
   755 				g_HasVoteStarted = false;
   756 				CreateNextVote();
   757 				SetupTimeleftTimer();
   758 			}
   759 		}
   760 	}
   761 	
   762 	return 0;
   763 }
   764 
   765 public Action:Timer_ChangeMap(Handle:hTimer, Handle:dp)
   766 {
   767 	new String:map[65];
   768 	
   769 	if (dp == INVALID_HANDLE)
   770 	{
   771 		if (!GetNextMap(map, sizeof(map)))
   772 		{
   773 			//No passed map and no set nextmap. fail!
   774 			return Plugin_Stop;	
   775 		}
   776 	}
   777 	else
   778 	{
   779 		ResetPack(dp);
   780 		ReadPackString(dp, map, sizeof(map));		
   781 	}
   782 	
   783 	ServerCommand("changelevel \"%s\"", map);
   784 	
   785 	return Plugin_Stop;
   786 }
   787 
   788 CreateNextVote()
   789 {
   790 	if(g_NextMapList != INVALID_HANDLE)
   791 	{
   792 		ClearArray(g_NextMapList);
   793 	}
   794 	
   795 	decl String:map[32];
   796 	new index, Handle:tempMaps  = CloneArray(g_MapList);
   797 	
   798 	GetCurrentMap(map, sizeof(map));
   799 	index = FindStringInArray(tempMaps, map);
   800 	if (index != -1)
   801 	{
   802 		RemoveFromArray(tempMaps, index);
   803 	}	
   804 	
   805 	if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
   806 	{
   807 		for (new i = 0; i < GetArraySize(g_OldMapList); i++)
   808 		{
   809 			GetArrayString(g_OldMapList, i, map, sizeof(map));
   810 			index = FindStringInArray(tempMaps, map);
   811 			if (index != -1)
   812 			{
   813 				RemoveFromArray(tempMaps, index);
   814 			}
   815 		}	
   816 	}
   817 
   818 	new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
   819 	for (new i = 0; i < limit; i++)
   820 	{
   821 		new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
   822 		GetArrayString(tempMaps, b, map, sizeof(map));		
   823 		PushArrayString(g_NextMapList, map);
   824 		RemoveFromArray(tempMaps, b);
   825 	}
   826 	
   827 	CloseHandle(tempMaps);
   828 }
   829 
   830 bool:CanVoteStart()
   831 {
   832 	if (g_WaitingForVote || g_HasVoteStarted)
   833 	{
   834 		return false;	
   835 	}
   836 	
   837 	return true;
   838 }
   839 
   840 NominateResult:InternalNominateMap(String:map[], bool:force, owner)
   841 {
   842 	if (!IsMapValid(map))
   843 	{
   844 		return Nominate_InvalidMap;
   845 	}
   846 	
   847 	new index;
   848 
   849 	if ((index = FindValueInArray(g_NominateOwners, owner)) != -1)
   850 	{
   851 		new String:oldmap[33];
   852 		GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
   853 		Call_StartForward(g_NominationsResetForward);
   854 		Call_PushString(oldmap);
   855 		Call_PushCell(owner);
   856 		Call_Finish();
   857 		
   858 		
   859 		SetArrayString(g_NominateList, index, map);
   860 		return Nominate_Replaced;
   861 	}
   862 	
   863 	/* Too many nominated maps. */
   864 	if (g_NominateCount >= GetConVarInt(g_Cvar_IncludeMaps) && !force)
   865 	{
   866 		return Nominate_VoteFull;
   867 	}
   868 	
   869 	/* Map already in the vote */
   870 	if (FindStringInArray(g_NominateList, map) != -1)
   871 	{
   872 		return Nominate_AlreadyInVote;	
   873 	}
   874 	
   875 	
   876 	PushArrayString(g_NominateList, map);
   877 	PushArrayCell(g_NominateOwners, owner);
   878 	g_NominateCount++;
   879 	
   880 	while (GetArraySize(g_NominateList) > GetConVarInt(g_Cvar_IncludeMaps))
   881 	{
   882 		new String:oldmap[33];
   883 		GetArrayString(g_NominateList, 0, oldmap, sizeof(oldmap));
   884 		Call_StartForward(g_NominationsResetForward);
   885 		Call_PushString(oldmap);
   886 		Call_PushCell(GetArrayCell(g_NominateOwners, 0));
   887 		Call_Finish();
   888 		
   889 		RemoveFromArray(g_NominateList, 0);
   890 		RemoveFromArray(g_NominateOwners, 0);
   891 	}
   892 	
   893 	return Nominate_Added;
   894 }
   895 
   896 /* Add natives to allow nominate and initiate vote to be call */
   897 
   898 /* native  bool:NominateMap(const String:map[], bool:force, &NominateError:error); */
   899 public Native_NominateMap(Handle:plugin, numParams)
   900 {
   901 	new len;
   902 	GetNativeStringLength(1, len);
   903 	
   904 	if (len <= 0)
   905 	{
   906 	  return false;
   907 	}
   908 	
   909 	new String:map[len+1];
   910 	GetNativeString(1, map, len+1);
   911 	
   912 	return _:InternalNominateMap(map, GetNativeCell(2), GetNativeCell(3));
   913 }
   914 
   915 /* native InitiateMapChooserVote(); */
   916 public Native_InitiateVote(Handle:plugin, numParams)
   917 {
   918 	new MapChange:when = MapChange:GetNativeCell(1);
   919 	new Handle:inputarray = Handle:GetNativeCell(2);
   920 	
   921 	LogMessage("Starting map vote because outside request");
   922 	InitiateVote(when, inputarray);
   923 }
   924 
   925 public Native_CanVoteStart(Handle:plugin, numParams)
   926 {
   927 	return CanVoteStart();	
   928 }
   929 
   930 public Native_CheckVoteDone(Handle:plugin, numParams)
   931 {
   932 	return g_MapVoteCompleted;
   933 }
   934 
   935 public Native_GetExcludeMapList(Handle:plugin, numParams)
   936 {
   937 	new Handle:array = Handle:GetNativeCell(1);
   938 	
   939 	if (array == INVALID_HANDLE)
   940 	{
   941 		return;	
   942 	}
   943 	
   944 	
   945 	new size = GetArraySize(g_OldMapList);
   946 	decl String:map[33];
   947 	
   948 	for (new i=0; i<size; i++)
   949 	{
   950 		GetArrayString(g_OldMapList, i, map, sizeof(map));
   951 		PushArrayString(array, map);	
   952 	}
   953 	
   954 	return;
   955 }