extensions/tf2/conditions.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Jul 06 17:37:35 2011 -0400 (2011-07-06)
changeset 3332 e0af1cba759f
parent 3233 21dbe3d3f620
child 3719 eda1f57e79b0
permissions -rw-r--r--
Fixed TF2 OnConditionAdded/Removed forwards to catch all cases (bug 4982, 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@3332
    36
#include <iplayerinfo.h>
skamonkey@3332
    37
skamonkey@3332
    38
typedef uint64_t condflags_t;
skamonkey@3332
    39
skamonkey@3332
    40
condflags_t g_PlayerConds[MAXPLAYERS+1];
skamonkey@3233
    41
skamonkey@3233
    42
IForward *g_addCondForward = NULL;
skamonkey@3233
    43
IForward *g_removeCondForward = NULL;
skamonkey@3233
    44
skamonkey@3233
    45
int playerCondOffset = -1;
skamonkey@3332
    46
int playerCondExOffset = -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@3332
    53
	uint32_t playerCond   =  *(uint32_t *)((intptr_t)pPlayer + playerCondOffset);
skamonkey@3332
    54
	uint32_t condBits     =  *(uint32_t *)((intptr_t)pPlayer + conditionBitsOffset);
skamonkey@3332
    55
	uint32_t playerCondEx =  *(uint32_t *)((intptr_t)pPlayer + playerCondExOffset);
skamonkey@3233
    56
skamonkey@3332
    57
	uint64_t playerCondExAdj = playerCondEx;
skamonkey@3332
    58
	playerCondExAdj <<= 32;
skamonkey@3233
    59
skamonkey@3332
    60
	return playerCond|condBits|playerCondExAdj;
skamonkey@3233
    61
}
skamonkey@3233
    62
skamonkey@3332
    63
void Conditions_OnGameFrame(bool simulating)
skamonkey@3233
    64
{
skamonkey@3332
    65
	if (!simulating)
skamonkey@3233
    66
		return;
skamonkey@3233
    67
skamonkey@3332
    68
	condflags_t oldconds;
skamonkey@3332
    69
	condflags_t newconds;
skamonkey@3332
    70
	
skamonkey@3332
    71
	condflags_t addedconds;
skamonkey@3332
    72
	condflags_t removedconds;
skamonkey@3233
    73
skamonkey@3332
    74
	int maxClients = gpGlobals->maxClients;
skamonkey@3332
    75
	for (int i = 1; i <= maxClients; i++)
skamonkey@3233
    76
	{
skamonkey@3332
    77
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
skamonkey@3332
    78
		if (!pPlayer || !pPlayer->IsInGame())
skamonkey@3332
    79
			continue;
skamonkey@3332
    80
skamonkey@3332
    81
		IPlayerInfo *info = pPlayer->GetPlayerInfo();
skamonkey@3332
    82
		if (info->IsHLTV() || info->IsReplay())
skamonkey@3332
    83
			continue;
skamonkey@3233
    84
skamonkey@3332
    85
		CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
skamonkey@3332
    86
		oldconds = g_PlayerConds[i];
skamonkey@3332
    87
		newconds = GetPlayerConds(pEntity);
skamonkey@3332
    88
skamonkey@3332
    89
		if (oldconds == newconds)
skamonkey@3332
    90
			continue;
skamonkey@3332
    91
skamonkey@3332
    92
		addedconds = newconds &~ oldconds;
skamonkey@3332
    93
		removedconds = oldconds &~ newconds;
skamonkey@3332
    94
skamonkey@3332
    95
		int j;
skamonkey@3332
    96
		condflags_t bit;
skamonkey@3233
    97
skamonkey@3332
    98
		for (j = 0; (bit = ((condflags_t)1 << j)) <= addedconds; j++)
skamonkey@3332
    99
		{
skamonkey@3332
   100
			if ((addedconds & bit) == bit)
skamonkey@3332
   101
			{
skamonkey@3332
   102
				g_addCondForward->PushCell(i);
skamonkey@3332
   103
				g_addCondForward->PushCell(j);
skamonkey@3332
   104
				g_addCondForward->Execute(NULL, NULL);
skamonkey@3332
   105
			}
skamonkey@3332
   106
		}
skamonkey@3332
   107
skamonkey@3332
   108
		for (j = 0; (bit = ((condflags_t)1 << j)) <= removedconds; j++)
skamonkey@3332
   109
		{
skamonkey@3332
   110
			if ((removedconds & bit) == bit)
skamonkey@3332
   111
			{
skamonkey@3332
   112
				g_removeCondForward->PushCell(i);
skamonkey@3332
   113
				g_removeCondForward->PushCell(j);
skamonkey@3332
   114
				g_removeCondForward->Execute(NULL, NULL);
skamonkey@3332
   115
			}
skamonkey@3332
   116
		}
skamonkey@3332
   117
skamonkey@3332
   118
		g_PlayerConds[i] = newconds;
skamonkey@3233
   119
	}
skamonkey@3233
   120
}
skamonkey@3233
   121
skamonkey@3332
   122
bool InitialiseConditionChecks()
skamonkey@3233
   123
{
skamonkey@3233
   124
	sm_sendprop_info_t prop;
skamonkey@3233
   125
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop))
skamonkey@3233
   126
	{
skamonkey@3233
   127
		g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset");
skamonkey@3233
   128
		return false;
skamonkey@3233
   129
	}
skamonkey@3233
   130
	
skamonkey@3233
   131
	playerCondOffset = prop.actual_offset;
skamonkey@3233
   132
	
skamonkey@3233
   133
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop))
skamonkey@3233
   134
	{
skamonkey@3233
   135
		g_pSM->LogError(myself, "Failed to find _condition_bits prop offset");
skamonkey@3233
   136
		return false;
skamonkey@3233
   137
	}
skamonkey@3233
   138
skamonkey@3233
   139
	conditionBitsOffset = prop.actual_offset;
skamonkey@3233
   140
skamonkey@3332
   141
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx", &prop))
skamonkey@3332
   142
	{
skamonkey@3332
   143
		g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx prop offset");
skamonkey@3233
   144
		return false;
skamonkey@3332
   145
	}
skamonkey@3332
   146
	
skamonkey@3332
   147
	playerCondExOffset = prop.actual_offset;
skamonkey@3233
   148
skamonkey@3332
   149
	if (playerCondOffset == -1 || playerCondExOffset == -1 || conditionBitsOffset == -1)
skamonkey@3332
   150
		return false;
skamonkey@3332
   151
	
skamonkey@3332
   152
	int maxClients = gpGlobals->maxClients;
skamonkey@3332
   153
	for (int i = 1; i <= maxClients; i++)
skamonkey@3233
   154
	{
skamonkey@3332
   155
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
skamonkey@3332
   156
		if (!pPlayer || !pPlayer->IsInGame())
skamonkey@3332
   157
			continue;
skamonkey@3332
   158
skamonkey@3332
   159
		g_PlayerConds[i] = GetPlayerConds(gamehelpers->ReferenceToEntity(i));
skamonkey@3233
   160
	}
skamonkey@3233
   161
skamonkey@3332
   162
	g_pSM->AddGameFrameHook(Conditions_OnGameFrame);
skamonkey@3233
   163
skamonkey@3233
   164
	return true;
skamonkey@3233
   165
}
skamonkey@3233
   166
skamonkey@3332
   167
void Conditions_OnClientPutInServer(int client)
skamonkey@3233
   168
{
skamonkey@3332
   169
	g_PlayerConds[client] = 0;
skamonkey@3233
   170
}
skamonkey@3233
   171
skamonkey@3332
   172
void RemoveConditionChecks()
skamonkey@3332
   173
{
skamonkey@3332
   174
	g_pSM->RemoveGameFrameHook(Conditions_OnGameFrame);
skamonkey@3332
   175
}
skamonkey@3332
   176