plugins/nextmap.sp
author Michael McKoy <ferret99gt@gmail.com>
Sat Jan 12 19:45:36 2008 +0000 (2008-01-12)
changeset 1845 a2e4c1cbf800
parent 1793 735f79933e12
child 1947 e73a1e51833b
permissions -rw-r--r--
New stock to get m_fFlags, and #defines for its bits.
     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 
    53 new g_MapPos = -1;
    54 new Handle:g_MapList = INVALID_HANDLE;
    55 new g_MapListSerial = -1;
    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_MapList = CreateArray(32);
    70 
    71 	HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
    72 	
    73 	g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
    74 	g_Cvar_Chattime = FindConVar("mp_chattime");
    75 	
    76 	RegConsoleCmd("say", Command_Say);
    77 	RegConsoleCmd("say_team", Command_Say);
    78 	
    79 	if (GetCommandFlags("nextmap") == INVALID_FCVAR_FLAGS)
    80 	{
    81 		RegServerCmd("nextmap", Command_Nextmap);
    82 	}
    83 
    84 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    85 	RegConsoleCmd("listmaps", Command_List);
    86 
    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);
    91 }
    92  
    93 public OnConfigsExecuted()
    94 {
    95 	decl String:lastMap[64], String:currentMap[64];
    96 	GetConVarString(g_Cvar_Nextmap, lastMap, 64);
    97 	GetCurrentMap(currentMap, 64);
    98 	
    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)
   103 	{
   104 		FindAndSetNextMap();
   105 	}
   106 }
   107  
   108 public OnMapEnd()
   109 {
   110 	g_IntermissionCalled = false;
   111 }
   112 
   113 public Action:Command_Say(client, args)
   114 {
   115 	decl String:text[192];
   116 	if (GetCmdArgString(text, sizeof(text)) < 1)
   117 	{
   118 		return Plugin_Continue;
   119 	}
   120 	
   121 	new startidx;
   122 	if (text[strlen(text)-1] == '"')
   123 	{
   124 		text[strlen(text)-1] = '\0';
   125 		startidx = 1;
   126 	}
   127 	
   128 	decl String:message[8];
   129 	BreakString(text[startidx], message, sizeof(message));
   130 	
   131 	if (strcmp(message, "nextmap", false) == 0)
   132 	{
   133 		decl String:map[32];
   134 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   135 		
   136 		PrintToChat(client, "%t", "Next Map", map);
   137 	}
   138 	
   139 	return Plugin_Continue;	
   140 }
   141 
   142 public Action:Command_SetNextmap(client, args)
   143 {
   144 	if (args < 1)
   145 	{
   146 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   147 		return Plugin_Handled;
   148 	}
   149 
   150 	decl String:map[64];
   151 	GetCmdArg(1, map, sizeof(map));
   152 
   153 	if (!IsMapValid(map))
   154 	{
   155 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   156 		return Plugin_Handled;
   157 	}
   158 
   159 	ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
   160 	LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
   161 
   162 	SetConVarString(g_Cvar_Nextmap, map);
   163 
   164 	return Plugin_Handled;
   165 }
   166 
   167 public Action:Command_List(client, args) 
   168 {
   169 	PrintToConsole(client, "Map Cycle:");
   170 	
   171 	new mapCount = GetArraySize(g_MapList);
   172 	decl String:mapName[32];
   173 	for (new i = 0; i < mapCount; i++)
   174 	{
   175 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   176 		PrintToConsole(client, "%s", mapName);
   177 	}
   178  
   179 	return Plugin_Handled;
   180 }
   181  
   182 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
   183 {
   184 	if (g_IntermissionCalled)
   185 	{
   186 		return Plugin_Handled;
   187 	}
   188 	
   189 	decl String:type[15];
   190 
   191 	/* If we don't get a valid string, bail out. */
   192 	if (BfReadString(bf, type, sizeof(type)) < 0)
   193 	{
   194 		return Plugin_Handled;
   195 	}
   196  
   197 	if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
   198 	{
   199 		g_IntermissionCalled = true;
   200 		
   201 		decl String:map[32];
   202 		new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
   203 		
   204 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   205 		
   206 		if (!IsMapValid(map))
   207 		{
   208 			if (g_MapPos == -1)
   209 			{
   210 				FindAndSetNextMap();
   211 			}
   212 			GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
   213 		}
   214 		
   215 		if (fChatTime < 2.0)
   216 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   217 		
   218 		new Handle:dp;
   219 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   220 		WritePackString(dp, map);
   221 	}
   222 	
   223 	return Plugin_Handled;
   224 }
   225 
   226 public Action:Command_Nextmap(args)
   227 {
   228 	decl String:map[64];
   229 	
   230 	GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   231 	
   232 	ReplyToCommand(0, "%t", "Next Map", map);
   233 	
   234 	return Plugin_Handled;
   235 }
   236  
   237 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   238 {
   239 	new String:map[32];
   240 	
   241 	ResetPack(dp);
   242 	ReadPackString(dp, map, sizeof(map));
   243  
   244 	InsertServerCommand("changelevel \"%s\"", map);
   245 	ServerExecute();
   246 	
   247 	LogMessage("Nextmap changed map to \"%s\"", map);
   248 	
   249 	return Plugin_Stop;
   250 }
   251  
   252 FindAndSetNextMap()
   253 {
   254 	if (ReadMapList(g_MapList, 
   255 			g_MapListSerial, 
   256 			"mapcyclefile", 
   257 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   258 		== INVALID_HANDLE)
   259 	{
   260 		if (g_MapListSerial == -1)
   261 		{
   262 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   263 			SetFailState("Mapcycle Not Found");
   264 		}
   265 	}
   266 	
   267 	new mapCount = GetArraySize(g_MapList);
   268 	decl String:mapName[32];
   269 	
   270 	if (g_MapPos == -1)
   271 	{
   272 		decl String:current[64];
   273 		GetCurrentMap(current, 64);
   274 
   275 		for (new i = 0; i < mapCount; i++)
   276 		{
   277 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   278 			if (strcmp(current, mapName, false) == 0)
   279 			{
   280 				g_MapPos = i;
   281 				break;
   282 			}
   283 		}
   284 		
   285 		if (g_MapPos == -1)
   286 			g_MapPos = 0;
   287 	}
   288 	
   289 	g_MapPos++;
   290 	if (g_MapPos >= mapCount)
   291 		g_MapPos = 0;	
   292  
   293  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   294 	SetConVarString(g_Cvar_Nextmap, mapName);
   295 }