core/smn_entities.cpp
author Asher Baker <asherkin@gmail.com>
Sun Aug 14 21:42:32 2011 +0100 (2011-08-14)
changeset 3367 18eb63085ca4
parent 3293 f688cf4a4201
child 3398 fcf4e97e0844
permissions -rw-r--r--
Added a reset argument to 'sm cvars' (bug 5043, 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 = pEdict->GetClassName();
   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 
   624 	int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
   625 	return g_HL2.ReferenceToBCompatRef(ref);
   626 }
   627 
   628 /* THIS GUY IS DEPRECATED. */
   629 static cell_t SetEntDataEnt(IPluginContext *pContext, const cell_t *params)
   630 {
   631 	CBaseEntity *pEntity;
   632 	edict_t *pEdict;
   633 
   634 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   635 	{
   636 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   637 	}
   638 
   639 	int offset = params[2];
   640 	if (offset <= 0 || offset > 32768)
   641 	{
   642 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   643 	}
   644 
   645 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   646 
   647 	if (params[3] == 0 || (unsigned)params[3] == INVALID_EHANDLE_INDEX)
   648 	{
   649 		hndl.Set(NULL);
   650 	} else {
   651 		CBaseEntity *pOther = GetEntity(params[3]);
   652 
   653 		if (!pOther)
   654 		{
   655 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   656 		}
   657 
   658 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   659 		hndl.Set(pHandleEnt);
   660 	}
   661 
   662 	if (params[4] && (pEdict != NULL))
   663 	{
   664 		g_HL2.SetEdictStateChanged(pEdict, offset);
   665 	}
   666 
   667 	return 1;
   668 }
   669 
   670 static cell_t SetEntDataEnt2(IPluginContext *pContext, const cell_t *params)
   671 {
   672 	CBaseEntity *pEntity;
   673 	edict_t *pEdict;
   674 
   675 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   676 	{
   677 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   678 	}
   679 
   680 	int offset = params[2];
   681 	if (offset <= 0 || offset > 32768)
   682 	{
   683 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   684 	}
   685 
   686 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
   687 
   688 	if ((unsigned)params[3] == INVALID_EHANDLE_INDEX)
   689 	{
   690 		hndl.Set(NULL);
   691 	}
   692 	else
   693 	{
   694 		CBaseEntity *pOther = GetEntity(params[3]);
   695 
   696 		if (!pOther)
   697 		{
   698 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[3]), params[3]);
   699 		}
   700 
   701 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
   702 		hndl.Set(pHandleEnt);
   703 	}
   704 
   705 	if (params[4] && (pEdict != NULL))
   706 	{
   707 		g_HL2.SetEdictStateChanged(pEdict, offset);
   708 	}
   709 
   710 	return 1;
   711 }
   712 
   713 static cell_t ChangeEdictState(IPluginContext *pContext, const cell_t *params)
   714 {
   715 	edict_t *pEdict = GetEdict(params[1]);
   716 
   717 	if (!pEdict)
   718 	{
   719 		return pContext->ThrowNativeError("Edict %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   720 	}
   721 
   722 	g_HL2.SetEdictStateChanged(pEdict, params[2]);
   723 
   724 	return 1;
   725 }
   726 
   727 static cell_t FindSendPropOffs(IPluginContext *pContext, const cell_t *params)
   728 {
   729 	char *cls, *prop;
   730 	pContext->LocalToString(params[1], &cls);
   731 	pContext->LocalToString(params[2], &prop);
   732 
   733 	SendProp *pSend = g_HL2.FindInSendTable(cls, prop);
   734 
   735 	if (!pSend)
   736 	{
   737 		return -1;
   738 	}
   739 
   740 	return pSend->GetOffset();
   741 }
   742 
   743 static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
   744 {
   745 	char *cls, *prop;
   746 	sm_sendprop_info_t info;
   747 	cell_t *pType, *pBits, *pLocal;
   748 
   749 	pContext->LocalToString(params[1], &cls);
   750 	pContext->LocalToString(params[2], &prop);
   751 
   752 	if (!g_HL2.FindSendPropInfo(cls, prop, &info))
   753 	{
   754 		return -1;
   755 	}
   756 
   757 	pContext->LocalToPhysAddr(params[3], &pType);
   758 	pContext->LocalToPhysAddr(params[4], &pBits);
   759 	pContext->LocalToPhysAddr(params[5], &pLocal);
   760 
   761 	switch (info.prop->GetType())
   762 	{
   763 	case DPT_Int:
   764 		{
   765 			*pType = PropField_Integer;
   766 			break;
   767 		}
   768 	case DPT_Float:
   769 		{
   770 			*pType = PropField_Float;
   771 			break;
   772 		}
   773 	case DPT_String:
   774 		{
   775 			*pType = PropField_String;
   776 			break;
   777 		}
   778 	case DPT_Vector:
   779 		{
   780 			*pType = PropField_Vector;
   781 			break;
   782 		}
   783 	default:
   784 		{
   785 			*pType = PropField_Unsupported;
   786 			break;
   787 		}
   788 	}
   789 
   790 	*pBits = info.prop->m_nBits;
   791 	*pLocal = info.prop->GetOffset();
   792 
   793 	return info.actual_offset;
   794 }
   795 
   796 static cell_t FindDataMapOffs(IPluginContext *pContext, const cell_t *params)
   797 {
   798 	CBaseEntity *pEntity;
   799 	datamap_t *pMap;
   800 	typedescription_t *td;
   801 	char *offset;
   802 	pEntity = GetEntity(params[1]);
   803 
   804 	if (!pEntity)
   805 	{
   806 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   807 	}
   808 
   809 	if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
   810 	{
   811 		return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
   812 	}
   813 
   814 	pContext->LocalToString(params[2], &offset);
   815 	if ((td=g_HL2.FindInDataMap(pMap, offset)) == NULL)
   816 	{
   817 		return -1;
   818 	}
   819 
   820 	if (params[0] == 4)
   821 	{
   822 		cell_t *pType, *pSize;
   823 
   824 		pContext->LocalToPhysAddr(params[3], &pType);
   825 		pContext->LocalToPhysAddr(params[4], &pSize);
   826 
   827 		switch (td->fieldType)
   828 		{
   829 		case FIELD_TICK:
   830 		case FIELD_MODELINDEX:
   831 		case FIELD_MATERIALINDEX:
   832 		case FIELD_INTEGER:
   833 		case FIELD_COLOR32:
   834 			{
   835 				*pType = PropField_Integer;
   836 				*pSize = 32;
   837 				break;
   838 			}
   839 		case FIELD_VECTOR:
   840 		case FIELD_POSITION_VECTOR:
   841 			{
   842 				*pType = PropField_Vector;
   843 				*pSize = 12;
   844 				break;
   845 			}
   846 		case FIELD_SHORT:
   847 			{
   848 				*pType = PropField_Integer;
   849 				*pSize = 16;
   850 				break;
   851 			}
   852 		case FIELD_BOOLEAN:
   853 			{
   854 				*pType = PropField_Integer;
   855 				*pSize = 1;
   856 				break;
   857 			}
   858 		case FIELD_CHARACTER:
   859 			{
   860 				if (td->fieldSize == 1)
   861 				{
   862 					*pType = PropField_Integer;
   863 					*pSize = 8;
   864 				}
   865 				else
   866 				{
   867 					*pType = PropField_String;
   868 					*pSize = 8 * td->fieldSize;
   869 				}
   870 				break;
   871 			}
   872 		case FIELD_MODELNAME:
   873 		case FIELD_SOUNDNAME:
   874 		case FIELD_STRING:
   875 			{
   876 				*pSize = sizeof(string_t);
   877 				*pType = PropField_String_T;
   878 				break;
   879 			}
   880 		case FIELD_FLOAT:
   881 		case FIELD_TIME:
   882 			{
   883 				*pType = PropField_Float;
   884 				*pSize = 32;
   885 				break;
   886 			}
   887 		case FIELD_EHANDLE:
   888 			{
   889 				*pType = PropField_Entity;
   890 				*pSize = 32;
   891 				break;
   892 			}
   893 		default:
   894 			{
   895 				*pType = PropField_Unsupported;
   896 				*pSize = 0;
   897 			}
   898 		}
   899 	}
   900 
   901 	return GetTypeDescOffs(td);
   902 }
   903 
   904 static cell_t GetEntDataString(IPluginContext *pContext, const cell_t *params)
   905 {
   906 	CBaseEntity *pEntity = GetEntity(params[1]);
   907 
   908 	if (!pEntity)
   909 	{
   910 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   911 	}
   912 
   913 	int offset = params[2];
   914 	if (offset < 0 || offset > 32768)
   915 	{
   916 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   917 	}
   918 
   919 	size_t len;
   920 	char *src = (char *)((uint8_t *)pEntity + offset);
   921 	pContext->StringToLocalUTF8(params[3], params[4], src, &len);
   922 
   923 	return len;
   924 }
   925 
   926 static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
   927 {
   928 	CBaseEntity *pEntity;
   929 	edict_t *pEdict;
   930 
   931 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
   932 	{
   933 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
   934 	}
   935 
   936 	int offset = params[2];
   937 	if (offset < 0 || offset > 32768)
   938 	{
   939 		return pContext->ThrowNativeError("Offset %d is invalid", offset);
   940 	}
   941 
   942 	char *src;
   943 	char *dest = (char *)((uint8_t *)pEntity + offset);
   944 
   945 	pContext->LocalToString(params[3], &src);
   946 	size_t len = strncopy(dest, src, params[4]);
   947 
   948 	if (params[5] && (pEdict != NULL))
   949 	{
   950 		g_HL2.SetEdictStateChanged(pEdict, offset);
   951 	}
   952 
   953 	return len;
   954 }
   955 
   956 #define FIND_PROP_DATA(td) \
   957 	datamap_t *pMap; \
   958 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL) \
   959 	{ \
   960 		return pContext->ThrowNativeError("Could not retrieve datamap"); \
   961 	} \
   962 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL) \
   963 	{ \
   964 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   965 			prop, \
   966 			params[1], \
   967 			class_name); \
   968 	}
   969 
   970 #define CHECK_SET_PROP_DATA_OFFSET() \
   971 	if (element >= td->fieldSize) \
   972 	{ \
   973 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
   974 			element, \
   975 			prop, \
   976 			td->fieldSize); \
   977 	} \
   978 	\
   979 	offset = GetTypeDescOffs(td) + (element * (td->fieldSizeInBytes / td->fieldSize));
   980 
   981 #define FIND_PROP_SEND(type, type_name) \
   982 	sm_sendprop_info_t info;\
   983 	IServerUnknown *pUnk = (IServerUnknown *)pEntity; \
   984 	IServerNetworkable *pNet = pUnk->GetNetworkable(); \
   985 	if (!pNet) \
   986 	{ \
   987 		return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]); \
   988 	} \
   989 	if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info)) \
   990 	{ \
   991 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)", \
   992 			prop, \
   993 			params[1], \
   994 			class_name); \
   995 	} \
   996 	\
   997 	offset = info.actual_offset; \
   998 	bit_count = info.prop->m_nBits; \
   999 	\
  1000 	switch (info.prop->GetType()) \
  1001 	{ \
  1002 	case type: \
  1003 		{ \
  1004 			if (element > 0) \
  1005 			{ \
  1006 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.", \
  1007 					prop, \
  1008 					element); \
  1009 			} \
  1010 			break; \
  1011 		} \
  1012 	case DPT_DataTable: \
  1013 		{ \
  1014 			SendProp *pProp; \
  1015 			FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name); \
  1016 			\
  1017 			offset += pProp->GetOffset(); \
  1018 			bit_count = pProp->m_nBits; \
  1019 			break; \
  1020 		} \
  1021 	default: \
  1022 		{ \
  1023 			return pContext->ThrowNativeError("SendProp %s type is not " type_name " (%d != %d)", \
  1024 				prop, \
  1025 				info.prop->GetType(), \
  1026 				type); \
  1027 		} \
  1028 	} \
  1029 
  1030 #define FIND_PROP_SEND_IN_SENDTABLE(info, pProp, element, type, type_name) \
  1031 	SendTable *pTable = info.prop->GetDataTable(); \
  1032 	if (!pTable) \
  1033 	{ \
  1034 		return pContext->ThrowNativeError("Error looking up DataTable for prop %s", \
  1035 			prop); \
  1036 	} \
  1037 	\
  1038 	int elementCount = pTable->GetNumProps(); \
  1039 	if (element >= elementCount) \
  1040 	{ \
  1041 		return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).", \
  1042 			element, \
  1043 			prop, \
  1044 			elementCount); \
  1045 	} \
  1046 	\
  1047 	pProp = pTable->GetProp(element); \
  1048 	if (pProp->GetType() != type) \
  1049 	{ \
  1050 		return pContext->ThrowNativeError("SendProp %s type is not " type_name " ([%d,%d] != %d)", \
  1051 			prop, \
  1052 			info.prop->GetType(), \
  1053 			info.prop->m_nBits, \
  1054 			type); \
  1055 	}
  1056 
  1057 
  1058 inline int MatchFieldAsInteger(int field_type)
  1059 {
  1060 	switch (field_type)
  1061 	{
  1062 	case FIELD_TICK:
  1063 	case FIELD_MODELINDEX:
  1064 	case FIELD_MATERIALINDEX:
  1065 	case FIELD_INTEGER:
  1066 	case FIELD_COLOR32:
  1067 		return 32;
  1068 	case FIELD_SHORT:
  1069 		return 16;
  1070 	case FIELD_CHARACTER:
  1071 		return 8;
  1072 	case FIELD_BOOLEAN:
  1073 		return 1;
  1074 	default:
  1075 		return 0;
  1076 	}
  1077 
  1078 	return 0;
  1079 }
  1080 
  1081 static cell_t GetEntPropArraySize(IPluginContext *pContext, const cell_t *params)
  1082 {
  1083 	CBaseEntity *pEntity;
  1084 	char *prop;
  1085 	const char *class_name;
  1086 	edict_t *pEdict;
  1087 
  1088 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1089 	{
  1090 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1091 	}
  1092 
  1093 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1094 	{
  1095 		class_name = "";
  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 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1123 					prop,
  1124 					params[1],
  1125 					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 	const char *class_name;
  1156 	edict_t *pEdict;
  1157 	int bit_count;
  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 	/* TODO: Find a way to lookup classname without having an edict - Is this a guaranteed prop? */
  1171 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1172 	{
  1173 		class_name = "";
  1174 	}
  1175 
  1176 	pContext->LocalToString(params[3], &prop);
  1177 
  1178 	switch (params[2])
  1179 	{
  1180 	case Prop_Data:
  1181 		{
  1182 			typedescription_t *td;
  1183 
  1184 			FIND_PROP_DATA(td);
  1185 
  1186 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1187 			{
  1188 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1189 					prop,
  1190 					td->fieldType);
  1191 			}
  1192 
  1193 			CHECK_SET_PROP_DATA_OFFSET();
  1194 
  1195 			break;
  1196 		}
  1197 	case Prop_Send:
  1198 		{
  1199 			FIND_PROP_SEND(DPT_Int, "integer");
  1200 			break;
  1201 		}
  1202 	default:
  1203 		{
  1204 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1205 		}
  1206 	}
  1207 
  1208 	if (bit_count < 1)
  1209 	{
  1210 		bit_count = params[4] * 8;
  1211 	}
  1212 
  1213 	if (bit_count >= 17)
  1214 	{
  1215 		return *(int32_t *)((uint8_t *)pEntity + offset);
  1216 	}
  1217 	else if (bit_count >= 9)
  1218 	{
  1219 		return *(int16_t *)((uint8_t *)pEntity + offset);
  1220 	}
  1221 	else if (bit_count >= 2)
  1222 	{
  1223 		return *(int8_t *)((uint8_t *)pEntity + offset);
  1224 	}
  1225 	else
  1226 	{
  1227 		return *(bool *)((uint8_t *)pEntity + offset) ? 1 : 0;
  1228 	}
  1229 
  1230 	return 0;
  1231 }
  1232 
  1233 static cell_t SetEntProp(IPluginContext *pContext, const cell_t *params)
  1234 {
  1235 	CBaseEntity *pEntity;
  1236 	char *prop;
  1237 	int offset;
  1238 	const char *class_name;
  1239 	edict_t *pEdict;
  1240 	int bit_count;
  1241 
  1242 	int element = 0;
  1243 	if (params[0] >= 6)
  1244 	{
  1245 		element = params[6];
  1246 	}
  1247 
  1248 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1249 	{
  1250 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1251 	}
  1252 
  1253 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1254 	{
  1255 		class_name = "";
  1256 	}
  1257 
  1258 	pContext->LocalToString(params[3], &prop);
  1259 
  1260 	switch (params[2])
  1261 	{
  1262 	case Prop_Data:
  1263 		{
  1264 			typedescription_t *td;
  1265 
  1266 			FIND_PROP_DATA(td);
  1267 
  1268 			if ((bit_count = MatchFieldAsInteger(td->fieldType)) == 0)
  1269 			{
  1270 				return pContext->ThrowNativeError("Data field %s is not an integer (%d)", 
  1271 					prop,
  1272 					td->fieldType);
  1273 			}
  1274 
  1275 			CHECK_SET_PROP_DATA_OFFSET();
  1276 
  1277 			break;
  1278 		}
  1279 	case Prop_Send:
  1280 		{
  1281 			FIND_PROP_SEND(DPT_Int, "integer");
  1282 			break;
  1283 		}
  1284 	default:
  1285 		{
  1286 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1287 		}
  1288 	}
  1289 
  1290 	if (bit_count < 1)
  1291 	{
  1292 		bit_count = params[5] * 8;
  1293 	}
  1294 
  1295 	if (bit_count >= 17)
  1296 	{
  1297 		*(int32_t *)((uint8_t *)pEntity + offset) = params[4];
  1298 	}
  1299 	else if (bit_count >= 9)
  1300 	{
  1301 		*(int16_t *)((uint8_t *)pEntity + offset) = (int16_t)params[4];
  1302 	}
  1303 	else if (bit_count >= 2)
  1304 	{
  1305 		*(int8_t *)((uint8_t *)pEntity + offset) = (int8_t)params[4];
  1306 	}
  1307 	else
  1308 	{
  1309 		*(bool *)((uint8_t *)pEntity + offset) = params[4] ? true : false;
  1310 	}
  1311 	
  1312 	if (params[2] == Prop_Send && (pEdict != NULL))
  1313 	{
  1314 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1315 	}
  1316 
  1317 	return 0;
  1318 }
  1319 
  1320 static cell_t GetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1321 {
  1322 	CBaseEntity *pEntity;
  1323 	char *prop;
  1324 	int offset;
  1325 	int bit_count;
  1326 	const char *class_name;
  1327 	edict_t *pEdict;
  1328 
  1329 	int element = 0;
  1330 	if (params[0] >= 4)
  1331 	{
  1332 		element = params[4];
  1333 	}
  1334 
  1335 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1336 	{
  1337 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1338 	}
  1339 
  1340 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1341 	{
  1342 		class_name = "";
  1343 	}
  1344 
  1345 	pContext->LocalToString(params[3], &prop);
  1346 
  1347 	switch (params[2])
  1348 	{
  1349 	case Prop_Data:
  1350 		{
  1351 			typedescription_t *td;
  1352 
  1353 			FIND_PROP_DATA(td);
  1354 
  1355 			if (td->fieldType != FIELD_FLOAT
  1356 				&& td->fieldType != FIELD_TIME)
  1357 			{
  1358 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1359 					prop,
  1360 					td->fieldType,
  1361 					FIELD_FLOAT,
  1362 					FIELD_TIME);
  1363 			}
  1364 
  1365 			CHECK_SET_PROP_DATA_OFFSET();
  1366 
  1367 			break;
  1368 		}
  1369 	case Prop_Send:
  1370 		{
  1371 			FIND_PROP_SEND(DPT_Float, "float");
  1372 			break;
  1373 		}
  1374 	default:
  1375 		{
  1376 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1377 		}
  1378 	}
  1379 
  1380 	float val = *(float *)((uint8_t *)pEntity + offset);
  1381 
  1382 	return sp_ftoc(val);
  1383 }
  1384 
  1385 static cell_t SetEntPropFloat(IPluginContext *pContext, const cell_t *params)
  1386 {
  1387 	CBaseEntity *pEntity;
  1388 	char *prop;
  1389 	int offset;
  1390 	int bit_count;
  1391 	const char *class_name;
  1392 	edict_t *pEdict;
  1393 
  1394 	int element = 0;
  1395 	if (params[0] >= 5)
  1396 	{
  1397 		element = params[5];
  1398 	}
  1399 
  1400 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1401 	{
  1402 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1403 	}
  1404 
  1405 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1406 	{
  1407 		class_name = "";
  1408 	}
  1409 
  1410 	pContext->LocalToString(params[3], &prop);
  1411 
  1412 	switch (params[2])
  1413 	{
  1414 	case Prop_Data:
  1415 		{
  1416 			typedescription_t *td;
  1417 
  1418 			FIND_PROP_DATA(td);
  1419 
  1420 			if (td->fieldType != FIELD_FLOAT
  1421 				&& td->fieldType != FIELD_TIME)
  1422 			{
  1423 				return pContext->ThrowNativeError("Data field %s is not a float (%d != [%d,%d])", 
  1424 					prop,
  1425 					td->fieldType,
  1426 					FIELD_FLOAT,
  1427 					FIELD_TIME);
  1428 			}
  1429 
  1430 			CHECK_SET_PROP_DATA_OFFSET();
  1431 
  1432 			break;
  1433 		}
  1434 	case Prop_Send:
  1435 		{
  1436 			FIND_PROP_SEND(DPT_Float, "float");
  1437 			break;
  1438 		}
  1439 	default:
  1440 		{
  1441 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1442 		}
  1443 	}
  1444 
  1445 	*(float *)((uint8_t *)pEntity + offset) = sp_ctof(params[4]);
  1446 
  1447 	if (params[2] == Prop_Send && (pEdict != NULL))
  1448 	{
  1449 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1450 	}
  1451 
  1452 	return 1;
  1453 }
  1454 
  1455 static cell_t GetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1456 {
  1457 	CBaseEntity *pEntity;
  1458 	char *prop;
  1459 	int offset;
  1460 	int bit_count;
  1461 	const char *class_name;
  1462 	edict_t *pEdict;
  1463 
  1464 	int element = 0;
  1465 	if (params[0] >= 4)
  1466  	{
  1467 		element = params[4];
  1468  	}
  1469 
  1470 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1471 	{
  1472 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1473 	}
  1474 
  1475 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1476 	{
  1477 		class_name = "";
  1478 	}
  1479 
  1480 	pContext->LocalToString(params[3], &prop);
  1481 
  1482 	switch (params[2])
  1483 	{
  1484 	case Prop_Data:
  1485 		{
  1486 			typedescription_t *td;
  1487 
  1488 			FIND_PROP_DATA(td);
  1489 
  1490 			if (td->fieldType != FIELD_EHANDLE)
  1491 			{
  1492 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1493 					prop,
  1494 					td->fieldType,
  1495 					FIELD_EHANDLE);
  1496 			}
  1497 
  1498 			CHECK_SET_PROP_DATA_OFFSET();
  1499 
  1500 			break;
  1501 		}
  1502 	case Prop_Send:
  1503 		{
  1504 			FIND_PROP_SEND(DPT_Int, "integer");
  1505 			break;
  1506 		}
  1507 	default:
  1508 		{
  1509 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1510 		}
  1511 	}
  1512 
  1513 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1514 
  1515 	int ref = g_HL2.IndexToReference(hndl.GetEntryIndex());
  1516 	return g_HL2.ReferenceToBCompatRef(ref);
  1517 }
  1518 
  1519 static cell_t SetEntPropEnt(IPluginContext *pContext, const cell_t *params)
  1520 {
  1521 	CBaseEntity *pEntity;
  1522 	char *prop;
  1523 	int offset;
  1524 	int bit_count;
  1525 	const char *class_name;
  1526 	edict_t *pEdict;
  1527 
  1528 	int element = 0;
  1529 	if (params[0] >= 5)
  1530  	{
  1531 		element = params[5];
  1532  	}
  1533 
  1534 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1535 	{
  1536 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1537 	}
  1538 
  1539 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1540 	{
  1541 		class_name = "";
  1542 	}
  1543 
  1544 	pContext->LocalToString(params[3], &prop);
  1545 
  1546 	switch (params[2])
  1547 	{
  1548 	case Prop_Data:
  1549 		{
  1550 			typedescription_t *td;
  1551 
  1552 			FIND_PROP_DATA(td);
  1553 
  1554 			if (td->fieldType != FIELD_EHANDLE)
  1555 			{
  1556 				return pContext->ThrowNativeError("Data field %s is not an entity (%d != %d)", 
  1557 					prop,
  1558 					td->fieldType,
  1559 					FIELD_EHANDLE);
  1560 			}
  1561 
  1562 			CHECK_SET_PROP_DATA_OFFSET();
  1563 
  1564 			break;
  1565 		}
  1566 	case Prop_Send:
  1567 		{
  1568 			FIND_PROP_SEND(DPT_Int, "integer");
  1569 			break;
  1570 		}
  1571 	default:
  1572 		{
  1573 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1574 		}
  1575 	}
  1576 
  1577 	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEntity + offset);
  1578 
  1579 	if (params[4] == -1)
  1580 	{
  1581 		hndl.Set(NULL);
  1582 	}
  1583 	else
  1584 	{
  1585 		CBaseEntity *pOther = GetEntity(params[4]);
  1586 
  1587 		if (!pOther)
  1588 		{
  1589 			return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[4]), params[4]);
  1590 		}
  1591 
  1592 		IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
  1593 		hndl.Set(pHandleEnt);
  1594 	}
  1595 
  1596 	if (params[2] == Prop_Send && (pEdict != NULL))
  1597 	{
  1598 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1599 	}
  1600 
  1601 	return 1;
  1602 }
  1603 
  1604 static cell_t GetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1605 {
  1606 	CBaseEntity *pEntity;
  1607 	char *prop;
  1608 	int offset;
  1609 	int bit_count;
  1610 	const char *class_name;
  1611 	edict_t *pEdict;
  1612 
  1613 	int element = 0;
  1614 	if (params[0] >= 5)
  1615  	{
  1616 		element = params[5];
  1617  	}
  1618 	
  1619 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1620 	{
  1621 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1622 	}
  1623 
  1624 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1625 	{
  1626 		class_name = "";
  1627 	}
  1628 
  1629 	pContext->LocalToString(params[3], &prop);
  1630 
  1631 	switch (params[2])
  1632 	{
  1633 	case Prop_Data:
  1634 		{
  1635 			typedescription_t *td;
  1636 			
  1637 			FIND_PROP_DATA(td);
  1638 
  1639 			if (td->fieldType != FIELD_VECTOR
  1640 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1641 			{
  1642 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1643 					prop,
  1644 					td->fieldType,
  1645 					FIELD_VECTOR,
  1646 					FIELD_POSITION_VECTOR);
  1647 			}
  1648 
  1649 			CHECK_SET_PROP_DATA_OFFSET();
  1650 
  1651 			break;
  1652 		}
  1653 	case Prop_Send:
  1654 		{
  1655 			FIND_PROP_SEND(DPT_Vector, "vector");
  1656 			break;
  1657 		}
  1658 	default:
  1659 		{
  1660 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1661 		}
  1662 	}
  1663 
  1664 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1665 
  1666 	cell_t *vec;
  1667 	pContext->LocalToPhysAddr(params[4], &vec);
  1668 
  1669 	vec[0] = sp_ftoc(v->x);
  1670 	vec[1] = sp_ftoc(v->y);
  1671 	vec[2] = sp_ftoc(v->z);
  1672 
  1673 	return 1;
  1674 }
  1675 
  1676 static cell_t SetEntPropVector(IPluginContext *pContext, const cell_t *params)
  1677 {
  1678 	CBaseEntity *pEntity;
  1679 	char *prop;
  1680 	int offset;
  1681 	int bit_count;
  1682 	const char *class_name;
  1683 	edict_t *pEdict;
  1684 
  1685 	int element = 0;
  1686 	if (params[0] >= 5)
  1687  	{
  1688 		element = params[5];
  1689  	}
  1690 	
  1691 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1692 	{
  1693 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1694 	}
  1695 
  1696 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1697 	{
  1698 		class_name = "";
  1699 	}
  1700 
  1701 	pContext->LocalToString(params[3], &prop);
  1702 
  1703 	switch (params[2])
  1704 	{
  1705 	case Prop_Data:
  1706 		{
  1707 			typedescription_t *td;
  1708 			
  1709 			FIND_PROP_DATA(td);
  1710 
  1711 			if (td->fieldType != FIELD_VECTOR
  1712 				&& td->fieldType != FIELD_POSITION_VECTOR)
  1713 			{
  1714 				return pContext->ThrowNativeError("Data field %s is not a vector (%d != [%d,%d])", 
  1715 					prop,
  1716 					td->fieldType,
  1717 					FIELD_VECTOR,
  1718 					FIELD_POSITION_VECTOR);
  1719 			}
  1720 
  1721 			CHECK_SET_PROP_DATA_OFFSET();
  1722 
  1723 			break;
  1724 		}
  1725 	case Prop_Send:
  1726 		{
  1727 			FIND_PROP_SEND(DPT_Vector, "vector");			
  1728 			break;
  1729 		}
  1730 	default:
  1731 		{
  1732 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1733 		}
  1734 	}
  1735 
  1736 	Vector *v = (Vector *)((uint8_t *)pEntity + offset);
  1737 
  1738 	cell_t *vec;
  1739 	pContext->LocalToPhysAddr(params[4], &vec);
  1740 
  1741 	v->x = sp_ctof(vec[0]);
  1742 	v->y = sp_ctof(vec[1]);
  1743 	v->z = sp_ctof(vec[2]);
  1744 
  1745 	if (params[2] == Prop_Send && (pEdict != NULL))
  1746 	{
  1747 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1748 	}
  1749 
  1750 	return 1;
  1751 }
  1752 
  1753 static cell_t GetEntPropString(IPluginContext *pContext, const cell_t *params)
  1754 {
  1755 	CBaseEntity *pEntity;
  1756 	char *prop;
  1757 	int offset;
  1758 	const char *class_name;
  1759 	edict_t *pEdict;
  1760 	bool bIsStringIndex;
  1761 
  1762 	int element = 0;
  1763 	if (params[0] >= 6)
  1764  	{
  1765 		element = params[6];
  1766  	}
  1767 	
  1768 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1769 	{
  1770 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1771 	}
  1772 
  1773 	if (!pEdict || (class_name = pEdict->GetClassName()) == NULL)
  1774 	{
  1775 		class_name = "";
  1776 	}
  1777 
  1778 	pContext->LocalToString(params[3], &prop);
  1779 
  1780 	bIsStringIndex = false;
  1781 
  1782 	switch (params[2])
  1783 	{
  1784 	case Prop_Data:
  1785 		{
  1786 			typedescription_t *td;
  1787 			
  1788 			FIND_PROP_DATA(td);
  1789 
  1790 			if (td->fieldType != FIELD_CHARACTER
  1791 				&& td->fieldType != FIELD_STRING
  1792 				&& td->fieldType != FIELD_MODELNAME 
  1793 				&& td->fieldType != FIELD_SOUNDNAME)
  1794 			{
  1795 				return pContext->ThrowNativeError("Data field %s is not a string (%d != %d)", 
  1796 					prop,
  1797 					td->fieldType,
  1798 					FIELD_CHARACTER);
  1799 			}
  1800 
  1801 			bIsStringIndex = (td->fieldType != FIELD_CHARACTER);
  1802 
  1803 			if (bIsStringIndex && element >= td->fieldSize)
  1804 			{
  1805 				return pContext->ThrowNativeError("Element %d is out of bounds (Prop %s has %d elements).",
  1806 					element,
  1807 					prop,
  1808 					td->fieldSize);
  1809 			}
  1810 			else if (element > 1)
  1811 			{
  1812 				return pContext->ThrowNativeError("Prop %s is not an array. Element %d is invalid.",
  1813 					prop,
  1814 					element);
  1815 			}
  1816 
  1817 			offset = GetTypeDescOffs(td);
  1818 			if (bIsStringIndex)
  1819 			{
  1820 				offset += (element * (td->fieldSizeInBytes / td->fieldSize));
  1821 			}
  1822 			break;
  1823 		}
  1824 	case Prop_Send:
  1825 		{
  1826 			sm_sendprop_info_t info;
  1827 			
  1828 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1829 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1830 			if (!pNet)
  1831 			{
  1832 				return pContext->ThrowNativeError("Edict %d (%d) is not networkable", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1833 			}
  1834 			if (!g_HL2.FindSendPropInfo(pNet->GetServerClass()->GetName(), prop, &info))
  1835 			{
  1836 				return pContext->ThrowNativeError("Property \"%s\" not found (entity %d/%s)",
  1837 					prop,
  1838 					params[1],
  1839 					class_name);
  1840 			}
  1841 
  1842 			offset = info.actual_offset;
  1843 
  1844 			if (info.prop->GetType() != DPT_String)
  1845 			{
  1846 				return pContext->ThrowNativeError("SendProp %s is not a string (%d != %d)",
  1847 					prop,
  1848 					info.prop->GetType(),
  1849 					DPT_String);
  1850 			}
  1851 			else if (element > 0)
  1852 			{
  1853 				return pContext->ThrowNativeError("SendProp %s is not an array. Element %d is invalid.",
  1854 					prop,
  1855 					element);
  1856  			}
  1857 			
  1858 			break;
  1859 		}
  1860 	default:
  1861 		{
  1862 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1863 		}
  1864 	}
  1865 
  1866 	size_t len;
  1867 	const char *src; 
  1868 	
  1869 	if (!bIsStringIndex)
  1870 	{
  1871 		src = (char *)((uint8_t *)pEntity + offset);
  1872 	}
  1873 	else
  1874 	{
  1875 		string_t idx;
  1876 
  1877 		idx = *(string_t *)((uint8_t *)pEntity + offset);
  1878 		src = (idx == NULL_STRING) ? "" : STRING(idx);
  1879 	}
  1880 
  1881 	pContext->StringToLocalUTF8(params[4], params[5], src, &len);
  1882 
  1883 	return len;
  1884 }
  1885 
  1886 static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
  1887 {
  1888 	CBaseEntity *pEntity;
  1889 	char *prop;
  1890 	int offset;
  1891 	int maxlen;
  1892 	edict_t *pEdict;
  1893 
  1894 	if (!IndexToAThings(params[1], &pEntity, &pEdict))
  1895 	{
  1896 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  1897 	}
  1898 
  1899 	switch (params[2])
  1900 	{
  1901 	case Prop_Data:
  1902 		{
  1903 			datamap_t *pMap;
  1904 			typedescription_t *td;
  1905 			if ((pMap=CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  1906 			{
  1907 				return pContext->ThrowNativeError("Unable to retrieve GetDataDescMap offset");
  1908 			}
  1909 			pContext->LocalToString(params[3], &prop);
  1910 			if ((td=g_HL2.FindInDataMap(pMap, prop)) == NULL)
  1911 			{
  1912 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1913 			}
  1914 			if (td->fieldType != FIELD_CHARACTER)
  1915 			{
  1916 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1917 			}
  1918 			offset = GetTypeDescOffs(td);
  1919 			maxlen = td->fieldSize;
  1920 			break;
  1921 		}
  1922 	case Prop_Send:
  1923 		{
  1924 			char *prop;
  1925 			IServerUnknown *pUnk = (IServerUnknown *)pEntity;
  1926 			IServerNetworkable *pNet = pUnk->GetNetworkable();
  1927 			if (!pNet)
  1928 			{
  1929 				return pContext->ThrowNativeError("The edict is not networkable");
  1930 			}
  1931 			pContext->LocalToString(params[3], &prop);
  1932 			SendProp *pSend = g_HL2.FindInSendTable(pNet->GetServerClass()->GetName(), prop);
  1933 			if (!pSend)
  1934 			{
  1935 				return pContext->ThrowNativeError("Property \"%s\" not found for entity %d", prop, params[1]);
  1936 			}
  1937 			if (pSend->GetType() != DPT_String)
  1938 			{
  1939 				return pContext->ThrowNativeError("Property \"%s\" is not a valid string", prop);
  1940 			}
  1941 			offset = pSend->GetOffset();
  1942 			maxlen = DT_MAX_STRING_BUFFERSIZE;
  1943 			break;
  1944 		}
  1945 	default:
  1946 		{
  1947 			return pContext->ThrowNativeError("Invalid Property type %d", params[2]);
  1948 		}
  1949 	}
  1950 
  1951 	char *src;
  1952 	char *dest = (char *)((uint8_t *)pEntity + offset);
  1953 
  1954 	pContext->LocalToString(params[4], &src);
  1955 	size_t len = strncopy(dest, src, maxlen);
  1956 
  1957 	if (params[2] == Prop_Send && (pEdict != NULL))
  1958 	{
  1959 		g_HL2.SetEdictStateChanged(pEdict, offset);
  1960 	}
  1961 
  1962 	return len;
  1963 }
  1964 
  1965 static int32_t SDKEntFlagToSMEntFlag(int flag)
  1966 {
  1967 	switch (flag)
  1968 	{
  1969 		case FL_ONGROUND:
  1970 			return ENTFLAG_ONGROUND;
  1971 		case FL_DUCKING:
  1972 			return ENTFLAG_DUCKING;
  1973 		case FL_WATERJUMP:
  1974 			return ENTFLAG_WATERJUMP;
  1975 		case FL_ONTRAIN:
  1976 			return ENTFLAG_ONTRAIN;
  1977 		case FL_INRAIN:
  1978 			return ENTFLAG_INRAIN;
  1979 		case FL_FROZEN:
  1980 			return ENTFLAG_FROZEN;
  1981 		case FL_ATCONTROLS:
  1982 			return ENTFLAG_ATCONTROLS;
  1983 		case FL_CLIENT:
  1984 			return ENTFLAG_CLIENT;
  1985 		case FL_FAKECLIENT:
  1986 			return ENTFLAG_FAKECLIENT;
  1987 		case FL_INWATER:
  1988 			return ENTFLAG_INWATER;
  1989 		case FL_FLY:
  1990 			return ENTFLAG_FLY;
  1991 		case FL_SWIM:
  1992 			return ENTFLAG_SWIM;
  1993 		case FL_CONVEYOR:
  1994 			return ENTFLAG_CONVEYOR;
  1995 		case FL_NPC:
  1996 			return ENTFLAG_NPC;
  1997 		case FL_GODMODE:
  1998 			return ENTFLAG_GODMODE;
  1999 		case FL_NOTARGET:
  2000 			return ENTFLAG_NOTARGET;
  2001 		case FL_AIMTARGET:
  2002 			return ENTFLAG_AIMTARGET;
  2003 		case FL_PARTIALGROUND:
  2004 			return ENTFLAG_PARTIALGROUND;
  2005 		case FL_STATICPROP:
  2006 			return ENTFLAG_STATICPROP;
  2007 		case FL_GRAPHED:
  2008 			return ENTFLAG_GRAPHED;
  2009 		case FL_GRENADE:
  2010 			return ENTFLAG_GRENADE;
  2011 		case FL_STEPMOVEMENT:
  2012 			return ENTFLAG_STEPMOVEMENT;
  2013 		case FL_DONTTOUCH:
  2014 			return ENTFLAG_DONTTOUCH;
  2015 		case FL_BASEVELOCITY:
  2016 			return ENTFLAG_BASEVELOCITY;
  2017 		case FL_WORLDBRUSH:
  2018 			return ENTFLAG_WORLDBRUSH;
  2019 		case FL_OBJECT:
  2020 			return ENTFLAG_OBJECT;
  2021 		case FL_KILLME:
  2022 			return ENTFLAG_KILLME;
  2023 		case FL_ONFIRE:
  2024 			return ENTFLAG_ONFIRE;
  2025 		case FL_DISSOLVING:
  2026 			return ENTFLAG_DISSOLVING;
  2027 		case FL_TRANSRAGDOLL:
  2028 			return ENTFLAG_TRANSRAGDOLL;
  2029 		case FL_UNBLOCKABLE_BY_PLAYER:
  2030 			return ENTFLAG_UNBLOCKABLE_BY_PLAYER;
  2031 #if SOURCE_ENGINE == SE_ALIENSWARM
  2032 		case FL_FREEZING:
  2033 			return ENTFLAG_FREEZING;
  2034 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
  2035 		case FL_EP2V_UNKNOWN:
  2036 			return ENTFLAG_EP2V_UNKNOWN1;
  2037 #endif
  2038 		default:
  2039 			return 0;
  2040 	}
  2041 }
  2042 
  2043 static int32_t SMEntFlagToSDKEntFlag(int32_t flag)
  2044 {
  2045 	switch (flag)
  2046 	{
  2047 		case ENTFLAG_ONGROUND:
  2048 			return FL_ONGROUND;
  2049 		case ENTFLAG_DUCKING:
  2050 			return FL_DUCKING;
  2051 		case ENTFLAG_WATERJUMP:
  2052 			return FL_WATERJUMP;
  2053 		case ENTFLAG_ONTRAIN:
  2054 			return FL_ONTRAIN;
  2055 		case ENTFLAG_INRAIN:
  2056 			return FL_INRAIN;
  2057 		case ENTFLAG_FROZEN:
  2058 			return FL_FROZEN;
  2059 		case ENTFLAG_ATCONTROLS:
  2060 			return FL_ATCONTROLS;
  2061 		case ENTFLAG_CLIENT:
  2062 			return FL_CLIENT;
  2063 		case ENTFLAG_FAKECLIENT:
  2064 			return FL_FAKECLIENT;
  2065 		case ENTFLAG_INWATER:
  2066 			return FL_INWATER;
  2067 		case ENTFLAG_FLY:
  2068 			return FL_FLY;
  2069 		case ENTFLAG_SWIM:
  2070 			return FL_SWIM;
  2071 		case ENTFLAG_CONVEYOR:
  2072 			return FL_CONVEYOR;
  2073 		case ENTFLAG_NPC:
  2074 			return FL_NPC;
  2075 		case ENTFLAG_GODMODE:
  2076 			return FL_GODMODE;
  2077 		case ENTFLAG_NOTARGET:
  2078 			return FL_NOTARGET;
  2079 		case ENTFLAG_AIMTARGET:
  2080 			return FL_AIMTARGET;
  2081 		case ENTFLAG_PARTIALGROUND:
  2082 			return FL_PARTIALGROUND;
  2083 		case ENTFLAG_STATICPROP:
  2084 			return FL_STATICPROP;
  2085 		case ENTFLAG_GRAPHED:
  2086 			return FL_GRAPHED;
  2087 		case ENTFLAG_GRENADE:
  2088 			return FL_GRENADE;
  2089 		case ENTFLAG_STEPMOVEMENT:
  2090 			return FL_STEPMOVEMENT;
  2091 		case ENTFLAG_DONTTOUCH:
  2092 			return FL_DONTTOUCH;
  2093 		case ENTFLAG_BASEVELOCITY:
  2094 			return FL_BASEVELOCITY;
  2095 		case ENTFLAG_WORLDBRUSH:
  2096 			return FL_WORLDBRUSH;
  2097 		case ENTFLAG_OBJECT:
  2098 			return FL_OBJECT;
  2099 		case ENTFLAG_KILLME:
  2100 			return FL_KILLME;
  2101 		case ENTFLAG_ONFIRE:
  2102 			return FL_ONFIRE;
  2103 		case ENTFLAG_DISSOLVING:
  2104 			return FL_DISSOLVING;
  2105 		case ENTFLAG_TRANSRAGDOLL:
  2106 			return FL_TRANSRAGDOLL;
  2107 		case ENTFLAG_UNBLOCKABLE_BY_PLAYER:
  2108 			return FL_UNBLOCKABLE_BY_PLAYER;
  2109 #if SOURCE_ENGINE == SE_ALIENSWARM
  2110 		case ENTFLAG_FREEZING:
  2111 			return FL_FREEZING;
  2112 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
  2113 		case ENTFLAG_EP2V_UNKNOWN1:
  2114 			return FL_EP2V_UNKNOWN;
  2115 #endif
  2116 		default:
  2117 			return 0;
  2118 	}
  2119 }
  2120 
  2121 static cell_t GetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2122 {
  2123 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2124 	if (!pEntity)
  2125 	{
  2126 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2127 	}
  2128 
  2129 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2130 	if (!prop)
  2131 	{
  2132 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2133 	}
  2134 
  2135 	typedescription_t *td;
  2136 	datamap_t *pMap;
  2137 
  2138 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2139 	{
  2140 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2141 	}
  2142 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2143 	{
  2144 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2145 			prop,
  2146 			params[1]);
  2147 	}
  2148 
  2149 	int offset = GetTypeDescOffs(td);
  2150 
  2151 	int32_t actual_flags = *(int32_t *)((uint8_t *)pEntity + offset);
  2152 	int32_t sm_flags = 0;
  2153 
  2154 	for (int32_t i = 0; i < 32; i++)
  2155 	{
  2156 		int32_t flag = (1<<i);
  2157 		if ((actual_flags & flag) == flag)
  2158 		{
  2159 			sm_flags |= SDKEntFlagToSMEntFlag(flag);
  2160 		}
  2161 	}
  2162 
  2163 	return sm_flags;
  2164 }
  2165 
  2166 static cell_t SetEntityFlags(IPluginContext *pContext, const cell_t *params)
  2167 {
  2168 	CBaseEntity *pEntity = g_HL2.ReferenceToEntity(params[1]);
  2169 	if (!pEntity)
  2170 	{
  2171 		return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
  2172 	}
  2173 
  2174 	const char *prop = g_pGameConf->GetKeyValue("m_fFlags");
  2175 	if (!prop)
  2176 	{
  2177 		return pContext->ThrowNativeError("Could not find m_fFlags prop in gamedata");
  2178 	}
  2179 
  2180 	typedescription_t *td;
  2181 	datamap_t *pMap;
  2182 
  2183 	if ((pMap = CBaseEntity_GetDataDescMap(pEntity)) == NULL)
  2184 	{
  2185 		return pContext->ThrowNativeError("Could not retrieve datamap");
  2186 	}
  2187 	if ((td = g_HL2.FindInDataMap(pMap, prop)) == NULL)
  2188 	{
  2189 		return pContext->ThrowNativeError("Property \"%s\" not found (entity %d)",
  2190 			prop,
  2191 			params[1]);
  2192 	}
  2193 
  2194 	int offset = GetTypeDescOffs(td);
  2195 
  2196 	int32_t sm_flags = params[2];
  2197 	int32_t actual_flags = 0;
  2198 
  2199 	for (int32_t i = 0; i < 32; i++)
  2200 	{
  2201 		int32_t flag = (1<<i);
  2202 		if ((sm_flags & flag) == flag)
  2203 		{
  2204 			actual_flags |= SMEntFlagToSDKEntFlag(flag);
  2205 		}
  2206 	}
  2207 
  2208 	*(int32_t *)((uint8_t *)pEntity + offset) = actual_flags;
  2209 
  2210 	return 0;
  2211 }
  2212 
  2213 REGISTER_NATIVES(entityNatives)
  2214 {
  2215 	{"ChangeEdictState",		ChangeEdictState},
  2216 	{"CreateEdict",				CreateEdict},
  2217 	{"FindDataMapOffs",			FindDataMapOffs},
  2218 	{"FindSendPropInfo",		FindSendPropInfo},
  2219 	{"FindSendPropOffs",		FindSendPropOffs},
  2220 	{"GetEdictClassname",		GetEdictClassname},
  2221 	{"GetEdictFlags",			GetEdictFlags},
  2222 	{"GetEntData",				GetEntData},
  2223 	{"GetEntDataEnt",			GetEntDataEnt},		/** DEPRECATED */
  2224 	{"GetEntDataEnt2",			GetEntDataEnt2},
  2225 	{"GetEntDataFloat",			GetEntDataFloat},
  2226 	{"GetEntDataString",		GetEntDataString},
  2227 	{"GetEntDataVector",		GetEntDataVector},
  2228 	{"GetEntProp",				GetEntProp},
  2229 	{"GetEntPropArraySize",		GetEntPropArraySize},
  2230 	{"GetEntPropEnt",			GetEntPropEnt},
  2231 	{"GetEntPropFloat",			GetEntPropFloat},
  2232 	{"GetEntPropString",		GetEntPropString},
  2233 	{"GetEntPropVector",		GetEntPropVector},
  2234 	{"GetEntityCount",			GetEntityCount},
  2235 	{"GetEntityFlags",			GetEntityFlags},
  2236 	{"GetEntityNetClass",		GetEntityNetClass},
  2237 	{"GetMaxEntities",			GetMaxEntities},
  2238 	{"IsEntNetworkable",		IsEntNetworkable},
  2239 	{"IsValidEdict",			IsValidEdict},
  2240 	{"IsValidEntity",			IsValidEntity},
  2241 	{"RemoveEdict",				RemoveEdict},
  2242 	{"SetEdictFlags",			SetEdictFlags},
  2243 	{"SetEntData",				SetEntData},
  2244 	{"SetEntDataEnt",			SetEntDataEnt},		/** DEPRECATED */
  2245 	{"SetEntDataEnt2",			SetEntDataEnt2},
  2246 	{"SetEntDataFloat",			SetEntDataFloat},
  2247 	{"SetEntDataVector",		SetEntDataVector},
  2248 	{"SetEntDataString",		SetEntDataString},
  2249 	{"SetEntityFlags",			SetEntityFlags},
  2250 	{"SetEntProp",				SetEntProp},
  2251 	{"SetEntPropEnt",			SetEntPropEnt},
  2252 	{"SetEntPropFloat",			SetEntPropFloat},
  2253 	{"SetEntPropString",		SetEntPropString},
  2254 	{"SetEntPropVector",		SetEntPropVector},
  2255 	{NULL,						NULL}
  2256 };