3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
6 #include <common/command/_mod.qh>
11 #include "../bot/api.qh"
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"
23 #include <server/mutators/_mod.qh>
24 #include <common/gamemodes/_mod.qh>
27 #include <common/vehicles/all.qh>
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>
40 #include <common/minigames/sv_minigames.qh>
42 #include <common/monsters/_mod.qh>
43 #include <common/monsters/sv_spawn.qh>
44 #include <common/monsters/sv_monsters.qh>
46 #include <lib/warpzone/common.qh>
48 // =========================================================
49 // Server side networked commands code, reworked by Samual
50 // Last updated: December 28th, 2011
51 // =========================================================
53 bool SV_ParseClientCommand_floodcheck(entity this)
55 entity store = IS_CLIENT(this) ? CS(this) : this; // unfortunately, we need to store these on the client initially
57 if (!timeout_status) // not while paused
59 if (time <= (store.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
61 store.cmd_floodcount += 1;
62 if (store.cmd_floodcount > autocvar_sv_clientcommand_antispam_count) return false; // too much spam, halt
66 store.cmd_floodtime = time;
67 store.cmd_floodcount = 1;
70 return true; // continue, as we're not flooding yet
74 // =======================
75 // Command Sub-Functions
76 // =======================
78 void ClientCommand_autoswitch(entity caller, int request, int argc)
82 case CMD_REQUEST_COMMAND:
86 CS(caller).autoswitch = InterpretBoolean(argv(1));
87 sprint(caller, strcat("^1autoswitch is currently turned ", (CS(caller).autoswitch ? "on" : "off"), ".\n"));
93 sprint(caller, "Incorrect parameters for ^2autoswitch^7\n");
94 case CMD_REQUEST_USAGE:
96 sprint(caller, "\nUsage:^3 cmd autoswitch selection\n");
97 sprint(caller, " Where 'selection' controls if autoswitch is on or off.\n");
103 void ClientCommand_clientversion(entity caller, int request, int argc) // internal command, used only by code
107 case CMD_REQUEST_COMMAND:
111 if (IS_CLIENT(caller))
113 CS(caller).version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
115 if (CS(caller).version < autocvar_gameversion_min || CS(caller).version > autocvar_gameversion_max)
117 CS(caller).version_mismatch = true;
118 ClientKill_TeamChange(caller, -2); // observe
120 else if (autocvar_g_campaign || autocvar_g_balance_teams)
122 // JoinBestTeam(caller, false, true);
124 else if (teamplay && !autocvar_sv_spectate && !(Player_GetForcedTeamIndex(caller) > 0))
126 TRANSMUTE(Observer, caller); // really?
127 stuffcmd(caller, "menu_showteamselect\n");
136 sprint(caller, "Incorrect parameters for ^2clientversion^7\n");
137 case CMD_REQUEST_USAGE:
139 sprint(caller, "\nUsage:^3 cmd clientversion version\n");
140 sprint(caller, " Where 'version' is the game version reported by caller.\n");
146 void ClientCommand_mv_getpicture(entity caller, int request, int argc) // internal command, used only by code
150 case CMD_REQUEST_COMMAND:
154 if (intermission_running) MapVote_SendPicture(caller, stof(argv(1)));
161 sprint(caller, "Incorrect parameters for ^2mv_getpicture^7\n");
162 case CMD_REQUEST_USAGE:
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");
171 void ClientCommand_wpeditor(entity caller, int request, int argc)
175 case CMD_REQUEST_COMMAND:
177 if (!autocvar_g_waypointeditor)
179 sprint(caller, "ERROR: this command works only if the waypoint editor is on\n");
185 if (argv(1) == "spawn")
187 if (!IS_PLAYER(caller))
188 sprint(caller, "ERROR: this command works only if you are player\n");
190 waypoint_spawn_fromeditor(caller);
192 else if (argv(1) == "remove")
194 if (!IS_PLAYER(caller))
195 sprint(caller, "ERROR: this command works only if you are player\n");
197 waypoint_remove_fromeditor(caller);
199 else if (argv(1) == "unreachable")
201 if (!IS_PLAYER(caller))
202 sprint(caller, "ERROR: this command works only if you are player\n");
204 waypoint_unreachable(caller);
206 else if (argv(1) == "saveall")
208 else if (argv(1) == "relinkall")
209 waypoint_schedulerelinkall();
216 sprint(caller, "Incorrect parameters for ^2wpeditor^7\n");
217 case CMD_REQUEST_USAGE:
219 sprint(caller, "\nUsage:^3 cmd wpeditor action\n");
220 sprint(caller, " Where 'action' can be: spawn, remove, unreachable, saveall, relinkall\n");
226 void ClientCommand_join(entity caller, int request)
230 case CMD_REQUEST_COMMAND:
233 if (IS_CLIENT(caller) && !IS_PLAYER(caller))
234 if (joinAllowed(caller))
237 return; // never fall through to usage
241 case CMD_REQUEST_USAGE:
243 sprint(caller, "\nUsage:^3 cmd join\n");
244 sprint(caller, " No arguments required.\n");
250 void ClientCommand_kill(entity caller, int request)
254 case CMD_REQUEST_COMMAND:
256 if(IS_SPEC(caller) || IS_OBSERVER(caller))
257 return; // no point warning about this, command does nothing
259 if(GetResourceAmount(caller, RESOURCE_HEALTH) <= 0)
261 sprint(caller, "Can't die - you are already dead!\n");
267 return; // never fall through to usage
271 case CMD_REQUEST_USAGE:
273 sprint(caller, "\nUsage:^3 cmd kill\n");
274 sprint(caller, " No arguments required.\n");
280 void ClientCommand_physics(entity caller, int request, int argc)
284 case CMD_REQUEST_COMMAND:
286 string command = strtolower(argv(1));
288 if (!autocvar_g_physics_clientselect)
290 sprint(caller, "Client physics selection is currently disabled.\n");
294 if (command == "list" || command == "help")
296 sprint(caller, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
300 if (Physics_Valid(command) || command == "default")
302 stuffcmd(caller, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
303 sprint(caller, strcat("^2Physics set successfully changed to ^3", command, "\n"));
309 sprint(caller, strcat("Current physics set: ^3", CS(caller).cvar_cl_physics, "\n"));
310 case CMD_REQUEST_USAGE:
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");
320 void ClientCommand_ready(entity caller, int request) // todo: anti-spam for toggling readyness
324 case CMD_REQUEST_COMMAND:
326 if (IS_CLIENT(caller))
328 if (warmup_stage || sv_ready_restart || g_race_qualifying == 2)
330 if (!readyrestart_happened || sv_ready_restart_repeatable)
332 if (time < game_starttime) // game is already restarting
334 if (caller.ready) // toggle
336 caller.ready = false;
337 if(IS_PLAYER(caller) || caller.caplayer == 1)
338 bprint(playername(caller, false), "^2 is ^1NOT^2 ready\n");
343 if(IS_PLAYER(caller) || caller.caplayer == 1)
344 bprint(playername(caller, false), "^2 is ready\n");
347 // cannot reset the game while a timeout is active!
348 if (!timeout_status) ReadyCount();
352 sprint(caller, "^1Game has already been restarted\n");
356 return; // never fall through to usage
360 case CMD_REQUEST_USAGE:
362 sprint(caller, "\nUsage:^3 cmd ready\n");
363 sprint(caller, " No arguments required.\n");
369 void ClientCommand_say(entity caller, int request, int argc, string command)
373 case CMD_REQUEST_COMMAND:
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
380 case CMD_REQUEST_USAGE:
382 sprint(caller, "\nUsage:^3 cmd say <message>\n");
383 sprint(caller, " Where 'message' is the string of text to say.\n");
389 void ClientCommand_say_team(entity caller, int request, int argc, string command)
393 case CMD_REQUEST_COMMAND:
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
401 case CMD_REQUEST_USAGE:
403 sprint(caller, "\nUsage:^3 cmd say_team <message>\n");
404 sprint(caller, " Where 'message' is the string of text to say.\n");
411 void ClientCommand_selectteam(entity caller, int request, int argc)
415 case CMD_REQUEST_COMMAND:
421 if (!IS_CLIENT(caller))
427 sprint(caller, "^7selectteam can only be used in teamgames\n");
430 if (Player_GetForcedTeamIndex(caller) > 0)
432 sprint(caller, "^7selectteam can not be used as your team is forced\n");
437 sprint(caller, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
445 selection = NUM_TEAM_1;
450 selection = NUM_TEAM_2;
455 selection = NUM_TEAM_3;
460 selection = NUM_TEAM_4;
473 if (caller.team == selection && selection != -1 && !IS_DEAD(caller))
475 sprint(caller, "^7You already are on that team.\n");
478 if (CS(caller).wasplayer && autocvar_g_changeteam_banned)
480 sprint(caller, "^1You cannot change team, forbidden by the server.\n");
483 if ((selection != -1) && autocvar_g_balance_teams &&
484 autocvar_g_balance_teams_prevent_imbalance)
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)
491 Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
492 TeamBalance_Destroy(balance);
495 TeamBalance_Destroy(balance);
497 ClientKill_TeamChange(caller, selection);
498 if (!IS_PLAYER(caller))
500 caller.team_selected = true; // avoids asking again for team selection on join
505 sprint(caller, "Incorrect parameters for ^2selectteam^7\n");
506 case CMD_REQUEST_USAGE:
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");
516 void ClientCommand_selfstuff(entity caller, int request, string command)
520 case CMD_REQUEST_COMMAND:
524 stuffcmd(caller, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
530 sprint(caller, "Incorrect parameters for ^2selfstuff^7\n");
531 case CMD_REQUEST_USAGE:
533 sprint(caller, "\nUsage:^3 cmd selfstuff <command>\n");
534 sprint(caller, " Where 'command' is the string to be stuffed to your client.\n");
540 void ClientCommand_sentcvar(entity caller, int request, int argc, string command)
544 case CMD_REQUEST_COMMAND:
551 if (argc == 2) // undefined cvar: use the default value on the server then
553 s = strcat(substring(command, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
557 GetCvars(caller, CS(caller), 1);
564 sprint(caller, "Incorrect parameters for ^2sentcvar^7\n");
565 case CMD_REQUEST_USAGE:
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");
574 void ClientCommand_spectate(entity caller, int request)
578 case CMD_REQUEST_COMMAND:
580 if (!intermission_running && IS_CLIENT(caller))
582 if((IS_SPEC(caller) || IS_OBSERVER(caller)) && argv(1) != "")
584 entity client = GetFilteredEntity(argv(1));
585 int spec_accepted = VerifyClientEntity(client, false, false);
586 if(spec_accepted > 0 && IS_PLAYER(client))
588 if(Spectate(caller, client))
589 return; // fall back to regular handling
593 int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, caller);
595 if (mutator_returnvalue == MUT_SPECCMD_RETURN) return;
597 if ((IS_PLAYER(caller) || mutator_returnvalue == MUT_SPECCMD_FORCE))
598 if (autocvar_sv_spectate == 1)
599 ClientKill_TeamChange(caller, -2); // observe
601 return; // never fall through to usage
605 case CMD_REQUEST_USAGE:
607 sprint(caller, "\nUsage:^3 cmd spectate <client>\n");
608 sprint(caller, " Where 'client' can be the player to spectate.\n");
614 void ClientCommand_suggestmap(entity caller, int request, int argc)
618 case CMD_REQUEST_COMMAND:
622 sprint(caller, strcat(MapVote_Suggest(caller, argv(1)), "\n"));
628 sprint(caller, "Incorrect parameters for ^2suggestmap^7\n");
629 case CMD_REQUEST_USAGE:
631 sprint(caller, "\nUsage:^3 cmd suggestmap map\n");
632 sprint(caller, " Where 'map' is the name of the map to suggest.\n");
638 void ClientCommand_tell(entity caller, int request, int argc, string command)
642 case CMD_REQUEST_COMMAND:
646 if(!IS_CLIENT(caller) && IS_REAL_CLIENT(caller)) // connecting
648 print_to(caller, "You can't ^2tell^7 a message while connecting.");
652 entity tell_to = GetIndexedEntity(argc, 1);
653 float tell_accepted = VerifyClientEntity(tell_to, true, false);
655 if (tell_accepted > 0) // the target is a real client
657 if (tell_to != caller) // and we're allowed to send to them :D
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.
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);
670 Say(caller, false, tell_to, msg, true);
673 else { print_to(caller, "You can't ^2tell^7 a message to yourself."); return; }
675 else if (argv(1) == "#0")
677 trigger_magicear_processmessage_forallears(caller, -1, NULL, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
680 else { print_to(caller, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
685 sprint(caller, "Incorrect parameters for ^2tell^7\n");
686 case CMD_REQUEST_USAGE:
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");
695 void ClientCommand_voice(entity caller, int request, int argc, string command)
699 case CMD_REQUEST_COMMAND:
703 entity e = GetVoiceMessage(argv(1));
706 sprint(caller, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
711 msg = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
712 VoiceMessage(caller, e, msg);
719 sprint(caller, "Incorrect parameters for ^2voice^7\n");
720 case CMD_REQUEST_USAGE:
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");
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)
736 case CMD_REQUEST_COMMAND:
739 return; // never fall through to usage
743 case CMD_REQUEST_USAGE:
745 sprint(caller, "\nUsage:^3 cmd \n");
746 sprint(caller, " No arguments required.\n");
754 // =====================================
755 // Macro system for networked commands
756 // =====================================
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") \
780 void ClientCommand_macro_help(entity caller)
782 #define CLIENT_COMMAND(name, function, description) \
783 { sprint(caller, " ^2", name, "^7: ", description, "\n"); }
785 CLIENT_COMMANDS(NULL, 0, 0, "");
786 #undef CLIENT_COMMAND
789 float ClientCommand_macro_command(int argc, entity caller, string command)
791 #define CLIENT_COMMAND(name, function, description) \
792 { if (name == strtolower(argv(0))) { function; return true; } }
794 CLIENT_COMMANDS(caller, CMD_REQUEST_COMMAND, argc, command);
795 #undef CLIENT_COMMAND
800 float ClientCommand_macro_usage(int argc, entity caller)
802 #define CLIENT_COMMAND(name, function, description) \
803 { if (name == strtolower(argv(1))) { function; return true; } }
805 CLIENT_COMMANDS(caller, CMD_REQUEST_USAGE, argc, "");
806 #undef CLIENT_COMMAND
811 void ClientCommand_macro_write_aliases(float fh)
813 #define CLIENT_COMMAND(name, function, description) \
814 { CMD_Write_Alias("qc_cmd_cmd", name, description); }
816 CLIENT_COMMANDS(NULL, 0, 0, "");
817 #undef CLIENT_COMMAND
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.
825 void SV_ParseClientCommand(entity this, string command)
827 // If invalid UTF-8, don't even parse it
828 string command2 = "";
829 float len = strlen(command);
831 for (i = 0; i < len; ++i)
832 command2 = strcat(command2, chr2str(str2chr(command, i)));
833 if (command != command2) return;
835 // if we're banned, don't even parse the command
836 if (Ban_MaybeEnforceBanOnce(this)) return;
838 int argc = tokenize_console(command);
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
846 switch (strtolower(argv(0)))
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
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");
864 /* NOTE: should this be disabled? It can be spammy perhaps, but hopefully it's okay for now */
865 if (argv(0) == "help")
869 sprint(this, "\nClient networked commands:\n");
870 ClientCommand_macro_help(this);
872 sprint(this, "\nCommon networked commands:\n");
873 CommonCommand_macro_help(this);
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");
879 else if (CommonCommand_macro_usage(argc, this)) // Instead of trying to call a command, we're going to see detailed information about it
883 else if (ClientCommand_macro_usage(argc, this)) // same, but for normal commands now
888 else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, this, strtolower(argv(0)), argc, command))
890 return; // handled by a mutator
892 else if (CheatCommand(this, argc))
894 return; // handled by server/cheats.qc
896 else if (CommonCommand_macro_command(argc, this, command))
898 return; // handled by server/command/common.qc
900 else if (ClientCommand_macro_command(argc, this, command)) // continue as usual and scan for normal commands
902 return; // handled by one of the above ClientCommand_* functions
906 clientcommand(this, command);