extensions/tf2/conditions.cpp
changeset 3233 21dbe3d3f620
child 3332 e0af1cba759f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/tf2/conditions.cpp	Sat Apr 09 00:33:55 2011 -0400
     1.3 @@ -0,0 +1,210 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Team Fortress 2 Extension
     1.8 + * Copyright (C) 2004-2011 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "extension.h"
    1.36 +#include "conditions.h"
    1.37 +#include "util.h"
    1.38 +
    1.39 +typedef int condflags_t;
    1.40 +#define CONDITION_TAUNTING 7
    1.41 +
    1.42 +IForward *g_addCondForward = NULL;
    1.43 +IForward *g_removeCondForward = NULL;
    1.44 +
    1.45 +CDetour *detConditionAdd = NULL;
    1.46 +CDetour *detConditionRemove = NULL;
    1.47 +CDetour *detConditionRemoveAll = NULL;
    1.48 +
    1.49 +int playerCondOffset = -1;
    1.50 +int conditionBitsOffset = -1;
    1.51 +
    1.52 +bool g_bIgnoreRemove;
    1.53 +
    1.54 +inline condflags_t GetPlayerConds(CBaseEntity *pPlayer)
    1.55 +{
    1.56 +	condflags_t cond1 = *(condflags_t *)((intptr_t)pPlayer + playerCondOffset);
    1.57 +	condflags_t cond2 = *(condflags_t *)((intptr_t)pPlayer + conditionBitsOffset);
    1.58 +	return cond1|cond2;
    1.59 +}
    1.60 +
    1.61 +DETOUR_DECL_MEMBER2(AddCond, void, int, condition, float, duration)
    1.62 +{
    1.63 +	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
    1.64 +	condflags_t oldconds = GetPlayerConds(pPlayer);
    1.65 +
    1.66 +	DETOUR_MEMBER_CALL(AddCond)(condition, duration);
    1.67 +
    1.68 +	int bit = 1 << condition;
    1.69 +
    1.70 +	// The player already had this condition so bug out
    1.71 +	if ((oldconds & bit) == bit)
    1.72 +		return;
    1.73 +
    1.74 +	// The condition wasn't actually added (this can happen!)
    1.75 +	if ((GetPlayerConds(pPlayer) & bit) != bit)
    1.76 +		return;
    1.77 +	
    1.78 +	int client = gamehelpers->EntityToBCompatRef(pPlayer);
    1.79 +
    1.80 +	g_addCondForward->PushCell(client);
    1.81 +	g_addCondForward->PushCell(condition);
    1.82 +	g_addCondForward->PushFloat(duration);
    1.83 +	g_addCondForward->Execute(NULL, NULL);
    1.84 +}
    1.85 +
    1.86 +DETOUR_DECL_MEMBER2(RemoveCond, void, int, condition, bool, bForceRemove)
    1.87 +{
    1.88 +	if (g_bIgnoreRemove)
    1.89 +	{
    1.90 +		DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
    1.91 +		return;
    1.92 +	}
    1.93 +
    1.94 +	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
    1.95 +
    1.96 +	condflags_t oldconds = GetPlayerConds(pPlayer);
    1.97 +	int bit = 1 << condition;
    1.98 +
    1.99 +	// If we didn't have it, it won't be removed
   1.100 +	if ((oldconds & bit) != bit)
   1.101 +	{
   1.102 +		DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
   1.103 +		return;
   1.104 +	}
   1.105 +
   1.106 +	DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
   1.107 +
   1.108 +	// Calling RemoveCond doesn't necessarily remove the cond...
   1.109 +	if ((GetPlayerConds(pPlayer) & bit) == bit)
   1.110 +	{
   1.111 +		return;
   1.112 +	}
   1.113 +
   1.114 +	int client = gamehelpers->EntityToBCompatRef(pPlayer);
   1.115 +
   1.116 +	DoRemoveCond(client, condition);
   1.117 +}
   1.118 +
   1.119 +DETOUR_DECL_MEMBER1(RemoveAllCond, void, CBaseEntity *, unknown)
   1.120 +{
   1.121 +	// We're going to ignore Remove here since it can be called multiple times for the same cond
   1.122 +	g_bIgnoreRemove = true;
   1.123 +
   1.124 +	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
   1.125 +
   1.126 +	int client = gamehelpers->EntityToBCompatRef(pPlayer);
   1.127 +	condflags_t conds = GetPlayerConds(pPlayer);
   1.128 +
   1.129 +	// I don't know what the unknown is, but it's always NULL
   1.130 +	DETOUR_MEMBER_CALL(RemoveAllCond)(unknown);
   1.131 +	
   1.132 +	int max = sizeof(condflags_t)*8;
   1.133 +	for (int i = 0; i < max; i++)
   1.134 +	{
   1.135 +		// Taunt flag removal doesn't go through RemoveCond nor OnCondRemoved (or was mangled by compiler?)
   1.136 +		// Better to "documentedly" give no removes rather than only in some cases
   1.137 +		if (i == CONDITION_TAUNTING)
   1.138 +			continue;
   1.139 +
   1.140 +		int bit = (1<<i);
   1.141 +		if ((conds & bit) == bit)
   1.142 +		{
   1.143 +			DoRemoveCond(client, i);
   1.144 +		}
   1.145 +	}
   1.146 +
   1.147 +	g_bIgnoreRemove = false;
   1.148 +}
   1.149 +
   1.150 +void DoRemoveCond(int client, int condition)
   1.151 +{
   1.152 +	g_removeCondForward->PushCell(client);
   1.153 +	g_removeCondForward->PushCell(condition);
   1.154 +	g_removeCondForward->Execute(NULL, NULL);
   1.155 +}
   1.156 +
   1.157 +bool InitialiseConditionDetours()
   1.158 +{
   1.159 +	sm_sendprop_info_t prop;
   1.160 +	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop))
   1.161 +	{
   1.162 +		g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset");
   1.163 +		return false;
   1.164 +	}
   1.165 +	
   1.166 +	playerCondOffset = prop.actual_offset;
   1.167 +	
   1.168 +	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop))
   1.169 +	{
   1.170 +		g_pSM->LogError(myself, "Failed to find _condition_bits prop offset");
   1.171 +		return false;
   1.172 +	}
   1.173 +
   1.174 +	conditionBitsOffset = prop.actual_offset;
   1.175 +
   1.176 +	if (playerCondOffset == -1 || conditionBitsOffset == -1)
   1.177 +		return false;
   1.178 +
   1.179 +	detConditionAdd = DETOUR_CREATE_MEMBER(AddCond, "AddCondition");
   1.180 +	if (detConditionAdd == NULL)
   1.181 +	{
   1.182 +		g_pSM->LogError(myself, "CTFPlayerShared::AddCond detour failed");
   1.183 +		return false;
   1.184 +	}
   1.185 +
   1.186 +	detConditionRemove = DETOUR_CREATE_MEMBER(RemoveCond, "RemoveCondition");
   1.187 +	if (detConditionRemove == NULL)
   1.188 +	{
   1.189 +		g_pSM->LogError(myself, "CTFPlayerShared::RemoveCond detour failed");
   1.190 +		return false;
   1.191 +	}
   1.192 +
   1.193 +	detConditionRemoveAll = DETOUR_CREATE_MEMBER(RemoveAllCond, "RemoveAllConditions");
   1.194 +	if (detConditionRemoveAll == NULL)
   1.195 +	{
   1.196 +		g_pSM->LogError(myself, "CTFPlayerShared::RemoveAllCond detour failed");
   1.197 +		return false;
   1.198 +	}
   1.199 +
   1.200 +	detConditionAdd->EnableDetour();
   1.201 +	detConditionRemove->EnableDetour();
   1.202 +	detConditionRemoveAll->EnableDetour();
   1.203 +
   1.204 +	return true;
   1.205 +}
   1.206 +
   1.207 +void RemoveConditionDetours()
   1.208 +{
   1.209 +	detConditionAdd->Destroy();
   1.210 +	detConditionRemove->Destroy();
   1.211 +	detConditionRemoveAll->Destroy();
   1.212 +}
   1.213 +