diff --git a/CalPlayer/CalamityPlayerOnHit.cs b/CalPlayer/CalamityPlayerOnHit.cs index a6e99b607d..71ae324a97 100644 --- a/CalPlayer/CalamityPlayerOnHit.cs +++ b/CalPlayer/CalamityPlayerOnHit.cs @@ -1405,16 +1405,15 @@ namespace CalamityMod.CalPlayer float lowestHealthCheck = 0f; int healTarget = Player.whoAmI; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player otherPlayer in Main.ActivePlayers) { - Player otherPlayer = Main.player[i]; - if (otherPlayer.active && !otherPlayer.dead && ((!Player.hostile && !otherPlayer.hostile) || Player.team == otherPlayer.team)) + if (!otherPlayer.dead && ((!Player.hostile && !otherPlayer.hostile) || Player.team == otherPlayer.team)) { float playerDist = Vector2.Distance(target.Center, otherPlayer.Center); if (playerDist < 1200f && (otherPlayer.statLifeMax2 - otherPlayer.statLife) > lowestHealthCheck) { lowestHealthCheck = otherPlayer.statLifeMax2 - otherPlayer.statLife; - healTarget = i; + healTarget = otherPlayer.whoAmI; } } } diff --git a/Events/AcidRainEvent.cs b/Events/AcidRainEvent.cs index d946bc32a9..6c7ee3be30 100644 --- a/Events/AcidRainEvent.cs +++ b/Events/AcidRainEvent.cs @@ -107,14 +107,8 @@ namespace CalamityMod.Events { get { - int playerCount = 0; - for (int i = 0; i < Main.player.Length; i++) - { - if (Main.player[i].active) - { - playerCount++; - } - } + int playerCount = Main.CurrentFrameFlags.ActivePlayersCount; + if (DownedBossSystem.downedPolterghast) return (int)(Math.Log(playerCount + Math.E - 1) * 170f); else if (DownedBossSystem.downedAquaticScourge) @@ -189,12 +183,7 @@ namespace CalamityMod.Events if (AcidRainEventIsOngoing || (!NPC.downedBoss1 && !Main.hardMode && !DownedBossSystem.downedAquaticScourge) || BossRushEvent.BossRushActive) return; - int playerCount = 0; - for (int i = 0; i < Main.player.Length; i++) - { - if (Main.player[i].active) - playerCount++; - } + int playerCount = Main.CurrentFrameFlags.ActivePlayersCount; if (playerCount > 0) { @@ -243,12 +232,8 @@ namespace CalamityMod.Events if (!HasStartedAcidicDownpour) { int sulphSeaWidth = SulphurousSea.BiomeWidth; - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[playerIndex]; - if (!player.active) - continue; - // An artificial biome can be made, and therefore, the event could be started by an artificial biome. // While fighting the event in an artificial biome is not bad, having it be started by a patch of Sulphurous Sand // would definitely be strange. @@ -387,11 +372,9 @@ namespace CalamityMod.Events if ((int)Main.time == (int)(Main.nightLength - 1f) && !Main.dayTime && Main.rand.NextBool(increasedEventChance ? 3 : 300)) { bool shouldNotStartEvent = false; - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player player in Main.ActivePlayers) { - if (!Main.player[playerIndex].active) - continue; - if (Main.player[playerIndex].Calamity().noStupidNaturalARSpawns) + if (player.Calamity().noStupidNaturalARSpawns) { shouldNotStartEvent = true; break; @@ -407,9 +390,9 @@ namespace CalamityMod.Events // Attempt to force an Acid Rain immediately after the EoC is dead when someone wanders to the sea. if (NPC.downedBoss1 && !DownedBossSystem.downedEoCAcidRain && !HasBeenForceStartedByEoCDefeat) { - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[playerIndex].active && Main.player[playerIndex].Calamity().ZoneSulphur) + if (player.Calamity().ZoneSulphur) { HasBeenForceStartedByEoCDefeat = true; TryStartEvent(); diff --git a/Events/BossRushEvent.cs b/Events/BossRushEvent.cs index d1bb856f19..1fcad8cc9b 100644 --- a/Events/BossRushEvent.cs +++ b/Events/BossRushEvent.cs @@ -318,14 +318,10 @@ namespace CalamityMod.Events new Boss(ModContent.NPCType(), spawnContext: type => { - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[playerIndex]; - if (p is not null && p.active) - { - if (p.FindBuffIndex(ModContent.BuffType()) > -1) - p.ClearBuff(ModContent.BuffType()); - } + if (p.FindBuffIndex(ModContent.BuffType()) > -1) + p.ClearBuff(ModContent.BuffType()); } Player player = Main.player[ClosestPlayerToWorldCenter]; @@ -413,19 +409,15 @@ namespace CalamityMod.Events BossRushDialogueSystem.StartDialogue(BossRushDialoguePhase.TierOneComplete); // Teleport players to where they came from - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[playerIndex]; - if (p is not null && p.active) + if (p.Calamity().BossRushReturnPosition.HasValue) { - if (p.Calamity().BossRushReturnPosition.HasValue) - { - CalamityPlayer.ModTeleport(p, p.Calamity().BossRushReturnPosition.Value, false, TeleportationStyleID.TeleportationPotion); - p.Calamity().BossRushReturnPosition = null; - } + CalamityPlayer.ModTeleport(p, p.Calamity().BossRushReturnPosition.Value, false, TeleportationStyleID.TeleportationPotion); p.Calamity().BossRushReturnPosition = null; - SoundEngine.PlaySound(TeleportSound with { Volume = 1.6f }, p.Center); } + p.Calamity().BossRushReturnPosition = null; + SoundEngine.PlaySound(TeleportSound with { Volume = 1.6f }, p.Center); } }, // Plantera: End of Tier 2 @@ -622,13 +614,9 @@ namespace CalamityMod.Events public static void End() { // Reset BossRushReturnPosition - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[playerIndex]; - if (p is not null && p.active) - { - p.Calamity().BossRushReturnPosition = null; - } + p.Calamity().BossRushReturnPosition = null; } if (Main.netMode == NetmodeID.SinglePlayer) @@ -729,18 +717,14 @@ namespace CalamityMod.Events CalamityUtils.KillAllHostileProjectiles(); HostileProjectileKillCounter = 3; - for (int playerIndex = 0; playerIndex < Main.maxPlayers; playerIndex++) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[playerIndex]; - if (p is not null && p.active) - { - p.Calamity().BossRushReturnPosition = p.Center; - Vector2? underworld = CalamityPlayer.GetUnderworldPosition(p); - if (!underworld.HasValue) - break; - CalamityPlayer.ModTeleport(p, underworld.Value, false, TeleportationStyleID.TeleportationPotion); - SoundEngine.PlaySound(TeleportSound with { Volume = 1.6f }, p.Center); - } + p.Calamity().BossRushReturnPosition = p.Center; + Vector2? underworld = CalamityPlayer.GetUnderworldPosition(p); + if (!underworld.HasValue) + break; + CalamityPlayer.ModTeleport(p, underworld.Value, false, TeleportationStyleID.TeleportationPotion); + SoundEngine.PlaySound(TeleportSound with { Volume = 1.6f }, p.Center); } } @@ -788,12 +772,12 @@ namespace CalamityMod.Events { if (Main.netMode != NetmodeID.MultiplayerClient) { - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player p in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (p.dead) continue; - int animation = Projectile.NewProjectile(new EntitySource_WorldEvent(), Main.player[i].Center, Vector2.Zero, ModContent.ProjectileType(), 0, 0f, i); + int animation = Projectile.NewProjectile(new EntitySource_WorldEvent(), p.Center, Vector2.Zero, ModContent.ProjectileType(), 0, 0f, p.whoAmI); if (Main.projectile.IndexInRange(animation)) Main.projectile[animation].ai[0] = tier; } diff --git a/Items/Accessories/ProfanedSoulArtifact.cs b/Items/Accessories/ProfanedSoulArtifact.cs index 73435d4366..5135eb5d63 100644 --- a/Items/Accessories/ProfanedSoulArtifact.cs +++ b/Items/Accessories/ProfanedSoulArtifact.cs @@ -36,10 +36,9 @@ namespace CalamityMod.Items.Accessories get { bool result = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -110,10 +109,9 @@ namespace CalamityMod.Items.Accessories // Visibility is not net synced, for example. bool alreadyDrawnShieldForPlayer = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -127,6 +125,7 @@ namespace CalamityMod.Items.Accessories // Scale the shield is drawn at. // The "i" parameter is to make different player's shields not be perfectly synced. + int i = player.whoAmI; float scale = 0.15f + 0.03f * (0.5f + 0.5f * (float)Math.Sin(Main.GlobalTimeWrappedHourly * 0.5f + i * 0.2f)); if (!alreadyDrawnShieldForPlayer) diff --git a/Items/Accessories/ProfanedSoulCrystal.cs b/Items/Accessories/ProfanedSoulCrystal.cs index 2e1ec84035..acee5d0e7e 100644 --- a/Items/Accessories/ProfanedSoulCrystal.cs +++ b/Items/Accessories/ProfanedSoulCrystal.cs @@ -42,10 +42,9 @@ namespace CalamityMod.Items.Accessories get { bool result = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); diff --git a/Items/Accessories/RoverDrive.cs b/Items/Accessories/RoverDrive.cs index cb944d74ce..439eb1af70 100644 --- a/Items/Accessories/RoverDrive.cs +++ b/Items/Accessories/RoverDrive.cs @@ -36,10 +36,9 @@ namespace CalamityMod.Items.Accessories get { bool result = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -104,10 +103,9 @@ namespace CalamityMod.Items.Accessories // Visibility is not net synced, for example. bool alreadyDrawnShieldForPlayer = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -119,7 +117,7 @@ namespace CalamityMod.Items.Accessories continue; // The shield very gently grows and shrinks - float scale = 0.15f + 0.03f * (0.5f + 0.5f * (float)Math.Sin(Main.GlobalTimeWrappedHourly * 0.5f + i * 0.2f)); + float scale = 0.15f + 0.03f * (0.5f + 0.5f * (float)Math.Sin(Main.GlobalTimeWrappedHourly * 0.5f + player.whoAmI * 0.2f)); if (!alreadyDrawnShieldForPlayer) { diff --git a/Items/Accessories/TheSponge.cs b/Items/Accessories/TheSponge.cs index 0c105a0a3d..bc93e1448c 100644 --- a/Items/Accessories/TheSponge.cs +++ b/Items/Accessories/TheSponge.cs @@ -45,10 +45,9 @@ namespace CalamityMod.Items.Accessories get { bool result = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -171,10 +170,9 @@ namespace CalamityMod.Items.Accessories // Visibility is not net synced, for example. bool alreadyDrawnShieldForPlayer = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -187,6 +185,7 @@ namespace CalamityMod.Items.Accessories // Scale the shield is drawn at. The Sponge shield gently grows and shrinks; it should be largely imperceptible. // The "i" parameter is to make different player's shields not be perfectly synced. + int i = player.whoAmI; float baseScale = 0.155f; float maxExtraScale = 0.025f; float extraScalePulseInterpolant = MathF.Pow(4f, MathF.Sin(Main.GlobalTimeWrappedHourly * 0.791f + i) - 1); diff --git a/Items/Armor/LunicCorps/LunicCorpsHelmet.cs b/Items/Armor/LunicCorps/LunicCorpsHelmet.cs index 58a4fa7c33..611ea672bc 100644 --- a/Items/Armor/LunicCorps/LunicCorpsHelmet.cs +++ b/Items/Armor/LunicCorps/LunicCorpsHelmet.cs @@ -41,10 +41,9 @@ namespace CalamityMod.Items.Armor.LunicCorps get { bool result = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -112,10 +111,9 @@ namespace CalamityMod.Items.Armor.LunicCorps // Visibility is not net synced, for example. bool alreadyDrawnShieldForPlayer = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active || player.outOfRange || player.dead) + if (player.outOfRange || player.dead) continue; CalamityPlayer modPlayer = player.Calamity(); @@ -126,6 +124,7 @@ namespace CalamityMod.Items.Armor.LunicCorps // Scale the shield is drawn at. The Lunic Corps shield sticks very close to the body to mimic Halo and occasionally pulses. // The "i" parameter is to make different player's shields not be perfectly synced. + int i = player.whoAmI; float baseScale = 0.11f; float maxExtraScale = 0.013f; float extraScalePulseInterpolant = MathF.Pow(12f, MathF.Sin(Main.GlobalTimeWrappedHourly * 1.6f + i) - 1); diff --git a/Items/Materials/WulfrumMetalScrap.cs b/Items/Materials/WulfrumMetalScrap.cs index 5752684df2..1e32325171 100644 --- a/Items/Materials/WulfrumMetalScrap.cs +++ b/Items/Materials/WulfrumMetalScrap.cs @@ -44,9 +44,9 @@ namespace CalamityMod.Items.Materials bool closePlayer = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - if ((Main.player[i].Center - Item.Center).Length() < 1200 && Main.player[i].GetModPlayer().battery) + if ((player.Center - Item.Center).Length() < 1200 && player.GetModPlayer().battery) { closePlayer = true; break; diff --git a/Items/Mounts/AlicornMount.cs b/Items/Mounts/AlicornMount.cs index 3e03ae4e5b..7f363781da 100644 --- a/Items/Mounts/AlicornMount.cs +++ b/Items/Mounts/AlicornMount.cs @@ -85,12 +85,8 @@ namespace CalamityMod.Items.Mounts public override void Dismount(Player player, ref bool skipDust) { bool anyPlayerOnFabMount = false; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player2 in Main.ActivePlayers) { - Player player2 = Main.player[i]; - if (!player2.active) - continue; - // The player that is dismounting is technically not on the mount anymore. if (player2.Calamity().fab && player2.whoAmI != player.whoAmI) { diff --git a/Items/Weapons/Rogue/Mycoroot.cs b/Items/Weapons/Rogue/Mycoroot.cs index 1234245668..c168bd84f7 100644 --- a/Items/Weapons/Rogue/Mycoroot.cs +++ b/Items/Weapons/Rogue/Mycoroot.cs @@ -43,12 +43,11 @@ namespace CalamityMod.Items.Weapons.Rogue if (spore.WithinBounds(Main.maxProjectiles)) Main.projectile[spore].ai[1] = 1f; } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player other in Main.ActivePlayers) { - Player other = Main.player[i]; - if (other is null || !other.active || other.dead) + if (other.dead) continue; - if ((other.team == player.team && player.team != 0) || player.whoAmI == i) + if ((other.team == player.team && player.team != 0) || player.whoAmI == other.whoAmI) { if (player.Distance(other.Center) <= 800f) other.AddBuff(ModContent.BuffType(), 900); diff --git a/NPCs/Abyss/BabyCannonballJellyfish.cs b/NPCs/Abyss/BabyCannonballJellyfish.cs index 1799c81f6d..f37e23f812 100644 --- a/NPCs/Abyss/BabyCannonballJellyfish.cs +++ b/NPCs/Abyss/BabyCannonballJellyfish.cs @@ -158,8 +158,13 @@ namespace CalamityMod.NPCs.Abyss NPC.height = (int)(NPC.height * 3f); NPC.position -= NPC.Size * 0.5f; //hitbox expansion + adjustments SoundEngine.PlaySound(SoundID.Item14, NPC.Center); //bomb sound - foreach (Player player in Main.player.Where(player => player.active && !player.dead && NPC.Hitbox.Intersects(player.Hitbox))) + foreach (Player player in Main.ActivePlayers) + { + if (player.dead || !NPC.Hitbox.Intersects(player.Hitbox)) + continue; + player.Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), explosionDamage, NPC.direction); + } for (int k = 0; k < 8; k++) { int dust = Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.MoonBoulder, 0f, -1f, 0, default, 1f); diff --git a/NPCs/Abyss/CannonballJellyfish.cs b/NPCs/Abyss/CannonballJellyfish.cs index 9ac54fa763..b13a6e1a4a 100644 --- a/NPCs/Abyss/CannonballJellyfish.cs +++ b/NPCs/Abyss/CannonballJellyfish.cs @@ -267,8 +267,14 @@ namespace CalamityMod.NPCs.Abyss NPC.width = NPC.height; NPC.position -= NPC.Size * 0.5f; //hitbox expansion + adjustments SoundEngine.PlaySound(SoundID.Item14, NPC.Center); //bomb sound - foreach (Player player in Main.player.Where(player => player.active && !player.dead && NPC.Hitbox.Intersects(player.Hitbox))) - player.Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage * 3, NPC.direction); //Due to how slow the jelly is, this might catch you unaware, but then you'll never get hit by it again + foreach (Player player in Main.ActivePlayers) + { + if (player.dead || !NPC.Hitbox.Intersects(player.Hitbox)) + continue; + + //Due to how slow the jelly is, this might catch you unaware, but then you'll never get hit by it again + player.Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage * 3, NPC.direction); + } for (int k = 0; k < 25; k++) { int dust = Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.MoonBoulder, 0f, -1f, 0, default, 2f); diff --git a/NPCs/AcidRain/BloodwormNormal.cs b/NPCs/AcidRain/BloodwormNormal.cs index c303aa1459..a525547dcb 100644 --- a/NPCs/AcidRain/BloodwormNormal.cs +++ b/NPCs/AcidRain/BloodwormNormal.cs @@ -56,10 +56,9 @@ namespace CalamityMod.NPCs.AcidRain NPC.velocity.X = xSpeed * NPC.ai[0]; NPC.spriteDirection = (int)NPC.ai[0]; bool flee = false; - for (int i = 0; i < Main.player.Length; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player.active && !player.dead && Vector2.Distance(player.Center, NPC.Center) <= 220f) + if (!player.dead && Vector2.Distance(player.Center, NPC.Center) <= 220f) { flee = true; break; diff --git a/NPCs/Astral/Atlas.cs b/NPCs/Astral/Atlas.cs index 10abbd321b..7baa072efa 100644 --- a/NPCs/Astral/Atlas.cs +++ b/NPCs/Astral/Atlas.cs @@ -263,13 +263,13 @@ namespace CalamityMod.NPCs.Astral centerY - height / 2, width, height); - for (int i = 0; i < Main.player.Length; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i].active && !Main.player[i].dead && Main.player[i].getRect().Intersects(hitbox)) + if (!player.dead && player.getRect().Intersects(hitbox)) { - Vector2 before = Main.player[i].velocity; - Main.player[i].Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage, NPC.direction); - Vector2 after = Main.player[i].velocity; + Vector2 before = player.velocity; + player.Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage, NPC.direction); + Vector2 after = player.velocity; Vector2 difference = after - before; float horMult = 3.5f; @@ -284,7 +284,7 @@ namespace CalamityMod.NPCs.Astral swingYeet = true; } - if (Main.player[i].noKnockback) + if (player.noKnockback) { horMult *= 0.5f; verMult *= 0.5f; @@ -292,7 +292,7 @@ namespace CalamityMod.NPCs.Astral difference *= new Vector2(horMult, verMult); - Main.player[i].velocity = before + difference; + player.velocity = before + difference; } } } diff --git a/NPCs/Astral/Nova.cs b/NPCs/Astral/Nova.cs index 4453eb8aea..3e29dbb6e2 100644 --- a/NPCs/Astral/Nova.cs +++ b/NPCs/Astral/Nova.cs @@ -209,12 +209,12 @@ namespace CalamityMod.NPCs.Astral if (Main.netMode != NetmodeID.MultiplayerClient) { - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i].getRect().Intersects(myRect)) + if (player.getRect().Intersects(myRect)) { - int direction = NPC.Center.X - Main.player[i].Center.X < 0 ? -1 : 1; - Main.player[i].Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage, direction); + int direction = NPC.Center.X - player.Center.X < 0 ? -1 : 1; + player.Hurt(PlayerDeathReason.ByNPC(NPC.whoAmI), NPC.damage, direction); } } } diff --git a/NPCs/CalamityAI.cs b/NPCs/CalamityAI.cs index 0bada01b73..a8fcf6dc2e 100644 --- a/NPCs/CalamityAI.cs +++ b/NPCs/CalamityAI.cs @@ -3934,16 +3934,13 @@ namespace CalamityMod.NPCs if (npc.position.Y > player.position.Y) { - for (int m = 0; m < Main.maxPlayers; m++) + foreach (Player m in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle rectangle2 = new Rectangle((int)m.position.X - noFlyZone, (int)m.position.Y - noFlyZone, noFlyZone * 2, height); + if (rectangle.Intersects(rectangle2)) { - Rectangle rectangle2 = new Rectangle((int)Main.player[m].position.X - noFlyZone, (int)Main.player[m].position.Y - noFlyZone, noFlyZone * 2, height); - if (rectangle.Intersects(rectangle2)) - { - outsideNoFlyZone = false; - break; - } + outsideNoFlyZone = false; + break; } } if (outsideNoFlyZone) @@ -4522,20 +4519,20 @@ namespace CalamityMod.NPCs } float succPower = 0.125f + finalPhaseDustRatio * 0.125f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - float distance = Vector2.Distance(Main.player[i].Center, npc.Center); - if (distance < suckDistance && Main.player[i].grappling[0] == -1) + float distance = Vector2.Distance(plr.Center, npc.Center); + if (distance < suckDistance && plr.grappling[0] == -1) { - if (Collision.CanHit(npc.Center, 1, 1, Main.player[i].Center, 1, 1)) + if (Collision.CanHit(npc.Center, 1, 1, plr.Center, 1, 1)) { float distanceRatio = distance / suckDistance; float multiplier = 1f - distanceRatio; - if (Main.player[i].Center.X < npc.Center.X) - Main.player[i].velocity.X += succPower * multiplier; + if (plr.Center.X < npc.Center.X) + plr.velocity.X += succPower * multiplier; else - Main.player[i].velocity.X -= succPower * multiplier; + plr.velocity.X -= succPower * multiplier; } } } diff --git a/NPCs/CalamityGlobalAI.cs b/NPCs/CalamityGlobalAI.cs index 1bf8483ec5..c6d2fbd8e3 100644 --- a/NPCs/CalamityGlobalAI.cs +++ b/NPCs/CalamityGlobalAI.cs @@ -1161,11 +1161,11 @@ namespace CalamityMod.NPCs int nailCount = Main.rand.Next(3, 6); int[] players = new int[nailCount]; int i = 0; - for (int j = 0; j < Main.player.Length; j++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[j].active && !Main.player[j].dead && Collision.CanHitLine(npc.position, npc.width, npc.height, Main.player[j].position, Main.player[j].width, Main.player[j].height)) + if (!plr.dead && Collision.CanHitLine(npc.position, npc.width, npc.height, plr.position, plr.width, plr.height)) { - players[i] = j; + players[i] = plr.whoAmI; i++; if (i == nailCount) { @@ -7637,9 +7637,9 @@ namespace CalamityMod.NPCs bool spawnTwinkle = npc.ai[1] >= (CalamityWorld.death ? 60f : 120f); if (!spawnTwinkle && npc.velocity.Y == 0f) { - for (int j = 0; j < Main.maxPlayers; j++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[j].active && !Main.player[j].dead && Main.player[j].Distance(npc.Center) < 800f && Main.player[j].Center.Y < npc.Center.Y && Math.Abs(Main.player[j].Center.X - npc.Center.X) < 20f) + if (!plr.dead && plr.Distance(npc.Center) < 800f && plr.Center.Y < npc.Center.Y && Math.Abs(plr.Center.X - npc.Center.X) < 20f) { spawnTwinkle = true; break; diff --git a/NPCs/CalamityGlobalNPC.cs b/NPCs/CalamityGlobalNPC.cs index 32d244820c..bee91818bf 100644 --- a/NPCs/CalamityGlobalNPC.cs +++ b/NPCs/CalamityGlobalNPC.cs @@ -6230,9 +6230,9 @@ namespace CalamityMod.NPCs #region Player Counts public static bool AnyLivingPlayers() { - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i] != null && Main.player[i].active && !Main.player[i].dead && !Main.player[i].ghost) + if (!player.dead && !player.ghost) { return true; } @@ -6247,15 +6247,7 @@ namespace CalamityMod.NPCs return 1; } - int players = 0; - for (int i = 0; i < Main.maxPlayers; i++) - { - if (Main.player[i] != null && Main.player[i].active) - { - players++; - } - } - return players; + return Main.CurrentFrameFlags.ActivePlayersCount; } #endregion diff --git a/NPCs/DesertScourge/DesertNuisanceHead.cs b/NPCs/DesertScourge/DesertNuisanceHead.cs index 9c0732dded..70af0714f8 100644 --- a/NPCs/DesertScourge/DesertNuisanceHead.cs +++ b/NPCs/DesertScourge/DesertNuisanceHead.cs @@ -167,16 +167,13 @@ namespace CalamityMod.NPCs.DesertScourge bool shouldDirectlyChase = true; if (NPC.position.Y > Main.player[NPC.target].position.Y) { - for (int m = 0; m < 255; m++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle rectangle2 = new Rectangle((int)plr.position.X - directChaseDistance, (int)plr.position.Y - directChaseDistance, directChaseDistance * 2, directChaseDistance * 2); + if (rectangle.Intersects(rectangle2)) { - Rectangle rectangle2 = new Rectangle((int)Main.player[m].position.X - directChaseDistance, (int)Main.player[m].position.Y - directChaseDistance, directChaseDistance * 2, directChaseDistance * 2); - if (rectangle.Intersects(rectangle2)) - { - shouldDirectlyChase = false; - break; - } + shouldDirectlyChase = false; + break; } } if (shouldDirectlyChase) diff --git a/NPCs/DesertScourge/DesertScourgeHead.cs b/NPCs/DesertScourge/DesertScourgeHead.cs index 4b558ac7d8..1ad7c807c8 100644 --- a/NPCs/DesertScourge/DesertScourgeHead.cs +++ b/NPCs/DesertScourge/DesertScourgeHead.cs @@ -354,18 +354,15 @@ namespace CalamityMod.NPCs.DesertScourge { int rectWidth = directChaseDistance * 2; int rectHeight = directChaseDistance * 2; - for (int m = 0; m < 255; m++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[m].active) + int rectX = (int)plr.position.X - directChaseDistance; + int rectY = (int)plr.position.Y - directChaseDistance; + Rectangle directChaseRect = new Rectangle(rectX, rectY, rectWidth, rectHeight); + if (rectangle.Intersects(directChaseRect)) { - int rectX = (int)Main.player[m].position.X - directChaseDistance; - int rectY = (int)Main.player[m].position.Y - directChaseDistance; - Rectangle directChaseRect = new Rectangle(rectX, rectY, rectWidth, rectHeight); - if (rectangle.Intersects(directChaseRect)) - { - shouldDirectlyChase = false; - break; - } + shouldDirectlyChase = false; + break; } } if (shouldDirectlyChase) diff --git a/NPCs/DraedonLabThings/Androomba.cs b/NPCs/DraedonLabThings/Androomba.cs index 99458991a0..ae09efe713 100644 --- a/NPCs/DraedonLabThings/Androomba.cs +++ b/NPCs/DraedonLabThings/Androomba.cs @@ -78,9 +78,8 @@ namespace CalamityMod.NPCs.DraedonLabThings NPC.ai[2] *= -1; } // If she sees someone, start running - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; if (Collision.CanHit(NPC.position, NPC.width, NPC.height, player.position, player.width, player.height)) { ChangeAI(1); @@ -143,12 +142,8 @@ namespace CalamityMod.NPCs.DraedonLabThings break; } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { Main.BestiaryTracker.Kills.RegisterKill(NPC); diff --git a/NPCs/DraedonLabThings/NanodroidDysfunctional.cs b/NPCs/DraedonLabThings/NanodroidDysfunctional.cs index 0b710d0dda..d9c9f41edc 100644 --- a/NPCs/DraedonLabThings/NanodroidDysfunctional.cs +++ b/NPCs/DraedonLabThings/NanodroidDysfunctional.cs @@ -36,12 +36,8 @@ namespace CalamityMod.NPCs.DraedonLabThings { Lighting.AddLight(NPC.Center, 0.25f, 0.1f, 0f); } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { NPC nPC = new NPC(); diff --git a/NPCs/DraedonLabThings/NanodroidPlagueGreen.cs b/NPCs/DraedonLabThings/NanodroidPlagueGreen.cs index 7063b0f70c..9d75bb45d2 100644 --- a/NPCs/DraedonLabThings/NanodroidPlagueGreen.cs +++ b/NPCs/DraedonLabThings/NanodroidPlagueGreen.cs @@ -36,12 +36,8 @@ namespace CalamityMod.NPCs.DraedonLabThings { Lighting.AddLight(NPC.Center, 0.03f, 0.3f, 0.05f); } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { NPC nPC = new NPC(); diff --git a/NPCs/DraedonLabThings/NanodroidPlagueRed.cs b/NPCs/DraedonLabThings/NanodroidPlagueRed.cs index 316f219b8c..d6ccfe8717 100644 --- a/NPCs/DraedonLabThings/NanodroidPlagueRed.cs +++ b/NPCs/DraedonLabThings/NanodroidPlagueRed.cs @@ -36,12 +36,8 @@ namespace CalamityMod.NPCs.DraedonLabThings { Lighting.AddLight(NPC.Center, 0.3f, 0.03f, 0f); } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { NPC nPC = new NPC(); diff --git a/NPCs/DraedonLabThings/RepairUnitCritter.cs b/NPCs/DraedonLabThings/RepairUnitCritter.cs index 9f8d5e76b0..0d71c27a94 100644 --- a/NPCs/DraedonLabThings/RepairUnitCritter.cs +++ b/NPCs/DraedonLabThings/RepairUnitCritter.cs @@ -131,12 +131,8 @@ namespace CalamityMod.NPCs.DraedonLabThings } Time++; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { Main.BestiaryTracker.Kills.RegisterKill(NPC); diff --git a/NPCs/ExoMechs/Thanatos/ThanatosBody1.cs b/NPCs/ExoMechs/Thanatos/ThanatosBody1.cs index b262f0b856..246be4a731 100644 --- a/NPCs/ExoMechs/Thanatos/ThanatosBody1.cs +++ b/NPCs/ExoMechs/Thanatos/ThanatosBody1.cs @@ -283,16 +283,16 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; @@ -347,16 +347,16 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; diff --git a/NPCs/ExoMechs/Thanatos/ThanatosBody2.cs b/NPCs/ExoMechs/Thanatos/ThanatosBody2.cs index 8c4d93d0a7..cb09c1182b 100644 --- a/NPCs/ExoMechs/Thanatos/ThanatosBody2.cs +++ b/NPCs/ExoMechs/Thanatos/ThanatosBody2.cs @@ -281,12 +281,12 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { @@ -345,16 +345,16 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; diff --git a/NPCs/ExoMechs/Thanatos/ThanatosTail.cs b/NPCs/ExoMechs/Thanatos/ThanatosTail.cs index 73454ad0cf..267eed162d 100644 --- a/NPCs/ExoMechs/Thanatos/ThanatosTail.cs +++ b/NPCs/ExoMechs/Thanatos/ThanatosTail.cs @@ -281,16 +281,16 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; @@ -345,16 +345,16 @@ namespace CalamityMod.NPCs.ExoMechs.Thanatos int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; diff --git a/NPCs/Leviathan/Anahita.cs b/NPCs/Leviathan/Anahita.cs index 6007a3c6b1..6c045a6ae8 100644 --- a/NPCs/Leviathan/Anahita.cs +++ b/NPCs/Leviathan/Anahita.cs @@ -489,13 +489,7 @@ namespace CalamityMod.NPCs.Leviathan float restingPlayerDistance = (float)Math.Sqrt(restingPlayerXDist * restingPlayerXDist + restingPlayerYDist * restingPlayerYDist); NPC.ai[1] += 1f; - int activePlayerAmt = 0; - for (int i = 0; i < 255; i++) - { - if (Main.player[i].active && !Main.player[i].dead && (vector - Main.player[i].Center).Length() < 1000f) - activePlayerAmt++; - } - NPC.ai[1] += activePlayerAmt / 2; + NPC.ai[1] += Main.CurrentFrameFlags.ActivePlayersCount / 2; bool spawnedBubble = false; float bubbleDelay = 20f; diff --git a/NPCs/NormalNPCs/ArmoredDiggerHead.cs b/NPCs/NormalNPCs/ArmoredDiggerHead.cs index a97631312d..8a25c963ad 100644 --- a/NPCs/NormalNPCs/ArmoredDiggerHead.cs +++ b/NPCs/NormalNPCs/ArmoredDiggerHead.cs @@ -165,16 +165,13 @@ namespace CalamityMod.NPCs.NormalNPCs bool outsideNoFlyZone = true; if (NPC.position.Y > Main.player[NPC.target].position.Y) { - for (int m = 0; m < 255; m++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle noFlyArea = new Rectangle((int)player.position.X - noFlyZone, (int)player.position.Y - noFlyZone, noFlyZone * 2, noFlyZone * 2); + if (rectangle.Intersects(noFlyArea)) { - Rectangle noFlyArea = new Rectangle((int)Main.player[m].position.X - noFlyZone, (int)Main.player[m].position.Y - noFlyZone, noFlyZone * 2, noFlyZone * 2); - if (rectangle.Intersects(noFlyArea)) - { - outsideNoFlyZone = false; - break; - } + outsideNoFlyZone = false; + break; } } if (outsideNoFlyZone) diff --git a/NPCs/Perforator/PerforatorHeadLarge.cs b/NPCs/Perforator/PerforatorHeadLarge.cs index 74c0216cc6..796db7929b 100644 --- a/NPCs/Perforator/PerforatorHeadLarge.cs +++ b/NPCs/Perforator/PerforatorHeadLarge.cs @@ -255,16 +255,13 @@ namespace CalamityMod.NPCs.Perforator bool outsideFlyingRadius = true; if (NPC.position.Y > player.position.Y) { - for (int m = 0; m < 255; m++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle rectangle2 = new Rectangle((int)plr.position.X - stopFlyingRadius, (int)plr.position.Y - stopFlyingRadius, stopFlyingRadius * 2, stopFlyingRadius * 2); + if (rectangle.Intersects(rectangle2)) { - Rectangle rectangle2 = new Rectangle((int)Main.player[m].position.X - stopFlyingRadius, (int)Main.player[m].position.Y - stopFlyingRadius, stopFlyingRadius * 2, stopFlyingRadius * 2); - if (rectangle.Intersects(rectangle2)) - { - outsideFlyingRadius = false; - break; - } + outsideFlyingRadius = false; + break; } } if (outsideFlyingRadius) diff --git a/NPCs/Perforator/PerforatorHeadSmall.cs b/NPCs/Perforator/PerforatorHeadSmall.cs index dd79644dc6..904ddb6b9e 100644 --- a/NPCs/Perforator/PerforatorHeadSmall.cs +++ b/NPCs/Perforator/PerforatorHeadSmall.cs @@ -228,16 +228,13 @@ namespace CalamityMod.NPCs.Perforator bool outsideFlyRadius = true; if (NPC.position.Y > player.position.Y) { - for (int m = 0; m < 255; m++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle rectangle2 = new Rectangle((int)plr.position.X - stopFlyingRadius, (int)plr.position.Y - stopFlyingRadius, stopFlyingRadius * 2, stopFlyingRadius * 2); + if (rectangle.Intersects(rectangle2)) { - Rectangle rectangle2 = new Rectangle((int)Main.player[m].position.X - stopFlyingRadius, (int)Main.player[m].position.Y - stopFlyingRadius, stopFlyingRadius * 2, stopFlyingRadius * 2); - if (rectangle.Intersects(rectangle2)) - { - outsideFlyRadius = false; - break; - } + outsideFlyRadius = false; + break; } } if (outsideFlyRadius) diff --git a/NPCs/PlagueEnemies/PlaguebringerMiniboss.cs b/NPCs/PlagueEnemies/PlaguebringerMiniboss.cs index 78e7b7d9d7..204bb62417 100644 --- a/NPCs/PlagueEnemies/PlaguebringerMiniboss.cs +++ b/NPCs/PlagueEnemies/PlaguebringerMiniboss.cs @@ -92,14 +92,7 @@ namespace CalamityMod.NPCs.PlagueEnemies NPC.timeLeft = 750; } } - int playerAmt = 0; - for (int i = 0; i < 255; i++) - { - if (Main.player[i].active && !Main.player[i].dead && (NPC.Center - Main.player[i].Center).Length() < 1000f) - { - playerAmt++; - } - } + int playerAmt = Main.CurrentFrameFlags.ActivePlayersCount; if (NPC.target < 0 || NPC.target == Main.maxPlayers || Main.player[NPC.target].dead || !Main.player[NPC.target].active) { NPC.TargetClosest(); diff --git a/NPCs/PlaguebringerGoliath/PlagueHomingMissile.cs b/NPCs/PlaguebringerGoliath/PlagueHomingMissile.cs index f60f882457..e83a3eaebc 100644 --- a/NPCs/PlaguebringerGoliath/PlagueHomingMissile.cs +++ b/NPCs/PlaguebringerGoliath/PlagueHomingMissile.cs @@ -140,10 +140,9 @@ namespace CalamityMod.NPCs.PlaguebringerGoliath } float distanceBeforeExploding = 42f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player.active && !player.dead && Vector2.Distance(player.Center, NPC.Center) <= distanceBeforeExploding) + if (!player.dead && Vector2.Distance(player.Center, NPC.Center) <= distanceBeforeExploding) { CheckDead(); NPC.life = 0; diff --git a/NPCs/PlaguebringerGoliath/PlaguebringerGoliath.cs b/NPCs/PlaguebringerGoliath/PlaguebringerGoliath.cs index 6b7f5bfe78..8f6b66774c 100644 --- a/NPCs/PlaguebringerGoliath/PlaguebringerGoliath.cs +++ b/NPCs/PlaguebringerGoliath/PlaguebringerGoliath.cs @@ -202,12 +202,7 @@ namespace CalamityMod.NPCs.PlaguebringerGoliath Vector2 vectorCenter = NPC.Center; // Count nearby players - int activePlayers = 0; - for (int i = 0; i < Main.maxPlayers; i++) - { - if (Main.player[i].active && !Main.player[i].dead && (vectorCenter - Main.player[i].Center).Length() < 1000f) - activePlayers++; - } + int activePlayers = Main.CurrentFrameFlags.ActivePlayersCount; // Get a target if (NPC.target < 0 || NPC.target == Main.maxPlayers || Main.player[NPC.target].dead || !Main.player[NPC.target].active) diff --git a/NPCs/PrimordialWyrm/PrimordialWyrmHead.cs b/NPCs/PrimordialWyrm/PrimordialWyrmHead.cs index 896e52d12c..eda8cb775b 100644 --- a/NPCs/PrimordialWyrm/PrimordialWyrmHead.cs +++ b/NPCs/PrimordialWyrm/PrimordialWyrmHead.cs @@ -601,16 +601,16 @@ namespace CalamityMod.NPCs.PrimordialWyrm int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; @@ -896,16 +896,16 @@ namespace CalamityMod.NPCs.PrimordialWyrm int numProjectiles = 0; float maxDistance = 2400f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (plr.dead) continue; - Vector2 playerCenter = Main.player[i].Center; + Vector2 playerCenter = plr.Center; float distance = Vector2.Distance(playerCenter, NPC.Center); if (distance < maxDistance) { - whoAmIArray[numProjectiles] = i; + whoAmIArray[numProjectiles] = plr.whoAmI; targetCenterArray[numProjectiles] = playerCenter; int projectileLimit = numProjectiles + 1; numProjectiles = projectileLimit; diff --git a/NPCs/Providence/Providence.cs b/NPCs/Providence/Providence.cs index 1b0547687b..e0cea09394 100644 --- a/NPCs/Providence/Providence.cs +++ b/NPCs/Providence/Providence.cs @@ -1266,10 +1266,10 @@ namespace CalamityMod.NPCs.Providence if (NPC.ai[3] % 60f == 0f && expertMode) { List targets = new List(); - for (int p = 0; p < Main.maxPlayers; p++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[p].active && !Main.player[p].dead) - targets.Add(p); + if (!plr.dead) + targets.Add(plr.whoAmI); if (targets.Count > 4) break; diff --git a/NPCs/SlimeGod/CrimulanPaladin.cs b/NPCs/SlimeGod/CrimulanPaladin.cs index 23bfbfd72c..b80fa876b4 100644 --- a/NPCs/SlimeGod/CrimulanPaladin.cs +++ b/NPCs/SlimeGod/CrimulanPaladin.cs @@ -431,10 +431,10 @@ namespace CalamityMod.NPCs.SlimeGod if (enraged && expertMode) { List targets = new List(); - for (int p = 0; p < Main.maxPlayers; p++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[p].active && !Main.player[p].dead) - targets.Add(p); + if (!plr.dead) + targets.Add(plr.whoAmI); if (targets.Count > 1) break; diff --git a/NPCs/SlimeGod/EbonianPaladin.cs b/NPCs/SlimeGod/EbonianPaladin.cs index 09a2661ab7..5f1f30779b 100644 --- a/NPCs/SlimeGod/EbonianPaladin.cs +++ b/NPCs/SlimeGod/EbonianPaladin.cs @@ -444,10 +444,10 @@ namespace CalamityMod.NPCs.SlimeGod if (enraged && expertMode) { List targets = new List(); - for (int p = 0; p < Main.maxPlayers; p++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[p].active && !Main.player[p].dead) - targets.Add(p); + if (!plr.dead) + targets.Add(plr.whoAmI); if (targets.Count > 1) break; diff --git a/NPCs/SlimeGod/SplitCrimulanPaladin.cs b/NPCs/SlimeGod/SplitCrimulanPaladin.cs index 344477b686..7b1e087116 100644 --- a/NPCs/SlimeGod/SplitCrimulanPaladin.cs +++ b/NPCs/SlimeGod/SplitCrimulanPaladin.cs @@ -413,10 +413,10 @@ namespace CalamityMod.NPCs.SlimeGod if (enraged && expertMode) { List targets = new List(); - for (int p = 0; p < Main.maxPlayers; p++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[p].active && !Main.player[p].dead) - targets.Add(p); + if (!plr.dead) + targets.Add(plr.whoAmI); if (targets.Count > 1) break; diff --git a/NPCs/SlimeGod/SplitEbonianPaladin.cs b/NPCs/SlimeGod/SplitEbonianPaladin.cs index 80772fbe31..dc61e4df74 100644 --- a/NPCs/SlimeGod/SplitEbonianPaladin.cs +++ b/NPCs/SlimeGod/SplitEbonianPaladin.cs @@ -430,10 +430,10 @@ namespace CalamityMod.NPCs.SlimeGod if (enraged && expertMode) { List targets = new List(); - for (int p = 0; p < Main.maxPlayers; p++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[p].active && !Main.player[p].dead) - targets.Add(p); + if (!plr.dead) + targets.Add(plr.whoAmI); if (targets.Count > 1) break; diff --git a/NPCs/TownNPCs/AndroombaFriendly.cs b/NPCs/TownNPCs/AndroombaFriendly.cs index 386817f6b0..843e7e3da3 100644 --- a/NPCs/TownNPCs/AndroombaFriendly.cs +++ b/NPCs/TownNPCs/AndroombaFriendly.cs @@ -96,12 +96,8 @@ namespace CalamityMod.NPCs.TownNPCs break; } - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - if (player is null || !player.active) - continue; - if (NPC.Hitbox.Intersects(player.HitboxForBestiaryNearbyCheck)) { Main.BestiaryTracker.Chats.RegisterChatStartWith(NPC); diff --git a/NPCs/TownNPCs/FAP.cs b/NPCs/TownNPCs/FAP.cs index 2aa452a208..099711abf7 100644 --- a/NPCs/TownNPCs/FAP.cs +++ b/NPCs/TownNPCs/FAP.cs @@ -93,11 +93,10 @@ namespace CalamityMod.NPCs.TownNPCs if (CalamityWorld.spawnedCirrus) return true; - for (int k = 0; k < Main.maxPlayers; k++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[k]; bool hasVodka = player.InventoryHas(ModContent.ItemType()) || player.PortableStorageHas(ModContent.ItemType()); - if (player.active && hasVodka) + if (hasVodka) return Main.hardMode; } return false; diff --git a/NPCs/TownNPCs/THIEF.cs b/NPCs/TownNPCs/THIEF.cs index ade7a0d833..8e93342d25 100644 --- a/NPCs/TownNPCs/THIEF.cs +++ b/NPCs/TownNPCs/THIEF.cs @@ -82,11 +82,10 @@ namespace CalamityMod.NPCs.TownNPCs if (CalamityWorld.spawnedBandit) return true; - for (int k = 0; k < Main.maxPlayers; k++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[k]; bool rich = player.InventoryHas(ItemID.PlatinumCoin) || player.PortableStorageHas(ItemID.PlatinumCoin); - if (player.active && rich) + if (rich) return NPC.downedBoss3; } return false; diff --git a/NPCs/VanillaNPCOverrides/Bosses/DeerclopsAI.cs b/NPCs/VanillaNPCOverrides/Bosses/DeerclopsAI.cs index a5e98cc159..ff0ea8bd53 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/DeerclopsAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/DeerclopsAI.cs @@ -539,22 +539,22 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses if (shadowHandTimer / shadowHandSpawnRate >= 3) npc.localAI[2] = 0f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - if (npc.Boss_CanShootExtraAt(i, rotation % 3, 3, 1200f, alwaysSkipMainTarget: false)) + if (npc.Boss_CanShootExtraAt(player.whoAmI, rotation % 3, 3, 1200f, alwaysSkipMainTarget: false)) { // Normal shadow hand spawn behavior // Modified to make the shadow hand spawns fair - RandomizeInsanityShadowFor(Main.player[i], isHostile: true, out var spawnPosition, out var spawnVelocity, out var ai, out var ai2); + RandomizeInsanityShadowFor(player, isHostile: true, out var spawnPosition, out var spawnVelocity, out var ai, out var ai2); // Spawn hands to cut the player off and force them back towards Deerclops // This only happens if the player is triggering Deerclops' increased DR float minShadowHandSpawnDistanceFromPlayer = 450f; - float playerDistanceFromDeerclops = Vector2.Distance(npc.Center, Main.player[i].Center); + float playerDistanceFromDeerclops = Vector2.Distance(npc.Center, player.Center); if (playerDistanceFromDeerclops >= 450f) { - spawnPosition = (Main.player[i].Center - npc.Center).SafeNormalize(Vector2.UnitY) * (playerDistanceFromDeerclops + minShadowHandSpawnDistanceFromPlayer); - spawnVelocity = (Main.player[i].Center - spawnPosition).SafeNormalize(Vector2.UnitY) * spawnVelocity.Length(); + spawnPosition = (player.Center - npc.Center).SafeNormalize(Vector2.UnitY) * (playerDistanceFromDeerclops + minShadowHandSpawnDistanceFromPlayer); + spawnVelocity = (player.Center - spawnPosition).SafeNormalize(Vector2.UnitY) * spawnVelocity.Length(); } Projectile.NewProjectile(npc.GetSource_FromAI(), spawnPosition, spawnVelocity, shadowHand, shadowHandDamage, 0f, Main.myPlayer, ai, ai2); diff --git a/NPCs/VanillaNPCOverrides/Bosses/DestroyerAI.cs b/NPCs/VanillaNPCOverrides/Bosses/DestroyerAI.cs index abbaa9444e..a6ff1b17e3 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/DestroyerAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/DestroyerAI.cs @@ -342,16 +342,13 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses if (npc.position.Y > player.position.Y) { - for (int m = 0; m < Main.maxPlayers; m++) + foreach (Player plr in Main.ActivePlayers) { - if (Main.player[m].active) + Rectangle noFlyRectangle = new Rectangle((int)plr.position.X - noFlyZone, (int)plr.position.Y - noFlyZone, noFlyZone * 2, height); + if (rectangle.Intersects(noFlyRectangle)) { - Rectangle noFlyRectangle = new Rectangle((int)Main.player[m].position.X - noFlyZone, (int)Main.player[m].position.Y - noFlyZone, noFlyZone * 2, height); - if (rectangle.Intersects(noFlyRectangle)) - { - outsideNoFlyZone = false; - break; - } + outsideNoFlyZone = false; + break; } } if (outsideNoFlyZone) diff --git a/NPCs/VanillaNPCOverrides/Bosses/EaterOfWorldsAI.cs b/NPCs/VanillaNPCOverrides/Bosses/EaterOfWorldsAI.cs index 012513d062..569bb91213 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/EaterOfWorldsAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/EaterOfWorldsAI.cs @@ -281,16 +281,13 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses noFlyZone = 100; bool freeMoveAnyway = true; - for (int k = 0; k < Main.maxPlayers; k++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[k].active) + Rectangle rectangle2 = new Rectangle((int)player.position.X - noFlyZone, (int)player.position.Y - noFlyZone, noFlyZone * 2, noFlyZone * 2); + if (rectangle.Intersects(rectangle2)) { - Rectangle rectangle2 = new Rectangle((int)Main.player[k].position.X - noFlyZone, (int)Main.player[k].position.Y - noFlyZone, noFlyZone * 2, noFlyZone * 2); - if (rectangle.Intersects(rectangle2)) - { - freeMoveAnyway = false; - break; - } + freeMoveAnyway = false; + break; } } if (freeMoveAnyway) @@ -430,9 +427,9 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses if (shouldDespawn && !bossRush) { bool everyoneDead = true; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i].active && !Main.player[i].dead) + if (!player.dead) { everyoneDead = false; break; diff --git a/NPCs/VanillaNPCOverrides/Bosses/EmpressofLightAI.cs b/NPCs/VanillaNPCOverrides/Bosses/EmpressofLightAI.cs index eeebea7535..d304bb653c 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/EmpressofLightAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/EmpressofLightAI.cs @@ -502,12 +502,12 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses continue; // Spawn extra Ethereal Lances per player. - for (int j = 0; j < Main.maxPlayers; j++) + foreach (Player player in Main.ActivePlayers) { - if (!npc.Boss_CanShootExtraAt(j, lanceFrequency % 3, 3, 2400f)) + if (!npc.Boss_CanShootExtraAt(player.whoAmI, lanceFrequency % 3, 3, 2400f)) continue; - Player extraPlayer = Main.player[j]; + Player extraPlayer = player; playerCenter = extraPlayer.Center; if (Vector2.Dot(extraPlayer.velocity.SafeNormalize(Vector2.UnitY), lanceDirection) > 0f) lanceDirection *= -1f; @@ -988,12 +988,11 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses continue; int multiplayerExtraStraightLances = (int)(npc.ai[1] / etherealLanceGateValue); - for (int l = 0; l < Main.maxPlayers; l++) + foreach (Player player in Main.ActivePlayers) { - if (!npc.Boss_CanShootExtraAt(l, multiplayerExtraStraightLances % 3, 3, 2400f)) + if (!npc.Boss_CanShootExtraAt(player.whoAmI, multiplayerExtraStraightLances % 3, 3, 2400f)) continue; - Player player = Main.player[l]; inverseTargetVel = -player.velocity; inverseTargetVel.SafeNormalize(-Vector2.UnitY); targetCenter = player.Center; diff --git a/NPCs/VanillaNPCOverrides/Bosses/MoonLordAI.cs b/NPCs/VanillaNPCOverrides/Bosses/MoonLordAI.cs index 8efb918101..b148838e04 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/MoonLordAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/MoonLordAI.cs @@ -842,10 +842,9 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses if (attackTimer == 0f && Main.netMode != NetmodeID.MultiplayerClient) { Vector2 leechNPCCenter = npc.Center + leechCenterOffset; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player6 in Main.ActivePlayers) { - Player player6 = Main.player[i]; - if (player6.active && !player6.dead && Vector2.Distance(player6.Center, leechNPCCenter) <= 3000f) + if (!player6.dead && Vector2.Distance(player6.Center, leechNPCCenter) <= 3000f) { Vector2 targetLeechDist = Main.player[npc.target].Center - leechNPCCenter; if (targetLeechDist != Vector2.Zero) diff --git a/NPCs/VanillaNPCOverrides/Bosses/QueenBeeAI.cs b/NPCs/VanillaNPCOverrides/Bosses/QueenBeeAI.cs index ef2300c683..020beef416 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/QueenBeeAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/QueenBeeAI.cs @@ -440,9 +440,9 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses // Bee spawn timer npc.ai[1] += 1f; int beeSpawnTimer = 0; - for (int i = 0; i < 255; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i].active && !Main.player[i].dead && (npc.Center - Main.player[i].Center).Length() < 1000f) + if (!player.dead && (npc.Center - player.Center).Length() < 1000f) beeSpawnTimer++; } npc.ai[1] += beeSpawnTimer / 2; diff --git a/Projectiles/Boss/OldDukeVortex.cs b/Projectiles/Boss/OldDukeVortex.cs index 4aafd1b355..c3e39933e9 100644 --- a/Projectiles/Boss/OldDukeVortex.cs +++ b/Projectiles/Boss/OldDukeVortex.cs @@ -124,10 +124,8 @@ namespace CalamityMod.Projectiles.Boss float distanceRequired = 800f * Projectile.scale; float succPower = Main.zenithWorld ? 1f : 0.5f; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { - Player player = Main.player[i]; - float distance = Vector2.Distance(player.Center, Projectile.Center); if (distance < distanceRequired && player.grappling[0] == -1) { diff --git a/Projectiles/Boss/OverlyDramaticDukeSummoner.cs b/Projectiles/Boss/OverlyDramaticDukeSummoner.cs index cb9ce5fdf4..a69053f2a5 100644 --- a/Projectiles/Boss/OverlyDramaticDukeSummoner.cs +++ b/Projectiles/Boss/OverlyDramaticDukeSummoner.cs @@ -107,9 +107,9 @@ namespace CalamityMod.Projectiles.Boss Projectile.alpha = (int)MathHelper.Lerp(0f, 255f, (Projectile.ai[0] - 600f) / 120f); bool canSpawnBoomer = false; - for (int i = 0; i < Main.player.Length; i++) + foreach (Player player in Main.ActivePlayers) { - if (!Main.player[i].dead && Projectile.Distance(Main.player[i].Center) < 12000f) + if (!player.dead && Projectile.Distance(player.Center) < 12000f) { canSpawnBoomer = true; break; diff --git a/Projectiles/CalamityGlobalProjectile.cs b/Projectiles/CalamityGlobalProjectile.cs index 234d740617..5e093f7fb3 100644 --- a/Projectiles/CalamityGlobalProjectile.cs +++ b/Projectiles/CalamityGlobalProjectile.cs @@ -3142,10 +3142,9 @@ namespace CalamityMod.Projectiles float lowestHealthCheck = 0f; int healTarget = projectile.owner; - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player otherPlayer in Main.ActivePlayers) { - Player otherPlayer = Main.player[i]; - if (otherPlayer.active && !otherPlayer.dead && ((!player.hostile && !otherPlayer.hostile) || player.team == otherPlayer.team)) + if (!otherPlayer.dead && ((!player.hostile && !otherPlayer.hostile) || player.team == otherPlayer.team)) { float playerDist = Vector2.Distance(projectile.Center, otherPlayer.Center); if (playerDist < distanceRequired && (otherPlayer.statLifeMax2 - otherPlayer.statLife) > lowestHealthCheck) diff --git a/Projectiles/Enemy/FlakAcid.cs b/Projectiles/Enemy/FlakAcid.cs index af856067b2..f9cb7d39bf 100644 --- a/Projectiles/Enemy/FlakAcid.cs +++ b/Projectiles/Enemy/FlakAcid.cs @@ -35,9 +35,9 @@ namespace CalamityMod.Projectiles.Enemy Projectile.velocity.Y += 0.15f; Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.PiOver2; - for (int i = 0; i < Main.player.Length; i++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[i].Distance(Projectile.Center) < 60f) + if (player.Distance(Projectile.Center) < 60f) { Projectile.Kill(); } diff --git a/Projectiles/Pets/LadShark.cs b/Projectiles/Pets/LadShark.cs index 773fa8c408..0c086d5ec6 100644 --- a/Projectiles/Pets/LadShark.cs +++ b/Projectiles/Pets/LadShark.cs @@ -78,9 +78,8 @@ namespace CalamityMod.Projectiles.Pets npc.Calamity().ladHearts = CalamityUtils.SecondsToFrames(9f); } } - for (int k = 0; k < Main.maxPlayers; k++) + foreach (Player players in Main.ActivePlayers) { - Player players = Main.player[k]; if (!players.dead && Vector2.Distance(Projectile.Center, players.Center) <= radius) { if (players.Calamity().ladHearts <= 0) diff --git a/Projectiles/Rogue/IcebreakerHammer.cs b/Projectiles/Rogue/IcebreakerHammer.cs index 958cbf080c..c383c2a93e 100644 --- a/Projectiles/Rogue/IcebreakerHammer.cs +++ b/Projectiles/Rogue/IcebreakerHammer.cs @@ -93,10 +93,9 @@ namespace CalamityMod.Projectiles.Rogue int buffType = ModContent.BuffType(); float radius = 112f; // 7 blocks - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player player in Main.ActivePlayers) { Player owner = Main.player[Projectile.owner]; - Player player = Main.player[i]; if ((owner.team != player.team || player.team == 0) && player.hostile && owner.hostile && !player.dead && !player.buffImmune[buffType] && Vector2.Distance(Projectile.Center, player.Center) <= radius) { if (player.FindBuffIndex(buffType) == -1) diff --git a/Projectiles/Summon/WulfrumDroid.cs b/Projectiles/Summon/WulfrumDroid.cs index b603524057..4418cf5779 100644 --- a/Projectiles/Summon/WulfrumDroid.cs +++ b/Projectiles/Summon/WulfrumDroid.cs @@ -315,14 +315,14 @@ namespace CalamityMod.Projectiles.Summon if (Main.netMode == NetmodeID.MultiplayerClient && mouseDistanceToOwner > 120f) { - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player p in Main.ActivePlayers) { - if (Main.player[i].active && !Main.player[i].dead && (Main.player[i].team == Owner.team || Main.player[i].team == 0)) + if (!p.dead && (p.team == Owner.team || p.team == 0)) { - float mouseDistanceToPotentialTarget = (Main.player[i].MountedCenter - Owner.Calamity().mouseWorld).Length(); + float mouseDistanceToPotentialTarget = (p.MountedCenter - Owner.Calamity().mouseWorld).Length(); if (mouseDistanceToPotentialTarget < 120f && mouseDistanceToOwner > mouseDistanceToPotentialTarget) { - playerToBuff = Main.player[i]; + playerToBuff = p; mouseDistanceToOwner = mouseDistanceToPotentialTarget; } } diff --git a/Projectiles/Typeless/BossRushEndEffectThing.cs b/Projectiles/Typeless/BossRushEndEffectThing.cs index fdb981ba22..00d0b82485 100644 --- a/Projectiles/Typeless/BossRushEndEffectThing.cs +++ b/Projectiles/Typeless/BossRushEndEffectThing.cs @@ -45,16 +45,13 @@ namespace CalamityMod.Projectiles.Typeless public override void OnKill(int timeLeft) { BossRushEvent.End(); - for (int i = Main.maxPlayers - 1; i >= 0; i--) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (p is null || !p.active) - continue; int rock = Item.NewItem(p.GetSource_Misc("CalamityMod_BossRushRock"), (int)p.position.X, (int)p.position.Y, p.width, p.height, ModContent.ItemType()); if (Main.netMode == NetmodeID.Server) { Main.timeItemSlotCannotBeReusedFor[rock] = 54000; - NetMessage.SendData(MessageID.InstancedItem, i, -1, null, rock); + NetMessage.SendData(MessageID.InstancedItem, p.whoAmI, -1, null, rock); } } } diff --git a/Projectiles/Typeless/WulfrumLureSignal.cs b/Projectiles/Typeless/WulfrumLureSignal.cs index cdf334d20e..7d233427c4 100644 --- a/Projectiles/Typeless/WulfrumLureSignal.cs +++ b/Projectiles/Typeless/WulfrumLureSignal.cs @@ -43,13 +43,13 @@ namespace CalamityMod.Projectiles.Typeless if (Main.netMode == NetmodeID.MultiplayerClient) { float closestPlayerDistance = (Main.LocalPlayer.Center - Projectile.Center).Length(); - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player plr in Main.ActivePlayers) { - float newDistance = (Main.player[i].Center - Projectile.Center).Length(); + float newDistance = (plr.Center - Projectile.Center).Length(); if (newDistance < closestPlayerDistance) { closestPlayerDistance = newDistance; - player = Main.player[i]; + player = plr; } } } diff --git a/Systems/WorldMiscUpdateSystem.cs b/Systems/WorldMiscUpdateSystem.cs index d51e8c69bd..9283e4383a 100644 --- a/Systems/WorldMiscUpdateSystem.cs +++ b/Systems/WorldMiscUpdateSystem.cs @@ -101,12 +101,12 @@ namespace CalamityMod.Systems // Attempt to summon lab critters manually since they refuse to exist when using vanilla's spawn methods. // This needs to check all players since the method only runs server-side. - for (int i = 0; i < Main.maxPlayers; i++) + foreach (Player p in Main.ActivePlayers) { - if (!Main.player[i].active || Main.player[i].dead) + if (p.dead) continue; - CalamityGlobalNPC.AttemptToSpawnLabCritters(Main.player[i]); + CalamityGlobalNPC.AttemptToSpawnLabCritters(p); } // Make the cultist countdown happen much more quickly. diff --git a/TileEntities/TEChargingStation.cs b/TileEntities/TEChargingStation.cs index 49027f7f6a..19a318b807 100644 --- a/TileEntities/TEChargingStation.cs +++ b/TileEntities/TEChargingStation.cs @@ -140,10 +140,9 @@ namespace CalamityMod.TileEntities // If this charging station breaks, anyone's who's viewing it is no longer viewing it. public override void OnKill() { - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active) + if (p.dead) continue; // Use reflection to stop TML from spitting an error here. diff --git a/TileEntities/TEHostileFireTurret.cs b/TileEntities/TEHostileFireTurret.cs index 39f2801005..f3e47b7243 100644 --- a/TileEntities/TEHostileFireTurret.cs +++ b/TileEntities/TEHostileFireTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostileIceTurret.cs b/TileEntities/TEHostileIceTurret.cs index a86f093a07..7b1a7705bd 100644 --- a/TileEntities/TEHostileIceTurret.cs +++ b/TileEntities/TEHostileIceTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostileLabTurret.cs b/TileEntities/TEHostileLabTurret.cs index eaec3d3652..739e752fe4 100644 --- a/TileEntities/TEHostileLabTurret.cs +++ b/TileEntities/TEHostileLabTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostileLaserTurret.cs b/TileEntities/TEHostileLaserTurret.cs index 139a7fd3c6..d1d36aa0b0 100644 --- a/TileEntities/TEHostileLaserTurret.cs +++ b/TileEntities/TEHostileLaserTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostileOnyxTurret.cs b/TileEntities/TEHostileOnyxTurret.cs index 4408656a11..f3bd4e45ec 100644 --- a/TileEntities/TEHostileOnyxTurret.cs +++ b/TileEntities/TEHostileOnyxTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostilePlagueTurret.cs b/TileEntities/TEHostilePlagueTurret.cs index f33c756275..3435fc73e4 100644 --- a/TileEntities/TEHostilePlagueTurret.cs +++ b/TileEntities/TEHostilePlagueTurret.cs @@ -85,10 +85,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +95,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TEHostileWaterTurret.cs b/TileEntities/TEHostileWaterTurret.cs index 5e6082e611..6634863735 100644 --- a/TileEntities/TEHostileWaterTurret.cs +++ b/TileEntities/TEHostileWaterTurret.cs @@ -2,6 +2,7 @@ using CalamityMod.Tiles.DraedonStructures; using Microsoft.Xna.Framework; using System.IO; +using System.Threading.Tasks; using Terraria; using Terraria.ModLoader; @@ -85,10 +86,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active || p.dead) + if (p.dead) continue; float distSQ = p.DistanceSQ(targetingCenter); @@ -96,7 +96,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = p.Center; - indexToSet = i; + indexToSet = p.whoAmI; } } diff --git a/TileEntities/TELabHologramProjector.cs b/TileEntities/TELabHologramProjector.cs index b61fc5d077..017ee44471 100644 --- a/TileEntities/TELabHologramProjector.cs +++ b/TileEntities/TELabHologramProjector.cs @@ -35,11 +35,10 @@ namespace CalamityMod.TileEntities // If one is, pop up. Vector2 projectorCenterPos = Center; float distSQ = PopupDistance * PopupDistance; - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active) // The lack of a dead check is intentional. Dead players keep hologram boxes on for amusement. - continue; + // The lack of a dead check is intentional. Dead players keep hologram boxes on for amusement. + if (p.DistanceSQ(projectorCenterPos) < distSQ) { PoppingUp = true; @@ -72,12 +71,8 @@ namespace CalamityMod.TileEntities // If this projector breaks, anyone who's viewing it is no longer viewing it. public override void OnKill() { - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active) - continue; - // Use reflection to stop TML from spitting an error here. // Try-catching will not stop this error, TML will print it to console anyway. The error is harmless. ModPlayer[] mpStorageArray = (ModPlayer[])typeof(Player).GetField("modPlayers", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(p); diff --git a/TileEntities/TEPowerCellFactory.cs b/TileEntities/TEPowerCellFactory.cs index da86d0bf1c..bc51f1ff86 100644 --- a/TileEntities/TEPowerCellFactory.cs +++ b/TileEntities/TEPowerCellFactory.cs @@ -103,12 +103,8 @@ namespace CalamityMod.TileEntities // If this factory breaks, anyone who's viewing it is no longer viewing it. public override void OnKill() { - for (int i = 0; i < Main.maxPlayers; ++i) + foreach (Player p in Main.ActivePlayers) { - Player p = Main.player[i]; - if (!p.active) - continue; - // Use reflection to stop TML from spitting an error here. // Try-catching will not stop this error, TML will print it to console anyway. The error is harmless. ModPlayer[] mpStorageArray = (ModPlayer[])typeof(Player).GetField("modPlayers", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(p); diff --git a/Utilities/DropHelper.cs b/Utilities/DropHelper.cs index 5ce9c2e0bf..6f356ca6ac 100644 --- a/Utilities/DropHelper.cs +++ b/Utilities/DropHelper.cs @@ -1154,9 +1154,8 @@ namespace CalamityMod NPC npc = info.npc; int idx = Item.NewItem(npc.GetSource_Loot(), npc.Center, itemId, stack, true, -1); Main.timeItemSlotCannotBeReusedFor[idx] = protectionTime; - for (int i = 0; i < Main.maxPlayers; ++i) - if (Main.player[i].active) - NetMessage.SendData(MessageID.InstancedItem, i, -1, null, idx); + foreach (Player player in Main.ActivePlayers) + NetMessage.SendData(MessageID.InstancedItem, player.whoAmI, -1, null, idx); Main.item[idx].active = false; } diff --git a/World/AstralBiome.cs b/World/AstralBiome.cs index 68a534ad96..511e3caa50 100644 --- a/World/AstralBiome.cs +++ b/World/AstralBiome.cs @@ -83,13 +83,10 @@ namespace CalamityMod.World // Clients in multiplayer don't drop meteors. if (Main.netMode == NetmodeID.MultiplayerClient) return; - for (int i = 0; i < 255; i++) + foreach (Player _ in Main.ActivePlayers) { - if (Main.player[i].active) - { - meteorDropped = false; - break; - } + meteorDropped = false; + break; } // Check whether there is already too much ore. @@ -257,11 +254,11 @@ namespace CalamityMod.World } int avoidRectangleSize = 35; Rectangle rectangle = new Rectangle((i - avoidRectangleSize) * 16, (j - avoidRectangleSize) * 16, avoidRectangleSize * 2 * 16, avoidRectangleSize * 2 * 16); - for (int k = 0; k < Main.maxPlayers; k++) + foreach (Player player in Main.ActivePlayers) { - if (Main.player[k].active) + if (player.active) { - Rectangle value = new Rectangle((int)(Main.player[k].position.X + (float)(Main.player[k].width / 2) - (float)(NPC.sWidth / 2) - (float)NPC.safeRangeX), (int)(Main.player[k].position.Y + (float)(Main.player[k].height / 2) - (float)(NPC.sHeight / 2) - (float)NPC.safeRangeY), NPC.sWidth + NPC.safeRangeX * 2, NPC.sHeight + NPC.safeRangeY * 2); + Rectangle value = new Rectangle((int)(player.position.X + (float)(player.width / 2) - (float)(NPC.sWidth / 2) - (float)NPC.safeRangeX), (int)(player.position.Y + (float)(player.height / 2) - (float)(NPC.sHeight / 2) - (float)NPC.safeRangeY), NPC.sWidth + NPC.safeRangeX * 2, NPC.sHeight + NPC.safeRangeY * 2); if (rectangle.Intersects(value)) { return false;