plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Fri Sep 14 02:11:10 2007 +0000 (2007-09-14)
changeset 1423 e73b3563f254
parent 1422 47de36d9386c
child 1439 e184b23ba4d8
permissions -rw-r--r--
- added 3 new natives for banning/unbanning and interception
- moved banning commands into new plugin, basebans
- improved quality and input methods of ban commands
- corrected svn props on a few files
- added an IsLANServer() to IGameHelpers API
- completely deprecated and removed old banning forwards
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Nextmap Plugin
     5  * Adds sm_nextmap cvar for changing map and nextmap chat trigger.
     6  *
     7  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    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.
    13  * 
    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
    17  * details.
    18  *
    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/>.
    21  *
    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>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37  
    38 public Plugin:myinfo = 
    39 {
    40 	name = "Nextmap",
    41 	author = "AlliedModders LLC",
    42 	description = "Provides nextmap and sm_nextmap",
    43 	version = SOURCEMOD_VERSION,
    44 	url = "http://www.sourcemod.net/"
    45 };
    46 
    47 new bool:g_IntermissionCalled;
    48 new UserMsg:g_VGUIMenu;
    49  
    50 new Handle:g_Cvar_Chattime;
    51 new Handle:g_Cvar_Nextmap;
    52 new Handle:g_Cvar_Mapcycle;
    53 
    54 new g_MapPos = -1;
    55 new Handle:g_MapList = INVALID_HANDLE;
    56  
    57 public OnPluginStart()
    58 {
    59 	LoadTranslations("common.phrases");
    60 	LoadTranslations("nextmap.phrases");
    61 	
    62 	g_VGUIMenu = GetUserMessageId("VGUIMenu");
    63 	if (g_VGUIMenu == INVALID_MESSAGE_ID)
    64 	{
    65 		LogError("FATAL: Cannot find VGUIMenu user message id. Nextmap not loaded.");
    66 		SetFailState("VGUIMenu Not Found");
    67 	}
    68 	
    69 	g_Cvar_Chattime = FindConVar("mp_chattime");
    70 	g_Cvar_Mapcycle = FindConVar("mapcyclefile");
    71 	
    72 	g_MapList = CreateArray(32);
    73 	
    74 	decl String:mapCycle[64];
    75 	GetConVarString(g_Cvar_Mapcycle, mapCycle, 64);
    76 	if (LoadMaps(mapCycle) < 1)
    77 	{
    78 		LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
    79 		SetFailState("Mapcycle Not Found");		
    80 	}	
    81 	
    82 	HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
    83 	HookConVarChange(g_Cvar_Mapcycle, ConVarChange_Mapcyclefile);
    84 	
    85 	g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
    86 	
    87 	RegConsoleCmd("say", Command_Say);
    88 	RegConsoleCmd("say_team", Command_Say);
    89 
    90 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    91 	RegConsoleCmd("listmaps", Command_List);
    92 
    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);
    97 }
    98  
    99 public OnMapStart()
   100 {
   101 	decl String:lastMap[64], String:currentMap[64];
   102 	GetConVarString(g_Cvar_Nextmap, lastMap, 64);
   103 	GetCurrentMap(currentMap, 64);
   104 	
   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)
   109 	{
   110 		FindAndSetNextMap();
   111 	}
   112 }
   113  
   114 public OnMapEnd()
   115 {
   116 	g_IntermissionCalled = false;
   117 }
   118  
   119 public ConVarChange_Mapcyclefile(Handle:convar, const String:oldValue[], const String:newValue[])
   120 {
   121 	if (strcmp(oldValue, newValue, false) != 0)
   122 	{
   123 		if (LoadMaps(newValue) < 1)
   124 		{
   125 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   126 			SetFailState("Mapcycle Not Found");	
   127 		}
   128 		
   129 		FindAndSetNextMap();
   130 	}
   131 }
   132  
   133 public Action:Command_Say(client, args)
   134 {
   135 	decl String:text[192];
   136 	if (GetCmdArgString(text, sizeof(text)) < 1)
   137 	{
   138 		return Plugin_Continue;
   139 	}
   140 	
   141 	new startidx;
   142 	if (text[strlen(text)-1] == '"')
   143 	{
   144 		text[strlen(text)-1] = '\0';
   145 		startidx = 1;
   146 	}
   147 	
   148 	decl String:message[8];
   149 	BreakString(text[startidx], message, sizeof(message));
   150 	
   151 	if (strcmp(message, "nextmap", false) == 0)
   152 	{
   153 		decl String:map[32];
   154 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   155 		
   156 		PrintToChatAll("%t", "Next Map", map);
   157 	}
   158 	
   159 	return Plugin_Continue;	
   160 }
   161 
   162 public Action:Command_SetNextmap(client, args)
   163 {
   164 	if (args < 1)
   165 	{
   166 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   167 		return Plugin_Handled;
   168 	}
   169 
   170 	decl String:map[64];
   171 	GetCmdArg(1, map, sizeof(map));
   172 
   173 	if (!IsMapValid(map))
   174 	{
   175 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   176 		return Plugin_Handled;
   177 	}
   178 
   179 	ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
   180 	LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
   181 
   182 	SetConVarString(g_Cvar_Nextmap, map);
   183 
   184 	return Plugin_Handled;
   185 }
   186 
   187 public Action:Command_List(client, args) 
   188 {
   189 	PrintToConsole(client, "Map Cycle:");
   190 	
   191 	new mapCount = GetArraySize(g_MapList);
   192 	decl String:mapName[32];
   193 	for (new i = 0; i < mapCount; i++)
   194 	{
   195 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   196 		PrintToConsole(client, "%s", mapName);
   197 	}
   198  
   199 	return Plugin_Handled;
   200 }
   201  
   202 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
   203 {
   204 	if (g_IntermissionCalled)
   205 	{
   206 		return Plugin_Handled;
   207 	}
   208 	
   209 	decl String:type[15];
   210 	BfReadString(bf, type, sizeof(type));
   211  
   212 	if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
   213 	{
   214 		g_IntermissionCalled = true;
   215 		
   216 		decl String:map[32];
   217 		new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
   218 		
   219 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   220 		
   221 		if (fChatTime < 2.0)
   222 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   223 		
   224 		new Handle:dp;
   225 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   226 		WritePackString(dp, map);
   227 	}
   228 	
   229 	return Plugin_Handled;
   230 }
   231  
   232 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   233 {
   234 	new String:map[32];
   235 	
   236 	ResetPack(dp);
   237 	ReadPackString(dp, map, sizeof(map));
   238  
   239 	InsertServerCommand("changelevel \"%s\"", map);
   240 	ServerExecute();
   241 	
   242 	LogMessage("Nextmap changed map to \"%s\"", map);
   243 	
   244 	return Plugin_Stop;
   245 }
   246  
   247 LoadMaps(const String:path[])
   248 {
   249 	if (!FileExists(path))
   250 	{
   251 		return 0;
   252 	}
   253  
   254 	new Handle:file = OpenFile(path, "r");
   255 	
   256 	if (file == INVALID_HANDLE)
   257 	{
   258 		LogError("[SM] Could not open file: %s", path);
   259 		return 0;
   260 	}
   261 	
   262 	ClearArray(g_MapList);
   263 	g_MapPos = -1;
   264 	
   265 	decl String:buffer[255];
   266 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   267 	{
   268 		TrimString(buffer);
   269 		if (buffer[0] == '\0' || buffer[0] == ';')
   270 		{
   271 			continue;
   272 		}
   273 		
   274 		if (IsMapValid(buffer))
   275 		{
   276 			PushArrayString(g_MapList, buffer);
   277 		}
   278 	}
   279 	
   280 	CloseHandle(file);
   281 
   282 	return GetArraySize(g_MapList);
   283 }
   284  
   285 FindAndSetNextMap()
   286 {
   287 	new mapCount = GetArraySize(g_MapList);
   288 	decl String:mapName[32];
   289 	
   290 	if (g_MapPos == -1)
   291 	{
   292 		decl String:current[64];
   293 		GetCurrentMap(current, 64);
   294 		
   295 
   296 		for (new i = 0; i < mapCount; i++)
   297 		{
   298 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   299 			if (strcmp(current, mapName, false) == 0)
   300 			{
   301 				g_MapPos = i;
   302 				break;
   303 			}
   304 		}
   305 		
   306 		if (g_MapPos == -1)
   307 			g_MapPos = 0;
   308 	}
   309 	
   310 	g_MapPos++;
   311 	if (g_MapPos >= mapCount)
   312 		g_MapPos = 0;	
   313  
   314  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   315 	SetConVarString(g_Cvar_Nextmap, mapName);
   316 }