extensions/tf2/conditions.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat Apr 09 00:33:55 2011 -0400 (2011-04-09)
changeset 3233 21dbe3d3f620
child 3332 e0af1cba759f
permissions -rw-r--r--
Added forwards to TF2 ext for cond addition and removal (bug 4851, r=fyren).
skamonkey@3233
     1
/**
skamonkey@3233
     2
 * vim: set ts=4 :
skamonkey@3233
     3
 * =============================================================================
skamonkey@3233
     4
 * SourceMod Team Fortress 2 Extension
skamonkey@3233
     5
 * Copyright (C) 2004-2011 AlliedModders LLC.  All rights reserved.
skamonkey@3233
     6
 * =============================================================================
skamonkey@3233
     7
 *
skamonkey@3233
     8
 * This program is free software; you can redistribute it and/or modify it under
skamonkey@3233
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
skamonkey@3233
    10
 * Free Software Foundation.
skamonkey@3233
    11
 * 
skamonkey@3233
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
skamonkey@3233
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
skamonkey@3233
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
skamonkey@3233
    15
 * details.
skamonkey@3233
    16
 *
skamonkey@3233
    17
 * You should have received a copy of the GNU General Public License along with
skamonkey@3233
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
skamonkey@3233
    19
 *
skamonkey@3233
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
skamonkey@3233
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
skamonkey@3233
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
skamonkey@3233
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
skamonkey@3233
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
skamonkey@3233
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
skamonkey@3233
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
skamonkey@3233
    27
 * or <http://www.sourcemod.net/license.php>.
skamonkey@3233
    28
 *
skamonkey@3233
    29
 * Version: $Id$
skamonkey@3233
    30
 */
skamonkey@3233
    31
skamonkey@3233
    32
#include "extension.h"
skamonkey@3233
    33
#include "conditions.h"
skamonkey@3233
    34
#include "util.h"
skamonkey@3233
    35
skamonkey@3233
    36
typedef int condflags_t;
skamonkey@3233
    37
#define CONDITION_TAUNTING 7
skamonkey@3233
    38
skamonkey@3233
    39
IForward *g_addCondForward = NULL;
skamonkey@3233
    40
IForward *g_removeCondForward = NULL;
skamonkey@3233
    41
skamonkey@3233
    42
CDetour *detConditionAdd = NULL;
skamonkey@3233
    43
CDetour *detConditionRemove = NULL;
skamonkey@3233
    44
CDetour *detConditionRemoveAll = NULL;
skamonkey@3233
    45
skamonkey@3233
    46
int playerCondOffset = -1;
skamonkey@3233
    47
int conditionBitsOffset = -1;
skamonkey@3233
    48
skamonkey@3233
    49
bool g_bIgnoreRemove;
skamonkey@3233
    50
skamonkey@3233
    51
inline condflags_t GetPlayerConds(CBaseEntity *pPlayer)
skamonkey@3233
    52
{
skamonkey@3233
    53
	condflags_t cond1 = *(condflags_t *)((intptr_t)pPlayer + playerCondOffset);
skamonkey@3233
    54
	condflags_t cond2 = *(condflags_t *)((intptr_t)pPlayer + conditionBitsOffset);
skamonkey@3233
    55
	return cond1|cond2;
skamonkey@3233
    56
}
skamonkey@3233
    57
skamonkey@3233
    58
DETOUR_DECL_MEMBER2(AddCond, void, int, condition, float, duration)
skamonkey@3233
    59
{
skamonkey@3233
    60
	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
skamonkey@3233
    61
	condflags_t oldconds = GetPlayerConds(pPlayer);
skamonkey@3233
    62
skamonkey@3233
    63
	DETOUR_MEMBER_CALL(AddCond)(condition, duration);
skamonkey@3233
    64
skamonkey@3233
    65
	int bit = 1 << condition;
skamonkey@3233
    66
skamonkey@3233
    67
	// The player already had this condition so bug out
skamonkey@3233
    68
	if ((oldconds & bit) == bit)
skamonkey@3233
    69
		return;
skamonkey@3233
    70
skamonkey@3233
    71
	// The condition wasn't actually added (this can happen!)
skamonkey@3233
    72
	if ((GetPlayerConds(pPlayer) & bit) != bit)
skamonkey@3233
    73
		return;
skamonkey@3233
    74
	
skamonkey@3233
    75
	int client = gamehelpers->EntityToBCompatRef(pPlayer);
skamonkey@3233
    76
skamonkey@3233
    77
	g_addCondForward->PushCell(client);
skamonkey@3233
    78
	g_addCondForward->PushCell(condition);
skamonkey@3233
    79
	g_addCondForward->PushFloat(duration);
skamonkey@3233
    80
	g_addCondForward->Execute(NULL, NULL);
skamonkey@3233
    81
}
skamonkey@3233
    82
skamonkey@3233
    83
DETOUR_DECL_MEMBER2(RemoveCond, void, int, condition, bool, bForceRemove)
skamonkey@3233
    84
{
skamonkey@3233
    85
	if (g_bIgnoreRemove)
skamonkey@3233
    86
	{
skamonkey@3233
    87
		DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
skamonkey@3233
    88
		return;
skamonkey@3233
    89
	}
skamonkey@3233
    90
skamonkey@3233
    91
	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
skamonkey@3233
    92
skamonkey@3233
    93
	condflags_t oldconds = GetPlayerConds(pPlayer);
skamonkey@3233
    94
	int bit = 1 << condition;
skamonkey@3233
    95
skamonkey@3233
    96
	// If we didn't have it, it won't be removed
skamonkey@3233
    97
	if ((oldconds & bit) != bit)
skamonkey@3233
    98
	{
skamonkey@3233
    99
		DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
skamonkey@3233
   100
		return;
skamonkey@3233
   101
	}
skamonkey@3233
   102
skamonkey@3233
   103
	DETOUR_MEMBER_CALL(RemoveCond)(condition, bForceRemove);
skamonkey@3233
   104
skamonkey@3233
   105
	// Calling RemoveCond doesn't necessarily remove the cond...
skamonkey@3233
   106
	if ((GetPlayerConds(pPlayer) & bit) == bit)
skamonkey@3233
   107
	{
skamonkey@3233
   108
		return;
skamonkey@3233
   109
	}
skamonkey@3233
   110
skamonkey@3233
   111
	int client = gamehelpers->EntityToBCompatRef(pPlayer);
skamonkey@3233
   112
skamonkey@3233
   113
	DoRemoveCond(client, condition);
skamonkey@3233
   114
}
skamonkey@3233
   115
skamonkey@3233
   116
DETOUR_DECL_MEMBER1(RemoveAllCond, void, CBaseEntity *, unknown)
skamonkey@3233
   117
{
skamonkey@3233
   118
	// We're going to ignore Remove here since it can be called multiple times for the same cond
skamonkey@3233
   119
	g_bIgnoreRemove = true;
skamonkey@3233
   120
skamonkey@3233
   121
	CBaseEntity *pPlayer = (CBaseEntity *)((intptr_t)this - playerSharedOffset->actual_offset);
skamonkey@3233
   122
skamonkey@3233
   123
	int client = gamehelpers->EntityToBCompatRef(pPlayer);
skamonkey@3233
   124
	condflags_t conds = GetPlayerConds(pPlayer);
skamonkey@3233
   125
skamonkey@3233
   126
	// I don't know what the unknown is, but it's always NULL
skamonkey@3233
   127
	DETOUR_MEMBER_CALL(RemoveAllCond)(unknown);
skamonkey@3233
   128
	
skamonkey@3233
   129
	int max = sizeof(condflags_t)*8;
skamonkey@3233
   130
	for (int i = 0; i < max; i++)
skamonkey@3233
   131
	{
skamonkey@3233
   132
		// Taunt flag removal doesn't go through RemoveCond nor OnCondRemoved (or was mangled by compiler?)
skamonkey@3233
   133
		// Better to "documentedly" give no removes rather than only in some cases
skamonkey@3233
   134
		if (i == CONDITION_TAUNTING)
skamonkey@3233
   135
			continue;
skamonkey@3233
   136
skamonkey@3233
   137
		int bit = (1<<i);
skamonkey@3233
   138
		if ((conds & bit) == bit)
skamonkey@3233
   139
		{
skamonkey@3233
   140
			DoRemoveCond(client, i);
skamonkey@3233
   141
		}
skamonkey@3233
   142
	}
skamonkey@3233
   143
skamonkey@3233
   144
	g_bIgnoreRemove = false;
skamonkey@3233
   145
}
skamonkey@3233
   146
skamonkey@3233
   147
void DoRemoveCond(int client, int condition)
skamonkey@3233
   148
{
skamonkey@3233
   149
	g_removeCondForward->PushCell(client);
skamonkey@3233
   150
	g_removeCondForward->PushCell(condition);
skamonkey@3233
   151
	g_removeCondForward->Execute(NULL, NULL);
skamonkey@3233
   152
}
skamonkey@3233
   153
skamonkey@3233
   154
bool InitialiseConditionDetours()
skamonkey@3233
   155
{
skamonkey@3233
   156
	sm_sendprop_info_t prop;
skamonkey@3233
   157
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop))
skamonkey@3233
   158
	{
skamonkey@3233
   159
		g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset");
skamonkey@3233
   160
		return false;
skamonkey@3233
   161
	}
skamonkey@3233
   162
	
skamonkey@3233
   163
	playerCondOffset = prop.actual_offset;
skamonkey@3233
   164
	
skamonkey@3233
   165
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop))
skamonkey@3233
   166
	{
skamonkey@3233
   167
		g_pSM->LogError(myself, "Failed to find _condition_bits prop offset");
skamonkey@3233
   168
		return false;
skamonkey@3233
   169
	}
skamonkey@3233
   170
skamonkey@3233
   171
	conditionBitsOffset = prop.actual_offset;
skamonkey@3233
   172
skamonkey@3233
   173
	if (playerCondOffset == -1 || conditionBitsOffset == -1)
skamonkey@3233
   174
		return false;
skamonkey@3233
   175
skamonkey@3233
   176
	detConditionAdd = DETOUR_CREATE_MEMBER(AddCond, "AddCondition");
skamonkey@3233
   177
	if (detConditionAdd == NULL)
skamonkey@3233
   178
	{
skamonkey@3233
   179
		g_pSM->LogError(myself, "CTFPlayerShared::AddCond detour failed");
skamonkey@3233
   180
		return false;
skamonkey@3233
   181
	}
skamonkey@3233
   182
skamonkey@3233
   183
	detConditionRemove = DETOUR_CREATE_MEMBER(RemoveCond, "RemoveCondition");
skamonkey@3233
   184
	if (detConditionRemove == NULL)
skamonkey@3233
   185
	{
skamonkey@3233
   186
		g_pSM->LogError(myself, "CTFPlayerShared::RemoveCond detour failed");
skamonkey@3233
   187
		return false;
skamonkey@3233
   188
	}
skamonkey@3233
   189
skamonkey@3233
   190
	detConditionRemoveAll = DETOUR_CREATE_MEMBER(RemoveAllCond, "RemoveAllConditions");
skamonkey@3233
   191
	if (detConditionRemoveAll == NULL)
skamonkey@3233
   192
	{
skamonkey@3233
   193
		g_pSM->LogError(myself, "CTFPlayerShared::RemoveAllCond detour failed");
skamonkey@3233
   194
		return false;
skamonkey@3233
   195
	}
skamonkey@3233
   196
skamonkey@3233
   197
	detConditionAdd->EnableDetour();
skamonkey@3233
   198
	detConditionRemove->EnableDetour();
skamonkey@3233
   199
	detConditionRemoveAll->EnableDetour();
skamonkey@3233
   200
skamonkey@3233
   201
	return true;
skamonkey@3233
   202
}
skamonkey@3233
   203
skamonkey@3233
   204
void RemoveConditionDetours()
skamonkey@3233
   205
{
skamonkey@3233
   206
	detConditionAdd->Destroy();
skamonkey@3233
   207
	detConditionRemove->Destroy();
skamonkey@3233
   208
	detConditionRemoveAll->Destroy();
skamonkey@3233
   209
}
skamonkey@3233
   210