forked from SilvDev/Various_Scripts_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl4d2_upgrade_block.sp
More file actions
447 lines (374 loc) · 12.6 KB
/
l4d2_upgrade_block.sp
File metadata and controls
447 lines (374 loc) · 12.6 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#define PLUGIN_VERSION "1.4"
/*======================================================================================
Plugin Info:
* Name : [L4D2] Upgrade Ammo Block - Grenade Launcher
* Author : SilverShot
* Descrp : Blocks the Grenade Launcher from picking up incendiary or explosive upgrade ammo.
* Link : https://forums.alliedmods.net/showthread.php?t=319354
* Plugins : https://sourcemod.net/plugins.php?exact=exact&sortby=title&search=1&author=Silvers
========================================================================================
Change Log:
1.4 (10-May-2020)
- Blocked test commands.
- Extra checks to prevent "IsAllowedGameMode" throwing errors.
- Various changes to tidy up code.
- Various optimizations and fixes.
1.3 (01-Apr-2020)
- Fixed "IsAllowedGameMode" from throwing errors when the "_tog" cvar was changed before MapStart.
1.2 (30-Oct-2019)
- Fixed glow not turning off after map transition or when a new upgrade pack is spawned.
1.1 (28-Oct-2019)
- Added cvar "l4d2_upgrade_block_glow" to turn glow on/off.
1.0 (27-Oct-2019)
- Initial release.
====================================================================================================*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#define CVAR_FLAGS FCVAR_NOTIFY
ConVar g_hCvarAllow, g_hCvarMPGameMode, g_hCvarModes, g_hCvarModesOff, g_hCvarModesTog, g_hCvarGlow, g_hCvarType;
bool g_bCvarAllow, g_bMapStarted, g_bHookedExplo, g_bHookedIncen;
int g_iCvarGlow, g_iCvarType;
int g_iEntities[2048];
int g_iEntMasks[2048];
bool g_bMasked[MAXPLAYERS+1];
// ====================================================================================================
// PLUGIN INFO / START / END
// ====================================================================================================
public Plugin myinfo =
{
name = "[L4D2] Upgrade Ammo Block - Grenade Launcher",
author = "SilverShot",
description = "Block the Grenade Launcher from picking up incendiary or explosive ammo.",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=319354"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
if( GetEngineVersion() != Engine_Left4Dead2 )
{
strcopy(error, err_max, "Plugin only supports Left 4 Dead 2.");
return APLRes_SilentFailure;
}
return APLRes_Success;
}
public void OnPluginStart()
{
g_hCvarAllow = CreateConVar( "l4d2_upgrade_block_allow", "1", "0=Plugin off, 1=Plugin on.", CVAR_FLAGS );
g_hCvarModes = CreateConVar( "l4d2_upgrade_block_modes", "", "Turn on the plugin in these game modes, separate by commas (no spaces). (Empty = all).", CVAR_FLAGS );
g_hCvarModesOff = CreateConVar( "l4d2_upgrade_block_modes_off", "", "Turn off the plugin in these game modes, separate by commas (no spaces). (Empty = none).", CVAR_FLAGS );
g_hCvarModesTog = CreateConVar( "l4d2_upgrade_block_modes_tog", "0", "Turn on the plugin in these game modes. 0=All, 1=Coop, 2=Survival, 4=Versus, 8=Scavenge. Add numbers together.", CVAR_FLAGS );
g_hCvarGlow = CreateConVar( "l4d2_upgrade_block_glow", "0", "When ammo pickup is blocked set upgrade pack glow to: 0=Off. 1=On.", CVAR_FLAGS );
g_hCvarType = CreateConVar( "l4d2_upgrade_block_type", "2", "Prevent grenade launchers picking up ammo from: 1=Explosive Ammo. 2=Incendiary Ammo. 3=Both.", CVAR_FLAGS );
CreateConVar( "l4d2_upgrade_block_version", PLUGIN_VERSION, "Upgrade Block plugin version.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "l4d2_upgrade_block");
g_hCvarMPGameMode = FindConVar("mp_gamemode");
g_hCvarMPGameMode.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesTog.AddChangeHook(ConVarChanged_Allow);
g_hCvarModes.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesOff.AddChangeHook(ConVarChanged_Allow);
g_hCvarAllow.AddChangeHook(ConVarChanged_Allow);
g_hCvarGlow.AddChangeHook(ConVarChanged_Cvars);
g_hCvarType.AddChangeHook(ConVarChanged_Cvars);
RegAdminCmd("sm_uboff", sm_uboff, ADMFLAG_ROOT, "Test command.");
RegAdminCmd("sm_ubon", sm_ubon, ADMFLAG_ROOT, "Test command.");
}
public Action sm_uboff(int c, int a)
{
for( int i = 1; i <= MaxClients; i++ )
{
if( IsClientInGame(i) && !IsFakeClient(i) )
{
g_bMasked[i] = false;
SetMask(i, false);
SDKUnhook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
if( !g_iCvarGlow )
SDKHook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
}
}
return Plugin_Handled;
}
public Action sm_ubon(int c, int a)
{
for( int i = 1; i <= MaxClients; i++ )
{
if( IsClientInGame(i) && !IsFakeClient(i) )
{
g_bMasked[i] = false;
OnWeaponEquip(i, 0);
SDKUnhook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
if( !g_iCvarGlow )
SDKHook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
}
}
return Plugin_Handled;
}
// ====================================================================================================
// CVARS
// ====================================================================================================
public void OnMapStart()
{
g_bMapStarted = true;
}
public void OnMapEnd()
{
g_bMapStarted = false;
}
public void OnConfigsExecuted()
{
IsAllowed();
}
public void ConVarChanged_Allow(Handle convar, const char[] oldValue, const char[] newValue)
{
IsAllowed();
}
public void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
{
GetCvars();
HookToggle();
}
void GetCvars()
{
g_iCvarGlow = g_hCvarGlow.IntValue;
g_iCvarType = g_hCvarType.IntValue;
}
void IsAllowed()
{
bool bCvarAllow = g_hCvarAllow.BoolValue;
bool bAllowMode = IsAllowedGameMode();
GetCvars();
if( g_bCvarAllow == false && bCvarAllow == true && bAllowMode == true )
{
g_bCvarAllow = true;
HookToggle();
}
else if( g_bCvarAllow == true && (bCvarAllow == false || bAllowMode == false) )
{
g_bCvarAllow = false;
HookToggle();
}
}
int g_iCurrentMode;
bool IsAllowedGameMode()
{
if( g_hCvarMPGameMode == null )
return false;
int iCvarModesTog = g_hCvarModesTog.IntValue;
if( iCvarModesTog != 0 )
{
if( g_bMapStarted == false )
return false;
g_iCurrentMode = 0;
int entity = CreateEntityByName("info_gamemode");
if( IsValidEntity(entity) )
{
DispatchSpawn(entity);
HookSingleEntityOutput(entity, "OnCoop", OnGamemode, true);
HookSingleEntityOutput(entity, "OnSurvival", OnGamemode, true);
HookSingleEntityOutput(entity, "OnVersus", OnGamemode, true);
HookSingleEntityOutput(entity, "OnScavenge", OnGamemode, true);
ActivateEntity(entity);
AcceptEntityInput(entity, "PostSpawnActivate");
if( IsValidEntity(entity) ) // Because sometimes "PostSpawnActivate" seems to kill the ent.
RemoveEdict(entity); // Because multiple plugins creating at once, avoid too many duplicate ents in the same frame
}
if( g_iCurrentMode == 0 )
return false;
if( !(iCvarModesTog & g_iCurrentMode) )
return false;
}
char sGameModes[64], sGameMode[64];
g_hCvarMPGameMode.GetString(sGameMode, sizeof(sGameMode));
Format(sGameMode, sizeof(sGameMode), ",%s,", sGameMode);
g_hCvarModes.GetString(sGameModes, sizeof(sGameModes));
if( sGameModes[0] )
{
Format(sGameModes, sizeof(sGameModes), ",%s,", sGameModes);
if( StrContains(sGameModes, sGameMode, false) == -1 )
return false;
}
g_hCvarModesOff.GetString(sGameModes, sizeof(sGameModes));
if( sGameModes[0] )
{
Format(sGameModes, sizeof(sGameModes), ",%s,", sGameModes);
if( StrContains(sGameModes, sGameMode, false) != -1 )
return false;
}
return true;
}
public void OnGamemode(const char[] output, int caller, int activator, float delay)
{
if( strcmp(output, "OnCoop") == 0 )
g_iCurrentMode = 1;
else if( strcmp(output, "OnSurvival") == 0 )
g_iCurrentMode = 2;
else if( strcmp(output, "OnVersus") == 0 )
g_iCurrentMode = 4;
else if( strcmp(output, "OnScavenge") == 0 )
g_iCurrentMode = 8;
}
// ====================================================================================================
// STUFF
// ====================================================================================================
void HookToggle()
{
for( int i = 1; i <= MaxClients; i++ )
{
if( IsClientInGame(i) && !IsFakeClient(i) )
{
g_bMasked[i] = false;
SetMask(i, false);
SDKUnhook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
if( !g_iCvarGlow )
SDKHook(i, SDKHook_WeaponEquipPost, OnWeaponEquip);
}
}
bool ok = g_bCvarAllow && g_iCvarType & (1<<0);
if( (!g_bHookedExplo && ok) || (g_bHookedExplo && !ok) )
{
g_bHookedExplo = !g_bHookedExplo;
int entity = -1;
while( (entity = FindEntityByClassname(entity, "upgrade_ammo_explosive")) != INVALID_ENT_REFERENCE )
{
g_iEntMasks[entity] = 0;
if( ok ) g_iEntities[entity] = EntIndexToEntRef(entity);
else g_iEntities[entity] = 0;
if( ok ) SDKHook(entity, SDKHook_Use, OnUse);
else SDKUnhook(entity, SDKHook_Use, OnUse);
}
}
ok = g_bCvarAllow && g_iCvarType & (1<<1);
if( (!g_bHookedIncen && ok) || (g_bHookedIncen && !ok) )
{
g_bHookedIncen = !g_bHookedIncen;
int entity = -1;
while( (entity = FindEntityByClassname(entity, "upgrade_ammo_incendiary")) != INVALID_ENT_REFERENCE )
{
g_iEntMasks[entity] = 0;
if( ok ) g_iEntities[entity] = EntIndexToEntRef(entity);
else g_iEntities[entity] = 0;
if( ok ) SDKHook(entity, SDKHook_Use, OnUse);
else SDKUnhook(entity, SDKHook_Use, OnUse);
}
}
if( g_bCvarAllow )
{
for( int i = 1; i <= MaxClients; i++ )
{
if( IsClientInGame(i) && !IsFakeClient(i) )
{
OnWeaponEquip(i, 0);
}
}
}
}
public void OnEntityCreated(int entity, const char[] classname)
{
if(
g_bCvarAllow &&
(g_bHookedExplo && strcmp(classname, "upgrade_ammo_explosive") == 0) ||
(g_bHookedIncen && strcmp(classname, "upgrade_ammo_incendiary") == 0)
)
{
SDKHook(entity, SDKHook_Use, OnUse);
g_iEntMasks[entity] = 0;
g_iEntities[entity] = EntIndexToEntRef(entity);
if( !g_iCvarGlow )
{
SDKHook(entity, SDKHook_SpawnPost, OnSpawn); // Otherwise the mask value is overwritten when entity spawned.
}
}
}
public void OnSpawn(int entity)
{
for( int i = 1; i <= MaxClients; i++ )
{
if( g_bMasked[i] && IsClientInGame(i) && GetClientTeam(i) == 2 && IsPlayerAlive(i) )
{
g_bMasked[i] = false;
OnWeaponEquip(i, 0);
}
}
}
public Action OnUse(int entity, int activator, int caller, UseType type, float value)
{
int weapon = GetPlayerWeaponSlot(activator, 0);
if( weapon != -1 && IsGrenadeLauncher(weapon) )
{
return Plugin_Handled;
}
return Plugin_Continue;
}
// ====================================================================================================
// STOP GLOW
// ====================================================================================================
public void OnClientPutInServer(int client)
{
if( g_bCvarAllow && !g_iCvarGlow && !IsFakeClient(client) )
{
g_bMasked[client] = false;
SDKHook(client, SDKHook_WeaponEquipPost, OnWeaponEquip);
}
}
public void OnWeaponEquip(int client, int weapon)
{
weapon = GetPlayerWeaponSlot(client, 0); // Because of !drop plugins not firing SDKHook_WeaponDropPost.
if( weapon != -1 && IsGrenadeLauncher(weapon) )
{
if( g_bMasked[client] == false )
{
g_bMasked[client] = true;
SetMask(client, true);
}
}
else if( g_bMasked[client] )
{
g_bMasked[client] = false;
SetMask(client, false);
}
}
void SetMask(int client, bool set)
{
int mask;
for( int i = 0; i < 2048; i++ )
{
if( IsValidEntRef(g_iEntities[i]) )
{
if( set )
{
mask = GetEntProp(i, Prop_Send, "m_iUsedBySurvivorsMask");
// Client not used
if( !(mask & (1<<client-1)) )
{
// Mark as temporary used
SetEntProp(i, Prop_Send, "m_iUsedBySurvivorsMask", mask | (1<<client-1));
g_iEntMasks[i] |= (1<<client-1);
}
} else {
// Reset temporary used
if( g_iEntMasks[i] & (1<<client-1) )
{
mask = GetEntProp(i, Prop_Send, "m_iUsedBySurvivorsMask");
SetEntProp(i, Prop_Send, "m_iUsedBySurvivorsMask", mask & ~(1<<client-1));
g_iEntMasks[i] &= ~(1<<client-1);
}
}
}
}
}
bool IsGrenadeLauncher(int weapon)
{
static char classname[32];
GetEdictClassname(weapon, classname, sizeof(classname));
if( strcmp(classname[7], "grenade_launcher") == 0 )
return true;
return false;
}
bool IsValidEntRef(int entity)
{
if( entity && EntRefToEntIndex(entity) != INVALID_ENT_REFERENCE )
return true;
return false;
}