plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 05:14:32 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2190 880e81f30b5e
parent 2168 c24a6fa957db
permissions -rw-r--r--
GetContext() now returns 'this' as a hack (shouldn't+can't be accessed anyway)
     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-2008 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 #include "include/nextmap.inc"
    38 
    39  
    40 public Plugin:myinfo = 
    41 {
    42 	name = "Nextmap",
    43 	author = "AlliedModders LLC",
    44 	description = "Provides nextmap and sm_nextmap",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49  
    50 new g_MapPos = -1;
    51 new Handle:g_MapList = INVALID_HANDLE;
    52 new g_MapListSerial = -1;
    53  
    54 public OnPluginStart()
    55 {
    56 	LoadTranslations("common.phrases");
    57 	LoadTranslations("nextmap.phrases");
    58 	
    59 	g_MapList = CreateArray(32);
    60 	
    61 	RegConsoleCmd("say", Command_Say);
    62 	RegConsoleCmd("say_team", Command_Say);
    63 	
    64 	RegConsoleCmd("nextmap", Command_Nextmap);
    65 
    66 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    67 	RegConsoleCmd("listmaps", Command_List);
    68 
    69 	// Set to the current map so OnMapStart() will know what to do
    70 	decl String:currentMap[64];
    71 	GetCurrentMap(currentMap, 64);
    72 	SetNextMap(currentMap);
    73 }
    74  
    75 public OnConfigsExecuted()
    76 {
    77 	decl String:lastMap[64], String:currentMap[64];
    78 	GetNextMap(lastMap, sizeof(lastMap));
    79 	GetCurrentMap(currentMap, 64);
    80 	
    81 	// 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
    82 	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
    83 	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
    84 	if (strcmp(lastMap, currentMap) == 0)
    85 	{
    86 		FindAndSetNextMap();
    87 	}
    88 }
    89 
    90 public Action:Command_Say(client, args)
    91 {
    92 	decl String:text[192];
    93 	if (GetCmdArgString(text, sizeof(text)) < 1)
    94 	{
    95 		return Plugin_Continue;
    96 	}
    97 	
    98 	new startidx;
    99 	if (text[strlen(text)-1] == '"')
   100 	{
   101 		text[strlen(text)-1] = '\0';
   102 		startidx = 1;
   103 	}
   104 	
   105 	decl String:message[8];
   106 	BreakString(text[startidx], message, sizeof(message));
   107 	
   108 	if (strcmp(message, "nextmap", false) == 0)
   109 	{
   110 		decl String:map[32];
   111 		GetNextMap(map, sizeof(map));
   112 		
   113 		PrintToChat(client, "[SM] %t", "Next Map", map);
   114 	}
   115 	
   116 	return Plugin_Continue;	
   117 }
   118 
   119 public Action:Command_SetNextmap(client, args)
   120 {
   121 	if (args < 1)
   122 	{
   123 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
   124 		return Plugin_Handled;
   125 	}
   126 
   127 	decl String:map[64];
   128 	GetCmdArg(1, map, sizeof(map));
   129 
   130 	if (!IsMapValid(map))
   131 	{
   132 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   133 		return Plugin_Handled;
   134 	}
   135 
   136 	ShowActivity(client, "%t", "Changed Next Map", map);
   137 	LogMessage("\"%L\" changed nextmap to \"%s\"", client, map);
   138 
   139 	SetNextMap(map);
   140 
   141 	return Plugin_Handled;
   142 }
   143 
   144 public Action:Command_List(client, args) 
   145 {
   146 	PrintToConsole(client, "Map Cycle:");
   147 	
   148 	new mapCount = GetArraySize(g_MapList);
   149 	decl String:mapName[32];
   150 	for (new i = 0; i < mapCount; i++)
   151 	{
   152 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   153 		PrintToConsole(client, "%s", mapName);
   154 	}
   155  
   156 	return Plugin_Handled;
   157 }
   158  
   159 public Action:Command_Nextmap(client, args)
   160 {
   161 	decl String:map[64];
   162 	
   163 	GetNextMap(map, sizeof(map));
   164 	
   165 	ReplyToCommand(client, "[SM] %t", "Next Map", map);
   166 	
   167 	return Plugin_Handled;
   168 }
   169  
   170 FindAndSetNextMap()
   171 {
   172 	if (ReadMapList(g_MapList, 
   173 			g_MapListSerial, 
   174 			"mapcyclefile", 
   175 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   176 		== INVALID_HANDLE)
   177 	{
   178 		if (g_MapListSerial == -1)
   179 		{
   180 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   181 			SetFailState("Mapcycle Not Found");
   182 		}
   183 	}
   184 	
   185 	new mapCount = GetArraySize(g_MapList);
   186 	decl String:mapName[32];
   187 	
   188 	if (g_MapPos == -1)
   189 	{
   190 		decl String:current[64];
   191 		GetCurrentMap(current, 64);
   192 
   193 		for (new i = 0; i < mapCount; i++)
   194 		{
   195 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   196 			if (strcmp(current, mapName, false) == 0)
   197 			{
   198 				g_MapPos = i;
   199 				break;
   200 			}
   201 		}
   202 		
   203 		if (g_MapPos == -1)
   204 			g_MapPos = 0;
   205 	}
   206 	
   207 	g_MapPos++;
   208 	if (g_MapPos >= mapCount)
   209 		g_MapPos = 0;	
   210  
   211  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   212 	SetNextMap(mapName);
   213 }