extensions/tf2/conditions.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jul 29 10:03:18 2013 -0400 (2013-07-29)
changeset 3960 d50b36dc6821
parent 3737 a4c5b91eef14
child 4000 da351d296ff8
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Team Fortress 2 Extension
     5  * Copyright (C) 2004-2011 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 #include "conditions.h"
    34 #include "util.h"
    35 
    36 #include <iplayerinfo.h>
    37 
    38 typedef uint64_t condflags_t;
    39 
    40 condflags_t g_PlayerConds[MAXPLAYERS+1];
    41 
    42 IForward *g_addCondForward = NULL;
    43 IForward *g_removeCondForward = NULL;
    44 
    45 int playerCondOffset = -1;
    46 int playerCondExOffset = -1;
    47 int conditionBitsOffset = -1;
    48 
    49 bool g_bIgnoreRemove;
    50 
    51 #define MAX_CONDS (sizeof(uint64_t) * 8)
    52 
    53 inline condflags_t GetPlayerConds(CBaseEntity *pPlayer)
    54 {
    55 	uint32_t playerCond   =  *(uint32_t *)((intptr_t)pPlayer + playerCondOffset);
    56 	uint32_t condBits     =  *(uint32_t *)((intptr_t)pPlayer + conditionBitsOffset);
    57 	uint32_t playerCondEx =  *(uint32_t *)((intptr_t)pPlayer + playerCondExOffset);
    58 
    59 	uint64_t playerCondExAdj = playerCondEx;
    60 	playerCondExAdj <<= 32;
    61 
    62 	return playerCond|condBits|playerCondExAdj;
    63 }
    64 
    65 void Conditions_OnGameFrame(bool simulating)
    66 {
    67 	if (!simulating)
    68 		return;
    69 
    70 	condflags_t oldconds;
    71 	condflags_t newconds;
    72 	
    73 	condflags_t addedconds;
    74 	condflags_t removedconds;
    75 
    76 	int maxClients = gpGlobals->maxClients;
    77 	for (int i = 1; i <= maxClients; i++)
    78 	{
    79 		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
    80 		if (!pPlayer || !pPlayer->IsInGame())
    81 			continue;
    82 
    83 		IPlayerInfo *info = pPlayer->GetPlayerInfo();
    84 		if (info->IsHLTV() || info->IsReplay())
    85 			continue;
    86 
    87 		CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
    88 		oldconds = g_PlayerConds[i];
    89 		newconds = GetPlayerConds(pEntity);
    90 
    91 		if (oldconds == newconds)
    92 			continue;
    93 
    94 		addedconds = newconds &~ oldconds;
    95 		removedconds = oldconds &~ newconds;
    96 
    97 		uint64_t j;
    98 		condflags_t bit;
    99 
   100 		for (j = 0; j < MAX_CONDS && (bit = ((condflags_t)1 << j)) <= addedconds; j++)
   101 		{
   102 			if ((addedconds & bit) == bit)
   103 			{
   104 				g_addCondForward->PushCell(i);
   105 				g_addCondForward->PushCell(j & 0xFFFFFFFF);
   106 				g_addCondForward->Execute(NULL, NULL);
   107 			}
   108 		}
   109 
   110 		for (j = 0; j < MAX_CONDS && (bit = ((condflags_t)1 << j)) <= removedconds; j++)
   111 		{
   112 			if ((removedconds & bit) == bit)
   113 			{
   114 				g_removeCondForward->PushCell(i);
   115 				g_removeCondForward->PushCell(j & 0xFFFFFFFF);
   116 				g_removeCondForward->Execute(NULL, NULL);
   117 			}
   118 		}
   119 
   120 		g_PlayerConds[i] = newconds;
   121 	}
   122 }
   123 
   124 bool InitialiseConditionChecks()
   125 {
   126 	sm_sendprop_info_t prop;
   127 	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCond", &prop))
   128 	{
   129 		g_pSM->LogError(myself, "Failed to find m_nPlayerCond prop offset");
   130 		return false;
   131 	}
   132 	
   133 	playerCondOffset = prop.actual_offset;
   134 	
   135 	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "_condition_bits", &prop))
   136 	{
   137 		g_pSM->LogError(myself, "Failed to find _condition_bits prop offset");
   138 		return false;
   139 	}
   140 
   141 	conditionBitsOffset = prop.actual_offset;
   142 
   143 	if (!gamehelpers->FindSendPropInfo("CTFPlayer", "m_nPlayerCondEx", &prop))
   144 	{
   145 		g_pSM->LogError(myself, "Failed to find m_nPlayerCondEx prop offset");
   146 		return false;
   147 	}
   148 	
   149 	playerCondExOffset = prop.actual_offset;
   150 
   151 	if (playerCondOffset == -1 || playerCondExOffset == -1 || conditionBitsOffset == -1)
   152 		return false;
   153 	
   154 	int maxClients = gpGlobals->maxClients;
   155 	for (int i = 1; i <= maxClients; i++)
   156 	{
   157 		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
   158 		if (!pPlayer || !pPlayer->IsInGame())
   159 			continue;
   160 
   161 		g_PlayerConds[i] = GetPlayerConds(gamehelpers->ReferenceToEntity(i));
   162 	}
   163 
   164 	g_pSM->AddGameFrameHook(Conditions_OnGameFrame);
   165 
   166 	return true;
   167 }
   168 
   169 void Conditions_OnClientPutInServer(int client)
   170 {
   171 	g_PlayerConds[client] = 0;
   172 }
   173 
   174 void RemoveConditionChecks()
   175 {
   176 	g_pSM->RemoveGameFrameHook(Conditions_OnGameFrame);
   177 }
   178