plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Tue Dec 04 01:01:05 2007 +0000 (2007-12-04)
changeset 1755 769ccc109f59
parent 1750 0f73390b01be
child 1783 e6e2f440376d
permissions -rw-r--r--
added empires to the list of mods that supports ShowMenu
     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 		if (ReadMapList(g_MapList, 
   100 				g_MapListSerial, 
   101 				"mapcyclefile", 
   102 				MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   103 			== INVALID_HANDLE)
   104 		{
   105 			if (g_MapListSerial == -1)
   106 			{
   107 				LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   108 				SetFailState("Mapcycle Not Found");
   109 			}
   110 		}
   111 		
   112 		FindAndSetNextMap();
   113 	}
   114 }
   115  
   116 public OnMapEnd()
   117 {
   118 	g_IntermissionCalled = false;
   119 }
   120 
   121 public Action:Command_Say(client, args)
   122 {
   123 	decl String:text[192];
   124 	if (GetCmdArgString(text, sizeof(text)) < 1)
   125 	{
   126 		return Plugin_Continue;
   127 	}
   128 	
   129 	new startidx;
   130 	if (text[strlen(text)-1] == '"')
   131 	{
   132 		text[strlen(text)-1] = '\0';
   133 		startidx = 1;
   134 	}
   135 	
   136 	decl String:message[8];
   137 	BreakString(text[startidx], message, sizeof(message));
   138 	
   139 	if (strcmp(message, "nextmap", false) == 0)
   140 	{
   141 		decl String:map[32];
   142 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   143 		
   144 		PrintToChatAll("%t", "Next Map", map);
   145 	}
   146 	
   147 	return Plugin_Continue;	
   148 }
   149 
   150 public Action:Command_SetNextmap(client, args)
   151 {
   152 	if (args < 1)
   153 	{
   154 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   155 		return Plugin_Handled;
   156 	}
   157 
   158 	decl String:map[64];
   159 	GetCmdArg(1, map, sizeof(map));
   160 
   161 	if (!IsMapValid(map))
   162 	{
   163 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   164 		return Plugin_Handled;
   165 	}
   166 
   167 	ShowActivity(client, "%t", "Cvar changed", "sm_nextmap", map);
   168 	LogMessage("\"%L\" changed sm_nextmap to \"%s\"", client, map);
   169 
   170 	SetConVarString(g_Cvar_Nextmap, map);
   171 
   172 	return Plugin_Handled;
   173 }
   174 
   175 public Action:Command_List(client, args) 
   176 {
   177 	PrintToConsole(client, "Map Cycle:");
   178 	
   179 	new mapCount = GetArraySize(g_MapList);
   180 	decl String:mapName[32];
   181 	for (new i = 0; i < mapCount; i++)
   182 	{
   183 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   184 		PrintToConsole(client, "%s", mapName);
   185 	}
   186  
   187 	return Plugin_Handled;
   188 }
   189  
   190 public Action:UserMsg_VGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
   191 {
   192 	if (g_IntermissionCalled)
   193 	{
   194 		return Plugin_Handled;
   195 	}
   196 	
   197 	decl String:type[15];
   198 
   199 	/* If we don't get a valid string, bail out. */
   200 	if (BfReadString(bf, type, sizeof(type)) < 0)
   201 	{
   202 		return Plugin_Handled;
   203 	}
   204  
   205 	if (BfReadByte(bf) == 1 && BfReadByte(bf) == 0 && (strcmp(type, "scores", false) == 0))
   206 	{
   207 		g_IntermissionCalled = true;
   208 		
   209 		decl String:map[32];
   210 		new Float:fChatTime = GetConVarFloat(g_Cvar_Chattime);
   211 		
   212 		GetConVarString(g_Cvar_Nextmap, map, sizeof(map));
   213 		
   214 		if (!IsMapValid(map))
   215 		{
   216 			GetArrayString(g_MapList, g_MapPos, map, sizeof(map));
   217 		}
   218 		
   219 		if (fChatTime < 2.0)
   220 			SetConVarFloat(g_Cvar_Chattime, 2.0);
   221 		
   222 		new Handle:dp;
   223 		CreateDataTimer(fChatTime - 1.0, Timer_ChangeMap, dp);
   224 		WritePackString(dp, map);
   225 	}
   226 	
   227 	return Plugin_Handled;
   228 }
   229  
   230 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   231 {
   232 	new String:map[32];
   233 	
   234 	ResetPack(dp);
   235 	ReadPackString(dp, map, sizeof(map));
   236  
   237 	InsertServerCommand("changelevel \"%s\"", map);
   238 	ServerExecute();
   239 	
   240 	LogMessage("Nextmap changed map to \"%s\"", map);
   241 	
   242 	return Plugin_Stop;
   243 }
   244  
   245 FindAndSetNextMap()
   246 {
   247 	new mapCount = GetArraySize(g_MapList);
   248 	decl String:mapName[32];
   249 	
   250 	if (g_MapPos == -1)
   251 	{
   252 		decl String:current[64];
   253 		GetCurrentMap(current, 64);
   254 
   255 		for (new i = 0; i < mapCount; i++)
   256 		{
   257 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   258 			if (strcmp(current, mapName, false) == 0)
   259 			{
   260 				g_MapPos = i;
   261 				break;
   262 			}
   263 		}
   264 		
   265 		if (g_MapPos == -1)
   266 			g_MapPos = 0;
   267 	}
   268 	
   269 	g_MapPos++;
   270 	if (g_MapPos >= mapCount)
   271 		g_MapPos = 0;	
   272  
   273  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   274 	SetConVarString(g_Cvar_Nextmap, mapName);
   275 }