Skip to content

Commit 7bd6a19

Browse files
authored
autopause: Fix not working on second half (#903)
Thanks to @theletterjwithadot for reporting. Fix: - Fixed team player storage becomes empty after team swap. Change: - Replaced arrays with string maps for better ID lookups.
1 parent d44cb66 commit 7bd6a19

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed
-7 Bytes
Binary file not shown.

addons/sourcemod/scripting/autopause.sp

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ char sDebugMessage[256];
3737
public Plugin myinfo =
3838
{
3939
name = "L4D2 Auto-pause",
40-
author = "Darkid, Griffin, StarterX4",
40+
author = "Darkid, Griffin, StarterX4, Forgetest, J.",
4141
description = "When a player disconnects due to crash, automatically pause the game. When they rejoin, give them a correct spawn timer.",
42-
version = "2.2",
42+
version = "2.3",
4343
url = "https://github.com/SirPlease/L4D2-Competitive-Rework"
4444
}
4545

@@ -49,11 +49,10 @@ ConVar
4949
convarForceUnpause,
5050
convarDebug;
5151

52-
Handle
52+
StringMap
5353
crashedPlayers,
5454
generalCrashers,
55-
infectedPlayers,
56-
survivorPlayers;
55+
teamPlayers;
5756

5857
bool
5958
bReadyUpIsAvailable,
@@ -67,10 +66,9 @@ public void OnPluginStart()
6766
convarForceUnpause = CreateConVar("autopause_forceunpause", "0", "Whether or not we force unpause when the crashed players have loaded back in");
6867
convarDebug = CreateConVar("autopause_apdebug", "0", "0: No Debugging - 1: Sourcemod Logs - 2: PrintToChat - 3: Both", _, true, 0.0, true, 3.0);
6968

70-
crashedPlayers = CreateTrie();
71-
generalCrashers = CreateArray(64);
72-
infectedPlayers = CreateArray(64);
73-
survivorPlayers = CreateArray(64);
69+
crashedPlayers = new StringMap();
70+
generalCrashers = new StringMap();
71+
teamPlayers = new StringMap();
7472

7573
HookEvent("round_start", Event_RoundStart);
7674
HookEvent("round_end", Event_RoundEnd);
@@ -112,13 +110,11 @@ public void OnClientPutInServer(int client)
112110
if (strcmp(sAuthId, "BOT") == 0)
113111
return;
114112

115-
int crasherIndex = FindStringInArray(generalCrashers, sAuthId);
116-
117-
if (crasherIndex == -1)
113+
if (!generalCrashers.ContainsKey(sAuthId))
118114
return;
119115

120-
RemoveFromArray(generalCrashers, crasherIndex);
121-
int remainingCrashers = GetArraySize(generalCrashers);
116+
generalCrashers.Remove(sAuthId);
117+
int remainingCrashers = generalCrashers.Size;
122118

123119
if (convarDebug.BoolValue)
124120
{
@@ -148,12 +144,19 @@ public void OnClientPutInServer(int client)
148144
}
149145
}
150146

147+
public void OnMapEnd()
148+
{
149+
teamPlayers.Clear();
150+
}
151+
151152
void Event_RoundStart(Event hEvent, char[] sEventName, bool dontBroadcast)
152153
{
153-
ClearTrie(crashedPlayers);
154-
ClearArray(generalCrashers);
155-
ClearArray(infectedPlayers);
156-
ClearArray(survivorPlayers);
154+
crashedPlayers.Clear();
155+
generalCrashers.Clear();
156+
157+
// @Forgetest: "player_team" happens before "round_start"
158+
// teamPlayers.Clear();
159+
157160
bRoundEnd = false;
158161
}
159162

@@ -175,36 +178,11 @@ void Event_PlayerTeam(Event hEvent, char[] sEventName, bool dontBroadcast)
175178
if (strcmp(sAuthId, "BOT") == 0)
176179
return;
177180

178-
int survivorIndex = FindStringInArray(survivorPlayers, sAuthId);
179-
int infectedIndex = FindStringInArray(infectedPlayers, sAuthId);
180-
181-
if (survivorIndex != -1)
182-
{
183-
RemoveFromArray(survivorPlayers, survivorIndex);
184-
185-
if (convarDebug.BoolValue)
186-
{
187-
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Removed player %s from the survivor team.", sEventName, sAuthId);
188-
DebugLog(sDebugMessage);
189-
}
190-
}
191-
192-
if (infectedIndex != -1)
193-
{
194-
RemoveFromArray(infectedPlayers, infectedIndex);
195-
196-
if (convarDebug.BoolValue)
197-
{
198-
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Removed player %s from the infected team.", sEventName, sAuthId);
199-
DebugLog(sDebugMessage);
200-
}
201-
}
202-
203181
int newTeam = hEvent.GetInt("team");
204182

205183
if (newTeam == L4D_TEAM_SURVIVOR)
206184
{
207-
PushArrayString(survivorPlayers, sAuthId);
185+
teamPlayers.SetValue(sAuthId, newTeam);
208186

209187
if (convarDebug.BoolValue)
210188
{
@@ -216,11 +194,11 @@ void Event_PlayerTeam(Event hEvent, char[] sEventName, bool dontBroadcast)
216194
{
217195
float fSpawnTime;
218196

219-
if (GetTrieValue(crashedPlayers, sAuthId, fSpawnTime))
197+
if (crashedPlayers.GetValue(sAuthId, fSpawnTime))
220198
{
221199
CountdownTimer CTimer_SpawnTimer = L4D2Direct_GetSpawnTimer(client);
222200
CTimer_Start(CTimer_SpawnTimer, fSpawnTime);
223-
RemoveFromTrie(crashedPlayers, sAuthId);
201+
crashedPlayers.Remove(sAuthId);
224202

225203
if (convarDebug.BoolValue)
226204
{
@@ -229,14 +207,24 @@ void Event_PlayerTeam(Event hEvent, char[] sEventName, bool dontBroadcast)
229207
}
230208
}
231209

232-
PushArrayString(infectedPlayers, sAuthId);
210+
teamPlayers.SetValue(sAuthId, newTeam);
233211

234212
if (convarDebug.BoolValue)
235213
{
236214
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Added player %s to the infected team.", sEventName, sAuthId);
237215
DebugLog(sDebugMessage);
238216
}
239217
}
218+
else if (teamPlayers.GetValue(sAuthId, newTeam))
219+
{
220+
teamPlayers.Remove(sAuthId);
221+
222+
if (convarDebug.BoolValue)
223+
{
224+
Format(sDebugMessage, sizeof(sDebugMessage), "[AutoPause (%s)] Removed player %s from the %s team.", sEventName, sAuthId, newTeam == L4D_TEAM_SURVIVOR ? "survivor" : "infected");
225+
DebugLog(sDebugMessage);
226+
}
227+
}
240228
}
241229

242230
void Event_PlayerDisconnect(Event hEvent, char[] sEventName, bool dontBroadcast)
@@ -252,7 +240,7 @@ void Event_PlayerDisconnect(Event hEvent, char[] sEventName, bool dontBroadcast)
252240
if (strcmp(sAuthId, "BOT") == 0)
253241
return;
254242

255-
if (FindStringInArray(infectedPlayers, sAuthId) == -1 && FindStringInArray(survivorPlayers, sAuthId) == -1)
243+
if (!teamPlayers.ContainsKey(sAuthId))
256244
return;
257245

258246
if (GetClientTeam(client) == L4D_TEAM_SURVIVOR && !IsPlayerAlive(client))
@@ -286,14 +274,15 @@ void Event_PlayerDisconnect(Event hEvent, char[] sEventName, bool dontBroadcast)
286274
else
287275
FakeClientCommand(client, "sm_pause");
288276

289-
if (FindStringInArray(generalCrashers, sAuthId) == -1)
290-
PushArrayString(generalCrashers, sAuthId);
277+
if (!generalCrashers.ContainsKey(sAuthId))
278+
generalCrashers.SetValue(sAuthId, true);
291279

292280
CPrintToChatAll("%t", "Crashed", client);
293281
}
294282
}
295283

296-
if (FindStringInArray(infectedPlayers, sAuthId) != -1)
284+
int team;
285+
if (teamPlayers.GetValue(sAuthId, team) && team == L4D_TEAM_INFECTED)
297286
{
298287
CountdownTimer CTimer_SpawnTimer = L4D2Direct_GetSpawnTimer(client);
299288
if (CTimer_SpawnTimer != CTimer_Null)
@@ -306,7 +295,7 @@ void Event_PlayerDisconnect(Event hEvent, char[] sEventName, bool dontBroadcast)
306295
DebugLog(sDebugMessage);
307296
}
308297

309-
SetTrieValue(crashedPlayers, sAuthId, fTimeLeft);
298+
crashedPlayers.SetValue(sAuthId, fTimeLeft);
310299
}
311300
}
312301
}

0 commit comments

Comments
 (0)