3 * =============================================================================
4 * SourceMod Team Fortress 2 Extension
5 * Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include <sourcemod_version.h>
33 #include "extension.h"
35 #include "RegNatives.h"
36 #include "iplayerinfo.h"
37 #include "sm_trie_tpl.h"
38 #include "criticals.h"
40 #include "conditions.h"
41 #include "gameplayrules.h"
42 #include "CDetour/detours.h"
46 * @brief Implement extension code here.
51 IGameConfig *g_pGameConf = NULL;
53 IBinTools *g_pBinTools = NULL;
55 SMEXT_LINK(&g_TF2Tools);
59 CGlobalVars *gpGlobals = NULL;
61 sm_sendprop_info_t *playerSharedOffset;
63 extern sp_nativeinfo_t g_TFNatives[];
67 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
69 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
71 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
73 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
77 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
80 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
84 playerSharedOffset = new sm_sendprop_info_t;
86 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
88 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
92 sharesys->AddDependency(myself, "bintools.ext", true, true);
94 char conf_error[255] = "";
95 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
99 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
104 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
106 sharesys->AddNatives(myself, g_TFNatives);
107 sharesys->RegisterLibrary(myself, "tf2");
109 plsys->AddPluginsListener(this);
111 playerhelpers->RegisterCommandTargetProcessor(this);
112 playerhelpers->AddClientListener(this);
114 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
115 g_getHolidayForward = forwards->CreateForward("TF2_OnGetHoliday", ET_Event, 1, NULL, Param_CellByRef);
116 g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
117 g_removeCondForward = forwards->CreateForward("TF2_OnConditionRemoved", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
118 g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL);
119 g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL);
123 m_CritDetoursEnabled = false;
124 m_GetHolidayDetourEnabled = false;
125 m_CondChecksEnabled = false;
126 m_RulesDetoursEnabled = false;
131 const char *TF2Tools::GetExtensionVerString()
133 return SM_FULL_VERSION;
136 const char *TF2Tools::GetExtensionDateString()
138 return SM_BUILD_TIMESTAMP;
141 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
143 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
145 gpGlobals = ismm->GetCGlobals();
147 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
149 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
151 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
153 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
154 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
159 void TF2Tools::SDK_OnUnload()
161 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
163 g_RegNatives.UnregisterAll();
164 gameconfs->CloseGameConfigFile(g_pGameConf);
165 playerhelpers->UnregisterCommandTargetProcessor(this);
166 playerhelpers->RemoveClientListener(this);
168 plsys->RemovePluginsListener(this);
170 forwards->ReleaseForward(g_critForward);
171 forwards->ReleaseForward(g_getHolidayForward);
172 forwards->ReleaseForward(g_addCondForward);
173 forwards->ReleaseForward(g_removeCondForward);
174 forwards->ReleaseForward(g_waitingPlayersStartForward);
175 forwards->ReleaseForward(g_waitingPlayersEndForward);
178 void TF2Tools::SDK_OnAllLoaded()
180 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
183 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
185 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
188 void TF2Tools::FireGameEvent( IGameEvent *event )
190 timersys->NotifyOfGameStart();
191 timersys->MapTimeLeftChanged();
194 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
196 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
201 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
203 if (pInterface == g_pBinTools)
208 return IExtensionInterface::QueryInterfaceDrop(pInterface);
211 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
213 g_RegNatives.UnregisterAll();
216 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
218 g_resourceEntity = FindResourceEntity();
221 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
225 unsigned int team_index = 0;
226 IGamePlayer *pPlayer, *pAdmin;
228 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
235 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
239 if (!pAdmin->IsInGame())
249 if (strcmp(info->pattern, "@red") == 0 )
253 else if (strcmp(info->pattern, "@blue") == 0)
262 info->num_targets = 0;
264 max_clients = playerhelpers->GetMaxClients();
266 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
269 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
273 if (!pPlayer->IsInGame())
277 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
281 if (pInfo->GetTeamIndex() != (int)team_index)
285 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
286 != COMMAND_TARGET_VALID)
290 info->targets[info->num_targets] = i;
294 if (info->num_targets == 0)
296 info->reason = COMMAND_TARGET_EMPTY_FILTER;
300 info->reason = COMMAND_TARGET_VALID;
303 info->target_name_style = COMMAND_TARGETNAME_RAW;
306 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
308 else if (team_index == 3)
310 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
316 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
318 if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
320 m_CritDetoursEnabled = InitialiseCritDetours();
323 if (!m_GetHolidayDetourEnabled && g_getHolidayForward->GetFunctionCount())
325 m_GetHolidayDetourEnabled = InitialiseGetHolidayDetour();
328 if (!m_CondChecksEnabled
329 && ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() )
332 m_CondChecksEnabled = InitialiseConditionChecks();
335 if (!m_RulesDetoursEnabled
336 && ( g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount() )
339 m_RulesDetoursEnabled = InitialiseRulesDetours();
343 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
345 if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
348 m_CritDetoursEnabled = false;
350 if (m_GetHolidayDetourEnabled && !g_getHolidayForward->GetFunctionCount())
352 RemoveGetHolidayDetour();
353 m_GetHolidayDetourEnabled = false;
355 if (m_CondChecksEnabled)
357 if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount())
359 RemoveConditionChecks();
360 m_CondChecksEnabled = false;
363 if (m_RulesDetoursEnabled)
365 if (!g_waitingPlayersStartForward->GetFunctionCount() && !g_waitingPlayersEndForward->GetFunctionCount())
367 RemoveRulesDetours();
368 m_RulesDetoursEnabled = false;
373 void TF2Tools::OnClientPutInServer(int client)
375 Conditions_OnClientPutInServer(client);
378 int FindResourceEntity()
380 return FindEntityByNetClass(-1, "CTFPlayerResource");
384 int FindEntityByNetClass(int start, const char *classname)
388 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
390 current = engine->PEntityOfEntIndex(i);
396 IServerNetworkable *network = current->GetNetworkable();
403 ServerClass *sClass = network->GetServerClass();
404 const char *name = sClass->GetName();
407 if (strcmp(name, classname) == 0)
416 TFClassType ClassnameToType(const char *classname)
418 static KTrie<TFClassType> trie;
419 static bool filled = false;
423 trie.insert("scout", TFClass_Scout);
424 trie.insert("sniper", TFClass_Sniper);
425 trie.insert("soldier", TFClass_Soldier);
426 trie.insert("demoman", TFClass_DemoMan);
427 trie.insert("demo", TFClass_DemoMan);
428 trie.insert("medic", TFClass_Medic);
429 trie.insert("heavy", TFClass_Heavy);
430 trie.insert("hwg", TFClass_Heavy);
431 trie.insert("pyro", TFClass_Pyro);
432 trie.insert("spy", TFClass_Spy);
433 trie.insert("engineer", TFClass_Engineer);
434 trie.insert("engy", TFClass_Engineer);
441 if (!(value = trie.retrieve(classname)))
443 return TFClass_Unknown;
452 * A picture of a blue crab given to me as a gift and stored here for safe keeping
454 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg