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_VoteMenu = INVALID_HANDLE;
75 new Handle:g_TeamScores = INVALID_HANDLE;
77 new bool:g_HasVoteStarted;
80 #define VOTE_EXTEND "##extend##"
82 public OnPluginStart()
84 LoadTranslations("mapchooser.phrases");
86 g_MapList = CreateArray(33);
87 g_OldMapList = CreateArray(33);
88 g_NextMapList = CreateArray(33);
89 g_TeamScores = CreateArray(2);
91 g_Cvar_StartTime = CreateConVar("sm_mapvote_start", "3.0", "Specifies when to start the vote based on time remaining.", _, true, 1.0);
92 g_Cvar_StartRounds = CreateConVar("sm_mapvote_startround", "2.0", "Specifies when to start the vote based on rounds remaining.", _, true, 1.0);
93 g_Cvar_StartFrags = CreateConVar("sm_mapvote_startfrags", "5.0", "Specifies when to start the vote base on frags remaining.", _, true, 1.0);
94 g_Cvar_ExtendTimeMax = CreateConVar("sm_extendmap_maxtime", "90", "Specifies the maximum amount of time a map can be extended", _, true, 0.0);
95 g_Cvar_ExtendTimeStep = CreateConVar("sm_extendmap_timestep", "15", "Specifies how much many more minutes each extension makes", _, true, 5.0);
96 g_Cvar_ExtendRoundMax = CreateConVar("sm_extendmap_maxrounds", "30", "Specfies the maximum amount of rounds a map can be extended", _, true, 0.0);
97 g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 5.0);
98 g_Cvar_ExtendFragMax = CreateConVar("sm_extendmap_maxfrags", "150", "Specfies the maximum frags allowed that a map can be extended.", _, true, 0.0);
99 g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
100 g_Cvar_Mapfile = CreateConVar("sm_mapvote_file", "configs/maps.ini", "Map file to use. (Def sourcemod/configs/maps.ini)");
101 g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
102 g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
103 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);
104 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);
106 RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_CHANGEMAP, "sm_mapvote - Forces MapChooser to attempt to run a map vote now.");
108 g_Cvar_Winlimit = FindConVar("mp_winlimit");
109 g_Cvar_Maxrounds = FindConVar("mp_maxrounds");
110 g_Cvar_Fraglimit = FindConVar("mp_fraglimit");
112 if (g_Cvar_Winlimit != INVALID_HANDLE || g_Cvar_Maxrounds != INVALID_HANDLE)
114 HookEvent("round_end", Event_RoundEnd);
117 if (g_Cvar_Fraglimit != INVALID_HANDLE)
119 HookEvent("player_death", Event_PlayerDeath);
122 AutoExecConfig(true, "mapchooser");
127 g_Cvar_Nextmap = FindConVar("sm_nextmap");
129 if (g_Cvar_Nextmap == INVALID_HANDLE)
131 LogError("FATAL: Cannot find sm_nextmap cvar. Mapchooser not loaded.");
132 SetFailState("sm_nextmap not found");
138 SetupTimeleftTimer();
139 SetConVarString(g_Cvar_Nextmap, "Pending Vote");
145 g_HasVoteStarted = false;
146 g_RetryTimer = INVALID_HANDLE;
147 g_VoteTimer = INVALID_HANDLE;
150 public OnMapTimeLeftChanged()
152 if (GetArraySize(g_MapList))
154 SetupTimeleftTimer();
161 if (GetMapTimeLeft(time) && time > 0)
163 new startTime = GetConVarInt(g_Cvar_StartTime) * 60;
164 if (time - startTime < 0)
170 if (g_VoteTimer != INVALID_HANDLE)
172 KillTimer(g_VoteTimer);
173 g_VoteTimer = INVALID_HANDLE;
176 g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE);
181 public Action:Timer_StartMapVote(Handle:timer)
183 if (!GetArraySize(g_MapList))
188 if (timer == g_RetryTimer)
190 g_RetryTimer = INVALID_HANDLE;
194 g_VoteTimer = INVALID_HANDLE;
202 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
203 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
205 if (!GetArraySize(g_MapList) || g_HasVoteStarted)
210 new winner = GetEventInt(event, "winner");
212 if (winner == 0 || winner == 1)
220 new team[2], teamPos = -1;
221 for (new i; i < GetArraySize(g_TeamScores); i++)
223 GetArrayArray(g_TeamScores, i, team);
224 if (team[0] == winner)
235 PushArrayArray(g_TeamScores, team);
240 SetArrayArray(g_TeamScores, teamPos, team);
243 if (g_Cvar_Winlimit != INVALID_HANDLE)
245 new winlimit = GetConVarInt(g_Cvar_Winlimit);
248 if (team[1] >= (winlimit - GetConVarInt(g_Cvar_StartRounds)))
255 if (g_Cvar_Maxrounds != INVALID_HANDLE)
257 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
260 if (total >= (maxrounds - GetConVarInt(g_Cvar_StartRounds)))
268 public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
270 if (!GetArraySize(g_MapList) || g_Cvar_Fraglimit == INVALID_HANDLE || g_HasVoteStarted)
275 if (!GetConVarInt(g_Cvar_Fraglimit))
280 new fragger = GetClientOfUserId(GetEventInt(event, "attacker"));
281 if (fragger && GetClientFrags(fragger) >= (GetConVarInt(g_Cvar_Fraglimit) - GetConVarInt(g_Cvar_StartFrags)))
287 public Action:Command_Mapvote(client, args)
291 return Plugin_Handled;
296 if (g_HasVoteStarted || g_RetryTimer != INVALID_HANDLE)
301 if (IsVoteInProgress())
303 // Can't start a vote, try again in 5 seconds.
304 g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE);
308 g_HasVoteStarted = true;
309 g_VoteMenu = CreateMenu(Handler_MapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
310 SetMenuTitle(g_VoteMenu, "Vote Nextmap");
313 for (new i = 0; i < GetArraySize(g_NextMapList); i++)
315 GetArrayString(g_NextMapList, i, map, sizeof(map));
316 AddMenuItem(g_VoteMenu, map, map);
319 if (GetConVarBool(g_Cvar_Extend))
321 new bool:allowExtend, time;
322 if (GetMapTimeLimit(time) && time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
327 if (g_Cvar_Winlimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Winlimit) < GetConVarInt(g_Cvar_ExtendRoundMax))
332 if (g_Cvar_Maxrounds != INVALID_HANDLE && GetConVarInt(g_Cvar_Maxrounds) < GetConVarInt(g_Cvar_ExtendRoundMax))
337 if (g_Cvar_Fraglimit != INVALID_HANDLE && GetConVarInt(g_Cvar_Fraglimit) < GetConVarInt(g_Cvar_ExtendFragMax))
344 AddMenuItem(g_VoteMenu, VOTE_EXTEND, "Extend Map");
348 SetMenuExitButton(g_VoteMenu, false);
349 VoteMenuToAll(g_VoteMenu, 20);
351 LogMessage("Voting for next map has started.");
352 PrintToChatAll("[SM] %t", "Nextmap Voting Started");
355 public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
361 g_VoteMenu = INVALID_HANDLE;
365 case MenuAction_Display:
367 decl String:buffer[255];
368 Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
370 new Handle:panel = Handle:param2;
371 SetPanelTitle(panel, buffer);
374 case MenuAction_DisplayItem:
376 if (GetMenuItemCount(menu) - 1 == param2)
378 decl String:map[64], String:buffer[255];
379 GetMenuItem(menu, param2, map, sizeof(map));
380 if (strcmp(map, VOTE_EXTEND, false) == 0)
382 Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
383 return RedrawMenuItem(buffer);
388 // Why am I commented out? Because BAIL hasn't decided yet if
389 // vote notification will be built into the Vote API.
390 /*case MenuAction_Select:
392 decl String:Name[32], String:Map[32];
393 GetClientName(param1, Name, sizeof(Name));
394 GetMenuItem(menu, param2, Map, sizeof(Map));
396 PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
399 case MenuAction_VoteCancel:
401 // If we receive 0 votes, pick at random.
402 if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
404 new count = GetMenuItemCount(menu);
405 new item = GetRandomInt(0, count - 1);
407 GetMenuItem(menu, item, map, sizeof(map));
409 while (strcmp(map, VOTE_EXTEND, false) == 0)
411 item = GetRandomInt(0, count - 1);
412 GetMenuItem(menu, item, map, sizeof(map));
419 // We were actually cancelled. What should we do?
423 case MenuAction_VoteEnd:
426 GetMenuItem(menu, param1, map, sizeof(map));
428 if (strcmp(map, VOTE_EXTEND, false) == 0)
431 if (GetMapTimeLimit(time))
433 if (time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
435 ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);
439 if (g_Cvar_Winlimit != INVALID_HANDLE)
441 new winlimit = GetConVarInt(g_Cvar_Winlimit);
442 if (winlimit && winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
444 SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
448 if (g_Cvar_Maxrounds != INVALID_HANDLE)
450 new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
451 if (maxrounds && maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
453 SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
457 if (g_Cvar_Fraglimit != INVALID_HANDLE)
459 new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
460 if (fraglimit && fraglimit < GetConVarInt(g_Cvar_ExtendFragMax))
462 SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));
466 PrintToChatAll("[SM] %t", "Current Map Extended");
467 LogMessage("Voting for next map has finished. The current map has been extended.");
469 // We extended, so we'll have to vote again.
470 g_HasVoteStarted = false;
472 SetupTimeleftTimer();
484 SetNextMap(const String:map[])
486 SetConVarString(g_Cvar_Nextmap, map);
487 PushArrayString(g_OldMapList, map);
489 if (GetArraySize(g_OldMapList) > GetConVarInt(g_Cvar_ExcludeMaps))
491 RemoveFromArray(g_OldMapList, 0);
494 PrintToChatAll("[SM] %t", "Nextmap Voting Finished", map);
495 LogMessage("Voting for next map has finished. Nextmap: %s.", map);
500 if(g_NextMapList != INVALID_HANDLE)
502 ClearArray(g_NextMapList);
505 new Handle:tempMaps = CreateArray(33);
507 for (new i = 0; i < GetArraySize(g_MapList); i++)
509 GetArrayString(g_MapList, i, map, sizeof(map));
510 PushArrayString(tempMaps, map);
513 if (GetArraySize(tempMaps) > GetConVarInt(g_Cvar_ExcludeMaps))
515 for (new i = 0; i < GetArraySize(g_OldMapList); i++)
517 GetArrayString(g_OldMapList, i, map, sizeof(map));
518 for (new j = 0; j < GetArraySize(tempMaps); j++)
520 decl String:temp[32];
521 GetArrayString(tempMaps, j, temp, sizeof(temp));
522 if (strcmp(temp, map) == 0)
524 RemoveFromArray(tempMaps, j);
531 new limit = (GetConVarInt(g_Cvar_IncludeMaps) < GetArraySize(tempMaps) ? GetConVarInt(g_Cvar_IncludeMaps) : GetArraySize(tempMaps));
532 for (new i = 0; i < limit; i++)
534 new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
535 GetArrayString(tempMaps, b, map, sizeof(map));
536 PushArrayString(g_NextMapList, map);
537 RemoveFromArray(tempMaps, b);
545 decl String:mapPath[256], String:mapFile[64];
546 GetConVarString(g_Cvar_Mapfile, mapFile, 64);
547 BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile);
548 fileFound = FileExists(mapPath);
551 new Handle:mapCycleFile = FindConVar("mapcyclefile");
552 GetConVarString(mapCycleFile, mapPath, sizeof(mapPath));
553 fileFound = FileExists(mapPath);
558 LogError("Unable to locate sm_mapvote_file or mapcyclefile, no maps loaded.");
560 if (g_MapList != INVALID_HANDLE)
562 ClearArray(g_MapList);
568 // If the file hasn't changed, there's no reason to reload
570 new fileTime = GetFileTime(mapPath, FileTime_LastChange);
571 if (g_mapFileTime == fileTime)
573 return GetArraySize(g_MapList);
576 g_mapFileTime = fileTime;
579 if (g_MapList != INVALID_HANDLE)
581 ClearArray(g_MapList);
584 LogMessage("[SM] Loading mapchooser map file [%s]", mapPath);
586 new Handle:file = OpenFile(mapPath, "rt");
587 if (file == INVALID_HANDLE)
589 LogError("[SM] Could not open file: %s", mapPath);
593 decl String:currentMap[32];
594 GetCurrentMap(currentMap, sizeof(currentMap));
596 decl String:buffer[64], len;
597 while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
601 if ((len = StrContains(buffer, ".bsp", false)) != -1)
606 if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer)
607 || strcmp(currentMap, buffer, false) == 0)
612 PushArrayString(g_MapList, buffer);
616 return GetArraySize(g_MapList);