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