Fully Rewrite LavaRendering System
This commit is contained in:
1 parent
e240546eed
commit
1749920b6c
15 files changed
+906
-2051
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<Main>("drawToScreen"), i => i.MatchBrfalse(out _), i => i.MatchLdarg0(), i => i.MatchLdcI4(1), i => i.MatchCall<Main>("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<Main>("drawToScreen"), i => i.MatchBrfalse(out _), i => i.MatchLdarg0(), i => i.MatchLdcI4(0), i => i.MatchCall<Main>("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<Main>("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<Main>("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<Main>("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<Main>("waterStyle"),
|
||||
i => i.MatchLdcR4(1), i => i.MatchLdcI4(1),
|
||||
i => i.MatchCall<Main>("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<Main>("bloodMoon"),
|
||||
i => i.MatchBrtrue(out _),
|
||||
i => i.MatchLdloc(out _), // biome
|
||||
i => i.MatchLdfld<CaptureBiome>("WaterStyle"),
|
||||
i => i.MatchBr(out _),
|
||||
i => i.MatchLdcI4(9),
|
||||
i => i.MatchLdcR4(1),
|
||||
i => i.MatchLdcI4(1),
|
||||
i => i.MatchCall<Main>("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<Main>("waterStyle"),
|
||||
i => i.MatchLdcR4(1),
|
||||
i => i.MatchLdcI4(1),
|
||||
i => i.MatchCall<Main>("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<CaptureBiome>("WaterStyle"),
|
||||
i => i.MatchLdcR4(1),
|
||||
i => i.MatchLdcI4(1),
|
||||
i => i.MatchCall<Main>("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<Main>("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<TileDrawing>("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<Func<bool, int, byte, bool>>((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<Texture2D>).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<Func<Texture2D, int, byte, Texture2D>>((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<Func<int, int>>(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<Func<int, int>>(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<Func<int, int>>(type => LavaRenderingSystem.goreLava());
|
||||
}
|
||||
|
||||
private void SplashEntityLava(ILContext il)
|
||||
{
|
||||
ILCursor cursor = new ILCursor(il);
|
||||
if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdfld<Entity>("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<Func<int, int>>(type => LavaRenderingSystem.dustLava());
|
||||
if (!cursor.TryGotoNext(MoveType.After, i => i.MatchLdfld<Entity>("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<Func<int, int>>(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<Player>("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<Main>("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<WaterStylesLoader>\" 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)
|
||||
{
|
||||
|
||||
@@ -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<int> GetSplashDust = lavaStyle.GetSplashDust;
|
||||
@@ -1310,7 +1310,7 @@ namespace CalamityMod
|
||||
#region Luminance
|
||||
private static void RegisterWorldInfoIcon(Mod luminance, string texturePath, string hoverTextKey, Func<WorldFileData, bool> 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<WorldFileData, bool> revengeanceEnabled = data =>
|
||||
{
|
||||
if (!data.TryGetHeaderData<WorldSelectionDifficultySystem>(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);
|
||||
}
|
||||
|
||||
@@ -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<PhasedGodRay>();
|
||||
float shootSpeed = HeldItem.shootSpeed;
|
||||
|
||||
@@ -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<WaterStylesLoader>().Get(type);
|
||||
if (modWaterStyle is CalamityModWaterStyle calWaterStyle)
|
||||
{
|
||||
return calWaterStyle;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using Terraria.ModLoader;
|
||||
|
||||
namespace CalamityMod.Systems.Graphic.LiquidSystem
|
||||
{
|
||||
public class ModLavaStyleLoader : ModSystem
|
||||
{
|
||||
private static readonly List<ModLavaStyle> ModStyleList = [];
|
||||
|
||||
public static IReadOnlyList<ModLavaStyle> 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<ModLavaStyle>.Register(instance);
|
||||
ModStyleList.Add(instance);
|
||||
ModCount = ModStyleList.Count;
|
||||
return type;
|
||||
}
|
||||
|
||||
public override void Unload()
|
||||
{
|
||||
ModStyleList.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<WaterfallManager, Asset<Texture2D>[]> WaterfallTextureField = new("waterfallTexture", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
|
||||
|
||||
public static ModLavaStyle[] LavaStyles = new ModLavaStyle[ModLavaStyleLoader.VanillaCount];
|
||||
public static Asset<Texture2D>[] Textures = new Asset<Texture2D>[ModLavaStyleLoader.VanillaCount];
|
||||
public static Asset<Texture2D>[] BlockTextures = new Asset<Texture2D>[ModLavaStyleLoader.VanillaCount];
|
||||
public static Asset<Texture2D>[] SlopeTextures = new Asset<Texture2D>[ModLavaStyleLoader.VanillaCount];
|
||||
public static Asset<Texture2D>[] WaterfallTextures = new Asset<Texture2D>[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<Texture2D>(modLavaStyle.Texture);
|
||||
BlockTextures[slot] = ModContent.Request<Texture2D>(modLavaStyle.BlockTexture);
|
||||
SlopeTextures[slot] = ModContent.Request<Texture2D>(modLavaStyle.SlopeTexture);
|
||||
WaterfallTextures[slot] = ModContent.Request<Texture2D>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Texture2D>).GetProperty(nameof(Asset<Texture2D>.Value)).GetMethod;
|
||||
|
||||
private void LoadILEdits()
|
||||
{
|
||||
if (Tex2DAssetGetter == null)
|
||||
{
|
||||
CalamityMod.Log.ILFailure("ModLavaStyle", "Cannot find Getter for Asset<Texture2D>::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<Texture2D>::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<Func<int, int>>(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<Func<int, int>>(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<Func<int, int>>(GetDropletGoreID);
|
||||
}
|
||||
|
||||
private static void SplashEntityLava(ILContext il)
|
||||
{
|
||||
const string PatchName = "Entity Lava Splashing (Item, Projectile, NPC, Player)";
|
||||
|
||||
ILCursor cursor = new ILCursor(il);
|
||||
|
||||
Func<Instruction, bool>[] predicates = [
|
||||
i => i.MatchLdfld<Entity>("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<Func<int, int>>(GetSplashDustID);
|
||||
|
||||
if (!cursor.TryGotoNext(MoveType.After,
|
||||
i => i.MatchLdfld<Entity>("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<Func<int, int>>(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<Player>(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<bool> patch)
|
||||
{
|
||||
int patchedCount = 0;
|
||||
while (true)
|
||||
{
|
||||
var patched = patch.Invoke();
|
||||
|
||||
if (patched)
|
||||
patchedCount++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return patchedCount;
|
||||
}
|
||||
|
||||
private static bool PatchNextGetTexture2DLdArg(ILCursor cursor, Action<int, FieldReference> 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<int, FieldReference> 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
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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<Texture2D>[] 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<IEmittableWaterStyle>(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<IPaintableWaterStyle>(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<WaterStylesLoader>().Get(type);
|
||||
}
|
||||
|
||||
private static bool TryGetModWaterStyleAs<T>(int type, out T style) where T : class
|
||||
{
|
||||
style = GetModWaterStyle(type) as T;
|
||||
return style != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ModLavaStyle> _content = [];
|
||||
|
||||
public static IReadOnlyList<ModLavaStyle> 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<Texture2D>(item.Texture, AssetRequestMode.AsyncLoad);
|
||||
LavaRenderingSystem.Textures.block[Slot] = ModContent.Request<Texture2D>(item.BlockTexture, AssetRequestMode.AsyncLoad);
|
||||
LavaRenderingSystem.Textures.slope[Slot] = ModContent.Request<Texture2D>(item.SlopeTexture, AssetRequestMode.AsyncLoad);
|
||||
LavaRenderingSystem.Textures.fall[Slot] = ModContent.Request<Texture2D>(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<ModLavaStyle>.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user