]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/teamplay.qc
Added Player_ChangeTeamKill and ClientKill_Now hooks.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
index 921bf0cef80c8ec64c0b4f8f9d0b4aca3890545e..d434571011d1eaea0b93a29d5fec62b42b24f42a 100644 (file)
@@ -1,18 +1,18 @@
 #include "teamplay.qh"
 
-#include "cl_client.qh"
+#include "client.qh"
 #include "race.qh"
 #include "scores.qh"
 #include "scores_rules.qh"
 
-#include "bot/bot.qh"
+#include "bot/api.qh"
 
 #include "command/vote.qh"
 
-#include "mutators/all.qh"
+#include "mutators/_mod.qh"
 
 #include "../common/deathtypes/all.qh"
-#include "../common/gamemodes/all.qh"
+#include "../common/gamemodes/_mod.qh"
 #include "../common/teams.qh"
 
 void TeamchangeFrags(entity e)
@@ -52,6 +52,11 @@ void InitGameplayMode()
        get_mi_min_max(1);
        world.mins = mi_min;
        world.maxs = mi_max;
+       // currently, NetRadiant's limit is 131072 qu for each side
+       // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
+       // set the distance according to map size but don't go over the limit to avoid issues with float precision
+       // in case somebody makes extremely large maps
+       max_shot_distance = min(230000, vlen(world.maxs - world.mins));
 
        MapInfo_LoadMapSettings(mapname);
        serverflags &= ~SERVERFLAG_TEAMPLAY;
@@ -82,25 +87,23 @@ void InitGameplayMode()
 
 string GetClientVersionMessage(entity this)
 {
-       string versionmsg;
        if (this.version_mismatch) {
                if(this.version < autocvar_gameversion) {
-                       versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
+                       return strcat("This is Xonotic ", autocvar_g_xonoticversion,
+                               "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
                } else {
-                       versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
+                       return strcat("This is Xonotic ", autocvar_g_xonoticversion,
+                               "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
                }
        } else {
-               versionmsg = "^2client version and server version are compatible.^8";
+               return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
        }
-       return versionmsg;
 }
 
 string getwelcomemessage(entity this)
 {
-       string s, modifications, motd;
-
        MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
-       modifications = M_ARGV(0, string);
+       string modifications = M_ARGV(0, string);
 
        if(g_weaponarena)
        {
@@ -109,7 +112,7 @@ string getwelcomemessage(entity this)
                else
                        modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
        }
-       else if(cvar("g_balance_blaster_weaponstart") == 0)
+       else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
                modifications = strcat(modifications, ", No start weapons");
        if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
                modifications = strcat(modifications, ", Low gravity");
@@ -124,9 +127,7 @@ string getwelcomemessage(entity this)
        modifications = substring(modifications, 2, strlen(modifications) - 2);
 
        string versionmessage = GetClientVersionMessage(this);
-
-       s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
-       s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
+       string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
 
        if(modifications != "")
                s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
@@ -151,13 +152,23 @@ string getwelcomemessage(entity this)
 
        s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
 
-       motd = autocvar_sv_motd;
+       string motd = autocvar_sv_motd;
        if (motd != "") {
                s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
        }
        return s;
 }
 
+void setcolor(entity this, int clr)
+{
+#if 0
+       this.clientcolors = clr;
+       this.team = (clr & 15) + 1;
+#else
+       builtin_setcolor(this, clr);
+#endif
+}
+
 void SetPlayerColors(entity pl, float _color)
 {
        /*string s;
@@ -179,50 +190,60 @@ void SetPlayerColors(entity pl, float _color)
        }
 }
 
-void SetPlayerTeam(entity pl, float t, float s, float noprint)
+bool SetPlayerTeamSimple(entity player, int teamnum)
 {
-       float _color;
-
-       if(t == 4)
-               _color = NUM_TEAM_4 - 1;
-       else if(t == 3)
-               _color = NUM_TEAM_3 - 1;
-       else if(t == 2)
-               _color = NUM_TEAM_2 - 1;
-       else
-               _color = NUM_TEAM_1 - 1;
-
-       SetPlayerColors(pl,_color);
-
-       if(t != s) {
-               LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
-
-               if(!noprint)
-               bprint(pl.netname, "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
+       if (player.team == teamnum)
+       {
+               return true;
        }
+       if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
+               player.team), Team_TeamToNumber(teamnum)) == true)
+       {
+               // Mutator has blocked team change.
+               return false;
+       }
+       int oldteam = player.team;
+       SetPlayerColors(player, teamnum - 1);
+       MUTATOR_CALLHOOK(Player_ChangedTeam, player, oldteam, player.team);
+       return true;
+}
 
+void SetPlayerTeam(entity pl, float t, float s, float noprint)
+{
+       if (t == s)
+       {
+               return;
+       }
+       float teamnum = Team_NumberToTeam(t);
+       SetPlayerTeamSimple(pl, teamnum);
+       LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
+       if (noprint)
+       {
+               return;
+       }
+       bprint(playername(pl, false), "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
 }
 
 // set c1...c4 to show what teams are allowed
 void CheckAllowedTeams (entity for_whom)
 {
-       int dm = 0;
+       int teams_mask = 0;
 
        c1 = c2 = c3 = c4 = -1;
        cb1 = cb2 = cb3 = cb4 = 0;
 
        string teament_name = string_null;
 
-       bool mutator_returnvalue = MUTATOR_CALLHOOK(GetTeamCount, dm, teament_name);
-       dm = M_ARGV(0, float);
+       bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
+       teams_mask = M_ARGV(0, float);
        teament_name = M_ARGV(1, string);
 
        if(!mutator_returnvalue)
        {
-               if(dm & BIT(0)) c1 = 0;
-               if(dm & BIT(1)) c2 = 0;
-               if(dm & BIT(2)) c3 = 0;
-               if(dm & BIT(3)) c4 = 0;
+               if(teams_mask & BIT(0)) c1 = 0;
+               if(teams_mask & BIT(1)) c2 = 0;
+               if(teams_mask & BIT(2)) c3 = 0;
+               if(teams_mask & BIT(3)) c4 = 0;
        }
 
        // find out what teams are allowed if necessary
@@ -244,24 +265,46 @@ void CheckAllowedTeams (entity for_whom)
        }
 
        // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
-       if(c3==-1 && c4==-1)
+       if(AvailableTeams() == 2)
        if(autocvar_bot_vs_human && for_whom)
        {
                if(autocvar_bot_vs_human > 0)
                {
-                       // bots are all blue
+                       // find last team available
+
                        if(IS_BOT_CLIENT(for_whom))
-                               c1 = c3 = c4 = -1;
+                       {
+                               if(c4 >= 0) { c3 = c2 = c1 = -1; }
+                               else if(c3 >= 0) { c4 = c2 = c1 = -1; }
+                               else { c4 = c3 = c1 = -1; }
+                               // no further cases, we know at least 2 teams exist
+                       }
                        else
-                               c2 = -1;
+                       {
+                               if(c1 >= 0) { c2 = c3 = c4 = -1; }
+                               else if(c2 >= 0) { c1 = c3 = c4 = -1; }
+                               else { c1 = c2 = c4 = -1; }
+                               // no further cases, bots have one of the teams
+                       }
                }
                else
                {
-                       // bots are all red
+                       // find first team available
+
                        if(IS_BOT_CLIENT(for_whom))
-                               c2 = c3 = c4 = -1;
+                       {
+                               if(c1 >= 0) { c2 = c3 = c4 = -1; }
+                               else if(c2 >= 0) { c1 = c3 = c4 = -1; }
+                               else { c1 = c2 = c4 = -1; }
+                               // no further cases, we know at least 2 teams exist
+                       }
                        else
-                               c1 = -1;
+                       {
+                               if(c4 >= 0) { c3 = c2 = c1 = -1; }
+                               else if(c3 >= 0) { c4 = c2 = c1 = -1; }
+                               else { c4 = c3 = c1 = -1; }
+                               // no further cases, bots have one of the teams
+                       }
                }
        }
 
@@ -451,8 +494,10 @@ float FindSmallestTeam(entity pl, float ignore_pl)
        {
                if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
                        return 1; // special case for campaign and player joining
-               else
-                       error(sprintf("Too few teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
+               else if(totalteams == 1) // single team
+                       LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
+               else // no teams, major no no
+                       error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
        }
 
        // count how many players are in each team
@@ -474,31 +519,33 @@ float FindSmallestTeam(entity pl, float ignore_pl)
 
        // now t is the minimum, or A minimum!
        if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
-               RandomSelection_Add(NULL, 1, string_null, 1, 1);
+               RandomSelection_AddFloat(1, 1, 1);
        if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
-               RandomSelection_Add(NULL, 2, string_null, 1, 1);
+               RandomSelection_AddFloat(2, 1, 1);
        if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
-               RandomSelection_Add(NULL, 3, string_null, 1, 1);
+               RandomSelection_AddFloat(3, 1, 1);
        if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
-               RandomSelection_Add(NULL, 4, string_null, 1, 1);
+               RandomSelection_AddFloat(4, 1, 1);
 
        return RandomSelection_chosen_float;
 }
 
 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
 {
-       float smallest, selectedteam;
-
        // don't join a team if we're not playing a team game
-       if(!teamplay)
+       if (!teamplay)
+       {
                return 0;
+       }
 
        // find out what teams are available
        CheckAllowedTeams(this);
 
+       float selectedteam;
+
        // if we don't care what team he ends up on, put him on whatever team he entered as.
        // if he's not on a valid team, then let other code put him on the smallest team
-       if(!forcebestteam)
+       if (!forcebestteam)
        {
                if(     c1 >= 0 && this.team == NUM_TEAM_1)
                        selectedteam = this.team;
@@ -511,14 +558,14 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
                else
                        selectedteam = -1;
 
-               if(selectedteam > 0)
+               if (selectedteam > 0)
                {
-                       if(!only_return_best)
+                       if (!only_return_best)
                        {
-                               SetPlayerColors(this, selectedteam - 1);
+                               SetPlayerTeamSimple(this, selectedteam);
 
                                // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
-                               // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
+                               // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
                                LogTeamchange(this.playerid, this.team, 99);
                        }
                        return selectedteam;
@@ -526,39 +573,31 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
                // otherwise end up on the smallest team (handled below)
        }
 
-       smallest = FindSmallestTeam(this, true);
+       float bestteam = FindSmallestTeam(this, true);
+       MUTATOR_CALLHOOK(JoinBestTeam, this, bestteam);
+       bestteam = M_ARGV(1, float);
 
-       if(!only_return_best && !this.bot_forced_team)
+       if (only_return_best || this.bot_forced_team)
+       {
+               return bestteam;
+       }
+       bestteam = Team_NumberToTeam(bestteam);
+       if (bestteam != -1)
        {
                TeamchangeFrags(this);
-               if(smallest == 1)
-               {
-                       SetPlayerColors(this, NUM_TEAM_1 - 1);
-               }
-               else if(smallest == 2)
-               {
-                       SetPlayerColors(this, NUM_TEAM_2 - 1);
-               }
-               else if(smallest == 3)
-               {
-                       SetPlayerColors(this, NUM_TEAM_3 - 1);
-               }
-               else if(smallest == 4)
-               {
-                       SetPlayerColors(this, NUM_TEAM_4 - 1);
-               }
-               else
-               {
-                       error("smallest team: invalid team\n");
-               }
-
-               LogTeamchange(this.playerid, this.team, 2); // log auto join
-
-               if(!IS_DEAD(this))
-                       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+               SetPlayerTeamSimple(this, bestteam);
        }
-
-       return smallest;
+       else
+       {
+               error("JoinBestTeam: invalid team\n");
+       }
+       LogTeamchange(this.playerid, this.team, 2); // log auto join
+       if (!IS_DEAD(this) && (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) ==
+               false))
+       {
+               Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+       }
+       return bestteam;
 }
 
 //void() ctf_playerchanged;
@@ -610,7 +649,6 @@ void SV_ChangeTeam(entity this, float _color)
        // not changing teams
        if(scolor == dcolor)
        {
-               //bprint("same team change\n");
                SetPlayerTeam(this, dteam, steam, true);
                return;
        }
@@ -639,16 +677,18 @@ void SV_ChangeTeam(entity this, float _color)
                TeamchangeFrags(this);
        }
 
-       MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
-
        SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
 
-       if(IS_PLAYER(this) && steam != dteam)
+       if(!IS_PLAYER(this) || (steam == dteam))
+       {
+               return;
+       }
+       // kill player when changing teams
+       if(IS_DEAD(this) || (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) == true))
        {
-               // kill player when changing teams
-               if(!IS_DEAD(this))
-                       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+               return;
        }
+       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
 }
 
 void ShufflePlayerOutOfTeam (float source_team)
@@ -783,7 +823,10 @@ void ShufflePlayerOutOfTeam (float source_team)
        TeamchangeFrags(selected);
        SetPlayerTeam(selected, smallestteam, source_team, false);
 
-       if(!IS_DEAD(selected))
-               Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
+       if (IS_DEAD(selected) || MUTATOR_CALLHOOK(Player_ChangeTeamKill, selected) == true)
+       {
+               return;
+       }
+       Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
        Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
 }