core/smn_entities.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 16 15:52:11 2013 -0800 (2013-02-16)
changeset 3791 47e1ee9cea86
parent 3762 0f6b4d692ed9
child 3806 c713139e894c
permissions -rw-r--r--
Move filesystem natives from Core to Logic (bug 5606, r=psychonic).
     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 	SendProp *pProp; \
   988 	IServerUnknown *pUnk = (IServerUnknown *)pEntity; \
   989 	IServerNetworkable *pNet = pUnk->GetNetworkable(); \
   990 	if (!pNet) \
   991 	{ \
   992 		return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]); \
   993 	} \
   994 	if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info)) \
   995 	{ \
   996 		const char *class_name = g_HL2.GetEntityClassname(pEntity); \
   997 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   998 			prop, \
   999 			params[1], \
  1000 			((class_name) ? class_name : "")); \
  1001 	} \
  1002 	\
  1003 	offset = info.actual_offset; \
  1004 	pProp = info.prop; \
  1005 	bit_count = pProp->m_nBits; \
  1006 	\
  1007 	switch (pProp->GetType()) \
  1008 	{ \
  1009 	case type: \
  1010 		{ \
  1011 			if (element != 0) \
  1012 			{ \
  1013 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.", \
  1014 					prop, \
  1015 					element); \
  1016 			} \
  1017 			break; \
  1018 		} \
  1019 	case DPT_DataTable: \
  1020 		{ \
  1021 			FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name); \
  1022 			\
  1023 			offset += pProp->GetOffset(); \
  1024 			bit_count = pProp->m_nBits; \
  1025 			break; \
  1026 		} \
  1027 	default: \
  1028 		{ \
  1029 			return pContext->ThrowNativeError("SendProp %s type is not " type_name " (%d != %d)", \
  1030 				prop, \
  1031 				pProp->GetType(), \
  1032 				type); \
  1033 		} \
  1034 	} \
  1035 
  1036 #define FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name) \
  1037 	SendTable *pTable = pProp->GetDataTable(); \
  1038 	if (!pTable) \
  1039 	{ \
  1040 		return pContext->ThrowNativeError("Error looking up DataTable for prop %s", \
  1041 			prop); \
  1042 	} \
  1043 	\
  1044 	int elementCount = pTable->GetNumProps(); \
  1045 	if (element < 0 || element >= elementCount) \
  1046 	{ \
  1047 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
  1048 			element, \
  1049 			prop, \
  1050 			elementCount); \
  1051 	} \
  1052 	\
  1053 	pProp = pTable->GetProp(element); \
  1054 	if (pProp->GetType() != type) \
  1055 	{ \
  1056 		return pContext->ThrowNativeError("SendProp %s type is not " type_name " ([%d,%d] != %d)", \
  1057 			prop, \
  1058 			pProp->GetType(), \
  1059 			pProp->m_nBits, \
  1060 			type); \
  1061 	}
  1062 
  1063 
  1064 inline int MatchFieldAsInteger(int field_type)
  1065 {
  1066 	switch (field_type)
  1067 	{
  1068 	case FIELD_TICK:
  1069 	case FIELD_MODELINDEX:
  1070 	case FIELD_MATERIALINDEX:
  1071 	case FIELD_INTEGER:
  1072 	case FIELD_COLOR32:
  1073 		return 32;
  1074 	case FIELD_SHORT:
  1075 		return 16;
  1076 	case FIELD_CHARACTER:
  1077 		return 8;
  1078 	case FIELD_BOOLEAN:
  1079 		return 1;
  1080 	default:
  1081 		return 0;
  1082 	}
  1083 
  1084 	return 0;
  1085 }
  1086 
  1087 static cell_t GetEntPropArraySize(IPluginContext *pContext, const cell_t *params)
  1088 {
  1089 	CBaseEntity *pEntity;
  1090 	char *prop;
  1091 	edict_t *pEdict;
  1092 
  1093 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1094 	{
  1095 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1096 	}
  1097 
  1098 	pContext->LocalToString(params[3], &prop);
  1099 
  1100 	switch (params[2])
  1101 	{
  1102 	case Prop_Data:
  1103 		{
  1104 			typedescription_t *td;
  1105 
  1106 			FIND_PROP_DATA(td);
  1107 
  1108 			return td->fieldSize;
  1109 		}
  1110 	case Prop_Send:
  1111 		{
  1112 			sm_sendprop_info_t info;
  1113 			
  1114 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1115 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1116 			if (!pNet)
  1117 			{
  1118 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1119 			}
  1120 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1121 			{
  1122 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1123 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1124 					prop,
  1125 					params[1],
  1126 					((class_name) ? class_name : ""));
  1127 			}
  1128 
  1129 			if (info.prop->GetType() != DPT_DataTable)
  1130 			{
  1131 				return 0;
  1132 			}
  1133 
  1134 			SendTable *pTable = info.prop->GetDataTable();
  1135 			if (!pTable)
  1136 			{
  1137 				return pContext->ThrowNativeError("Error looking up DataTable for prop %s", prop);
  1138 			}
  1139 			
  1140 			return pTable->GetNumProps();
  1141 		}
  1142 	default:
  1143 		{
  1144 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1145 		}
  1146 	}
  1147 
  1148 	return 1;
  1149 }
  1150 
  1151 static cell_t GetEntProp(IPluginContext *pContext, const cell_t *params)
  1152 {
  1153 	CBaseEntity *pEntity;
  1154 	char *prop;
  1155 	int offset;
  1156 	edict_t *pEdict;
  1157 	int bit_count;
  1158 	bool is_unsigned = false;
  1159 
  1160 	int element = 0;
  1161 	if (params[0] >= 5)
  1162 	{
  1163 		element = params[5];
  1164 	}
  1165 
  1166 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1167 	{
  1168 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1169 	}
  1170 
  1171 	pContext->LocalToString(params[3], &prop);
  1172 
  1173 	switch (params[2])
  1174 	{
  1175 	case Prop_Data:
  1176 		{
  1177 			typedescription_t *td;
  1178 
  1179 			FIND_PROP_DATA(td);
  1180 
  1181 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1182 			{
  1183 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1184 					prop,
  1185 					td->fieldType);
  1186 			}
  1187 
  1188 			CHECK_SET_PROP_DATA_OFFSET();
  1189 
  1190 			break;
  1191 		}
  1192 	case Prop_Send:
  1193 		{
  1194 			FIND_PROP_SEND(DPT_Int, "integer");
  1195 			is_unsigned = ((pProp->GetFlags() & SPROP_UNSIGNED) == SPROP_UNSIGNED);
  1196 
  1197 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1198 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1199 			if (pProp->GetFlags() & SPROP_VARINT)
  1200 			{
  1201 				bit_count = sizeof(int) * 8;
  1202 			}
  1203 #endif
  1204 			break;
  1205 		}
  1206 	default:
  1207 		{
  1208 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1209 		}
  1210 	}
  1211 
  1212 	if (bit_count < 1)
  1213 	{
  1214 		bit_count = params[4] * 8;
  1215 	}
  1216 
  1217 	if (bit_count >= 17)
  1218 	{
  1219 		return *(int32_t *)((uint8_t *)pEntity + offset);
  1220 	}
  1221 	else if (bit_count >= 9)
  1222 	{
  1223 		if (is_unsigned)
  1224 		{
  1225 			return *(uint16_t *)((uint8_t *)pEntity + offset);
  1226 		}
  1227 		else
  1228 		{
  1229 			return *(int16_t *)((uint8_t *)pEntity + offset);
  1230 		}
  1231 	}
  1232 	else if (bit_count >= 2)
  1233 	{
  1234 		if (is_unsigned)
  1235 		{
  1236 			return *(uint8_t *)((uint8_t *)pEntity + offset);
  1237 		}
  1238 		else
  1239 		{
  1240 			return *(int8_t *)((uint8_t *)pEntity + offset);
  1241 		}
  1242 	}
  1243 	else
  1244 	{
  1245 		return *(bool *)((uint8_t *)pEntity + offset) ? 1 : 0;
  1246 	}
  1247 
  1248 	return 0;
  1249 }
  1250 
  1251 static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
  1252 {
  1253 	CBaseEntity *pEntity;
  1254 	char *prop;
  1255 	int offset;
  1256 	edict_t *pEdict;
  1257 	int bit_count;
  1258 
  1259 	int element = 0;
  1260 	if (params[0] >= 6)
  1261 	{
  1262 		element = params[6];
  1263 	}
  1264 
  1265 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1266 	{
  1267 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1268 	}
  1269 
  1270 	pContext->LocalToString(params[3], &prop);
  1271 
  1272 	switch (params[2])
  1273 	{
  1274 	case Prop_Data:
  1275 		{
  1276 			typedescription_t *td;
  1277 
  1278 			FIND_PROP_DATA(td);
  1279 
  1280 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1281 			{
  1282 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1283 					prop,
  1284 					td->fieldType);
  1285 			}
  1286 
  1287 			CHECK_SET_PROP_DATA_OFFSET();
  1288 
  1289 			break;
  1290 		}
  1291 	case Prop_Send:
  1292 		{
  1293 			FIND_PROP_SEND(DPT_Int, "integer");
  1294 
  1295 			// This isn't in CS:S yet, but will be, doesn't hurt to add now, and will save us a build later
  1296 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
  1297 			if (pProp->GetFlags() & SPROP_VARINT)
  1298 			{
  1299 				bit_count = sizeof(int) * 8;
  1300 			}
  1301 #endif
  1302 			break;
  1303 		}
  1304 	default:
  1305 		{
  1306 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1307 		}
  1308 	}
  1309 
  1310 	if (bit_count < 1)
  1311 	{
  1312 		bit_count = params[5] * 8;
  1313 	}
  1314 
  1315 	if (bit_count >= 17)
  1316 	{
  1317 		*(int32_t *)((uint8_t *)pEntity + offset) = params[4];
  1318 	}
  1319 	else if (bit_count >= 9)
  1320 	{
  1321 		*(int16_t *)((uint8_t *)pEntity + offset) = (int16_t)params[4];
  1322 	}
  1323 	else if (bit_count >= 2)
  1324 	{
  1325 		*(int8_t *)((uint8_t *)pEntity + offset) = (int8_t)params[4];
  1326 	}
  1327 	else
  1328 	{
  1329 		*(bool *)((uint8_t *)pEntity + offset) = params[4] ? true : false;
  1330 	}
  1331 	
  1332 	if (params[2] == Prop_Send && (pEdict != NULL))
  1333 	{
  1334 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1335 	}
  1336 
  1337 	return 0;
  1338 }
  1339 
  1340 static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1341 {
  1342 	CBaseEntity *pEntity;
  1343 	char *prop;
  1344 	int offset;
  1345 	int bit_count;
  1346 	edict_t *pEdict;
  1347 
  1348 	int element = 0;
  1349 	if (params[0] >= 4)
  1350 	{
  1351 		element = params[4];
  1352 	}
  1353 
  1354 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1355 	{
  1356 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1357 	}
  1358 
  1359 	pContext->LocalToString(params[3], &prop);
  1360 
  1361 	switch (params[2])
  1362 	{
  1363 	case Prop_Data:
  1364 		{
  1365 			typedescription_t *td;
  1366 
  1367 			FIND_PROP_DATA(td);
  1368 
  1369 			if (td->fieldType != FIELD_FLOAT
  1370 				&& td->fieldType != FIELD_TIME)
  1371 			{
  1372 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1373 					prop,
  1374 					td->fieldType,
  1375 					FIELD_FLOAT,
  1376 					FIELD_TIME);
  1377 			}
  1378 
  1379 			CHECK_SET_PROP_DATA_OFFSET();
  1380 
  1381 			break;
  1382 		}
  1383 	case Prop_Send:
  1384 		{
  1385 			FIND_PROP_SEND(DPT_Float, "float");
  1386 			break;
  1387 		}
  1388 	default:
  1389 		{
  1390 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1391 		}
  1392 	}
  1393 
  1394 	float val = *(float *)((uint8_t *)pEntity + offset);
  1395 
  1396 	return sp_ftoc(val);
  1397 }
  1398 
  1399 static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1400 {
  1401 	CBaseEntity *pEntity;
  1402 	char *prop;
  1403 	int offset;
  1404 	int bit_count;
  1405 	edict_t *pEdict;
  1406 
  1407 	int element = 0;
  1408 	if (params[0] >= 5)
  1409 	{
  1410 		element = params[5];
  1411 	}
  1412 
  1413 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1414 	{
  1415 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1416 	}
  1417 
  1418 	pContext->LocalToString(params[3], &prop);
  1419 
  1420 	switch (params[2])
  1421 	{
  1422 	case Prop_Data:
  1423 		{
  1424 			typedescription_t *td;
  1425 
  1426 			FIND_PROP_DATA(td);
  1427 
  1428 			if (td->fieldType != FIELD_FLOAT
  1429 				&& td->fieldType != FIELD_TIME)
  1430 			{
  1431 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1432 					prop,
  1433 					td->fieldType,
  1434 					FIELD_FLOAT,
  1435 					FIELD_TIME);
  1436 			}
  1437 
  1438 			CHECK_SET_PROP_DATA_OFFSET();
  1439 
  1440 			break;
  1441 		}
  1442 	case Prop_Send:
  1443 		{
  1444 			FIND_PROP_SEND(DPT_Float, "float");
  1445 			break;
  1446 		}
  1447 	default:
  1448 		{
  1449 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1450 		}
  1451 	}
  1452 
  1453 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[4]);
  1454 
  1455 	if (params[2] == Prop_Send && (pEdict != NULL))
  1456 	{
  1457 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1458 	}
  1459 
  1460 	return 1;
  1461 }
  1462 
  1463 static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1464 {
  1465 	CBaseEntity *pEntity;
  1466 	char *prop;
  1467 	int offset;
  1468 	int bit_count;
  1469 	edict_t *pEdict;
  1470 
  1471 	int element = 0;
  1472 	if (params[0] >= 4)
  1473  	{
  1474 		element = params[4];
  1475  	}
  1476 
  1477 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1478 	{
  1479 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1480 	}
  1481 
  1482 	pContext->LocalToString(params[3], &prop);
  1483 
  1484 	switch (params[2])
  1485 	{
  1486 	case Prop_Data:
  1487 		{
  1488 			typedescription_t *td;
  1489 
  1490 			FIND_PROP_DATA(td);
  1491 
  1492 			if (td->fieldType != FIELD_EHANDLE)
  1493 			{
  1494 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1495 					prop,
  1496 					td->fieldType,
  1497 					FIELD_EHANDLE);
  1498 			}
  1499 
  1500 			CHECK_SET_PROP_DATA_OFFSET();
  1501 
  1502 			break;
  1503 		}
  1504 	case Prop_Send:
  1505 		{
  1506 			FIND_PROP_SEND(DPT_Int, "integer");
  1507 			break;
  1508 		}
  1509 	default:
  1510 		{
  1511 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1512 		}
  1513 	}
  1514 
  1515 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1516 	CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(hndl.GetEntryIndex());
  1517 
  1518 	if (!pHandleEntity || hndl != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
  1519 		return -1;
  1520 
  1521 	return g_HL2.EntityToBCompatRef(pHandleEntity);
  1522 }
  1523 
  1524 static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1525 {
  1526 	CBaseEntity *pEntity;
  1527 	char *prop;
  1528 	int offset;
  1529 	int bit_count;
  1530 	edict_t *pEdict;
  1531 
  1532 	int element = 0;
  1533 	if (params[0] >= 5)
  1534  	{
  1535 		element = params[5];
  1536  	}
  1537 
  1538 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1539 	{
  1540 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1541 	}
  1542 
  1543 	pContext->LocalToString(params[3], &prop);
  1544 
  1545 	switch (params[2])
  1546 	{
  1547 	case Prop_Data:
  1548 		{
  1549 			typedescription_t *td;
  1550 
  1551 			FIND_PROP_DATA(td);
  1552 
  1553 			if (td->fieldType != FIELD_EHANDLE)
  1554 			{
  1555 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1556 					prop,
  1557 					td->fieldType,
  1558 					FIELD_EHANDLE);
  1559 			}
  1560 
  1561 			CHECK_SET_PROP_DATA_OFFSET();
  1562 
  1563 			break;
  1564 		}
  1565 	case Prop_Send:
  1566 		{
  1567 			FIND_PROP_SEND(DPT_Int, "integer");
  1568 			break;
  1569 		}
  1570 	default:
  1571 		{
  1572 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1573 		}
  1574 	}
  1575 
  1576 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1577 
  1578 	if (params[4] == -1)
  1579 	{
  1580 		hndl.Set(NULL);
  1581 	}
  1582 	else
  1583 	{
  1584 		CBaseEntity *pOther = GetEntity(params[4]);
  1585 
  1586 		if (!pOther)
  1587 		{
  1588 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[4]), params[4]);
  1589 		}
  1590 
  1591 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
  1592 		hndl.Set(pHandleEnt);
  1593 	}
  1594 
  1595 	if (params[2] == Prop_Send && (pEdict != NULL))
  1596 	{
  1597 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1598 	}
  1599 
  1600 	return 1;
  1601 }
  1602 
  1603 static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1604 {
  1605 	CBaseEntity *pEntity;
  1606 	char *prop;
  1607 	int offset;
  1608 	int bit_count;
  1609 	edict_t *pEdict;
  1610 
  1611 	int element = 0;
  1612 	if (params[0] >= 5)
  1613  	{
  1614 		element = params[5];
  1615  	}
  1616 	
  1617 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1618 	{
  1619 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1620 	}
  1621 
  1622 	pContext->LocalToString(params[3], &prop);
  1623 
  1624 	switch (params[2])
  1625 	{
  1626 	case Prop_Data:
  1627 		{
  1628 			typedescription_t *td;
  1629 			
  1630 			FIND_PROP_DATA(td);
  1631 
  1632 			if (td->fieldType != FIELD_VECTOR
  1633 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1634 			{
  1635 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1636 					prop,
  1637 					td->fieldType,
  1638 					FIELD_VECTOR,
  1639 					FIELD_POSITION_VECTOR);
  1640 			}
  1641 
  1642 			CHECK_SET_PROP_DATA_OFFSET();
  1643 
  1644 			break;
  1645 		}
  1646 	case Prop_Send:
  1647 		{
  1648 			FIND_PROP_SEND(DPT_Vector, "vector");
  1649 			break;
  1650 		}
  1651 	default:
  1652 		{
  1653 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1654 		}
  1655 	}
  1656 
  1657 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1658 
  1659 	cell_t *vec;
  1660 	pContext->LocalToPhysAddr(params[4], &vec);
  1661 
  1662 	vec[0] = sp_ftoc(v->x);
  1663 	vec[1] = sp_ftoc(v->y);
  1664 	vec[2] = sp_ftoc(v->z);
  1665 
  1666 	return 1;
  1667 }
  1668 
  1669 static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1670 {
  1671 	CBaseEntity *pEntity;
  1672 	char *prop;
  1673 	int offset;
  1674 	int bit_count;
  1675 	edict_t *pEdict;
  1676 
  1677 	int element = 0;
  1678 	if (params[0] >= 5)
  1679  	{
  1680 		element = params[5];
  1681  	}
  1682 	
  1683 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1684 	{
  1685 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1686 	}
  1687 
  1688 	pContext->LocalToString(params[3], &prop);
  1689 
  1690 	switch (params[2])
  1691 	{
  1692 	case Prop_Data:
  1693 		{
  1694 			typedescription_t *td;
  1695 			
  1696 			FIND_PROP_DATA(td);
  1697 
  1698 			if (td->fieldType != FIELD_VECTOR
  1699 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1700 			{
  1701 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1702 					prop,
  1703 					td->fieldType,
  1704 					FIELD_VECTOR,
  1705 					FIELD_POSITION_VECTOR);
  1706 			}
  1707 
  1708 			CHECK_SET_PROP_DATA_OFFSET();
  1709 
  1710 			break;
  1711 		}
  1712 	case Prop_Send:
  1713 		{
  1714 			FIND_PROP_SEND(DPT_Vector, "vector");			
  1715 			break;
  1716 		}
  1717 	default:
  1718 		{
  1719 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1720 		}
  1721 	}
  1722 
  1723 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1724 
  1725 	cell_t *vec;
  1726 	pContext->LocalToPhysAddr(params[4], &vec);
  1727 
  1728 	v->x = sp_ctof(vec[0]);
  1729 	v->y = sp_ctof(vec[1]);
  1730 	v->z = sp_ctof(vec[2]);
  1731 
  1732 	if (params[2] == Prop_Send && (pEdict != NULL))
  1733 	{
  1734 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1735 	}
  1736 
  1737 	return 1;
  1738 }
  1739 
  1740 static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
  1741 {
  1742 	CBaseEntity *pEntity;
  1743 	char *prop;
  1744 	int offset;
  1745 	edict_t *pEdict;
  1746 	bool bIsStringIndex;
  1747 
  1748 	int element = 0;
  1749 	if (params[0] >= 6)
  1750  	{
  1751 		element = params[6];
  1752  	}
  1753 	
  1754 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1755 	{
  1756 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1757 	}
  1758 
  1759 	pContext->LocalToString(params[3], &prop);
  1760 
  1761 	bIsStringIndex = false;
  1762 
  1763 	switch (params[2])
  1764 	{
  1765 	case Prop_Data:
  1766 		{
  1767 			typedescription_t *td;
  1768 			
  1769 			FIND_PROP_DATA(td);
  1770 
  1771 			if (td->fieldType != FIELD_CHARACTER
  1772 				&& td->fieldType != FIELD_STRING
  1773 				&& td->fieldType != FIELD_MODELNAME 
  1774 				&& td->fieldType != FIELD_SOUNDNAME)
  1775 			{
  1776 				return pContext->ThrowNativeError("Data field %s is not a string (%d != %d)", 
  1777 					prop,
  1778 					td->fieldType,
  1779 					FIELD_CHARACTER);
  1780 			}
  1781 
  1782 			bIsStringIndex = (td->fieldType != FIELD_CHARACTER);
  1783 
  1784 			if (bIsStringIndex && (element < 0 || element >= td->fieldSize))
  1785 			{
  1786 				return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).",
  1787 					element,
  1788 					prop,
  1789 					td->fieldSize);
  1790 			}
  1791 			else if (element != 0)
  1792 			{
  1793 				return pContext->ThrowNativeError("Prop %s is not an array. Element %d is invalid.",
  1794 					prop,
  1795 					element);
  1796 			}
  1797 
  1798 			offset = GetTypeDescOffs(td);
  1799 			if (bIsStringIndex)
  1800 			{
  1801 				offset += (element * (td->fieldSizeInBytes / td->fieldSize));
  1802 			}
  1803 			break;
  1804 		}
  1805 	case Prop_Send:
  1806 		{
  1807 			sm_sendprop_info_t info;
  1808 			
  1809 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1810 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1811 			if (!pNet)
  1812 			{
  1813 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1814 			}
  1815 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1816 			{
  1817 				const char *class_name = g_HL2.GetEntityClassname(pEntity);
  1818 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1819 					prop,
  1820 					params[1],
  1821 					((class_name) ? class_name : ""));
  1822 			}
  1823 
  1824 			offset = info.actual_offset;
  1825 
  1826 			if (info.prop->GetType() != DPT_String)
  1827 			{
  1828 				return pContext->ThrowNativeError("SendProp %s is not a string (%d != %d)",
  1829 					prop,
  1830 					info.prop->GetType(),
  1831 					DPT_String);
  1832 			}
  1833 			else if (element != 0)
  1834 			{
  1835 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.",
  1836 					prop,
  1837 					element);
  1838  			}
  1839 			
  1840 			break;
  1841 		}
  1842 	default:
  1843 		{
  1844 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1845 		}
  1846 	}
  1847 
  1848 	size_t len;
  1849 	const char *src; 
  1850 	
  1851 	if (!bIsStringIndex)
  1852 	{
  1853 		src = (char *)((uint8_t *)pEntity + offset);
  1854 	}
  1855 	else
  1856 	{
  1857 		string_t idx;
  1858 
  1859 		idx = *(string_t *)((uint8_t *)pEntity + offset);
  1860 		src = (idx == NULL_STRING) ? "" : STRING(idx);
  1861 	}
  1862 
  1863 	pContext->StringToLocalUTF8(params[4], params[5], src, &len);
  1864 
  1865 	return len;
  1866 }
  1867 
  1868 static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
  1869 {
  1870 	CBaseEntity *pEntity;
  1871 	char *prop;
  1872 	int offset;
  1873 	int maxlen;
  1874 	edict_t *pEdict;
  1875 
  1876 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1877 	{
  1878 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1879 	}
  1880 
  1881 	switch (params[2])
  1882 	{
  1883 	case Prop_Data:
  1884 		{
  1885 			datamap_t *pMap;
  1886 			typedescription_t *td;
  1887 			if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  1888 			{
  1889 				return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
  1890 			}
  1891 			pContext->LocalToString(params[3], &prop);
  1892 			if ((td=g_HL2.FindInDataMap(pMap, prop)) == NULL)
  1893 			{
  1894 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1895 			}
  1896 			if (td->fieldType != FIELD_CHARACTER)
  1897 			{
  1898 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1899 			}
  1900 			offset = GetTypeDescOffs(td);
  1901 			maxlen = td->fieldSize;
  1902 			break;
  1903 		}
  1904 	case Prop_Send:
  1905 		{
  1906 			char *prop;
  1907 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1908 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1909 			if (!pNet)
  1910 			{
  1911 				return pContext->ThrowNativeError("The edict is not networkable");
  1912 			}
  1913 			pContext->LocalToString(params[3], &prop);
  1914 			SendProp *pSend = g_HL2.FindInSendTable(pNet->GetServerClass()->GetName(), prop);
  1915 			if (!pSend)
  1916 			{
  1917 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1918 			}
  1919 			if (pSend->GetType() != DPT_String)
  1920 			{
  1921 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1922 			}
  1923 			offset = pSend->GetOffset();
  1924 			maxlen = DT_MAX_STRING_BUFFERSIZE;
  1925 			break;
  1926 		}
  1927 	default:
  1928 		{
  1929 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1930 		}
  1931 	}
  1932 
  1933 	char *src;
  1934 	char *dest = (char *)((uint8_t *)pEntity + offset);
  1935 
  1936 	pContext->LocalToString(params[4], &src);
  1937 	size_t len = strncopy(dest, src, maxlen);
  1938 
  1939 	if (params[2] == Prop_Send && (pEdict != NULL))
  1940 	{
  1941 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1942 	}
  1943 
  1944 	return len;
  1945 }
  1946 
  1947 static int32_t SDKEntFlagToSMEntFlag(int flag)
  1948 {
  1949 	switch (flag)
  1950 	{
  1951 		case FL_ONGROUND:
  1952 			return ENTFLAG_ONGROUND;
  1953 		case FL_DUCKING:
  1954 			return ENTFLAG_DUCKING;
  1955 		case FL_WATERJUMP:
  1956 			return ENTFLAG_WATERJUMP;
  1957 		case FL_ONTRAIN:
  1958 			return ENTFLAG_ONTRAIN;
  1959 		case FL_INRAIN:
  1960 			return ENTFLAG_INRAIN;
  1961 		case FL_FROZEN:
  1962 			return ENTFLAG_FROZEN;
  1963 		case FL_ATCONTROLS:
  1964 			return ENTFLAG_ATCONTROLS;
  1965 		case FL_CLIENT:
  1966 			return ENTFLAG_CLIENT;
  1967 		case FL_FAKECLIENT:
  1968 			return ENTFLAG_FAKECLIENT;
  1969 		case FL_INWATER:
  1970 			return ENTFLAG_INWATER;
  1971 		case FL_FLY:
  1972 			return ENTFLAG_FLY;
  1973 		case FL_SWIM:
  1974 			return ENTFLAG_SWIM;
  1975 		case FL_CONVEYOR:
  1976 			return ENTFLAG_CONVEYOR;
  1977 		case FL_NPC:
  1978 			return ENTFLAG_NPC;
  1979 		case FL_GODMODE:
  1980 			return ENTFLAG_GODMODE;
  1981 		case FL_NOTARGET:
  1982 			return ENTFLAG_NOTARGET;
  1983 		case FL_AIMTARGET:
  1984 			return ENTFLAG_AIMTARGET;
  1985 		case FL_PARTIALGROUND:
  1986 			return ENTFLAG_PARTIALGROUND;
  1987 		case FL_STATICPROP:
  1988 			return ENTFLAG_STATICPROP;
  1989 		case FL_GRAPHED:
  1990 			return ENTFLAG_GRAPHED;
  1991 		case FL_GRENADE:
  1992 			return ENTFLAG_GRENADE;
  1993 		case FL_STEPMOVEMENT:
  1994 			return ENTFLAG_STEPMOVEMENT;
  1995 		case FL_DONTTOUCH:
  1996 			return ENTFLAG_DONTTOUCH;
  1997 		case FL_BASEVELOCITY:
  1998 			return ENTFLAG_BASEVELOCITY;
  1999 		case FL_WORLDBRUSH:
  2000 			return ENTFLAG_WORLDBRUSH;
  2001 		case FL_OBJECT:
  2002 			return ENTFLAG_OBJECT;
  2003 		case FL_KILLME:
  2004 			return ENTFLAG_KILLME;
  2005 		case FL_ONFIRE:
  2006 			return ENTFLAG_ONFIRE;
  2007 		case FL_DISSOLVING:
  2008 			return ENTFLAG_DISSOLVING;
  2009 		case FL_TRANSRAGDOLL:
  2010 			return ENTFLAG_TRANSRAGDOLL;
  2011 		case FL_UNBLOCKABLE_BY_PLAYER:
  2012 			return ENTFLAG_UNBLOCKABLE_BY_PLAYER;
  2013 #if SOURCE_ENGINE == SE_ALIENSWARM
  2014 		case FL_FREEZING:
  2015 			return ENTFLAG_FREEZING;
  2016 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2017 		case FL_EP2V_UNKNOWN:
  2018 			return ENTFLAG_EP2V_UNKNOWN1;
  2019 #endif
  2020 		default:
  2021 			return 0;
  2022 	}
  2023 }
  2024 
  2025 static int32_t SMEntFlagToSDKEntFlag(int32_t flag)
  2026 {
  2027 	switch (flag)
  2028 	{
  2029 		case ENTFLAG_ONGROUND:
  2030 			return FL_ONGROUND;
  2031 		case ENTFLAG_DUCKING:
  2032 			return FL_DUCKING;
  2033 		case ENTFLAG_WATERJUMP:
  2034 			return FL_WATERJUMP;
  2035 		case ENTFLAG_ONTRAIN:
  2036 			return FL_ONTRAIN;
  2037 		case ENTFLAG_INRAIN:
  2038 			return FL_INRAIN;
  2039 		case ENTFLAG_FROZEN:
  2040 			return FL_FROZEN;
  2041 		case ENTFLAG_ATCONTROLS:
  2042 			return FL_ATCONTROLS;
  2043 		case ENTFLAG_CLIENT:
  2044 			return FL_CLIENT;
  2045 		case ENTFLAG_FAKECLIENT:
  2046 			return FL_FAKECLIENT;
  2047 		case ENTFLAG_INWATER:
  2048 			return FL_INWATER;
  2049 		case ENTFLAG_FLY:
  2050 			return FL_FLY;
  2051 		case ENTFLAG_SWIM:
  2052 			return FL_SWIM;
  2053 		case ENTFLAG_CONVEYOR:
  2054 			return FL_CONVEYOR;
  2055 		case ENTFLAG_NPC:
  2056 			return FL_NPC;
  2057 		case ENTFLAG_GODMODE:
  2058 			return FL_GODMODE;
  2059 		case ENTFLAG_NOTARGET:
  2060 			return FL_NOTARGET;
  2061 		case ENTFLAG_AIMTARGET:
  2062 			return FL_AIMTARGET;
  2063 		case ENTFLAG_PARTIALGROUND:
  2064 			return FL_PARTIALGROUND;
  2065 		case ENTFLAG_STATICPROP:
  2066 			return FL_STATICPROP;
  2067 		case ENTFLAG_GRAPHED:
  2068 			return FL_GRAPHED;
  2069 		case ENTFLAG_GRENADE:
  2070 			return FL_GRENADE;
  2071 		case ENTFLAG_STEPMOVEMENT:
  2072 			return FL_STEPMOVEMENT;
  2073 		case ENTFLAG_DONTTOUCH:
  2074 			return FL_DONTTOUCH;
  2075 		case ENTFLAG_BASEVELOCITY:
  2076 			return FL_BASEVELOCITY;
  2077 		case ENTFLAG_WORLDBRUSH:
  2078 			return FL_WORLDBRUSH;
  2079 		case ENTFLAG_OBJECT:
  2080 			return FL_OBJECT;
  2081 		case ENTFLAG_KILLME:
  2082 			return FL_KILLME;
  2083 		case ENTFLAG_ONFIRE:
  2084 			return FL_ONFIRE;
  2085 		case ENTFLAG_DISSOLVING:
  2086 			return FL_DISSOLVING;
  2087 		case ENTFLAG_TRANSRAGDOLL:
  2088 			return FL_TRANSRAGDOLL;
  2089 		case ENTFLAG_UNBLOCKABLE_BY_PLAYER:
  2090 			return FL_UNBLOCKABLE_BY_PLAYER;
  2091 #if SOURCE_ENGINE == SE_ALIENSWARM
  2092 		case ENTFLAG_FREEZING:
  2093 			return FL_FREEZING;
  2094 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE || SOURCE_ENGINE == SE_CSS
  2095 		case ENTFLAG_EP2V_UNKNOWN1:
  2096 			return FL_EP2V_UNKNOWN;
  2097 #endif
  2098 		default:
  2099 			return 0;
  2100 	}
  2101 }
  2102 
  2103 static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2104 {
  2105 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2106 	if (!pEntity)
  2107 	{
  2108 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2109 	}
  2110 
  2111 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2112 	if (!prop)
  2113 	{
  2114 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2115 	}
  2116 
  2117 	typedescription_t *td;
  2118 	datamap_t *pMap;
  2119 
  2120 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2121 	{
  2122 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2123 	}
  2124 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2125 	{
  2126 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2127 			prop,
  2128 			params[1]);
  2129 	}
  2130 
  2131 	int offset = GetTypeDescOffs(td);
  2132 
  2133 	int32_t actual_flags = *(int32_t *)((uint8_t *)pEntity + offset);
  2134 	int32_t sm_flags = 0;
  2135 
  2136 	for (int32_t i = 0; i < 32; i++)
  2137 	{
  2138 		int32_t flag = (1<<i);
  2139 		if ((actual_flags & flag) == flag)
  2140 		{
  2141 			sm_flags |= SDKEntFlagToSMEntFlag(flag);
  2142 		}
  2143 	}
  2144 
  2145 	return sm_flags;
  2146 }
  2147 
  2148 static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2149 {
  2150 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2151 	if (!pEntity)
  2152 	{
  2153 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2154 	}
  2155 
  2156 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2157 	if (!prop)
  2158 	{
  2159 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2160 	}
  2161 
  2162 	typedescription_t *td;
  2163 	datamap_t *pMap;
  2164 
  2165 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2166 	{
  2167 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2168 	}
  2169 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2170 	{
  2171 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2172 			prop,
  2173 			params[1]);
  2174 	}
  2175 
  2176 	int offset = GetTypeDescOffs(td);
  2177 
  2178 	int32_t sm_flags = params[2];
  2179 	int32_t actual_flags = 0;
  2180 
  2181 	for (int32_t i = 0; i < 32; i++)
  2182 	{
  2183 		int32_t flag = (1<<i);
  2184 		if ((sm_flags & flag) == flag)
  2185 		{
  2186 			actual_flags |= SMEntFlagToSDKEntFlag(flag);
  2187 		}
  2188 	}
  2189 
  2190 	*(int32_t *)((uint8_t *)pEntity + offset) = actual_flags;
  2191 
  2192 	return 0;
  2193 }
  2194 
  2195 static cell_t GetEntityAddress(IPluginContext *pContext, const cell_t *params)
  2196 {
  2197 	CBaseEntity * pEntity = GetEntity(params[1]);
  2198 	if (!pEntity)
  2199 	{
  2200 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2201 	}
  2202 	return reinterpret_cast<cell_t>(pEntity);
  2203 }
  2204 
  2205 REGISTER_NATIVES(entityNatives)
  2206 {
  2207 	{"ChangeEdictState",		ChangeEdictState},
  2208 	{"CreateEdict",				CreateEdict},
  2209 	{"FindDataMapOffs",			FindDataMapOffs},
  2210 	{"FindSendPropInfo",		FindSendPropInfo},
  2211 	{"FindSendPropOffs",		FindSendPropOffs},
  2212 	{"GetEdictClassname",		GetEdictClassname},
  2213 	{"GetEdictFlags",			GetEdictFlags},
  2214 	{"GetEntData",				GetEntData},
  2215 	{"GetEntDataEnt",			GetEntDataEnt},		/** DEPRECATED */
  2216 	{"GetEntDataEnt2",			GetEntDataEnt2},
  2217 	{"GetEntDataFloat",			GetEntDataFloat},
  2218 	{"GetEntDataString",		GetEntDataString},
  2219 	{"GetEntDataVector",		GetEntDataVector},
  2220 	{"GetEntProp",				GetEntProp},
  2221 	{"GetEntPropArraySize",		GetEntPropArraySize},
  2222 	{"GetEntPropEnt",			GetEntPropEnt},
  2223 	{"GetEntPropFloat",			GetEntPropFloat},
  2224 	{"GetEntPropString",		GetEntPropString},
  2225 	{"GetEntPropVector",		GetEntPropVector},
  2226 	{"GetEntityCount",			GetEntityCount},
  2227 	{"GetEntityFlags",			GetEntityFlags},
  2228 	{"GetEntityNetClass",		GetEntityNetClass},
  2229 	{"GetMaxEntities",			GetMaxEntities},
  2230 	{"IsEntNetworkable",		IsEntNetworkable},
  2231 	{"IsValidEdict",			IsValidEdict},
  2232 	{"IsValidEntity",			IsValidEntity},
  2233 	{"RemoveEdict",				RemoveEdict},
  2234 	{"SetEdictFlags",			SetEdictFlags},
  2235 	{"SetEntData",				SetEntData},
  2236 	{"SetEntDataEnt",			SetEntDataEnt},		/** DEPRECATED */
  2237 	{"SetEntDataEnt2",			SetEntDataEnt2},
  2238 	{"SetEntDataFloat",			SetEntDataFloat},
  2239 	{"SetEntDataVector",		SetEntDataVector},
  2240 	{"SetEntDataString",		SetEntDataString},
  2241 	{"SetEntityFlags",			SetEntityFlags},
  2242 	{"SetEntProp",				SetEntProp},
  2243 	{"SetEntPropEnt",			SetEntPropEnt},
  2244 	{"SetEntPropFloat",			SetEntPropFloat},
  2245 	{"SetEntPropString",		SetEntPropString},
  2246 	{"SetEntPropVector",		SetEntPropVector},
  2247 	{"GetEntityAddress",		GetEntityAddress},
  2248 	{NULL,						NULL}
  2249 };