forked from SilvDev/Various_Scripts_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4d2_reload_fix.sp
388 lines (317 loc) · 11.6 KB
/
l4d2_reload_fix.sp
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
/*
* No Reload Animation Fix - Picking Up Same Weapon
* Copyright (C) 2023 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.6"
/*======================================================================================
Plugin Info:
* Name : [L4D2] No Reload Animation Fix
* Author : SilverShot & HarryPotter
* Descrp : Prevent filling the clip and skipping the reload animation when taking the same weapon.
* Link : https://forums.alliedmods.net/showthread.php?t=333100
* Plugins : https://sourcemod.net/plugins.php?exact=exact&sortby=title&search=1&author=Silvers
========================================================================================
Change Log:
1.6 (25-May-2023)
- Added cvar "l4d2_reload_fix_give" to optionally prevent "give" command with same type of weapon setting to the previous weapons ammo.
1.5 (20-Aug-2022)
- Records all weapons clip and ammo. Thanks to "HarryPotter" for writing.
1.4 (29-Mar-2022)
- Fixed not always detecting the correct current weapon. Thanks to "Forgetest" for fixing.
1.3 (02-Nov-2021)
- Fixed treating different weapon skins as the same weapon. Thanks to "tRololo312312" for reporting.
1.2 (06-Jul-2021)
- Fixed throwing errors about invalid entity. Thanks to "HarryPotter" for reporting.
1.1 (27-Jun-2021)
- Fixed throwing errors about invalid entity. Thanks to "HarryPotter" for reporting.
1.0 (19-Jun-2021)
- Initial release.
======================================================================================*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#define CVAR_FLAGS FCVAR_NOTIFY
#define MAX_SKIN 4
enum WeaponID
{
ID_NONE,
//ID_PISTOL,
//ID_DUAL_PISTOL,
ID_SMG,
ID_PUMPSHOTGUN,
ID_RIFLE,
ID_AUTOSHOTGUN,
ID_HUNTING_RIFLE,
ID_SMG_SILENCED,
ID_SMG_MP5,
ID_CHROMESHOTGUN,
//ID_MAGNUM,
ID_AK47,
ID_RIFLE_DESERT,
ID_SNIPER_MILITARY,
ID_GRENADE,
ID_SG552,
ID_M60,
ID_AWP,
ID_SCOUT,
ID_SPASSHOTGUN,
ID_WEAPON_MAX
}
int g_iClipAmmo[MAXPLAYERS + 1][view_as<int>(ID_WEAPON_MAX)][MAX_SKIN];
bool g_bIgnored[MAXPLAYERS + 1];
StringMap g_hWeaponName;
int g_iOffsetAmmo;
int g_iPrimaryAmmoType;
bool g_bLateLoad;
ConVar g_hCvarGive;
// ====================================================================================================
// PLUGIN INFO / START / END
// ====================================================================================================
public Plugin myinfo =
{
name = "[L4D2] No Reload Animation Fix",
author = "SilverShot, HarryPotter",
description = "Prevent filling the clip and skipping the reload animation when taking the same weapon.",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=333100"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
EngineVersion test = GetEngineVersion();
if( test != Engine_Left4Dead2 )
{
strcopy(error, err_max, "Plugin only supports Left 4 Dead 2.");
return APLRes_SilentFailure;
}
g_bLateLoad = late;
RegPluginLibrary("l4d2_reload_fix");
return APLRes_Success;
}
public void OnPluginStart()
{
// Lateload
if( g_bLateLoad )
{
for( int i = 1; i <= MaxClients; i++ )
{
ClearClientAmmo(i);
if( IsClientInGame(i) )
{
SDKHook(i, SDKHook_WeaponCanUsePost, WeaponCanUse);
}
}
}
// Offsets to setting reserve ammo
g_iOffsetAmmo = FindSendPropInfo("CTerrorPlayer", "m_iAmmo");
g_iPrimaryAmmoType = FindSendPropInfo("CBaseCombatWeapon", "m_iPrimaryAmmoType");
g_hCvarGive = CreateConVar("l4d2_reload_fix_give", "1", "When using the give command and replacing the same weapon type, transfer ammo to the new weapon. 0=No. 1=Yes.", CVAR_FLAGS);
CreateConVar("l4d2_reload_fix_version", PLUGIN_VERSION, "No Reload Animation Fix plugin version.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "l4d2_reload_fix");
g_hCvarGive.AddChangeHook(ConVarChanged_Cvars);
SetWeaponClassName();
HookEvent("player_spawn", Event_PlayerSpawn);
HookEvent("round_start", Event_RoundStart);
HookEvent("weapon_drop", Event_Weapon_Drop);
}
public void OnConfigsExecuted()
{
GetCvars();
}
void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
{
GetCvars();
}
void GetCvars()
{
static bool hooked;
bool setting = g_hCvarGive.BoolValue;
if( hooked && setting )
{
RemoveCommandListener(CommandListener, "give");
hooked = false;
}
else if( !hooked && !setting )
{
AddCommandListener(CommandListener, "give");
hooked = true;
}
}
Action CommandListener(int client, const char[] command, int args)
{
g_bIgnored[client] = true;
RequestFrame(OnFrameIgnore, client); // Don't need userid for this
return Plugin_Continue;
}
void OnFrameIgnore(int client)
{
g_bIgnored[client] = false;
}
public void OnClientPutInServer(int client)
{
g_bIgnored[client] = false;
SDKHook(client, SDKHook_WeaponCanUsePost, WeaponCanUse);
}
// Fix picking up weapons filling the clip
void WeaponCanUse(int client, int weapon)
{
if( weapon == -1 ) return;
if( g_bIgnored[client] ) return;
// Validate team
if( GetClientTeam(client) == 2 )
{
// Validate weapon
int current = GetPlayerWeaponSlot(client, 0);
if( current != -1 )
{
static char sCurrent_ClassName[32];
GetEntityClassname(current, sCurrent_ClassName, sizeof(sCurrent_ClassName));
WeaponID current_weaponid = ID_NONE;
if( !g_hWeaponName.GetValue(sCurrent_ClassName, current_weaponid) ) return;
int current_skin = GetEntProp(current, Prop_Send, "m_nSkin");
// Store clip size
g_iClipAmmo[client][current_weaponid][current_skin] = GetEntProp(current, Prop_Send, "m_iClip1");
// PrintToChatAll("%N WeaponCanUse Old Weapon %s (skin:%d), clip: %d", client, sCurrent_ClassName, current_skin, GetEntProp(current, Prop_Send, "m_iClip1"));
}
static char sWeapon_ClassName[32];
GetEntityClassname(weapon, sWeapon_ClassName, sizeof(sWeapon_ClassName));
WeaponID weapon_weaponid = ID_NONE;
if( !g_hWeaponName.GetValue(sWeapon_ClassName, weapon_weaponid) ) return;
// Modify on next frame so we get new weapons reserve ammo
DataPack dPack = new DataPack();
dPack.WriteCell(GetClientUserId(client));
dPack.WriteCell(EntIndexToEntRef(weapon));
dPack.WriteCell(weapon_weaponid);
RequestFrame(OnFrame, dPack);
}
}
void OnFrame(DataPack dPack)
{
dPack.Reset();
int client = dPack.ReadCell();
client = GetClientOfUserId(client);
if( !client || !IsClientInGame(client))
{
delete dPack;
return;
}
int weapon = dPack.ReadCell();
weapon = EntRefToEntIndex(weapon);
if( weapon == INVALID_ENT_REFERENCE )
{
delete dPack;
return;
}
WeaponID weapon_weaponid = dPack.ReadCell();
int weapon_skin = GetEntProp(weapon, Prop_Send, "m_nSkin"); // skin available on this frame
// static char sWeapon_ClassName[32];
// GetEntityClassname(weapon, sWeapon_ClassName, sizeof(sWeapon_ClassName));
// PrintToChatAll("%N WeaponCanUse New Weapon %s (skin:%d)", client, sWeapon_ClassName, weapon_skin);
if( g_iClipAmmo[client][weapon_weaponid][weapon_skin] == -1 )
{
delete dPack;
return;
}
delete dPack;
// Validate weapon
if( weapon == GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon") )
{
int clip = GetEntProp(weapon, Prop_Send, "m_iClip1");
// Restore clip size to previous
SetEntProp(weapon, Prop_Send, "m_iClip1", g_iClipAmmo[client][weapon_weaponid][weapon_skin]);
// Add new ammo received to reserve ammo
int ammo = GetOrSetPlayerAmmo(client, weapon);
GetOrSetPlayerAmmo(client, weapon, ammo + clip - g_iClipAmmo[client][weapon_weaponid][weapon_skin]);
}
}
// Reset arrays
void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( !client || !IsClientInGame(client) ) return;
ClearClientAmmo(client);
}
void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
for(int client = 1; client <= MaxClients; client++)
{
ClearClientAmmo(client);
}
}
// Save ammo when dropped
void Event_Weapon_Drop(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( !client || !IsClientInGame(client) ) return;
int weapon = event.GetInt("propid");
if( weapon <= MaxClients || !IsValidEntity(weapon) ) return;
static char sWeapon_ClassName[32];
GetEntityClassname(weapon, sWeapon_ClassName, sizeof(sWeapon_ClassName));
WeaponID weapon_weaponid = ID_NONE;
if( !g_hWeaponName.GetValue(sWeapon_ClassName, weapon_weaponid) ) return;
int weapon_skin = GetEntProp(weapon, Prop_Send, "m_nSkin");
g_iClipAmmo[client][weapon_weaponid][weapon_skin] = GetEntProp(weapon, Prop_Send, "m_iClip1");
// PrintToChatAll("%N Drop weapon %s (skin:%d), clip: %d", client, sWeapon_ClassName, weapon_skin, GetEntProp(weapon, Prop_Send, "m_iClip1"));
}
// 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;
}
// Weapon ID's
void SetWeaponClassName()
{
g_hWeaponName = new StringMap();
g_hWeaponName.SetValue("", ID_NONE);
//g_hWeaponName.SetValue("weapon_pistol", ID_PISTOL);
//g_hWeaponName.SetValue("weapon_pistol", ID_DUAL_PISTOL);
g_hWeaponName.SetValue("weapon_smg", ID_SMG);
g_hWeaponName.SetValue("weapon_pumpshotgun", ID_PUMPSHOTGUN);
g_hWeaponName.SetValue("weapon_rifle", ID_RIFLE);
g_hWeaponName.SetValue("weapon_autoshotgun", ID_AUTOSHOTGUN);
g_hWeaponName.SetValue("weapon_hunting_rifle", ID_HUNTING_RIFLE);
g_hWeaponName.SetValue("weapon_smg_silenced", ID_SMG_SILENCED);
g_hWeaponName.SetValue("weapon_smg_mp5", ID_SMG_MP5);
g_hWeaponName.SetValue("weapon_shotgun_chrome", ID_CHROMESHOTGUN);
//g_hWeaponName.SetValue("weapon_pistol_magnum", ID_MAGNUM);
g_hWeaponName.SetValue("weapon_rifle_ak47", ID_AK47);
g_hWeaponName.SetValue("weapon_rifle_desert", ID_RIFLE_DESERT);
g_hWeaponName.SetValue("weapon_sniper_military", ID_SNIPER_MILITARY);
g_hWeaponName.SetValue("weapon_grenade_launcher", ID_GRENADE);
g_hWeaponName.SetValue("weapon_rifle_sg552", ID_SG552);
g_hWeaponName.SetValue("weapon_rifle_m60", ID_M60);
g_hWeaponName.SetValue("weapon_sniper_awp", ID_AWP);
g_hWeaponName.SetValue("weapon_sniper_scout", ID_SCOUT);
g_hWeaponName.SetValue("weapon_shotgun_spas", ID_SPASSHOTGUN);
}
// Reset array
void ClearClientAmmo(int client)
{
for( WeaponID weapon = ID_NONE; weapon < ID_WEAPON_MAX ; ++weapon )
{
for( int skin = 0; skin < MAX_SKIN; skin++ )
{
g_iClipAmmo[client][weapon][skin] = -1;
}
}
}