-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnBeam.cs
More file actions
285 lines (232 loc) · 9.71 KB
/
SpawnBeam.cs
File metadata and controls
285 lines (232 loc) · 9.71 KB
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
using System.Drawing;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Timers;
using CS2TraceRay.Class;
using CS2TraceRay.Enum;
using CS2TraceRay.Struct;
using SpawnBeam.Extensions;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
namespace SpawnBeam;
[MinimumApiVersion(160)]
public class SpawnBeam : BasePlugin
{
public override string ModuleName => "SpawnBeam";
public override string ModuleDescription => "A plugin that displays visual square outlines around spawn points for both teams.";
public override string ModuleAuthor => "CVHNups";
public override string ModuleVersion => "1.0.3";
private List<SpawnPoint> tSpawns = new List<SpawnPoint>();
private List<SpawnPoint> ctSpawns = new List<SpawnPoint>();
private Dictionary<int, bool> isPlayerTP = new Dictionary<int, bool>();
private Dictionary<int, bool> isInSquare = new Dictionary<int, bool>();
private CounterStrikeSharp.API.Modules.Timers.Timer? tpTimer;
float squareRadius = 60f;
float squareWidth = 0.5f;
// TODO
bool isBeamDisable = false;
public override void Load(bool hotReload)
{
Console.WriteLine($"{ModuleName} loaded successfully!");
RegisterEventHandler<EventRoundPoststart>(OnRoundStarted);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
AddCommand("css_tp", "Toggle auto teleport to spawn", Command_Tp);
AddCommand("css_random", "Teleport to random spawn point", Command_TpRandom);
AddCommandListener("noclip", OnNoclipCommand);
tpTimer = AddTimer(0.5f, CheckPlayerPositions, TimerFlags.REPEAT);
}
public override void Unload(bool hotReload)
{
tpTimer?.Kill();
}
private void CheckPlayerPositions()
{
var players = Utilities.GetPlayers().Where(p =>
p.IsValid &&
p.PlayerPawn.Value != null &&
isPlayerTP.ContainsKey((int)p.Index) &&
isPlayerTP[(int)p.Index]);
foreach (var player in players)
{
bool isNoClip = player.PlayerPawn.Value.MoveType == MoveType_t.MOVETYPE_NOCLIP;
if (isNoClip)
{
continue;
}
int index = (int)player.Index;
var playerPos = player.PlayerPawn.Value.AbsOrigin;
var nearbySpawn = FindNearbySpawn(playerPos);
bool currentlyInSquare = nearbySpawn != null;
bool wasInSquare = isInSquare.ContainsKey(index) && isInSquare[index];
if (currentlyInSquare && !wasInSquare)
{
Vector tpPos = new Vector(nearbySpawn.AbsOrigin.X, nearbySpawn.AbsOrigin.Y, playerPos.Z);
if (player.PlayerPawn.Value.AbsOrigin.X != tpPos.X && player.PlayerPawn.Value.AbsOrigin.Y != tpPos.Y)
{
player.PlayerPawn.Value.Teleport(tpPos, null, new Vector(0, 0, 0));
player.PrintToChat($" [Spawn{ChatColors.Lime}Beam{ChatColors.Default}] Teleported to spawn point!");
}
isInSquare[index] = currentlyInSquare;
}
else if (!currentlyInSquare && wasInSquare)
{
isInSquare.Remove(index);
}
}
}
private SpawnPoint? FindNearbySpawn(Vector playerPos)
{
foreach (var spawn in tSpawns.Concat(ctSpawns))
{
if (IsInsideSquare(playerPos, spawn.AbsOrigin, squareRadius))
return spawn;
}
return null;
}
private bool IsInsideSquare(Vector point, Vector center, float radius)
{
float halfRadius = radius / 2;
return point.X >= center.X - halfRadius &&
point.X <= center.X + halfRadius &&
point.Y >= center.Y - halfRadius &&
point.Y <= center.Y + halfRadius;
}
// now it should works, thanks: https://github.com/schwarper/CS2TraceRay/blob/main/CS2TraceRay/Class/PlayerExtensions.cs#L85
private float GetGroundLevel(Vector spawnOrigin)
{
if (spawnOrigin == null)
return 0.0f;
CGameTrace trace = TraceRay.TraceShape(spawnOrigin, new QAngle(90, 0, 0), TraceMask.MaskAll, Contents.Sky, 0);
if (trace.EndPos != null)
{
return trace.EndPos.Z;
}
return spawnOrigin.Z;
}
// thanks https://github.com/exkludera/cs2-noclip/blob/main/noclip.cs
private HookResult OnNoclipCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || !player.PawnIsAlive)
return HookResult.Continue;
if (player.Team == CsTeam.Spectator || player.Team == CsTeam.None)
return HookResult.Continue;
int index = (int)player.Index;
if (player.PlayerPawn.Value.MoveType == MoveType_t.MOVETYPE_NOCLIP)
{
player.PlayerPawn.Value.MoveType = MoveType_t.MOVETYPE_WALK;
Schema.SetSchemaValue(player.PlayerPawn.Value.Handle, "CBaseEntity", "m_nActualMoveType", 2);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseEntity", "m_MoveType");
if (isInSquare.ContainsKey(index))
{
isInSquare.Remove(index);
}
}
else
{
player.PlayerPawn.Value.MoveType = MoveType_t.MOVETYPE_NOCLIP;
Schema.SetSchemaValue(player.PlayerPawn.Value.Handle, "CBaseEntity", "m_nActualMoveType", 8);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CBaseEntity", "m_MoveType");
if (isInSquare.ContainsKey(index))
{
isInSquare.Remove(index);
}
}
return HookResult.Handled;
}
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
private void Command_Tp(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid)
return;
int index = (int)player.Index;
if (isPlayerTP.ContainsKey(index))
{
isPlayerTP[index] = !isPlayerTP[index];
}
else
{
isPlayerTP[index] = true;
}
string status = isPlayerTP[index] ? $"{ChatColors.Lime}ON" : $"{ChatColors.Orange}OFF";
info.ReplyToCommand($"[Spawn{ChatColors.Lime}Beam{ChatColors.Default}] Auto teleport: {status}");
}
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
private void Command_TpRandom(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid || player.PlayerPawn.Value == null)
return;
List<SpawnPoint> spawns;
string teamName;
if (player.Team == CsTeam.Terrorist)
{
spawns = tSpawns;
teamName = $"{ChatColors.Yellow}Terrorist{ChatColors.Default}";
}
else if (player.Team == CsTeam.CounterTerrorist)
{
spawns = ctSpawns;
teamName = $"{ChatColors.Blue}Counter Terrorist{ChatColors.Default}";
}
else
{
info.ReplyToCommand($"[Spawn{ChatColors.Lime}Beam{ChatColors.Default}] You must be on a team to use this command!");
return;
}
if (spawns.Count == 0)
{
info.ReplyToCommand($"[Spawn{ChatColors.Lime}Beam{ChatColors.Default}] No spawn points found for your team!");
return;
}
var randomSpawn = spawns[Random.Shared.Next(spawns.Count)];
player.PlayerPawn.Value.Teleport(randomSpawn.AbsOrigin, randomSpawn.CBodyComponent?.SceneNode?.AbsRotation!, new Vector(0, 0, 0));
info.ReplyToCommand($"[Spawn{ChatColors.Lime}Beam{ChatColors.Default}] Teleported to random {teamName} spawn point!");
}
[GameEventHandler]
private HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
var player = @event.Userid;
if (player != null && player.IsValid)
{
int index = (int)player.Index;
isPlayerTP.Remove(index);
isInSquare.Remove(index);
}
return HookResult.Continue;
}
[GameEventHandler]
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
var player = @event.Userid;
player.ExecuteClientCommandFromServer("buddha");
player.ExecuteClientCommandFromServer("buddha_ignore_bots 1");
return HookResult.Continue;
}
[GameEventHandler]
private HookResult OnRoundStarted(EventRoundPoststart @event, GameEventInfo info)
{
tSpawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_terrorist").ToList();
ctSpawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_counterterrorist").ToList();
var spawnConfigs = new[]
{
(tSpawns, Color.Yellow),
(ctSpawns, Color.Cyan)
};
foreach (var (spawns, color) in spawnConfigs)
{
foreach (var spawn in spawns)
{
Vector centerPoint = spawn.AbsOrigin;
float groundZ = GetGroundLevel(centerPoint);
Vector startPos = new Vector(centerPoint.X + squareRadius / 2, centerPoint.Y - squareRadius / 2, groundZ + 20f);
Vector endPos = new Vector(centerPoint.X - squareRadius / 2, centerPoint.Y + squareRadius / 2, groundZ + 20f);
BeamHelper.SquareBeam(startPos, endPos, squareRadius, squareWidth, color);
}
}
return HookResult.Continue;
}
}