skamonkey@3233: /** skamonkey@3233: * vim: set ts=4 : skamonkey@3233: * ============================================================================= skamonkey@3233: * SourceMod Team Fortress 2 Extension skamonkey@3233: * Copyright (C) 2004-2011 AlliedModders LLC. All rights reserved. skamonkey@3233: * ============================================================================= skamonkey@3233: * skamonkey@3233: * This program is free software; you can redistribute it and/or modify it under skamonkey@3233: * the terms of the GNU General Public License, version 3.0, as published by the skamonkey@3233: * Free Software Foundation. skamonkey@3233: * skamonkey@3233: * This program is distributed in the hope that it will be useful, but WITHOUT skamonkey@3233: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS skamonkey@3233: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more skamonkey@3233: * details. skamonkey@3233: * skamonkey@3233: * You should have received a copy of the GNU General Public License along with skamonkey@3233: * this program. If not, see . skamonkey@3233: * skamonkey@3233: * As a special exception, AlliedModders LLC gives you permission to link the skamonkey@3233: * code of this program (as well as its derivative works) to "Half-Life 2," the skamonkey@3233: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software skamonkey@3233: * by the Valve Corporation. You must obey the GNU General Public License in skamonkey@3233: * all respects for all other code used. Additionally, AlliedModders LLC grants skamonkey@3233: * this exception to all derivative works. AlliedModders LLC defines further skamonkey@3233: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), skamonkey@3233: * or . skamonkey@3233: * skamonkey@3233: * Version: $Id$ skamonkey@3233: */ skamonkey@3233: skamonkey@3233: #include "extension.h" skamonkey@3233: #include "conditions.h" skamonkey@3233: #include "util.h" skamonkey@3233: skamonkey@3233: typedef int condflags_t; skamonkey@3233: #define CONDITION_TAUNTING 7 skamonkey@3233: skamonkey@3233: IForward *g_addCondForward = NULL; skamonkey@3233: IForward *g_removeCondForward = NULL; skamonkey@3233: skamonkey@3233: CDetour *detConditionAdd = NULL; skamonkey@3233: CDetour *detConditionRemove = NULL; skamonkey@3233: CDetour *detConditionRemoveAll = NULL; skamonkey@3233: skamonkey@3233: int playerCondOffset = -1; skamonkey@3233: int conditionBitsOffset = -1; skamonkey@3233: skamonkey@3233: bool g_bIgnoreRemove; skamonkey@3233: skamonkey@3233: inline condflags_t GetPlayerConds(CBaseEntity *pPlayer) skamonkey@3233: { skamonkey@3233: condflags_t cond1 = *(condflags_t *)((intptr_t)pPlayer + playerCondOffset); skamonkey@3233: condflags_t cond2 = *(condflags_t *)((intptr_t)pPlayer + conditionBitsOffset); skamonkey@3233: return cond1|cond2; skamonkey@3233: } skamonkey@3233: skamonkey@3233: DETOUR_DECL_MEMBER2(AddCond, void, int, condition, float, duration) skamonkey@3233: { skamonkey@3233: CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); skamonkey@3233: condflags_t oldconds = GetPlayerConds(pPlayer); skamonkey@3233: skamonkey@3233: DETOUR_MEMBER_CALL(AddCond)(condition, duration); skamonkey@3233: skamonkey@3233: int bit = 1 << condition; skamonkey@3233: skamonkey@3233: // The player already had this condition so bug out skamonkey@3233: if ((oldconds & bit) == bit) skamonkey@3233: return; skamonkey@3233: skamonkey@3233: // The condition wasn't actually added (this can happen!) skamonkey@3233: if ((GetPlayerConds(pPlayer) & bit) != bit) skamonkey@3233: return; skamonkey@3233: skamonkey@3233: int client = gamehelpers->EntityToBCompatRef(pPlayer); skamonkey@3233: skamonkey@3233: g_addCondForward->PushCell(client); skamonkey@3233: g_addCondForward->PushCell(condition); skamonkey@3233: g_addCondForward->PushFloat(duration); skamonkey@3233: g_addCondForward->Execute(NULL, NULL); skamonkey@3233: } skamonkey@3233: skamonkey@3233: DETOUR_DECL_MEMBER2(RemoveCond, void, int, condition, bool, bForceRemove) skamonkey@3233: { skamonkey@3233: if (g_bIgnoreRemove) skamonkey@3233: { skamonkey@3233: DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); skamonkey@3233: return; skamonkey@3233: } skamonkey@3233: skamonkey@3233: CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); skamonkey@3233: skamonkey@3233: condflags_t oldconds = GetPlayerConds(pPlayer); skamonkey@3233: int bit = 1 << condition; skamonkey@3233: skamonkey@3233: // If we didn't have it, it won't be removed skamonkey@3233: if ((oldconds & bit) != bit) skamonkey@3233: { skamonkey@3233: DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); skamonkey@3233: return; skamonkey@3233: } skamonkey@3233: skamonkey@3233: DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove); skamonkey@3233: skamonkey@3233: // Calling RemoveCond doesn't necessarily remove the cond... skamonkey@3233: if ((GetPlayerConds(pPlayer) & bit) == bit) skamonkey@3233: { skamonkey@3233: return; skamonkey@3233: } skamonkey@3233: skamonkey@3233: int client = gamehelpers->EntityToBCompatRef(pPlayer); skamonkey@3233: skamonkey@3233: DoRemoveCond(client, condition); skamonkey@3233: } skamonkey@3233: skamonkey@3233: DETOUR_DECL_MEMBER1(RemoveAllCond, void, CBaseEntity *, unknown) skamonkey@3233: { skamonkey@3233: // We're going to ignore Remove here since it can be called multiple times for the same cond skamonkey@3233: g_bIgnoreRemove = true; skamonkey@3233: skamonkey@3233: CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset); skamonkey@3233: skamonkey@3233: int client = gamehelpers->EntityToBCompatRef(pPlayer); skamonkey@3233: condflags_t conds = GetPlayerConds(pPlayer); skamonkey@3233: skamonkey@3233: // I don't know what the unknown is, but it's always NULL skamonkey@3233: DETOUR_MEMBER_CALL(RemoveAllCond)(unknown); skamonkey@3233: skamonkey@3233: int max = sizeof(condflags_t)*8; skamonkey@3233: for (int i = 0; i < max; i++) skamonkey@3233: { skamonkey@3233: // Taunt flag removal doesn't go through RemoveCond nor OnCondRemoved (or was mangled by compiler?) skamonkey@3233: // Better to "documentedly" give no removes rather than only in some cases skamonkey@3233: if (i == CONDITION_TAUNTING) skamonkey@3233: continue; skamonkey@3233: skamonkey@3233: int bit = (1<PushCell(client); skamonkey@3233: g_removeCondForward->PushCell(condition); skamonkey@3233: g_removeCondForward->Execute(NULL, NULL); skamonkey@3233: } skamonkey@3233: skamonkey@3233: bool InitialiseConditionDetours() skamonkey@3233: { skamonkey@3233: sm_sendprop_info_t prop; skamonkey@3233: if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop)) skamonkey@3233: { skamonkey@3233: g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset"); skamonkey@3233: return false; skamonkey@3233: } skamonkey@3233: skamonkey@3233: playerCondOffset = prop.actual_offset; skamonkey@3233: skamonkey@3233: if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop)) skamonkey@3233: { skamonkey@3233: g_pSM->LogError(myself, "Failed to find _condition_bits prop offset"); skamonkey@3233: return false; skamonkey@3233: } skamonkey@3233: skamonkey@3233: conditionBitsOffset = prop.actual_offset; skamonkey@3233: skamonkey@3233: if (playerCondOffset == -1 || conditionBitsOffset == -1) skamonkey@3233: return false; skamonkey@3233: skamonkey@3233: detConditionAdd = DETOUR_CREATE_MEMBER(AddCond, "AddCondition"); skamonkey@3233: if (detConditionAdd == NULL) skamonkey@3233: { skamonkey@3233: g_pSM->LogError(myself, "CTFPlayerShared::AddCond detour failed"); skamonkey@3233: return false; skamonkey@3233: } skamonkey@3233: skamonkey@3233: detConditionRemove = DETOUR_CREATE_MEMBER(RemoveCond, "RemoveCondition"); skamonkey@3233: if (detConditionRemove == NULL) skamonkey@3233: { skamonkey@3233: g_pSM->LogError(myself, "CTFPlayerShared::RemoveCond detour failed"); skamonkey@3233: return false; skamonkey@3233: } skamonkey@3233: skamonkey@3233: detConditionRemoveAll = DETOUR_CREATE_MEMBER(RemoveAllCond, "RemoveAllConditions"); skamonkey@3233: if (detConditionRemoveAll == NULL) skamonkey@3233: { skamonkey@3233: g_pSM->LogError(myself, "CTFPlayerShared::RemoveAllCond detour failed"); skamonkey@3233: return false; skamonkey@3233: } skamonkey@3233: skamonkey@3233: detConditionAdd->EnableDetour(); skamonkey@3233: detConditionRemove->EnableDetour(); skamonkey@3233: detConditionRemoveAll->EnableDetour(); skamonkey@3233: skamonkey@3233: return true; skamonkey@3233: } skamonkey@3233: skamonkey@3233: void RemoveConditionDetours() skamonkey@3233: { skamonkey@3233: detConditionAdd->Destroy(); skamonkey@3233: detConditionRemove->Destroy(); skamonkey@3233: detConditionRemoveAll->Destroy(); skamonkey@3233: } skamonkey@3233: