-
Notifications
You must be signed in to change notification settings - Fork 1
/
NadeManager.cs
128 lines (106 loc) · 3.71 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
using CounterStrikeSharp.API.Core;
using Microsoft.Extensions.Logging;
using ChaseMod.Utils.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using ChaseMod.Utils;
using CounterStrikeSharp.API;
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 EnableHooks()
{
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 DisableHooks()
{
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;
}
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();
}
}