From 0e094e8234d378bab2d2be9bcea8bb3247b5df64 Mon Sep 17 00:00:00 2001 From: Flowaria Date: Thu, 31 Oct 2024 21:25:23 +0900 Subject: [PATCH] BossTypeDict and EncryptedSchematicIDRelationshipDict --- CalamityLists.cs | 105 ---------------- NPCs/CalamityGlobalNPC.cs | 2 +- .../EncryptedSchematicIDRelationshipDict.cs | 52 ++++++++ Systems/Collections/NPCs/BossTypeDict.cs | 112 ++++++++++++++++++ TileEntities/TECodebreaker.cs | 52 ++++---- UI/DraedonSummoning/CodebreakerUI.cs | 27 +++-- 6 files changed, 206 insertions(+), 144 deletions(-) create mode 100644 Systems/Collections/EncryptedSchematicIDRelationshipDict.cs create mode 100644 Systems/Collections/NPCs/BossTypeDict.cs diff --git a/CalamityLists.cs b/CalamityLists.cs index 37bd1162d4..a93774f121 100644 --- a/CalamityLists.cs +++ b/CalamityLists.cs @@ -2,37 +2,6 @@ using System.Collections.Generic; using CalamityMod.BiomeManagers; using CalamityMod.Enums; -using CalamityMod.Items.DraedonMisc; -using CalamityMod.NPCs.AquaticScourge; -using CalamityMod.NPCs.AstrumAureus; -using CalamityMod.NPCs.AstrumDeus; -using CalamityMod.NPCs.BrimstoneElemental; -using CalamityMod.NPCs.Bumblebirb; -using CalamityMod.NPCs.CalClone; -using CalamityMod.NPCs.CeaselessVoid; -using CalamityMod.NPCs.Crabulon; -using CalamityMod.NPCs.Cryogen; -using CalamityMod.NPCs.DesertScourge; -using CalamityMod.NPCs.DevourerofGods; -using CalamityMod.NPCs.ExoMechs.Apollo; -using CalamityMod.NPCs.ExoMechs.Ares; -using CalamityMod.NPCs.ExoMechs.Artemis; -using CalamityMod.NPCs.ExoMechs.Thanatos; -using CalamityMod.NPCs.HiveMind; -using CalamityMod.NPCs.Leviathan; -using CalamityMod.NPCs.OldDuke; -using CalamityMod.NPCs.Perforator; -using CalamityMod.NPCs.PlaguebringerGoliath; -using CalamityMod.NPCs.Polterghast; -using CalamityMod.NPCs.ProfanedGuardians; -using CalamityMod.NPCs.Providence; -using CalamityMod.NPCs.Ravager; -using CalamityMod.NPCs.Signus; -using CalamityMod.NPCs.SlimeGod; -using CalamityMod.NPCs.StormWeaver; -using CalamityMod.NPCs.SupremeCalamitas; -using CalamityMod.NPCs.Yharon; -using Terraria.ID; using Terraria.ModLoader; using static Terraria.ModLoader.ModContent; @@ -40,10 +9,6 @@ namespace CalamityMod { public sealed class CalamityLists : ModSystem { - public static SortedDictionary bossTypes; - - public static Dictionary EncryptedSchematicIDRelationship; - /// /// Each Sunken Sea subbiome has a correspoding spawn condition boolean value and a biome type. /// @@ -51,71 +16,6 @@ namespace CalamityMod public override void OnModLoad() { - bossTypes = new SortedDictionary() - { - { NPCID.KingSlime, 1 }, - { NPCType(), 2 }, - { NPCID.EyeofCthulhu, 3 }, - { NPCType(), 4 }, - { NPCID.EaterofWorldsHead, 5 }, - { NPCID.EaterofWorldsBody, 5 }, - { NPCID.EaterofWorldsTail, 5 }, - { NPCID.BrainofCthulhu, 6 }, - { NPCType(), 7 }, - { NPCType(), 8 }, - { NPCID.QueenBee, 9 }, - { NPCID.SkeletronHead, 10 }, - { NPCType(), 11 }, - { NPCType(), 11 }, - { NPCType(), 11 }, - { NPCID.WallofFlesh, 12 }, - { NPCType(), 13 }, - { NPCID.Retinazer, 14 }, - { NPCID.Spazmatism, 14 }, - { NPCType(), 15 }, - { NPCID.TheDestroyer, 16 }, - { NPCType(), 17 }, - { NPCID.SkeletronPrime, 18 }, - { NPCType(), 19 }, - { NPCID.Plantera, 20 }, - { NPCType(), 21 }, - { NPCType(), 21 }, - { NPCType(), 22 }, - { NPCID.Golem, 23 }, - { NPCType(), 24 }, - { NPCID.DukeFishron, 25 }, - { NPCType(), 26 }, - { NPCID.CultistBoss, 27 }, - { NPCType(), 28 }, - { NPCID.MoonLordCore, 29 }, - { NPCType(), 30 }, - { NPCType(), 31 }, - { NPCType(), 32 }, - { NPCType(), 33 }, - { NPCType(), 34 }, - { NPCType(), 35 }, - { NPCType(), 36 }, - { NPCType(), 37 }, - { NPCType(), 38 }, - { NPCType(), 39 }, - { NPCType(), 40 }, - { NPCType(), 41 }, - { NPCType(), 41 }, - { NPCType(), 41 }, - { NPCType(), 41 }, - { NPCID.QueenSlimeBoss, 42 }, - { NPCID.HallowBoss, 43 }, - { NPCID.Deerclops, 44 } - }; - - EncryptedSchematicIDRelationship = new Dictionary() - { - [1] = ItemType(), - [2] = ItemType(), - [3] = ItemType(), - [4] = ItemType(), - }; - SunkenSeaBiomeCorrespondentValues = new() { { SunkenSeaBiomeFlags.UndergroundDesert, (spawnInfo => spawnInfo.Player.ZoneDesert, -1 /* None needed. */) }, @@ -129,11 +29,6 @@ namespace CalamityMod public override void Unload() { - bossTypes?.Clear(); - bossTypes = null; - - EncryptedSchematicIDRelationship = null; - SunkenSeaBiomeCorrespondentValues = null; } } diff --git a/NPCs/CalamityGlobalNPC.cs b/NPCs/CalamityGlobalNPC.cs index cd46825be7..936c08ddbe 100644 --- a/NPCs/CalamityGlobalNPC.cs +++ b/NPCs/CalamityGlobalNPC.cs @@ -8637,7 +8637,7 @@ namespace CalamityMod.NPCs { if (!GetDownedBossVariable(npc.type)) { - CalamityLists.bossTypes.TryGetValue(npc.type, out int newBossTypeJustDowned); + BossTypeDict.TryGet(npc.type, out int newBossTypeJustDowned); for (int i = 0; i < Main.maxPlayers; i++) { diff --git a/Systems/Collections/EncryptedSchematicIDRelationshipDict.cs b/Systems/Collections/EncryptedSchematicIDRelationshipDict.cs new file mode 100644 index 0000000000..216b4a70e9 --- /dev/null +++ b/Systems/Collections/EncryptedSchematicIDRelationshipDict.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Linq; +using CalamityMod.Items.DraedonMisc; +using Terraria.ModLoader; +using static Terraria.ModLoader.ModContent; + +namespace CalamityMod.Systems.Collections +{ + public sealed class EncryptedSchematicIDRelationshipDict : ModSystem + { + public static IDictionary Dict { get; private set; } + + public override void OnModLoad() + { + var dict = new Dictionary() + { + [1] = ItemType(), + [2] = ItemType(), + [3] = ItemType(), + [4] = ItemType(), + }; + + Dict = dict; + } + + public override void Unload() + { + Dict?.Clear(); + Dict = null; + } + + public static bool TryGet(int schematicID, out int schematicItemType) + { + return Dict.TryGetValue(schematicID, out schematicItemType); + } + + public static bool TryGetKey(int schematicItemType, out int schematicID) + { + try + { + var pair = Dict.First(pair => pair.Value == schematicItemType); + schematicID = pair.Key; + return true; + } + catch + { + schematicID = 0; + return false; + } + } + } +} diff --git a/Systems/Collections/NPCs/BossTypeDict.cs b/Systems/Collections/NPCs/BossTypeDict.cs new file mode 100644 index 0000000000..20ddc6d87e --- /dev/null +++ b/Systems/Collections/NPCs/BossTypeDict.cs @@ -0,0 +1,112 @@ +using System.Collections.Generic; +using CalamityMod.NPCs.AquaticScourge; +using CalamityMod.NPCs.AstrumAureus; +using CalamityMod.NPCs.AstrumDeus; +using CalamityMod.NPCs.BrimstoneElemental; +using CalamityMod.NPCs.Bumblebirb; +using CalamityMod.NPCs.CalClone; +using CalamityMod.NPCs.CeaselessVoid; +using CalamityMod.NPCs.Crabulon; +using CalamityMod.NPCs.Cryogen; +using CalamityMod.NPCs.DesertScourge; +using CalamityMod.NPCs.DevourerofGods; +using CalamityMod.NPCs.ExoMechs.Apollo; +using CalamityMod.NPCs.ExoMechs.Ares; +using CalamityMod.NPCs.ExoMechs.Artemis; +using CalamityMod.NPCs.ExoMechs.Thanatos; +using CalamityMod.NPCs.HiveMind; +using CalamityMod.NPCs.Leviathan; +using CalamityMod.NPCs.OldDuke; +using CalamityMod.NPCs.Perforator; +using CalamityMod.NPCs.PlaguebringerGoliath; +using CalamityMod.NPCs.Polterghast; +using CalamityMod.NPCs.ProfanedGuardians; +using CalamityMod.NPCs.Providence; +using CalamityMod.NPCs.Ravager; +using CalamityMod.NPCs.Signus; +using CalamityMod.NPCs.SlimeGod; +using CalamityMod.NPCs.StormWeaver; +using CalamityMod.NPCs.SupremeCalamitas; +using CalamityMod.NPCs.Yharon; +using Terraria.ID; +using Terraria.ModLoader; +using static Terraria.ModLoader.ModContent; + +namespace CalamityMod.Systems.Collections +{ + public sealed class BossTypeDict : ModSystem + { + public static IDictionary Dict { get; private set; } + + public override void OnModLoad() + { + Dict = new SortedDictionary() + { + { NPCID.KingSlime, 1 }, + { NPCType(), 2 }, + { NPCID.EyeofCthulhu, 3 }, + { NPCType(), 4 }, + { NPCID.EaterofWorldsHead, 5 }, + { NPCID.EaterofWorldsBody, 5 }, + { NPCID.EaterofWorldsTail, 5 }, + { NPCID.BrainofCthulhu, 6 }, + { NPCType(), 7 }, + { NPCType(), 8 }, + { NPCID.QueenBee, 9 }, + { NPCID.SkeletronHead, 10 }, + { NPCType(), 11 }, + { NPCType(), 11 }, + { NPCType(), 11 }, + { NPCID.WallofFlesh, 12 }, + { NPCType(), 13 }, + { NPCID.Retinazer, 14 }, + { NPCID.Spazmatism, 14 }, + { NPCType(), 15 }, + { NPCID.TheDestroyer, 16 }, + { NPCType(), 17 }, + { NPCID.SkeletronPrime, 18 }, + { NPCType(), 19 }, + { NPCID.Plantera, 20 }, + { NPCType(), 21 }, + { NPCType(), 21 }, + { NPCType(), 22 }, + { NPCID.Golem, 23 }, + { NPCType(), 24 }, + { NPCID.DukeFishron, 25 }, + { NPCType(), 26 }, + { NPCID.CultistBoss, 27 }, + { NPCType(), 28 }, + { NPCID.MoonLordCore, 29 }, + { NPCType(), 30 }, + { NPCType(), 31 }, + { NPCType(), 32 }, + { NPCType(), 33 }, + { NPCType(), 34 }, + { NPCType(), 35 }, + { NPCType(), 36 }, + { NPCType(), 37 }, + { NPCType(), 38 }, + { NPCType(), 39 }, + { NPCType(), 40 }, + { NPCType(), 41 }, + { NPCType(), 41 }, + { NPCType(), 41 }, + { NPCType(), 41 }, + { NPCID.QueenSlimeBoss, 42 }, + { NPCID.HallowBoss, 43 }, + { NPCID.Deerclops, 44 } + }; + } + + public override void Unload() + { + Dict?.Clear(); + Dict = null; + } + + public static bool TryGet(int npcType, out int type) + { + return Dict.TryGetValue(npcType, out type); + } + } +} diff --git a/TileEntities/TECodebreaker.cs b/TileEntities/TECodebreaker.cs index dedeb3ed1b..892883f846 100644 --- a/TileEntities/TECodebreaker.cs +++ b/TileEntities/TECodebreaker.cs @@ -3,6 +3,7 @@ using CalamityMod.CustomRecipes; using CalamityMod.Items.DraedonMisc; using CalamityMod.Items.Materials; using CalamityMod.Packets; +using CalamityMod.Systems.Collections; using CalamityMod.Tiles.DraedonSummoner; using Microsoft.Xna.Framework; using Terraria; @@ -39,17 +40,16 @@ namespace CalamityMod.TileEntities get { // You can't decrypt nothing. - if (HeldSchematicID == 0 || !CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(HeldSchematicID)) + if (HeldSchematicID == 0 || !EncryptedSchematicIDRelationshipDict.TryGet(HeldSchematicID, out var schematicItemType)) return 0; - int schematicType = CalamityLists.EncryptedSchematicIDRelationship[HeldSchematicID]; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return 500; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return 950; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return 1750; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return 5000; return 0; @@ -72,17 +72,16 @@ namespace CalamityMod.TileEntities get { // You can't decrypt nothing. - if (HeldSchematicID == 0 || !CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(HeldSchematicID)) + if (HeldSchematicID == 0 || !EncryptedSchematicIDRelationshipDict.TryGet(HeldSchematicID, out var schematicItemType)) return false; - int schematicType = CalamityLists.EncryptedSchematicIDRelationship[HeldSchematicID]; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return ContainsDecryptionComputer; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return ContainsDecryptionComputer && ContainsSensorArray; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return ContainsDecryptionComputer && ContainsSensorArray && ContainsAdvancedDisplay; - if (schematicType == ModContent.ItemType()) + if (schematicItemType == ModContent.ItemType()) return ContainsDecryptionComputer && ContainsSensorArray && ContainsAdvancedDisplay && ContainsVoltageRegulationSystem; return false; @@ -94,15 +93,14 @@ namespace CalamityMod.TileEntities get { // You can't decrypt nothing. - if (HeldSchematicID == 0 || !CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(HeldSchematicID)) + if (HeldSchematicID == 0 || !EncryptedSchematicIDRelationshipDict.TryGet(HeldSchematicID, out var schematicItemType)) return string.Empty; - int schematicType = CalamityLists.EncryptedSchematicIDRelationship[HeldSchematicID]; - if (schematicType == ModContent.ItemType() - || schematicType == ModContent.ItemType() - || schematicType == ModContent.ItemType() - || schematicType == ModContent.ItemType()) - return CalamityUtils.GetTextValueFromModItem(schematicType, "Content"); + if (schematicItemType == ModContent.ItemType() + || schematicItemType == ModContent.ItemType() + || schematicItemType == ModContent.ItemType() + || schematicItemType == ModContent.ItemType()) + return CalamityUtils.GetTextValueFromModItem(schematicItemType, "Content"); return string.Empty; } @@ -155,8 +153,8 @@ namespace CalamityMod.TileEntities if (ContainsCoolingCell) Item.NewItem(source, x * 16, y * 16, 32, 32, ModContent.ItemType()); - if (CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(HeldSchematicID)) - Item.NewItem(source, x * 16, y * 16, 32, 32, CalamityLists.EncryptedSchematicIDRelationship[HeldSchematicID]); + if (EncryptedSchematicIDRelationshipDict.TryGet(HeldSchematicID, out var schematicItemType)) + Item.NewItem(source, x * 16, y * 16, 32, 32, schematicItemType); while (InputtedCellCount > 0) { @@ -233,27 +231,25 @@ namespace CalamityMod.TileEntities anythingChanged = false; // Do nothing if no valid schematic is inputted at the moment. - if (!CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(HeldSchematicID)) + if (!EncryptedSchematicIDRelationshipDict.TryGet(HeldSchematicID, out var schematicItemType)) return; - int schematicType = CalamityLists.EncryptedSchematicIDRelationship[HeldSchematicID]; - - if (!RecipeUnlockHandler.HasUnlockedT2ArsenalRecipes && schematicType == ModContent.ItemType()) + if (!RecipeUnlockHandler.HasUnlockedT2ArsenalRecipes && schematicItemType == ModContent.ItemType()) { RecipeUnlockHandler.HasUnlockedT2ArsenalRecipes = true; anythingChanged = true; } - if (!RecipeUnlockHandler.HasUnlockedT3ArsenalRecipes && schematicType == ModContent.ItemType()) + if (!RecipeUnlockHandler.HasUnlockedT3ArsenalRecipes && schematicItemType == ModContent.ItemType()) { RecipeUnlockHandler.HasUnlockedT3ArsenalRecipes = true; anythingChanged = true; } - if (!RecipeUnlockHandler.HasUnlockedT4ArsenalRecipes && schematicType == ModContent.ItemType()) + if (!RecipeUnlockHandler.HasUnlockedT4ArsenalRecipes && schematicItemType == ModContent.ItemType()) { RecipeUnlockHandler.HasUnlockedT4ArsenalRecipes = true; anythingChanged = true; } - if (!RecipeUnlockHandler.HasUnlockedT5ArsenalRecipes && schematicType == ModContent.ItemType()) + if (!RecipeUnlockHandler.HasUnlockedT5ArsenalRecipes && schematicItemType == ModContent.ItemType()) { RecipeUnlockHandler.HasUnlockedT5ArsenalRecipes = true; anythingChanged = true; diff --git a/UI/DraedonSummoning/CodebreakerUI.cs b/UI/DraedonSummoning/CodebreakerUI.cs index df1ef0c32d..b50d05d200 100644 --- a/UI/DraedonSummoning/CodebreakerUI.cs +++ b/UI/DraedonSummoning/CodebreakerUI.cs @@ -1,10 +1,10 @@ using System; -using System.Linq; using System.Text; using CalamityMod.Items.DraedonMisc; using CalamityMod.Items.Materials; using CalamityMod.NPCs.ExoMechs; using CalamityMod.Packets; +using CalamityMod.Systems.Collections; using CalamityMod.TileEntities; using CalamityMod.World; using Microsoft.Xna.Framework; @@ -228,7 +228,7 @@ namespace CalamityMod.UI.DraedonSummoning int schematicType = 0; if (codebreakerTileEntity.HeldSchematicID > 0) - schematicType = CalamityLists.EncryptedSchematicIDRelationship[codebreakerTileEntity.HeldSchematicID]; + schematicType = EncryptedSchematicIDRelationshipDict.Dict[codebreakerTileEntity.HeldSchematicID]; if (schematicType == ModContent.ItemType()) schematicIconTexture = ModContent.Request("CalamityMod/Items/DraedonMisc/EncryptedSchematicPlanetoid").Value; @@ -422,9 +422,16 @@ namespace CalamityMod.UI.DraedonSummoning { // If the player's hand item is empty and the codebreaker has a schematic, grab it. // This doesn't work if the Codebreaker is busy decrypting the schematic in question. - if (playerHandItem.IsAir && CalamityLists.EncryptedSchematicIDRelationship.ContainsKey(codebreakerTileEntity.HeldSchematicID) && codebreakerTileEntity.DecryptionCountdown <= 0) + + bool isAir = playerHandItem.IsAir; + bool countdownDone = codebreakerTileEntity.DecryptionCountdown <= 0; + bool hasValidSchematicID = EncryptedSchematicIDRelationshipDict.TryGet(codebreakerTileEntity.HeldSchematicID, out var schematicItemType); + bool hasHandHoldingValidSchematicItem = EncryptedSchematicIDRelationshipDict.TryGetKey(playerHandItem.type, out var handHoldSchematicID); + bool hasMouseHoldingValidSchematicItem = EncryptedSchematicIDRelationshipDict.TryGetKey(Main.mouseItem.type, out var mouseHoldSchematicID); + + if (isAir && countdownDone && hasValidSchematicID) { - playerHandItem.SetDefaults(CalamityLists.EncryptedSchematicIDRelationship[codebreakerTileEntity.HeldSchematicID]); + playerHandItem.SetDefaults(schematicItemType); codebreakerTileEntity.HeldSchematicID = 0; codebreakerTileEntity.DecryptionCountdown = 0; codebreakerTileEntity.SyncContainedStuff(); @@ -434,9 +441,9 @@ namespace CalamityMod.UI.DraedonSummoning } // Otherwise, if the player has an encrypted schematic and the Codebreaker doesn't, insert it into the machine. - else if (CalamityLists.EncryptedSchematicIDRelationship.ContainsValue(playerHandItem.type) && codebreakerTileEntity.HeldSchematicID == 0) + else if (hasHandHoldingValidSchematicItem && hasMouseHoldingValidSchematicItem && codebreakerTileEntity.HeldSchematicID == 0) { - codebreakerTileEntity.HeldSchematicID = CalamityLists.EncryptedSchematicIDRelationship.First(i => i.Value == Main.mouseItem.type).Key; + codebreakerTileEntity.HeldSchematicID = mouseHoldSchematicID; playerHandItem.TurnToAir(); codebreakerTileEntity.SyncContainedStuff(); SoundEngine.PlaySound(SoundID.Grab); @@ -445,15 +452,15 @@ namespace CalamityMod.UI.DraedonSummoning } // Lastly, if the player has an encrypted schematic but so does the Codebreaker, swap the two. - else if (CalamityLists.EncryptedSchematicIDRelationship.ContainsValue(playerHandItem.type) && codebreakerTileEntity.HeldSchematicID != 0) + else if (hasHandHoldingValidSchematicItem && hasValidSchematicID) { - int previouslyHeldSchematic = CalamityLists.EncryptedSchematicIDRelationship[codebreakerTileEntity.HeldSchematicID]; + int previouslyHeldSchematic = schematicItemType; // If the schematics are the same, don't actually do anything, just play the sound as an illusion, to prevent having to send a packet. SoundEngine.PlaySound(SoundID.Grab); - if (playerHandItem.type != previouslyHeldSchematic) + if (playerHandItem.type != previouslyHeldSchematic && hasMouseHoldingValidSchematicItem) { - codebreakerTileEntity.HeldSchematicID = CalamityLists.EncryptedSchematicIDRelationship.First(i => i.Value == Main.mouseItem.type).Key; + codebreakerTileEntity.HeldSchematicID = mouseHoldSchematicID; playerHandItem.SetDefaults(previouslyHeldSchematic); codebreakerTileEntity.SyncContainedStuff(); AwaitingDecryptionTextClose = false;