]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Replace some player loops with fancy ones
authorMario <mario@smbclan.net>
Wed, 23 Dec 2015 22:30:17 +0000 (08:30 +1000)
committerMario <mario@smbclan.net>
Wed, 23 Dec 2015 22:30:17 +0000 (08:30 +1000)
qcsrc/common/mutators/mutator/buffs/buffs.qc
qcsrc/common/mutators/mutator/superspec/superspec.qc
qcsrc/server/_all.qh
qcsrc/server/anticheat.qc
qcsrc/server/cl_player.qc
qcsrc/server/cl_player.qh
qcsrc/server/command/common.qc
qcsrc/server/sv_main.qc
qcsrc/server/teamplay.qc
qcsrc/server/weapons/weaponsystem.qc

index 23bf8a8d4055587f0b496956ae411fc8a0ba3a88..9552bc767c9eaef3e5074ca265ca60676194eb27 100644 (file)
@@ -697,15 +697,15 @@ MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
        {
                float best_distance = autocvar_g_buffs_swapper_range;
                entity closest = world;
-               entity player;
-               FOR_EACH_PLAYER(player)
-               if(DIFF_TEAM(self, player))
-               if(player.deadflag == DEAD_NO && !player.frozen && !player.vehicle)
-               if(vlen(self.origin - player.origin) <= best_distance)
-               {
-                       best_distance = vlen(self.origin - player.origin);
-                       closest = player;
-               }
+               FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
+                       if(it.deadflag == DEAD_NO && !it.frozen && !it.vehicle)
+                       if(DIFF_TEAM(it, self))
+                       if(vlen(self.origin - it.origin) <= best_distance)
+                       {
+                               best_distance = vlen(self.origin - it.origin);
+                               closest = it;
+                       }
+               ));
 
                if(closest)
                {
index 887b1842345098eb42b9d70d2468feb04a0be58c..3c183c726f0823722f0d78950aea46ad55862c05 100644 (file)
@@ -101,10 +101,10 @@ MUTATOR_HOOKFUNCTION(superspec, ItemTouch)
 {SELFPARAM();
        entity _item = self;
 
-       entity e;
-       FOR_EACH_CLIENT(e) if (IS_SPEC(e) || IS_OBSERVER(e))
-       {
-               setself(e);
+       FOREACH_CLIENT(true, LAMBDA(
+               if(!IS_SPEC(it) && !IS_OBSERVER(it))
+                       continue;
+               setself(it);
                if(self.superspec_flags & SSF_ITEMMSG)
                        if(superspec_filteritem(self, _item))
                        {
@@ -144,7 +144,7 @@ MUTATOR_HOOKFUNCTION(superspec, ItemTouch)
                                }
                        }
                }
-       }
+       ));
 
        setself(this);
 
index f277af62ab7a6953f7d2a7c96f2e914c8aa9d093..affb8e0b4e12522603798a6e6437c2ce49ba7561 100644 (file)
@@ -31,11 +31,11 @@ const string STR_OBSERVER = "observer";
 
 #define FOREACH_CLIENT(cond, body) \
        MACRO_BEGIN { \
-               int i = 0; \
-               for (entity it = NULL; (it = nextent(it)) && (etof(it) <= maxclients); ++i) \
+               for(int i = 1; i <= maxclients; ++i) \
                { \
-                       if (!IS_CLIENT(it)) continue; \
-                       if (cond) { LAMBDA(body) } \
+                       entity it = ftoe(i); \
+                       if(it == NULL || !IS_CLIENT(it)) continue; \
+                       if(cond) { LAMBDA(body) } \
                } \
        } MACRO_END
 
index 739695e77ea5aa9ff13d7a3887a37fa00762d9a2..79442299e197343ccd6b4c32aa769c0f7bf5e173 100644 (file)
@@ -232,11 +232,10 @@ void anticheat_fixangle()
 
 void anticheat_endframe()
 {SELFPARAM();
-       entity e;
-       FOR_EACH_CLIENT(e)
-               if (e.fixangle) {
-                       WITH(entity, self, e, anticheat_fixangle());
-               }
+       FOREACH_CLIENT(true, LAMBDA(
+               if(it.fixangle)
+                       WITH(entity, self, it, anticheat_fixangle());
+       ));
        anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
 }
 
index cbe38ac5a5cb16dd232eb2eea27d5757f10fe462..d205e65692fbdae38ed09d618d2e59f8e4265ac1 100644 (file)
@@ -209,8 +209,8 @@ void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, int de
        }
 }
 
-void calculate_player_respawn_time()
-{SELFPARAM();
+void calculate_player_respawn_time(entity this)
+{
        if(g_ca)
                return;
 
@@ -223,13 +223,12 @@ void calculate_player_respawn_time()
        float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
 
        float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
-       entity pl;
        if (teamplay)
        {
-               FOR_EACH_PLAYER(pl)
-                       if (pl != self)
-                               if (pl.team == self.team)
-                                       ++pcount;
+               FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
+                       if(it.team == this.team)
+                               ++pcount;
+               ));
                if (sdelay_small_count == 0)
                        sdelay_small_count = 1;
                if (sdelay_large_count == 0)
@@ -237,9 +236,9 @@ void calculate_player_respawn_time()
        }
        else
        {
-               FOR_EACH_PLAYER(pl)
-                       if (pl != self)
-                               ++pcount;
+               FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
+                       ++pcount;
+               ));
                if (sdelay_small_count == 0)
                {
                        if (g_cts)
@@ -278,22 +277,22 @@ void calculate_player_respawn_time()
                sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
 
        if(waves)
-               self.respawn_time = ceil((time + sdelay) / waves) * waves;
+               this.respawn_time = ceil((time + sdelay) / waves) * waves;
        else
-               self.respawn_time = time + sdelay;
+               this.respawn_time = time + sdelay;
 
        if(sdelay < sdelay_max)
-               self.respawn_time_max = time + sdelay_max;
+               this.respawn_time_max = time + sdelay_max;
        else
-               self.respawn_time_max = self.respawn_time;
+               this.respawn_time_max = this.respawn_time;
 
-       if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
-               self.respawn_countdown = 10; // first number to count down from is 10
+       if((sdelay + waves >= 5.0) && (this.respawn_time - time > 1.75))
+               this.respawn_countdown = 10; // first number to count down from is 10
        else
-               self.respawn_countdown = -1; // do not count down
+               this.respawn_countdown = -1; // do not count down
 
        if(autocvar_g_forced_respawn)
-               self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
+               this.respawn_flags = this.respawn_flags | RESPAWN_FORCE;
 }
 
 void PlayerDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
@@ -595,7 +594,7 @@ void PlayerDamage (entity inflictor, entity attacker, float damage, int deathtyp
                self.deadflag = DEAD_DYING;
 
                // when to allow respawn
-               calculate_player_respawn_time();
+               calculate_player_respawn_time(this);
 
                self.death_time = time;
                if (random() < 0.5)
index e55b1c6147fac06acf8af7e1b6bed992e73bc96e..b71a4e2d87641b8e39d3200b22f6f7fcdc5744ff 100644 (file)
@@ -27,7 +27,7 @@ void PlayerCorpseDamage(entity inflictor, entity attacker, float damage, int dea
        : (gametype_setting_tmp == 0 || autocvar_g_respawn_delay_forced) ? max(0, autocvar_g_##str) \
        : gametype_setting_tmp)
 
-void calculate_player_respawn_time();
+void calculate_player_respawn_time(entity this);
 
 void ClientKill_Now_TeamChange();
 
index 484ecb2104d208ac969ba5c50825eeb1e186d1c3..a252b1f501130a5e2705d1efe391d78f980d4411 100644 (file)
@@ -73,7 +73,7 @@ float VerifyClientNumber(float tmp_number)
 
 entity GetIndexedEntity(float argc, float start_index)
 {
-       entity tmp_player, selection;
+       entity selection;
        float tmp_number, index;
        string tmp_string;
 
@@ -114,8 +114,13 @@ entity GetIndexedEntity(float argc, float start_index)
                }
                else  // no, maybe it's a name?
                {
-                       FOR_EACH_CLIENT(tmp_player)
-                       if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index))) selection = tmp_player;
+                       FOREACH_CLIENT(true, LAMBDA(
+                               if(strdecolorize(it.netname) == strdecolorize(argv(start_index)))
+                               {
+                                       selection = it;
+                                       break; // no reason to keep looking
+                               }
+                       ));
 
                        index = (start_index + 1);
                }
@@ -129,7 +134,7 @@ entity GetIndexedEntity(float argc, float start_index)
 // find a player which matches the input string, and return their entity
 entity GetFilteredEntity(string input)
 {
-       entity tmp_player, selection;
+       entity selection;
        float tmp_number;
 
        if (substring(input, 0, 1) == "#") tmp_number = stof(substring(input, 1, -1));
@@ -142,8 +147,13 @@ entity GetFilteredEntity(string input)
        else
        {
                selection = world;
-               FOR_EACH_CLIENT(tmp_player)
-               if (strdecolorize(tmp_player.netname) == strdecolorize(input)) selection = tmp_player;
+               FOREACH_CLIENT(true, LAMBDA(
+                       if(strdecolorize(it.netname) == strdecolorize(input))
+                       {
+                               selection = it;
+                               break; // no reason to keep looking
+                       }
+               ));
        }
 
        return selection;
@@ -185,8 +195,6 @@ void timeout_handler_reset()
 void timeout_handler_think()
 {
        SELFPARAM();
-       entity tmp_player;
-
        switch (timeout_status)
        {
                case TIMEOUT_ACTIVE:
@@ -209,8 +217,9 @@ void timeout_handler_think()
                                cvar_set("slowmo", ftos(orig_slowmo));
 
                                // unlock the view for players so they can move around again
-                               FOR_EACH_REALPLAYER(tmp_player)
-                               tmp_player.fixangle = false;
+                               FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), LAMBDA(
+                                       it.fixangle = false;
+                               ));
 
                                timeout_handler_reset();
                        }
@@ -235,14 +244,16 @@ void timeout_handler_think()
                                cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
 
                                // reset all the flood variables
-                               FOR_EACH_CLIENT(tmp_player)
-                               tmp_player.nickspamcount = tmp_player.nickspamtime = tmp_player.floodcontrol_chat =
-                                           tmp_player.floodcontrol_chatteam = tmp_player.floodcontrol_chattell =
-                                                   tmp_player.floodcontrol_voice = tmp_player.floodcontrol_voiceteam = 0;
+                               FOREACH_CLIENT(true, LAMBDA(
+                                       it.nickspamcount = it.nickspamtime = it.floodcontrol_chat = 
+                                               it.floodcontrol_chatteam = it.floodcontrol_chattell =
+                                                       it.floodcontrol_voice = it.floodcontrol_voiceteam = 0;
+                               ));
 
                                // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
-                               FOR_EACH_REALPLAYER(tmp_player)
-                               tmp_player.lastV_angle = tmp_player.v_angle;
+                               FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), LAMBDA(
+                                       it.lastV_angle = it.v_angle;
+                               ));
 
                                self.nextthink = time;  // think again next frame to handle it under TIMEOUT_ACTIVE code
                        }
@@ -759,7 +770,6 @@ void CommonCommand_who(float request, entity caller, float argc)
                case CMD_REQUEST_COMMAND:
                {
                        float total_listed_players, is_bot;
-                       entity tmp_player;
 
                        float privacy = (caller && autocvar_sv_status_privacy);
                        string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
@@ -770,9 +780,8 @@ void CommonCommand_who(float request, entity caller, float argc)
                                "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
 
                        total_listed_players = 0;
-                       FOR_EACH_CLIENT(tmp_player)
-                       {
-                               is_bot = (IS_BOT_CLIENT(tmp_player));
+                       FOREACH_CLIENT(true, LAMBDA(
+                               is_bot = (IS_BOT_CLIENT(it));
 
                                if (is_bot)
                                {
@@ -786,21 +795,21 @@ void CommonCommand_who(float request, entity caller, float argc)
                                }
                                else
                                {
-                                       tmp_netaddress = tmp_player.netaddress;
-                                       tmp_crypto_idfp = tmp_player.crypto_idfp;
+                                       tmp_netaddress = it.netaddress;
+                                       tmp_crypto_idfp = it.crypto_idfp;
                                }
 
                                print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
-                                       etof(tmp_player),
-                                       tmp_player.netname,
-                                       tmp_player.ping,
-                                       tmp_player.ping_packetloss,
-                                       process_time(1, time - tmp_player.jointime),
+                                       etof(it),
+                                       it.netname,
+                                       it.ping,
+                                       it.ping_packetloss,
+                                       process_time(1, time - it.jointime),
                                        tmp_netaddress,
                                        tmp_crypto_idfp));
 
                                ++total_listed_players;
-                       }
+                       ));
 
                        print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
 
index cb348848950cf0d7733e9d164119aaae393d488a..427a215c8b54cdbd827dde9011f24960f6af9e21 100644 (file)
@@ -231,13 +231,8 @@ void StartFrame()
        bot_serverframe();
        anticheat_startframe();
        MUTATOR_CALLHOOK(SV_StartFrame);
-       {
-        entity e;
-        FOR_EACH_CLIENT(e)
-        {
-            GlobalStats_update(e);
-        }
-    }
+
+    FOREACH_CLIENT(true, LAMBDA(GlobalStats_update(it)));
 }
 
 .vector originjitter;
index 0662aab68ece77f479b001efc99f4a11cee5c488..15d33f5341527f5e03ad5fa4b5c62960c66e8d2f 100644 (file)
@@ -308,26 +308,24 @@ float PlayerValue(entity p)
 // teams that are allowed will now have their player counts stored in c1...c4
 void GetTeamCounts(entity ignore)
 {
-       entity head;
        float value, bvalue;
        // now count how many players are on each team already
 
        // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
        // also remember the lowest-scoring player
 
-       FOR_EACH_CLIENT(head)
-       {
+       FOREACH_CLIENT(true, LAMBDA(
                float t;
-               if(IS_PLAYER(head) || head.caplayer)
-                       t = head.team;
-               else if(head.team_forced > 0)
-                       t = head.team_forced; // reserve the spot
+               if(IS_PLAYER(it) || it.caplayer)
+                       t = it.team;
+               else if(it.team_forced > 0)
+                       t = it.team_forced; // reserve the spot
                else
                        continue;
-               if(head != ignore)// && head.netname != "")
+               if(it != ignore)// && it.netname != "")
                {
-                       value = PlayerValue(head);
-                       if(IS_BOT_CLIENT(head))
+                       value = PlayerValue(it);
+                       if(IS_BOT_CLIENT(it))
                                bvalue = value;
                        else
                                bvalue = 0;
@@ -364,7 +362,7 @@ void GetTeamCounts(entity ignore)
                                }
                        }
                }
-       }
+       ));
 
        // if the player who has a forced team has not joined yet, reserve the spot
        if(autocvar_g_campaign)
index 2aa11b2c962d4521ddf2ad69abd3cdf20edf2d26..abc2ffa802b657fd87dea9ee0b9a2106ad67e72d 100644 (file)
@@ -377,8 +377,10 @@ void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(
 
        if (this)
        {
-               entity e;
-               FOR_EACH_CLIENT(e) if (e == actor || (IS_SPEC(e) && e.enemy == actor)) wframe_send(e, this, a, restartanim);
+               FOREACH_CLIENT(true, LAMBDA(
+                       if(it == actor || (IS_SPEC(it) && it.enemy == actor))
+                               wframe_send(it, this, a, restartanim);
+               ));
        }
 
        if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)