extensions/tf2/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Thu Apr 10 04:49:17 2008 +0000 (2008-04-10)
changeset 1979 d36fac1a8203
parent 1963 97a66409edac
child 2069 80d56f1d58fe
permissions -rw-r--r--
Added Critical Hit hook to TF2 extension
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
pred@1963
     4
 * SourceMod Team Fortress 2 Extension
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "extension.h"
pred@1963
    33
#include "util.h"
ds@1952
    34
#include "RegNatives.h"
ds@1952
    35
#include "iplayerinfo.h"
pred@1963
    36
#include "sm_trie_tpl.h"
pred@1979
    37
#include "criticals.h"
ds@1952
    38
ds@1952
    39
/**
ds@1952
    40
 * @file extension.cpp
ds@1952
    41
 * @brief Implement extension code here.
ds@1952
    42
 */
ds@1952
    43
ds@1952
    44
ds@1952
    45
TF2Tools g_TF2Tools;
ds@1952
    46
IGameConfig *g_pGameConf = NULL;
ds@1952
    47
ds@1952
    48
IBinTools *g_pBinTools = NULL;
ds@1952
    49
ds@1952
    50
SMEXT_LINK(&g_TF2Tools);
ds@1952
    51
pred@1979
    52
ICvar *icvar = NULL;
pred@1979
    53
pred@1963
    54
CGlobalVars *gpGlobals = NULL;
pred@1963
    55
pred@1963
    56
sm_sendprop_info_t *playerSharedOffset;
ds@1952
    57
ds@1952
    58
extern sp_nativeinfo_t g_TFNatives[];
ds@1952
    59
pred@1963
    60
int g_resourceEntity;
pred@1963
    61
pred@1963
    62
SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
pred@1963
    63
ds@1952
    64
bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
    65
{
pred@1963
    66
	ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
pred@1963
    67
	if (sc == NULL)
pred@1963
    68
	{
pred@1963
    69
		snprintf(error, maxlength, "Could not find CTFPlayer server class");
pred@1963
    70
pred@1963
    71
		return false;
pred@1963
    72
	}
pred@1963
    73
pred@1963
    74
	playerSharedOffset = new sm_sendprop_info_t;
pred@1963
    75
pred@1963
    76
	if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
pred@1963
    77
	{
pred@1963
    78
		snprintf(error, maxlength, "Could not find DT_TFPlayerShared data table");
pred@1963
    79
pred@1963
    80
		return false;
pred@1963
    81
	}
pred@1963
    82
ds@1952
    83
	sharesys->AddDependency(myself, "bintools.ext", true, true);
ds@1952
    84
ds@1952
    85
	char conf_error[255];
ds@1952
    86
	if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
ds@1952
    87
	{
ds@1952
    88
		if (error)
ds@1952
    89
		{
ds@1952
    90
			snprintf(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
ds@1952
    91
		}
ds@1952
    92
		return false;
ds@1952
    93
	}
ds@1952
    94
ds@1952
    95
	sharesys->AddNatives(myself, g_TFNatives);
ds@1952
    96
	sharesys->RegisterLibrary(myself, "tf2");
ds@1952
    97
ds@1952
    98
	playerhelpers->RegisterCommandTargetProcessor(this);
ds@1952
    99
pred@1979
   100
	spengine = g_pSM->GetScriptingEngine();
pred@1979
   101
pred@1979
   102
	g_pCVar = icvar;
pred@1979
   103
ds@1952
   104
	return true;
ds@1952
   105
}
ds@1952
   106
ds@1952
   107
bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
   108
{
ds@1952
   109
	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
   110
pred@1963
   111
	gpGlobals = ismm->GetCGlobals();
pred@1963
   112
pred@1963
   113
	SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963
   114
pred@1979
   115
	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
pred@1979
   116
pred@1979
   117
	GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
pred@1979
   118
ds@1952
   119
	return true;
ds@1952
   120
}
ds@1952
   121
ds@1952
   122
void TF2Tools::SDK_OnUnload()
ds@1952
   123
{
pred@1963
   124
	SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963
   125
ds@1952
   126
	g_RegNatives.UnregisterAll();
ds@1952
   127
	gameconfs->CloseGameConfigFile(g_pGameConf);
pred@1963
   128
	playerhelpers->UnregisterCommandTargetProcessor(this);
ds@1952
   129
}
ds@1952
   130
ds@1952
   131
void TF2Tools::SDK_OnAllLoaded()
ds@1952
   132
{
ds@1952
   133
	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
pred@1979
   134
pred@1979
   135
	g_CriticalHitManager.Init();
ds@1952
   136
}
ds@1952
   137
pred@1979
   138
bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
pred@1979
   139
{
pred@1979
   140
	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
pred@1979
   141
}
pred@1979
   142
pred@1979
   143
ds@1952
   144
bool TF2Tools::QueryRunning(char *error, size_t maxlength)
ds@1952
   145
{
ds@1952
   146
	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   147
ds@1952
   148
	return true;
ds@1952
   149
}
ds@1952
   150
ds@1952
   151
bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
ds@1952
   152
{
ds@1952
   153
	if (pInterface == g_pBinTools)
ds@1952
   154
	{
ds@1952
   155
		return false;
ds@1952
   156
	}
ds@1952
   157
ds@1952
   158
	return IExtensionInterface::QueryInterfaceDrop(pInterface);
ds@1952
   159
}
ds@1952
   160
ds@1952
   161
void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
ds@1952
   162
{
ds@1952
   163
	g_RegNatives.UnregisterAll();
ds@1952
   164
}
ds@1952
   165
pred@1963
   166
void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
ds@1952
   167
{
pred@1963
   168
	g_resourceEntity = FindResourceEntity();
pred@1963
   169
}
ds@1952
   170
ds@1952
   171
ds@1952
   172
bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952
   173
{
ds@1952
   174
	int max_clients;
ds@1952
   175
	IPlayerInfo *pInfo;
ds@1952
   176
	unsigned int team_index = 0;
ds@1952
   177
	IGamePlayer *pPlayer, *pAdmin;
ds@1952
   178
ds@1952
   179
	if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
ds@1952
   180
	{
ds@1952
   181
		return false;
ds@1952
   182
	}
ds@1952
   183
ds@1952
   184
	if (info->admin)
ds@1952
   185
	{
ds@1952
   186
		if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
ds@1952
   187
		{
ds@1952
   188
			return false;
ds@1952
   189
		}
ds@1952
   190
		if (!pAdmin->IsInGame())
ds@1952
   191
		{
ds@1952
   192
			return false;
ds@1952
   193
		}
ds@1952
   194
	}
ds@1952
   195
	else
ds@1952
   196
	{
ds@1952
   197
		pAdmin = NULL;
ds@1952
   198
	}
ds@1952
   199
ds@1952
   200
	if (strcmp(info->pattern, "@red") == 0 )
ds@1952
   201
	{
ds@1952
   202
		team_index = 2;
ds@1952
   203
	}
ds@1952
   204
	else if (strcmp(info->pattern, "@blue") == 0)
ds@1952
   205
	{
ds@1952
   206
		team_index = 3;
ds@1952
   207
	}
ds@1952
   208
	else
ds@1952
   209
	{
ds@1952
   210
		return false;
ds@1952
   211
	}
ds@1952
   212
ds@1952
   213
	info->num_targets = 0;
ds@1952
   214
ds@1952
   215
	max_clients = playerhelpers->GetMaxClients();
ds@1952
   216
	for (int i = 1; 
ds@1952
   217
		 i <= max_clients && (cell_t)info->num_targets < info->max_targets; 
ds@1952
   218
		 i++)
ds@1952
   219
	{
ds@1952
   220
		if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
ds@1952
   221
		{
ds@1952
   222
			continue;
ds@1952
   223
		}
ds@1952
   224
		if (!pPlayer->IsInGame())
ds@1952
   225
		{
ds@1952
   226
			continue;
ds@1952
   227
		}
ds@1952
   228
		if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
ds@1952
   229
		{
ds@1952
   230
			continue;
ds@1952
   231
		}
ds@1952
   232
		if (pInfo->GetTeamIndex() != (int)team_index)
ds@1952
   233
		{
ds@1952
   234
			continue;
ds@1952
   235
		}
ds@1952
   236
		if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags) 
ds@1952
   237
			!= COMMAND_TARGET_VALID)
ds@1952
   238
		{
ds@1952
   239
			continue;
ds@1952
   240
		}
ds@1952
   241
		info->targets[info->num_targets] = i;
ds@1952
   242
		info->num_targets++;
ds@1952
   243
	}
ds@1952
   244
ds@1952
   245
	if (info->num_targets == 0)
ds@1952
   246
	{
ds@1952
   247
		info->reason = COMMAND_TARGET_EMPTY_FILTER;
ds@1952
   248
	}
ds@1952
   249
	else
ds@1952
   250
	{
ds@1952
   251
		info->reason = COMMAND_TARGET_VALID;
ds@1952
   252
	}
ds@1952
   253
ds@1952
   254
	info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
   255
	if (team_index == 2)
ds@1952
   256
	{
ds@1952
   257
		UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
ds@1952
   258
	}
ds@1952
   259
	else if (team_index == 3)
ds@1952
   260
	{
ds@1952
   261
		UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
ds@1952
   262
	}
ds@1952
   263
ds@1952
   264
	return true;
ds@1952
   265
}
ds@1952
   266
pred@1963
   267
int FindResourceEntity()
pred@1963
   268
{
pred@1963
   269
	return FindEntityByNetClass(-1, "CTFPlayerResource");
pred@1963
   270
}
pred@1963
   271
pred@1963
   272
pred@1963
   273
int FindEntityByNetClass(int start, const char *classname)
pred@1963
   274
{
pred@1963
   275
	edict_t *current;
pred@1963
   276
pred@1963
   277
	for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
pred@1963
   278
	{
pred@1963
   279
		current = engine->PEntityOfEntIndex(i);
pred@1963
   280
		if (current == NULL)
pred@1963
   281
		{
pred@1963
   282
			continue;
pred@1963
   283
		}
pred@1963
   284
pred@1963
   285
		IServerNetworkable *network = current->GetNetworkable();
pred@1963
   286
pred@1963
   287
		if (network == NULL)
pred@1963
   288
		{
pred@1963
   289
			continue;
pred@1963
   290
		}
pred@1963
   291
pred@1963
   292
		ServerClass *sClass = network->GetServerClass();
pred@1963
   293
		const char *name = sClass->GetName();
pred@1963
   294
		
pred@1963
   295
pred@1963
   296
		if (strcmp(name, classname) == 0)
pred@1963
   297
		{
pred@1963
   298
			return i;
pred@1963
   299
		}
pred@1963
   300
	}
pred@1963
   301
pred@1963
   302
	return -1;
pred@1963
   303
}
pred@1963
   304
pred@1963
   305
TFClassType ClassnameToType(const char *classname)
pred@1963
   306
{
pred@1963
   307
	static KTrie<TFClassType> trie;
pred@1963
   308
	static bool filled = false;
pred@1963
   309
	
pred@1963
   310
	if (!filled)
pred@1963
   311
	{
pred@1963
   312
		trie.insert("scout", TFClass_Scout);
pred@1963
   313
		trie.insert("sniper", TFClass_Sniper);
pred@1963
   314
		trie.insert("soldier", TFClass_Soldier);
pred@1963
   315
		trie.insert("demoman", TFClass_DemoMan);
pred@1963
   316
		trie.insert("demo", TFClass_DemoMan);
pred@1963
   317
		trie.insert("medic", TFClass_Medic);
pred@1963
   318
		trie.insert("heavy", TFClass_Heavy);
pred@1963
   319
		trie.insert("hwg", TFClass_Heavy);
pred@1963
   320
		trie.insert("pyro", TFClass_Pyro);
pred@1963
   321
		trie.insert("spy", TFClass_Spy);
pred@1963
   322
		trie.insert("engineer", TFClass_Engineer);
pred@1963
   323
		trie.insert("engy", TFClass_Engineer);
pred@1963
   324
pred@1963
   325
		filled = true;	
pred@1963
   326
	}
pred@1963
   327
	
pred@1963
   328
	TFClassType *value;
pred@1963
   329
pred@1963
   330
	if (!(value = trie.retrieve(classname)))
pred@1963
   331
	{
pred@1963
   332
		return TFClass_Unknown;	
pred@1963
   333
	}
pred@1963
   334
pred@1963
   335
	return *value;
pred@1963
   336
}
pred@1963
   337
pred@1963
   338
pred@1963
   339
pred@1963
   340
/**
pred@1963
   341
 * A picture of a blue crab given to me as a gift and stored here for safe keeping
pred@1963
   342
 *
pred@1963
   343
 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg
pred@1963
   344
 */
pred@1963
   345