diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 2e597b395a..403e6967ff 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -10,6 +10,7 @@ using Terraria.GameContent.Drawing; using Terraria.GameContent.Events; using Terraria.GameContent.ItemDropRules; using Terraria.GameContent.Liquid; +using Terraria.GameContent.UI.Elements; using Terraria.GameContent.UI.States; using Terraria.Graphics.Light; using Terraria.ModLoader; @@ -80,6 +81,7 @@ namespace CalamityMod.ILEditing On_NPC.Collision_DecideFallThroughPlatforms += EnableCalamityBossPlatformCollision; IL_Wiring.HitWireSingle += AddTwinklersToStatue; On_Player.UpdateItemDye += FindCalamityItemDyeShader; + On_AWorldListItem.GetDifficulty += GetDifficultyOverride; On_Item.GetShimmered += ShimmerEffectEdits; // Mana Burn (Chaos Stone) and Chalice of the Blood God diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index 0e7d73a087..1407494672 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -35,13 +35,16 @@ using Terraria.GameContent.Achievements; using Terraria.GameContent.Drawing; using Terraria.GameContent.Events; using Terraria.GameContent.Liquid; +using Terraria.GameContent.UI.Elements; using Terraria.GameInput; using Terraria.Graphics; using Terraria.Graphics.Light; using Terraria.Graphics.Shaders; using Terraria.ID; +using Terraria.IO; using Terraria.Localization; using Terraria.ModLoader; +using Terraria.ModLoader.IO; using Terraria.UI.Gamepad; namespace CalamityMod.ILEditing @@ -1365,6 +1368,35 @@ namespace CalamityMod.ILEditing } #endregion + #region Custom world selection difficulties + internal static void GetDifficultyOverride(Terraria.GameContent.UI.Elements.On_AWorldListItem.orig_GetDifficulty orig, AWorldListItem self, out string expertText, out Color gameModeColor) + { + // Run the original code and pull out the original text and text color + orig(self, out expertText, out gameModeColor); + + string difficultyText = expertText; + Color difficultyColor = gameModeColor; + + // Go through the World Selection Difficulty System's World Difficulty list backwards and choose the latest difficulty that applies + for (int i = WorldSelectionDifficultySystem.WorldDifficulties.Count - 1; i >= 0; i--) + { + WorldSelectionDifficultySystem.WorldDifficulty d = WorldSelectionDifficultySystem.WorldDifficulties[i]; + { + if (d.function(self)) + { + difficultyText = d.name; + difficultyColor = d.color; + break; + } + } + } + + // Set the text and text color + expertText = difficultyText; + gameModeColor = difficultyColor; + } + #endregion + #region Shimmer effect edits public static void ShimmerEffectEdits(Terraria.On_Item.orig_GetShimmered orig, Item self) { diff --git a/Localization/en-US/Mods.CalamityMod.UI.hjson b/Localization/en-US/Mods.CalamityMod.UI.hjson index a9b8439125..b201409388 100644 --- a/Localization/en-US/Mods.CalamityMod.UI.hjson +++ b/Localization/en-US/Mods.CalamityMod.UI.hjson @@ -31,6 +31,7 @@ ChangingTheRules: "[c/919191:You cannot change the rules now!]" NoDifficulty: None NoDifficultyInfo: The classic Terraria experience, with no Calamity difficulty changes Revengeance: Revengeance +Rev: Rev RevengeanceShortInfo: "[c/F54254:The intended Calamity experience!]" RevengeanceExpandedInfo: ''' @@ -55,6 +56,7 @@ DeathExpandedInfo: ''' DeathActivate: Death is active; enjoy the fun. DeathDeactivate: Death is not active; not fun enough for you? +Legend: Legend // Calamity Health Bar ExoTwinsNameNormal: XS-01 Artemis and XS-03 Apollo ExoTwinsNameHekate: Eyes of XB-∞ Hekate diff --git a/ModSupport/ModCalls.cs b/ModSupport/ModCalls.cs index 71d46643ad..88bd4e665a 100644 --- a/ModSupport/ModCalls.cs +++ b/ModSupport/ModCalls.cs @@ -16,6 +16,7 @@ using CalamityMod.World; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; +using Terraria.GameContent.UI.Elements; using Terraria.Localization; using Terraria.ModLoader; @@ -553,6 +554,23 @@ namespace CalamityMod DifficultyModeSystem.Difficulties.Add(newMode); DifficultyModeSystem.CalculateDifficultyData(); } + + public static void AddWorldScreenDifficulty(string name, Func function, Color color, int index = -1) + { + // Construct the difficulty + WorldSelectionDifficultySystem.WorldDifficulty difficulty = new WorldSelectionDifficultySystem.WorldDifficulty(name, function, color); + + // Add the new difficulty + // If no index was provided, just add it to the end of the list, otherwise insert it at the desired spot on the list + if (index == -1) + { + WorldSelectionDifficultySystem.WorldDifficulties.Add(difficulty); + } + else + { + WorldSelectionDifficultySystem.WorldDifficulties.Insert(index, difficulty); + } + } #endregion #region Rogue Class @@ -1162,6 +1180,36 @@ namespace CalamityMod AddCustomDifficulty(mode); return null; + case "AddWorldScreenDifficulty": + case "AddWorldSelectionDifficulty": + { + if (args.Length < 2) + return new ArgumentNullException("ERROR: Must specify a difficulty mode name as a string."); + if (args.Length < 3) + return new ArgumentNullException("ERROR: Must specify a Func."); + if (args.Length < 4) + return new ArgumentNullException("ERROR: Must specify a Color."); + + if (args.Length >= 5) + { + if (!(args[4] is int)) + return new ArgumentException("ERROR: The fourth argument to \"AddWorldScreenDifficulty\" must be an int."); + } + if (!(args[3] is Color color)) + return new ArgumentException("ERROR: The third argument to \"AddWorldScreenDifficulty\" must be a Color."); + if (!(args[2] is Func worldFunction)) + return new ArgumentException("ERROR: The second argument to \"AddWorldScreenDifficulty\" must be a Func."); + if (!(args[1] is string)) + return new ArgumentException("ERROR: The first argument to \"AddWorldScreenDifficulty\" must be a string."); + + if (args.Length >= 5) + AddWorldScreenDifficulty(args[1].ToString(), worldFunction, color, (int)args[4]); + else + AddWorldScreenDifficulty(args[1].ToString(), worldFunction, color); + return null; + } + + case "GetLight": case "GetLightLevel": case "GetLightStrength": diff --git a/Systems/WorldSelectionDifficultySystem.cs b/Systems/WorldSelectionDifficultySystem.cs new file mode 100644 index 0000000000..37f9bb3852 --- /dev/null +++ b/Systems/WorldSelectionDifficultySystem.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using CalamityMod.World; +using Microsoft.Xna.Framework; +using Terraria.GameContent.UI.Elements; +using Terraria.IO; +using Terraria.Localization; +using Terraria.ModLoader; +using Terraria.ModLoader.IO; + +namespace CalamityMod.Systems +{ + public class WorldSelectionDifficultySystem : ModSystem + { + public override void PostSetupContent() + { + // Load all of the World Difficulties to the World Difficulty list + // The difficulty the world uses will be the latest applicable one, so these are added in order from easiest to hardest + + string revenge = CalamityUtils.GetTextValue("UI.Revengeance"); + string death = CalamityUtils.GetTextValue("UI.Death"); + string rev = CalamityUtils.GetTextValue("UI.Rev"); + string master = Language.GetTextValue("UI.Master"); + string legend = CalamityUtils.GetTextValue("UI.Legend"); + string plus = "+"; + + WorldDifficulties.Add(new WorldDifficulty(revenge, GetRevengeance, new(211, 42, 42))); + WorldDifficulties.Add(new WorldDifficulty(death, GetDeath, new(192, 64, 219))); + WorldDifficulties.Add(new WorldDifficulty(rev + plus + master, GetMasterRevengeance, new(124, 153, 242))); + WorldDifficulties.Add(new WorldDifficulty(death + plus + master, GetMasterDeath, new(255, 25, 255))); + WorldDifficulties.Add(new WorldDifficulty(rev + plus + legend, GetLegendRevengeance, new(240, 128, 128))); + WorldDifficulties.Add(new WorldDifficulty(death + plus + legend, GetLegendDeath, new(220, 255, 132))); + } + + public override void SaveWorldHeader(TagCompound tag) + { + // Since CalamityWorld is static, and therefore invalid for TryGetHeaderData, make copies for Rev and Death + tag["RevengeanceMode"] = CalamityWorld.revenge; + tag["DeathMode"] = CalamityWorld.death; + } + + public record WorldDifficulty(string name, Func function, Color color); + + public static List WorldDifficulties = new List(); + public static bool GetRevengeance(AWorldListItem item) + { + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("RevengeanceMode") && tag.GetBool("RevengeanceMode")) + { + return true; + } + } + return false; + } + + public static bool GetDeath(AWorldListItem item) + { + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("DeathMode") && tag.GetBool("DeathMode")) + { + return true; + } + } + return false; + } + + public static bool GetMasterRevengeance(AWorldListItem item) + { + // Grab data from the listed world + FieldInfo worldDataField = typeof(UIWorldListItem).GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance); + WorldFileData worldData = (WorldFileData)worldDataField.GetValue(item); + + int trueGameMode = worldData.GameMode; + if (worldData.ForTheWorthy) + { + trueGameMode++; + } + + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("RevengeanceMode") && tag.GetBool("RevengeanceMode") && trueGameMode == 2) + { + return true; + } + } + return false; + } + + public static bool GetMasterDeath(AWorldListItem item) + { + // Grab data from the listed world + FieldInfo worldDataField = typeof(UIWorldListItem).GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance); + WorldFileData worldData = (WorldFileData)worldDataField.GetValue(item); + + int trueGameMode = worldData.GameMode; + if (worldData.ForTheWorthy) + { + trueGameMode++; + } + + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("DeathMode") && tag.GetBool("DeathMode") && trueGameMode == 2) + { + return true; + } + } + return false; + } + + public static bool GetLegendRevengeance(AWorldListItem item) + { + // Grab data from the listed world + FieldInfo worldDataField = typeof(UIWorldListItem).GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance); + WorldFileData worldData = (WorldFileData)worldDataField.GetValue(item); + + int trueGameMode = worldData.GameMode; + if (worldData.ForTheWorthy) + { + trueGameMode++; + } + + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("RevengeanceMode") && tag.GetBool("RevengeanceMode") && trueGameMode == 3) + { + return true; + } + } + return false; + } + + public static bool GetLegendDeath(AWorldListItem item) + { + // Grab data from the listed world + FieldInfo worldDataField = typeof(UIWorldListItem).GetField("_data", BindingFlags.NonPublic | BindingFlags.Instance); + WorldFileData worldData = (WorldFileData)worldDataField.GetValue(item); + + int trueGameMode = worldData.GameMode; + if (worldData.ForTheWorthy) + { + trueGameMode++; + } + + if (item.Data.TryGetHeaderData(out TagCompound tag)) + { + if (tag.ContainsKey("DeathMode") && tag.GetBool("DeathMode") && trueGameMode == 3) + { + return true; + } + } + return false; + } + } +}