-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchampionship.sp
424 lines (320 loc) · 14.1 KB
/
championship.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#define PLUGIN_AUTHOR "rgsilva"
#define PLUGIN_VERSION "1.6"
#pragma semicolon 1
#define MAX_ARG_LENGTH 64
#define MAX_BACKUP_SIZE (MAXPLAYERS * 2)
#define MAXROUNDS 0
#define TIMELIMIT 0
#define RESTARTGAME_TIME "3"
#define RESETGAME_TIME "2"
public Plugin:myinfo =
{
name = "Championship",
author = PLUGIN_AUTHOR,
description = "Custom championship mode",
version = PLUGIN_VERSION,
url = "https://github.com/rgsilva/sourcemod-plugins"
};
bool championshipRunning;
bool nextRoundRestartGame;
bool nextRoundSwitchTeams;
int overtimeIndex;
int normalRounds;
int overtimeRounds;
int normalMoney;
int overtimeMoney;
Handle cvarWinLimit;
Handle cvarMaxRounds;
Handle cvarTimeLimit;
Handle cvarRestartGame;
Handle cvarStartMoney;
Handle cvarVerbose;
Handle cvarDebug;
int originalMaxRounds;
int originalTimeLimit;
int originalStartMoney;
bool backupAvailable;
int backupClientFrags[MAX_BACKUP_SIZE];
int backupClientDeaths[MAX_BACKUP_SIZE];
int backupCtScore;
int backupTrScore;
bool pluginVerbose;
bool pluginDebug;
public OnPluginStart()
{
pluginVerbose = true;
pluginDebug = false;
SanityCheck();
championshipRunning = false;
overtimeIndex = 0;
nextRoundRestartGame = false;
nextRoundSwitchTeams = false;
backupAvailable = false;
RegConsoleCmd("start_championship", CmdStartChampionship);
RegConsoleCmd("end_championship", CmdEndChampionship);
cvarWinLimit = FindConVar("mp_winlimit");
cvarMaxRounds = FindConVar("mp_maxrounds");
cvarTimeLimit = FindConVar("mp_timelimit");
cvarRestartGame = FindConVar("mp_restartgame");
cvarStartMoney = FindConVar("mp_startmoney");
cvarVerbose = CreateConVar("championship_verbose", "1", "Show verbose messages", _, true, 0.0, true, 1.0);
HookConVarChange(cvarVerbose, OnConVarChanged);
cvarDebug = CreateConVar("championship_debug", "0", "Show debug messages", _, true, 0.0, true, 1.0);
HookConVarChange(cvarDebug, OnConVarChanged);
HookEvent("round_start", OnRoundStart);
HookEvent("round_end", OnRoundEnd);
}
void SanityCheck() {
if (MAX_BACKUP_SIZE <= MaxClients || MAX_BACKUP_SIZE <= MAXPLAYERS) {
ThrowError("WARNING: MAX_BACKUP_SIZE will be reached! This will cause a buffer overflow! MAX_BACKUP_SIZE = %d, MaxClients = %d, MAXPLAYERS = %d!", MAX_BACKUP_SIZE, MaxClients, MAXPLAYERS);
}
}
public OnConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[]) {
if (convar == cvarVerbose) {
pluginVerbose = GetConVarBool(cvarVerbose);
} else if (convar == cvarDebug) {
pluginDebug = GetConVarBool(cvarDebug);
}
}
public OnMapStart() {
EndChampionship(true);
}
public Action CmdStartChampionship(int client, int args) {
if (args != 4) {
ReplyToCommand(client, "[SM] Usage: start_championship <normal rounds> <overtime rounds> <normal money amount> <overtime money amount>");
return Plugin_Handled;
}
if (championshipRunning) {
ReplyToCommand(client, "[SM] Cannot start championship: another one is running. Please end it first.");
return Plugin_Handled;
}
new String:temp[MAX_ARG_LENGTH];
GetCmdArg(1, temp, sizeof(temp));
int _normalRounds = StringToInt(temp);
GetCmdArg(2, temp, sizeof(temp));
int _overtimeRounds = StringToInt(temp);
GetCmdArg(3, temp, sizeof(temp));
int _normalMoney = StringToInt(temp);
GetCmdArg(4, temp, sizeof(temp));
int _overtimeMoney = StringToInt(temp);
if (_normalRounds <= 2 || _overtimeRounds <= 2 || _normalRounds % 2 != 0 || _overtimeRounds % 2 != 0) {
ReplyToCommand(client, "[SM] Cannot start championship: number of rounds must be a multiple of 2 and bigger than 2!");
return Plugin_Handled;
}
if (_normalMoney <= 0 || _overtimeMoney <= 0) {
ReplyToCommand(client, "[SM] Cannot start championship: money must be bigger than zero!");
return Plugin_Handled;
}
ReplyToCommand(client, "[SM] Starting championship with %d normal rounds (start money: %d) and %d overtime ones (start money: %d). Have fun and good luck!",
_normalRounds, _normalMoney, _overtimeRounds, _overtimeMoney);
StartChampionship(_normalRounds, _overtimeRounds, _normalMoney, _overtimeMoney);
return Plugin_Handled;
}
public Action CmdEndChampionship(int client, int args) {
if (!championshipRunning) {
ReplyToCommand(client, "[SM] Cannot end championship: there isn't one running. Please start it first.");
return Plugin_Handled;
}
EndChampionship(false);
return Plugin_Handled;
}
void StartChampionship(int _normalRounds, int _overtimeRounds, int _normalMoney, int _overtimeMoney) {
overtimeIndex = 0;
originalMaxRounds = GetConVarInt(cvarMaxRounds);
originalTimeLimit = GetConVarInt(cvarTimeLimit);
originalStartMoney = GetConVarInt(cvarStartMoney);
SetConVarInt(cvarMaxRounds, MAXROUNDS, false, pluginDebug);
SetConVarInt(cvarTimeLimit, TIMELIMIT, false, pluginDebug);
UpdateWinLimit(16);
championshipRunning = true;
normalRounds = _normalRounds;
overtimeRounds = _overtimeRounds;
normalMoney = _normalMoney;
overtimeMoney = _overtimeMoney;
PrintToChatAll("\x03[Championship] A new championship is starting! Have fun everyone, and good luck!");
SetConVarString(cvarRestartGame, RESTARTGAME_TIME, false, pluginDebug);
}
void EndChampionship(bool silent) {
if (!silent) {
PrintToChatAll("\x03[Championship] The championship has ended!");
}
championshipRunning = false;
overtimeIndex = 0;
nextRoundRestartGame = false;
nextRoundSwitchTeams = false;
backupAvailable = false;
SetConVarInt(cvarMaxRounds, originalMaxRounds, false, pluginDebug);
SetConVarInt(cvarTimeLimit, originalTimeLimit, false, pluginDebug);
SetConVarInt(cvarStartMoney, originalStartMoney, false, pluginDebug);
}
void EndWithWinner(int winnerTeam) {
PrintToChatAll("\x03[Championship] And the winner is... the %s!", (winnerTeam == CS_TEAM_CT ? "Counter-Terrorists" : "Terrorists"));
EndChampionship(false);
}
public Action OnRoundStart(Event event, const char[] name, bool dontBroadcast) {
if (!championshipRunning) {
return Plugin_Handled;
}
// Should we switch teams?
if (nextRoundSwitchTeams) {
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] nextRoundSwitchTeams is true!"); }
nextRoundSwitchTeams = false;
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Switching team scores"); }
// Switch the team scores as well.
int tmp = CS_GetTeamScore(CS_TEAM_T);
CS_SetTeamScore(CS_TEAM_T, CS_GetTeamScore(CS_TEAM_CT));
CS_SetTeamScore(CS_TEAM_CT, tmp);
SetTeamScore(CS_TEAM_T, CS_GetTeamScore(CS_TEAM_T));
SetTeamScore(CS_TEAM_CT, CS_GetTeamScore(CS_TEAM_CT));
// Switch every client's team.
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Switching player teams"); }
for (int client = 1; client <= MaxClients; client++) {
if (IsClientInGame(client) && GetClientTeam(client) > 1) {
CS_SwitchTeam(client, (GetClientTeam(client) == CS_TEAM_T) ? CS_TEAM_CT : CS_TEAM_T);
}
}
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Switch completed!"); }
}
// Should we restart the game?
if (nextRoundSwitchTeams || nextRoundRestartGame) {
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] nextRoundSwitchTeams (%d) or nextRoundRestartGame (%d) is true!", nextRoundSwitchTeams, nextRoundRestartGame); }
nextRoundRestartGame = false;
// Backup everyone's scores.
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Triggering score backup"); }
BackupScores();
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Triggering game restart"); }
SetConVarString(cvarRestartGame, RESETGAME_TIME, false, pluginDebug);
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Exiting OnRoundStart"); }
return Plugin_Handled;
}
// Restore a backup if available.
if (backupAvailable) {
RestoreScores();
}
// Standard round stuff!
int ctScore = GetTeamScore(CS_TEAM_CT);
int tScore = GetTeamScore(CS_TEAM_T);
if (ctScore + tScore >= normalRounds) {
overtimeIndex = ((ctScore + tScore - normalRounds) / overtimeRounds) + 1;
if (pluginVerbose) { PrintToChatAll("\x03[Championship] Overtime (%d) round started.", overtimeIndex); }
} else {
if (pluginVerbose) { PrintToChatAll("\x03[Championship] Normal round started.", overtimeIndex); }
}
return Plugin_Handled;
}
public Action OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
if (!championshipRunning) {
return Plugin_Handled;
}
int ctScore = GetTeamScore(CS_TEAM_CT);
int tScore = GetTeamScore(CS_TEAM_T);
if (ctScore + tScore <= normalRounds) {
// We are normal time. We can use the direct score values.
if (pluginVerbose) { PrintToChatAll("\x03[Championship] Normal round finished. CT score: %d, TR score: %d", ctScore, tScore); }
HandleNormalRoundEnd(ctScore, tScore);
} else {
// We are in overtime! We must calculate the correct score for these rounds.
int ctOvertimeScore = ctScore - normalRounds/2 - (overtimeRounds/2 * (overtimeIndex - 1));
int tOvertimeScore = tScore - normalRounds/2 - (overtimeRounds/2 * (overtimeIndex - 1));
if (pluginVerbose) { PrintToChatAll("\x03[Championship] Overtime (%d) round finished. CT score: %d, TR score: %d", overtimeIndex, ctOvertimeScore, tOvertimeScore); }
HandleOvertimeRoundEnd(ctOvertimeScore, tOvertimeScore);
}
return Plugin_Handled;
}
void HandleNormalRoundEnd(int ctScore, int tScore) {
// Keep win limit set to NR/2 + 1.
UpdateWinLimit(normalRounds/2 + 1);
// If any team is over NR/2 points, it's a winner.
if (ctScore > normalRounds/2) {
// CT wins
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] CT wins!"); }
EndWithWinner(CS_TEAM_CT);
} else if (tScore > normalRounds/2) {
// TR wins
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] TR wins!"); }
EndWithWinner(CS_TEAM_T);
} else if (ctScore + tScore == normalRounds/2) {
// NR/2 reached, let's switch sides!
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] NR/2 reached"); }
SetNextRoundMoney(normalMoney);
RestartGame(true);
} else if (ctScore + tScore == normalRounds && ctScore == tScore) {
// NR reached and it's a tie - let's start overtime!
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] NR reached, starting overtime"); }
UpdateWinLimit(normalRounds/2 + overtimeRounds/2 + 1);
SetNextRoundMoney(overtimeMoney);
RestartGame(false);
PrintToChatAll("\x03[Championship] It's a tie! Let's see who wins in the overtime!");
}
}
void HandleOvertimeRoundEnd(int ctScore, int tScore) {
UpdateWinLimit(normalRounds/2 + (overtimeRounds/2 * overtimeIndex) + 1);
if (ctScore > overtimeRounds/2) {
// CT wins
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] CT wins!"); }
EndWithWinner(CS_TEAM_CT);
} else if (tScore > overtimeRounds/2) {
// TR wins
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] TR wins!"); }
EndWithWinner(CS_TEAM_T);
} else if (ctScore + tScore == overtimeRounds/2) {
// OR/2 reached, let's switch sides!
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] OR/2 reached, switching sides!"); }
SetNextRoundMoney(overtimeMoney);
RestartGame(true);
} else if (ctScore + tScore == overtimeRounds && ctScore == tScore) {
// OR reached and it's a tie - let's start a new overtime!
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] OR reached, starting new overtime!"); }
UpdateWinLimit(normalRounds/2 + (overtimeRounds/2 * (overtimeIndex+1)) + 1);
SetNextRoundMoney(overtimeMoney);
RestartGame(false);
PrintToChatAll("\x03[Championship] It's a tie! Let's see who wins in the next overtime!");
}
}
void UpdateWinLimit(int winLimit) {
SetConVarInt(cvarWinLimit, winLimit, false, pluginDebug);
}
void SetNextRoundMoney(int money) {
SetConVarInt(cvarStartMoney, money);
}
void RestartGame(bool switchTeams) {
nextRoundRestartGame = true;
nextRoundSwitchTeams = switchTeams;
if (switchTeams) {
PrintToChatAll("\x03[Championship] Switching sides in the next round!");
}
}
void BackupScores() {
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Creating score backup"); }
backupCtScore = CS_GetTeamScore(CS_TEAM_CT);
backupTrScore = CS_GetTeamScore(CS_TEAM_T);
for (int client = 1; client <= MaxClients; client++) {
if (IsClientInGame(client) && GetClientTeam(client) > 1) {
backupClientFrags[client] = GetEntProp(client, Prop_Data, "m_iFrags");
backupClientDeaths[client] = GetEntProp(client, Prop_Data, "m_iDeaths");
} else {
backupClientFrags[client] = 0;
backupClientDeaths[client] = 0;
}
}
backupAvailable = true;
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Scores backup created!"); }
}
void RestoreScores() {
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Restoring score backup"); }
CS_SetTeamScore(CS_TEAM_CT, backupCtScore);
SetTeamScore(CS_TEAM_CT, backupCtScore);
CS_SetTeamScore(CS_TEAM_T, backupTrScore);
SetTeamScore(CS_TEAM_T, backupTrScore);
for (int client = 1; client <= MaxClients; client++) {
if (IsClientInGame(client) && GetClientTeam(client) > 1) {
SetEntProp(client, Prop_Data, "m_iFrags", backupClientFrags[client]);
SetEntProp(client, Prop_Data, "m_iDeaths", backupClientDeaths[client]);
}
}
backupAvailable = false;
if (pluginDebug) { PrintToChatAll("\x05[Championship] [DEBUG] Scores backup restored!"); }
}