3 * =============================================================================
4 * SourceMod Rock The Vote Plugin
5 * Creates a map vote when the required number of players have requested one.
7 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
39 public Plugin:myinfo =
41 name = "Map Nominations",
42 author = "AlliedModders LLC",
43 description = "Provides Map Nominations",
44 version = SOURCEMOD_VERSION,
45 url = "http://www.sourcemod.net/"
48 new Handle:g_Cvar_ExcludeOld = INVALID_HANDLE;
49 new Handle:g_Cvar_ExcludeCurrent = INVALID_HANDLE;
51 new Handle:g_MapList = INVALID_HANDLE;
52 new Handle:g_MapMenu = INVALID_HANDLE;
53 new g_mapFileSerial = -1;
55 #define MAPSTATUS_ENABLED (1<<0)
56 #define MAPSTATUS_DISABLED (1<<1)
57 #define MAPSTATUS_EXCLUDE_CURRENT (1<<2)
58 #define MAPSTATUS_EXCLUDE_PREVIOUS (1<<3)
59 #define MAPSTATUS_EXCLUDE_NOMINATED (1<<4)
63 public OnPluginStart()
65 LoadTranslations("common.phrases");
66 LoadTranslations("nominations.phrases");
68 new arraySize = ByteCountToCells(33);
69 g_MapList = CreateArray(arraySize);
71 g_Cvar_ExcludeOld = CreateConVar("sm_nominate_excludeold", "1", "Specifies if the current map should be excluded from the Nominations list", 0, true, 0.00, true, 1.0);
72 g_Cvar_ExcludeCurrent = CreateConVar("sm_nominate_excludecurrent", "1", "Specifies if the MapChooser excluded maps should also be excluded from Nominations", 0, true, 0.00, true, 1.0);
74 RegConsoleCmd("say", Command_Say);
75 RegConsoleCmd("say_team", Command_Say);
77 RegConsoleCmd("sm_nominate", Command_Nominate);
79 RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
81 g_mapTrie = CreateTrie();
84 public OnConfigsExecuted()
86 if (ReadMapList(g_MapList,
89 MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
92 if (g_mapFileSerial == -1)
94 SetFailState("Unable to create a valid map list.");
101 public OnNominationRemoved(const String:map[], owner)
105 /* Is the map in our list? */
106 if (!GetTrieValue(g_mapTrie, map, status))
111 /* Was the map disabled due to being nominated */
112 if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
117 SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);
120 public Action:Command_Addmap(client, args)
124 ReplyToCommand(client, "[SM] Usage: sm_nominate_addmap <mapname>");
125 return Plugin_Handled;
128 decl String:mapname[64];
129 GetCmdArg(1, mapname, sizeof(mapname));
133 if (!GetTrieValue(g_mapTrie, mapname, status))
135 ReplyToCommand(client, "%t", "Map was not found", mapname);
136 return Plugin_Handled;
139 new NominateResult:result = NominateMap(mapname, true, 0);
141 if (result > Nominate_Replaced)
143 /* We assume already in vote is the casue because the maplist does a Map Validity check and we forced, so it can't be full */
144 ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
146 return Plugin_Handled;
150 SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
153 ReplyToCommand(client, "%t", "Map Inserted", mapname);
154 LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
156 return Plugin_Handled;
159 public Action:Command_Say(client, args)
163 return Plugin_Continue;
166 decl String:text[192];
167 if (!GetCmdArgString(text, sizeof(text)))
169 return Plugin_Continue;
173 if(text[strlen(text)-1] == '"')
175 text[strlen(text)-1] = '\0';
179 new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
181 if (strcmp(text[startidx], "nominate", false) == 0)
183 AttemptNominate(client);
186 SetCmdReplySource(old);
188 return Plugin_Continue;
191 public Action:Command_Nominate(client, args)
195 return Plugin_Handled;
200 AttemptNominate(client);
201 return Plugin_Handled;
204 decl String:mapname[64];
205 GetCmdArg(1, mapname, sizeof(mapname));
208 if (!GetTrieValue(g_mapTrie, mapname, status))
210 ReplyToCommand(client, "%t", "Map was not found", mapname);
211 return Plugin_Handled;
214 if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
216 if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
218 ReplyToCommand(client, "[SM] %t", "Can't Nominate Current Map");
221 if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
223 ReplyToCommand(client, "[SM] %t", "Map in Exclude List");
226 if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
228 ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
231 return Plugin_Handled;
234 new NominateResult:result = NominateMap(mapname, false, client);
236 if (result > Nominate_Replaced)
238 if (result == Nominate_AlreadyInVote)
240 ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
244 ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
247 return Plugin_Handled;
250 /* Map was nominated! - Disable the menu item and update the trie */
252 SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
254 decl String:name[64];
255 GetClientName(client, name, sizeof(name));
256 PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
258 return Plugin_Continue;
261 AttemptNominate(client)
263 SetMenuTitle(g_MapMenu, "%T", "Nominate Title", client);
264 DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);
271 if (g_MapMenu != INVALID_HANDLE)
273 CloseHandle(g_MapMenu);
274 g_MapMenu = INVALID_HANDLE;
277 ClearTrie(g_mapTrie);
279 g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
283 new Handle:excludeMaps = INVALID_HANDLE;
284 decl String:currentMap[32];
286 if (GetConVarBool(g_Cvar_ExcludeOld))
288 excludeMaps = CreateArray(ByteCountToCells(33));
289 GetExcludeMapList(excludeMaps);
292 if (GetConVarBool(g_Cvar_ExcludeCurrent))
294 GetCurrentMap(currentMap, sizeof(currentMap));
298 for (new i = 0; i < GetArraySize(g_MapList); i++)
300 new status = MAPSTATUS_ENABLED;
302 GetArrayString(g_MapList, i, map, sizeof(map));
304 if (GetConVarBool(g_Cvar_ExcludeCurrent))
306 if (StrEqual(map, currentMap))
308 status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
312 /* Dont bother with this check if the current map check passed */
313 if (GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
315 if (FindStringInArray(excludeMaps, map) != -1)
317 status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
321 AddMenuItem(g_MapMenu, map, map);
322 SetTrieValue(g_mapTrie, map, status);
325 SetMenuExitButton(g_MapMenu, true);
327 if (excludeMaps != INVALID_HANDLE)
329 CloseHandle(excludeMaps);
333 public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
337 case MenuAction_Select:
339 decl String:map[64], String:name[64];
340 GetMenuItem(menu, param2, map, sizeof(map));
342 GetClientName(param1, name, 64);
344 new NominateResult:result = NominateMap(map, false, param1);
346 /* Don't need to check for InvalidMap because the menu did that already */
347 if (result == Nominate_AlreadyInVote)
349 PrintToChat(param1, "[SM] %t", "Map Already Nominated");
352 else if (result == Nominate_VoteFull)
354 PrintToChat(param1, "[SM] %t", "Max Nominations");
358 SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
360 if (result == Nominate_Replaced)
362 PrintToChatAll("[SM] %t", "Map Nomination Changed", name, map);
366 PrintToChatAll("[SM] %t", "Map Nominated", name, map);
369 case MenuAction_DrawItem:
372 GetMenuItem(menu, param2, map, sizeof(map));
376 if (!GetTrieValue(g_mapTrie, map, status))
378 LogError("Menu selection of item not in trie. Major logic problem somewhere.");
379 return ITEMDRAW_DEFAULT;
382 if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
384 return ITEMDRAW_DISABLED;
387 return ITEMDRAW_DEFAULT;
391 case MenuAction_DisplayItem:
394 GetMenuItem(menu, param2, map, sizeof(map));
398 if (!GetTrieValue(g_mapTrie, map, status))
400 LogError("Menu selection of item not in trie. Major logic problem somewhere.");
404 decl String:display[100];
406 if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
408 if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
410 Format(display, sizeof(display), "%s (%T)", map, "Current Map", param1);
411 return RedrawMenuItem(display);
414 if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
416 Format(display, sizeof(display), "%s (%T)", map, "Recently Played", param1);
417 return RedrawMenuItem(display);
420 if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
422 Format(display, sizeof(display), "%s (%T)", map, "Nominated", param1);
423 return RedrawMenuItem(display);