-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNadeManager.cs
163 lines (135 loc) · 4.78 KB
/
NadeManager.cs
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
using CounterStrikeSharp.API.Core;
using Microsoft.Extensions.Logging;
using ChaseMod.Utils.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using ChaseMod.Utils;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Utils;
using System.Drawing;
using CounterStrikeSharp.API.Modules.Timers;
namespace ChaseMod;
internal class NadeManager
{
private readonly ChaseMod _plugin;
private readonly PlayerFreezeManager _playerFreezeManager;
private readonly RoundStartFreezeTimeManager _roundStartFreezeTimeManager;
public NadeManager(
ChaseMod chaseMod, PlayerFreezeManager playerFreezeManager,
RoundStartFreezeTimeManager roundStartFreezeTimeManager)
{
_plugin = chaseMod;
_playerFreezeManager = playerFreezeManager;
_roundStartFreezeTimeManager = roundStartFreezeTimeManager;
}
public void OnLoad()
{
GrenadeFunctions.CSmokeGrenadeProjectile_CreateFunc.Hook(CSmokeGrenadeProjectile_CreateHook, HookMode.Post);
_plugin.RegisterEventHandler<EventPlayerBlind>((@event, info) =>
{
if (!ChaseModUtils.IsRealPlayer(@event.Attacker) || !ChaseModUtils.IsRealPlayer(@event.Userid))
{
return HookResult.Continue;
}
if (@event.Attacker.Team == @event.Userid.Team)
{
@event.Userid.PlayerPawn.Value!.BlindUntilTime = 0;
}
return HookResult.Continue;
});
}
public void OnUnload()
{
GrenadeFunctions.CSmokeGrenadeProjectile_CreateFunc.Unhook(CSmokeGrenadeProjectile_CreateHook, HookMode.Post);
}
private HookResult CSmokeGrenadeProjectile_CreateHook(DynamicHook hook)
{
ChaseMod.Logger.LogDebug("Freezenade thrown");
var smoke = hook.GetReturn<CSmokeGrenadeProjectile>();
smoke.NextThinkTick = -1;
Utilities.SetStateChanged(smoke, "CBaseEntity", "m_nNextThinkTick");
_plugin.AddTimer(_plugin.Config.StunThrowTime, () =>
{
Server.NextFrame(() =>
{
FreezeGrenadeExplode(smoke);
});
});
return HookResult.Continue;
}
private void FreezeGrenadeExplode(CSmokeGrenadeProjectile smoke)
{
ChaseMod.Logger.LogDebug("Freezenade explode");
if (!smoke.IsValid)
{
return;
}
if (_roundStartFreezeTimeManager.IsInFreezeTime())
{
smoke.Remove();
return;
}
var smokeProjectileOrigin = smoke.AbsOrigin;
if (smokeProjectileOrigin == null)
{
return;
}
var thrower = smoke.OwnerEntity;
if (!thrower.IsValid || thrower.Value == null)
{
return;
}
if (_plugin.Config.FreezeRingParticle.Enabled)
{
DoRingParticle(smokeProjectileOrigin);
}
var players = ChaseModUtils.GetAllRealPlayers();
foreach (var player in players)
{
var pawn = player.PlayerPawn.Value!;
if (pawn.LifeState != (byte)LifeState_t.LIFE_ALIVE)
{
continue;
}
if (!_plugin.Config.StunSameTeam && player.TeamNum == thrower.Value.TeamNum)
{
continue;
}
var playerOrigin = pawn.AbsOrigin;
if (playerOrigin == null)
{
ChaseMod.Logger.LogWarning("Freezenade: other pawn has null AbsOrigin");
continue;
}
var distance = playerOrigin.Distance(smokeProjectileOrigin);
ChaseMod.Logger.LogDebug($"Distance between FreezeNade and {player.PlayerName} = {distance}");
if (distance > _plugin.Config.StunFreezeRadius)
{
continue;
}
_playerFreezeManager.Freeze(player, _plugin.Config.StunFreezeTime, true, true, false);
}
smoke.Remove();
}
private void DoRingParticle(Vector position)
{
var particle = Utilities.CreateEntityByName<CParticleSystem>("info_particle_system");
if (particle == null)
{
ChaseMod.Logger.LogWarning("Particle system failed to spawn");
return;
}
particle.EffectName = _plugin.Config.FreezeRingParticle.VpcfFile;
particle.Teleport(position, QAngle.Zero, Vector.Zero);
particle.TintCP = 1;
particle.Tint = Color.FromArgb(255, 0, 64, 255);
particle.StartActive = true;
particle.DispatchSpawn();
_plugin.AddTimer(_plugin.Config.FreezeRingParticle.Lifetime, () => {
if (!particle.IsValid)
{
return;
}
particle.AcceptInput("DestroyImmediately");
}, TimerFlags.STOP_ON_MAPCHANGE);
}
}