extensions/sdktools/teamnatives.cpp
branchrefac-jit
changeset 2168 c24a6fa957db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sdktools/teamnatives.cpp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,177 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod SDKTools Extension
     1.8 + * Copyright (C) 2004-2008 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 <sh_vector.h>
    1.37 +
    1.38 +struct TeamInfo
    1.39 +{
    1.40 +	const char *ClassName;
    1.41 +	CBaseEntity *pEnt;
    1.42 +};
    1.43 +
    1.44 +SourceHook::CVector<TeamInfo> g_Teams;
    1.45 +
    1.46 +bool FindTeamEntities(SendTable *pTable, const char *name)
    1.47 +{
    1.48 +	if (strcmp(pTable->GetName(), name) == 0)
    1.49 +	{
    1.50 +		return true;
    1.51 +	}
    1.52 +
    1.53 +	int props = pTable->GetNumProps();
    1.54 +	SendProp *prop;
    1.55 +
    1.56 +	for (int i=0; i<props; i++)
    1.57 +	{
    1.58 +		prop = pTable->GetProp(i);
    1.59 +		if (prop->GetDataTable())
    1.60 +		{
    1.61 +			if (FindTeamEntities(prop->GetDataTable(), name))
    1.62 +			{
    1.63 +				return true;
    1.64 +			}
    1.65 +		}
    1.66 +	}
    1.67 +
    1.68 +	return false;
    1.69 +}
    1.70 +
    1.71 +void SDKTools::OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
    1.72 +{
    1.73 +	g_Teams.clear();
    1.74 +	g_Teams.resize(1);
    1.75 +
    1.76 +	for (int i=0; i<edictCount; i++)
    1.77 +	{
    1.78 +		edict_t *pEdict = engine->PEntityOfEntIndex(i);
    1.79 +		if (!pEdict || pEdict->IsFree())
    1.80 +		{
    1.81 +			continue;
    1.82 +		}
    1.83 +		if (!pEdict->GetNetworkable())
    1.84 +		{
    1.85 +			continue;
    1.86 +		}
    1.87 +
    1.88 +		ServerClass *pClass = pEdict->GetNetworkable()->GetServerClass();
    1.89 +		if (FindTeamEntities(pClass->m_pTable, "DT_Team"))
    1.90 +		{
    1.91 +			SendProp *pTeamNumProp = g_pGameHelpers->FindInSendTable(pClass->GetName(), "m_iTeamNum");
    1.92 +
    1.93 +			if (pTeamNumProp != NULL)
    1.94 +			{
    1.95 +				int offset = pTeamNumProp->GetOffset();
    1.96 +				CBaseEntity *pEnt = pEdict->GetUnknown()->GetBaseEntity();
    1.97 +				int TeamIndex = *(int *)((unsigned char *)pEnt + offset);
    1.98 +
    1.99 +				if (TeamIndex >= (int)g_Teams.size())
   1.100 +				{
   1.101 +					g_Teams.resize(TeamIndex+1);
   1.102 +				}
   1.103 +				g_Teams[TeamIndex].ClassName = pClass->GetName();
   1.104 +				g_Teams[TeamIndex].pEnt = pEnt;
   1.105 +			}
   1.106 +		}
   1.107 +	}
   1.108 +}
   1.109 +
   1.110 +static cell_t GetTeamCount(IPluginContext *pContext, const cell_t *params)
   1.111 +{
   1.112 +	return g_Teams.size();
   1.113 +}
   1.114 +
   1.115 +static cell_t GetTeamName(IPluginContext *pContext, const cell_t *params)
   1.116 +{
   1.117 +	int teamindex = params[1];
   1.118 +	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
   1.119 +	{
   1.120 +		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
   1.121 +	}
   1.122 +
   1.123 +	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_szTeamname")->GetOffset();
   1.124 +	char *name = (char *)((unsigned char *)g_Teams[teamindex].pEnt + offset);
   1.125 +
   1.126 +	pContext->StringToLocalUTF8(params[2], params[3], name, NULL);
   1.127 +
   1.128 +	return 1;
   1.129 +}
   1.130 +
   1.131 +static cell_t GetTeamScore(IPluginContext *pContext, const cell_t *params)
   1.132 +{
   1.133 +	int teamindex = params[1];
   1.134 +	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
   1.135 +	{
   1.136 +		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
   1.137 +	}
   1.138 +
   1.139 +	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_iScore")->GetOffset();
   1.140 +
   1.141 +	return *(int *)((unsigned char *)g_Teams[teamindex].pEnt + offset);
   1.142 +}
   1.143 +
   1.144 +static cell_t SetTeamScore(IPluginContext *pContext, const cell_t *params)
   1.145 +{
   1.146 +	int teamindex = params[1];
   1.147 +	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
   1.148 +	{
   1.149 +		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
   1.150 +	}
   1.151 +
   1.152 +	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_iScore")->GetOffset();
   1.153 +	*(int *)((unsigned char *)g_Teams[teamindex].pEnt + offset) = params[2];
   1.154 +
   1.155 +	return 1;
   1.156 +}
   1.157 +
   1.158 +static cell_t GetTeamClientCount(IPluginContext *pContext, const cell_t *params)
   1.159 +{
   1.160 +	int teamindex = params[1];
   1.161 +	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
   1.162 +	{
   1.163 +		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
   1.164 +	}
   1.165 +
   1.166 +	SendProp *pProp = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "\"player_array\"");
   1.167 +	ArrayLengthSendProxyFn fn = pProp->GetArrayLengthProxy();
   1.168 +
   1.169 +	return fn(g_Teams[teamindex].pEnt, 0);
   1.170 +}
   1.171 +
   1.172 +sp_nativeinfo_t g_TeamNatives[] = 
   1.173 +{
   1.174 +	{"GetTeamCount",			GetTeamCount},
   1.175 +	{"GetTeamName",				GetTeamName},
   1.176 +	{"GetTeamScore",			GetTeamScore},
   1.177 +	{"SetTeamScore",			SetTeamScore},
   1.178 +	{"GetTeamClientCount",		GetTeamClientCount},
   1.179 +	{NULL,						NULL}
   1.180 +};