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 public Plugin:myinfo =
41 author = "AlliedModders LLC",
42 description = "Automated Map Voting",
43 version = SOURCEMOD_VERSION,
44 url = "http://www.sourcemod.net/"
47 new Handle:g_Cvar_Winlimit = INVALID_HANDLE;
48 new Handle:g_Cvar_Maxrounds = INVALID_HANDLE;
49 new Handle:g_Cvar_Fraglimit = INVALID_HANDLE;
51 new Handle:g_Cvar_Nextmap = INVALID_HANDLE;
53 new Handle:g_Cvar_StartTime = INVALID_HANDLE;
54 new Handle:g_Cvar_StartRounds = INVALID_HANDLE;
55 new Handle:g_Cvar_StartFrags = INVALID_HANDLE;
56 new Handle:g_Cvar_ExtendTimeMax = INVALID_HANDLE;
57 new Handle:g_Cvar_ExtendTimeStep = INVALID_HANDLE;
58 new Handle:g_Cvar_ExtendRoundMax = INVALID_HANDLE;
59 new Handle:g_Cvar_ExtendRoundStep = INVALID_HANDLE;
60 new Handle:g_Cvar_ExtendFragMax = INVALID_HANDLE;
61 new Handle:g_Cvar_ExtendFragStep = INVALID_HANDLE;
62 new Handle:g_Cvar_Mapfile = INVALID_HANDLE;
63 new Handle:g_Cvar_ExcludeMaps = INVALID_HANDLE;
64 new Handle:g_Cvar_IncludeMaps = INVALID_HANDLE;
65 new Handle:g_Cvar_NoVoteMode = INVALID_HANDLE;
66 new Handle:g_Cvar_Extend = INVALID_HANDLE;
68 new Handle:g_VoteTimer = INVALID_HANDLE;
69 new Handle:g_RetryTimer = INVALID_HANDLE;
71 new Handle:g_MapList = INVALID_HANDLE;
72 new Handle:g_OldMapList = INVALID_HANDLE;
73 new Handle:g_NextMapList = INVALID_HANDLE;
74 new Handle:g_TeamScores = INVALID_HANDLE;
75 new Handle:g_VoteMenu = INVALID_HANDLE;
78 new bool:g_HasVoteStarted;
81 #define VOTE_EXTEND "##extend##"
83 public OnPluginStart()
85 LoadTranslations("mapchooser.phrases");
87 new arraySize = ByteCountToCells(33);
88 g_MapList = CreateArray(arraySize);
89 g_OldMapList = CreateArray(arraySize);
90 g_NextMapList = CreateArray(arraySize);
92 g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
93 g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining.", _, true, 1.0);
94 g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
95 g_Cvar_ExtendTimeMax = CreateConVar("sm_extendmap_maxtime", "90", "Specifies the maximum amount of time a map can be extended", _, true, 0.0);
96 g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
97 g_Cvar_ExtendRoundMax = CreateConVar("sm_extendmap_maxrounds", "30", "Specfies the maximum amount of rounds a map can be extended", _, true, 0.0);
98 g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 5.0);
99 g_Cvar_ExtendFragMax = CreateConVar("sm_extendmap_maxfrags", "150", "Specfies the maximum frags allowed that a map can be extended.", _, true, 0.0);
100 g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
101 g_Cvar_Mapfile = CreateConVar("sm_mapvote_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
102 g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
103 g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
104 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);
105 g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "1", "Specifies whether or not MapChooser will allow the map to be extended.", _, true, 0.0, true, 1.0);
107 RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
109 g_Cvar_Winlimit = FindConVar("mp_winlimit");
110 g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
111 g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
113 if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
115 HookEvent("round_end", Event_RoundEnd);
118 if (g_Cvar_Fraglimit != INVALID_HANDLE)
120 HookEvent("player_death", Event_PlayerDeath);
123 AutoExecConfig(true, "mapchooser");
126 public OnConfigsExecuted()
128 g_Cvar_Nextmap = FindConVar("sm_nextmap");
130 if (g_Cvar_Nextmap == INVALID_HANDLE)
132 LogError("FATAL: Cannot find sm_nextmap cvar. Mapchooser not loaded.");
133 SetFailState("sm_nextmap not found");
136 if (LoadMaps(g_MapList, g_mapFileTime, g_Cvar_Mapfile))
139 SetupTimeleftTimer();
140 SetConVarString(g_Cvar_Nextmap, "Pending Vote");
143 if (g_TeamScores != INVALID_HANDLE)
145 CloseHandle(g_TeamScores);
148 g_TeamScores = CreateArray(2);
154 g_HasVoteStarted = false;
156 if (g_VoteTimer != INVALID_HANDLE)
158 KillTimer(g_VoteTimer);
159 g_VoteTimer = INVALID_HANDLE;
162 if (g_RetryTimer != INVALID_HANDLE)
164 KillTimer(g_RetryTimer);
165 g_RetryTimer = INVALID_HANDLE;
169 public OnMapTimeLeftChanged()
171 if (GetArraySize(g_MapList))
173 SetupTimeleftTimer();
180 if (GetMapTimeLeft(time) && time > 0)
182 new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
183 if (time - startTime < 0)
189 if (g_VoteTimer != INVALID_HANDLE)
191 KillTimer(g_VoteTimer);
192 g_VoteTimer = INVALID_HANDLE;
195 g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote);
200 public Action:Timer_StartMapVote(Handle:timer)
202 if (!GetArraySize(g_MapList))
207 if (timer == g_RetryTimer)
209 g_RetryTimer = INVALID_HANDLE;
213 g_VoteTimer = INVALID_HANDLE;
221 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
222 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
224 if (!GetArraySize(g_MapList) || g_HasVoteStarted)
229 new winner = GetEventInt(event, "winner");
231 if (winner == 0 || winner == 1)
238 new team[2], teamPos = -1;
239 for (new i; i < GetArraySize(g_TeamScores); i++)
241 GetArrayArray(g_TeamScores, i, team);
242 if (team[0] == winner)
253 PushArrayArray(g_TeamScores, team);
258 SetArrayArray(g_TeamScores, teamPos, team);
261 if (g_Cvar_Winlimit != INVALID_HANDLE)
263 new winlimit = GetConVarInt(g_Cvar_Winlimit);
266 if (team[1] >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
273 if (g_Cvar_Maxrounds != INVALID_HANDLE)
275 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
278 if (g_TotalRounds >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
286 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
288 if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
293 if (!GetConVarInt(g_Cvar_Fraglimit))
298 new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
299 if (fragger && GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
305 public Action:Command_Mapvote(client, args)
309 return Plugin_Handled;
314 if (g_HasVoteStarted || g_RetryTimer != INVALID_HANDLE)
319 if (IsVoteInProgress())
321 // Can't start a vote, try again in 5 seconds.
322 g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote);
326 g_HasVoteStarted = true;
327 g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
328 SetMenuTitle(g_VoteMenu, "Vote Nextmap");
331 for (new i = 0; i < GetArraySize(g_NextMapList); i++)
333 GetArrayString(g_NextMapList, i, map, sizeof(map));
334 AddMenuItem(g_VoteMenu, map, map);
337 if (GetConVarBool(g_Cvar_Extend))
339 new bool:allowExtend, time;
340 if (GetMapTimeLimit(time) && time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
345 if (g_Cvar_Winlimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Winlimit) < GetConVarInt(g_Cvar_ExtendRoundMax))
350 if (g_Cvar_Maxrounds != INVALID_HANDLE && GetConVarInt(g_Cvar_Maxrounds) < GetConVarInt(g_Cvar_ExtendRoundMax))
355 if (g_Cvar_Fraglimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Fraglimit) < GetConVarInt(g_Cvar_ExtendFragMax))
362 AddMenuItem(g_VoteMenu, VOTE_EXTEND, "Extend Map");
366 SetMenuExitButton(g_VoteMenu, false);
367 VoteMenuToAll(g_VoteMenu, 20);
369 LogMessage("Voting for next map has started.");
370 PrintToChatAll("[SM] %t", "Nextmap Voting Started");
373 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
379 g_VoteMenu = INVALID_HANDLE;
383 case MenuAction_Display:
385 decl String:buffer[255];
386 Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
388 new Handle:panel = Handle:param2;
389 SetPanelTitle(panel, buffer);
392 case MenuAction_DisplayItem:
394 if (GetMenuItemCount(menu) - 1 == param2)
396 decl String:map[64], String:buffer[255];
397 GetMenuItem(menu, param2, map, sizeof(map));
398 if (strcmp(map, VOTE_EXTEND, false) == 0)
400 Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
401 return RedrawMenuItem(buffer);
406 // Why am I commented out? Because BAIL hasn't decided yet if
407 // vote notification will be built into the Vote API.
408 /*case MenuAction_Select:
410 decl String:Name[32], String:Map[32];
411 GetClientName(param1, Name, sizeof(Name));
412 GetMenuItem(menu, param2, Map, sizeof(Map));
414 PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
417 case MenuAction_VoteCancel:
419 // If we receive 0 votes, pick at random.
420 if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
422 new count = GetMenuItemCount(menu);
423 new item = GetRandomInt(0, count - 1);
425 GetMenuItem(menu, item, map, sizeof(map));
427 while (strcmp(map, VOTE_EXTEND, false) == 0)
429 item = GetRandomInt(0, count - 1);
430 GetMenuItem(menu, item, map, sizeof(map));
437 // We were actually cancelled. What should we do?
441 case MenuAction_VoteEnd:
444 GetMenuItem(menu, param1, map, sizeof(map));
446 if (strcmp(map, VOTE_EXTEND, false) == 0)
449 if (GetMapTimeLimit(time))
451 if (time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
453 ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);
457 if (g_Cvar_Winlimit != INVALID_HANDLE)
459 new winlimit = GetConVarInt(g_Cvar_Winlimit);
460 if (winlimit && winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
462 SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
466 if (g_Cvar_Maxrounds != INVALID_HANDLE)
468 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
469 if (maxrounds && maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
471 SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
475 if (g_Cvar_Fraglimit != INVALID_HANDLE)
477 new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
478 if (fraglimit && fraglimit < GetConVarInt(g_Cvar_ExtendFragMax))
480 SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));
484 PrintToChatAll("[SM] %t", "Current Map Extended");
485 LogMessage("Voting for next map has finished. The current map has been extended.");
487 // We extended, so we'll have to vote again.
488 g_HasVoteStarted = false;
490 SetupTimeleftTimer();
502 SetNextMap(const String:map[])
504 SetConVarString(g_Cvar_Nextmap, map);
505 PushArrayString(g_OldMapList, map);
507 if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
509 RemoveFromArray(g_OldMapList, 0);
512 PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map);
513 LogMessage("Voting for next map has finished. Nextmap: %s.", map);
518 if(g_NextMapList != INVALID_HANDLE)
520 ClearArray(g_NextMapList);
524 new Handle:tempMaps = CloneArray(g_MapList);
526 if (GetConVarInt(g_Cvar_ExcludeMaps) && GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
528 for (new i = 0; i < GetArraySize(g_OldMapList); i++)
530 GetArrayString(g_OldMapList, i, map, sizeof(map));
531 new index = FindStringInArray(tempMaps, map);
534 RemoveFromArray(tempMaps, index);
540 // If we didn't check against ExcludeMaps, we have to remove the current map.
541 GetCurrentMap(map, sizeof(map));
542 new index = FindStringInArray(tempMaps, map);
545 RemoveFromArray(tempMaps, index);
549 new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
550 for (new i = 0; i < limit; i++)
552 new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
553 GetArrayString(tempMaps, b, map, sizeof(map));
554 PushArrayString(g_NextMapList, map);
555 RemoveFromArray(tempMaps, b);
558 CloseHandle(tempMaps);