plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Tue Sep 02 03:58:34 2008 +0000 (2008-09-02)
changeset 2245 d8518466c06c
parent 2215 bcbfee080d08
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed amb1927 - client command case sensitivity did not match the server's logic.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Nextmap Plugin
ds@1952
     5
 * Adds sm_nextmap cvar for changing map and nextmap chat trigger.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
pred@2077
    37
#include "include/nextmap.inc"
pred@2077
    38
ds@1952
    39
public Plugin:myinfo = 
ds@1952
    40
{
ds@1952
    41
	name = "Nextmap",
ds@1952
    42
	author = "AlliedModders LLC",
ds@1952
    43
	description = "Provides nextmap and sm_nextmap",
ds@1952
    44
	version = SOURCEMOD_VERSION,
ds@1952
    45
	url = "http://www.sourcemod.net/"
ds@1952
    46
};
ds@1952
    47
ds@1952
    48
 
ds@1952
    49
new g_MapPos = -1;
ds@1952
    50
new Handle:g_MapList = INVALID_HANDLE;
ds@1952
    51
new g_MapListSerial = -1;
pred@2203
    52
pred@2203
    53
new g_CurrentMapStartTime;
ds@1952
    54
 
ds@1952
    55
public OnPluginStart()
ds@1952
    56
{
ds@1952
    57
	LoadTranslations("common.phrases");
ds@1952
    58
	LoadTranslations("nextmap.phrases");
ds@1952
    59
	
ds@1952
    60
	g_MapList = CreateArray(32);
ds@1952
    61
pred@2203
    62
	RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
ds@1952
    63
	RegConsoleCmd("listmaps", Command_List);
ds@1952
    64
ds@1952
    65
	// Set to the current map so OnMapStart() will know what to do
ds@1952
    66
	decl String:currentMap[64];
ds@1952
    67
	GetCurrentMap(currentMap, 64);
pred@2077
    68
	SetNextMap(currentMap);
ds@1952
    69
}
pred@2203
    70
pred@2203
    71
public OnMapStart()
pred@2203
    72
{
pred@2203
    73
	g_CurrentMapStartTime = GetTime();
pred@2203
    74
}
ds@1952
    75
 
ds@1952
    76
public OnConfigsExecuted()
ds@1952
    77
{
ds@1952
    78
	decl String:lastMap[64], String:currentMap[64];
pred@2077
    79
	GetNextMap(lastMap, sizeof(lastMap));
ds@1952
    80
	GetCurrentMap(currentMap, 64);
ds@1952
    81
	
ds@1952
    82
	// 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
ds@1952
    83
	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
ds@1952
    84
	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
ds@1952
    85
	if (strcmp(lastMap, currentMap) == 0)
ds@1952
    86
	{
ds@1952
    87
		FindAndSetNextMap();
ds@1952
    88
	}
ds@1952
    89
}
ds@1952
    90
ds@1952
    91
public Action:Command_List(client, args) 
ds@1952
    92
{
ds@1952
    93
	PrintToConsole(client, "Map Cycle:");
ds@1952
    94
	
ds@1952
    95
	new mapCount = GetArraySize(g_MapList);
ds@1952
    96
	decl String:mapName[32];
ds@1952
    97
	for (new i = 0; i < mapCount; i++)
ds@1952
    98
	{
ds@1952
    99
		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
ds@1952
   100
		PrintToConsole(client, "%s", mapName);
ds@1952
   101
	}
ds@1952
   102
 
ds@1952
   103
	return Plugin_Handled;
ds@1952
   104
}
pred@2206
   105
  
ds@1952
   106
FindAndSetNextMap()
ds@1952
   107
{
ds@1952
   108
	if (ReadMapList(g_MapList, 
ds@1952
   109
			g_MapListSerial, 
ds@1952
   110
			"mapcyclefile", 
ds@1952
   111
			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
ds@1952
   112
		== INVALID_HANDLE)
ds@1952
   113
	{
ds@1952
   114
		if (g_MapListSerial == -1)
ds@1952
   115
		{
ds@1952
   116
			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
ds@1952
   117
			SetFailState("Mapcycle Not Found");
ds@1952
   118
		}
ds@1952
   119
	}
ds@1952
   120
	
ds@1952
   121
	new mapCount = GetArraySize(g_MapList);
ds@1952
   122
	decl String:mapName[32];
ds@1952
   123
	
ds@1952
   124
	if (g_MapPos == -1)
ds@1952
   125
	{
ds@1952
   126
		decl String:current[64];
ds@1952
   127
		GetCurrentMap(current, 64);
ds@1952
   128
ds@1952
   129
		for (new i = 0; i < mapCount; i++)
ds@1952
   130
		{
ds@1952
   131
			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
ds@1952
   132
			if (strcmp(current, mapName, false) == 0)
ds@1952
   133
			{
ds@1952
   134
				g_MapPos = i;
ds@1952
   135
				break;
ds@1952
   136
			}
ds@1952
   137
		}
ds@1952
   138
		
ds@1952
   139
		if (g_MapPos == -1)
ds@1952
   140
			g_MapPos = 0;
ds@1952
   141
	}
ds@1952
   142
	
ds@1952
   143
	g_MapPos++;
ds@1952
   144
	if (g_MapPos >= mapCount)
ds@1952
   145
		g_MapPos = 0;	
ds@1952
   146
 
ds@1952
   147
 	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
pred@2077
   148
	SetNextMap(mapName);
ds@1952
   149
}
pred@2203
   150
pred@2203
   151
public Action:Command_MapHistory(client, args)
pred@2203
   152
{
pred@2203
   153
	new mapCount = GetMapHistorySize();
pred@2203
   154
	
pred@2203
   155
	decl String:mapName[32];
pred@2203
   156
	decl String:changeReason[100];
pred@2203
   157
	decl String:timeString[100];
pred@2203
   158
	decl String:playedTime[100];
pred@2203
   159
	new startTime;
pred@2203
   160
	
pred@2203
   161
	new lastMapStartTime = g_CurrentMapStartTime;
pred@2203
   162
	
pred@2203
   163
	PrintToConsole(client, "Map History:\n");
pred@2203
   164
	PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
pred@2203
   165
	
pred@2203
   166
	GetCurrentMap(mapName, sizeof(mapName));
pred@2203
   167
	PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
pred@2203
   168
	
pred@2203
   169
	for (new i=0; i<mapCount; i++)
pred@2203
   170
	{
pred@2203
   171
		GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
pred@2203
   172
pred@2203
   173
		FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
pred@2203
   174
		FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
pred@2203
   175
		
pred@2203
   176
		PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
pred@2203
   177
		
pred@2203
   178
		lastMapStartTime = startTime;
pred@2203
   179
	}
pred@2203
   180
}
pred@2203
   181
pred@2203
   182
FormatTimeDuration(String:buffer[], maxlen, time)
pred@2203
   183
{
pred@2203
   184
	new	days = time / 86400;
pred@2203
   185
	new	hours = (time / 3600) % 24;
pred@2203
   186
	new	minutes = (time / 60) % 60;
pred@2203
   187
	new	seconds =  time % 60;
pred@2203
   188
	
pred@2203
   189
	if (days > 0)
pred@2203
   190
	{
pred@2215
   191
		return Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
pred@2203
   192
	}
pred@2203
   193
	else if (hours > 0)
pred@2203
   194
	{
pred@2215
   195
		return Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);		
pred@2203
   196
	}
pred@2203
   197
	else if (minutes > 0)
pred@2203
   198
	{
pred@2215
   199
		return Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);		
pred@2203
   200
	}
pred@2203
   201
	else
pred@2203
   202
	{
pred@2215
   203
		return Format(buffer, maxlen, "%is", seconds);		
pred@2203
   204
	}
pred@2215
   205
}