core/smn_entities.cpp
changeset 2879 16c4fb1d4836
parent 2748 ff29fb92953c
child 2913 141ce1313e4a
     1.1 --- a/core/smn_entities.cpp	Mon Dec 14 23:08:15 2009 -0800
     1.2 +++ b/core/smn_entities.cpp	Tue Dec 15 23:14:53 2009 -0800
     1.3 @@ -69,17 +69,36 @@
     1.4  
     1.5  inline edict_t *GetEdict(cell_t num)
     1.6  {
     1.7 +	edict_t *pEdict;
     1.8 +	if (!IndexToAThings(num, NULL, &pEdict))
     1.9 +	{
    1.10 +		return NULL;
    1.11 +	}
    1.12 +	
    1.13 +	return pEdict;
    1.14 +}
    1.15 +
    1.16 +inline CBaseEntity *GetEntity(cell_t num)
    1.17 +{
    1.18 +	CBaseEntity *pEntity;
    1.19 +	if (!IndexToAThings(num, &pEntity, NULL))
    1.20 +	{
    1.21 +		return NULL;
    1.22 +	}
    1.23 +
    1.24 +	return pEntity;
    1.25 +}
    1.26 +
    1.27 +/* Given an entity reference or index, fill out a CBaseEntity and/or edict.
    1.28 +   If lookup is successful, returns true and writes back the two parameters.
    1.29 +   If lookup fails, returns false and doesn't touch the params.  */
    1.30 +bool IndexToAThings(cell_t num, CBaseEntity **pEntData, edict_t **pEdictData)
    1.31 +{
    1.32  	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
    1.33  
    1.34  	if (!pEntity)
    1.35  	{
    1.36 -		return 0;
    1.37 -	}
    1.38 -
    1.39 -	edict_t *pEdict = ::BaseEntityToEdict(pEntity);
    1.40 -	if (!pEdict || pEdict->IsFree())
    1.41 -	{
    1.42 -		return NULL;
    1.43 +		return false;
    1.44  	}
    1.45  
    1.46  	int index = g_HL2.ReferenceToIndex(num);
    1.47 @@ -88,42 +107,35 @@
    1.48  		CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    1.49  		if (!pPlayer || !pPlayer->IsConnected())
    1.50  		{
    1.51 -			return NULL;
    1.52 +			return false;
    1.53  		}
    1.54  	}
    1.55  
    1.56 -	return pEdict;
    1.57 -}
    1.58 -
    1.59 -edict_t *GetEntity(cell_t num, CBaseEntity **pData)
    1.60 -{
    1.61 -	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
    1.62 -
    1.63 -	if (!pEntity)
    1.64 +	if (pEntData)
    1.65  	{
    1.66 -		return 0;
    1.67 +		*pEntData = pEntity;
    1.68  	}
    1.69  
    1.70 -	int index = g_HL2.ReferenceToIndex(num);
    1.71 -	if (index > 0 && index <= g_Players.GetMaxClients())
    1.72 +	if (pEdictData)
    1.73  	{
    1.74 -		CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    1.75 -		if (!pPlayer || !pPlayer->IsConnected())
    1.76 +		edict_t *pEdict = ::BaseEntityToEdict(pEntity);
    1.77 +		if (pEdict->IsFree())
    1.78  		{
    1.79 -			return NULL;
    1.80 +			pEdict = NULL;
    1.81  		}
    1.82 +
    1.83 +		*pEdictData = pEdict;
    1.84  	}
    1.85  
    1.86 -	*pData = pEntity;
    1.87 -
    1.88 -	edict_t *pEdict = ::BaseEntityToEdict(pEntity);
    1.89 -	if (!pEdict || pEdict->IsFree())
    1.90 -	{
    1.91 -		return NULL;
    1.92 -	}
    1.93 -
    1.94 -	return pEdict;
    1.95 +	return true;
    1.96  }
    1.97 +/*
    1.98 +  <predcrab> AThings is like guaranteed r+ by dvander though
    1.99 +  <predcrab> he wont even read the rest of the patch to nit stuff
   1.100 +  <Fyren> Like I wasn't going to r? you anyway!
   1.101 +  <predcrab> he still sees!
   1.102 +  <predcrab> he's everywhere
   1.103 +*/
   1.104  
   1.105  class VEmptyClass {};
   1.106  datamap_t *VGetDataDescMap(CBaseEntity *pThisPtr, int offset)
   1.107 @@ -312,8 +324,7 @@
   1.108  
   1.109  static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
   1.110  {
   1.111 -	CBaseEntity *pEntity;
   1.112 -	GetEntity(params[1], &pEntity);
   1.113 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.114  
   1.115  	if (!pEntity)
   1.116  	{
   1.117 @@ -342,9 +353,9 @@
   1.118  static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
   1.119  {
   1.120  	CBaseEntity *pEntity;
   1.121 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.122 +	edict_t *pEdict;
   1.123  
   1.124 -	if (!pEntity)
   1.125 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.126  	{
   1.127  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.128  	}
   1.129 @@ -386,8 +397,7 @@
   1.130  
   1.131  static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   1.132  {
   1.133 -	CBaseEntity *pEntity;
   1.134 -	GetEntity(params[1], &pEntity);
   1.135 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.136  
   1.137  	if (!pEntity)
   1.138  	{
   1.139 @@ -408,9 +418,9 @@
   1.140  static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   1.141  {
   1.142  	CBaseEntity *pEntity;
   1.143 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.144 +	edict_t *pEdict;
   1.145  
   1.146 -	if (!pEntity)
   1.147 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.148  	{
   1.149  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.150  	}
   1.151 @@ -433,8 +443,7 @@
   1.152  
   1.153  static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
   1.154  {
   1.155 -	CBaseEntity *pEntity;
   1.156 -	GetEntity(params[1], &pEntity);
   1.157 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.158  
   1.159  	if (!pEntity)
   1.160  	{
   1.161 @@ -462,9 +471,9 @@
   1.162  static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
   1.163  {
   1.164  	CBaseEntity *pEntity;
   1.165 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.166 +	edict_t *pEdict;
   1.167  
   1.168 -	if (!pEntity)
   1.169 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.170  	{
   1.171  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.172  	}
   1.173 @@ -495,8 +504,7 @@
   1.174  /* THIS GUY IS DEPRECATED. */
   1.175  static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   1.176  {
   1.177 -	CBaseEntity *pEntity;
   1.178 -	GetEntity(params[1], &pEntity);
   1.179 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.180  
   1.181  	if (!pEntity)
   1.182  	{
   1.183 @@ -532,7 +540,10 @@
   1.184  	edict_t *pStoredEdict;
   1.185  	CBaseEntity *pStoredEntity;
   1.186  
   1.187 -	pStoredEdict = GetEntity(index, &pStoredEntity);
   1.188 +	if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
   1.189 +	{
   1.190 +		return -1;
   1.191 +	}
   1.192  
   1.193  	if (pStoredEdict == NULL || pStoredEntity == NULL)
   1.194  	{
   1.195 @@ -556,8 +567,7 @@
   1.196  
   1.197  static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   1.198  {
   1.199 -	CBaseEntity *pEntity;
   1.200 -	GetEntity(params[1], &pEntity);
   1.201 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.202  
   1.203  	if (pEntity == NULL)
   1.204  	{
   1.205 @@ -580,9 +590,9 @@
   1.206  static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   1.207  {
   1.208  	CBaseEntity *pEntity;
   1.209 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.210 +	edict_t *pEdict;
   1.211  
   1.212 -	if (!pEntity)
   1.213 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.214  	{
   1.215  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.216  	}
   1.217 @@ -599,8 +609,7 @@
   1.218  	{
   1.219  		hndl.Set(NULL);
   1.220  	} else {
   1.221 -		CBaseEntity *pOther;
   1.222 -		GetEntity(params[3], &pOther);
   1.223 +		CBaseEntity *pOther = GetEntity(params[3]);
   1.224  
   1.225  		if (!pOther)
   1.226  		{
   1.227 @@ -622,9 +631,9 @@
   1.228  static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   1.229  {
   1.230  	CBaseEntity *pEntity;
   1.231 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.232 +	edict_t *pEdict;
   1.233  
   1.234 -	if (!pEntity)
   1.235 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.236  	{
   1.237  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.238  	}
   1.239 @@ -643,8 +652,7 @@
   1.240  	}
   1.241  	else
   1.242  	{
   1.243 -		CBaseEntity *pOther;
   1.244 -		GetEntity(params[3], &pOther);
   1.245 +		CBaseEntity *pOther = GetEntity(params[3]);
   1.246  
   1.247  		if (!pOther)
   1.248  		{
   1.249 @@ -752,7 +760,7 @@
   1.250  	datamap_t *pMap;
   1.251  	typedescription_t *td;
   1.252  	char *offset;
   1.253 -	GetEntity(params[1], &pEntity);
   1.254 +	pEntity = GetEntity(params[1]);
   1.255  
   1.256  	if (!pEntity)
   1.257  	{
   1.258 @@ -856,8 +864,7 @@
   1.259  
   1.260  static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
   1.261  {
   1.262 -	CBaseEntity *pEntity;
   1.263 -	GetEntity(params[1], &pEntity);
   1.264 +	CBaseEntity *pEntity = GetEntity(params[1]);
   1.265  
   1.266  	if (!pEntity)
   1.267  	{
   1.268 @@ -880,9 +887,9 @@
   1.269  static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
   1.270  {
   1.271  	CBaseEntity *pEntity;
   1.272 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.273 +	edict_t *pEdict;
   1.274  
   1.275 -	if (!pEntity)
   1.276 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.277  	{
   1.278  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.279  	}
   1.280 @@ -968,9 +975,7 @@
   1.281  	edict_t *pEdict;
   1.282  	int bit_count;
   1.283  
   1.284 -	pEdict = GetEntity(params[1], &pEntity);
   1.285 -
   1.286 -	if (!pEntity)
   1.287 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.288  	{
   1.289  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.290  	}
   1.291 @@ -1060,9 +1065,7 @@
   1.292  	edict_t *pEdict;
   1.293  	int bit_count;
   1.294  
   1.295 -	pEdict = GetEntity(params[1], &pEntity);
   1.296 -
   1.297 -	if (!pEntity)
   1.298 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.299  	{
   1.300  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.301  	}
   1.302 @@ -1150,9 +1153,7 @@
   1.303  	const char *class_name;
   1.304  	edict_t *pEdict;
   1.305  
   1.306 -	pEdict = GetEntity(params[1], &pEntity);
   1.307 -
   1.308 -	if (!pEntity)
   1.309 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.310  	{
   1.311  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.312  	}
   1.313 @@ -1221,9 +1222,7 @@
   1.314  	const char *class_name;
   1.315  	edict_t *pEdict;
   1.316  
   1.317 -	pEdict = GetEntity(params[1], &pEntity);
   1.318 -
   1.319 -	if (!pEntity)
   1.320 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.321  	{
   1.322  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.323  	}
   1.324 @@ -1297,9 +1296,7 @@
   1.325  	const char *class_name;
   1.326  	edict_t *pEdict;
   1.327  
   1.328 -	pEdict = GetEntity(params[1], &pEntity);
   1.329 -
   1.330 -	if (!pEntity)
   1.331 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.332  	{
   1.333  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.334  	}
   1.335 @@ -1367,9 +1364,7 @@
   1.336  	const char *class_name;
   1.337  	edict_t *pEdict;
   1.338  
   1.339 -	pEdict = GetEntity(params[1], &pEntity);
   1.340 -
   1.341 -	if (!pEntity)
   1.342 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.343  	{
   1.344  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.345  	}
   1.346 @@ -1431,8 +1426,7 @@
   1.347  	}
   1.348  	else
   1.349  	{
   1.350 -		CBaseEntity *pOther;
   1.351 -		GetEntity(params[4], &pOther);
   1.352 +		CBaseEntity *pOther = GetEntity(params[4]);
   1.353  
   1.354  		if (!pOther)
   1.355  		{
   1.356 @@ -1459,9 +1453,7 @@
   1.357  	const char *class_name;
   1.358  	edict_t *pEdict;
   1.359  	
   1.360 -	pEdict = GetEntity(params[1], &pEntity);
   1.361 -
   1.362 -	if (!pEntity)
   1.363 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.364  	{
   1.365  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.366  	}
   1.367 @@ -1537,9 +1529,7 @@
   1.368  	const char *class_name;
   1.369  	edict_t *pEdict;
   1.370  	
   1.371 -	pEdict = GetEntity(params[1], &pEntity);
   1.372 -
   1.373 -	if (!pEntity)
   1.374 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.375  	{
   1.376  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.377  	}
   1.378 @@ -1621,9 +1611,7 @@
   1.379  	edict_t *pEdict;
   1.380  	bool bIsStringIndex;
   1.381  	
   1.382 -	pEdict = GetEntity(params[1], &pEntity);
   1.383 -
   1.384 -	if (!pEntity)
   1.385 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.386  	{
   1.387  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.388  	}
   1.389 @@ -1711,9 +1699,9 @@
   1.390  	char *prop;
   1.391  	int offset;
   1.392  	int maxlen;
   1.393 -	edict_t *pEdict = GetEntity(params[1], &pEntity);
   1.394 +	edict_t *pEdict;
   1.395  
   1.396 -	if (!pEntity)
   1.397 +	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   1.398  	{
   1.399  		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   1.400  	}