2 #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/effects/all.qh>
24 #include <common/mapinfo.qh>
25 #include <common/notifications/all.qh>
26 #include <common/physics/player.qh>
27 #include <common/teams.qh>
28 #include <common/util.qh>
29 #include <common/triggers/triggers.qh>
31 #include <common/minigames/sv_minigames.qh>
33 #include <common/monsters/all.qc>
34 #include <common/monsters/spawn.qh>
35 #include <common/monsters/sv_monsters.qh>
37 #include <lib/warpzone/common.qh>
39 void ClientKill_TeamChange(float targetteam); // 0 = don't change, -1 = auto, -2 = spec
41 // =========================================================
42 // Server side networked commands code, reworked by Samual
43 // Last updated: December 28th, 2011
44 // =========================================================
46 float SV_ParseClientCommand_floodcheck()
49 if (!timeout_status) // not while paused
51 if (time <= (self.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
53 self.cmd_floodcount += 1;
54 if (self.cmd_floodcount > autocvar_sv_clientcommand_antispam_count) return false; // too much spam, halt
58 self.cmd_floodtime = time;
59 self.cmd_floodcount = 1;
62 return true; // continue, as we're not flooding yet
66 // =======================
67 // Command Sub-Functions
68 // =======================
70 void ClientCommand_autoswitch(float request, float argc)
75 case CMD_REQUEST_COMMAND:
79 self.autoswitch = InterpretBoolean(argv(1));
80 sprint(self, strcat("^1autoswitch is currently turned ", (self.autoswitch ? "on" : "off"), ".\n"));
86 sprint(self, "Incorrect parameters for ^2autoswitch^7\n");
87 case CMD_REQUEST_USAGE:
89 sprint(self, "\nUsage:^3 cmd autoswitch selection\n");
90 sprint(self, " Where 'selection' controls if autoswitch is on or off.\n");
96 void ClientCommand_clientversion(float request, float argc) // internal command, used only by code
101 case CMD_REQUEST_COMMAND:
107 self.version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
109 if (self.version < autocvar_gameversion_min || self.version > autocvar_gameversion_max)
111 self.version_mismatch = 1;
112 ClientKill_TeamChange(-2); // observe
114 else if (autocvar_g_campaign || autocvar_g_balance_teams)
116 // JoinBestTeam(self, false, true);
118 else if (teamplay && !autocvar_sv_spectate && !(self.team_forced > 0))
120 TRANSMUTE(Observer, self); // really?
121 stuffcmd(self, "menu_showteamselect\n");
130 sprint(self, "Incorrect parameters for ^2clientversion^7\n");
131 case CMD_REQUEST_USAGE:
133 sprint(self, "\nUsage:^3 cmd clientversion version\n");
134 sprint(self, " Where 'version' is the game version reported by self.\n");
140 void ClientCommand_mv_getpicture(float request, float argc) // internal command, used only by code
145 case CMD_REQUEST_COMMAND:
149 if (intermission_running) MapVote_SendPicture(stof(argv(1)));
156 sprint(self, "Incorrect parameters for ^2mv_getpicture^7\n");
157 case CMD_REQUEST_USAGE:
159 sprint(self, "\nUsage:^3 cmd mv_getpicture mapid\n");
160 sprint(self, " Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
166 void ClientCommand_join(float request)
171 case CMD_REQUEST_COMMAND:
175 if (!IS_PLAYER(self) && !lockteams && !gameover)
177 if (self.caplayer) return;
178 if (nJoinAllowed(self, self))
180 if (autocvar_g_campaign) campaign_bots_may_start = true;
181 TRANSMUTE(Player, self);
182 PlayerScore_Clear(self);
183 Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CPID_PREVENT_JOIN);
184 Send_Notification(NOTIF_ALL, world, MSG_INFO, ((teamplay && self.team != -1) ? APP_TEAM_ENT(this, INFO_JOIN_PLAY_TEAM) : INFO_JOIN_PLAY), self.netname);
189 // player may not join because of g_maxplayers is set
190 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_JOIN_PREVENT);
194 return; // never fall through to usage
198 case CMD_REQUEST_USAGE:
200 sprint(self, "\nUsage:^3 cmd join\n");
201 sprint(self, " No arguments required.\n");
207 void ClientCommand_physics(float request, float argc)
212 case CMD_REQUEST_COMMAND:
214 string command = strtolower(argv(1));
216 if (!autocvar_g_physics_clientselect)
218 sprint(self, "Client physics selection is currently disabled.\n");
222 if (command == "list" || command == "help")
224 sprint(self, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
228 if (Physics_Valid(command) || command == "default")
230 stuffcmd(self, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
231 sprint(self, strcat("^2Physics set successfully changed to ^3", command, "\n"));
237 sprint(self, strcat("Current physics set: ^3", self.cvar_cl_physics, "\n"));
238 case CMD_REQUEST_USAGE:
240 sprint(self, "\nUsage:^3 cmd physics <physics>\n");
241 sprint(self, " See 'cmd physics list' for available physics sets.\n");
242 sprint(self, " Argument 'default' resets to standard physics.\n");
248 void ClientCommand_ready(float request) // todo: anti-spam for toggling readyness
253 case CMD_REQUEST_COMMAND:
257 if (warmup_stage || autocvar_sv_ready_restart || g_race_qualifying == 2)
259 if (!readyrestart_happened || autocvar_sv_ready_restart_repeatable)
261 if (time < game_starttime) // game is already restarting
263 if (self.ready) // toggle
266 bprint(self.netname, "^2 is ^1NOT^2 ready\n");
271 bprint(self.netname, "^2 is ready\n");
274 // cannot reset the game while a timeout is active!
275 if (!timeout_status) ReadyCount();
279 sprint(self, "^1Game has already been restarted\n");
283 return; // never fall through to usage
287 case CMD_REQUEST_USAGE:
289 sprint(self, "\nUsage:^3 cmd ready\n");
290 sprint(self, " No arguments required.\n");
296 void ClientCommand_say(float request, float argc, string command)
301 case CMD_REQUEST_COMMAND:
303 if (argc >= 2) Say(self, false, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
304 return; // never fall through to usage
308 case CMD_REQUEST_USAGE:
310 sprint(self, "\nUsage:^3 cmd say <message>\n");
311 sprint(self, " Where 'message' is the string of text to say.\n");
317 void ClientCommand_say_team(float request, float argc, string command)
322 case CMD_REQUEST_COMMAND:
324 if (argc >= 2) Say(self, true, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
325 return; // never fall through to usage
329 case CMD_REQUEST_USAGE:
331 sprint(self, "\nUsage:^3 cmd say_team <message>\n");
332 sprint(self, " Where 'message' is the string of text to say.\n");
338 void ClientCommand_selectteam(float request, float argc)
343 case CMD_REQUEST_COMMAND:
351 if (self.team_forced <= 0)
359 case "red": selection = NUM_TEAM_1;
361 case "blue": selection = NUM_TEAM_2;
363 case "yellow": selection = NUM_TEAM_3;
365 case "pink": selection = NUM_TEAM_4;
367 case "auto": selection = (-1);
370 default: selection = 0;
376 if (self.team == selection && !IS_DEAD(self))
378 sprint(self, "^7You already are on that team.\n");
380 else if (self.wasplayer && autocvar_g_changeteam_banned)
382 sprint(self, "^1You cannot change team, forbidden by the server.\n");
386 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
388 CheckAllowedTeams(self);
390 if (!TeamSmallerEqThanTeam(Team_TeamToNumber(selection), Team_TeamToNumber(self.team), self))
392 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
396 ClientKill_TeamChange(selection);
402 sprint(self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
407 sprint(self, "^7selectteam can not be used as your team is forced\n");
412 sprint(self, "^7selectteam can only be used in teamgames\n");
420 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
421 case CMD_REQUEST_USAGE:
423 sprint(self, "\nUsage:^3 cmd selectteam team\n");
424 sprint(self, " Where 'team' is the prefered team to try and join.\n");
425 sprint(self, " Full list of options here: \"red, blue, yellow, pink, auto\"\n");
431 void ClientCommand_selfstuff(float request, string command)
436 case CMD_REQUEST_COMMAND:
440 stuffcmd(self, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
446 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
447 case CMD_REQUEST_USAGE:
449 sprint(self, "\nUsage:^3 cmd selfstuff <command>\n");
450 sprint(self, " Where 'command' is the string to be stuffed to your client.\n");
456 void ClientCommand_sentcvar(float request, float argc, string command)
461 case CMD_REQUEST_COMMAND:
468 if (argc == 2) // undefined cvar: use the default value on the server then
470 s = strcat(substring(command, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
481 sprint(self, "Incorrect parameters for ^2sentcvar^7\n");
482 case CMD_REQUEST_USAGE:
484 sprint(self, "\nUsage:^3 cmd sentcvar <cvar>\n");
485 sprint(self, " Where 'cvar' is the cvar plus arguments to send to the server.\n");
491 void ClientCommand_spectate(float request)
496 case CMD_REQUEST_COMMAND:
500 int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, self);
502 if (mutator_returnvalue == MUT_SPECCMD_RETURN) return;
504 if ((IS_PLAYER(self) || mutator_returnvalue == MUT_SPECCMD_FORCE) && autocvar_sv_spectate == 1) ClientKill_TeamChange(-2); // observe
506 return; // never fall through to usage
510 case CMD_REQUEST_USAGE:
512 sprint(self, "\nUsage:^3 cmd spectate\n");
513 sprint(self, " No arguments required.\n");
519 void ClientCommand_suggestmap(float request, float argc)
524 case CMD_REQUEST_COMMAND:
528 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
534 sprint(self, "Incorrect parameters for ^2suggestmap^7\n");
535 case CMD_REQUEST_USAGE:
537 sprint(self, "\nUsage:^3 cmd suggestmap map\n");
538 sprint(self, " Where 'map' is the name of the map to suggest.\n");
544 void ClientCommand_tell(float request, float argc, string command)
549 case CMD_REQUEST_COMMAND:
553 entity tell_to = GetIndexedEntity(argc, 1);
554 float tell_accepted = VerifyClientEntity(tell_to, true, false);
556 if (tell_accepted > 0) // the target is a real client
558 if (tell_to != self) // and we're allowed to send to them :D
560 // workaround for argv indexes indexing ascii chars instead of utf8 chars
561 // In this case when the player name contains utf8 chars
562 // the message gets partially trimmed in the beginning.
563 // Potentially this bug affects any substring call that uses
564 // argv_start_index and argv_end_index.
566 string utf8_enable_save = cvar_string("utf8_enable");
567 cvar_set("utf8_enable", "0");
568 string msg = substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token));
569 cvar_set("utf8_enable", utf8_enable_save);
571 Say(self, false, tell_to, msg, true);
574 else { print_to(self, "You can't ^2tell^7 a message to yourself."); return; }
576 else if (argv(1) == "#0")
578 trigger_magicear_processmessage_forallears(self, -1, world, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
581 else { print_to(self, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
586 sprint(self, "Incorrect parameters for ^2tell^7\n");
587 case CMD_REQUEST_USAGE:
589 sprint(self, "\nUsage:^3 cmd tell client <message>\n");
590 sprint(self, " Where 'client' is the entity number or name of the player to send 'message' to.\n");
596 void ClientCommand_voice(float request, float argc, string command)
601 case CMD_REQUEST_COMMAND:
605 entity e = GetVoiceMessage(argv(1));
608 sprint(this, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
611 if (argc >= 3) VoiceMessage(this, e, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
612 else VoiceMessage(this, e, "");
619 sprint(self, "Incorrect parameters for ^2voice^7\n");
620 case CMD_REQUEST_USAGE:
622 sprint(self, "\nUsage:^3 cmd voice messagetype <soundname>\n");
623 sprint(self, " 'messagetype' is the type of broadcast to do, like team only or such,\n");
624 sprint(self, " and 'soundname' is the string/filename of the sound/voice message to play.\n");
630 /* use this when creating a new command, making sure to place it in alphabetical order... also,
631 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
632 void ClientCommand_(float request)
636 case CMD_REQUEST_COMMAND:
639 return; // never fall through to usage
643 case CMD_REQUEST_USAGE:
645 sprint(self, "\nUsage:^3 cmd \n");
646 sprint(self, " No arguments required.\n");
654 // =====================================
655 // Macro system for networked commands
656 // =====================================
658 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
659 #define CLIENT_COMMANDS(request, arguments, command) \
660 CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(request, arguments), "Whether or not to switch automatically when getting a better weapon") \
661 CLIENT_COMMAND("clientversion", ClientCommand_clientversion(request, arguments), "Release version of the game") \
662 CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(request, arguments), "Retrieve mapshot picture from the server") \
663 CLIENT_COMMAND("join", ClientCommand_join(request), "Become a player in the game") \
664 CLIENT_COMMAND("physics", ClientCommand_physics(request, arguments), "Change physics set") \
665 CLIENT_COMMAND("ready", ClientCommand_ready(request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
666 CLIENT_COMMAND("say", ClientCommand_say(request, arguments, command), "Print a message to chat to all players") \
667 CLIENT_COMMAND("say_team", ClientCommand_say_team(request, arguments, command), "Print a message to chat to all team mates") \
668 CLIENT_COMMAND("selectteam", ClientCommand_selectteam(request, arguments), "Attempt to choose a team to join into") \
669 CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(request, command), "Stuffcmd a command to your own client") \
670 CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(request, arguments, command), "New system for sending a client cvar to the server") \
671 CLIENT_COMMAND("spectate", ClientCommand_spectate(request), "Become an observer") \
672 CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(request, arguments), "Suggest a map to the mapvote at match end") \
673 CLIENT_COMMAND("tell", ClientCommand_tell(request, arguments, command), "Send a message directly to a player") \
674 CLIENT_COMMAND("voice", ClientCommand_voice(request, arguments, command), "Send voice message via sound") \
675 CLIENT_COMMAND("minigame", ClientCommand_minigame(request, arguments, command), "Start a minigame") \
678 void ClientCommand_macro_help()
681 #define CLIENT_COMMAND(name, function, description) \
682 { sprint(self, " ^2", name, "^7: ", description, "\n"); }
684 CLIENT_COMMANDS(0, 0, "");
685 #undef CLIENT_COMMAND
688 float ClientCommand_macro_command(float argc, string command)
690 #define CLIENT_COMMAND(name, function, description) \
691 { if (name == strtolower(argv(0))) { function; return true; } }
693 CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
694 #undef CLIENT_COMMAND
699 float ClientCommand_macro_usage(float argc)
701 #define CLIENT_COMMAND(name, function, description) \
702 { if (name == strtolower(argv(1))) { function; return true; } }
704 CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc, "");
705 #undef CLIENT_COMMAND
710 void ClientCommand_macro_write_aliases(float fh)
712 #define CLIENT_COMMAND(name, function, description) \
713 { CMD_Write_Alias("qc_cmd_cmd", name, description); }
715 CLIENT_COMMANDS(0, 0, "");
716 #undef CLIENT_COMMAND
719 // ======================================
720 // Main Function Called By Engine (cmd)
721 // ======================================
722 // If this function exists, server game code parses clientcommand before the engine code gets it.
724 void SV_ParseClientCommand(string command)
727 // If invalid UTF-8, don't even parse it
728 string command2 = "";
729 float len = strlen(command);
731 for (i = 0; i < len; ++i)
732 command2 = strcat(command2, chr2str(str2chr(command, i)));
733 if (command != command2) return;
735 // if we're banned, don't even parse the command
736 if (Ban_MaybeEnforceBanOnce(this)) return;
738 float argc = tokenize_console(command);
740 // Guide for working with argc arguments by example:
741 // argc: 1 - 2 - 3 - 4
742 // argv: 0 - 1 - 2 - 3
743 // cmd vote - master - login - password
746 switch (strtolower(argv(0)))
748 // exempt commands which are not subject to floodcheck
749 case "begin": break; // handled by engine in host_cmd.c
750 case "download": break; // handled by engine in cl_parse.c
751 case "mv_getpicture": break; // handled by server in this file
752 case "pause": break; // handled by engine in host_cmd.c
753 case "prespawn": break; // handled by engine in host_cmd.c
754 case "sentcvar": break; // handled by server in this file
755 case "spawn": break; // handled by engine in host_cmd.c
756 case "c2s": Net_ClientCommand(this, command); return; // handled by net.qh
759 if (SV_ParseClientCommand_floodcheck()) break; // "true": continue, as we're not flooding yet
760 else return; // "false": not allowed to continue, halt // print("^1ERROR: ^7ANTISPAM CAUGHT: ", command, ".\n");
763 /* NOTE: should this be disabled? It can be spammy perhaps, but hopefully it's okay for now */
764 if (argv(0) == "help")
768 sprint(this, "\nClient networked commands:\n");
769 ClientCommand_macro_help();
771 sprint(this, "\nCommon networked commands:\n");
772 CommonCommand_macro_help(this);
774 sprint(this, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
775 sprint(this, "For help about a specific command, type cmd help COMMAND\n");
778 else if (CommonCommand_macro_usage(argc, this)) // Instead of trying to call a command, we're going to see detailed information about it
782 else if (ClientCommand_macro_usage(argc)) // same, but for normal commands now
787 else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, strtolower(argv(0)), argc, command))
789 return; // handled by a mutator
791 else if (CheatCommand(argc))
793 return; // handled by server/cheats.qc
795 else if (CommonCommand_macro_command(argc, this, command))
797 return; // handled by server/command/common.qc
799 else if (ClientCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
801 return; // handled by one of the above ClientCommand_* functions
805 clientcommand(this, command);