using System; using System.Collections.Generic; using System.Reflection; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.GameContent.Liquid; using Terraria.GameContent; using Terraria.Graphics; using Terraria.ModLoader; using System.Linq; using Terraria.ID; using Microsoft.Xna.Framework; using CalamityMod.Items.Potions.Alcohol; namespace CalamityMod.Systems { public abstract class ModLavaStyle : ModTexturedType { /// /// The ID of the lava style. /// public int Slot { get; private set; } = -1; public override string Name => base.Name; public override string Texture => base.Texture; public virtual string BlockTexture => Texture + "_Block"; public virtual string SlopeTexture => Texture + "_Slope"; public virtual string WaterfallTexture => Texture + "_Waterfall"; protected sealed override void Register() { Slot = LavaStylesLoader.Register(this); } public sealed override void SetupContent() { SetStaticDefaults(); } /// /// Allows you to determine how much light this lava emits.
/// It can also let you light up the block in front of this lava.
/// Keep in mind this also effects what color the lavafalls emit
/// See for vanilla tile light values to use as a reference.
///
/// The x position in tile coordinates. /// The y position in tile coordinates. /// The red component of light, usually a value between 0 and 1 /// The green component of light, usually a value between 0 and 1 /// The blue component of light, usually a value between 0 and 1 public virtual void ModifyLight(int i, int j, ref float r, ref float g, ref float b) { r = 0.55f; g = 0.33f; b = 0.11f; } /// /// Allows water styles to manipulate what color the liquid is drawn to, this can allow waters to be see-throughable to see backgrounds (surface and underground backgrounds not walls) /// /// X position of the water /// Y position of the water /// The vertexColor of the water color, this is both used to get the current color and to set the color of the water public virtual void DrawColor(int x, int y, ref VertexColors liquidColor, bool isSlope) { } /// /// The ID of the dust that is created when anything splashes in lava. /// public virtual int GetSplashDust() { return DustID.Lava; } /// /// The ID of the gore that represents droplets of lava falling down from a block. Return (or another existing droplet gore). /// public virtual int GetDropletGore() { return GoreID.LavaDrip; } /// /// Return true if the player is in the correct zone to activate the lava. /// /// public virtual bool IsLavaActive() { return false; } /// /// Return false if the waterfall made by the lavastyle should have a glowmask /// /// public virtual bool LavafallGlowmask() { return true; } /// /// Allows your lavastyle to inflict debuffs to Players and NPCs when they enter your lava style /// /// The Player that is inflicted with the debuff apon entering the lavastyle /// The duration of the OnFire! debuff. This allows for easy replacement of OnFire public virtual void InflictDebuff(Player player, int onfireDuration) { } } public class LavaStylesLoader : ModSystem { private static readonly MethodInfo ResizeArrayMethodInfo; static LavaStylesLoader() { ResizeArrayMethodInfo = typeof(ModContent).GetMethod("ResizeArrays", BindingFlags.NonPublic | BindingFlags.Static); } private static void ResizeArrays(ResizeArray_orig orig, bool unloading) { orig.Invoke(unloading); 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); } 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 Load() { if (ResizeArrayMethodInfo != null) { MonoModHooks.Add(ResizeArrayMethodInfo, ResizeArrays); } } 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; } } } } } }