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;
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 if (!LoadMaps(g_MapList, g_mapFileTime))
73 LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
74 SetFailState("Mapcycle Not Found");
77 HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
79 g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
80 g_Cvar_Chattime = FindConVar("mp_chattime");
82 RegConsoleCmd("say", Command_Say);
83 RegConsoleCmd("say_team", Command_Say);
85 RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
86 RegConsoleCmd("listmaps", Command_List);
88 // Set to the current map so OnMapStart() will know what to do
89 decl String:currentMap[64];
90 GetCurrentMap(currentMap, 64);
91 SetConVarString(g_Cvar_Nextmap, currentMap);
96 decl String:lastMap[64], String:currentMap[64];
97 GetConVarString(g_Cvar_Nextmap, lastMap, 64);
98 GetCurrentMap(currentMap, 64);
100 // 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
101 // some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
102 // not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
103 if (strcmp(lastMap, currentMap) == 0)
105 if (!LoadMaps(g_MapList, g_mapFileTime))
107 LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
108 SetFailState("Mapcycle Not Found");
117 g_IntermissionCalled = false;
120 public Action:Command_Say(client, args)
122 decl String:text[192];
123 if (GetCmdArgString(text, sizeof(text)) < 1)
125 return Plugin_Continue;
129 if (text[strlen(text)-1] == '"')
131 text[strlen(text)-1] = '\0';
135 decl String:message[8];
136 BreakString(text[startidx], message, sizeof(message));
138 if (strcmp(message, "nextmap", false) == 0)
141 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
143 PrintToChatAll("%t", "Next Map", map);
146 return Plugin_Continue;
149 public Action:Command_SetNextmap(client, args)
153 ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
154 return Plugin_Handled;
158 GetCmdArg(1, map, sizeof(map));
160 if (!IsMapValid(map))
162 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
163 return Plugin_Handled;
166 ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
167 LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
169 SetConVarString(g_Cvar_Nextmap, map);
171 return Plugin_Handled;
174 public Action:Command_List(client, args)
176 PrintToConsole(client, "Map Cycle:");
178 new mapCount = GetArraySize(g_MapList);
179 decl String:mapName[32];
180 for (new i = 0; i < mapCount; i++)
182 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
183 PrintToConsole(client, "%s", mapName);
186 return Plugin_Handled;
189 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
191 if (g_IntermissionCalled)
193 return Plugin_Handled;
196 decl String:type[15];
197 BfReadString(bf, type, sizeof(type));
199 if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
201 g_IntermissionCalled = true;
204 new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
206 GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
208 if (!IsMapValid(map))
210 GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
214 SetConVarFloat(g_Cvar_Chattime, 2.0);
217 CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
218 WritePackString(dp, map);
221 return Plugin_Handled;
224 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
229 ReadPackString(dp, map, sizeof(map));
231 InsertServerCommand("changelevel \"%s\"", map);
234 LogMessage("Nextmap changed map to \"%s\"", map);
241 new mapCount = GetArraySize(g_MapList);
242 decl String:mapName[32];
246 decl String:current[64];
247 GetCurrentMap(current, 64);
249 for (new i = 0; i < mapCount; i++)
251 GetArrayString(g_MapList, i, mapName, sizeof(mapName));
252 if (strcmp(current, mapName, false) == 0)
264 if (g_MapPos >= mapCount)
267 GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
268 SetConVarString(g_Cvar_Nextmap, mapName);