Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_RadioComponent.c
Go to the documentation of this file.
1
7
9enum ERadioType
10{
14 R107M
15}
16
18enum EFreqUnit
19{
20 KHZ = 0,
21 MHZ = 1
22}
23
25class SCR_RadioProcAnimCtx
26{
27 int sAnimSignal;
28 float fStartVal;
29 float fEndVal;
30 float fAnimSpeed;
31 float fTimeSlice;
32 bool bInProgress;
33}
34
35[EntityEditorProps(category: "GameScripted/Gadgets", description: "Radio gadget", color: "0 0 255 255")]
36class SCR_RadioComponentClass : SCR_GadgetComponentClass
37{
38}
39
40class SCR_RadioComponent : SCR_GadgetComponent
41{
42 [Attribute("0", UIWidgets.ComboBox, "Radio category", "", ParamEnumArray.FromEnum(ERadioCategory), category: "Radio")]
43 protected int m_iRadioCategory;
44
45 [Attribute("0", UIWidgets.ComboBox, "Radio type", "", ParamEnumArray.FromEnum(ERadioType), category: "Radio")]
46 protected int m_iRadioType;
47
48 protected bool m_bIsPowered = true; // radio on/off toggle
49 protected BaseRadioComponent m_BaseRadioComp;
50 protected SignalsManagerComponent m_SignalManager;
51
52 // signals
53 protected bool m_bSignalInit = false;
54 protected int m_iSignalPower = -1;
55 protected int m_iSignalChannelKhz = -1;
56 protected int m_iSignalChannelMhz = -1;
57
58 // proc anims
59 protected ref array<ref SCR_RadioProcAnimCtx> m_aProcAnims = new array<ref SCR_RadioProcAnimCtx>();
60 protected bool m_bAnimInProgress;
61
62 //------------------------------------------------------------------------------------------------
64 void RadioToggle()
65 {
66 m_BaseRadioComp.SetPower( !m_BaseRadioComp.IsPowered() );
67 m_bIsPowered = m_BaseRadioComp.IsPowered();
68
69 PlayerController pc = GetGame().GetPlayerController();
70 if (pc)
71 {
72 SCR_VONController vonController = SCR_VONController.Cast(pc.FindComponent(SCR_VONController));
73 if (vonController)
74 {
75 array<ref SCR_VONEntry> entries = {};
76 vonController.GetVONEntries(entries);
77
78 foreach (SCR_VONEntry entry : entries)
79 {
80 SCR_VONEntryRadio radioEntry = SCR_VONEntryRadio.Cast(entry);
81 BaseRadioComponent radio = radioEntry.GetTransceiver().GetRadio();
82 if (radio == m_BaseRadioComp)
83 entry.SetUsable(radio.IsPowered());
84 }
85 }
86 }
87
88 if (m_bIsPowered)
89 {
90 if ( m_iRadioType == ERadioType.ANPRC68 || ERadioType.ANPRC77 )
91 SetKnobAnim(m_iSignalPower, 1, 0.3, false);
92 }
93 else
94 {
95 if ( m_iRadioType == ERadioType.ANPRC68 || ERadioType.ANPRC77 )
96 SetKnobAnim(m_iSignalPower, 0, 0.3, false);
97 }
98 }
99
100 //------------------------------------------------------------------------------------------------
104 bool ChangeFrequencyStep(bool direction)
105 {
106 if (!m_BaseRadioComp.IsPowered() || m_bAnimInProgress)
107 return false;
108
109 BaseTransceiver tsv = m_BaseRadioComp.GetTransceiver(0);
110 if (!tsv)
111 return false;
112
113 int freq = tsv.GetFrequency();
114 int freqMax = tsv.GetMaxFrequency();
115 int freqMin = tsv.GetMinFrequency();
116
117 if ( ( freq + tsv.GetFrequencyResolution() ) > freqMax && direction)
118 return false;
119 else if ( ( freq - tsv.GetFrequencyResolution() ) < freqMin && !direction)
120 return false;
121 else
122 {
123 if (direction)
124 freq += tsv.GetFrequencyResolution();
125 else
126 freq -= tsv.GetFrequencyResolution();
127
128 tsv.SetFrequency(freq);
129
130 float targetAngle;
131
132 // proc anims
133 if ( m_iRadioType != ERadioType.ANPRC68 )
134 {
135 targetAngle = GetKnobAngle(EFreqUnit.MHZ);
136 SetKnobAnim(m_iSignalChannelMhz, targetAngle, 0.3, true);
137 }
138
139 targetAngle = GetKnobAngle(EFreqUnit.KHZ);
140 SetKnobAnim(m_iSignalChannelKhz, targetAngle, 0.3, false);
141 }
142
143 return true;
144 }
145
146 //------------------------------------------------------------------------------------------------
150 protected float GetKnobAngle(EFreqUnit freqUnit)
151 {
152 int step, stepsCount;
153 int currentStep = 0;
154
155 stepsCount = GetKnobAnimStepCount(freqUnit);
156 BaseTransceiver tsv = m_BaseRadioComp.GetTransceiver(0);
157 if (!tsv)
158 return 0.0;
159
160 step = (tsv.GetMinFrequency() - tsv.GetFrequency()) * -1;
161
162 // Special case of preset number of channels
163 if (m_iRadioType == ERadioType.ANPRC68)
164 {
165 if (step > 0)
166 currentStep = step/tsv.GetFrequencyResolution();
167 }
168 // MHz, looking for the first two digits of a 5 digits frequency, steps are based on models dial
169 else if (freqUnit == EFreqUnit.MHZ)
170 {
171 if (step > 0)
172 currentStep = Math.Floor(step/1000);
173 }
174 // kHz
175 else
176 {
177 if (step >= 1000)
178 {
179 while (step >= 1000)
180 {
181 step -= 1000;
182 }
183 }
184
185 // half steps 050, 150 etc
186 if ((step/50) % 2 == 0)
187 currentStep = 0;
188 else
189 currentStep = 1;
190
191 step = Math.Floor(step/100);
192 currentStep = currentStep + (step * 2); // 20 steps(000 - 100) instead of 10 + possible half step (050)
193 }
194
195 return currentStep * (360/stepsCount);
196
197 }
198
199 //------------------------------------------------------------------------------------------------
200 // Proc anims
201 //------------------------------------------------------------------------------------------------
202
203 //------------------------------------------------------------------------------------------------
205 protected void UpdateKnobState()
206 {
207 bool state;
208
209 if (m_bIsPowered)
210 state = 1;
211 else
212 state = 0;
213
214 if (!m_SignalManager)
215 return;
216
217 if (!m_bSignalInit)
218 InitSignals();
219
220 m_SignalManager.SetSignalValue( m_iSignalPower, (float)state );
221 m_SignalManager.SetSignalValue( m_iSignalChannelKhz, GetKnobAngle(EFreqUnit.KHZ) );
222
223 if ( m_iRadioType != ERadioType.ANPRC68 )
224 m_SignalManager.SetSignalValue( m_iSignalChannelMhz, GetKnobAngle(EFreqUnit.MHZ) );
225 }
226
227 //------------------------------------------------------------------------------------------------
233 protected void SetKnobAnim(int signal, float targetAngle, float speed, bool clumpAnim)
234 {
235 if (m_bAnimInProgress || !m_SignalManager)
236 return;
237
238 SCR_RadioProcAnimCtx animCtx = new SCR_RadioProcAnimCtx();
239 animCtx.sAnimSignal = signal;
240 animCtx.fStartVal = m_SignalManager.GetSignalValue(signal);
241 animCtx.fEndVal = targetAngle;
242 animCtx.fAnimSpeed = speed;
243 animCtx.fTimeSlice = 0;
244 animCtx.bInProgress = true;
245
246 m_aProcAnims.Insert(animCtx);
247
248 if (!clumpAnim)
249 {
250 m_bAnimInProgress = true;
252 }
253 }
254
255 //------------------------------------------------------------------------------------------------
258 protected void AnimKnob(float timeSlice)
259 {
260 int inProgress;
261
262 for ( int i = 0; i < m_aProcAnims.Count(); i++ )
263 {
264 SCR_RadioProcAnimCtx animCtx = m_aProcAnims[i];
265 if (!animCtx)
266 continue;
267
268 if (animCtx.bInProgress)
269 inProgress++;
270
271 float newAngle;
272 animCtx.fTimeSlice += timeSlice / animCtx.fAnimSpeed;
273
274 // if ended
275 if ( animCtx.fTimeSlice >= 1 )
276 {
277 animCtx.bInProgress = false;
278 newAngle = animCtx.fEndVal;
279 }
280 else
281 {
282 // Turn counter clockwise if closer
283 if ( Math.AbsFloat(animCtx.fStartVal - animCtx.fEndVal) > 180 )
284 {
285 if ( animCtx.fStartVal < 0)
286 animCtx.fStartVal += 360;
287 else
288 animCtx.fStartVal -= 360;
289 }
290
291 float lerp = Math.Lerp(animCtx.fStartVal, animCtx.fEndVal, animCtx.fTimeSlice);
292 newAngle = lerp;
293 }
294
295 m_SignalManager.SetSignalValue(animCtx.sAnimSignal, newAngle);
296 }
297
298 if (inProgress <= 0)
299 {
300 m_bAnimInProgress = false;
301 m_aProcAnims.Clear();
303 }
304 }
305
306 //------------------------------------------------------------------------------------------------
310 protected int GetKnobAnimStepCount(EFreqUnit freqUnit)
311 {
312 if ( m_iRadioType == ERadioType.ANPRC68 )
313 {
314 return 10;
315 }
316 else if ( m_iRadioType == ERadioType.R148 )
317 {
318 if (freqUnit == EFreqUnit.MHZ)
319 return 15;
320 else
321 return 20;
322 }
323 else if ( m_iRadioType == ERadioType.ANPRC77 )
324 {
325 if (freqUnit == EFreqUnit.MHZ)
326 return 24; // 22 + 1 empty & +1 duplicated
327 else
328 return 24; // 20 +4 empty ones
329 }
330
331 return 1;
332 }
333
334 //------------------------------------------------------------------------------------------------
336 protected void InitSignals()
337 {
338 // cache signals
339 m_iSignalPower = m_SignalManager.FindSignal("Power");
340 m_iSignalChannelKhz = m_SignalManager.FindSignal("Channel_k");
341 m_iSignalChannelMhz = m_SignalManager.FindSignal("CHannel_M");
342
343 if (m_iSignalPower != -1 && m_iSignalChannelKhz != -1 && m_iSignalChannelMhz != -1)
344 m_bSignalInit = true;
345 }
346
347 //------------------------------------------------------------------------------------------------
348 // Overrides
349 //------------------------------------------------------------------------------------------------
350
351 //------------------------------------------------------------------------------------------------
352 override void ModeSwitch(EGadgetMode mode, IEntity charOwner)
353 {
354 super.ModeSwitch(mode, charOwner);
355
356 // Update knobs
357 UpdateKnobState();
358 }
359
360 //------------------------------------------------------------------------------------------------
361 override void ActivateGadgetUpdate()
362 {
363 super.ActivateGadgetUpdate();
364
365 InventoryItemComponent itemComponent = InventoryItemComponent.Cast(GetOwner().FindComponent(InventoryItemComponent));
366 if (itemComponent)
367 itemComponent.ActivateOwner(true); // required for the procedural animations to function
368 }
369
370 //------------------------------------------------------------------------------------------------
371 override void DeactivateGadgetUpdate()
372 {
373 super.DeactivateGadgetUpdate();
374
375 InventoryItemComponent itemComponent = InventoryItemComponent.Cast(GetOwner().FindComponent(InventoryItemComponent));
376 if (itemComponent)
377 itemComponent.ActivateOwner(false);
378 }
379
380 //------------------------------------------------------------------------------------------------
381 override void UpdateVisibility(EGadgetMode mode)
382 {
383 if (m_iRadioCategory != ERadioCategory.MANPACK) // let radio backpack visibility be handled by inventory
384 super.UpdateVisibility(mode);
385 }
386
387 //------------------------------------------------------------------------------------------------
388 override EGadgetType GetType()
389 {
390 if (m_iRadioCategory == ERadioCategory.MANPACK)
391 return EGadgetType.RADIO_BACKPACK;
392 else
393 return EGadgetType.RADIO;
394 }
395
396 //------------------------------------------------------------------------------------------------
398 ERadioType GetRadioType()
399 {
400 return m_iRadioType;
401 }
402
403 //------------------------------------------------------------------------------------------------
404 override bool IsVisibleEquipped()
405 {
406 if (m_iRadioCategory == ERadioCategory.MANPACK)
407 return true;
408 else
409 return false;
410 }
411
412 //------------------------------------------------------------------------------------------------
414 BaseRadioComponent GetRadioComponent()
415 {
416 return m_BaseRadioComp;
417 }
418
419 //------------------------------------------------------------------------------------------------
420 override void Update(float timeSlice)
421 {
422 // knob anim
423 if (!m_bAnimInProgress)
424 {
426 return;
427 }
428
429 AnimKnob(timeSlice);
430 }
431
432 //------------------------------------------------------------------------------------------------
433 override void OnPostInit(IEntity owner)
434 {
435 super.OnPostInit(owner);
436
437 // BaseRadioComponent
438 m_BaseRadioComp = BaseRadioComponent.Cast(owner.FindComponent(BaseRadioComponent));
439 if (!m_BaseRadioComp)
440 Print("SCR_RadioEntity: Failed acquiring BaseRadioComponent", LogLevel.WARNING);
441
442 // signal manager
443 m_SignalManager = SignalsManagerComponent.Cast(owner.FindComponent( SignalsManagerComponent ) );
444 }
445}
ArmaReforgerScripted GetGame()
Definition game.c:1398
override void ModeSwitch(EGadgetMode mode, IEntity charOwner)
override EGadgetType GetType()
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
override bool IsVisibleEquipped()
vector direction
override void ActivateGadgetUpdate()
override void DeactivateGadgetUpdate()
SCR_GadgetComponentClass ANPRC68
SCR_GadgetComponentClass R148
enum ERadioCategory KHZ
Frequency unit prefix.
enum ERadioCategory ANPRC77
ERadioCategory
Radio slot.
@ PERSONAL
@ MANPACK
SignalsManagerComponent m_SignalManager
enum EVehicleType IEntity
proto external Managed FindComponent(typename typeName)
BaseTransceiver GetTransceiver()
Associated transceiver.
IEntity GetOwner()
Owner entity of the fuel tank.
BaseRadioComponentClass BaseTransceiver
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