plugins/nextmap.sp
author Matt Woodrow <pred@alliedmods.net>
Tue Jul 15 00:24:08 2008 +0000 (2008-07-15)
changeset 2207 3a97a56ded62
parent 2206 6b946d8524d8
child 2215 bcbfee080d08
permissions -rw-r--r--
Fixed amb1810 - Clientprefs no longer blocks load.
     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 public Plugin:myinfo = 
    40 {
    41 	name = "Nextmap",
    42 	author = "AlliedModders LLC",
    43 	description = "Provides nextmap and sm_nextmap",
    44 	version = SOURCEMOD_VERSION,
    45 	url = "http://www.sourcemod.net/"
    46 };
    47 
    48  
    49 new g_MapPos = -1;
    50 new Handle:g_MapList = INVALID_HANDLE;
    51 new g_MapListSerial = -1;
    52 
    53 new g_CurrentMapStartTime;
    54  
    55 public OnPluginStart()
    56 {
    57 	LoadTranslations("common.phrases");
    58 	LoadTranslations("nextmap.phrases");
    59 	
    60 	g_MapList = CreateArray(32);
    61 
    62 	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
    63 	RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
    64 	RegConsoleCmd("listmaps", Command_List);
    65 
    66 	// Set to the current map so OnMapStart() will know what to do
    67 	decl String:currentMap[64];
    68 	GetCurrentMap(currentMap, 64);
    69 	SetNextMap(currentMap);
    70 }
    71 
    72 public OnMapStart()
    73 {
    74 	g_CurrentMapStartTime = GetTime();
    75 }
    76  
    77 public OnConfigsExecuted()
    78 {
    79 	decl String:lastMap[64], String:currentMap[64];
    80 	GetNextMap(lastMap, sizeof(lastMap));
    81 	GetCurrentMap(currentMap, 64);
    82 	
    83 	// 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
    84 	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
    85 	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
    86 	if (strcmp(lastMap, currentMap) == 0)
    87 	{
    88 		FindAndSetNextMap();
    89 	}
    90 }
    91 
    92 public Action:Command_SetNextmap(client, args)
    93 {
    94 	if (args < 1)
    95 	{
    96 		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
    97 		return Plugin_Handled;
    98 	}
    99 
   100 	decl String:map[64];
   101 	GetCmdArg(1, map, sizeof(map));
   102 
   103 	if (!IsMapValid(map))
   104 	{
   105 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   106 		return Plugin_Handled;
   107 	}
   108 
   109 	ShowActivity(client, "%t", "Changed Next Map", map);
   110 	LogMessage("\"%L\" changed nextmap to \"%s\"", client, map);
   111 
   112 	SetNextMap(map);
   113 
   114 	return Plugin_Handled;
   115 }
   116 
   117 public Action:Command_List(client, args) 
   118 {
   119 	PrintToConsole(client, "Map Cycle:");
   120 	
   121 	new mapCount = GetArraySize(g_MapList);
   122 	decl String:mapName[32];
   123 	for (new i = 0; i < mapCount; i++)
   124 	{
   125 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   126 		PrintToConsole(client, "%s", mapName);
   127 	}
   128  
   129 	return Plugin_Handled;
   130 }
   131   
   132 FindAndSetNextMap()
   133 {
   134 	if (ReadMapList(g_MapList, 
   135 			g_MapListSerial, 
   136 			"mapcyclefile", 
   137 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   138 		== INVALID_HANDLE)
   139 	{
   140 		if (g_MapListSerial == -1)
   141 		{
   142 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   143 			SetFailState("Mapcycle Not Found");
   144 		}
   145 	}
   146 	
   147 	new mapCount = GetArraySize(g_MapList);
   148 	decl String:mapName[32];
   149 	
   150 	if (g_MapPos == -1)
   151 	{
   152 		decl String:current[64];
   153 		GetCurrentMap(current, 64);
   154 
   155 		for (new i = 0; i < mapCount; i++)
   156 		{
   157 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   158 			if (strcmp(current, mapName, false) == 0)
   159 			{
   160 				g_MapPos = i;
   161 				break;
   162 			}
   163 		}
   164 		
   165 		if (g_MapPos == -1)
   166 			g_MapPos = 0;
   167 	}
   168 	
   169 	g_MapPos++;
   170 	if (g_MapPos >= mapCount)
   171 		g_MapPos = 0;	
   172  
   173  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   174 	SetNextMap(mapName);
   175 }
   176 
   177 public Action:Command_MapHistory(client, args)
   178 {
   179 	new mapCount = GetMapHistorySize();
   180 	
   181 	decl String:mapName[32];
   182 	decl String:changeReason[100];
   183 	decl String:timeString[100];
   184 	decl String:playedTime[100];
   185 	new startTime;
   186 	
   187 	new lastMapStartTime = g_CurrentMapStartTime;
   188 	
   189 	PrintToConsole(client, "Map History:\n");
   190 	PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
   191 	
   192 	GetCurrentMap(mapName, sizeof(mapName));
   193 	PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
   194 	
   195 	for (new i=0; i<mapCount; i++)
   196 	{
   197 		GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
   198 
   199 		FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
   200 		FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
   201 		
   202 		PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
   203 		
   204 		lastMapStartTime = startTime;
   205 	}
   206 }
   207 
   208 FormatTimeDuration(String:buffer[], maxlen, time)
   209 {
   210 	new	days = time / 86400;
   211 	new	hours = (time / 3600) % 24;
   212 	new	minutes = (time / 60) % 60;
   213 	new	seconds =  time % 60;
   214 	
   215 	if (days > 0)
   216 	{
   217 		Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
   218 	}
   219 	else if (hours > 0)
   220 	{
   221 		Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);		
   222 	}
   223 	else if (minutes > 0)
   224 	{
   225 		Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);		
   226 	}
   227 	else
   228 	{
   229 		Format(buffer, maxlen, "%is", seconds);		
   230 	}
   231 }