-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepTechHighlander.sp
More file actions
1544 lines (1293 loc) · 42.8 KB
/
StepTechHighlander.sp
File metadata and controls
1544 lines (1293 loc) · 42.8 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sourcemod>
#include <timers>
#include <tf2_stocks>
#include <NamePool.sp>
#include <str_util.sp>
#include <MapRule.sp>
#include <SetLocation.sp>
#include <Math.sp>
#define NAME "StepTechHighlander"
#define VERSION "1.0.0-SNAPSHOT"
#define MAX_MAP_NAME_LENGTH 32
#define MAX_CMD_LENGTH 64
#define MAX_CMD_DESC_LENGTH 255
#define MAX_COMMANDS 16
#define MAX_TEAM_ID_LENGTH 16
#define COMP_SIZE 9
#define MAX_CLASS_ID_LENGTH 16
#define RESTART_TOURNAMENT_CMD "mp_tournament_restart\n"
#define ANY_CLASS_INDEX 9
#define CLASSES_SIZE 9
#define LOBBY_MODE_COUNT_DOWN 10
#define LOBBY_MODE_INFO_INTERVAL 5.0
#define ACK_COUNT_DOWN 5
#define MAX_SCORE 100
#define MAP_RULE_CHECK_TICK 0.5
public Plugin myinfo = {
name = NAME,
author = "Steppy",
description = "Enables highlander mode",
version = VERSION,
url = "-"
}
Class classes[CLASSES_SIZE];
ConVar cvarTeamSize = null;
ConVar cvarMpTournament = null;
ConVar cvarMpRestartGame = null;
ConVar cvarTfBotQuota = null;
ConVar cvarTfBotDifficulty = null;
CommandInfo commands[MAX_COMMANDS];
int commandsLen = 0;
ReadyState readyState;
NamePool namePool;
bool mapRuleDebug[MAXPLAYERS];
int fullRoundsPlayed = 0;
int blueRoundScore = 0;
public void OnPluginStart() {
LogMessage("Starting %s v%s", NAME, VERSION);
DefClass(TFClass_Scout, "scout", true);
DefClass(TFClass_Soldier, "soldier");
DefClass(TFClass_Pyro, "pyro");
DefClass(TFClass_DemoMan, "demoman");
DefClass(TFClass_Heavy, "heavyweapons");
DefClass(TFClass_Engineer, "engineer");
DefClass(TFClass_Medic, "medic");
DefClass(TFClass_Sniper, "sniper");
DefClass(TFClass_Spy, "spy");
//con vars
cvarTeamSize = CreateConVar("st_teamsize", "9", "To how many players each team should have", FCVAR_NOTIFY, true, 0.0);
AutoExecConfig();
cvarMpTournament = FindConVar("mp_tournament");
cvarMpTournament.AddChangeHook(Event_MpTournamentChange);
cvarMpTournament.IntValue = 1;
cvarMpRestartGame = FindConVar("mp_restartgame");
cvarTfBotQuota = FindConVar("tf_bot_quota");
cvarTfBotDifficulty = FindConVar("tf_bot_difficulty");
//some con var settings
FindConVar("tf_bot_keep_class_after_death").IntValue = 1; //make sure bots don't just switch their class, since we manage that
FindConVar("mp_autoteambalance").IntValue = 0; //make sure bots stay in the team we assign them
FindConVar("tf_bot_reevaluate_class_in_spawnroom").IntValue = 0; //don't suicide
//commands
RegCommand("st_help", Command_Help, "- Displays a command overview");
RegCommand("st_map", Command_Map, "[mapname]|random - Changes the map");
RegCommand("st_team_size", Command_TeamSize, "[teamsize] - Set how many players each team should have");
RegCommand("st_tp_bots", Command_Teleport_Bots, "- Teleports all bots of your team to your location");
RegCommand("st_lobby", Command_Lobby, "<clean> - Switch to lobby mode. Use clean lobby to kick all bots.");
RegCommand("eggseck", Command_Eggseck, "start|pause - Starts or pauses the match (Bitte hau mich nicht, Nepa, der Command war Craftis Idee ^^')");
RegCommand("st_update_team_comp", Command_UpdateTeamComp, "- Update the classes of the bots");
RegCommand("st_difficulty", Command_Difficulty, "[0-3] - Change the bot difficulty");
RegCommand("st_restart", Command_Restart, "<[inSecs]> - Restarts the server in the specified number of seconds");
RegCommand("menu", Command_Menu, "- Open a quick access menu for some of the commands");
RegCommand("st_set", Command_Set, "[name] - Give your current position a name to reference it later in another command or such");
RegAdminCmd("st_namepool_add", Command_NamePoolAdd, ADMFLAG_GENERIC, "[class name]|[class id]|any [names...] - add names to the namepool");
RegAdminCmd("st_test", Command_Test, ADMFLAG_GENERIC, "The usual test command");
RegAdminCmd("map_rule", Command_MapRule, ADMFLAG_GENERIC, "Manage map rules for the current map");
//listener
HookEvent("tournament_stateupdate", Event_ReadyUp);
HookEvent("teamplay_round_win", Event_TeamplayRoundWin);
HookEvent("teamplay_point_captured", Event_ControlPointCapped);
HookEvent("player_spawn", Event_PlayerSpawn);
ServerCommand("exec namepool\n"); //reload namepool
StartLobbyMode(.clean = true);
int playerCount = GetPlayerCount();
if (playerCount > 0) {
//in case we got reloaded
CreateTimer(0.0, Timer_UpdateTeamComp); //delay, so we don't interfere with clean lobby
}
//timers
CreateTimer(LOBBY_MODE_INFO_INTERVAL, Timer_LobbyModeInfo, _, TIMER_REPEAT);
CreateTimer(MAP_RULE_CHECK_TICK, Timer_MapRuleCheck, _, TIMER_REPEAT);
}
public void OnClientConnected(int client) {
if (IsBot(client)) return;
if (GetPlayerCount() == 1) {
//first player to join -> clean lobby start
StartLobbyMode(.clean = true);
}
}
public void OnClientDisconnect(int client) {
if (IsBot(client)) return;
if (GetPlayerCount() == 1) {
//last player to quit
StartLobbyMode(.clean = true);
} else {
//update team comp after client disconnected
CreateTimer(0.0, Timer_UpdateTeamComp);
}
}
public void OnMapStart() {
blueRoundScore = 0;
StartLobbyMode(.clean = true);
LoadMapRules();
}
int GetPlayerCount() {
int count = 0;
for (int client = 1; client < MaxClients; client++) {
if (!IsClientConnected(client)) continue;
if (IsBot(client)) continue;
count++;
}
return count;
}
void DefClass(TFClassType classtype, char[] id, bool clean = false) {
static int index = 0;
if (clean) {
index = 0;
}
classes[index].classType = classtype;
strcopy(classes[index].id, MAX_CLASS_ID_LENGTH, id);
classes[index].index = index;
index++;
}
Class Wrap(TFClassType classtype) {
for (int i = 0; i < sizeof(classes); i++) {
if (classes[i].classType == classtype) {
return classes[i];
}
}
ThrowError("Invalid class type");
//just here to please the compiler
Class c;
return c;
}
void RegCommand(const char[] name, ConCmd executor, char[] description, int flags = 0) {
RegConsoleCmd(name, executor, description, flags);
CommandInfo info;
strcopy(info.name, sizeof(info.name), name);
strcopy(info.description, sizeof(info.description), description);
commands[commandsLen++] = info;
}
enum struct Class {
TFClassType classType;
char id[16];
int index;
}
enum struct CommandInfo {
char name[MAX_CMD_LENGTH];
char description[MAX_CMD_DESC_LENGTH];
}
void Log4All(int client, const char[] msg, any ...) {
char clientName[MAX_NAME_LENGTH];
GetClientName(client, clientName, sizeof(clientName));
char prefixFormat[] = "[INFO](%s) ";
char prefix[MAX_NAME_LENGTH + sizeof(prefixFormat)];
Format(prefix, sizeof(prefix), prefixFormat, clientName);
int messageSize = strlen(msg) + 255;
char[] message = new char[messageSize];
VFormat(message, messageSize, msg, 3);
PrintToChatAll("%s%s", prefix, message);
PrintToServer("%s%s", prefix, message);
}
int GetClientsInTeam(TFTeam team, int[] buf, int bufSize) {
int teamSize = 0;
for (int client = 1; client <= MaxClients; client++) {
if (teamSize >= bufSize) break;
if (IsClientInGame(client) && TF2_GetClientTeam(client) == team) {
buf[teamSize++] = client;
}
}
return teamSize;
}
bool isBotOnlyTeam(TFTeam team) {
int[] teamClients = new int[MaxClients];
int teamSize = GetClientsInTeam(team, teamClients, MaxClients);
for (int i = 0; i < teamSize; i++) {
int client = teamClients[i];
if (!IsBot(client)) {
return false;
}
}
return true;
}
/**
* Checks whether the provided client is a bot
*
* @return true if the client is a bot, false if it is a player, an invalid client or not connected
*/
bool IsBot(int client) {
return client >= 0 && client <= MaxClients && IsClientConnected(client) && IsFakeClient(client);
}
int GetClientByName(const char[] name) {
for (int client = 1; client < MaxClients; client++) {
if (!IsClientConnected(client)) continue;
char clientName[MAX_NAME_LENGTH];
GetClientName(client, clientName, sizeof(clientName));
if (strcmp(clientName, name) == 0) {
return client;
}
}
return 0;
}
//commands
Action Command_Set(int client, int args) {
if (args < 1) {
ReplyToCommand(client, "Please provide a name for your current location");
return Plugin_Handled;
}
if (!CheckIsPlayer(client)) {
return Plugin_Handled;
}
char name[MAX_LOCATION_NAME_SIZE];
GetCmdArg(1, name, sizeof(name));
AddSetLocation(client, name);
ReplyToCommand(client, "Successfully set location '%s' at your current position", name);
return Plugin_Handled;
}
Action Command_MapRule(int client, int args) {
Command_MapRule0(client, args);
return Plugin_Handled;
}
void Command_MapRule0(int client, int args) {
//no args
if (args == 0) {
ReplyToCommand(client, "Manage the map rules for the current map. Usage:");
ShowMapRuleCommandUsage(client);
return;
}
char subCommand[32];
GetCmdArg(1, subCommand, sizeof(subCommand));
//list
if (streq("list", subCommand)) {
if (activeMapRulesLen == 0) {
ReplyToCommand(client, "Currently there are no maprules defined");
return;
}
ReplyToCommand(client, "ID | active score range | activation time | action | description");
for (int i = 0; i < activeMapRulesLen; i++) {
MapRule rule;
rule = activeMapRules[i];
char action[32];
switch (rule.action) {
case MapRuleAction_Kill: action = "kill";
case MapRuleAction_Tp: action = "tp";
}
ReplyToCommand(client, "%d | %d..%d | %.1f sec | %s | %s", rule.id, rule.scoreRange.start, rule.scoreRange.end, rule.activationTime, action, rule.description);
}
return;
}
//delete
if (streq("delete", subCommand)) {
if (args < 2) {
ReplyToCommand(client, "Missing argument: id");
return;
}
int id;
if (!GetCmdArgIntEx(2, id)) {
ReplyToCommand(client, "Please provide the id of the map rule you want to delete as whole number");
return;
}
DeleteMapRule(id);
ReplyToCommand(client, "Deleted map rule with id %d", id);
return;
}
//add
if (streq("add", subCommand)) {
if (!CheckIsPlayer(client)) {
return;
}
int argNum = 2;
if (args < argNum) {
//show detailed overview:
ReplyToCommand(client, "Defines a map rule for the current map which is active only in the provided score range. \
Requires two locations (can be set over !st_set) which will define a circle as area of the map rule. activationTime is the number of seconds a \
client has to spend inside the area of the rule for it to get enforced.");
ReplyToCommand(client, "Examples: ");
ReplyToCommand(client, "!map_rule add 0..2 a->b 5.0 kill Circle_of_death - Defines a map rule which is active as long as blue has captured \
between 0 and 2 control points this round, with a circular area from a to b which kills all bots which spend 5 seconds inside");
ReplyToCommand(client, "!map_rule add 3 a->b 3.0 tp c Gate_5 - When blue has scored exactly three points, teleport all clients to location c after 3 seconds");
return;
}
//score range
Range scoreRange;
char scoreRangeArg[16];
GetCmdArg(argNum++, scoreRangeArg, sizeof(scoreRangeArg));
int index = IndexOf(scoreRangeArg, "..");
if (index < 0) {
int value;
if (!StringToIntEx(scoreRangeArg, value)) {
ReplyToCommand(client, "Please provide a single number (3) or a range (2..7) as score range");
return;
}
scoreRange.start = value;
scoreRange.end = value;
} else {
char arg[sizeof(scoreRangeArg)];
strcopy(arg, index + 1, scoreRangeArg);
if (strlen(arg) == 0) {
scoreRange.start = 0;
} else if (!StringToIntEx(arg, scoreRange.start)) {
ReplyToCommand(client, "Invalid range start: '%s' is not a whole number", arg);
return;
}
strcopy(arg, sizeof(arg), scoreRangeArg[index + 2]);
if (strlen(arg) == 0) {
scoreRange.end = MAX_SCORE;
} else if (!StringToIntEx(arg, scoreRange.end)) {
ReplyToCommand(client, "Invalid range end: '%s' is not a whole number", arg);
return;
}
}
//location range
if (args < argNum) {
ReplyToCommand(client, "Missing argument: locationRange");
return;
}
char locationRangeArg[2 * MAX_LOCATION_NAME_SIZE + 2];
GetCmdArg(argNum++, locationRangeArg, sizeof(locationRangeArg));
int arrowIndex = IndexOf(locationRangeArg, "->");
if (arrowIndex < 0) {
ReplyToCommand(client, "Please provide a valid location range (eg.: loc1->loc2)");
return;
}
char locationName[MAX_LOCATION_NAME_SIZE];
strcopy(locationName, arrowIndex + 1, locationRangeArg);
SetLocation locA;
if (!GetSetLocation(locA, client, locationName)) {
ReplyToCommand(client, "Invalid location name: You have not set a location with the name '%s'", locationName);
return;
}
strcopy(locationName, sizeof(locationName), locationRangeArg[arrowIndex + 2]);
SetLocation locB;
if (!GetSetLocation(locB, client, locationName)) {
ReplyToCommand(client, "Invalid location name: You have not set a location with the name '%s'", locationName);
return;
}
float center[3];
center[0] = (locA.location[0] + locB.location[0]) / 2; //calculate the center of the two points
center[1] = (locA.location[1] + locB.location[1]) / 2;
center[2] = (locA.location[2] + locB.location[2]) / 2;
float dx = locA.location[0] - locB.location[0];
float dz = locA.location[1] - locB.location[1];
LogMessage("dx: %f", dx);
LogMessage("dz: %f", dz);
float distanceSquared = dx * dx + dz * dz;
float distance = SquareRoot(distanceSquared);
if (distance < 0.1) {
ReplyToCommand(client, "Warning: locations are very close to each other. It is unlikely that this map rule will be useful");
}
float radius = distance / 2;
LogMessage("distanceSquared: %f", distanceSquared);
LogMessage("distance: %f", distance);
LogMessage("radius: %f", radius);
//activation time
if (args < argNum) {
ReplyToCommand(client, "Missing argument: activation time");
return;
}
float activationTime;
if (!GetCmdArgFloatEx(argNum++, activationTime)) {
ReplyToCommand(client, "Please provide the activation time in seconds, e.g.: 3.0 for three seconds");
return;
}
//action
if (args < argNum) {
ReplyToCommand(client, "Missing argument: action");
return;
}
MapRuleAction action;
char actionArg[16];
GetCmdArg(argNum++, actionArg, sizeof(actionArg));
if (streq("kill", actionArg)) {
action = MapRuleAction_Kill;
} else if (streq("tp", actionArg)) {
action = MapRuleAction_Tp;
} else {
ReplyToCommand(client, "Invalid action '%s'. Please use 'kill' or 'tp'", actionArg);
return;
}
float teleportLocation[3];
switch (action) {
case MapRuleAction_Tp: {
if (args < argNum) {
ReplyToCommand(client, "Missing argument: teleport location");
return;
}
char teleportLocationName[MAX_LOCATION_NAME_SIZE];
GetCmdArg(argNum++, teleportLocationName, sizeof(teleportLocationName));
SetLocation setLocation;
if (!GetSetLocation(setLocation, client, teleportLocationName)) {
ReplyToCommand(client, "Invalid location name: You have not set a location with the name '%s'", teleportLocationName);
return;
}
teleportLocation = setLocation.location;
}
}
//description
if (args < argNum) {
ReplyToCommand(client, "Missing argument: description");
return;
}
char description[MAX_DESCRIPTION_SIZE];
GetCmdArg(argNum++, description, sizeof(description));
//build it all together
MapRule rule;
GetCurrentMap(rule.map, sizeof(rule.map));
GetClientAbsOrigin(client, rule.center);
rule.scoreRange = scoreRange;
rule.center = center;
rule.radius = radius;
rule.activationTime = activationTime;
rule.action = action;
rule.targetLocation = teleportLocation;
rule.description = description;
AddMapRule(rule);
ReplyToCommand(client, "map rule has been added with id %d", rule.id);
return;
}
//tp
if (streq("tp", subCommand)) {
if (!CheckIsPlayer(client)) {
return;
}
//id
if (args < 2) {
ReplyToCommand(client, "Missing argument: id");
return;
}
int id;
if (!GetCmdArgIntEx(2, id)) {
ReplyToCommand(client, "Please provide the id of the map rule you want to teleport to as a whole number");
return;
}
bool target = false;
if (args >= 3) {
char param[16];
GetCmdArg(3, param, sizeof(param));
if (streq("--target", param)) {
target = true;
} else {
ReplyToCommand(client, "Invalid parameter '%s'", param);
return;
}
}
MapRule rule;
bool found = false;
for (int i = 0; i < activeMapRulesLen; i++) {
rule = activeMapRules[i];
if (rule.id == id) {
found = true;
break;
}
}
if (!found) {
ReplyToCommand(client, "There is no map rule with the id %d on the current map!", id);
return;
}
if (target) {
if (rule.action != MapRuleAction_Tp) {
ReplyToCommand(client, "rule %d has no target location to teleport to", id);
return;
}
TeleportEntity(client, rule.targetLocation);
} else {
TeleportEntity(client, rule.center);
}
return;
}
//debug
if (streq("debug", subCommand)) {
if (!CheckIsPlayer(client)) {
return;
}
bool enable = !mapRuleDebug[client];
mapRuleDebug[client] = enable;
if (enable) {
ReplyToCommand(client, "Debug mode now enabled. You will now get a notification if a map rule would apply to you.");
} else {
ReplyToCommand(client, "Debug mode now disabled");
}
return;
}
ReplyToCommand(client, "Invalid sub command. Use one of the following:");
ShowMapRuleCommandUsage(client);
}
bool CheckIsPlayer(int client) {
if (client == 0) {
ReplyToCommand(client, "This command may only be executed by a player and not the console");
return false;
}
return true;
}
void ShowMapRuleCommandUsage(int client) {
ReplyToCommand(client, "list - List all map rules for the current map");
ReplyToCommand(client, "delete [id] - Delete a map rule with the given id");
ReplyToCommand(client, "add [scoreRange] [locationRange] [activationTime] [action] <[action parameters]> [description] - Add a map rule. Run without arguments for detailed syntax.");
ReplyToCommand(client, "tp [id] <--target> - Teleport to the location of a map rule, or it's target location");
ReplyToCommand(client, "debug - Toggle debug mode. Adds a visual hint which map rule would apply to your client");
}
Action Command_Test(int client, int args) {
ReplyToCommand(client, "Blue round score is %d", blueRoundScore);
return Plugin_Handled;
}
Action Command_Help(int client, int args) {
ReplyToCommand(client, "%s v%s", NAME, VERSION);
ReplyToCommand(client, "Values in [] need to be replaced, values in <> are optional. '!' is only required in chat, not in console.")
for (int i = 0; i < commandsLen; i++) {
ReplyToCommand(client, "!%s %s", commands[i].name, commands[i].description);
}
return Plugin_Handled;
}
Action Command_TeamSize(int client, int args) {
if (args == 0) {
ReplyToCommand(client, "Current team size is %d", cvarTeamSize.IntValue);
return Plugin_Handled;
}
int newSize;
if (GetCmdArgIntEx(1, newSize) && newSize >= 0) {
int prevSize = cvarTeamSize.IntValue;
cvarTeamSize.IntValue = newSize;
Log4All(client, "set team size from %d to %d", prevSize, newSize);
UpdateTeamComposition(.playersPerTeam = newSize);
} else {
ReplyToCommand(client, "team size must be a positive integer");
}
return Plugin_Handled;
}
char nextMap[MAX_MAP_NAME_LENGTH];
Handle mapChangeTimer = null;
bool EqualsRandom(const char[] str) {
return strcmp(str, "random", false) == 0;
}
Action Command_Map(int client, int args) {
char mapName[MAX_MAP_NAME_LENGTH];
GetCmdArg(1, mapName, sizeof(mapName));
if (!(IsMapValid(mapName) || EqualsRandom(mapName))) {
ReplyToCommand(client, "Invalid map name: %s", mapName);
return Plugin_Handled;
}
Log4All(client, "changed map to %s", mapName);
if (mapChangeTimer != null) {
CloseHandle(mapChangeTimer);
}
nextMap = mapName;
mapChangeTimer = CreateTimer(1.0, Timer_ChangeMap, _, TIMER_REPEAT);
return Plugin_Handled;
}
Action Timer_ChangeMap(Handle timer) {
static int countDown = ACK_COUNT_DOWN;
if (countDown <= 0) {
if (EqualsRandom(nextMap)) {
ServerCommand("randommap\n");
} else {
ForceChangeLevel(nextMap, "player requested level change");
}
countDown = ACK_COUNT_DOWN;
mapChangeTimer = null;
return Plugin_Stop;
}
PrintCenterTextAll("map change in %d...", countDown);
countDown--;
return Plugin_Continue;
}
Action Command_Teleport_Bots(int client, int args) {
if (client == 0) {
ReplyToCommand(client, "Only players may execute that command!");
return Plugin_Handled;
}
TeleportBots(client, .log = true);
return Plugin_Handled;
}
Action Command_Restart(int client, int args) {
int inSecs = ACK_COUNT_DOWN;
if (args > 0) {
if (!GetCmdArgIntEx(1, inSecs)) {
ReplyToCommand(client, "First argument has to be a whole number of seconds");
return Plugin_Handled;
}
}
RestartServer(client, inSecs);
return Plugin_Handled;
}
void RestartServer(int client = -1, int countdown = 0) {
if (client >= 0) {
Log4All(client, "Restarted the server");
}
float delay = 1.0;
if (countdown == 0) {
delay = 0.0;
}
CreateTimer(delay, Timer_RestartServer, countdown, TIMER_REPEAT);
}
Action Timer_RestartServer(Handle handle, int inSecs) {
static int countDown = -1;
if (countDown < 0) {
countDown = inSecs;
}
if (countDown > 0) {
PrintCenterTextAll("server restart in %d...", countDown);
countDown--;
return Plugin_Continue;
}
countDown = -1;
char currentMap[MAX_MAP_NAME_LENGTH];
GetCurrentMap(currentMap, sizeof(currentMap));
ForceChangeLevel(currentMap, "server restart");
return Plugin_Stop;
}
void TeleportBots(int client, bool log = false, int executer = -1) {
TFTeam team = TF2_GetClientTeam(client);
int[] teamClients = new int[MaxClients];
int teamSize = GetClientsInTeam(team, teamClients, MaxClients);
float location[3];
GetClientAbsOrigin(client, location);
float angle[3];
GetClientAbsAngles(client, angle);
for (int i = 0; i < teamSize; i++) {
int bot = teamClients[i];
if (IsBot(bot)) {
TeleportEntity(bot, location, angle);
}
}
if (log) {
if (executer == -1) {
executer = client;
}
char teamColor[4];
GetTeamIdentifier(team, teamColor, sizeof(teamColor));
ToLowercase(teamColor);
char clientName[MAX_NAME_LENGTH];
GetClientName(client, clientName, sizeof(clientName));
Log4All(executer, "Teleported the %s bots to %s", teamColor, clientName);
}
}
Action Command_Lobby(int client, int args) {
bool clean = false;
if (args > 0) {
char arg[8];
GetCmdArg(1, arg, sizeof(arg));
if (strcmp(arg, "clean") == 0) {
clean = true;
} else {
ReplyToCommand(client, "Invalid parameter. Use 'clean' to switch to a clean lobby mode.");
return Plugin_Handled;
}
}
StartLobbyMode(client, clean);
return Plugin_Handled;
}
void StartLobbyMode(int client = 0, bool clean = false) {
if (clean) {
KickBots();
}
fullRoundsPlayed = 0;
blueRoundScore = 0;
cvarMpTournament.IntValue = 1;
ServerCommand(RESTART_TOURNAMENT_CMD);
Log4All(client, "Switched to lobby mode");
}
void KickBots() {
ServerCommand("tf_bot_kick all\n");
cvarTfBotQuota.IntValue = 0; //make sure they don't fill up again
}
bool IsLobbyModeActive() {
return cvarMpTournament.IntValue == 1;
}
Action Timer_LobbyModeInfo(Handle handle) {
if (IsLobbyModeActive()) {
PrintHintTextToAll("Lobby mode - ready up to start the round (F4) or use '!st_help' in chat for a command overview");
}
return Plugin_Continue;
}
enum struct ApplyingMapRule {
MapRule rule;
int since; //how long the rule applied to a client in ticks
}
Action Timer_MapRuleCheck(Handle handle) {
static ApplyingMapRule appliyingRules[MAXPLAYERS];
if (activeMapRulesLen == 0) {
return Plugin_Continue;
}
for (int client = 1; client <= MaxClients; client++) {
if (!IsClientInGame(client)) {
continue;
}
bool debugModeActive = mapRuleDebug[client];
//find applying map rules by location
MapRule[] rulesInRange = new MapRule[activeMapRulesLen];
int rulesInRangeLen = 0;
for (int i = 0; i < activeMapRulesLen; i++) {
MapRule rule;
rule = activeMapRules[i];
if (rule.appliesTo(client)) {
rulesInRange[rulesInRangeLen++] = rule;
}
}
ApplyingMapRule applyingRule;
applyingRule = appliyingRules[client];
//find active rule with smallest distance to player
MapRule rule;
bool found = false;
for (int i = 0; i < rulesInRangeLen; i++) {
MapRule candidate;
candidate = rulesInRange[i];
if (!candidate.scoreRange.contains(blueRoundScore)) {
continue;
}
if (found) {
float clientLocation[3];
GetClientAbsOrigin(client, clientLocation);
if (DistanceSquared(clientLocation, candidate.center) < DistanceSquared(clientLocation, rule.center)) {
rule = candidate;
}
} else {
found = true;
rule = candidate;
}
}
//update apply data
if (found) {
if (rule.id == applyingRule.rule.id) {
applyingRule.since++;
} else {
applyingRule.rule = rule;
applyingRule.since = 1;
}
} else {
applyingRule.since = 0;
}
if (debugModeActive) {
Panel panel = new Panel();
panel.SetTitle("map rule debug info");
char text[100];
int drawn = 0;
int activeRuleId = 0;
if (applyingRule.since > 0) {
activeRuleId = applyingRule.rule.id;
Format(text, sizeof(text), "active: %d | %s", activeRuleId, applyingRule.rule.description);
panel.DrawText(text);
panel.DrawText("---other rules---");
drawn += 2;
}
int drawBound = 8;
int numHiddenRules = rulesInRangeLen + (drawn / 2) - drawBound;
bool willFit = numHiddenRules <= 0;
if (!willFit) {
drawBound--;
}
for (int i = 0; i < rulesInRangeLen && drawn < drawBound; i++) {
MapRule r;
r = rulesInRange[i];
if (r.id == activeRuleId) {
continue;
}
char activeAtScore[6];
if (r.scoreRange.contains(blueRoundScore)) {
activeAtScore = "true";
} else {
activeAtScore = "false";
}
Format(text, sizeof(text), "%d | active at current score: %s | %s", r.id, activeAtScore, r.description);
panel.DrawText(text);
drawn++;
}
if (!willFit) {
Format(text, sizeof(text), "...and %d others...", numHiddenRules);
panel.DrawText(text);
}
if (drawn > 0) {
panel.Send(client, PanelHandler_MapRuleDebug, 1);
}
delete panel;
}
//enforce rule if needed
if (applyingRule.since > 0 && applyingRule.since * MAP_RULE_CHECK_TICK >= rule.activationTime) {
bool enforced = false;
switch (rule.action) {
case MapRuleAction_Kill: {
if (IsBot(client)) {
char botName[MAX_NAME_LENGTH];
GetClientName(client, botName, sizeof(botName));
ServerCommand("tf_bot_kill \"%s\"\n", botName);
enforced = true;
}
}
case MapRuleAction_Tp: {
TeleportEntity(client, rule.targetLocation);
enforced = true;
}
}
if (enforced) {
PrintToChat(client, "A map rule has been enforced on you");
LogMessage("enforced map rule %d on client %d", rule.id, client);
applyingRule.since = 0;
}
}
appliyingRules[client] = applyingRule; //store the changes
}
return Plugin_Continue;
}
int PanelHandler_MapRuleDebug(Menu menu, MenuAction action, int param1, int param2) {
return 0;
}
Action Command_Eggseck(int client, int args) {
char script[16];
GetCmdArg(1, script, sizeof(script));
if (strcmp(script, "start") == 0) {
StartMatch(client);
} else if (strcmp(script, "pause") == 0) {
StartLobbyMode(client);
} else {
ReplyToCommand(client, "Invalid parameter. Use start or pause.");
}
return Plugin_Handled;
}
Action Command_UpdateTeamComp(int client, int args) {
UpdateTeamComposition();
Log4All(client, "Updated team composition");
return Plugin_Handled;
}