core/smn_entities.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jan 22 23:18:27 2013 -0500 (2013-01-22)
changeset 3749 4dc3d723c490
parent 3712 ea253cc4fbf6
child 3762 0f6b4d692ed9
permissions -rw-r--r--
Triggering build against new mac libprotobuf.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 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 "sm_globals.h"
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "PlayerManager.h"
    36 #include "HalfLife2.h"
    37 #include <IGameConfigs.h>
    38 #include "sm_stringutil.h"
    39 #include "logic_bridge.h"
    40 
    41 // These values need to mirror the values in entity_prop_stocks
    42 #define ENTFLAG_ONGROUND		(1 << 0)
    43 #define ENTFLAG_DUCKING			(1 << 1)
    44 #define ENTFLAG_WATERJUMP		(1 << 2)
    45 #define ENTFLAG_ONTRAIN			(1 << 3)
    46 #define ENTFLAG_INRAIN			(1 << 4)
    47 #define ENTFLAG_FROZEN			(1 << 5)
    48 #define ENTFLAG_ATCONTROLS		(1 << 6)
    49 #define ENTFLAG_CLIENT			(1 << 7)
    50 #define ENTFLAG_FAKECLIENT		(1 << 8)
    51 #define ENTFLAG_INWATER			(1 << 9)
    52 #define ENTFLAG_FLY				(1 << 10)
    53 #define ENTFLAG_SWIM			(1 << 11)
    54 #define ENTFLAG_CONVEYOR		(1 << 12)
    55 #define ENTFLAG_NPC				(1 << 13)
    56 #define ENTFLAG_GODMODE			(1 << 14)
    57 #define ENTFLAG_NOTARGET		(1 << 15)
    58 #define ENTFLAG_AIMTARGET		(1 << 16)
    59 #define ENTFLAG_PARTIALGROUND	(1 << 17)
    60 #define ENTFLAG_STATICPROP		(1 << 18)
    61 #define ENTFLAG_GRAPHED			(1 << 19)
    62 #define ENTFLAG_GRENADE			(1 << 20)
    63 #define ENTFLAG_STEPMOVEMENT	(1 << 21)
    64 #define ENTFLAG_DONTTOUCH		(1 << 22)
    65 #define ENTFLAG_BASEVELOCITY	(1 << 23)
    66 #define ENTFLAG_WORLDBRUSH		(1 << 24)
    67 #define ENTFLAG_OBJECT			(1 << 25)
    68 #define ENTFLAG_KILLME			(1 << 26)
    69 #define ENTFLAG_ONFIRE			(1 << 27)
    70 #define ENTFLAG_DISSOLVING		(1 << 28)
    71 #define ENTFLAG_TRANSRAGDOLL	(1 << 29)
    72 #define ENTFLAG_UNBLOCKABLE_BY_PLAYER	(1 << 30)
    73 #define ENTFLAG_FREEZING		(1 << 31)
    74 #define ENTFLAG_EP2V_UNKNOWN1	(1 << 31)
    75 
    76 // Not defined in the sdk as we have no clue what it is
    77 #define FL_EP2V_UNKNOWN			(1 << 2)
    78 
    79 enum PropType
    80 {
    81 	Prop_Send = 0,
    82 	Prop_Data
    83 };
    84 
    85 enum PropFieldType
    86 {
    87 	PropField_Unsupported,		/**< The type is unsupported. */
    88 	PropField_Integer,			/**< Valid for SendProp and Data fields */
    89 	PropField_Float,			/**< Valid for SendProp and Data fields */
    90 	PropField_Entity,			/**< Valid for Data fields only (SendProp shows as int) */
    91 	PropField_Vector,			/**< Valid for SendProp and Data fields */
    92 	PropField_String,			/**< Valid for SendProp and Data fields */
    93 	PropField_String_T,			/**< Valid for Data fields.  Read only! */
    94 };
    95 
    96 inline edict_t *BaseEntityToEdict(CBaseEntity *pEntity)
    97 {
    98 	IServerUnknown *pUnk = (IServerUnknown *)pEntity;
    99 	IServerNetworkable *pNet = pUnk->GetNetworkable();
   100 
   101 	if (!pNet)
   102 	{
   103 		return NULL;
   104 	}
   105 
   106 	return pNet->GetEdict();
   107 }
   108 
   109 inline edict_t *GetEdict(cell_t num)
   110 {
   111 	edict_t *pEdict;
   112 	if (!IndexToAThings(num, NULL, &pEdict))
   113 	{
   114 		return NULL;
   115 	}
   116 	
   117 	return pEdict;
   118 }
   119 
   120 inline CBaseEntity *GetEntity(cell_t num)
   121 {
   122 	CBaseEntity *pEntity;
   123 	if (!IndexToAThings(num, &pEntity, NULL))
   124 	{
   125 		return NULL;
   126 	}
   127 
   128 	return pEntity;
   129 }
   130 
   131 /* Given an entity reference or index, fill out a CBaseEntity and/or edict.
   132    If lookup is successful, returns true and writes back the two parameters.
   133    If lookup fails, returns false and doesn't touch the params.  */
   134 bool IndexToAThings(cell_t num, CBaseEntity **pEntData, edict_t **pEdictData)
   135 {
   136 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(num);
   137 
   138 	if (!pEntity)
   139 	{
   140 		return false;
   141 	}
   142 
   143 	int index = g_HL2.ReferenceToIndex(num);
   144 	if (index > 0 && index <= g_Players.GetMaxClients())
   145 	{
   146 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   147 		if (!pPlayer || !pPlayer->IsConnected())
   148 		{
   149 			return false;
   150 		}
   151 	}
   152 
   153 	if (pEntData)
   154 	{
   155 		*pEntData = pEntity;
   156 	}
   157 
   158 	if (pEdictData)
   159 	{
   160 		edict_t *pEdict = ::BaseEntityToEdict(pEntity);
   161 		if (!pEdict || pEdict->IsFree())
   162 		{
   163 			pEdict = NULL;
   164 		}
   165 
   166 		*pEdictData = pEdict;
   167 	}
   168 
   169 	return true;
   170 }
   171 /*
   172   <predcrab> AThings is like guaranteed r+ by dvander though
   173   <predcrab> he wont even read the rest of the patch to nit stuff
   174   <Fyren> Like I wasn't going to r? you anyway!
   175   <predcrab> he still sees!
   176   <predcrab> he's everywhere
   177 */
   178 
   179 class VEmptyClass {};
   180 datamap_t *VGetDataDescMap(CBaseEntity *pThisPtr, int offset)
   181 {
   182 	void **this_ptr = *reinterpret_cast<void ***>(&pThisPtr);
   183 	void **vtable = *reinterpret_cast<void ***>(pThisPtr);
   184 	void *vfunc = vtable[offset];
   185 
   186 	union
   187 	{
   188 		datamap_t *(VEmptyClass::*mfpnew)();
   189 #ifndef PLATFORM_POSIX
   190 		void *addr;
   191 	} u;
   192 	u.addr = vfunc;
   193 #else
   194 		struct  
   195 		{
   196 			void *addr;
   197 			intptr_t adjustor;
   198 		} s;
   199 	} u;
   200 	u.s.addr = vfunc;
   201 	u.s.adjustor = 0;
   202 #endif
   203 
   204 	return (datamap_t *)(reinterpret_cast<VEmptyClass *>(this_ptr)->*u.mfpnew)();
   205 }
   206 
   207 inline datamap_t *CBaseEntity_GetDataDescMap(CBaseEntity *pEntity)
   208 {
   209 	int offset;
   210 
   211 	if (!g_pGameConf->GetOffset("GetDataDescMap", &offset) || !offset)
   212 	{
   213 		return NULL;
   214 	}
   215 
   216 	return VGetDataDescMap(pEntity, offset);
   217 }
   218 
   219 /* :TODO: Move this! */
   220 datamap_t *CHalfLife2::GetDataMap(CBaseEntity *pEntity)
   221 {
   222 	return CBaseEntity_GetDataDescMap(pEntity);
   223 }
   224 
   225 static cell_t GetMaxEntities(IPluginContext *pContext, const cell_t *params)
   226 {
   227 	return gpGlobals->maxEntities;
   228 }
   229 
   230 static cell_t CreateEdict(IPluginContext *pContext, const cell_t *params)
   231 {
   232 	edict_t *pEdict = engine->CreateEdict();
   233 
   234 	if (!pEdict)
   235 	{
   236 		return 0;
   237 	}
   238 
   239 	return IndexOfEdict(pEdict);
   240 }
   241 
   242 static cell_t RemoveEdict(IPluginContext *pContext, const cell_t *params)
   243 {
   244 	edict_t *pEdict = GetEdict(params[1]);
   245 
   246 	if (!pEdict)
   247 	{
   248 		return pContext->ThrowNativeError("Edict %d (%d) is not a valid edict", g_HL2.ReferenceToIndex(params[1]), params[1]);
   249 	}
   250 
   251 	engine->RemoveEdict(pEdict);
   252 
   253 	return 1;
   254 }
   255 
   256 static cell_t IsValidEdict(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	edict_t *pEdict = GetEdict(params[1]);
   259 
   260 	if (!pEdict)
   261 	{
   262 		return 0;
   263 	}
   264 
   265 	/* Shouldn't be necessary... not sure though */
   266 	return pEdict->IsFree() ? 0 : 1;
   267 }
   268 
   269 static cell_t IsValidEntity(IPluginContext *pContext, const cell_t *params)
   270 {
   271 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
   272 
   273 	return (pEntity != NULL) ? 1 : 0;
   274 }
   275 
   276 static cell_t IsEntNetworkable(IPluginContext *pContext, const cell_t *params)
   277 {
   278 	edict_t *pEdict = GetEdict(params[1]);
   279 
   280 	if (!pEdict)
   281 	{
   282 		return 0;
   283 	}
   284 
   285 	return (pEdict->GetNetworkable() != NULL) ? 1 : 0;
   286 }
   287 
   288 static cell_t GetEntityCount(IPluginContext *pContext, const cell_t *params)
   289 {
   290 	return engine->GetEntityCount();
   291 }
   292 
   293 static cell_t GetEdictFlags(IPluginContext *pContext, const cell_t *params)
   294 {
   295 	edict_t *pEdict = GetEdict(params[1]);
   296 
   297 	if (!pEdict)
   298 	{
   299 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   300 	}
   301 
   302 	return pEdict->m_fStateFlags;
   303 }
   304 
   305 static cell_t SetEdictFlags(IPluginContext *pContext, const cell_t *params)
   306 {
   307 	edict_t *pEdict = GetEdict(params[1]);
   308 
   309 	if (!pEdict)
   310 	{
   311 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   312 	}
   313 
   314 	pEdict->m_fStateFlags = params[2];
   315 
   316 	return 1;
   317 }
   318 
   319 static cell_t GetEdictClassname(IPluginContext *pContext, const cell_t *params)
   320 {
   321 	edict_t *pEdict = GetEdict(params[1]);
   322 
   323 	if (!pEdict)
   324 	{
   325 		return pContext->ThrowNativeError("Invalid edict (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   326 	}
   327 
   328 	const char *cls = g_HL2.GetEntityClassname(pEdict);
   329 
   330 	if (!cls || cls[0] == '\0')
   331 	{
   332 		return 0;
   333 	}
   334 
   335 	pContext->StringToLocal(params[2], params[3], cls);
   336 
   337 	return 1;
   338 }
   339 
   340 static cell_t GetEntityNetClass(IPluginContext *pContext, const cell_t *params)
   341 {
   342 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
   343 
   344 	IServerUnknown *pUnk = (IServerUnknown *)pEntity;
   345 
   346 	if (!pEntity)
   347 	{
   348 		return pContext->ThrowNativeError("Invalid entity (%d - %d)", g_HL2.ReferenceToIndex(params[1]), params[1]);
   349 	}
   350 
   351 	IServerNetworkable *pNet = pUnk->GetNetworkable();
   352 	if (!pNet)
   353 	{
   354 		return 0;
   355 	}
   356 
   357 	ServerClass *pClass = pNet->GetServerClass();
   358 
   359 	pContext->StringToLocal(params[2], params[3], pClass->GetName());
   360 
   361 	return 1;
   362 }
   363 
   364 static cell_t GetEntData(IPluginContext *pContext, const cell_t *params)
   365 {
   366 	CBaseEntity *pEntity = GetEntity(params[1]);
   367 
   368 	if (!pEntity)
   369 	{
   370 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   371 	}
   372 
   373 	int offset = params[2];
   374 	if (offset <= 0 || offset > 32768)
   375 	{
   376 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   377 	}
   378 
   379 	switch (params[3])
   380 	{
   381 	case 4:
   382 		return *(int *)((uint8_t *)pEntity + offset);
   383 	case 2:
   384 		return *(short *)((uint8_t *)pEntity + offset);
   385 	case 1:
   386 		return *((uint8_t *)pEntity + offset);
   387 	default:
   388 		return pContext->ThrowNativeError("Integer size %d is invalid", params[3]);
   389 	}
   390 }
   391 
   392 static cell_t SetEntData(IPluginContext *pContext, const cell_t *params)
   393 {
   394 	CBaseEntity *pEntity;
   395 	edict_t *pEdict;
   396 
   397 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   398 	{
   399 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   400 	}
   401 
   402 	int offset = params[2];
   403 	if (offset <= 0 || offset > 32768)
   404 	{
   405 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   406 	}
   407 
   408 	if (params[5] && (pEdict != NULL))
   409 	{
   410 		g_HL2.SetEdictStateChanged(pEdict, offset);
   411 	}
   412 
   413 	switch (params[4])
   414 	{
   415 	case 4:
   416 		{
   417 			*(int *)((uint8_t *)pEntity + offset) = params[3];
   418 			break;
   419 		}
   420 	case 2:
   421 		{
   422 			*(short *)((uint8_t *)pEntity + offset) = params[3];
   423 			break;
   424 		}
   425 	case 1:
   426 		{
   427 			*((uint8_t *)pEntity + offset) = params[3];
   428 			break;
   429 		}
   430 	default:
   431 		return pContext->ThrowNativeError("Integer size %d is invalid", params[4]);
   432 	}
   433 
   434 	return 1;
   435 }
   436 
   437 static cell_t GetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   438 {
   439 	CBaseEntity *pEntity = GetEntity(params[1]);
   440 
   441 	if (!pEntity)
   442 	{
   443 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   444 	}
   445 
   446 	int offset = params[2];
   447 	if (offset <= 0 || offset > 32768)
   448 	{
   449 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   450 	}
   451 
   452 	float f = *(float *)((uint8_t *)pEntity + offset);
   453 
   454 	return sp_ftoc(f);
   455 }
   456 
   457 static cell_t SetEntDataFloat(IPluginContext *pContext, const cell_t *params)
   458 {
   459 	CBaseEntity *pEntity;
   460 	edict_t *pEdict;
   461 
   462 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   463 	{
   464 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   465 	}
   466 
   467 	int offset = params[2];
   468 	if (offset <= 0 || offset > 32768)
   469 	{
   470 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   471 	}
   472 
   473 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[3]);
   474 
   475 	if (params[4] && (pEdict != NULL))
   476 	{
   477 		g_HL2.SetEdictStateChanged(pEdict, offset);
   478 	}
   479 
   480 	return 1;
   481 }
   482 
   483 static cell_t GetEntDataVector(IPluginContext *pContext, const cell_t *params)
   484 {
   485 	CBaseEntity *pEntity = GetEntity(params[1]);
   486 
   487 	if (!pEntity)
   488 	{
   489 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   490 	}
   491 
   492 	int offset = params[2];
   493 	if (offset <= 0 || offset > 32768)
   494 	{
   495 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   496 	}
   497 
   498 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
   499 
   500 	cell_t *vec;
   501 	pContext->LocalToPhysAddr(params[3], &vec);
   502 
   503 	vec[0] = sp_ftoc(v->x);
   504 	vec[1] = sp_ftoc(v->y);
   505 	vec[2] = sp_ftoc(v->z);
   506 
   507 	return 1;
   508 }
   509 
   510 static cell_t SetEntDataVector(IPluginContext *pContext, const cell_t *params)
   511 {
   512 	CBaseEntity *pEntity;
   513 	edict_t *pEdict;
   514 
   515 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   516 	{
   517 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   518 	}
   519 
   520 	int offset = params[2];
   521 	if (offset <= 0 || offset > 32768)
   522 	{
   523 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   524 	}
   525 
   526 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
   527 
   528 	cell_t *vec;
   529 	pContext->LocalToPhysAddr(params[3], &vec);
   530 
   531 	v->x = sp_ctof(vec[0]);
   532 	v->y = sp_ctof(vec[1]);
   533 	v->z = sp_ctof(vec[2]);
   534 
   535 	if (params[4] && (pEdict != NULL))
   536 	{
   537 		g_HL2.SetEdictStateChanged(pEdict, offset);
   538 	}
   539 
   540 	return 1;
   541 }
   542 
   543 /* THIS GUY IS DEPRECATED. */
   544 static cell_t GetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   545 {
   546 	CBaseEntity *pEntity = GetEntity(params[1]);
   547 
   548 	if (!pEntity)
   549 	{
   550 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   551 	}
   552 
   553 	int offset = params[2];
   554 	if (offset <= 0 || offset > 32768)
   555 	{
   556 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   557 	}
   558 
   559 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   560 
   561 	if (!hndl.IsValid())
   562 	{
   563 		return 0;
   564 	}
   565 
   566 	int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
   567 	return g_HL2.ReferenceToBCompatRef(ref);
   568 }
   569 
   570 int CheckBaseHandle(CBaseHandle &hndl)
   571 {
   572 	if (!hndl.IsValid())
   573 	{
   574 		return -1;
   575 	}
   576 
   577 	int index = hndl.GetEntryIndex();
   578 
   579 	edict_t *pStoredEdict;
   580 	CBaseEntity *pStoredEntity;
   581 
   582 	if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
   583 	{
   584 		return -1;
   585 	}
   586 
   587 	if (pStoredEdict == NULL || pStoredEntity == NULL)
   588 	{
   589 		return -1;
   590 	}
   591 
   592 	IServerEntity *pSE = pStoredEdict->GetIServerEntity();
   593 
   594 	if (pSE == NULL)
   595 	{
   596 		return -1;
   597 	}
   598 
   599 	if (pSE->GetRefEHandle() != hndl)
   600 	{
   601 		return -1;
   602 	}
   603 
   604 	return index;
   605 }
   606 
   607 static cell_t GetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   608 {
   609 	CBaseEntity *pEntity = GetEntity(params[1]);
   610 
   611 	if (pEntity == NULL)
   612 	{
   613 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   614 	}
   615 
   616 	int offset = params[2];
   617 	if (offset <= 0 || offset > 32768)
   618 	{
   619 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   620 	}
   621 
   622 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   623 	CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(hndl.GetEntryIndex());
   624 
   625 	if (!pHandleEntity || hndl != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
   626 		return -1;
   627 
   628 	return g_HL2.EntityToBCompatRef(pHandleEntity);
   629 }
   630 
   631 /* THIS GUY IS DEPRECATED. */
   632 static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   633 {
   634 	CBaseEntity *pEntity;
   635 	edict_t *pEdict;
   636 
   637 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   638 	{
   639 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   640 	}
   641 
   642 	int offset = params[2];
   643 	if (offset <= 0 || offset > 32768)
   644 	{
   645 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   646 	}
   647 
   648 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   649 
   650 	if (params[3] == 0 || (unsigned)params[3] == INVALID_EHANDLE_INDEX)
   651 	{
   652 		hndl.Set(NULL);
   653 	} else {
   654 		CBaseEntity *pOther = GetEntity(params[3]);
   655 
   656 		if (!pOther)
   657 		{
   658 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   659 		}
   660 
   661 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   662 		hndl.Set(pHandleEnt);
   663 	}
   664 
   665 	if (params[4] && (pEdict != NULL))
   666 	{
   667 		g_HL2.SetEdictStateChanged(pEdict, offset);
   668 	}
   669 
   670 	return 1;
   671 }
   672 
   673 static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   674 {
   675 	CBaseEntity *pEntity;
   676 	edict_t *pEdict;
   677 
   678 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   679 	{
   680 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   681 	}
   682 
   683 	int offset = params[2];
   684 	if (offset <= 0 || offset > 32768)
   685 	{
   686 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   687 	}
   688 
   689 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   690 
   691 	if ((unsigned)params[3] == INVALID_EHANDLE_INDEX)
   692 	{
   693 		hndl.Set(NULL);
   694 	}
   695 	else
   696 	{
   697 		CBaseEntity *pOther = GetEntity(params[3]);
   698 
   699 		if (!pOther)
   700 		{
   701 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   702 		}
   703 
   704 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   705 		hndl.Set(pHandleEnt);
   706 	}
   707 
   708 	if (params[4] && (pEdict != NULL))
   709 	{
   710 		g_HL2.SetEdictStateChanged(pEdict, offset);
   711 	}
   712 
   713 	return 1;
   714 }
   715 
   716 static cell_t ChangeEdictState(IPluginContext *pContext, const cell_t *params)
   717 {
   718 	edict_t *pEdict = GetEdict(params[1]);
   719 
   720 	if (!pEdict)
   721 	{
   722 		return pContext->ThrowNativeError("Edict %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   723 	}
   724 
   725 	g_HL2.SetEdictStateChanged(pEdict, params[2]);
   726 
   727 	return 1;
   728 }
   729 
   730 static cell_t FindSendPropOffs(IPluginContext *pContext, const cell_t *params)
   731 {
   732 	char *cls, *prop;
   733 	pContext->LocalToString(params[1], &cls);
   734 	pContext->LocalToString(params[2], &prop);
   735 
   736 	SendProp *pSend = g_HL2.FindInSendTable(cls, prop);
   737 
   738 	if (!pSend)
   739 	{
   740 		return -1;
   741 	}
   742 
   743 	return pSend->GetOffset();
   744 }
   745 
   746 static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
   747 {
   748 	char *cls, *prop;
   749 	sm_sendprop_info_t info;
   750 	cell_t *pType, *pBits, *pLocal;
   751 
   752 	pContext->LocalToString(params[1], &cls);
   753 	pContext->LocalToString(params[2], &prop);
   754 
   755 	if (!g_HL2.FindSendPropInfo(cls, prop, &info))
   756 	{
   757 		return -1;
   758 	}
   759 
   760 	pContext->LocalToPhysAddr(params[3], &pType);
   761 	pContext->LocalToPhysAddr(params[4], &pBits);
   762 	pContext->LocalToPhysAddr(params[5], &pLocal);
   763 
   764 	switch (info.prop->GetType())
   765 	{
   766 	case DPT_Int:
   767 		{
   768 			*pType = PropField_Integer;
   769 			break;
   770 		}
   771 	case DPT_Float:
   772 		{
   773 			*pType = PropField_Float;
   774 			break;
   775 		}
   776 	case DPT_String:
   777 		{
   778 			*pType = PropField_String;
   779 			break;
   780 		}
   781 	case DPT_Vector:
   782 		{
   783 			*pType = PropField_Vector;
   784 			break;
   785 		}
   786 	default:
   787 		{
   788 			*pType = PropField_Unsupported;
   789 			break;
   790 		}
   791 	}
   792 
   793 	*pBits = info.prop->m_nBits;
   794 	*pLocal = info.prop->GetOffset();
   795 
   796 	return info.actual_offset;
   797 }
   798 
   799 static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
   800 {
   801 	CBaseEntity *pEntity;
   802 	datamap_t *pMap;
   803 	typedescription_t *td;
   804 	char *offset;
   805 	pEntity = GetEntity(params[1]);
   806 
   807 	if (!pEntity)
   808 	{
   809 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   810 	}
   811 
   812 	if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
   813 	{
   814 		return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
   815 	}
   816 
   817 	pContext->LocalToString(params[2], &offset);
   818 	if ((td=g_HL2.FindInDataMap(pMap, offset)) == NULL)
   819 	{
   820 		return -1;
   821 	}
   822 
   823 	if (params[0] == 4)
   824 	{
   825 		cell_t *pType, *pSize;
   826 
   827 		pContext->LocalToPhysAddr(params[3], &pType);
   828 		pContext->LocalToPhysAddr(params[4], &pSize);
   829 
   830 		switch (td->fieldType)
   831 		{
   832 		case FIELD_TICK:
   833 		case FIELD_MODELINDEX:
   834 		case FIELD_MATERIALINDEX:
   835 		case FIELD_INTEGER:
   836 		case FIELD_COLOR32:
   837 			{
   838 				*pType = PropField_Integer;
   839 				*pSize = 32;
   840 				break;
   841 			}
   842 		case FIELD_VECTOR:
   843 		case FIELD_POSITION_VECTOR:
   844 			{
   845 				*pType = PropField_Vector;
   846 				*pSize = 12;
   847 				break;
   848 			}
   849 		case FIELD_SHORT:
   850 			{
   851 				*pType = PropField_Integer;
   852 				*pSize = 16;
   853 				break;
   854 			}
   855 		case FIELD_BOOLEAN:
   856 			{
   857 				*pType = PropField_Integer;
   858 				*pSize = 1;
   859 				break;
   860 			}
   861 		case FIELD_CHARACTER:
   862 			{
   863 				if (td->fieldSize == 1)
   864 				{
   865 					*pType = PropField_Integer;
   866 					*pSize = 8;
   867 				}
   868 				else
   869 				{
   870 					*pType = PropField_String;
   871 					*pSize = 8 * td->fieldSize;
   872 				}
   873 				break;
   874 			}
   875 		case FIELD_MODELNAME:
   876 		case FIELD_SOUNDNAME:
   877 		case FIELD_STRING:
   878 			{
   879 				*pSize = sizeof(string_t);
   880 				*pType = PropField_String_T;
   881 				break;
   882 			}
   883 		case FIELD_FLOAT:
   884 		case FIELD_TIME:
   885 			{
   886 				*pType = PropField_Float;
   887 				*pSize = 32;
   888 				break;
   889 			}
   890 		case FIELD_EHANDLE:
   891 			{
   892 				*pType = PropField_Entity;
   893 				*pSize = 32;
   894 				break;
   895 			}
   896 		default:
   897 			{
   898 				*pType = PropField_Unsupported;
   899 				*pSize = 0;
   900 			}
   901 		}
   902 	}
   903 
   904 	return GetTypeDescOffs(td);
   905 }
   906 
   907 static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
   908 {
   909 	CBaseEntity *pEntity = GetEntity(params[1]);
   910 
   911 	if (!pEntity)
   912 	{
   913 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   914 	}
   915 
   916 	int offset = params[2];
   917 	if (offset < 0 || offset > 32768)
   918 	{
   919 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   920 	}
   921 
   922 	size_t len;
   923 	char *src = (char *)((uint8_t *)pEntity + offset);
   924 	pContext->StringToLocalUTF8(params[3], params[4], src, &len);
   925 
   926 	return len;
   927 }
   928 
   929 static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
   930 {
   931 	CBaseEntity *pEntity;
   932 	edict_t *pEdict;
   933 
   934 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   935 	{
   936 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   937 	}
   938 
   939 	int offset = params[2];
   940 	if (offset < 0 || offset > 32768)
   941 	{
   942 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   943 	}
   944 
   945 	char *src;
   946 	char *dest = (char *)((uint8_t *)pEntity + offset);
   947 
   948 	pContext->LocalToString(params[3], &src);
   949 	size_t len = strncopy(dest, src, params[4]);
   950 
   951 	if (params[5] && (pEdict != NULL))
   952 	{
   953 		g_HL2.SetEdictStateChanged(pEdict, offset);
   954 	}
   955 
   956 	return len;
   957 }
   958 
   959 #define FIND_PROP_DATA(td) \
   960 	datamap_t *pMap; \
   961 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL) \
   962 	{ \
   963 		return pContext->ThrowNativeError("Could not retrieve datamap"); \
   964 	} \
   965 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL) \
   966 	{ \
   967 		const char *class_name = g_HL2.GetEntityClassname(pEntity); \
   968 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   969 			prop, \
   970 			params[1], \
   971 			((class_name) ? class_name : "")); \
   972 	}
   973 
   974 #define CHECK_SET_PROP_DATA_OFFSET() \
   975 	if (element < 0 || element >= td->fieldSize) \
   976 	{ \
   977 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
   978 			element, \
   979 			prop, \
   980 			td->fieldSize); \
   981 	} \
   982 	\
   983 	offset = GetTypeDescOffs(td) + (element * (td->fieldSizeInBytes / td->fieldSize));
   984 
   985 #define FIND_PROP_SEND(type, type_name) \
   986 	sm_sendprop_info_t info;\
   987 	IServerUnknown *pUnk = (IServerUnknown *)pEntity; \
   988 	IServerNetworkable *pNet = pUnk->GetNetworkable(); \
   989 	if (!pNet) \
   990 	{ \
   991 		return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]); \
   992 	} \
   993 	if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info)) \
   994 	{ \
   995 		const char *class_name = g_HL2.GetEntityClassname(pEntity); \
   996 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   997 			prop, \
   998 			params[1], \
   999 			((class_name) ? class_name : "")); \
  1000 	} \
  1001 	\
  1002 	offset = info.actual_offset; \
  1003 	bit_count = info.prop->m_nBits; \
  1004 	\
  1005 	switch (info.prop->GetType()) \
  1006 	{ \
  1007 	case type: \
  1008 		{ \
  1009 			if (element != 0) \
  1010 			{ \
  1011 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.", \
  1012 					prop, \
  1013 					element); \
  1014 			} \
  1015 			break; \
  1016 		} \
  1017 	case DPT_DataTable: \
  1018 		{ \
  1019 			SendProp *pProp; \
  1020 			FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name); \
  1021 			\
  1022 			offset += pProp->GetOffset(); \
  1023 			bit_count = pProp->m_nBits; \
  1024 			break; \
  1025 		} \
  1026 	default: \
  1027 		{ \
  1028 			return pContext->ThrowNativeError("SendProp %s type is not " type_name " (%d != %d)", \
  1029 				prop, \
  1030 				info.prop->GetType(), \
  1031 				type); \
  1032 		} \
  1033 	} \
  1034 
  1035 #define FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name) \
  1036 	SendTable *pTable = info.prop->GetDataTable(); \
  1037 	if (!pTable) \
  1038 	{ \
  1039 		return pContext->ThrowNativeError("Error looking up DataTable for prop %s", \
  1040 			prop); \
  1041 	} \
  1042 	\
  1043 	int elementCount = pTable->GetNumProps(); \
  1044 	if (element < 0 || element >= elementCount) \
  1045 	{ \
  1046 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
  1047 			element, \
  1048 			prop, \
  1049 			elementCount); \
  1050 	} \
  1051 	\
  1052 	pProp = pTable->GetProp(element); \
  1053 	if (pProp->GetType() != type) \
  1054 	{ \
  1055 		return pContext->ThrowNativeError("SendProp %s type is not " type_name " ([%d,%d] != %d)", \
  1056 			prop, \
  1057 			info.prop->GetType(), \
  1058 			info.prop->m_nBits, \
  1059 			type); \
  1060 	}
  1061 
  1062 
  1063 inline int MatchFieldAsInteger(int field_type)
  1064 {
  1065 	switch (field_type)
  1066 	{
  1067 	case FIELD_TICK:
  1068 	case FIELD_MODELINDEX:
  1069 	case FIELD_MATERIALINDEX:
  1070 	case FIELD_INTEGER:
  1071 	case FIELD_COLOR32:
  1072 		return 32;
  1073 	case FIELD_SHORT:
  1074 		return 16;
  1075 	case FIELD_CHARACTER:
  1076 		return 8;
  1077 	case FIELD_BOOLEAN:
  1078 		return 1;
  1079 	default:
  1080 		return 0;
  1081 	}
  1082 
  1083 	return 0;
  1084 }
  1085 
  1086 static cell_t GetEntPropArraySize(IPluginContext *pContext, const cell_t *params)
  1087 {
  1088 	CBaseEntity *pEntity;
  1089 	char *prop;
  1090 	edict_t *pEdict;
  1091 
  1092 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1093 	{
  1094 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1095 	}
  1096 
  1097 	pContext->LocalToString(params[3], &prop);
  1098 
  1099 	switch (params[2])
  1100 	{
  1101 	case Prop_Data:
  1102 		{
  1103 			typedescription_t *td;
  1104 
  1105 			FIND_PROP_DATA(td);
  1106 
  1107 			return td->fieldSize;
  1108 		}
  1109 	case Prop_Send:
  1110 		{
  1111 			sm_sendprop_info_t info;
  1112 			
  1113 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1114 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1115 			if (!pNet)
  1116 			{
  1117 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1118 			}
  1119 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1120 			{
  1121 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1122 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1123 					prop,
  1124 					params[1],
  1125 					((class_name) ? class_name : ""));
  1126 			}
  1127 
  1128 			if (info.prop->GetType() != DPT_DataTable)
  1129 			{
  1130 				return 0;
  1131 			}
  1132 
  1133 			SendTable *pTable = info.prop->GetDataTable();
  1134 			if (!pTable)
  1135 			{
  1136 				return pContext->ThrowNativeError("Error looking up DataTable for prop %s", prop);
  1137 			}
  1138 			
  1139 			return pTable->GetNumProps();
  1140 		}
  1141 	default:
  1142 		{
  1143 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1144 		}
  1145 	}
  1146 
  1147 	return 1;
  1148 }
  1149 
  1150 static cell_t GetEntProp(IPluginContext *pContext, const cell_t *params)
  1151 {
  1152 	CBaseEntity *pEntity;
  1153 	char *prop;
  1154 	int offset;
  1155 	edict_t *pEdict;
  1156 	int bit_count;
  1157 	bool is_unsigned = false;
  1158 
  1159 	int element = 0;
  1160 	if (params[0] >= 5)
  1161 	{
  1162 		element = params[5];
  1163 	}
  1164 
  1165 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1166 	{
  1167 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1168 	}
  1169 
  1170 	pContext->LocalToString(params[3], &prop);
  1171 
  1172 	switch (params[2])
  1173 	{
  1174 	case Prop_Data:
  1175 		{
  1176 			typedescription_t *td;
  1177 
  1178 			FIND_PROP_DATA(td);
  1179 
  1180 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1181 			{
  1182 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1183 					prop,
  1184 					td->fieldType);
  1185 			}
  1186 
  1187 			CHECK_SET_PROP_DATA_OFFSET();
  1188 
  1189 			break;
  1190 		}
  1191 	case Prop_Send:
  1192 		{
  1193 			FIND_PROP_SEND(DPT_Int, "integer");
  1194 			is_unsigned = ((info.prop->GetFlags() & SPROP_UNSIGNED) == SPROP_UNSIGNED);
  1195 
  1196 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1197 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1198 			if (info.prop->GetFlags() & SPROP_VARINT)
  1199 			{
  1200 				bit_count = sizeof(int) * 8;
  1201 			}
  1202 #endif
  1203 			break;
  1204 		}
  1205 	default:
  1206 		{
  1207 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1208 		}
  1209 	}
  1210 
  1211 	if (bit_count < 1)
  1212 	{
  1213 		bit_count = params[4] * 8;
  1214 	}
  1215 
  1216 	if (bit_count >= 17)
  1217 	{
  1218 		return *(int32_t *)((uint8_t *)pEntity + offset);
  1219 	}
  1220 	else if (bit_count >= 9)
  1221 	{
  1222 		if (is_unsigned)
  1223 		{
  1224 			return *(uint16_t *)((uint8_t *)pEntity + offset);
  1225 		}
  1226 		else
  1227 		{
  1228 			return *(int16_t *)((uint8_t *)pEntity + offset);
  1229 		}
  1230 	}
  1231 	else if (bit_count >= 2)
  1232 	{
  1233 		if (is_unsigned)
  1234 		{
  1235 			return *(uint8_t *)((uint8_t *)pEntity + offset);
  1236 		}
  1237 		else
  1238 		{
  1239 			return *(int8_t *)((uint8_t *)pEntity + offset);
  1240 		}
  1241 	}
  1242 	else
  1243 	{
  1244 		return *(bool *)((uint8_t *)pEntity + offset) ? 1 : 0;
  1245 	}
  1246 
  1247 	return 0;
  1248 }
  1249 
  1250 static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
  1251 {
  1252 	CBaseEntity *pEntity;
  1253 	char *prop;
  1254 	int offset;
  1255 	edict_t *pEdict;
  1256 	int bit_count;
  1257 
  1258 	int element = 0;
  1259 	if (params[0] >= 6)
  1260 	{
  1261 		element = params[6];
  1262 	}
  1263 
  1264 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1265 	{
  1266 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1267 	}
  1268 
  1269 	pContext->LocalToString(params[3], &prop);
  1270 
  1271 	switch (params[2])
  1272 	{
  1273 	case Prop_Data:
  1274 		{
  1275 			typedescription_t *td;
  1276 
  1277 			FIND_PROP_DATA(td);
  1278 
  1279 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1280 			{
  1281 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1282 					prop,
  1283 					td->fieldType);
  1284 			}
  1285 
  1286 			CHECK_SET_PROP_DATA_OFFSET();
  1287 
  1288 			break;
  1289 		}
  1290 	case Prop_Send:
  1291 		{
  1292 			FIND_PROP_SEND(DPT_Int, "integer");
  1293 
  1294 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1295 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1296 			if (info.prop->GetFlags() & SPROP_VARINT)
  1297 			{
  1298 				bit_count = sizeof(int) * 8;
  1299 			}
  1300 #endif
  1301 			break;
  1302 		}
  1303 	default:
  1304 		{
  1305 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1306 		}
  1307 	}
  1308 
  1309 	if (bit_count < 1)
  1310 	{
  1311 		bit_count = params[5] * 8;
  1312 	}
  1313 
  1314 	if (bit_count >= 17)
  1315 	{
  1316 		*(int32_t *)((uint8_t *)pEntity + offset) = params[4];
  1317 	}
  1318 	else if (bit_count >= 9)
  1319 	{
  1320 		*(int16_t *)((uint8_t *)pEntity + offset) = (int16_t)params[4];
  1321 	}
  1322 	else if (bit_count >= 2)
  1323 	{
  1324 		*(int8_t *)((uint8_t *)pEntity + offset) = (int8_t)params[4];
  1325 	}
  1326 	else
  1327 	{
  1328 		*(bool *)((uint8_t *)pEntity + offset) = params[4] ? true : false;
  1329 	}
  1330 	
  1331 	if (params[2] == Prop_Send && (pEdict != NULL))
  1332 	{
  1333 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1334 	}
  1335 
  1336 	return 0;
  1337 }
  1338 
  1339 static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1340 {
  1341 	CBaseEntity *pEntity;
  1342 	char *prop;
  1343 	int offset;
  1344 	int bit_count;
  1345 	edict_t *pEdict;
  1346 
  1347 	int element = 0;
  1348 	if (params[0] >= 4)
  1349 	{
  1350 		element = params[4];
  1351 	}
  1352 
  1353 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1354 	{
  1355 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1356 	}
  1357 
  1358 	pContext->LocalToString(params[3], &prop);
  1359 
  1360 	switch (params[2])
  1361 	{
  1362 	case Prop_Data:
  1363 		{
  1364 			typedescription_t *td;
  1365 
  1366 			FIND_PROP_DATA(td);
  1367 
  1368 			if (td->fieldType != FIELD_FLOAT
  1369 				&& td->fieldType != FIELD_TIME)
  1370 			{
  1371 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1372 					prop,
  1373 					td->fieldType,
  1374 					FIELD_FLOAT,
  1375 					FIELD_TIME);
  1376 			}
  1377 
  1378 			CHECK_SET_PROP_DATA_OFFSET();
  1379 
  1380 			break;
  1381 		}
  1382 	case Prop_Send:
  1383 		{
  1384 			FIND_PROP_SEND(DPT_Float, "float");
  1385 			break;
  1386 		}
  1387 	default:
  1388 		{
  1389 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1390 		}
  1391 	}
  1392 
  1393 	float val = *(float *)((uint8_t *)pEntity + offset);
  1394 
  1395 	return sp_ftoc(val);
  1396 }
  1397 
  1398 static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1399 {
  1400 	CBaseEntity *pEntity;
  1401 	char *prop;
  1402 	int offset;
  1403 	int bit_count;
  1404 	edict_t *pEdict;
  1405 
  1406 	int element = 0;
  1407 	if (params[0] >= 5)
  1408 	{
  1409 		element = params[5];
  1410 	}
  1411 
  1412 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1413 	{
  1414 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1415 	}
  1416 
  1417 	pContext->LocalToString(params[3], &prop);
  1418 
  1419 	switch (params[2])
  1420 	{
  1421 	case Prop_Data:
  1422 		{
  1423 			typedescription_t *td;
  1424 
  1425 			FIND_PROP_DATA(td);
  1426 
  1427 			if (td->fieldType != FIELD_FLOAT
  1428 				&& td->fieldType != FIELD_TIME)
  1429 			{
  1430 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1431 					prop,
  1432 					td->fieldType,
  1433 					FIELD_FLOAT,
  1434 					FIELD_TIME);
  1435 			}
  1436 
  1437 			CHECK_SET_PROP_DATA_OFFSET();
  1438 
  1439 			break;
  1440 		}
  1441 	case Prop_Send:
  1442 		{
  1443 			FIND_PROP_SEND(DPT_Float, "float");
  1444 			break;
  1445 		}
  1446 	default:
  1447 		{
  1448 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1449 		}
  1450 	}
  1451 
  1452 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[4]);
  1453 
  1454 	if (params[2] == Prop_Send && (pEdict != NULL))
  1455 	{
  1456 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1457 	}
  1458 
  1459 	return 1;
  1460 }
  1461 
  1462 static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1463 {
  1464 	CBaseEntity *pEntity;
  1465 	char *prop;
  1466 	int offset;
  1467 	int bit_count;
  1468 	edict_t *pEdict;
  1469 
  1470 	int element = 0;
  1471 	if (params[0] >= 4)
  1472  	{
  1473 		element = params[4];
  1474  	}
  1475 
  1476 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1477 	{
  1478 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1479 	}
  1480 
  1481 	pContext->LocalToString(params[3], &prop);
  1482 
  1483 	switch (params[2])
  1484 	{
  1485 	case Prop_Data:
  1486 		{
  1487 			typedescription_t *td;
  1488 
  1489 			FIND_PROP_DATA(td);
  1490 
  1491 			if (td->fieldType != FIELD_EHANDLE)
  1492 			{
  1493 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1494 					prop,
  1495 					td->fieldType,
  1496 					FIELD_EHANDLE);
  1497 			}
  1498 
  1499 			CHECK_SET_PROP_DATA_OFFSET();
  1500 
  1501 			break;
  1502 		}
  1503 	case Prop_Send:
  1504 		{
  1505 			FIND_PROP_SEND(DPT_Int, "integer");
  1506 			break;
  1507 		}
  1508 	default:
  1509 		{
  1510 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1511 		}
  1512 	}
  1513 
  1514 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1515 	CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(hndl.GetEntryIndex());
  1516 
  1517 	if (!pHandleEntity || hndl != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
  1518 		return -1;
  1519 
  1520 	return g_HL2.EntityToBCompatRef(pHandleEntity);
  1521 }
  1522 
  1523 static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1524 {
  1525 	CBaseEntity *pEntity;
  1526 	char *prop;
  1527 	int offset;
  1528 	int bit_count;
  1529 	edict_t *pEdict;
  1530 
  1531 	int element = 0;
  1532 	if (params[0] >= 5)
  1533  	{
  1534 		element = params[5];
  1535  	}
  1536 
  1537 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1538 	{
  1539 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1540 	}
  1541 
  1542 	pContext->LocalToString(params[3], &prop);
  1543 
  1544 	switch (params[2])
  1545 	{
  1546 	case Prop_Data:
  1547 		{
  1548 			typedescription_t *td;
  1549 
  1550 			FIND_PROP_DATA(td);
  1551 
  1552 			if (td->fieldType != FIELD_EHANDLE)
  1553 			{
  1554 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1555 					prop,
  1556 					td->fieldType,
  1557 					FIELD_EHANDLE);
  1558 			}
  1559 
  1560 			CHECK_SET_PROP_DATA_OFFSET();
  1561 
  1562 			break;
  1563 		}
  1564 	case Prop_Send:
  1565 		{
  1566 			FIND_PROP_SEND(DPT_Int, "integer");
  1567 			break;
  1568 		}
  1569 	default:
  1570 		{
  1571 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1572 		}
  1573 	}
  1574 
  1575 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1576 
  1577 	if (params[4] == -1)
  1578 	{
  1579 		hndl.Set(NULL);
  1580 	}
  1581 	else
  1582 	{
  1583 		CBaseEntity *pOther = GetEntity(params[4]);
  1584 
  1585 		if (!pOther)
  1586 		{
  1587 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[4]), params[4]);
  1588 		}
  1589 
  1590 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
  1591 		hndl.Set(pHandleEnt);
  1592 	}
  1593 
  1594 	if (params[2] == Prop_Send && (pEdict != NULL))
  1595 	{
  1596 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1597 	}
  1598 
  1599 	return 1;
  1600 }
  1601 
  1602 static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1603 {
  1604 	CBaseEntity *pEntity;
  1605 	char *prop;
  1606 	int offset;
  1607 	int bit_count;
  1608 	edict_t *pEdict;
  1609 
  1610 	int element = 0;
  1611 	if (params[0] >= 5)
  1612  	{
  1613 		element = params[5];
  1614  	}
  1615 	
  1616 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1617 	{
  1618 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1619 	}
  1620 
  1621 	pContext->LocalToString(params[3], &prop);
  1622 
  1623 	switch (params[2])
  1624 	{
  1625 	case Prop_Data:
  1626 		{
  1627 			typedescription_t *td;
  1628 			
  1629 			FIND_PROP_DATA(td);
  1630 
  1631 			if (td->fieldType != FIELD_VECTOR
  1632 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1633 			{
  1634 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1635 					prop,
  1636 					td->fieldType,
  1637 					FIELD_VECTOR,
  1638 					FIELD_POSITION_VECTOR);
  1639 			}
  1640 
  1641 			CHECK_SET_PROP_DATA_OFFSET();
  1642 
  1643 			break;
  1644 		}
  1645 	case Prop_Send:
  1646 		{
  1647 			FIND_PROP_SEND(DPT_Vector, "vector");
  1648 			break;
  1649 		}
  1650 	default:
  1651 		{
  1652 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1653 		}
  1654 	}
  1655 
  1656 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1657 
  1658 	cell_t *vec;
  1659 	pContext->LocalToPhysAddr(params[4], &vec);
  1660 
  1661 	vec[0] = sp_ftoc(v->x);
  1662 	vec[1] = sp_ftoc(v->y);
  1663 	vec[2] = sp_ftoc(v->z);
  1664 
  1665 	return 1;
  1666 }
  1667 
  1668 static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1669 {
  1670 	CBaseEntity *pEntity;
  1671 	char *prop;
  1672 	int offset;
  1673 	int bit_count;
  1674 	edict_t *pEdict;
  1675 
  1676 	int element = 0;
  1677 	if (params[0] >= 5)
  1678  	{
  1679 		element = params[5];
  1680  	}
  1681 	
  1682 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1683 	{
  1684 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1685 	}
  1686 
  1687 	pContext->LocalToString(params[3], &prop);
  1688 
  1689 	switch (params[2])
  1690 	{
  1691 	case Prop_Data:
  1692 		{
  1693 			typedescription_t *td;
  1694 			
  1695 			FIND_PROP_DATA(td);
  1696 
  1697 			if (td->fieldType != FIELD_VECTOR
  1698 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1699 			{
  1700 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1701 					prop,
  1702 					td->fieldType,
  1703 					FIELD_VECTOR,
  1704 					FIELD_POSITION_VECTOR);
  1705 			}
  1706 
  1707 			CHECK_SET_PROP_DATA_OFFSET();
  1708 
  1709 			break;
  1710 		}
  1711 	case Prop_Send:
  1712 		{
  1713 			FIND_PROP_SEND(DPT_Vector, "vector");			
  1714 			break;
  1715 		}
  1716 	default:
  1717 		{
  1718 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1719 		}
  1720 	}
  1721 
  1722 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1723 
  1724 	cell_t *vec;
  1725 	pContext->LocalToPhysAddr(params[4], &vec);
  1726 
  1727 	v->x = sp_ctof(vec[0]);
  1728 	v->y = sp_ctof(vec[1]);
  1729 	v->z = sp_ctof(vec[2]);
  1730 
  1731 	if (params[2] == Prop_Send && (pEdict != NULL))
  1732 	{
  1733 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1734 	}
  1735 
  1736 	return 1;
  1737 }
  1738 
  1739 static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
  1740 {
  1741 	CBaseEntity *pEntity;
  1742 	char *prop;
  1743 	int offset;
  1744 	edict_t *pEdict;
  1745 	bool bIsStringIndex;
  1746 
  1747 	int element = 0;
  1748 	if (params[0] >= 6)
  1749  	{
  1750 		element = params[6];
  1751  	}
  1752 	
  1753 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1754 	{
  1755 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1756 	}
  1757 
  1758 	pContext->LocalToString(params[3], &prop);
  1759 
  1760 	bIsStringIndex = false;
  1761 
  1762 	switch (params[2])
  1763 	{
  1764 	case Prop_Data:
  1765 		{
  1766 			typedescription_t *td;
  1767 			
  1768 			FIND_PROP_DATA(td);
  1769 
  1770 			if (td->fieldType != FIELD_CHARACTER
  1771 				&& td->fieldType != FIELD_STRING
  1772 				&& td->fieldType != FIELD_MODELNAME 
  1773 				&& td->fieldType != FIELD_SOUNDNAME)
  1774 			{
  1775 				return pContext->ThrowNativeError("Data field %s is not a string (%d != %d)", 
  1776 					prop,
  1777 					td->fieldType,
  1778 					FIELD_CHARACTER);
  1779 			}
  1780 
  1781 			bIsStringIndex = (td->fieldType != FIELD_CHARACTER);
  1782 
  1783 			if (bIsStringIndex && (element < 0 || element >= td->fieldSize))
  1784 			{
  1785 				return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).",
  1786 					element,
  1787 					prop,
  1788 					td->fieldSize);
  1789 			}
  1790 			else if (element != 0)
  1791 			{
  1792 				return pContext->ThrowNativeError("Prop %s is not an array. Element %d is invalid.",
  1793 					prop,
  1794 					element);
  1795 			}
  1796 
  1797 			offset = GetTypeDescOffs(td);
  1798 			if (bIsStringIndex)
  1799 			{
  1800 				offset += (element * (td->fieldSizeInBytes / td->fieldSize));
  1801 			}
  1802 			break;
  1803 		}
  1804 	case Prop_Send:
  1805 		{
  1806 			sm_sendprop_info_t info;
  1807 			
  1808 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1809 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1810 			if (!pNet)
  1811 			{
  1812 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1813 			}
  1814 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1815 			{
  1816 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1817 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1818 					prop,
  1819 					params[1],
  1820 					((class_name) ? class_name : ""));
  1821 			}
  1822 
  1823 			offset = info.actual_offset;
  1824 
  1825 			if (info.prop->GetType() != DPT_String)
  1826 			{
  1827 				return pContext->ThrowNativeError("SendProp %s is not a string (%d != %d)",
  1828 					prop,
  1829 					info.prop->GetType(),
  1830 					DPT_String);
  1831 			}
  1832 			else if (element != 0)
  1833 			{
  1834 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.",
  1835 					prop,
  1836 					element);
  1837  			}
  1838 			
  1839 			break;
  1840 		}
  1841 	default:
  1842 		{
  1843 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1844 		}
  1845 	}
  1846 
  1847 	size_t len;
  1848 	const char *src; 
  1849 	
  1850 	if (!bIsStringIndex)
  1851 	{
  1852 		src = (char *)((uint8_t *)pEntity + offset);
  1853 	}
  1854 	else
  1855 	{
  1856 		string_t idx;
  1857 
  1858 		idx = *(string_t *)((uint8_t *)pEntity + offset);
  1859 		src = (idx == NULL_STRING) ? "" : STRING(idx);
  1860 	}
  1861 
  1862 	pContext->StringToLocalUTF8(params[4], params[5], src, &len);
  1863 
  1864 	return len;
  1865 }
  1866 
  1867 static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
  1868 {
  1869 	CBaseEntity *pEntity;
  1870 	char *prop;
  1871 	int offset;
  1872 	int maxlen;
  1873 	edict_t *pEdict;
  1874 
  1875 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1876 	{
  1877 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1878 	}
  1879 
  1880 	switch (params[2])
  1881 	{
  1882 	case Prop_Data:
  1883 		{
  1884 			datamap_t *pMap;
  1885 			typedescription_t *td;
  1886 			if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  1887 			{
  1888 				return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
  1889 			}
  1890 			pContext->LocalToString(params[3], &prop);
  1891 			if ((td=g_HL2.FindInDataMap(pMap, prop)) == NULL)
  1892 			{
  1893 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1894 			}
  1895 			if (td->fieldType != FIELD_CHARACTER)
  1896 			{
  1897 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1898 			}
  1899 			offset = GetTypeDescOffs(td);
  1900 			maxlen = td->fieldSize;
  1901 			break;
  1902 		}
  1903 	case Prop_Send:
  1904 		{
  1905 			char *prop;
  1906 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1907 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1908 			if (!pNet)
  1909 			{
  1910 				return pContext->ThrowNativeError("The edict is not networkable");
  1911 			}
  1912 			pContext->LocalToString(params[3], &prop);
  1913 			SendProp *pSend = g_HL2.FindInSendTable(pNet->GetServerClass()->GetName(), prop);
  1914 			if (!pSend)
  1915 			{
  1916 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1917 			}
  1918 			if (pSend->GetType() != DPT_String)
  1919 			{
  1920 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1921 			}
  1922 			offset = pSend->GetOffset();
  1923 			maxlen = DT_MAX_STRING_BUFFERSIZE;
  1924 			break;
  1925 		}
  1926 	default:
  1927 		{
  1928 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1929 		}
  1930 	}
  1931 
  1932 	char *src;
  1933 	char *dest = (char *)((uint8_t *)pEntity + offset);
  1934 
  1935 	pContext->LocalToString(params[4], &src);
  1936 	size_t len = strncopy(dest, src, maxlen);
  1937 
  1938 	if (params[2] == Prop_Send && (pEdict != NULL))
  1939 	{
  1940 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1941 	}
  1942 
  1943 	return len;
  1944 }
  1945 
  1946 static int32_t SDKEntFlagToSMEntFlag(int flag)
  1947 {
  1948 	switch (flag)
  1949 	{
  1950 		case FL_ONGROUND:
  1951 			return ENTFLAG_ONGROUND;
  1952 		case FL_DUCKING:
  1953 			return ENTFLAG_DUCKING;
  1954 		case FL_WATERJUMP:
  1955 			return ENTFLAG_WATERJUMP;
  1956 		case FL_ONTRAIN:
  1957 			return ENTFLAG_ONTRAIN;
  1958 		case FL_INRAIN:
  1959 			return ENTFLAG_INRAIN;
  1960 		case FL_FROZEN:
  1961 			return ENTFLAG_FROZEN;
  1962 		case FL_ATCONTROLS:
  1963 			return ENTFLAG_ATCONTROLS;
  1964 		case FL_CLIENT:
  1965 			return ENTFLAG_CLIENT;
  1966 		case FL_FAKECLIENT:
  1967 			return ENTFLAG_FAKECLIENT;
  1968 		case FL_INWATER:
  1969 			return ENTFLAG_INWATER;
  1970 		case FL_FLY:
  1971 			return ENTFLAG_FLY;
  1972 		case FL_SWIM:
  1973 			return ENTFLAG_SWIM;
  1974 		case FL_CONVEYOR:
  1975 			return ENTFLAG_CONVEYOR;
  1976 		case FL_NPC:
  1977 			return ENTFLAG_NPC;
  1978 		case FL_GODMODE:
  1979 			return ENTFLAG_GODMODE;
  1980 		case FL_NOTARGET:
  1981 			return ENTFLAG_NOTARGET;
  1982 		case FL_AIMTARGET:
  1983 			return ENTFLAG_AIMTARGET;
  1984 		case FL_PARTIALGROUND:
  1985 			return ENTFLAG_PARTIALGROUND;
  1986 		case FL_STATICPROP:
  1987 			return ENTFLAG_STATICPROP;
  1988 		case FL_GRAPHED:
  1989 			return ENTFLAG_GRAPHED;
  1990 		case FL_GRENADE:
  1991 			return ENTFLAG_GRENADE;
  1992 		case FL_STEPMOVEMENT:
  1993 			return ENTFLAG_STEPMOVEMENT;
  1994 		case FL_DONTTOUCH:
  1995 			return ENTFLAG_DONTTOUCH;
  1996 		case FL_BASEVELOCITY:
  1997 			return ENTFLAG_BASEVELOCITY;
  1998 		case FL_WORLDBRUSH:
  1999 			return ENTFLAG_WORLDBRUSH;
  2000 		case FL_OBJECT:
  2001 			return ENTFLAG_OBJECT;
  2002 		case FL_KILLME:
  2003 			return ENTFLAG_KILLME;
  2004 		case FL_ONFIRE:
  2005 			return ENTFLAG_ONFIRE;
  2006 		case FL_DISSOLVING:
  2007 			return ENTFLAG_DISSOLVING;
  2008 		case FL_TRANSRAGDOLL:
  2009 			return ENTFLAG_TRANSRAGDOLL;
  2010 		case FL_UNBLOCKABLE_BY_PLAYER:
  2011 			return ENTFLAG_UNBLOCKABLE_BY_PLAYER;
  2012 #if SOURCE_ENGINE == SE_ALIENSWARM
  2013 		case FL_FREEZING:
  2014 			return ENTFLAG_FREEZING;
  2015 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2016 		case FL_EP2V_UNKNOWN:
  2017 			return ENTFLAG_EP2V_UNKNOWN1;
  2018 #endif
  2019 		default:
  2020 			return 0;
  2021 	}
  2022 }
  2023 
  2024 static int32_t SMEntFlagToSDKEntFlag(int32_t flag)
  2025 {
  2026 	switch (flag)
  2027 	{
  2028 		case ENTFLAG_ONGROUND:
  2029 			return FL_ONGROUND;
  2030 		case ENTFLAG_DUCKING:
  2031 			return FL_DUCKING;
  2032 		case ENTFLAG_WATERJUMP:
  2033 			return FL_WATERJUMP;
  2034 		case ENTFLAG_ONTRAIN:
  2035 			return FL_ONTRAIN;
  2036 		case ENTFLAG_INRAIN:
  2037 			return FL_INRAIN;
  2038 		case ENTFLAG_FROZEN:
  2039 			return FL_FROZEN;
  2040 		case ENTFLAG_ATCONTROLS:
  2041 			return FL_ATCONTROLS;
  2042 		case ENTFLAG_CLIENT:
  2043 			return FL_CLIENT;
  2044 		case ENTFLAG_FAKECLIENT:
  2045 			return FL_FAKECLIENT;
  2046 		case ENTFLAG_INWATER:
  2047 			return FL_INWATER;
  2048 		case ENTFLAG_FLY:
  2049 			return FL_FLY;
  2050 		case ENTFLAG_SWIM:
  2051 			return FL_SWIM;
  2052 		case ENTFLAG_CONVEYOR:
  2053 			return FL_CONVEYOR;
  2054 		case ENTFLAG_NPC:
  2055 			return FL_NPC;
  2056 		case ENTFLAG_GODMODE:
  2057 			return FL_GODMODE;
  2058 		case ENTFLAG_NOTARGET:
  2059 			return FL_NOTARGET;
  2060 		case ENTFLAG_AIMTARGET:
  2061 			return FL_AIMTARGET;
  2062 		case ENTFLAG_PARTIALGROUND:
  2063 			return FL_PARTIALGROUND;
  2064 		case ENTFLAG_STATICPROP:
  2065 			return FL_STATICPROP;
  2066 		case ENTFLAG_GRAPHED:
  2067 			return FL_GRAPHED;
  2068 		case ENTFLAG_GRENADE:
  2069 			return FL_GRENADE;
  2070 		case ENTFLAG_STEPMOVEMENT:
  2071 			return FL_STEPMOVEMENT;
  2072 		case ENTFLAG_DONTTOUCH:
  2073 			return FL_DONTTOUCH;
  2074 		case ENTFLAG_BASEVELOCITY:
  2075 			return FL_BASEVELOCITY;
  2076 		case ENTFLAG_WORLDBRUSH:
  2077 			return FL_WORLDBRUSH;
  2078 		case ENTFLAG_OBJECT:
  2079 			return FL_OBJECT;
  2080 		case ENTFLAG_KILLME:
  2081 			return FL_KILLME;
  2082 		case ENTFLAG_ONFIRE:
  2083 			return FL_ONFIRE;
  2084 		case ENTFLAG_DISSOLVING:
  2085 			return FL_DISSOLVING;
  2086 		case ENTFLAG_TRANSRAGDOLL:
  2087 			return FL_TRANSRAGDOLL;
  2088 		case ENTFLAG_UNBLOCKABLE_BY_PLAYER:
  2089 			return FL_UNBLOCKABLE_BY_PLAYER;
  2090 #if SOURCE_ENGINE == SE_ALIENSWARM
  2091 		case ENTFLAG_FREEZING:
  2092 			return FL_FREEZING;
  2093 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2094 		case ENTFLAG_EP2V_UNKNOWN1:
  2095 			return FL_EP2V_UNKNOWN;
  2096 #endif
  2097 		default:
  2098 			return 0;
  2099 	}
  2100 }
  2101 
  2102 static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2103 {
  2104 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2105 	if (!pEntity)
  2106 	{
  2107 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2108 	}
  2109 
  2110 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2111 	if (!prop)
  2112 	{
  2113 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2114 	}
  2115 
  2116 	typedescription_t *td;
  2117 	datamap_t *pMap;
  2118 
  2119 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2120 	{
  2121 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2122 	}
  2123 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2124 	{
  2125 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2126 			prop,
  2127 			params[1]);
  2128 	}
  2129 
  2130 	int offset = GetTypeDescOffs(td);
  2131 
  2132 	int32_t actual_flags = *(int32_t *)((uint8_t *)pEntity + offset);
  2133 	int32_t sm_flags = 0;
  2134 
  2135 	for (int32_t i = 0; i < 32; i++)
  2136 	{
  2137 		int32_t flag = (1<<i);
  2138 		if ((actual_flags & flag) == flag)
  2139 		{
  2140 			sm_flags |= SDKEntFlagToSMEntFlag(flag);
  2141 		}
  2142 	}
  2143 
  2144 	return sm_flags;
  2145 }
  2146 
  2147 static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2148 {
  2149 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2150 	if (!pEntity)
  2151 	{
  2152 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2153 	}
  2154 
  2155 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2156 	if (!prop)
  2157 	{
  2158 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2159 	}
  2160 
  2161 	typedescription_t *td;
  2162 	datamap_t *pMap;
  2163 
  2164 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2165 	{
  2166 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2167 	}
  2168 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2169 	{
  2170 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2171 			prop,
  2172 			params[1]);
  2173 	}
  2174 
  2175 	int offset = GetTypeDescOffs(td);
  2176 
  2177 	int32_t sm_flags = params[2];
  2178 	int32_t actual_flags = 0;
  2179 
  2180 	for (int32_t i = 0; i < 32; i++)
  2181 	{
  2182 		int32_t flag = (1<<i);
  2183 		if ((sm_flags & flag) == flag)
  2184 		{
  2185 			actual_flags |= SMEntFlagToSDKEntFlag(flag);
  2186 		}
  2187 	}
  2188 
  2189 	*(int32_t *)((uint8_t *)pEntity + offset) = actual_flags;
  2190 
  2191 	return 0;
  2192 }
  2193 
  2194 static cell_t GetEntityAddress(IPluginContext *pContext, const cell_t *params)
  2195 {
  2196 	CBaseEntity * pEntity = GetEntity(params[1]);
  2197 	if (!pEntity)
  2198 	{
  2199 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2200 	}
  2201 	return reinterpret_cast<cell_t>(pEntity);
  2202 }
  2203 
  2204 REGISTER_NATIVES(entityNatives)
  2205 {
  2206 	{"ChangeEdictState",		ChangeEdictState},
  2207 	{"CreateEdict",				CreateEdict},
  2208 	{"FindDataMapOffs",			FindDataMapOffs},
  2209 	{"FindSendPropInfo",		FindSendPropInfo},
  2210 	{"FindSendPropOffs",		FindSendPropOffs},
  2211 	{"GetEdictClassname",		GetEdictClassname},
  2212 	{"GetEdictFlags",			GetEdictFlags},
  2213 	{"GetEntData",				GetEntData},
  2214 	{"GetEntDataEnt",			GetEntDataEnt},		/** DEPRECATED */
  2215 	{"GetEntDataEnt2",			GetEntDataEnt2},
  2216 	{"GetEntDataFloat",			GetEntDataFloat},
  2217 	{"GetEntDataString",		GetEntDataString},
  2218 	{"GetEntDataVector",		GetEntDataVector},
  2219 	{"GetEntProp",				GetEntProp},
  2220 	{"GetEntPropArraySize",		GetEntPropArraySize},
  2221 	{"GetEntPropEnt",			GetEntPropEnt},
  2222 	{"GetEntPropFloat",			GetEntPropFloat},
  2223 	{"GetEntPropString",		GetEntPropString},
  2224 	{"GetEntPropVector",		GetEntPropVector},
  2225 	{"GetEntityCount",			GetEntityCount},
  2226 	{"GetEntityFlags",			GetEntityFlags},
  2227 	{"GetEntityNetClass",		GetEntityNetClass},
  2228 	{"GetMaxEntities",			GetMaxEntities},
  2229 	{"IsEntNetworkable",		IsEntNetworkable},
  2230 	{"IsValidEdict",			IsValidEdict},
  2231 	{"IsValidEntity",			IsValidEntity},
  2232 	{"RemoveEdict",				RemoveEdict},
  2233 	{"SetEdictFlags",			SetEdictFlags},
  2234 	{"SetEntData",				SetEntData},
  2235 	{"SetEntDataEnt",			SetEntDataEnt},		/** DEPRECATED */
  2236 	{"SetEntDataEnt2",			SetEntDataEnt2},
  2237 	{"SetEntDataFloat",			SetEntDataFloat},
  2238 	{"SetEntDataVector",		SetEntDataVector},
  2239 	{"SetEntDataString",		SetEntDataString},
  2240 	{"SetEntityFlags",			SetEntityFlags},
  2241 	{"SetEntProp",				SetEntProp},
  2242 	{"SetEntPropEnt",			SetEntPropEnt},
  2243 	{"SetEntPropFloat",			SetEntPropFloat},
  2244 	{"SetEntPropString",		SetEntPropString},
  2245 	{"SetEntPropVector",		SetEntPropVector},
  2246 	{"GetEntityAddress",		GetEntityAddress},
  2247 	{NULL,						NULL}
  2248 };