-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathteam_consistency.sp
332 lines (284 loc) · 12.4 KB
/
team_consistency.sp
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
332
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <colors>
#include <readyup>
#include <builtinvotes>
#define ROUNDEND_DELAY 3.0
#define IS_VALID_CLIENT(%1) (%1 > 0 && %1 <= MaxClients)
#define IS_SURVIVOR(%1) (GetClientTeam(%1) == 2)
#define IS_INFECTED(%1) (GetClientTeam(%1) == 3)
#define IS_VALID_INGAME(%1) (IS_VALID_CLIENT(%1) && IsClientInGame(%1))
#define IS_VALID_SURVIVOR(%1) (IS_VALID_INGAME(%1) && IS_SURVIVOR(%1))
#define IS_VALID_INFECTED(%1) (IS_VALID_INGAME(%1) && IS_INFECTED(%1))
#define TEAM_SPECTATOR 1
#define TEAM_SURVIVOR 2
#define TEAM_INFECTED 3
enum VoteType
{
VoteType_Clear
}
Handle hVote = INVALID_HANDLE;
VoteType g_VoteType;
bool g_bRoundLive = false;
Handle g_hCvarDebug = null;
Handle g_hTriePlayerTeam = null;
public Plugin myinfo =
{
name = "Team Consistency",
author = "devilesk",
description = "Maintain consistent teams throughout a match.",
version = "1.2.0",
url = "/~https://github.com/devilesk/rl4d2l-plugins"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
RegPluginLibrary("team_consistency");
CreateNative("ClearTeamConsistency", Native_ClearTeamConsistency);
return APLRes_Success;
}
public int Native_ClearTeamConsistency(Handle plugin, int numParams)
{
ClearTrie(g_hTriePlayerTeam);
return 1;
}
public void OnPluginStart()
{
g_hCvarDebug = CreateConVar("team_consistency_debug", "1", "Whether or not to debug to console", 0);
RegAdminCmd("sm_swap", Swap_Cmd, ADMFLAG_KICK, "");
RegConsoleCmd("sm_untrackteamreset", Reset_Cmd, "Clear tracked teams.");
RegConsoleCmd("sm_untrackteam", UntrackTeam_Cmd, "");
HookEvent("round_end", Event_RoundEnd, EventHookMode_PostNoCopy);
HookEvent("player_team", PlayerTeam_Event, EventHookMode_Post);
g_hTriePlayerTeam = CreateTrie();
}
public void OnMapStart() {
g_bRoundLive = false;
}
public void OnRoundLiveCountdownPre() {
g_bRoundLive = true;
}
// Store the logical team that players are on when the round ends
public void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast) {
if ( !g_bRoundLive ) {
return;
}
g_bRoundLive = false;
int survivorLogicalTeam = GameRules_GetProp("m_bAreTeamsFlipped");
int infectedLogicalTeam = 1 - survivorLogicalTeam;
PrintDebug("[Event_RoundEnd] Survivor index: %i Infected index: %i", survivorLogicalTeam, infectedLogicalTeam);
for ( int client = 1; client <= MaxClients; client++ )
{
if (!IsClientInGame(client) || IsFakeClient(client)) continue;
char sSteamId[32];
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
if (IS_VALID_SURVIVOR(client))
{
SetTrieValue(g_hTriePlayerTeam, sSteamId, survivorLogicalTeam);
PrintDebug("[Event_RoundEnd] Saving survivor %N %s logical team %i.", client, sSteamId, survivorLogicalTeam);
}
else if (IS_VALID_INFECTED(client)) {
SetTrieValue(g_hTriePlayerTeam, sSteamId, infectedLogicalTeam);
PrintDebug("[Event_RoundEnd] Saving infected %N %s logical team %i.", client, sSteamId, infectedLogicalTeam);
}
}
}
public Action UntrackTeam_Cmd(int client, int args) {
char sSteamId[32];
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
PrintDebug("[UntrackTeam_Cmd] Removing saved logical team for client: %i %N", client, client);
RemoveFromTrie(g_hTriePlayerTeam, sSteamId);
CPrintToChatAll("{default}[{green}Team Consistency{default}] Cleared tracked team value for {olive}%N{default}.", client);
return Plugin_Handled;
}
// Untrack swap targets so the swaps goes through
public Action Swap_Cmd(int client, int args) {
if (args < 1)
{
return Plugin_Continue;
}
char argbuf[MAX_NAME_LENGTH];
int[] targets = new int[MaxClients+1];
int target;
int targetCount;
char target_name[MAX_TARGET_LENGTH];
bool tn_is_ml;
char sSteamId[32];
for (int i = 1; i <= args; i++)
{
GetCmdArg(i, argbuf, sizeof(argbuf));
targetCount = ProcessTargetString(
argbuf,
0,
targets,
MaxClients+1,
COMMAND_FILTER_NO_BOTS,
target_name,
sizeof(target_name),
tn_is_ml);
for (int j = 0; j < targetCount; j++)
{
target = targets[j];
if (!IsClientInGame(target) || IsFakeClient(target)) continue;
GetClientAuthId(target, AuthId_Steam2, sSteamId, sizeof(sSteamId));
PrintDebug("[Swap_Cmd] Removing saved logical team for client: %i %N", target, target);
RemoveFromTrie(g_hTriePlayerTeam, sSteamId);
}
}
return Plugin_Continue;
}
void ClearTrackedTeams() {
PrintDebug("[ClearTrackedTeams] Clearing tracked teams");
ClearTrie(g_hTriePlayerTeam);
CPrintToChatAll("{default}[{green}Team Consistency{default}] Cleared tracked teams.");
}
public Action Reset_Cmd(int client, int args) {
bool bIsAdmin = CheckCommandAccess(client, "sm_untrackteamreset", ADMFLAG_KICK, true);
if (bIsAdmin) {
ClearTrackedTeams();
}
else {
char prompt[100];
Format(prompt, sizeof(prompt), "Clear tracked teams?");
StartVote(client, prompt, VoteType_Clear);
FakeClientCommand(client, "Vote Yes");
}
return Plugin_Handled;
}
public void StartVote(int client, const char[] sVoteHeader, VoteType voteType) {
int iNumPlayers;
int[] players = new int[MaxClients];
for (int i = 1; i <= MaxClients; i++) {
if (!IsClientConnected(i) || !IsClientInGame(i)) continue;
if (IsSpectator(i) || IsFakeClient(i)) continue;
players[iNumPlayers++] = i;
}
g_VoteType = voteType;
hVote = CreateBuiltinVote(VoteActionHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End);
SetBuiltinVoteArgument(hVote, sVoteHeader);
SetBuiltinVoteInitiator(hVote, client);
SetBuiltinVoteResultCallback(hVote, VoteResultHandler);
DisplayBuiltinVote(hVote, players, iNumPlayers, 20);
}
public void VoteActionHandler(Handle vote, BuiltinVoteAction action, int param1, int param2) {
switch (action) {
case BuiltinVoteAction_End: {
hVote = INVALID_HANDLE;
delete vote;
}
case BuiltinVoteAction_Cancel: {
DisplayBuiltinVoteFail(vote, view_as<BuiltinVoteFailReason>(param1));
}
}
}
public void VoteResultHandler(Handle vote, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info) {
for (int i = 0; i < num_items; i++) {
if (item_info[i][BUILTINVOTEINFO_ITEM_INDEX] == BUILTINVOTES_VOTE_YES) {
if (item_info[i][BUILTINVOTEINFO_ITEM_VOTES] > (num_clients / 2)) {
DisplayBuiltinVotePass(vote, "Clearing tracked teams...");
PrintToChatAll("\x01[\x04Team Consistency\x01] Vote passed! Clearing tracked teams...");
PrintDebug("[VoteResultHandler] Vote passed! Clearing tracked teams...");
if (g_VoteType == VoteType_Clear)
ClearTrackedTeams();
return;
}
}
}
DisplayBuiltinVoteFail(vote, BuiltinVoteFail_Loses);
}
// Trigger team consistency check whenever a tracked player joins survivor or infected
public void PlayerTeam_Event(Handle event, const char[] name, bool dontBroadcast) {
// No check while round is live
if (g_bRoundLive) {
return;
}
int team = GetEventInt(event, "team");
int client = GetClientOfUserId(GetEventInt(event, "userid"));
if (!IsClientInGame(client) || IsFakeClient(client) || team == TEAM_SPECTATOR) return;
char sSteamId[32];
int playerLogicalTeam;
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
if (GetTrieValue(g_hTriePlayerTeam, sSteamId, playerLogicalTeam)) {
PrintDebug("[PlayerTeam_Event] Found saved logical team %i for client: %i %N team: %i", playerLogicalTeam, client, client, team);
RequestFrame(SetTeams);
}
else {
PrintDebug("[PlayerTeam_Event] No saved logical team for client: %i %N team: %i", client, client, team);
}
}
// Move all tracked players on the wrong team to spectator first and then move them to the correct team
void SetTeams() {
int survivorLogicalTeam = GameRules_GetProp("m_bAreTeamsFlipped");
int infectedLogicalTeam = 1 - survivorLogicalTeam;
char sSteamId[32];
int playerLogicalTeam;
PrintDebug("[SetTeams] Logical team %i as survivor, logical team %i as infected", survivorLogicalTeam, infectedLogicalTeam);
PrintDebug("[SetTeams] Moving players to spectator...");
for (int client = 1; client <= MaxClients; client++)
{
if (!IsClientInGame(client) || IsFakeClient(client)) continue;
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
if (GetTrieValue(g_hTriePlayerTeam, sSteamId, playerLogicalTeam)) {
int clientTeam = GetClientTeam(client);
if (clientTeam == TEAM_SPECTATOR) {
PrintDebug("[SetTeams] Already spectator %N %s. Saved logical team: %i", client, sSteamId, playerLogicalTeam);
continue;
}
if (playerLogicalTeam == survivorLogicalTeam && clientTeam != TEAM_SURVIVOR) {
ChangeClientTeam(client, TEAM_SPECTATOR);
PrintDebug("[SetTeams] Moving to spectator %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
else if (playerLogicalTeam == infectedLogicalTeam && clientTeam != TEAM_INFECTED) {
ChangeClientTeam(client, TEAM_SPECTATOR);
PrintDebug("[SetTeams] Moving to spectator %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
else {
PrintDebug("[SetTeams] On the correct team %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
}
else {
//ChangeClientTeam(client, TEAM_SPECTATOR);
PrintDebug("[SetTeams] Player not found %N %s", client, sSteamId);
}
}
PrintDebug("[SetTeams] Moving players to teams...");
for (int client = 1; client <= MaxClients; client++)
{
if (!IsClientInGame(client) || IsFakeClient(client)) continue;
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
if (GetTrieValue(g_hTriePlayerTeam, sSteamId, playerLogicalTeam)) {
int clientTeam = GetClientTeam(client);
if (playerLogicalTeam == survivorLogicalTeam && clientTeam != TEAM_SURVIVOR) {
int flags = GetCommandFlags("sb_takecontrol");
SetCommandFlags("sb_takecontrol", flags & ~FCVAR_CHEAT);
FakeClientCommand(client, "sb_takecontrol");
SetCommandFlags("sb_takecontrol", flags);
CPrintToChatAll("{default}[{green}Team Consistency{default}] Moved {olive}%N{default} to survivor.", client);
PrintDebug("[SetTeams] Moving to survivor %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
else if (playerLogicalTeam == infectedLogicalTeam && clientTeam != TEAM_INFECTED) {
ChangeClientTeam(client, TEAM_INFECTED);
CPrintToChatAll("{default}[{green}Team Consistency{default}] Moved {olive}%N{default} to infected.", client);
PrintDebug("[SetTeams] Moving to infected %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
else {
PrintDebug("[SetTeams] On the correct team %N %s. Team: %i. Saved logical team: %i", client, sSteamId, clientTeam, playerLogicalTeam);
}
}
else {
PrintDebug("[SetTeams] Player not found %N %s", client, sSteamId);
}
}
}
bool IsSpectator(int client) {
return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == TEAM_SPECTATOR;
}
stock void PrintDebug(const char[] Message, any ...) {
if (GetConVarBool(g_hCvarDebug)) {
char DebugBuff[256];
VFormat(DebugBuff, sizeof(DebugBuff), Message, 2);
LogMessage(DebugBuff);
//PrintToChatAll(DebugBuff);
}
}