ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
pred@1963: * SourceMod Team Fortress 2 Extension
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "extension.h"
pred@1963: #include "util.h"
ds@1952: #include "RegNatives.h"
ds@1952: #include "iplayerinfo.h"
pred@1963: #include "sm_trie_tpl.h"
ds@1952:
ds@1952: /**
ds@1952: * @file extension.cpp
ds@1952: * @brief Implement extension code here.
ds@1952: */
ds@1952:
ds@1952:
ds@1952: TF2Tools g_TF2Tools;
ds@1952: IGameConfig *g_pGameConf = NULL;
ds@1952:
ds@1952: IBinTools *g_pBinTools = NULL;
ds@1952:
ds@1952: SMEXT_LINK(&g_TF2Tools);
ds@1952:
pred@1963: CGlobalVars *gpGlobals = NULL;
pred@1963:
pred@1963: sm_sendprop_info_t *playerSharedOffset;
ds@1952:
ds@1952: extern sp_nativeinfo_t g_TFNatives[];
ds@1952:
pred@1963: int g_resourceEntity;
pred@1963:
pred@1963: SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
pred@1963:
ds@1952: bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952: {
pred@1963: ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
pred@1963: if (sc == NULL)
pred@1963: {
pred@1963: snprintf(error, maxlength, "Could not find CTFPlayer server class");
pred@1963:
pred@1963: return false;
pred@1963: }
pred@1963:
pred@1963: playerSharedOffset = new sm_sendprop_info_t;
pred@1963:
pred@1963: if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
pred@1963: {
pred@1963: snprintf(error, maxlength, "Could not find DT_TFPlayerShared data table");
pred@1963:
pred@1963: return false;
pred@1963: }
pred@1963:
ds@1952: sharesys->AddDependency(myself, "bintools.ext", true, true);
ds@1952:
ds@1952: char conf_error[255];
ds@1952: if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
ds@1952: {
ds@1952: if (error)
ds@1952: {
ds@1952: snprintf(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
ds@1952: }
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: sharesys->AddNatives(myself, g_TFNatives);
ds@1952: sharesys->RegisterLibrary(myself, "tf2");
ds@1952:
ds@1952: playerhelpers->RegisterCommandTargetProcessor(this);
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952: {
ds@1952: GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952:
pred@1963: gpGlobals = ismm->GetCGlobals();
pred@1963:
pred@1963: SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: void TF2Tools::SDK_OnUnload()
ds@1952: {
pred@1963: SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963:
ds@1952: g_RegNatives.UnregisterAll();
ds@1952: gameconfs->CloseGameConfigFile(g_pGameConf);
pred@1963: playerhelpers->UnregisterCommandTargetProcessor(this);
ds@1952: }
ds@1952:
ds@1952: void TF2Tools::SDK_OnAllLoaded()
ds@1952: {
ds@1952: SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
ds@1952: }
ds@1952:
ds@1952: bool TF2Tools::QueryRunning(char *error, size_t maxlength)
ds@1952: {
ds@1952: SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
ds@1952: {
ds@1952: if (pInterface == g_pBinTools)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: return IExtensionInterface::QueryInterfaceDrop(pInterface);
ds@1952: }
ds@1952:
ds@1952: void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
ds@1952: {
ds@1952: g_RegNatives.UnregisterAll();
ds@1952: }
ds@1952:
pred@1963: void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
ds@1952: {
pred@1963: g_resourceEntity = FindResourceEntity();
pred@1963: }
ds@1952:
ds@1952:
ds@1952: bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952: {
ds@1952: int max_clients;
ds@1952: IPlayerInfo *pInfo;
ds@1952: unsigned int team_index = 0;
ds@1952: IGamePlayer *pPlayer, *pAdmin;
ds@1952:
ds@1952: if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: if (info->admin)
ds@1952: {
ds@1952: if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952: if (!pAdmin->IsInGame())
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: pAdmin = NULL;
ds@1952: }
ds@1952:
ds@1952: if (strcmp(info->pattern, "@red") == 0 )
ds@1952: {
ds@1952: team_index = 2;
ds@1952: }
ds@1952: else if (strcmp(info->pattern, "@blue") == 0)
ds@1952: {
ds@1952: team_index = 3;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: info->num_targets = 0;
ds@1952:
ds@1952: max_clients = playerhelpers->GetMaxClients();
ds@1952: for (int i = 1;
ds@1952: i <= max_clients && (cell_t)info->num_targets < info->max_targets;
ds@1952: i++)
ds@1952: {
ds@1952: if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952: if (!pPlayer->IsInGame())
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952: if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952: if (pInfo->GetTeamIndex() != (int)team_index)
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952: if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
ds@1952: != COMMAND_TARGET_VALID)
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952: info->targets[info->num_targets] = i;
ds@1952: info->num_targets++;
ds@1952: }
ds@1952:
ds@1952: if (info->num_targets == 0)
ds@1952: {
ds@1952: info->reason = COMMAND_TARGET_EMPTY_FILTER;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: info->reason = COMMAND_TARGET_VALID;
ds@1952: }
ds@1952:
ds@1952: info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952: if (team_index == 2)
ds@1952: {
ds@1952: UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
ds@1952: }
ds@1952: else if (team_index == 3)
ds@1952: {
ds@1952: UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
ds@1952: }
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
pred@1963: int FindResourceEntity()
pred@1963: {
pred@1963: return FindEntityByNetClass(-1, "CTFPlayerResource");
pred@1963: }
pred@1963:
pred@1963:
pred@1963: int FindEntityByNetClass(int start, const char *classname)
pred@1963: {
pred@1963: edict_t *current;
pred@1963:
pred@1963: for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
pred@1963: {
pred@1963: current = engine->PEntityOfEntIndex(i);
pred@1963: if (current == NULL)
pred@1963: {
pred@1963: continue;
pred@1963: }
pred@1963:
pred@1963: IServerNetworkable *network = current->GetNetworkable();
pred@1963:
pred@1963: if (network == NULL)
pred@1963: {
pred@1963: continue;
pred@1963: }
pred@1963:
pred@1963: ServerClass *sClass = network->GetServerClass();
pred@1963: const char *name = sClass->GetName();
pred@1963:
pred@1963:
pred@1963: if (strcmp(name, classname) == 0)
pred@1963: {
pred@1963: return i;
pred@1963: }
pred@1963: }
pred@1963:
pred@1963: return -1;
pred@1963: }
pred@1963:
pred@1963: TFClassType ClassnameToType(const char *classname)
pred@1963: {
pred@1963: static KTrie trie;
pred@1963: static bool filled = false;
pred@1963:
pred@1963: if (!filled)
pred@1963: {
pred@1963: trie.insert("scout", TFClass_Scout);
pred@1963: trie.insert("scout", TFClass_Scout);
pred@1963: trie.insert("sniper", TFClass_Sniper);
pred@1963: trie.insert("soldier", TFClass_Soldier);
pred@1963: trie.insert("demoman", TFClass_DemoMan);
pred@1963: trie.insert("demo", TFClass_DemoMan);
pred@1963: trie.insert("medic", TFClass_Medic);
pred@1963: trie.insert("heavy", TFClass_Heavy);
pred@1963: trie.insert("hwg", TFClass_Heavy);
pred@1963: trie.insert("pyro", TFClass_Pyro);
pred@1963: trie.insert("spy", TFClass_Spy);
pred@1963: trie.insert("engineer", TFClass_Engineer);
pred@1963: trie.insert("engy", TFClass_Engineer);
pred@1963:
pred@1963: filled = true;
pred@1963: }
pred@1963:
pred@1963: TFClassType *value;
pred@1963:
pred@1963: if (!(value = trie.retrieve(classname)))
pred@1963: {
pred@1963: return TFClass_Unknown;
pred@1963: }
pred@1963:
pred@1963: return *value;
pred@1963: }
pred@1963:
pred@1963:
pred@1963:
pred@1963: /**
pred@1963: * A picture of a blue crab given to me as a gift and stored here for safe keeping
pred@1963: *
pred@1963: * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg
pred@1963: */
pred@1963: