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 saved game files. */
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)
67 * Returns the maximum number of entities.
69 * @return Maximum number of entities.
71 native GetMaxEntities();
74 * Returns the number of entities in the server.
76 * @return Number of entities in the server.
78 native GetEntityCount();
81 * Returns whether or not an entity is valid. Returns false
82 * if there is no matching CBaseEntity for this edict index.
84 * @param edict Index of the entity/edict.
85 * @return True if valid, false otherwise.
87 native bool:IsValidEntity(edict);
90 * Returns whether or not an edict index is valid.
92 * @param edict Index of the edict.
93 * @return True if valid, false otherwise.
95 native bool:IsValidEdict(edict);
98 * Returns whether or not an entity is a valid networkable edict.
100 * @param edict Index of the edict.
101 * @return True if networkable, false if invalid or not networkable.
103 native bool:IsEntNetworkable(edict);
106 * Creates a new edict (the basis of a networkable entity)
108 * @return Index of the edict, 0 on failure.
110 native CreateEdict();
113 * Removes an edict from the world.
115 * @param edict Index of the edict.
117 * @error Invalid edict index.
119 native RemoveEdict(edict);
122 * Returns the flags on an edict. These are not the same as entity flags.
124 * @param edict Index of the entity.
125 * @return Edict flags.
126 * @error Invalid edict index.
128 native GetEdictFlags(edict);
131 * Sets the flags on an edict. These are not the same as entity flags.
133 * @param edict Index of the entity.
134 * @param flags Flags to set.
136 * @error Invalid edict index.
138 native SetEdictFlags(edict, flags);
141 * Retrieves an edict classname.
143 * @param edict Index of the entity.
144 * @param clsname Buffer to store the classname.
145 * @param maxlength Maximum length of the buffer.
146 * @return True on success, false if there is no classname set.
148 native bool:GetEdictClassname(edict, String:clsname[], maxlength);
151 * Retrieves an entity's networkable serverclass name.
152 * This is not the same as the classname and is used for networkable state changes.
154 * @param edict Index of the entity.
155 * @param clsname Buffer to store the serverclass name.
156 * @param maxlength Maximum lnegth of the buffer.
157 * @return True on success, false if the edict is not networkable.
158 * @error Invalid edict index.
160 native bool:GetEntityNetClass(edict, String:clsname[], maxlength);
163 * @section Entity offset functions
165 * Offsets should be specified in byte distance from the CBaseEntity
166 * structure, not short (double byte) or integer (four byte) multiples.
167 * It is somewhat common practice to use offsets aligned to their final
168 * type, and thus make sure you are not falling to this error in SourceMod.
169 * For example, if your "integer-aligned" offset was 119, your byte-aligned
170 * offset is 119*4, or 476.
172 * Specifying incorrect offsets or the incorrect data type for an offset
173 * can have fatal consequences. If you are hardcoding offsets, and the
174 * layout of CBaseEntity does not match, you can easily crash the server.
176 * The reasonable bounds for offsets is greater than or equal to 0 and
177 * below 32768. Offsets out of these bounds will throw an error. However,
178 * this does not represent any real range, it is simply a sanity check for
179 * illegal values. Any range outside of the CBaseEntity structure's private
180 * size will cause undefined behaviour or even crash.
184 * Marks an entity as state changed. This can be useful if you set an offset
185 * and wish for it to be immediately changed over the network. By default this
186 * is not done for offset setting functions.
188 * @param edict Index to the edict.
189 * @param offset Offset to mark as changed. If 0,
190 * the entire edict is marked as changed.
192 * @error Invalid entity or offset out of bounds.
194 native ChangeEdictState(edict, offset = 0);
197 * Peeks into an entity's object data and retrieves the integer value at
200 * @param entity Edict index.
201 * @param offset Offset to use.
202 * @param size Number of bytes to read (valid values are 1, 2, or 4).
203 * @return Value at the given memory location.
204 * @error Invalid entity or offset out of reasonable bounds.
206 native GetEntData(entity, offset, size=4);
209 * Peeks into an entity's object data and sets the integer value at
212 * @param entity Edict index.
213 * @param offset Offset to use.
214 * @param size Number of bytes to write (valid values are 1, 2, or 4).
215 * @param changeState If true, change will be sent over the network.
216 * @return Value at the given memory location.
217 * @error Invalid entity or offset out of reasonable bounds.
220 native SetEntData(entity, offset, value, size=4, bool:changeState=false);
223 * Peeks into an entity's object data and retrieves the float value at
226 * @param entity Edict index.
227 * @param offset Offset to use.
228 * @return Value at the given memory location.
229 * @error Invalid entity or offset out of reasonable bounds.
231 native Float:GetEntDataFloat(entity, offset);
234 * Peeks into an entity's object data and sets the integer value at
237 * @param entity Edict index.
238 * @param offset Offset to use.
239 * @param changeState If true, change will be sent over the network.
240 * @return Value at the given memory location.
241 * @error Invalid entity or offset out of reasonable bounds.
244 native SetEntDataFloat(entity, offset, Float:value, bool:changeState=false);
247 * Peeks into an entity's object data and retrieves the entity handle
248 * info at the given offset.
250 * @param entity Edict index.
251 * @param offset Offset to use.
252 * @return Entity index at the given location, or 0 if none.
253 * @error Invalid entity or offset out of reasonable bounds.
255 native GetEntDataEnt(entity, offset);
258 * Peeks into an entity's object data and sets the entity handle info
259 * at the given offset.
261 * @param entity Edict index.
262 * @param offset Offset to use.
263 * @param other Entity index to set, or 0 to clear.
264 * @param changeState If true, change will be sent over the network.
266 * @error Invalid entity or offset out of reasonable bounds.
268 native SetEntDataEnt(entity, offset, other, bool:changeState=false);
271 * Peeks into an entity's object data and retrieves the vector at the
273 * @note Both a Vector and a QAngle are three floats. This is a
274 * convenience function and will work with both types.
276 * @param entity Edict index.
277 * @param offset Offset to use.
278 * @param vec Vector buffer to store data in.
280 * @error Invalid entity or offset out of reasonable bounds.
282 native GetEntDataVector(entity, offset, Float:vec[3]);
285 * Peeks into an entity's object data and sets the vector at the given
287 * @note Both a Vector and a QAngle are three floats. This is a
288 * convenience function and will work with both types.
290 * @param entity Edict index.
291 * @param offset Offset to use.
292 * @param vec Vector to set.
293 * @param changeState If true, change will be sent over the network.
295 * @error Invalid entity or offset out of reasonable bounds.
297 native SetEntDataVector(entity, offset, const Float:vec[3], bool:changeState=false);
300 * Peeks into an entity's object data and retrieves the string at
303 * @param entity Edict index.
304 * @param offset Offset to use.
305 * @param buffer Destination string buffer.
306 * @param maxlen Maximum length of output string buffer.
307 * @return Number of non-null bytes written.
308 * @error Invalid entity or offset out of reasonable bounds.
310 native GetEntDataString(entity, offset, String:buffer[], maxlen);
313 * Peeks into an entity's object data and sets the string at
316 * @param entity Edict index.
317 * @param offset Offset to use.
318 * @param buffer String to set.
319 * @param maxlen Maximum length of bytes to write.
320 * @param changeState If true, change will be sent over the network.
321 * @return Number of non-null bytes written.
322 * @error Invalid entity or offset out of reasonable bounds.
324 native SetEntDataString(entity, offset, const String:buffer[], maxlen, bool:changeState=false);
331 * Given a ServerClass name, finds a networkable send property offset.
332 * This information is cached for future calls.
334 * @param cls Classname.
335 * @param prop Property name.
336 * @return An offset, or -1 on failure.
338 native FindSendPropOffs(const String:cls[], const String:prop[]);
341 * Given an entity, finds a datamap property offset.
342 * This information is cached for future calls.
344 * @param entity Entity index.
345 * @param prop Property name.
346 * @return An offset, or -1 on failure.
348 native FindDataMapOffs(entity, const String:prop[]);
351 * Wrapper function for finding a send property for a particular entity.
353 * @param ent Entity index.
354 * @param prop Property name.
355 * @return An offset, or -1 on failure.
357 stock GetEntSendPropOffs(ent, const String:prop[])
361 if (!GetEntityNetClass(ent, cls, sizeof(cls)))
366 return FindSendPropOffs(cls, prop);
370 * Gets a network property as an integer; wrapper around GetEntData().
372 * @param entity Edict index.
373 * @param type Property type.
374 * @param prop Property to use.
375 * @param size Number of bytes to read (valid values are 1, 2, or 4).
376 * @return Value at the given property offset.
377 * @error Invalid entity or property not found.
379 stock GetEntProp(entity, PropType:type, const String:prop[], size=4)
387 offs = GetEntSendPropOffs(entity, prop);
391 offs = FindDataMapOffs(entity, prop);
395 ThrowError("Invalid Property type %d", type);
401 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
404 return GetEntData(entity, offs, size);
408 * Sets a network property as an integer; wrapper around GetEntData().
410 * @param entity Edict index.
411 * @param type Property type.
412 * @param prop Property to use.
413 * @param size Number of bytes to write (valid values are 1, 2, or 4).
414 * @error Invalid entity or offset out of reasonable bounds.
417 stock SetEntProp(entity, PropType:type, const String:prop[], value, size=4)
425 offs = GetEntSendPropOffs(entity, prop);
429 offs = FindDataMapOffs(entity, prop);
433 ThrowError("Invalid Property type %d", type);
439 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
442 return SetEntData(entity, offs, value, size, true);
446 * Gets a network property as a float; wrapper around GetEntDataFloat().
448 * @param entity Edict index.
449 * @param type Property type.
450 * @param prop Property to use.
451 * @return Value at the given property offset.
452 * @error Invalid entity or offset out of reasonable bounds.
454 stock Float:GetEntPropFloat(entity, PropType:type, const String:prop[])
462 offs = GetEntSendPropOffs(entity, prop);
466 offs = FindDataMapOffs(entity, prop);
470 ThrowError("Invalid Property type %d", type);
476 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
479 return GetEntDataFloat(entity, offs);
483 * Sets a network property as a float; wrapper around SetEntDataFloat().
485 * @param entity Edict index.
486 * @param type Property type.
487 * @param prop Property to use.
488 * @param value Value to set.
490 * @error Invalid entity or offset out of reasonable bounds.
492 stock SetEntPropFloat(entity, PropType:type, const String:prop[], Float:value)
500 offs = GetEntSendPropOffs(entity, prop);
504 offs = FindDataMapOffs(entity, prop);
508 ThrowError("Invalid Property type %d", type);
514 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
517 return SetEntDataFloat(entity, offs, value, true);
521 * Gets a network property as a handle entity; wrapper around GetEntDataEnt().
523 * @param entity Edict index.
524 * @param type Property type.
525 * @param prop Property to use.
526 * @return Entity index at the given property, or 0 if none.
527 * @error Invalid entity or offset out of reasonable bounds.
529 stock GetEntPropEnt(entity, PropType:type, const String:prop[])
537 offs = GetEntSendPropOffs(entity, prop);
541 offs = FindDataMapOffs(entity, prop);
545 ThrowError("Invalid Property type %d", type);
551 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
554 return GetEntDataEnt(entity, offs);
558 * Sets a network property as a handle entity; wrapper around SetEntDataEnt().
560 * @param entity Edict index.
561 * @param type Property type.
562 * @param prop Property to use.
563 * @param other Entity index to set, or 0 to unset.
565 * @error Invalid entity or offset out of reasonable bounds.
567 stock SetEntPropEnt(entity, PropType:type, const String:prop[], other)
575 offs = GetEntSendPropOffs(entity, prop);
579 offs = FindDataMapOffs(entity, prop);
583 ThrowError("Invalid Property type %d", type);
589 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
592 return SetEntDataEnt(entity, offs, other, true);
596 * Gets a network property as a vector; wrapper around GetEntDataVector().
597 * @note Both a Vector and a QAngle are three floats. This is a
598 * convenience function and will work with both types.
600 * @param entity Edict index.
601 * @param type Property type.
602 * @param prop Property to use.
603 * @param vec Vector buffer to store data in.
605 * @error Invalid entity or offset out of reasonable bounds.
607 stock GetEntPropVector(entity, PropType:type, const String:prop[], Float:vec[3])
615 offs = GetEntSendPropOffs(entity, prop);
619 offs = FindDataMapOffs(entity, prop);
623 ThrowError("Invalid Property type %d", type);
629 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
632 return GetEntDataVector(entity, offs, vec);
636 * Sets a network property as a vector; wrapper around SetEntDataVector().
637 * @note Both a Vector and a QAngle are three floats. This is a
638 * convenience function and will work with both types.
640 * @param entity Edict index.
641 * @param type Property type.
642 * @param prop Property to use.
643 * @param vec Vector to set.
645 * @error Invalid entity or offset out of reasonable bounds.
647 stock SetEntPropVector(entity, PropType:type, const String:prop[], const Float:vec[3])
655 offs = GetEntSendPropOffs(entity, prop);
659 offs = FindDataMapOffs(entity, prop);
663 ThrowError("Invalid Property type %d", type);
669 ThrowError("Property \"%s\" not found for entity %d", prop, entity);
672 return SetEntDataVector(entity, offs, vec, true);
676 * Gets a network property as a string.
678 * @param entity Edict index.
679 * @param type Property type.
680 * @param prop Property to use.
681 * @param buffer Destination string buffer.
682 * @param maxlen Maximum length of output string buffer.
683 * @return Number of non-null bytes written.
684 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
686 native GetEntPropString(entity, PropType:type, const String:prop[], String:buffer[], maxlen);
689 * Sets a network property as a string.
691 * @param entity Edict index.
692 * @param type Property type.
693 * @param prop Property to use.
694 * @param buffer String to set.
695 * @return Number of non-null bytes written.
696 * @error Invalid entity, offset out of reasonable bounds, or property is not a valid string.
698 native SetEntPropString(entity, PropType:type, const String:prop[], const String:buffer[]);
701 * Copies an array of cells from an entity at a given offset.
703 * @param entity Entity index.
704 * @param offset Offset to use.
705 * @param array Array to read into.
706 * @param arraySize Number of values to read.
707 * @param dataSize Size of each value in bytes (1, 2, or 4).
709 * @error Invalid entity or offset out of reasonable bounds.
711 stock GetEntDataArray(entity, offset, array[], arraySize, dataSize=4)
713 for (new i=0; i<arraySize; i++)
715 array[i] = GetEntData(entity, offset + i*dataSize, dataSize)
721 * Copies an array of cells to an entity at a given offset.
723 * @param entity Entity index.
724 * @param offset Offset to use.
725 * @param array Array of values to copy.
726 * @param arraySize Number of values to copy.
727 * @param dataSize Size of each value in bytes (1, 2, or 4).
728 * @param changeState True to set the network state as changed; false otherwise.
730 * @error Invalid entity or offset out of reasonable bounds.
732 stock SetEntDataArray(entity, offset, const array[], arraySize, dataSize=4, bool:changeState=false)
734 for (new i=0; i<arraySize; i++)
736 SetEntData(entity, offset + i*dataSize, array[i], dataSize, changeState);