]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/command/cmd.qc
Merge branch 'master' into Lyberta/TeamplayOverhaul
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / cmd.qc
index c897966aa4e9b717d05873de2217af836092352a..0e6781e374120ba2c964ff7edd444715cb13b729 100644 (file)
@@ -1,18 +1,24 @@
-#include <common/command/command.qh>
 #include "cmd.qh"
 
+#include <server/defs.qh>
+#include <server/miscfunctions.qh>
+
+#include <common/command/_mod.qh>
+
 #include "common.qh"
 #include "vote.qh"
 
 #include "../campaign.qh"
 #include "../cheats.qh"
-#include "../cl_player.qh"
+#include "../client.qh"
+#include "../player.qh"
 #include "../ipban.qh"
 #include "../mapvoting.qh"
 #include "../scores.qh"
 #include "../teamplay.qh"
 
-#include "../mutators/all.qh"
+#include <server/mutators/_mod.qh>
+#include <common/gamemodes/_mod.qh>
 
 #ifdef SVQC
        #include <common/vehicles/all.qh>
 
 #include <common/constants.qh>
 #include <common/deathtypes/all.qh>
+#include <common/effects/all.qh>
 #include <common/mapinfo.qh>
-#include <common/notifications.qh>
+#include <common/notifications/all.qh>
 #include <common/physics/player.qh>
 #include <common/teams.qh>
 #include <common/util.qh>
-#include <common/triggers/triggers.qh>
+#include <common/mapobjects/triggers.qh>
 
 #include <common/minigames/sv_minigames.qh>
 
-#include <common/monsters/all.qc>
-#include <common/monsters/spawn.qh>
+#include <common/monsters/_mod.qh>
+#include <common/monsters/sv_spawn.qh>
 #include <common/monsters/sv_monsters.qh>
 
 #include <lib/warpzone/common.qh>
 
-void ClientKill_TeamChange(float targetteam);  // 0 = don't change, -1 = auto, -2 = spec
-
 // =========================================================
 //  Server side networked commands code, reworked by Samual
 //  Last updated: December 28th, 2011
 // =========================================================
 
-float SV_ParseClientCommand_floodcheck()
+bool SV_ParseClientCommand_floodcheck(entity this)
 {
-       SELFPARAM();
+       entity store = IS_CLIENT(this) ? CS(this) : this; // unfortunately, we need to store these on the client initially
+
        if (!timeout_status)  // not while paused
        {
-               if (time <= (self.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
+               if (time <= (store.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
                {
-                       self.cmd_floodcount += 1;
-                       if (self.cmd_floodcount > autocvar_sv_clientcommand_antispam_count)   return false;  // too much spam, halt
+                       store.cmd_floodcount += 1;
+                       if (store.cmd_floodcount > autocvar_sv_clientcommand_antispam_count)   return false;  // too much spam, halt
                }
                else
                {
-                       self.cmd_floodtime = time;
-                       self.cmd_floodcount = 1;
+                       store.cmd_floodtime = time;
+                       store.cmd_floodcount = 1;
                }
        }
        return true;  // continue, as we're not flooding yet
@@ -66,58 +72,56 @@ float SV_ParseClientCommand_floodcheck()
 //  Command Sub-Functions
 // =======================
 
-void ClientCommand_autoswitch(float request, float argc)
+void ClientCommand_autoswitch(entity caller, float request, float argc)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argv(1) != "")
                        {
-                               self.autoswitch = InterpretBoolean(argv(1));
-                               sprint(self, strcat("^1autoswitch is currently turned ", (self.autoswitch ? "on" : "off"), ".\n"));
+                               CS(caller).autoswitch = InterpretBoolean(argv(1));
+                               sprint(caller, strcat("^1autoswitch is currently turned ", (CS(caller).autoswitch ? "on" : "off"), ".\n"));
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2autoswitch^7\n");
+                       sprint(caller, "Incorrect parameters for ^2autoswitch^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd autoswitch selection\n");
-                       sprint(self, "  Where 'selection' controls if autoswitch is on or off.\n");
+                       sprint(caller, "\nUsage:^3 cmd autoswitch selection\n");
+                       sprint(caller, "  Where 'selection' controls if autoswitch is on or off.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_clientversion(float request, float argc)  // internal command, used only by code
+void ClientCommand_clientversion(entity caller, float request, float argc)  // internal command, used only by code
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argv(1) != "")
                        {
-                               if (IS_CLIENT(self))
+                               if (IS_CLIENT(caller))
                                {
-                                       self.version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
+                                       CS(caller).version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
 
-                                       if (self.version < autocvar_gameversion_min || self.version > autocvar_gameversion_max)
+                                       if (CS(caller).version < autocvar_gameversion_min || CS(caller).version > autocvar_gameversion_max)
                                        {
-                                               self.version_mismatch = 1;
-                                               ClientKill_TeamChange(-2);  // observe
+                                               CS(caller).version_mismatch = true;
+                                               ClientKill_TeamChange(caller, -2);  // observe
                                        }
                                        else if (autocvar_g_campaign || autocvar_g_balance_teams)
                                        {
-                                               // JoinBestTeam(self, false, true);
+                                               // JoinBestTeam(caller, false, true);
                                        }
-                                       else if (teamplay && !autocvar_sv_spectate && !(self.team_forced > 0))
+                                       else if (teamplay && !autocvar_sv_spectate && !(caller.team_forced > 0))
                                        {
-                                               self.classname = STR_OBSERVER;  // really?
-                                               stuffcmd(self, "menu_showteamselect\n");
+                                               TRANSMUTE(Observer, caller);  // really?
+                                               stuffcmd(caller, "menu_showteamselect\n");
                                        }
                                }
 
@@ -126,86 +130,67 @@ void ClientCommand_clientversion(float request, float argc)  // internal command
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2clientversion^7\n");
+                       sprint(caller, "Incorrect parameters for ^2clientversion^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd clientversion version\n");
-                       sprint(self, "  Where 'version' is the game version reported by self.\n");
+                       sprint(caller, "\nUsage:^3 cmd clientversion version\n");
+                       sprint(caller, "  Where 'version' is the game version reported by caller.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_mv_getpicture(float request, float argc)  // internal command, used only by code
+void ClientCommand_mv_getpicture(entity caller, float request, float argc)  // internal command, used only by code
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argv(1) != "")
                        {
-                               if (intermission_running) MapVote_SendPicture(stof(argv(1)));
+                               if (intermission_running) MapVote_SendPicture(caller, stof(argv(1)));
 
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2mv_getpicture^7\n");
+                       sprint(caller, "Incorrect parameters for ^2mv_getpicture^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd mv_getpicture mapid\n");
-                       sprint(self, "  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
+                       sprint(caller, "\nUsage:^3 cmd mv_getpicture mapid\n");
+                       sprint(caller, "  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_join(float request)
+void ClientCommand_join(entity caller, float request)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (IS_CLIENT(self))
-                       {
-                               if (!IS_PLAYER(self) && !lockteams && !gameover)
-                               {
-                                       if (self.caplayer) return;
-                                       if (nJoinAllowed(self, self))
-                                       {
-                                               if (autocvar_g_campaign)   campaign_bots_may_start = 1;
-                                               self.classname = STR_PLAYER;
-                                               PlayerScore_Clear(self);
-                                               Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_PREVENT_JOIN);
-                                               Send_Notification(NOTIF_ALL, world, MSG_INFO, ((teamplay && self.team != -1) ? APP_TEAM_ENT_4(this, INFO_JOIN_PLAY_TEAM_) : INFO_JOIN_PLAY), self.netname);
-                                               PutClientInServer();
-                                       }
-                                       else
-                                       {
-                                               // player may not join because of g_maxplayers is set
-                                               Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_JOIN_PREVENT);
-                                       }
-                               }
-                       }
+                       if (!game_stopped)
+                       if (IS_CLIENT(caller) && !IS_PLAYER(caller))
+                       if (joinAllowed(caller))
+                               Join(caller);
+
                        return;  // never fall through to usage
                }
 
                default:
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd join\n");
-                       sprint(self, "  No arguments required.\n");
+                       sprint(caller, "\nUsage:^3 cmd join\n");
+                       sprint(caller, "  No arguments required.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_physics(float request, float argc)
+void ClientCommand_physics(entity caller, float request, float argc)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
@@ -214,60 +199,61 @@ void ClientCommand_physics(float request, float argc)
 
                        if (!autocvar_g_physics_clientselect)
                        {
-                               sprint(self, "Client physics selection is currently disabled.\n");
+                               sprint(caller, "Client physics selection is currently disabled.\n");
                                return;
                        }
 
                        if (command == "list" || command == "help")
                        {
-                               sprint(self, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
+                               sprint(caller, strcat("Available physics sets: \n\n", autocvar_g_physics_clientselect_options, " default\n"));
                                return;
                        }
 
                        if (Physics_Valid(command) || command == "default")
                        {
-                               stuffcmd(self, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
-                               sprint(self, strcat("^2Physics set successfully changed to ^3", command, "\n"));
+                               stuffcmd(caller, strcat("\nseta cl_physics ", command, "\nsendcvar cl_physics\n"));
+                               sprint(caller, strcat("^2Physics set successfully changed to ^3", command, "\n"));
                                return;
                        }
                }
 
                default:
-                       sprint(self, strcat("Current physics set: ^3", self.cvar_cl_physics, "\n"));
+                       sprint(caller, strcat("Current physics set: ^3", CS(caller).cvar_cl_physics, "\n"));
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd physics <physics>\n");
-                       sprint(self, "  See 'cmd physics list' for available physics sets.\n");
-                       sprint(self, "  Argument 'default' resets to standard physics.\n");
+                       sprint(caller, "\nUsage:^3 cmd physics <physics>\n");
+                       sprint(caller, "  See 'cmd physics list' for available physics sets.\n");
+                       sprint(caller, "  Argument 'default' resets to standard physics.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_ready(float request)  // todo: anti-spam for toggling readyness
+void ClientCommand_ready(entity caller, float request)  // todo: anti-spam for toggling readyness
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (IS_CLIENT(self))
+                       if (IS_CLIENT(caller))
                        {
-                               if (warmup_stage || autocvar_sv_ready_restart || g_race_qualifying == 2)
+                               if (warmup_stage || sv_ready_restart || g_race_qualifying == 2)
                                {
-                                       if (!readyrestart_happened || autocvar_sv_ready_restart_repeatable)
+                                       if (!readyrestart_happened || sv_ready_restart_repeatable)
                                        {
                                                if (time < game_starttime) // game is already restarting
                                                        return;
-                                               if (self.ready)            // toggle
+                                               if (caller.ready)            // toggle
                                                {
-                                                       self.ready = false;
-                                                       bprint(self.netname, "^2 is ^1NOT^2 ready\n");
+                                                       caller.ready = false;
+                                                       if(IS_PLAYER(caller) || caller.caplayer == 1)
+                                                               bprint(playername(caller, false), "^2 is ^1NOT^2 ready\n");
                                                }
                                                else
                                                {
-                                                       self.ready = true;
-                                                       bprint(self.netname, "^2 is ready\n");
+                                                       caller.ready = true;
+                                                       if(IS_PLAYER(caller) || caller.caplayer == 1)
+                                                               bprint(playername(caller, false), "^2 is ready\n");
                                                }
 
                                                // cannot reset the game while a timeout is active!
@@ -275,7 +261,7 @@ void ClientCommand_ready(float request)  // todo: anti-spam for toggling readyne
                                        }
                                        else
                                        {
-                                               sprint(self, "^1Game has already been restarted\n");
+                                               sprint(caller, "^1Game has already been restarted\n");
                                        }
                                }
                        }
@@ -285,176 +271,185 @@ void ClientCommand_ready(float request)  // todo: anti-spam for toggling readyne
                default:
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd ready\n");
-                       sprint(self, "  No arguments required.\n");
+                       sprint(caller, "\nUsage:^3 cmd ready\n");
+                       sprint(caller, "  No arguments required.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_say(float request, float argc, string command)
+void ClientCommand_say(entity caller, float request, float argc, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (argc >= 2)   Say(self, false, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
+                       if (argc >= 2)   Say(caller, false, NULL, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
                        return;  // never fall through to usage
                }
 
                default:
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd say <message>\n");
-                       sprint(self, "  Where 'message' is the string of text to say.\n");
+                       sprint(caller, "\nUsage:^3 cmd say <message>\n");
+                       sprint(caller, "  Where 'message' is the string of text to say.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_say_team(float request, float argc, string command)
+void ClientCommand_say_team(entity caller, float request, float argc, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (argc >= 2)   Say(self, true, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
+                       if (argc >= 2)   Say(caller, true, NULL, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1);
                        return;  // never fall through to usage
                }
 
                default:
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd say_team <message>\n");
-                       sprint(self, "  Where 'message' is the string of text to say.\n");
+                       sprint(caller, "\nUsage:^3 cmd say_team <message>\n");
+                       sprint(caller, "  Where 'message' is the string of text to say.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_selectteam(float request, float argc)
+.bool team_selected;
+void ClientCommand_selectteam(entity caller, float request, float argc)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (argv(1) != "")
+                       if (argv(1) == "")
                        {
-                               if (IS_CLIENT(self))
+                               return;
+                       }
+                       if (!IS_CLIENT(caller))
+                       {
+                               return;
+                       }
+                       if (!teamplay)
+                       {
+                               sprint(caller, "^7selectteam can only be used in teamgames\n");
+                               return;
+                       }
+                       if (caller.team_forced > 0)
+                       {
+                               sprint(caller, "^7selectteam can not be used as your team is forced\n");
+                               return;
+                       }
+                       if (lockteams)
+                       {
+                               sprint(caller, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
+                               return;
+                       }
+                       float selection;
+                       switch (argv(1))
+                       {
+                               case "red":
                                {
-                                       if (teamplay)
-                                       {
-                                               if (self.team_forced <= 0)
-                                               {
-                                                       if (!lockteams)
-                                                       {
-                                                               float selection;
-
-                                                               switch (argv(1))
-                                                               {
-                                                                       case "red": selection = NUM_TEAM_1;
-                                                                               break;
-                                                                       case "blue": selection = NUM_TEAM_2;
-                                                                               break;
-                                                                       case "yellow": selection = NUM_TEAM_3;
-                                                                               break;
-                                                                       case "pink": selection = NUM_TEAM_4;
-                                                                               break;
-                                                                       case "auto": selection = (-1);
-                                                                               break;
-
-                                                                       default: selection = 0;
-                                                                               break;
-                                                               }
-
-                                                               if (selection)
-                                                               {
-                                                                       if (self.team == selection && !IS_DEAD(self))
-                                                                       {
-                                                                               sprint(self, "^7You already are on that team.\n");
-                                                                       }
-                                                                       else if (self.wasplayer && autocvar_g_changeteam_banned)
-                                                                       {
-                                                                               sprint(self, "^1You cannot change team, forbidden by the server.\n");
-                                                                       }
-                                                                       else
-                                                                       {
-                                                                               if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
-                                                                               {
-                                                                                       CheckAllowedTeams(self);
-                                                                                       GetTeamCounts(self);
-                                                                                       if (!TeamSmallerEqThanTeam(Team_TeamToNumber(selection), Team_TeamToNumber(self.team), self))
-                                                                                       {
-                                                                                               Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
-                                                                                               return;
-                                                                                       }
-                                                                               }
-                                                                               ClientKill_TeamChange(selection);
-                                                                       }
-                                                               }
-                                                       }
-                                                       else
-                                                       {
-                                                               sprint(self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
-                                                       }
-                                               }
-                                               else
-                                               {
-                                                       sprint(self, "^7selectteam can not be used as your team is forced\n");
-                                               }
-                                       }
-                                       else
-                                       {
-                                               sprint(self, "^7selectteam can only be used in teamgames\n");
-                                       }
+                                       selection = NUM_TEAM_1;
+                                       break;
+                               }
+                               case "blue":
+                               {
+                                       selection = NUM_TEAM_2;
+                                       break;
                                }
+                               case "yellow":
+                               {
+                                       selection = NUM_TEAM_3;
+                                       break;
+                               }
+                               case "pink":
+                               {
+                                       selection = NUM_TEAM_4;
+                                       break;
+                               }
+                               case "auto":
+                               {
+                                       selection = (-1);
+                                       break;
+                               }
+                               default:
+                               {
+                                       return;
+                               }
+                       }
+                       if (caller.team == selection && selection != -1 && !IS_DEAD(caller))
+                       {
+                               sprint(caller, "^7You already are on that team.\n");
+                               return;
+                       }
+                       if (CS(caller).wasplayer && autocvar_g_changeteam_banned)
+                       {
+                               sprint(caller, "^1You cannot change team, forbidden by the server.\n");
                                return;
                        }
+                       if ((selection != -1) && autocvar_g_balance_teams &&
+                               autocvar_g_balance_teams_prevent_imbalance)
+                       {
+                               entity balance = TeamBalance_CheckAllowedTeams(caller);
+                               TeamBalance_GetTeamCounts(balance, caller);
+                               if ((Team_IndexToBit(Team_TeamToIndex(selection)) &
+                                       TeamBalance_FindBestTeams(balance, caller, false)) == 0)
+                               {
+                                       Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
+                                       TeamBalance_Destroy(balance);
+                                       return;
+                               }
+                               TeamBalance_Destroy(balance);
+                       }
+                       ClientKill_TeamChange(caller, selection);
+                       if (!IS_PLAYER(caller))
+                       {
+                               caller.team_selected = true; // avoids asking again for team selection on join
+                       }
+                       return;
                }
-
                default:
-                       sprint(self, "Incorrect parameters for ^2selectteam^7\n");
+                       sprint(caller, "Incorrect parameters for ^2selectteam^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd selectteam team\n");
-                       sprint(self, "  Where 'team' is the prefered team to try and join.\n");
-                       sprint(self, "  Full list of options here: \"red, blue, yellow, pink, auto\"\n");
+                       sprint(caller, "\nUsage:^3 cmd selectteam team\n");
+                       sprint(caller, "  Where 'team' is the prefered team to try and join.\n");
+                       sprint(caller, "  Full list of options here: \"red, blue, yellow, pink, auto\"\n");
                        return;
                }
        }
 }
 
-void ClientCommand_selfstuff(float request, string command)
+void ClientCommand_selfstuff(entity caller, float request, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argv(1) != "")
                        {
-                               stuffcmd(self, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
+                               stuffcmd(caller, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2selectteam^7\n");
+                       sprint(caller, "Incorrect parameters for ^2selfstuff^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd selfstuff <command>\n");
-                       sprint(self, "  Where 'command' is the string to be stuffed to your client.\n");
+                       sprint(caller, "\nUsage:^3 cmd selfstuff <command>\n");
+                       sprint(caller, "  Where 'command' is the string to be stuffed to your client.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_sentcvar(float request, float argc, string command)
+void ClientCommand_sentcvar(entity caller, float request, float argc, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
@@ -470,91 +465,107 @@ void ClientCommand_sentcvar(float request, float argc, string command)
                                        tokenize_console(s);
                                }
 
-                               GetCvars(1);
+                               GetCvars(caller, CS(caller), 1);
 
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2sentcvar^7\n");
+                       sprint(caller, "Incorrect parameters for ^2sentcvar^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd sentcvar <cvar>\n");
-                       sprint(self, "  Where 'cvar' is the cvar plus arguments to send to the server.\n");
+                       sprint(caller, "\nUsage:^3 cmd sentcvar <cvar>\n");
+                       sprint(caller, "  Where 'cvar' is the cvar plus arguments to send to the server.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_spectate(float request)
+void ClientCommand_spectate(entity caller, float request)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (IS_CLIENT(self))
+                       if (!intermission_running && IS_CLIENT(caller))
                        {
-                               int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, self);
+                               if((IS_SPEC(caller) || IS_OBSERVER(caller)) && argv(1) != "")
+                               {
+                                       entity client = GetFilteredEntity(argv(1));
+                                       int spec_accepted = VerifyClientEntity(client, false, false);
+                                       if(spec_accepted > 0 && IS_PLAYER(client))
+                                       {
+                                               if(Spectate(caller, client))
+                                                       return; // fall back to regular handling
+                                       }
+                               }
+
+                               int mutator_returnvalue = MUTATOR_CALLHOOK(ClientCommand_Spectate, caller);
 
                                if (mutator_returnvalue == MUT_SPECCMD_RETURN) return;
 
-                               if ((IS_PLAYER(self) || mutator_returnvalue == MUT_SPECCMD_FORCE) && autocvar_sv_spectate == 1) ClientKill_TeamChange(-2); // observe
+                               if ((IS_PLAYER(caller) || mutator_returnvalue == MUT_SPECCMD_FORCE))
+                               if (autocvar_sv_spectate == 1)
+                                       ClientKill_TeamChange(caller, -2); // observe
                        }
-                       return;                                                                                                                        // never fall through to usage
+                       return; // never fall through to usage
                }
 
                default:
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd spectate\n");
-                       sprint(self, "  No arguments required.\n");
+                       sprint(caller, "\nUsage:^3 cmd spectate <client>\n");
+                       sprint(caller, "  Where 'client' can be the player to spectate.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_suggestmap(float request, float argc)
+void ClientCommand_suggestmap(entity caller, float request, float argc)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argv(1) != "")
                        {
-                               sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
+                               sprint(caller, strcat(MapVote_Suggest(caller, argv(1)), "\n"));
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2suggestmap^7\n");
+                       sprint(caller, "Incorrect parameters for ^2suggestmap^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd suggestmap map\n");
-                       sprint(self, "  Where 'map' is the name of the map to suggest.\n");
+                       sprint(caller, "\nUsage:^3 cmd suggestmap map\n");
+                       sprint(caller, "  Where 'map' is the name of the map to suggest.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_tell(float request, float argc, string command)
+void ClientCommand_tell(entity caller, float request, float argc, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
                {
                        if (argc >= 3)
                        {
+                               if(!IS_CLIENT(caller) && IS_REAL_CLIENT(caller)) // connecting
+                               {
+                                       print_to(caller, "You can't ^2tell^7 a message while connecting.");
+                                       return;
+                               }
+
                                entity tell_to = GetIndexedEntity(argc, 1);
                                float tell_accepted = VerifyClientEntity(tell_to, true, false);
 
                                if (tell_accepted > 0)   // the target is a real client
                                {
-                                       if (tell_to != self) // and we're allowed to send to them :D
+                                       if (tell_to != caller) // and we're allowed to send to them :D
                                        {
                                                // workaround for argv indexes indexing ascii chars instead of utf8 chars
                                                // In this case when the player name contains utf8 chars
@@ -567,34 +578,33 @@ void ClientCommand_tell(float request, float argc, string command)
                                                string msg = substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token));
                                                cvar_set("utf8_enable", utf8_enable_save);
 
-                                               Say(self, false, tell_to, msg, true);
+                                               Say(caller, false, tell_to, msg, true);
                                                return;
                                        }
-                                       else { print_to(self, "You can't ^2tell^7 a message to yourself."); return; }
+                                       else { print_to(caller, "You can't ^2tell^7 a message to yourself."); return; }
                                }
                                else if (argv(1) == "#0")
                                {
-                                       trigger_magicear_processmessage_forallears(self, -1, world, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
+                                       trigger_magicear_processmessage_forallears(caller, -1, NULL, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
                                        return;
                                }
-                               else { print_to(self, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
+                               else { print_to(caller, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2tell^7\n");
+                       sprint(caller, "Incorrect parameters for ^2tell^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd tell client <message>\n");
-                       sprint(self, "  Where 'client' is the entity number or name of the player to send 'message' to.\n");
+                       sprint(caller, "\nUsage:^3 cmd tell client <message>\n");
+                       sprint(caller, "  Where 'client' is the entity number or name of the player to send 'message' to.\n");
                        return;
                }
        }
 }
 
-void ClientCommand_voice(float request, float argc, string command)
+void ClientCommand_voice(entity caller, float request, float argc, string command)
 {
-       SELFPARAM();
        switch (request)
        {
                case CMD_REQUEST_COMMAND:
@@ -604,23 +614,25 @@ void ClientCommand_voice(float request, float argc, string command)
                                entity e = GetVoiceMessage(argv(1));
                                if (!e)
                                {
-                                       sprint(this, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
+                                       sprint(caller, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
                                        return;
                                }
-                               if (argc >= 3) VoiceMessage(this, e, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
-                               else VoiceMessage(this, e, "");
+                               string msg = "";
+                               if (argc >= 3)
+                                       msg = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
+                               VoiceMessage(caller, e, msg);
 
                                return;
                        }
                }
 
                default:
-                       sprint(self, "Incorrect parameters for ^2voice^7\n");
+                       sprint(caller, "Incorrect parameters for ^2voice^7\n");
                case CMD_REQUEST_USAGE:
                {
-                       sprint(self, "\nUsage:^3 cmd voice messagetype <soundname>\n");
-                       sprint(self, "  'messagetype' is the type of broadcast to do, like team only or such,\n");
-                       sprint(self, "  and 'soundname' is the string/filename of the sound/voice message to play.\n");
+                       sprint(caller, "\nUsage:^3 cmd voice messagetype <soundname>\n");
+                       sprint(caller, "  'messagetype' is the type of broadcast to do, like team only or such,\n");
+                       sprint(caller, "  and 'soundname' is the string/filename of the sound/voice message to play.\n");
                        return;
                }
        }
@@ -628,7 +640,7 @@ void ClientCommand_voice(float request, float argc, string command)
 
 /* use this when creating a new command, making sure to place it in alphabetical order... also,
 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
-void ClientCommand_(float request)
+void ClientCommand_(entity caller, float request)
 {
     switch(request)
     {
@@ -641,8 +653,8 @@ void ClientCommand_(float request)
         default:
         case CMD_REQUEST_USAGE:
         {
-            sprint(self, "\nUsage:^3 cmd \n");
-            sprint(self, "  No arguments required.\n");
+            sprint(caller, "\nUsage:^3 cmd \n");
+            sprint(caller, "  No arguments required.\n");
             return;
         }
     }
@@ -655,52 +667,51 @@ void ClientCommand_(float request)
 // =====================================
 
 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
-#define CLIENT_COMMANDS(request, arguments, command) \
-       CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(request, arguments), "Whether or not to switch automatically when getting a better weapon") \
-       CLIENT_COMMAND("clientversion", ClientCommand_clientversion(request, arguments), "Release version of the game") \
-       CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(request, arguments), "Retrieve mapshot picture from the server") \
-       CLIENT_COMMAND("join", ClientCommand_join(request), "Become a player in the game") \
-       CLIENT_COMMAND("physics", ClientCommand_physics(request, arguments), "Change physics set") \
-       CLIENT_COMMAND("ready", ClientCommand_ready(request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
-       CLIENT_COMMAND("say", ClientCommand_say(request, arguments, command), "Print a message to chat to all players") \
-       CLIENT_COMMAND("say_team", ClientCommand_say_team(request, arguments, command), "Print a message to chat to all team mates") \
-       CLIENT_COMMAND("selectteam", ClientCommand_selectteam(request, arguments), "Attempt to choose a team to join into") \
-       CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(request, command), "Stuffcmd a command to your own client") \
-       CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(request, arguments, command), "New system for sending a client cvar to the server") \
-       CLIENT_COMMAND("spectate", ClientCommand_spectate(request), "Become an observer") \
-       CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(request, arguments), "Suggest a map to the mapvote at match end") \
-       CLIENT_COMMAND("tell", ClientCommand_tell(request, arguments, command), "Send a message directly to a player") \
-       CLIENT_COMMAND("voice", ClientCommand_voice(request, arguments, command), "Send voice message via sound") \
-       CLIENT_COMMAND("minigame", ClientCommand_minigame(request, arguments, command), "Start a minigame") \
+#define CLIENT_COMMANDS(ent, request, arguments, command) \
+       CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(ent, request, arguments), "Whether or not to switch automatically when getting a better weapon") \
+       CLIENT_COMMAND("clientversion", ClientCommand_clientversion(ent, request, arguments), "Release version of the game") \
+       CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(ent, request, arguments), "Retrieve mapshot picture from the server") \
+       CLIENT_COMMAND("join", ClientCommand_join(ent, request), "Become a player in the game") \
+       CLIENT_COMMAND("physics", ClientCommand_physics(ent, request, arguments), "Change physics set") \
+       CLIENT_COMMAND("ready", ClientCommand_ready(ent, request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
+       CLIENT_COMMAND("say", ClientCommand_say(ent, request, arguments, command), "Print a message to chat to all players") \
+       CLIENT_COMMAND("say_team", ClientCommand_say_team(ent, request, arguments, command), "Print a message to chat to all team mates") \
+       CLIENT_COMMAND("selectteam", ClientCommand_selectteam(ent, request, arguments), "Attempt to choose a team to join into") \
+       CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(ent, request, command), "Stuffcmd a command to your own client") \
+       CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(ent, request, arguments, command), "New system for sending a client cvar to the server") \
+       CLIENT_COMMAND("spectate", ClientCommand_spectate(ent, request), "Become an observer") \
+       CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(ent, request, arguments), "Suggest a map to the mapvote at match end") \
+       CLIENT_COMMAND("tell", ClientCommand_tell(ent, request, arguments, command), "Send a message directly to a player") \
+       CLIENT_COMMAND("voice", ClientCommand_voice(ent, request, arguments, command), "Send voice message via sound") \
+       CLIENT_COMMAND("minigame", ClientCommand_minigame(ent, request, arguments, command), "Start a minigame") \
        /* nothing */
 
-void ClientCommand_macro_help()
+void ClientCommand_macro_help(entity caller)
 {
-       SELFPARAM();
        #define CLIENT_COMMAND(name, function, description) \
-               { sprint(self, "  ^2", name, "^7: ", description, "\n"); }
+               { sprint(caller, "  ^2", name, "^7: ", description, "\n"); }
 
-       CLIENT_COMMANDS(0, 0, "");
+       CLIENT_COMMANDS(NULL, 0, 0, "");
 #undef CLIENT_COMMAND
 }
 
-float ClientCommand_macro_command(float argc, string command)
+float ClientCommand_macro_command(float argc, entity caller, string command)
 {
        #define CLIENT_COMMAND(name, function, description) \
                { if (name == strtolower(argv(0))) { function; return true; } }
 
-       CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
+       CLIENT_COMMANDS(caller, CMD_REQUEST_COMMAND, argc, command);
 #undef CLIENT_COMMAND
 
        return false;
 }
 
-float ClientCommand_macro_usage(float argc)
+float ClientCommand_macro_usage(float argc, entity caller)
 {
        #define CLIENT_COMMAND(name, function, description) \
                { if (name == strtolower(argv(1))) { function; return true; } }
 
-       CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc, "");
+       CLIENT_COMMANDS(caller, CMD_REQUEST_USAGE, argc, "");
 #undef CLIENT_COMMAND
 
        return false;
@@ -711,7 +722,7 @@ void ClientCommand_macro_write_aliases(float fh)
        #define CLIENT_COMMAND(name, function, description) \
                { CMD_Write_Alias("qc_cmd_cmd", name, description); }
 
-       CLIENT_COMMANDS(0, 0, "");
+       CLIENT_COMMANDS(NULL, 0, 0, "");
 #undef CLIENT_COMMAND
 }
 
@@ -720,9 +731,8 @@ void ClientCommand_macro_write_aliases(float fh)
 // ======================================
 // If this function exists, server game code parses clientcommand before the engine code gets it.
 
-void SV_ParseClientCommand(string command)
+void SV_ParseClientCommand(entity this, string command)
 {
-       SELFPARAM();
        // If invalid UTF-8, don't even parse it
        string command2 = "";
        float len = strlen(command);
@@ -732,7 +742,7 @@ void SV_ParseClientCommand(string command)
        if (command != command2) return;
 
        // if we're banned, don't even parse the command
-       if (Ban_MaybeEnforceBanOnce(self)) return;
+       if (Ban_MaybeEnforceBanOnce(this)) return;
 
        float argc = tokenize_console(command);
 
@@ -755,7 +765,7 @@ void SV_ParseClientCommand(string command)
                case "c2s": Net_ClientCommand(this, command); return; // handled by net.qh
 
                default:
-                       if (SV_ParseClientCommand_floodcheck()) break; // "true": continue, as we're not flooding yet
+                       if (SV_ParseClientCommand_floodcheck(this)) break; // "true": continue, as we're not flooding yet
                        else return;                                   // "false": not allowed to continue, halt // print("^1ERROR: ^7ANTISPAM CAUGHT: ", command, ".\n");
        }
 
@@ -764,43 +774,43 @@ void SV_ParseClientCommand(string command)
        {
                if (argc == 1)
                {
-                       sprint(self, "\nClient networked commands:\n");
-                       ClientCommand_macro_help();
+                       sprint(this, "\nClient networked commands:\n");
+                       ClientCommand_macro_help(this);
 
-                       sprint(self, "\nCommon networked commands:\n");
-                       CommonCommand_macro_help(self);
+                       sprint(this, "\nCommon networked commands:\n");
+                       CommonCommand_macro_help(this);
 
-                       sprint(self, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
-                       sprint(self, "For help about a specific command, type cmd help COMMAND\n");
+                       sprint(this, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
+                       sprint(this, "For help about a specific command, type cmd help COMMAND\n");
                        return;
                }
-               else if (CommonCommand_macro_usage(argc, self))  // Instead of trying to call a command, we're going to see detailed information about it
+               else if (CommonCommand_macro_usage(argc, this))  // Instead of trying to call a command, we're going to see detailed information about it
                {
                        return;
                }
-               else if (ClientCommand_macro_usage(argc))  // same, but for normal commands now
+               else if (ClientCommand_macro_usage(argc, this))  // same, but for normal commands now
                {
                        return;
                }
        }
-       else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, strtolower(argv(0)), argc, command))
+       else if (MUTATOR_CALLHOOK(SV_ParseClientCommand, this, strtolower(argv(0)), argc, command))
        {
                return;  // handled by a mutator
        }
-       else if (CheatCommand(argc))
+       else if (CheatCommand(this, argc))
        {
                return;  // handled by server/cheats.qc
        }
-       else if (CommonCommand_macro_command(argc, self, command))
+       else if (CommonCommand_macro_command(argc, this, command))
        {
                return;                                          // handled by server/command/common.qc
        }
-       else if (ClientCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
+       else if (ClientCommand_macro_command(argc, this, command)) // continue as usual and scan for normal commands
        {
                return;                                          // handled by one of the above ClientCommand_* functions
        }
        else
        {
-               clientcommand(self, command);
+               clientcommand(this, command);
        }
 }