forked from SirPlease/L4D2-Competitive-Rework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautopause.sp
More file actions
312 lines (249 loc) · 9.44 KB
/
Copy pathautopause.sp
File metadata and controls
312 lines (249 loc) · 9.44 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
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
/*
SourcePawn is Copyright (C) 2006-2015 AlliedModders LLC. All rights reserved.
SourceMod is Copyright (C) 2006-2015 AlliedModders LLC. All rights reserved.
Pawn and SMALL are Copyright (C) 1997-2015 ITB CompuPhase.
Source is Copyright (C) Valve Corporation.
All trademarks are property of their respective owners.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma newdecls required
#include <sourcemod>
#include <left4dhooks>
#include <colors>
#undef REQUIRE_PLUGIN
#include <readyup>
#include <pause>
#define DEBUG_SM 1
#define DEBUG_CHAT 2
char sDebugMessage[256];
public Plugin myinfo =
{
name = "L4D2 Auto-pause",
author = "Darkid, Griffin, StarterX4, Forgetest, J.",
description = "When a player disconnects due to crash, automatically pause the game. When they rejoin, give them a correct spawn timer.",
version = "2.3",
url = "https://github.com/SirPlease/L4D2-Competitive-Rework"
}
ConVar
convarEnabled,
convarForce,
convarForceUnpause,
convarDebug;
StringMap
crashedPlayers,
generalCrashers,
teamPlayers;
bool
bReadyUpIsAvailable,
bPauseIsAvailable,
bRoundEnd;
public void OnPluginStart()
{
convarEnabled = CreateConVar("autopause_enable", "1", "Whether or not to automatically pause when a player crashes.");
convarForce = CreateConVar("autopause_force", "0", "Whether or not to force pause when a player crashes.");
convarForceUnpause = CreateConVar("autopause_forceunpause", "0", "Whether or not we force unpause when the crashed players have loaded back in");
convarDebug = CreateConVar("autopause_apdebug", "0", "0: No Debugging - 1: Sourcemod Logs - 2: PrintToChat - 3: Both", _, true, 0.0, true, 3.0);
crashedPlayers = new StringMap();
generalCrashers = new StringMap();
teamPlayers = new StringMap();
HookEvent("round_start", Event_RoundStart);
HookEvent("round_end", Event_RoundEnd);
HookEvent("player_team", Event_PlayerTeam);
HookEvent("player_disconnect", Event_PlayerDisconnect, EventHookMode_Pre);
LoadTranslations("autopause.phrases");
}
public void OnAllPluginsLoaded()
{
bReadyUpIsAvailable = LibraryExists("readyup");
bPauseIsAvailable = LibraryExists("pause");
}
public void OnLibraryRemoved(const char[] name)
{
if (strcmp(name, "readyup") == 0)
bReadyUpIsAvailable = false;
if (strcmp(name, "pause") == 0)
bPauseIsAvailable = false;
}
public void OnLibraryAdded(const char[] name)
{
if (strcmp(name, "readyup") == 0)
bReadyUpIsAvailable = true;
if (strcmp(name, "pause") == 0)
bPauseIsAvailable = true;
}
public void OnClientPutInServer(int client)
{
char sAuthId[64];
GetClientAuthId(client, AuthId_Steam2, sAuthId, sizeof(sAuthId));
if (strcmp(sAuthId, "BOT") == 0)
return;
if (!generalCrashers.ContainsKey(sAuthId))
return;
generalCrashers.Remove(sAuthId);
int remainingCrashers = generalCrashers.Size;
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[Autopause (OnClientPutInServer)] Crashed Player %s rejoined.", sAuthId);
DebugLog(sDebugMessage);
}
if (convarForceUnpause.BoolValue && bPauseIsAvailable && IsInPause())
{
if (!remainingCrashers)
{
CPrintToChatAll("%t", "Unpausing");
ServerCommand("sm_forceunpause");
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[Autopause (OnClientPutInServer)] All crashed players rejoined. Force Unpause was triggered.");
DebugLog(sDebugMessage);
}
}
else
{
CPrintToChatAll("%t", "Waiting", remainingCrashers, remainingCrashers > 1 ? "players" : "player");
// https://github.com/Target5150/MoYu_Server_Stupid_Plugins/blob/78a5204e757e8a45309872ab2705c509500a9f6e/The%20Last%20Stand/readyup/readyup/panel.inc#L387
// to solve plural problem in languages
}
}
}
public void OnMapEnd()
{
teamPlayers.Clear();
}
void Event_RoundStart(Event hEvent, char[] sEventName, bool dontBroadcast)
{
crashedPlayers.Clear();
generalCrashers.Clear();
// @Forgetest: "player_team" happens before "round_start"
// teamPlayers.Clear();
bRoundEnd = false;
}
void Event_RoundEnd(Event hEvent, char[] sEventName, bool dontBroadcast)
{
bRoundEnd = true;
}
void Event_PlayerTeam(Event hEvent, char[] sEventName, bool dontBroadcast)
{
int client = GetClientOfUserId(hEvent.GetInt("userid"));
if (client <= 0 || client > MaxClients)
return;
char sAuthId[64];
GetClientAuthId(client, AuthId_Steam2, sAuthId, sizeof(sAuthId));
if (strcmp(sAuthId, "BOT") == 0)
return;
int newTeam = hEvent.GetInt("team");
if (newTeam == L4D_TEAM_SURVIVOR)
{
teamPlayers.SetValue(sAuthId, newTeam);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Added player %s to the survivor team.", sEventName, sAuthId);
DebugLog(sDebugMessage);
}
}
else if (newTeam == L4D_TEAM_INFECTED)
{
float fSpawnTime;
if (crashedPlayers.GetValue(sAuthId, fSpawnTime))
{
CountdownTimer CTimer_SpawnTimer = L4D2Direct_GetSpawnTimer(client);
CTimer_Start(CTimer_SpawnTimer, fSpawnTime);
crashedPlayers.Remove(sAuthId);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Player %s rejoined the infected, set spawn timer to %f.", sEventName, sAuthId, fSpawnTime);
DebugLog(sDebugMessage);
}
}
teamPlayers.SetValue(sAuthId, newTeam);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Added player %s to the infected team.", sEventName, sAuthId);
DebugLog(sDebugMessage);
}
}
else if (teamPlayers.GetValue(sAuthId, newTeam))
{
teamPlayers.Remove(sAuthId);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Removed player %s from the %s team.", sEventName, sAuthId, newTeam == L4D_TEAM_SURVIVOR ? "survivor" : "infected");
DebugLog(sDebugMessage);
}
}
}
void Event_PlayerDisconnect(Event hEvent, char[] sEventName, bool dontBroadcast)
{
int client = GetClientOfUserId(hEvent.GetInt("userid"));
if (client <= 0 || client > MaxClients)
return;
char sAuthId[64];
GetClientAuthId(client, AuthId_Steam2, sAuthId, sizeof(sAuthId));
if (strcmp(sAuthId, "BOT") == 0)
return;
if (!teamPlayers.ContainsKey(sAuthId))
return;
if (GetClientTeam(client) == L4D_TEAM_SURVIVOR && !IsPlayerAlive(client))
{
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Player %N left the game but is a dead Survivor", sEventName, client);
DebugLog(sDebugMessage);
}
return;
}
char sReason[128];
hEvent.GetString("reason", sReason, sizeof(sReason));
char sTimedOut[64];
Format(sTimedOut, sizeof(sTimedOut), "%N timed out", client);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Player %N (%s) left the game: %s", sEventName, client, sAuthId, sReason);
DebugLog(sDebugMessage);
}
if (strcmp(sReason, sTimedOut) == 0 || strcmp(sReason, "No Steam logon") == 0)
{
if (convarEnabled.BoolValue && (!bReadyUpIsAvailable || !IsInReady()) && !bRoundEnd)
{
if (convarForce.BoolValue)
ServerCommand("sm_forcepause");
else
FakeClientCommand(client, "sm_pause");
if (!generalCrashers.ContainsKey(sAuthId))
generalCrashers.SetValue(sAuthId, true);
CPrintToChatAll("%t", "Crashed", client);
}
}
int team;
if (teamPlayers.GetValue(sAuthId, team) && team == L4D_TEAM_INFECTED)
{
CountdownTimer CTimer_SpawnTimer = L4D2Direct_GetSpawnTimer(client);
if (CTimer_SpawnTimer != CTimer_Null)
{
float fTimeLeft = CTimer_GetRemainingTime(CTimer_SpawnTimer);
if (convarDebug.BoolValue)
{
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Player %s left the game with %f time until spawn.", sEventName, sAuthId, fTimeLeft);
DebugLog(sDebugMessage);
}
crashedPlayers.SetValue(sAuthId, fTimeLeft);
}
}
}
void DebugLog(char[] sMessage)
{
int flags = convarDebug.IntValue;
if (flags & DEBUG_SM)
LogMessage(sMessage);
if (flags & DEBUG_CHAT)
PrintToChatAll(sMessage);
}