forked from SilvDev/Various_Scripts_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl4d_unscope.sp
391 lines (318 loc) · 12.4 KB
/
l4d_unscope.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
389
390
391
/*
* Unscope Sniper On Shoot
* Copyright (C) 2022 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.10"
/*======================================================================================
Plugin Info:
* Name : [L4D & L4D2] Unscope Sniper On Shoot
* Author : SilverShot
* Descrp : Un-zooms Sniper scopes on each shot.
* Link : https://forums.alliedmods.net/showthread.php?t=322064
* Plugins : https://sourcemod.net/plugins.php?exact=exact&sortby=title&search=1&author=Silvers
========================================================================================
Change Log:
1.10 (11-Dec-2022)
- Changes to fix compile warnings on SourceMod 1.11.
1.9 (11-Oct-2021)
- Fixed Zoom and Unzoom logic being inverted.
1.8 (02-Jul-2021)
- Fixed randommly vocalizing. No more hackish workarounds, zooms and unzooms correctly. Thanks to "Iizuka07" for testing.
1.7 (01-Jul-2021)
- Fixed crashing by deleting entities in "SDKHook_SpawnPost" instead of "OnEntityCreated". Thanks to "Iizuka07" for reporting.
1.6 (29-Jun-2021)
- Fixed vocalizing when unscoping. Thanks to "Iizuka07" for reporting.
1.5 (26-Jun-2021)
- Fixed rescope feature adding zoom when changing weapon straight after. Thanks to "Iizuka07" for reporting.
1.4 (24-May-2021)
- Added cvar "l4d_unscope_rescope" to rescope bolt action snipers after shooting and reloading. Requested by "AI0702".
1.3 (23-May-2021)
- Fixed not unscoping while the players eyes are moving into scoped view. Thanks to "AI0702" for reporting.
- Removed gamedata dependency.
1.2a (24-Sep-2020)
- Compatibility update for L4D2's "The Last Stand" update.
- GameData .txt file updated.
1.2 (10-May-2020)
- Added better error log message when gamedata file is missing.
- Extra checks to prevent "IsAllowedGameMode" throwing errors.
1.1 (01-Apr-2020)
- Fixed "IsAllowedGameMode" from throwing errors when the "_tog" cvar was changed before MapStart.
1.0 (14-Mar-2020)
- Initial release.
======================================================================================*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#define CVAR_FLAGS FCVAR_NOTIFY
#define GAMEDATA "l4d_unscope"
ConVar g_hCvarAllow, g_hCvarModes, g_hCvarModesOff, g_hCvarModesTog, g_hCvarMPGameMode, g_hCvarRescope, g_hCvarTypes;
bool g_bCvarAllow, g_bMapStarted, g_bLeft4Dead2;
int g_iCvarRescope, g_iCvarTypes;
Handle g_hTimerRescope[MAXPLAYERS+1];
StringMap g_aWeaponIDs;
ArrayList g_hTypes;
// ====================================================================================================
// PLUGIN START / END
// ====================================================================================================
public Plugin myinfo =
{
name = "[L4D & L4D2] Unscope Sniper On Shoot",
author = "SilverShot",
description = "Un-zooms Sniper scopes on each shot.",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=322064"
}
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_unscope_allow", "1", "0=Plugin off, 1=Plugin on.", CVAR_FLAGS );
g_hCvarModes = CreateConVar( "l4d_unscope_modes", "", "Turn on the plugin in these game modes, separate by commas (no spaces). (Empty = all).", CVAR_FLAGS );
g_hCvarModesOff = CreateConVar( "l4d_unscope_modes_off", "", "Turn off the plugin in these game modes, separate by commas (no spaces). (Empty = none).", CVAR_FLAGS );
g_hCvarModesTog = CreateConVar( "l4d_unscope_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_hCvarRescope = CreateConVar( "l4d_unscope_rescope", "1", "0=Off. 1=Rescope the view after shooting and reloading bolt action rifles (AWP and Scout).", CVAR_FLAGS );
g_hCvarTypes = CreateConVar( "l4d_unscope_types", g_bLeft4Dead2 ? "15" : "1", "1=Hunting Rifle. L4D2 only: 2=Sniper Military, 4=Sniper AWP, 8=Sniper Scout. 15=All. Add numbers together.", CVAR_FLAGS );
CreateConVar( "l4d_unscope_version", PLUGIN_VERSION, "Unscope Sniper plugin version.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "l4d_unscope");
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_hCvarRescope.AddChangeHook(ConVarChanged_Cvars);
g_hCvarTypes.AddChangeHook(ConVarChanged_Cvars);
g_aWeaponIDs = new StringMap();
g_hTypes = new ArrayList();
g_aWeaponIDs.SetValue("weapon_hunting_rifle", 6);
g_aWeaponIDs.SetValue("weapon_sniper_awp", 35);
g_aWeaponIDs.SetValue("weapon_sniper_scout", 36);
}
// ====================================================================================================
// CVARS
// ====================================================================================================
public void OnMapStart()
{
g_bMapStarted = true;
}
public void OnMapEnd()
{
g_bMapStarted = false;
}
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_iCvarRescope = g_hCvarRescope.IntValue;
g_iCvarTypes = g_hCvarTypes.IntValue;
g_hTypes.Clear();
if( g_iCvarTypes & (1<<0) ) g_hTypes.Push(6); // weapon_hunting_rifle
if( g_bLeft4Dead2 )
{
if( g_iCvarTypes & (1<<1) ) g_hTypes.Push(10); // weapon_sniper_military
if( g_iCvarTypes & (1<<2) ) g_hTypes.Push(35); // weapon_sniper_awp
if( g_iCvarTypes & (1<<3) ) g_hTypes.Push(36); // weapon_sniper_scout
}
}
void IsAllowed()
{
bool bCvarAllow = g_hCvarAllow.BoolValue;
bool bAllowMode = IsAllowedGameMode();
GetCvars();
if( g_bCvarAllow == false && bCvarAllow == true && bAllowMode == true )
{
g_bCvarAllow = true;
HookEvent("weapon_fire", Event_WeaponFire);
HookEvent("weapon_zoom", Event_WeaponZoom);
}
else if( g_bCvarAllow == true && (bCvarAllow == false || bAllowMode == false) )
{
g_bCvarAllow = false;
UnhookEvent("weapon_fire", Event_WeaponFire);
UnhookEvent("weapon_zoom", Event_WeaponZoom);
}
}
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
// ====================================================================================================
void Event_WeaponFire(Event event, const char[] name, bool dontBroadcast)
{
int weaponID = event.GetInt("weaponid");
if( g_hTypes.FindValue(weaponID) != -1 )
{
int client = GetClientOfUserId(event.GetInt("userid"));
// Unscope
if( GetEntPropEnt(client, Prop_Send, "m_hZoomOwner") != -1 )
{
UnzoomWeapon(client);
// Rescope
if( g_iCvarRescope && (weaponID == 35 || weaponID == 36) )
{
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( GetEntProp(weapon, Prop_Send, "m_iClip1") > 1 )
{
delete g_hTimerRescope[client];
g_hTimerRescope[client] = CreateTimer(1.1, TimerRescope, GetClientUserId(client));
}
}
} else {
// Prevent rescoping if firing before scoping timer finishes
delete g_hTimerRescope[client];
}
}
}
// Prevent scoping while reloading
void Event_WeaponZoom(Event event, const char[] name, bool dontBroadcast)
{
if( g_iCvarRescope )
{
int client = GetClientOfUserId(event.GetInt("userid"));
if( g_hTimerRescope[client] != null )
{
if( GetEntPropEnt(client, Prop_Send, "m_hZoomOwner") != -1 )
{
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon != -1 )
{
// Verify sniper?
static char classname[24];
GetEdictClassname(weapon, classname, sizeof(classname));
if( g_aWeaponIDs.ContainsKey(classname) )
{
UnzoomWeapon(client);
}
}
}
}
}
}
public void OnClientDisconnect(int client)
{
g_hTimerRescope[client] = null;
}
Action TimerRescope(Handle timer, any client)
{
client = GetClientOfUserId(client);
if( client && IsClientInGame(client) )
{
g_hTimerRescope[client] = null;
// Only scope if on ground
int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if( weapon != -1 && GetPlayerWeaponSlot(client, 0) == weapon && GetEntityFlags(client) & FL_ONGROUND )
{
ZoomWeapon(client);
}
}
return Plugin_Continue;
}
void UnzoomWeapon(int client)
{
SetEntPropEnt(client, Prop_Send, "m_hZoomOwner", -1);
SetEntPropFloat(client, Prop_Send, "m_flFOVTime", 0.0);
SetEntPropFloat(client, Prop_Send, "m_flFOVRate", 0.0);
SetEntProp(client, Prop_Send, "m_iFOV", 0);
}
void ZoomWeapon(int client)
{
SetEntPropEnt(client, Prop_Send, "m_hZoomOwner", client);
SetEntPropFloat(client, Prop_Send, "m_flFOVTime", GetGameTime());
SetEntPropFloat(client, Prop_Send, "m_flFOVRate", 0.3000);
SetEntProp(client, Prop_Send, "m_iFOV", 40);
}