Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
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 
7 class 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
53  m_OnlyIfWeaponIs = "";
54  GetGame().ConfigGetText(string.Format("%1 onlyIfWeaponIs", m_Name), m_OnlyIfWeaponIs);
55 
56  // onlyIfBulletIs
57  m_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  {
125  m_OverrideParticle = -1;
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[]
146  array<float> v = new array<float>;
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
210  if (m_IlluminateWorld)
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
354 class WeaponParticlesOnFire : WeaponParticlesBase {}
355 
356 // BULLET EJECT particles
357 class WeaponParticlesOnBulletCasingEject : WeaponParticlesBase {}
358 
359 // OVERHEATING particles
360 class WeaponParticlesOnOverheating: WeaponParticlesBase
361 {
362  override void OnParticleCreated(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search, Particle p)
363  {
364  muzzle_owner.RegisterOverheatingParticle(p, m_OnlyWithinOverheatLimitsMin, m_OnlyWithinOverheatLimitsMax, p.GetParticleID(), muzzle_owner, p.m_DefaultPos, p.m_DefaultOri );
365  }
366 
367  override void OnDeactivate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
368  {
369  if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
370  {
371  weapon.KillAllOverheatingParticles();
372  }
373  }
374 
375  override void OnUpdate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
376  {
377  OnActivate(weapon, 0, ammoType, muzzle_owner, suppressor, config_to_search);
378  }
379 }
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 }
ItemBase
Definition: inventoryitem.c:730
m_OverridePoint
string m_OverridePoint
Definition: weaponparticles.c:16
GetGame
proto native CGame GetGame()
m_OnlyWithinRainLimitsMin
float m_OnlyWithinRainLimitsMin
Definition: weaponparticles.c:11
OverheatingParticle
Definition: weaponparticles.c:381
m_LocalPos
protected vector m_LocalPos
Cached local pos.
Definition: effect.c:60
m_PositionOffset
vector m_PositionOffset
Definition: weaponparticles.c:18
particle_id
int particle_id
Definition: smokesimulation.c:3
Particle
Legacy way of using particles in the game.
Definition: particle.c:6
OnParticleCreated
class WeaponParticlesBase OnParticleCreated(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search, Particle p)
Definition: weaponparticles.c:362
m_OnlyWithinHealthLabelMin
int m_OnlyWithinHealthLabelMin
Definition: weaponparticles.c:7
m_IgnoreIfSuppressed
bool m_IgnoreIfSuppressed
Definition: weaponparticles.c:3
m_Name
string m_Name
Definition: bioslobbyservice.c:35
MuzzleFlashLight_1
void MuzzleFlashLight_1()
Definition: muzzleflashlight.c:24
WeaponParticlesBase
Definition: weaponparticles.c:7
OnDeactivate
void OnDeactivate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
Definition: weaponparticles.c:231
m_Parent
protected Widget m_Parent
Definition: sizetochild.c:92
m_OnlyIfBulletIs
string m_OnlyIfBulletIs
Definition: weaponparticles.c:14
ErrorEx
enum ShapeType ErrorEx
m_OnlyWithinOverheatLimitsMin
float m_OnlyWithinOverheatLimitsMin
Definition: weaponparticles.c:9
ParticleList
Definition: particlelist.c:11
vector
Definition: enconvert.c:105
m_OnlyWithinHealthLabelMax
int m_OnlyWithinHealthLabelMax
Definition: weaponparticles.c:8
ItemSuppressor
Definition: mosin_compensator.c:1
m_OnlyWithinOverheatLimitsMax
float m_OnlyWithinOverheatLimitsMax
Definition: weaponparticles.c:10
ErrorExSeverity
ErrorExSeverity
Definition: endebug.c:61
Object
Definition: objecttyped.c:1
ScriptedLightBase
Definition: pointlightbase.c:1
OnActivate
void OnActivate(ItemBase weapon, int muzzle_index, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
Definition: weaponparticles.c:158
array< float >
PrtTest
Definition: gameplay.c:1496
m_MuzzleIndex
int m_MuzzleIndex
Definition: weaponparticles.c:5
OnUpdate
void OnUpdate(ItemBase weapon, string ammoType, ItemBase muzzle_owner, ItemBase suppressor, string config_to_search)
Definition: weaponparticles.c:236
m_OverrideDirectionPoint
string m_OverrideDirectionPoint
Definition: weaponparticles.c:13
m_IlluminateWorld
bool m_IlluminateWorld
Definition: weaponparticles.c:2
m_OnlyIfBoltIsOpen
bool m_OnlyIfBoltIsOpen
Definition: weaponparticles.c:4
m_OverrideDirectionVector
vector m_OverrideDirectionVector
Definition: weaponparticles.c:17
WeaponStateBase
represent weapon state base
Definition: bullethide.c:1
Weapon_Base
shorthand
Definition: boltactionrifle_base.c:5
m_OverrideParticle
int m_OverrideParticle
Definition: weaponparticles.c:6
m_OnlyWithinRainLimitsMax
float m_OnlyWithinRainLimitsMax
Definition: weaponparticles.c:12
m_OnlyIfWeaponIs
string m_OnlyIfWeaponIs
Definition: weaponparticles.c:15
Math
Definition: enmath.c:6
m_LocalOri
protected vector m_LocalOri
Local orientation set by SetAttachedLocalOri, only used by EffectParticle.
Definition: effect.c:62
ParticleManager
void ParticleManager(ParticleManagerSettings settings)
Constructor (ctor)
Definition: particlemanager.c:84
Vector
proto native vector Vector(float x, float y, float z)
Vector constructor from components.