plugins/nextmap.sp
author David Anderson <dvander@alliedmods.net>
Wed Jul 16 03:46:10 2008 +0000 (2008-07-16)
changeset 2211 be9f9063b727
parent 2206 6b946d8524d8
child 2215 bcbfee080d08
permissions -rw-r--r--
Fixed a bug where natives with no owner could be bound (crashing).
Fixed a bug where FakeNative entries were leaked.
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
ds@1952
    62
	RegAdminCmd("sm_setnextmap", Command_SetNextmap, ADMFLAG_CHANGEMAP, "sm_setnextmap <map>");
pred@2203
    63
	RegAdminCmd("sm_maphistory", Command_MapHistory, ADMFLAG_CHANGEMAP, "Shows the most recent maps played");
ds@1952
    64
	RegConsoleCmd("listmaps", Command_List);
ds@1952
    65
ds@1952
    66
	// Set to the current map so OnMapStart() will know what to do
ds@1952
    67
	decl String:currentMap[64];
ds@1952
    68
	GetCurrentMap(currentMap, 64);
pred@2077
    69
	SetNextMap(currentMap);
ds@1952
    70
}
pred@2203
    71
pred@2203
    72
public OnMapStart()
pred@2203
    73
{
pred@2203
    74
	g_CurrentMapStartTime = GetTime();
pred@2203
    75
}
ds@1952
    76
 
ds@1952
    77
public OnConfigsExecuted()
ds@1952
    78
{
ds@1952
    79
	decl String:lastMap[64], String:currentMap[64];
pred@2077
    80
	GetNextMap(lastMap, sizeof(lastMap));
ds@1952
    81
	GetCurrentMap(currentMap, 64);
ds@1952
    82
	
ds@1952
    83
	// 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
    84
	// some other plugin/command), we don't want to scramble the map cycle. Or for example, admin switches to a custom map
ds@1952
    85
	// not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret
ds@1952
    86
	if (strcmp(lastMap, currentMap) == 0)
ds@1952
    87
	{
ds@1952
    88
		FindAndSetNextMap();
ds@1952
    89
	}
ds@1952
    90
}
ds@1952
    91
ds@1952
    92
public Action:Command_SetNextmap(client, args)
ds@1952
    93
{
ds@1952
    94
	if (args < 1)
ds@1952
    95
	{
ds@1952
    96
		ReplyToCommand(client, "[SM] Usage: sm_setnextmap <map>");
ds@1952
    97
		return Plugin_Handled;
ds@1952
    98
	}
ds@1952
    99
ds@1952
   100
	decl String:map[64];
ds@1952
   101
	GetCmdArg(1, map, sizeof(map));
ds@1952
   102
ds@1952
   103
	if (!IsMapValid(map))
ds@1952
   104
	{
ds@1952
   105
		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
ds@1952
   106
		return Plugin_Handled;
ds@1952
   107
	}
ds@1952
   108
pred@2077
   109
	ShowActivity(client, "%t", "Changed Next Map", map);
pred@2077
   110
	LogMessage("\"%L\" changed nextmap to \"%s\"", client, map);
ds@1952
   111
pred@2077
   112
	SetNextMap(map);
ds@1952
   113
ds@1952
   114
	return Plugin_Handled;
ds@1952
   115
}
ds@1952
   116
ds@1952
   117
public Action:Command_List(client, args) 
ds@1952
   118
{
ds@1952
   119
	PrintToConsole(client, "Map Cycle:");
ds@1952
   120
	
ds@1952
   121
	new mapCount = GetArraySize(g_MapList);
ds@1952
   122
	decl String:mapName[32];
ds@1952
   123
	for (new i = 0; i < mapCount; i++)
ds@1952
   124
	{
ds@1952
   125
		GetArrayString(g_MapList, i, mapName, sizeof(mapName));
ds@1952
   126
		PrintToConsole(client, "%s", mapName);
ds@1952
   127
	}
ds@1952
   128
 
ds@1952
   129
	return Plugin_Handled;
ds@1952
   130
}
pred@2206
   131
  
ds@1952
   132
FindAndSetNextMap()
ds@1952
   133
{
ds@1952
   134
	if (ReadMapList(g_MapList, 
ds@1952
   135
			g_MapListSerial, 
ds@1952
   136
			"mapcyclefile", 
ds@1952
   137
			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT)
ds@1952
   138
		== INVALID_HANDLE)
ds@1952
   139
	{
ds@1952
   140
		if (g_MapListSerial == -1)
ds@1952
   141
		{
ds@1952
   142
			LogError("FATAL: Cannot load map cycle. Nextmap not loaded.");
ds@1952
   143
			SetFailState("Mapcycle Not Found");
ds@1952
   144
		}
ds@1952
   145
	}
ds@1952
   146
	
ds@1952
   147
	new mapCount = GetArraySize(g_MapList);
ds@1952
   148
	decl String:mapName[32];
ds@1952
   149
	
ds@1952
   150
	if (g_MapPos == -1)
ds@1952
   151
	{
ds@1952
   152
		decl String:current[64];
ds@1952
   153
		GetCurrentMap(current, 64);
ds@1952
   154
ds@1952
   155
		for (new i = 0; i < mapCount; i++)
ds@1952
   156
		{
ds@1952
   157
			GetArrayString(g_MapList, i, mapName, sizeof(mapName));
ds@1952
   158
			if (strcmp(current, mapName, false) == 0)
ds@1952
   159
			{
ds@1952
   160
				g_MapPos = i;
ds@1952
   161
				break;
ds@1952
   162
			}
ds@1952
   163
		}
ds@1952
   164
		
ds@1952
   165
		if (g_MapPos == -1)
ds@1952
   166
			g_MapPos = 0;
ds@1952
   167
	}
ds@1952
   168
	
ds@1952
   169
	g_MapPos++;
ds@1952
   170
	if (g_MapPos >= mapCount)
ds@1952
   171
		g_MapPos = 0;	
ds@1952
   172
 
ds@1952
   173
 	GetArrayString(g_MapList, g_MapPos, mapName, sizeof(mapName));
pred@2077
   174
	SetNextMap(mapName);
ds@1952
   175
}
pred@2203
   176
pred@2203
   177
public Action:Command_MapHistory(client, args)
pred@2203
   178
{
pred@2203
   179
	new mapCount = GetMapHistorySize();
pred@2203
   180
	
pred@2203
   181
	decl String:mapName[32];
pred@2203
   182
	decl String:changeReason[100];
pred@2203
   183
	decl String:timeString[100];
pred@2203
   184
	decl String:playedTime[100];
pred@2203
   185
	new startTime;
pred@2203
   186
	
pred@2203
   187
	new lastMapStartTime = g_CurrentMapStartTime;
pred@2203
   188
	
pred@2203
   189
	PrintToConsole(client, "Map History:\n");
pred@2203
   190
	PrintToConsole(client, "Map : Started : Played Time : Reason for ending");
pred@2203
   191
	
pred@2203
   192
	GetCurrentMap(mapName, sizeof(mapName));
pred@2203
   193
	PrintToConsole(client, "%02i. %s (Current Map)", 0, mapName);
pred@2203
   194
	
pred@2203
   195
	for (new i=0; i<mapCount; i++)
pred@2203
   196
	{
pred@2203
   197
		GetMapHistory(i, mapName, sizeof(mapName), changeReason, sizeof(changeReason), startTime);
pred@2203
   198
pred@2203
   199
		FormatTimeDuration(timeString, sizeof(timeString), GetTime() - startTime);
pred@2203
   200
		FormatTimeDuration(playedTime, sizeof(playedTime), lastMapStartTime - startTime);
pred@2203
   201
		
pred@2203
   202
		PrintToConsole(client, "%02i. %s : %s ago : %s : %s", i+1, mapName, timeString, playedTime, changeReason);
pred@2203
   203
		
pred@2203
   204
		lastMapStartTime = startTime;
pred@2203
   205
	}
pred@2203
   206
}
pred@2203
   207
pred@2203
   208
FormatTimeDuration(String:buffer[], maxlen, time)
pred@2203
   209
{
pred@2203
   210
	new	days = time / 86400;
pred@2203
   211
	new	hours = (time / 3600) % 24;
pred@2203
   212
	new	minutes = (time / 60) % 60;
pred@2203
   213
	new	seconds =  time % 60;
pred@2203
   214
	
pred@2203
   215
	if (days > 0)
pred@2203
   216
	{
pred@2203
   217
		Format(buffer, maxlen, "%id %ih %im", days, hours, (seconds >= 30) ? minutes+1 : minutes);
pred@2203
   218
	}
pred@2203
   219
	else if (hours > 0)
pred@2203
   220
	{
pred@2203
   221
		Format(buffer, maxlen, "%ih %im", hours, (seconds >= 30) ? minutes+1 : minutes);		
pred@2203
   222
	}
pred@2203
   223
	else if (minutes > 0)
pred@2203
   224
	{
pred@2203
   225
		Format(buffer, maxlen, "%im", (seconds >= 30) ? minutes+1 : minutes);		
pred@2203
   226
	}
pred@2203
   227
	else
pred@2203
   228
	{
pred@2203
   229
		Format(buffer, maxlen, "%is", seconds);		
pred@2203
   230
	}
pred@2203
   231
}