Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_RadioCoverageSystem.c
Go to the documentation of this file.
1void OnCoverageChangedDelegate(SCR_CoverageRadioComponent radio);
3typedef ScriptInvokerBase<OnCoverageChangedDelegate> OnCoverageChangedInvoker;
4
6{
7 override static void InitInfo(WorldSystemInfo outInfo)
8 {
9 outInfo.SetAbstract(false);
10 }
11
12 protected bool m_bSignalSignature;
13 protected bool m_bUpdateCoverage;
14 protected bool m_bForcedPause;
15
17
18 protected ref array<SCR_CoverageRadioComponent> m_aRadioComponents = {};
19
21
22 protected static const int UPDATE_DELAY = 2000;
23
24 //------------------------------------------------------------------------------------------------
26 {
27 World world = GetGame().GetWorld();
28
29 if (!world)
30 return null;
31
32 return SCR_RadioCoverageSystem.Cast(world.FindSystem(SCR_RadioCoverageSystem));
33 }
34
35 //------------------------------------------------------------------------------------------------
36 static bool UpdateAll(bool forceRecalculation = false)
37 {
39
40 if (system)
41 return system.UpdateRadios(forceRecalculation);
42
43 return false;
44 }
45
46 //------------------------------------------------------------------------------------------------
47 override event bool ShouldBePaused()
48 {
49 return true;
50 }
51
52 //------------------------------------------------------------------------------------------------
53 override void OnStarted()
54 {
55 if (!m_bForcedPause)
56 {
57 UpdateRadios(true);
58 GetGame().GetCallqueue().CallLater(UpdateRadios, UPDATE_DELAY, true, false);
59 }
60 }
61
62 //------------------------------------------------------------------------------------------------
63 override void OnStopped()
64 {
65 GetGame().GetCallqueue().Remove(UpdateRadios);
66 }
67
68 //------------------------------------------------------------------------------------------------
69 protected bool UpdateRadios(bool forceUpdateCoverage = false)
70 {
71 m_bUpdateCoverage = false;
72
73 foreach (SCR_CoverageRadioComponent radio : m_aRadioComponents)
74 {
76 }
77
78 if (!m_bUpdateCoverage && !forceUpdateCoverage)
79 return m_bUpdateCoverage;
80
81 if (Replication.IsServer())
82 {
83 for (int i = 0, count = m_mEncryptionSignalSources.Count(); i < count; i++)
84 {
86 }
87 }
88
89 return m_bUpdateCoverage;
90 }
91
92 //------------------------------------------------------------------------------------------------
93 protected void UpdateRadioComponent(notnull SCR_CoverageRadioComponent component)
94 {
95 bool rangesHandled;
96 string encryption = component.GetEncryptionKey();
97
98 // Radio has been turned on or off
99 if (!component.WasPowered() && component.IsPowered())
100 {
101 m_bUpdateCoverage = true;
102 HandleRanges(component);
103 component.OnPowerToggle(true);
104 rangesHandled = true;
105
106 if (Replication.IsServer() && component.IsSource() && !m_mEncryptionSignalSources.Get(encryption))
107 m_mEncryptionSignalSources.Set(encryption, component);
108 }
109 else if (component.WasPowered() && !component.IsPowered())
110 {
111 m_bUpdateCoverage = true;
112 component.OnPowerToggle(false);
113
114 if (Replication.IsServer() && m_mEncryptionSignalSources.Get(encryption) == component)
115 FindNewSource(encryption);
116 }
117
118 // Signal range has changed
119 float currentRange = component.GetTransceiver(0).GetRange();
120
121 if (currentRange != component.GetSavedRange())
122 {
123 m_bUpdateCoverage = true;
124 component.OnRangeChanged(currentRange);
125
126 if (!rangesHandled)
127 HandleRanges(component, currentRange);
128 }
129
130 // Encryption key has changed
131 string savedEncryption = component.GetSavedEncryption();
132
133 if (encryption != savedEncryption)
134 {
135 m_bUpdateCoverage = true;
136
137 if (Replication.IsServer() && m_mEncryptionSignalSources.Get(savedEncryption) == component)
138 FindNewSource(savedEncryption);
139
140 if (Replication.IsServer() && component.IsSource() && !m_mEncryptionSignalSources.Get(encryption))
141 m_mEncryptionSignalSources.Set(encryption, component);
142
143 component.OnEncryptionChanged(encryption);
144 }
145 }
146
147 //------------------------------------------------------------------------------------------------
148 void OnCoverageChanged(notnull SCR_CoverageRadioComponent radio)
149 {
151 m_OnCoverageChanged.Invoke(radio);
152 }
153
154 //------------------------------------------------------------------------------------------------
162
163 //------------------------------------------------------------------------------------------------
164 void TogglePeriodicUpdates(bool toggle)
165 {
166 m_bForcedPause = !toggle;
167
168 if (toggle)
169 OnStarted();
170 else
171 OnStopped();
172 }
173
174 //------------------------------------------------------------------------------------------------
175 void RegisterRadioComponent(notnull SCR_CoverageRadioComponent component, float range = -1)
176 {
177 if (m_aRadioComponents.Contains(component))
178 return;
179
180 m_aRadioComponents.Insert(component);
181
182 if (!component.IsPowered())
183 return;
184
185 if (Replication.IsServer() && component.IsSource() && !m_mEncryptionSignalSources.Get(component.GetEncryptionKey()))
186 m_mEncryptionSignalSources.Set(component.GetEncryptionKey(), component);
187
188 HandleRanges(component, range);
189 }
190
191 //------------------------------------------------------------------------------------------------
192 void UnregisterRadioComponent(notnull SCR_CoverageRadioComponent component)
193 {
194 m_aRadioComponents.RemoveItem(component);
195
196 if (!GetGame().InPlayMode())
197 return;
198
199 string encryption = component.GetEncryptionKey();
200
201 if (Replication.IsServer() && m_mEncryptionSignalSources.Get(encryption) == component)
202 FindNewSource(encryption);
203
204 UpdateRadios(true);
205 }
206
207 //------------------------------------------------------------------------------------------------
208 protected void FindNewSource(string encryptionKey)
209 {
210 m_mEncryptionSignalSources.Set(encryptionKey, null);
211
212 foreach (SCR_CoverageRadioComponent newSource : m_aRadioComponents)
213 {
214 if (!newSource.IsPowered() || !newSource.IsSource() || newSource.GetEncryptionKey() != encryptionKey)
215 continue;
216
217 m_mEncryptionSignalSources.Set(encryptionKey, newSource);
218 break;
219 }
220 }
221
222 //------------------------------------------------------------------------------------------------
223 protected void HandleRanges(notnull SCR_CoverageRadioComponent component, float range = -1)
224 {
225 if (range < 0)
226 range = component.GetTransceiver(0).GetRange();
227
228 float rangeSq = range * range;
229
230 vector position = component.GetOwner().GetOrigin();
231 int otherRange;
232 int distanceSq;
233
234 foreach (SCR_CoverageRadioComponent radio : m_aRadioComponents)
235 {
236 if (radio == component || !radio.IsPowered())
237 continue;
238
239 distanceSq = vector.DistanceSqXZ(radio.GetOwner().GetOrigin(), position);
240
241 if (distanceSq <= rangeSq)
242 {
243 component.AddRadioInRange(radio);
244 radio.AddRadioInRangeOf(component);
245 }
246 else
247 {
248 component.RemoveRadioInRange(radio);
249 radio.RemoveRadioInRangeOf(component);
250 }
251
252 otherRange = radio.GetTransceiver(0).GetRange();
253
254 if (distanceSq <= (otherRange * otherRange))
255 {
256 radio.AddRadioInRange(component);
257 component.AddRadioInRangeOf(radio);
258 }
259 }
260 }
261
262 //------------------------------------------------------------------------------------------------
263 protected void UpdateCoverage(string encryptionKey)
264 {
265 SCR_CoverageRadioComponent source = m_mEncryptionSignalSources.Get(encryptionKey);
266
267 if (!source)
268 return;
269
270 // Signature changes each update so we know which radios have already been pinged to prevent infinite loops
271 foreach (SCR_CoverageRadioComponent radio : m_aRadioComponents)
272 {
273 radio.PrepareCoverageUpdate(encryptionKey, m_bSignalSignature);
274 }
275
276 // Forward ping
278 source.Ping(encryptionKey, m_bSignalSignature);
279
280 // Backward ping
282 source.Ping(encryptionKey, m_bSignalSignature, true);
283
284 foreach (SCR_CoverageRadioComponent radio : m_aRadioComponents)
285 {
286 radio.FinishCoverageUpdate(encryptionKey);
287 }
288 }
289}
290
291enum SCR_ERadioCoverageStatus
292{
296 BOTH_WAYS
297}
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector position
SCR_RadioCoverageSystem SEND
func OnCoverageChangedDelegate
SCR_RadioCoverageSystem RECEIVE
ScriptInvokerBase< OnCoverageChangedDelegate > OnCoverageChangedInvoker
Main replication API.
Definition Replication.c:14
void HandleRanges(notnull SCR_CoverageRadioComponent component, float range=-1)
void UnregisterRadioComponent(notnull SCR_CoverageRadioComponent component)
bool UpdateRadios(bool forceUpdateCoverage=false)
void FindNewSource(string encryptionKey)
void TogglePeriodicUpdates(bool toggle)
OnCoverageChangedInvoker GetOnCoverageChanged()
void RegisterRadioComponent(notnull SCR_CoverageRadioComponent component, float range=-1)
void UpdateCoverage(string encryptionKey)
ref OnCoverageChangedInvoker m_OnCoverageChanged
ref map< string, SCR_CoverageRadioComponent > m_mEncryptionSignalSources
ref array< SCR_CoverageRadioComponent > m_aRadioComponents
override event bool ShouldBePaused()
static SCR_RadioCoverageSystem GetInstance()
void UpdateRadioComponent(notnull SCR_CoverageRadioComponent component)
void OnCoverageChanged(notnull SCR_CoverageRadioComponent radio)
static bool UpdateAll(bool forceRecalculation=false)
Definition World.c:16
Structure holding world system meta-information required by the engine.
Definition Types.c:486
@ NONE
When Shape is created and not initialized yet.
Definition ShapeType.c:15