11using Game . Shared ;
22
3+ using Source ;
34using Source . Common ;
5+ using Source . Common . Engine ;
6+ using Source . Common . Mathematics ;
7+
8+ using System . Numerics ;
49
510namespace Game . Server ;
611
12+ using DEFINE = Source . DEFINE < FogController > ;
13+
714[ LinkEntityToClass ( "env_fog_controller" ) ]
815[ NetworkName ( "CFogController" ) ]
916public 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}
0 commit comments