3 * =============================================================================
4 * SourceMod Mapchooser Plugin
5 * Creates a map vote at appropriate times, setting sm_nextmap to the winning
8 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
9 * =============================================================================
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.
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
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/>.
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>.
38 #define assert(%1) if (!(%1)) ThrowError("Debug Assertion Failed");
39 #define assert_msg(%1,%2) if (!(%1)) ThrowError(%2);
42 #define assert_msg(%1,%2)
50 public Plugin:myinfo =
53 author = "AlliedModders LLC",
54 description = "Automated Map Voting",
55 version = SOURCEMOD_VERSION,
56 url = "http://www.sourcemod.net/"
60 new Handle:g_Cvar_Winlimit = INVALID_HANDLE;
61 new Handle:g_Cvar_Maxrounds = INVALID_HANDLE;
62 new Handle:g_Cvar_Fraglimit = INVALID_HANDLE;
63 new Handle:g_Cvar_Bonusroundtime = INVALID_HANDLE;
66 new Handle:g_Cvar_StartTime = INVALID_HANDLE;
67 new Handle:g_Cvar_StartRounds = INVALID_HANDLE;
68 new Handle:g_Cvar_StartFrags = INVALID_HANDLE;
69 new Handle:g_Cvar_ExtendTimeStep = INVALID_HANDLE;
70 new Handle:g_Cvar_ExtendRoundStep = INVALID_HANDLE;
71 new Handle:g_Cvar_ExtendFragStep = INVALID_HANDLE;
72 new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
73 new Handle:g_Cvar_IncludeMaps = INVALID_HANDLE;
74 new Handle:g_Cvar_NoVoteMode = INVALID_HANDLE;
75 new Handle:g_Cvar_Extend = INVALID_HANDLE;
76 new Handle:g_Cvar_DontChange = INVALID_HANDLE;
77 new Handle:g_Cvar_EndOfMapVote = INVALID_HANDLE;
78 new Handle:g_Cvar_VoteDuration = INVALID_HANDLE;
79 new Handle:g_Cvar_RunOff = INVALID_HANDLE;
80 new Handle:g_Cvar_RunOffPercent = INVALID_HANDLE;
82 new Handle:g_VoteTimer = INVALID_HANDLE;
83 new Handle:g_RetryTimer = INVALID_HANDLE;
86 new Handle:g_MapList = INVALID_HANDLE;
87 new Handle:g_NominateList = INVALID_HANDLE;
88 new Handle:g_NominateOwners = INVALID_HANDLE;
89 new Handle:g_OldMapList = INVALID_HANDLE;
90 new Handle:g_NextMapList = INVALID_HANDLE;
91 new Handle:g_VoteMenu = INVALID_HANDLE;
95 new bool:g_HasVoteStarted;
96 new bool:g_WaitingForVote;
97 new bool:g_MapVoteCompleted;
98 new bool:g_ChangeMapAtRoundEnd;
99 new bool:g_ChangeMapInProgress;
100 new g_mapFileSerial = -1;
102 new g_NominateCount = 0;
103 new MapChange:g_ChangeTime;
105 new Handle:g_NominationsResetForward = INVALID_HANDLE;
106 new Handle:g_MapVoteStartedForward = INVALID_HANDLE;
108 /* Upper bound of how many team there could be */
110 new g_winCount[MAXTEAMS];
112 #define VOTE_EXTEND "##extend##"
113 #define VOTE_DONTCHANGE "##dontchange##"
115 public OnPluginStart()
117 LoadTranslations("mapchooser.phrases");
118 LoadTranslations("common.phrases");
120 new arraySize = ByteCountToCells(33);
121 g_MapList = CreateArray(arraySize);
122 g_NominateList = CreateArray(arraySize);
123 g_NominateOwners = CreateArray(1);
124 g_OldMapList = CreateArray(arraySize);
125 g_NextMapList = CreateArray(arraySize);
127 g_Cvar_EndOfMapVote = CreateConVar("sm_mapvote_endvote", "1", "Specifies if MapChooser should run an end of map vote", _, true, 0.0, true, 1.0);
129 g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
130 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);
131 g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
132 g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
133 g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 1.0);
134 g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
135 g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
136 g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
137 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);
138 g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "0", "Number of extensions allowed each map.", _, true, 0.0);
139 g_Cvar_DontChange = CreateConVar("sm_mapvote_dontchange", "1", "Specifies if a 'Don't Change' option should be added to early votes", _, true, 0.0);
140 g_Cvar_VoteDuration = CreateConVar("sm_mapvote_voteduration", "20", "Specifies how long the mapvote should be available for.", _, true, 5.0);
141 g_Cvar_RunOff = CreateConVar("sm_mapvote_runoff", "0", "Hold run of votes if winning choice is less than a certain margin", _, true, 0.0, true, 1.0);
142 g_Cvar_RunOffPercent = CreateConVar("sm_mapvote_runoffpercent", "50", "If winning choice has less than this percent of votes, hold a runoff", _, true, 0.0, true, 100.0);
144 RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
145 RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
147 g_Cvar_Winlimit = FindConVar("mp_winlimit");
148 g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
149 g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
150 g_Cvar_Bonusroundtime = FindConVar("mp_bonusroundtime");
152 if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
154 decl String:folder[64];
155 GetGameFolderName(folder, sizeof(folder));
157 if (strcmp(folder, "tf") == 0)
159 HookEvent("teamplay_win_panel", Event_TeamPlayWinPanel);
160 HookEvent("teamplay_restart_round", Event_TFRestartRound);
161 HookEvent("arena_win_panel", Event_TeamPlayWinPanel);
163 else if (strcmp(folder, "nucleardawn") == 0)
165 HookEvent("round_win", Event_RoundEnd);
169 HookEvent("round_end", Event_RoundEnd);
173 if (g_Cvar_Fraglimit != INVALID_HANDLE)
175 HookEvent("player_death", Event_PlayerDeath);
178 AutoExecConfig(true, "mapchooser");
180 //Change the mp_bonusroundtime max so that we have time to display the vote
181 //If you display a vote during bonus time good defaults are 17 vote duration and 19 mp_bonustime
182 if (g_Cvar_Bonusroundtime != INVALID_HANDLE)
184 SetConVarBounds(g_Cvar_Bonusroundtime, ConVarBound_Upper, true, 30.0);
187 g_NominationsResetForward = CreateGlobalForward("OnNominationRemoved", ET_Ignore, Param_String, Param_Cell);
188 g_MapVoteStartedForward = CreateGlobalForward("OnMapVoteStarted", ET_Ignore);
191 public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
193 RegPluginLibrary("mapchooser");
195 CreateNative("NominateMap", Native_NominateMap);
196 CreateNative("RemoveNominationByMap", Native_RemoveNominationByMap);
197 CreateNative("RemoveNominationByOwner", Native_RemoveNominationByOwner);
198 CreateNative("InitiateMapChooserVote", Native_InitiateVote);
199 CreateNative("CanMapChooserStartVote", Native_CanVoteStart);
200 CreateNative("HasEndOfMapVoteFinished", Native_CheckVoteDone);
201 CreateNative("GetExcludeMapList", Native_GetExcludeMapList);
202 CreateNative("GetNominatedMapList", Native_GetNominatedMapList);
203 CreateNative("EndOfMapVoteEnabled", Native_EndOfMapVoteEnabled);
205 return APLRes_Success;
208 public OnConfigsExecuted()
210 if (ReadMapList(g_MapList,
213 MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
217 if (g_mapFileSerial == -1)
219 LogError("Unable to create a valid map list.");
224 SetupTimeleftTimer();
230 g_MapVoteCompleted = false;
233 ClearArray(g_NominateList);
234 ClearArray(g_NominateOwners);
236 for (new i=0; i<MAXTEAMS; i++)
242 /* Check if mapchooser will attempt to start mapvote during bonus round time - TF2 Only */
243 if ((g_Cvar_Bonusroundtime != INVALID_HANDLE) && !GetConVarInt(g_Cvar_StartRounds))
245 if (GetConVarFloat(g_Cvar_Bonusroundtime) <= GetConVarFloat(g_Cvar_VoteDuration))
247 LogError("Warning - Bonus Round Time shorter than Vote Time. Votes during bonus round may not have time to complete");
254 g_HasVoteStarted = false;
255 g_WaitingForVote = false;
256 g_ChangeMapAtRoundEnd = false;
257 g_ChangeMapInProgress = false;
259 g_VoteTimer = INVALID_HANDLE;
260 g_RetryTimer = INVALID_HANDLE;
263 GetCurrentMap(map, sizeof(map));
264 PushArrayString(g_OldMapList, map);
266 if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
268 RemoveFromArray(g_OldMapList, 0);
272 public OnClientDisconnect(client)
274 new index = FindValueInArray(g_NominateOwners, client);
281 new String:oldmap[33];
282 GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
283 Call_StartForward(g_NominationsResetForward);
284 Call_PushString(oldmap);
285 Call_PushCell(GetArrayCell(g_NominateOwners, index));
288 RemoveFromArray(g_NominateOwners, index);
289 RemoveFromArray(g_NominateList, index);
293 public Action:Command_SetNextmap(client, args)
297 ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
298 return Plugin_Handled;
302 GetCmdArg(1, map, sizeof(map));
304 if (!IsMapValid(map))
306 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
307 return Plugin_Handled;
310 ShowActivity(client, "%t", "Changed Next Map", map);
311 LogAction(client, -1, "\"%L\" changed nextmap to \"%s\"", client, map);
314 g_MapVoteCompleted = true;
316 return Plugin_Handled;
319 public OnMapTimeLeftChanged()
321 if (GetArraySize(g_MapList))
323 SetupTimeleftTimer();
330 if (GetMapTimeLeft(time) && time > 0)
332 new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
333 if (time - startTime < 0 && GetConVarBool(g_Cvar_EndOfMapVote) && !g_MapVoteCompleted && !g_HasVoteStarted)
335 InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
339 if (g_VoteTimer != INVALID_HANDLE)
341 KillTimer(g_VoteTimer);
342 g_VoteTimer = INVALID_HANDLE;
345 //g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, _, TIMER_FLAG_NO_MAPCHANGE);
347 g_VoteTimer = CreateDataTimer(float(time - startTime), Timer_StartMapVote, data, TIMER_FLAG_NO_MAPCHANGE);
348 WritePackCell(data, _:MapChange_MapEnd);
349 WritePackCell(data, _:INVALID_HANDLE);
355 public Action:Timer_StartMapVote(Handle:timer, Handle:data)
357 if (timer == g_RetryTimer)
359 g_WaitingForVote = false;
360 g_RetryTimer = INVALID_HANDLE;
364 g_VoteTimer = INVALID_HANDLE;
367 if (!GetArraySize(g_MapList) || !GetConVarBool(g_Cvar_EndOfMapVote) || g_MapVoteCompleted || g_HasVoteStarted)
372 new MapChange:mapChange = MapChange:ReadPackCell(data);
373 new Handle:hndl = Handle:ReadPackCell(data);
375 InitiateVote(mapChange, hndl);
380 public Event_TFRestartRound(Handle:event, const String:name[], bool:dontBroadcast)
382 /* Game got restarted - reset our round count tracking */
386 public Event_TeamPlayWinPanel(Handle:event, const String:name[], bool:dontBroadcast)
388 if (g_ChangeMapAtRoundEnd)
390 g_ChangeMapAtRoundEnd = false;
391 CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
392 g_ChangeMapInProgress = true;
395 new bluescore = GetEventInt(event, "blue_score");
396 new redscore = GetEventInt(event, "red_score");
398 if(GetEventInt(event, "round_complete") == 1 || StrEqual(name, "arena_win_panel"))
402 if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted || !GetConVarBool(g_Cvar_EndOfMapVote))
407 CheckMaxRounds(g_TotalRounds);
409 switch(GetEventInt(event, "winning_team"))
413 CheckWinLimit(bluescore);
417 CheckWinLimit(redscore);
419 //We need to do nothing on winning_team == 0 this indicates stalemate.
427 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
428 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
430 if (g_ChangeMapAtRoundEnd)
432 g_ChangeMapAtRoundEnd = false;
433 CreateTimer(2.0, Timer_ChangeMap, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
434 g_ChangeMapInProgress = true;
438 if (strcmp(name, "round_win") == 0)
441 winner = GetEventInt(event, "team");
445 winner = GetEventInt(event, "winner");
448 if (winner == 0 || winner == 1 || !GetConVarBool(g_Cvar_EndOfMapVote))
453 if (winner >= MAXTEAMS)
455 SetFailState("Mod exceed maximum team count - Please file a bug report.");
460 g_winCount[winner]++;
462 if (!GetArraySize(g_MapList) || g_HasVoteStarted || g_MapVoteCompleted)
467 CheckWinLimit(g_winCount[winner]);
468 CheckMaxRounds(g_TotalRounds);
471 public CheckWinLimit(winner_score)
473 if (g_Cvar_Winlimit != INVALID_HANDLE)
475 new winlimit = GetConVarInt(g_Cvar_Winlimit);
478 if (winner_score >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
480 InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
486 public CheckMaxRounds(roundcount)
488 if (g_Cvar_Maxrounds != INVALID_HANDLE)
490 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
493 if (roundcount >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
495 InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
501 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
503 if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
508 if (!GetConVarInt(g_Cvar_Fraglimit) || !GetConVarBool(g_Cvar_EndOfMapVote))
513 if (g_MapVoteCompleted)
518 new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
525 if (GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
527 InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
531 public Action:Command_Mapvote(client, args)
533 InitiateVote(MapChange_MapEnd, INVALID_HANDLE);
535 return Plugin_Handled;
539 * Starts a new map vote
541 * @param when When the resulting map change should occur.
542 * @param inputlist Optional list of maps to use for the vote, otherwise an internal list of nominations + random maps will be used.
543 * @param noSpecials Block special vote options like extend/nochange (upgrade this to bitflags instead?)
545 InitiateVote(MapChange:when, Handle:inputlist=INVALID_HANDLE)
547 g_WaitingForVote = true;
549 if (IsVoteInProgress())
551 // Can't start a vote, try again in 5 seconds.
552 //g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote, _, TIMER_FLAG_NO_MAPCHANGE);
555 g_RetryTimer = CreateDataTimer(5.0, Timer_StartMapVote, data, TIMER_FLAG_NO_MAPCHANGE);
556 WritePackCell(data, _:when);
557 WritePackCell(data, _:inputlist);
562 /* If the main map vote has completed (and chosen result) and its currently changing (not a delayed change) we block further attempts */
563 if (g_MapVoteCompleted && g_ChangeMapInProgress)
570 g_WaitingForVote = false;
572 g_HasVoteStarted = true;
573 g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
574 SetMenuTitle(g_VoteMenu, "Vote Nextmap");
575 SetVoteResultCallback(g_VoteMenu, Handler_MapVoteFinished);
577 /* Call OnMapVoteStarted() Forward */
578 Call_StartForward(g_MapVoteStartedForward);
582 * TODO: Make a proper decision on when to clear the nominations list.
583 * Currently it clears when used, and stays if an external list is provided.
584 * Is this the right thing to do? External lists will probably come from places
585 * like sm_mapvote from the adminmenu in the future.
590 /* No input given - User our internal nominations and maplist */
591 if (inputlist == INVALID_HANDLE)
593 new nominateCount = GetArraySize(g_NominateList);
594 new voteSize = GetConVarInt(g_Cvar_IncludeMaps);
596 /* Smaller of the two - It should be impossible for nominations to exceed the size though (cvar changed mid-map?) */
597 new nominationsToAdd = nominateCount >= voteSize ? voteSize : nominateCount;
600 for (new i=0; i<nominationsToAdd; i++)
602 GetArrayString(g_NominateList, i, map, sizeof(map));
603 AddMenuItem(g_VoteMenu, map, map);
604 RemoveStringFromArray(g_NextMapList, map);
606 /* Notify Nominations that this map is now free */
607 Call_StartForward(g_NominationsResetForward);
608 Call_PushString(map);
609 Call_PushCell(GetArrayCell(g_NominateOwners, i));
613 /* Clear out the rest of the nominations array */
614 for (new i=nominationsToAdd; i<nominateCount; i++)
616 GetArrayString(g_NominateList, i, map, sizeof(map));
617 /* These maps shouldn't be excluded from the vote as they weren't really nominated at all */
619 /* Notify Nominations that this map is now free */
620 Call_StartForward(g_NominationsResetForward);
621 Call_PushString(map);
622 Call_PushCell(GetArrayCell(g_NominateOwners, i));
626 /* There should currently be 'nominationsToAdd' unique maps in the vote */
628 new i = nominationsToAdd;
630 new availableMaps = GetArraySize(g_NextMapList);
634 GetArrayString(g_NextMapList, count, map, sizeof(map));
637 /* Insert the map and increment our count */
638 AddMenuItem(g_VoteMenu, map, map);
641 if (count >= availableMaps)
643 //Run out of maps, this will have to do.
648 /* Wipe out our nominations list - Nominations have already been informed of this */
649 ClearArray(g_NominateOwners);
650 ClearArray(g_NominateList);
652 else //We were given a list of maps to start the vote with
654 new size = GetArraySize(inputlist);
656 for (new i=0; i<size; i++)
658 GetArrayString(inputlist, i, map, sizeof(map));
662 AddMenuItem(g_VoteMenu, map, map);
667 /* Do we add any special items? */
668 if ((when == MapChange_Instant || when == MapChange_RoundEnd) && GetConVarBool(g_Cvar_DontChange))
670 AddMenuItem(g_VoteMenu, VOTE_DONTCHANGE, "Don't Change");
672 else if (GetConVarBool(g_Cvar_Extend) && g_Extends < GetConVarInt(g_Cvar_Extend))
674 AddMenuItem(g_VoteMenu, VOTE_EXTEND, "Extend Map");
677 new voteDuration = GetConVarInt(g_Cvar_VoteDuration);
679 SetMenuExitButton(g_VoteMenu, false);
680 VoteMenuToAll(g_VoteMenu, voteDuration);
682 LogAction(-1, -1, "Voting for next map has started.");
683 PrintToChatAll("[SM] %t", "Nextmap Voting Started");
686 public Handler_VoteFinishedGeneric(Handle:menu,
689 const client_info[][2],
691 const item_info[][2])
694 GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map));
696 if (strcmp(map, VOTE_EXTEND, false) == 0)
701 if (GetMapTimeLimit(time))
705 ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);
709 if (g_Cvar_Winlimit != INVALID_HANDLE)
711 new winlimit = GetConVarInt(g_Cvar_Winlimit);
714 SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
718 if (g_Cvar_Maxrounds != INVALID_HANDLE)
720 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
723 SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
727 if (g_Cvar_Fraglimit != INVALID_HANDLE)
729 new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
732 SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));
736 PrintToChatAll("[SM] %t", "Current Map Extended", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
737 LogAction(-1, -1, "Voting for next map has finished. The current map has been extended.");
739 // We extended, so we'll have to vote again.
740 g_HasVoteStarted = false;
742 SetupTimeleftTimer();
745 else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
747 PrintToChatAll("[SM] %t", "Current Map Stays", RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
748 LogAction(-1, -1, "Voting for next map has finished. 'No Change' was the winner");
750 g_HasVoteStarted = false;
752 SetupTimeleftTimer();
756 if (g_ChangeTime == MapChange_MapEnd)
760 else if (g_ChangeTime == MapChange_Instant)
763 CreateDataTimer(2.0, Timer_ChangeMap, data);
764 WritePackString(data, map);
765 g_ChangeMapInProgress = false;
767 else // MapChange_RoundEnd
770 g_ChangeMapAtRoundEnd = true;
773 g_HasVoteStarted = false;
774 g_MapVoteCompleted = true;
776 PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map, RoundToFloor(float(item_info[0][VOTEINFO_ITEM_VOTES])/float(num_votes)*100), num_votes);
777 LogAction(-1, -1, "Voting for next map has finished. Nextmap: %s.", map);
781 public Handler_MapVoteFinished(Handle:menu,
784 const client_info[][2],
786 const item_info[][2])
788 if (GetConVarBool(g_Cvar_RunOff) && num_items > 1)
790 new Float:winningvotes = float(item_info[0][VOTEINFO_ITEM_VOTES]);
791 new Float:required = num_votes * (GetConVarFloat(g_Cvar_RunOffPercent) / 100.0);
793 if (winningvotes <= required)
795 /* Insufficient Winning margin - Lets do a runoff */
796 g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
797 SetMenuTitle(g_VoteMenu, "Runoff Vote Nextmap");
798 SetVoteResultCallback(g_VoteMenu, Handler_VoteFinishedGeneric);
801 decl String:info1[32];
802 decl String:info2[32];
804 GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], map, sizeof(map), _, info1, sizeof(info1));
805 AddMenuItem(g_VoteMenu, map, info1);
806 GetMenuItem(menu, item_info[1][VOTEINFO_ITEM_INDEX], map, sizeof(map), _, info2, sizeof(info2));
807 AddMenuItem(g_VoteMenu, map, info2);
809 new voteDuration = GetConVarInt(g_Cvar_VoteDuration);
810 SetMenuExitButton(g_VoteMenu, false);
811 VoteMenuToAll(g_VoteMenu, voteDuration);
814 new Float:map1percent = float(item_info[0][VOTEINFO_ITEM_VOTES])/ float(num_votes) * 100;
815 new Float:map2percent = float(item_info[1][VOTEINFO_ITEM_VOTES])/ float(num_votes) * 100;
818 PrintToChatAll("[SM] %t", "Starting Runoff", GetConVarFloat(g_Cvar_RunOffPercent), info1, map1percent, info2, map2percent);
819 LogMessage("Voting for next map was indecisive, beginning runoff vote");
825 Handler_VoteFinishedGeneric(menu, num_votes, num_clients, client_info, num_items, item_info);
828 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
834 g_VoteMenu = INVALID_HANDLE;
838 case MenuAction_Display:
840 decl String:buffer[255];
841 Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
843 new Handle:panel = Handle:param2;
844 SetPanelTitle(panel, buffer);
847 case MenuAction_DisplayItem:
849 if (GetMenuItemCount(menu) - 1 == param2)
851 decl String:map[64], String:buffer[255];
852 GetMenuItem(menu, param2, map, sizeof(map));
853 if (strcmp(map, VOTE_EXTEND, false) == 0)
855 Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
856 return RedrawMenuItem(buffer);
858 else if (strcmp(map, VOTE_DONTCHANGE, false) == 0)
860 Format(buffer, sizeof(buffer), "%T", "Dont Change", param1);
861 return RedrawMenuItem(buffer);
866 case MenuAction_VoteCancel:
868 // If we receive 0 votes, pick at random.
869 if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
871 new count = GetMenuItemCount(menu);
872 new item = GetRandomInt(0, count - 1);
874 GetMenuItem(menu, item, map, sizeof(map));
876 while (strcmp(map, VOTE_EXTEND, false) == 0)
878 item = GetRandomInt(0, count - 1);
879 GetMenuItem(menu, item, map, sizeof(map));
886 // We were actually cancelled. I guess we do nothing.
889 g_HasVoteStarted = false;
890 g_MapVoteCompleted = true;
897 public Action:Timer_ChangeMap(Handle:hTimer, Handle:dp)
899 g_ChangeMapInProgress = false;
903 if (dp == INVALID_HANDLE)
905 if (!GetNextMap(map, sizeof(map)))
907 //No passed map and no set nextmap. fail!
914 ReadPackString(dp, map, sizeof(map));
917 ForceChangeLevel(map, "Map Vote");
922 bool:RemoveStringFromArray(Handle:array, String:str[])
924 new index = FindStringInArray(array, str);
927 RemoveFromArray(array, index);
936 assert(g_NextMapList)
937 ClearArray(g_NextMapList);
940 new Handle:tempMaps = CloneArray(g_MapList);
942 GetCurrentMap(map, sizeof(map));
943 RemoveStringFromArray(tempMaps, map);
945 if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
947 for (new i = 0; i < GetArraySize(g_OldMapList); i++)
949 GetArrayString(g_OldMapList, i, map, sizeof(map));
950 RemoveStringFromArray(tempMaps, map);
954 new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
955 for (new i = 0; i < limit; i++)
957 new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
958 GetArrayString(tempMaps, b, map, sizeof(map));
959 PushArrayString(g_NextMapList, map);
960 RemoveFromArray(tempMaps, b);
963 CloseHandle(tempMaps);
968 if (g_WaitingForVote || g_HasVoteStarted)
976 NominateResult:InternalNominateMap(String:map[], bool:force, owner)
978 if (!IsMapValid(map))
980 return Nominate_InvalidMap;
985 /* Look to replace an existing nomination by this client - Nominations made with owner = 0 aren't replaced */
986 if (owner && ((index = FindValueInArray(g_NominateOwners, owner)) != -1))
988 new String:oldmap[33];
989 GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
990 Call_StartForward(g_NominationsResetForward);
991 Call_PushString(oldmap);
992 Call_PushCell(owner);
995 SetArrayString(g_NominateList, index, map);
996 return Nominate_Replaced;
999 /* Too many nominated maps. */
1000 if (g_NominateCount >= GetConVarInt(g_Cvar_IncludeMaps) && !force)
1002 return Nominate_VoteFull;
1005 /* Map already in the vote */
1006 if (FindStringInArray(g_NominateList, map) != -1)
1008 return Nominate_AlreadyInVote;
1012 PushArrayString(g_NominateList, map);
1013 PushArrayCell(g_NominateOwners, owner);
1016 while (GetArraySize(g_NominateList) > GetConVarInt(g_Cvar_IncludeMaps))
1018 new String:oldmap[33];
1019 GetArrayString(g_NominateList, 0, oldmap, sizeof(oldmap));
1020 Call_StartForward(g_NominationsResetForward);
1021 Call_PushString(oldmap);
1022 Call_PushCell(GetArrayCell(g_NominateOwners, 0));
1025 RemoveFromArray(g_NominateList, 0);
1026 RemoveFromArray(g_NominateOwners, 0);
1029 return Nominate_Added;
1032 /* Add natives to allow nominate and initiate vote to be call */
1034 /* native bool:NominateMap(const String:map[], bool:force, &NominateError:error); */
1035 public Native_NominateMap(Handle:plugin, numParams)
1038 GetNativeStringLength(1, len);
1045 new String:map[len+1];
1046 GetNativeString(1, map, len+1);
1048 return _:InternalNominateMap(map, GetNativeCell(2), GetNativeCell(3));
1051 bool:InternalRemoveNominationByMap(String:map[])
1053 for (new i = 0; i < GetArraySize(g_NominateList); i++)
1055 new String:oldmap[33];
1056 GetArrayString(g_NominateList, i, oldmap, sizeof(oldmap));
1058 if(strcmp(map, oldmap, false) == 0)
1060 Call_StartForward(g_NominationsResetForward);
1061 Call_PushString(oldmap);
1062 Call_PushCell(GetArrayCell(g_NominateOwners, i));
1065 RemoveFromArray(g_NominateList, i);
1066 RemoveFromArray(g_NominateOwners, i);
1076 /* native bool:RemoveNominationByMap(const String:map[]); */
1077 public Native_RemoveNominationByMap(Handle:plugin, numParams)
1080 GetNativeStringLength(1, len);
1087 new String:map[len+1];
1088 GetNativeString(1, map, len+1);
1090 return _:InternalRemoveNominationByMap(map);
1093 bool:InternalRemoveNominationByOwner(owner)
1097 if (owner && ((index = FindValueInArray(g_NominateOwners, owner)) != -1))
1099 new String:oldmap[33];
1100 GetArrayString(g_NominateList, index, oldmap, sizeof(oldmap));
1102 Call_StartForward(g_NominationsResetForward);
1103 Call_PushString(oldmap);
1104 Call_PushCell(owner);
1107 RemoveFromArray(g_NominateList, index);
1108 RemoveFromArray(g_NominateOwners, index);
1117 /* native bool:RemoveNominationByOwner(owner); */
1118 public Native_RemoveNominationByOwner(Handle:plugin, numParams)
1120 return _:InternalRemoveNominationByOwner(GetNativeCell(1));
1123 /* native InitiateMapChooserVote(); */
1124 public Native_InitiateVote(Handle:plugin, numParams)
1126 new MapChange:when = MapChange:GetNativeCell(1);
1127 new Handle:inputarray = Handle:GetNativeCell(2);
1129 LogAction(-1, -1, "Starting map vote because outside request");
1130 InitiateVote(when, inputarray);
1133 public Native_CanVoteStart(Handle:plugin, numParams)
1135 return CanVoteStart();
1138 public Native_CheckVoteDone(Handle:plugin, numParams)
1140 return g_MapVoteCompleted;
1143 public Native_EndOfMapVoteEnabled(Handle:plugin, numParams)
1145 return GetConVarBool(g_Cvar_EndOfMapVote);
1148 public Native_GetExcludeMapList(Handle:plugin, numParams)
1150 new Handle:array = Handle:GetNativeCell(1);
1152 if (array == INVALID_HANDLE)
1156 new size = GetArraySize(g_OldMapList);
1157 decl String:map[33];
1159 for (new i=0; i<size; i++)
1161 GetArrayString(g_OldMapList, i, map, sizeof(map));
1162 PushArrayString(array, map);
1168 public Native_GetNominatedMapList(Handle:plugin, numParams)
1170 new Handle:maparray = Handle:GetNativeCell(1);
1171 new Handle:ownerarray = Handle:GetNativeCell(2);
1173 if (maparray == INVALID_HANDLE)
1176 decl String:map[33];
1178 for (new i = 0; i < GetArraySize(g_NominateList); i++)
1180 GetArrayString(g_NominateList, i, map, sizeof(map));
1181 PushArrayString(maparray, map);
1183 // If the optional parameter for an owner list was passed, then we need to fill that out as well
1184 if(ownerarray != INVALID_HANDLE)
1186 new index = GetArrayCell(g_NominateOwners, i);
1187 PushArrayCell(ownerarray, index);