-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Coach.cs
287 lines (245 loc) · 11.8 KB
/
Coach.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Cvars;
using System.Text.Json;
namespace MatchZy;
public partial class MatchZy
{
public CounterStrikeSharp.API.Modules.Timers.Timer? coachKillTimer = null;
public HashSet<CCSPlayerController> GetAllCoaches()
{
HashSet<CCSPlayerController> coaches = new(matchzyTeam1.coach);
coaches.UnionWith(matchzyTeam2.coach);
return coaches;
}
public void HandleCoachCommand(CCSPlayerController? player, string side)
{
if (!IsPlayerValid(player)) return;
if (isPractice)
{
ReplyToUserCommand(player, "Coach command can only be used in match mode!");
return;
}
if (IsWingmanMode())
{
ReplyToUserCommand(player, "Coach command cannot be used in wingman!");
return;
}
side = side.Trim().ToLower();
if (side != "t" && side != "ct")
{
ReplyToUserCommand(player, "Usage: .coach t or .coach ct");
return;
}
if (matchzyTeam1.coach.Contains(player!) || matchzyTeam2.coach.Contains(player!))
{
ReplyToUserCommand(player, "You are already coaching a team!");
return;
}
Team matchZyCoachTeam;
if (side == "t")
{
matchZyCoachTeam = reverseTeamSides["TERRORIST"];
}
else if (side == "ct")
{
matchZyCoachTeam = reverseTeamSides["CT"];
}
else
{
return;
}
// if (matchZyCoachTeam.coach != null) {
// ReplyToUserCommand(player, "Coach slot for this team has been already taken!");
// return;
// }
matchZyCoachTeam.coach.Add(player!);
player!.Clan = $"[{matchZyCoachTeam.teamName} COACH]";
if (player.InGameMoneyServices != null) player.InGameMoneyServices.Account = 0;
ReplyToUserCommand(player, $"You are now coaching {matchZyCoachTeam.teamName}! Use .uncoach to stop coaching");
PrintToAllChat($"{ChatColors.Green}{player.PlayerName}{ChatColors.Default} is now coaching {ChatColors.Green}{matchZyCoachTeam.teamName}{ChatColors.Default}!");
}
public void HandleCoaches()
{
coachKillTimer?.Kill();
coachKillTimer = null;
HashSet<CCSPlayerController> coaches = GetAllCoaches();
if (IsWingmanMode() || coaches.Count == 0) return;
if (spawnsData.Values.Any(list => list.Count == 0)) GetSpawns();
if (coachSpawns.Count == 0 ||
coachSpawns[(byte)CsTeam.CounterTerrorist].Count == 0 ||
coachSpawns[(byte)CsTeam.Terrorist].Count == 0)
{
Log($"[HandleCoaches] No coach spawns found, player positions will not be swapped!");
return;
}
int freezeTime = ConVar.Find("mp_freezetime")!.GetPrimitiveValue<int>();
freezeTime = freezeTime > 2 ? freezeTime: 2;
coachKillTimer ??= AddTimer(freezeTime - 1f, KillCoaches);
Random random = new();
foreach (CCSPlayerController coach in coaches)
{
if (!IsPlayerValid(coach)) continue;
Team coachTeam = matchzyTeam1.coach.Contains(coach) ? matchzyTeam1 : matchzyTeam2;
int coachTeamNum = teamSides[coachTeam] == "CT" ? 3 : 2;
coach.InGameMoneyServices!.Account = 0;
AddTimer(0.5f, () => HandleCoachTeam(coach));
coach.ActionTrackingServices!.MatchStats.Kills = 0;
coach.ActionTrackingServices!.MatchStats.Deaths = 0;
coach.ActionTrackingServices!.MatchStats.Assists = 0;
coach.ActionTrackingServices!.MatchStats.Damage = 0;
SetPlayerInvisible(player: coach, setWeaponsInvisible: false);
// Stopping the coaches from moving, so that they don't block the players.
coach.PlayerPawn.Value!.MoveType = MoveType_t.MOVETYPE_NONE;
coach.PlayerPawn.Value!.ActualMoveType = MoveType_t.MOVETYPE_NONE;
List<Position> coachTeamSpawns = coachSpawns[coach.TeamNum];
Position coachPosition = new(coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
// Picking a random position for the coach (from coachSpawns) to teleport them.
Position newPosition = coachTeamSpawns[random.Next(0, coachTeamSpawns.Count)];
// Elevating coach before dropping the C4 to prevent it going inside the ground.
AddTimer(0.05f, () =>
{
coach!.PlayerPawn.Value!.Teleport(new Vector(coachPosition.PlayerPosition.X, coachPosition.PlayerPosition.Y, coachPosition.PlayerPosition.Z + 20.0f), coachPosition.PlayerAngle, new Vector(0, 0, 0));
HandleCoachWeapons(coach);
coach!.PlayerPawn.Value.Teleport(newPosition.PlayerPosition, newPosition.PlayerAngle, new Vector(0, 0, 0));
});
}
List<CCSPlayerController> players = Utilities.GetPlayers();
HashSet<Position> occupiedSpawns = new();
HashSet<CCSPlayerController> incorrectSpawnedPlayers = new();
// We will loop on the players 2 times, first loop is to get all the players who are on a non-competitive spawn, and to get all the non-occupied competitive spawn.
// In the next loop, we will teleport the non-competitive spawned players to an available competitive spawn.
foreach (CCSPlayerController player in players)
{
if (!IsPlayerValid(player) || coaches.Contains(player)) continue;
List<Position> teamPositions = spawnsData[player.TeamNum];
Position playerPosition = new(player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
bool isCompetitiveSpawn = false;
foreach (Position position in teamPositions)
{
if (position.Equals(playerPosition))
{
occupiedSpawns.Add(position);
isCompetitiveSpawn = true;
break;
}
}
if (isCompetitiveSpawn) continue;
// The player is not on a competitive spawn, we will put them on one in the next loop.
incorrectSpawnedPlayers.Add(player);
}
foreach (CCSPlayerController player in incorrectSpawnedPlayers)
{
if (!IsPlayerValid(player) || coaches.Contains(player)) continue;
List<Position> teamPositions = spawnsData[player.TeamNum];
Position playerPosition = new(player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
foreach (Position position in teamPositions)
{
if (occupiedSpawns.Contains(position)) continue;
occupiedSpawns.Add(position);
AddTimer(0.1f, () =>
{
player!.PlayerPawn.Value.Teleport(position.PlayerPosition, position.PlayerAngle, new Vector(0, 0, 0));
});
break;
}
}
}
private void HandleCoachWeapons(CCSPlayerController coach)
{
if (!IsPlayerValid(coach)) return;
DropWeaponByDesignerName(coach, "weapon_c4");
coach.RemoveWeapons();
}
public CsTeam GetCoachTeam(CCSPlayerController coach)
{
if (matchzyTeam1.coach.Contains(coach))
{
if (teamSides[matchzyTeam1] == "CT")
{
return CsTeam.CounterTerrorist;
}
else if (teamSides[matchzyTeam1] == "TERRORIST")
{
return CsTeam.Terrorist;
}
}
if (matchzyTeam2.coach.Contains(coach))
{
if (teamSides[matchzyTeam2] == "CT")
{
return CsTeam.CounterTerrorist;
}
else if (teamSides[matchzyTeam2] == "TERRORIST")
{
return CsTeam.Terrorist;
}
}
return CsTeam.Spectator;
}
private void HandleCoachTeam(CCSPlayerController playerController)
{
CsTeam oldTeam = GetCoachTeam(playerController);
if (playerController.Team != oldTeam)
{
playerController.ChangeTeam(CsTeam.Spectator);
AddTimer(0.01f, () => playerController.ChangeTeam(oldTeam));
}
if (playerController.InGameMoneyServices != null) playerController.InGameMoneyServices.Account = 0;
}
private void KillCoaches()
{
if (isPaused || IsTacticalTimeoutActive()) return;
HashSet<CCSPlayerController> coaches = GetAllCoaches();
if (IsWingmanMode() || coaches.Count == 0) return;
string suicidePenalty = GetConvarStringValue(ConVar.Find("mp_suicide_penalty"));
string specFreezeTime = GetConvarStringValue(ConVar.Find("spec_freeze_time"));
string specFreezeTimeLock = GetConvarStringValue(ConVar.Find("spec_freeze_time_lock"));
string specFreezeDeathanim = GetConvarStringValue(ConVar.Find("spec_freeze_deathanim_time"));
Server.ExecuteCommand("mp_suicide_penalty 0;spec_freeze_time 0; spec_freeze_time_lock 0; spec_freeze_deathanim_time 0;");
foreach (var coach in coaches)
{
if (!IsPlayerValid(coach)) continue;
if (isPaused || IsTacticalTimeoutActive()) continue;
Position coachPosition = new(coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
coach!.PlayerPawn.Value!.Teleport(new Vector(coachPosition.PlayerPosition.X, coachPosition.PlayerPosition.Y, coachPosition.PlayerPosition.Z + 20.0f), coachPosition.PlayerAngle, new Vector(0, 0, 0));
// Dropping the C4 if it was picked up or passed to the coach.
DropWeaponByDesignerName(coach, "weapon_c4");
coach.PlayerPawn.Value!.CommitSuicide(explode: false, force: true);
}
Server.ExecuteCommand($"mp_suicide_penalty {suicidePenalty}; spec_freeze_time {specFreezeTime}; spec_freeze_time_lock {specFreezeTimeLock}; spec_freeze_deathanim_time {specFreezeDeathanim};");
}
private void GetCoachSpawns()
{
coachSpawns = GetEmptySpawnsData();
try
{
string spawnsConfigPath = Path.Combine(ModuleDirectory, "spawns", "coach", $"{Server.MapName}.json");
string spawnsConfig = File.ReadAllText(spawnsConfigPath);
var jsonDictionary = JsonSerializer.Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(spawnsConfig);
if (jsonDictionary is null) return;
foreach (var entry in jsonDictionary)
{
byte team = byte.Parse(entry.Key);
List<Position> positionList = new();
foreach (var positionData in entry.Value)
{
string[] vectorArray = positionData["Vector"].Split(' ');
string[] angleArray = positionData["QAngle"].Split(' ');
// Parse position and angle
Vector vector = new(float.Parse(vectorArray[0]), float.Parse(vectorArray[1]), float.Parse(vectorArray[2]));
QAngle qAngle = new(float.Parse(angleArray[0]), float.Parse(angleArray[1]), float.Parse(angleArray[2]));
Position position = new(vector, qAngle);
positionList.Add(position);
}
coachSpawns[team] = positionList;
}
Log($"[GetCoachSpawns] Loaded {coachSpawns.Count} coach spawns");
}
catch (Exception ex)
{
Log($"[GetCoachSpawns - FATAL] Error getting coach spawns. [ERROR]: {ex.Message}");
}
}
}