plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Fri Dec 14 05:54:36 2007 +0000 (2007-12-14)
changeset 1791 38e83ea09a24
parent 1783 e6e2f440376d
child 1793 735f79933e12
permissions -rw-r--r--
fixed amb1282 - elev1.wav is cstrike only
     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 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    80 	RegConsoleCmd("listmaps", Command_List);
    81 
    82 	// Set to the current map so OnMapStart() will know what to do
    83 	decl String:currentMap[64];
    84 	GetCurrentMap(currentMap, 64);
    85 	SetConVarString(g_Cvar_Nextmap, currentMap);
    86 }
    87  
    88 public OnConfigsExecuted()
    89 {
    90 	decl String:lastMap[64], String:currentMap[64];
    91 	GetConVarString(g_Cvar_Nextmap, lastMap, 64);
    92 	GetCurrentMap(currentMap, 64);
    93 	
    94 	// 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
    95 	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
    96 	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
    97 	if (strcmp(lastMap, currentMap) == 0)
    98 	{
    99 		FindAndSetNextMap();
   100 	}
   101 }
   102  
   103 public OnMapEnd()
   104 {
   105 	g_IntermissionCalled = false;
   106 }
   107 
   108 public Action:Command_Say(client, args)
   109 {
   110 	decl String:text[192];
   111 	if (GetCmdArgString(text, sizeof(text)) < 1)
   112 	{
   113 		return Plugin_Continue;
   114 	}
   115 	
   116 	new startidx;
   117 	if (text[strlen(text)-1] == '"')
   118 	{
   119 		text[strlen(text)-1] = '\0';
   120 		startidx = 1;
   121 	}
   122 	
   123 	decl String:message[8];
   124 	BreakString(text[startidx], message, sizeof(message));
   125 	
   126 	if (strcmp(message, "nextmap", false) == 0)
   127 	{
   128 		decl String:map[32];
   129 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   130 		
   131 		PrintToChatAll("%t", "Next Map", map);
   132 	}
   133 	
   134 	return Plugin_Continue;	
   135 }
   136 
   137 public Action:Command_SetNextmap(client, args)
   138 {
   139 	if (args < 1)
   140 	{
   141 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   142 		return Plugin_Handled;
   143 	}
   144 
   145 	decl String:map[64];
   146 	GetCmdArg(1, map, sizeof(map));
   147 
   148 	if (!IsMapValid(map))
   149 	{
   150 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   151 		return Plugin_Handled;
   152 	}
   153 
   154 	ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
   155 	LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
   156 
   157 	SetConVarString(g_Cvar_Nextmap, map);
   158 
   159 	return Plugin_Handled;
   160 }
   161 
   162 public Action:Command_List(client, args) 
   163 {
   164 	PrintToConsole(client, "Map Cycle:");
   165 	
   166 	new mapCount = GetArraySize(g_MapList);
   167 	decl String:mapName[32];
   168 	for (new i = 0; i < mapCount; i++)
   169 	{
   170 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   171 		PrintToConsole(client, "%s", mapName);
   172 	}
   173  
   174 	return Plugin_Handled;
   175 }
   176  
   177 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
   178 {
   179 	if (g_IntermissionCalled)
   180 	{
   181 		return Plugin_Handled;
   182 	}
   183 	
   184 	decl String:type[15];
   185 
   186 	/* If we don't get a valid string, bail out. */
   187 	if (BfReadString(bf, type, sizeof(type)) < 0)
   188 	{
   189 		return Plugin_Handled;
   190 	}
   191  
   192 	if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
   193 	{
   194 		g_IntermissionCalled = true;
   195 		
   196 		decl String:map[32];
   197 		new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
   198 		
   199 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   200 		
   201 		if (!IsMapValid(map))
   202 		{
   203 			if (g_MapPos == -1)
   204 			{
   205 				FindAndSetNextMap();
   206 			}
   207 			GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
   208 		}
   209 		
   210 		if (fChatTime < 2.0)
   211 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   212 		
   213 		new Handle:dp;
   214 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   215 		WritePackString(dp, map);
   216 	}
   217 	
   218 	return Plugin_Handled;
   219 }
   220  
   221 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   222 {
   223 	new String:map[32];
   224 	
   225 	ResetPack(dp);
   226 	ReadPackString(dp, map, sizeof(map));
   227  
   228 	InsertServerCommand("changelevel \"%s\"", map);
   229 	ServerExecute();
   230 	
   231 	LogMessage("Nextmap changed map to \"%s\"", map);
   232 	
   233 	return Plugin_Stop;
   234 }
   235  
   236 FindAndSetNextMap()
   237 {
   238 	if (ReadMapList(g_MapList, 
   239 			g_MapListSerial, 
   240 			"mapcyclefile", 
   241 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   242 		== INVALID_HANDLE)
   243 	{
   244 		if (g_MapListSerial == -1)
   245 		{
   246 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   247 			SetFailState("Mapcycle Not Found");
   248 		}
   249 	}
   250 	
   251 	new mapCount = GetArraySize(g_MapList);
   252 	decl String:mapName[32];
   253 	
   254 	if (g_MapPos == -1)
   255 	{
   256 		decl String:current[64];
   257 		GetCurrentMap(current, 64);
   258 
   259 		for (new i = 0; i < mapCount; i++)
   260 		{
   261 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   262 			if (strcmp(current, mapName, false) == 0)
   263 			{
   264 				g_MapPos = i;
   265 				break;
   266 			}
   267 		}
   268 		
   269 		if (g_MapPos == -1)
   270 			g_MapPos = 0;
   271 	}
   272 	
   273 	g_MapPos++;
   274 	if (g_MapPos >= mapCount)
   275 		g_MapPos = 0;	
   276  
   277  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   278 	SetConVarString(g_Cvar_Nextmap, mapName);
   279 }