Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_BuildingSoundComponent.c
Go to the documentation of this file.
2{
5}
6
7[ComponentEditorProps(category: "GameScripted/Sound", description: "THIS IS THE SCRIPT DESCRIPTION.")]
8class SCR_BuildingSoundComponentClass : SoundComponentClass
10 [Attribute("0", uiwidget: UIWidgets.ComboBox, "Spawning type", "", enumType: SCR_ESpawnType, category: "Spawn Setup")]
11 SCR_ESpawnType m_eSpawnType;
12
13 [Attribute("0", UIWidgets.Slider, "Minimum distance from selected point for sound to be played [m]", "0 60 1", category: "Spawn Setup")]
14 int m_iMinimumDistance;
15
16 [Attribute("8", UIWidgets.Slider, "Time interval when maximum WindSpeed [s]", "0 60 1", category: "Spawn Setup")]
17 int m_iTimeIntervalMin;
18
19 [Attribute("20", UIWidgets.Slider, "Time interval when minimum WindSpeed [s]", "0 60 1", category: "Spawn Setup")]
20 int m_iTimeIntervalMax;
21
22 [Attribute("0 0 0", UIWidgets.Coords, "Mins OOB Point", category: "Spawn Setup")]
23 vector m_vMins;
24
25 [Attribute("0 0 0", UIWidgets.Coords, "Maxs OOB Point", category: "Spawn Setup")]
26 vector m_vMaxs;
27
28 [Attribute(category: "Spawn Setup")]
29 ref array<ref PointInfo> m_aSpawnPoints;
30
31 [Attribute("0", UIWidgets.Auto, "Update Interior signal", category: "Spawn Setup")]
32 bool m_bInteriorSignal;
33
34 void SCR_BuildingSoundComponentClass(IEntityComponentSource componentSource, IEntitySource parentSource, IEntitySource prefabSource)
35 {
36 // Make sure m_vMins is smaller than m_vMaxs.
37 // This is necessary because these values are used as inputs for Math.RandomFloat(a,b) that requires that a < b.
38 bool invalid = false;
39 for (int i=0; i<3; ++i)
40 {
41 // Handle wrong inputs
42 if (m_vMins[i] > m_vMaxs[i])
43 {
44 m_vMaxs[i] = m_vMins[i] + 0.1;
45 invalid = true;
46 }
47 }
48
49 // Display the warning in the edit mode. We don't want the play mode spammed.
50 // The idea is that the author catches their mistake in the log.
51 if (invalid && SCR_Global.IsEditMode())
52 {
53 if (prefabSource)
54 Print(string.Format("Requirement not met: Mins <= Maxs on %1 %2", this, prefabSource.GetName()), LogLevel.WARNING);
55 else
56 Print("Requirement not met: Mins <= Maxs on " + this, LogLevel.WARNING);
57 }
58 }
59
60 private float CalculateLocalOffset(int i)
61 {
62 if (m_vMins[i]==m_vMaxs[i])
63 return m_vMins[i];
64
65 return Math.RandomFloat(m_vMins[i], m_vMaxs[i]);
66 }
67
68 vector CalculateOBBLocalOffset()
69 {
70 vector v;
71 v[0] = CalculateLocalOffset(0);
72 v[1] = CalculateLocalOffset(1);
73 v[2] = CalculateLocalOffset(2);
74 return v;
75 }
76
77 vector CalculatePointsLocalOffset()
78 {
79 vector mat[4];
80 m_aSpawnPoints.GetRandomElement().GetLocalTransform(mat);
81
82 return mat[3];
83 }
84}
85
86class SCR_BuildingSoundComponent : SoundComponent
87{
88 private static const float RANDOM_PERCENTAGE_MIN = 0.7;
89 private static const float RANDOM_PERCENTAGE_MAX = 1.3;
90
91 protected float m_fTriggerInterval;
92
93 //------------------------------------------------------------------------------------------------
94 private void TriggeredSoundHandler(IEntity owner, float timeSlice)
95 {
96 // Get prefab data
97 SCR_BuildingSoundComponentClass prefabData = SCR_BuildingSoundComponentClass.Cast(GetComponentData(owner));
98
99 // Get remetition time
100 GetTimeInterval(prefabData);
101
102 // Get sound position
103 vector mat[4];
104 Math3D.MatrixIdentity4(mat);
105 mat[3] = GetSoundPosition(prefabData);
106
107 // Check distance
108 if (prefabData.m_iMinimumDistance > 0)
109 {
110 vector cameraMat[4];
111 owner.GetWorld().GetCurrentCamera(cameraMat);
112
113 // Dont play sound if sound position is too close to listener
114 if (vector.Distance(mat[3], cameraMat[3]) < prefabData.m_iMinimumDistance)
115 {
116 return;
117 }
118 }
119
120 //Set Interior signal
121 if (prefabData.m_bInteriorSignal)
122 {
123 SetInteriorSignal(owner);
124 }
125
126 // Play sound
127 SoundEventTransform(SCR_SoundEvent.SOUND_CREAK, mat);
128 }
129
130 //------------------------------------------------------------------------------------------------
131 private void SetInteriorSignal(IEntity owner)
132 {
133 bool interior;
134
135 float gInterior = AudioSystem.GetVariableValue("GInterior", "{A60F08955792B575}Sounds/_SharedData/Variables/GlobalVariables.conf");
136
137 if (gInterior == 1)
138 {
139 vector mat[4];
140 owner.GetWorld().GetCurrentCamera(mat);
141
142 interior = IsInWorldBounds(mat[3], owner);
143 }
144
145 SignalsManagerComponent signalsManagerComponent = SignalsManagerComponent.Cast(owner.FindComponent(SignalsManagerComponent));
146 if (signalsManagerComponent)
147 signalsManagerComponent.SetSignalValue(signalsManagerComponent.AddOrFindSignal("Interior"), interior);
148 }
149
150 //------------------------------------------------------------------------------------------------
151 private bool IsInWorldBounds(vector point, IEntity entity)
152 {
153 vector mins = vector.Zero;
154 vector maxs = vector.Zero;
155
156 entity.GetWorldBounds(mins, maxs);
157
158 return !(point[0] < mins[0] || point[0] > maxs[0] || point[1] < mins[1] || point[1] > maxs[1] || point[2] < mins[2] || point[2] > maxs[2]);
159 }
160
161 //------------------------------------------------------------------------------------------------
162 private void GetTimeInterval(SCR_BuildingSoundComponentClass prefabData)
163 {
164 GameSignalsManager signals = GetGame().GetSignalsManager();
165 float windSpeed = signals.GetSignalValue(signals.AddOrFindSignal("WindSpeed"));
166 float timeInterval = Interpolate(windSpeed, SCR_AmbientSoundsComponent.WINDSPEED_MIN, SCR_AmbientSoundsComponent.WINDSPEED_MAX, prefabData.m_iTimeIntervalMax, prefabData.m_iTimeIntervalMin);
167
168 m_fTriggerInterval = Math.RandomFloat(timeInterval * RANDOM_PERCENTAGE_MIN, timeInterval * RANDOM_PERCENTAGE_MAX);
169 }
170
171 //------------------------------------------------------------------------------------------------
172 private vector GetSoundPosition(SCR_BuildingSoundComponentClass prefabData)
173 {
174 vector v;
175
176 if (prefabData.m_eSpawnType == SCR_ESpawnType.BOUNDING_BOX)
177 {
178 v = prefabData.CalculateOBBLocalOffset();
179 }
180 else
181 {
182 v = prefabData.CalculatePointsLocalOffset();
183 }
184
185 // From Local To World Space
186 return GetOwner().CoordToParent(v);
187 }
188
189 //------------------------------------------------------------------------------------------------
190 private float Interpolate(float in, float Xmin, float Xmax, float Ymin, float Ymax)
191 {
192 if (in <= Xmin)
193 return Ymin;
194
195 if (in >= Xmax)
196 return Ymax;
197
198 return ((Ymin * (Xmax - in) + Ymax * (in - Xmin)) / (Xmax - Xmin));
199 }
200
201 //------------------------------------------------------------------------------------------------
202 override void UpdateSoundJob(IEntity owner, float timeSlice)
203 {
204 super.UpdateSoundJob(owner, timeSlice);
205
206 m_fTriggerInterval -= timeSlice;
207
208 if (m_fTriggerInterval > 0)
209 return;
210
211 TriggeredSoundHandler(owner, timeSlice);
212 }
213
214 //------------------------------------------------------------------------------------------------
215 override void OnUpdateSoundJobBegin(IEntity owner)
216 {
217 GetTimeInterval(SCR_BuildingSoundComponentClass.Cast(GetComponentData(owner)));
218 }
219
220 //------------------------------------------------------------------------------------------------
221 // constructor
225 void SCR_BuildingSoundComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
226 {
227 SCR_BuildingSoundComponentClass prefabData = SCR_BuildingSoundComponentClass.Cast(GetComponentData(GetOwner()));
228
229 if (prefabData.m_eSpawnType == SCR_ESpawnType.BOUNDING_BOX)
230 {
231 if (prefabData.m_vMins == vector.Zero && prefabData.m_vMaxs == vector.Zero)
232 {
233 SetScriptedMethodsCall(false);
234 return;
235 }
236 }
237 else if (prefabData.m_eSpawnType == SCR_ESpawnType.POINTS)
238 {
239 if (!prefabData.m_aSpawnPoints)
240 {
241 SetScriptedMethodsCall(false);
242 return;
243 }
244
245 if (prefabData.m_aSpawnPoints.IsEmpty())
246 {
247 SetScriptedMethodsCall(false);
248 return;
249 }
250 }
251
252 SetScriptedMethodsCall(true);
253 }
254}
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum SCR_ESpawnType ComponentEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.")
SCR_CharacterSoundComponentClass GetComponentData()
enum EVehicleType IEntity
proto external Managed FindComponent(typename typeName)
proto external BaseWorld GetWorld()
proto external void GetWorldBounds(out vector mins, out vector maxs)
proto external vector CoordToParent(vector coord)
Definition Math.c:13
static bool IsEditMode()
Definition Functions.c:1566
proto external IEntity GetOwner()
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