Skip to content

Commit 3818afb

Browse files
authored
Merge pull request #287 from Yahzy/fix/fog-controller
Port env_fog_controller
2 parents 23b6668 + a63cce5 commit 3818afb

4 files changed

Lines changed: 305 additions & 1 deletion

File tree

‎Game.Server/FogController.cs‎

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
using Game.Shared;
22

3+
using Source;
34
using Source.Common;
5+
using Source.Common.Engine;
6+
using Source.Common.Mathematics;
7+
8+
using System.Numerics;
49

510
namespace Game.Server;
611

12+
using DEFINE = Source.DEFINE<FogController>;
13+
714
[LinkEntityToClass("env_fog_controller")]
815
[NetworkName("CFogController")]
916
public partial class FogController : BaseEntity
1017
{
18+
public const int SF_FOG_MASTER = 0x0001;
19+
20+
const int FOG_CONTROLLER_COLORPRIMARY_LERP = 1;
21+
const int FOG_CONTROLLER_COLORSECONDARY_LERP = 2;
22+
const int FOG_CONTROLLER_START_LERP = 4;
23+
const int FOG_CONTROLLER_END_LERP = 8;
24+
const int FOG_CONTROLLER_MAXDENSITY_LERP = 16;
25+
1126
[NetworkVar("m_fog")]
1227
public partial ref readonly FogParams Fog { get; }
1328
[NetworkTable(NoBase = true)]
@@ -34,4 +49,269 @@ public partial class FogController : BaseEntity
3449
new(nameof(Fog.Duration)) { Encoding = NetEncoding.Float, Bits = 0, Flags = PropFlags.NoScale, Low = 0.0f, High = 0.0f },
3550
new(nameof(Fog.HDRColorScale)) { Bits = 0, Flags = PropFlags.NoScale, Low = 0.0f, High = 0.0f },
3651
];
52+
53+
public bool UseAngles;
54+
public int ChangedVariables;
55+
56+
public static readonly new DataMap DataDesc = new(typeof(FogController), BaseEntity.DataDesc, [
57+
DEFINE.INPUTFUNC(FieldType.Float, "SetStartDist", nameof(InputSetStartDist), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetStartDist(data))),
58+
DEFINE.INPUTFUNC(FieldType.Float, "SetEndDist", nameof(InputSetEndDist), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetEndDist(data))),
59+
DEFINE.INPUTFUNC(FieldType.Float, "SetMaxDensity", nameof(InputSetMaxDensity), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetMaxDensity(data))),
60+
DEFINE.INPUTFUNC(FieldType.Void, "TurnOn", nameof(InputTurnOn), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputTurnOn(data))),
61+
DEFINE.INPUTFUNC(FieldType.Void, "TurnOff", nameof(InputTurnOff), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputTurnOff(data))),
62+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColor", nameof(InputSetColor), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColor(data))),
63+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondary", nameof(InputSetColorSecondary), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondary(data))),
64+
DEFINE.INPUTFUNC(FieldType.Integer, "SetFarZ", nameof(InputSetFarZ), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetFarZ(data))),
65+
DEFINE.INPUTFUNC(FieldType.String, "SetAngles", nameof(InputSetAngles), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetAngles(data))),
66+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColorHDR", nameof(InputSetColorHDR), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorHDR(data))),
67+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryHDR", nameof(InputSetColorSecondaryHDR), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryHDR(data))),
68+
DEFINE.INPUTFUNC(FieldType.Boolean, "SetRadial", nameof(InputSetRadial), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetRadial(data))),
69+
70+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColorLerpTo", nameof(InputSetColorLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorLerpTo(data))),
71+
DEFINE.INPUTFUNC(FieldType.Color32, "SetColorSecondaryLerpTo", nameof(InputSetColorSecondaryLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetColorSecondaryLerpTo(data))),
72+
DEFINE.INPUTFUNC(FieldType.Float, "SetStartDistLerpTo", nameof(InputSetStartDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetStartDistLerpTo(data))),
73+
DEFINE.INPUTFUNC(FieldType.Float, "SetEndDistLerpTo", nameof(InputSetEndDistLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetEndDistLerpTo(data))),
74+
DEFINE.INPUTFUNC(FieldType.Float, "SetMaxDensityLerpTo", nameof(InputSetMaxDensityLerpTo), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputSetMaxDensityLerpTo(data))),
75+
DEFINE.INPUTFUNC(FieldType.Void, "StartFogTransition", nameof(InputStartFogTransition), (INPUTFUNCPTR)((self, data) => ((FogController)self).InputStartFogTransition(data))),
76+
77+
DEFINE.KEYFIELD(nameof(UseAngles), FieldType.Boolean, "use_angles"),
78+
DEFINE.KEYFIELD("Fog.ColorPrimary", FieldType.Color32, "fogcolor"),
79+
DEFINE.KEYFIELD("Fog.ColorSecondary", FieldType.Color32, "fogcolor2"),
80+
DEFINE.KEYFIELD("Fog.ColorPrimaryHDR", FieldType.Color32, "fogcolor_hdr"),
81+
DEFINE.KEYFIELD("Fog.ColorSecondaryHDR", FieldType.Color32, "fogcolor2_hdr"),
82+
DEFINE.KEYFIELD("Fog.DirPrimary", FieldType.Vector, "fogdir"),
83+
DEFINE.KEYFIELD("Fog.Enable", FieldType.Boolean, "fogenable"),
84+
DEFINE.KEYFIELD("Fog.Blend", FieldType.Boolean, "fogblend"),
85+
DEFINE.KEYFIELD("Fog.Start", FieldType.Float, "fogstart"),
86+
DEFINE.KEYFIELD("Fog.End", FieldType.Float, "fogend"),
87+
DEFINE.KEYFIELD("Fog.MaxDensity", FieldType.Float, "fogmaxdensity"),
88+
DEFINE.KEYFIELD("Fog.FarZ", FieldType.Float, "farz"),
89+
DEFINE.KEYFIELD("Fog.Duration", FieldType.Float, "foglerptime"),
90+
DEFINE.KEYFIELD("Fog.Radial", FieldType.Boolean, "fogradial"),
91+
DEFINE.KEYFIELD("Fog.HDRColorScale", FieldType.Float, "HDRColorScale"),
92+
93+
DEFINE.THINKFUNC(nameof(SetLerpValues)),
94+
95+
DEFINE.FIELD(nameof(ChangedVariables), FieldType.Integer),
96+
97+
DEFINE.FIELD("Fog.LerpTime", FieldType.Time),
98+
DEFINE.FIELD("Fog.ColorPrimaryLerpTo", FieldType.Color32),
99+
DEFINE.FIELD("Fog.ColorSecondaryLerpTo", FieldType.Color32),
100+
DEFINE.FIELD("Fog.StartLerpTo", FieldType.Float),
101+
DEFINE.FIELD("Fog.EndLerpTo", FieldType.Float),
102+
DEFINE.FIELD("Fog.MaxDensityLerpTo", FieldType.Float),
103+
]);
104+
public override DataMap? GetDataDescMap() => DataDesc;
105+
106+
public FogController() {
107+
// Make sure that old maps without fog fields don't get wacked out fog values.
108+
ref FogParams fog = ref FogForModify();
109+
fog.Enable = false;
110+
fog.MaxDensity = 1.0f;
111+
fog.HDRColorScale = 1.0f;
112+
fog.MaxDensityLerpTo = -1.0f;
113+
}
114+
115+
static int AbsTrunc(float value) {
116+
int i = (int)value;
117+
return i < 0 ? -i : i;
118+
}
119+
120+
public override void Spawn() {
121+
base.Spawn();
122+
123+
ref FogParams fog = ref FogForModify();
124+
fog.ColorPrimaryLerpTo = fog.ColorPrimary;
125+
fog.ColorSecondaryLerpTo = fog.ColorSecondary;
126+
127+
if (fog.MaxDensity > 0.95 && fog.Enable) {
128+
if ((fog.FarZ < 250.0f && fog.FarZ > 0.0f) || (AbsTrunc(fog.End - fog.Start) <= 4 && (AbsTrunc(fog.Start) <= 49 || AbsTrunc(fog.End) <= 49))) {
129+
Warning("Deleting env_fog_controller because it has silly values for Start/End/FarZ.\n");
130+
Util.Remove(this);
131+
}
132+
}
133+
}
134+
135+
public override void Activate() {
136+
base.Activate();
137+
138+
if (UseAngles) {
139+
ref FogParams fog = ref FogForModify();
140+
MathLib.AngleVectors(GetAbsAngles(), out fog.DirPrimary);
141+
fog.DirPrimary *= -1.0f;
142+
}
143+
}
144+
145+
public override EdictFlags UpdateTransmitState() => SetTransmitState(EdictFlags.Always);
146+
147+
public bool IsMaster() => HasSpawnFlags(SF_FOG_MASTER);
148+
149+
public void InputSetStartDist(InputData inputdata) => FogForModify().Start = inputdata.Value.Float();
150+
151+
public void InputSetEndDist(InputData inputdata) => FogForModify().End = inputdata.Value.Float();
152+
153+
// Input handler for setting the maximum density of the fog. This lets us bring
154+
// the start distance in without the scene fogging too much.
155+
public void InputSetMaxDensity(InputData inputdata) => FogForModify().MaxDensity = inputdata.Value.Float();
156+
157+
public void InputTurnOn(InputData inputdata) => FogForModify().Enable = true;
158+
159+
public void InputTurnOff(InputData inputdata) => FogForModify().Enable = false;
160+
161+
public void InputSetColor(InputData inputdata) => FogForModify().ColorPrimary = inputdata.Value.Color32();
162+
163+
public void InputSetColorSecondary(InputData inputdata) => FogForModify().ColorSecondary = inputdata.Value.Color32();
164+
165+
public void InputSetFarZ(InputData inputdata) {
166+
int farZ = inputdata.Value.Int();
167+
if ((uint)(farZ - 1) <= 248) {
168+
Warning($"Ignoring SetFarZ on fog controller: Silly value {farZ} given!\n");
169+
return;
170+
}
171+
172+
FogForModify().FarZ = farZ;
173+
}
174+
175+
public void InputSetColorHDR(InputData inputdata) => FogForModify().ColorPrimaryHDR = inputdata.Value.Color32();
176+
177+
public void InputSetColorSecondaryHDR(InputData inputdata) => FogForModify().ColorSecondaryHDR = inputdata.Value.Color32();
178+
179+
public void InputSetAngles(InputData inputdata) {
180+
Span<float> anglesBase = stackalloc float[3];
181+
UTIL_StringToVector(anglesBase, inputdata.Value.String());
182+
QAngle angles = new(anglesBase[0], anglesBase[1], anglesBase[2]);
183+
184+
SetAbsAngles(angles);
185+
186+
ref FogParams fog = ref FogForModify();
187+
MathLib.AngleVectors(GetAbsAngles(), out fog.DirPrimary);
188+
fog.DirPrimary *= -1.0f;
189+
}
190+
191+
public void InputSetRadial(InputData inputdata) => FogForModify().Radial = inputdata.Value.Bool();
192+
193+
public override int DrawDebugTextOverlays() {
194+
int textOffset = base.DrawDebugTextOverlays();
195+
196+
if ((DebugOverlays & DebugOverlayBits.Text) != 0) {
197+
ref readonly FogParams fog = ref Fog;
198+
199+
EntityText(textOffset++, $"State: {(fog.Enable ? "On" : "Off")}", 0);
200+
EntityText(textOffset++, $"Start: {fog.Start,3:0}", 0);
201+
EntityText(textOffset++, $"End : {fog.End,3:0}", 0);
202+
203+
Color color = fog.ColorPrimary;
204+
EntityText(textOffset++, $"1) Red : {color.R}", 0);
205+
EntityText(textOffset++, $"1) Green: {color.G}", 0);
206+
EntityText(textOffset++, $"1) Blue : {color.B}", 0);
207+
208+
color = fog.ColorSecondary;
209+
EntityText(textOffset++, $"2) Red : {color.R}", 0);
210+
EntityText(textOffset++, $"2) Green: {color.G}", 0);
211+
EntityText(textOffset++, $"2) Blue : {color.B}", 0);
212+
}
213+
214+
return textOffset;
215+
}
216+
217+
public void InputSetColorLerpTo(InputData inputdata) {
218+
ChangedVariables |= FOG_CONTROLLER_COLORPRIMARY_LERP;
219+
FogForModify().ColorPrimaryLerpTo = inputdata.Value.Color32();
220+
}
221+
222+
public void InputSetColorSecondaryLerpTo(InputData inputdata) {
223+
ChangedVariables |= FOG_CONTROLLER_COLORSECONDARY_LERP;
224+
FogForModify().ColorSecondaryLerpTo = inputdata.Value.Color32();
225+
}
226+
227+
public void InputSetStartDistLerpTo(InputData inputdata) {
228+
ChangedVariables |= FOG_CONTROLLER_START_LERP;
229+
FogForModify().StartLerpTo = inputdata.Value.Float();
230+
}
231+
232+
public void InputSetEndDistLerpTo(InputData inputdata) {
233+
ChangedVariables |= FOG_CONTROLLER_END_LERP;
234+
FogForModify().EndLerpTo = inputdata.Value.Float();
235+
}
236+
237+
public void InputSetMaxDensityLerpTo(InputData inputdata) {
238+
ChangedVariables |= FOG_CONTROLLER_MAXDENSITY_LERP;
239+
FogForModify().MaxDensityLerpTo = inputdata.Value.Float();
240+
}
241+
242+
public void InputStartFogTransition(InputData inputdata) {
243+
SetThink(SetLerpValues);
244+
245+
ref FogParams fog = ref FogForModify();
246+
fog.LerpTime = gpGlobals.CurTime + fog.Duration + 0.1;
247+
SetNextThink(gpGlobals.CurTime + fog.Duration);
248+
}
249+
250+
public void SetLerpValues() {
251+
ref FogParams fog = ref FogForModify();
252+
253+
if ((ChangedVariables & FOG_CONTROLLER_COLORPRIMARY_LERP) != 0)
254+
fog.ColorPrimary = fog.ColorPrimaryLerpTo;
255+
256+
if ((ChangedVariables & FOG_CONTROLLER_COLORSECONDARY_LERP) != 0)
257+
fog.ColorSecondary = fog.ColorSecondaryLerpTo;
258+
259+
if ((ChangedVariables & FOG_CONTROLLER_START_LERP) != 0)
260+
fog.Start = fog.StartLerpTo;
261+
262+
if ((ChangedVariables & FOG_CONTROLLER_END_LERP) != 0)
263+
fog.End = fog.EndLerpTo;
264+
265+
if ((ChangedVariables & FOG_CONTROLLER_MAXDENSITY_LERP) != 0)
266+
fog.MaxDensity = fog.MaxDensityLerpTo;
267+
268+
ChangedVariables = 0;
269+
fog.LerpTime = gpGlobals.CurTime;
270+
}
271+
}
272+
273+
public class FogSystem(ReadOnlySpan<char> name) : AutoGameSystem(name), IGameEventListener2
274+
{
275+
public static readonly FogSystem s_FogSystem = new("FogSystem");
276+
277+
Handle<FogController> MasterController = new();
278+
279+
// Clear out the fog controller.
280+
public override void LevelInitPreEntity() {
281+
MasterController = new();
282+
gameeventmanager?.AddListener(this, "round_start", true);
283+
}
284+
285+
// On level load find the master fog controller. If no controller is
286+
// set as Master, use the first fog controller found.
287+
public void FireGameEvent(IGameEvent ev) => InitMasterController();
288+
289+
public override void LevelInitPostEntity() {
290+
InitMasterController();
291+
292+
// HACK: Singleplayer games don't get a call to CBasePlayer::Spawn on level transitions.
293+
// CBasePlayer::Activate is called before this is called so that's too soon to set up the fog controller.
294+
// We don't have a hook similar to Activate that happens after LevelInitPostEntity
295+
// is called, or we could just do this in the player itself.
296+
if (gpGlobals.MaxClients == 1) {
297+
BasePlayer? player = Util.GetLocalPlayer();
298+
if (player != null && player.Local.PlayerFog.Ctrl.Get() == null)
299+
player.InitFogController();
300+
}
301+
}
302+
303+
void InitMasterController() {
304+
FogController? fogController = null;
305+
while ((fogController = (FogController?)gEntList.FindEntityByClassname(fogController, "env_fog_controller")) != null) {
306+
if (fogController.IsEFlagSet(EFL.KillMe))
307+
continue;
308+
309+
if (MasterController.Get() == null || fogController.IsMaster())
310+
MasterController.Set(fogController);
311+
}
312+
}
313+
314+
public void SetMasterController(FogController? master) => MasterController.Set(master);
315+
316+
public FogController? GetMasterFogController() => MasterController.Get();
37317
}

‎Game.Server/GameInterface.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ public bool DLLInit(IServiceProvider services) {
333333
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(LagCompensationGlobals).TypeHandle);
334334
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Templates).TypeHandle);
335335
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(PointTemplatePrecacher).TypeHandle);
336+
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(FogSystem).TypeHandle);
336337

337338
#if GMOD_DLL
338339
if (CommandLine.FindParm("-noaddons") == 0)

‎Game.Server/Player.cs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public void SetArmorValue(int value) {
270270

271271
public static readonly new DataMap DataDesc = new(typeof(BasePlayer), BaseCombatCharacter.DataDesc, [
272272
DEFINE<BasePlayer>.INPUTFUNC(FieldType.Integer, "SetHealth", nameof(InputSetHealth), (INPUTFUNCPTR)((self, data) => ((BasePlayer)self).InputSetHealth(data))),
273+
DEFINE<BasePlayer>.INPUTFUNC(FieldType.String, "SetFogController", nameof(InputSetFogController), (INPUTFUNCPTR)((self, data) => ((BasePlayer)self).InputSetFogController(data))),
273274
]);
274275
public override DataMap? GetDataDescMap() => DataDesc;
275276

@@ -295,6 +296,18 @@ public void InputSetHealth(InputData inputdata) {
295296
}
296297
}
297298

299+
// Set the fog controller data per player.
300+
public void InputSetFogController(InputData inputdata) {
301+
// Find the fog controller with the given name.
302+
if (gEntList.FindEntityByName(null, inputdata.Value.String()) is FogController fogController)
303+
Local.PlayerFogForModify().Ctrl.Set(fogController);
304+
}
305+
306+
public void InitFogController() {
307+
// Setup with the default master controller.
308+
Local.PlayerFogForModify().Ctrl.Set(FogSystem.s_FogSystem.GetMasterFogController());
309+
}
310+
298311
static InlineArray1024<char> InfoBuffer;
299312

300313
public ReadOnlySpan<char> GetInfo(ReadOnlySpan<char> name) {
@@ -1112,7 +1125,7 @@ public override void Spawn() {
11121125

11131126
IncrementInterpolationFrame();
11141127

1115-
// InitFogController();
1128+
InitFogController();
11161129

11171130
DmgTake = 0;
11181131
DmgSave = 0;

‎Game.Server/SkyCamera.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public SkyCamera() {
5151
Next = ClassList;
5252
ClassList = this;
5353
SkyboxData.Fog.MaxDensity = 1.0f;
54+
SkyboxData.Fog.HDRColorScale = 1.0f;
55+
SkyboxData.Fog.MaxDensityLerpTo = -1.0f;
5456
}
5557

5658
public override void UpdateOnRemove() {
@@ -85,12 +87,20 @@ public override bool KeyValue(ReadOnlySpan<char> keyName, ReadOnlySpan<char> val
8587
Util.StringToColor32(out SkyboxData.Fog.ColorPrimary, value);
8688
else if (FStrEq(keyName, "fogcolor2"))
8789
Util.StringToColor32(out SkyboxData.Fog.ColorSecondary, value);
90+
else if (FStrEq(keyName, "fogcolor_hdr"))
91+
Util.StringToColor32(out SkyboxData.Fog.ColorPrimaryHDR, value);
92+
else if (FStrEq(keyName, "fogcolor2_hdr"))
93+
Util.StringToColor32(out SkyboxData.Fog.ColorSecondaryHDR, value);
8894
else if (FStrEq(keyName, "fogstart"))
8995
SkyboxData.Fog.Start = strtof(value, out _);
9096
else if (FStrEq(keyName, "fogend"))
9197
SkyboxData.Fog.End = strtof(value, out _);
9298
else if (FStrEq(keyName, "fogmaxdensity"))
9399
SkyboxData.Fog.MaxDensity = strtof(value, out _);
100+
else if (FStrEq(keyName, "fogradial"))
101+
SkyboxData.Fog.Radial = atoi(value) != 0;
102+
else if (FStrEq(keyName, "HDRColorScale"))
103+
SkyboxData.Fog.HDRColorScale = strtof(value, out _);
94104
else
95105
return base.KeyValue(keyName, value);
96106

0 commit comments

Comments
 (0)