plugins/nextmap.sp
author Fyren <fyrenmoo@gmail.com>
Mon Apr 13 16:39:53 2009 -0700 (2009-04-13)
changeset 2696 32a50b96b644
parent 2692 126e2d41c2b3
child 2701 d1b1494bb260
permissions -rw-r--r--
Fix OnClientConnect rejectmsg handling, improve doc (bug 3690, r=dvander)
     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 	decl String:game[128];
    58 	GetGameFolderName(game, sizeof(game));
    59 
    60 	if (StrEqual(game, "left4dead", false)
    61 			|| StrEqual(game, "dystopia", false)
    62 			|| StrEqual(game, "synergy", false))
    63 		return;
    64 
    65 	LoadTranslations("common.phrases");
    66 	LoadTranslations("nextmap.phrases");
    67 	
    68 	g_MapList = CreateArray(32);
    69 
    70 	RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
    71 	RegConsoleCmd("listmaps", Command_List);
    72 
    73 	// Set to the current map so OnMapStart() will know what to do
    74 	decl String:currentMap[64];
    75 	GetCurrentMap(currentMap, 64);
    76 	SetNextMap(currentMap);
    77 }
    78 
    79 public OnMapStart()
    80 {
    81 	g_CurrentMapStartTime = GetTime();
    82 }
    83  
    84 public OnConfigsExecuted()
    85 {
    86 	decl String:game[128];
    87 	GetGameFolderName(game, sizeof(game));
    88 
    89 	if (StrEqual(game, "left4dead", false)
    90 			|| StrEqual(game, "dystopia", false)
    91 			|| StrEqual(game, "synergy", false))
    92 		return;
    93 
    94 	decl String:lastMap[64], String:currentMap[64];
    95 	GetNextMap(lastMap, sizeof(lastMap));
    96 	GetCurrentMap(currentMap, 64);
    97 	
    98 	// 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
    99 	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
   100 	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
   101 	if (strcmp(lastMap, currentMap) == 0)
   102 	{
   103 		FindAndSetNextMap();
   104 	}
   105 }
   106 
   107 public Action:Command_List(client, args) 
   108 {
   109 	PrintToConsole(client, "Map Cycle:");
   110 	
   111 	new mapCount = GetArraySize(g_MapList);
   112 	decl String:mapName[32];
   113 	for (new i = 0; i < mapCount; i++)
   114 	{
   115 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   116 		PrintToConsole(client, "%s", mapName);
   117 	}
   118  
   119 	return Plugin_Handled;
   120 }
   121   
   122 FindAndSetNextMap()
   123 {
   124 	if (ReadMapList(g_MapList, 
   125 			g_MapListSerial, 
   126 			"mapcyclefile", 
   127 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   128 		== INVALID_HANDLE)
   129 	{
   130 		if (g_MapListSerial == -1)
   131 		{
   132 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   133 			SetFailState("Mapcycle Not Found");
   134 		}
   135 	}
   136 	
   137 	new mapCount = GetArraySize(g_MapList);
   138 	decl String:mapName[32];
   139 	
   140 	if (g_MapPos == -1)
   141 	{
   142 		decl String:current[64];
   143 		GetCurrentMap(current, 64);
   144 
   145 		for (new i = 0; i < mapCount; i++)
   146 		{
   147 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   148 			if (strcmp(current, mapName, false) == 0)
   149 			{
   150 				g_MapPos = i;
   151 				break;
   152 			}
   153 		}
   154 		
   155 		if (g_MapPos == -1)
   156 			g_MapPos = 0;
   157 	}
   158 	
   159 	g_MapPos++;
   160 	if (g_MapPos >= mapCount)
   161 		g_MapPos = 0;	
   162  
   163  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   164 	SetNextMap(mapName);
   165 }
   166 
   167 public Action:Command_MapHistory(client, args)
   168 {
   169 	new mapCount = GetMapHistorySize();
   170 	
   171 	decl String:mapName[32];
   172 	decl String:changeReason[100];
   173 	decl String:timeString[100];
   174 	decl String:playedTime[100];
   175 	new startTime;
   176 	
   177 	new lastMapStartTime = g_CurrentMapStartTime;
   178 	
   179 	PrintToConsole(client, "Map History:\n");
   180 	PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
   181 	
   182 	GetCurrentMap(mapName, sizeof(mapName));
   183 	PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
   184 	
   185 	for (new i=0; i<mapCount; i++)
   186 	{
   187 		GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
   188 
   189 		FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
   190 		FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
   191 		
   192 		PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
   193 		
   194 		lastMapStartTime = startTime;
   195 	}
   196 }
   197 
   198 FormatTimeDuration(String:buffer[], maxlen, time)
   199 {
   200 	new	days = time / 86400;
   201 	new	hours = (time / 3600) % 24;
   202 	new	minutes = (time / 60) % 60;
   203 	new	seconds =  time % 60;
   204 	
   205 	if (days > 0)
   206 	{
   207 		return Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
   208 	}
   209 	else if (hours > 0)
   210 	{
   211 		return Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);		
   212 	}
   213 	else if (minutes > 0)
   214 	{
   215 		return Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);		
   216 	}
   217 	else
   218 	{
   219 		return Format(buffer, maxlen, "%is", seconds);		
   220 	}
   221 }