Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_NightModeGameModeComponent.c
Go to the documentation of this file.
1[ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "Component to handle night mode")]
5
6class SCR_NightModeGameModeComponent : SCR_BaseGameModeComponent
7{
8 [Attribute("0", desc: "Set true if you want the GM to be able to set global nightmode. Do not change in runtime", category: "Settings")]
9 protected bool m_bAllowGlobalNightMode;
10
11 [Attribute("0.1", desc: "0 means the sun is setting, 1 means the max value the sun is under (though in reality max value is around 0.5 and the sun is fully set around 0.1) this value dictates when the max nightmode EV value is reached if the sun is at the current state", params: "0.001 1", category: "Settings")]
12 protected float m_fSunStateMaxNightMode;
13
14 [Attribute("0", desc: "Value of EV when nightmode is NOT enabled", params: "0 12", category: "Settings")]
15 protected int m_iEVValueNormalMode;
16
17 [Attribute("2", desc: "Value of EV when nightmode is enabled", params: "0 12", category: "Settings")]
18 protected int m_iEVValueNightMode;
19
20 [Attribute("SOUND_E_TOGGLE_FLASHLIGHT", desc: "SFX when local editor nightMode is enabled/disabled", category: "Sound")]
21 protected string m_sLocalEditorNightModeToggledSound;
22
23 [Attribute("ManualCameraLight", desc: "Local nightmode shortcut. Toggle On and off")]
24 protected string m_sToggleLocalNightModeShortcut;
25
26 //~ States
27 protected bool m_bLocalEditorNightModeEnabled;
28 protected bool m_bLocalEditorNightModeOnEditorClose;
29 protected bool m_bUnlimitedEditorIsOpen;
30 protected bool m_bGlobalNightModeEnabled;
31 protected bool m_IsPreviewingTimeOrWeather;
32 protected float m_fNightModeAlpha = 0;
33
34 //~ Update
35 protected bool m_bUpdateEachFrame;
36 protected float m_fCurrentUpdateTickInSeconds;
37 protected static const float UPDATE_TICK_IN_SECONDS = 1;
38
39 //~ Ref
40 protected BaseWorld m_World;
41 protected TimeAndWeatherManagerEntity m_TimeAndWeatherManager;
43 protected BaseWeatherStateTransitionManager m_WeatherStateTransitionManager;
44
45 //~ Script Invokers
46 protected ref ScriptInvokerBool m_OnLocalEditorNightModeEnabledChanged;
47 protected ref ScriptInvokerBool m_OnGlobalNightModeEnabledChanged;
48 protected ref ScriptInvokerBool m_OnNightModeEnabledChanged;
49
50 //------------------------------------------------------------------------------------------------
51 //~ Onframe is only active if LocalEditor and/or Global nightmode is enabled. The EV value depends on where the sun is in the sky to gratually brighten the night as the night gets darker
52 override void EOnFrame(IEntity owner, float timeSlice)
53 {
54 //~ If onframe is enabled but sun is not set check each second not each frame
55 if (!m_bUpdateEachFrame)
56 {
57 m_fCurrentUpdateTickInSeconds += timeSlice;
58
59 if (m_fCurrentUpdateTickInSeconds < UPDATE_TICK_IN_SECONDS)
60 return;
61 else
62 m_fCurrentUpdateTickInSeconds = 0;
63 }
64
65 //~ Get sun direction
66 vector Sundirection, moondirection;
67 float moonphase;
68 m_TimeAndWeatherManager.GetCurrentSunMoonDirAndPhase(Sundirection, moondirection, moonphase);
69
70 //~ Normalize sun direction using m_fSunStateMaxNightMode as value when the EV is at it's strongest if the sun is down
71 float normalizedSun = Math.Clamp(Sundirection[1] / m_fSunStateMaxNightMode, 0, 1);
72
73 //~ Check if it should update each frame if sun is at a state that the EV is set
74 m_bUpdateEachFrame = (normalizedSun != 0);
75
76 //~ If alpha value changed update EV otherwise do nothing to prevent the EV from updating unncessesarrly
77 if (normalizedSun != m_fNightModeAlpha)
78 {
79 m_fNightModeAlpha = normalizedSun;
80 m_World.AdjustCameraEV(0, Math.Lerp(m_iEVValueNormalMode, m_iEVValueNightMode, m_fNightModeAlpha));
81 }
82 }
83
84 //------------------------------------------------------------------------------------------------
87 {
88 return m_bLocalEditorNightModeEnabled;
89 }
90
91 //------------------------------------------------------------------------------------------------
93 {
94 EnableLocalEditorNightMode(!m_bLocalEditorNightModeEnabled);
95 }
96
97 //------------------------------------------------------------------------------------------------
102 void EnableLocalEditorNightMode(bool enable, bool playSound = true)
103 {
104 if (m_bLocalEditorNightModeEnabled == enable)
105 return;
106
107 //~ Cannot set nightmode if editor is not unlimited
108 if (enable && (!m_EditorManager || m_EditorManager.IsLimited() || !CanEnableNightMode()))
109 return;
110
111 m_bLocalEditorNightModeEnabled = enable;
112
113 EnableNightMode(enable);
114
115 if (playSound && !SCR_StringHelper.IsEmptyOrWhiteSpace(m_sLocalEditorNightModeToggledSound))
116 SCR_UISoundEntity.SoundEvent(m_sLocalEditorNightModeToggledSound);
117
118 //~ Global night mode state changed
119 if (m_OnLocalEditorNightModeEnabledChanged)
120 m_OnLocalEditorNightModeEnabledChanged.Invoke(enable);
121 }
122
123 //------------------------------------------------------------------------------------------------
126 {
127 return m_bAllowGlobalNightMode;
128 }
129
130 //------------------------------------------------------------------------------------------------
133 {
134 return m_bGlobalNightModeEnabled;
135 }
136
137 //------------------------------------------------------------------------------------------------
142 void EnableGlobalNightMode(bool enable, int nightModeChangerPlayerID = -1)
143 {
144 //~ Server only and check if value can be changed and is changed
145 if (m_bGlobalNightModeEnabled == enable || !IsGlobalNightModeAllowed() || !GetGameMode().IsMaster())
146 return;
147
148 if (enable && !CanEnableNightMode())
149 return;
150
151 //~ Execute enable nightmode logic
152 RPC_EnableGlobalNightMode(enable, nightModeChangerPlayerID);
153 Rpc(RPC_EnableGlobalNightMode, enable, nightModeChangerPlayerID);
154 }
155
156 //------------------------------------------------------------------------------------------------
157 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
158 protected void RPC_EnableGlobalNightMode(bool enable, int nightModeChangerPlayerID)
159 {
160 //~ Cannot change global nightmode or is already the same state as given state
161 if (m_bGlobalNightModeEnabled == enable || !IsGlobalNightModeAllowed())
162 return;
163
164 m_bGlobalNightModeEnabled = enable;
165
166 //~ Enable the nightmode
167 EnableNightMode(enable);
168
169 //~ Global night mode state changed
170 if (m_OnGlobalNightModeEnabledChanged)
171 m_OnGlobalNightModeEnabledChanged.Invoke(enable);
172
173 //~ Send notification
174 if (nightModeChangerPlayerID > 0)
175 {
176 if (enable)
177 SCR_NotificationsComponent.SendLocal(ENotification.EDITOR_GLOBAL_NIGHTMODE_ENABLED, nightModeChangerPlayerID);
178 else
179 SCR_NotificationsComponent.SendLocal(ENotification.EDITOR_GLOBAL_NIGHTMODE_DISABLED, nightModeChangerPlayerID);
180 }
181 }
182
183 //------------------------------------------------------------------------------------------------
189
190 //------------------------------------------------------------------------------------------------
193 {
194 return m_TimeAndWeatherManager != null;
195 }
196
197 //------------------------------------------------------------------------------------------------
198 //~ Called by EnableLocal or EnableGlobal night mode functions which actually enables the night mode
199 protected void EnableNightMode(bool enable)
200 {
201 //~ If trying to enable but cannot enable if both LocalEditor and GlobalNightmode are not enable do not enable
202 if (enable && (!CanEnableNightMode() || (!IsLocalEditorNightModeEnabled() || (IsLocalEditorNightModeEnabled() && !m_bUnlimitedEditorIsOpen)) && !IsGlobalNightModeEnabled()))
203 return;
204 //~ If trying to disable but localEditorNight mode is enabled (and unlimited editor is open) or Global Night mode is enabled than do not disable
205 else if (!enable && ((IsLocalEditorNightModeEnabled() && m_bUnlimitedEditorIsOpen) || IsGlobalNightModeEnabled()))
206 return;
207
208 //~ On Night mode enabled start on frame
209 if (IsNightModeEnabled() && !SCR_Enum.HasPartialFlag(GetEventMask(), EntityEvent.FRAME))
210 {
211 SetEventMask(GetOwner(), EntityEvent.FRAME);
212 }
213 //~ On nightmode disabled stop on frame and set EV to default
214 else if (!IsNightModeEnabled())
215 {
216 //~ Set EV to default
217 if (m_fNightModeAlpha != 0)
218 {
219 m_fNightModeAlpha = 0;
220 m_World.AdjustCameraEV(0, m_iEVValueNormalMode);
221 }
222
223 //~ Stop OnFrame
224 if (SCR_Enum.HasPartialFlag(GetEventMask(), EntityEvent.FRAME))
225 ClearEventMask(GetOwner(), EntityEvent.FRAME);
226 }
227
228 //~ Reset update values
229 if (enable)
230 {
231 m_bUpdateEachFrame = true;
232 m_fCurrentUpdateTickInSeconds = 0;
233 }
234
235 //~ Nightmode state changed (Can be by localEditor or GlobalNightMode)
236 if (m_OnNightModeEnabledChanged)
237 m_OnNightModeEnabledChanged.Invoke(enable);
238
239 //~ Check if previewing time or weather state and set the correct settings
241 }
242
243 //------------------------------------------------------------------------------------------------
244 protected void OnEditorOpened()
245 {
246 m_bUnlimitedEditorIsOpen = !m_EditorManager.IsLimited();
247
248 if (m_bUnlimitedEditorIsOpen)
249 EnableLocalEditorNightMode(m_bLocalEditorNightModeOnEditorClose);
250 }
251
252 //------------------------------------------------------------------------------------------------
253 protected void OnEditorClosed()
254 {
255 m_bUnlimitedEditorIsOpen = false;
256
257 //~ Disable lightmode as GlobalLightmode was disabled
258 if (!m_EditorManager.IsLimited())
259 {
260 m_bLocalEditorNightModeOnEditorClose = IsLocalEditorNightModeEnabled();
262 }
263 }
264
265 //------------------------------------------------------------------------------------------------
266 protected void OnEditorLimitedChanged(bool isLimited)
267 {
268 //~ If limited disable local editor nightmode until rights are returned and the player turns it on again
269 if (isLimited)
271 }
272
273 //------------------------------------------------------------------------------------------------
275 {
276 //~ Check if previewing state changed
277 bool newIsPreviewing = m_WeatherStateTransitionManager.IsPreviewingWind() || m_WeatherStateTransitionManager.IsPreviewingState() || m_WeatherStateTransitionManager.IsPreviewingDateTime();
278 if (m_IsPreviewingTimeOrWeather == newIsPreviewing)
279 return;
280
281 m_IsPreviewingTimeOrWeather = newIsPreviewing;
282
283 //~ If local nightmode is enabled but global is not and previewing the scene then turn off nightmode
284 if (!m_bUnlimitedEditorIsOpen || !IsLocalEditorNightModeEnabled() || IsGlobalNightModeEnabled())
285 return;
286
287 if (m_IsPreviewingTimeOrWeather)
288 {
289 //~ Pause the OnFrame
290 if (SCR_Enum.HasPartialFlag(GetEventMask(), EntityEvent.FRAME))
291 ClearEventMask(GetOwner(), EntityEvent.FRAME);
292
293 //~ Disable nightmode
294 if (m_fNightModeAlpha != 0)
295 {
296 m_fNightModeAlpha = 0;
297 m_World.AdjustCameraEV(0, m_iEVValueNormalMode);
298
299 SCR_NotificationsComponent.SendLocal(ENotification.EDITOR_PREVIEWING_IN_NIGHTMODE);
300
301 if (!SCR_StringHelper.IsEmptyOrWhiteSpace(m_sLocalEditorNightModeToggledSound))
302 SCR_UISoundEntity.SoundEvent(m_sLocalEditorNightModeToggledSound);
303 }
304 }
305 else
306 {
307 //~ Pause continue the on frame again
308 if (!SCR_Enum.HasPartialFlag(GetEventMask(), EntityEvent.FRAME))
309 {
310 SetEventMask(GetOwner(), EntityEvent.FRAME);
311
312 if (m_TimeAndWeatherManager.IsSunSet() && !SCR_StringHelper.IsEmptyOrWhiteSpace(m_sLocalEditorNightModeToggledSound))
313 SCR_UISoundEntity.SoundEvent(m_sLocalEditorNightModeToggledSound);
314 }
315 }
316 }
317
318 //------------------------------------------------------------------------------------------------
319 protected void OnWeatherStatePreview(bool preview, string stateName)
320 {
322 }
323
324 //------------------------------------------------------------------------------------------------
325 protected void OnWindPreview(bool preview, float windSpeed = -1, float windAngleDegrees = -1)
326 {
328 }
329
330 //------------------------------------------------------------------------------------------------
331 protected void OnDateTimePreview(bool preview, int year = -1, int month = -1, int day = -1, float timeOfTheDay = -1)
332 {
334 }
335
336 //------------------------------------------------------------------------------------------------
339 {
340 if (!m_OnLocalEditorNightModeEnabledChanged)
341 m_OnLocalEditorNightModeEnabledChanged = new ScriptInvokerBool();
342
343 return m_OnLocalEditorNightModeEnabledChanged;
344 }
345
346 //------------------------------------------------------------------------------------------------
349 {
350 if (!m_OnGlobalNightModeEnabledChanged)
351 m_OnGlobalNightModeEnabledChanged = new ScriptInvokerBool();
352
353 return m_OnGlobalNightModeEnabledChanged;
354 }
355
356 //------------------------------------------------------------------------------------------------
359 {
360 if (!m_OnNightModeEnabledChanged)
361 m_OnNightModeEnabledChanged = new ScriptInvokerBool();
362
363 return m_OnNightModeEnabledChanged;
364 }
365
366 //------------------------------------------------------------------------------------------------
368 {
369 //~ Editor Manager not yet set so that means it was obtained by editorManagerCore. Make sure to no longer listen to OnEditorManagerInitOwner
370 if (!m_EditorManager)
371 {
372 m_EditorManager = editorManager;
373
375 if (core)
376 core.Event_OnEditorManagerInitOwner.Remove(OnEditorManagerCreated);
377 }
378
379 //~ Listen to events
380 m_EditorManager.GetOnOpened().Insert(OnEditorOpened);
381 m_EditorManager.GetOnClosed().Insert(OnEditorClosed);
382 m_EditorManager.GetOnLimitedChange().Insert(OnEditorLimitedChanged);
383
384 if (m_EditorManager.IsOpened())
386 }
387
388 //------------------------------------------------------------------------------------------------
389 override void EOnInit(IEntity owner)
390 {
392 return;
393
394 GetGame().GetInputManager().AddActionListener(m_sToggleLocalNightModeShortcut, EActionTrigger.DOWN, ToggleLocalNightModeShortcut);
395
396 //~ Get world
397 m_World = GetOwner().GetWorld();
398 if (!m_World)
399 {
400 m_World = GetGame().GetWorld();
401 if (!m_World)
402 Print("'SCR_NightModeGameModeComponent' could not find World this means nightmode can never be enabled!", LogLevel.ERROR);
403
404 return;
405 }
406
407 //~ Get time manager
408 ChimeraWorld world = ChimeraWorld.CastFrom(m_World);
409 if (!world)
410 return;
411
412 m_TimeAndWeatherManager = world.GetTimeAndWeatherManager();
413 if (!m_TimeAndWeatherManager)
414 {
415 Print("'SCR_NightModeGameModeComponent' could not find TimeAndWeatherEntity this means nightmode can never be enabled!", LogLevel.ERROR);
416 return;
417 }
418
419 //~ Get transition manager to make sure it can check if dateTime or weather is being previewed
420 m_WeatherStateTransitionManager = m_TimeAndWeatherManager.GetTransitionManager();
421 if (m_WeatherStateTransitionManager)
422 {
423 m_TimeAndWeatherManager.GetOnWeatherStatePreview().Insert(OnWeatherStatePreview);
424 m_TimeAndWeatherManager.GetOnWindPreview().Insert(OnWindPreview);
425 m_TimeAndWeatherManager.GetOnDateTimePreview().Insert(OnDateTimePreview);
426 }
427
428 //~ Get EditorManager
429 SCR_EditorManagerEntity editorManager = SCR_EditorManagerEntity.GetInstance();
430 if (editorManager)
431 {
432 m_EditorManager = editorManager;
433 OnEditorManagerCreated(editorManager);
434 }
435 else
436 {
437 //~ Editor manager does not exist yet (e.g., on mission start), wait for it to be created
439 if (core)
440 core.Event_OnEditorManagerInitOwner.Insert(OnEditorManagerCreated);
441 }
442 }
443
444 //------------------------------------------------------------------------------------------------
445 override void OnPostInit(IEntity owner)
446 {
447 SetEventMask(owner, EntityEvent.INIT);
448 }
449
450 //------------------------------------------------------------------------------------------------
451 override void OnDelete(IEntity owner)
452 {
454 return;
455
456 super.OnDelete(owner);
457
458 if (m_EditorManager)
459 {
460 m_EditorManager.GetOnOpened().Remove(OnEditorOpened);
461 m_EditorManager.GetOnClosed().Remove(OnEditorClosed);
462 m_EditorManager.GetOnLimitedChange().Remove(OnEditorLimitedChanged);
463 }
464
465 if (m_TimeAndWeatherManager && m_WeatherStateTransitionManager)
466 {
467 m_TimeAndWeatherManager.GetOnWeatherStatePreview().Remove(OnWeatherStatePreview);
468 m_TimeAndWeatherManager.GetOnWindPreview().Remove(OnWindPreview);
469 m_TimeAndWeatherManager.GetOnDateTimePreview().Remove(OnDateTimePreview);
470 }
471
472 GetGame().GetInputManager().RemoveActionListener(m_sToggleLocalNightModeShortcut, EActionTrigger.DOWN, ToggleLocalNightModeShortcut);
473 }
474
475 //------------------------------------------------------------------------------------------------
476 override bool RplSave(ScriptBitWriter writer)
477 {
479 writer.WriteBool(m_bGlobalNightModeEnabled);
480
481 return true;
482 }
483
484 //------------------------------------------------------------------------------------------------
485 override bool RplLoad(ScriptBitReader reader)
486 {
487 bool globalNightModeEnabled;
488
490 {
491 reader.ReadBool(globalNightModeEnabled);
492 RPC_EnableGlobalNightMode(globalNightModeEnabled, -1);
493 }
494
495 return true;
496 }
497}
ENotification
ArmaReforgerScripted GetGame()
Definition game.c:1398
override bool RplLoad(ScriptBitReader reader)
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_BaseGameMode GetGameMode()
void SCR_BaseGameModeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
override bool RplSave(ScriptBitWriter writer)
void OnEditorClosed()
Method called when character has a shovel in hands and any editor mode is closed.
void OnEditorOpened()
Method called when character has a shovel in hands and any editor mode is opned.
void SCR_EditorManagerEntity(IEntitySource src, IEntity parent)
bool IsGlobalNightModeAllowed()
void OnWeatherStatePreview(bool preview, string stateName)
bool IsNightModeEnabled()
bool IsLocalEditorNightModeEnabled()
bool CanEnableNightMode()
ScriptInvokerBool GetOnNightModeEnabledChanged()
void OnEditorLimitedChanged(bool isLimited)
void OnEditorManagerCreated(SCR_EditorManagerEntity editorManager)
ScriptInvokerBool GetOnGlobalNightModeEnabledChanged()
void UpdatePreviewingTimeOrWeather()
ScriptInvokerBool GetOnLocalEditorNightModeEnabledChanged()
void ToggleLocalNightModeShortcut()
void EnableLocalEditorNightMode(bool enable, bool playSound=true)
void EnableNightMode(bool enable)
void OnDateTimePreview(bool preview, int year=-1, int month=-1, int day=-1, float timeOfTheDay=-1)
bool IsGlobalNightModeEnabled()
void OnWindPreview(bool preview, float windSpeed=-1, float windAngleDegrees=-1)
void RPC_EnableGlobalNightMode(bool enable, int nightModeChangerPlayerID)
void EnableGlobalNightMode(bool enable, int nightModeChangerPlayerID=-1)
override void EOnFrame(IEntity owner, float timeSlice)
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
ScriptInvokerBase< ScriptInvokerBoolMethod > ScriptInvokerBool
SCR_EditorManagerEntity m_EditorManager
proto external BaseWorld GetWorld()
Core component to manage SCR_EditorManagerEntity.
static bool IsEditMode()
Definition Functions.c:1566
static bool IsEmptyOrWhiteSpace(string input)
ScriptInvokerBase< SCR_TimeAndWeatherManager_OnWindPreview > GetOnWindPreview()
ScriptInvokerBase< SCR_TimeAndWeatherManager_OnWeatherStatePreview > GetOnWeatherStatePreview()
ScriptInvokerBase< SCR_TimeAndWeatherManager_OnDateTimePreview > GetOnDateTimePreview()
IEntity GetOwner()
Owner entity of the fuel tank.
override void EOnInit(IEntity owner)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
EntityEvent
Various entity events.
Definition EntityEvent.c:14
EActionTrigger
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14