forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoach.cs
209 lines (182 loc) · 8.45 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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Cvars;
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;
}
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;
int freezeTime = ConVar.Find("mp_freezetime")!.GetPrimitiveValue<int>();
coachKillTimer ??= AddTimer(freezeTime - 1.5f, KillCoaches);
HashSet<CCSPlayerController> coaches = GetAllCoaches();
foreach (var 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;
bool isCompetitiveSpawn = false;
Position coachPosition = new(coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
List<Position> teamPositions = spawnsData[(byte)coachTeamNum];
// Elevating the coach so that they don't block the players.
coach!.PlayerPawn.Value!.Teleport(new Vector(coach.PlayerPawn.Value.CBodyComponent!.SceneNode!.AbsOrigin.X, coach.PlayerPawn.Value.CBodyComponent!.SceneNode!.AbsOrigin.Y, coach.PlayerPawn.Value.CBodyComponent!.SceneNode!.AbsOrigin.Z + 75.0f), coach.PlayerPawn.Value.EyeAngles, new Vector(0, 0, 0));
coach.PlayerPawn.Value!.MoveType = MoveType_t.MOVETYPE_NONE;
coach.PlayerPawn.Value!.ActualMoveType = MoveType_t.MOVETYPE_NONE;
SetPlayerInvisible(player: coach, setWeaponsInvisible: false);
foreach (Position position in teamPositions)
{
if (position.Equals(coachPosition))
{
isCompetitiveSpawn = true;
break;
}
}
if (isCompetitiveSpawn)
{
foreach (var key in playerData.Keys)
{
CCSPlayerController player = playerData[key];
if (!IsPlayerValid(player) || coaches.Contains(player) || player.TeamNum != (byte)coachTeamNum) continue;
bool playerOnCompetitiveSpawn = false;
Position playerPosition = new(player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, player.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
foreach (Position position in teamPositions)
{
if (position.Equals(playerPosition))
{
playerOnCompetitiveSpawn = true;
break;
}
}
// No need to swap the player if they are already on a competitive spawn.
if (playerOnCompetitiveSpawn) continue;
// Swapping positions of the coach and the player so that the coach doesn't take any competitive spawn.
AddTimer(0.1f, () =>
{
coach!.PlayerPawn.Value.Teleport(new Vector(playerPosition.PlayerPosition.X, playerPosition.PlayerPosition.Y, playerPosition.PlayerPosition.Z + 150.0f), playerPosition.PlayerAngle, new Vector(0, 0, 0));
player!.PlayerPawn.Value.Teleport(coachPosition.PlayerPosition, coachPosition.PlayerAngle, new Vector(0, 0, 0));
});
}
}
HandleCoachWeapons(coach);
}
}
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()
{
HashSet<CCSPlayerController> coaches = GetAllCoaches();
string suicidePenalty = GetConvarStringValue(ConVar.Find("mp_suicide_penalty"));
string deathDropGunEnabled = GetConvarStringValue(ConVar.Find("mp_death_drop_gun"));
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; mp_death_drop_gun 0;spec_freeze_time 0; spec_freeze_time_lock 0; spec_freeze_deathanim_time 0;");
// Adding timer to make sure above commands are executed successfully.
AddTimer(0.5f, () =>
{
foreach (var coach in coaches)
{
if (!IsPlayerValid(coach)) continue;
coach.PlayerPawn.Value!.CommitSuicide(explode: false, force: true);
}
Server.ExecuteCommand($"mp_suicide_penalty {suicidePenalty}; mp_death_drop_gun {deathDropGunEnabled}; spec_freeze_time {specFreezeTime}; spec_freeze_time_lock {specFreezeTimeLock}; spec_freeze_deathanim_time {specFreezeDeathanim};");
});
}
}