plugins/nextmap.sp
author Borja Ferrer <faluco@alliedmods.net>
Fri Sep 28 20:13:42 2007 +0000 (2007-09-28)
changeset 1499 e55ac4555a2b
parent 1492 9df4d5db498d
child 1531 766b08c6e6c2
permissions -rw-r--r--
synced linux build
     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 new g_mapFileTime;
    57  
    58 public OnPluginStart()
    59 {
    60 	LoadTranslations("common.phrases");
    61 	LoadTranslations("nextmap.phrases");
    62 	
    63 	g_VGUIMenu = GetUserMessageId("VGUIMenu");
    64 	if (g_VGUIMenu == INVALID_MESSAGE_ID)
    65 	{
    66 		LogError("FATAL: Cannot find VGUIMenu user message id. Nextmap not loaded.");
    67 		SetFailState("VGUIMenu Not Found");
    68 	}
    69 	
    70 	g_Cvar_Chattime = FindConVar("mp_chattime");
    71 	g_Cvar_Mapcycle = FindConVar("mapcyclefile");
    72 	
    73 	g_MapList = CreateArray(32);
    74 
    75 	if (!LoadMaps())
    76 	{
    77 		LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
    78 		SetFailState("Mapcycle Not Found");		
    79 	}	
    80 	
    81 	HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu);
    82 	HookConVarChange(g_Cvar_Mapcycle, ConVarChange_Mapcyclefile);
    83 	
    84 	g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY);
    85 	
    86 	RegConsoleCmd("say", Command_Say);
    87 	RegConsoleCmd("say_team", Command_Say);
    88 
    89 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    90 	RegConsoleCmd("listmaps", Command_List);
    91 
    92 	// Set to the current map so OnMapStart() will know what to do
    93 	decl String:currentMap[64];
    94 	GetCurrentMap(currentMap, 64);
    95 	SetConVarString(g_Cvar_Nextmap, currentMap);
    96 }
    97  
    98 public OnMapStart()
    99 {
   100 	decl String:lastMap[64], String:currentMap[64];
   101 	GetConVarString(g_Cvar_Nextmap, lastMap, 64);
   102 	GetCurrentMap(currentMap, 64);
   103 	
   104 	// 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
   105 	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
   106 	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
   107 	if (strcmp(lastMap, currentMap) == 0)
   108 	{
   109 		if (!LoadMaps())
   110 		{
   111 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   112 			SetFailState("Mapcycle Not Found");	
   113 		}		
   114 		
   115 		FindAndSetNextMap();
   116 	}
   117 }
   118  
   119 public OnMapEnd()
   120 {
   121 	g_IntermissionCalled = false;
   122 }
   123  
   124 public ConVarChange_Mapcyclefile(Handle:convar, const String:oldValue[], const String:newValue[])
   125 {
   126 	if (strcmp(oldValue, newValue, false) != 0)
   127 	{
   128 		if (!LoadMaps())
   129 		{
   130 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   131 			SetFailState("Mapcycle Not Found");	
   132 		}
   133 		
   134 		FindAndSetNextMap();
   135 	}
   136 }
   137  
   138 public Action:Command_Say(client, args)
   139 {
   140 	decl String:text[192];
   141 	if (GetCmdArgString(text, sizeof(text)) < 1)
   142 	{
   143 		return Plugin_Continue;
   144 	}
   145 	
   146 	new startidx;
   147 	if (text[strlen(text)-1] == '"')
   148 	{
   149 		text[strlen(text)-1] = '\0';
   150 		startidx = 1;
   151 	}
   152 	
   153 	decl String:message[8];
   154 	BreakString(text[startidx], message, sizeof(message));
   155 	
   156 	if (strcmp(message, "nextmap", false) == 0)
   157 	{
   158 		decl String:map[32];
   159 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   160 		
   161 		PrintToChatAll("%t", "Next Map", map);
   162 	}
   163 	
   164 	return Plugin_Continue;	
   165 }
   166 
   167 public Action:Command_SetNextmap(client, args)
   168 {
   169 	if (args < 1)
   170 	{
   171 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   172 		return Plugin_Handled;
   173 	}
   174 
   175 	decl String:map[64];
   176 	GetCmdArg(1, map, sizeof(map));
   177 
   178 	if (!IsMapValid(map))
   179 	{
   180 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   181 		return Plugin_Handled;
   182 	}
   183 
   184 	ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
   185 	LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
   186 
   187 	SetConVarString(g_Cvar_Nextmap, map);
   188 
   189 	return Plugin_Handled;
   190 }
   191 
   192 public Action:Command_List(client, args) 
   193 {
   194 	PrintToConsole(client, "Map Cycle:");
   195 	
   196 	new mapCount = GetArraySize(g_MapList);
   197 	decl String:mapName[32];
   198 	for (new i = 0; i < mapCount; i++)
   199 	{
   200 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   201 		PrintToConsole(client, "%s", mapName);
   202 	}
   203  
   204 	return Plugin_Handled;
   205 }
   206  
   207 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
   208 {
   209 	if (g_IntermissionCalled)
   210 	{
   211 		return Plugin_Handled;
   212 	}
   213 	
   214 	decl String:type[15];
   215 	BfReadString(bf, type, sizeof(type));
   216  
   217 	if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
   218 	{
   219 		g_IntermissionCalled = true;
   220 		
   221 		decl String:map[32];
   222 		new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
   223 		
   224 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   225 		
   226 		if (fChatTime < 2.0)
   227 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   228 		
   229 		new Handle:dp;
   230 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   231 		WritePackString(dp, map);
   232 	}
   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 LoadMaps()
   253 {
   254 	decl String:mapCycle[64];
   255 	GetConVarString(g_Cvar_Mapcycle, mapCycle, 64);	
   256 	
   257 	if (!FileExists(mapCycle))
   258 	{
   259 		LogError("[SM] Could not find file: %s", mapCycle);
   260 		return 0;
   261 	}
   262 	
   263 	new fileTime =  GetFileTime(mapCycle, FileTime_LastChange);
   264 	if (g_mapFileTime == fileTime)
   265 	{
   266 		return GetArraySize(g_MapList);
   267 	}
   268 	
   269 	g_mapFileTime = fileTime;
   270  
   271 	new Handle:file = OpenFile(mapCycle, "r");
   272 	
   273 	if (file == INVALID_HANDLE)
   274 	{
   275 		LogError("[SM] Could not open file: %s", mapCycle);
   276 		return 0;
   277 	}
   278 	
   279 	g_MapPos = -1;
   280 	if (g_MapList != INVALID_HANDLE)
   281 	{
   282 		ClearArray(g_MapList);
   283 	}
   284 	
   285 	decl String:buffer[255];
   286 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   287 	{
   288 		TrimString(buffer);
   289 		if (buffer[0] == '\0' || buffer[0] == ';')
   290 		{
   291 			continue;
   292 		}
   293 		
   294 		if (IsMapValid(buffer))
   295 		{
   296 			PushArrayString(g_MapList, buffer);
   297 		}
   298 	}
   299 	
   300 	CloseHandle(file);
   301 
   302 	return GetArraySize(g_MapList);
   303 }
   304  
   305 FindAndSetNextMap()
   306 {
   307 	new mapCount = GetArraySize(g_MapList);
   308 	decl String:mapName[32];
   309 	
   310 	if (g_MapPos == -1)
   311 	{
   312 		decl String:current[64];
   313 		GetCurrentMap(current, 64);
   314 		
   315 
   316 		for (new i = 0; i < mapCount; i++)
   317 		{
   318 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   319 			if (strcmp(current, mapName, false) == 0)
   320 			{
   321 				g_MapPos = i;
   322 				break;
   323 			}
   324 		}
   325 		
   326 		if (g_MapPos == -1)
   327 			g_MapPos = 0;
   328 	}
   329 	
   330 	g_MapPos++;
   331 	if (g_MapPos >= mapCount)
   332 		g_MapPos = 0;	
   333  
   334  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   335 	SetConVarString(g_Cvar_Nextmap, mapName);
   336 }