plugins/nextmap.sp
author Downtown1 <downtown1@gmail.com>
Mon Aug 24 08:55:34 2009 +1200 (2009-08-24)
changeset 2775 8a4cfe1215a7
parent 2701 d1b1494bb260
child 2839 e53806a99a15
permissions -rw-r--r--
Added sm_resetcvar command to basecommands (bug 3636, r=pred)
     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 APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
    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 	{
    64 		strcopy(error, err_max, "Nextmap is incompatible with this game");
    65 		return APLRes_SilentFailure;
    66 	}
    67 
    68 	return APLRes_Success;
    69 }
    70 
    71 
    72 public OnPluginStart()
    73 {
    74 
    75 	LoadTranslations("common.phrases");
    76 	LoadTranslations("nextmap.phrases");
    77 	
    78 	g_MapList = CreateArray(32);
    79 
    80 	RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
    81 	RegConsoleCmd("listmaps", Command_List);
    82 
    83 	// Set to the current map so OnMapStart() will know what to do
    84 	decl String:currentMap[64];
    85 	GetCurrentMap(currentMap, 64);
    86 	SetNextMap(currentMap);
    87 }
    88 
    89 public OnMapStart()
    90 {
    91 	g_CurrentMapStartTime = GetTime();
    92 }
    93  
    94 public OnConfigsExecuted()
    95 {
    96 	decl String:lastMap[64], String:currentMap[64];
    97 	GetNextMap(lastMap, sizeof(lastMap));
    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 		FindAndSetNextMap();
   106 	}
   107 }
   108 
   109 public Action:Command_List(client, args) 
   110 {
   111 	PrintToConsole(client, "Map Cycle:");
   112 	
   113 	new mapCount = GetArraySize(g_MapList);
   114 	decl String:mapName[32];
   115 	for (new i = 0; i < mapCount; i++)
   116 	{
   117 		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   118 		PrintToConsole(client, "%s", mapName);
   119 	}
   120  
   121 	return Plugin_Handled;
   122 }
   123   
   124 FindAndSetNextMap()
   125 {
   126 	if (ReadMapList(g_MapList, 
   127 			g_MapListSerial, 
   128 			"mapcyclefile", 
   129 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
   130 		== INVALID_HANDLE)
   131 	{
   132 		if (g_MapListSerial == -1)
   133 		{
   134 			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
   135 			SetFailState("Mapcycle Not Found");
   136 		}
   137 	}
   138 	
   139 	new mapCount = GetArraySize(g_MapList);
   140 	decl String:mapName[32];
   141 	
   142 	if (g_MapPos == -1)
   143 	{
   144 		decl String:current[64];
   145 		GetCurrentMap(current, 64);
   146 
   147 		for (new i = 0; i < mapCount; i++)
   148 		{
   149 			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
   150 			if (strcmp(current, mapName, false) == 0)
   151 			{
   152 				g_MapPos = i;
   153 				break;
   154 			}
   155 		}
   156 		
   157 		if (g_MapPos == -1)
   158 			g_MapPos = 0;
   159 	}
   160 	
   161 	g_MapPos++;
   162 	if (g_MapPos >= mapCount)
   163 		g_MapPos = 0;	
   164  
   165  	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
   166 	SetNextMap(mapName);
   167 }
   168 
   169 public Action:Command_MapHistory(client, args)
   170 {
   171 	new mapCount = GetMapHistorySize();
   172 	
   173 	decl String:mapName[32];
   174 	decl String:changeReason[100];
   175 	decl String:timeString[100];
   176 	decl String:playedTime[100];
   177 	new startTime;
   178 	
   179 	new lastMapStartTime = g_CurrentMapStartTime;
   180 	
   181 	PrintToConsole(client, "Map History:\n");
   182 	PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
   183 	
   184 	GetCurrentMap(mapName, sizeof(mapName));
   185 	PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
   186 	
   187 	for (new i=0; i<mapCount; i++)
   188 	{
   189 		GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
   190 
   191 		FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
   192 		FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
   193 		
   194 		PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
   195 		
   196 		lastMapStartTime = startTime;
   197 	}
   198 }
   199 
   200 FormatTimeDuration(String:buffer[], maxlen, time)
   201 {
   202 	new	days = time / 86400;
   203 	new	hours = (time / 3600) % 24;
   204 	new	minutes = (time / 60) % 60;
   205 	new	seconds =  time % 60;
   206 	
   207 	if (days > 0)
   208 	{
   209 		return Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
   210 	}
   211 	else if (hours > 0)
   212 	{
   213 		return Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);		
   214 	}
   215 	else if (minutes > 0)
   216 	{
   217 		return Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);		
   218 	}
   219 	else
   220 	{
   221 		return Format(buffer, maxlen, "%is", seconds);		
   222 	}
   223 }