]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Added SetPlayerTeamSimple and Player_ChangedTeam hook.
authorLyberta <lyberta@lyberta.net>
Sat, 11 Mar 2017 13:48:01 +0000 (16:48 +0300)
committerLyberta <lyberta@lyberta.net>
Sat, 11 Mar 2017 16:02:31 +0000 (19:02 +0300)
qcsrc/server/client.qc
qcsrc/server/mutators/events.qh
qcsrc/server/teamplay.qc
qcsrc/server/teamplay.qh

index ddbf0fd1c76d089a6b881c10011d344de7b3c3bc..482dcf3763d422db51d40d4cc764fb99dde9e08c 100644 (file)
@@ -271,7 +271,9 @@ void PutObserverInServer(entity this)
        if (mutator_returnvalue) {
            // mutator prevents resetting teams+score
        } else {
+               int oldteam = this.team;
                this.team = -1;  // move this as it is needed to log the player spectating in eventlog
+               MUTATOR_CALLHOOK(Player_ChangedTeam, this, oldteam, this.team); // Lyberta: added hook
         this.frags = FRAGS_SPECTATOR;
         PlayerScore_Clear(this);  // clear scores when needed
     }
index 7523b3aac6ca26a3b54daaec7d2ad1de09a89f1d..45380e37b62200cdcfeb14c4efae79683b7a1383 100644 (file)
@@ -880,7 +880,9 @@ MUTATOR_HOOKABLE(PrepareExplosionByDamage, EV_PrepareExplosionByDamage);
     /**/
 MUTATOR_HOOKABLE(MonsterModel, EV_MonsterModel);
 
-/**/
+/**
+ * Called before player changes their team. Return true to block team change.
+ */
 #define EV_Player_ChangeTeam(i, o) \
     /** player */         i(entity, MUTATOR_ARGV_0_entity) \
        /** current team */   i(float, MUTATOR_ARGV_1_float) \
@@ -888,6 +890,16 @@ MUTATOR_HOOKABLE(MonsterModel, EV_MonsterModel);
     /**/
 MUTATOR_HOOKABLE(Player_ChangeTeam, EV_Player_ChangeTeam);
 
+/**
+ * Called after player has changed their team.
+ */
+#define EV_Player_ChangedTeam(i, o) \
+    /** player */         i(entity, MUTATOR_ARGV_0_entity) \
+       /** old team */   i(float, MUTATOR_ARGV_1_float) \
+       /** current team */       i(float, MUTATOR_ARGV_2_float) \
+    /**/
+MUTATOR_HOOKABLE(Player_ChangedTeam, EV_Player_ChangedTeam);
+
 /**/
 #define EV_URI_GetCallback(i, o) \
        /** id */       i(float, MUTATOR_ARGV_0_float) \
index d8bf72cd6b2a87bc27cc3700053716840c3bd8b3..70dc937e58feb108eed0ce922caf09749aa30cc0 100644 (file)
@@ -190,28 +190,38 @@ 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(playername(pl, false), "^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
@@ -550,7 +560,7 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
                {
                        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 client.qc/ClientConnect the player_id is 0 the log attempt is rejected
@@ -565,22 +575,11 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
 
        if(!only_return_best && !this.bot_forced_team)
        {
-               TeamchangeFrags(this);
-               if(smallest == 1)
+               smallest = Team_NumberToTeam(smallest);
+               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);
+                       TeamchangeFrags(this);
+                       SetPlayerTeamSimple(this, smallest);
                }
                else
                {
@@ -645,7 +644,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;
        }
@@ -674,8 +672,6 @@ 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)
index 127ac7a6d30caffc887e9571c40104148813eadb..3895bf6b6f2cbeaee00d823ead1bdef06903ca1d 100644 (file)
@@ -28,6 +28,12 @@ string getwelcomemessage(entity this);
 
 void SetPlayerColors(entity pl, float _color);
 
+/// \brief Sets the team of the player.
+/// \param[in,out] player Player to adjust.
+/// \param[in] teamnum Team number to set. See TEAM_NUM constants.
+/// \return True if team switch was successful, false otherwise.
+bool SetPlayerTeamSimple(entity player, int teamnum);
+
 void SetPlayerTeam(entity pl, float t, float s, float noprint);
 
 // set c1...c4 to show what teams are allowed