-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathl4d_reload_interrupt.sp
More file actions
810 lines (684 loc) · 25.6 KB
/
l4d_reload_interrupt.sp
File metadata and controls
810 lines (684 loc) · 25.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
* Reload Interrupt
* Copyright (C) 2026 Silvers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define PLUGIN_VERSION "1.15"
/*======================================================================================
Plugin Info:
* Name : [L4D & L4D2] Reload Interrupt
* Author : SilverShot
* Descrp : Shooting cancels reloading like TF2 weapons. Also can auto reload weapons after shooting.
* Link : https://forums.alliedmods.net/showthread.php?t=324395
* Plugins : https://sourcemod.net/plugins.php?exact=exact&sortby=title&search=1&author=Silvers
========================================================================================
Change Log:
1.15 (14-Mar-2026)
- Changed cvar "l4d_reload_interrupt_weapons" to control if the M60 and Grenade Launcher should be included. Requested by "DosonShiro".
1.14 (05-Nov-2024)
- Fixed the plugin not working in Left 4 Dead 1. Thanks to "ZBzibing" for reporting.
1.13 (17-Jun-2024)
- Fixed shotguns losing ammo in L4D1 or clip size increasing.
1.12 (03-Dec-2022)
- Fixed unknown sound errors in console. Thanks to "TBK Duy" for reporting.
1.11 (26-Nov-2022)
- Fixed breaking under certain conditions until swapping weapon. Thanks to "TBK Duy" for reporting.
1.10 (15-Jan-2022)
- Fixed L4D1 not reading reserve ammo correctly. Thanks to "ZBzibing" for reporting.
1.9 (27-Oct-2021)
- Fixed the plugin wiping reserve ammo if it was turned off during gameplay.
1.8 (06-Oct-2021)
- Fixed going AFK and bots reloading emptying the reserve clip. Thanks to "TQH" for reporting.
1.7 (13-Apr-2021)
- Another precache sound fix attempt. Should be right now.
1.6 (27-Mar-2021)
- Fixed precache sound errors by verifying files exist before. Thanks to "Maur0" for reporting.
1.5 (20-Mar-2021)
- Fixed some console errors due to not precaching sounds. Thanks to "Balloons" for reporting.
1.4 (28-Sep-2020)
- Blocked bots from using to prevent any bugs.
- Fixed the plugin sometimes wiping ammo when quickly switching weapons (Gear Transfer - grenades issue).
1.3 (07-Jun-2020)
- Fixed L4D1 issues. Thanks to "jamalsheref2" for reporting.
1.2 (19-May-2020)
- Added extra check on switching weapons. Thanks to "Crasher_3637" for reporting.
- Added reset vars on player spawn and death.
1.1 (16-May-2020)
- Fixed reserve ammo increasing. Thanks to "Crasher_3637" for reporting.
1.0 (15-May-2020)
- 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_hCvarRestart, g_hCvarWeapons;
bool g_bCvarAllow, g_bMapStarted, g_bLeft4Dead2;
int g_iCvarRestart, g_iOffsetAmmo, g_iPrimaryAmmoType, g_iCvarWeapons;
int g_iForceTicks; // How many times to force the IN_RELOAD buttons (otherwise some weapons e.g. shotguns will not auto reload after multiple shots).
int g_iLastAmmo[MAXPLAYERS+1];
int g_iLastClip[MAXPLAYERS+1];
int g_iLastHook[MAXPLAYERS+1];
int g_iWasShoot[MAXPLAYERS+1];
int g_iAutoReload[MAXPLAYERS+1];
float g_fFrames[MAXPLAYERS+1];
StringMap g_hWeaponClasses;
StringMap g_hWeaponAllowed;
// Primary sound + incendiary sound (if available)
char g_sWeaponSounds[][][] =
{
{
")weapons/pistol/gunfire/pistol_fire.wav",
")weapons/pistol/gunfire/pistol_fire.wav"
},
{
")weapons/magnum/gunfire/magnum_shoot.wav",
")weapons/magnum/gunfire/magnum_shoot.wav"
},
{
")weapons/awp/gunfire/awp1.wav",
")weapons/awp/gunfire/awp1.wav"
},
{
")weapons/hunting_rifle/gunfire/hunting_rifle_fire_1.wav",
")weapons/hunting_rifle/gunfire/hunting_rifle_fire_1_incendiary.wav"
},
{
")weapons/rifle/gunfire/rifle_fire_1.wav",
")weapons/rifle/gunfire/rifle_fire_1_incendiary.wav"
},
{
")weapons/rifle_ak47/gunfire/rifle_fire_1.wav",
")weapons/rifle_ak47/gunfire/rifle_fire_1_incendiary.wav"
},
{
")weapons/rifle_desert/gunfire/rifle_fire_1.wav",
")weapons/rifle_desert/gunfire/rifle_fire_1_incendiary.wav"
},
{
")weapons/scout/gunfire/scout_fire-1.wav",
")weapons/scout/gunfire/scout_fire-1.wav"
},
{
")weapons/sg552/gunfire/sg552-1.wav",
")weapons/sg552/gunfire/sg552-1.wav"
},
{
")weapons/smg/gunfire/smg_fire_1.wav",
")weapons/smg/gunfire/smg_fire_1_incendiary.wav"
},
{
")weapons/smg_silenced/gunfire/smg_fire_1.wav",
")weapons/smg_silenced/gunfire/smg_fire_1_incendiary.wav"
},
{
")weapons/mp5navy/gunfire/mp5-1.wav",
")weapons/mp5navy/gunfire/mp5-1.wav"
},
{
")weapons/sniper_military/gunfire/sniper_military_fire_1.wav",
")weapons/sniper_military/gunfire/sniper_military_fire_1_incendiary.wav"
}
};
// ====================================================================================================
// PLUGIN INFO / START / END
// ====================================================================================================
public Plugin myinfo =
{
name = "[L4D & L4D2] Reload Interrupt",
author = "SilverShot",
description = "Shooting cancels reloading like TF2 weapons. Also can auto reload weapons after shooting.",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=324395"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
EngineVersion test = GetEngineVersion();
if( test == Engine_Left4Dead ) g_bLeft4Dead2 = false;
else if( test == Engine_Left4Dead2 ) g_bLeft4Dead2 = true;
else
{
strcopy(error, err_max, "Plugin only supports Left 4 Dead 1 & 2.");
return APLRes_SilentFailure;
}
return APLRes_Success;
}
public void OnPluginStart()
{
// ====================================================================================================
// CVARS
// ====================================================================================================
g_hCvarAllow = CreateConVar( "l4d_reload_interrupt_allow", "1", "0=Plugin off, 1=Plugin on.", CVAR_FLAGS );
g_hCvarModes = CreateConVar( "l4d_reload_interrupt_modes", "", "Turn on the plugin in these game modes, separate by commas (no spaces). (Empty = all).", CVAR_FLAGS );
g_hCvarModesOff = CreateConVar( "l4d_reload_interrupt_modes_off", "", "Turn off the plugin in these game modes, separate by commas (no spaces). (Empty = none).", CVAR_FLAGS );
g_hCvarModesTog = CreateConVar( "l4d_reload_interrupt_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_hCvarRestart = CreateConVar( "l4d_reload_interrupt_restart", "1", "0=Off. 1=Restart reloading when reloading was interrupted by shooting. 2=Auto reload anytime shooting stops.", CVAR_FLAGS );
g_hCvarWeapons = CreateConVar( "l4d_reload_interrupt_weapons", "524287", "Allowed weapons (add numbers together). See plugin thread for details (values and classnames are too long to display in cvar description). 524287=All.", CVAR_FLAGS );
CreateConVar( "l4d_reload_interrupt_version", PLUGIN_VERSION, "Reload Interrupt plugin version.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "l4d_reload_interrupt");
g_hCvarMPGameMode = FindConVar("mp_gamemode");
g_hCvarMPGameMode.AddChangeHook(ConVarChanged_Allow);
g_hCvarModes.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesOff.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesTog.AddChangeHook(ConVarChanged_Allow);
g_hCvarAllow.AddChangeHook(ConVarChanged_Allow);
g_hCvarRestart.AddChangeHook(ConVarChanged_Cvars);
g_hCvarWeapons.AddChangeHook(ConVarChanged_Cvars);
// ====================================================================================================
// STUFF
// ====================================================================================================
// Required to press reload over several frames otherwise shotguns for example will not reload. They will stop on multiple shots anyway when restart is set to 1 because the game resets m_bInReload to 0 sometimes.
g_iForceTicks = RoundFloat(1 / GetTickInterval());
g_iForceTicks = RoundToFloor(g_iForceTicks / 5.0);
// Offsets to setting reserve ammo
g_iOffsetAmmo = FindSendPropInfo("CTerrorPlayer", "m_iAmmo");
g_iPrimaryAmmoType = FindSendPropInfo("CBaseCombatWeapon", "m_iPrimaryAmmoType");
// Indexes for sounds
g_hWeaponClasses = new StringMap();
g_hWeaponClasses.SetValue("weapon_pistol", 0);
g_hWeaponClasses.SetValue("weapon_hunting_rifle", 3);
g_hWeaponClasses.SetValue("weapon_rifle", 4);
g_hWeaponClasses.SetValue("weapon_smg", 9);
if( g_bLeft4Dead2 )
{
g_hWeaponClasses.SetValue("weapon_pistol_magnum", 1);
g_hWeaponClasses.SetValue("weapon_sniper_awp", 2);
g_hWeaponClasses.SetValue("weapon_rifle_ak47", 5);
g_hWeaponClasses.SetValue("weapon_rifle_desert", 6);
g_hWeaponClasses.SetValue("weapon_sniper_scout", 7);
g_hWeaponClasses.SetValue("weapon_rifle_sg552", 8);
g_hWeaponClasses.SetValue("weapon_smg_silenced", 10);
g_hWeaponClasses.SetValue("weapon_smg_mp5", 11);
g_hWeaponClasses.SetValue("weapon_sniper_military", 12);
}
// Allowed weapons cvar
g_hWeaponAllowed = new StringMap();
g_hWeaponAllowed.SetValue("weapon_autoshotgun", 1);
g_hWeaponAllowed.SetValue("weapon_hunting_rifle", 2);
g_hWeaponAllowed.SetValue("weapon_pistol", 4);
g_hWeaponAllowed.SetValue("weapon_pistol_magnum", 8);
g_hWeaponAllowed.SetValue("weapon_pumpshotgun", 16);
g_hWeaponAllowed.SetValue("weapon_rifle", 32);
g_hWeaponAllowed.SetValue("weapon_rifle_ak47", 64);
g_hWeaponAllowed.SetValue("weapon_rifle_desert", 128);
g_hWeaponAllowed.SetValue("weapon_rifle_sg552", 256);
g_hWeaponAllowed.SetValue("weapon_shotgun_chrome", 512);
g_hWeaponAllowed.SetValue("weapon_shotgun_spas", 1024);
g_hWeaponAllowed.SetValue("weapon_smg", 2048);
g_hWeaponAllowed.SetValue("weapon_smg_mp5", 4096);
g_hWeaponAllowed.SetValue("weapon_smg_silenced", 8192);
g_hWeaponAllowed.SetValue("weapon_sniper_awp", 16384);
g_hWeaponAllowed.SetValue("weapon_sniper_military", 32768);
g_hWeaponAllowed.SetValue("weapon_sniper_scout", 65536);
g_hWeaponAllowed.SetValue("weapon_grenade_launcher", 131072);
g_hWeaponAllowed.SetValue("weapon_rifle_m60", 262144);
// L4D1 fix:
if( !g_bLeft4Dead2 )
{
for( int i = 0; i < sizeof(g_sWeaponSounds); i++ )
{
g_sWeaponSounds[i][0][0] = '^';
g_sWeaponSounds[i][1][0] = '^';
}
}
}
// ====================================================================================================
// CVARS
// ====================================================================================================
public void OnConfigsExecuted()
{
IsAllowed();
}
void ConVarChanged_Allow(Handle convar, const char[] oldValue, const char[] newValue)
{
IsAllowed();
}
void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
{
GetCvars();
}
void GetCvars()
{
g_iCvarWeapons = g_hCvarWeapons.IntValue;
g_iCvarRestart = g_hCvarRestart.IntValue;
HookFireEvent(!g_bLeft4Dead2 || g_iCvarRestart == 2);
}
void IsAllowed()
{
bool bCvarAllow = g_hCvarAllow.BoolValue;
bool bAllowMode = IsAllowedGameMode();
GetCvars();
if( g_bCvarAllow == false && bCvarAllow == true && bAllowMode == true )
{
g_bCvarAllow = true;
HookFireEvent(!g_bLeft4Dead2 ||g_iCvarRestart == 2);
HookEvents();
// Hook active clients
int weapon;
for( int i = 1; i <= MaxClients; i++ )
{
if( IsClientInGame(i) && GetClientTeam(i) == 2 && IsPlayerAlive(i) && !IsFakeClient(i) )
{
SDKHook(i, SDKHook_WeaponSwitchPost, OnSwitchWeapon);
// Validate current weapon
weapon = GetEntPropEnt(i, Prop_Send, "m_hActiveWeapon");
if( weapon > 0 )
{
OnSwitchWeapon(i, weapon);
}
}
}
}
else if( g_bCvarAllow == true && (bCvarAllow == false || bAllowMode == false) )
{
g_bCvarAllow = false;
HookFireEvent(false);
UnhookEvents();
// Unhook active clients
int last;
for( int i = 1; i <= MaxClients; i++ )
{
last = g_iLastHook[i];
if( last && EntRefToEntIndex(last) != INVALID_ENT_REFERENCE )
{
SDKUnhook(last, SDKHook_Reload, OnReload);
}
}
ResetPlugin();
}
}
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;
}
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;
}
// ====================================================================================================
// EVENTS
// ====================================================================================================
// Only hook fire event if auto reload 2 enabled.
void HookFireEvent(bool hook)
{
static bool hooked;
if( hook == true && hooked == false )
{
hooked = true;
HookEvent("weapon_fire", Event_WeaponFire);
}
else if( hook == false && hooked == true )
{
hooked = false;
UnhookEvent("weapon_fire", Event_WeaponFire);
}
}
void HookEvents()
{
if( !g_bLeft4Dead2 )
HookEvent("weapon_reload", Event_WeaponReload);
HookEvent("ammo_pickup", Event_AmmoPickup);
HookEvent("player_spawn", Event_PlayerSpawn);
HookEvent("player_death", Event_PlayerDeath);
HookEvent("player_team", Event_PlayerTeam);
HookEvent("round_end", Event_RoundEnd, EventHookMode_PostNoCopy);
}
void UnhookEvents()
{
if( !g_bLeft4Dead2 )
UnhookEvent("weapon_reload", Event_WeaponReload);
UnhookEvent("ammo_pickup", Event_AmmoPickup);
UnhookEvent("player_spawn", Event_PlayerSpawn);
UnhookEvent("player_death", Event_PlayerDeath);
UnhookEvent("player_team", Event_PlayerTeam);
UnhookEvent("round_end", Event_RoundEnd, EventHookMode_PostNoCopy);
}
// Auto reload after shooting
void Event_WeaponFire(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( (g_iAutoReload[client] || !g_bLeft4Dead2) && !IsFakeClient(client) )
{
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon > -1 )
{
if( !g_bLeft4Dead2 )
g_iLastClip[client] = GetEntProp(weapon, Prop_Send, "m_iClip1") - 1;
if( g_iCvarRestart == 2 )
g_iWasShoot[client] = g_iForceTicks;
}
}
}
void Event_WeaponReload(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( client && !IsFakeClient(client) )
{
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon != -1 && EntIndexToEntRef(weapon) == g_iLastHook[client] )
{
OnReload(weapon);
}
}
}
// Correct stored ammo values when picking up ammo during reload
void Event_AmmoPickup(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( client && !IsFakeClient(client) )
{
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon != -1 && GetEntProp(weapon, Prop_Send, "m_bInReload") )
{
g_iLastAmmo[client] = GetOrSetPlayerAmmo(client, weapon) - g_iLastClip[client];
}
}
}
void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( client && GetClientTeam(client) == 2 && !IsFakeClient(client) )
{
ResetVars(client);
SDKHook(client, SDKHook_WeaponSwitchPost, OnSwitchWeapon);
}
}
void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( client )
{
ResetVars(client);
SDKUnhook(client, SDKHook_WeaponSwitchPost, OnSwitchWeapon);
}
}
void Event_PlayerTeam(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( client )
{
ResetVars(client);
SDKUnhook(client, SDKHook_WeaponSwitchPost, OnSwitchWeapon);
}
}
void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
ResetPlugin();
}
public void OnMapStart()
{
g_bMapStarted = true;
char sPath[PLATFORM_MAX_PATH];
for( int i = 0; i < sizeof(g_sWeaponSounds); i++ )
{
Format(sPath, sizeof(sPath), "sound/%s", g_sWeaponSounds[i][0][1]);
if( FileExists(sPath) )
{
// PrintToServer("CACHE SOUND A %d [%s]", i, sPath);
PrecacheSound(g_sWeaponSounds[i][0][1]);
}
Format(sPath, sizeof(sPath), "sound/%s", g_sWeaponSounds[i][1][1]);
if( FileExists(sPath) )
{
// PrintToServer("CACHE SOUND B %d [%s]", i, sPath);
PrecacheSound(g_sWeaponSounds[i][1][1]);
}
}
}
public void OnMapEnd()
{
g_bMapStarted = false;
ResetPlugin();
}
void ResetPlugin()
{
for( int i = 1; i <= MaxClients; i++ )
{
ResetVars(i);
if( IsClientInGame(i) )
{
SDKUnhook(i, SDKHook_WeaponSwitchPost, OnSwitchWeapon);
}
}
}
void ResetVars(int client)
{
g_iLastAmmo[client] = 0;
g_iLastClip[client] = 0;
g_iLastHook[client] = 0;
g_iWasShoot[client] = 0;
g_iAutoReload[client] = 0;
}
// ====================================================================================================
// RELOAD STUFF
// ====================================================================================================
void OnSwitchWeapon(int client, int weapon)
{
// Unhook last weapon
if( g_bLeft4Dead2 )
{
int last = g_iLastHook[client];
if( last && EntRefToEntIndex(last) != INVALID_ENT_REFERENCE )
{
SDKUnhook(last, SDKHook_Reload, OnReload);
}
}
g_iLastAmmo[client] = 0;
g_iLastClip[client] = 0;
g_iLastHook[client] = 0;
g_iWasShoot[client] = 0;
g_iAutoReload[client] = 0;
weapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
if( !IsValidEntity(weapon) || IsFakeClient(client) )
return;
// Validate new weapon
char sWeapon[32];
GetEdictClassname(weapon, sWeapon, sizeof(sWeapon));
// Allowed weapons
int offset;
g_hWeaponAllowed.GetValue(sWeapon, offset);
if( offset == 0 || !(g_iCvarWeapons & offset) )
return;
// Ignore melee - probably not required since above check doesn't include melee weapons
if( g_bLeft4Dead2 && strcmp(sWeapon[7], "melee") == 0 )
return;
if( strncmp(sWeapon[7], "pistol", 6) == 0 )
{
g_iAutoReload[client] = 2;
g_iLastHook[client] = EntIndexToEntRef(weapon);
if( g_bLeft4Dead2 ) // Exception reported: Hook type not supported on this game
SDKHook(weapon, SDKHook_Reload, OnReload);
if( !g_bLeft4Dead2 )
{
g_iLastAmmo[client] = GetOrSetPlayerAmmo(client, weapon);
g_iLastClip[client] = GetEntProp(weapon, Prop_Send, "m_iClip1");
}
}
else
{
offset = GetEntData(weapon, g_iPrimaryAmmoType) * 4; // Thanks to "Root" or whoever for this method of not hard-coding offsets: https://github.com/zadroot/AmmoManager/blob/master/scripting/ammo_manager.sp
if( offset )
{
switch( g_bLeft4Dead2 )
{
case true: g_iAutoReload[client] = (offset == 28 || offset == 32) ? 3 : 1;
case false: g_iAutoReload[client] = offset == 24 ? 3 : 1;
}
g_iLastHook[client] = EntIndexToEntRef(weapon);
if( g_bLeft4Dead2 )
SDKHook(weapon, SDKHook_Reload, OnReload);
if( !g_bLeft4Dead2 )
{
g_iLastAmmo[client] = GetOrSetPlayerAmmo(client, weapon);
g_iLastClip[client] = GetEntProp(weapon, Prop_Send, "m_iClip1");
}
}
}
}
// Store reserve ammo and clip ammo to restore if shooting interrupts reloading.
void OnReload(int weapon)
{
int client = GetEntPropEnt(weapon, Prop_Send, "m_hOwner");
if( g_fFrames[client] == GetGameTime() )
return;
else
g_fFrames[client] = GetGameTime();
if( g_iAutoReload[client] == 3 ) // Shotguns don't store clip size, they don't reset to 0 on reload
{
g_iLastClip[client] = -1;
}
else
{
if( g_iAutoReload[client] == 1 ) // Save reserve ammo value for non-pistols
g_iLastAmmo[client] = GetOrSetPlayerAmmo(client, weapon);
if( g_bLeft4Dead2 )
g_iLastClip[client] = GetEntProp(weapon, Prop_Send, "m_iClip1");
RequestFrame(OnFrame, EntIndexToEntRef(weapon)); // Set clip ammo so it doesn't display as 0
}
}
void OnFrame(int weapon)
{
weapon = EntRefToEntIndex(weapon);
if( IsValidEntity(weapon) && GetEntProp(weapon, Prop_Send, "m_bInReload") )
{
int client = GetEntPropEnt(weapon, Prop_Send, "m_hOwner");
if( client > 0 && !IsFakeClient(client) )
{
SetEntProp(weapon, Prop_Send, "m_iClip1", g_iLastClip[client]);
if( g_bLeft4Dead2 )
GetOrSetPlayerAmmo(client, weapon, g_iLastAmmo[client]);
else
GetOrSetPlayerAmmo(client, weapon, g_iLastAmmo[client] - g_iLastClip[client]);
}
}
}
public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon)
{
// Survivor shooting
if( buttons & IN_ATTACK && g_iAutoReload[client] && !IsFakeClient(client) )
{
if( g_iLastClip[client] ) // && GetClientTeam(client) == 2 && IsPlayerAlive(client) // Probably don't need checks, they should be handled via events and hooking OnReload
{
// Has weapon, is reloading and clip not empty
weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon > 0 && GetEntProp(weapon, Prop_Send, "m_bInReload") )
{
// Play shooting sounds since it bugs and doesn't play when shooting after during reload
int index = -1;
static char classname[32];
GetEdictClassname(weapon, classname, sizeof(classname));
g_hWeaponClasses.GetValue(classname, index);
if( index != -1 )
{
// Play incendiary ammo sound if required
int type = (g_bLeft4Dead2 && GetEntProp(weapon, Prop_Send, "m_upgradeBitVec") & 1);
EmitSoundToClient(client, g_sWeaponSounds[index][type], client, SNDCHAN_WEAPON, SNDLEVEL_MINIBIKE, SND_NOFLAGS, index ? 1.0 : 0.649902); // Pistol has reduced volume
}
// Auto reload after shooting?
if( g_iCvarRestart == 1 )
{
g_iWasShoot[client] = g_iForceTicks;
}
// Not a shotgun, interrupt reload
if( g_iLastClip[client] != -1 )
{
// Stop reloading
float time = GetGameTime();
SetEntProp(weapon, Prop_Send, "m_bInReload", 0);
SetEntPropFloat(client, Prop_Send, "m_flNextAttack", time);
SetEntPropFloat(weapon, Prop_Send, "m_flNextPrimaryAttack", time);
// SetEntPropFloat(weapon, Prop_Send, "m_flNextSecondaryAttack", time); // No (enabled) secondary weapon functions in L4D/2
// Restore clip and reserve ammo
SetEntProp(weapon, Prop_Send, "m_iClip1", g_iLastClip[client]);
if( g_bLeft4Dead2 )
GetOrSetPlayerAmmo(client, weapon, g_iLastAmmo[client]);
else
GetOrSetPlayerAmmo(client, weapon, g_iLastAmmo[client] - g_iLastClip[client]);
}
}
}
}
else if( g_iWasShoot[client] && !IsFakeClient(client) )
{
// Auto reload
g_iWasShoot[client]--;
buttons |= IN_RELOAD;
}
return Plugin_Continue;
}
// Reserve ammo
int GetOrSetPlayerAmmo(int client, int iWeapon, int iAmmo = -1)
{
int offset = GetEntData(iWeapon, g_iPrimaryAmmoType) * 4; // Thanks to "Root" or whoever for this method of not hard-coding offsets: https://github.com/zadroot/AmmoManager/blob/master/scripting/ammo_manager.sp
if( offset )
{
if( iAmmo != -1 ) SetEntData(client, g_iOffsetAmmo + offset, iAmmo);
else return GetEntData(client, g_iOffsetAmmo + offset);
}
return 0;
}