From 5a506556b6aeade7b4143b20deae9fe89b99af81 Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 16:40:39 -0700 Subject: [PATCH 1/6] Implement IVisibleThroughWater interface and associated helper class. --- Walls/IVisibleThroughWater.cs | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Walls/IVisibleThroughWater.cs diff --git a/Walls/IVisibleThroughWater.cs b/Walls/IVisibleThroughWater.cs new file mode 100644 index 0000000000..1514e9137f --- /dev/null +++ b/Walls/IVisibleThroughWater.cs @@ -0,0 +1,51 @@ +using System; +using System.Diagnostics; +using Microsoft.Xna.Framework; +using Terraria.Localization; +using Terraria.Map; +using Terraria.ModLoader; + +namespace CalamityMod.Walls +{ + /// + /// Walls that implement this interface may be revealed on the map through + /// water. This allows them to show on the map despite always being + /// submerged. + /// + public interface IVisibleThroughWater : ILoadable + { + int WaterMapEntry { get; set; } + } + + internal static class VisibleThroughWater + { + public const float WaterTransparency = 0.5f; + public static readonly Color WaterColor = new Color(9, 61, 191); + + public static void AddEntries(this IVisibleThroughWater visibleThroughWater, Color baseColor, LocalizedText text = null) + { + AssertIsWall(visibleThroughWater, out var wall); + wall.AddMapEntry(baseColor, text); + wall.AddMapEntry(Color.Lerp(baseColor, WaterColor, WaterTransparency), text); + } + + public static void InitializeWaterMapEntryLookups() + { + foreach (var visibleThroughWater in CalamityMod.Instance.GetContent()) + { + AssertIsWall(visibleThroughWater, out var wall); + visibleThroughWater.WaterMapEntry = MapHelper.wallLookup[wall.Type] + 1; + } + } + + [DebuggerStepThrough] + [StackTraceHidden] + private static void AssertIsWall(IVisibleThroughWater visibleThroughWater, out ModWall wall) + { + if (visibleThroughWater is not ModWall theWall) + throw new InvalidCastException($"{nameof(IVisibleThroughWater)} is implemented on type {visibleThroughWater.GetType().FullName}; said type should inherit from {nameof(ModWall)}"); + + wall = theWall; + } + } +} From 195eb51b2dd484ed76e0802fb03445022f6930bc Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 16:54:52 -0700 Subject: [PATCH 2/6] Associated IL edit for showing the waterlogged wall map entry. --- ILEditing/ILChangesLoading.cs | 4 ++ ILEditing/VanillaStupidityFixingILChanges.cs | 59 ++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 04892f9102..2c8fb20700 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -13,6 +13,7 @@ using Terraria.GameContent.Liquid; using Terraria.GameContent.UI.Elements; using Terraria.GameContent.UI.States; using Terraria.Graphics.Light; +using Terraria.Map; using Terraria.ModLoader; namespace CalamityMod.ILEditing @@ -160,6 +161,9 @@ namespace CalamityMod.ILEditing // Fix vanilla not accounting for multiple bobbers when fishing with truffle worm IL_Player.ItemCheck_CheckFishingBobbers += FixTruffleWormFishing; + + // Allow specified walls to be visible through water on the map + IL_MapHelper.CreateMapTile += UseVisibleThroughWaterMapTile; //Additional detours that are in their own item files given they are only relevant to these specific items: //Rover drive detours on Player.DrawInfernoRings to draw its shield diff --git a/ILEditing/VanillaStupidityFixingILChanges.cs b/ILEditing/VanillaStupidityFixingILChanges.cs index 379e93326d..11e06f825f 100644 --- a/ILEditing/VanillaStupidityFixingILChanges.cs +++ b/ILEditing/VanillaStupidityFixingILChanges.cs @@ -7,12 +7,14 @@ using CalamityMod.Items.Materials; using CalamityMod.Items.TreasureBags.MiscGrabBags; using CalamityMod.NPCs.AcidRain; using CalamityMod.NPCs.NormalNPCs; +using CalamityMod.Walls; using Microsoft.Xna.Framework; using Mono.Cecil.Cil; using MonoMod.Cil; using Terraria; using Terraria.DataStructures; using Terraria.ID; +using Terraria.Map; using Terraria.ModLoader; namespace CalamityMod.ILEditing @@ -898,5 +900,62 @@ namespace CalamityMod.ILEditing } } #endregion + + #region Render Special Map Colors + private static void UseVisibleThroughWaterMapTile(ILContext il) + { + var c = new ILCursor(il); + + if (!c.TryGotoNext(x => x.MatchCall("get_Item"))) + { + LogFailure("Use VisibleThroughWater Map Tile", "Could not locate call to Terraria.Map.TileMap::get_Item."); + return; + } + + int tileIndex = -1; + if (!c.TryGotoNext(x => x.MatchStloc(out tileIndex)) || tileIndex == -1) + { + LogFailure("Use VisibleThroughWater Map Tile", "Could not determine the local variable index tile is pushed to."); + return; + } + + if (!c.TryGotoNext(x => x.MatchCall("liquidType"))) + { + LogFailure("Use VisibleThroughWater Map Tile", "Could not locate call to Terraria.Tile::liquidType."); + return; + } + + int liquidTypeIndex = -1; + if (!c.TryGotoNext(x => x.MatchStloc(out liquidTypeIndex)) || liquidTypeIndex == -1) + { + LogFailure("Use VisibleThroughWater Map Tile", "Could not determine the local variable index liquidType is pushed to."); + return; + } + + int relativeMapTypeIndex = -1; + if (!c.TryGotoNext(MoveType.After, x => x.MatchStloc(out relativeMapTypeIndex)) || relativeMapTypeIndex == -1) + { + LogFailure("Use VisibleThroughWater Map Tile", "Could not determine the local variable index of the relative map type."); + return; + } + + c.Emit(OpCodes.Ldloc_0); + c.Emit(OpCodes.Ldloc, relativeMapTypeIndex); + c.Emit(OpCodes.Ldloc, liquidTypeIndex); + c.EmitDelegate( + (Tile tile, int relativeMapType, int liquidType) => + { + if (liquidType != LiquidID.Water) + return relativeMapType; + + if (WallLoader.GetWall(tile.WallType) is IVisibleThroughWater visibleThroughWater) + return visibleThroughWater.WaterMapEntry; + + return relativeMapType; + } + ); + c.Emit(OpCodes.Stloc, relativeMapTypeIndex); + } + #endregion } } From def3fe933b9dc21784034c5287cf06e3a06dc6f1 Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 16:57:25 -0700 Subject: [PATCH 3/6] Apply IVisibleThroughWater to all existing walls that it was applied to on the branch. --- Walls/IVisibleThroughWater.cs | 2 +- Walls/NavystoneWall.cs | 6 ++++-- Walls/NavystoneWallSafe.cs | 6 ++++-- Walls/SmoothNavystoneWall.cs | 6 ++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Walls/IVisibleThroughWater.cs b/Walls/IVisibleThroughWater.cs index 1514e9137f..6beb43fcb2 100644 --- a/Walls/IVisibleThroughWater.cs +++ b/Walls/IVisibleThroughWater.cs @@ -22,7 +22,7 @@ namespace CalamityMod.Walls public const float WaterTransparency = 0.5f; public static readonly Color WaterColor = new Color(9, 61, 191); - public static void AddEntries(this IVisibleThroughWater visibleThroughWater, Color baseColor, LocalizedText text = null) + public static void AddMapEntryWithWaterVisibility(this IVisibleThroughWater visibleThroughWater, Color baseColor, LocalizedText text = null) { AssertIsWall(visibleThroughWater, out var wall); wall.AddMapEntry(baseColor, text); diff --git a/Walls/NavystoneWall.cs b/Walls/NavystoneWall.cs index 80c3b49708..500df762c1 100644 --- a/Walls/NavystoneWall.cs +++ b/Walls/NavystoneWall.cs @@ -2,12 +2,14 @@ using Terraria.ModLoader; namespace CalamityMod.Walls { - public class NavystoneWall : ModWall + public class NavystoneWall : ModWall, IVisibleThroughWater { + int IVisibleThroughWater.WaterMapEntry { get; set; } + public override void SetStaticDefaults() { DustType = 96; - AddMapEntry(new Color(16, 45, 48)); + this.AddMapEntryWithWaterVisibility(new Color(16, 45, 48)); } public override void NumDust(int i, int j, bool fail, ref int num) => num = fail ? 1 : 3; diff --git a/Walls/NavystoneWallSafe.cs b/Walls/NavystoneWallSafe.cs index 4690d5bddf..ef276c3a45 100644 --- a/Walls/NavystoneWallSafe.cs +++ b/Walls/NavystoneWallSafe.cs @@ -4,13 +4,15 @@ using Terraria.ModLoader; namespace CalamityMod.Walls { - public class NavystoneWallSafe : ModWall + public class NavystoneWallSafe : ModWall, IVisibleThroughWater { + int IVisibleThroughWater.WaterMapEntry { get; set; } + public override void SetStaticDefaults() { Main.wallHouse[Type] = true; DustType = 96; - AddMapEntry(new Color(0, 50, 50)); + this.AddMapEntryWithWaterVisibility(new Color(0, 50, 50)); } public override void NumDust(int i, int j, bool fail, ref int num) => num = fail ? 1 : 3; diff --git a/Walls/SmoothNavystoneWall.cs b/Walls/SmoothNavystoneWall.cs index efffbffede..09dcbd2e6d 100644 --- a/Walls/SmoothNavystoneWall.cs +++ b/Walls/SmoothNavystoneWall.cs @@ -5,13 +5,15 @@ using Terraria.ModLoader; namespace CalamityMod.Walls { - public class SmoothNavystoneWall : ModWall + public class SmoothNavystoneWall : ModWall, IVisibleThroughWater { + int IVisibleThroughWater.WaterMapEntry { get; set; } + public override void SetStaticDefaults() { Main.wallHouse[Type] = true; - AddMapEntry(new Color(27, 35, 36)); + this.AddMapEntryWithWaterVisibility(new Color(27, 35, 36)); } public override bool CreateDust(int i, int j, ref int type) From b2974a7735f191ca9637c11bcd298f9745bedc6b Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 17:06:07 -0700 Subject: [PATCH 4/6] Actually call InitializeWateRMapEntryLookups. --- Walls/IVisibleThroughWater.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Walls/IVisibleThroughWater.cs b/Walls/IVisibleThroughWater.cs index 6beb43fcb2..189038f712 100644 --- a/Walls/IVisibleThroughWater.cs +++ b/Walls/IVisibleThroughWater.cs @@ -19,6 +19,14 @@ namespace CalamityMod.Walls internal static class VisibleThroughWater { + private sealed class VisibleThroughWaterSystem : ModSystem + { + public override void AddRecipes() + { + InitializeWaterMapEntryLookups(); + } + } + public const float WaterTransparency = 0.5f; public static readonly Color WaterColor = new Color(9, 61, 191); From 5884ec3c3f7eace6928a2bccf53776f1997d1d60 Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 17:06:36 -0700 Subject: [PATCH 5/6] Prefer PostSetupContent. --- Walls/IVisibleThroughWater.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Walls/IVisibleThroughWater.cs b/Walls/IVisibleThroughWater.cs index 189038f712..4fb76f80ca 100644 --- a/Walls/IVisibleThroughWater.cs +++ b/Walls/IVisibleThroughWater.cs @@ -21,7 +21,7 @@ namespace CalamityMod.Walls { private sealed class VisibleThroughWaterSystem : ModSystem { - public override void AddRecipes() + public override void PostSetupContent() { InitializeWaterMapEntryLookups(); } From 92c2c1044e86ed52d39140f0eaafac514c60a8f7 Mon Sep 17 00:00:00 2001 From: tomat Date: Sat, 11 May 2024 17:10:57 -0700 Subject: [PATCH 6/6] Revert "Prefer PostSetupContent." This reverts commit 5884ec3c3f7eace6928a2bccf53776f1997d1d60. --- Walls/IVisibleThroughWater.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Walls/IVisibleThroughWater.cs b/Walls/IVisibleThroughWater.cs index 4fb76f80ca..189038f712 100644 --- a/Walls/IVisibleThroughWater.cs +++ b/Walls/IVisibleThroughWater.cs @@ -21,7 +21,7 @@ namespace CalamityMod.Walls { private sealed class VisibleThroughWaterSystem : ModSystem { - public override void PostSetupContent() + public override void AddRecipes() { InitializeWaterMapEntryLookups(); }