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