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;
66 new Handle:g_VoteTimer = INVALID_HANDLE;
67 new Handle:g_RetryTimer = INVALID_HANDLE;
69 new Handle:g_MapList = INVALID_HANDLE;
70 new Handle:g_OldMapList = INVALID_HANDLE;
71 new Handle:g_NextMapList = INVALID_HANDLE;
72 new Handle:g_VoteMenu = INVALID_HANDLE;
73 new Handle:g_TeamScores = INVALID_HANDLE;
75 new bool:g_HasVoteStarted;
78 public OnPluginStart()
80 LoadTranslations("mapchooser.phrases");
82 g_MapList = CreateArray(33);
83 g_OldMapList = CreateArray(33);
84 g_NextMapList = CreateArray(33);
85 g_TeamScores = CreateArray(2);
87 g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
88 g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining.", _, true, 1.0);
89 g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
90 g_Cvar_ExtendTimeMax = CreateConVar("sm_extendmap_maxtime", "90", "Specifies the maximum amount of time a map can be extended", _, true, 0.0);
91 g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
92 g_Cvar_ExtendRoundMax = CreateConVar("sm_extendmap_maxrounds", "30", "Specfies the maximum amount of rounds a map can be extended", _, true, 0.0);
93 g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 5.0);
94 g_Cvar_ExtendFragMax = CreateConVar("sm_extendmap_maxfrags", "150", "Specfies the maximum frags allowed that a map can be extended.", _, true, 0.0);
95 g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
96 g_Cvar_Mapfile = CreateConVar("sm_mapvote_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
97 g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
98 g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
100 RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
102 g_Cvar_Winlimit = FindConVar("mp_winlimit");
103 g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
104 g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
106 if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
108 HookEvent("round_end", Event_RoundEnd);
111 if (g_Cvar_Fraglimit != INVALID_HANDLE)
113 HookEvent("player_death", Event_PlayerDeath);
116 AutoExecConfig(true, "mapchooser");
121 g_Cvar_Nextmap = FindConVar("sm_nextmap");
123 if (g_Cvar_Nextmap == INVALID_HANDLE)
125 LogError("FATAL: Cannot find sm_nextmap cvar. Mapchooser not loaded.");
126 SetFailState("sm_nextmap not found");
132 SetupTimeleftTimer();
138 g_HasVoteStarted = false;
139 g_RetryTimer = INVALID_HANDLE;
140 g_VoteTimer = INVALID_HANDLE;
143 public OnMapTimeLeftChanged()
145 if (GetArraySize(g_MapList))
147 SetupTimeleftTimer();
154 if (GetMapTimeLeft(time) && time > 0)
156 new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
157 if (time - startTime < 0)
163 if (g_VoteTimer != INVALID_HANDLE)
165 KillTimer(g_VoteTimer);
166 g_VoteTimer = INVALID_HANDLE;
169 g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE);
174 public Action:Timer_StartMapVote(Handle:timer)
176 if (!GetArraySize(g_MapList))
181 if (timer == g_RetryTimer)
183 g_RetryTimer = INVALID_HANDLE;
187 g_VoteTimer = INVALID_HANDLE;
195 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
196 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
198 if (!GetArraySize(g_MapList) || g_HasVoteStarted)
203 new winner = GetEventInt(event, "winner");
205 if (winner == 0 || winner == 1)
213 new team[2], teamPos = -1;
214 for (new i; i < GetArraySize(g_TeamScores); i++)
216 GetArrayArray(g_TeamScores, i, team);
217 if (team[0] == winner)
228 PushArrayArray(g_TeamScores, team);
233 SetArrayArray(g_TeamScores, teamPos, team);
236 if (g_Cvar_Winlimit != INVALID_HANDLE)
238 new winlimit = GetConVarInt(g_Cvar_Winlimit);
241 if (team[1] >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
248 if (g_Cvar_Maxrounds != INVALID_HANDLE)
250 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
253 if (total >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
261 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
263 if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
268 if (!GetConVarInt(g_Cvar_Fraglimit))
273 new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
274 if (fragger && GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
280 public Action:Command_Mapvote(client, args)
284 return Plugin_Handled;
289 if (g_HasVoteStarted || g_RetryTimer != INVALID_HANDLE)
294 if (IsVoteInProgress())
296 // Can't start a vote, try again in 5 seconds.
297 g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE);
301 g_HasVoteStarted = true;
302 g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
303 SetMenuTitle(g_VoteMenu, "Vote Nextmap");
306 for (new i = 0; i < GetArraySize(g_NextMapList); i++)
308 GetArrayString(g_NextMapList, i, map, sizeof(map));
309 AddMenuItem(g_VoteMenu, map, map);
312 new bool:allowExtend, time;
313 if (GetMapTimeLimit(time) && time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
318 if (g_Cvar_Winlimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Winlimit) < GetConVarInt(g_Cvar_ExtendRoundMax))
323 if (g_Cvar_Maxrounds != INVALID_HANDLE && GetConVarInt(g_Cvar_Maxrounds) < GetConVarInt(g_Cvar_ExtendRoundMax))
328 if (g_Cvar_Fraglimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Fraglimit) < GetConVarInt(g_Cvar_ExtendFragMax))
335 AddMenuItem(g_VoteMenu, "##extend##", "Extend Map");
338 SetMenuExitButton(g_VoteMenu, false);
339 VoteMenuToAll(g_VoteMenu, 20);
341 LogMessage("Voting for next map has started.");
342 PrintToChatAll("[SM] %t", "Nextmap Voting Started");
345 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
351 g_VoteMenu = INVALID_HANDLE;
355 case MenuAction_Display:
357 decl String:buffer[255];
358 Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
360 new Handle:panel = Handle:param2;
361 SetPanelTitle(panel, buffer);
364 case MenuAction_DisplayItem:
366 if (GetMenuItemCount(menu) - 1 == param2)
368 decl String:map[64], String:buffer[255];
369 GetMenuItem(menu, param2, map, sizeof(map));
370 if (strcmp(map, "##extend##", false))
372 Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
373 return RedrawMenuItem(buffer);
378 // Why am I commented out? Because BAIL hasn't decided yet if
379 // vote notification will be built into the Vote API.
380 /*case MenuAction_Select:
382 decl String:Name[32], String:Map[32];
383 GetClientName(param1, Name, sizeof(Name));
384 GetMenuItem(menu, param2, Map, sizeof(Map));
386 PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
389 case MenuAction_VoteCancel:
391 // If we receive 0 votes, pick at random.
392 if (param1 == VoteCancel_NoVotes)
394 new count = GetMenuItemCount(menu);
395 new item = GetRandomInt(0, count - 1);
397 GetMenuItem(menu, item, map, sizeof(map));
399 while (strcmp(map, "##extend##", false) == 0)
401 item = GetRandomInt(0, count - 1);
402 GetMenuItem(menu, item, map, sizeof(map));
409 // We were actually cancelled. What should we do?
413 case MenuAction_VoteEnd:
416 GetMenuItem(menu, param1, map, sizeof(map));
418 if (strcmp(map, "##extend##", false) == 0)
421 if (GetMapTimeLimit(time))
423 if (time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
425 ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);
429 if (g_Cvar_Winlimit != INVALID_HANDLE)
431 new winlimit = GetConVarInt(g_Cvar_Winlimit);
432 if (winlimit && winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
434 SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
438 if (g_Cvar_Maxrounds != INVALID_HANDLE)
440 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
441 if (maxrounds && maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
443 SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
447 if (g_Cvar_Fraglimit != INVALID_HANDLE)
449 new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
450 if (fraglimit && fraglimit < GetConVarInt(g_Cvar_ExtendFragMax))
452 SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));
456 PrintToChatAll("[SM] %t", "Current Map Extended");
457 LogMessage("Voting for next map has finished. The current map has been extended.");
459 // We extended, so we'll have to vote again.
460 g_HasVoteStarted = false;
462 SetupTimeleftTimer();
474 SetNextMap(const String:map[])
476 SetConVarString(g_Cvar_Nextmap, map);
477 PushArrayString(g_OldMapList, map);
479 if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
481 RemoveFromArray(g_OldMapList, 0);
484 PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map);
485 LogMessage("Voting for next map has finished. Nextmap: %s.", map);
490 if(g_NextMapList != INVALID_HANDLE)
492 ClearArray(g_NextMapList);
496 new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(g_MapList) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(g_MapList));
498 new bool:oldMaps = false;
499 if (limit > GetConVarInt(g_Cvar_ExcludeMaps))
501 limit -= GetArraySize(g_OldMapList);
505 for (new i = 0; i < limit; i++)
507 new b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
508 GetArrayString(g_MapList, b, map, sizeof(map));
510 while (IsStringInArray(g_NextMapList, map) || (oldMaps && IsStringInArray(g_OldMapList, map)))
512 b = GetRandomInt(0, GetArraySize(g_MapList) - 1);
513 GetArrayString(g_MapList, b, map, sizeof(map));
516 PushArrayString(g_NextMapList, map);
524 decl String:mapPath[256], String:mapFile[64];
525 GetConVarString(g_Cvar_Mapfile, mapFile, 64);
526 BuildPath(Path_SM, mapPath, sizeof(mapFile), mapFile);
527 fileFound = FileExists(mapPath);
530 new Handle:mapCycleFile = FindConVar("mapcyclefile");
531 GetConVarString(mapCycleFile, mapPath, sizeof(mapPath));
532 fileFound = FileExists(mapPath);
537 LogError("Unable to locate sm_mapvote_file or mapcyclefile, no maps loaded.");
539 if (g_MapList != INVALID_HANDLE)
541 ClearArray(g_MapList);
547 // If the file hasn't changed, there's no reason to reload
549 new fileTime = GetFileTime(mapPath, FileTime_LastChange);
550 if (g_mapFileTime == fileTime)
552 return GetArraySize(g_MapList);
555 g_mapFileTime = fileTime;
558 if (g_MapList != INVALID_HANDLE)
560 ClearArray(g_MapList);
563 LogMessage("[SM] Loading mapchooser map file [%s]", mapPath);
565 new Handle:file = OpenFile(mapPath, "rt");
566 if (file == INVALID_HANDLE)
568 LogError("[SM] Could not open file: %s", mapPath);
572 decl String:currentMap[32];
573 GetCurrentMap(currentMap, sizeof(currentMap));
575 decl String:buffer[64], len;
576 while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
580 if ((len = StrContains(buffer, ".bsp", false)) != -1)
585 if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer)
586 || strcmp(currentMap, buffer, false) == 0)
591 PushArrayString(g_MapList, buffer);
595 return GetArraySize(g_MapList);