Merge branch 'terencehill/bot_ai' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / cmd.qc
1 #include "cmd.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5
6 #include <common/command/_mod.qh>
7
8 #include "common.qh"
9 #include "vote.qh"
10
11 #include "../bot/api.qh"
12
13 #include "../campaign.qh"
14 #include "../cheats.qh"
15 #include "../client.qh"
16 #include "../clientkill.qh"
17 #include "../player.qh"
18 #include "../ipban.qh"
19 #include "../mapvoting.qh"
20 #include "../scores.qh"
21 #include "../teamplay.qh"
22
23 #include <server/mutators/_mod.qh>
24 #include <common/gamemodes/_mod.qh>
25
26 #ifdef SVQC
27         #include <common/vehicles/all.qh>
28 #endif
29
30 #include <common/constants.qh>
31 #include <common/deathtypes/all.qh>
32 #include <common/effects/all.qh>
33 #include <common/mapinfo.qh>
34 #include <common/notifications/all.qh>
35 #include <common/physics/player.qh>
36 #include <common/teams.qh>
37 #include <common/util.qh>
38 #include <common/mapobjects/triggers.qh>
39
40 #include <common/minigames/sv_minigames.qh>
41
42 #include <common/monsters/_mod.qh>
43 #include <common/monsters/sv_spawn.qh>
44 #include <common/monsters/sv_monsters.qh>
45
46 #include <lib/warpzone/common.qh>
47
48 // =========================================================
49 //  Server side networked commands code, reworked by Samual
50 //  Last updated: December 28th, 2011
51 // =========================================================
52
53 bool SV_ParseClientCommand_floodcheck(entity this)
54 {
55         entity store = IS_CLIENT(this) ? CS(this) : this; // unfortunately, we need to store these on the client initially
56
57         if (!timeout_status)  // not while paused
58         {
59                 if (time <= (store.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
60                 {
61                         store.cmd_floodcount += 1;
62                         if (store.cmd_floodcount > autocvar_sv_clientcommand_antispam_count)   return false;  // too much spam, halt
63                 }
64                 else
65                 {
66                         store.cmd_floodtime = time;
67                         store.cmd_floodcount = 1;
68                 }
69         }
70         return true;  // continue, as we're not flooding yet
71 }
72
73
74 // =======================
75 //  Command Sub-Functions
76 // =======================
77
78 void ClientCommand_autoswitch(entity caller, int request, int argc)
79 {
80         switch (request)
81         {
82                 case CMD_REQUEST_COMMAND:
83                 {
84                         if (argv(1) != "")
85                         {
86                                 CS(caller).autoswitch = InterpretBoolean(argv(1));
87                                 sprint(caller, strcat("^1autoswitch is currently turned ", (CS(caller).autoswitch ? "on" : "off"), ".\n"));
88                                 return;
89                         }
90                 }
91
92                 default:
93                         sprint(caller, "Incorrect parameters for ^2autoswitch^7\n");
94                 case CMD_REQUEST_USAGE:
95                 {
96                         sprint(caller, "\nUsage:^3 cmd autoswitch selection\n");
97                         sprint(caller, "  Where 'selection' controls if autoswitch is on or off.\n");
98                         return;
99                 }
100         }
101 }
102
103 void ClientCommand_clientversion(entity caller, int request, int argc)  // internal command, used only by code
104 {
105         switch (request)
106         {
107                 case CMD_REQUEST_COMMAND:
108                 {
109                         if (argv(1) != "")
110                         {
111                                 if (IS_CLIENT(caller))
112                                 {
113                                         CS(caller).version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
114
115                                         if (CS(caller).version < autocvar_gameversion_min || CS(caller).version > autocvar_gameversion_max)
116                                         {
117                                                 CS(caller).version_mismatch = true;
118                                                 ClientKill_TeamChange(caller, -2);  // observe
119                                         }
120                                         else if (autocvar_g_campaign || autocvar_g_balance_teams)
121                                         {
122                                                 // JoinBestTeam(caller, false, true);
123                                         }
124                                         else if (teamplay && !autocvar_sv_spectate && !(Player_GetForcedTeamIndex(caller) > 0))
125                                         {
126                                                 TRANSMUTE(Observer, caller);  // really?
127                                                 stuffcmd(caller, "menu_showteamselect\n");
128                                         }
129                                 }
130
131                                 return;
132                         }
133                 }
134
135                 default:
136                         sprint(caller, "Incorrect parameters for ^2clientversion^7\n");
137                 case CMD_REQUEST_USAGE:
138                 {
139                         sprint(caller, "\nUsage:^3 cmd clientversion version\n");
140                         sprint(caller, "  Where 'version' is the game version reported by caller.\n");
141                         return;
142                 }
143         }
144 }
145
146 void ClientCommand_mv_getpicture(entity caller, int request, int argc)  // internal command, used only by code
147 {
148         switch (request)
149         {
150                 case CMD_REQUEST_COMMAND:
151                 {
152                         if (argv(1) != "")
153                         {
154                                 if (intermission_running) MapVote_SendPicture(caller, stof(argv(1)));
155
156                                 return;
157                         }
158                 }
159
160                 default:
161                         sprint(caller, "Incorrect parameters for ^2mv_getpicture^7\n");
162                 case CMD_REQUEST_USAGE:
163                 {
164                         sprint(caller, "\nUsage:^3 cmd mv_getpicture mapid\n");
165                         sprint(caller, "  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
166                         return;
167                 }
168         }
169 }
170
171 void ClientCommand_wpeditor(entity caller, int request, int argc)
172 {
173         switch (request)
174         {
175                 case CMD_REQUEST_COMMAND:
176                 {
177                         if (!autocvar_g_waypointeditor)
178                         {
179                                 sprint(caller, "ERROR: this command works only if the waypoint editor is on\n");
180                                 return;
181                         }
182
183                         if (argv(1) != "")
184                         {
185                                 if (argv(1) == "spawn")
186                                 {
187                                         if (!IS_PLAYER(caller))
188                                                 sprint(caller, "ERROR: this command works only if you are player\n");
189                                         else
190                                                 waypoint_spawn_fromeditor(caller);
191                                 }
192                                 else if (argv(1) == "remove")
193                                 {
194                                         if (!IS_PLAYER(caller))
195                                                 sprint(caller, "ERROR: this command works only if you are player\n");
196                                         else
197                                                 waypoint_remove_fromeditor(caller);
198                                 }
199                                 else if (argv(1) == "unreachable")
200                                 {
201                                         if (!IS_PLAYER(caller))
202                                                 sprint(caller, "ERROR: this command works only if you are player\n");
203                                         else
204                                                 waypoint_unreachable(caller);
205                                 }
206                                 else if (argv(1) == "saveall")
207                                         waypoint_saveall();
208                                 else if (argv(1) == "relinkall")
209                                         waypoint_schedulerelinkall();
210
211                                 return;
212                         }
213                 }
214
215                 default:
216                         sprint(caller, "Incorrect parameters for ^2wpeditor^7\n");
217                 case CMD_REQUEST_USAGE:
218                 {
219                         sprint(caller, "\nUsage:^3 cmd wpeditor action\n");
220                         sprint(caller, "  Where 'action' can be: spawn, remove, unreachable, saveall, relinkall\n");
221                         return;
222                 }
223         }
224 }
225
226 void ClientCommand_join(entity caller, int request)
227 {
228         switch (request)
229         {
230                 case CMD_REQUEST_COMMAND:
231                 {
232                         if (!game_stopped)
233                         if (IS_CLIENT(caller) && !IS_PLAYER(caller))
234                         if (joinAllowed(caller))
235                                 Join(caller);
236
237                         return;  // never fall through to usage
238                 }
239
240                 default:
241                 case CMD_REQUEST_USAGE:
242                 {
243                         sprint(caller, "\nUsage:^3 cmd join\n");
244                         sprint(caller, "  No arguments required.\n");
245                         return;
246                 }
247         }
248 }
249
250 void ClientCommand_kill(entity caller, int request)
251 {
252         switch (request)
253         {
254                 case CMD_REQUEST_COMMAND:
255                 {
256                         if(IS_SPEC(caller) || IS_OBSERVER(caller))
257                                 return; // no point warning about this, command does nothing
258
259                         if(GetResourceAmount(caller, RESOURCE_HEALTH) <= 0)
260                         {
261                                 sprint(caller, "Can't die - you are already dead!\n");
262                                 return;
263                         }
264
265                         ClientKill(caller);
266
267                         return;  // never fall through to usage
268                 }
269
270                 default:
271                 case CMD_REQUEST_USAGE:
272                 {
273                         sprint(caller, "\nUsage:^3 cmd kill\n");
274                         sprint(caller, "  No arguments required.\n");
275                         return;
276                 }
277         }
278 }
279
280 void ClientCommand_physics(entity caller, int request, int argc)
281 {
282         switch (request)
283         {
284                 case CMD_REQUEST_COMMAND:
285                 {
286                         string command = strtolower(argv(1));
287
288                         if (!autocvar_g_physics_clientselect)
289                         {
290                                 sprint(caller, "Client physics selection is currently disabled.\n");
291                                 return;
292                         }
293
294                         if (command == "list" || command == "help")
295                         {
296                                 sprint(caller, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
297                                 return;
298                         }
299
300                         if (Physics_Valid(command) || command == "default")
301                         {
302                                 stuffcmd(caller, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
303                                 sprint(caller, strcat("^2Physics set successfully changed to ^3", command, "\n"));
304                                 return;
305                         }
306                 }
307
308                 default:
309                         sprint(caller, strcat("Current physics set: ^3", CS(caller).cvar_cl_physics, "\n"));
310                 case CMD_REQUEST_USAGE:
311                 {
312                         sprint(caller, "\nUsage:^3 cmd physics <physics>\n");
313                         sprint(caller, "  See 'cmd physics list' for available physics sets.\n");
314                         sprint(caller, "  Argument 'default' resets to standard physics.\n");
315                         return;
316                 }
317         }
318 }
319
320 void ClientCommand_ready(entity caller, int request)  // todo: anti-spam for toggling readyness
321 {
322         switch (request)
323         {
324                 case CMD_REQUEST_COMMAND:
325                 {
326                         if (IS_CLIENT(caller))
327                         {
328                                 if (warmup_stage || sv_ready_restart || g_race_qualifying == 2)
329                                 {
330                                         if (!readyrestart_happened || sv_ready_restart_repeatable)
331                                         {
332                                                 if (time < game_starttime) // game is already restarting
333                                                         return;
334                                                 if (caller.ready)            // toggle
335                                                 {
336                                                         caller.ready = false;
337                                                         if(IS_PLAYER(caller) || caller.caplayer == 1)
338                                                                 bprint(playername(caller, false), "^2 is ^1NOT^2 ready\n");
339                                                 }
340                                                 else
341                                                 {
342                                                         caller.ready = true;
343                                                         if(IS_PLAYER(caller) || caller.caplayer == 1)
344                                                                 bprint(playername(caller, false), "^2 is ready\n");
345                                                 }
346
347                                                 // cannot reset the game while a timeout is active!
348                                                 if (!timeout_status) ReadyCount();
349                                         }
350                                         else
351                                         {
352                                                 sprint(caller, "^1Game has already been restarted\n");
353                                         }
354                                 }
355                         }
356                         return;  // never fall through to usage
357                 }
358
359                 default:
360                 case CMD_REQUEST_USAGE:
361                 {
362                         sprint(caller, "\nUsage:^3 cmd ready\n");
363                         sprint(caller, "  No arguments required.\n");
364                         return;
365                 }
366         }
367 }
368
369 void ClientCommand_say(entity caller, int request, int argc, string command)
370 {
371         switch (request)
372         {
373                 case CMD_REQUEST_COMMAND:
374                 {
375                         if (argc >= 2)   Say(caller, false, NULL, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
376                         return;  // never fall through to usage
377                 }
378
379                 default:
380                 case CMD_REQUEST_USAGE:
381                 {
382                         sprint(caller, "\nUsage:^3 cmd say <message>\n");
383                         sprint(caller, "  Where 'message' is the string of text to say.\n");
384                         return;
385                 }
386         }
387 }
388
389 void ClientCommand_say_team(entity caller, int request, int argc, string command)
390 {
391         switch (request)
392         {
393                 case CMD_REQUEST_COMMAND:
394                 {
395                         if (argc >= 2)
396                                 Say(caller, true, NULL, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
397                         return;  // never fall through to usage
398                 }
399
400                 default:
401                 case CMD_REQUEST_USAGE:
402                 {
403                         sprint(caller, "\nUsage:^3 cmd say_team <message>\n");
404                         sprint(caller, "  Where 'message' is the string of text to say.\n");
405                         return;
406                 }
407         }
408 }
409
410 .bool team_selected;
411 void ClientCommand_selectteam(entity caller, int request, int argc)
412 {
413         switch (request)
414         {
415                 case CMD_REQUEST_COMMAND:
416                 {
417                         if (argv(1) == "")
418                         {
419                                 return;
420                         }
421                         if (!IS_CLIENT(caller))
422                         {
423                                 return;
424                         }
425                         if (!teamplay)
426                         {
427                                 sprint(caller, "^7selectteam can only be used in teamgames\n");
428                                 return;
429                         }
430                         if (Player_GetForcedTeamIndex(caller) > 0)
431                         {
432                                 sprint(caller, "^7selectteam can not be used as your team is forced\n");
433                                 return;
434                         }
435                         if (lockteams)
436                         {
437                                 sprint(caller, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
438                                 return;
439                         }
440                         float selection;
441                         switch (argv(1))
442                         {
443                                 case "red":
444                                 {
445                                         selection = NUM_TEAM_1;
446                                         break;
447                                 }
448                                 case "blue":
449                                 {
450                                         selection = NUM_TEAM_2;
451                                         break;
452                                 }
453                                 case "yellow":
454                                 {
455                                         selection = NUM_TEAM_3;
456                                         break;
457                                 }
458                                 case "pink":
459                                 {
460                                         selection = NUM_TEAM_4;
461                                         break;
462                                 }
463                                 case "auto":
464                                 {
465                                         selection = (-1);
466                                         break;
467                                 }
468                                 default:
469                                 {
470                                         return;
471                                 }
472                         }
473                         if (caller.team == selection && selection != -1 && !IS_DEAD(caller))
474                         {
475                                 sprint(caller, "^7You already are on that team.\n");
476                                 return;
477                         }
478                         if (CS(caller).wasplayer && autocvar_g_changeteam_banned)
479                         {
480                                 sprint(caller, "^1You cannot change team, forbidden by the server.\n");
481                                 return;
482                         }
483                         if ((selection != -1) && autocvar_g_balance_teams &&
484                                 autocvar_g_balance_teams_prevent_imbalance)
485                         {
486                                 entity balance = TeamBalance_CheckAllowedTeams(caller);
487                                 TeamBalance_GetTeamCounts(balance, caller);
488                                 if ((Team_IndexToBit(Team_TeamToIndex(selection)) &
489                                         TeamBalance_FindBestTeams(balance, caller, false)) == 0)
490                                 {
491                                         Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
492                                         TeamBalance_Destroy(balance);
493                                         return;
494                                 }
495                                 TeamBalance_Destroy(balance);
496                         }
497                         ClientKill_TeamChange(caller, selection);
498                         if (!IS_PLAYER(caller))
499                         {
500                                 caller.team_selected = true; // avoids asking again for team selection on join
501                         }
502                         return;
503                 }
504                 default:
505                         sprint(caller, "Incorrect parameters for ^2selectteam^7\n");
506                 case CMD_REQUEST_USAGE:
507                 {
508                         sprint(caller, "\nUsage:^3 cmd selectteam team\n");
509                         sprint(caller, "  Where 'team' is the prefered team to try and join.\n");
510                         sprint(caller, "  Full list of options here: \"red, blue, yellow, pink, auto\"\n");
511                         return;
512                 }
513         }
514 }
515
516 void ClientCommand_selfstuff(entity caller, int request, string command)
517 {
518         switch (request)
519         {
520                 case CMD_REQUEST_COMMAND:
521                 {
522                         if (argv(1) != "")
523                         {
524                                 stuffcmd(caller, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
525                                 return;
526                         }
527                 }
528
529                 default:
530                         sprint(caller, "Incorrect parameters for ^2selfstuff^7\n");
531                 case CMD_REQUEST_USAGE:
532                 {
533                         sprint(caller, "\nUsage:^3 cmd selfstuff <command>\n");
534                         sprint(caller, "  Where 'command' is the string to be stuffed to your client.\n");
535                         return;
536                 }
537         }
538 }
539
540 void ClientCommand_sentcvar(entity caller, int request, int argc, string command)
541 {
542         switch (request)
543         {
544                 case CMD_REQUEST_COMMAND:
545                 {
546                         if (argv(1) != "")
547                         {
548                                 // float tokens;
549                                 string s;
550
551                                 if (argc == 2)  // undefined cvar: use the default value on the server then
552                                 {
553                                         s = strcat(substring(command, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
554                                         tokenize_console(s);
555                                 }
556
557                                 GetCvars(caller, CS(caller), 1);
558
559                                 return;
560                         }
561                 }
562
563                 default:
564                         sprint(caller, "Incorrect parameters for ^2sentcvar^7\n");
565                 case CMD_REQUEST_USAGE:
566                 {
567                         sprint(caller, "\nUsage:^3 cmd sentcvar <cvar>\n");
568                         sprint(caller, "  Where 'cvar' is the cvar plus arguments to send to the server.\n");
569                         return;
570                 }
571         }
572 }
573
574 void ClientCommand_spectate(entity caller, int request)
575 {
576         switch (request)
577         {
578                 case CMD_REQUEST_COMMAND:
579                 {
580                         if (!intermission_running && IS_CLIENT(caller))
581                         {
582                                 if((IS_SPEC(caller) || IS_OBSERVER(caller)) && argv(1) != "")
583                                 {
584                                         entity client = GetFilteredEntity(argv(1));
585                                         int spec_accepted = VerifyClientEntity(client, false, false);
586                                         if(spec_accepted > 0 && IS_PLAYER(client))
587                                         {
588                                                 if(Spectate(caller, client))
589                                                         return; // fall back to regular handling
590                                         }
591                                 }
592
593                                 int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, caller);
594
595                                 if (mutator_returnvalue == MUT_SPECCMD_RETURN) return;
596
597                                 if ((IS_PLAYER(caller) || mutator_returnvalue == MUT_SPECCMD_FORCE))
598                                 if (autocvar_sv_spectate == 1)
599                                         ClientKill_TeamChange(caller, -2); // observe
600                         }
601                         return; // never fall through to usage
602                 }
603
604                 default:
605                 case CMD_REQUEST_USAGE:
606                 {
607                         sprint(caller, "\nUsage:^3 cmd spectate <client>\n");
608                         sprint(caller, "  Where 'client' can be the player to spectate.\n");
609                         return;
610                 }
611         }
612 }
613
614 void ClientCommand_suggestmap(entity caller, int request, int argc)
615 {
616         switch (request)
617         {
618                 case CMD_REQUEST_COMMAND:
619                 {
620                         if (argv(1) != "")
621                         {
622                                 sprint(caller, strcat(MapVote_Suggest(caller, argv(1)), "\n"));
623                                 return;
624                         }
625                 }
626
627                 default:
628                         sprint(caller, "Incorrect parameters for ^2suggestmap^7\n");
629                 case CMD_REQUEST_USAGE:
630                 {
631                         sprint(caller, "\nUsage:^3 cmd suggestmap map\n");
632                         sprint(caller, "  Where 'map' is the name of the map to suggest.\n");
633                         return;
634                 }
635         }
636 }
637
638 void ClientCommand_tell(entity caller, int request, int argc, string command)
639 {
640         switch (request)
641         {
642                 case CMD_REQUEST_COMMAND:
643                 {
644                         if (argc >= 3)
645                         {
646                                 if(!IS_CLIENT(caller) && IS_REAL_CLIENT(caller)) // connecting
647                                 {
648                                         print_to(caller, "You can't ^2tell^7 a message while connecting.");
649                                         return;
650                                 }
651
652                                 entity tell_to = GetIndexedEntity(argc, 1);
653                                 float tell_accepted = VerifyClientEntity(tell_to, true, false);
654
655                                 if (tell_accepted > 0)   // the target is a real client
656                                 {
657                                         if (tell_to != caller) // and we're allowed to send to them :D
658                                         {
659                                                 // workaround for argv indexes indexing ascii chars instead of utf8 chars
660                                                 // In this case when the player name contains utf8 chars
661                                                 // the message gets partially trimmed in the beginning.
662                                                 // Potentially this bug affects any substring call that uses
663                                                 // argv_start_index and argv_end_index.
664
665                                                 string utf8_enable_save = cvar_string("utf8_enable");
666                                                 cvar_set("utf8_enable", "0");
667                                                 string msg = substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token));
668                                                 cvar_set("utf8_enable", utf8_enable_save);
669
670                                                 Say(caller, false, tell_to, msg, true);
671                                                 return;
672                                         }
673                                         else { print_to(caller, "You can't ^2tell^7 a message to yourself."); return; }
674                                 }
675                                 else if (argv(1) == "#0")
676                                 {
677                                         trigger_magicear_processmessage_forallears(caller, -1, NULL, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
678                                         return;
679                                 }
680                                 else { print_to(caller, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
681                         }
682                 }
683
684                 default:
685                         sprint(caller, "Incorrect parameters for ^2tell^7\n");
686                 case CMD_REQUEST_USAGE:
687                 {
688                         sprint(caller, "\nUsage:^3 cmd tell client <message>\n");
689                         sprint(caller, "  Where 'client' is the entity number or name of the player to send 'message' to.\n");
690                         return;
691                 }
692         }
693 }
694
695 void ClientCommand_voice(entity caller, int request, int argc, string command)
696 {
697         switch (request)
698         {
699                 case CMD_REQUEST_COMMAND:
700                 {
701                         if (argv(1) != "")
702                         {
703                                 entity e = GetVoiceMessage(argv(1));
704                                 if (!e)
705                                 {
706                                         sprint(caller, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
707                                         return;
708                                 }
709                                 string msg = "";
710                                 if (argc >= 3)
711                                         msg = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
712                                 VoiceMessage(caller, e, msg);
713
714                                 return;
715                         }
716                 }
717
718                 default:
719                         sprint(caller, "Incorrect parameters for ^2voice^7\n");
720                 case CMD_REQUEST_USAGE:
721                 {
722                         sprint(caller, "\nUsage:^3 cmd voice messagetype <soundname>\n");
723                         sprint(caller, "  'messagetype' is the type of broadcast to do, like team only or such,\n");
724                         sprint(caller, "  and 'soundname' is the string/filename of the sound/voice message to play.\n");
725                         return;
726                 }
727         }
728 }
729
730 /* use this when creating a new command, making sure to place it in alphabetical order... also,
731 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
732 void ClientCommand_(entity caller, int request)
733 {
734     switch(request)
735     {
736         case CMD_REQUEST_COMMAND:
737         {
738
739             return; // never fall through to usage
740         }
741
742         default:
743         case CMD_REQUEST_USAGE:
744         {
745             sprint(caller, "\nUsage:^3 cmd \n");
746             sprint(caller, "  No arguments required.\n");
747             return;
748         }
749     }
750 }
751 */
752
753
754 // =====================================
755 //  Macro system for networked commands
756 // =====================================
757
758 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
759 #define CLIENT_COMMANDS(ent, request, arguments, command) \
760         CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(ent, request, arguments), "Whether or not to switch automatically when getting a better weapon") \
761         CLIENT_COMMAND("clientversion", ClientCommand_clientversion(ent, request, arguments), "Release version of the game") \
762         CLIENT_COMMAND("join", ClientCommand_join(ent, request), "Become a player in the game") \
763         CLIENT_COMMAND("kill", ClientCommand_kill(ent, request), "Become a member of the dead") \
764         CLIENT_COMMAND("minigame", ClientCommand_minigame(ent, request, arguments, command), "Start a minigame") \
765         CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(ent, request, arguments), "Retrieve mapshot picture from the server") \
766         CLIENT_COMMAND("physics", ClientCommand_physics(ent, request, arguments), "Change physics set") \
767         CLIENT_COMMAND("ready", ClientCommand_ready(ent, request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
768         CLIENT_COMMAND("say", ClientCommand_say(ent, request, arguments, command), "Print a message to chat to all players") \
769         CLIENT_COMMAND("say_team", ClientCommand_say_team(ent, request, arguments, command), "Print a message to chat to all team mates") \
770         CLIENT_COMMAND("selectteam", ClientCommand_selectteam(ent, request, arguments), "Attempt to choose a team to join into") \
771         CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(ent, request, command), "Stuffcmd a command to your own client") \
772         CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(ent, request, arguments, command), "New system for sending a client cvar to the server") \
773         CLIENT_COMMAND("spectate", ClientCommand_spectate(ent, request), "Become an observer") \
774         CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(ent, request, arguments), "Suggest a map to the mapvote at match end") \
775         CLIENT_COMMAND("tell", ClientCommand_tell(ent, request, arguments, command), "Send a message directly to a player") \
776         CLIENT_COMMAND("voice", ClientCommand_voice(ent, request, arguments, command), "Send voice message via sound") \
777         CLIENT_COMMAND("wpeditor", ClientCommand_wpeditor(ent, request, arguments), "Waypoint editor commands") \
778         /* nothing */
779
780 void ClientCommand_macro_help(entity caller)
781 {
782         #define CLIENT_COMMAND(name, function, description) \
783                 { sprint(caller, "  ^2", name, "^7: ", description, "\n"); }
784
785         CLIENT_COMMANDS(NULL, 0, 0, "");
786 #undef CLIENT_COMMAND
787 }
788
789 float ClientCommand_macro_command(int argc, entity caller, string command)
790 {
791         #define CLIENT_COMMAND(name, function, description) \
792                 { if (name == strtolower(argv(0))) { function; return true; } }
793
794         CLIENT_COMMANDS(caller, CMD_REQUEST_COMMAND, argc, command);
795 #undef CLIENT_COMMAND
796
797         return false;
798 }
799
800 float ClientCommand_macro_usage(int argc, entity caller)
801 {
802         #define CLIENT_COMMAND(name, function, description) \
803                 { if (name == strtolower(argv(1))) { function; return true; } }
804
805         CLIENT_COMMANDS(caller, CMD_REQUEST_USAGE, argc, "");
806 #undef CLIENT_COMMAND
807
808         return false;
809 }
810
811 void ClientCommand_macro_write_aliases(float fh)
812 {
813         #define CLIENT_COMMAND(name, function, description) \
814                 { CMD_Write_Alias("qc_cmd_cmd", name, description); }
815
816         CLIENT_COMMANDS(NULL, 0, 0, "");
817 #undef CLIENT_COMMAND
818 }
819
820 // ======================================
821 //  Main Function Called By Engine (cmd)
822 // ======================================
823 // If this function exists, server game code parses clientcommand before the engine code gets it.
824
825 void SV_ParseClientCommand(entity this, string command)
826 {
827         // If invalid UTF-8, don't even parse it
828         string command2 = "";
829         float len = strlen(command);
830         float i;
831         for (i = 0; i < len; ++i)
832                 command2 = strcat(command2, chr2str(str2chr(command, i)));
833         if (command != command2) return;
834
835         // if we're banned, don't even parse the command
836         if (Ban_MaybeEnforceBanOnce(this)) return;
837
838         int argc = tokenize_console(command);
839
840         // Guide for working with argc arguments by example:
841         // argc:   1    - 2      - 3     - 4
842         // argv:   0    - 1      - 2     - 3
843         // cmd     vote - master - login - password
844
845         // for floodcheck
846         switch (strtolower(argv(0)))
847         {
848                 // exempt commands which are not subject to floodcheck
849                 case "begin": break;                               // handled by engine in host_cmd.c
850                 case "download": break;                            // handled by engine in cl_parse.c
851                 case "mv_getpicture": break;                       // handled by server in this file
852                 case "wpeditor": break;                            // handled by server in this file
853                 case "pause": break;                               // handled by engine in host_cmd.c
854                 case "prespawn": break;                            // handled by engine in host_cmd.c
855                 case "sentcvar": break;                            // handled by server in this file
856                 case "spawn": break;                               // handled by engine in host_cmd.c
857                 case "c2s": Net_ClientCommand(this, command); return; // handled by net.qh
858
859                 default:
860                         if (SV_ParseClientCommand_floodcheck(this)) break; // "true": continue, as we're not flooding yet
861                         else return;                                   // "false": not allowed to continue, halt // print("^1ERROR: ^7ANTISPAM CAUGHT: ", command, ".\n");
862         }
863
864         /* NOTE: should this be disabled? It can be spammy perhaps, but hopefully it's okay for now */
865         if (argv(0) == "help")
866         {
867                 if (argc == 1)
868                 {
869                         sprint(this, "\nClient networked commands:\n");
870                         ClientCommand_macro_help(this);
871
872                         sprint(this, "\nCommon networked commands:\n");
873                         CommonCommand_macro_help(this);
874
875                         sprint(this, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
876                         sprint(this, "For help about a specific command, type cmd help COMMAND\n");
877                         return;
878                 }
879                 else if (CommonCommand_macro_usage(argc, this))  // Instead of trying to call a command, we're going to see detailed information about it
880                 {
881                         return;
882                 }
883                 else if (ClientCommand_macro_usage(argc, this))  // same, but for normal commands now
884                 {
885                         return;
886                 }
887         }
888         else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, this, strtolower(argv(0)), argc, command))
889         {
890                 return;  // handled by a mutator
891         }
892         else if (CheatCommand(this, argc))
893         {
894                 return;  // handled by server/cheats.qc
895         }
896         else if (CommonCommand_macro_command(argc, this, command))
897         {
898                 return;                                          // handled by server/command/common.qc
899         }
900         else if (ClientCommand_macro_command(argc, this, command)) // continue as usual and scan for normal commands
901         {
902                 return;                                          // handled by one of the above ClientCommand_* functions
903         }
904         else
905         {
906                 clientcommand(this, command);
907         }
908 }