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