summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/include/smlib/weapons.inc
blob: 417053eafce9ee3be6d5824015c6ca88afd05602 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#if defined _smlib_weapons_included
	#endinput
#endif
#define _smlib_weapons_included

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

#define MAX_WEAPON_OFFSET		64
#define MAX_WEAPON_SLOTS		6	// hud item selection slots
#define MAX_WEAPON_POSITIONS	20	// max number of items within a slot
#define MAX_WEAPONS				48	// Max number of weapons availabl
#define WEAPON_NOCLIP			-1	// clip sizes set to this tell the weapon it doesn't use a clip
#define	MAX_AMMO_TYPES			32
#define MAX_AMMO_SLOTS 			32	// not really slots

#define MAX_WEAPON_STRING		80
#define MAX_WEAPON_PREFIX		16
#define MAX_WEAPON_AMMO_NAME	32

/*
 * Gets the owner (usually a client) of the weapon
 *
 * @param weapon		Weapon Entity.
 * @return				Owner of the weapon or INVALID_ENT_REFERENCE if the weapon has no owner.
 */
stock int Weapon_GetOwner(int weapon)
{
	return GetEntPropEnt(weapon, Prop_Data, "m_hOwner");
}

/*
 * Sets the owner (usually a client) of the weapon
 *
 * @param weapon		Weapon Entity.
 * @param entity		Entity Index.
 * @noreturn
 */
stock void Weapon_SetOwner(int weapon, int entity)
{
	SetEntPropEnt(weapon, Prop_Data, "m_hOwner", entity);
}

/*
 * Checks whether the entity is a valid weapon or not.
 *
 * @param weapon		Weapon Entity.
 * @return				True if the entity is a valid weapon, false otherwise.
 */
stock bool Weapon_IsValid(int weapon)
{
	if (!IsValidEdict(weapon)) {
		return false;
	}

	return Entity_ClassNameMatches(weapon, "weapon_", true);
}

/*
 * Create's a weapon and spawns it in the world at the specified location.
 *
 * @param className		Classname String of the weapon to spawn
 * @param absOrigin		Absolute Origin Vector where to spawn the weapon.
 * @param absAngles		Absolute Angles Vector.
 * @return				Weapon Index of the created weapon or INVALID_ENT_REFERENCE on error.
 */
stock int Weapon_Create(const char[] className, float absOrigin[3], float absAngles[3])
{
	int weapon = Entity_Create(className);

	if (weapon == INVALID_ENT_REFERENCE) {
		return INVALID_ENT_REFERENCE;
	}

	Entity_SetAbsOrigin(weapon, absOrigin);
	Entity_SetAbsAngles(weapon, absAngles);

	DispatchSpawn(weapon);

	return weapon;
}

/*
 * Create's a weapon and spawns it in the world at the specified location.
 *
 * @param className		Classname String of the weapon to spawn
 * @param absOrigin		Absolute Origin Vector where to spawn the weapon.
 * @param absAngles		Absolute Angles Vector.
 * @return				Weapon Index of the created weapon or INVALID_ENT_REFERENCE on error.
 */
stock int Weapon_CreateForOwner(int client, const char[] className)
{
	float absOrigin[3], absAngles[3];
	Entity_GetAbsOrigin(client, absOrigin);
	Entity_GetAbsAngles(client, absAngles);

	int weapon = Weapon_Create(className, absOrigin, absAngles);

	if (weapon == INVALID_ENT_REFERENCE) {
		return INVALID_ENT_REFERENCE;
	}

	Entity_SetOwner(weapon, client);

	return weapon;
}

/*
 * Gets the weapon's subtype.
 * The subtype is only used when a player has multiple weapons of the same type.
 *
 * @param weapon		Weapon Entity.
 * @return				Subtype of the weapon.
 */
stock int Weapon_GetSubType(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iSubType");
}

/*
 * Is the weapon currently reloading ?
 *
 * @param weapon		Weapon Entity.
 * @return				True if weapon is currently reloading, false if not.
 */
stock bool Weapon_IsReloading(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_bInReload") != 0;
}

/*
 * Weapon m_iState
 */
#define WEAPON_IS_ONTARGET				0x40
#define WEAPON_NOT_CARRIED				0	// Weapon is on the ground
#define WEAPON_IS_CARRIED_BY_PLAYER		1	// This client is carrying this weapon.
#define WEAPON_IS_ACTIVE				2	// This client is carrying this weapon and it's the currently held weapon

/*
 * Get's the state of the weapon.
 * This returns whether the weapon is currently carried by a client,
 * if it is active and if it is on a target.
 *
 * @param weapon		Weapon Entity.
 * @return				Weapon State.
 */
stock int Weapon_GetState(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iState");
}

/*
 * Returns whether the weapon can fire primary ammo under water.
 *
 * @param weapon		Weapon Entity.
 * @return				True or False.
 */
stock bool Weapon_FiresUnderWater(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_bFiresUnderwater") != 0;
}

/*
 * Sets if the weapon can fire primary ammo under water.
 *
 * @param weapon		Weapon Entity.
 * @param can			True or False.
 */
stock void Weapon_SetFiresUnderWater(int weapon, bool can=true)
{
	SetEntProp(weapon, Prop_Data, "m_bFiresUnderwater", can);
}

/*
 * Returns whether the weapon can fire secondary ammo under water.
 *
 * @param weapon		Weapon Entity.
 * @return				True or False.
 */
stock bool Weapon_FiresUnderWaterAlt(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_bAltFiresUnderwater") != 0;
}

/*
 * Sets if the weapon can fire secondary ammo under water.
 *
 * @param weapon		Weapon Entity.
 * @param can			True or False.
 */
stock void Weapon_SetFiresUnderWaterAlt(int weapon, bool can=true)
{
	SetEntProp(weapon, Prop_Data, "m_bAltFiresUnderwater", can);
}

/*
 * Gets the primary ammo Type (int offset)
 *
 * @param weapon		Weapon Entity.
 * @return				Primary ammo type value.
 */
stock int Weapon_GetPrimaryAmmoType(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iPrimaryAmmoType");
}

/*
 * Sets the primary ammo Type (int offset)
 *
 * @param weapon		Weapon Entity.
 * @param type			Primary ammo type value.
 */
stock void Weapon_SetPrimaryAmmoType(int weapon, int type)
{
	SetEntProp(weapon, Prop_Data, "m_iPrimaryAmmoType", type);
}

/*
 * Gets the secondary ammo Type (int offset)
 *
 * @param weapon		Weapon Entity.
 * @return				Secondary ammo type value.
 */
stock int Weapon_GetSecondaryAmmoType(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iSecondaryAmmoType");
}

/*
 * Sets the secondary ammo Type (int offset)
 *
 * @param weapon		Weapon Entity.
 * @param type			Secondary ammo type value.
 */
stock void Weapon_SetSecondaryAmmoType(int weapon, int type)
{
	SetEntProp(weapon, Prop_Data, "m_iSecondaryAmmoType", type);
}

/*
 * Gets the primary clip count of a weapon.
 *
 * @param weapon		Weapon Entity.
 * @return				Primary Clip count.
 */
stock int Weapon_GetPrimaryClip(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iClip1");
}

/*
 * Sets the primary clip count of a weapon.
 *
 * @param weapon		Weapon Entity.
 * @param value			Clip Count value.
 */
stock void Weapon_SetPrimaryClip(int weapon, int value)
{
	SetEntProp(weapon, Prop_Data, "m_iClip1", value);
}

/*
 * Gets the secondary clip count of a weapon.
 *
 * @param weapon		Weapon Entity.
 * @return				Secondy Clip count.
 */
stock int Weapon_GetSecondaryClip(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iClip2");
}

/*
 * Sets the secondary clip count of a weapon.
 *
 * @param weapon		Weapon Entity.
 * @param value			Clip Count value.
 */
stock void Weapon_SetSecondaryClip(int weapon, int value)
{
	SetEntProp(weapon, Prop_Data, "m_iClip2", value);
}

/*
 * Sets the primary & secondary clip count of a weapon.
 *
 * @param weapon		Weapon Entity.
 * @param primary		Primary Clip Count value.
 * @param secondary		Primary Clip Count value.
 */
stock void Weapon_SetClips(int weapon, int primary, int secondary)
{
	Weapon_SetPrimaryClip(weapon, primary);
	Weapon_SetSecondaryClip(weapon, secondary);
}

/*
 * Gets the primary ammo count of a weapon.
 * This is only used when the weapon is not carried
 * by a player to give a player ammo when he picks up
 * the weapon.
 *
 * @param weapon		Weapon Entity.
 * @return				Primary Ammo Count.
 */
stock int Weapon_GetPrimaryAmmoCount(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iPrimaryAmmoCount");
}

/*
 * Sets the primary ammo count of a weapon.
 * This is only used when the weapon is not carried
 * by a player to give a player ammo when he picks up
 * the weapon.
 *
 * @param weapon		Weapon Entity.
 * @param value			Primary Ammo Count.
 */
stock void Weapon_SetPrimaryAmmoCount(int weapon, int value)
{
	SetEntProp(weapon, Prop_Data, "m_iPrimaryAmmoCount", value);
}

/*
 * Gets the secondary ammo count of a weapon.
 * This is only used when the weapon is not carried
 * by a player to give a player ammo when he picks up
 * the weapon.
 *
 * @param weapon		Weapon Entity.
 * @return				Secondary Ammo Count.
 */
stock int Weapon_GetSecondaryAmmoCount(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_iSecondaryAmmoCount");
}

/*
 * Sets the secodary ammo count of a weapon.
 * This is only used when the weapon is not carried
 * by a player to give a player ammo when he picks up
 * the weapon.
 *
 * @param weapon		Weapon Entity.
 * @param value			Secondary Ammo Count.
 */
stock void Weapon_SetSecondaryAmmoCount(int weapon, int value)
{
	SetEntProp(weapon, Prop_Data, "m_iSecondaryAmmoCount", value);
}

/*
 * Sets both, the primary & the secondary ammo count of a weapon.
 * This is only used when the weapon is not carried
 * by a player to give a player ammo when he picks up
 * the weapon.
 *
 * @param weapon		Weapon Entity.
 * @value primary		Primary Ammo Count.
 * @value secondary		Secondary Ammo Count.
 */
stock void Weapon_SetAmmoCounts(int weapon, int primary, int secondary)
{
	Weapon_SetPrimaryAmmoCount(weapon, primary);
	Weapon_SetSecondaryAmmoCount(weapon, secondary);
}

/*
 * Gets the Model Index of the weapon's view model.
 *
 * @param weapon		Weapon Entity.
 * @return				View Model Index.
 */
stock int Weapon_GetViewModelIndex(int weapon)
{
	return GetEntProp(weapon, Prop_Data, "m_nViewModelIndex");
}

/*
 * Sets the Model Index of the weapon's view model.
 * You can get the Model Index by precaching a model with PrecacheModel().
 *
 * @param weapon		Weapon Entity.
 * @param index			Model Index.
 * @noreturn
 */
stock void Weapon_SetViewModelIndex(int weapon, int index)
{
	SetEntProp(weapon, Prop_Data, "m_nViewModelIndex", index);
	ChangeEdictState(weapon, FindDataMapInfo(weapon, "m_nViewModelIndex"));
}