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;
54 new Handle:g_MapList = INVALID_HANDLE;
55 new g_MapListSerial = -1;
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_MapList = CreateArray(32);
71 HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
73 g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
74 g_Cvar_Chattime = FindConVar("mp_chattime");
76 RegConsoleCmd("say", Command_Say);
77 RegConsoleCmd("say_team", Command_Say);
79 if (GetCommandFlags("nextmap") == INVALID_FCVAR_FLAGS)
81 RegServerCmd("nextmap", Command_Nextmap);
84 RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
85 RegConsoleCmd("listmaps", Command_List);
87 // Set to the current map so OnMapStart() will know what to do
88 decl String:currentMap[64];
89 GetCurrentMap(currentMap, 64);
90 SetConVarString(g_Cvar_Nextmap, currentMap);
93 public OnConfigsExecuted()
95 decl String:lastMap[64], String:currentMap[64];
96 GetConVarString(g_Cvar_Nextmap, lastMap, 64);
97 GetCurrentMap(currentMap, 64);
99 // 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
100 // some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
101 // not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
102 if (strcmp(lastMap, currentMap) == 0)
110 g_IntermissionCalled = false;
113 public Action:Command_Say(client, args)
115 decl String:text[192];
116 if (GetCmdArgString(text, sizeof(text)) < 1)
118 return Plugin_Continue;
122 if (text[strlen(text)-1] == '"')
124 text[strlen(text)-1] = '\0';
128 decl String:message[8];
129 BreakString(text[startidx], message, sizeof(message));
131 if (strcmp(message, "nextmap", false) == 0)
134 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
136 PrintToChat(client, "%t", "Next Map", map);
139 return Plugin_Continue;
142 public Action:Command_SetNextmap(client, args)
146 ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
147 return Plugin_Handled;
151 GetCmdArg(1, map, sizeof(map));
153 if (!IsMapValid(map))
155 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
156 return Plugin_Handled;
159 ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
160 LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
162 SetConVarString(g_Cvar_Nextmap, map);
164 return Plugin_Handled;
167 public Action:Command_List(client, args)
169 PrintToConsole(client, "Map Cycle:");
171 new mapCount = GetArraySize(g_MapList);
172 decl String:mapName[32];
173 for (new i = 0; i < mapCount; i++)
175 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
176 PrintToConsole(client, "%s", mapName);
179 return Plugin_Handled;
182 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
184 if (g_IntermissionCalled)
186 return Plugin_Handled;
189 decl String:type[15];
191 /* If we don't get a valid string, bail out. */
192 if (BfReadString(bf, type, sizeof(type)) < 0)
194 return Plugin_Handled;
197 if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
199 g_IntermissionCalled = true;
202 new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
204 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
206 if (!IsMapValid(map))
212 GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
216 SetConVarFloat(g_Cvar_Chattime, 2.0);
219 CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
220 WritePackString(dp, map);
223 return Plugin_Handled;
226 public Action:Command_Nextmap(args)
230 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
232 ReplyToCommand(0, "%t", "Next Map", map);
234 return Plugin_Handled;
237 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
242 ReadPackString(dp, map, sizeof(map));
244 InsertServerCommand("changelevel \"%s\"", map);
247 LogMessage("Nextmap changed map to \"%s\"", map);
254 if (ReadMapList(g_MapList,
257 MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
260 if (g_MapListSerial == -1)
262 LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
263 SetFailState("Mapcycle Not Found");
267 new mapCount = GetArraySize(g_MapList);
268 decl String:mapName[32];
272 decl String:current[64];
273 GetCurrentMap(current, 64);
275 for (new i = 0; i < mapCount; i++)
277 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
278 if (strcmp(current, mapName, false) == 0)
290 if (g_MapPos >= mapCount)
293 GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
294 SetConVarString(g_Cvar_Nextmap, mapName);