]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/mutators/mutator/gamemode_ctf.qc
Fix a bunch of loops
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_ctf.qc
index d4524479d167dec53dc1d9dd363290390a46bf16..9100c52ba73d9aab6df4587d5c86965f683d9ffb 100644 (file)
@@ -401,7 +401,6 @@ bool ctf_CheckPassDirection(vector head_center, vector passer_center, vector pas
 bool ctf_CaptureShield_CheckStatus(entity p)
 {
        int s, s2, s3, s4, se, se2, se3, se4, sr, ser;
-       entity e;
        int players_worseeq, players_total;
 
        if(ctf_captureshield_max_ratio <= 0)
@@ -418,21 +417,20 @@ bool ctf_CaptureShield_CheckStatus(entity p)
                return false;
 
        players_total = players_worseeq = 0;
-       FOR_EACH_PLAYER(e)
-       {
-               if(DIFF_TEAM(e, p))
+       FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
+               if(DIFF_TEAM(it, p))
                        continue;
-               se = PlayerScore_Add(e, SP_CTF_CAPS, 0);
-               se2 = PlayerScore_Add(e, SP_CTF_PICKUPS, 0);
-               se3 = PlayerScore_Add(e, SP_CTF_RETURNS, 0);
-               se4 = PlayerScore_Add(e, SP_CTF_FCKILLS, 0);
+               se = PlayerScore_Add(it, SP_CTF_CAPS, 0);
+               se2 = PlayerScore_Add(it, SP_CTF_PICKUPS, 0);
+               se3 = PlayerScore_Add(it, SP_CTF_RETURNS, 0);
+               se4 = PlayerScore_Add(it, SP_CTF_FCKILLS, 0);
 
                ser = ((se - se2) + (se3 + se4));
 
                if(ser <= sr)
                        ++players_worseeq;
                ++players_total;
-       }
+       ));
 
        // player is in the worse half, if >= half the players are better than him, or consequently, if < half of the players are worse
        // use this rule here
@@ -544,7 +542,6 @@ void ctf_Handle_Drop(entity flag, entity player, int droptype)
 
 void ctf_Handle_Retrieve(entity flag, entity player)
 {
-       entity tmp_player; // temporary entity which the FOR_EACH_PLAYER loop uses to scan players
        entity sender = flag.pass_sender;
 
        // transfer flag to player
@@ -573,15 +570,14 @@ void ctf_Handle_Retrieve(entity flag, entity player)
        _sound(player, CH_TRIGGER, flag.snd_flag_pass, VOL_BASE, ATTEN_NORM);
        ctf_EventLog("receive", flag.team, player);
 
-       FOR_EACH_REALPLAYER(tmp_player)
-       {
-               if(tmp_player == sender)
-                       Send_Notification(NOTIF_ONE, tmp_player, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_SENT_) : CENTER_CTF_PASS_SENT_NEUTRAL), player.netname);
-               else if(tmp_player == player)
-                       Send_Notification(NOTIF_ONE, tmp_player, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_RECEIVED_) : CENTER_CTF_PASS_RECEIVED_NEUTRAL), sender.netname);
-               else if(SAME_TEAM(tmp_player, sender))
-                       Send_Notification(NOTIF_ONE, tmp_player, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_OTHER_) : CENTER_CTF_PASS_OTHER_NEUTRAL), sender.netname, player.netname);
-       }
+       FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), LAMBDA(
+               if(it == sender)
+                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_SENT_) : CENTER_CTF_PASS_SENT_NEUTRAL), player.netname);
+               else if(it == player)
+                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_RECEIVED_) : CENTER_CTF_PASS_RECEIVED_NEUTRAL), sender.netname);
+               else if(SAME_TEAM(it, sender))
+                       Send_Notification(NOTIF_ONE, it, MSG_CENTER, ((flag.team) ? APP_TEAM_ENT_4(flag, CENTER_CTF_PASS_OTHER_) : CENTER_CTF_PASS_OTHER_NEUTRAL), sender.netname, player.netname);
+       ));
 
        // create new waypoint
        ctf_FlagcarrierWaypoints(player);
@@ -796,7 +792,6 @@ void ctf_Handle_Pickup(entity flag, entity player, int pickuptype)
 {
        // declarations
        float pickup_dropped_score; // used to calculate dropped pickup score
-       entity tmp_entity; // temporary entity
 
        // attach the flag to the player
        flag.owner = player;
@@ -837,19 +832,16 @@ void ctf_Handle_Pickup(entity flag, entity player, int pickuptype)
        Send_Notification(NOTIF_TEAM_EXCEPT, player, MSG_CHOICE, ((flag.team) ? APP_TEAM_ENT_4(flag, CHOICE_CTF_PICKUP_TEAM_) : CHOICE_CTF_PICKUP_TEAM_NEUTRAL), Team_ColorCode(player.team), player.netname);
 
        if(!flag.team)
-       FOR_EACH_PLAYER(tmp_entity)
-       if(tmp_entity != player)
-       if(DIFF_TEAM(player, tmp_entity))
-               Send_Notification(NOTIF_ONE, tmp_entity, MSG_CHOICE, CHOICE_CTF_PICKUP_ENEMY_NEUTRAL, Team_ColorCode(player.team), player.netname);
+               FOREACH_CLIENT(IS_PLAYER(it) && it != player && DIFF_TEAM(it, player), LAMBDA(Send_Notification(NOTIF_ONE, it, MSG_CHOICE, CHOICE_CTF_PICKUP_ENEMY_NEUTRAL, Team_ColorCode(player.team), player.netname)));
 
        if(flag.team)
-       FOR_EACH_PLAYER(tmp_entity)
-       if(tmp_entity != player)
-       if(CTF_SAMETEAM(flag, tmp_entity))
-       if(SAME_TEAM(player, tmp_entity))
-               Send_Notification(NOTIF_ONE, tmp_entity, MSG_CHOICE, APP_TEAM_ENT_4(flag, CHOICE_CTF_PICKUP_TEAM_), Team_ColorCode(player.team), player.netname);
-       else
-               Send_Notification(NOTIF_ONE, tmp_entity, MSG_CHOICE, ((SAME_TEAM(flag, player)) ? CHOICE_CTF_PICKUP_ENEMY_TEAM : CHOICE_CTF_PICKUP_ENEMY), Team_ColorCode(player.team), player.netname);
+               FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
+                       if(CTF_SAMETEAM(flag, it))
+                       if(SAME_TEAM(player, it))
+                               Send_Notification(NOTIF_ONE, it, MSG_CHOICE, APP_TEAM_ENT_4(flag, CHOICE_CTF_PICKUP_TEAM_), Team_ColorCode(player.team), player.netname);
+                       else
+                               Send_Notification(NOTIF_ONE, it, MSG_CHOICE, ((SAME_TEAM(flag, player)) ? CHOICE_CTF_PICKUP_ENEMY_TEAM : CHOICE_CTF_PICKUP_ENEMY), Team_ColorCode(player.team), player.netname);
+               ));
 
        _sound(player, CH_TRIGGER, flag.snd_flag_taken, VOL_BASE, ATTEN_NONE);
 
@@ -999,8 +991,7 @@ void ctf_CheckStalemate()
 
                if (!wpforenemy_announced)
                {
-                       FOR_EACH_REALPLAYER(tmp_entity)
-                               Send_Notification(NOTIF_ONE, tmp_entity, MSG_CENTER, ((tmp_entity.flagcarried) ? CENTER_CTF_STALEMATE_CARRIER : CENTER_CTF_STALEMATE_OTHER));
+                       FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), LAMBDA(Send_Notification(NOTIF_ONE, it, MSG_CENTER, ((it.flagcarried) ? CENTER_CTF_STALEMATE_CARRIER : CENTER_CTF_STALEMATE_OTHER))));
 
                        wpforenemy_announced = true;
                }
@@ -1040,8 +1031,7 @@ void ctf_FlagThink()
 
        // captureshield
        if(self == ctf_worldflaglist) // only for the first flag
-               FOR_EACH_CLIENT(tmp_entity)
-                       ctf_CaptureShield_Update(tmp_entity, 1); // release shield only
+               FOREACH_CLIENT(true, LAMBDA(ctf_CaptureShield_Update(it, 1))); // release shield only
 
        // sanity checks
        if(self.mins != CTF_FLAG.m_mins || self.maxs != CTF_FLAG.m_maxs) { // reset the flag boundaries in case it got squished
@@ -1199,7 +1189,7 @@ METHOD(Flag, giveTo, bool(Flag this, entity flag, entity toucher))
        }
 
        int num_perteam = 0;
-       entity tmp_entity; FOR_EACH_PLAYER(tmp_entity) if(SAME_TEAM(toucher, tmp_entity)) { ++num_perteam; }
+       FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(toucher, it), LAMBDA(++num_perteam));
 
        // special touch behaviors
        if(toucher.frozen) { return; }
@@ -1548,16 +1538,14 @@ int havocbot_ctf_teamcount(entity bot, vector org, float tc_radius)
                return 0;
 
        int c = 0;
-       entity head;
 
-       FOR_EACH_PLAYER(head)
-       {
-               if(DIFF_TEAM(head, bot) || head.deadflag != DEAD_NO || head == bot)
+       FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
+               if(DIFF_TEAM(it, bot) || it.deadflag != DEAD_NO || it == bot)
                        continue;
 
-               if(vlen(head.origin - org) < tc_radius)
+               if(vlen(it.origin - org) < tc_radius)
                        ++c;
-       }
+       ));
 
        return c;
 }
@@ -1697,7 +1685,7 @@ void havocbot_goalrating_ctf_carrieritems(float ratingscale, vector org, float s
 void havocbot_ctf_reset_role(entity bot)
 {
        float cdefense, cmiddle, coffense;
-       entity mf, ef, head;
+       entity mf, ef;
        float c;
 
        if(bot.deadflag != DEAD_NO)
@@ -1732,9 +1720,7 @@ void havocbot_ctf_reset_role(entity bot)
 
        // if there is only me on the team switch to offense
        c = 0;
-       FOR_EACH_PLAYER(head)
-       if(SAME_TEAM(head, bot))
-               ++c;
+       FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, bot), LAMBDA(++c));
 
        if(c==1)
        {
@@ -2086,20 +2072,16 @@ void havocbot_role_ctf_defense()
                navigation_goalrating_start();
 
                // if enemies are closer to our base, go there
-               entity head, closestplayer = world;
+               entity closestplayer = world;
                float distance, bestdistance = 10000;
-               FOR_EACH_PLAYER(head)
-               {
-                       if(head.deadflag!=DEAD_NO)
-                               continue;
-
-                       distance = vlen(org - head.origin);
+               FOREACH_CLIENT(IS_PLAYER(it) && it.deadflag == DEAD_NO, LAMBDA(
+                       distance = vlen(org - it.origin);
                        if(distance<bestdistance)
                        {
-                               closestplayer = head;
+                               closestplayer = it;
                                bestdistance = distance;
                        }
-               }
+               ));
 
                if(closestplayer)
                if(DIFF_TEAM(closestplayer, self))
@@ -2547,7 +2529,6 @@ MUTATOR_HOOKFUNCTION(ctf, SV_ParseClientCommand)
                if(!g_ctf)
                        return true;
 
-               entity _player;
                int _team = 0;
                bool found = false;
 
@@ -2562,16 +2543,15 @@ MUTATOR_HOOKFUNCTION(ctf, SV_ParseClientCommand)
                        }
                }
 
-               FOR_EACH_PLAYER(_player)
-               {
-                       if(_player.flagcarried && (_player.team == _team || _team == 0))
+               FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
+                       if(it.flagcarried && (it.team == _team || _team == 0))
                        {
                                found = true;
-                               if(_team == 0 && IS_SPEC(self) && self.enemy == _player)
-                                       continue; // already spectating a fc, try to find the other fc
-                               return superspec_Spectate(_player);
+                               if(_team == 0 && IS_SPEC(self) && self.enemy == it)
+                                       continue; // already spectating this fc, try another
+                               return superspec_Spectate(it);
                        }
-               }
+               ));
 
                if(!found)
                        superspec_msg("", "", self, "No active flag carrier\n", 1);