extensions/tf2/conditions.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 08 22:02:42 2013 -0700 (2013-08-08)
changeset 3980 dc7dfcd855dc
parent 3737 a4c5b91eef14
child 4000 da351d296ff8
permissions -rw-r--r--
More Windows build fix.
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@3737
    38
typedef uint64_t condflags_t;
skamonkey@3332
    39
skamonkey@3737
    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@3737
    51
#define MAX_CONDS (sizeof(uint64_t) * 8)
skamonkey@3737
    52
skamonkey@3737
    53
inline condflags_t GetPlayerConds(CBaseEntity *pPlayer)
skamonkey@3233
    54
{
skamonkey@3737
    55
	uint32_t playerCond   =  *(uint32_t *)((intptr_t)pPlayer + playerCondOffset);
skamonkey@3737
    56
	uint32_t condBits     =  *(uint32_t *)((intptr_t)pPlayer + conditionBitsOffset);
skamonkey@3737
    57
	uint32_t playerCondEx =  *(uint32_t *)((intptr_t)pPlayer + playerCondExOffset);
skamonkey@3233
    58
skamonkey@3737
    59
	uint64_t playerCondExAdj = playerCondEx;
skamonkey@3737
    60
	playerCondExAdj <<= 32;
skamonkey@3737
    61
skamonkey@3737
    62
	return playerCond|condBits|playerCondExAdj;
skamonkey@3233
    63
}
skamonkey@3233
    64
skamonkey@3332
    65
void Conditions_OnGameFrame(bool simulating)
skamonkey@3233
    66
{
skamonkey@3332
    67
	if (!simulating)
skamonkey@3233
    68
		return;
skamonkey@3233
    69
skamonkey@3737
    70
	condflags_t oldconds;
skamonkey@3737
    71
	condflags_t newconds;
skamonkey@3332
    72
	
skamonkey@3737
    73
	condflags_t addedconds;
skamonkey@3737
    74
	condflags_t removedconds;
skamonkey@3233
    75
skamonkey@3332
    76
	int maxClients = gpGlobals->maxClients;
skamonkey@3737
    77
	for (int i = 1; i <= maxClients; i++)
skamonkey@3233
    78
	{
skamonkey@3332
    79
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
skamonkey@3332
    80
		if (!pPlayer || !pPlayer->IsInGame())
skamonkey@3332
    81
			continue;
skamonkey@3332
    82
skamonkey@3332
    83
		IPlayerInfo *info = pPlayer->GetPlayerInfo();
skamonkey@3332
    84
		if (info->IsHLTV() || info->IsReplay())
skamonkey@3332
    85
			continue;
skamonkey@3233
    86
skamonkey@3332
    87
		CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
skamonkey@3737
    88
		oldconds = g_PlayerConds[i];
skamonkey@3737
    89
		newconds = GetPlayerConds(pEntity);
skamonkey@3332
    90
skamonkey@3737
    91
		if (oldconds == newconds)
skamonkey@3332
    92
			continue;
skamonkey@3332
    93
skamonkey@3737
    94
		addedconds = newconds &~ oldconds;
skamonkey@3737
    95
		removedconds = oldconds &~ newconds;
skamonkey@3737
    96
skamonkey@3737
    97
		uint64_t j;
skamonkey@3737
    98
		condflags_t bit;
skamonkey@3737
    99
skamonkey@3737
   100
		for (j = 0; j < MAX_CONDS && (bit = ((condflags_t)1 << j)) <= addedconds; j++)
skamonkey@3720
   101
		{
skamonkey@3737
   102
			if ((addedconds & bit) == bit)
skamonkey@3737
   103
			{
skamonkey@3737
   104
				g_addCondForward->PushCell(i);
skamonkey@3737
   105
				g_addCondForward->PushCell(j & 0xFFFFFFFF);
skamonkey@3737
   106
				g_addCondForward->Execute(NULL, NULL);
skamonkey@3737
   107
			}
skamonkey@3720
   108
		}
skamonkey@3233
   109
skamonkey@3737
   110
		for (j = 0; j < MAX_CONDS && (bit = ((condflags_t)1 << j)) <= removedconds; j++)
skamonkey@3332
   111
		{
skamonkey@3737
   112
			if ((removedconds & bit) == bit)
skamonkey@3737
   113
			{
skamonkey@3737
   114
				g_removeCondForward->PushCell(i);
skamonkey@3737
   115
				g_removeCondForward->PushCell(j & 0xFFFFFFFF);
skamonkey@3737
   116
				g_removeCondForward->Execute(NULL, NULL);
skamonkey@3737
   117
			}
skamonkey@3332
   118
		}
skamonkey@3332
   119
skamonkey@3737
   120
		g_PlayerConds[i] = newconds;
skamonkey@3233
   121
	}
skamonkey@3233
   122
}
skamonkey@3233
   123
skamonkey@3332
   124
bool InitialiseConditionChecks()
skamonkey@3233
   125
{
skamonkey@3233
   126
	sm_sendprop_info_t prop;
skamonkey@3233
   127
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop))
skamonkey@3233
   128
	{
skamonkey@3233
   129
		g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset");
skamonkey@3233
   130
		return false;
skamonkey@3233
   131
	}
skamonkey@3233
   132
	
skamonkey@3233
   133
	playerCondOffset = prop.actual_offset;
skamonkey@3233
   134
	
skamonkey@3233
   135
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop))
skamonkey@3233
   136
	{
skamonkey@3233
   137
		g_pSM->LogError(myself, "Failed to find _condition_bits prop offset");
skamonkey@3233
   138
		return false;
skamonkey@3233
   139
	}
skamonkey@3233
   140
skamonkey@3233
   141
	conditionBitsOffset = prop.actual_offset;
skamonkey@3233
   142
skamonkey@3332
   143
	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx", &prop))
skamonkey@3332
   144
	{
skamonkey@3332
   145
		g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx prop offset");
skamonkey@3233
   146
		return false;
skamonkey@3332
   147
	}
skamonkey@3332
   148
	
skamonkey@3332
   149
	playerCondExOffset = prop.actual_offset;
skamonkey@3233
   150
skamonkey@3737
   151
	if (playerCondOffset == -1 || playerCondExOffset == -1 || conditionBitsOffset == -1)
skamonkey@3332
   152
		return false;
skamonkey@3332
   153
	
skamonkey@3332
   154
	int maxClients = gpGlobals->maxClients;
skamonkey@3332
   155
	for (int i = 1; i <= maxClients; i++)
skamonkey@3233
   156
	{
skamonkey@3332
   157
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
skamonkey@3332
   158
		if (!pPlayer || !pPlayer->IsInGame())
skamonkey@3332
   159
			continue;
skamonkey@3332
   160
skamonkey@3737
   161
		g_PlayerConds[i] = GetPlayerConds(gamehelpers->ReferenceToEntity(i));
skamonkey@3233
   162
	}
skamonkey@3233
   163
skamonkey@3332
   164
	g_pSM->AddGameFrameHook(Conditions_OnGameFrame);
skamonkey@3233
   165
skamonkey@3233
   166
	return true;
skamonkey@3233
   167
}
skamonkey@3233
   168
skamonkey@3332
   169
void Conditions_OnClientPutInServer(int client)
skamonkey@3233
   170
{
skamonkey@3737
   171
	g_PlayerConds[client] = 0;
skamonkey@3233
   172
}
skamonkey@3233
   173
skamonkey@3332
   174
void RemoveConditionChecks()
skamonkey@3332
   175
{
skamonkey@3332
   176
	g_pSM->RemoveGameFrameHook(Conditions_OnGameFrame);
skamonkey@3332
   177
}
skamonkey@3332
   178