core/NextMap.cpp
author David Anderson <dvander@alliedmods.net>
Sun Feb 08 15:40:48 2009 -0500 (2009-02-08)
changeset 2571 5c995a647735
parent 2400 76b7b4575e21
child 2583 d3b2571c378b
permissions -rw-r--r--
Fixed documentation typos in dbi.inc (bug 3587).
pred@2077
     1
/**
pred@2077
     2
 * vim: set ts=4 :
pred@2077
     3
 * =============================================================================
pred@2077
     4
 * SourceMod
pred@2077
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
pred@2077
     6
 * =============================================================================
pred@2077
     7
 *
pred@2077
     8
 * This program is free software; you can redistribute it and/or modify it under
pred@2077
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
pred@2077
    10
 * Free Software Foundation.
pred@2077
    11
 * 
pred@2077
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
pred@2077
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
pred@2077
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
pred@2077
    15
 * details.
pred@2077
    16
 *
pred@2077
    17
 * You should have received a copy of the GNU General Public License along with
pred@2077
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
pred@2077
    19
 *
pred@2077
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
pred@2077
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
pred@2077
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
pred@2077
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
pred@2077
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
pred@2077
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
pred@2077
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
pred@2077
    27
 * or <http://www.sourcemod.net/license.php>.
pred@2077
    28
 *
pred@2077
    29
 * Version: $Id$
pred@2077
    30
 */
pred@2077
    31
pred@2077
    32
#include "NextMap.h"
pred@2077
    33
#include "Logger.h"
pred@2077
    34
#include "sourcemm_api.h"
pred@2077
    35
#include "sm_stringutil.h"
pred@2203
    36
#include "sourcehook.h"
pred@2203
    37
#include "sm_srvcmds.h"
pred@2077
    38
pred@2077
    39
NextMapManager g_NextMap;
pred@2077
    40
pred@2077
    41
SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
pred@2077
    42
ds@2400
    43
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2203
    44
SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
pred@2203
    45
#else
pred@2203
    46
extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
pred@2203
    47
extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
pred@2203
    48
#endif
pred@2203
    49
pred@2203
    50
ConCommand *changeLevelCmd = NULL;
pred@2203
    51
pred@2077
    52
ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
pred@2077
    53
pred@2203
    54
bool g_forcedChange = false;
pred@2203
    55
pred@2077
    56
void NextMapManager::OnSourceModAllInitialized_Post()
pred@2077
    57
{
ds@2400
    58
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2077
    59
	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
pred@2077
    60
#else
pred@2077
    61
	SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
pred@2077
    62
#endif
pred@2203
    63
ds@2400
    64
	ConCommand *pCmd = FindCommand("changelevel");
pred@2203
    65
	if (pCmd != NULL)
pred@2203
    66
	{
pred@2203
    67
		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
pred@2203
    68
		changeLevelCmd = pCmd;
pred@2203
    69
	}
pred@2077
    70
}
pred@2077
    71
pred@2077
    72
void NextMapManager::OnSourceModShutdown()
pred@2077
    73
{
ds@2400
    74
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2077
    75
	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
pred@2077
    76
#else
pred@2077
    77
	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
pred@2077
    78
#endif
pred@2203
    79
pred@2203
    80
	if (changeLevelCmd != NULL)
pred@2203
    81
	{
pred@2203
    82
		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
pred@2203
    83
	}
pred@2203
    84
pred@2203
    85
	SourceHook::List<MapChangeData *>::iterator iter;
pred@2203
    86
	iter = m_mapHistory.begin();
pred@2203
    87
pred@2203
    88
	while (iter != m_mapHistory.end())
pred@2203
    89
	{
pred@2203
    90
		delete (MapChangeData *)*iter;
pred@2203
    91
		iter = m_mapHistory.erase(iter);
pred@2203
    92
	}
pred@2077
    93
}
pred@2077
    94
pred@2077
    95
const char *NextMapManager::GetNextMap()
pred@2077
    96
{
pred@2077
    97
	return sm_nextmap.GetString();
pred@2077
    98
}
pred@2077
    99
pred@2077
   100
bool NextMapManager::SetNextMap(const char *map)
pred@2077
   101
{
pred@2077
   102
	if (!engine->IsMapValid(map))
pred@2077
   103
	{
pred@2077
   104
		return false;
pred@2077
   105
	}
pred@2077
   106
pred@2077
   107
	sm_nextmap.SetValue(map);
pred@2077
   108
pred@2077
   109
	return true;
pred@2077
   110
}
pred@2077
   111
pred@2077
   112
void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
pred@2077
   113
{
pred@2203
   114
	if (g_forcedChange)
pred@2203
   115
	{
pred@2203
   116
		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
pred@2203
   117
		RETURN_META(MRES_IGNORED);
pred@2203
   118
	}
pred@2203
   119
pred@2077
   120
	const char *newmap = sm_nextmap.GetString();
pred@2077
   121
pred@2077
   122
	if (newmap[0] == 0 || !engine->IsMapValid(newmap))
pred@2077
   123
	{
pred@2077
   124
		RETURN_META(MRES_IGNORED);
pred@2077
   125
	}
pred@2077
   126
pred@2077
   127
	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
pred@2077
   128
pred@2203
   129
	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
pred@2203
   130
	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
pred@2203
   131
pred@2077
   132
	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
pred@2077
   133
}
pred@2203
   134
pred@2203
   135
void NextMapManager::OnSourceModLevelChange( const char *mapName )
pred@2203
   136
{
pred@2203
   137
	/* Skip the first 'mapchange' when the server starts up */
pred@2203
   138
	if (m_tempChangeInfo.startTime != 0)
pred@2203
   139
	{
pred@2203
   140
		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
pred@2203
   141
		{
pred@2203
   142
			/* The map change was as we expected */
pred@2203
   143
			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
pred@2203
   144
		}
pred@2203
   145
		else
pred@2203
   146
		{
pred@2203
   147
			/* Something intercepted the mapchange */
pred@2203
   148
			char newReason[255];
pred@2203
   149
			UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
pred@2203
   150
			m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
pred@2203
   151
		}
pred@2203
   152
pred@2203
   153
		/* TODO: Should this be customizable? */
pred@2203
   154
		if (m_mapHistory.size() > 20)
pred@2203
   155
		{
pred@2203
   156
			SourceHook::List<MapChangeData *>::iterator iter;
pred@2203
   157
			iter = m_mapHistory.begin();
pred@2203
   158
pred@2203
   159
			delete (MapChangeData *)*iter;
pred@2203
   160
pred@2203
   161
			m_mapHistory.erase(iter);
pred@2203
   162
		}
pred@2203
   163
	}
pred@2203
   164
pred@2203
   165
	m_tempChangeInfo.m_mapName[0] ='\0';
pred@2203
   166
	m_tempChangeInfo.m_changeReason[0] = '\0';
pred@2203
   167
	m_tempChangeInfo.startTime = time(NULL);
pred@2203
   168
	UTIL_Format(lastMap, sizeof(lastMap), mapName);
pred@2203
   169
}
pred@2203
   170
pred@2203
   171
void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
pred@2203
   172
{
pred@2203
   173
	/* Store the mapname and reason */
pred@2203
   174
	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
pred@2203
   175
	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
pred@2203
   176
pred@2203
   177
	/* Change level and skip our hook */
pred@2203
   178
	g_forcedChange = true;
pred@2203
   179
	engine->ChangeLevel(mapName, NULL);
pred@2203
   180
	g_forcedChange = false;
pred@2203
   181
}
pred@2203
   182
pred@2203
   183
NextMapManager::NextMapManager()
pred@2203
   184
{
pred@2203
   185
	m_tempChangeInfo = MapChangeData();
pred@2203
   186
	m_mapHistory = SourceHook::List<MapChangeData *>();
pred@2203
   187
}
pred@2203
   188
ds@2400
   189
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2203
   190
void CmdChangeLevelCallback(const CCommand &command)
pred@2203
   191
{
pred@2203
   192
#else
pred@2203
   193
void CmdChangeLevelCallback()
pred@2203
   194
{
pred@2203
   195
	CCommand command;
pred@2203
   196
#endif
pred@2203
   197
pred@2203
   198
	if (command.ArgC() < 2)
pred@2203
   199
	{
pred@2203
   200
		return;
pred@2203
   201
	}
pred@2203
   202
pred@2203
   203
	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
pred@2203
   204
	{
pred@2203
   205
		UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
pred@2203
   206
		UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");
pred@2203
   207
	}
pred@2205
   208
}