3 * =============================================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * =============================================================================
7 * This file is part of the SourceMod/SourcePawn SDK.
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, version 3.0, as published by the
11 * Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
21 * As a special exception, AlliedModders LLC gives you permission to link the
22 * code of this program (as well as its derivative works) to "Half-Life 2," the
23 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
24 * by the Valve Corporation. You must obey the GNU General Public License in
25 * all respects for all other code used. Additionally, AlliedModders LLC grants
26 * this exception to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
28 * or <http://www.sourcemod.net/license.php>.
33 #if defined _entity_included
36 #define _entity_included
39 * Property types for entities.
43 Prop_Send = 0, /**< This property is networked. */
44 Prop_Data = 1, /**< This property is for save game data fields. */
48 * @section For more information on these, see the HL2SDK (public/edict.h)
50 #define FL_EDICT_CHANGED (1<<0) /**< Game DLL sets this when the entity state changes
51 Mutually exclusive with FL_EDICT_PARTIAL_CHANGE. */
52 #define FL_EDICT_FREE (1<<1) /**< this edict if free for reuse */
53 #define FL_EDICT_FULL (1<<2) /**< this is a full server entity */
54 #define FL_EDICT_FULLCHECK (0<<0) /**< call ShouldTransmit() each time, this is a fake flag */
55 #define FL_EDICT_ALWAYS (1<<3) /**< always transmit this entity */
56 #define FL_EDICT_DONTSEND (1<<4) /**< don't transmit this entity */
57 #define FL_EDICT_PVSCHECK (1<<5) /**< always transmit entity, but cull against PVS */
58 #define FL_EDICT_PENDING_DORMANT_CHECK (1<<6)
59 #define FL_EDICT_DIRTY_PVS_INFORMATION (1<<7)
60 #define FL_FULL_EDICT_CHANGED (1<<8)
64 PropField_Unsupported, /**< The type is unsupported. */
65 PropField_Integer, /**< Valid for SendProp and Data fields */
66 PropField_Float, /**< Valid for SendProp and Data fields */
67 PropField_Entity, /**< Valid for Data fields only (SendProp shows as int) */
68 PropField_Vector, /**< Valid for SendProp and Data fields */
69 PropField_String, /**< Valid for SendProp and Data fields */
77 * Returns the maximum number of entities.
79 * @return Maximum number of entities.
81 native GetMaxEntities();
84 * Returns the number of entities in the server.
86 * @return Number of entities in the server.
88 native GetEntityCount();
91 * Returns whether or not an entity is valid. Returns false
92 * if there is no matching CBaseEntity for this edict index.
94 * @param edict Index of the entity/edict.
95 * @return True if valid, false otherwise.
97 native bool:IsValidEntity(edict);
100 * Returns whether or not an edict index is valid.
102 * @param edict Index of the edict.
103 * @return True if valid, false otherwise.
105 native bool:IsValidEdict(edict);
108 * Returns whether or not an entity is a valid networkable edict.
110 * @param edict Index of the edict.
111 * @return True if networkable, false if invalid or not networkable.
113 native bool:IsEntNetworkable(edict);
116 * Creates a new edict (the basis of a networkable entity)
118 * @return Index of the edict, 0 on failure.
120 native CreateEdict();
123 * Removes an edict from the world.
125 * @param edict Index of the edict.
127 * @error Invalid edict index.
129 native RemoveEdict(edict);
132 * Returns the flags on an edict. These are not the same as entity flags.
134 * @param edict Index of the entity.
135 * @return Edict flags.
136 * @error Invalid edict index.
138 native GetEdictFlags(edict);
141 * Sets the flags on an edict. These are not the same as entity flags.
143 * @param edict Index of the entity.
144 * @param flags Flags to set.
146 * @error Invalid edict index.
148 native SetEdictFlags(edict, flags);
151 * Retrieves an edict classname.
153 * @param edict Index of the entity.
154 * @param clsname Buffer to store the classname.
155 * @param maxlength Maximum length of the buffer.
156 * @return True on success, false if there is no classname set.
158 native bool:GetEdictClassname(edict, String:clsname[], maxlength);
161 * Retrieves an entity's networkable serverclass name.
162 * This is not the same as the classname and is used for networkable state changes.
164 * @param edict Index of the entity.
165 * @param clsname Buffer to store the serverclass name.
166 * @param maxlength Maximum lnegth of the buffer.
167 * @return True on success, false if the edict is not networkable.
168 * @error Invalid edict index.
170 native bool:GetEntityNetClass(edict, String:clsname[], maxlength);
173 * @section Entity offset functions
175 * Offsets should be specified in byte distance from the CBaseEntity
176 * structure, not short (double byte) or integer (four byte) multiples.
177 * It is somewhat common practice to use offsets aligned to their final
178 * type, and thus make sure you are not falling to this error in SourceMod.
179 * For example, if your "integer-aligned" offset was 119, your byte-aligned
180 * offset is 119*4, or 476.
182 * Specifying incorrect offsets or the incorrect data type for an offset
183 * can have fatal consequences. If you are hardcoding offsets, and the
184 * layout of CBaseEntity does not match, you can easily crash the server.
186 * The reasonable bounds for offsets is greater than or equal to 0 and
187 * below 32768. Offsets out of these bounds will throw an error. However,
188 * this does not represent any real range, it is simply a sanity check for
189 * illegal values. Any range outside of the CBaseEntity structure's private
190 * size will cause undefined behaviour or even crash.
194 * Marks an entity as state changed. This can be useful if you set an offset
195 * and wish for it to be immediately changed over the network. By default this
196 * is not done for offset setting functions.
198 * @param edict Index to the edict.
199 * @param offset Offset to mark as changed. If 0,
200 * the entire edict is marked as changed.
202 * @error Invalid entity or offset out of bounds.
204 native ChangeEdictState(edict, offset = 0);
207 * Peeks into an entity's object data and retrieves the integer value at
210 * @param entity Edict index.
211 * @param offset Offset to use.
212 * @param size Number of bytes to read (valid values are 1, 2, or 4).
213 * @return Value at the given memory location.
214 * @error Invalid entity or offset out of reasonable bounds.
216 native GetEntData(entity, offset, size=4);
219 * Peeks into an entity's object data and sets the integer value at
222 * @param entity Edict index.
223 * @param offset Offset to use.
224 * @param size Number of bytes to write (valid values are 1, 2, or 4).
225 * @param changeState If true, change will be sent over the network.
226 * @return Value at the given memory location.
227 * @error Invalid entity or offset out of reasonable bounds.
230 native SetEntData(entity, offset, any:value, size=4, bool:changeState=false);
233 * Peeks into an entity's object data and retrieves the float value at
236 * @param entity Edict index.
237 * @param offset Offset to use.
238 * @return Value at the given memory location.
239 * @error Invalid entity or offset out of reasonable bounds.
241 native Float:GetEntDataFloat(entity, offset);
244 * Peeks into an entity's object data and sets the integer value at
247 * @param entity Edict index.
248 * @param offset Offset to use.
249 * @param changeState If true, change will be sent over the network.
250 * @return Value at the given memory location.
251 * @error Invalid entity or offset out of reasonable bounds.
254 native SetEntDataFloat(entity, offset, Float:value, bool:changeState=false);
257 * This function is deprecated. Use GetEntDataEnt2 instead, for
258 * reasons explained in the notes.
260 * Note: This function returns 0 on failure, which may be misleading,
261 * as the number 0 is also used for the world entity index.
263 * Note: This function makes no attempt to validate the returned
264 * entity, and in fact, it could be garbage or completely unexpected.
266 * @param entity Edict index.
267 * @param offset Offset to use.
268 * @return Entity index at the given location, or 0 if none.
269 * @error Invalid entity or offset out of reasonable bounds.
271 native GetEntDataEnt(entity, offset);
274 * This function is deprecated. Use GetEntDataEnt2 instead, for
275 * reasons explained in the notes.
277 * Note: This function uses 0 as an indicator to unset data, but
278 * 0 is also the world entity index. Thus, the a property cannot
279 * be set to the world entity using this native.
281 * @param entity Edict index.
282 * @param offset Offset to use.
283 * @param other Entity index to set, or 0 to clear.
284 * @param changeState If true, change will be sent over the network.
286 * @error Invalid entity or offset out of reasonable bounds.
288 native SetEntDataEnt(entity, offset, other, bool:changeState=false);
291 * Peeks into an entity's object data and retrieves the entity index
292 * at the given offset.
294 * Note: This will only work on offsets that are stored as "entity
295 * handles" (which usually looks like m_h* in properties). These
296 * are not SourceMod Handles, but internal Source structures.
298 * @param entity Edict index.
299 * @param offset Offset to use.
300 * @return Entity index at the given location. If there is no entity,
301 * or the stored entity is invalid, then -1 is returned.
302 * @error Invalid input entity, or offset out of reasonable bounds.
304 native GetEntDataEnt2(entity, offset);
307 * Peeks into an entity's object data and sets the entity index at the
310 * Note: This will only work on offsets that are stored as "entity
311 * handles" (which usually looks like m_h* in properties). These
312 * are not SourceMod Handles, but internal Source structures.
314 * @param entity Edict index.
315 * @param offset Offset to use.
316 * @param other Entity index to set, or -1 to clear.
317 * @param changeState If true, change will be sent over the network.
319 * @error Invalid input entity, or offset out of reasonable bounds.
321 native SetEntDataEnt2(entity, offset, other, bool:changeState=false);
324 * Peeks into an entity's object data and retrieves the vector at the
326 * @note Both a Vector and a QAngle are three floats. This is a
327 * convenience function and will work with both types.
329 * @param entity Edict index.
330 * @param offset Offset to use.
331 * @param vec Vector buffer to store data in.
333 * @error Invalid entity or offset out of reasonable bounds.
335 native GetEntDataVector(entity, offset, Float:vec[3]);
338 * Peeks into an entity's object data and sets the vector at the given
340 * @note Both a Vector and a QAngle are three floats. This is a
341 * convenience function and will work with both types.
343 * @param entity Edict index.
344 * @param offset Offset to use.
345 * @param vec Vector to set.
346 * @param changeState If true, change will be sent over the network.
348 * @error Invalid entity or offset out of reasonable bounds.
350 native SetEntDataVector(entity, offset, const Float:vec[3], bool:changeState=false);
353 * Peeks into an entity's object data and retrieves the string at
356 * @param entity Edict index.
357 * @param offset Offset to use.
358 * @param buffer Destination string buffer.
359 * @param maxlen Maximum length of output string buffer.
360 * @return Number of non-null bytes written.
361 * @error Invalid entity or offset out of reasonable bounds.
363 native GetEntDataString(entity, offset, String:buffer[], maxlen);
366 * Peeks into an entity's object data and sets the string at
369 * @param entity Edict index.
370 * @param offset Offset to use.
371 * @param buffer String to set.
372 * @param maxlen Maximum length of bytes to write.
373 * @param changeState If true, change will be sent over the network.
374 * @return Number of non-null bytes written.
375 * @error Invalid entity or offset out of reasonable bounds.
377 native SetEntDataString(entity, offset, const String:buffer[], maxlen, bool:changeState=false);
384 * Given a ServerClass name, finds a networkable send property offset.
385 * This information is cached for future calls.
387 * Note, this function may return offsets that do not work!
388 * If a property is nested beneath a parent object, the resulting offset
389 * will be invalid for direct use with data functions. Therefore, you
390 * should use FindSendPropInfo() instead. An example of such a property is
391 * CTFPlayer::DT_LocalPlayer.m_nDisguiseClass on Team Fortress.
393 * @param cls Classname.
394 * @param prop Property name.
395 * @return An offset, or -1 on failure.
397 native FindSendPropOffs(const String:cls[], const String:prop[]);
400 * Given a ServerClass name, finds a networkable send property offset.
401 * This information is cached for future calls.
403 * Note: This function will correctly compute nested offsets, unlike
404 * FindSendPropOffs(). YOU SHOULD NOT use this function to self-compute
405 * nested offsets. For example, it is okay to add indexes for arrays,
406 * but not to add DT_LocalPlayer to m_nDisguiseClass.
408 * @param cls Classname.
409 * @param prop Property name.
410 * @param type Optional parameter to store the type.
411 * @param num_bits Optional parameter to store the number of bits the field
412 * uses, if applicable (otherwise 0 is stored). The number
413 * of bits varies for integers and floats, and is always 0
415 * @param local_offset Optional parameter to store the local offset, as
416 * FindSendPropOffs() would return.
417 * @return On success, returns an absolutely computed offset.
418 * If no offset is available, 0 is returned.
419 * If the property is not found, -1 is returned.
421 native FindSendPropInfo(const String:cls[],
423 &PropFieldType:type=PropFieldType:0,
428 * Given an entity, finds a datamap property offset.
429 * This information is cached for future calls.
431 * @param entity Entity index.
432 * @param prop Property name.
433 * @param type Optional parameter to store the type.
434 * @param num_bits Optional parameter to store the number of bits the field
435 * uses. The bit count will either be 1 (for boolean) or
436 * divisible by 8 (including 0 if unknown).
437 * @return An offset, or -1 on failure.
439 native FindDataMapOffs(entity,
441 &PropFieldType:type=PropFieldType:0,
445 * Wrapper function for finding a send property for a particular entity.
447 * @param ent Entity index.
448 * @param prop Property name.
449 * @param actual Defaults to false for backwards compatibility.
450 * If true, the newer FindSendPropInfo() function
452 * @return An offset, or -1 on failure.
454 stock GetEntSendPropOffs(ent, const String:prop[], bool:actual=false)
458 if (!GetEntityNetClass(ent, cls, sizeof(cls)))
465 return FindSendPropInfo(cls, prop);
469 return FindSendPropOffs(cls, prop);
474 * Retrieves an integer value from an entity's property.
476 * This function is considered safer and more robust over GetEntData,
477 * because it performs strict offset checking and typing rules. There is a
478 * very minor performance hit from this.
480 * @param entity Entity/edict index.
481 * @param type Property type.
482 * @param prop Property name.
483 * @param size Number of bytes to write (valid values are 1, 2, or 4).
484 * This value is auto-detected, and the size parameter is
485 * only used as a fallback in case detection fails.
486 * @return Value at the given property offset.
487 * @error Invalid entity or property not found.
489 native GetEntProp(entity, PropType:type, const String:prop[], size=4);
492 * Sets an integer value in an entity's property.
494 * This function is considered safer and more robust over SetEntData,
495 * because it performs strict offset checking and typing rules. There is a
496 * very minor performance hit from this.
498 * @param entity Entity/edict index.
499 * @param type Property type.
500 * @param prop Property name.
501 * @param size Number of bytes to write (valid values are 1, 2, or 4).
502 * This value is auto-detected, and the size parameter is
503 * only used as a fallback in case detection fails.
504 * @error Invalid entity or offset out of reasonable bounds.
507 native SetEntProp(entity, PropType:type, const String:prop[], any:value, size=4);
510 * Retrieves a float value from an entity's property.
512 * This function is considered safer and more robust over GetEntDataFloat,
513 * because it performs strict offset checking and typing rules. There is a
514 * very minor performance hit from this.
516 * @param entity Entity/edict index.
517 * @param type Property type.
518 * @param prop Property name.
519 * @return Value at the given property offset.
520 * @error Invalid entity or offset out of reasonable bounds.
522 native Float:GetEntPropFloat(entity, PropType:type, const String:prop[]);
525 * Sets a float value in an entity's property.
527 * This function is considered safer and more robust over SetEntDataFloat,
528 * because it performs strict offset checking and typing rules. There is a
529 * very minor performance hit from this.
531 * @param entity Entity/edict index.
532 * @param type Property type.
533 * @param prop Property name.
534 * @param value Value to set.
536 * @error Invalid entity or offset out of reasonable bounds.
538 native SetEntPropFloat(entity, PropType:type, const String:prop[], Float:value);
541 * Retrieves an entity index from an entity's property.
543 * This function is considered safer and more robust over GetEntDataEnt*,
544 * because it performs strict offset checking and typing rules. There is a
545 * very minor performance hit from this.
547 * @param entity Entity/edict index.
548 * @param type Property type.
549 * @param prop Property name.
550 * @return Entity index at the given property.
551 * If there is no entity, or the entity is not valid,
552 * then -1 is returned.
553 * @error Invalid entity or offset out of reasonable bounds.
555 native GetEntPropEnt(entity, PropType:type, const String:prop[]);
558 * Sets an entity index in an entity's property.
560 * This function is considered safer and more robust over SetEntDataEnt*,
561 * because it performs strict offset checking and typing rules. There is a
562 * very minor performance hit from this.
564 * @param entity Entity/edict index.
565 * @param type Property type.
566 * @param prop Property name.
567 * @param other Entity index to set, or -1 to unset.
569 * @error Invalid entity or offset out of reasonable bounds.
571 native SetEntPropEnt(entity, PropType:type, const String:prop[], other);
574 * Retrieves a vector of floats from an entity, given a named network property.
576 * This function is considered safer and more robust over GetEntDataVector,
577 * because it performs strict offset checking and typing rules. There is a
578 * very minor performance hit from this.
580 * @param entity Entity/edict index.
581 * @param type Property type.
582 * @param prop Property name.
583 * @param vec Vector buffer to store data in.
585 * @error Invalid entity, property not found, or property not
586 * actually a vector data type.
588 native GetEntPropVector(entity, PropType:type, const String:prop[], Float:vec[3]);
591 * Sets a vector of floats in an entity, given a named network property.
593 * This function is considered safer and more robust over SetEntDataVector,
594 * because it performs strict offset checking and typing rules. There is a
595 * very minor performance hit from this.
597 * @param entity Entity/edict index.
598 * @param type Property type.
599 * @param prop Property name.
600 * @param vec Vector to set.
602 * @error Invalid entity, property not found, or property not
603 * actually a vector data type.
605 native SetEntPropVector(entity, PropType:type, const String:prop[], const Float:vec[3]);
608 * Gets a network property as a string.
610 * @param entity Edict index.
611 * @param type Property type.
612 * @param prop Property to use.
613 * @param buffer Destination string buffer.
614 * @param maxlen Maximum length of output string buffer.
615 * @return Number of non-null bytes written.
616 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
618 native GetEntPropString(entity, PropType:type, const String:prop[], String:buffer[], maxlen);
621 * Sets a network property as a string.
623 * @param entity Edict index.
624 * @param type Property type.
625 * @param prop Property to use.
626 * @param buffer String to set.
627 * @return Number of non-null bytes written.
628 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
630 native SetEntPropString(entity, PropType:type, const String:prop[], const String:buffer[]);
633 * Copies an array of cells from an entity at a given offset.
635 * @param entity Entity index.
636 * @param offset Offset to use.
637 * @param array Array to read into.
638 * @param arraySize Number of values to read.
639 * @param dataSize Size of each value in bytes (1, 2, or 4).
641 * @error Invalid entity or offset out of reasonable bounds.
643 stock GetEntDataArray(entity, offset, array[], arraySize, dataSize=4)
645 for (new i=0; i<arraySize; i++)
647 array[i] = GetEntData(entity, offset + i*dataSize, dataSize)
652 * Copies an array of cells to an entity at a given offset.
654 * @param entity Entity index.
655 * @param offset Offset to use.
656 * @param array Array of values to copy.
657 * @param arraySize Number of values to copy.
658 * @param dataSize Size of each value in bytes (1, 2, or 4).
659 * @param changeState True to set the network state as changed; false otherwise.
661 * @error Invalid entity or offset out of reasonable bounds.
663 stock SetEntDataArray(entity, offset, const array[], arraySize, dataSize=4, bool:changeState=false)
665 for (new i=0; i<arraySize; i++)
667 SetEntData(entity, offset + i*dataSize, array[i], dataSize, changeState);