diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 2c264115df..6213bc5ad5 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -9,12 +9,10 @@ using Terraria.GameContent.Biomes; 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.GameInput; using Terraria.Graphics.CameraModifiers; -using Terraria.Graphics.Light; using Terraria.Map; using Terraria.ModLoader; @@ -102,45 +100,6 @@ namespace CalamityMod.ILEditing On_Player.CheckMana_int_bool_bool += AllowNegativeCheckMana; On_Player.CheckMana_Item_int_bool_bool += AllowNegativeCheckMana; - //LavaStyles - if (ExternalMods.biomeLava == null) - { - //Rendering/Drawing - IL_Main.DoDraw += DoDrawLavas; - IL_Main.RenderWater += RenderLavas; - IL_Main.RenderBackground += RenderLavaBackgrounds; - IL_Main.DrawCapture += DrawLavatoCapture; - IL_TileDrawing.Draw += AddTileLiquidDrawing; - - //Blocking - IL_LiquidRenderer.DrawNormalLiquids += BlockLavaDrawing; - On_TileDrawing.DrawTile_LiquidBehindTile += BlockLavaDrawingForSlopes; - On_TileDrawing.DrawPartialLiquid += BlockLavaDrawingForSlopes2; - On_WaterfallManager.DrawWaterfall_int_int_int_float_Vector2_Rectangle_Color_SpriteEffects += LavafallRemover; - IL_Main.oldDrawWater += BlockRetroLightingLava; - - //Replacing - IL_LiquidRenderer.InternalPrepareDraw += LavaBubbleReplacer; - IL_TileDrawing.EmitLiquidDrops += LavaDropletReplacer; - IL_NPC.Collision_WaterCollision += SplashEntityLava; - IL_Projectile.Update += SplashEntityLava; - IL_Item.MoveInWorld += SplashEntityLava; - IL_Player.Update += SplashEntityLava; - IL_Player.Update += PlayerDebuffEdit; - - //Other - On_WaterfallManager.Draw += LavaFallRedrawer; - On_WaterfallManager.StylizeColor += WaterfallGlowmaskEditor; - - //Waterfall light - On_WaterfallManager.AddLight += LavafallLightEditor; - } - - // Liquid Lighting and alpha (Liquid Viusuals) - IL_TileLightScanner.GetTileLight += ApplyLiquidEmit; - IL_LiquidRenderer.DrawNormalLiquids += LiquidDrawColors; //Liquid Light - IL_TileDrawing.DrawTile_LiquidBehindTile += LiquidSlopeDrawColors; - // Custom grappling On_Player.GrappleMovement += CustomGrappleMovementCheck; On_Player.UpdatePettingAnimal += CustomGrapplePreDefaultMovement; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index a714b31754..c3dee41605 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Reflection; using CalamityMod.Balancing; using CalamityMod.CalPlayer; using CalamityMod.Cooldowns; @@ -38,18 +37,14 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Mono.Cecil.Cil; using MonoMod.Cil; -using ReLogic.Content; using Terraria; using Terraria.Audio; using Terraria.DataStructures; using Terraria.GameContent; 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.Capture; using Terraria.Graphics.Light; using Terraria.Graphics.Shaders; using Terraria.ID; @@ -973,647 +968,6 @@ namespace CalamityMod.ILEditing } #endregion - #region Lava Style Edits - //Nine Layers of Hell brought to you by LIONEIGHTCAKE!! - //All new liquid rendering for lava can be found in Systems/LavaRendering.cs - - //To save on time, a LOT of the edits are from the BiomeLava mod (owned by myself, lion8cake). Obv every edit has been changed to reflect - //Calamity's style of IL edits and to remove any unnessesary edits not relivant to calamity. In the loading of the detours and edits - //BiomeLava is a check for whether they load or not. If BiomeLava is active all ModLavaStyles will be turned into ModCalls for BiomeLava. - - #region Lava Rendering - private static void DoDrawLavas(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdsfld
("drawToScreen"), i => i.MatchBrfalse(out _), i => i.MatchLdarg0(), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawWaters"))) - { - LogFailure("DoDraw Lava", "Could not locate the drawing of Background Waters"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLavas(isBackground: true); - }); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdsfld
("drawToScreen"), i => i.MatchBrfalse(out _), i => i.MatchLdarg0(), i => i.MatchLdcI4(0), i => i.MatchCall
("DrawWaters"))) - { - LogFailure("DoDraw Lava", "Could not locate the drawing of Waters"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLavas(); - }); - } - - private static void RenderLavas(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(0), i => i.MatchCall
("DrawWaters"))) - { - LogFailure("Render Lava", "Could not locate the drawing of Waters"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLavas(); - }); - } - - private static void RenderLavaBackgrounds(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdcI4(1), i => i.MatchCall
("DrawWaters"))) - { - LogFailure("Render Lava Backgroumds", "Could not locate the drawing of Background Waters"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLavas(isBackground: true); - }); - } - - private static void DrawLavatoCapture(ILContext il) - { - ILCursor cursor = new ILCursor(il); - - // - // line :: array = liquidAlpha.ToArray(); - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdsfld
("liquidAlpha"), - i => i.MatchCall(out _), - i => i.MatchStloc(out _))) - { - LogFailure("Draw lavas to captures", "Could not locate the saving of water alphas"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.LavaAlpha.CopyTo(LavaRenderingSystem.AlphaSave, 0); - }); - - // - // line :: for (int i = 0; i <= 10; i++) - if (!cursor.TryGotoNext(MoveType.Before, - i => i.MatchLdcI4(0), - i => i.MatchStloc(out _), - i => i.MatchBr(out _), - i => i.MatchLdloc(out _), // i - i => i.MatchLdcI4(1), - i => i.MatchBeq(out _))) - { - LogFailure("Draw lavas to captures", "Could not locate the resetting of water alphas"); - return; - } - cursor.EmitDelegate(() => - { - for (int i = 0; i < 1; i++) - { - LavaRenderingSystem.LavaAlpha[i] = ((i == LavaRenderingSystem.LavaStyle) ? 1f : 0f); - } - }); - - // - // line :: this.DrawLiquid(true, Main.waterStyle, 1f, true); - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdarg0(), - i => i.MatchLdcI4(1), - i => i.MatchLdsfld
("waterStyle"), - i => i.MatchLdcR4(1), i => i.MatchLdcI4(1), - i => i.MatchCall
("DrawLiquid"))) - { - LogFailure("Draw lavas to captures", "Could not locate the background of liquid capture drawing"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLiquid(bg: true, LavaRenderingSystem.LavaStyle); - }); - - // - // line :: this.DrawLiquid(true, Main.bloodMoon ? 9 : biome.WaterStyle, 1f, true); - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdarg0(), - i => i.MatchLdcI4(1), - i => i.MatchLdsfld
("bloodMoon"), - i => i.MatchBrtrue(out _), - i => i.MatchLdloc(out _), // biome - i => i.MatchLdfld("WaterStyle"), - i => i.MatchBr(out _), - i => i.MatchLdcI4(9), - i => i.MatchLdcR4(1), - i => i.MatchLdcI4(1), - i => i.MatchCall
("DrawLiquid"))) - { - LogFailure("Draw lavas to captures", "Could not locate the second background of liquid capture drawing"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLiquid(bg: true, LavaRenderingSystem.LavaStyle); - }); - - // - // line :: this.DrawLiquid(false, Main.waterStyle, 1f, true); - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdarg0(), - i => i.MatchLdcI4(0), - i => i.MatchLdsfld
("waterStyle"), - i => i.MatchLdcR4(1), - i => i.MatchLdcI4(1), - i => i.MatchCall
("DrawLiquid"))) - { - LogFailure("Draw lavas to captures", "Could not locate the liquid capture drawing"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLiquid(bg: false, LavaRenderingSystem.LavaStyle); - }); - - // - // line :: this.DrawLiquid(false, biome.WaterStyle, 1f, true); - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdarg0(), - i => i.MatchLdcI4(0), - i => i.MatchLdloc(out _), - i => i.MatchLdfld("WaterStyle"), - i => i.MatchLdcR4(1), - i => i.MatchLdcI4(1), - i => i.MatchCall
("DrawLiquid"))) - { - LogFailure("Draw lavas to captures", "Could not locate the second liquid capture drawing"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.Instance.DrawLiquid(bg: false, LavaRenderingSystem.LavaStyle); - }); - - // - // line :: Main.liquidAlpha = array; - if (!cursor.TryGotoNext(MoveType.After, - i => i.MatchLdloc(out _), - i => i.MatchStsfld
("liquidAlpha"))) - { - LogFailure("Draw lavas to captures", "Could not locate water style value returner"); - return; - } - cursor.EmitDelegate(() => - { - LavaRenderingSystem.AlphaSave.CopyTo(LavaRenderingSystem.LavaAlpha, 0); - }); - } - - private void AddTileLiquidDrawing(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg0(), i => i.MatchLdarg1(), i => i.MatchLdcI4(0), i => i.MatchLdarg(out _), i => i.MatchLdloc1(), i => i.MatchLdloc2(), i => i.MatchLdloc(13), i => i.MatchLdloc(14), i => i.MatchLdloc(15), i => i.MatchCall("DrawTile_LiquidBehindTile"))) - { - LogFailure("Tile Lava Drawing", "Could not locate the drawing of Liquid Behind Tile drawing"); - return; - } - cursor.EmitLdloc1(); - cursor.EmitLdloc2(); - cursor.EmitLdloc(13); - cursor.EmitLdloc(14); - cursor.EmitLdloc(15); - cursor.EmitDelegate((Vector2 unscaledPosition, Vector2 vector, int j, int i, Tile tile) => - { - LavaRenderingSystem.Instance.DrawTile_LiquidBehindTile(solidLayer: false, inFrontOfPlayers: false, -1, unscaledPosition, vector, j, i, tile); - }); - } - #endregion - - #region Lava Blocking - private void BlockLavaDrawing(ILContext il) - { - const string NameOfType = nameof(LiquidRenderer.LiquidDrawCache.Type); - const string NameOfIsVisible = nameof(LiquidRenderer.LiquidDrawCache.IsVisible); - - //This edit to DrawNormalLiquids makes lavas in normal and white lighting draw with an alpha and with new textures - //If the parameter for the waterstyle is more than the max waterstyles then its subtracted by the max water style count and thats the lava style ID - ILCursor cursor = new ILCursor(il); - - var drawCacheTypeField = typeof(LiquidRenderer.LiquidDrawCache).GetRuntimeField(NameOfType); - if (drawCacheTypeField == null) - { - LogFailure("Liquid Renderer Drawing", $"Could not find {nameof(LiquidRenderer.LiquidDrawCache)}::{NameOfType}"); - return; - } - - var drawCacheIsVisibleField = typeof(LiquidRenderer.LiquidDrawCache).GetRuntimeField(NameOfIsVisible); - if (drawCacheIsVisibleField == null) - { - LogFailure("Liquid Renderer Drawing", $"Could not find {nameof(LiquidRenderer.LiquidDrawCache)}::{NameOfIsVisible}"); - return; - } - - //Continue if statement, basically - //if the liquid being drawn is lava and the water style is greater than the max water styles or if the liquid is water and less than the max water styles then the draw code is ran - //otherwise the loop/s are continued for the next liquid - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchStloc(4), i => i.MatchBr(out _), i => i.MatchLdloc(2), i => i.MatchLdfld(drawCacheIsVisibleField))) - { - LogFailure("Liquid Renderer Drawing", "Could not locate the IsVisible boolean check"); - return; - } - cursor.EmitLdarg3(); - cursor.EmitLdloc2(); //Initiated Liquid Draw Cache (needed for the Type parameter) - cursor.EmitLdfld(drawCacheTypeField); - cursor.EmitDelegate>((IsVisible, style, type) => - { - // Not Visible, False - if (!IsVisible) - return false; - - // Visible Lava, True if it's Modded Style - if (type == LiquidID.Lava) - return style >= LavaRenderingSystem.Instance.WaterStyleMaxCount + 1; - - // Other Liquid, True if it's valid liquid style - return style <= LavaRenderingSystem.Instance.WaterStyleMaxCount; - }); - - //Lava alpha color, if the liquid drawn is lava, multiply num by the water alpha - if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchLdloc(2), i => i.MatchLdfld(drawCacheTypeField), i => i.MatchStloc(8))) - { - LogFailure("Liquid Renderer Drawing", "Could not locate creation of the local variable num2 (the liquid type holder variable)"); - return; - } - cursor.EmitLdloc(8); - cursor.EmitLdloca(7); - cursor.EmitLdarg(4); - cursor.EmitDelegate((int num2, ref float num, float globalAlpha) => - { - if (num2 == LiquidID.Lava) - { - num *= globalAlpha; - } - }); - - //Conditionally replace the liquid texture whether the liquid is lava or water - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCallvirt(typeof(Asset).GetMethod("get_Value", BindingFlags.Public | BindingFlags.Instance)))) - { - LogFailure("Liquid Renderer Drawing", "Could not locate the Texture2D array of liquids"); - return; - } - cursor.EmitLdarg3(); - cursor.EmitLdloc2(); //Initiated Liquid Draw Cache (needed for the Type parameter) - cursor.EmitLdfld(drawCacheTypeField); - cursor.EmitDelegate>((initialTexture, style, type) => - { - if (style >= LavaRenderingSystem.Instance.WaterStyleMaxCount + 1 && type == LiquidID.Lava) - return LavaRenderingSystem.Textures.liquid[style - LavaRenderingSystem.Instance.WaterStyleMaxCount - 1].Value; - else - return initialTexture; - }); - } - private void BlockLavaDrawingForSlopes(On_TileDrawing.orig_DrawTile_LiquidBehindTile orig, TileDrawing self, bool solidLayer, bool inFrontOfPlayers, int waterStyleOverride, Vector2 screenPosition, Vector2 screenOffset, int tileX, int tileY, Tile tileCache) - { - if (tileCache.LiquidType == LiquidID.Lava) - return; - - Tile tile = Main.tile[tileX + 1, tileY]; - if (tile.LiquidType == LiquidID.Lava) - return; - - Tile tile2 = Main.tile[tileX - 1, tileY]; - if (tile2.LiquidType == LiquidID.Lava) - return; - - Tile tile3 = Main.tile[tileX, tileY - 1]; - if (tile3.LiquidType == LiquidID.Lava) - return; - - Tile tile4 = Main.tile[tileX, tileY + 1]; - if (tile4.LiquidType == LiquidID.Lava) - return; - - orig.Invoke(self, solidLayer, inFrontOfPlayers, waterStyleOverride, screenPosition, screenOffset, tileX, tileY, tileCache); - } - - private void BlockLavaDrawingForSlopes2(On_TileDrawing.orig_DrawPartialLiquid orig, TileDrawing self, bool behindBlocks, Tile tileCache, ref Vector2 position, ref Rectangle liquidSize, int liquidType, ref VertexColors colors) - { - if (liquidType == 1) - { - return; - } - orig.Invoke(self, behindBlocks, tileCache, ref position, ref liquidSize, liquidType, ref colors); - } - - private void LavafallRemover(On_WaterfallManager.orig_DrawWaterfall_int_int_int_float_Vector2_Rectangle_Color_SpriteEffects orig, WaterfallManager self, int waterfallType, int x, int y, float opacity, Vector2 position, Rectangle sourceRect, Color color, SpriteEffects effects) - { - if (waterfallType == 1) - { - return; - } - orig.Invoke(self, waterfallType, x, y, opacity, position, sourceRect, color, effects); - } - - private void BlockRetroLightingLava(ILContext il) - { - ILCursor cursor = new ILCursor(il); - ILLabel target = cursor.DefineLabel(); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchCgt(), i => i.MatchLdarg1(), i => i.MatchOr(), i => i.MatchBrfalse(out target))) - { - LogFailure("old Drawing Waters", "Could not locate the if statement that contains the check for liquid types, amounts and wether the liquid has a bg (arg1) or not"); - return; - } - if (target == null) - { - LogFailure("old Drawing Waters", "The BrFalse returned null, the boolean check getting was unsuccessful"); - return; - } - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdindU1(), i => i.MatchSub(), i => i.MatchConvR4(), i => i.MatchStloc(14))) - { - LogFailure("old Drawing Waters", "Could not locate the num3 local variable creation to put the if check after"); - return; - } - cursor.EmitLdloc(12); - cursor.EmitLdloc(11); - cursor.EmitDelegate((int i, int j) => - { - return Main.tile[i, j].LiquidType == LiquidID.Lava; - }); - cursor.EmitBrtrue(target); - } - #endregion - - #region Lava Replacing - private void LavaBubbleReplacer(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcI4(16), i => i.MatchLdcI4(16), i => i.MatchLdcI4(35))) - { - LogFailure("Ambient lava bubble replacer", "Could not locate the bubble newdust parameters"); - return; - } - cursor.EmitDelegate>(type => LavaRenderingSystem.dustLava()); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdcI4(16), i => i.MatchLdcI4(8), i => i.MatchLdcI4(35))) - { - LogFailure("Ambient lava bubble replacer", "Could not locate the surface bubble newdust parameters"); - return; - } - cursor.EmitDelegate>(type2 => LavaRenderingSystem.dustLava()); - } - - private void LavaDropletReplacer(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdarg(out _), i => i.MatchLdcI4(374), i => i.MatchBneUn(out _), i => i.MatchLdcI4(716))) - { - LogFailure("Ambient lava droplet replacer", "Could not locate the lava droplet newgore parameters"); - return; - } - cursor.EmitDelegate>(type => LavaRenderingSystem.goreLava()); - } - - private void SplashEntityLava(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdfld("width"), i => i.MatchLdcI4(12), i => i.MatchAdd(), i => i.MatchLdcI4(24), i => i.MatchLdcI4(35))) - { - LogFailure("Entity Lava Splashing (Item, Projectile, NPC, Player)", "Could not locate the first lava bubble splashing"); - return; - } - cursor.EmitDelegate>(type => LavaRenderingSystem.dustLava()); - if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdfld("width"), i => i.MatchLdcI4(12), i => i.MatchAdd(), i => i.MatchLdcI4(24), i => i.MatchLdcI4(35))) - { - LogFailure("Entity Lava Splashing (Item, Projectile, NPC, Player)", "Could not locate the second lava bubble splashing"); - return; - } - cursor.EmitDelegate>(type2 => LavaRenderingSystem.dustLava()); - } - - private void PlayerDebuffEdit(ILContext il) - { - //Injects code directly at the position where the OnFire debuff is handled - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchLdarg0(), i => i.MatchLdcI4(24), i => i.MatchLdloc(161), i => i.MatchLdcI4(1), i => i.MatchLdcI4(0), i => i.MatchCall("AddBuff"))) - { - LogFailure("Player Update Lava Debuff", "Could not locate the infliction of the On Fire! debuff inside the Player Update code"); - return; - } - cursor.EmitLdarg0(); - cursor.EmitLdloc(161); - cursor.EmitDelegate((Player player, int onFiretime) => - { - LavaStylesLoader.InflictDebuff(player, LavaRenderingSystem.LavaStyle, onFiretime); - }); - } - #endregion - - #region Other - private Color WaterfallGlowmaskEditor(On_WaterfallManager.orig_StylizeColor orig, float alpha, int maxSteps, int waterfallType, int y, int s, Tile tileCache, Color aColor) - { - if (LavaRenderingSystem.LavaStyle != 0 && !LavaStylesLoader.Get(LavaRenderingSystem.LavaStyle).LavafallGlowmask()) - { - return aColor; - } - else - { - return orig.Invoke(alpha, maxSteps, waterfallType, y, s, tileCache, aColor); - } - } - - private void LavaFallRedrawer(On_WaterfallManager.orig_Draw orig, WaterfallManager self, SpriteBatch spriteBatch) - { - orig.Invoke(self, spriteBatch); - LavaRenderingSystem.Instance.InitialDrawLavafall(self); - } - #endregion - - #endregion - - #region Liquid Visuals - //Contains all liquid light and liquid alpha (seethrough-ability) - private static void ApplyLiquidEmit(ILContext il) - { - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.Before, i => i.MatchRet())) - { - LogFailure("Liquid Light Emit", "ret instruction is missing????? how is this even possible?????"); - return; - } - - cursor.EmitLdloc0(); // This should be tile - cursor.EmitLdarg1(); // x - cursor.EmitLdarg2(); // y - cursor.EmitLdarg3(); // ref OutputColor - cursor.EmitDelegate((Tile tile, int x, int y, ref Vector3 lightColor) => - { - if (tile.HasTile || tile.LiquidAmount <= 0) - return; - - if (tile.LiquidType == LiquidID.Water) - { - float R = 0f; - float G = 0f; - float B = 0f; - CalamityWaterLoader.ModifyLightSetup(in tile, x, y, Main.waterStyle, ref R, ref G, ref B); - - lightColor.X = Math.Max(lightColor.X, R); - lightColor.Y = Math.Max(lightColor.Y, G); - lightColor.Z = Math.Max(lightColor.Z, B); - } - else if (tile.LiquidType == LiquidID.Lava && ExternalMods.biomeLava == null) - { - Vector3 lavaLight = new Vector3(0.55f, 0.33f, 0.11f); - - float R = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.X : 0f; - float G = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.Y : 0f; - float B = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.Z : 0f; - LavaStylesLoader.ModifyLightSetup(x, y, LavaRenderingSystem.LavaStyle, ref R, ref G, ref B); - - for (int styleIndex = 0; styleIndex < LavaStylesLoader.TotalCount; styleIndex++) - { - if (LavaRenderingSystem.LavaAlpha[styleIndex] > 0f && styleIndex != LavaRenderingSystem.LavaStyle) - { - float r = styleIndex == 0 ? lavaLight.X : 0f; - float g = styleIndex == 0 ? lavaLight.Y : 0f; - float b = styleIndex == 0 ? lavaLight.Z : 0f; - LavaStylesLoader.ModifyLightSetup(x, y, styleIndex, ref r, ref g, ref b); - - float r2 = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.X : 0f; - float g2 = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.Y : 0f; - float b2 = LavaRenderingSystem.LavaStyle == 0 ? lavaLight.Z : 0f; - LavaStylesLoader.ModifyLightSetup(x, y, LavaRenderingSystem.LavaStyle, ref r2, ref g2, ref b2); - - R = float.Lerp(r, r2, LavaRenderingSystem.LavaAlpha[LavaRenderingSystem.LavaStyle]); - G = float.Lerp(g, g2, LavaRenderingSystem.LavaAlpha[LavaRenderingSystem.LavaStyle]); - B = float.Lerp(b, b2, LavaRenderingSystem.LavaAlpha[LavaRenderingSystem.LavaStyle]); - } - } - - if (R != 0.0f || G != 0.0f || B != 0.0f) - { - float colorManipulator = (float)(270 - Main.mouseTextColor) / 900f; - R += colorManipulator; - G += colorManipulator; - B += colorManipulator; - } - - lightColor.X = Math.Max(lightColor.X, R); - lightColor.Y = Math.Max(lightColor.Y, G); - lightColor.Z = Math.Max(lightColor.Z, B); - } - }); - } - - private static void LavafallLightEditor(On_WaterfallManager.orig_AddLight orig, int waterfallType, int x, int y) - { - if (waterfallType == 1) - { - float r = 0.55f; - float g = 0.33f; - float b = 0.11f; - LavaStylesLoader.ModifyLightSetup(x, y, LavaRenderingSystem.LavaStyle, ref r, ref g, ref b); - Lighting.AddLight(x, y, r, g, b); - return; - } - orig.Invoke(waterfallType, x, y); - } - - private static void LiquidDrawColors(ILContext il) - { - const string TypeFieldName = nameof(LiquidRenderer.LiquidDrawCache.Type); - - ILCursor cursor = new ILCursor(il); - if (!cursor.TryGotoNext(MoveType.Before, c => c.MatchLdarg2(), c => c.MatchLdloc(3), c => c.MatchLdloc(4), c => c.MatchCall
("DrawTileInWater"))) - { - LogFailure("Liquid Draw Colors", "Could not locate the liquid vertex colors for drawing"); - return; - } - - cursor.Emit(OpCodes.Ldloc, 3); - cursor.Emit(OpCodes.Ldloc, 4); - cursor.Emit(OpCodes.Ldloc_2); - cursor.EmitLdfld(typeof(LiquidRenderer.LiquidDrawCache).GetField(TypeFieldName)); - cursor.Emit(OpCodes.Ldloca, 9); - - cursor.EmitDelegate((int x, int y, byte liquidType, ref VertexColors initialColor) => - { - if (liquidType == LiquidID.Water) - { - CalamityWaterLoader.DrawColorSetup(x, y, Main.waterStyle, ref initialColor); - } - else if (liquidType == LiquidID.Lava && ExternalMods.biomeLava == null) - { - LavaStylesLoader.DrawColorSetup(x, y, LavaRenderingSystem.LavaStyle, ref initialColor); - } - }); - } - - private static void LiquidSlopeDrawColors(ILContext il) - { - ILCursor cursor = new ILCursor(il); - - var loaderGetMethod = typeof(LoaderManager) - .GetMethod(nameof(LoaderManager.Get)) - .MakeGenericMethod([typeof(WaterStylesLoader)]); - if (loaderGetMethod is null) - { - LogFailure("Liquid Slope Draw Colors", "Cannot find \"LoaderManager.Get\" Method"); - return; - } - - var getTotalCountGetter = typeof(Loader) - .GetProperty("TotalCount", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) - .GetMethod; - if (getTotalCountGetter is null) - { - LogFailure("Liquid Slope Draw Colors", "Cannot find \"Loader.TotalCount (internal)\" Method"); - return; - } - - if (!cursor.TryGotoNext(MoveType.After, - c => c.MatchCall(loaderGetMethod), - c => c.MatchCallOrCallvirt(getTotalCountGetter))) - { - LogFailure("Liquid Slope Draw Colors", "Could not locate the liquid slope vertex colors for drawing"); - return; - } - - // callvirt Loader.TotalCount - // [We are here] - // stloc.s totalCount - - // Starts from callvirt (ret: int) - // Stack now have TotalCount and about to pop by local variable, so: - cursor.EmitLdarg(8); // Push tileCache - cursor.EmitLdarg(6); // Push tileX - cursor.EmitLdarg(7); // Push tileY - cursor.EmitLdloca(14); // Push Color* - cursor.EmitDelegate((Tile tileCache, int x, int y, ref VertexColors initialColor) => - { - // TODO: Properly handle Transition between styles - // That will require extra il injection to achieve - if (tileCache.LiquidType == LiquidID.Water) - CalamityWaterLoader.DrawColorSetup(x, y, Main.waterStyle, ref initialColor, true); - - // Known Issue: Sloped Lava Rendering actually does not work on here! (It's been a case even before edited in 2024/08/30) - // Reason: Flag6 is "IsWater" flag, and since we don't modified that behaviour "yet" it will never be triggered here - // Fix this when there will be new lava style with dynamic color changes - else if (tileCache.LiquidType == LiquidID.Lava) - LavaStylesLoader.DrawColorSetup(x, y, LavaRenderingSystem.LavaStyle, ref initialColor); - }); - - // And now remaining TotalCount is now could pass to local variable - - #region Intended Result for Reference - // callvirt Loader.TotalCount push 1 - // - // ldarg 8 (tileCache) push 1 - // ldarg 6 (tileX) push 1 - // ldarg 7 (tileY) push 1 - // ldloca 14 (vertices) push 1 - // call ModdedDelegate pop 4 - // - // stloc.s totalCount pop 1 - #endregion - } - #endregion - #region Statue Additions private static void AddTwinklersToStatue(ILContext il) { diff --git a/ModSupport/WeakReferenceSupport.cs b/ModSupport/WeakReferenceSupport.cs index fec7bf9065..55450e36ef 100644 --- a/ModSupport/WeakReferenceSupport.cs +++ b/ModSupport/WeakReferenceSupport.cs @@ -54,6 +54,7 @@ using CalamityMod.Projectiles.DraedonsArsenal; using CalamityMod.Projectiles.Summon; using CalamityMod.Projectiles.Summon.Umbrella; using CalamityMod.Systems; +using CalamityMod.Systems.Graphic.LiquidSystem; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -203,10 +204,9 @@ namespace CalamityMod if (biomelava == null) return; - foreach (ModLavaStyle item in LavaStylesLoader.Content) + foreach (ModLavaStyle lavaStyle in ModLavaStyleLoader.AllStyles) { - int type = item.Slot; - ModLavaStyle lavaStyle = LavaStylesLoader.Get(type); + int type = lavaStyle.Slot; if (lavaStyle != null) { Func GetSplashDust = lavaStyle.GetSplashDust; @@ -1310,7 +1310,7 @@ namespace CalamityMod #region Luminance private static void RegisterWorldInfoIcon(Mod luminance, string texturePath, string hoverTextKey, Func shouldAppear, byte priority) => luminance.Call("RegisterWorldInfoIcon", texturePath, hoverTextKey, shouldAppear, priority); - + private static void LuminanceSupport() { Mod luminance = ExternalMods.luminance; @@ -1324,7 +1324,7 @@ namespace CalamityMod return tagData.ContainsKey("DeathMode") && tagData.GetBool("DeathMode"); }; - + Func revengeanceEnabled = data => { if (!data.TryGetHeaderData(out var tagData)) @@ -1332,7 +1332,7 @@ namespace CalamityMod return tagData.ContainsKey("RevengeanceMode") && tagData.GetBool("RevengeanceMode") && !(tagData.ContainsKey("DeathMode") && tagData.GetBool("DeathMode")); }; - + RegisterWorldInfoIcon(luminance, "CalamityMod/UI/ModeIndicator/ModeIndicator_Death", "Mods.CalamityMod.UI.Death", deathEnabled, 50); RegisterWorldInfoIcon(luminance, "CalamityMod/UI/ModeIndicator/ModeIndicator_Rev", "Mods.CalamityMod.UI.Revengeance", revengeanceEnabled, 50); } diff --git a/Projectiles/Magic/AetherfluxCannonHoldout.cs b/Projectiles/Magic/AetherfluxCannonHoldout.cs index f854372844..22726a859c 100644 --- a/Projectiles/Magic/AetherfluxCannonHoldout.cs +++ b/Projectiles/Magic/AetherfluxCannonHoldout.cs @@ -8,7 +8,6 @@ using Terraria.Audio; using Terraria.GameContent; using Terraria.ID; using Terraria.ModLoader; -using static CalamityMod.Systems.LavaRenderingSystem; namespace CalamityMod.Projectiles.Magic { @@ -83,7 +82,7 @@ namespace CalamityMod.Projectiles.Magic SoundStyle fire = new("CalamityMod/Sounds/Item/MagnaCannonShot"); SoundEngine.PlaySound(fire with { Volume = 0.4f, Pitch = Main.rand.NextFloat(0.1f, 0.3f) }, Projectile.Center); } - + int projID = ModContent.ProjectileType(); float shootSpeed = HeldItem.shootSpeed; diff --git a/Systems/Graphic/CalamityModWaterStyle.cs b/Systems/Graphic/CalamityModWaterStyle.cs index 15b3388551..222adb2a54 100644 --- a/Systems/Graphic/CalamityModWaterStyle.cs +++ b/Systems/Graphic/CalamityModWaterStyle.cs @@ -30,40 +30,4 @@ namespace CalamityMod.Systems { } } - - internal static class CalamityWaterLoader - { - internal static void ModifyLightSetup(ref readonly Tile tile, int i, int j, int type, ref float r, ref float g, ref float b) - { - if (TryGetCalamityWaterStyle(type, out var styles)) - { - styles.ModifyLight(in tile, i, j, ref r, ref g, ref b); - } - } - - internal static void DrawColorSetup(int x, int y, int type, ref VertexColors liquidColor, bool isSlope = false) - { - if (TryGetCalamityWaterStyle(type, out var styles)) - { - styles.DrawColor(x, y, ref liquidColor, isSlope); - } - } - - internal static bool TryGetCalamityWaterStyle(int type, out CalamityModWaterStyle waterStyle) - { - waterStyle = GetCalamityWaterStyle(type); - return waterStyle != null; - } - - internal static CalamityModWaterStyle GetCalamityWaterStyle(int type) - { - var modWaterStyle = LoaderManager.Get().Get(type); - if (modWaterStyle is CalamityModWaterStyle calWaterStyle) - { - return calWaterStyle; - } - - return null; - } - } } diff --git a/Systems/Graphic/LavaRenderingSystem.cs b/Systems/Graphic/LavaRenderingSystem.cs deleted file mode 100644 index f2f6b5e36e..0000000000 --- a/Systems/Graphic/LavaRenderingSystem.cs +++ /dev/null @@ -1,1184 +0,0 @@ -using System; -using System.Diagnostics; -using System.Linq; -using System.Reflection; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using ReLogic.Content; -using Terraria; -using Terraria.GameContent; -using Terraria.GameContent.Liquid; -using Terraria.Graphics; -using Terraria.ID; -using Terraria.ModLoader; -using static Terraria.WaterfallManager; - -namespace CalamityMod.Systems -{ - [Autoload(Side = ModSide.Client)] - public sealed class LavaRenderingSystem : ModSystem - { - //Welcome to Calamity's lava rendering. Prepare your eyes - public static LavaRenderingSystem Instance; - - internal static readonly FastField[]> WaterfallTextures = new("waterfallTexture", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); - - public static int LavaStyle; - public static float[] LavaAlpha = new float[1]; - internal static float[] AlphaSave = new float[1]; - - // Holds the Texture Arrays for all the lava textures. - // These are used for the lava styles. They are seperate from Textureasset.Instance._liquidTexture as they will conflict with ModWaterStyle - // Can hold up to 255 lava styles (more than enough) (excluding the normal lava texture which is liquidTexture 1) - public struct Textures - { - public static Asset[] liquid = new Asset[1]; - public static Asset[] slope = new Asset[1]; - public static Asset[] block = new Asset[1]; - public static Asset[] fall = new Asset[1]; - } - - public int WaterStyleMaxCount = Main.maxLiquidTypes; - - public override void Load() - { - Instance = this; - } - - public override void OnModLoad() - { - // Lava Texture - Textures.liquid[0] = LiquidRenderer.Instance._liquidTextures[1]; - Textures.slope[0] = TextureAssets.LiquidSlope[1]; - Textures.block[0] = TextureAssets.Liquid[1]; - Textures.fall[0] = WaterfallTextures.Get(Main.instance.waterfallManager)[1]; - } - - public override void ResizeArrays() - { - // Lava Texture Resizing are handled on LavaStylesLoader - WaterStyleMaxCount = ModContent.GetContent().Count() + Main.maxLiquidTypes; - } - - public void DrawLavas(bool isBackground = false) - { - Main.drewLava = false; - if (!isBackground) - { - LavaStyle = 0; - LavaStylesLoader.IsLavaActive(); - for (int i = 0; i < 1; i++) - { - if (LavaStyle != i) - { - LavaAlpha[i] = Math.Max(LavaAlpha[i] - 0.2f, 0f); - } - else - { - LavaAlpha[i] = Math.Min(LavaAlpha[i] + 0.2f, 1f); - } - } - LavaStylesLoader.UpdateLiquidAlphas(); - } - bool flag = false; - for (int j = 0; j < LavaStylesLoader.TotalCount; j++) - { - if (LavaAlpha[j] > 0f && j != LavaStyle) - { - DrawLiquid(isBackground, j, isBackground ? 1f : LavaAlpha[j], drawSinglePassLiquids: false); - flag = true; - } - } - DrawLiquid(isBackground, LavaStyle, flag ? LavaAlpha[LavaStyle] : 1f); - } - - protected internal void DrawLiquid(bool bg = false, int lavaStyle = 0, float Alpha = 1f, bool drawSinglePassLiquids = true) - { - if (!Lighting.NotRetro) // Is Retro - { - oldDrawLava(bg, lavaStyle, Alpha); - return; - } - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - Vector2 drawOffset = (Vector2)(Main.drawToScreen ? Vector2.Zero : new Vector2((float)Main.offScreenRange, (float)Main.offScreenRange)) - Main.screenPosition; - if (bg && !LiquidEdgeRenderer.Active) - { - DrawLiquidBehindTiles(lavaStyle); - } - LiquidRenderer.Instance.DrawNormalLiquids(Main.spriteBatch, drawOffset, lavaStyle + WaterStyleMaxCount + 1, Alpha, bg); - if (!bg) - { - TimeLogger.DrawTime(4, stopwatch.Elapsed.TotalMilliseconds); - } - } - - public void oldDrawLava(bool bg = false, int Style = 0, float Alpha = 1f) - { - float num = 0f; - float num12 = 99999f; - float num23 = 99999f; - int num27 = -1; - int num28 = -1; - Vector2 vector = new((float)Main.offScreenRange, (float)Main.offScreenRange); - if (Main.drawToScreen) - { - vector = Vector2.Zero; - } - _ = new Color[4]; - int num29 = (int)(255f * (1f - Main.gfxQuality) + 40f * Main.gfxQuality); - _ = Main.gfxQuality; - _ = Main.gfxQuality; - int num30 = (int)((Main.screenPosition.X - vector.X) / 16f - 1f); - int num31 = (int)((Main.screenPosition.X + (float)Main.screenWidth + vector.X) / 16f) + 2; - int num32 = (int)((Main.screenPosition.Y - vector.Y) / 16f - 1f); - int num2 = (int)((Main.screenPosition.Y + (float)Main.screenHeight + vector.Y) / 16f) + 5; - if (num30 < 5) - { - num30 = 5; - } - if (num31 > Main.maxTilesX - 5) - { - num31 = Main.maxTilesX - 5; - } - if (num32 < 5) - { - num32 = 5; - } - if (num2 > Main.maxTilesY - 5) - { - num2 = Main.maxTilesY - 5; - } - Vector2 vector2; - Rectangle value; - Color newColor; - for (int i = num32; i < num2 + 4; i++) - { - for (int j = num30 - 2; j < num31 + 2; j++) - { - if (Main.tile[j, i].LiquidAmount <= 0 || (Main.tile[j, i].HasUnactuatedTile && Main.tileSolid[Main.tile[j, i].TileType] && !Main.tileSolidTop[Main.tile[j, i].TileType]) || !(Lighting.Brightness(j, i) > 0f || bg) || Main.tile[j, i].LiquidType != LiquidID.Lava) - { - continue; - } - Color color = Lighting.GetColor(j, i); - float num3 = 256 - Main.tile[j, i].LiquidAmount; - num3 /= 32f; - int num4 = 0; - if (Main.tile[j, i].LiquidType == LiquidID.Lava) - { - /*if (Main.drewLava) //disallows the back liquid to not draw until its alpha hits 1f apparently - { - continue; - }*/ - float num5 = Math.Abs((float)(j * 16 + 8) - (Main.screenPosition.X + (float)(Main.screenWidth / 2))); - float num6 = Math.Abs((float)(i * 16 + 8) - (Main.screenPosition.Y + (float)(Main.screenHeight / 2))); - if (num5 < (float)(Main.screenWidth * 2) && num6 < (float)(Main.screenHeight * 2)) - { - float num7 = (float)Math.Sqrt(num5 * num5 + num6 * num6); - float num8 = 1f - num7 / ((float)Main.screenWidth * 0.75f); - if (num8 > 0f) - { - num += num8; - } - } - if (num5 < num12) - { - num12 = num5; - num27 = j * 16 + 8; - } - if (num6 < num23) - { - num23 = num5; - num28 = i * 16 + 8; - } - } - if (num4 == 0) - { - num4 = Style; - } - if (Main.drewLava) - { - continue; - } - float num9 = 0.5f; - if (bg) - { - num9 = 1f; - } - num9 *= Alpha; - Main.DrawTileInWater(-Main.screenPosition + vector, j, i); //lily pads - vector2 = new((float)(j * 16), (float)(i * 16 + (int)num3 * 2)); - value = new(0, 0, 16, 16 - (int)num3 * 2); - bool flag2 = true; - if (Main.tile[j, i + 1].LiquidAmount < 245 && (!Main.tile[j, i + 1].HasUnactuatedTile || !Main.tileSolid[Main.tile[j, i + 1].TileType] || Main.tileSolidTop[Main.tile[j, i + 1].TileType])) - { - float num10 = 256 - Main.tile[j, i + 1].LiquidAmount; - num10 /= 32f; - num9 = 0.5f * (8f - num3) / 4f; - if ((double)num9 > 0.55) - { - num9 = 0.55f; - } - if ((double)num9 < 0.35) - { - num9 = 0.35f; - } - float num11 = num3 / 2f; - if (Main.tile[j, i + 1].LiquidAmount < 200) - { - if (bg) - { - continue; - } - if (Main.tile[j, i - 1].LiquidAmount > 0 && Main.tile[j, i - 1].LiquidAmount > 0) - { - value = new(0, 4, 16, 16); - num9 = 0.5f; - } - else if (Main.tile[j, i - 1].LiquidAmount > 0) - { - vector2 = new((float)(j * 16), (float)(i * 16 + 4)); - value = new(0, 4, 16, 12); - num9 = 0.5f; - } - else if (Main.tile[j, i + 1].LiquidAmount > 0) - { - vector2 = new((float)(j * 16), (float)(i * 16 + (int)num3 * 2 + (int)num10 * 2)); - value = new(0, 4, 16, 16 - (int)num3 * 2); - } - else - { - vector2 = new((float)(j * 16 + (int)num11), (float)(i * 16 + (int)num11 * 2 + (int)num10 * 2)); - value = new(0, 4, 16 - (int)num11 * 2, 16 - (int)num11 * 2); - } - } - else - { - num9 = 0.5f; - value = new(0, 4, 16, 16 - (int)num3 * 2 + (int)num10 * 2); - } - } - else if (Main.tile[j, i - 1].LiquidAmount > 32) - { - value = new(0, 4, value.Width, value.Height); - } - else if (num3 < 1f && Main.tile[j, i - 1].HasUnactuatedTile && Main.tileSolid[Main.tile[j, i - 1].TileType] && !Main.tileSolidTop[Main.tile[j, i - 1].TileType]) - { - vector2 = new((float)(j * 16), (float)(i * 16)); - value = new(0, 4, 16, 16); - } - else - { - for (int k = i + 1; k < i + 6 && (!Main.tile[j, k].HasUnactuatedTile || !Main.tileSolid[Main.tile[j, k].TileType] || Main.tileSolidTop[Main.tile[j, k].TileType]); k++) - { - if (Main.tile[j, k].LiquidAmount < 200) - { - flag2 = false; - break; - } - } - if (!flag2) - { - num9 = 0.5f; - value = new(0, 4, 16, 16); - } - else if (Main.tile[j, i - 1].LiquidAmount > 0) - { - value = new(0, 2, value.Width, value.Height); - } - } - if ((color.R > 20 || color.B > 20 || color.G > 20) && value.Y < 4) - { - int num13 = color.R; - if (color.G > num13) - { - num13 = color.G; - } - if (color.B > num13) - { - num13 = color.B; - } - num13 /= 30; - if (Main.rand.Next(20000) < num13) - { - newColor = new(255, 255, 255); - int num14 = Dust.NewDust(new Vector2((float)(j * 16), vector2.Y - 2f), 16, 8, DustID.TintableDustLighted, 0f, 0f, 254, newColor, 0.75f); - Dust obj = Main.dust[num14]; - obj.velocity *= 0f; - } - } - if (Main.tile[j, i].LiquidType == LiquidID.Lava) - { - num9 *= 1.8f; - if (num9 > 1f) - { - num9 = 1f; - } - if (Main.instance.IsActive && !Main.gamePaused && Dust.lavaBubbles < 200) - { - if (Main.tile[j, i].LiquidAmount > 200 && Main.rand.NextBool(700)) - { - Dust.NewDust(new Vector2((float)(j * 16), (float)(i * 16)), 16, 16, dustLava()); - } - if (value.Y == 0 && Main.rand.NextBool(350)) - { - int num15 = Dust.NewDust(new Vector2((float)(j * 16), (float)(i * 16) + num3 * 2f - 8f), 16, 8, dustLava(), 0f, 0f, 50, default(Color), 1.5f); - Dust obj2 = Main.dust[num15]; - obj2.velocity *= 0.8f; - Main.dust[num15].velocity.X *= 2f; - Main.dust[num15].velocity.Y -= (float)Main.rand.Next(1, 7) * 0.1f; - if (Main.rand.NextBool(10)) - { - Main.dust[num15].velocity.Y *= Main.rand.Next(2, 5); - } - Main.dust[num15].noGravity = true; - } - } - } - float num16 = (float)(int)color.R * num9; - float num17 = (float)(int)color.G * num9; - float num18 = (float)(int)color.B * num9; - float num19 = (float)(int)color.A * num9; - color = new((int)(byte)num16, (int)(byte)num17, (int)(byte)num18, (int)(byte)num19); - if (Lighting.NotRetro && !bg) - { - Color color2 = color; - if (((double)(int)color2.R > (double)num29 * 0.6 || (double)(int)color2.G > (double)num29 * 0.65 || (double)(int)color2.B > (double)num29 * 0.7)) - { - for (int l = 0; l < 4; l++) - { - int num20 = 0; - int num21 = 0; - int width = 8; - int height = 8; - Color color3 = color2; - Color color4 = Lighting.GetColor(j, i); - if (l == 0) - { - color4 = Lighting.GetColor(j - 1, i - 1); - if (value.Height < 8) - { - height = value.Height; - } - } - if (l == 1) - { - color4 = Lighting.GetColor(j + 1, i - 1); - num20 = 8; - if (value.Height < 8) - { - height = value.Height; - } - } - if (l == 2) - { - color4 = Lighting.GetColor(j - 1, i + 1); - num21 = 8; - height = 8 - (16 - value.Height); - } - if (l == 3) - { - color4 = Lighting.GetColor(j + 1, i + 1); - num20 = 8; - num21 = 8; - height = 8 - (16 - value.Height); - } - num16 = (float)(int)color4.R * num9; - num17 = (float)(int)color4.G * num9; - num18 = (float)(int)color4.B * num9; - num19 = (float)(int)color4.A * num9; - color4 = new((int)(byte)num16, (int)(byte)num17, (int)(byte)num18, (int)(byte)num19); - color3.R = (byte)((color2.R * 3 + color4.R * 2) / 5); - color3.G = (byte)((color2.G * 3 + color4.G * 2) / 5); - color3.B = (byte)((color2.B * 3 + color4.B * 2) / 5); - color3.A = (byte)((color2.A * 3 + color4.A * 2) / 5); - Main.spriteBatch.Draw(Textures.block[num4].Value, vector2 - Main.screenPosition + new Vector2((float)num20, (float)num21) + vector, (Rectangle?)new Rectangle(value.X + num20, value.Y + num21, width, height), color3, 0f, default(Vector2), 1f, (SpriteEffects)0, 0f); - } - } - else - { - Main.spriteBatch.Draw(Textures.block[num4].Value, vector2 - Main.screenPosition + vector, (Rectangle?)value, color, 0f, default(Vector2), 1f, (SpriteEffects)0, 0f); - } - } - else - { - if (value.Y < 4) - { - value.X += (int)(Main.wFrame * 18f); - } - Main.spriteBatch.Draw(Textures.block[num4].Value, vector2 - Main.screenPosition + vector, (Rectangle?)value, color, 0f, default(Vector2), 1f, (SpriteEffects)0, 0f); - } - if (!Main.tile[j, i + 1].IsHalfBlock) - { - continue; - } - color = Lighting.GetColor(j, i + 1); - num16 = (float)(int)color.R * num9; - num17 = (float)(int)color.G * num9; - num18 = (float)(int)color.B * num9; - num19 = (float)(int)color.A * num9; - color = new((int)(byte)num16, (int)(byte)num17, (int)(byte)num18, (int)(byte)num19); - vector2 = new((float)(j * 16), (float)(i * 16 + 16)); - Main.spriteBatch.Draw(Textures.block[num4].Value, vector2 - Main.screenPosition + vector, (Rectangle?)new Rectangle(0, 4, 16, 8), color, 0f, default(Vector2), 1f, (SpriteEffects)0, 0f); - float num22 = 6f; - float num24 = 0.75f; - num22 = 4f; - num24 = 0.5f; - for (int m = 0; (float)m < num22; m++) - { - int num25 = i + 2 + m; - if (WorldGen.SolidTile(j, num25)) - { - break; - } - float num26 = 1f - (float)m / num22; - num26 *= num24; - vector2 = new((float)(j * 16), (float)(num25 * 16 - 2)); - Main.spriteBatch.Draw(Textures.block[num4].Value, vector2 - Main.screenPosition + vector, (Rectangle?)new Rectangle(0, 18, 16, 16), color * num26, 0f, default(Vector2), 1f, (SpriteEffects)0, 0f); - } - } - } - if (!Main.drewLava) - { - Main.ambientLavaX = num27; - Main.ambientLavaY = num28; - Main.ambientLavaStrength = num; - } - Main.drewLava = true; - } - - private void DrawLiquidBehindTiles(int lavaStyleOverride = -1) - { - Vector2 unscaledPosition = Main.Camera.UnscaledPosition; - Vector2 vector = new((float)Main.offScreenRange, (float)Main.offScreenRange); - if (Main.drawToScreen) - { - vector = Vector2.Zero; - } - GetScreenDrawArea(unscaledPosition, vector + (Main.Camera.UnscaledPosition - Main.Camera.ScaledPosition), out var firstTileX, out var lastTileX, out var firstTileY, out var lastTileY); - for (int i = firstTileY; i < lastTileY + 4; i++) - { - for (int j = firstTileX - 2; j < lastTileX + 2; j++) - { - Tile tile = Main.tile[j, i]; - if (tile != null) - { - DrawTile_LiquidBehindTile(solidLayer: false, inFrontOfPlayers: false, lavaStyleOverride, unscaledPosition, vector, j, i, tile); - } - } - } - } - - private void GetScreenDrawArea(Vector2 screenPosition, Vector2 offSet, out int firstTileX, out int lastTileX, out int firstTileY, out int lastTileY) - { - firstTileX = (int)((screenPosition.X - offSet.X) / 16f - 1f); - lastTileX = (int)((screenPosition.X + (float)Main.screenWidth + offSet.X) / 16f) + 2; - firstTileY = (int)((screenPosition.Y - offSet.Y) / 16f - 1f); - lastTileY = (int)((screenPosition.Y + (float)Main.screenHeight + offSet.Y) / 16f) + 5; - if (firstTileX < 4) - { - firstTileX = 4; - } - if (lastTileX > Main.maxTilesX - 4) - { - lastTileX = Main.maxTilesX - 4; - } - if (firstTileY < 4) - { - firstTileY = 4; - } - if (lastTileY > Main.maxTilesY - 4) - { - lastTileY = Main.maxTilesY - 4; - } - if (Main.sectionManager.AnyUnfinishedSections) - { - TimeLogger.DetailedDrawReset(); - WorldGen.SectionTileFrameWithCheck(firstTileX, firstTileY, lastTileX, lastTileY); - TimeLogger.DetailedDrawTime(5); - } - if (Main.sectionManager.AnyNeedRefresh) - { - WorldGen.RefreshSections(firstTileX, firstTileY, lastTileX, lastTileY); - } - } - - public void DrawTile_LiquidBehindTile(bool solidLayer, bool inFrontOfPlayers, int waterStyleOverride, Vector2 screenPosition, Vector2 screenOffset, int tileX, int tileY, Tile tileCache) - { - Tile tile = Main.tile[tileX + 1, tileY]; - Tile tile2 = Main.tile[tileX - 1, tileY]; - Tile tile3 = Main.tile[tileX, tileY - 1]; - Tile tile4 = Main.tile[tileX, tileY + 1]; - if ((tileCache.LiquidType != LiquidID.Lava && tile.LiquidType != LiquidID.Lava && tile2.LiquidType != LiquidID.Lava && tile3.LiquidType != LiquidID.Lava && tile4.LiquidType != LiquidID.Lava)) - { - return; - } - //bool[] _tileSolidTop = (bool[])typeof(TileDrawing).GetField("_tileSolidTop", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(null); - if (!tileCache.HasTile || tileCache.IsActuated /*|| _tileSolidTop[tileCache.TileType]*/ || (tileCache.IsHalfBlock && (tile2.LiquidAmount > 160 || tile.LiquidAmount > 160) && Main.instance.waterfallManager.CheckForWaterfall(tileX, tileY)) || (TileID.Sets.BlocksWaterDrawingBehindSelf[tileCache.TileType] && tileCache.Slope == 0)) - { - return; - } - int num = 0; - bool flag = false; - bool flag2 = false; - bool flag3 = false; - bool flag4 = false; - bool flag5 = false; - int num2 = 0; - bool flag6 = true; - int num3 = (int)tileCache.Slope; - int num4 = (int)tileCache.BlockType; - if (tileCache.TileType == 546 && tileCache.LiquidAmount > 0) - { - flag5 = true; - flag4 = true; - flag = true; - flag2 = true; - num = tileCache.LiquidAmount; - } - else - { - if (tileCache.LiquidAmount > 0 && num4 != 0 && (num4 != 1 || tileCache.LiquidAmount > 160)) - { - flag5 = true; - if (tileCache.LiquidAmount > num) - { - num = tileCache.LiquidAmount; - } - } - if (tile2.LiquidAmount > 0 && num3 != 1 && num3 != 3) - { - flag = true; - if (tile2.LiquidAmount > num) - { - num = tile2.LiquidAmount; - } - } - if (tile.LiquidAmount > 0 && num3 != 2 && num3 != 4) - { - flag2 = true; - if (tile.LiquidAmount > num) - { - num = tile.LiquidAmount; - } - } - if (tile3.LiquidAmount > 0 && num3 != 3 && num3 != 4) - { - flag3 = true; - } - if (tile4.LiquidAmount > 0 && num3 != 1 && num3 != 2) - { - if (tile4.LiquidAmount > 240) - { - flag4 = true; - } - } - } - if (!flag3 && !flag4 && !flag && !flag2 && !flag5) - { - return; - } - if (waterStyleOverride != -1) - { - LavaStyle = waterStyleOverride; - } - if (num2 == 0) - { - num2 = LavaStyle; - } - Lighting.GetCornerColors(tileX, tileY, out var vertices); - Vector2 vector = new((float)(tileX * 16), (float)(tileY * 16)); - Rectangle liquidSize = new(0, 4, 16, 16); - if (flag4 && (flag || flag2)) - { - flag = true; - flag2 = true; - } - if (tileCache.HasTile && (Main.tileSolidTop[tileCache.TileType] || !Main.tileSolid[tileCache.TileType])) - { - return; - } - if ((!flag3 || !(flag || flag2)) && !(flag4 && flag3)) - { - if (flag3) - { - liquidSize = new(0, 4, 16, 4); - if (tileCache.IsHalfBlock || tileCache.Slope != 0) - { - liquidSize = new(0, 4, 16, 12); - } - } - else if (flag4 && !flag && !flag2) - { - vector = new((float)(tileX * 16), (float)(tileY * 16 + 12)); - liquidSize = new(0, 4, 16, 4); - } - else - { - float num8 = (float)(256 - num) / 32f; - int y = 4; - if (tile3.LiquidAmount == 0 && (num4 != 0 || !WorldGen.SolidTile(tileX, tileY - 1))) - { - y = 0; - } - int num5 = (int)num8 * 2; - if (tileCache.Slope != 0) - { - vector = new((float)(tileX * 16), (float)(tileY * 16 + num5)); - liquidSize = new(0, num5, 16, 16 - num5); - } - else if ((flag && flag2) || tileCache.IsHalfBlock) - { - vector = new((float)(tileX * 16), (float)(tileY * 16 + num5)); - liquidSize = new(0, y, 16, 16 - num5); - } - else if (flag) - { - vector = new((float)(tileX * 16), (float)(tileY * 16 + num5)); - liquidSize = new(0, y, 4, 16 - num5); - } - else - { - vector = new((float)(tileX * 16 + 12), (float)(tileY * 16 + num5)); - liquidSize = new(0, y, 4, 16 - num5); - } - } - } - Vector2 position = vector - screenPosition + screenOffset; - float num6 = 1f; - if ((double)tileY <= Main.worldSurface || num6 > 1f) - { - num6 = 1f; - if (tileCache.WallType == 21) - { - num6 = 0.9f; - } - else if (tileCache.WallType > 0) - { - num6 = 0.6f; - } - } - if (tileCache.IsHalfBlock && tile3.LiquidAmount > 0 && tileCache.WallType > 0) - { - num6 = 0f; - } - if (num3 == 4 && tile2.LiquidAmount == 0 && !WorldGen.SolidTile(tileX - 1, tileY)) - { - num6 = 0f; - } - if (num3 == 3 && tile.LiquidAmount == 0 && !WorldGen.SolidTile(tileX + 1, tileY)) - { - num6 = 0f; - } - ref Color bottomLeftColor = ref vertices.BottomLeftColor; - bottomLeftColor *= num6; - ref Color bottomRightColor = ref vertices.BottomRightColor; - bottomRightColor *= num6; - ref Color topLeftColor = ref vertices.TopLeftColor; - topLeftColor *= num6; - ref Color topRightColor = ref vertices.TopRightColor; - topRightColor *= num6; - bool flag7 = false; - if (flag6) - { - for (int i = 0; i < LavaStylesLoader.TotalCount; i++) - { - if (LavaAlpha[i] > 0f && i != num2) - { - DrawPartialLiquid(!solidLayer, tileCache, ref position, ref liquidSize, i, ref vertices); - flag7 = true; - break; - } - } - } - VertexColors colors = vertices; - float num7 = (flag7 ? LavaAlpha[num2] : 1f); - ref Color bottomLeftColor2 = ref colors.BottomLeftColor; - bottomLeftColor2 *= num7; - ref Color bottomRightColor2 = ref colors.BottomRightColor; - bottomRightColor2 *= num7; - ref Color topLeftColor2 = ref colors.TopLeftColor; - topLeftColor2 *= num7; - ref Color topRightColor2 = ref colors.TopRightColor; - topRightColor2 *= num7; - DrawPartialLiquid(!solidLayer, tileCache, ref position, ref liquidSize, num2, ref colors); - } - - private void DrawPartialLiquid(bool behindBlocks, Tile tileCache, ref Vector2 position, ref Rectangle liquidSize, int liquidType, ref VertexColors colors) - { - int num = (int)tileCache.Slope; - bool flag = !TileID.Sets.BlocksWaterDrawingBehindSelf[tileCache.TileType]; - if (!behindBlocks) - { - flag = false; - } - if (flag || num == 0) - { - Main.tileBatch.Draw(Textures.block[liquidType].Value, position, liquidSize, colors, default(Vector2), 1f, (SpriteEffects)0); - return; - } - liquidSize.X += 18 * (num - 1); - switch (num) - { - case 1: - Main.tileBatch.Draw(Textures.slope[liquidType].Value, position, liquidSize, colors, Vector2.Zero, 1f, (SpriteEffects)0); - break; - case 2: - Main.tileBatch.Draw(Textures.slope[liquidType].Value, position, liquidSize, colors, Vector2.Zero, 1f, (SpriteEffects)0); - break; - case 3: - Main.tileBatch.Draw(Textures.slope[liquidType].Value, position, liquidSize, colors, Vector2.Zero, 1f, (SpriteEffects)0); - break; - case 4: - Main.tileBatch.Draw(Textures.slope[liquidType].Value, position, liquidSize, colors, Vector2.Zero, 1f, (SpriteEffects)0); - break; - } - } - - public void InitialDrawLavafall(WaterfallManager waterfallManager) - { - for (int i = 0; i < LavaStylesLoader.TotalCount; i++) - { - if (LavaAlpha[i] > 0f) - { - DrawLavafall(waterfallManager, i, LavaAlpha[i]); - } - } - } - - internal void DrawLavafall(WaterfallManager waterfallManager, int Style = 0, float Alpha = 1f) - { - int waterfallDist = (int)typeof(WaterfallManager).GetField("waterfallDist", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - int rainFrameForeground = (int)typeof(WaterfallManager).GetField("rainFrameForeground", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - int rainFrameBackground = (int)typeof(WaterfallManager).GetField("rainFrameBackground", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - int snowFrameForeground = (int)typeof(WaterfallManager).GetField("snowFrameForeground", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - WaterfallData[] waterfalls = (WaterfallData[])typeof(WaterfallManager).GetField("waterfalls", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - int currentMax = (int)typeof(WaterfallManager).GetField("currentMax", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - int slowFrame = (int)typeof(WaterfallManager).GetField("slowFrame", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance).GetValue(waterfallManager); - Main.tileSolid[546] = false; - float num = 0f; - float num12 = 99999f; - float num23 = 99999f; - int num34 = -1; - int num45 = -1; - float num47 = 0f; - int num50 = -1; - int num2 = -1; - for (int i = 0; i < currentMax; i++) - { - if (waterfalls[i].type != 1) - { - continue; - } - int num3 = 0; - int num4 = Style; - int num5 = waterfalls[i].x; - int num6 = waterfalls[i].y; - int num7 = 0; - int num8 = 0; - int num9 = 0; - int num10 = 0; - int num11 = 0; - int num13 = 0; - int num14; - int num15; - if (waterfalls[i].stopAtStep == 0) - { - continue; - } - num14 = 32 * slowFrame; - int num22 = 0; - num15 = waterfallDist; - Color color4 = Color.White; - for (int k = 0; k < num15; k++) - { - if (num22 >= 2) - { - break; - } - typeof(WaterfallManager).GetMethod("AddLight", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Invoke(null, new object[] { 1, num5, num6 }); - Tile tile3 = Main.tile[num5, num6]; - if (tile3.HasUnactuatedTile && Main.tileSolid[tile3.TileType] && !Main.tileSolidTop[tile3.TileType] && !TileID.Sets.Platforms[tile3.TileType] && tile3.BlockType == 0) - { - break; - } - Tile tile4 = Main.tile[num5 - 1, num6]; - Tile tile5 = Main.tile[num5, num6 + 1]; - Tile tile6 = Main.tile[num5 + 1, num6]; - if (WorldGen.SolidTile(tile5) && !tile3.IsHalfBlock) - { - num3 = 8; - } - else if (num8 != 0) - { - num3 = 0; - } - int num24 = 0; - int num25 = num10; - int num26 = 0; - int num27 = 0; - bool flag2 = false; - if (tile5.TopSlope && !tile3.IsHalfBlock && tile5.TileType != 19) - { - flag2 = true; - if (tile5.Slope == (SlopeType)1) - { - num24 = 1; - num26 = 1; - num9 = 1; - num10 = num9; - } - else - { - num24 = -1; - num26 = -1; - num9 = -1; - num10 = num9; - } - num27 = 1; - } - else if ((!WorldGen.SolidTile(tile5) && !tile5.BottomSlope && !tile3.IsHalfBlock) || (!tile5.HasTile && !tile3.IsHalfBlock)) - { - num22 = 0; - num27 = 1; - num26 = 0; - } - else if ((WorldGen.SolidTile(tile4) || tile4.TopSlope || tile4.LiquidAmount > 0) && !WorldGen.SolidTile(tile6) && tile6.LiquidAmount == 0) - { - if (num9 == -1) - { - num22++; - } - num26 = 1; - num27 = 0; - num9 = 1; - } - else if ((WorldGen.SolidTile(tile6) || tile6.TopSlope || tile6.LiquidAmount > 0) && !WorldGen.SolidTile(tile4) && tile4.LiquidAmount == 0) - { - if (num9 == 1) - { - num22++; - } - num26 = -1; - num27 = 0; - num9 = -1; - } - else if (((!WorldGen.SolidTile(tile6) && !tile3.TopSlope) || tile6.LiquidAmount == 0) && !WorldGen.SolidTile(tile4) && !tile3.TopSlope && tile4.LiquidAmount == 0) - { - num27 = 0; - num26 = num9; - } - else - { - num22++; - num27 = 0; - num26 = 0; - } - if (num22 >= 2) - { - num9 *= -1; - num26 *= -1; - } - Color color5 = Lighting.GetColor(num5, num6); - if (k > 50) - { - typeof(WaterfallManager).GetMethod("TrySparkling", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Invoke(null, new object[] { num5, num6, num9, color5 }); - } - float alpha = GetLavafallAlpha(Alpha, num15, num6, k, tile3); - color5 = (Color)typeof(WaterfallManager).GetMethod("StylizeColor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Invoke(null, new object[] { alpha, num15, 1, num6, k, tile3, color5 }); - float num33 = Math.Abs((float)(num5 * 16 + 8) - (Main.screenPosition.X + (float)(Main.screenWidth / 2))); - float num35 = Math.Abs((float)(num6 * 16 + 8) - (Main.screenPosition.Y + (float)(Main.screenHeight / 2))); - if (num33 < (float)(Main.screenWidth * 2) && num35 < (float)(Main.screenHeight * 2)) - { - float num36 = (float)Math.Sqrt(num33 * num33 + num35 * num35); - float num37 = 1f - num36 / ((float)Main.screenWidth * 0.75f); - if (num37 > 0f) - { - num += num37; - } - } - if (num33 < num12) - { - num12 = num33; - num34 = num5 * 16 + 8; - } - if (num35 < num23) - { - num23 = num33; - num45 = num6 * 16 + 8; - } - int num38 = tile3.LiquidAmount / 16; - if (flag2 && num9 != num25) - { - int num39 = 2; - if (num25 == 1) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16 + 16 - num39)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38 - num39), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + 16 - num39)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38 - num39), (SpriteEffects)0); - } - } - if (num7 == 0 && num24 != 0 && num8 == 1 && num9 != num10) - { - num24 = 0; - num9 = num10; - color5 = Color.White; - if (num9 == 1) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16 + 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16 + 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - } - if (num11 != 0 && num26 == 0 && num27 == 1) - { - if (num9 == 1) - { - if (num13 != num4) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3 + 8)) - Main.screenPosition, new Rectangle(num14, 0, 16, 16 - num38 - 8), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3 + 8)) - Main.screenPosition, new Rectangle(num14, 0, 16, 16 - num38 - 8), (SpriteEffects)1); - } - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3 + 8)) - Main.screenPosition, new Rectangle(num14, 0, 16, 16 - num38 - 8), (SpriteEffects)0); - } - } - if (num3 == 8 && num8 == 1 && num11 == 0) - { - if (num10 == -1) - { - if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 8), (SpriteEffects)0); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 8), (SpriteEffects)0); - } - } - else if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 8), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 8), (SpriteEffects)1); - } - } - if (num24 != 0 && num7 == 0) - { - if (num25 == 1) - { - if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - } - else if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)0); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)0); - } - } - if (num27 == 1 && num24 == 0 && num11 == 0) - { - if (num9 == -1) - { - if (num8 == 0) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3)) - Main.screenPosition, new Rectangle(num14, 0, 16, 16 - num38), (SpriteEffects)0); - } - else if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)0); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)0); - } - } - else if (num8 == 0) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3)) - Main.screenPosition, new Rectangle(num14, 0, 16, 16 - num38), (SpriteEffects)1); - } - else if (num13 != num4) - { - DrawLavafall(num13, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - else - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 - 16), (float)(num6 * 16)) - Main.screenPosition, new Rectangle(num14, 24, 32, 16 - num38), (SpriteEffects)1); - } - } - else - { - switch (num26) - { - case 1: - if (Main.tile[num5, num6].LiquidAmount > 0 && !Main.tile[num5, num6].IsHalfBlock) - { - break; - } - if (num24 == 1) - { - for (int m = 0; m < 8; m++) - { - int num43 = m * 2; - int num44 = 14 - m * 2; - int num46 = num43; - num3 = 8; - if (num7 == 0 && m < 2) - { - num46 = 4; - } - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 + num43), (float)(num6 * 16 + num3 + num46)) - Main.screenPosition, new Rectangle(16 + num14 + num44, 0, 2, 16 - num3), (SpriteEffects)1); - } - } - else - { - int height2 = 16; - if (TileID.Sets.BlocksWaterDrawingBehindSelf[Main.tile[num5, num6].TileType]) - { - height2 = 8; - } - else if (TileID.Sets.BlocksWaterDrawingBehindSelf[Main.tile[num5, num6 + 1].TileType]) - { - height2 = 8; - } - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3)) - Main.screenPosition, new Rectangle(16 + num14, 0, 16, height2), (SpriteEffects)1); - } - break; - case -1: - if (Main.tile[num5, num6].LiquidAmount > 0 && !Main.tile[num5, num6].IsHalfBlock) - { - break; - } - if (num24 == -1) - { - for (int l = 0; l < 8; l++) - { - int num40 = l * 2; - int num41 = l * 2; - int num42 = 14 - l * 2; - num3 = 8; - if (num7 == 0 && l > 5) - { - num42 = 4; - } - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16 + num40), (float)(num6 * 16 + num3 + num42)) - Main.screenPosition, new Rectangle(16 + num14 + num41, 0, 2, 16 - num3), (SpriteEffects)1); - } - } - else - { - int height = 16; - if (TileID.Sets.BlocksWaterDrawingBehindSelf[Main.tile[num5, num6].TileType]) - { - height = 8; - } - else if (TileID.Sets.BlocksWaterDrawingBehindSelf[Main.tile[num5, num6 + 1].TileType]) - { - height = 8; - } - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3)) - Main.screenPosition, new Rectangle(16 + num14, 0, 16, height), (SpriteEffects)0); - } - break; - case 0: - if (num27 == 0) - { - if (Main.tile[num5, num6].LiquidAmount <= 0 || Main.tile[num5, num6].IsHalfBlock) - { - DrawLavafall(num4, num5, num6, alpha, new Vector2((float)(num5 * 16), (float)(num6 * 16 + num3)) - Main.screenPosition, new Rectangle(16 + num14, 0, 16, 16), (SpriteEffects)0); - } - k = 1000; - } - break; - } - } - if (tile3.LiquidAmount > 0 && !tile3.IsHalfBlock) - { - k = 1000; - } - num8 = num27; - num10 = num9; - num7 = num26; - num5 += num26; - num6 += num27; - num11 = num24; - color4 = color5; - if (num13 != num4) - { - num13 = num4; - } - if ((tile4.HasTile && (tile4.TileType == 189 || tile4.TileType == 196)) || (tile6.HasTile && (tile6.TileType == 189 || tile6.TileType == 196)) || (tile5.HasTile && (tile5.TileType == 189 || tile5.TileType == 196))) - { - num15 = (int)(40f * ((float)Main.maxTilesX / 4200f) * Main.gfxQuality); - } - } - } - Main.ambientWaterfallX = num34; - Main.ambientWaterfallY = num45; - Main.ambientWaterfallStrength = num; - Main.ambientLavafallX = num50; - Main.ambientLavafallY = num2; - Main.ambientLavafallStrength = num47; - Main.tileSolid[546] = true; - } - - private void DrawLavafall(int waterfallType, int x, int y, float opacity, Vector2 position, Rectangle sourceRect, SpriteEffects effects) - { - Texture2D value = Textures.fall[waterfallType].Value; - Main.spriteBatch.Draw(value, position, (Rectangle?)sourceRect, Lighting.GetColor(x, y) * opacity, 0f, default, 1f, effects, 0f); - } - - private static float GetLavafallAlpha(float Alpha, int maxSteps, int y, int s, Tile tileCache) - { - float num = (tileCache.WallType != 0 || !((double)y < Main.worldSurface)) ? (1f * Alpha) : Alpha; - if (s > maxSteps - 10) - { - num *= (float)(maxSteps - s) / 10f; - } - return num; - } - - public static int dustLava() - { - if (LavaStyle >= 1) - { - return LavaStylesLoader.Get(LavaStyle).GetSplashDust(); - } - else - { - return DustID.Lava; - } - } - - //just better as a method rather than hardcoded type values - public static int goreLava() - { - if (LavaStyle >= 1) - { - return LavaStylesLoader.Get(LavaStyle).GetDropletGore(); - } - else - { - return GoreID.LavaDrip; - } - } - } -} diff --git a/Systems/Graphic/LiquidSystem/IEmittableWaterStyle.cs b/Systems/Graphic/LiquidSystem/IEmittableWaterStyle.cs new file mode 100644 index 0000000000..fe39f6dae0 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/IEmittableWaterStyle.cs @@ -0,0 +1,9 @@ +using Terraria; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + public interface IEmittableWaterStyle + { + void ModifyLight(ref readonly Tile tile, int i, int j, int type, ref float r, ref float g, ref float b); + } +} diff --git a/Systems/Graphic/LiquidSystem/IPaintableWaterStyle.cs b/Systems/Graphic/LiquidSystem/IPaintableWaterStyle.cs new file mode 100644 index 0000000000..c933759fe4 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/IPaintableWaterStyle.cs @@ -0,0 +1,9 @@ +using Terraria.Graphics; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + public interface IPaintableWaterStyle + { + void DrawColor(int x, int y, int type, ref VertexColors liquidColor, bool isSlope = false); + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleLoader.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleLoader.cs new file mode 100644 index 0000000000..910366a695 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleLoader.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + public class ModLavaStyleLoader : ModSystem + { + private static readonly List ModStyleList = []; + + public static IReadOnlyList AllStyles => ModStyleList; + + + public const int VanillaCount = 1; + public static int ModCount { get; private set; } = 0; + public static int TotalCount => ModCount + VanillaCount; + + internal static int Register(ModLavaStyle instance) + { + int type = TotalCount; + ModTypeLookup.Register(instance); + ModStyleList.Add(instance); + ModCount = ModStyleList.Count; + return type; + } + + public override void Unload() + { + ModStyleList.Clear(); + } + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs new file mode 100644 index 0000000000..be4713e5fe --- /dev/null +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem.cs @@ -0,0 +1,116 @@ +using System; +using System.Reflection; +using Microsoft.Xna.Framework.Graphics; +using ReLogic.Content; +using Terraria; +using Terraria.GameContent; +using Terraria.GameContent.Liquid; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + [Autoload(Side = ModSide.Client)] + public sealed partial class ModLavaStyleSystem : ModSystem + { + internal static readonly FastField[]> WaterfallTextureField = new("waterfallTexture", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + + public static ModLavaStyle[] LavaStyles = new ModLavaStyle[ModLavaStyleLoader.VanillaCount]; + public static Asset[] Textures = new Asset[ModLavaStyleLoader.VanillaCount]; + public static Asset[] BlockTextures = new Asset[ModLavaStyleLoader.VanillaCount]; + public static Asset[] SlopeTextures = new Asset[ModLavaStyleLoader.VanillaCount]; + public static Asset[] WaterfallTextures = new Asset[ModLavaStyleLoader.VanillaCount]; + public static float[] LavaAlpha = new float[ModLavaStyleLoader.VanillaCount]; + + public static int LavaStyle = 2; + + public static bool Initialized { get; private set; } = false; + public static bool TextureArrayReady { get; private set; } = false; + + public override void ResizeArrays() + { + var modLavaStyles = ModLavaStyleLoader.AllStyles; + var totalCount = ModLavaStyleLoader.TotalCount; + + Array.Resize(ref LavaStyles, totalCount); + Array.Resize(ref Textures, totalCount); + Array.Resize(ref BlockTextures, totalCount); + Array.Resize(ref SlopeTextures, totalCount); + Array.Resize(ref WaterfallTextures, totalCount); + Array.Resize(ref LavaAlpha, totalCount); + + foreach (var modLavaStyle in modLavaStyles) + { + var slot = modLavaStyle.Slot; + LavaStyles[slot] = modLavaStyle; + Textures[slot] = ModContent.Request(modLavaStyle.Texture); + BlockTextures[slot] = ModContent.Request(modLavaStyle.BlockTexture); + SlopeTextures[slot] = ModContent.Request(modLavaStyle.SlopeTexture); + WaterfallTextures[slot] = ModContent.Request(modLavaStyle.WaterfallTexture); + } + + LavaAlpha[0] = 1.0f; // Setting Vanilla Lava to Full Alpha + Textures[0] = LiquidRenderer.Instance._liquidTextures[1]; + SlopeTextures[0] = TextureAssets.LiquidSlope[1]; + BlockTextures[0] = TextureAssets.Liquid[1]; + WaterfallTextures[0] = WaterfallTextureField.Get(Main.instance.waterfallManager)[1]; + + TextureArrayReady = true; + } + + public override void OnModLoad() + { + if (ExternalMods.biomeLava == null) + { + LoadILEdits(); + Main.QueueMainThreadAction(PrepareRT); + Main.OnPreDraw += UpdateRT; + Initialized = true; + } + } + + public override void OnModUnload() + { + if (Initialized) + { + Main.QueueMainThreadAction(DisposeRT); + Main.OnPreDraw -= UpdateRT; + Initialized = false; + } + + TextureArrayReady = false; + } + + public override void PreUpdatePlayers() + { + LavaStyle = 0; + foreach (var lavaStyle in ModLavaStyleLoader.AllStyles) + { + bool? flag = lavaStyle?.IsLavaActive(); + if (flag != null && flag == true) + { + LavaStyle = lavaStyle.Slot; + } + } + + for (int type = 0; type < ModLavaStyleLoader.TotalCount; type++) + { + if (LavaStyle == type) + { + LavaAlpha[type] += 0.2f; + if (LavaAlpha[type] > 1f) + { + LavaAlpha[type] = 1f; + } + } + else + { + LavaAlpha[type] -= 0.2f; + if (LavaAlpha[type] < 0f) + { + LavaAlpha[type] = 0f; + } + } + } + } + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_API.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_API.cs new file mode 100644 index 0000000000..3e118184b8 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_API.cs @@ -0,0 +1,57 @@ +using Terraria; +using Terraria.Graphics; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + [Autoload(Side = ModSide.Client)] + public sealed partial class ModLavaStyleSystem : ModSystem + { + public static ModLavaStyle CurrentLavaStyle => LavaStyles[LavaStyle]; + + public static void ModifyLightSetup(int i, int j, int style, ref float r, ref float g, ref float b) + { + if (LavaStyles[style] is ModLavaStyle styles) + { + styles.ModifyLight(i, j, ref r, ref g, ref b); + } + } + + public static void DrawColorSetup(int x, int y, int type, ref VertexColors liquidColor, bool isSlope = false) + { + if (LavaStyles[type] is ModLavaStyle styles) + { + styles.DrawColor(x, y, ref liquidColor, isSlope); + } + } + + public static int GetDropletGoreID(int oldID = -1) + { + if (CurrentLavaStyle is ModLavaStyle lavaStyle) + { + return lavaStyle.GetDropletGore(); + } + + return (oldID >= 0) ? oldID : GoreID.LavaDrip; + } + + public static int GetSplashDustID(int oldID = -1) + { + if (CurrentLavaStyle is ModLavaStyle lavaStyle) + { + return lavaStyle.GetSplashDust(); + } + + return (oldID >= 0) ? oldID : DustID.Lava; + } + + public static void InflictDebuff(Player player, int onFireTime) + { + if (CurrentLavaStyle is ModLavaStyle lavaStyle) + { + lavaStyle.InflictDebuff(player, onFireTime); + } + } + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs new file mode 100644 index 0000000000..2bbf344253 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_ILEdit.cs @@ -0,0 +1,378 @@ +using System; +using System.Reflection; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Mono.Cecil; +using Mono.Cecil.Cil; +using MonoMod.Cil; +using ReLogic.Content; +using Terraria; +using Terraria.GameContent; +using Terraria.GameContent.Drawing; +using Terraria.GameContent.Liquid; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + [Autoload(Side = ModSide.Client)] + public sealed partial class ModLavaStyleSystem : ModSystem + { + private static readonly MethodInfo Tex2DAssetGetter = typeof(Asset).GetProperty(nameof(Asset.Value)).GetMethod; + + private void LoadILEdits() + { + if (Tex2DAssetGetter == null) + { + CalamityMod.Log.ILFailure("ModLavaStyle", "Cannot find Getter for Asset::Value"); + return; + } + + // Graphic + IL_LiquidRenderer.DrawNormalLiquids += DrawNormalLiquidPatch; + IL_TileDrawing.DrawPartialLiquid += DrawPartialLiquidPatch; + IL_Main.oldDrawWater += DrawOldWaterPatch; + On_WaterfallManager.DrawWaterfall_int_int_int_float_Vector2_Rectangle_Color_SpriteEffects += DrawWaterfall; + On_WaterfallManager.AddLight += WaterfallAddLight; + On_WaterfallManager.StylizeColor += WaterfallGlowmaskEditor; + + // Mechanics + IL_LiquidRenderer.InternalPrepareDraw += LavaBubbleReplacer; + IL_TileDrawing.EmitLiquidDrops += LavaDropletReplacer; + IL_NPC.Collision_WaterCollision += SplashEntityLava; + IL_Projectile.Update += SplashEntityLava; + IL_Item.MoveInWorld += SplashEntityLava; + IL_Player.Update += SplashEntityLava; + IL_Player.Update += PlayerDebuffEdit; + } + + #region Drawing IL Edits + + private static void DrawNormalLiquidPatch(ILContext il) + { + const string PatchName = "ModLavaStyle::DrawNormalLiquid"; + + var cursor = new ILCursor(il); + + var patched = PatchNextGetTexture2DLdLoc(cursor, (textureIdxLocIdx, textureArrayField) => + { + cursor.EmitLdloc(textureIdxLocIdx); + cursor.EmitDelegate((Texture2D origTex, int textureIdx) => + { + return (textureIdx == LiquidID.Lava) ? LavaRT : origTex; + }); + }); + + if (!patched) + { + CalamityMod.Log.ILFailure(PatchName, "Unable to Locate Asset::Value call"); + } + } + + private static void DrawPartialLiquidPatch(ILContext il) + { + const string PatchName = "ModLavaStyle::DrawPartialLiquids"; + const int DesiredPatchCount = 5; + + var cursor = new ILCursor(il); + + int patchedCount = PatchMultiple(cursor, () => + { + return PatchNextGetTexture2DLdArg(cursor, (textureIdxArgIdx, textureArrayField) => + { + if (textureArrayField.Name == nameof(TextureAssets.Liquid)) + { + cursor.EmitLdarg(textureIdxArgIdx); + cursor.EmitDelegate((Texture2D origTex, int textureIdx) => + { + return (textureIdx == LiquidID.Lava) ? LavaBlockRT : origTex; + }); + } + else if (textureArrayField.Name == nameof(TextureAssets.LiquidSlope)) + { + cursor.EmitLdarg(textureIdxArgIdx); + cursor.EmitDelegate((Texture2D origTex, int textureIdx) => + { + return (textureIdx == LiquidID.Lava) ? LavaSlopeRT : origTex; + }); + } + else + { + CalamityMod.Log.ILFailure(PatchName, $"Texture Array We referencing is [{textureArrayField.Name}] Which is not intended. Skipping"); + } + }); + }); + + // Finalizing Patch + if (patchedCount <= 0) + { + CalamityMod.Log.ILFailure(PatchName, "Unable to patch any of the texture reference"); + return; + } + else + { + if (patchedCount != DesiredPatchCount) + { + CalamityMod.Log.Warn($"We did patched into {patchedCount} entry which is unmatching with desired count ({DesiredPatchCount}) when designed this iledit. Please check if anything is broken, If not update the count accordingly to suppress this message!"); + CalamityMod.Log.Warn($"Location: ModLavaStyleSystem_ILEdit.cs :: DrawPartialLiquidPatch"); + } + } + } + + private static void DrawOldWaterPatch(ILContext il) + { + const string PatchName = "ModLavaStyle::DrawOldWater"; + const int DesiredPatchCount = 10; + + var cursor = new ILCursor(il); + + int patchedCount = PatchMultiple(cursor, () => + { + return PatchNextGetTexture2DLdLoc(cursor, (textureIdxLocIdx, textureArrayField) => + { + if (textureArrayField.Name == nameof(TextureAssets.Liquid)) + { + cursor.EmitLdloc(textureIdxLocIdx); + cursor.EmitDelegate((Texture2D origTex, int textureIdx) => + { + return (textureIdx == LiquidID.Lava) ? LavaBlockRT : origTex; + }); + } + else if (textureArrayField.Name == nameof(TextureAssets.LiquidSlope)) + { + cursor.EmitLdloc(textureIdxLocIdx); + cursor.EmitDelegate((Texture2D origTex, int textureIdx) => + { + return (textureIdx == LiquidID.Lava) ? LavaSlopeRT : origTex; + }); + } + else + { + CalamityMod.Log.ILFailure(PatchName, $"Texture Array We referencing is [{textureArrayField.Name}] Which is not intended. Skipping"); + } + }); + }); + + // Finalizing Patch + if (patchedCount <= 0) + { + CalamityMod.Log.ILFailure(PatchName, "Unable to patch any of the texture reference"); + return; + } + else + { + if (patchedCount != DesiredPatchCount) + { + CalamityMod.Log.Warn($"We did patched into {patchedCount} entry which is unmatching with desired count ({DesiredPatchCount}) when designed this iledit. Please check if anything is broken, If not update the count accordingly to suppress this message!"); + CalamityMod.Log.Warn($"Patch Name: {PatchName}"); + } + } + } + + private static void DrawWaterfall(On_WaterfallManager.orig_DrawWaterfall_int_int_int_float_Vector2_Rectangle_Color_SpriteEffects orig, WaterfallManager self, int waterfallType, int x, int y, float opacity, Microsoft.Xna.Framework.Vector2 position, Microsoft.Xna.Framework.Rectangle sourceRect, Microsoft.Xna.Framework.Color color, SpriteEffects effects) + { + if (waterfallType == LiquidID.Lava) + { + Main.spriteBatch.Draw(LavaWaterfallRT, position, sourceRect, color, 0f, Vector2.Zero, 1f, effects, 0f); + } + else + { + orig(self, waterfallType, x, y, opacity, position, sourceRect, color, effects); + } + } + + private static void WaterfallAddLight(On_WaterfallManager.orig_AddLight orig, int waterfallType, int x, int y) + { + if (waterfallType == LiquidID.Lava && CurrentLavaStyle is ModLavaStyle lavaStyle) + { + float r = 0.55f; + float g = 0.33f; + float b = 0.11f; + lavaStyle.ModifyLight(x, y, ref r, ref g, ref b); + Lighting.AddLight(x, y, r, g, b); + return; + } + orig.Invoke(waterfallType, x, y); + } + + private static Color WaterfallGlowmaskEditor(On_WaterfallManager.orig_StylizeColor orig, float alpha, int maxSteps, int waterfallType, int y, int s, Tile tileCache, Color aColor) + { + if (waterfallType == LiquidID.Lava && !(CurrentLavaStyle?.LavafallGlowmask() ?? false)) + { + return aColor; + } + else + { + return orig.Invoke(alpha, maxSteps, waterfallType, y, s, tileCache, aColor); + } + } + + #endregion + + #region Mechanics IL Edits + private static void LavaBubbleReplacer(ILContext il) + { + const string PatchName = "Ambient lava bubble replacer"; + + ILCursor cursor = new ILCursor(il); + if (!cursor.TryGotoNext(MoveType.After, + i => i.MatchLdcI4(16), + i => i.MatchLdcI4(16), + i => i.MatchLdcI4(DustID.Lava))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the bubble newdust parameters"); + return; + } + + // int dustID + cursor.EmitDelegate>(GetSplashDustID); + + if (!cursor.TryGotoNext(MoveType.After, + i => i.MatchLdcI4(16), + i => i.MatchLdcI4(8), + i => i.MatchLdcI4(DustID.Lava))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the surface bubble newdust parameters"); + return; + } + + // int dustID + cursor.EmitDelegate>(GetSplashDustID); + } + + private static void LavaDropletReplacer(ILContext il) + { + const string PatchName = "Ambient lava droplet replacer"; + + ILCursor cursor = new ILCursor(il); + if (!cursor.TryGotoNext(MoveType.After, + i => i.MatchLdarg(out _), + i => i.MatchLdcI4(374), + i => i.MatchBneUn(out _), + i => i.MatchLdcI4(GoreID.LavaDrip))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the lava droplet newgore parameters"); + return; + } + + // int goreID + cursor.EmitDelegate>(GetDropletGoreID); + } + + private static void SplashEntityLava(ILContext il) + { + const string PatchName = "Entity Lava Splashing (Item, Projectile, NPC, Player)"; + + ILCursor cursor = new ILCursor(il); + + Func[] predicates = [ + i => i.MatchLdfld("width"), + i => i.MatchLdcI4(12), + i => i.MatchAdd(), + i => i.MatchLdcI4(24), + i => i.MatchLdcI4(DustID.Lava) + ]; + if (!cursor.TryGotoNext(MoveType.After, predicates)) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the first lava bubble splashing"); + return; + } + + // int dustID + cursor.EmitDelegate>(GetSplashDustID); + + if (!cursor.TryGotoNext(MoveType.After, + i => i.MatchLdfld("width"), + i => i.MatchLdcI4(12), + i => i.MatchAdd(), + i => i.MatchLdcI4(24), + i => i.MatchLdcI4(DustID.Lava))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the second lava bubble splashing"); + return; + } + + // int dustID + cursor.EmitDelegate>(GetSplashDustID); + } + + private static void PlayerDebuffEdit(ILContext il) + { + const string PatchName = "Player Update Lava Debuff"; + + //Injects code directly at the position where the OnFire debuff is handled + ILCursor cursor = new ILCursor(il); + + if (!cursor.TryGotoNext(MoveType.Before, + i => i.MatchLdarg0(), + i => i.MatchLdcI4(24), + i => i.MatchLdloc(161), + i => i.MatchLdcI4(1), + i => i.MatchLdcI4(0), + i => i.MatchCall(nameof(Player.AddBuff)))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the infliction of the On Fire! debuff inside the Player Update code"); + return; + } + cursor.EmitLdarg0(); // Player player + cursor.EmitLdloc(161); // int OnFireTime + cursor.EmitDelegate(InflictDebuff); + } + #endregion + + #region Patch Utils + + private static int PatchMultiple(ILCursor cursor, Func patch) + { + int patchedCount = 0; + while (true) + { + var patched = patch.Invoke(); + + if (patched) + patchedCount++; + else + break; + } + + return patchedCount; + } + + private static bool PatchNextGetTexture2DLdArg(ILCursor cursor, Action patcher) + { + int textureArgIdx = 0; + FieldReference textureArrayField = null; + if (cursor.TryGotoNext(MoveType.After, + x => x.MatchLdsfld(out textureArrayField) || x.MatchLdfld(out textureArrayField), + x => x.MatchLdarg(out textureArgIdx), + x => x.MatchLdelemRef(), + x => x.MatchCallOrCallvirt(Tex2DAssetGetter))) + { + patcher.Invoke(textureArgIdx, textureArrayField); + return true; + } + + return false; + } + + private static bool PatchNextGetTexture2DLdLoc(ILCursor cursor, Action patcher) + { + int textureLocalIdx = 0; + FieldReference textureArrayField = null; + if (cursor.TryGotoNext(MoveType.After, + x => x.MatchLdsfld(out textureArrayField) || x.MatchLdfld(out textureArrayField), + x => x.MatchLdloc(out textureLocalIdx), + x => x.MatchLdelemRef(), + x => x.MatchCallOrCallvirt(Tex2DAssetGetter))) + { + patcher.Invoke(textureLocalIdx, textureArrayField); + return true; + } + + return false; + } + + #endregion + + } +} diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs new file mode 100644 index 0000000000..c7224286bc --- /dev/null +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs @@ -0,0 +1,102 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ReLogic.Content; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + [Autoload(Side = ModSide.Client)] + public sealed partial class ModLavaStyleSystem : ModSystem + { + public static RenderTarget2D LavaRT { get; private set; } + public static RenderTarget2D LavaBlockRT { get; private set; } + public static RenderTarget2D LavaSlopeRT { get; private set; } + public static RenderTarget2D LavaWaterfallRT { get; private set; } + + public const int TextureWidth = 48; + public const int TextureHeight = 1360; + + public const int BlockTextureWidth = 16; + public const int BlockTextureHeight = 16; + + public const int SlopeTextureWidth = 72; + public const int SlopeTextureHeight = 16; + + public const int WaterfallTextureWidth = 512; + public const int WaterfallTextureHeight = 40; + + private static void PrepareRT() + { + LavaRT = CreateRT(TextureWidth, TextureHeight); + LavaBlockRT = CreateRT(BlockTextureWidth, BlockTextureHeight); + LavaSlopeRT = CreateRT(SlopeTextureWidth, SlopeTextureHeight); + LavaWaterfallRT = CreateRT(WaterfallTextureWidth, WaterfallTextureHeight); + } + + private static RenderTarget2D CreateRT(int width, int height) + { + return new(Main.instance.GraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents); + } + + private static void DisposeRT() + { + LavaRT?.Dispose(); + LavaBlockRT?.Dispose(); + LavaSlopeRT?.Dispose(); + LavaWaterfallRT?.Dispose(); + } + + private static void UpdateRT(GameTime time) + { + if (!Initialized || !TextureArrayReady) + return; + + var graphicsDevice = Main.instance.GraphicsDevice; + graphicsDevice.SetRenderTarget(LavaRT); + graphicsDevice.Clear(Color.Transparent); + Begin(); + DrawTextures(Textures, TextureWidth, TextureHeight); + End(); + + graphicsDevice.SetRenderTarget(LavaBlockRT); + graphicsDevice.Clear(Color.Transparent); + Begin(); + DrawTextures(BlockTextures, BlockTextureWidth, BlockTextureHeight); + End(); + + graphicsDevice.SetRenderTarget(LavaSlopeRT); + graphicsDevice.Clear(Color.Transparent); + Begin(); + DrawTextures(SlopeTextures, SlopeTextureWidth, SlopeTextureHeight); + End(); + + graphicsDevice.SetRenderTarget(LavaWaterfallRT); + graphicsDevice.Clear(Color.Transparent); + Begin(); + DrawTextures(WaterfallTextures, WaterfallTextureWidth, WaterfallTextureHeight); + End(); + + graphicsDevice.SetRenderTarget(null); + } + + private static void Begin() + { + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.Identity); + } + + private static void End() + { + Main.spriteBatch.End(); + } + + private static void DrawTextures(Asset[] textures, int width, int height) + { + var totalCount = ModLavaStyleLoader.TotalCount; + for (int i = 0; i < totalCount; i++) + { + Main.spriteBatch.Draw(textures[i].Value, Vector2.Zero, null, Color.White * LavaAlpha[i]); + } + } + } +} diff --git a/Systems/Graphic/LiquidSystem/SpecialLiquidDrawingSystem.cs b/Systems/Graphic/LiquidSystem/SpecialLiquidDrawingSystem.cs new file mode 100644 index 0000000000..11c069b828 --- /dev/null +++ b/Systems/Graphic/LiquidSystem/SpecialLiquidDrawingSystem.cs @@ -0,0 +1,195 @@ +using System; +using Microsoft.Xna.Framework; +using MonoMod.Cil; +using Terraria; +using Terraria.GameContent.Drawing; +using Terraria.GameContent.Liquid; +using Terraria.Graphics; +using Terraria.Graphics.Light; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.LiquidSystem +{ + [Autoload(Side = ModSide.Client)] + public sealed class SpecialLiquidDrawingSystem : ModSystem + { + public override void OnModLoad() + { + On_TileLightScanner.GetTileLight += ApplyLiquidEmit; + IL_LiquidRenderer.DrawNormalLiquids += LiquidDrawColors; //Liquid Light + On_TileDrawing.DrawPartialLiquid += LiquidSlopeDrawColors; + } + + private static void ModifyEmit(Tile tile, int x, int y, ref Vector3 lightColor) + { + if (tile.HasTile || tile.LiquidAmount <= 0) + return; + + if (tile.LiquidType == LiquidID.Water && TryGetModWaterStyleAs(Main.waterStyle, out var waterStyle)) + { + float R = 0f; + float G = 0f; + float B = 0f; + + waterStyle.ModifyLight(in tile, x, y, Main.waterStyle, ref R, ref G, ref B); + + lightColor.X = Math.Max(lightColor.X, R); + lightColor.Y = Math.Max(lightColor.Y, G); + lightColor.Z = Math.Max(lightColor.Z, B); + } + else if (tile.LiquidType == LiquidID.Lava && ModLavaStyleSystem.Initialized) + { + Vector3 lavaLight = new Vector3(0.55f, 0.33f, 0.11f); + + float R = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.X : 0f; + float G = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.Y : 0f; + float B = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.Z : 0f; + ModLavaStyleSystem.ModifyLightSetup(x, y, ModLavaStyleSystem.LavaStyle, ref R, ref G, ref B); + + for (int styleIndex = 0; styleIndex < ModLavaStyleLoader.TotalCount; styleIndex++) + { + if (ModLavaStyleSystem.LavaAlpha[styleIndex] > 0f && styleIndex != ModLavaStyleSystem.LavaStyle) + { + float r = styleIndex == 0 ? lavaLight.X : 0f; + float g = styleIndex == 0 ? lavaLight.Y : 0f; + float b = styleIndex == 0 ? lavaLight.Z : 0f; + ModLavaStyleSystem.ModifyLightSetup(x, y, styleIndex, ref r, ref g, ref b); + + float r2 = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.X : 0f; + float g2 = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.Y : 0f; + float b2 = ModLavaStyleSystem.LavaStyle == 0 ? lavaLight.Z : 0f; + ModLavaStyleSystem.ModifyLightSetup(x, y, ModLavaStyleSystem.LavaStyle, ref r2, ref g2, ref b2); + + R = float.Lerp(r, r2, ModLavaStyleSystem.LavaAlpha[ModLavaStyleSystem.LavaStyle]); + G = float.Lerp(g, g2, ModLavaStyleSystem.LavaAlpha[ModLavaStyleSystem.LavaStyle]); + B = float.Lerp(b, b2, ModLavaStyleSystem.LavaAlpha[ModLavaStyleSystem.LavaStyle]); + } + } + + if (R != 0.0f || G != 0.0f || B != 0.0f) + { + float colorManipulator = (float)(270 - Main.mouseTextColor) / 900f; + R += colorManipulator; + G += colorManipulator; + B += colorManipulator; + } + + lightColor.X = Math.Max(lightColor.X, R); + lightColor.Y = Math.Max(lightColor.Y, G); + lightColor.Z = Math.Max(lightColor.Z, B); + } + } + + private static void ModifyColor(int x, int y, byte liquidType, ref VertexColors initialColor, bool isSlope = false) + { + if (liquidType == LiquidID.Water && TryGetModWaterStyleAs(Main.waterStyle, out var waterStyle)) + { + waterStyle.DrawColor(x, y, Main.waterStyle, ref initialColor, isSlope); + } + else if (liquidType == LiquidID.Lava && ModLavaStyleSystem.Initialized) + { + ModLavaStyleSystem.DrawColorSetup(x, y, ModLavaStyleSystem.LavaStyle, ref initialColor, isSlope); + } + } + + #region IL Edits + + private static void ApplyLiquidEmit(On_TileLightScanner.orig_GetTileLight orig, TileLightScanner self, int x, int y, out Vector3 outputColor) + { + orig(self, x, y, out outputColor); + ModifyEmit(Main.tile[x, y], x, y, ref outputColor); + } + + private static void LiquidDrawColors(ILContext il) + { + const string PatchName = "Liquid Draw Colors"; + + ILCursor cursor = new ILCursor(il); + + var typeField = typeof(LiquidRenderer.LiquidDrawCache).GetField(nameof(LiquidRenderer.LiquidDrawCache.Type)); + if (typeField == null) + { + CalamityMod.Log.ILFailure(PatchName, "Could not find FieldInfo for LiquidDrawCache.Type"); + return; + } + + var lightingGetCornerColorsMethod = typeof(Lighting).GetMethod(nameof(Lighting.GetCornerColors)); + if (lightingGetCornerColorsMethod == null) + { + CalamityMod.Log.ILFailure(PatchName, "Could not find MethodInfo for GetCornerColors"); + return; + } + + var mainDrawTileInWaterMethod = typeof(Main).GetMethod(nameof(Main.DrawTileInWater)); + if (mainDrawTileInWaterMethod == null) + { + CalamityMod.Log.ILFailure(PatchName, "Could not find FieldInfo for DrawTileInWater"); + return; + } + + int typeLocalIdx = 0; + if (!cursor.TryGotoNext(MoveType.Before, + c => c.MatchLdloc(out _), // This is the local index for LiquidDrawCache*. Use it if you want for future + c => c.MatchLdfld(typeField), + c => c.MatchStloc(out typeLocalIdx))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the local index for Liquid Type"); + return; + } + + int vertexColorLocalIdx = 0; + if (!cursor.TryGotoNext(MoveType.Before, + c => c.MatchLdloca(out vertexColorLocalIdx), + c => c.MatchLdcR4(out _), + c => c.MatchCallOrCallvirt(lightingGetCornerColorsMethod))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the local index for VertexColors"); + return; + } + + int xLocalIdx = 0; + int yLocalIdx = 0; + if (!cursor.TryGotoNext(MoveType.Before, + c => c.MatchLdarg(out _), // Vector2 drawOffset + c => c.MatchLdloc(out xLocalIdx), + c => c.MatchLdloc(out yLocalIdx), + c => c.MatchCallOrCallvirt(mainDrawTileInWaterMethod))) + { + CalamityMod.Log.ILFailure(PatchName, "Could not locate the liquid vertex colors for drawing"); + return; + } + + cursor.EmitLdloc(typeLocalIdx); + cursor.EmitLdloc(xLocalIdx); + cursor.EmitLdloc(yLocalIdx); + cursor.EmitLdloca(vertexColorLocalIdx); + cursor.EmitDelegate((int type, int x, int y, ref VertexColors initialColor) => + { + ModifyColor(x, y, (byte)type, ref initialColor); + }); + } + + private void LiquidSlopeDrawColors(On_TileDrawing.orig_DrawPartialLiquid orig, TileDrawing self, bool behindBlocks, Tile tileCache, ref Vector2 position, ref Rectangle liquidSize, int liquidType, ref VertexColors colors) + { + tileCache.TilePos(out var x, out var y); + var type = tileCache.TileType; + var isFullblock = type == 0 || (!TileID.Sets.BlocksWaterDrawingBehindSelf[type] && behindBlocks); + ModifyColor(x, y, (byte)liquidType, ref colors, isSlope: !isFullblock); + orig(self, behindBlocks, tileCache, ref position, ref liquidSize, liquidType, ref colors); + } + + #endregion + + private static ModWaterStyle GetModWaterStyle(int type) + { + return LoaderManager.Get().Get(type); + } + + private static bool TryGetModWaterStyleAs(int type, out T style) where T : class + { + style = GetModWaterStyle(type) as T; + return style != null; + } + } +} diff --git a/Systems/Graphic/ModLavaStyle.cs b/Systems/Graphic/ModLavaStyle.cs index 65e859515c..30dc5c62a2 100644 --- a/Systems/Graphic/ModLavaStyle.cs +++ b/Systems/Graphic/ModLavaStyle.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework.Graphics; -using ReLogic.Content; +using CalamityMod.Systems.Graphic.LiquidSystem; using Terraria; using Terraria.Graphics; using Terraria.ID; @@ -28,7 +25,7 @@ namespace CalamityMod.Systems protected sealed override void Register() { - Slot = LavaStylesLoader.Register(this); + Slot = ModLavaStyleLoader.Register(this); } public sealed override void SetupContent() @@ -107,135 +104,4 @@ namespace CalamityMod.Systems { } } - - public class LavaStylesLoader : ModSystem - { - private static readonly List _content = []; - - public static IReadOnlyList Content => _content; - - public static int VanillaCount => 1; - - public static int ModCount => _content.Count; - - public static int TotalCount => VanillaCount + ModCount; - - public override void ResizeArrays() - { - int totalCount = TotalCount; - Array.Resize(ref LavaRenderingSystem.Textures.liquid, totalCount); - Array.Resize(ref LavaRenderingSystem.Textures.block, totalCount); - Array.Resize(ref LavaRenderingSystem.Textures.slope, totalCount); - Array.Resize(ref LavaRenderingSystem.Textures.fall, totalCount); - Array.Resize(ref LavaRenderingSystem.LavaAlpha, totalCount); - Array.Resize(ref LavaRenderingSystem.AlphaSave, totalCount); - } - - public override void PostSetupContent() - { - foreach (ModLavaStyle item in Content) - { - int Slot = item.Slot; - LavaRenderingSystem.Textures.liquid[Slot] = ModContent.Request(item.Texture, AssetRequestMode.AsyncLoad); - LavaRenderingSystem.Textures.block[Slot] = ModContent.Request(item.BlockTexture, AssetRequestMode.AsyncLoad); - LavaRenderingSystem.Textures.slope[Slot] = ModContent.Request(item.SlopeTexture, AssetRequestMode.AsyncLoad); - LavaRenderingSystem.Textures.fall[Slot] = ModContent.Request(item.WaterfallTexture, AssetRequestMode.AsyncLoad); - } - } - - public static ModLavaStyle Get(int type) - { - type -= VanillaCount; - return type >= 0 && type < _content.Count ? _content[type] : null; - } - - internal static int Register(ModLavaStyle instance) - { - int type = TotalCount; - ModTypeLookup.Register(instance); - _content.Add(instance); - return type; - } - - private delegate void ResizeArray_orig(bool unloading); - - public static void UpdateLiquidAlphas() - { - if (LavaRenderingSystem.LavaStyle >= VanillaCount) - { - for (int i = 0; i < VanillaCount; i++) - { - LavaRenderingSystem.LavaAlpha[i] -= 0.2f; - if (LavaRenderingSystem.LavaAlpha[i] < 0f) - { - LavaRenderingSystem.LavaAlpha[i] = 0f; - } - } - } - foreach (ModLavaStyle item in Content) - { - int type = item.Slot; - if (LavaRenderingSystem.LavaStyle == type) - { - LavaRenderingSystem.LavaAlpha[type] += 0.2f; - if (LavaRenderingSystem.LavaAlpha[type] > 1f) - { - LavaRenderingSystem.LavaAlpha[type] = 1f; - } - } - else - { - LavaRenderingSystem.LavaAlpha[type] -= 0.2f; - if (LavaRenderingSystem.LavaAlpha[type] < 0f) - { - LavaRenderingSystem.LavaAlpha[type] = 0f; - } - } - } - } - - public static void ModifyLightSetup(int i, int j, int style, ref float r, ref float g, ref float b) - { - ModLavaStyle lavaStyle = Get(style); - if (lavaStyle != null) - { - lavaStyle?.ModifyLight(i, j, ref r, ref g, ref b); - } - } - - internal static void DrawColorSetup(int x, int y, int type, ref VertexColors liquidColor, bool isSlope = false) - { - ModLavaStyle styles = Get(type); - if (styles != null) - { - styles?.DrawColor(x, y, ref liquidColor, isSlope); - } - } - - public static void InflictDebuff(Player player, int type, int onfireDuration) - { - ModLavaStyle lavaStyle = Get(type); - if (lavaStyle != null) - { - lavaStyle?.InflictDebuff(player, onfireDuration); - } - } - - public static void IsLavaActive() - { - foreach (ModLavaStyle item in Content) - { - int type = item.Slot; - ModLavaStyle lavaStyle = Get(type); - if (lavaStyle != null) - { - bool? flag = lavaStyle?.IsLavaActive(); - if (flag != null && flag == true) - { - LavaRenderingSystem.LavaStyle = lavaStyle.Slot; - } - } - } - } - } }