diff --git a/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs b/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs index 9fdab1f84a..b8902c380b 100644 --- a/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs @@ -68,16 +68,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers if (ExcludedNPCs.Contains(npc.type) || npc.IsABestiaryIconDummy) return false; - // Safety check for weird MP bug when getting global npcs. - if (!npc.TryGetGlobalNPC(out var calNPC) || !npc.TryGetGlobalNPC(out var polNPC)) - return false; - - // Do not draw if the npc does not have miracle blight, or has the polarity effect. - if (calNPC.miracleBlight <= 0 || polNPC.CurPolarity > 0f) - return false; - - // Do not draw if the current player has the trippy effect. - if (Main.LocalPlayer.Calamity().trippy) + if (!CalamityDrawParameterNPC.DrawingMiracleBlight[npc.whoAmI]) return false; return true; diff --git a/NPCs/CalamityDrawParameterNPC.cs b/NPCs/CalamityDrawParameterNPC.cs new file mode 100644 index 0000000000..f128694f22 --- /dev/null +++ b/NPCs/CalamityDrawParameterNPC.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Steamworks; +using Terraria; +using Terraria.DataStructures; +using Terraria.ModLoader; +namespace CalamityMod.NPCs +{ + // Why this class is exist? + // It because we want to avoid Threading Issues while We using NPC's properties on Draw Thread + public sealed class CalamityDrawParameterNPC : GlobalNPC + { + public override bool InstancePerEntity => false; + + #region Draw Parameters + public static bool[] DrawingMiracleBlight { get; private set; } + public static bool[] DrawingPolarity { get; private set; } + #endregion + + public override void Load() + { + DrawingMiracleBlight = new bool[Main.maxNPCs]; + DrawingPolarity = new bool[Main.maxNPCs]; + } + + public override void Unload() + { + DrawingMiracleBlight = null; + DrawingPolarity = null; + } + + public override void SetDefaults(NPC entity) => ResetParameters(entity); + + public override bool PreAI(NPC npc) + { + if (npc is null) + return true; + var whoAmI = npc.whoAmI; + DrawingMiracleBlight[whoAmI] = ShouldDrawMiracleBlight(npc); + DrawingPolarity[whoAmI] = ShouldDrawPolarity(npc); + return true; + } + + private static void ResetParameters(NPC npc) + { + if (npc is null) + return; + var whoAmI = npc.whoAmI; + DrawingMiracleBlight[whoAmI] = false; + DrawingPolarity[whoAmI] = false; + } + + private static bool ShouldDrawMiracleBlight(NPC npc) + { + if (npc is null || !npc.active) + return false; + // Safety check for weird MP bug when getting global npcs. + if (!npc.TryGetGlobalNPCSafer(out var calNPC) || !npc.TryGetGlobalNPCSafer(out var polNPC)) + return false; + // Do not draw if the npc does not have miracle blight, or has the polarity effect. + if (calNPC.miracleBlight <= 0 || polNPC.CurPolarity > 0f) + return false; + // Do not draw if the current player has the trippy effect. + if (Main.LocalPlayer.Calamity().trippy) + return false; + return true; + } + + private static bool ShouldDrawPolarity(NPC npc) + { + if (npc is null || !npc.active) + return false; + // Safety check for weird MP bug when getting global npcs. + if (!npc.TryGetGlobalNPCSafer(out var calNPC) || !npc.TryGetGlobalNPCSafer(out var polNPC)) + return false; + // I don't know who would be using this while also inflicting miracle blight, but in that rare case, do not draw these. + if (calNPC.miracleBlight > 0) + return false; + // Do not draw if the npc doesn't have the polarity effect. + if (polNPC.CurPolarity <= 0f) + return false; + return true; + } + } +} diff --git a/NPCs/CalamityPolarityNPC.cs b/NPCs/CalamityPolarityNPC.cs index b414c40154..62389ac49f 100644 --- a/NPCs/CalamityPolarityNPC.cs +++ b/NPCs/CalamityPolarityNPC.cs @@ -96,7 +96,7 @@ namespace CalamityMod.NPCs public override void PostDraw(NPC npc, SpriteBatch spriteBatch, Vector2 screenPos, Color drawColor) { // I don't know who would be using this while also inflicting miracle blight, but in that rare case, do not draw these. - if (npc.Calamity().miracleBlight > 0) + if (CalamityDrawParameterNPC.DrawingMiracleBlight[npc.whoAmI]) return; Main.spriteBatch.End(); diff --git a/Utilities/GlobalEntityUtils.cs b/Utilities/GlobalEntityUtils.cs new file mode 100644 index 0000000000..ff5a022bfc --- /dev/null +++ b/Utilities/GlobalEntityUtils.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terraria.ModLoader; +using Terraria; + +namespace CalamityMod +{ + public static partial class CalamityUtils + { + /// + /// Actual "Thread-Safe" version of TryGetGlobalNPC + /// + /// GlobalNPC type to get + /// Input NPC + /// GlobalNPC output + /// true If we successfully get GlobalNPC. otherwise false + public static bool TryGetGlobalNPCSafer(this NPC npc, out T globalNPC) where T : GlobalNPC + { + try + { + if (npc is null) + { + globalNPC = null; + return false; + } + + int slot = ModContent.GetInstance()?.PerEntityIndex ?? -1; + int length = npc.EntityGlobals.Length; + if (slot < 0 || slot >= length) + { + globalNPC = null; + return false; + } + npc.TryGetGlobalNPC(out globalNPC); + return globalNPC != null; + } + catch + { + globalNPC = null; + return false; + } + } + } +} diff --git a/build.txt b/build.txt index cf9028fcf7..ed16d2e562 100644 --- a/build.txt +++ b/build.txt @@ -1,5 +1,5 @@ author = Fabsol and the Dev Team -version = 2.0.4.003 +version = 2.0.4.004 displayName = Calamity Mod modReferences = CalamityModMusic homepage = https://discord.gg/calamity