diff --git a/CalamityMod.cs b/CalamityMod.cs index 584629ed88..4747cffa47 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -109,6 +109,10 @@ namespace CalamityMod public override void Load() { Instance = this; + + // Initialize the BossStats struct as early as it is safe to do so + NPCStats.Load(); + heartOriginal2 = Main.heartTexture; heartOriginal = Main.heart2Texture; rainOriginal = Main.rainTexture; @@ -256,18 +260,16 @@ namespace CalamityMod bossVelocityDamageScaleValues = null; CalamityLists.UnloadLists(); + NPCStats.Unload(); thorium = null; fargosMutant = false; - Instance = null; - PopupGUIManager.UnloadGUIs(); InvasionProgressUIManager.UnloadGUIs(); BossRushEvent.Unload(); SchematicLoader.UnloadEverything(); BossHealthBarManager.Unload(); - base.Unload(); TileFraming.Unload(); @@ -289,6 +291,9 @@ namespace CalamityMod rainOriginal = null; manaOriginal = null; carpetOriginal = null; + + Instance = null; + base.Unload(); } #endregion diff --git a/Utilities/NPCStats.cs b/Utilities/NPCStats.cs index 4f734ef78b..28d13662b1 100644 --- a/Utilities/NPCStats.cs +++ b/Utilities/NPCStats.cs @@ -37,11 +37,29 @@ namespace CalamityMod { public static class NPCStats { - // I want to die + private const double ExpertContactVanillaMultiplier = 2D; + private const double NormalProjectileVanillaMultiplier = 2D; + private const double ExpertProjectileVanillaMultiplier = 4D; + + #region Boss Stats Container Struct + internal struct BossStats + { + public static SortedDictionary ExpertDamageMultiplier; + public static SortedDictionary ContactDamageValues; + public static SortedDictionary, int[]> ProjectileDamageValues; + }; + #endregion + + #region Stat Retrieval Methods public static void GetNPCDamage(this NPC npc) { - double damageAdjustment = GetExpertDamageMultiplier(npc) * 2D; - BossStats.ContactDamageValues.TryGetValue(npc.type, out int[] contactDamage); + double damageAdjustment = GetExpertDamageMultiplier(npc) * ExpertContactVanillaMultiplier; + + // Safety check: If for some reason the contact damage array is not initialized yet, set the NPC's damage to 1. + bool exists = BossStats.ContactDamageValues.TryGetValue(npc.type, out int[] contactDamage); + if (!exists) + npc.damage = 1; + int normalDamage = contactDamage[0]; int expertDamage = (int)Math.Round(contactDamage[1] / damageAdjustment); int revengeanceDamage = (int)Math.Round(contactDamage[2] / damageAdjustment); @@ -50,10 +68,17 @@ namespace CalamityMod npc.damage = CalamityWorld.death ? deathDamage : CalamityWorld.revenge ? revengeanceDamage : Main.expertMode ? expertDamage : normalDamage; } + // Gets the amount of damage a given projectile should do from this NPC. + // Automatically compensates for Terraria's internal spaghetti scaling. public static int GetProjectileDamage(this NPC npc, int projType) { - double damageAdjustment = Main.expertMode ? 4D : 2D; - BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npc.type, projType), out int[] projectileDamage); + double damageAdjustment = Main.expertMode ? ExpertProjectileVanillaMultiplier : NormalProjectileVanillaMultiplier; + + // Safety check: If for some reason the projectile damage array is not initialized yet, return 1. + bool exists = BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npc.type, projType), out int[] projectileDamage); + if (!exists) + return 1; + int normalDamage = (int)Math.Round(projectileDamage[0] / damageAdjustment); int expertDamage = (int)Math.Round(projectileDamage[1] / damageAdjustment); int revengeanceDamage = (int)Math.Round(projectileDamage[2] / damageAdjustment); @@ -62,10 +87,17 @@ namespace CalamityMod return CalamityWorld.death ? deathDamage : CalamityWorld.revenge ? revengeanceDamage : Main.expertMode ? expertDamage : normalDamage; } + // Gets the amount of damage this projectile should do from a given NPC. + // Automatically compensates for Terraria's internal spaghetti scaling. public static int GetProjectileDamage(this Projectile projectile, int npcType) { - double damageAdjustment = Main.expertMode ? 4D : 2D; - BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npcType, projectile.type), out int[] projectileDamage); + double damageAdjustment = Main.expertMode ? ExpertProjectileVanillaMultiplier : NormalProjectileVanillaMultiplier; + + // Safety check: If for some reason the projectile damage array is not initialized yet, return 1. + bool exists = BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npcType, projectile.type), out int[] projectileDamage); + if (!exists) + return 1; + int normalDamage = (int)Math.Round(projectileDamage[0] / damageAdjustment); int expertDamage = (int)Math.Round(projectileDamage[1] / damageAdjustment); int revengeanceDamage = (int)Math.Round(projectileDamage[2] / damageAdjustment); @@ -74,10 +106,16 @@ namespace CalamityMod return CalamityWorld.death ? deathDamage : CalamityWorld.revenge ? revengeanceDamage : Main.expertMode ? expertDamage : normalDamage; } + // Gets the raw amount of damage a projectile should do from this NPC. + // That is, this doesn't adjust the value to compensate for Terraria's internal spaghetti scaling. public static int GetProjectileDamageNoScaling(this NPC npc, int projType) { - BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npc.type, projType), out int[] projectileDamage); - return CalamityWorld.death ? projectileDamage[3] : CalamityWorld.revenge ? projectileDamage[2] : Main.expertMode ? projectileDamage[1] : projectileDamage[0]; + bool exists = BossStats.ProjectileDamageValues.TryGetValue(new Tuple(npc.type, projType), out int[] projectileDamage); + return !exists ? 1 // Base case for safety, in case the array is not initialized yet. + : CalamityWorld.death ? projectileDamage[3] + : CalamityWorld.revenge ? projectileDamage[2] + : Main.expertMode ? projectileDamage[1] + : projectileDamage[0]; } /// @@ -89,16 +127,17 @@ namespace CalamityMod /// public static double GetExpertDamageMultiplier(this NPC npc, bool? master = null) { - if (!BossStats.ExpertDamageMultiplier.ContainsKey(npc.type)) - return 1D; - - BossStats.ExpertDamageMultiplier.TryGetValue(npc.type, out double damageMult); - return damageMult; + bool exists = BossStats.ExpertDamageMultiplier.TryGetValue(npc.type, out double damageMult); + return exists ? damageMult : 1D; } + #endregion - public struct BossStats + #region Load/Unload + // A static function, called exactly once, which initializes the BossStats struct at a predictable time. + // This is necessary to ensure this dictionary is populated as early as possible. + internal static void Load() { - public static SortedDictionary ExpertDamageMultiplier = new SortedDictionary + BossStats.ExpertDamageMultiplier = new SortedDictionary { { NPCID.KingSlime, 0.8 }, @@ -215,8 +254,7 @@ namespace CalamityMod { ModContent.NPCType(), 0.8 } }; - - public static SortedDictionary ContactDamageValues = new SortedDictionary + BossStats.ContactDamageValues = new SortedDictionary { { NPCID.KingSlime, new int[] { 40, 64, 80, 88, 96 } }, @@ -470,8 +508,7 @@ namespace CalamityMod { ModContent.NPCType(), new int[] { 350, 560, 592, 608, 768 } } }; - - public static SortedDictionary, int[]> ProjectileDamageValues = new SortedDictionary, int[]> + BossStats.ProjectileDamageValues = new SortedDictionary, int[]> { { new Tuple(ModContent.NPCType(), ModContent.ProjectileType()), new int[] { 0, 0, 44, 48, 66 } }, @@ -724,6 +761,15 @@ namespace CalamityMod { new Tuple(ModContent.NPCType(), ModContent.ProjectileType()), new int[] { 400, 600, 632, 648, 948 } }, { new Tuple(ModContent.NPCType(), ModContent.ProjectileType()), new int[] { 350, 528, 556, 572, 834 } } }; - }; - } + } + + // Destroys the BossStats struct to save memory because mod assemblies will not be fully unloaded until TML 1.4. + internal static void Unload() + { + BossStats.ExpertDamageMultiplier = null; + BossStats.ContactDamageValues = null; + BossStats.ProjectileDamageValues = null; + } + #endregion + } }