From 1610d1259966fc7faba96a4a3fd1954f969da5d7 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 24 Dec 2015 14:58:24 +1000 Subject: [PATCH] Fix a bunch of loops --- qcsrc/server/mutators/mutator/gamemode_ctf.qc | 106 +++++++----------- .../mutators/mutator/gamemode_domination.qc | 41 +++---- .../mutators/mutator/gamemode_invasion.qc | 62 +++++----- .../mutators/mutator/gamemode_keyhunt.qc | 74 +++++------- .../server/mutators/mutator/gamemode_race.qc | 22 ++-- qcsrc/server/portals.qc | 19 ++-- 6 files changed, 133 insertions(+), 191 deletions(-) diff --git a/qcsrc/server/mutators/mutator/gamemode_ctf.qc b/qcsrc/server/mutators/mutator/gamemode_ctf.qc index d4524479d..9100c52ba 100644 --- a/qcsrc/server/mutators/mutator/gamemode_ctf.qc +++ b/qcsrc/server/mutators/mutator/gamemode_ctf.qc @@ -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 0 && round_handler_GetEndTime() - time <= 0) { - FOR_EACH_MONSTER(head) - Monster_Remove(head); + FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it))); Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER); Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER); @@ -206,21 +204,23 @@ float Invasion_CheckWinner() float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0; - FOR_EACH_MONSTER(head) if(head.health > 0) - { - if((get_monsterinfo(head.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER) - ++supermonster_count; - ++total_alive_monsters; - - if(teamplay) - switch(head.team) + FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA( + if(it.health > 0) { - case NUM_TEAM_1: ++red_alive; break; - case NUM_TEAM_2: ++blue_alive; break; - case NUM_TEAM_3: ++yellow_alive; break; - case NUM_TEAM_4: ++pink_alive; break; + if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER) + ++supermonster_count; + ++total_alive_monsters; + + if(teamplay) + switch(it.team) + { + case NUM_TEAM_1: ++red_alive; break; + case NUM_TEAM_2: ++blue_alive; break; + case NUM_TEAM_3: ++yellow_alive; break; + case NUM_TEAM_4: ++pink_alive; break; + } } - } + )); if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned) { @@ -262,18 +262,18 @@ float Invasion_CheckWinner() else { winner_team = NUM_TEAM_4; } } else - FOR_EACH_PLAYER(head) { - float cs = PlayerScore_Add(head, SP_KILLS, 0); - if(cs > winning_score) - { - winning_score = cs; - winner = head; - } + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + float cs = PlayerScore_Add(it, SP_KILLS, 0); + if(cs > winning_score) + { + winning_score = cs; + winner = it; + } + )); } - FOR_EACH_MONSTER(head) - Monster_Remove(head); + FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it))); if(teamplay) { @@ -294,20 +294,18 @@ float Invasion_CheckWinner() return 1; } -float Invasion_CheckPlayers() +bool Invasion_CheckPlayers() { return true; } void Invasion_RoundStart() { - entity e; - float numplayers = 0; - FOR_EACH_PLAYER(e) - { - e.player_blocked = 0; + int numplayers = 0; + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + it.player_blocked = false; ++numplayers; - } + )); if(inv_roundcnt < inv_maxrounds) inv_roundcnt += 1; // a limiter to stop crazy counts diff --git a/qcsrc/server/mutators/mutator/gamemode_keyhunt.qc b/qcsrc/server/mutators/mutator/gamemode_keyhunt.qc index e985ee5c0..042497254 100644 --- a/qcsrc/server/mutators/mutator/gamemode_keyhunt.qc +++ b/qcsrc/server/mutators/mutator/gamemode_keyhunt.qc @@ -158,7 +158,7 @@ void kh_ScoreRules(float teams) float kh_KeyCarrier_waypointsprite_visible_for_player(entity e) // runs all the time {SELFPARAM(); - if(!IS_PLAYER(e) || self.team != e.team) + if(!IS_PLAYER(e) || DIFF_TEAM(self, e)) if(!kh_tracking_enabled) return false; @@ -178,7 +178,6 @@ float kh_Key_waypointsprite_visible_for_player(entity e) // ?? void kh_update_state() { - entity player; entity key; float s; float f; @@ -193,10 +192,7 @@ void kh_update_state() s |= pow(32, key.count) * f; } - FOR_EACH_CLIENT(player) - { - player.kh_state = s; - } + FOREACH_CLIENT(true, LAMBDA(it.kh_state = s)); FOR_EACH_KH_KEY(key) { @@ -646,7 +642,7 @@ void kh_WinnerTeam(float teem) // runs when a team wins // Samual: Teem?.... TE void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a flag carrier off the map { - entity player, key, attacker; + entity key, attacker; float players; float keys; float f; @@ -673,9 +669,7 @@ void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a fl float of, fragsleft, i, j, thisteam; of = autocvar_g_balance_keyhunt_score_destroyed_ownfactor; - FOR_EACH_PLAYER(player) - if(player.team != teem) - ++players; + FOREACH_CLIENT(IS_PLAYER(it) && it.team != teem, LAMBDA(++players)); FOR_EACH_KH_KEY(key) if(key.owner && key.team != teem) @@ -708,20 +702,16 @@ void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a fl continue; players = 0; - FOR_EACH_PLAYER(player) - if(player.team == thisteam) - ++players; + FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA(++players)); DistributeEvenly_Init(fragsleft, j); fragsleft = DistributeEvenly_Get(j - 1); DistributeEvenly_Init(DistributeEvenly_Get(1), players); - FOR_EACH_PLAYER(player) - if(player.team == thisteam) - { - f = DistributeEvenly_Get(1); - kh_Scores_Event(player, world, "destroyed", f, 0); - } + FOREACH_CLIENT(IS_PLAYER(it) && it.team == thisteam, LAMBDA( + f = DistributeEvenly_Get(1); + kh_Scores_Event(it, world, "destroyed", f, 0); + )); --j; } @@ -737,9 +727,6 @@ void kh_LoserTeam(float teem, entity lostkey) // runs when a player pushes a fl void kh_Key_Think() // runs all the time {SELFPARAM(); - entity head; - //entity player; // needed by FOR_EACH_PLAYER - if(intermission_running) return; @@ -778,16 +765,15 @@ void kh_Key_Think() // runs all the time if(kh_interferemsg_time && time > kh_interferemsg_time) { kh_interferemsg_time = 0; - FOR_EACH_PLAYER(head) - { - if(head.team == kh_interferemsg_team) - if(head.kh_next) - Send_Notification(NOTIF_ONE, head, MSG_CENTER, CENTER_KEYHUNT_MEET); + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + if(it.team == kh_interferemsg_team) + if(it.kh_next) + Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_MEET); else - Send_Notification(NOTIF_ONE, head, MSG_CENTER, CENTER_KEYHUNT_HELP); + Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_KEYHUNT_HELP); else - Send_Notification(NOTIF_ONE, head, MSG_CENTER, APP_TEAM_NUM_4(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE_)); - } + Send_Notification(NOTIF_ONE, it, MSG_CENTER, APP_TEAM_NUM_4(kh_interferemsg_team, CENTER_KEYHUNT_INTERFERE_)); + )); } self.nextthink = time + 0.05; @@ -932,12 +918,10 @@ float kh_CheckPlayers(float num) { float t_team = kh_Team_ByID(num); float players = 0; - entity tmp_player; - FOR_EACH_PLAYER(tmp_player) - if(tmp_player.deadflag == DEAD_NO) - if(!tmp_player.BUTTON_CHAT) - if(tmp_player.team == t_team) - ++players; + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + if(it.deadflag == DEAD_NO && !it.BUTTON_CHAT && it.team == t_team) + ++players; + )); if (!players) { return t_team; } } @@ -998,7 +982,6 @@ void kh_EnableTrackingDevice() // runs after each round void kh_StartRound() // runs at the start of each round { float i, players, teem; - entity player; if(time < game_starttime) { @@ -1021,15 +1004,14 @@ void kh_StartRound() // runs at the start of each round teem = kh_Team_ByID(i); players = 0; entity my_player = world; - FOR_EACH_PLAYER(player) - if(player.deadflag == DEAD_NO) - if(!player.BUTTON_CHAT) - if(player.team == teem) - { - ++players; - if(random() * players <= 1) - my_player = player; - } + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + if(it.deadflag == DEAD_NO && !it.BUTTON_CHAT && it.team == teem) + { + ++players; + if(random() * players <= 1) + my_player = it; + } + )); kh_Key_Spawn(my_player, 360 * i / kh_teams, i); } diff --git a/qcsrc/server/mutators/mutator/gamemode_race.qc b/qcsrc/server/mutators/mutator/gamemode_race.qc index 124bc653a..8b1375694 100644 --- a/qcsrc/server/mutators/mutator/gamemode_race.qc +++ b/qcsrc/server/mutators/mutator/gamemode_race.qc @@ -110,17 +110,15 @@ void race_EventLog(string mode, entity actor) // use an alias for easy changing float WinningCondition_Race(float fraglimit) { float wc; - entity p; float n, c; n = 0; c = 0; - FOR_EACH_PLAYER(p) - { + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( ++n; - if(p.race_completed) + if(it.race_completed) ++c; - } + )); if(n && (n == c)) return WINNING_YES; wc = WinningCondition_Scores(fraglimit, 0); @@ -228,17 +226,15 @@ MUTATOR_HOOKFUNCTION(rc, reset_map_global) race_ClearRecords(); PlayerScore_Sort(race_place, 0, 1, 0); - entity e; - FOR_EACH_CLIENT(e) - { - if(e.race_place) + FOREACH_CLIENT(true, LAMBDA( + if(it.race_place) { - s = PlayerScore_Add(e, SP_RACE_FASTEST, 0); + s = PlayerScore_Add(it, SP_RACE_FASTEST, 0); if(!s) - e.race_place = 0; + it.race_place = 0; } - race_EventLog(ftos(e.race_place), e); - } + race_EventLog(ftos(it.race_place), it); + )); if(g_race_qualifying == 2) { diff --git a/qcsrc/server/portals.qc b/qcsrc/server/portals.qc index 8d6f9dc70..8f311f169 100644 --- a/qcsrc/server/portals.qc +++ b/qcsrc/server/portals.qc @@ -454,7 +454,7 @@ void Portal_Think_TryTeleportPlayer(entity e, vector g) void Portal_Think() {SELFPARAM(); - entity e, o; + entity o; vector g; #ifdef PORTALS_ARE_NOT_SOLID @@ -473,18 +473,17 @@ void Portal_Think() fixedmakevectors(self.mangle); - FOR_EACH_PLAYER(e) - { - if(e != o) - if(IS_INDEPENDENT_PLAYER(e) || IS_INDEPENDENT_PLAYER(o)) + FOREACH_CLIENT(IS_PLAYER(it), LAMBDA( + if(it != o) + if(IS_INDEPENDENT_PLAYER(it) || IS_INDEPENDENT_PLAYER(o)) continue; // cannot go through someone else's portal - if(e != o || time >= self.portal_activatetime) - Portal_Think_TryTeleportPlayer(e, g); + if(it != o || time >= self.portal_activatetime) + Portal_Think_TryTeleportPlayer(it, g); - if(e.hook) - Portal_Think_TryTeleportPlayer(e.hook, g); - } + if(it.hook) + Portal_Think_TryTeleportPlayer(it.hook, g); + )); self.solid = SOLID_TRIGGER; self.aiment = o; #endif -- 2.39.2