-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCAP.sp
515 lines (426 loc) · 14 KB
/
CAP.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#pragma semicolon 1
/*--------------------------------------------------------------------------------
/ Includes
/-------------------------------------------------------------------------------*/
#include "CAP.inc"
#include "PLAYERS.sp"
/*--------------------------------------------------------------------------------
/ Globals
/-------------------------------------------------------------------------------*/
const INFO_NOTUSED_MAXLEN = 2; //For places where info isn't used
const ISTRING_MAXLEN = 5; //0-9999
static CapMode_Stage = 0;
/*--------------------------------------------------------------------------------
/ Commands
/-------------------------------------------------------------------------------*/
public Action:Command_mcp_menu(client, args)
{
if(client == 0)
return Plugin_Handled;
if(PLAYERS_isHumanNotCap(client))
LoadChooseClassMenu(client);
else if(client == PLAYERS_GetCaptainClient(0) || client == PLAYERS_GetCaptainClient(1))
LoadCaptainPickMenu(client);
else
PrintToConsole(client, "[MCP] You've already been picked");
return Plugin_Handled;
}
public Action:Command_mcp_start(client, args)
{
if(client == 0)
return Plugin_Handled;
if(CapMode_Stage == 0){
LoadStartMenu(client);
}
else
PrintToConsole(client, "[MCP] Captain Pick has already started");
return Plugin_Handled;
}
/*--------------------------------------------------------------------------------
/ Hooks
/-------------------------------------------------------------------------------*/
public Action:Event_Command_jointeam(client, const String:command[], argc)
{
return Plugin_Handled;
}
public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
if(CapMode_Stage != 2)
return Plugin_Continue;
new client = GetClientOfUserId(GetEventInt(event, "userid"));
new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
new deathflags = GetEventInt(event, "death_flags");
new TFClassType:class = TF2_GetPlayerClass(client);
//If a Medic, not dead ringing, and didn't suicide
if (class == TFClass_Medic && !(deathflags & 32) && attacker != client){
CapMode_Stage = 3;
PLAYERS_SetCaptainClient(0, attacker);
PLAYERS_SetCaptainClient(1, client);
PickTeams();
}
else
PrintToChatAll("[CAP] PlayerDeath Test Fail");//TODO, see if a suicide with damage will win
return Plugin_Continue;
}
//TODO: Move player to team, when ready, otherwise it moves a player to team 2/3, not BLU/RED
//Change to OnClientPostAdminCheck
public Action:Event_PlayerActivate(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
new String:steamid[STEAMID_MAX] = "";
new bool:isAuth = IsClientAuthorized(client);
if(!PLAYERS_isHumanNotCap(client))
return Plugin_Continue;
if(isAuth){
GetClientAuthString(client, steamid, STEAMID_MAX);
for(new i = 0; i < 2; i++){
if(strcmp(steamid,PLAYERS_GetCaptainSteamID(i)) == 0){
PLAYERS_SetCaptainClient(i, client);
PLAYERS_SetCaptainSteamID(i, "");
return Plugin_Continue;
}
}
ChangeClientTeam(client, 1);
PLAYERS_GetClass(client);
}
else
PrintToChatAll("[CAP] Shit just got REAL ERROR");
return Plugin_Continue;
}
public Action:Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
new String:steamid[STEAMID_MAX] = "";
new bool:isAuth = IsClientAuthorized(client);
for(new i = 0; i < 2; i++){
if(client == PLAYERS_GetCaptainClient(i)){
if(isAuth){
GetClientAuthString(client, steamid, STEAMID_MAX);
PLAYERS_SetCaptainSteamID(i, steamid);
PrintToChatAll("Medic disconnected. Waiting 30 seconds for re-connect before getting a new medic");
//TODO: Create time for new medic
}
else{
new newmedic_client = GetRandomInt(1, CLIENT_LAST);
new othermedic_client;
if(i == 0)
othermedic_client = PLAYERS_GetCaptainClient(1);
else
othermedic_client = PLAYERS_GetCaptainClient(0);
while(!PLAYERS_isHumanNotCap(newmedic_client) && newmedic_client == othermedic_client)
{
newmedic_client = GetRandomInt(1, CLIENT_LAST);
}
PrintToChatAll("Un-Auth medic disconnected. New medic selected");
PLAYERS_SetCaptainClient(i , newmedic_client);
}
}
}
PLAYERS_ClearPlayer(client);
return Plugin_Continue;
}
/*--------------------------------------------------------------------------------
/ Menus
/-------------------------------------------------------------------------------*/
public Menu_Start(Handle:menu, MenuAction:action, client, param)
{
if (action == MenuAction_Select)
{
new String:info[INFO_NOTUSED_MAXLEN];
GetMenuItem(menu, param, info, INFO_NOTUSED_MAXLEN);
switch(param)
{
//Team Size
case 0:
{
//Create Load Menu Function in PLAYERS
}
//Pick Method
case 1:
{
//Create Load Menu Function in PLAYERS
}
//Labs
case 2:
{
//Create Load Menu Function in PLAYERS
}
//Start
case 3:
{
CapMode_Stage = 1;
PrintToChatAll("[CAP] Captain Mode Started...(0 Seconds)");
CreateTimer(0.0, Timer_BeginCapMode);
}
}
}
else if (action == MenuAction_End)
{
CloseHandle(menu);
}
}
public Menu_CaptainPick(Handle:menu, MenuAction:action, client, param)
{
if (action == MenuAction_Select)
{
new String:info[INFO_NOTUSED_MAXLEN];
GetMenuItem(menu, param, info, INFO_NOTUSED_MAXLEN);
LoadCaptainPickClassMenu(client, param);
}
else if (action == MenuAction_End)
{
CloseHandle(menu);
}
}
public Menu_CaptainPickClass(Handle:menu, MenuAction:action, client, param)
{
if (action == MenuAction_Select)
{
new String:idString[ISTRING_MAXLEN];
GetMenuItem(menu, param, idString, ISTRING_MAXLEN);
new player_client = StringToInt(idString);
new captain_id;
if(client == PLAYERS_GetCaptainClient(0))
captain_id = 0;
else if(client == PLAYERS_GetCaptainClient(1))
captain_id = 1;
else{
PrintToChat(client, "[CAP] You're not a captain");
return;
}
if(!PLAYERS_CaptainPicksPlayer(captain_id,player_client))
PrintToChat(client, "[CAP] It's not your turn to choose a player");
}
else if (action == MenuAction_Cancel)
{
LoadCaptainPickMenu(client);
}
else if (action == MenuAction_End)
{
CloseHandle(menu);
}
}
public Menu_ChooseClass(Handle:menu, MenuAction:action, client, param)
{
if (action == MenuAction_Select)
{
new String:info[INFO_NOTUSED_MAXLEN];
GetMenuItem(menu, param, info, INFO_NOTUSED_MAXLEN);
new class = param;
if(class == PLAYER_CLASS_MAX)
PLAYERS_ClassPicked(client);
else
{
PLAYERS_ToggleClass(client,class);
LoadChooseClassMenu(client);
}
}
else if (action == MenuAction_End)
{
CloseHandle(menu);
}
}
/*--------------------------------------------------------------------------------
/ Timers
/-------------------------------------------------------------------------------*/
public Action:Timer_BeginCapMode(Handle:timer)
{
PLAYERS_Reset();
PLAYERS_GetClientEnd();
for(new i = 1; i <= CLIENT_LAST ;i++){
if(PLAYERS_isHumanNotCap(i))
PLAYERS_GetClass(i);
}
}
/*--------------------------------------------------------------------------------
/ Load Menu Functions
/-------------------------------------------------------------------------------*/
LoadStartMenu(client)
{
new Handle:menu = CreateMenu(Menu_Start);
AddMenuItem(menu, "", "Team Size");
AddMenuItem(menu, "", "Pick Method");
AddMenuItem(menu, "", "Labs");
AddMenuItem(menu, "", "Start");
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, 0);
}
LoadCaptainPickMenu(client)
{
new String:iString[ISTRING_MAXLEN];
new String:menu_i[MENUITEM_NAME_MAXLEN];
new String:menuitems[PLAYER_CLASS_MAX][MENUITEM_NAME_MAXLEN];
new String:classcountString[ISTRING_MAXLEN];
new class_count = 0;
new Handle:menu = CreateMenu(Menu_CaptainPick);
PLAYERS_GetClassStrings(menuitems);
for(new iClass = 0; iClass < PLAYER_CLASS_MAX; iClass++){
if(PLAYER_isClassEnabled(iClass)){
for(new iClient = 0; iClient <= CLIENT_LAST; iClient++){
if(PLAYERS_isHumanNotCap(iClient) && PLAYERS_isClass(iClient, iClass))
class_count++;
}
menu_i = "[";
IntToString(class_count, classcountString, ISTRING_MAXLEN);
StrCat(menu_i, MENUITEM_NAME_MAXLEN, classcountString);
StrCat(menu_i, 2, "]");
StrCat(menu_i, MENUITEM_NAME_MAXLEN, menuitems[iClass]);
IntToString(iClass, iString, ISTRING_MAXLEN);
AddMenuItem(menu, iString, menu_i);
}
}
IntToString(PLAYER_CLASS_MAX, iString, ISTRING_MAXLEN);
AddMenuItem(menu, iString, "All");
SetMenuExitButton(menu, false);
DisplayMenu(menu, client, 0);
}
LoadCaptainPickClassMenu(client, class)
{
new String:name[PLAYER_NAME_MAXLEN];
new String:istring[ISTRING_MAXLEN];
new Handle:menu = CreateMenu(Menu_CaptainPickClass);
if(class == PLAYER_CLASS_MAX){
for(new i = 1; i <= CLIENT_LAST ;i++){
if(PLAYERS_isHumanNotCap(i)){
GetClientName(i, name, PLAYER_NAME_MAXLEN);
IntToString(i,istring,ISTRING_MAXLEN);
AddMenuItem(menu, istring, name);
}
}
}
else{
for(new i = 1; i <= CLIENT_LAST ;i++){
if(PLAYERS_isHumanNotCap(i)){
if(PLAYERS_isClass(i, class)){
GetClientName(i, name, PLAYER_NAME_MAXLEN);
IntToString(i,istring,ISTRING_MAXLEN);
AddMenuItem(menu, istring, name);
}
}
}
}
SetMenuTitle(menu, "Pick a Player");
SetMenuExitButton(menu, false);
SetMenuExitBackButton(menu, true);
DisplayMenu(menu, client, 0);
}
LoadChooseClassMenu(client)
{
new String:iString[ISTRING_MAXLEN];
new String:menu_i[MENUITEM_NAME_MAXLEN];
new String:menuitems[PLAYER_CLASS_MAX][MENUITEM_NAME_MAXLEN];
new Handle:menu = CreateMenu(Menu_ChooseClass);
PLAYERS_GetClassStrings(menuitems);
//TODO: FIX
for(new i = 0; i < PLAYER_CLASS_MAX; i++){
if(PLAYER_isClassEnabled(i)){
if(PLAYERS_isClass(client,i))
menu_i = "[X]";
else
menu_i = "[ ]";
StrCat(menu_i, MENUITEM_NAME_MAXLEN, menuitems[i]);
IntToString(i, iString, ISTRING_MAXLEN);
AddMenuItem(menu, iString, menu_i);
}
}
IntToString(PLAYER_CLASS_MAX, iString, ISTRING_MAXLEN);
AddMenuItem(menu, iString, "Done");
SetMenuTitle(menu, "What class do you want to play?");
SetMenuExitButton(menu, false);
DisplayMenu(menu, client, 0);
}
/*--------------------------------------------------------------------------------
/ Helper Functions
/-------------------------------------------------------------------------------*/
CaptainMode()
{
{
new player_count = 0;
for(new i = 1; i <= CLIENT_LAST ;i++){
if(PLAYERS_isHumanNotCap(i))
player_count++;
}
if(player_count < 2){
StopPlugin("Not enough players to play, need atleast 2");
return;
}
}
if(CapMode_Stage != 1)
return;
CapMode_Stage = 2;
//Add Hooks
//-prevent a players moving out of their designated team
//-move joined players to designated team
//-save captions steamid that leave the server
if(!AddCommandListener(Event_Command_jointeam, "jointeam"))
SetFailState("Could not hook an event.");
if(!HookEventEx("player_activate", Event_PlayerActivate, EventHookMode_Post))
SetFailState("Could not hook an event.");
if(!HookEventEx("player_disconnect", Event_PlayerDisconnect, EventHookMode_Pre))
SetFailState("Could not hook an event.");
{
//Randomly pick 2 medics, 1 for each team
new medic_client[2];
medic_client[0] = GetRandomInt(1, CLIENT_LAST);
while(!PLAYERS_isHumanNotCap(medic_client[0])){
medic_client[0] = GetRandomInt(1, CLIENT_LAST);
}
medic_client[1] = GetRandomInt(1, CLIENT_LAST);
while(!PLAYERS_isHumanNotCap(medic_client[1]) || medic_client[0] == medic_client[1])
{
medic_client[1] = GetRandomInt(1, CLIENT_LAST);
}
//Distribute the medics for a fight to see who picks first
for(new i = 0; i < 2; i++){
PLAYERS_SetCaptainClient(i, medic_client[i]);
TF2_SetPlayerClass(medic_client[i], TFClass_Medic);
}
}
//All players that were'nt selected to be medic get moved to spectate
for(new i = 1; i <= CLIENT_LAST ;i++){
if(PLAYERS_isHumanNotCap(i))
ChangeClientTeam(i, 1);
}
//Restart Round
PrintToChatAll("[CAP] Medic Fight Begins");
ServerCommand("mp_restartgame 1");
//Add Hook to look for Medic death
if(!HookEventEx("player_death", Event_PlayerDeath, EventHookMode_Post))
SetFailState("Could not hook an event.");
}
PickTeams()
{
UnhookEvent("player_death", Event_PlayerDeath, EventHookMode_Post);
//Winning Medic goes to BLU team
ChangeClientTeam(PLAYERS_GetCaptainClient(0), 3);
ChangeClientTeam(PLAYERS_GetCaptainClient(1), 2);
LoadCaptainPickMenu(PLAYERS_GetCaptainClient(0));
}
//TODO: Ability to stop or abort plugin
StopPlugin(String:msg[CHATALL_MSG_MAX])
{
new String:mcp_msg[7] = "[MCP] ";
StrCat(mcp_msg, CHATALL_MSG_MAX + 7, msg);
PrintToChatAll(mcp_msg);
PrintToChatAll("[MCP] Plugin Stopped");
//TODO: Find a better test to see if hooks are enabled
if(CapMode_Stage > 1){
RemoveCommandListener(Event_Command_jointeam, "jointeam");
UnhookEvent("player_activate", Event_PlayerActivate, EventHookMode_Post);
UnhookEvent("player_disconnect", Event_PlayerDisconnect, EventHookMode_Pre);
}
if(CapMode_Stage == 2){
UnhookEvent("player_death", Event_PlayerDeath, EventHookMode_Post);
}
PLAYERS_Reset();
CapMode_Stage = 0;
}
/*--------------------------------------------------------------------------------
* TODO:
* -Add Hooks to players that leave and join game
* -Add ability to abort plugin
* -Add command to re-pick class
* -Add a timer, so waiting players don't have to wait too long
* --Need to make fix for classpicked in-case someone joins later or enters class late
* -On map end, stop plugin
/-------------------------------------------------------------------------------*/