Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_TimeAndWeatherHandlerComponent.c
Go to the documentation of this file.
2 {
3 }
4 
5 class SCR_TimeAndWeatherHandlerComponent : SCR_BaseGameModeComponent
6 {
7  [Attribute("8", UIWidgets.Slider, "Starting time of day (hours)", "0 23 1")]
8  protected int m_iStartingHours;
9 
10  [Attribute("0", UIWidgets.Slider, "Starting time of day (minutes)", "0 59 1")]
11  protected int m_iStartingMinutes;
12 
13  [Attribute("0")]
14  protected bool m_bRandomStartingDaytime;
15 
16  [Attribute("1", UIWidgets.Slider, "Time acceleration during the day (1 = 100%, 2 = 200% etc)", "0.1 12 0.1")]
17  protected float m_fDayTimeAcceleration;
18 
19  [Attribute("1", UIWidgets.Slider, "Time acceleration during the night (1 = 100%, 2 = 200% etc)", "0.1 12 0.1")]
20  protected float m_fNightTimeAcceleration;
21 
22  [Attribute("0")]
23  protected bool m_bRandomStartingWeather;
24 
25  [Attribute("0", desc: "Weather can change during gameplay")]
26  protected bool m_bRandomWeatherChanges;
27 
28  [Attribute("0", desc: "Scenario header setting will overwrite these values.")]
29  protected bool m_bAllowHeaderSettings;
30 
31  const int DAYTIME_CHECK_PERIOD = 60000; //ms
32  const float DEFAULT_DAYTIME_START = 5.0;
33  const float DEFAULT_DAYTIME_END = 19.0;
34  const int DAY_DURATION = 24 * 60 * 60;
35 
36  protected bool m_bDaytimeAcceleration = true;
37  protected bool m_bSavedSettingApplied = false;
38 
39  protected static SCR_TimeAndWeatherHandlerComponent s_Instance;
40 
41  //------------------------------------------------------------------------------------------------
43  static SCR_TimeAndWeatherHandlerComponent GetInstance()
44  {
45  return s_Instance;
46  }
47 
48  //------------------------------------------------------------------------------------------------
54  void SetupDaytimeAndWeather(int hours, int minutes, int seconds = 0, string loadedWeatherState = "", bool loadDone = false)
55  {
57  return;
58 
59  m_bSavedSettingApplied = loadDone;
60 
61  ChimeraWorld world = ChimeraWorld.CastFrom(GetOwner().GetWorld());
62  if (!world)
63  return;
64 
65  TimeAndWeatherManagerEntity manager = world.GetTimeAndWeatherManager();
66  if (!manager)
67  return;
68 
69  float sunrise, morning, evening, sunset;
70 
71  // Use world-calculated sunrise and sunset values if possible, otherwise use defaults
72  if (manager.GetSunriseHour(sunrise))
73  {
74  manager.GetSunsetHour(sunset);
75  }
76  else
77  {
78  sunrise = DEFAULT_DAYTIME_START;
79  sunset = DEFAULT_DAYTIME_END;
80  }
81 
82  if (m_bRandomStartingDaytime && !loadDone)
83  {
84  // Compile a list of presets based on the sunrise and sunset times of current world if we're randomizing
85  morning = sunrise + 0.25; // Just so it's not still completely dark at the start
86  float noon = (sunrise + sunset) * 0.5;
87  float afternoon = (noon + sunset) * 0.5;
88  evening = sunset - 0.5;
89  float night = noon + 12;
90 
91  if (night >= 24)
92  night -= 24;
93 
94  array<float> startingTimes = {morning, noon, afternoon, evening, night};
95 
96  // Add weights so evening / night is a bit more rare
97  Math.Randomize(-1);
98  int index = SCR_ArrayHelper.GetWeightedIndex({25, 25, 25, 15, 10}, Math.RandomFloat01());
99  float startingTime;
100 
101  if (startingTimes.IsIndexValid(index))
102  startingTime = startingTimes[index];
103  else
104  startingTime = startingTimes.GetRandomElement();
105 
106  manager.TimeToHoursMinutesSeconds(startingTime, hours, minutes, seconds);
107  }
108 
109  if (m_bRandomStartingWeather && !loadDone)
110  {
111  array<ref WeatherState> weatherStates = {};
112  manager.GetWeatherStatesList(weatherStates);
113 
114  if (!weatherStates.IsEmpty())
115  {
116  Math.Randomize(-1);
117  manager.ForceWeatherTo(!m_bRandomWeatherChanges, weatherStates.GetRandomElement().GetStateName());
118  }
119  }
120 
121  if (!loadedWeatherState.IsEmpty())
122  manager.ForceWeatherTo(!m_bRandomWeatherChanges, loadedWeatherState);
123 
124  manager.SetHoursMinutesSeconds(hours, minutes, seconds);
125 
126  // Periodically check if the acceleration is correct, based on time of day
127  // SetTimeEvent is not usable since it requires changing Periodicity attribute directly in the manager entity in the world layer
129  {
131  GetGame().GetCallqueue().Remove(HandleDaytimeAcceleration);
132  GetGame().GetCallqueue().CallLater(HandleDaytimeAcceleration, DAYTIME_CHECK_PERIOD, true, false);
133  }
134  }
135 
136  //------------------------------------------------------------------------------------------------
137  protected void HandleDaytimeAcceleration(bool setup = false)
138  {
139  ChimeraWorld world = ChimeraWorld.CastFrom(GetOwner().GetWorld());
140  if (!world)
141  return;
142 
143  TimeAndWeatherManagerEntity manager = world.GetTimeAndWeatherManager();
144  if (!manager)
145  return;
146 
147  if (manager.IsNightHour(manager.GetTimeOfTheDay()))
148  {
149  if (m_bDaytimeAcceleration || setup)
150  {
151  m_bDaytimeAcceleration = false;
152  manager.SetDayDuration(DAY_DURATION / m_fNightTimeAcceleration);
153  }
154  }
155  else
156  {
157  if (!m_bDaytimeAcceleration || setup)
158  {
159  m_bDaytimeAcceleration = true;
160  manager.SetDayDuration(DAY_DURATION / m_fDayTimeAcceleration);
161  }
162  }
163  }
164 
165  //------------------------------------------------------------------------------------------------
166  override void OnWorldPostProcess(World world)
167  {
168  super.OnWorldPostProcess(world);
169 
170  if (!Replication.IsServer() || !GetGame().InPlayMode())
171  return;
172 
173  if (s_Instance != this)
174  {
175  Print("Multiple instances of SCR_TimeAndWeatherHandlerComponent detected.", LogLevel.WARNING);
176  return;
177  }
178 
179  SetupDaytimeAndWeather(m_iStartingHours, m_iStartingMinutes);
180  }
181 
182  //------------------------------------------------------------------------------------------------
183  override void OnPostInit(IEntity owner)
184  {
185  // Allow only one instance
186  if (s_Instance || !GetGame().InPlayMode())
187  return;
188 
189  // Allow permanent startign daytime & weather for debugging purposes
190 #ifdef TDM_CLI_SELECTION
191  return;
192 #endif
193 
194  s_Instance = this;
195 
196  if (!Replication.IsServer())
197  return;
198 
199  SCR_MissionHeader header = SCR_MissionHeader.Cast(GetGame().GetMissionHeader());
200 
201  if (m_bAllowHeaderSettings && header && header.m_bOverrideScenarioTimeAndWeather)
202  {
203  m_iStartingHours = header.m_iStartingHours;
204  m_iStartingMinutes = header.m_iStartingMinutes;
205  m_bRandomStartingDaytime = header.m_bRandomStartingDaytime;
206  m_fDayTimeAcceleration = header.m_fDayTimeAcceleration;
207  m_fNightTimeAcceleration = header.m_fNightTimeAcceleration;
208  m_bRandomStartingWeather = header.m_bRandomStartingWeather;
209  m_bRandomWeatherChanges = header.m_bRandomWeatherChanges;
210  }
211  }
212 }
ChimeraWorld
Definition: ChimeraWorld.c:12
DAY_DURATION
const int DAY_DURATION
Definition: SCR_TimeAndWeatherHandlerComponent.c:34
DEFAULT_DAYTIME_START
const float DEFAULT_DAYTIME_START
Definition: SCR_TimeAndWeatherHandlerComponent.c:32
m_iStartingMinutes
protected int m_iStartingMinutes
Definition: SCR_TimeAndWeatherHandlerComponent.c:11
m_bRandomWeatherChanges
protected bool m_bRandomWeatherChanges
Definition: SCR_TimeAndWeatherHandlerComponent.c:26
SCR_ArrayHelper
Definition: SCR_ArrayHelper.c:1
Attribute
SCR_TimeAndWeatherHandlerComponentClass SCR_BaseGameModeComponentClass Attribute("8", UIWidgets.Slider, "Starting time of day (hours)", "0 23 1")] protected int m_iStartingHours
m_bRandomStartingWeather
protected bool m_bRandomStartingWeather
Definition: SCR_TimeAndWeatherHandlerComponent.c:23
GetInstance
SCR_TextsTaskManagerComponentClass ScriptComponentClass GetInstance()
Definition: SCR_TextsTaskManagerComponent.c:50
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_TimeAndWeatherHandlerComponentClass
Definition: SCR_TimeAndWeatherHandlerComponent.c:1
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
DEFAULT_DAYTIME_END
const float DEFAULT_DAYTIME_END
Definition: SCR_TimeAndWeatherHandlerComponent.c:33
m_fDayTimeAcceleration
protected float m_fDayTimeAcceleration
Definition: SCR_TimeAndWeatherHandlerComponent.c:17
m_fNightTimeAcceleration
protected float m_fNightTimeAcceleration
Definition: SCR_TimeAndWeatherHandlerComponent.c:20
m_bAllowHeaderSettings
protected bool m_bAllowHeaderSettings
Definition: SCR_TimeAndWeatherHandlerComponent.c:29
m_bRandomStartingDaytime
protected bool m_bRandomStartingDaytime
Definition: SCR_TimeAndWeatherHandlerComponent.c:14
OnWorldPostProcess
override void OnWorldPostProcess(World world)
Definition: SCR_TimeAndWeatherHandlerComponent.c:166
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_TimeAndWeatherHandlerComponent.c:183
m_bSavedSettingApplied
protected bool m_bSavedSettingApplied
Definition: SCR_TimeAndWeatherHandlerComponent.c:37
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
DAYTIME_CHECK_PERIOD
const int DAYTIME_CHECK_PERIOD
Definition: SCR_TimeAndWeatherHandlerComponent.c:31
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SetupDaytimeAndWeather
void SetupDaytimeAndWeather(int hours, int minutes, int seconds=0, string loadedWeatherState="", bool loadDone=false)
Definition: SCR_TimeAndWeatherHandlerComponent.c:54
m_bDaytimeAcceleration
protected bool m_bDaytimeAcceleration
Definition: SCR_TimeAndWeatherHandlerComponent.c:36
SCR_BaseGameModeComponentClass
Definition: SCR_BaseGameModeComponent.c:2
SCR_MissionHeader
Definition: SCR_MissionHeader.c:1
HandleDaytimeAcceleration
protected void HandleDaytimeAcceleration(bool setup=false)
Definition: SCR_TimeAndWeatherHandlerComponent.c:137
SCR_BaseGameModeComponent
void SCR_BaseGameModeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_BaseGameModeComponent.c:199