From 5f6d7687eae269fa68ca4d9c675fe368ffa46b47 Mon Sep 17 00:00:00 2001 From: Yahzy Date: Thu, 19 Nov 2026 00:39:48 +0100 Subject: [PATCH 1/4] Port env_fog_controller and attach the master fog controller to players --- Game.Server/FogController.cs | 228 +++++++++++++++++++++++++++++++++++ Game.Server/GameInterface.cs | 1 + Game.Server/Player.cs | 15 ++- 3 files changed, 243 insertions(+), 1 deletion(-) diff --git a/Game.Server/FogController.cs b/Game.Server/FogController.cs index 064dc9c8..c5a0f4f4 100644 --- a/Game.Server/FogController.cs +++ b/Game.Server/FogController.cs @@ -1,13 +1,27 @@ using Game.Shared; +using Source; using Source.Common; +using Source.Common.Engine; +using Source.Common.Mathematics; + +using System.Numerics; namespace Game.Server; +using DEFINE = Source.DEFINE; + [LinkEntityToClass("env_fog_controller")] [NetworkName("CFogController")] public partial class FogController : BaseEntity { + public const int SF_FOG_MASTER = 0x0001; + + const int FOG_CONTROLLER_COLORPRIMARY_LERP = 1; + const int FOG_CONTROLLER_COLORSECONDARY_LERP = 2; + const int FOG_CONTROLLER_START_LERP = 4; + const int FOG_CONTROLLER_END_LERP = 8; + [NetworkVar("m_fog")] public partial ref readonly FogParams Fog { get; } [NetworkTable(NoBase = true)] @@ -34,4 +48,218 @@ public partial class FogController : BaseEntity new(nameof(Fog.Duration)) { Encoding = NetEncoding.Float, Bits = 0, Flags = PropFlags.NoScale, Low = 0.0f, High = 0.0f }, new(nameof(Fog.HDRColorScale)) { Bits = 0, Flags = PropFlags.NoScale, Low = 0.0f, High = 0.0f }, ]; + + public bool UseAngles; + public int ChangedVariables; + + public static readonly new DataMap DataDesc = new(typeof(FogController), BaseEntity.DataDesc, [ + DEFINE.INPUTFUNC(FieldType.Float, "SetStartDist", nameof(InputSetStartDist), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetStartDist(data))), + DEFINE.INPUTFUNC(FieldType.Float, "SetEndDist", nameof(InputSetEndDist), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetEndDist(data))), + DEFINE.INPUTFUNC(FieldType.Float, "SetMaxDensity", nameof(InputSetMaxDensity), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetMaxDensity(data))), + DEFINE.INPUTFUNC(FieldType.Void, "TurnOn", nameof(InputTurnOn), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputTurnOn(data))), + DEFINE.INPUTFUNC(FieldType.Void, "TurnOff", nameof(InputTurnOff), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputTurnOff(data))), + DEFINE.INPUTFUNC(FieldType.Color32, "SetColor", nameof(InputSetColor), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColor(data))), + DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondary", nameof(InputSetColorSecondary), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondary(data))), + DEFINE.INPUTFUNC(FieldType.Integer, "SetFarZ", nameof(InputSetFarZ), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetFarZ(data))), + DEFINE.INPUTFUNC(FieldType.String, "SetAngles", nameof(InputSetAngles), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetAngles(data))), + + DEFINE.INPUTFUNC(FieldType.Color32, "SetColorLerpTo", nameof(InputSetColorLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorLerpTo(data))), + DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryLerpTo", nameof(InputSetColorSecondaryLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryLerpTo(data))), + DEFINE.INPUTFUNC(FieldType.Float, "SetStartDistLerpTo", nameof(InputSetStartDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetStartDistLerpTo(data))), + DEFINE.INPUTFUNC(FieldType.Float, "SetEndDistLerpTo", nameof(InputSetEndDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetEndDistLerpTo(data))), + DEFINE.INPUTFUNC(FieldType.Void, "StartFogTransition", nameof(InputStartFogTransition), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputStartFogTransition(data))), + + DEFINE.KEYFIELD(nameof(UseAngles), FieldType.Boolean, "use_angles"), + DEFINE.KEYFIELD("Fog.ColorPrimary", FieldType.Color32, "fogcolor"), + DEFINE.KEYFIELD("Fog.ColorSecondary", FieldType.Color32, "fogcolor2"), + DEFINE.KEYFIELD("Fog.DirPrimary", FieldType.Vector, "fogdir"), + DEFINE.KEYFIELD("Fog.Enable", FieldType.Boolean, "fogenable"), + DEFINE.KEYFIELD("Fog.Blend", FieldType.Boolean, "fogblend"), + DEFINE.KEYFIELD("Fog.Start", FieldType.Float, "fogstart"), + DEFINE.KEYFIELD("Fog.End", FieldType.Float, "fogend"), + DEFINE.KEYFIELD("Fog.MaxDensity", FieldType.Float, "fogmaxdensity"), + DEFINE.KEYFIELD("Fog.FarZ", FieldType.Float, "farz"), + DEFINE.KEYFIELD("Fog.Duration", FieldType.Float, "foglerptime"), + + DEFINE.THINKFUNC(nameof(SetLerpValues)), + + DEFINE.FIELD(nameof(ChangedVariables), FieldType.Integer), + + DEFINE.FIELD("Fog.LerpTime", FieldType.Time), + DEFINE.FIELD("Fog.ColorPrimaryLerpTo", FieldType.Color32), + DEFINE.FIELD("Fog.ColorSecondaryLerpTo", FieldType.Color32), + DEFINE.FIELD("Fog.StartLerpTo", FieldType.Float), + DEFINE.FIELD("Fog.EndLerpTo", FieldType.Float), + ]); + public override DataMap? GetDataDescMap() => DataDesc; + + public FogController() { + // Make sure that old maps without fog fields don't get wacked out fog values. + FogForModify().Enable = false; + FogForModify().MaxDensity = 1.0f; + } + + public override void Spawn() { + base.Spawn(); + + ref FogParams fog = ref FogForModify(); + fog.ColorPrimaryLerpTo = fog.ColorPrimary; + fog.ColorSecondaryLerpTo = fog.ColorSecondary; + } + + public override void Activate() { + base.Activate(); + + if (UseAngles) { + ref FogParams fog = ref FogForModify(); + MathLib.AngleVectors(GetAbsAngles(), out fog.DirPrimary); + fog.DirPrimary *= -1.0f; + } + } + + public override EdictFlags UpdateTransmitState() => SetTransmitState(EdictFlags.Always); + + public bool IsMaster() => HasSpawnFlags(SF_FOG_MASTER); + + public void InputSetStartDist(InputData inputdata) => FogForModify().Start = inputdata.Value.Float(); + + public void InputSetEndDist(InputData inputdata) => FogForModify().End = inputdata.Value.Float(); + + // Input handler for setting the maximum density of the fog. This lets us bring + // the start distance in without the scene fogging too much. + public void InputSetMaxDensity(InputData inputdata) => FogForModify().MaxDensity = inputdata.Value.Float(); + + public void InputTurnOn(InputData inputdata) => FogForModify().Enable = true; + + public void InputTurnOff(InputData inputdata) => FogForModify().Enable = false; + + public void InputSetColor(InputData inputdata) => FogForModify().ColorPrimary = inputdata.Value.Color32(); + + public void InputSetColorSecondary(InputData inputdata) => FogForModify().ColorSecondary = inputdata.Value.Color32(); + + public void InputSetFarZ(InputData inputdata) => FogForModify().FarZ = inputdata.Value.Int(); + + public void InputSetAngles(InputData inputdata) { + Span anglesBase = stackalloc float[3]; + UTIL_StringToVector(anglesBase, inputdata.Value.String()); + QAngle angles = new(anglesBase[0], anglesBase[1], anglesBase[2]); + + SetAbsAngles(angles); + + ref FogParams fog = ref FogForModify(); + MathLib.AngleVectors(GetAbsAngles(), out fog.DirPrimary); + fog.DirPrimary *= -1.0f; + } + + public override int DrawDebugTextOverlays() { + int textOffset = base.DrawDebugTextOverlays(); + + if ((DebugOverlays & DebugOverlayBits.Text) != 0) { + ref readonly FogParams fog = ref Fog; + + EntityText(textOffset++, $"State: {(fog.Enable ? "On" : "Off")}", 0); + EntityText(textOffset++, $"Start: {fog.Start,3:0}", 0); + EntityText(textOffset++, $"End : {fog.End,3:0}", 0); + + Color color = fog.ColorPrimary; + EntityText(textOffset++, $"1) Red : {color.R}", 0); + EntityText(textOffset++, $"1) Green: {color.G}", 0); + EntityText(textOffset++, $"1) Blue : {color.B}", 0); + + color = fog.ColorSecondary; + EntityText(textOffset++, $"2) Red : {color.R}", 0); + EntityText(textOffset++, $"2) Green: {color.G}", 0); + EntityText(textOffset++, $"2) Blue : {color.B}", 0); + } + + return textOffset; + } + + public void InputSetColorLerpTo(InputData inputdata) { + ChangedVariables |= FOG_CONTROLLER_COLORPRIMARY_LERP; + FogForModify().ColorPrimaryLerpTo = inputdata.Value.Color32(); + } + + public void InputSetColorSecondaryLerpTo(InputData inputdata) { + ChangedVariables |= FOG_CONTROLLER_COLORSECONDARY_LERP; + FogForModify().ColorSecondaryLerpTo = inputdata.Value.Color32(); + } + + public void InputSetStartDistLerpTo(InputData inputdata) { + ChangedVariables |= FOG_CONTROLLER_START_LERP; + FogForModify().StartLerpTo = inputdata.Value.Float(); + } + + public void InputSetEndDistLerpTo(InputData inputdata) { + ChangedVariables |= FOG_CONTROLLER_END_LERP; + FogForModify().EndLerpTo = inputdata.Value.Float(); + } + + public void InputStartFogTransition(InputData inputdata) { + SetThink(SetLerpValues); + + ref FogParams fog = ref FogForModify(); + fog.LerpTime = gpGlobals.CurTime + fog.Duration + 0.1; + SetNextThink(gpGlobals.CurTime + fog.Duration); + } + + public void SetLerpValues() { + ref FogParams fog = ref FogForModify(); + + if ((ChangedVariables & FOG_CONTROLLER_COLORPRIMARY_LERP) != 0) + fog.ColorPrimary = fog.ColorPrimaryLerpTo; + + if ((ChangedVariables & FOG_CONTROLLER_COLORSECONDARY_LERP) != 0) + fog.ColorSecondary = fog.ColorSecondaryLerpTo; + + if ((ChangedVariables & FOG_CONTROLLER_START_LERP) != 0) + fog.Start = fog.StartLerpTo; + + if ((ChangedVariables & FOG_CONTROLLER_END_LERP) != 0) + fog.End = fog.EndLerpTo; + + ChangedVariables = 0; + fog.LerpTime = gpGlobals.CurTime; + } +} + +public class FogSystem(ReadOnlySpan name) : AutoGameSystem(name) +{ + public static readonly FogSystem s_FogSystem = new("FogSystem"); + + FogController? MasterController; + + // Clear out the fog controller. + public override void LevelInitPreEntity() { + MasterController = null; + } + + // On level load find the master fog controller. If no controller is + // set as Master, use the first fog controller found. + public override void LevelInitPostEntity() { + FogController? fogController = null; + do { + fogController = (FogController?)gEntList.FindEntityByClassname(fogController, "env_fog_controller"); + if (fogController != null) { + if (MasterController == null) + MasterController = fogController; + else { + if (fogController.IsMaster()) + MasterController = fogController; + } + } + } while (fogController != null); + + // HACK: Singleplayer games don't get a call to CBasePlayer::Spawn on level transitions. + // CBasePlayer::Activate is called before this is called so that's too soon to set up the fog controller. + // We don't have a hook similar to Activate that happens after LevelInitPostEntity + // is called, or we could just do this in the player itself. + if (gpGlobals.MaxClients == 1) { + BasePlayer? player = Util.GetLocalPlayer(); + if (player != null && player.Local.PlayerFog.Ctrl.Get() == null) + player.InitFogController(); + } + } + + public FogController? GetMasterFogController() => MasterController; } diff --git a/Game.Server/GameInterface.cs b/Game.Server/GameInterface.cs index e3c8af26..4eccd7ce 100644 --- a/Game.Server/GameInterface.cs +++ b/Game.Server/GameInterface.cs @@ -333,6 +333,7 @@ public bool DLLInit(IServiceProvider services) { System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(LagCompensationGlobals).TypeHandle); System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Templates).TypeHandle); System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(PointTemplatePrecacher).TypeHandle); + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(FogSystem).TypeHandle); #if GMOD_DLL if (CommandLine.FindParm("-noaddons") == 0) diff --git a/Game.Server/Player.cs b/Game.Server/Player.cs index e636c799..dccc44fb 100644 --- a/Game.Server/Player.cs +++ b/Game.Server/Player.cs @@ -270,6 +270,7 @@ public void SetArmorValue(int value) { public static readonly new DataMap DataDesc = new(typeof(BasePlayer), BaseCombatCharacter.DataDesc, [ DEFINE.INPUTFUNC(FieldType.Integer, "SetHealth", nameof(InputSetHealth), (INPUTFUNCPTR)((self, data) => ((BasePlayer)self).InputSetHealth(data))), + DEFINE.INPUTFUNC(FieldType.String, "SetFogController", nameof(InputSetFogController), (INPUTFUNCPTR)((self, data) => ((BasePlayer)self).InputSetFogController(data))), ]); public override DataMap? GetDataDescMap() => DataDesc; @@ -295,6 +296,18 @@ public void InputSetHealth(InputData inputdata) { } } + // Set the fog controller data per player. + public void InputSetFogController(InputData inputdata) { + // Find the fog controller with the given name. + if (gEntList.FindEntityByName(null, inputdata.Value.String()) is FogController fogController) + Local.PlayerFogForModify().Ctrl.Set(fogController); + } + + public void InitFogController() { + // Setup with the default master controller. + Local.PlayerFogForModify().Ctrl.Set(FogSystem.s_FogSystem.GetMasterFogController()); + } + static InlineArray1024 InfoBuffer; public ReadOnlySpan GetInfo(ReadOnlySpan name) { @@ -1112,7 +1125,7 @@ public override void Spawn() { IncrementInterpolationFrame(); - // InitFogController(); + InitFogController(); DmgTake = 0; DmgSave = 0; From c2c995f6ee17733b5d0bc6ef09bc3067e940dc22 Mon Sep 17 00:00:00 2001 From: Yahzy Date: Thu, 19 Nov 2026 00:48:06 +0100 Subject: [PATCH 2/4] Add radial fog support to env_fog_controller --- Game.Server/FogController.cs | 8 ++++++++ Game.Shared/GameRules.cs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/Game.Server/FogController.cs b/Game.Server/FogController.cs index c5a0f4f4..1f8d6da8 100644 --- a/Game.Server/FogController.cs +++ b/Game.Server/FogController.cs @@ -62,6 +62,7 @@ public partial class FogController : BaseEntity DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondary", nameof(InputSetColorSecondary), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondary(data))), DEFINE.INPUTFUNC(FieldType.Integer, "SetFarZ", nameof(InputSetFarZ), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetFarZ(data))), DEFINE.INPUTFUNC(FieldType.String, "SetAngles", nameof(InputSetAngles), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetAngles(data))), + DEFINE.INPUTFUNC(FieldType.Boolean, "SetRadial", nameof(InputSetRadial), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetRadial(data))), DEFINE.INPUTFUNC(FieldType.Color32, "SetColorLerpTo", nameof(InputSetColorLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorLerpTo(data))), DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryLerpTo", nameof(InputSetColorSecondaryLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryLerpTo(data))), @@ -80,6 +81,7 @@ public partial class FogController : BaseEntity DEFINE.KEYFIELD("Fog.MaxDensity", FieldType.Float, "fogmaxdensity"), DEFINE.KEYFIELD("Fog.FarZ", FieldType.Float, "farz"), DEFINE.KEYFIELD("Fog.Duration", FieldType.Float, "foglerptime"), + DEFINE.KEYFIELD("Fog.Radial", FieldType.Boolean, "fogradial"), DEFINE.THINKFUNC(nameof(SetLerpValues)), @@ -151,6 +153,8 @@ public void InputSetAngles(InputData inputdata) { fog.DirPrimary *= -1.0f; } + public void InputSetRadial(InputData inputdata) => FogForModify().Radial = inputdata.Value.Bool(); + public override int DrawDebugTextOverlays() { int textOffset = base.DrawDebugTextOverlays(); @@ -241,6 +245,10 @@ public override void LevelInitPostEntity() { do { fogController = (FogController?)gEntList.FindEntityByClassname(fogController, "env_fog_controller"); if (fogController != null) { + // If this is an official map then force on radial fog. + if (g_pGameRules.IsOfficialMap()) + fogController.FogForModify().Radial = true; + if (MasterController == null) MasterController = fogController; else { diff --git a/Game.Shared/GameRules.cs b/Game.Shared/GameRules.cs index 2192acc1..0d398992 100644 --- a/Game.Shared/GameRules.cs +++ b/Game.Shared/GameRules.cs @@ -796,5 +796,9 @@ public virtual void ProcessVerboseLogOutput() { } public virtual bool IsHolidayActive(int holiday) { return false; } public virtual bool IsManualMapChangeOkay(out ReadOnlySpan reason) { reason = default; return true; } + +#if GAME_DLL + public virtual bool IsOfficialMap() => false; +#endif } #endif From d23aa1a0b57a5d249505b8a28328228d2a0daaf7 Mon Sep 17 00:00:00 2001 From: Yahzy Date: Thu, 19 Nov 2026 02:42:32 +0100 Subject: [PATCH 3/4] Read fogradial on sky_camera and force it on official maps --- Game.Server/SkyCamera.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Game.Server/SkyCamera.cs b/Game.Server/SkyCamera.cs index 613fed0b..bc11ae13 100644 --- a/Game.Server/SkyCamera.cs +++ b/Game.Server/SkyCamera.cs @@ -91,6 +91,8 @@ public override bool KeyValue(ReadOnlySpan keyName, ReadOnlySpan val SkyboxData.Fog.End = strtof(value, out _); else if (FStrEq(keyName, "fogmaxdensity")) SkyboxData.Fog.MaxDensity = strtof(value, out _); + else if (FStrEq(keyName, "fogradial")) + SkyboxData.Fog.Radial = atoi(value) != 0; else return base.KeyValue(keyName, value); @@ -125,5 +127,9 @@ public override void Activate() { } } #endif + + // matches the behavior of fog controller + if (g_pGameRules.IsOfficialMap()) + SkyboxData.Fog.Radial = true; } } From a63cce59465c2f435976a8fe096307fc2b306a44 Mon Sep 17 00:00:00 2001 From: march <106459595+marchc1@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:59:00 -0800 Subject: [PATCH 4/4] Fix what I could find wrong with fog controller --- Game.Server/FogController.cs | 90 +++++++++++++++++++++++++++--------- Game.Server/SkyCamera.cs | 12 +++-- Game.Shared/GameRules.cs | 4 -- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/Game.Server/FogController.cs b/Game.Server/FogController.cs index 1f8d6da8..7a44b8d1 100644 --- a/Game.Server/FogController.cs +++ b/Game.Server/FogController.cs @@ -21,6 +21,7 @@ public partial class FogController : BaseEntity const int FOG_CONTROLLER_COLORSECONDARY_LERP = 2; const int FOG_CONTROLLER_START_LERP = 4; const int FOG_CONTROLLER_END_LERP = 8; + const int FOG_CONTROLLER_MAXDENSITY_LERP = 16; [NetworkVar("m_fog")] public partial ref readonly FogParams Fog { get; } @@ -62,17 +63,22 @@ public partial class FogController : BaseEntity DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondary", nameof(InputSetColorSecondary), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondary(data))), DEFINE.INPUTFUNC(FieldType.Integer, "SetFarZ", nameof(InputSetFarZ), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetFarZ(data))), DEFINE.INPUTFUNC(FieldType.String, "SetAngles", nameof(InputSetAngles), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetAngles(data))), + DEFINE.INPUTFUNC(FieldType.Color32, "SetColorHDR", nameof(InputSetColorHDR), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorHDR(data))), + DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryHDR", nameof(InputSetColorSecondaryHDR), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryHDR(data))), DEFINE.INPUTFUNC(FieldType.Boolean, "SetRadial", nameof(InputSetRadial), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetRadial(data))), DEFINE.INPUTFUNC(FieldType.Color32, "SetColorLerpTo", nameof(InputSetColorLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorLerpTo(data))), DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryLerpTo", nameof(InputSetColorSecondaryLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryLerpTo(data))), DEFINE.INPUTFUNC(FieldType.Float, "SetStartDistLerpTo", nameof(InputSetStartDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetStartDistLerpTo(data))), DEFINE.INPUTFUNC(FieldType.Float, "SetEndDistLerpTo", nameof(InputSetEndDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetEndDistLerpTo(data))), + DEFINE.INPUTFUNC(FieldType.Float, "SetMaxDensityLerpTo", nameof(InputSetMaxDensityLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetMaxDensityLerpTo(data))), DEFINE.INPUTFUNC(FieldType.Void, "StartFogTransition", nameof(InputStartFogTransition), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputStartFogTransition(data))), DEFINE.KEYFIELD(nameof(UseAngles), FieldType.Boolean, "use_angles"), DEFINE.KEYFIELD("Fog.ColorPrimary", FieldType.Color32, "fogcolor"), DEFINE.KEYFIELD("Fog.ColorSecondary", FieldType.Color32, "fogcolor2"), + DEFINE.KEYFIELD("Fog.ColorPrimaryHDR", FieldType.Color32, "fogcolor_hdr"), + DEFINE.KEYFIELD("Fog.ColorSecondaryHDR", FieldType.Color32, "fogcolor2_hdr"), DEFINE.KEYFIELD("Fog.DirPrimary", FieldType.Vector, "fogdir"), DEFINE.KEYFIELD("Fog.Enable", FieldType.Boolean, "fogenable"), DEFINE.KEYFIELD("Fog.Blend", FieldType.Boolean, "fogblend"), @@ -82,6 +88,7 @@ public partial class FogController : BaseEntity DEFINE.KEYFIELD("Fog.FarZ", FieldType.Float, "farz"), DEFINE.KEYFIELD("Fog.Duration", FieldType.Float, "foglerptime"), DEFINE.KEYFIELD("Fog.Radial", FieldType.Boolean, "fogradial"), + DEFINE.KEYFIELD("Fog.HDRColorScale", FieldType.Float, "HDRColorScale"), DEFINE.THINKFUNC(nameof(SetLerpValues)), @@ -92,13 +99,22 @@ public partial class FogController : BaseEntity DEFINE.FIELD("Fog.ColorSecondaryLerpTo", FieldType.Color32), DEFINE.FIELD("Fog.StartLerpTo", FieldType.Float), DEFINE.FIELD("Fog.EndLerpTo", FieldType.Float), + DEFINE.FIELD("Fog.MaxDensityLerpTo", FieldType.Float), ]); public override DataMap? GetDataDescMap() => DataDesc; public FogController() { // Make sure that old maps without fog fields don't get wacked out fog values. - FogForModify().Enable = false; - FogForModify().MaxDensity = 1.0f; + ref FogParams fog = ref FogForModify(); + fog.Enable = false; + fog.MaxDensity = 1.0f; + fog.HDRColorScale = 1.0f; + fog.MaxDensityLerpTo = -1.0f; + } + + static int AbsTrunc(float value) { + int i = (int)value; + return i < 0 ? -i : i; } public override void Spawn() { @@ -107,6 +123,13 @@ public override void Spawn() { ref FogParams fog = ref FogForModify(); fog.ColorPrimaryLerpTo = fog.ColorPrimary; fog.ColorSecondaryLerpTo = fog.ColorSecondary; + + if (fog.MaxDensity > 0.95 && fog.Enable) { + if ((fog.FarZ < 250.0f && fog.FarZ > 0.0f) || (AbsTrunc(fog.End - fog.Start) <= 4 && (AbsTrunc(fog.Start) <= 49 || AbsTrunc(fog.End) <= 49))) { + Warning("Deleting env_fog_controller because it has silly values for Start/End/FarZ.\n"); + Util.Remove(this); + } + } } public override void Activate() { @@ -139,7 +162,19 @@ public override void Activate() { public void InputSetColorSecondary(InputData inputdata) => FogForModify().ColorSecondary = inputdata.Value.Color32(); - public void InputSetFarZ(InputData inputdata) => FogForModify().FarZ = inputdata.Value.Int(); + public void InputSetFarZ(InputData inputdata) { + int farZ = inputdata.Value.Int(); + if ((uint)(farZ - 1) <= 248) { + Warning($"Ignoring SetFarZ on fog controller: Silly value {farZ} given!\n"); + return; + } + + FogForModify().FarZ = farZ; + } + + public void InputSetColorHDR(InputData inputdata) => FogForModify().ColorPrimaryHDR = inputdata.Value.Color32(); + + public void InputSetColorSecondaryHDR(InputData inputdata) => FogForModify().ColorSecondaryHDR = inputdata.Value.Color32(); public void InputSetAngles(InputData inputdata) { Span anglesBase = stackalloc float[3]; @@ -199,6 +234,11 @@ public void InputSetEndDistLerpTo(InputData inputdata) { FogForModify().EndLerpTo = inputdata.Value.Float(); } + public void InputSetMaxDensityLerpTo(InputData inputdata) { + ChangedVariables |= FOG_CONTROLLER_MAXDENSITY_LERP; + FogForModify().MaxDensityLerpTo = inputdata.Value.Float(); + } + public void InputStartFogTransition(InputData inputdata) { SetThink(SetLerpValues); @@ -222,41 +262,32 @@ public void SetLerpValues() { if ((ChangedVariables & FOG_CONTROLLER_END_LERP) != 0) fog.End = fog.EndLerpTo; + if ((ChangedVariables & FOG_CONTROLLER_MAXDENSITY_LERP) != 0) + fog.MaxDensity = fog.MaxDensityLerpTo; + ChangedVariables = 0; fog.LerpTime = gpGlobals.CurTime; } } -public class FogSystem(ReadOnlySpan name) : AutoGameSystem(name) +public class FogSystem(ReadOnlySpan name) : AutoGameSystem(name), IGameEventListener2 { public static readonly FogSystem s_FogSystem = new("FogSystem"); - FogController? MasterController; + Handle MasterController = new(); // Clear out the fog controller. public override void LevelInitPreEntity() { - MasterController = null; + MasterController = new(); + gameeventmanager?.AddListener(this, "round_start", true); } // On level load find the master fog controller. If no controller is // set as Master, use the first fog controller found. + public void FireGameEvent(IGameEvent ev) => InitMasterController(); + public override void LevelInitPostEntity() { - FogController? fogController = null; - do { - fogController = (FogController?)gEntList.FindEntityByClassname(fogController, "env_fog_controller"); - if (fogController != null) { - // If this is an official map then force on radial fog. - if (g_pGameRules.IsOfficialMap()) - fogController.FogForModify().Radial = true; - - if (MasterController == null) - MasterController = fogController; - else { - if (fogController.IsMaster()) - MasterController = fogController; - } - } - } while (fogController != null); + InitMasterController(); // HACK: Singleplayer games don't get a call to CBasePlayer::Spawn on level transitions. // CBasePlayer::Activate is called before this is called so that's too soon to set up the fog controller. @@ -269,5 +300,18 @@ public override void LevelInitPostEntity() { } } - public FogController? GetMasterFogController() => MasterController; + void InitMasterController() { + FogController? fogController = null; + while ((fogController = (FogController?)gEntList.FindEntityByClassname(fogController, "env_fog_controller")) != null) { + if (fogController.IsEFlagSet(EFL.KillMe)) + continue; + + if (MasterController.Get() == null || fogController.IsMaster()) + MasterController.Set(fogController); + } + } + + public void SetMasterController(FogController? master) => MasterController.Set(master); + + public FogController? GetMasterFogController() => MasterController.Get(); } diff --git a/Game.Server/SkyCamera.cs b/Game.Server/SkyCamera.cs index bc11ae13..28b21013 100644 --- a/Game.Server/SkyCamera.cs +++ b/Game.Server/SkyCamera.cs @@ -51,6 +51,8 @@ public SkyCamera() { Next = ClassList; ClassList = this; SkyboxData.Fog.MaxDensity = 1.0f; + SkyboxData.Fog.HDRColorScale = 1.0f; + SkyboxData.Fog.MaxDensityLerpTo = -1.0f; } public override void UpdateOnRemove() { @@ -85,6 +87,10 @@ public override bool KeyValue(ReadOnlySpan keyName, ReadOnlySpan val Util.StringToColor32(out SkyboxData.Fog.ColorPrimary, value); else if (FStrEq(keyName, "fogcolor2")) Util.StringToColor32(out SkyboxData.Fog.ColorSecondary, value); + else if (FStrEq(keyName, "fogcolor_hdr")) + Util.StringToColor32(out SkyboxData.Fog.ColorPrimaryHDR, value); + else if (FStrEq(keyName, "fogcolor2_hdr")) + Util.StringToColor32(out SkyboxData.Fog.ColorSecondaryHDR, value); else if (FStrEq(keyName, "fogstart")) SkyboxData.Fog.Start = strtof(value, out _); else if (FStrEq(keyName, "fogend")) @@ -93,6 +99,8 @@ public override bool KeyValue(ReadOnlySpan keyName, ReadOnlySpan val SkyboxData.Fog.MaxDensity = strtof(value, out _); else if (FStrEq(keyName, "fogradial")) SkyboxData.Fog.Radial = atoi(value) != 0; + else if (FStrEq(keyName, "HDRColorScale")) + SkyboxData.Fog.HDRColorScale = strtof(value, out _); else return base.KeyValue(keyName, value); @@ -127,9 +135,5 @@ public override void Activate() { } } #endif - - // matches the behavior of fog controller - if (g_pGameRules.IsOfficialMap()) - SkyboxData.Fog.Radial = true; } } diff --git a/Game.Shared/GameRules.cs b/Game.Shared/GameRules.cs index 0d398992..2192acc1 100644 --- a/Game.Shared/GameRules.cs +++ b/Game.Shared/GameRules.cs @@ -796,9 +796,5 @@ public virtual void ProcessVerboseLogOutput() { } public virtual bool IsHolidayActive(int holiday) { return false; } public virtual bool IsManualMapChangeOkay(out ReadOnlySpan reason) { reason = default; return true; } - -#if GAME_DLL - public virtual bool IsOfficialMap() => false; -#endif } #endif