diff --git a/NPCs/VanillaNPCOverrides/Bosses/PlanteraAI.cs b/NPCs/VanillaNPCOverrides/Bosses/PlanteraAI.cs index 75c8d4c46a..c4b29a59c4 100644 --- a/NPCs/VanillaNPCOverrides/Bosses/PlanteraAI.cs +++ b/NPCs/VanillaNPCOverrides/Bosses/PlanteraAI.cs @@ -627,14 +627,14 @@ namespace CalamityMod.NPCs.VanillaNPCOverrides.Bosses if (Main.getGoodWorld) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].aiStyle == NPCAIStyleID.PlanteraHook) + if (n.aiStyle == NPCAIStyleID.PlanteraHook) { for (int j = 0; j < totalTentacles / 2 - 1; j++) { int hookIndex = NPC.NewNPC(npc.GetSource_FromAI(), (int)npc.Center.X, (int)npc.Center.Y, NPCID.PlanterasTentacle, npc.whoAmI); - Main.npc[hookIndex].ai[3] = i + 1; + Main.npc[hookIndex].ai[3] = n.whoAmI + 1; } } } diff --git a/Projectiles/DraedonsArsenal/TeslaCannonShot.cs b/Projectiles/DraedonsArsenal/TeslaCannonShot.cs index a9ac5ec782..91dfa10a9d 100644 --- a/Projectiles/DraedonsArsenal/TeslaCannonShot.cs +++ b/Projectiles/DraedonsArsenal/TeslaCannonShot.cs @@ -96,14 +96,14 @@ namespace CalamityMod.Projectiles.DraedonsArsenal { if (arcs < 10) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].CanBeChasedBy(Projectile, false) || !Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (!n.CanBeChasedBy(Projectile, false) || !Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) continue; - if (Projectile.Center.ManhattanDistance(Main.npc[i].Center) < 600f) + if (Projectile.Center.ManhattanDistance(n.Center) < 600f) { - Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, Projectile.SafeDirectionTo(Main.npc[i].Center) * 2f, Projectile.type, (int)(Projectile.damage * 0.6), Projectile.knockBack * 0.6f, Projectile.owner, 0f, -1f); + Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, Projectile.SafeDirectionTo(n.Center) * 2f, Projectile.type, (int)(Projectile.damage * 0.6), Projectile.knockBack * 0.6f, Projectile.owner, 0f, -1f); Projectile.ai[1] = 60f; diff --git a/Projectiles/Magic/BlackAnurianPlankton.cs b/Projectiles/Magic/BlackAnurianPlankton.cs index e60b867d46..321fb1c5bb 100644 --- a/Projectiles/Magic/BlackAnurianPlankton.cs +++ b/Projectiles/Magic/BlackAnurianPlankton.cs @@ -1,4 +1,4 @@ -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; using System; using Terraria; using Terraria.ModLoader; @@ -57,14 +57,14 @@ namespace CalamityMod.Projectiles.Magic if (Projectile.ai[0] > 30f) { Projectile.ai[0] = 30f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && !Main.npc[i].wet) + if (n.CanBeChasedBy(Projectile, false) && !n.wet) { - float npcX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcX = n.position.X + (float)(n.width / 2); + float npcY = n.position.Y + (float)(n.height / 2); float npcDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcY); - if (npcDist < 800f && npcDist < homingRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height)) + if (npcDist < 800f && npcDist < homingRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, n.position, n.width, n.height)) { homingRange = npcDist; projX = npcX; diff --git a/Projectiles/Magic/EventHorizonStar.cs b/Projectiles/Magic/EventHorizonStar.cs index e5ad6b45e2..98a656a697 100644 --- a/Projectiles/Magic/EventHorizonStar.cs +++ b/Projectiles/Magic/EventHorizonStar.cs @@ -93,15 +93,15 @@ namespace CalamityMod.Projectiles.Magic float inertia = 25f; float homingSpeed = 23f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (homingRange + extraDistance)) + if (Vector2.Distance(n.Center, Projectile.Center) < (homingRange + extraDistance)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Magic/HadalUrnJellyfish.cs b/Projectiles/Magic/HadalUrnJellyfish.cs index 4d4aef0ef4..62e5e44d96 100644 --- a/Projectiles/Magic/HadalUrnJellyfish.cs +++ b/Projectiles/Magic/HadalUrnJellyfish.cs @@ -75,17 +75,17 @@ namespace CalamityMod.Projectiles.Magic //Detect if any enemies are very close int maxDistance = 10; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (Main.npc[i].width / 2) + (Main.npc[i].height / 2); + float extraDistance = (n.width / 2) + (n.height / 2); bool canHit = true; if (extraDistance < maxDistance) - canHit = Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1); + canHit = Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1); - if (Projectile.WithinRange(Main.npc[i].Center, maxDistance + extraDistance) && canHit) + if (Projectile.WithinRange(n.Center, maxDistance + extraDistance) && canHit) { neartarget = true; } diff --git a/Projectiles/Magic/NebulaCloudCore.cs b/Projectiles/Magic/NebulaCloudCore.cs index d6666420d1..ed319b9e30 100644 --- a/Projectiles/Magic/NebulaCloudCore.cs +++ b/Projectiles/Magic/NebulaCloudCore.cs @@ -169,17 +169,17 @@ namespace CalamityMod.Projectiles.Magic int npcTrack = 0; if (Projectile.ai[1] == 0f) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(this)) + if (n.CanBeChasedBy(this)) { - Vector2 npcCenter = Main.npc[i].Center; - if (Projectile.Distance(npcCenter) < homingRange && Collision.CanHit(new Vector2(Projectile.position.X + Projectile.width / 2, Projectile.position.Y + Projectile.height / 2), 1, 1, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height)) + Vector2 npcCenter = n.Center; + if (Projectile.Distance(npcCenter) < homingRange && Collision.CanHit(new Vector2(Projectile.position.X + Projectile.width / 2, Projectile.position.Y + Projectile.height / 2), 1, 1, n.position, n.width, n.height)) { homingRange = Projectile.Distance(npcCenter); projCenter = npcCenter; isHoming = true; - npcTrack = i; + npcTrack = n.whoAmI; } } } diff --git a/Projectiles/Magic/SHPB.cs b/Projectiles/Magic/SHPB.cs index 0e15e88b72..27a7ba82ed 100644 --- a/Projectiles/Magic/SHPB.cs +++ b/Projectiles/Magic/SHPB.cs @@ -68,12 +68,12 @@ namespace CalamityMod.Projectiles.Magic float projY = Projectile.Center.Y; float explodeRange = 250f; bool canExplode = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - float npcX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcX = n.position.X + (float)(n.width / 2); + float npcY = n.position.Y + (float)(n.height / 2); float npcDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcY); if (npcDist < explodeRange) { diff --git a/Projectiles/Magic/WulfrumBolt.cs b/Projectiles/Magic/WulfrumBolt.cs index cfc61cf935..3fc17e8371 100644 --- a/Projectiles/Magic/WulfrumBolt.cs +++ b/Projectiles/Magic/WulfrumBolt.cs @@ -67,10 +67,8 @@ namespace CalamityMod.Projectiles.Magic float bestScore = 0; NPC bestTarget = null; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC potentialTarget in Main.ActiveNPCs) { - NPC potentialTarget = Main.npc[i]; - if (!potentialTarget.CanBeChasedBy(null, false)) continue; diff --git a/Projectiles/Magic/WulfrumManaDrain.cs b/Projectiles/Magic/WulfrumManaDrain.cs index 91f819ab36..94200a851e 100644 --- a/Projectiles/Magic/WulfrumManaDrain.cs +++ b/Projectiles/Magic/WulfrumManaDrain.cs @@ -94,11 +94,9 @@ namespace CalamityMod.Projectiles.Magic { float collisionPoint = 0f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC struckNPC in Main.ActiveNPCs) { - NPC struckNPC = Main.npc[i]; - - if (!struckNPC.active || struckNPC.townNPC || struckNPC.friendly) + if (struckNPC.townNPC || struckNPC.friendly) continue; float distance = struckNPC.Distance(Projectile.Center); diff --git a/Projectiles/Melee/BalefulHarvesterProjectile.cs b/Projectiles/Melee/BalefulHarvesterProjectile.cs index 6abc9e648a..b97798b738 100644 --- a/Projectiles/Melee/BalefulHarvesterProjectile.cs +++ b/Projectiles/Melee/BalefulHarvesterProjectile.cs @@ -73,21 +73,19 @@ namespace CalamityMod.Projectiles.Melee else { float homingRange = 1000f; - int inc; - for (int i = 0; i < Main.maxNPCs; i = inc + 1) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float npcX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcX = n.position.X + (float)(n.width / 2); + float npcY = n.position.Y + (float)(n.height / 2); float npcDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcY); - if (npcDist < homingRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height)) + if (npcDist < homingRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, n.position, n.width, n.height)) { homingRange = npcDist; - Projectile.ai[0] = (float)i; + Projectile.ai[0] = (float)n.whoAmI; } } - inc = i; } } diff --git a/Projectiles/Melee/CosmicShivTrail.cs b/Projectiles/Melee/CosmicShivTrail.cs index 01c7d802d6..ce3d12178f 100644 --- a/Projectiles/Melee/CosmicShivTrail.cs +++ b/Projectiles/Melee/CosmicShivTrail.cs @@ -115,10 +115,9 @@ namespace CalamityMod.Projectiles.Melee return; Player owner = Main.player[Projectile.owner]; - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC target in Main.ActiveNPCs) { - NPC target = Main.npc[i]; - if (!target.active || target.dontTakeDamage || target.friendly) + if (target.dontTakeDamage || target.friendly) continue; // Shock any valid target within range. Check all four corners of their hitbox. @@ -137,7 +136,7 @@ namespace CalamityMod.Projectiles.Melee target.StrikeNPC(target.CalculateHitInfo(damage, 0, crit, 0)); if (Main.netMode != NetmodeID.SinglePlayer) - NetMessage.SendData(MessageID.DamageNPC, -1, -1, null, i, damage, 0f, 0f, crit ? 1 : 0, 0, 0); + NetMessage.SendData(MessageID.DamageNPC, -1, -1, null, target.whoAmI, damage, 0f, 0f, crit ? 1 : 0, 0, 0); } } } diff --git a/Projectiles/Melee/DivineHatchetBoomerang.cs b/Projectiles/Melee/DivineHatchetBoomerang.cs index bb9734bcd1..5f92f2f2dd 100644 --- a/Projectiles/Melee/DivineHatchetBoomerang.cs +++ b/Projectiles/Melee/DivineHatchetBoomerang.cs @@ -165,21 +165,20 @@ namespace CalamityMod.Projectiles.Melee //find a nearby NPC to track float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { bool hasHitNPC = false; for (int j = 0; j < previousNPCs.Count; j++) { - if (previousNPCs[j] == i) + if (previousNPCs[j] == npc.whoAmI) { hasHitNPC = true; } } - NPC npc = Main.npc[i]; if (npc == target) { - previousNPCs.Add(i); + previousNPCs.Add(npc.whoAmI); } if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC) { @@ -187,7 +186,7 @@ namespace CalamityMod.Projectiles.Melee if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Melee/DraconicSpark.cs b/Projectiles/Melee/DraconicSpark.cs index 0f0d594376..fc168a7351 100644 --- a/Projectiles/Melee/DraconicSpark.cs +++ b/Projectiles/Melee/DraconicSpark.cs @@ -46,12 +46,12 @@ namespace CalamityMod.Projectiles.Melee float targetY = Projectile.Center.Y; bool foundTarget = false; float maxRange = MaxHomingRange; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - float iterCenterX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float iterCenterY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float iterCenterX = n.position.X + (float)(n.width / 2); + float iterCenterY = n.position.Y + (float)(n.height / 2); float dist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - iterCenterX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - iterCenterY); if (dist < maxRange) { diff --git a/Projectiles/Melee/DragonRageStaff.cs b/Projectiles/Melee/DragonRageStaff.cs index 5973f399e6..20943dfd06 100644 --- a/Projectiles/Melee/DragonRageStaff.cs +++ b/Projectiles/Melee/DragonRageStaff.cs @@ -191,13 +191,12 @@ namespace CalamityMod.Projectiles.Melee Rectangle myRect = Projectile.Hitbox; if (Projectile.owner == Main.myPlayer) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; bool voodooDolls = Projectile.owner < Main.maxPlayers && (npc.type == NPCID.Guide && player.killGuide || npc.type == NPCID.Clothier && player.killClothier); bool friendlyProjs = Projectile.friendly && (!npc.friendly || voodooDolls); bool hostileProjs = Projectile.hostile && npc.friendly && !npc.dontTakeDamageFromHostiles; - if (npc.active && !npc.dontTakeDamage && (friendlyProjs || hostileProjs) && (Projectile.owner < 0 || npc.immune[Projectile.owner] == 0 || Projectile.maxPenetrate == 1)) + if (!npc.dontTakeDamage && (friendlyProjs || hostileProjs) && (Projectile.owner < 0 || npc.immune[Projectile.owner] == 0 || Projectile.maxPenetrate == 1)) { if (npc.noTileCollide || !Projectile.ownerHitCheck || (ProjectileLoader.CanHitNPC(Projectile, npc) ?? false)) { diff --git a/Projectiles/Melee/Galaxia_AriesWrath.cs b/Projectiles/Melee/Galaxia_AriesWrath.cs index d0e7cd2ecc..b2e1e6e179 100644 --- a/Projectiles/Melee/Galaxia_AriesWrath.cs +++ b/Projectiles/Melee/Galaxia_AriesWrath.cs @@ -88,9 +88,8 @@ namespace CalamityMod.Projectiles.Melee { float longestReach = MaxProjReach; NPC target = null; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (!excludedTargets.Contains(npc) && npc.CanBeChasedBy() && !npc.friendly && !npc.townNPC) { float distance = Vector2.Distance(hitFrom, npc.Center); diff --git a/Projectiles/Melee/GalaxyBlast.cs b/Projectiles/Melee/GalaxyBlast.cs index fffe60486e..40daf00f84 100644 --- a/Projectiles/Melee/GalaxyBlast.cs +++ b/Projectiles/Melee/GalaxyBlast.cs @@ -79,16 +79,15 @@ namespace CalamityMod.Projectiles.Melee { int npcTracker = -1; float homingRange = 60f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC in Main.ActiveNPCs) { - NPC nPC = Main.npc[i]; if (nPC.CanBeChasedBy(Projectile, false)) { float npcDistance = Projectile.Distance(nPC.Center); if (npcDistance < homingRange && Collision.CanHitLine(Projectile.Center, 0, 0, nPC.Center, 0, 0)) { homingRange = npcDistance; - npcTracker = i; + npcTracker = nPC.whoAmI; } } } diff --git a/Projectiles/Melee/GalaxyBlastType2.cs b/Projectiles/Melee/GalaxyBlastType2.cs index d48506f7bc..972ec53f1c 100644 --- a/Projectiles/Melee/GalaxyBlastType2.cs +++ b/Projectiles/Melee/GalaxyBlastType2.cs @@ -80,16 +80,15 @@ namespace CalamityMod.Projectiles.Melee { int npcTracker = -1; float homingRange = 60f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC in Main.ActiveNPCs) { - NPC nPC = Main.npc[i]; if (nPC.CanBeChasedBy(Projectile, false)) { float npcDistance = Projectile.Distance(nPC.Center); if (npcDistance < homingRange && Collision.CanHitLine(Projectile.Center, 0, 0, nPC.Center, 0, 0)) { homingRange = npcDistance; - npcTracker = i; + npcTracker = nPC.whoAmI; } } } diff --git a/Projectiles/Melee/GalaxyBlastType3.cs b/Projectiles/Melee/GalaxyBlastType3.cs index 42903a8258..4555006dfc 100644 --- a/Projectiles/Melee/GalaxyBlastType3.cs +++ b/Projectiles/Melee/GalaxyBlastType3.cs @@ -79,16 +79,15 @@ namespace CalamityMod.Projectiles.Melee { int npcTracker = -1; float homingRange = 60f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC in Main.ActiveNPCs) { - NPC nPC = Main.npc[i]; if (nPC.CanBeChasedBy(Projectile, false)) { float npcDistance = Projectile.Distance(nPC.Center); if (npcDistance < homingRange && Collision.CanHitLine(Projectile.Center, 0, 0, nPC.Center, 0, 0)) { homingRange = npcDistance; - npcTracker = i; + npcTracker = nPC.whoAmI; } } } diff --git a/Projectiles/Melee/MendedBiomeBlade_TrueGrovetendersTouch.cs b/Projectiles/Melee/MendedBiomeBlade_TrueGrovetendersTouch.cs index fe00ddb07e..f48f6e2a5e 100644 --- a/Projectiles/Melee/MendedBiomeBlade_TrueGrovetendersTouch.cs +++ b/Projectiles/Melee/MendedBiomeBlade_TrueGrovetendersTouch.cs @@ -132,9 +132,8 @@ namespace CalamityMod.Projectiles.Melee { float longestReach = MaxTangleReach; NPC target = null; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (!excludedTargets.Contains(npc) && npc.CanBeChasedBy() && !npc.friendly && !npc.townNPC) { float distance = Vector2.Distance(hitFrom, npc.Center); diff --git a/Projectiles/Melee/StygianShieldThrown.cs b/Projectiles/Melee/StygianShieldThrown.cs index bea6327383..72d22920b4 100644 --- a/Projectiles/Melee/StygianShieldThrown.cs +++ b/Projectiles/Melee/StygianShieldThrown.cs @@ -115,17 +115,16 @@ namespace CalamityMod.Projectiles.Melee // Find the closest NPC targetable float range = MaxHomingRange; int targetNPC = -1; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC target in Main.ActiveNPCs) { - NPC target = Main.npc[i]; - if (!target.CanBeChasedBy(Projectile) || PreviousNPCs.Contains(i)) + if (!target.CanBeChasedBy(Projectile) || PreviousNPCs.Contains(target.whoAmI)) continue; float distance = Vector2.Distance(target.Center, Projectile.Center); if (distance < range && Collision.CanHit(Projectile, target)) { range = distance; - targetNPC = i; + targetNPC = target.whoAmI; } } diff --git a/Projectiles/Melee/TenebreusTidesWaterSpear.cs b/Projectiles/Melee/TenebreusTidesWaterSpear.cs index 05a3840fc4..ed67f22819 100644 --- a/Projectiles/Melee/TenebreusTidesWaterSpear.cs +++ b/Projectiles/Melee/TenebreusTidesWaterSpear.cs @@ -121,9 +121,8 @@ namespace CalamityMod.Projectiles.Melee int whoAmI = -1; Vector2 targetSpot = Projectile.Center; float detectRange = 700f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float targetDist = Vector2.Distance(npc.Center, Projectile.Center); @@ -131,7 +130,7 @@ namespace CalamityMod.Projectiles.Melee { detectRange = targetDist; targetSpot = npc.Center; - whoAmI = i; + whoAmI = npc.whoAmI; } } } diff --git a/Projectiles/Melee/TenebreusTidesWaterSword.cs b/Projectiles/Melee/TenebreusTidesWaterSword.cs index f4b305e391..b4227689fc 100644 --- a/Projectiles/Melee/TenebreusTidesWaterSword.cs +++ b/Projectiles/Melee/TenebreusTidesWaterSword.cs @@ -103,9 +103,8 @@ namespace CalamityMod.Projectiles.Melee int whoAmI = -1; Vector2 targetSpot = Projectile.Center; float detectRange = 700f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float targetDist = Vector2.Distance(npc.Center, Projectile.Center); @@ -113,7 +112,7 @@ namespace CalamityMod.Projectiles.Melee { detectRange = targetDist; targetSpot = npc.Center; - whoAmI = i; + whoAmI = npc.whoAmI; } } } diff --git a/Projectiles/Melee/Tornado.cs b/Projectiles/Melee/Tornado.cs index 8a0fe3f3eb..6e1b19a6df 100644 --- a/Projectiles/Melee/Tornado.cs +++ b/Projectiles/Melee/Tornado.cs @@ -82,9 +82,8 @@ namespace CalamityMod.Projectiles.Melee } float projX = Projectile.Center.X; float projY = Projectile.Center.Y; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, npc.Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) { float npcCenterX = npc.position.X + (float)(npc.width / 2); diff --git a/Projectiles/Melee/TrueBiomeBlade_LamentationsOfTheChained.cs b/Projectiles/Melee/TrueBiomeBlade_LamentationsOfTheChained.cs index 34e6c90b6f..cde782bee1 100644 --- a/Projectiles/Melee/TrueBiomeBlade_LamentationsOfTheChained.cs +++ b/Projectiles/Melee/TrueBiomeBlade_LamentationsOfTheChained.cs @@ -142,9 +142,8 @@ namespace CalamityMod.Projectiles.Melee { float longestReach = MaxTangleReach; NPC target = null; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (!excludedTargets.Contains(npc) && npc.CanBeChasedBy() && !npc.friendly && !npc.townNPC) { float distance = Vector2.Distance(hitFrom, npc.Center); diff --git a/Projectiles/Melee/UrchinSpikeFugu.cs b/Projectiles/Melee/UrchinSpikeFugu.cs index 32fa5908ae..301d95a46a 100644 --- a/Projectiles/Melee/UrchinSpikeFugu.cs +++ b/Projectiles/Melee/UrchinSpikeFugu.cs @@ -29,15 +29,14 @@ namespace CalamityMod.Projectiles.Melee { float maxRange = 100f; int npcIndex = -1; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, npc.position, npc.width, npc.height)) { float targetDist = (npc.Center - Projectile.Center).Length(); if (targetDist < maxRange) { - npcIndex = i; + npcIndex = npc.whoAmI; maxRange = targetDist; } } diff --git a/Projectiles/Melee/VoidEssence.cs b/Projectiles/Melee/VoidEssence.cs index f04b58b948..692aea191f 100644 --- a/Projectiles/Melee/VoidEssence.cs +++ b/Projectiles/Melee/VoidEssence.cs @@ -88,19 +88,15 @@ namespace CalamityMod.Projectiles.Melee int targetIdx = -1; float maxHomingRange = 400f; bool hasHomingTarget = false; - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (npc == null || !npc.active) - continue; - // Won't home in through walls and won't chase invulnerable targets. if (npc.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, npc.Center, 1, 1)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < maxHomingRange) { - targetIdx = i; + targetIdx = npc.whoAmI; maxHomingRange = dist; hasHomingTarget = true; } diff --git a/Projectiles/Melee/WulfrumScrewdriverProj.cs b/Projectiles/Melee/WulfrumScrewdriverProj.cs index 055f9e5eaf..e06f69f78e 100644 --- a/Projectiles/Melee/WulfrumScrewdriverProj.cs +++ b/Projectiles/Melee/WulfrumScrewdriverProj.cs @@ -132,10 +132,8 @@ namespace CalamityMod.Projectiles.Melee float bestScore = 0; NPC bestTarget = null; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC potentialTarget in Main.ActiveNPCs) { - NPC potentialTarget = Main.npc[i]; - if (!potentialTarget.CanBeChasedBy(null, false)) continue; diff --git a/Projectiles/Melee/Yoyos/ObliteratorYoyo.cs b/Projectiles/Melee/Yoyos/ObliteratorYoyo.cs index d630b7e18f..216ed1159c 100644 --- a/Projectiles/Melee/Yoyos/ObliteratorYoyo.cs +++ b/Projectiles/Melee/Yoyos/ObliteratorYoyo.cs @@ -65,19 +65,15 @@ namespace CalamityMod.Projectiles.Melee.Yoyos { List targets = new List(); float laserRange = 300f; - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC n in Main.ActiveNPCs) { - ref NPC n = ref Main.npc[i]; - if (n is null || !n.active) - continue; - if (n.CanBeChasedBy(Projectile, false) && (n.Center - Projectile.Center).Length() <= laserRange && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - targets.Add(i); + targets.Add(n.whoAmI); // Bosses are added 5 times instead of 1 so that they are preferentially but not exclusively targeted. if (n.boss) for (int j = 0; j < 4; ++j) - targets.Add(i); + targets.Add(n.whoAmI); } } if (targets.Count == 0) diff --git a/Projectiles/Melee/Yoyos/OracleYoyo.cs b/Projectiles/Melee/Yoyos/OracleYoyo.cs index 7c0b87a262..54db87c83f 100644 --- a/Projectiles/Melee/Yoyos/OracleYoyo.cs +++ b/Projectiles/Melee/Yoyos/OracleYoyo.cs @@ -221,10 +221,9 @@ namespace CalamityMod.Projectiles.Melee.Yoyos return; Player owner = Main.player[Projectile.owner]; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC target in Main.ActiveNPCs) { - NPC target = Main.npc[i]; - if (!target.active || target.dontTakeDamage || target.friendly) + if (target.dontTakeDamage || target.friendly) continue; // Shock any valid target within range. Check all four corners of their hitbox. @@ -241,7 +240,7 @@ namespace CalamityMod.Projectiles.Melee.Yoyos int finalDamage = (int)owner.GetTotalDamage().ApplyTo(baseDamage); if (Projectile.owner == Main.myPlayer) { - Projectile p = Projectile.NewProjectileDirect(Projectile.GetSource_FromThis(), target.Center, Vector2.Zero, ModContent.ProjectileType(), finalDamage, 0f, Projectile.owner, i); + Projectile p = Projectile.NewProjectileDirect(Projectile.GetSource_FromThis(), target.Center, Vector2.Zero, ModContent.ProjectileType(), finalDamage, 0f, Projectile.owner, target.whoAmI); if (p.whoAmI.WithinBounds(Main.maxProjectiles)) p.DamageType = DamageClass.MeleeNoSpeed; } diff --git a/Projectiles/Ranged/DrataliornusFlame.cs b/Projectiles/Ranged/DrataliornusFlame.cs index c80e073847..d898a477a0 100644 --- a/Projectiles/Ranged/DrataliornusFlame.cs +++ b/Projectiles/Ranged/DrataliornusFlame.cs @@ -73,11 +73,9 @@ namespace CalamityMod.Projectiles.Ranged int possibleTarget = -1; float closestDistance = 700f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - - if (npc.active && npc.chaseable && npc.lifeMax > 5 && !npc.dontTakeDamage && !npc.friendly && + if (npc.chaseable && npc.lifeMax > 5 && !npc.dontTakeDamage && !npc.friendly && !npc.immortal && Collision.CanHit(Projectile.Center, 0, 0, npc.Center, 0, 0)) { float distance = Vector2.Distance(Projectile.Center, npc.Center); @@ -85,7 +83,7 @@ namespace CalamityMod.Projectiles.Ranged if (closestDistance > distance) { closestDistance = distance; - possibleTarget = i; + possibleTarget = npc.whoAmI; } } } diff --git a/Projectiles/Ranged/ExoLight.cs b/Projectiles/Ranged/ExoLight.cs index 25dc0f5244..0971c27bc8 100644 --- a/Projectiles/Ranged/ExoLight.cs +++ b/Projectiles/Ranged/ExoLight.cs @@ -71,10 +71,10 @@ namespace CalamityMod.Projectiles.Ranged Projectile.scale = MathHelper.Lerp(0.001f, 1f, Utils.GetLerpValue(0f, 25f, Time, true)); if (Projectile.localAI[0] == 0f) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile.GetSource_FromThis(), false)) - NPCDestination = Main.npc[i].Center + Main.npc[i].velocity * 5f; + if (n.CanBeChasedBy(Projectile.GetSource_FromThis(), false)) + NPCDestination = n.Center + n.velocity * 5f; } InitialCenter = Projectile.Center; Projectile.localAI[0] = 1f; diff --git a/Projectiles/Ranged/Nanomachine.cs b/Projectiles/Ranged/Nanomachine.cs index 75f9d5c6a7..d6410d7a2a 100644 --- a/Projectiles/Ranged/Nanomachine.cs +++ b/Projectiles/Ranged/Nanomachine.cs @@ -84,20 +84,20 @@ namespace CalamityMod.Projectiles.Ranged int targetID = 0; if (Projectile.ai[1] == 0f) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && (Projectile.ai[1] == 0f || Projectile.ai[1] == (float)(i + 1))) + if (n.CanBeChasedBy(Projectile, false) && (Projectile.ai[1] == 0f || Projectile.ai[1] == (float)(i + 1))) { - float targetX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float targetY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float targetX = n.position.X + (float)(n.width / 2); + float targetY = n.position.Y + (float)(n.height / 2); float targetDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - targetX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - targetY); - if (targetDist < constant && Collision.CanHit(new Vector2(Projectile.position.X + (float)(Projectile.width / 2), Projectile.position.Y + (float)(Projectile.height / 2)), 1, 1, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height)) + if (targetDist < constant && Collision.CanHit(new Vector2(Projectile.position.X + (float)(Projectile.width / 2), Projectile.position.Y + (float)(Projectile.height / 2)), 1, 1, n.position, n.width, n.height)) { constant = targetDist; projX = targetX; projY = targetY; canHit = true; - targetID = i; + targetID = n.whoAmI; } } } diff --git a/Projectiles/Ranged/ThePackMissile.cs b/Projectiles/Ranged/ThePackMissile.cs index 4275d0a5f7..75b884a75a 100644 --- a/Projectiles/Ranged/ThePackMissile.cs +++ b/Projectiles/Ranged/ThePackMissile.cs @@ -36,11 +36,11 @@ namespace CalamityMod.Projectiles.Ranged Vector2 targetCenter = Projectile.Center; float minTargetDistance = 2500f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - float distanceFromTarget = Projectile.Center.ManhattanDistance(Main.npc[i].Center); + float distanceFromTarget = Projectile.Center.ManhattanDistance(n.Center); if (distanceFromTarget < 200f) { if (Projectile.owner == Main.myPlayer) @@ -58,7 +58,7 @@ namespace CalamityMod.Projectiles.Ranged else if (distanceFromTarget < minTargetDistance) { minTargetDistance = distanceFromTarget; - targetCenter = Main.npc[i].Center; + targetCenter = n.Center; homeIn = true; } } diff --git a/Projectiles/Ranged/UniversalGenesisStarcaller.cs b/Projectiles/Ranged/UniversalGenesisStarcaller.cs index 8fedbd47d4..3fe690c60b 100644 --- a/Projectiles/Ranged/UniversalGenesisStarcaller.cs +++ b/Projectiles/Ranged/UniversalGenesisStarcaller.cs @@ -87,9 +87,8 @@ namespace CalamityMod.Projectiles.Ranged bool bossFound = false; int life = 0; Vector2 targetVec = Projectile.Center; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (bossFound && !npc.IsABoss()) continue; if (npc.CanBeChasedBy(Projectile, false)) diff --git a/Projectiles/Ranged/VeriumBulletProj.cs b/Projectiles/Ranged/VeriumBulletProj.cs index c88c124d7d..58860907cd 100644 --- a/Projectiles/Ranged/VeriumBulletProj.cs +++ b/Projectiles/Ranged/VeriumBulletProj.cs @@ -84,9 +84,8 @@ namespace CalamityMod.Projectiles.Ranged if (!homeIn) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float extraDistance = (npc.width / 2) + (npc.height / 2); diff --git a/Projectiles/Rogue/AuroradicalStar.cs b/Projectiles/Rogue/AuroradicalStar.cs index 30c8ad5c1c..e68ea44f36 100644 --- a/Projectiles/Rogue/AuroradicalStar.cs +++ b/Projectiles/Rogue/AuroradicalStar.cs @@ -72,16 +72,15 @@ namespace CalamityMod.Projectiles.Rogue //Home in float maxDistance = 800f; int targetIndex = -1; - int i; - for (i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (Main.npc[i].width / 2) + (Main.npc[i].height / 2); + float extraDistance = (n.width / 2) + (n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance)) { - targetIndex = i; + targetIndex = n.whoAmI; break; } } diff --git a/Projectiles/Rogue/BlunderBoosterLightning.cs b/Projectiles/Rogue/BlunderBoosterLightning.cs index a6c2dfa5ae..191a430104 100644 --- a/Projectiles/Rogue/BlunderBoosterLightning.cs +++ b/Projectiles/Rogue/BlunderBoosterLightning.cs @@ -57,16 +57,15 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = -1; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/BouncyBol.cs b/Projectiles/Rogue/BouncyBol.cs index 0d312c0f8d..35e9a14dd2 100644 --- a/Projectiles/Rogue/BouncyBol.cs +++ b/Projectiles/Rogue/BouncyBol.cs @@ -1,4 +1,4 @@ -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; using System.Collections.Generic; using Terraria; using Terraria.ID; @@ -47,21 +47,20 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { bool hasHitNPC = false; for (int j = 0; j < previousNPCs.Count; j++) { - if (previousNPCs[j] == i) + if (previousNPCs[j] == npc.whoAmI) { hasHitNPC = true; } } - NPC npc = Main.npc[i]; if (npc == target) { - previousNPCs.Add(i); + previousNPCs.Add(npc.whoAmI); } if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC) { @@ -69,7 +68,7 @@ namespace CalamityMod.Projectiles.Rogue if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/CobaltEnergy.cs b/Projectiles/Rogue/CobaltEnergy.cs index 69d3ed5d4e..eab8edce99 100644 --- a/Projectiles/Rogue/CobaltEnergy.cs +++ b/Projectiles/Rogue/CobaltEnergy.cs @@ -59,21 +59,20 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { bool hasHitNPC = false; for (int j = 0; j < previousNPCs.Count; j++) { - if (previousNPCs[j] == i) + if (previousNPCs[j] == npc.whoAmI) { hasHitNPC = true; } } - NPC npc = Main.npc[i]; if (npc == target) { - previousNPCs.Add(i); + previousNPCs.Add(npc.whoAmI); } if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC) { @@ -81,7 +80,7 @@ namespace CalamityMod.Projectiles.Rogue if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/DestructionBolt.cs b/Projectiles/Rogue/DestructionBolt.cs index d7dad8bd93..fd67b5fbff 100644 --- a/Projectiles/Rogue/DestructionBolt.cs +++ b/Projectiles/Rogue/DestructionBolt.cs @@ -55,16 +55,16 @@ namespace CalamityMod.Projectiles.Rogue { int index = -1; float otherConstant = 300f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - Vector2 npcCenter = Main.npc[i].Center; + Vector2 npcCenter = n.Center; float targetDist = Vector2.Distance(npcCenter, Projectile.Center); if (targetDist < otherConstant && index == -1 && Collision.CanHitLine(Projectile.Center, 1, 1, npcCenter, 1, 1)) { otherConstant = targetDist; - index = i; + index = n.whoAmI; } } } diff --git a/Projectiles/Rogue/ElementalDiskProj.cs b/Projectiles/Rogue/ElementalDiskProj.cs index 8f2c0c7268..3f90caa6b7 100644 --- a/Projectiles/Rogue/ElementalDiskProj.cs +++ b/Projectiles/Rogue/ElementalDiskProj.cs @@ -116,9 +116,8 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 300f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float extraDistance = npc.width / 2 + npc.height / 2; diff --git a/Projectiles/Rogue/EnchantedAxeProj.cs b/Projectiles/Rogue/EnchantedAxeProj.cs index e983fd061d..9501976f77 100644 --- a/Projectiles/Rogue/EnchantedAxeProj.cs +++ b/Projectiles/Rogue/EnchantedAxeProj.cs @@ -73,16 +73,15 @@ namespace CalamityMod.Projectiles.Rogue float minDist = 999f; int index = 0; // Get the closest enemy to the axe - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/EquanimityDarkShard.cs b/Projectiles/Rogue/EquanimityDarkShard.cs index 46fcc5ab9f..5b056ca4a0 100644 --- a/Projectiles/Rogue/EquanimityDarkShard.cs +++ b/Projectiles/Rogue/EquanimityDarkShard.cs @@ -1,4 +1,4 @@ -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; using Terraria; using Terraria.ModLoader; using Terraria.ID; @@ -30,16 +30,15 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/FishboneBoomerangProjectile.cs b/Projectiles/Rogue/FishboneBoomerangProjectile.cs index b49727d24e..5786c00fd0 100644 --- a/Projectiles/Rogue/FishboneBoomerangProjectile.cs +++ b/Projectiles/Rogue/FishboneBoomerangProjectile.cs @@ -168,18 +168,18 @@ namespace CalamityMod.Projectiles.Rogue float closestNPCDistance = 10000f; float targettingDistance = 400f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (i == target.whoAmI) + if (n.whoAmI == target.whoAmI) continue; - if (Main.npc[i].CanBeChasedBy(Projectile)) + if (n.CanBeChasedBy(Projectile)) { - float potentialNewDistance = (Projectile.Center - Main.npc[i].Center).Length(); + float potentialNewDistance = (Projectile.Center - n.Center).Length(); if (potentialNewDistance < targettingDistance && potentialNewDistance < closestNPCDistance) { closestNPCDistance = potentialNewDistance; - newTarget = Main.npc[i]; + newTarget = n; } } } diff --git a/Projectiles/Rogue/GildedDaggerProj.cs b/Projectiles/Rogue/GildedDaggerProj.cs index 56a0f11ac7..1c85297dee 100644 --- a/Projectiles/Rogue/GildedDaggerProj.cs +++ b/Projectiles/Rogue/GildedDaggerProj.cs @@ -1,4 +1,4 @@ -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic; using Terraria; @@ -70,21 +70,20 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { bool hasHitNPC = false; for (int j = 0; j < previousNPCs.Count; j++) { - if (previousNPCs[j] == i) + if (previousNPCs[j] == npc.whoAmI) { hasHitNPC = true; } } - NPC npc = Main.npc[i]; if (npc == target) { - previousNPCs.Add(i); + previousNPCs.Add(npc.whoAmI); } if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC) { @@ -92,7 +91,7 @@ namespace CalamityMod.Projectiles.Rogue if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/GleamingDaggerProj.cs b/Projectiles/Rogue/GleamingDaggerProj.cs index ad36a49670..447bf599d0 100644 --- a/Projectiles/Rogue/GleamingDaggerProj.cs +++ b/Projectiles/Rogue/GleamingDaggerProj.cs @@ -1,4 +1,4 @@ -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic; using Terraria; @@ -70,21 +70,20 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { bool hasHitNPC = false; for (int j = 0; j < previousNPCs.Count; j++) { - if (previousNPCs[j] == i) + if (previousNPCs[j] == npc.whoAmI) { hasHitNPC = true; } } - NPC npc = Main.npc[i]; if (npc == target) { - previousNPCs.Add(i); + previousNPCs.Add(npc.whoAmI); } if (npc.CanBeChasedBy(Projectile, false) && npc != target && !hasHitNPC) { @@ -92,7 +91,7 @@ namespace CalamityMod.Projectiles.Rogue if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/IceStarProjectile.cs b/Projectiles/Rogue/IceStarProjectile.cs index 1756a355ab..549445eefe 100644 --- a/Projectiles/Rogue/IceStarProjectile.cs +++ b/Projectiles/Rogue/IceStarProjectile.cs @@ -51,15 +51,15 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = Projectile.Calamity().stealthStrike ? 800f : 400f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/IcebreakerHammer.cs b/Projectiles/Rogue/IcebreakerHammer.cs index c383c2a93e..cfc9332937 100644 --- a/Projectiles/Rogue/IcebreakerHammer.cs +++ b/Projectiles/Rogue/IcebreakerHammer.cs @@ -64,10 +64,9 @@ namespace CalamityMod.Projectiles.Rogue int buffType = ModContent.BuffType(); float radius = 112f; // 7 blocks - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC in Main.ActiveNPCs) { - NPC nPC = Main.npc[i]; - if (nPC.active && !nPC.dontTakeDamage && !nPC.buffImmune[buffType] && Vector2.Distance(Projectile.Center, nPC.Center) <= radius) + if (!nPC.dontTakeDamage && !nPC.buffImmune[buffType] && Vector2.Distance(Projectile.Center, nPC.Center) <= radius) { if (nPC.FindBuffIndex(buffType) == -1) nPC.AddBuff(buffType, 60, false); diff --git a/Projectiles/Rogue/NanoblackSplit.cs b/Projectiles/Rogue/NanoblackSplit.cs index 02acd5659f..5b5b9e37f4 100644 --- a/Projectiles/Rogue/NanoblackSplit.cs +++ b/Projectiles/Rogue/NanoblackSplit.cs @@ -128,10 +128,9 @@ namespace CalamityMod.Projectiles.Rogue bool bossFound = false; int target = -1; float minDist = HomingStartRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.type == NPCID.TargetDummy) + if (npc.type == NPCID.TargetDummy) continue; // If we've found a valid boss target, ignore ALL targets which aren't bosses. @@ -149,7 +148,7 @@ namespace CalamityMod.Projectiles.Rogue if (npc.boss) bossFound = true; minDist = distToNPC; - target = i; + target = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/NanoblackStealthSplit.cs b/Projectiles/Rogue/NanoblackStealthSplit.cs index f47d6f7550..4881561c22 100644 --- a/Projectiles/Rogue/NanoblackStealthSplit.cs +++ b/Projectiles/Rogue/NanoblackStealthSplit.cs @@ -137,10 +137,9 @@ namespace CalamityMod.Projectiles.Rogue bool bossFound = false; int target = -1; float minDist = HomingStartRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.type == NPCID.TargetDummy) + if (npc.type == NPCID.TargetDummy) continue; // If we've found a valid boss target, ignore ALL targets which aren't bosses. @@ -158,7 +157,7 @@ namespace CalamityMod.Projectiles.Rogue if (npc.boss) bossFound = true; minDist = distToNPC; - target = i; + target = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/NightsGazeStar.cs b/Projectiles/Rogue/NightsGazeStar.cs index b44f27a51d..33284710b1 100644 --- a/Projectiles/Rogue/NightsGazeStar.cs +++ b/Projectiles/Rogue/NightsGazeStar.cs @@ -1,4 +1,4 @@ - + using CalamityMod.Buffs.DamageOverTime; using Microsoft.Xna.Framework; using Terraria; @@ -65,16 +65,15 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/NychthemeronOrb.cs b/Projectiles/Rogue/NychthemeronOrb.cs index 367c0c5c4e..2927406455 100644 --- a/Projectiles/Rogue/NychthemeronOrb.cs +++ b/Projectiles/Rogue/NychthemeronOrb.cs @@ -64,16 +64,15 @@ namespace CalamityMod.Projectiles.Rogue // Follow Enemy float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/RadiantStar2.cs b/Projectiles/Rogue/RadiantStar2.cs index 91ef243b35..7b46365b10 100644 --- a/Projectiles/Rogue/RadiantStar2.cs +++ b/Projectiles/Rogue/RadiantStar2.cs @@ -1,4 +1,4 @@ -using CalamityMod.Buffs.DamageOverTime; +using CalamityMod.Buffs.DamageOverTime; using CalamityMod.CalPlayer; using CalamityMod.Dusts; using Microsoft.Xna.Framework; @@ -41,30 +41,30 @@ namespace CalamityMod.Projectiles.Rogue float projY = Projectile.Center.Y; float homeRange = Projectile.Calamity().stealthStrike ? 1800f : 600f; float homingSpeed = 0.25f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) { - float npcCenterX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcCenterY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcCenterX = n.position.X + (float)(n.width / 2); + float npcCenterY = n.position.Y + (float)(n.height / 2); float targetDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcCenterX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcCenterY); if (targetDist < homeRange) { - if (Main.npc[i].position.X < projX) + if (n.position.X < projX) { - Main.npc[i].velocity.X += homingSpeed; + n.velocity.X += homingSpeed; } else { - Main.npc[i].velocity.X -= homingSpeed; + n.velocity.X -= homingSpeed; } - if (Main.npc[i].position.Y < projY) + if (n.position.Y < projY) { - Main.npc[i].velocity.Y += homingSpeed; + n.velocity.Y += homingSpeed; } else { - Main.npc[i].velocity.Y -= homingSpeed; + n.velocity.Y -= homingSpeed; } } } diff --git a/Projectiles/Rogue/RadiantStarKnife.cs b/Projectiles/Rogue/RadiantStarKnife.cs index 2486e3c7eb..bcf0aae582 100644 --- a/Projectiles/Rogue/RadiantStarKnife.cs +++ b/Projectiles/Rogue/RadiantStarKnife.cs @@ -41,9 +41,8 @@ namespace CalamityMod.Projectiles.Rogue float projY = Projectile.Center.Y; float homeRange = Projectile.Calamity().stealthStrike ? 1800f : 600f; float homingSpeed = 0.25f; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, npc.Center, 1, 1) && !npc.boss) { float npcX = npc.position.X + (float)(npc.width / 2); diff --git a/Projectiles/Rogue/RealityRuptureMini.cs b/Projectiles/Rogue/RealityRuptureMini.cs index 3e7ae26d62..60e8562298 100644 --- a/Projectiles/Rogue/RealityRuptureMini.cs +++ b/Projectiles/Rogue/RealityRuptureMini.cs @@ -53,16 +53,16 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 350f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); - bool canHit = Projectile.Calamity().stealthStrike || Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); + bool canHit = Projectile.Calamity().stealthStrike || Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && canHit) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && canHit) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/ReaperProjectile.cs b/Projectiles/Rogue/ReaperProjectile.cs index f716ff8824..14e9786b1a 100644 --- a/Projectiles/Rogue/ReaperProjectile.cs +++ b/Projectiles/Rogue/ReaperProjectile.cs @@ -87,10 +87,9 @@ namespace CalamityMod.Projectiles.Rogue Vector2 homingTarget = Projectile.Center; bool foundTarget = false; Vector2 targetVelocity = new Vector2(0f); - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC potentialTarget in Main.ActiveNPCs) { - NPC potentialTarget = Main.npc[i]; - if (potentialTarget.CanBeChasedBy(Projectile, false) && Projectile.WithinRange(Main.npc[i].Center, homingRange)) + if (potentialTarget.CanBeChasedBy(Projectile, false) && Projectile.WithinRange(potentialTarget.Center, homingRange)) { if (!foundTarget) { diff --git a/Projectiles/Rogue/RegulusEnergy.cs b/Projectiles/Rogue/RegulusEnergy.cs index abfca67bbe..542fdcb0f4 100644 --- a/Projectiles/Rogue/RegulusEnergy.cs +++ b/Projectiles/Rogue/RegulusEnergy.cs @@ -54,15 +54,15 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 500f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/ScourgeoftheDesertProj.cs b/Projectiles/Rogue/ScourgeoftheDesertProj.cs index 1f708cbb8c..169e781b77 100644 --- a/Projectiles/Rogue/ScourgeoftheDesertProj.cs +++ b/Projectiles/Rogue/ScourgeoftheDesertProj.cs @@ -101,10 +101,10 @@ namespace CalamityMod.Projectiles.Rogue } if (InsideTiles) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile.GetSource_FromThis(), false)) - NPCDestination = Main.npc[i].Center + Main.npc[i].velocity * 5f; + if (n.CanBeChasedBy(Projectile.GetSource_FromThis(), false)) + NPCDestination = n.Center + n.velocity * 5f; } TimeUnderground++; diff --git a/Projectiles/Rogue/ShatteredSunKnife.cs b/Projectiles/Rogue/ShatteredSunKnife.cs index e27b3b368d..1f44a9e1ef 100644 --- a/Projectiles/Rogue/ShatteredSunKnife.cs +++ b/Projectiles/Rogue/ShatteredSunKnife.cs @@ -90,30 +90,30 @@ namespace CalamityMod.Projectiles.Rogue { float projX = Projectile.Center.X; float projY = Projectile.Center.Y; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) { - float npcCenterX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcCenterY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcCenterX = n.position.X + (float)(n.width / 2); + float npcCenterY = n.position.Y + (float)(n.height / 2); float targetDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcCenterX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcCenterY); if (targetDist < 600f) { - if (Main.npc[i].position.X < projX) + if (n.position.X < projX) { - Main.npc[i].velocity.X += 0.25f; + n.velocity.X += 0.25f; } else { - Main.npc[i].velocity.X -= 0.25f; + n.velocity.X -= 0.25f; } - if (Main.npc[i].position.Y < projY) + if (n.position.Y < projY) { - Main.npc[i].velocity.Y += 0.25f; + n.velocity.Y += 0.25f; } else { - Main.npc[i].velocity.Y -= 0.25f; + n.velocity.Y -= 0.25f; } } } diff --git a/Projectiles/Rogue/ShatteredSunScorchedBlade.cs b/Projectiles/Rogue/ShatteredSunScorchedBlade.cs index 36deeca44d..3f7f5fd760 100644 --- a/Projectiles/Rogue/ShatteredSunScorchedBlade.cs +++ b/Projectiles/Rogue/ShatteredSunScorchedBlade.cs @@ -112,30 +112,30 @@ namespace CalamityMod.Projectiles.Rogue { float projX = Projectile.Center.X; float projY = Projectile.Center.Y; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1) && !CalamityPlayer.areThereAnyDamnBosses) { - float npcCenterX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcCenterY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcCenterX = n.position.X + (float)(n.width / 2); + float npcCenterY = n.position.Y + (float)(n.height / 2); float targetDistance = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcCenterX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcCenterY); if (targetDistance < 600f) { - if (Main.npc[i].position.X < projX) + if (n.position.X < projX) { - Main.npc[i].velocity.X += 0.25f; + n.velocity.X += 0.25f; } else { - Main.npc[i].velocity.X -= 0.25f; + n.velocity.X -= 0.25f; } - if (Main.npc[i].position.Y < projY) + if (n.position.Y < projY) { - Main.npc[i].velocity.Y += 0.25f; + n.velocity.Y += 0.25f; } else { - Main.npc[i].velocity.Y -= 0.25f; + n.velocity.Y -= 0.25f; } } } diff --git a/Projectiles/Rogue/ShockGrenadeBolt.cs b/Projectiles/Rogue/ShockGrenadeBolt.cs index 6433576e54..e33e28c6db 100644 --- a/Projectiles/Rogue/ShockGrenadeBolt.cs +++ b/Projectiles/Rogue/ShockGrenadeBolt.cs @@ -54,16 +54,15 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Rogue/SpearofDestinyProjectile.cs b/Projectiles/Rogue/SpearofDestinyProjectile.cs index ff055dbf9b..7caa8c056a 100644 --- a/Projectiles/Rogue/SpearofDestinyProjectile.cs +++ b/Projectiles/Rogue/SpearofDestinyProjectile.cs @@ -63,16 +63,16 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 200f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); - bool canHit = Projectile.Calamity().stealthStrike || Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); + bool canHit = Projectile.Calamity().stealthStrike || Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && canHit) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && canHit) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/StellarKnifeProj.cs b/Projectiles/Rogue/StellarKnifeProj.cs index e9a8c194d0..7d70ca9c02 100644 --- a/Projectiles/Rogue/StellarKnifeProj.cs +++ b/Projectiles/Rogue/StellarKnifeProj.cs @@ -1,4 +1,4 @@ -using CalamityMod.Buffs.DamageOverTime; +using CalamityMod.Buffs.DamageOverTime; using CalamityMod.Dusts; using Microsoft.Xna.Framework; using System.IO; @@ -69,15 +69,15 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 600f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (n.CanBeChasedBy(Projectile, false) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/SupernovaHoming.cs b/Projectiles/Rogue/SupernovaHoming.cs index a9793a17b9..21dc29bd9d 100644 --- a/Projectiles/Rogue/SupernovaHoming.cs +++ b/Projectiles/Rogue/SupernovaHoming.cs @@ -47,15 +47,15 @@ namespace CalamityMod.Projectiles.Rogue float maxDistance = 500f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Rogue/TerraDiskProjectile.cs b/Projectiles/Rogue/TerraDiskProjectile.cs index e68019dd05..6fec82862f 100644 --- a/Projectiles/Rogue/TerraDiskProjectile.cs +++ b/Projectiles/Rogue/TerraDiskProjectile.cs @@ -143,9 +143,8 @@ namespace CalamityMod.Projectiles.Rogue const float maxDistance = 300f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float extraDistance = npc.width / 2 + npc.height / 2; diff --git a/Projectiles/Rogue/TimeBoltKnife.cs b/Projectiles/Rogue/TimeBoltKnife.cs index 6f82034099..88aafef7fb 100644 --- a/Projectiles/Rogue/TimeBoltKnife.cs +++ b/Projectiles/Rogue/TimeBoltKnife.cs @@ -132,9 +132,8 @@ namespace CalamityMod.Projectiles.Rogue int whoAmI = -1; Vector2 targetSpot = Projectile.Center; float detectRange = 1000f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float targetDist = Vector2.Distance(npc.Center, Projectile.Center); @@ -142,7 +141,7 @@ namespace CalamityMod.Projectiles.Rogue { detectRange = targetDist; targetSpot = npc.Center; - whoAmI = i; + whoAmI = npc.whoAmI; } } } @@ -304,10 +303,9 @@ namespace CalamityMod.Projectiles.Rogue int buffType = ModContent.BuffType(); - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (npc.active && !npc.dontTakeDamage && !npc.buffImmune[buffType] && Vector2.Distance(Projectile.Center, npc.Center) <= radius) + if (!npc.dontTakeDamage && !npc.buffImmune[buffType] && Vector2.Distance(Projectile.Center, npc.Center) <= radius) { if (npc.FindBuffIndex(buffType) == -1) npc.AddBuff(buffType, 60, false); diff --git a/Projectiles/Rogue/VeneratedKnife.cs b/Projectiles/Rogue/VeneratedKnife.cs index 1e98831f53..b1762f4465 100644 --- a/Projectiles/Rogue/VeneratedKnife.cs +++ b/Projectiles/Rogue/VeneratedKnife.cs @@ -37,16 +37,15 @@ namespace CalamityMod.Projectiles.Rogue { float minDist = 999f; int index = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < minDist) { minDist = dist; - index = i; + index = npc.whoAmI; } } } diff --git a/Projectiles/Summon/AstrageldonSummon.cs b/Projectiles/Summon/AstrageldonSummon.cs index 5bb58587ae..bc1610fa94 100644 --- a/Projectiles/Summon/AstrageldonSummon.cs +++ b/Projectiles/Summon/AstrageldonSummon.cs @@ -123,9 +123,8 @@ namespace CalamityMod.Projectiles.Summon } else { - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { bool lineOfSight = Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, npc.position, npc.width, npc.height); diff --git a/Projectiles/Summon/BabyPlaguebringer.cs b/Projectiles/Summon/BabyPlaguebringer.cs index c2e73d0f19..10230156e1 100644 --- a/Projectiles/Summon/BabyPlaguebringer.cs +++ b/Projectiles/Summon/BabyPlaguebringer.cs @@ -100,9 +100,8 @@ namespace CalamityMod.Projectiles.Summon } if (!canAttack) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC2 in Main.ActiveNPCs) { - NPC nPC2 = Main.npc[i]; if (nPC2.CanBeChasedBy(Projectile, false)) { float targetDist = Vector2.Distance(nPC2.Center, Projectile.Center); diff --git a/Projectiles/Summon/BrimstoneElementalMinion.cs b/Projectiles/Summon/BrimstoneElementalMinion.cs index 26c28e707c..5dfabdaa7b 100644 --- a/Projectiles/Summon/BrimstoneElementalMinion.cs +++ b/Projectiles/Summon/BrimstoneElementalMinion.cs @@ -117,9 +117,8 @@ namespace CalamityMod.Projectiles.Summon } if (!canAttack && !passive) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC2 in Main.ActiveNPCs) { - NPC nPC2 = Main.npc[i]; if (nPC2.CanBeChasedBy(Projectile, false)) { float targetDist = Vector2.Distance(nPC2.Center, Projectile.Center); diff --git a/Projectiles/Summon/CnidarianJellyfishOnTheString.cs b/Projectiles/Summon/CnidarianJellyfishOnTheString.cs index 362bf33118..ed6e82f054 100644 --- a/Projectiles/Summon/CnidarianJellyfishOnTheString.cs +++ b/Projectiles/Summon/CnidarianJellyfishOnTheString.cs @@ -143,16 +143,16 @@ namespace CalamityMod.Projectiles.Summon int[] targetArray = new int[maxTargets]; int targetsAquired = 0; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { if (targetsAquired == maxTargets) break; - if (Main.npc[i].CanBeChasedBy(Projectile)) + if (n.CanBeChasedBy(Projectile)) { - if ((CnidarianPos - Main.npc[i].Center).Length() < targettingDistance) + if ((CnidarianPos - n.Center).Length() < targettingDistance) { - targetArray[targetsAquired] = i; + targetArray[targetsAquired] = n.whoAmI; targetsAquired++; } } diff --git a/Projectiles/Summon/CosmicEnergySpiral.cs b/Projectiles/Summon/CosmicEnergySpiral.cs index c62e9bcf6b..1de027a696 100644 --- a/Projectiles/Summon/CosmicEnergySpiral.cs +++ b/Projectiles/Summon/CosmicEnergySpiral.cs @@ -76,9 +76,8 @@ namespace CalamityMod.Projectiles.Summon } else { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC2 in Main.ActiveNPCs) { - NPC nPC2 = Main.npc[i]; if (nPC2.CanBeChasedBy(Projectile, false)) { float maxTargetDist = Vector2.Distance(nPC2.Center, Projectile.Center); @@ -87,7 +86,7 @@ namespace CalamityMod.Projectiles.Summon targetDist = maxTargetDist; projPos = nPC2.Center; canAttack = true; - target = i; + target = nPC2.whoAmI; } } } diff --git a/Projectiles/Summon/CosmicViperHomingRocket.cs b/Projectiles/Summon/CosmicViperHomingRocket.cs index fb259bdc3e..9ef293710e 100644 --- a/Projectiles/Summon/CosmicViperHomingRocket.cs +++ b/Projectiles/Summon/CosmicViperHomingRocket.cs @@ -86,15 +86,15 @@ namespace CalamityMod.Projectiles.Summon } if (!homeIn) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Summon/Dreadmine.cs b/Projectiles/Summon/Dreadmine.cs index 51b9079b8c..e5a621e983 100644 --- a/Projectiles/Summon/Dreadmine.cs +++ b/Projectiles/Summon/Dreadmine.cs @@ -140,17 +140,17 @@ namespace CalamityMod.Projectiles.Summon } if (!canAttack) { - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float npcX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float npcY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float npcX = n.position.X + (float)(n.width / 2); + float npcY = n.position.Y + (float)(n.height / 2); float npcDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - npcX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - npcY); if (npcDist < attackDistance) { attackDistance = npcDist; - center12 = Main.npc[i].Center; + center12 = n.Center; canAttack = true; } } diff --git a/Projectiles/Summon/EndoCooperBody.cs b/Projectiles/Summon/EndoCooperBody.cs index 259fe2c373..d86a78e584 100644 --- a/Projectiles/Summon/EndoCooperBody.cs +++ b/Projectiles/Summon/EndoCooperBody.cs @@ -173,9 +173,8 @@ namespace CalamityMod.Projectiles.Summon } if (!gotoenemy) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC nPC2 in Main.ActiveNPCs) { - NPC nPC2 = Main.npc[i]; if (nPC2.CanBeChasedBy(Projectile, false)) { float disttoobjective = Vector2.Distance(nPC2.Center, Projectile.Center); diff --git a/Projectiles/Summon/EndoIceShard.cs b/Projectiles/Summon/EndoIceShard.cs index 968df25c26..1ae45e4685 100644 --- a/Projectiles/Summon/EndoIceShard.cs +++ b/Projectiles/Summon/EndoIceShard.cs @@ -80,12 +80,8 @@ namespace CalamityMod.Projectiles.Summon } if (!hasHomingTarget) { - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (npc == null || !npc.active) - continue; - if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); diff --git a/Projectiles/Summon/HowlsHeartFireball.cs b/Projectiles/Summon/HowlsHeartFireball.cs index 1d30b2da43..d87d06d379 100644 --- a/Projectiles/Summon/HowlsHeartFireball.cs +++ b/Projectiles/Summon/HowlsHeartFireball.cs @@ -82,9 +82,8 @@ namespace CalamityMod.Projectiles.Summon } if (!homeIn) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float extraDistance = (npc.width / 2) + (npc.height / 2); diff --git a/Projectiles/Summon/IceSentry.cs b/Projectiles/Summon/IceSentry.cs index 170aa4b52c..e6532ff2ba 100644 --- a/Projectiles/Summon/IceSentry.cs +++ b/Projectiles/Summon/IceSentry.cs @@ -117,16 +117,15 @@ namespace CalamityMod.Projectiles.Summon float maxDistance = 1000f; int possibleTarget = -1; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile)) { float npcDistance = Projectile.Distance(npc.Center); if (npcDistance < maxDistance) { maxDistance = npcDistance; - possibleTarget = i; + possibleTarget = npc.whoAmI; } } } diff --git a/Projectiles/Summon/OldDukeSharkVomit.cs b/Projectiles/Summon/OldDukeSharkVomit.cs index ac6ea79da9..e0133fd23e 100644 --- a/Projectiles/Summon/OldDukeSharkVomit.cs +++ b/Projectiles/Summon/OldDukeSharkVomit.cs @@ -73,18 +73,14 @@ namespace CalamityMod.Projectiles.Summon } if (!hasHomingTarget) { - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (npc is null || !npc.active) - continue; - if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < maxHomingRange && Collision.CanHit(Projectile.Center, Projectile.width, Projectile.height, npc.Center, npc.width, npc.height)) { - targetIdx = i; + targetIdx = npc.whoAmI; maxHomingRange = dist; hasHomingTarget = true; } diff --git a/Projectiles/Summon/SakuraBullet.cs b/Projectiles/Summon/SakuraBullet.cs index f6b29e3023..ccc5cff891 100644 --- a/Projectiles/Summon/SakuraBullet.cs +++ b/Projectiles/Summon/SakuraBullet.cs @@ -65,9 +65,8 @@ namespace CalamityMod.Projectiles.Summon } if (!homeIn) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if ((npc.CanBeChasedBy(Projectile, false) || (npc.type == NPCID.DukeFishron && (!npc.dontTakeDamage || npc.ai[0] > 9f))) && npc.active) { float extraDistance = (npc.width / 2) + (npc.height / 2); diff --git a/Projectiles/Summon/SepulcherMinion.cs b/Projectiles/Summon/SepulcherMinion.cs index 131c073214..48a9ab090f 100644 --- a/Projectiles/Summon/SepulcherMinion.cs +++ b/Projectiles/Summon/SepulcherMinion.cs @@ -503,16 +503,16 @@ namespace CalamityMod.Projectiles.Summon } float distance = searchDistance * searchDistance; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy() && Main.npc[i].type != heartType) + if (n.CanBeChasedBy() && n.type != heartType) { - float extraDistance = (Main.npc[i].width / 2) + (Main.npc[i].height / 2); + float extraDistance = (n.width / 2) + (n.height / 2); - if (Main.npc[i].WithinRange(Projectile.Center, distance + extraDistance)) + if (n.WithinRange(Projectile.Center, distance + extraDistance)) { - distance = Main.npc[i].DistanceSQ(Projectile.Center); - closestTarget = Main.npc[i]; + distance = n.DistanceSQ(Projectile.Center); + closestTarget = n; } } } diff --git a/Projectiles/Summon/SilvaCrystal.cs b/Projectiles/Summon/SilvaCrystal.cs index 2df2ba0adb..c1779f7474 100644 --- a/Projectiles/Summon/SilvaCrystal.cs +++ b/Projectiles/Summon/SilvaCrystal.cs @@ -114,20 +114,17 @@ namespace CalamityMod.Projectiles.Summon } if (targetID < 0) { - int targetIDCopy; - for (int i = 0; i < Main.maxNPCs; i = targetIDCopy + 1) + foreach (NPC target in Main.ActiveNPCs) { - NPC target = Main.npc[i]; if (target.CanBeChasedBy(Projectile, false)) { float targetDistance = Projectile.Distance(target.Center); if (targetDistance < attackRange && Collision.CanHitLine(Projectile.Center, 0, 0, target.Center, 0, 0)) { attackRange = targetDistance; - targetID = i; + targetID = target.whoAmI; } } - targetIDCopy = i; } } if (targetID != -1) diff --git a/Projectiles/Summon/SolarGod.cs b/Projectiles/Summon/SolarGod.cs index e6564dbf6f..86c0d50238 100644 --- a/Projectiles/Summon/SolarGod.cs +++ b/Projectiles/Summon/SolarGod.cs @@ -100,9 +100,8 @@ namespace CalamityMod.Projectiles.Summon } if (!foundTarget) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float npcDist = Vector2.Distance(Projectile.Center, npc.Center); diff --git a/Projectiles/Summon/SolarPixie.cs b/Projectiles/Summon/SolarPixie.cs index 2f85317f15..e812f41623 100644 --- a/Projectiles/Summon/SolarPixie.cs +++ b/Projectiles/Summon/SolarPixie.cs @@ -100,9 +100,8 @@ namespace CalamityMod.Projectiles.Summon } if (!foundTarget) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float npcDist = Vector2.Distance(Projectile.Center, npc.Center); diff --git a/Projectiles/Summon/Spikecrag.cs b/Projectiles/Summon/Spikecrag.cs index 1390798a71..8c274335b4 100644 --- a/Projectiles/Summon/Spikecrag.cs +++ b/Projectiles/Summon/Spikecrag.cs @@ -58,13 +58,13 @@ namespace CalamityMod.Projectiles.Summon float maxDistance = 1000f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, Projectile.width, Projectile.height, Main.npc[i].Center, Main.npc[i].width, Main.npc[i].height)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, Projectile.width, Projectile.height, n.Center, n.width, n.height)) { homeIn = true; break; diff --git a/Projectiles/Summon/StormjawBaby.cs b/Projectiles/Summon/StormjawBaby.cs index 2f8b3a1654..a3e4514dda 100644 --- a/Projectiles/Summon/StormjawBaby.cs +++ b/Projectiles/Summon/StormjawBaby.cs @@ -111,15 +111,14 @@ namespace CalamityMod.Projectiles.Summon } if (targetIndex < 0) { - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { float num1 = (npc.Center - Projectile.Center).Length(); if (num1 < maxDistance) { - targetIndex = i; + targetIndex = npc.whoAmI; maxDistance = num1; } } @@ -309,9 +308,8 @@ namespace CalamityMod.Projectiles.Summon else { Rectangle rectangle = new Rectangle((int)(Projectile.position.X + Projectile.velocity.X * 0.5f - 4f), (int)(Projectile.position.Y + Projectile.velocity.Y * 0.5f - 4f), Projectile.width + 8, Projectile.height + 8); - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false) && npc.immune[Projectile.owner] <= 0) { Rectangle rect = npc.getRect(); diff --git a/Projectiles/Summon/Umbrella/MagicBird.cs b/Projectiles/Summon/Umbrella/MagicBird.cs index 84ee27ffa4..960cbb188e 100644 --- a/Projectiles/Summon/Umbrella/MagicBird.cs +++ b/Projectiles/Summon/Umbrella/MagicBird.cs @@ -88,18 +88,14 @@ namespace CalamityMod.Projectiles.Summon.Umbrella } if (!hasHomingTarget) { - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (npc is null || !npc.active) - continue; - if (npc.CanBeChasedBy(Projectile, false)) { float dist = (Projectile.Center - npc.Center).Length(); if (dist < maxHomingRange) { - targetIdx = i; + targetIdx = npc.whoAmI; maxHomingRange = dist; hasHomingTarget = true; } diff --git a/Projectiles/Summon/Umbrella/MagicUmbrella.cs b/Projectiles/Summon/Umbrella/MagicUmbrella.cs index 718cd70589..18ec15c00a 100644 --- a/Projectiles/Summon/Umbrella/MagicUmbrella.cs +++ b/Projectiles/Summon/Umbrella/MagicUmbrella.cs @@ -255,16 +255,15 @@ namespace CalamityMod.Projectiles.Summon.Umbrella return selectedTarget.whoAmI; } - for (int i = 0; i < Main.maxNPCs; i++) - { - NPC npc = Main.npc[i]; - if (npc.CanBeChasedBy(this) && (npc.boss || !blackListedTargets.Contains(i))) + foreach (NPC npc in Main.ActiveNPCs) + { + if (npc.CanBeChasedBy(this) && (npc.boss || !blackListedTargets.Contains(npc.whoAmI))) { float npcDist = npc.Distance(center); if (npcDist <= MagicHat.Range && (npcDist <= closestDist || closestDist == -1f)) { closestDist = npcDist; - target = i; + target = npc.whoAmI; } } } diff --git a/Projectiles/Summon/VictideSeaSnail.cs b/Projectiles/Summon/VictideSeaSnail.cs index 235ee2837f..67314786a2 100644 --- a/Projectiles/Summon/VictideSeaSnail.cs +++ b/Projectiles/Summon/VictideSeaSnail.cs @@ -154,9 +154,8 @@ namespace CalamityMod.Projectiles.Summon bool foundTarget = false; float maxDist = 300f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile, false)) { if (Vector2.Distance(Projectile.Center, npc.Center) < maxDist && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, npc.position, npc.width, npc.height)) diff --git a/Projectiles/Summon/VoidConcentrationBlackhole.cs b/Projectiles/Summon/VoidConcentrationBlackhole.cs index 058c45b90f..68a08c994f 100644 --- a/Projectiles/Summon/VoidConcentrationBlackhole.cs +++ b/Projectiles/Summon/VoidConcentrationBlackhole.cs @@ -166,14 +166,14 @@ namespace CalamityMod.Projectiles.Summon } Projectile.frameCounter++; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC n in Main.ActiveNPCs) + { + if (!n.friendly && CalamityGlobalNPC.ShouldAffectNPC(n)) { - if (Main.npc[i].active && !Main.npc[i].friendly && CalamityGlobalNPC.ShouldAffectNPC(Main.npc[i])) - { - if (Vector2.Distance(Projectile.Center, Main.npc[i].Center) <= radius) - ApplySucc(Main.npc[i]); - } + if (Vector2.Distance(Projectile.Center, n.Center) <= radius) + ApplySucc(n); } + } if (Projectile.scale >= 2.5f) //it's boom o' clock diff --git a/Projectiles/Summon/WaterElementalMinion.cs b/Projectiles/Summon/WaterElementalMinion.cs index 1e3faffa33..2681d5dc12 100644 --- a/Projectiles/Summon/WaterElementalMinion.cs +++ b/Projectiles/Summon/WaterElementalMinion.cs @@ -125,14 +125,14 @@ namespace CalamityMod.Projectiles.Summon } if (!canAttack) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float targetX = Main.npc[i].position.X + (float)(Main.npc[i].width / 2); - float targetY = Main.npc[i].position.Y + (float)(Main.npc[i].height / 2); + float targetX = n.position.X + (float)(n.width / 2); + float targetY = n.position.Y + (float)(n.height / 2); float targetDist = Math.Abs(Projectile.position.X + (float)(Projectile.width / 2) - targetX) + Math.Abs(Projectile.position.Y + (float)(Projectile.height / 2) - targetY); - if (targetDist < attackRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height)) + if (targetDist < attackRange && Collision.CanHit(Projectile.position, Projectile.width, Projectile.height, n.position, n.width, n.height)) { attackRange = targetDist; projX = targetX; diff --git a/Projectiles/Summon/WulfrumEnergyBurst.cs b/Projectiles/Summon/WulfrumEnergyBurst.cs index 3a042342ef..e5237425b9 100644 --- a/Projectiles/Summon/WulfrumEnergyBurst.cs +++ b/Projectiles/Summon/WulfrumEnergyBurst.cs @@ -65,10 +65,8 @@ namespace CalamityMod.Projectiles.Summon float bestScore = 0; NPC bestTarget = null; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC potentialTarget in Main.ActiveNPCs) { - NPC potentialTarget = Main.npc[i]; - if (!potentialTarget.CanBeChasedBy(null, false)) continue; diff --git a/Projectiles/Summon/WulfrumFusionBolt.cs b/Projectiles/Summon/WulfrumFusionBolt.cs index 1208dce2db..292eb5a80a 100644 --- a/Projectiles/Summon/WulfrumFusionBolt.cs +++ b/Projectiles/Summon/WulfrumFusionBolt.cs @@ -68,10 +68,8 @@ namespace CalamityMod.Projectiles.Summon float bestScore = 0; NPC bestTarget = null; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC potentialTarget in Main.ActiveNPCs) { - NPC potentialTarget = Main.npc[i]; - if (!potentialTarget.CanBeChasedBy(null, false)) continue; diff --git a/Projectiles/Typeless/CursorProj.cs b/Projectiles/Typeless/CursorProj.cs index 059f41d89a..6cdd482946 100644 --- a/Projectiles/Typeless/CursorProj.cs +++ b/Projectiles/Typeless/CursorProj.cs @@ -57,16 +57,16 @@ namespace CalamityMod.Projectiles.Typeless { int targetID = -1; float hitDistance = 300f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - Vector2 targetCenter = Main.npc[i].Center; + Vector2 targetCenter = n.Center; float targetDist = Vector2.Distance(targetCenter, Projectile.Center); if (targetDist < hitDistance && targetID == -1 && Collision.CanHitLine(Projectile.Center, 1, 1, targetCenter, 1, 1)) { hitDistance = targetDist; - targetID = i; + targetID = n.whoAmI; } } } diff --git a/Projectiles/Typeless/CursorProjSplit.cs b/Projectiles/Typeless/CursorProjSplit.cs index f107778b0b..72409defed 100644 --- a/Projectiles/Typeless/CursorProjSplit.cs +++ b/Projectiles/Typeless/CursorProjSplit.cs @@ -58,16 +58,16 @@ namespace CalamityMod.Projectiles.Typeless { int targetID = -1; float hitDistance = 300f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - Vector2 targetCenter = Main.npc[i].Center; + Vector2 targetCenter = n.Center; float targetDist = Vector2.Distance(targetCenter, Projectile.Center); if (targetDist < hitDistance && targetID == -1 && Collision.CanHitLine(Projectile.Center, 1, 1, targetCenter, 1, 1)) { hitDistance = targetDist; - targetID = i; + targetID = n.whoAmI; } } } diff --git a/Projectiles/Typeless/KelvinCatalystStar.cs b/Projectiles/Typeless/KelvinCatalystStar.cs index 7945edcc91..e4dceba154 100644 --- a/Projectiles/Typeless/KelvinCatalystStar.cs +++ b/Projectiles/Typeless/KelvinCatalystStar.cs @@ -41,15 +41,15 @@ namespace CalamityMod.Projectiles.Typeless float maxDistance = 500f; bool homeIn = false; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - float extraDistance = (float)(Main.npc[i].width / 2) + (float)(Main.npc[i].height / 2); + float extraDistance = (float)(n.width / 2) + (float)(n.height / 2); - if (Vector2.Distance(Main.npc[i].Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, Main.npc[i].Center, 1, 1)) + if (Vector2.Distance(n.Center, Projectile.Center) < (maxDistance + extraDistance) && Collision.CanHit(Projectile.Center, 1, 1, n.Center, 1, 1)) { - center = Main.npc[i].Center; + center = n.Center; homeIn = true; break; } diff --git a/Projectiles/Typeless/LunicBeam.cs b/Projectiles/Typeless/LunicBeam.cs index cdcbcd0404..fd41325c2a 100644 --- a/Projectiles/Typeless/LunicBeam.cs +++ b/Projectiles/Typeless/LunicBeam.cs @@ -46,16 +46,16 @@ namespace CalamityMod.Projectiles.Typeless { int targetID = -1; float hitDistance = 150f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - Vector2 targetCenter = Main.npc[i].Center; + Vector2 targetCenter = n.Center; float targetDist = Vector2.Distance(targetCenter, Projectile.Center); if (targetDist < hitDistance && targetID == -1 && Collision.CanHitLine(Projectile.Center, 1, 1, targetCenter, 1, 1)) { hitDistance = targetDist; - targetID = i; + targetID = n.whoAmI; } } } diff --git a/Projectiles/Typeless/MagnusBeam.cs b/Projectiles/Typeless/MagnusBeam.cs index 5c1004dc87..65c993b872 100644 --- a/Projectiles/Typeless/MagnusBeam.cs +++ b/Projectiles/Typeless/MagnusBeam.cs @@ -37,16 +37,16 @@ namespace CalamityMod.Projectiles.Typeless { int targetIdx = -1; float npcRange = 150f; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].active && Main.npc[i].CanBeChasedBy(Projectile, false)) + if (n.CanBeChasedBy(Projectile, false)) { - Vector2 npcPos = Main.npc[i].Center; + Vector2 npcPos = n.Center; float npcDist = Vector2.Distance(npcPos, Projectile.Center); if (npcDist < npcRange && targetIdx == -1 && Collision.CanHitLine(Projectile.Center, 1, 1, npcPos, 1, 1)) { npcRange = npcDist; - targetIdx = i; + targetIdx = n.whoAmI; } } } diff --git a/Projectiles/Typeless/OmegaBlueTentacle.cs b/Projectiles/Typeless/OmegaBlueTentacle.cs index 43a84d9e1f..3aa52f4ea3 100644 --- a/Projectiles/Typeless/OmegaBlueTentacle.cs +++ b/Projectiles/Typeless/OmegaBlueTentacle.cs @@ -95,16 +95,15 @@ namespace CalamityMod.Projectiles.Typeless Projectile.ai[0] = 10 + Main.rand.Next(10); float maxDistance = hentai ? 900f : 600f; int target = -1; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(Projectile)) { float npcDistance = Projectile.Distance(npc.Center); if (npcDistance < maxDistance) { maxDistance = npcDistance; - target = i; + target = npc.whoAmI; } } } diff --git a/Skies/CryogenSky.cs b/Skies/CryogenSky.cs index 1bbd055d07..cca9964898 100644 --- a/Skies/CryogenSky.cs +++ b/Skies/CryogenSky.cs @@ -101,15 +101,7 @@ namespace CalamityMod.Skies if (CryogenIndex >= 0 && Main.npc[CryogenIndex].active && Main.npc[CryogenIndex].type == cryogenType) return true; - CryogenIndex = -1; - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == cryogenType) - { - CryogenIndex = i; - break; - } - } + CryogenIndex = NPC.FindFirstNPC(cryogenType); return CryogenIndex != -1; } diff --git a/Skies/DrunkCrabScreenShaderData.cs b/Skies/DrunkCrabScreenShaderData.cs index 6eee00117e..2d67cb7706 100644 --- a/Skies/DrunkCrabScreenShaderData.cs +++ b/Skies/DrunkCrabScreenShaderData.cs @@ -47,14 +47,7 @@ namespace CalamityMod.Skies if (!shouldForceDeactivateCirrusShader) { - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == CirrusType) - { - CirrusIndex = i; - break; - } - } + CirrusIndex = NPC.FindFirstNPC(CirrusType); } } else @@ -62,16 +55,7 @@ namespace CalamityMod.Skies if (CrabIndex >= 0 && Main.npc[CrabIndex].active && Main.npc[CrabIndex].type == CrabType) return; - CrabIndex = -1; - - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == CrabType) - { - CrabIndex = i; - break; - } - } + CrabIndex = NPC.FindFirstNPC(CrabType); } } diff --git a/Skies/SCalScreenShaderData.cs b/Skies/SCalScreenShaderData.cs index c8541d92ea..9282bb4878 100644 --- a/Skies/SCalScreenShaderData.cs +++ b/Skies/SCalScreenShaderData.cs @@ -24,15 +24,7 @@ namespace CalamityMod.Skies { return; } - SCalIndex = -1; - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == SCalType) - { - SCalIndex = i; - break; - } - } + SCalIndex = NPC.FindFirstNPC(SCalType); } public override void Update(GameTime gameTime) diff --git a/Skies/SCalSky.cs b/Skies/SCalSky.cs index dd3e8ed6ae..078a00fefc 100644 --- a/Skies/SCalSky.cs +++ b/Skies/SCalSky.cs @@ -196,15 +196,7 @@ namespace CalamityMod.Skies if (SCalIndex >= 0 && Main.npc[SCalIndex].active && Main.npc[SCalIndex].type == SCalType) return true; - SCalIndex = -1; - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == SCalType) - { - SCalIndex = i; - break; - } - } + SCalIndex = NPC.FindFirstNPC(SCalType); return SCalIndex != -1; } diff --git a/Skies/StormWeaverFlashSky.cs b/Skies/StormWeaverFlashSky.cs index 42471c402e..82de86c339 100644 --- a/Skies/StormWeaverFlashSky.cs +++ b/Skies/StormWeaverFlashSky.cs @@ -18,15 +18,7 @@ namespace CalamityMod.Skies if (StormWeaverHeadIndex >= 0 && Main.npc[StormWeaverHeadIndex].active && Main.npc[StormWeaverHeadIndex].type == weaverType) return; - StormWeaverHeadIndex = -1; - for (int i = 0; i < Main.npc.Length; i++) - { - if (Main.npc[i].active && Main.npc[i].type == weaverType) - { - StormWeaverHeadIndex = i; - break; - } - } + StormWeaverHeadIndex = NPC.FindFirstNPC(weaverType); } public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth) diff --git a/TileEntities/TEPlayerFireTurret.cs b/TileEntities/TEPlayerFireTurret.cs index ab3c4b4289..5b7e5eec73 100644 --- a/TileEntities/TEPlayerFireTurret.cs +++ b/TileEntities/TEPlayerFireTurret.cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerIceTurret.cs b/TileEntities/TEPlayerIceTurret.cs index 70c2a4695f..392f479ad8 100644 --- a/TileEntities/TEPlayerIceTurret.cs +++ b/TileEntities/TEPlayerIceTurret.cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerLabTurret.cs b/TileEntities/TEPlayerLabTurret.cs index 3fe6b8dcf0..c6a0f41649 100644 --- a/TileEntities/TEPlayerLabTurret.cs +++ b/TileEntities/TEPlayerLabTurret.cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerLaserTurret .cs b/TileEntities/TEPlayerLaserTurret .cs index 04da3948c7..d59516f463 100644 --- a/TileEntities/TEPlayerLaserTurret .cs +++ b/TileEntities/TEPlayerLaserTurret .cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerOnyxTurret.cs b/TileEntities/TEPlayerOnyxTurret.cs index be4a48643a..89b1cfe8d9 100644 --- a/TileEntities/TEPlayerOnyxTurret.cs +++ b/TileEntities/TEPlayerOnyxTurret.cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerPlagueTurret.cs b/TileEntities/TEPlayerPlagueTurret.cs index 4e32a901c3..fc089f89f5 100644 --- a/TileEntities/TEPlayerPlagueTurret.cs +++ b/TileEntities/TEPlayerPlagueTurret.cs @@ -69,10 +69,9 @@ namespace CalamityMod.TileEntities //This turret can switch targets at any time - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -80,7 +79,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/TileEntities/TEPlayerWaterTurret.cs b/TileEntities/TEPlayerWaterTurret.cs index 25768063d9..c4b2d4b3f8 100644 --- a/TileEntities/TEPlayerWaterTurret.cs +++ b/TileEntities/TEPlayerWaterTurret.cs @@ -88,10 +88,9 @@ namespace CalamityMod.TileEntities Vector2 ret = InvalidTarget; float distSQToBeat = MaxRange * MaxRange; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; - if (!npc.active || npc.friendly || npc.CountsAsACritter) + if (npc.friendly || npc.CountsAsACritter) continue; float distSQ = npc.DistanceSQ(targetingCenter); @@ -99,7 +98,7 @@ namespace CalamityMod.TileEntities { distSQToBeat = distSQ; ret = npc.Center; - indexToSet = i; + indexToSet = npc.whoAmI; } } diff --git a/UI/BossHealthBarManager.cs b/UI/BossHealthBarManager.cs index 6c8057c280..f800dada03 100644 --- a/UI/BossHealthBarManager.cs +++ b/UI/BossHealthBarManager.cs @@ -331,17 +331,17 @@ namespace CalamityMod.UI if (npc.ai[0] == 2f) life = 0L; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - bool isMoonLordPiece = Main.npc[i].type == NPCID.MoonLordHand || Main.npc[i].type == NPCID.MoonLordHead; - if (!Main.npc[i].active || !isMoonLordPiece || Main.npc[i].ai[3] != npc.whoAmI) + bool isMoonLordPiece = n.type == NPCID.MoonLordHand || n.type == NPCID.MoonLordHead; + if (!isMoonLordPiece || n.ai[3] != npc.whoAmI) continue; // Don't count HP towards the total if the NPC is in its dead state. - if (Main.npc[i].Calamity().newAI[0] == 1f) + if (n.Calamity().newAI[0] == 1f) continue; - life += checkingForMaxLife ? Main.npc[i].lifeMax : Main.npc[i].life; + life += checkingForMaxLife ? n.lifeMax : n.life; } return life; @@ -365,15 +365,15 @@ namespace CalamityMod.UI public override void Update(IBigProgressBar currentBar, ref BigProgressBarInfo info) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - // Ignore inactive NPCs and NPCs that should not be given a bar, even if it meets other criteria. - if (!Main.npc[i].active || BossExclusionList.Contains(Main.npc[i].type)) + // Ignore NPCs that should not be given a bar, even if it meets other criteria. + if (BossExclusionList.Contains(n.type)) continue; - bool isEoWSegment = Main.npc[i].type == NPCID.EaterofWorldsBody || Main.npc[i].type == NPCID.EaterofWorldsTail; - if ((Main.npc[i].IsABoss() && !isEoWSegment) || MinibossHPBarList.Contains(Main.npc[i].type) || Main.npc[i].Calamity().CanHaveBossHealthBar) - AttemptToAddBar(i); + bool isEoWSegment = n.type == NPCID.EaterofWorldsBody || n.type == NPCID.EaterofWorldsTail; + if ((n.IsABoss() && !isEoWSegment) || MinibossHPBarList.Contains(n.type) || n.Calamity().CanHaveBossHealthBar) + AttemptToAddBar(n.whoAmI); } for (int i = 0; i < Bars.Count; i++) @@ -467,12 +467,12 @@ namespace CalamityMod.UI return life; // Otherwise, check if any of said relationship NPCs are enraged. - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].active || Main.npc[i].life <= 0 || !OneToMany[NPCType].Contains(Main.npc[i].type)) + if (n.life <= 0 || !OneToMany[NPCType].Contains(n.type)) continue; - life += Main.npc[i].life; + life += n.life; } return life; @@ -499,12 +499,12 @@ namespace CalamityMod.UI return maxLife; // Otherwise, check if any of said relationship NPCs are enraged. - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].active || Main.npc[i].life <= 0 || !OneToMany[NPCType].Contains(Main.npc[i].type)) + if (n.life <= 0 || !OneToMany[NPCType].Contains(n.type)) continue; - maxLife += Main.npc[i].lifeMax; + maxLife += n.lifeMax; } return maxLife; @@ -525,12 +525,12 @@ namespace CalamityMod.UI return false; // Otherwise, check if any of said relationship NPCs are enraged. - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].active || Main.npc[i].life <= 0 || !OneToMany[NPCType].Contains(Main.npc[i].type)) + if (n.life <= 0 || !OneToMany[NPCType].Contains(n.type)) continue; - if (Main.npc[i].Calamity().CurrentlyEnraged) + if (n.Calamity().CurrentlyEnraged) return true; } return false; @@ -551,12 +551,12 @@ namespace CalamityMod.UI return false; // Otherwise, check if any of said relationship NPCs are increasing their defense or DR. - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].active || Main.npc[i].life <= 0 || !OneToMany[NPCType].Contains(Main.npc[i].type)) + if (n.life <= 0 || !OneToMany[NPCType].Contains(n.type)) continue; - if (Main.npc[i].Calamity().CurrentlyIncreasingDefenseOrDR) + if (n.Calamity().CurrentlyIncreasingDefenseOrDR) return true; } return false; diff --git a/UI/BossRushUI.cs b/UI/BossRushUI.cs index 57742fa04f..59fe2b7009 100644 --- a/UI/BossRushUI.cs +++ b/UI/BossRushUI.cs @@ -39,14 +39,12 @@ namespace CalamityMod.UI { int totalLife = 0; int totalLifeMax = 0; - for (int i = 0; i < Main.npc.Length; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!Main.npc[i].active) - continue; - if (types.Contains(Main.npc[i].type)) + if (types.Contains(n.type)) { - totalLife += Main.npc[i].life; - totalLifeMax += Main.npc[i].lifeMax; + totalLife += n.life; + totalLifeMax += n.lifeMax; } } diff --git a/UI/CalamitasEnchantments/EnchantmentManager.cs b/UI/CalamitasEnchantments/EnchantmentManager.cs index 3f76bf1485..bae63dcc0b 100644 --- a/UI/CalamitasEnchantments/EnchantmentManager.cs +++ b/UI/CalamitasEnchantments/EnchantmentManager.cs @@ -282,9 +282,9 @@ namespace CalamityMod.UI.CalamitasEnchants bool orbIsPresent = false; int orbType = ModContent.NPCType(); - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (Main.npc[i].type != orbType || Main.npc[i].target != player.whoAmI || !Main.npc[i].active) + if (n.type != orbType || n.target != player.whoAmI) continue; orbIsPresent = true; diff --git a/UI/VanillaBossBars/ExoMechsBossBar.cs b/UI/VanillaBossBars/ExoMechsBossBar.cs index 1dcca283e8..fe9edf9c4e 100644 --- a/UI/VanillaBossBars/ExoMechsBossBar.cs +++ b/UI/VanillaBossBars/ExoMechsBossBar.cs @@ -96,9 +96,8 @@ namespace CalamityMod.UI.VanillaBossBars public void ValidateAllMechs(ref BigProgressBarInfo info) { - for (int i = 0; i < Main.maxNPCs; i++) - { - NPC target = Main.npc[i]; + foreach (NPC target in Main.ActiveNPCs) + { // Find out first whether or not each of the mechs are in hiding if (target.type == NPCType()) HideAres = target.Opacity < 0.5f; @@ -119,33 +118,29 @@ namespace CalamityMod.UI.VanillaBossBars public bool FindMechsAgain(ref BigProgressBarInfo info) { - for (int i = 0; i < Main.maxNPCs; i++) - { - NPC target = Main.npc[i]; - if (target.active) + foreach (NPC target in Main.ActiveNPCs) + { + // Get the index of the mech that's not hiding + if (target.type == NPCType() && !HideAres) { - // Get the index of the mech that's not hiding - if (target.type == NPCType() && !HideAres) - { - info.npcIndexToAimAt = i; - return true; - } - else if (target.type == NPCType() && !HideArtemis) - { - info.npcIndexToAimAt = i; - return true; - } - else if (target.type == NPCType() && !HideThanatos) - { - info.npcIndexToAimAt = i; - return true; - } - // Failsafe - else if (target.type == NPCType() || target.type == NPCType() || target.type == NPCType()) - { - info.npcIndexToAimAt = i; - return true; - } + info.npcIndexToAimAt = target.whoAmI; + return true; + } + else if (target.type == NPCType() && !HideArtemis) + { + info.npcIndexToAimAt = target.whoAmI; + return true; + } + else if (target.type == NPCType() && !HideThanatos) + { + info.npcIndexToAimAt = target.whoAmI; + return true; + } + // Failsafe + else if (target.type == NPCType() || target.type == NPCType() || target.type == NPCType()) + { + info.npcIndexToAimAt = target.whoAmI; + return true; } } return false; diff --git a/UI/VanillaBossBars/LeviathanAnahitaBossBar.cs b/UI/VanillaBossBars/LeviathanAnahitaBossBar.cs index 1347d038d0..8394d9a395 100644 --- a/UI/VanillaBossBars/LeviathanAnahitaBossBar.cs +++ b/UI/VanillaBossBars/LeviathanAnahitaBossBar.cs @@ -52,11 +52,10 @@ namespace CalamityMod.UI.VanillaBossBars // Determine Anahita's shield health only if she's solo (she can't just block Leviathan out of existence) if (target.type == NPCType() && !NPC.AnyNPCs(NPCType())) - { - for (int i = 0; i < Main.maxNPCs; i++) + { + foreach (NPC part in Main.ActiveNPCs) { - NPC part = Main.npc[i]; - if (part.active && part.type == NPCType()) + if (part.type == NPCType()) { shield += part.life; shieldMax += part.lifeMax; @@ -68,17 +67,16 @@ namespace CalamityMod.UI.VanillaBossBars public bool FindTheRightFish(ref BigProgressBarInfo info) { - for (int i = 0; i < Main.maxNPCs; i++) - { - NPC target = Main.npc[i]; + foreach (NPC target in Main.ActiveNPCs) + { if (target.active && target.type == NPCType()) { - info.npcIndexToAimAt = i; + info.npcIndexToAimAt = target.whoAmI; return true; } else if (target.active && target.type == NPCType()) { - info.npcIndexToAimAt = i; + info.npcIndexToAimAt = target.whoAmI; return true; } } diff --git a/Utilities/DropHelper.cs b/Utilities/DropHelper.cs index 6f356ca6ac..7b78e5db41 100644 --- a/Utilities/DropHelper.cs +++ b/Utilities/DropHelper.cs @@ -212,16 +212,15 @@ namespace CalamityMod int r = wormHead.whoAmI; float minDist = 1E+06f; - for (int i = 0; i < Main.npc.Length; ++i) + foreach (NPC n in Main.ActiveNPCs) { - NPC n = Main.npc[i]; - if (n != null && n.active && idsToCheck.Contains(n.type)) + if (idsToCheck.Contains(n.type)) { float dist = (n.Center - playerPos).Length(); if (dist < minDist) { minDist = dist; - r = i; + r = n.whoAmI; } } } @@ -535,10 +534,9 @@ namespace CalamityMod public static IItemDropRuleCondition AnglerFedToTrasherCondition = If((info) => { bool trasherNearby = false; - for (int i = 0; i < Main.maxNPCs; ++i) + foreach (NPC nearby in Main.ActiveNPCs) { - NPC nearby = Main.npc[i]; - if (nearby is null || !nearby.active || nearby.type != ModContent.NPCType()) + if (nearby.type != ModContent.NPCType()) continue; if (info.npc.Distance(nearby.Center) < TrasherEatDistance) { diff --git a/Utilities/NPCUtils.cs b/Utilities/NPCUtils.cs index 84565bea36..59af3caafa 100644 --- a/Utilities/NPCUtils.cs +++ b/Utilities/NPCUtils.cs @@ -38,9 +38,9 @@ namespace CalamityMod return 0; int count = 0; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC n in Main.ActiveNPCs) { - if (!typesToCheck.Contains(Main.npc[i].type) || !Main.npc[i].active) + if (!typesToCheck.Contains(n.type)) continue; count++; @@ -174,18 +174,14 @@ namespace CalamityMod public static bool AnyBossNPCS(bool checkForMechs = false) { - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - if (Main.npc[i] != null) + if (npc.IsABoss()) { - NPC npc = Main.npc[i]; - if (npc.IsABoss()) - { - // Added due to the new mech boss ore progression, return true if any mech is alive and checkForMechs is true, reduces mech boss projectile damage if true. - if (checkForMechs) - return npc.type == NPCID.TheDestroyer || npc.type == NPCID.SkeletronPrime || npc.type == NPCID.Spazmatism || npc.type == NPCID.Retinazer; - return true; - } + // Added due to the new mech boss ore progression, return true if any mech is alive and checkForMechs is true, reduces mech boss projectile damage if true. + if (checkForMechs) + return npc.type == NPCID.TheDestroyer || npc.type == NPCID.SkeletronPrime || npc.type == NPCID.Spazmatism || npc.type == NPCID.Retinazer; + return true; } } return FindFirstProjectile(ProjectileType()) != -1; diff --git a/Utilities/ProjectileUtils.cs b/Utilities/ProjectileUtils.cs index f68b178eea..528ec0c0fb 100644 --- a/Utilities/ProjectileUtils.cs +++ b/Utilities/ProjectileUtils.cs @@ -233,9 +233,8 @@ namespace CalamityMod int[] array = new int[Main.maxNPCs]; int targetArrayA = 0; int targetArrayB = 0; - for (int i = 0; i < Main.maxNPCs; i++) + foreach (NPC npc in Main.ActiveNPCs) { - NPC npc = Main.npc[i]; if (npc.CanBeChasedBy(projectile, false)) { float enemyDist = Vector2.Distance(projectile.Center, npc.Center); @@ -243,12 +242,12 @@ namespace CalamityMod { if (Collision.CanHit(projectile.position, 1, 1, npc.position, npc.width, npc.height) && enemyDist > 50f) { - array[targetArrayB] = i; + array[targetArrayB] = npc.whoAmI; targetArrayB++; } else if (targetArrayB == 0) { - array[targetArrayA] = i; + array[targetArrayA] = npc.whoAmI; targetArrayA++; } } @@ -293,7 +292,7 @@ namespace CalamityMod { if (targetArrayIndex < maxTargets) { - targetArray[targetArrayIndex] = i; + targetArray[targetArrayIndex] = n.whoAmI; targetArrayIndex++; homeIn = true; }