Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
weaponparticles.c
Go to the documentation of this file.
1/*
2 Author: Boris Vacula
3 For documentation go to: DayZ Confluence -> How-to articles -> Weapon muzzle flash particle system configuration
4 This system plays effect(s) on any weapon that is fired/jammed/ruined/...
5*/
6
7class WeaponParticlesBase // This class represents every particle effect you see in config within OnFire or OnOverheating events
8{
9 bool m_IlluminateWorld;
10 bool m_IgnoreIfSuppressed;
11 bool m_OnlyIfBoltIsOpen;
12 int m_MuzzleIndex;
13 int m_OverrideParticle;
14 int m_OnlyWithinHealthLabelMin;
15 int m_OnlyWithinHealthLabelMax;
16 float m_OnlyWithinOverheatLimitsMin;
17 float m_OnlyWithinOverheatLimitsMax;
18 float m_OnlyWithinRainLimitsMin;
19 float m_OnlyWithinRainLimitsMax;
20 string m_OverrideDirectionPoint;
21 string m_OnlyIfBulletIs;
22 string m_OnlyIfWeaponIs;
23 string m_OverridePoint;
24 vector m_OverrideDirectionVector;
25 vector m_PositionOffset;
26
27 string m_Name;
28
29 //======================================
30 // PRELOAD EVERYTHING
31 //======================================
32
33 void WeaponParticlesBase(ItemBase muzzle_owner, string config_OnFire_entry)
34 {
35 m_Name = config_OnFire_entry;
36
37 // ignoreIfSuppressed
38 m_IgnoreIfSuppressed = GetGame().ConfigGetFloat(string.Format("%1 ignoreIfSuppressed", m_Name));
39
40 // onlyIfBoltIsOpen
41 m_OnlyIfBoltIsOpen = GetGame().ConfigGetFloat(string.Format("%1 onlyIfBoltIsOpen", m_Name));
42
43 // illuminateWorld
44 m_IlluminateWorld = GetGame().ConfigGetFloat(string.Format("%1 illuminateWorld", m_Name));
45
46 m_MuzzleIndex = -1;
47 if (GetGame().ConfigIsExisting(string.Format("%1 muzzleIndex", m_Name)))
48 {
49 m_MuzzleIndex = GetGame().ConfigGetInt(string.Format("%1 muzzleIndex", m_Name));
50 }
51
52 // onlyIfWeaponIs
54 GetGame().ConfigGetText(string.Format("%1 onlyIfWeaponIs", m_Name), m_OnlyIfWeaponIs);
55
56 // onlyIfBulletIs
58 GetGame().ConfigGetText(string.Format("%1 onlyIfBulletIs", m_Name), m_OnlyIfBulletIs);
59
60 // onlyWithinHealthLabel[]
61 array<float> health_limit = new array<float>;
62 GetGame().ConfigGetFloatArray(string.Format("%1 onlyWithinHealthLabel", m_Name), health_limit);
63
64 if (health_limit.Count() == 2)
65 {
66 m_OnlyWithinHealthLabelMin = health_limit.Get(0);
67 m_OnlyWithinHealthLabelMax = health_limit.Get(1);
68 }
69 else
70 {
71 // Disable this filter
74 }
75
76 // onlyWithinOverheatLimits[]
77 array<float> overheat_limit = new array<float>;
78 GetGame().ConfigGetFloatArray(string.Format("%1 onlyWithinOverheatLimits", m_Name), overheat_limit);
79
80 if (overheat_limit.Count() == 2)
81 {
82 m_OnlyWithinOverheatLimitsMin = overheat_limit.Get(0);
83 m_OnlyWithinOverheatLimitsMax = overheat_limit.Get(1);
84 }
85 else
86 {
87 // Disable this filter
90 }
91
92 // onlyWithinRainLimits[]
93 array<float> rain_limit = new array<float>;
94 GetGame().ConfigGetFloatArray(string.Format("%1 onlyWithinRainLimits", m_Name), rain_limit);
95
96 if (rain_limit.Count() == 2)
97 {
98 m_OnlyWithinRainLimitsMin = rain_limit.Get(0);
99 m_OnlyWithinRainLimitsMax = rain_limit.Get(1);
100 }
101 else
102 {
103 // Disable this filter
106 }
107
108 // overridePoint
109 m_OverridePoint = "";
110 GetGame().ConfigGetText(string.Format("%1 overridePoint", m_Name), m_OverridePoint);
111
112 if (m_OverridePoint == "")
113 m_OverridePoint = "Usti hlavne"; // default memory point name
114
115 // overrideParticle
116 string particle_name = "";
117 GetGame().ConfigGetText( string.Format("%1 overrideParticle", m_Name), particle_name);
118
119 if (particle_name != "")
120 {
121 m_OverrideParticle = ParticleList.GetParticleIDByName(particle_name);
122 }
123 else
124 {
126 ErrorEx(string.Format("'%1' does not contain a definition for 'overrideparticle'",
127 config_OnFire_entry), ErrorExSeverity.INFO);
128 }
129
130 // overrideDirectionPoint
132 GetGame().ConfigGetText(string.Format("%1 overrideDirectionPoint", m_Name), m_OverrideDirectionPoint);
133
134 if (m_OverrideDirectionPoint == "")
135 {
136 // overrideDirectionVector
137 vector test_ori = GetGame().ConfigGetVector(string.Format("%1 overrideDirectionVector", m_Name));
138
139 if (test_ori != vector.Zero)
140 {
141 m_OverrideDirectionVector = test_ori;
142 }
143 }
144
145 // positionOffset[]
147 GetGame().ConfigGetFloatArray(string.Format("%1 positionOffset", m_Name), v);
148
149 if (v.Count() == 3)
150 {
151 float v1 = v.Get(0);
152 float v2 = v.Get(1);
153 float v3 = v.Get(2);
154 m_PositionOffset = Vector(v1, v2, v3);
155 }
156 }
157
158
159
160 //======================================
161 // PLAY PARTICLES
162 //======================================
163 // It is important to know that this block of script is called for weapons and muzzle attachments alike.
164 // Thus weapon == muzzle_owner when this is called for a weapon, and weapon != muzzle_owner when this is called for a suppressor.
165 void OnActivate(ItemBase weapon, int muzzle_index, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
166 {
167 if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
168 {
169 // Handle effect's parameters
170 if ( PrtTest.m_GunParticlesState ) // Check if particles are enabled by debug
171 {
172 if ( m_MuzzleIndex == -1 || m_MuzzleIndex == muzzle_index )
173 {
174 if ( CheckBoltStateCondition(weapon) ) // onlyIfBoltIsOpen
175 {
176 if ( !suppressor || suppressor.IsRuined() || !(m_IgnoreIfSuppressed) ) // ignoreIfSuppressed
177 {
178 if ( CheckHealthCondition( muzzle_owner.GetHealthLevel() ) ) // onlyWithinHealthLabel
179 {
180 if ( CheckOverheatingCondition( muzzle_owner.GetOverheatingCoef() ) ) // onlyWithinOverheatLimits
181 {
182 if ( CheckRainCondition( GetGame().GetWeather().GetRain().GetActual() ) ) // onlyWithinRainLimits
183 {
184 if ( m_OnlyIfBulletIs == "" || m_OnlyIfBulletIs == ammoType ) // onlyIfBulletIs
185 {
186 if ( m_OnlyIfWeaponIs == "" || m_OnlyIfWeaponIs == weapon.GetType() ) // onlyIfWeaponIs
187 {
188 // Get particle ID
189 int particle_id = CheckParticleOverride(ammoType);
190
191 if (ParticleList.IsValidId(particle_id))
192 {
193 // Get position of the particle
194 vector local_pos = muzzle_owner.GetSelectionPositionLS(m_OverridePoint);
195 local_pos += m_PositionOffset;
196
197 // Set orientation of the particle
198 vector particle_ori = CheckOrientationOverride(local_pos, muzzle_owner);
199
200 // Create particle
201 Particle p = ParticleManager.GetInstance().PlayOnObject( particle_id, muzzle_owner, local_pos, particle_ori );
202 OnParticleCreated(weapon, ammoType, muzzle_owner, suppressor, config_to_search, p);
203 }
204 else
205 {
206 ErrorEx(string.Format("No valid particle found for: '%1'", m_Name));
207 }
208
209 // Create light
211 {
212 vector global_pos = muzzle_owner.ModelToWorld(local_pos + Vector(-0.2, 0, 0));
213 int randX = Math.RandomInt( 0,10 );
214 if ( randX > 8 )
215 ScriptedLightBase.CreateLight( MuzzleFlashLight_2, global_pos );
216 else if ( randX > 4 )
217 ScriptedLightBase.CreateLight( MuzzleFlashLight_1, global_pos );
218 else
219 ScriptedLightBase.CreateLight(MuzzleFlashLight, global_pos);
220 }
221 }
222 }
223 }
224 }
225 }
226 }
227 }
228 }
229 }
230 }
231 }
232
233 void OnParticleCreated(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search, Particle p)
234 {
235
236 }
237
238 void OnDeactivate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
239 {
240
241 }
242
243 void OnUpdate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
244 {
245
246 }
247
248
249 //==============================================
250 // HANDLE CONFIG PARAMETERS
251 //==============================================
252
253
254 // OnlyWithinHealthLabelMin & OnlyWithinHealthLabelMax
255 bool CheckBoltStateCondition(ItemBase weapon)
256 {
257 if ( m_OnlyIfBoltIsOpen )
258 {
259 Weapon_Base wb = Weapon_Base.Cast( weapon );
260 WeaponStateBase current_state = wb.GetCurrentState();
261 return current_state.IsBoltOpen();
262 }
263
264 return true;
265 }
266
267 // OnlyWithinHealthLabelMin & OnlyWithinHealthLabelMax
268 bool CheckHealthCondition(int health_label)
269 {
270 return ( (health_label >= m_OnlyWithinHealthLabelMin) && (health_label <= m_OnlyWithinHealthLabelMax) );
271 }
272
273 // OnlyWithinOverheatLimitsMin & OnlyWithinOverheatLimitsMax
274 bool CheckOverheatingCondition(float overheating_coef)
275 {
276 return ( (overheating_coef >= m_OnlyWithinOverheatLimitsMin) && (overheating_coef <= m_OnlyWithinOverheatLimitsMax) );
277 }
278
279 // OnlyWithinRainLimitsMin & OnlyWithinRainLimitsMax
280 bool CheckRainCondition(float rain_coef)
281 {
282 return ( (rain_coef >= m_OnlyWithinRainLimitsMin) && (rain_coef <= m_OnlyWithinRainLimitsMax) );
283 }
284
285 // muzzleFlashParticle
286 int CheckParticleOverride(string ammoType)
287 {
288 int particle_id = -1;
289
290 string particle_file = "";
291 string cfg_path = "CfgAmmo " + ammoType + " muzzleFlashParticle";
292 if (GetGame().ConfigGetText( cfg_path, particle_file))
293 particle_id = ParticleList.GetParticleIDByName(particle_file);
294
295 // Config is accessed only once because the data is saved into a map for repeated access.
296
297 if ( particle_id > 0 || m_OverrideParticle == -1)
298 {
299 if (particle_file == "")
300 {
301 ErrorEx(string.Format("Cannot spawn particle effect because item %1 is missing config parameter muzzleFlashParticle!", ammoType), ErrorExSeverity.INFO);
302 }
303 else
304 {
305 particle_id = ParticleList.GetParticleIDByName(particle_file);
306
307 if (particle_id == 0)
308 {
309 string devStr;
310 #ifdef DEVELOPER
311 devStr = " Make sure it's registered there and then rebuild Scripts and Graphics PBOs.";
312 #endif
313 ErrorEx(string.Format("Cannot play particle effect with name %1 because no such file is registered in ParticleList.c!%2", particle_file, devStr));
314 m_OverrideParticle = particle_id; // Prevents another appearence of the above error.
315 }
316 }
317 }
318 else
319 {
321 }
322
323 return particle_id;
324 }
325
326 // OverrideDirectionPoint & OverrideDirectionVector
327 vector CheckOrientationOverride(vector local_pos, ItemBase muzzle_owner)
328 {
329 vector particle_ori = "0 0 0";
330 if (m_OverrideDirectionPoint != "")
331 {
332 vector target_pos = muzzle_owner.GetSelectionPositionLS(m_OverrideDirectionPoint);
333 target_pos = vector.Direction(local_pos, target_pos);
334 particle_ori = target_pos.VectorToAngles();
335 }
336 else
337 {
338 if (m_OverrideDirectionVector != Vector(0, 0, 0))
339 {
340 particle_ori = m_OverrideDirectionVector;
341 }
342
343 if (muzzle_owner.IsInherited(ItemSuppressor))
344 {
345 particle_ori = particle_ori + Vector(0,0,270); // This rotation is necesarry due to suppressors being rotated into ground in their p3d files
346 }
347 }
348
349 return particle_ori;
350 }
351}
352
353// FIRE particles
354class WeaponParticlesOnFire : WeaponParticlesBase {}
355
356// BULLET EJECT particles
357class WeaponParticlesOnBulletCasingEject : WeaponParticlesBase {}
358
359// OVERHEATING particles
360class WeaponParticlesOnOverheating: WeaponParticlesBase
361{
362 override void OnParticleCreated(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search, Particle p)
364 muzzle_owner.RegisterOverheatingParticle(p, m_OnlyWithinOverheatLimitsMin, m_OnlyWithinOverheatLimitsMax, p.GetParticleID(), muzzle_owner, p.m_DefaultPos, p.m_DefaultOri );
367 override void OnDeactivate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
369 if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
371 weapon.KillAllOverheatingParticles();
375 override void OnUpdate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
377 OnActivate(weapon, 0, ammoType, muzzle_owner, suppressor, config_to_search);
380
382{
383 Particle m_Particle;
384 int m_ParticleID;
385 Object m_Parent;
386 vector m_LocalPos;
387 vector m_LocalOri;
388
389 float m_OverheatingLimitMin;
390 float m_OverheatingLimitMax;
391
392 void RegisterParticle( Particle p)
393 {
394 m_Particle = p;
395 }
396
397 Particle GetParticle()
398 {
399 return m_Particle;
400 }
401
402 void SetOverheatingLimitMin(float min)
403 {
404 m_OverheatingLimitMin = min;
405 }
406
407 void SetOverheatingLimitMax(float max)
408 {
409 m_OverheatingLimitMax = max;
410 }
411
412 float GetOverheatingLimitMin()
413 {
414 return m_OverheatingLimitMin;
415 }
416
417 float GetOverheatingLimitMax()
418 {
419 return m_OverheatingLimitMax;
420 }
421
422 void SetParticleParams(int particle_id, Object parent, vector local_pos, vector local_ori)
423 {
424 m_ParticleID = particle_id;
425 m_Parent = parent;
426 m_LocalPos = local_pos;
427 m_LocalOri = local_ori;
428 }
429
430 int GetParticleID()
431 {
432 return m_ParticleID;
433 }
434
435 Object GetParticleParent()
436 {
437 return m_Parent;
438 }
439
440 vector GetParticlePos()
441 {
442 return m_LocalPos;
443 }
444
445 vector GetParticleOri()
446 {
447 return m_LocalOri;
448 }
449}
Definition enmath.c:7
Legacy way of using particles in the game.
Definition particle.c:7
static Particle PlayOnObject(int particle_id, Object parent_obj, vector local_pos="0 0 0", vector local_ori="0 0 0", bool force_world_rotation=false)
Creates a particle emitter, attaches it on the given object and activates it.
Definition particle.c:152
int GetParticleID()
Gets particle id.
Definition particle.c:297
represent weapon state base
Definition bullethide.c:2
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
vector m_LocalPos
Cached local pos.
Definition effect.c:62
vector m_LocalOri
Local orientation set by SetAttachedLocalOri, only used by EffectParticle.
Definition effect.c:64
proto native CGame GetGame()
ErrorExSeverity
Definition endebug.c:62
enum ShapeType ErrorEx
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
void OnDeactivate(PlayerBase player)
Definition heavymetal.c:211
void MuzzleFlashLight_1()
void ParticleManager(ParticleManagerSettings settings)
Constructor (ctor)
Widget m_Parent
Definition sizetochild.c:92
int particle_id
class SyncedValue m_Name
void OnActivate()
float m_OnlyWithinOverheatLimitsMin
bool m_IlluminateWorld
vector m_OverrideDirectionVector
int m_OverrideParticle
string m_OverrideDirectionPoint
float m_OnlyWithinRainLimitsMax
class WeaponParticlesBase OnParticleCreated(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search, Particle p)
bool m_OnlyIfBoltIsOpen
vector m_PositionOffset
string m_OnlyIfWeaponIs
string m_OnlyIfBulletIs
int m_MuzzleIndex
float m_OnlyWithinRainLimitsMin
bool m_IgnoreIfSuppressed
string m_OverridePoint
int m_OnlyWithinHealthLabelMin
float m_OnlyWithinOverheatLimitsMax
int m_OnlyWithinHealthLabelMax