3 * =============================================================================
4 * SourceMod Nextmap Plugin
5 * Adds sm_nextmap cvar for changing map and nextmap chat trigger.
7 * SourceMod (C)2004-2007 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>.
38 public Plugin:myinfo =
41 author = "AlliedModders LLC",
42 description = "Provides nextmap and sm_nextmap",
43 version = SOURCEMOD_VERSION,
44 url = "http://www.sourcemod.net/"
47 new bool:g_IntermissionCalled;
48 new UserMsg:g_VGUIMenu;
50 new Handle:g_Cvar_Chattime;
51 new Handle:g_Cvar_Nextmap;
52 new Handle:g_Cvar_Mapcycle;
55 new Handle:g_MapList = INVALID_HANDLE;
57 public OnPluginStart()
59 LoadTranslations("common.phrases");
60 LoadTranslations("nextmap.phrases");
62 g_VGUIMenu = GetUserMessageId("VGUIMenu");
63 if (g_VGUIMenu == INVALID_MESSAGE_ID)
65 LogError("FATAL: Cannot find VGUIMenu user message id. Nextmap not loaded.");
66 SetFailState("VGUIMenu Not Found");
69 g_Cvar_Chattime = FindConVar("mp_chattime");
70 g_Cvar_Mapcycle = FindConVar("mapcyclefile");
72 g_MapList = CreateArray(32);
74 decl String:mapCycle[64];
75 GetConVarString(g_Cvar_Mapcycle, mapCycle, 64);
76 if (LoadMaps(mapCycle) < 1)
78 LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
79 SetFailState("Mapcycle Not Found");
82 HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
83 HookConVarChange(g_Cvar_Mapcycle, ConVarChange_Mapcyclefile);
85 g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
87 RegConsoleCmd("say", Command_Say);
88 RegConsoleCmd("say_team", Command_Say);
90 RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
91 RegConsoleCmd("listmaps", Command_List);
93 // Set to the current map so OnMapStart() will know what to do
94 decl String:currentMap[64];
95 GetCurrentMap(currentMap, 64);
96 SetConVarString(g_Cvar_Nextmap, currentMap);
101 decl String:lastMap[64], String:currentMap[64];
102 GetConVarString(g_Cvar_Nextmap, lastMap, 64);
103 GetCurrentMap(currentMap, 64);
105 // Why am I doing this? If we switched to a new map, but it wasn't what we expected (Due to sm_map, sm_votemap, or
106 // some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
107 // not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
108 if (strcmp(lastMap, currentMap) == 0)
116 g_IntermissionCalled = false;
119 public ConVarChange_Mapcyclefile(Handle:convar, const String:oldValue[], const String:newValue[])
121 if (strcmp(oldValue, newValue, false) != 0)
123 if (LoadMaps(newValue) < 1)
125 LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
126 SetFailState("Mapcycle Not Found");
133 public Action:Command_Say(client, args)
135 decl String:text[192];
136 GetCmdArgString(text, sizeof(text));
139 if (text[strlen(text)-1] == '"')
141 text[strlen(text)-1] = '\0';
145 decl String:message[8];
146 BreakString(text[startidx], message, sizeof(message));
148 if (strcmp(message, "nextmap", false) == 0)
151 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
153 PrintToChatAll("%t", "Next Map", map);
156 return Plugin_Continue;
159 public Action:Command_SetNextmap(client, args)
163 ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
164 return Plugin_Handled;
168 GetCmdArg(1, map, sizeof(map));
170 if (!IsMapValid(map))
172 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
173 return Plugin_Handled;
176 ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
177 LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
179 SetConVarString(g_Cvar_Nextmap, map);
181 return Plugin_Handled;
184 public Action:Command_List(client, args)
186 PrintToConsole(client, "Map Cycle:");
188 new mapCount = GetArraySize(g_MapList);
189 decl String:mapName[32];
190 for (new i = 0; i < mapCount; i++)
192 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
193 PrintToConsole(client, "%s", mapName);
196 return Plugin_Handled;
199 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
201 if (g_IntermissionCalled)
203 return Plugin_Handled;
206 decl String:type[15];
207 BfReadString(bf, type, sizeof(type));
209 if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
211 g_IntermissionCalled = true;
214 new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
216 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
219 SetConVarFloat(g_Cvar_Chattime, 2.0);
222 CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
223 WritePackString(dp, map);
226 return Plugin_Handled;
229 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
234 ReadPackString(dp, map, sizeof(map));
236 InsertServerCommand("changelevel \"%s\"", map);
239 LogMessage("Nextmap changed map to \"%s\"", map);
244 LoadMaps(const String:path[])
246 if (!FileExists(path))
251 new Handle:file = OpenFile(path, "r");
253 if (file == INVALID_HANDLE)
255 LogError("[SM] Could not open file: %s", path);
259 ClearArray(g_MapList);
262 decl String:buffer[255];
263 while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
266 if (buffer[0] == '\0' || buffer[0] == ';')
271 if (IsMapValid(buffer))
273 PushArrayString(g_MapList, buffer);
279 return GetArraySize(g_MapList);
284 new mapCount = GetArraySize(g_MapList);
285 decl String:mapName[32];
289 decl String:current[64];
290 GetCurrentMap(current, 64);
293 for (new i = 0; i < mapCount; i++)
295 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
296 if (strcmp(current, mapName, false) == 0)
308 if (g_MapPos >= mapCount)
311 GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
312 SetConVarString(g_Cvar_Nextmap, mapName);