extensions/sdktools/teamnatives.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sat Apr 12 23:03:30 2008 +0000 (2008-04-12)
changeset 1997 bc77ecae3bda
parent 1993 8c08aaff4d39
child 2274 80a5a7d4b259
permissions -rw-r--r--
fixed amb1586 for real
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod SDKTools Extension
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 *
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "extension.h"
ds@1952
    33
#include <sh_vector.h>
ds@1952
    34
ds@1952
    35
struct TeamInfo
ds@1952
    36
{
ds@1952
    37
	const char *ClassName;
ds@1952
    38
	CBaseEntity *pEnt;
ds@1952
    39
};
ds@1952
    40
ds@1952
    41
SourceHook::CVector<TeamInfo> g_Teams;
ds@1952
    42
ds@1952
    43
bool FindTeamEntities(SendTable *pTable, const char *name)
ds@1952
    44
{
faluco@1997
    45
	if (strcmp(pTable->GetName(), name) == 0)
faluco@1997
    46
	{
faluco@1997
    47
		return true;
faluco@1997
    48
	}
faluco@1997
    49
ds@1952
    50
	int props = pTable->GetNumProps();
ds@1952
    51
	SendProp *prop;
ds@1952
    52
ds@1952
    53
	for (int i=0; i<props; i++)
ds@1952
    54
	{
ds@1952
    55
		prop = pTable->GetProp(i);
ds@1952
    56
		if (prop->GetDataTable())
ds@1952
    57
		{
ds@1952
    58
			if (FindTeamEntities(prop->GetDataTable(), name))
ds@1952
    59
			{
ds@1952
    60
				return true;
ds@1952
    61
			}
ds@1952
    62
		}
ds@1952
    63
	}
ds@1952
    64
ds@1952
    65
	return false;
ds@1952
    66
}
ds@1952
    67
faluco@1993
    68
void SDKTools::OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
ds@1952
    69
{
ds@1952
    70
	g_Teams.clear();
ds@1952
    71
	g_Teams.resize(1);
ds@1952
    72
ds@1952
    73
	for (int i=0; i<edictCount; i++)
ds@1952
    74
	{
ds@1952
    75
		edict_t *pEdict = engine->PEntityOfEntIndex(i);
ds@1952
    76
		if (!pEdict || pEdict->IsFree())
ds@1952
    77
		{
ds@1952
    78
			continue;
ds@1952
    79
		}
ds@1952
    80
		if (!pEdict->GetNetworkable())
ds@1952
    81
		{
ds@1952
    82
			continue;
ds@1952
    83
		}
ds@1952
    84
ds@1952
    85
		ServerClass *pClass = pEdict->GetNetworkable()->GetServerClass();
ds@1952
    86
		if (FindTeamEntities(pClass->m_pTable, "DT_Team"))
ds@1952
    87
		{
ds@1952
    88
			SendProp *pTeamNumProp = g_pGameHelpers->FindInSendTable(pClass->GetName(), "m_iTeamNum");
ds@1952
    89
ds@1952
    90
			if (pTeamNumProp != NULL)
ds@1952
    91
			{
ds@1952
    92
				int offset = pTeamNumProp->GetOffset();
ds@1952
    93
				CBaseEntity *pEnt = pEdict->GetUnknown()->GetBaseEntity();
ds@1952
    94
				int TeamIndex = *(int *)((unsigned char *)pEnt + offset);
ds@1952
    95
ds@1952
    96
				if (TeamIndex >= (int)g_Teams.size())
ds@1952
    97
				{
ds@1952
    98
					g_Teams.resize(TeamIndex+1);
ds@1952
    99
				}
ds@1952
   100
				g_Teams[TeamIndex].ClassName = pClass->GetName();
ds@1952
   101
				g_Teams[TeamIndex].pEnt = pEnt;
ds@1952
   102
			}
ds@1952
   103
		}
ds@1952
   104
	}
ds@1952
   105
}
ds@1952
   106
ds@1952
   107
static cell_t GetTeamCount(IPluginContext *pContext, const cell_t *params)
ds@1952
   108
{
ds@1952
   109
	return g_Teams.size();
ds@1952
   110
}
ds@1952
   111
ds@1952
   112
static cell_t GetTeamName(IPluginContext *pContext, const cell_t *params)
ds@1952
   113
{
ds@1952
   114
	int teamindex = params[1];
faluco@1997
   115
	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
ds@1952
   116
	{
ds@1952
   117
		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
ds@1952
   118
	}
ds@1952
   119
ds@1952
   120
	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_szTeamname")->GetOffset();
ds@1952
   121
	char *name = (char *)((unsigned char *)g_Teams[teamindex].pEnt + offset);
ds@1952
   122
ds@1952
   123
	pContext->StringToLocalUTF8(params[2], params[3], name, NULL);
ds@1952
   124
ds@1952
   125
	return 1;
ds@1952
   126
}
ds@1952
   127
ds@1952
   128
static cell_t GetTeamScore(IPluginContext *pContext, const cell_t *params)
ds@1952
   129
{
ds@1952
   130
	int teamindex = params[1];
faluco@1997
   131
	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
ds@1952
   132
	{
ds@1952
   133
		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
ds@1952
   134
	}
ds@1952
   135
ds@1952
   136
	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_iScore")->GetOffset();
ds@1952
   137
ds@1952
   138
	return *(int *)((unsigned char *)g_Teams[teamindex].pEnt + offset);
ds@1952
   139
}
ds@1952
   140
ds@1952
   141
static cell_t SetTeamScore(IPluginContext *pContext, const cell_t *params)
ds@1952
   142
{
ds@1952
   143
	int teamindex = params[1];
faluco@1997
   144
	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
ds@1952
   145
	{
ds@1952
   146
		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
ds@1952
   147
	}
ds@1952
   148
ds@1952
   149
	static int offset = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "m_iScore")->GetOffset();
ds@1952
   150
	*(int *)((unsigned char *)g_Teams[teamindex].pEnt + offset) = params[2];
ds@1952
   151
ds@1952
   152
	return 1;
ds@1952
   153
}
ds@1952
   154
ds@1952
   155
static cell_t GetTeamClientCount(IPluginContext *pContext, const cell_t *params)
ds@1952
   156
{
ds@1952
   157
	int teamindex = params[1];
faluco@1997
   158
	if (!g_Teams[teamindex].ClassName || (teamindex > (int)g_Teams.size()))
ds@1952
   159
	{
ds@1952
   160
		pContext->ThrowNativeError("Team index %d is invalid", teamindex);
ds@1952
   161
	}
ds@1952
   162
ds@1952
   163
	SendProp *pProp = g_pGameHelpers->FindInSendTable(g_Teams[teamindex].ClassName, "\"player_array\"");
ds@1952
   164
	ArrayLengthSendProxyFn fn = pProp->GetArrayLengthProxy();
ds@1952
   165
ds@1952
   166
	return fn(g_Teams[teamindex].pEnt, 0);
ds@1952
   167
}
ds@1952
   168
ds@1952
   169
sp_nativeinfo_t g_TeamNatives[] = 
ds@1952
   170
{
ds@1952
   171
	{"GetTeamCount",			GetTeamCount},
ds@1952
   172
	{"GetTeamName",				GetTeamName},
ds@1952
   173
	{"GetTeamScore",			GetTeamScore},
ds@1952
   174
	{"SetTeamScore",			SetTeamScore},
ds@1952
   175
	{"GetTeamClientCount",		GetTeamClientCount},
ds@1952
   176
	{NULL,						NULL}
ds@1952
   177
};