From b6723f4e8fca782664298afefc180967bda936e0 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 24 Dec 2015 08:30:17 +1000 Subject: [PATCH] Replace some player loops with fancy ones --- qcsrc/common/mutators/mutator/buffs/buffs.qc | 18 ++--- .../mutators/mutator/superspec/superspec.qc | 10 +-- qcsrc/server/_all.qh | 8 +-- qcsrc/server/anticheat.qc | 9 ++- qcsrc/server/cl_player.qc | 37 +++++------ qcsrc/server/cl_player.qh | 2 +- qcsrc/server/command/common.qc | 65 +++++++++++-------- qcsrc/server/sv_main.qc | 9 +-- qcsrc/server/teamplay.qc | 20 +++--- qcsrc/server/weapons/weaponsystem.qc | 6 +- 10 files changed, 93 insertions(+), 91 deletions(-) diff --git a/qcsrc/common/mutators/mutator/buffs/buffs.qc b/qcsrc/common/mutators/mutator/buffs/buffs.qc index 23bf8a8d4..9552bc767 100644 --- a/qcsrc/common/mutators/mutator/buffs/buffs.qc +++ b/qcsrc/common/mutators/mutator/buffs/buffs.qc @@ -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) { diff --git a/qcsrc/common/mutators/mutator/superspec/superspec.qc b/qcsrc/common/mutators/mutator/superspec/superspec.qc index 887b18423..3c183c726 100644 --- a/qcsrc/common/mutators/mutator/superspec/superspec.qc +++ b/qcsrc/common/mutators/mutator/superspec/superspec.qc @@ -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); diff --git a/qcsrc/server/_all.qh b/qcsrc/server/_all.qh index f277af62a..affb8e0b4 100644 --- a/qcsrc/server/_all.qh +++ b/qcsrc/server/_all.qh @@ -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 diff --git a/qcsrc/server/anticheat.qc b/qcsrc/server/anticheat.qc index 739695e77..79442299e 100644 --- a/qcsrc/server/anticheat.qc +++ b/qcsrc/server/anticheat.qc @@ -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()); } diff --git a/qcsrc/server/cl_player.qc b/qcsrc/server/cl_player.qc index cbe38ac5a..d205e6569 100644 --- a/qcsrc/server/cl_player.qc +++ b/qcsrc/server/cl_player.qc @@ -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) diff --git a/qcsrc/server/cl_player.qh b/qcsrc/server/cl_player.qh index e55b1c614..b71a4e2d8 100644 --- a/qcsrc/server/cl_player.qh +++ b/qcsrc/server/cl_player.qh @@ -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(); diff --git a/qcsrc/server/command/common.qc b/qcsrc/server/command/common.qc index 484ecb210..a252b1f50 100644 --- a/qcsrc/server/command/common.qc +++ b/qcsrc/server/command/common.qc @@ -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.")); diff --git a/qcsrc/server/sv_main.qc b/qcsrc/server/sv_main.qc index cb3488489..427a215c8 100644 --- a/qcsrc/server/sv_main.qc +++ b/qcsrc/server/sv_main.qc @@ -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; diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index 0662aab68..15d33f534 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -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) diff --git a/qcsrc/server/weapons/weaponsystem.qc b/qcsrc/server/weapons/weaponsystem.qc index 2aa11b2c9..abc2ffa80 100644 --- a/qcsrc/server/weapons/weaponsystem.qc +++ b/qcsrc/server/weapons/weaponsystem.qc @@ -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) -- 2.39.2