-
Notifications
You must be signed in to change notification settings - Fork 2
/
SharpGrapple.cs
331 lines (268 loc) · 12.8 KB
/
SharpGrapple.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
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
using System.Drawing;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
namespace SharpGrapple
{
public class PlayerGrappleInfo
{
public bool IsPlayerGrappling { get; set; }
public Vector? GrappleRaycast { get; set; }
public bool GrappleBeamSpawned { get; set; }
public CBeam? GrappleWire { get; set; }
}
[MinimumApiVersion(125)]
public partial class SharpGrapple : BasePlugin
{
public override string ModuleName => "SharpGrapple";
public override string ModuleVersion => "0.1";
public override string ModuleAuthor => "DEAFPS https://github.com/DEAFPS/";
private Dictionary<int, PlayerGrappleInfo> playerGrapples = new Dictionary<int, PlayerGrappleInfo>();
private Dictionary<int, CCSPlayerController> connectedPlayers = new Dictionary<int, CCSPlayerController>();
public void InitPlayer(CCSPlayerController player)
{
if (player.IsBot || !player.IsValid)
{
return;
}
else
{
connectedPlayers[player.Slot] = player;
Console.WriteLine($"Added player {player.PlayerName} with UserID {player.UserId} to connectedPlayers");
// Initialize PlayerGrappleInfo for the player
playerGrapples[player.Slot] = new PlayerGrappleInfo();
}
}
public override void Load(bool hotReload)
{
Console.WriteLine("[SharpGrapple] Loading...");
ConVar.Find("player_ping_token_cooldown")?.SetValue(0f);
if (hotReload) Utilities.GetPlayers().ForEach(InitPlayer);
RegisterEventHandler<EventPlayerConnectFull>((@event, info) =>
{
InitPlayer(@event.Userid);
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerDisconnect>((@event, info) =>
{
var player = @event.Userid;
if (player.IsBot || !player.IsValid)
{
return HookResult.Continue;
}
else
{
if (connectedPlayers.TryGetValue(player.Slot, out var connectedPlayer))
{
connectedPlayers.Remove(player.Slot);
Console.WriteLine($"Removed player {connectedPlayer.PlayerName} with UserID {connectedPlayer.UserId} from connectedPlayers");
}
playerGrapples.Remove(player.Slot);
return HookResult.Continue;
}
});
RegisterEventHandler<EventPlayerDeath>((@event, info) =>
{
DetachGrapple(@event.Userid);
return HookResult.Continue;
});
RegisterEventHandler<EventRoundEnd>((@event, info) =>
{
Utilities.GetPlayers().ForEach(DetachGrapple);
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerPing>((@event, info) =>
{
var player = @event.Userid;
if (player.IsBot || !player.IsValid)
{
return HookResult.Continue;
}
else
{
GrappleHandler(player, @event);
}
return HookResult.Continue;
});
RegisterListener<Listeners.OnTick>(() =>
{
foreach (var playerEntry in connectedPlayers)
{
var player = playerEntry.Value;
if (player == null || !player.IsValid || player.IsBot || !player.PawnIsAlive)
continue;
if (playerGrapples.TryGetValue(player.Slot, out var grappleInfo) && grappleInfo.IsPlayerGrappling)
{
if (player == null || player.PlayerPawn == null || player.PlayerPawn?.Value?.CBodyComponent == null || !player.IsValid || !player.PawnIsAlive)
continue;
Vector? playerPosition = player.PlayerPawn?.Value.CBodyComponent?.SceneNode?.AbsOrigin;
QAngle? viewAngles = player.PlayerPawn?.Value?.EyeAngles;
if (playerPosition == null || viewAngles == null)
continue;
Vector? grappleTarget = playerGrapples[player.Slot].GrappleRaycast;
if (grappleTarget == null)
{
Console.WriteLine($"Skipping player {player.PlayerName} due to null grappleTarget.");
continue;
}
if (playerGrapples[player.Slot].GrappleWire == null)
{
playerGrapples[player.Slot].GrappleWire = Utilities.CreateEntityByName<CBeam>("beam");
if (playerGrapples[player.Slot].GrappleWire == null)
{
Console.WriteLine($"Failed to create beam...");
return;
}
var grappleWire = playerGrapples[player.Slot]?.GrappleWire;
if (grappleWire != null)
{
grappleWire.Render = Color.LimeGreen;
grappleWire.Width = 1.5f;
grappleWire.EndPos.X = grappleTarget.X;
grappleWire.EndPos.Y = grappleTarget.Y;
grappleWire.EndPos.Z = grappleTarget.Z;
grappleWire.DispatchSpawn();
}
}
if (IsPlayerCloseToTarget(player, grappleTarget, playerPosition, 100))
{
DetachGrapple(player);
continue;
}
var angleDifference = CalculateAngleDifference(new Vector(viewAngles.X, viewAngles.Y, viewAngles.Z), grappleTarget - playerPosition);
if (angleDifference > 180.0f)
{
DetachGrapple(player);
Console.WriteLine($"Player {player.PlayerName} looked away from the grapple target.");
continue;
}
if (player == null || player.PlayerPawn == null || player.PlayerPawn.Value.CBodyComponent == null || !player.IsValid || !player.PawnIsAlive || grappleTarget == null || viewAngles == null)
{
Console.WriteLine($"Skipping player {player?.PlayerName} due to other nulls");
continue;
}
PullPlayer(player, grappleTarget, playerPosition, viewAngles);
if (IsPlayerCloseToTarget(player, grappleTarget, playerPosition, 100))
{
DetachGrapple(player);
Console.WriteLine($"Player {player.PlayerName} reached the grapple target");
}
}
}
});
Console.WriteLine("[SharpGrapple] Plugin Loaded");
}
public void GrappleHandler(CCSPlayerController? player, EventPlayerPing ping)
{
if (player == null) return;
if (!playerGrapples.ContainsKey(player.Slot))
playerGrapples[player.Slot] = new PlayerGrappleInfo();
DetachGrapple(player);
playerGrapples[player.Slot].IsPlayerGrappling = true;
playerGrapples[player.Slot].GrappleRaycast = new Vector(ping.X, ping.Y, ping.Z);
}
private void PullPlayer(CCSPlayerController player, Vector grappleTarget, Vector playerPosition, QAngle viewAngles)
{
if (!player.IsValid || !player.PawnIsAlive)
{
Console.WriteLine("Invalid or dead player.");
return;
}
if (player.PlayerPawn?.Value?.CBodyComponent?.SceneNode == null)
{
Console.WriteLine("SceneNode is null. Skipping pull.");
return;
}
var direction = grappleTarget - playerPosition;
var distance = direction.Length();
direction = new Vector(direction.X / distance, direction.Y / distance, direction.Z / distance); // Normalize manually
float grappleSpeed = 500.0f;
var buttons = player.Buttons;
float adjustmentFactor = 0.5f;
var forwardVector = CalculateForwardVector(new Vector(viewAngles.X, viewAngles.Y, viewAngles.Z));
var rightVector = CalculateRightVector(new Vector(viewAngles.X, viewAngles.Y, viewAngles.Z));
if ((buttons & PlayerButtons.Moveright) != 0)
{
direction += rightVector * adjustmentFactor;
}
else if ((buttons & PlayerButtons.Moveleft) != 0)
{
direction -= rightVector * adjustmentFactor;
}
direction = new Vector(direction.X / direction.Length(), direction.Y / direction.Length(), direction.Z / direction.Length());
var newVelocity = new Vector(
direction.X * grappleSpeed,
direction.Y * grappleSpeed,
direction.Z * grappleSpeed
);
if (player.PlayerPawn.Value.AbsVelocity != null)
{
player.PlayerPawn.Value.AbsVelocity.X = newVelocity.X;
player.PlayerPawn.Value.AbsVelocity.Y = newVelocity.Y;
player.PlayerPawn.Value.AbsVelocity.Z = newVelocity.Z;
}
else
{
Console.WriteLine("AbsVelocity is null.");
return;
}
var grappleWire = playerGrapples[player.Slot].GrappleWire;
if (grappleWire != null)
{
grappleWire.Teleport(playerPosition, new QAngle(0, 0, 0), new Vector(0, 0, 0));
}
else
{
Console.WriteLine("GrappleWire is null.");
}
}
private Vector CalculateForwardVector(Vector viewAngles)
{
return new Vector(0, 0, 0);
//float pitch = viewAngles.X * (float)Math.PI / 180.0f;
//float yaw = viewAngles.Y * (float)Math.PI / 180.0f;
//float x = (float)(Math.Cos(pitch) * Math.Cos(yaw));
//float y = (float)(Math.Cos(pitch) * Math.Sin(yaw));
//float z = (float)(-Math.Sin(pitch));
//return new Vector(x, y, z);
}
private Vector CalculateRightVector(Vector viewAngles)
{
float yaw = (viewAngles.Y - 90.0f) * (float)Math.PI / 180.0f;
float x = (float)Math.Cos(yaw);
float y = (float)Math.Sin(yaw);
float z = 0.0f;
return new Vector(x, y, z);
}
private bool IsPlayerCloseToTarget(CCSPlayerController player, Vector grappleTarget, Vector playerPosition, float thresholdDistance)
{
var direction = grappleTarget - playerPosition;
var distance = direction.Length();
return distance < thresholdDistance;
}
private void DetachGrapple(CCSPlayerController player)
{
if (playerGrapples.TryGetValue(player.Slot, out var grappleInfo))
{
grappleInfo.IsPlayerGrappling = false;
grappleInfo.GrappleRaycast = null;
if (grappleInfo.GrappleWire != null)
{
grappleInfo?.GrappleWire.Remove();
grappleInfo!.GrappleWire = null;
}
}
}
private float CalculateAngleDifference(Vector angles1, Vector angles2)
{
float pitchDiff = Math.Abs(angles1.X - angles2.X);
float yawDiff = Math.Abs(angles1.Y - angles2.Y);
pitchDiff = pitchDiff > 180.0f ? 360.0f - pitchDiff : pitchDiff;
yawDiff = yawDiff > 180.0f ? 360.0f - yawDiff : yawDiff;
return Math.Max(pitchDiff, yawDiff);
}
}
}