From e008fcf78e569ca98bf8e53adbf78d5f5e2a80c5 Mon Sep 17 00:00:00 2001 From: GinYuH <72181395+GinYuH@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:55:20 -0700 Subject: [PATCH] made it possible for other mods to access shop alerts --- ModSupport/ModCalls.cs | 53 +++++- NPCs/CalamityGlobalTownNPC.cs | 313 +++++----------------------------- NPCs/TownNPCs/DILF.cs | 1 - NPCs/TownNPCs/FAP.cs | 1 - NPCs/TownNPCs/SEAHOE.cs | 1 - NPCs/TownNPCs/THIEF.cs | 1 - NPCs/TownNPCs/WITCH.cs | 1 - 7 files changed, 93 insertions(+), 278 deletions(-) diff --git a/ModSupport/ModCalls.cs b/ModSupport/ModCalls.cs index 26dbdc982e..d2d544eb14 100644 --- a/ModSupport/ModCalls.cs +++ b/ModSupport/ModCalls.cs @@ -1807,6 +1807,16 @@ namespace CalamityMod } #endregion + #region Town NPC Alert support + public static void RegisterTownNPCShop(int id, Predicate getShop, Action setShop) + { + if (!CalamityGlobalNPC.npcAlertList.Contains((id, getShop, setShop))) + { + CalamityGlobalNPC.npcAlertList.Add((id, getShop, setShop)); + } + } + #endregion + #region Call public static object Call(params object[] args) @@ -2808,12 +2818,43 @@ namespace CalamityMod case "AddDebuffDisplay": case "DisplayDebuff": case "DebuffIcon": - if (args.Length < 2 || args[1] is not Texture2D texture) - return new ArgumentException("ERROR: The first argument to \"RegisterDebuff\" must be the texture of a debuff as a Texture2D"); - if (args.Length != 3 || args[2] is not Predicate debuffCheck) - return new ArgumentException("ERROR: The second argument to \"RegisterDebuff\" Must be a Predicate that checks if an NPC meets the conditions for the debuff."); - RegisterDebuff(texture, debuffCheck); - return null; + { + if (args.Length < 2 || args[1] is not Texture2D texture) + return new ArgumentException("ERROR: The first argument to \"RegisterDebuff\" must be the texture of a debuff as a Texture2D"); + if (args.Length != 3 || args[2] is not Predicate debuffCheck) + return new ArgumentException("ERROR: The second argument to \"RegisterDebuff\" Must be a Predicate that checks if an NPC meets the conditions for the debuff."); + RegisterDebuff(texture, debuffCheck); + return null; + } + + case "RegisterNPCShop": + case "RegisterShop": + case "RegisterTownNPCShop": + case "AddNPCShop": + case "AddShop": + case "AddTownNPCShop": + { + if (args.Length < 2 || args[1] is not int npc) + return new ArgumentException("ERROR: The first argument to \"RegisterTownNPCShop\" must be the id of the NPC"); + if (args.Length < 3 || args[2] is not Predicate shopCheck) + return new ArgumentException("ERROR: The second argument to \"RegisterTownNPCShop\" Must be a Predicate that checks if the new shop variable bool is true or not."); + if (args.Length != 4 || args[3] is not Action shopSet) + return new ArgumentException("ERROR: The third argument to \"RegisterTownNPCShop\" Must be a Action that is able to get and set the player's shop variable bool."); + RegisterTownNPCShop(npc, shopCheck, shopSet); + return null; + } + + case "SetNewShopVariable": + case "SendNPCShopAlert": + case "SendNPCAlert": + { + if (args.Length < 2 || args[1] is not int[] npcs) + return new ArgumentException("ERROR: The first argument to \"SetNewShopVariable\" must be an integer array of npc ids that should be alerted."); + if (args.Length != 3 || args[2] is not bool alreadySet) + return new ArgumentException("ERROR: The third argument to \"SetNewShopVariable\" Must be a bool that determines if the shop alert should show."); + CalamityGlobalNPC.SetNewShopVariable(npcs, alreadySet); + return null; + } default: return new ArgumentException("ERROR: Invalid method name."); diff --git a/NPCs/CalamityGlobalTownNPC.cs b/NPCs/CalamityGlobalTownNPC.cs index 835121d912..af1216139c 100644 --- a/NPCs/CalamityGlobalTownNPC.cs +++ b/NPCs/CalamityGlobalTownNPC.cs @@ -18,6 +18,7 @@ using CalamityMod.NPCs.TownNPCs; using CalamityMod.World; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using System; using System.Collections.Generic; using Terraria; using Terraria.GameContent; @@ -335,13 +336,13 @@ namespace CalamityMod.NPCs // PatreonNames can be null, so can global names, it short circuits in the next step if so int combinedLength = (patreonNames?.Length ?? 0) + (globalNames?.Length ?? 0); int index = Main.rand.Next(numVanillaNames + combinedLength); - + // If the roll isn't low enough, then a "vanilla name" was picked, meaning we change nothing. if (index >= combinedLength) return currentName; - - + + // Change the name to be a randomly selected Patreon name if the roll is low enough. if (index >= globalNames.Length) return patreonNames[index - globalNames.Length]; @@ -622,120 +623,47 @@ namespace CalamityMod.NPCs #endregion #region NPC New Shop Alert + + public static List<(int, Predicate, Action)> npcAlertList = new List<(int, Predicate, Action)>() + { + (NPCID.Merchant, (Player player) => player.Calamity().newMerchantInventory, (Player player, bool enabled) =>{ player.Calamity().newMerchantInventory = enabled; }), + (NPCID.Painter, (Player player) => player.Calamity().newPainterInventory,(Player player, bool enabled) =>{ player.Calamity().newPainterInventory = enabled; }), + (NPCID.Golfer, (Player player) => player.Calamity().newGolferInventory, (Player player, bool enabled) =>{ player.Calamity().newGolferInventory = enabled; }), + (NPCID.BestiaryGirl, (Player player) => player.Calamity().newZoologistInventory,(Player player, bool enabled) =>{ player.Calamity().newZoologistInventory = enabled; }), + (NPCID.DyeTrader, (Player player) => player.Calamity().newDyeTraderInventory, (Player player, bool enabled) =>{ player.Calamity().newDyeTraderInventory = enabled; }), + (NPCID.PartyGirl, (Player player) => player.Calamity().newPartyGirlInventory,(Player player, bool enabled) =>{ player.Calamity().newPartyGirlInventory = enabled; }), + (NPCID.Stylist, (Player player) => player.Calamity().newStylistInventory, (Player player, bool enabled) =>{ player.Calamity().newStylistInventory = enabled; }), + (NPCID.Demolitionist, (Player player) => player.Calamity().newDemolitionistInventory, (Player player, bool enabled) =>{ player.Calamity().newDemolitionistInventory = enabled; }), + (NPCID.Dryad, (Player player) => player.Calamity().newDryadInventory, (Player player, bool enabled) =>{ player.Calamity().newDryadInventory = enabled; }), + (NPCID.DD2Bartender, (Player player) => player.Calamity().newTavernkeepInventory, (Player player, bool enabled) =>{ player.Calamity().newTavernkeepInventory = enabled; }), + (NPCID.ArmsDealer, (Player player) => player.Calamity().newArmsDealerInventory, (Player player, bool enabled) =>{ player.Calamity().newArmsDealerInventory = enabled; }), + (NPCID.GoblinTinkerer, (Player player) => player.Calamity().newGoblinTinkererInventory,(Player player, bool enabled) =>{ player.Calamity().newGoblinTinkererInventory = enabled; }), + (NPCID.WitchDoctor, (Player player) => player.Calamity().newWitchDoctorInventory, (Player player, bool enabled) =>{ player.Calamity().newWitchDoctorInventory = enabled; }), + (NPCID.Clothier, (Player player) => player.Calamity().newClothierInventory, (Player player, bool enabled) =>{ player.Calamity().newClothierInventory = enabled; }), + (NPCID.Mechanic, (Player player) => player.Calamity().newMechanicInventory, (Player player, bool enabled) =>{ player.Calamity().newMechanicInventory = enabled; }), + (NPCID.Pirate, (Player player) => player.Calamity().newPirateInventory, (Player player, bool enabled) =>{ player.Calamity().newPirateInventory = enabled; }), + (NPCID.Truffle, (Player player) => player.Calamity().newTruffleInventory,(Player player, bool enabled) =>{ player.Calamity().newTruffleInventory = enabled; }), + (NPCID.Wizard, (Player player) => player.Calamity().newWizardInventory, (Player player, bool enabled) =>{ player.Calamity().newWizardInventory = enabled; }), + (NPCID.Steampunker, (Player player) => player.Calamity().newSteampunkerInventory, (Player player, bool enabled) =>{ player.Calamity().newSteampunkerInventory = enabled; }), + (NPCID.Cyborg,(Player player) => player.Calamity().newCyborgInventory, (Player player, bool enabled) =>{ player.Calamity().newCyborgInventory = enabled; }), + (NPCID.Princess, (Player player) => player.Calamity().newPrincessInventory,(Player player, bool enabled) =>{ player.Calamity().newPrincessInventory = enabled; }), + (NPCID.SkeletonMerchant, (Player player) => player.Calamity().newSkeletonMerchantInventory, (Player player, bool enabled) =>{ player.Calamity().newSkeletonMerchantInventory = enabled; }), + (NPCType(), (Player player) => player.Calamity().newAmidiasInventory,(Player player, bool enabled) =>{ player.Calamity().newAmidiasInventory = enabled; }), + (NPCType(), (Player player) => player.Calamity().newBanditInventory,(Player player, bool enabled) =>{ player.Calamity().newBanditInventory = enabled; }), + (NPCType(), (Player player) => player.Calamity().newCirrusInventory,(Player player, bool enabled) =>{ player.Calamity().newCirrusInventory = enabled; }), + (NPCType(), (Player player) => player.Calamity().newPermafrostInventory,(Player player, bool enabled) =>{ player.Calamity().newPermafrostInventory = enabled; }), + (NPCType(), (Player player) => player.Calamity().newCalamitasInventory,(Player player, bool enabled) =>{ player.Calamity().newCalamitasInventory = enabled; }) // lol + }; + public void TownNPCAlertSystem(NPC npc, Mod mod, SpriteBatch spriteBatch) { if (CalamityConfig.Instance.ShopNewAlert && npc.townNPC) { - if (npc.type == NPCType() && Main.LocalPlayer.Calamity().newPermafrostInventory) + for (int i = 0; i < npcAlertList.Count; i++) { - DrawNewInventoryAlert(npc); - } - else if (npc.type == NPCType() && Main.LocalPlayer.Calamity().newCirrusInventory) - { - DrawNewInventoryAlert(npc); - } - else if (npc.type == NPCType() && Main.LocalPlayer.Calamity().newAmidiasInventory) - { - DrawNewInventoryAlert(npc); - } - else if (npc.type == NPCType() && Main.LocalPlayer.Calamity().newBanditInventory) - { - DrawNewInventoryAlert(npc); - } - else - { - switch (npc.type) + if (npc.type == npcAlertList[i].Item1 && npcAlertList[i].Item2(Main.LocalPlayer)) { - case NPCID.Merchant: - if (Main.LocalPlayer.Calamity().newMerchantInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Painter: - if (Main.LocalPlayer.Calamity().newPainterInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Golfer: - if (Main.LocalPlayer.Calamity().newGolferInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.BestiaryGirl: - if (Main.LocalPlayer.Calamity().newZoologistInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.DyeTrader: - if (Main.LocalPlayer.Calamity().newDyeTraderInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.PartyGirl: - if (Main.LocalPlayer.Calamity().newPartyGirlInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Stylist: - if (Main.LocalPlayer.Calamity().newStylistInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Demolitionist: - if (Main.LocalPlayer.Calamity().newDemolitionistInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Dryad: - if (Main.LocalPlayer.Calamity().newDryadInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.DD2Bartender: - if (Main.LocalPlayer.Calamity().newTavernkeepInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.ArmsDealer: - if (Main.LocalPlayer.Calamity().newArmsDealerInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.GoblinTinkerer: - if (Main.LocalPlayer.Calamity().newGoblinTinkererInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.WitchDoctor: - if (Main.LocalPlayer.Calamity().newWitchDoctorInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Clothier: - if (Main.LocalPlayer.Calamity().newClothierInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Mechanic: - if (Main.LocalPlayer.Calamity().newMechanicInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Pirate: - if (Main.LocalPlayer.Calamity().newPirateInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Truffle: - if (Main.LocalPlayer.Calamity().newTruffleInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Wizard: - if (Main.LocalPlayer.Calamity().newWizardInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Steampunker: - if (Main.LocalPlayer.Calamity().newSteampunkerInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Cyborg: - if (Main.LocalPlayer.Calamity().newCyborgInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.Princess: - if (Main.LocalPlayer.Calamity().newPrincessInventory) - DrawNewInventoryAlert(npc); - break; - case NPCID.SkeletonMerchant: - if (Main.LocalPlayer.Calamity().newSkeletonMerchantInventory) - DrawNewInventoryAlert(npc); - break; - default: - break; + DrawNewInventoryAlert(npc); } } @@ -771,176 +699,27 @@ namespace CalamityMod.NPCs public override void OnChatButtonClicked(NPC npc, bool firstButton) { - if (npc.townNPC) + for (int i = 0; i < npcAlertList.Count; i++) { - switch (npc.type) + if (npc.type == npcAlertList[i].Item1) { - case NPCID.Merchant: - Main.LocalPlayer.Calamity().newMerchantInventory = false; - break; - case NPCID.Painter: - Main.LocalPlayer.Calamity().newPainterInventory = false; - break; - case NPCID.Golfer: - Main.LocalPlayer.Calamity().newGolferInventory = false; - break; - case NPCID.BestiaryGirl: - Main.LocalPlayer.Calamity().newZoologistInventory = false; - break; - case NPCID.DyeTrader: - Main.LocalPlayer.Calamity().newDyeTraderInventory = false; - break; - case NPCID.PartyGirl: - Main.LocalPlayer.Calamity().newPartyGirlInventory = false; - break; - case NPCID.Stylist: - Main.LocalPlayer.Calamity().newStylistInventory = false; - break; - case NPCID.Demolitionist: - Main.LocalPlayer.Calamity().newDemolitionistInventory = false; - break; - case NPCID.Dryad: - Main.LocalPlayer.Calamity().newDryadInventory = false; - break; - case NPCID.DD2Bartender: - Main.LocalPlayer.Calamity().newTavernkeepInventory = false; - break; - case NPCID.ArmsDealer: - Main.LocalPlayer.Calamity().newArmsDealerInventory = false; - break; - case NPCID.GoblinTinkerer: - Main.LocalPlayer.Calamity().newGoblinTinkererInventory = false; - break; - case NPCID.WitchDoctor: - Main.LocalPlayer.Calamity().newWitchDoctorInventory = false; - break; - case NPCID.Clothier: - Main.LocalPlayer.Calamity().newClothierInventory = false; - break; - case NPCID.Mechanic: - Main.LocalPlayer.Calamity().newMechanicInventory = false; - break; - case NPCID.Pirate: - Main.LocalPlayer.Calamity().newPirateInventory = false; - break; - case NPCID.Truffle: - Main.LocalPlayer.Calamity().newTruffleInventory = false; - break; - case NPCID.Wizard: - Main.LocalPlayer.Calamity().newWizardInventory = false; - break; - case NPCID.Steampunker: - Main.LocalPlayer.Calamity().newSteampunkerInventory = false; - break; - case NPCID.Cyborg: - Main.LocalPlayer.Calamity().newCyborgInventory = false; - break; - case NPCID.Princess: - Main.LocalPlayer.Calamity().newPrincessInventory = false; - break; - case NPCID.SkeletonMerchant: - Main.LocalPlayer.Calamity().newSkeletonMerchantInventory = false; - break; - default: - break; + npcAlertList[i].Item3(Main.LocalPlayer, false); } } } public static void SetNewShopVariable(int[] types, bool alreadySet) { + string npcname = ContentSamples.NpcsByNetId[types[0]].FullName; if (!alreadySet) { for (int i = 0; i < types.Length; i++) { - if (types[i] == NPCType()) + for (int n = 0; n < npcAlertList.Count; n++) { - Main.LocalPlayer.Calamity().newPermafrostInventory = true; - } - else if (types[i] == NPCType()) - { - Main.LocalPlayer.Calamity().newCirrusInventory = true; - } - else if (types[i] == NPCType()) - { - Main.LocalPlayer.Calamity().newAmidiasInventory = true; - } - else if (types[i] == NPCType()) - { - Main.LocalPlayer.Calamity().newBanditInventory = true; - } - else - { - switch (types[i]) + if (types[i] == npcAlertList[n].Item1) { - case NPCID.Merchant: - Main.LocalPlayer.Calamity().newMerchantInventory = true; - break; - case NPCID.Painter: - Main.LocalPlayer.Calamity().newPainterInventory = true; - break; - case NPCID.Golfer: - Main.LocalPlayer.Calamity().newGolferInventory = true; - break; - case NPCID.BestiaryGirl: - Main.LocalPlayer.Calamity().newZoologistInventory = true; - break; - case NPCID.DyeTrader: - Main.LocalPlayer.Calamity().newDyeTraderInventory = true; - break; - case NPCID.PartyGirl: - Main.LocalPlayer.Calamity().newPartyGirlInventory = true; - break; - case NPCID.Stylist: - Main.LocalPlayer.Calamity().newStylistInventory = true; - break; - case NPCID.Demolitionist: - Main.LocalPlayer.Calamity().newDemolitionistInventory = true; - break; - case NPCID.Dryad: - Main.LocalPlayer.Calamity().newDryadInventory = true; - break; - case NPCID.DD2Bartender: - Main.LocalPlayer.Calamity().newTavernkeepInventory = true; - break; - case NPCID.ArmsDealer: - Main.LocalPlayer.Calamity().newArmsDealerInventory = true; - break; - case NPCID.GoblinTinkerer: - Main.LocalPlayer.Calamity().newGoblinTinkererInventory = true; - break; - case NPCID.WitchDoctor: - Main.LocalPlayer.Calamity().newWitchDoctorInventory = true; - break; - case NPCID.Clothier: - Main.LocalPlayer.Calamity().newClothierInventory = true; - break; - case NPCID.Mechanic: - Main.LocalPlayer.Calamity().newMechanicInventory = true; - break; - case NPCID.Pirate: - Main.LocalPlayer.Calamity().newPirateInventory = true; - break; - case NPCID.Truffle: - Main.LocalPlayer.Calamity().newTruffleInventory = true; - break; - case NPCID.Wizard: - Main.LocalPlayer.Calamity().newWizardInventory = true; - break; - case NPCID.Steampunker: - Main.LocalPlayer.Calamity().newSteampunkerInventory = true; - break; - case NPCID.Cyborg: - Main.LocalPlayer.Calamity().newCyborgInventory = true; - break; - case NPCID.Princess: - Main.LocalPlayer.Calamity().newPrincessInventory = true; - break; - case NPCID.SkeletonMerchant: - Main.LocalPlayer.Calamity().newSkeletonMerchantInventory = true; - break; - default: - break; + npcAlertList[n].Item3(Main.LocalPlayer, true); } } } diff --git a/NPCs/TownNPCs/DILF.cs b/NPCs/TownNPCs/DILF.cs index f1b7d475c9..248600cb46 100644 --- a/NPCs/TownNPCs/DILF.cs +++ b/NPCs/TownNPCs/DILF.cs @@ -132,7 +132,6 @@ namespace CalamityMod.NPCs.TownNPCs { if (firstButton) { - Main.LocalPlayer.Calamity().newPermafrostInventory = false; shopName = "Shop"; } } diff --git a/NPCs/TownNPCs/FAP.cs b/NPCs/TownNPCs/FAP.cs index 2f8095a4c1..bb1caa7fa9 100644 --- a/NPCs/TownNPCs/FAP.cs +++ b/NPCs/TownNPCs/FAP.cs @@ -290,7 +290,6 @@ namespace CalamityMod.NPCs.TownNPCs { if (firstButton) { - Main.LocalPlayer.Calamity().newCirrusInventory = false; shopName = "Shop"; } else diff --git a/NPCs/TownNPCs/SEAHOE.cs b/NPCs/TownNPCs/SEAHOE.cs index 12a0425361..08e568cf92 100644 --- a/NPCs/TownNPCs/SEAHOE.cs +++ b/NPCs/TownNPCs/SEAHOE.cs @@ -185,7 +185,6 @@ namespace CalamityMod.NPCs.TownNPCs { if (firstButton) { - Main.LocalPlayer.Calamity().newAmidiasInventory = false; shopName = "Shop"; } else diff --git a/NPCs/TownNPCs/THIEF.cs b/NPCs/TownNPCs/THIEF.cs index ebf707b8c0..e01af8030c 100644 --- a/NPCs/TownNPCs/THIEF.cs +++ b/NPCs/TownNPCs/THIEF.cs @@ -249,7 +249,6 @@ namespace CalamityMod.NPCs.TownNPCs { if (firstButton) { - Main.LocalPlayer.Calamity().newBanditInventory = false; shopName = "Shop"; } else diff --git a/NPCs/TownNPCs/WITCH.cs b/NPCs/TownNPCs/WITCH.cs index 3b5aec0b0e..235e9558f7 100644 --- a/NPCs/TownNPCs/WITCH.cs +++ b/NPCs/TownNPCs/WITCH.cs @@ -135,7 +135,6 @@ namespace CalamityMod.NPCs.TownNPCs if (firstButton) { Main.playerInventory = true; - Main.LocalPlayer.Calamity().newCalamitasInventory = false; CalamitasEnchantUI.NPCIndex = NPC.whoAmI; CalamitasEnchantUI.CurrentlyViewing = true;