areadamage.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // DEPRECATED: Backwards compatibility class to prevent existing mods breaking
  2. // I wish I could rename this to AreaDamageTimer, but can't, because of backwards compatibility with mods
  3. class AreaDamageBase : AreaDamageManager
  4. {
  5. protected AreaDamageBase m_AreaDamage;
  6. protected float m_PlayerDamage;
  7. protected float m_OthersDamage;
  8. protected string m_AmmoName;
  9. protected int m_DamageType;
  10. protected float m_LoopInterval;
  11. protected float m_DeferDuration;
  12. protected ref array<string> m_HitZones;
  13. protected ref array<string> m_RaycastSources;
  14. protected ref array<typename> m_DamageableTypes
  15. protected ref Timer m_LoopTimer;
  16. protected ref Timer m_DeferTimer;
  17. void AreaDamageBase(EntityAI parent)
  18. {
  19. m_AreaDamage = this;
  20. m_PlayerDamage = 0.0;
  21. m_OthersDamage = 0.0;
  22. m_AmmoName = "MeleeDamage";
  23. m_DamageType = DamageType.CUSTOM;
  24. m_LoopInterval = 1.0;
  25. m_DeferDuration = 1.0;
  26. m_HitZones = new array<string>;
  27. m_RaycastSources = new array<string>;
  28. m_DamageableTypes = new array<typename>;
  29. m_DamageableTypes.Insert(DayZPlayer);
  30. m_LoopTimer = new Timer(CALL_CATEGORY_SYSTEM);
  31. m_DeferTimer = new Timer(CALL_CATEGORY_SYSTEM);
  32. m_TriggerBaseClass = "AreaDamageTrigger";
  33. }
  34. //! events
  35. void OnEnter(Object object)
  36. {
  37. if ( GetGame().IsServer() )
  38. {
  39. OnEnterServer(object);
  40. }
  41. else
  42. {
  43. OnEnterClient(object);
  44. }
  45. }
  46. void OnEnterClient(Object object) {}
  47. void OnEnterServer(Object object) {}
  48. void OnLeave(Object object)
  49. {
  50. if ( GetGame().IsServer() )
  51. {
  52. OnLeaveServer(object);
  53. }
  54. else
  55. {
  56. OnLeaveClient(object);
  57. }
  58. }
  59. void OnLeaveClient(Object object) {}
  60. void OnLeaveServer(Object object)
  61. {
  62. //! stop all running timers
  63. if ( m_DeferTimer && m_DeferTimer.IsRunning() )
  64. {
  65. m_DeferTimer.Stop();
  66. }
  67. if ( m_LoopTimer && m_LoopTimer.IsRunning() )
  68. {
  69. m_LoopTimer.Stop();
  70. }
  71. }
  72. protected void EvaluateDamage_Loop(Object object)
  73. {
  74. m_LoopTimer.Run(m_LoopInterval, this, "EvaluateDamage", new Param1<Object>(object), true);
  75. }
  76. protected void EvaluateDamage_Defer(Object object)
  77. {
  78. m_DeferTimer.Run(m_DeferDuration, this, "EvaluateDamage", new Param1<Object>(object), false);
  79. }
  80. protected void EvaluateDamage_DeferLoop(Object object)
  81. {
  82. m_DeferTimer.Run(m_DeferDuration, this, "EvaluateDamage_Loop", new Param1<Object>(object), false);
  83. }
  84. protected void EvaluateDamage(Object object)
  85. {
  86. string hitzone;
  87. if ( m_RaycastSources.Count() )
  88. {
  89. hitzone = GetRaycastedHitZone(object, m_RaycastSources);
  90. EvaluateDamage_Common(object, hitzone);
  91. }
  92. else
  93. {
  94. hitzone = GetRandomHitZone(m_HitZones);
  95. EvaluateDamage_Common(object, hitzone);
  96. }
  97. }
  98. protected void EvaluateDamage_Common(Object object, string hitzone)
  99. {
  100. if ( object && object.IsAlive() )
  101. {
  102. if ( object.IsAnyInherited( m_DamageableTypes ) )
  103. {
  104. //If we are hitting an infected or animal, we increase the damage dealt as they do not bleed
  105. //Change is multiplier
  106. /*DayZInfected dayzInfected = DayZInfected.Cast(object);
  107. DayZAnimal dayzAnimal = DayZAnimal.Cast(object);
  108. EntityAI eai = EntityAI.Cast(object);
  109. if ( dayzInfected || dayzAnimal )
  110. {
  111. //Agents should not take damage from fireplace, but just in case, keep multiplier relatively low
  112. if ( hitzone )
  113. {
  114. eai.ProcessDirectDamage(m_DamageType, EntityAI.Cast(m_ParentObject), hitzone, m_AmmoName, "0.5 0.5 0.5", 8);
  115. }
  116. else
  117. eai.ProcessDirectDamage(m_DamageType, EntityAI.Cast(m_ParentObject), "", m_AmmoName, "0.5 0.5 0.5", 8);
  118. }
  119. else*/
  120. {
  121. object.ProcessDirectDamage(m_DamageType, m_ParentObject, hitzone, m_AmmoName, "0.5 0.5 0.5", 1);
  122. }
  123. PostDamageActions();
  124. }
  125. }
  126. }
  127. //! ------------------------------------------------------
  128. //! common
  129. //!
  130. protected string GetRandomHitZone(array<string> hitzones)
  131. {
  132. Math.Randomize(-1);
  133. int idx = Math.RandomInt( 0, 100 ) % hitzones.Count();
  134. return hitzones[idx];
  135. }
  136. protected string GetRaycastedHitZone(Object victim, array<string> raycast_sources_str)
  137. {
  138. // Vertical raycast start positions: Center, North East, North West, South East, South West
  139. //vector raycast_sources[5] = {"0.0 0.1 0.0", "0.2 0.1 0.2", "-.2 0.1 0.2", "0.2 0.1 -.2", "-.2 0.1 -.2"};
  140. string hitzone;
  141. vector contact_pos;
  142. vector contact_dir;
  143. int contactComponent;
  144. bool isSteppedOn = false;
  145. array<vector> raycast_sources = new array<vector>;
  146. ref set<Object> victims = new set<Object>;
  147. // convert Array of string to array of Vectors
  148. for ( int v = 0; v < raycast_sources_str.Count(); ++v)
  149. {
  150. raycast_sources.Insert(raycast_sources_str[v].ToVector());
  151. }
  152. for ( int i = 0; i < raycast_sources.Count(); ++i )
  153. {
  154. vector raycast_start_pos = m_AreaDamageTrigger.ModelToWorld( raycast_sources.Get(i) );
  155. vector raycast_end_pos = "0 0.5 0" + raycast_start_pos;
  156. //#ifdef DEVELOPER
  157. //Debug.DrawArrow( raycast_start_pos, raycast_end_pos );
  158. //#endif
  159. DayZPhysics.RaycastRV( raycast_start_pos, raycast_end_pos, contact_pos, contact_dir, contactComponent, victims , null, m_AreaDamageTrigger, true, false, ObjIntersectIFire);
  160. for ( int j = 0; j < victims.Count(); ++j )
  161. {
  162. Object contact_obj = victims.Get(j);
  163. if ( contact_obj.IsAnyInherited(m_DamageableTypes) )
  164. {
  165. isSteppedOn = true;
  166. break;
  167. }
  168. }
  169. if ( isSteppedOn )
  170. {
  171. EntityAI eai = EntityAI.Cast(victim);
  172. if ( eai )
  173. {
  174. hitzone = eai.GetDamageZoneNameByComponentIndex(contactComponent);
  175. break;
  176. }
  177. }
  178. }
  179. if ( isSteppedOn )
  180. {
  181. return hitzone;
  182. }
  183. else
  184. {
  185. // Damage random leg since we don't know what part of player's body was caught in the trap.
  186. string dmg_zone_rnd = "LeftFoot";
  187. if ( Math.RandomIntInclusive(0, 1) == 1 )
  188. dmg_zone_rnd = "RightFoot";
  189. return dmg_zone_rnd;
  190. }
  191. }
  192. //! -----------------------------------------------------
  193. //! DEBUG
  194. //!
  195. #ifdef DEVELOPER
  196. ref array<Shape> triggerAreaShapes = new array<Shape>();
  197. void EnableDebug(bool pState = false)
  198. {
  199. //if (GetGame() && (!GetGame().IsClient() || GetGame().IsMultiplayer()))
  200. //return;
  201. if ( pState )
  202. {
  203. Debug_DrawArea();
  204. }
  205. else
  206. {
  207. Debug_CleanupShapes(triggerAreaShapes);
  208. }
  209. }
  210. protected void Debug_DrawArea()
  211. {
  212. vector min = m_AreaPosition + m_ExtentMin;
  213. vector max = m_AreaPosition + m_ExtentMax;
  214. triggerAreaShapes.Insert(Debug.DrawBox(min, max));
  215. }
  216. private void Debug_CleanupShapes(array<Shape> shapesArr)
  217. {
  218. for ( int it = 0; it < shapesArr.Count(); ++it )
  219. {
  220. Debug.RemoveShape( shapesArr[it] );
  221. }
  222. shapesArr.Clear();
  223. }
  224. #endif
  225. }