plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 21:51:30 2007 +0000 (2007-10-14)
changeset 1559 bb3fd890699d
parent 1531 766b08c6e6c2
child 1606 4fb3959c2c96
permissions -rw-r--r--
fixed bug in build tool
     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 (!IsMapValid(map))
   227 		{
   228 			GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
   229 		}
   230 		
   231 		if (fChatTime < 2.0)
   232 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   233 		
   234 		new Handle:dp;
   235 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   236 		WritePackString(dp, map);
   237 	}
   238 	
   239 	return Plugin_Handled;
   240 }
   241  
   242 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   243 {
   244 	new String:map[32];
   245 	
   246 	ResetPack(dp);
   247 	ReadPackString(dp, map, sizeof(map));
   248  
   249 	InsertServerCommand("changelevel \"%s\"", map);
   250 	ServerExecute();
   251 	
   252 	LogMessage("Nextmap changed map to \"%s\"", map);
   253 	
   254 	return Plugin_Stop;
   255 }
   256  
   257 LoadMaps()
   258 {
   259 	decl String:mapCycle[64];
   260 	GetConVarString(g_Cvar_Mapcycle, mapCycle, 64);	
   261 	
   262 	if (!FileExists(mapCycle))
   263 	{
   264 		LogError("[SM] Could not find file: %s", mapCycle);
   265 		return 0;
   266 	}
   267 	
   268 	new fileTime =  GetFileTime(mapCycle, FileTime_LastChange);
   269 	if (g_mapFileTime == fileTime)
   270 	{
   271 		return GetArraySize(g_MapList);
   272 	}
   273 	
   274 	g_mapFileTime = fileTime;
   275  
   276 	new Handle:file = OpenFile(mapCycle, "r");
   277 	
   278 	if (file == INVALID_HANDLE)
   279 	{
   280 		LogError("[SM] Could not open file: %s", mapCycle);
   281 		return 0;
   282 	}
   283 	
   284 	g_MapPos = -1;
   285 	if (g_MapList != INVALID_HANDLE)
   286 	{
   287 		ClearArray(g_MapList);
   288 	}
   289 	
   290 	decl String:buffer[255];
   291 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   292 	{
   293 		TrimString(buffer);
   294 		if (buffer[0] == '\0' || buffer[0] == ';')
   295 		{
   296 			continue;
   297 		}
   298 		
   299 		if (IsMapValid(buffer))
   300 		{
   301 			PushArrayString(g_MapList, buffer);
   302 		}
   303 	}
   304 	
   305 	CloseHandle(file);
   306 
   307 	return GetArraySize(g_MapList);
   308 }
   309  
   310 FindAndSetNextMap()
   311 {
   312 	new mapCount = GetArraySize(g_MapList);
   313 	decl String:mapName[32];
   314 	
   315 	if (g_MapPos == -1)
   316 	{
   317 		decl String:current[64];
   318 		GetCurrentMap(current, 64);
   319 
   320 		for (new i = 0; i < mapCount; i++)
   321 		{
   322 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   323 			if (strcmp(current, mapName, false) == 0)
   324 			{
   325 				g_MapPos = i;
   326 				break;
   327 			}
   328 		}
   329 		
   330 		if (g_MapPos == -1)
   331 			g_MapPos = 0;
   332 	}
   333 	
   334 	g_MapPos++;
   335 	if (g_MapPos >= mapCount)
   336 		g_MapPos = 0;	
   337  
   338  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   339 	SetConVarString(g_Cvar_Nextmap, mapName);
   340 }