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