1 #include "../../common/command/command.qh"
7 #include "../campaign.qh"
8 #include "../cheats.qh"
9 #include "../cl_player.qh"
10 #include "../ipban.qh"
11 #include "../mapvoting.qh"
12 #include "../scores.qh"
13 #include "../teamplay.qh"
15 #include "../mutators/all.qh"
18 #include "../../common/vehicles/all.qh"
21 #include "../../common/constants.qh"
22 #include "../../common/deathtypes/all.qh"
23 #include "../../common/mapinfo.qh"
24 #include "../../common/notifications.qh"
25 #include "../../common/physics.qh"
26 #include "../../common/teams.qh"
27 #include "../../common/util.qh"
28 #include "../../common/triggers/triggers.qh"
30 #include "../../common/minigames/sv_minigames.qh"
32 #include "../../common/monsters/all.qc"
33 #include "../../common/monsters/spawn.qh"
34 #include "../../common/monsters/sv_monsters.qh"
36 #include "../../lib/warpzone/common.qh"
38 void ClientKill_TeamChange(float targetteam); // 0 = don't change, -1 = auto, -2 = spec
40 // =========================================================
41 // Server side networked commands code, reworked by Samual
42 // Last updated: December 28th, 2011
43 // =========================================================
45 float SV_ParseClientCommand_floodcheck()
48 if (!timeout_status) // not while paused
50 if (time <= (self.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
52 self.cmd_floodcount += 1;
53 if (self.cmd_floodcount > autocvar_sv_clientcommand_antispam_count) return false; // too much spam, halt
57 self.cmd_floodtime = time;
58 self.cmd_floodcount = 1;
61 return true; // continue, as we're not flooding yet
65 // =======================
66 // Command Sub-Functions
67 // =======================
69 void ClientCommand_autoswitch(float request, float argc)
74 case CMD_REQUEST_COMMAND:
78 self.autoswitch = InterpretBoolean(argv(1));
79 sprint(self, strcat("^1autoswitch is currently turned ", (self.autoswitch ? "on" : "off"), ".\n"));
85 sprint(self, "Incorrect parameters for ^2autoswitch^7\n");
86 case CMD_REQUEST_USAGE:
88 sprint(self, "\nUsage:^3 cmd autoswitch selection\n");
89 sprint(self, " Where 'selection' controls if autoswitch is on or off.\n");
95 void ClientCommand_clientversion(float request, float argc) // internal command, used only by code
100 case CMD_REQUEST_COMMAND:
106 self.version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
108 if (self.version < autocvar_gameversion_min || self.version > autocvar_gameversion_max)
110 self.version_mismatch = 1;
111 ClientKill_TeamChange(-2); // observe
113 else if (autocvar_g_campaign || autocvar_g_balance_teams)
115 // JoinBestTeam(self, false, true);
117 else if (teamplay && !autocvar_sv_spectate && !(self.team_forced > 0))
119 self.classname = STR_OBSERVER; // really?
120 stuffcmd(self, "menu_showteamselect\n");
129 sprint(self, "Incorrect parameters for ^2clientversion^7\n");
130 case CMD_REQUEST_USAGE:
132 sprint(self, "\nUsage:^3 cmd clientversion version\n");
133 sprint(self, " Where 'version' is the game version reported by self.\n");
139 void ClientCommand_mv_getpicture(float request, float argc) // internal command, used only by code
144 case CMD_REQUEST_COMMAND:
148 if (intermission_running) MapVote_SendPicture(stof(argv(1)));
155 sprint(self, "Incorrect parameters for ^2mv_getpicture^7\n");
156 case CMD_REQUEST_USAGE:
158 sprint(self, "\nUsage:^3 cmd mv_getpicture mapid\n");
159 sprint(self, " Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
165 void ClientCommand_join(float request)
170 case CMD_REQUEST_COMMAND:
174 if (!IS_PLAYER(self) && !lockteams && !gameover)
176 if (self.caplayer) return;
177 if (nJoinAllowed(self))
179 if (autocvar_g_campaign) campaign_bots_may_start = 1;
180 self.classname = STR_PLAYER;
181 PlayerScore_Clear(self);
182 Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_PREVENT_JOIN);
183 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_JOIN_PLAY, self.netname);
188 // player may not join because of g_maxplayers is set
189 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_JOIN_PREVENT);
193 return; // never fall through to usage
197 case CMD_REQUEST_USAGE:
199 sprint(self, "\nUsage:^3 cmd join\n");
200 sprint(self, " No arguments required.\n");
206 void ClientCommand_physics(float request, float argc)
211 case CMD_REQUEST_COMMAND:
213 string command = strtolower(argv(1));
215 if (!autocvar_g_physics_clientselect)
217 sprint(self, "Client physics selection is currently disabled.\n");
221 if (command == "list" || command == "help")
223 sprint(self, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
227 if (Physics_Valid(command) || command == "default")
229 stuffcmd(self, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
230 sprint(self, strcat("^2Physics set successfully changed to ^3", command, "\n"));
236 sprint(self, strcat("Current physics set: ^3", self.cvar_cl_physics, "\n"));
237 case CMD_REQUEST_USAGE:
239 sprint(self, "\nUsage:^3 cmd physics <physics>\n");
240 sprint(self, " See 'cmd physics list' for available physics sets.\n");
241 sprint(self, " Argument 'default' resets to standard physics.\n");
247 void ClientCommand_ready(float request) // todo: anti-spam for toggling readyness
252 case CMD_REQUEST_COMMAND:
256 if (warmup_stage || autocvar_sv_ready_restart || g_race_qualifying == 2)
258 if (!readyrestart_happened || autocvar_sv_ready_restart_repeatable)
260 if (time < game_starttime) // game is already restarting
262 if (self.ready) // toggle
265 bprint(self.netname, "^2 is ^1NOT^2 ready\n");
270 bprint(self.netname, "^2 is ready\n");
273 // cannot reset the game while a timeout is active!
274 if (!timeout_status) ReadyCount();
278 sprint(self, "^1Game has already been restarted\n");
282 return; // never fall through to usage
286 case CMD_REQUEST_USAGE:
288 sprint(self, "\nUsage:^3 cmd ready\n");
289 sprint(self, " No arguments required.\n");
295 void ClientCommand_say(float request, float argc, string command)
300 case CMD_REQUEST_COMMAND:
302 if (argc >= 2) Say(self, false, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
303 return; // never fall through to usage
307 case CMD_REQUEST_USAGE:
309 sprint(self, "\nUsage:^3 cmd say <message>\n");
310 sprint(self, " Where 'message' is the string of text to say.\n");
316 void ClientCommand_say_team(float request, float argc, string command)
321 case CMD_REQUEST_COMMAND:
323 if (argc >= 2) Say(self, true, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
324 return; // never fall through to usage
328 case CMD_REQUEST_USAGE:
330 sprint(self, "\nUsage:^3 cmd say_team <message>\n");
331 sprint(self, " Where 'message' is the string of text to say.\n");
337 void ClientCommand_selectteam(float request, float argc)
342 case CMD_REQUEST_COMMAND:
350 if (self.team_forced <= 0)
358 case "red": selection = NUM_TEAM_1;
360 case "blue": selection = NUM_TEAM_2;
362 case "yellow": selection = NUM_TEAM_3;
364 case "pink": selection = NUM_TEAM_4;
366 case "auto": selection = (-1);
369 default: selection = 0;
375 if (self.team == selection && self.deadflag == DEAD_NO)
377 sprint(self, "^7You already are on that team.\n");
379 else if (self.wasplayer && autocvar_g_changeteam_banned)
381 sprint(self, "^1You cannot change team, forbidden by the server.\n");
385 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
387 CheckAllowedTeams(self);
389 if (!TeamSmallerEqThanTeam(Team_TeamToNumber(selection), Team_TeamToNumber(self.team), self))
391 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
395 ClientKill_TeamChange(selection);
401 sprint(self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
406 sprint(self, "^7selectteam can not be used as your team is forced\n");
411 sprint(self, "^7selectteam can only be used in teamgames\n");
419 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
420 case CMD_REQUEST_USAGE:
422 sprint(self, "\nUsage:^3 cmd selectteam team\n");
423 sprint(self, " Where 'team' is the prefered team to try and join.\n");
424 sprint(self, " Full list of options here: \"red, blue, yellow, pink, auto\"\n");
430 void ClientCommand_selfstuff(float request, string command)
435 case CMD_REQUEST_COMMAND:
439 stuffcmd(self, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
445 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
446 case CMD_REQUEST_USAGE:
448 sprint(self, "\nUsage:^3 cmd selfstuff <command>\n");
449 sprint(self, " Where 'command' is the string to be stuffed to your client.\n");
455 void ClientCommand_sentcvar(float request, float argc, string command)
460 case CMD_REQUEST_COMMAND:
467 if (argc == 2) // undefined cvar: use the default value on the server then
469 s = strcat(substring(command, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
480 sprint(self, "Incorrect parameters for ^2sentcvar^7\n");
481 case CMD_REQUEST_USAGE:
483 sprint(self, "\nUsage:^3 cmd sentcvar <cvar>\n");
484 sprint(self, " Where 'cvar' is the cvar plus arguments to send to the server.\n");
490 void ClientCommand_spectate(float request)
495 case CMD_REQUEST_COMMAND:
499 int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, self);
501 if (mutator_returnvalue == MUT_SPECCMD_RETURN) return;
503 if ((IS_PLAYER(self) || mutator_returnvalue == MUT_SPECCMD_FORCE) && autocvar_sv_spectate == 1) ClientKill_TeamChange(-2); // observe
505 return; // never fall through to usage
509 case CMD_REQUEST_USAGE:
511 sprint(self, "\nUsage:^3 cmd spectate\n");
512 sprint(self, " No arguments required.\n");
518 void ClientCommand_suggestmap(float request, float argc)
523 case CMD_REQUEST_COMMAND:
527 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
533 sprint(self, "Incorrect parameters for ^2suggestmap^7\n");
534 case CMD_REQUEST_USAGE:
536 sprint(self, "\nUsage:^3 cmd suggestmap map\n");
537 sprint(self, " Where 'map' is the name of the map to suggest.\n");
543 void ClientCommand_tell(float request, float argc, string command)
548 case CMD_REQUEST_COMMAND:
552 entity tell_to = GetIndexedEntity(argc, 1);
553 float tell_accepted = VerifyClientEntity(tell_to, true, false);
555 if (tell_accepted > 0) // the target is a real client
557 if (tell_to != self) // and we're allowed to send to them :D
559 // workaround for argv indexes indexing ascii chars instead of utf8 chars
560 // In this case when the player name contains utf8 chars
561 // the message gets partially trimmed in the beginning.
562 // Potentially this bug affects any substring call that uses
563 // argv_start_index and argv_end_index.
565 string utf8_enable_save = cvar_string("utf8_enable");
566 cvar_set("utf8_enable", "0");
567 string msg = substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token));
568 cvar_set("utf8_enable", utf8_enable_save);
570 Say(self, false, tell_to, msg, true);
573 else { print_to(self, "You can't ^2tell^7 a message to yourself."); return; }
575 else if (argv(1) == "#0")
577 trigger_magicear_processmessage_forallears(self, -1, world, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
580 else { print_to(self, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
585 sprint(self, "Incorrect parameters for ^2tell^7\n");
586 case CMD_REQUEST_USAGE:
588 sprint(self, "\nUsage:^3 cmd tell client <message>\n");
589 sprint(self, " Where 'client' is the entity number or name of the player to send 'message' to.\n");
595 void ClientCommand_voice(float request, float argc, string command)
600 case CMD_REQUEST_COMMAND:
604 if (argc >= 3) VoiceMessage(argv(1), substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
605 else VoiceMessage(argv(1), "");
612 sprint(self, "Incorrect parameters for ^2voice^7\n");
613 case CMD_REQUEST_USAGE:
615 sprint(self, "\nUsage:^3 cmd voice messagetype <soundname>\n");
616 sprint(self, " 'messagetype' is the type of broadcast to do, like team only or such,\n");
617 sprint(self, " and 'soundname' is the string/filename of the sound/voice message to play.\n");
623 /* use this when creating a new command, making sure to place it in alphabetical order... also,
624 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
625 void ClientCommand_(float request)
629 case CMD_REQUEST_COMMAND:
632 return; // never fall through to usage
636 case CMD_REQUEST_USAGE:
638 sprint(self, "\nUsage:^3 cmd \n");
639 sprint(self, " No arguments required.\n");
647 // =====================================
648 // Macro system for networked commands
649 // =====================================
651 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
652 #define CLIENT_COMMANDS(request, arguments, command) \
653 CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(request, arguments), "Whether or not to switch automatically when getting a better weapon") \
654 CLIENT_COMMAND("clientversion", ClientCommand_clientversion(request, arguments), "Release version of the game") \
655 CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(request, arguments), "Retrieve mapshot picture from the server") \
656 CLIENT_COMMAND("join", ClientCommand_join(request), "Become a player in the game") \
657 CLIENT_COMMAND("physics", ClientCommand_physics(request, arguments), "Change physics set") \
658 CLIENT_COMMAND("ready", ClientCommand_ready(request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
659 CLIENT_COMMAND("say", ClientCommand_say(request, arguments, command), "Print a message to chat to all players") \
660 CLIENT_COMMAND("say_team", ClientCommand_say_team(request, arguments, command), "Print a message to chat to all team mates") \
661 CLIENT_COMMAND("selectteam", ClientCommand_selectteam(request, arguments), "Attempt to choose a team to join into") \
662 CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(request, command), "Stuffcmd a command to your own client") \
663 CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(request, arguments, command), "New system for sending a client cvar to the server") \
664 CLIENT_COMMAND("spectate", ClientCommand_spectate(request), "Become an observer") \
665 CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(request, arguments), "Suggest a map to the mapvote at match end") \
666 CLIENT_COMMAND("tell", ClientCommand_tell(request, arguments, command), "Send a message directly to a player") \
667 CLIENT_COMMAND("voice", ClientCommand_voice(request, arguments, command), "Send voice message via sound") \
668 CLIENT_COMMAND("minigame", ClientCommand_minigame(request, arguments, command), "Start a minigame") \
671 void ClientCommand_macro_help()
674 #define CLIENT_COMMAND(name, function, description) \
675 { sprint(self, " ^2", name, "^7: ", description, "\n"); }
677 CLIENT_COMMANDS(0, 0, "");
678 #undef CLIENT_COMMAND
681 float ClientCommand_macro_command(float argc, string command)
683 #define CLIENT_COMMAND(name, function, description) \
684 { if (name == strtolower(argv(0))) { function; return true; } }
686 CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
687 #undef CLIENT_COMMAND
692 float ClientCommand_macro_usage(float argc)
694 #define CLIENT_COMMAND(name, function, description) \
695 { if (name == strtolower(argv(1))) { function; return true; } }
697 CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc, "");
698 #undef CLIENT_COMMAND
703 void ClientCommand_macro_write_aliases(float fh)
705 #define CLIENT_COMMAND(name, function, description) \
706 { CMD_Write_Alias("qc_cmd_cmd", name, description); }
708 CLIENT_COMMANDS(0, 0, "");
709 #undef CLIENT_COMMAND
712 // ======================================
713 // Main Function Called By Engine (cmd)
714 // ======================================
715 // If this function exists, server game code parses clientcommand before the engine code gets it.
717 void SV_ParseClientCommand(string command)
720 // If invalid UTF-8, don't even parse it
721 string command2 = "";
722 float len = strlen(command);
724 for (i = 0; i < len; ++i)
725 command2 = strcat(command2, chr2str(str2chr(command, i)));
726 if (command != command2) return;
728 // if we're banned, don't even parse the command
729 if (Ban_MaybeEnforceBanOnce(self)) return;
731 float argc = tokenize_console(command);
733 // Guide for working with argc arguments by example:
734 // argc: 1 - 2 - 3 - 4
735 // argv: 0 - 1 - 2 - 3
736 // cmd vote - master - login - password
739 switch (strtolower(argv(0)))
741 // exempt commands which are not subject to floodcheck
742 case "begin": break; // handled by engine in host_cmd.c
743 case "download": break; // handled by engine in cl_parse.c
744 case "mv_getpicture": break; // handled by server in this file
745 case "pause": break; // handled by engine in host_cmd.c
746 case "prespawn": break; // handled by engine in host_cmd.c
747 case "sentcvar": break; // handled by server in this file
748 case "spawn": break; // handled by engine in host_cmd.c
751 if (SV_ParseClientCommand_floodcheck()) break; // "true": continue, as we're not flooding yet
752 else return; // "false": not allowed to continue, halt // print("^1ERROR: ^7ANTISPAM CAUGHT: ", command, ".\n");
755 /* NOTE: should this be disabled? It can be spammy perhaps, but hopefully it's okay for now */
756 if (argv(0) == "help")
760 sprint(self, "\nClient networked commands:\n");
761 ClientCommand_macro_help();
763 sprint(self, "\nCommon networked commands:\n");
764 CommonCommand_macro_help(self);
766 sprint(self, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
767 sprint(self, "For help about a specific command, type cmd help COMMAND\n");
770 else if (CommonCommand_macro_usage(argc, self)) // Instead of trying to call a command, we're going to see detailed information about it
774 else if (ClientCommand_macro_usage(argc)) // same, but for normal commands now
779 else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, strtolower(argv(0)), argc, command))
781 return; // handled by a mutator
783 else if (CheatCommand(argc))
785 return; // handled by server/cheats.qc
787 else if (CommonCommand_macro_command(argc, self, command))
789 return; // handled by server/command/common.qc
791 else if (ClientCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
793 return; // handled by one of the above ClientCommand_* functions
797 clientcommand(self, command);