plugins/include/entity.inc
author David Anderson <dvander@alliedmods.net>
Fri Mar 02 19:11:04 2007 +0000 (2007-03-02)
changeset 561 49f639ad7d5e
parent 558 fc0eec032efb
child 610 2fe5613a35e3
permissions -rw-r--r--
added entity functions and stocks for send props
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _entity_included
    17  #endinput
    18 #endif
    19 #define _entity_included
    20 
    21 
    22 /**
    23  * For more information on these, see the HL2SDK (public/edict.h)
    24  */
    25 #define FL_EDICT_CHANGED	(1<<0)	/**< Game DLL sets this when the entity state changes
    26 										 Mutually exclusive with FL_EDICT_PARTIAL_CHANGE. */
    27 #define FL_EDICT_FREE		(1<<1)	/**< this edict if free for reuse */
    28 #define FL_EDICT_FULL		(1<<2)	/**< this is a full server entity */
    29 #define FL_EDICT_FULLCHECK	(0<<0)  /**< call ShouldTransmit() each time, this is a fake flag */
    30 #define FL_EDICT_ALWAYS		(1<<3)	/**< always transmit this entity */
    31 #define FL_EDICT_DONTSEND	(1<<4)	/**< don't transmit this entity */
    32 #define FL_EDICT_PVSCHECK	(1<<5)	/**< always transmit entity, but cull against PVS */
    33 #define FL_EDICT_PENDING_DORMANT_CHECK	(1<<6)
    34 #define FL_EDICT_DIRTY_PVS_INFORMATION	(1<<7)
    35 #define FL_FULL_EDICT_CHANGED			(1<<8)
    36 
    37 /**
    38  * Returns the maximum number of entities.
    39  *
    40  * @return				Maximum number of entities.
    41  */
    42 native GetMaxEntities();
    43 
    44 /**
    45  * Returns the number of entities in the server.
    46  *
    47  * @return				Number of entities in the server.
    48  */
    49 native GetEntityCount();
    50 
    51 /**
    52  * Returns whether or not an entity is valid.  Returns false
    53  * if there is no matching CBaseEntity for this edict index.
    54  *
    55  * @param edict			Index of the entity/edict.
    56  * @return				True if valid, false otherwise.
    57  */
    58 native bool:IsValidEntity(edict);
    59 
    60 /**
    61  * Returns whether or not an edict index is valid.
    62  *
    63  * @param edict			Index of the edict.
    64  * @return				True if valid, false otherwise.
    65  */
    66 native bool:IsValidEdict(edict);
    67 
    68 /**
    69  * Returns whether or not an entity is a valid networkable edict.
    70  *
    71  * @param edict			Index of the edict.
    72  * @return				True if networkable, false if invalid or not networkable.
    73  */
    74 native bool:IsEntNetworkable(edict);
    75 
    76 /**
    77  * Creates a new edict (the basis of a networkable entity)
    78  *
    79  * @return				Index of the edict, 0 on failure.
    80  */
    81 native CreateEdict();
    82 
    83 /** 
    84  * Removes an edict from the world.
    85  *
    86  * @param edict			Index of the edict.
    87  * @noreturn
    88  * @error				Invalid edict index.
    89  */
    90 native RemoveEdict(edict);
    91 
    92 /**
    93  * Returns the flags on an edict.  These are not the same as entity flags.
    94  *
    95  * @param edict			Index of the entity.
    96  * @return				Edict flags.
    97  * @error				Invalid edict index.
    98  */
    99 native GetEdictFlags(edict);
   100 
   101 /**
   102  * Sets the flags on an edict.  These are not the same as entity flags.
   103  *
   104  * @param edict			Index of the entity.
   105  * @param flags			Flags to set.
   106  * @noreturn
   107  * @error				Invalid edict index.
   108  */
   109 native SetEdictFlags(edict, flags);
   110 
   111 /**
   112  * Retrieves an edict classname.
   113  *
   114  * @param edict			Index of the entity.
   115  * @param clsname		Buffer to store the classname.
   116  * @param maxlength		Maximum length of the buffer.
   117  * @return				True on success, false if there is no classname set.
   118  */
   119 native bool:GetEdictClassname(edict, String:clsname[], maxlength);
   120 
   121 /**
   122  * Retrieves an entity's networkable serverclass name.
   123  * This is not the same as the classname and is used for networkable state changes.
   124  *
   125  * @param edict			Index of the entity.
   126  * @param clsname		Buffer to store the serverclass name.
   127  * @param maxlength		Maximum lnegth of the buffer.
   128  * @return				True on success, false if the edict is not networkable.
   129  * @error				Invalid edict index.
   130  */
   131 native bool:GetEntityNetClass(edict, String:clsname[], maxlength);
   132 
   133 /**
   134  * @section Entity offset functions
   135  *
   136  * Offsets should be specified in byte distance from the CBaseEntity 
   137  * structure, not short (double byte) or integer (four byte) multiples.  
   138  * It is somewhat common practice to use offsets aligned to their final 
   139  * type, and thus make sure you are not falling to this error in SourceMod.
   140  * For example, if your "integer-aligned" offset was 119, your byte-aligned
   141  * offset is 119*4, or 476.
   142  
   143  * Specifying incorrect offsets or the incorrect data type for an offset
   144  * can have fatal consequences.  If you are hardcoding offsets, and the 
   145  * layout of CBaseEntity does not match, you can easily crash the server.
   146  *
   147  * The reasonable bounds for offsets is greater than or equal to 0 and 
   148  * below 32768.  Offsets out of these bounds will throw an error.  However,
   149  * this does not represent any real range, it is simply a sanity check for
   150  * illegal values.  Any range outside of the CBaseEntity structure's private
   151  * size will cause undefined behaviour or even crash.
   152  */
   153  
   154 /**
   155  * Marks an entity as state changed.  This can be useful if you set an offset 
   156  * and wish for it to be immediately changed over the network.  By default this 
   157  * is not done for offset setting functions.  
   158  *
   159  * @param edict			Index to the edict.
   160  * @param offset		Offset to mark as changed.  If 0, 
   161  *						the entire edict is marked as changed.
   162  * @noreturn
   163  * @error				Invalid entity or offset out of bounds.
   164  */
   165 native ChangeEdictState(edict, offset = 0);
   166 
   167 /**
   168  * Peeks into an entity's object data and retrieves the integer value at 
   169  * the given offset.
   170  * 
   171  * @param entity		Edict index.
   172  * @param offset		Offset to use.
   173  * @param size			Number of bytes to read (valid values are 1, 2, or 4).
   174  * @return				Value at the given memory location.
   175  * @error				Invalid entity or offset out of reasonable bounds.
   176  */
   177 native GetEntData(entity, offset, size=4);
   178 
   179 /**
   180  * Peeks into an entity's object data and sets the integer value at 
   181  * the given offset.
   182  *
   183  * @param entity		Edict index.
   184  * @param offset		Offset to use.
   185  * @param size			Number of bytes to write (valid values are 1, 2, or 4).
   186  * @param changeState	If true, change will be sent over the network.
   187  * @return				Value at the given memory location.
   188  * @error				Invalid entity or offset out of reasonable bounds.
   189  * @noreturn
   190  */
   191 native SetEntData(entity, offset, value, size=4, bool:changeState=false);
   192 
   193 /**
   194  * Peeks into an entity's object data and retrieves the float value at 
   195  * the given offset.
   196  * 
   197  * @param entity		Edict index.
   198  * @param offset		Offset to use.
   199  * @return				Value at the given memory location.
   200  * @error				Invalid entity or offset out of reasonable bounds.
   201  */
   202 native Float:GetEntDataFloat(entity, offset);
   203 
   204 /**
   205  * Peeks into an entity's object data and sets the integer value at 
   206  * the given offset.
   207  *
   208  * @param entity		Edict index.
   209  * @param offset		Offset to use.
   210  * @param changeState	If true, change will be sent over the network.
   211  * @return				Value at the given memory location.
   212  * @error				Invalid entity or offset out of reasonable bounds.
   213  * @noreturn
   214  */
   215 native SetEntDataFloat(entity, offset, Float:value, bool:changeState=false);
   216 
   217 /**
   218  * Peeks into an entity's object data and retrieves the entity handle
   219  * info at the given offset.
   220  * 
   221  * @param entity		Edict index.
   222  * @param offset		Offset to use.
   223  * @return				Entity index at the given location, or 0 if none.
   224  * @error				Invalid entity or offset out of reasonable bounds.
   225  */
   226 native GetEntDataEnt(entity, offset);
   227 
   228 /**
   229  * Peeks into an entity's object data and sest the entity handle info
   230  * at the given offset.
   231  * 
   232  * @param entity		Edict index.
   233  * @param offset		Offset to use.
   234  * @param other			Entity index to set, or 0 to clear.
   235  * @param changeState	If true, change will be sent over the network.
   236  * @noreturn
   237  * @error				Invalid entity or offset out of reasonable bounds.
   238  */
   239 native SetEntDataEnt(entity, offset, other, bool:changeState=false);
   240 
   241 /**
   242  * Peeks into an entity's object data and retrieves the vector at the 
   243  * given offset.
   244  * @note Both a Vector and a QAngle are three floats.  This is a 
   245  * convenience function and will work with both types.
   246  * 
   247  * @param entity		Edict index.
   248  * @param offset		Offset to use.
   249  * @param vec			Vector buffer to store data in.
   250  * @noreturn
   251  * @error				Invalid entity or offset out of reasonable bounds.
   252  */
   253 native GetEntDataVector(entity, offset, Float:vec[3]);
   254 
   255 /**
   256  * Peeks into an entity's object data and sets the vector at the given 
   257  * offset.
   258  * @note Both a Vector and a QAngle are three floats.  This is a 
   259  * convenience function and will work with both types.
   260  * 
   261  * @param entity		Edict index.
   262  * @param offset		Offset to use.
   263  * @param vec			Vector to set.
   264  * @param changeState	If true, change will be sent over the network.
   265  * @noreturn
   266  * @error				Invalid entity or offset out of reasonable bounds.
   267  */
   268 native SetEntDataVector(entity, offset, const Float:vec[3], bool:changeState=false);
   269 
   270 /**
   271  * Given a ServerClass name, finds a networkable send property offset.
   272  * This information is cached for future calls.
   273  *
   274  * @param cls			Classname.
   275  * @param prop			Property name.
   276  * @return				An offset, or -1 on failure.
   277  */
   278 native FindSendPropOffs(const String:cls[], const String:prop[]);
   279 
   280 /**
   281  * Wrapper function for finding a send property for a particular entity.
   282  *
   283  * @param ent			Entity index.
   284  * @param prop			Property name.
   285  * @return				An offset, or -1 on failure.
   286  */
   287 stock GetEntSendPropOffs(ent, const String:prop[])
   288 {
   289 	decl String:cls[64];
   290 	
   291 	if (!GetEntityNetClass(ent, cls, sizeof(cls)))
   292 	{
   293 		return -1;
   294 	}
   295 	
   296 	return FindSendPropOffs(cls, prop);
   297 }
   298 
   299 /**
   300  * Gets a network property as an integer; wrapper around GetEntData().
   301  * 
   302  * @param entity		Edict index.
   303  * @param prop			Property to use.
   304  * @param size			Number of bytes to read (valid values are 1, 2, or 4).
   305  * @return				Value at the given property offset.
   306  * @error				Invalid entity or property not found.
   307  */
   308 stock GetEntProp(entity, const String:prop[], size=4)
   309 {
   310 	new offs = GetEntSendPropOffs(entity, prop);
   311 	if (offs == -1)
   312 	{
   313 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   314 	}
   315 	return GetEntData(entity, offs, size);
   316 }
   317 
   318 /**
   319  * Sets a network property as an integer; wrapper around GetEntData().
   320  *
   321  * @param entity		Edict index.
   322  * @param prop			Property to use.
   323  * @param size			Number of bytes to write (valid values are 1, 2, or 4).
   324  * @error				Invalid entity or offset out of reasonable bounds.
   325  * @noreturn
   326  */
   327 stock SetEntProp(entity, const String:prop[], value, size=4)
   328 {
   329 	new offs = GetEntSendPropOffs(entity, prop);
   330 	if (offs == -1)
   331 	{
   332 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   333 	}
   334 	return SetEntData(entity, offs, value, size, true);
   335 }
   336 
   337 /**
   338  * Gets a network property as a float; wrapper around GetEntDataFloat().
   339  * 
   340  * @param entity		Edict index.
   341  * @param prop			Property to use.
   342  * @return				Value at the given property offset..
   343  * @error				Invalid entity or offset out of reasonable bounds.
   344  */
   345 stock Float:GetEntPropFloat(entity, const String:prop[])
   346 {
   347 	new offs = GetEntSendPropOffs(entity, prop);
   348 	if (offs == -1)
   349 	{
   350 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   351 	}
   352 	return GetEntDataFloat(entity, offs);
   353 }
   354 
   355 /**
   356  * Sets a network property as a float; wrapper around SetEntDataFloat().
   357  * 
   358  * @param entity		Edict index.
   359  * @param prop			Property to use.
   360  * @param value			Value to set.
   361  * @noreturn
   362  * @error				Invalid entity or offset out of reasonable bounds.
   363  */
   364 stock SetEntPropFloat(entity, const String:prop[], Float:value)
   365 {
   366 	new offs = GetEntSendPropOffs(entity, prop);
   367 	if (offs == -1)
   368 	{
   369 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   370 	}
   371 	return SetEntDataFloat(entity, offs, value, true);
   372 }
   373 
   374 /**
   375  * Gets a network property as a handle entity; wrapper around GetEntDataEnt().
   376  * 
   377  * @param entity		Edict index.
   378  * @param prop			Property to use.
   379  * @return				Entity index at the given property, or 0 if none.
   380  * @error				Invalid entity or offset out of reasonable bounds.
   381  */
   382 stock GetEntPropEnt(entity, const String:prop[])
   383 {
   384 	new offs = GetEntSendPropOffs(entity, prop);
   385 	if (offs == -1)
   386 	{
   387 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   388 	}
   389 	return GetEntDataEnt(entity, offs);
   390 }
   391 
   392 /**
   393  * Sets a network property as a handle entity; wrapper around SetEntDataEnt().
   394  * 
   395  * @param entity		Edict index.
   396  * @param prop			Property to use.
   397  * @param other			Entity index to set, or 0 to unset.
   398  * @noreturn
   399  * @error				Invalid entity or offset out of reasonable bounds.
   400  */
   401 stock SetEntPropEnt(entity, const String:prop[], other)
   402 {
   403 	new offs = GetEntSendPropOffs(entity, prop);
   404 	if (offs == -1)
   405 	{
   406 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   407 	}
   408 	return SetEntDataEnt(entity, offs, other, true);
   409 }
   410 
   411 /**
   412  * Gets a network property as a vector; wrapper around GetEntDataVector().
   413  * @note Both a Vector and a QAngle are three floats.  This is a 
   414  * convenience function and will work with both types.
   415  * 
   416  * @param entity		Edict index.
   417  * @param prop			Property to use.
   418  * @param vec			Vector buffer to store data in.
   419  * @noreturn
   420  * @error				Invalid entity or offset out of reasonable bounds.
   421  */
   422 stock GetEntPropVector(entity, const String:prop[], Float:vec[3])
   423 {
   424 	new offs = GetEntSendPropOffs(entity, prop);
   425 	if (offs == -1)
   426 	{
   427 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   428 	}
   429 	return GetEntDataVector(entity, offs, vec);
   430 }
   431 
   432 /**
   433  * Sets a network property as a vector; wrapper around SetEntDataVector().
   434  * @note Both a Vector and a QAngle are three floats.  This is a 
   435  * convenience function and will work with both types.
   436  * 
   437  * @param entity		Edict index.
   438  * @param prop			Property to use.
   439  * @param vec			Vector to set.
   440  * @noreturn
   441  * @error				Invalid entity or offset out of reasonable bounds.
   442  */
   443 stock SetEntPropVector(entity, const String:prop[], const Float:vec[3])
   444 {
   445 	new offs = GetEntSendPropOffs(entity, prop);
   446 	if (offs == -1)
   447 	{
   448 		ThrowError("Property \"%s\" not found for entity %d", prop, entity);
   449 	}
   450 	return SetEntDataVector(entity, offs, vec, true);
   451 }