areadamagecomponentraycasted.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //! Newer implementation equivalent of "AreaDamageRegularRaycasted", hitzone selection only
  2. class AreaDamageComponentRaycasted : AreaDamageComponent
  3. {
  4. // Defined in local space of the trigger
  5. ref array<vector> m_RaycastSources;
  6. vector m_RaycastEndOffset;
  7. ref array<ref RaycastRVResult> m_RaycastCache;
  8. int m_RaycastCachedIndex;
  9. void AreaDamageComponentRaycasted(AreaDamageManager parent)
  10. {
  11. m_RaycastSources = new array<vector>;
  12. m_RaycastEndOffset = "0 0.5 0";
  13. m_RaycastCache = new array<ref RaycastRVResult>;
  14. m_RaycastCachedIndex = -1;
  15. }
  16. override void OnTriggerCreated()
  17. {
  18. super.OnTriggerCreated();
  19. ClearCache();
  20. }
  21. void SetRaycastSources( array<string> raycast_sources )
  22. {
  23. m_RaycastSources.Clear();
  24. // convert Array of string to array of Vectors
  25. int nrOfSources = raycast_sources.Count();
  26. for ( int v = 0; v < nrOfSources; ++v)
  27. {
  28. m_RaycastSources.Insert(raycast_sources[v].ToVector());
  29. }
  30. }
  31. void SetRaycastSourcesVector( array<vector> raycast_sources )
  32. {
  33. m_RaycastSources = raycast_sources;
  34. }
  35. void SetRaycastLength(float length)
  36. {
  37. m_RaycastEndOffset = Vector(0, length, 0);
  38. }
  39. override void OnStayFinishServerEvent()
  40. {
  41. super.OnStayFinishServerEvent();
  42. ClearCache();
  43. }
  44. override protected AreaDamageComponentData GetAreaDamageComponentData(Object object)
  45. {
  46. AreaDamageComponentData data = new AreaDamageComponentData;
  47. data.Hitzone = GetRaycastedHitZone(object);
  48. return data;
  49. }
  50. protected void ClearCache()
  51. {
  52. m_RaycastCache.Clear();
  53. m_RaycastCachedIndex = -1;
  54. }
  55. protected string GetRaycastedHitZone(Object victim)
  56. {
  57. int nrOfCachedResults = m_RaycastSources.Count();
  58. for ( int c = 0; c < nrOfCachedResults; ++c )
  59. {
  60. RaycastRVResult cachedRes = m_RaycastCache[c];
  61. if ( cachedRes.obj == victim )
  62. return victim.GetDamageZoneNameByComponentIndex(cachedRes.component);
  63. }
  64. int nrOfSources = m_RaycastSources.Count();
  65. array<ref RaycastRVResult> victims = new array<ref RaycastRVResult>;
  66. string hitzone = "";
  67. AreaDamageTriggerBase trigger = m_Parent.GetTrigger();
  68. RaycastRVParams params = new RaycastRVParams( vector.Zero, vector.Zero, trigger, 0.0 );
  69. params.type = ObjIntersectIFire;
  70. params.flags = CollisionFlags.ONLYDYNAMIC;
  71. for ( int i = m_RaycastCachedIndex + 1; i < nrOfSources; ++i )
  72. {
  73. m_RaycastCachedIndex = i;
  74. params.begPos = trigger.ModelToWorld( m_RaycastSources[i] );
  75. params.endPos = params.begPos + m_RaycastEndOffset;
  76. if ( DayZPhysics.RaycastRVProxy(params, victims) )
  77. {
  78. for ( int j = 0; j < victims.Count(); ++j )
  79. {
  80. RaycastRVResult res = victims[j];
  81. if (res.obj == victim)
  82. hitzone = victim.GetDamageZoneNameByComponentIndex(res.component);
  83. if (res.obj.IsAnyInherited(m_DamageableTypes))
  84. m_RaycastCache.Insert(res);
  85. }
  86. if ( !( hitzone == "") )
  87. return hitzone;
  88. victims.Clear();
  89. }
  90. }
  91. return GetFallbackHitZone(victim);
  92. }
  93. protected string GetFallbackHitZone(Object victim)
  94. {
  95. Error(string.Format("[WARNING] :: [%1] :: [AreaDamageComponentRaycasted] :: No proper HitZone found for damaging %2, using fallback.",
  96. m_Parent, Object.GetDebugName(victim)));
  97. // Fallbacks, currently are implemented assuming that foot/leg damagezones would be desired to damage
  98. if ( victim.IsInherited(DayZPlayer) || victim.IsInherited(DayZInfected) )
  99. {
  100. // Damage random leg since we don't know what part of player's body was caught in the trap.
  101. if ( Math.RandomIntInclusive(0, 1) == 1 )
  102. return "RightFoot";
  103. return "LeftFoot";
  104. }
  105. else
  106. {
  107. array<string> damageZones = new array<string>;
  108. victim.GetDamageZones(damageZones);
  109. int nrOfDmgZones = damageZones.Count();
  110. if (nrOfDmgZones > 0)
  111. {
  112. for (int z = 0; z < nrOfDmgZones; ++z)
  113. {
  114. if ( damageZones[z].Contains("Foot") || damageZones[z].Contains("Leg") )
  115. return damageZones[z];
  116. }
  117. return damageZones.GetRandomElement();
  118. }
  119. else
  120. return "";
  121. }
  122. }
  123. }