3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
16 #if defined _entity_included
19 #define _entity_included
22 * Property types for entities.
26 Prop_Send = 0, /**< This property is networked. */
27 Prop_Data = 1, /**< This property is for saved game files. */
31 * @section For more information on these, see the HL2SDK (public/edict.h)
33 #define FL_EDICT_CHANGED (1<<0) /**< Game DLL sets this when the entity state changes
34 Mutually exclusive with FL_EDICT_PARTIAL_CHANGE. */
35 #define FL_EDICT_FREE (1<<1) /**< this edict if free for reuse */
36 #define FL_EDICT_FULL (1<<2) /**< this is a full server entity */
37 #define FL_EDICT_FULLCHECK (0<<0) /**< call ShouldTransmit() each time, this is a fake flag */
38 #define FL_EDICT_ALWAYS (1<<3) /**< always transmit this entity */
39 #define FL_EDICT_DONTSEND (1<<4) /**< don't transmit this entity */
40 #define FL_EDICT_PVSCHECK (1<<5) /**< always transmit entity, but cull against PVS */
41 #define FL_EDICT_PENDING_DORMANT_CHECK (1<<6)
42 #define FL_EDICT_DIRTY_PVS_INFORMATION (1<<7)
43 #define FL_FULL_EDICT_CHANGED (1<<8)
50 * Returns the maximum number of entities.
52 * @return Maximum number of entities.
54 native GetMaxEntities();
57 * Returns the number of entities in the server.
59 * @return Number of entities in the server.
61 native GetEntityCount();
64 * Returns whether or not an entity is valid. Returns false
65 * if there is no matching CBaseEntity for this edict index.
67 * @param edict Index of the entity/edict.
68 * @return True if valid, false otherwise.
70 native bool:IsValidEntity(edict);
73 * Returns whether or not an edict index is valid.
75 * @param edict Index of the edict.
76 * @return True if valid, false otherwise.
78 native bool:IsValidEdict(edict);
81 * Returns whether or not an entity is a valid networkable edict.
83 * @param edict Index of the edict.
84 * @return True if networkable, false if invalid or not networkable.
86 native bool:IsEntNetworkable(edict);
89 * Creates a new edict (the basis of a networkable entity)
91 * @return Index of the edict, 0 on failure.
96 * Removes an edict from the world.
98 * @param edict Index of the edict.
100 * @error Invalid edict index.
102 native RemoveEdict(edict);
105 * Returns the flags on an edict. These are not the same as entity flags.
107 * @param edict Index of the entity.
108 * @return Edict flags.
109 * @error Invalid edict index.
111 native GetEdictFlags(edict);
114 * Sets the flags on an edict. These are not the same as entity flags.
116 * @param edict Index of the entity.
117 * @param flags Flags to set.
119 * @error Invalid edict index.
121 native SetEdictFlags(edict, flags);
124 * Retrieves an edict classname.
126 * @param edict Index of the entity.
127 * @param clsname Buffer to store the classname.
128 * @param maxlength Maximum length of the buffer.
129 * @return True on success, false if there is no classname set.
131 native bool:GetEdictClassname(edict, String:clsname[], maxlength);
134 * Retrieves an entity's networkable serverclass name.
135 * This is not the same as the classname and is used for networkable state changes.
137 * @param edict Index of the entity.
138 * @param clsname Buffer to store the serverclass name.
139 * @param maxlength Maximum lnegth of the buffer.
140 * @return True on success, false if the edict is not networkable.
141 * @error Invalid edict index.
143 native bool:GetEntityNetClass(edict, String:clsname[], maxlength);
146 * @section Entity offset functions
148 * Offsets should be specified in byte distance from the CBaseEntity
149 * structure, not short (double byte) or integer (four byte) multiples.
150 * It is somewhat common practice to use offsets aligned to their final
151 * type, and thus make sure you are not falling to this error in SourceMod.
152 * For example, if your "integer-aligned" offset was 119, your byte-aligned
153 * offset is 119*4, or 476.
155 * Specifying incorrect offsets or the incorrect data type for an offset
156 * can have fatal consequences. If you are hardcoding offsets, and the
157 * layout of CBaseEntity does not match, you can easily crash the server.
159 * The reasonable bounds for offsets is greater than or equal to 0 and
160 * below 32768. Offsets out of these bounds will throw an error. However,
161 * this does not represent any real range, it is simply a sanity check for
162 * illegal values. Any range outside of the CBaseEntity structure's private
163 * size will cause undefined behaviour or even crash.
167 * Marks an entity as state changed. This can be useful if you set an offset
168 * and wish for it to be immediately changed over the network. By default this
169 * is not done for offset setting functions.
171 * @param edict Index to the edict.
172 * @param offset Offset to mark as changed. If 0,
173 * the entire edict is marked as changed.
175 * @error Invalid entity or offset out of bounds.
177 native ChangeEdictState(edict, offset = 0);
180 * Peeks into an entity's object data and retrieves the integer value at
183 * @param entity Edict index.
184 * @param offset Offset to use.
185 * @param size Number of bytes to read (valid values are 1, 2, or 4).
186 * @return Value at the given memory location.
187 * @error Invalid entity or offset out of reasonable bounds.
189 native GetEntData(entity, offset, size=4);
192 * Peeks into an entity's object data and sets the integer value at
195 * @param entity Edict index.
196 * @param offset Offset to use.
197 * @param size Number of bytes to write (valid values are 1, 2, or 4).
198 * @param changeState If true, change will be sent over the network.
199 * @return Value at the given memory location.
200 * @error Invalid entity or offset out of reasonable bounds.
203 native SetEntData(entity, offset, value, size=4, bool:changeState=false);
206 * Peeks into an entity's object data and retrieves the float value at
209 * @param entity Edict index.
210 * @param offset Offset to use.
211 * @return Value at the given memory location.
212 * @error Invalid entity or offset out of reasonable bounds.
214 native Float:GetEntDataFloat(entity, offset);
217 * Peeks into an entity's object data and sets the integer value at
220 * @param entity Edict index.
221 * @param offset Offset to use.
222 * @param changeState If true, change will be sent over the network.
223 * @return Value at the given memory location.
224 * @error Invalid entity or offset out of reasonable bounds.
227 native SetEntDataFloat(entity, offset, Float:value, bool:changeState=false);
230 * Peeks into an entity's object data and retrieves the entity handle
231 * info at the given offset.
233 * @param entity Edict index.
234 * @param offset Offset to use.
235 * @return Entity index at the given location, or 0 if none.
236 * @error Invalid entity or offset out of reasonable bounds.
238 native GetEntDataEnt(entity, offset);
241 * Peeks into an entity's object data and sets the entity handle info
242 * at the given offset.
244 * @param entity Edict index.
245 * @param offset Offset to use.
246 * @param other Entity index to set, or 0 to clear.
247 * @param changeState If true, change will be sent over the network.
249 * @error Invalid entity or offset out of reasonable bounds.
251 native SetEntDataEnt(entity, offset, other, bool:changeState=false);
254 * Peeks into an entity's object data and retrieves the vector at the
256 * @note Both a Vector and a QAngle are three floats. This is a
257 * convenience function and will work with both types.
259 * @param entity Edict index.
260 * @param offset Offset to use.
261 * @param vec Vector buffer to store data in.
263 * @error Invalid entity or offset out of reasonable bounds.
265 native GetEntDataVector(entity, offset, Float:vec[3]);
268 * Peeks into an entity's object data and sets the vector at the given
270 * @note Both a Vector and a QAngle are three floats. This is a
271 * convenience function and will work with both types.
273 * @param entity Edict index.
274 * @param offset Offset to use.
275 * @param vec Vector to set.
276 * @param changeState If true, change will be sent over the network.
278 * @error Invalid entity or offset out of reasonable bounds.
280 native SetEntDataVector(entity, offset, const Float:vec[3], bool:changeState=false);
283 * Peeks into an entity's object data and retrieves the string at
286 * @param entity Edict index.
287 * @param offset Offset to use.
288 * @param buffer Destination string buffer.
289 * @param maxlen Maximum length of output string buffer.
290 * @return Number of non-null bytes written.
291 * @error Invalid entity or offset out of reasonable bounds.
293 native GetEntDataString(entity, offset, String:buffer[], maxlen);
296 * Peeks into an entity's object data and sets the string at
299 * @param entity Edict index.
300 * @param offset Offset to use.
301 * @param buffer String to set.
302 * @param maxlen Maximum length of bytes to write.
303 * @param changeState If true, change will be sent over the network.
304 * @return Number of non-null bytes written.
305 * @error Invalid entity or offset out of reasonable bounds.
307 native SetEntDataString(entity, offset, const String:buffer[], maxlen, bool:changeState=false);
314 * Given a ServerClass name, finds a networkable send property offset.
315 * This information is cached for future calls.
317 * @param cls Classname.
318 * @param prop Property name.
319 * @return An offset, or -1 on failure.
321 native FindSendPropOffs(const String:cls[], const String:prop[]);
324 * Given an entity, finds a datamap property offset.
325 * This information is cached for future calls.
327 * @param entity Entity index.
328 * @param prop Property name.
329 * @return An offset, or -1 on failure.
331 native FindDataMapOffs(entity, const String:prop[]);
334 * Wrapper function for finding a send property for a particular entity.
336 * @param ent Entity index.
337 * @param prop Property name.
338 * @return An offset, or -1 on failure.
340 stock GetEntSendPropOffs(ent, const String:prop[])
344 if (!GetEntityNetClass(ent, cls, sizeof(cls)))
349 return FindSendPropOffs(cls, prop);
353 * Gets a network property as an integer; wrapper around GetEntData().
355 * @param entity Edict index.
356 * @param type Property type.
357 * @param prop Property to use.
358 * @param size Number of bytes to read (valid values are 1, 2, or 4).
359 * @return Value at the given property offset.
360 * @error Invalid entity or property not found.
362 stock GetEntProp(entity, PropType:type, const String:prop[], size=4)
370 offs = GetEntSendPropOffs(entity, prop);
374 offs = FindDataMapOffs(entity, prop);
378 ThrowError("Invalid Property type %d", type);
384 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
387 return GetEntData(entity, offs, size);
391 * Sets a network property as an integer; wrapper around GetEntData().
393 * @param entity Edict index.
394 * @param type Property type.
395 * @param prop Property to use.
396 * @param size Number of bytes to write (valid values are 1, 2, or 4).
397 * @error Invalid entity or offset out of reasonable bounds.
400 stock SetEntProp(entity, PropType:type, const String:prop[], value, size=4)
408 offs = GetEntSendPropOffs(entity, prop);
412 offs = FindDataMapOffs(entity, prop);
416 ThrowError("Invalid Property type %d", type);
422 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
425 return SetEntData(entity, offs, value, size, true);
429 * Gets a network property as a float; wrapper around GetEntDataFloat().
431 * @param entity Edict index.
432 * @param type Property type.
433 * @param prop Property to use.
434 * @return Value at the given property offset.
435 * @error Invalid entity or offset out of reasonable bounds.
437 stock Float:GetEntPropFloat(entity, PropType:type, const String:prop[])
445 offs = GetEntSendPropOffs(entity, prop);
449 offs = FindDataMapOffs(entity, prop);
453 ThrowError("Invalid Property type %d", type);
459 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
462 return GetEntDataFloat(entity, offs);
466 * Sets a network property as a float; wrapper around SetEntDataFloat().
468 * @param entity Edict index.
469 * @param type Property type.
470 * @param prop Property to use.
471 * @param value Value to set.
473 * @error Invalid entity or offset out of reasonable bounds.
475 stock SetEntPropFloat(entity, PropType:type, const String:prop[], Float:value)
483 offs = GetEntSendPropOffs(entity, prop);
487 offs = FindDataMapOffs(entity, prop);
491 ThrowError("Invalid Property type %d", type);
497 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
500 return SetEntDataFloat(entity, offs, value, true);
504 * Gets a network property as a handle entity; wrapper around GetEntDataEnt().
506 * @param entity Edict index.
507 * @param type Property type.
508 * @param prop Property to use.
509 * @return Entity index at the given property, or 0 if none.
510 * @error Invalid entity or offset out of reasonable bounds.
512 stock GetEntPropEnt(entity, PropType:type, const String:prop[])
520 offs = GetEntSendPropOffs(entity, prop);
524 offs = FindDataMapOffs(entity, prop);
528 ThrowError("Invalid Property type %d", type);
534 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
537 return GetEntDataEnt(entity, offs);
541 * Sets a network property as a handle entity; wrapper around SetEntDataEnt().
543 * @param entity Edict index.
544 * @param type Property type.
545 * @param prop Property to use.
546 * @param other Entity index to set, or 0 to unset.
548 * @error Invalid entity or offset out of reasonable bounds.
550 stock SetEntPropEnt(entity, PropType:type, const String:prop[], other)
558 offs = GetEntSendPropOffs(entity, prop);
562 offs = FindDataMapOffs(entity, prop);
566 ThrowError("Invalid Property type %d", type);
572 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
575 return SetEntDataEnt(entity, offs, other, true);
579 * Gets a network property as a vector; wrapper around GetEntDataVector().
580 * @note Both a Vector and a QAngle are three floats. This is a
581 * convenience function and will work with both types.
583 * @param entity Edict index.
584 * @param type Property type.
585 * @param prop Property to use.
586 * @param vec Vector buffer to store data in.
588 * @error Invalid entity or offset out of reasonable bounds.
590 stock GetEntPropVector(entity, PropType:type, const String:prop[], Float:vec[3])
598 offs = GetEntSendPropOffs(entity, prop);
602 offs = FindDataMapOffs(entity, prop);
606 ThrowError("Invalid Property type %d", type);
612 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
615 return GetEntDataVector(entity, offs, vec);
619 * Sets a network property as a vector; wrapper around SetEntDataVector().
620 * @note Both a Vector and a QAngle are three floats. This is a
621 * convenience function and will work with both types.
623 * @param entity Edict index.
624 * @param type Property type.
625 * @param prop Property to use.
626 * @param vec Vector to set.
628 * @error Invalid entity or offset out of reasonable bounds.
630 stock SetEntPropVector(entity, PropType:type, const String:prop[], const Float:vec[3])
638 offs = GetEntSendPropOffs(entity, prop);
642 offs = FindDataMapOffs(entity, prop);
646 ThrowError("Invalid Property type %d", type);
652 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
655 return SetEntDataVector(entity, offs, vec, true);
659 * Gets a network property as a string.
661 * @param entity Edict index.
662 * @param type Property type.
663 * @param prop Property to use.
664 * @param buffer Destination string buffer.
665 * @param maxlen Maximum length of output string buffer.
666 * @return Number of non-null bytes written.
667 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
669 native GetEntPropString(entity, PropType:type, const String:prop[], String:buffer[], maxlen);
672 * Sets a network property as a string.
674 * @param entity Edict index.
675 * @param type Property type.
676 * @param prop Property to use.
677 * @param buffer String to set.
678 * @return Number of non-null bytes written.
679 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
681 native SetEntPropString(entity, PropType:type, const String:prop[], const String:buffer[]);
684 * Copies an array of cells from an entity at a given offset.
686 * @param entity Entity index.
687 * @param offset Offset to use.
688 * @param array Array to read into.
689 * @param arraySize Number of values to read.
690 * @param dataSize Size of each value in bytes (1, 2, or 4).
692 * @error Invalid entity or offset out of reasonable bounds.
694 stock GetEntDataArray(entity, offset, array[], arraySize, dataSize=4)
696 for (new i=0; i<arraySize; i++)
698 array[i] = GetEntData(entity, offset + i*dataSize, dataSize)
704 * Copies an array of cells to an entity at a given offset.
706 * @param entity Entity index.
707 * @param offset Offset to use.
708 * @param array Array of values to copy.
709 * @param arraySize Number of values to copy.
710 * @param dataSize Size of each value in bytes (1, 2, or 4).
711 * @param changeState True to set the network state as changed; false otherwise.
713 * @error Invalid entity or offset out of reasonable bounds.
715 stock SetEntDataArray(entity, offset, const array[], arraySize, dataSize=4, bool:changeState=false)
717 for (new i=0; i<arraySize; i++)
719 SetEntData(entity, offset + i*dataSize, array[i], dataSize, changeState);