summaryrefslogtreecommitdiff
path: root/sourcemod-1.5-dev/scripting/include/smlib/edicts.inc
blob: 7b719aa0b72950ff30df855b221a3d0884100f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#if defined _smlib_edicts_included
	#endinput
#endif
#define _smlib_edicts_included

#include <sourcemod>
#include <smlib/entities>

/*
 * Finds an edict by it's name
 * It only finds the first occurence.
 *
 * @param name			Name of the entity you want so search.
 * @return				Edict Index or INVALID_ENT_REFERENCE if no entity was found.
 */
stock Edict_FindByName(const String:name[])
{
	new maxEntities = GetMaxEntities();
	for (new edict=0; edict < maxEntities; edict++) {
		
		if (!IsValidEdict(edict)) {
			continue;
		}

		if (Entity_NameMatches(edict, name)) {
			return edict;
		}
	}

	return INVALID_ENT_REFERENCE;
}

/*
 * Finds an edict by its HammerID.
 * The newer version of Valve's Hammer editor
 * sets a unique ID for each entity in a map.
 * It only finds the first occurence.
 *
 * @param hammerId		Hammer editor ID
 * @return				Edict Index or INVALID_ENT_REFERENCE if no entity was found.
 */
stock Edict_FindByHammerId(hammerId)
{
	new maxEntities = GetMaxEntities();
	for (new edict=0; edict < maxEntities; edict++) {
		
		if (!IsValidEdict(edict)) {
			continue;
		}
		
		if (Entity_GetHammerId(edict) == hammerId) {
			return edict;
		}
	}

	return INVALID_ENT_REFERENCE;
}

/**
 * Searches for the closest edict in relation to the given origin
 *
 * @param vecOrigin_center	3 dimensional origin array
 * @param clientsOnly		True if you only want to search for clients
 * @param ignoreEntity		Ignore this entity
 * @return					Edict Index or INVALID_ENT_REFERENCE if no entity was found.
 */
stock Edict_GetClosest(Float:vecOrigin_center[3], bool:clientsOnly=false, ignoreEntity=-1)
{	
	decl Float:vecOrigin_edict[3];
	new Float:smallestDistance = 0.0;
	new closestEdict = INVALID_ENT_REFERENCE;
	
	new maxEntities;
	
	if (clientsOnly) {
		maxEntities = MaxClients;
	}
	else {
		maxEntities = GetMaxEntities();
	}

	for (new edict=1; edict <= maxEntities; edict++) {
		
		if (!IsValidEdict(edict)) {
			continue;
		}
		
		if (ignoreEntity >= 0 && edict == ignoreEntity) {
			continue;
		}
			
		if (GetEntSendPropOffs(edict, "m_vecOrigin") == -1) {
			continue;
		}

		Entity_GetAbsOrigin(edict, vecOrigin_edict);
		
		new Float:edict_distance = GetVectorDistance(vecOrigin_center, vecOrigin_edict, true);
		
		if (edict_distance < smallestDistance || smallestDistance == 0.0) {
			smallestDistance = edict_distance;
			closestEdict = edict;
		}
	}
	
	return closestEdict;
}

/**
 * Searches for the closest edict in relation to the given edict.
 *
 * @param edict				Edict index
 * @param clientsOnly		True if you only want to search for clients
 * @return					The closest edict or INVALID_ENT_REFERENCE
 */
stock Edict_GetClosestToEdict(edict, bool:clientsOnly=false)
{	
	decl Float:vecOrigin[3];
	
	if (GetEntSendPropOffs(edict, "m_vecOrigin") == -1) {
		return INVALID_ENT_REFERENCE;
	}
	
	Entity_GetAbsOrigin(edict, vecOrigin);

	return Edict_GetClosest(vecOrigin, clientsOnly, edict);
}