-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullTeamFix.cs
242 lines (213 loc) · 7.86 KB
/
FullTeamFix.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
namespace FullTeamFix;
public class FullTeamFix : BasePlugin
{
public override string ModuleName => "FullTeamFix";
public override string ModuleVersion => "0.0.1";
public override string ModuleAuthor => "ShookEagle";
public override string ModuleDescription => "Fixes Full teams by adding a spwan on a pre-existing one.";
public Random Random = new Random();
public enum JoinTeamReason
{
TeamsFull = 1,
TerroristTeamFull = 2,
CTTeamFull = 3,
TTeamLimit = 7,
CTTeamLimit = 8
}
public int TerroristSpawns = -1;
public int CTSpawns = -1;
public bool respectlimitteams = true;
private Dictionary<CCSPlayerController, int> SelectedTeam = new Dictionary<CCSPlayerController, int>();
public class CustomSpawnPoint
{
public CsTeam Team { get; set; }
public required string Origin { get; set; }
public required string Angle { get; set; }
}
public override void Load(bool hotReload)
{
RegisterListener<Listeners.OnMapStart>(OnMapStart);
AddCommandListener("jointeam", Command_Jointeam, HookMode.Pre);
}
public HookResult Command_Jointeam(CCSPlayerController? player, CommandInfo info)
{
if (player != null && player.IsValid && info.ArgCount > 0 && info.ArgByIndex(0).ToLower() == "jointeam")
{
if (info.ArgCount > 1)
{
string teamArg = info.ArgByIndex(1);
if (int.TryParse(teamArg, out int teamId))
{
if (teamId >= (int)CsTeam.Spectator && teamId <= (int)CsTeam.CounterTerrorist)
{
SelectedTeam[player] = teamId;
}
}
else
{
Console.WriteLine("Failed to parse team ID.");
}
}
}
return HookResult.Continue;
}
[GameEventHandler(HookMode.Pre)]
public HookResult TeamJoinFailed(EventJointeamFailed @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
if (player == null || !player.IsValid)
{
return HookResult.Continue;
}
JoinTeamReason m_eReason = (JoinTeamReason)@event.Reason;
int iTs = Utilities.GetPlayers().Where(p => p.Team == CsTeam.Terrorist).Count();
int iCTs = Utilities.GetPlayers().Where(p => p.Team == CsTeam.CounterTerrorist).Count();
if (!SelectedTeam.ContainsKey(player))
{
SelectedTeam[player] = 0;
}
switch (m_eReason)
{
case JoinTeamReason.TeamsFull:
if (iCTs >= CTSpawns && iTs >= TerroristSpawns)
{
NewSpawnFromDefault("t");
NewSpawnFromDefault("ct");
return HookResult.Continue;
}
break;
case JoinTeamReason.TerroristTeamFull:
if (iTs >= TerroristSpawns)
{
return NewSpawnFromDefault("t") ? HookResult.Continue : HookResult.Stop;
}
break;
case JoinTeamReason.CTTeamFull:
if (iCTs >= CTSpawns)
{
return NewSpawnFromDefault("ct") ? HookResult.Continue : HookResult.Stop;
}
break;
default:
{
return HookResult.Continue;
}
}
return HookResult.Continue;
}
public void OnMapStart(string mapname)
{
AddTimer(0.1f, () =>
{
TerroristSpawns = 0;
CTSpawns = 0;
var tSpawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_terrorist");
var ctSpawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_counterterrorist");
foreach (var spawn in tSpawns)
{
TerroristSpawns++;
}
foreach (var spawn in ctSpawns)
{
CTSpawns++;
}
});
}
public bool NewSpawnFromDefault(string team)
{
var ctspawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_counterterrorist").ToList();
var tspawns = Utilities.FindAllEntitiesByDesignerName<SpawnPoint>("info_player_terrorist").ToList();
if (team == "ct" && ctspawns.Count == 0)
{
Logger.LogError("This map contains no CT spawns.");
return false;
}
if (team == "t" && tspawns.Count == 0)
{
Logger.LogError("This map contains no T spawns.");
return false;
}
var spawn = (team == "ct") ? ctspawns[Random.Next(ctspawns.Count)] : tspawns[Random.Next(tspawns.Count)];
var origin = spawn.AbsOrigin;
var angle = spawn.AbsRotation;
if (origin == null || angle == null)
{
Logger.LogError("Origin or angle not found when attempting to create a spawn.");
return false;
}
var point = new CustomSpawnPoint
{
Team = team == "ct" ? CsTeam.CounterTerrorist : CsTeam.Terrorist,
Origin = VectorToString(new Vector3(origin.X, origin.Y, origin.Z)),
Angle = VectorToString(new Vector3(angle.X, angle.Y, angle.Z))
};
if (CreateEntity(point))
{
if (team == "ct") CTSpawns++;
else TerroristSpawns++;
Logger.LogInformation($"New {team} spawn created.");
return true;
}
else
{
Logger.LogInformation("Spawn creation failed.");
return false;
}
}
public bool CreateEntity(CustomSpawnPoint spawnPoint)
{
var noVel = new Vector(0f, 0f, 0f);
SpawnPoint? entity;
if (spawnPoint.Team == CsTeam.Terrorist)
{
entity = Utilities.CreateEntityByName<CInfoPlayerTerrorist>("info_player_terrorist");
}
else
{
entity = Utilities.CreateEntityByName<CInfoPlayerCounterterrorist>("info_player_counterterrorist");
}
if (entity == null)
{
return false;
}
var angle = StringToVector(spawnPoint.Angle);
entity.Teleport(NormalVectorToValve(StringToVector(spawnPoint.Origin)), new QAngle(angle.X, angle.Y, angle.Z), noVel);
entity.DispatchSpawn();
return true;
}
private static string VectorToString(Vector3 vec)
{
return $"{vec.X}|{vec.Y}|{vec.Z}";
}
private static Vector3 StringToVector(string str)
{
var explode = str.Split("|");
return new Vector3(x: float.Parse(explode[0]), y: float.Parse(explode[1]), z: float.Parse(explode[2]));
}
private static Vector NormalVectorToValve(Vector3 v)
{
return new Vector(v.X, v.Y, v.Z);
}
public static int GetTeamPlayerCount(CsTeam team)
{
return Utilities.GetPlayers().Count(p => p.Team == team);
}
}