dayzaihitcomponents.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //! holds hit components and its weights for attack from AI (used on each type - DayZPlayerType, DayZAnimalType, etc.)
  2. class DayZAIHitComponent
  3. {
  4. string m_Name; //! "Head", "Torso", etc.
  5. int m_Weight; //! [0..1]
  6. };
  7. class DayZAIHitComponentHelpers
  8. {
  9. /**
  10. * @fn RegisterHitComponent
  11. * @brief Register Hit Component for AI targeting
  12. *
  13. * Register Hit Component for AI targeting to array defined on entity's Type (DayZPlayerType, DayZInfectedType, etc.)
  14. * Uses component name from damage system (zone) and its weight.
  15. *
  16. * @param[in] pHitComponents \p array that keeps registered compoenents
  17. * @param[in] pName \p component name (zone in Damage System)
  18. * @param[in] pWeight \p weight of the component (probability)
  19. **/
  20. static void RegisterHitComponent(array<ref DayZAIHitComponent> pHitComponents, string pName, float pWeight)
  21. {
  22. DayZAIHitComponent newComponent = new DayZAIHitComponent();
  23. newComponent.m_Name = pName;
  24. newComponent.m_Weight = pWeight;
  25. pHitComponents.Insert(newComponent);
  26. }
  27. /**
  28. * @fn ComputeHitComponentProbability
  29. * @brief Computes hit component probability based on weights
  30. *
  31. * @param[in] pHitComponents \p array that keeps registered compoenents
  32. * @param[out] pHitComponent \p selected component name (can be passed to Damage System)
  33. *
  34. * @return true if found, false otherwise
  35. **/
  36. static bool SelectMostProbableHitComponent(array<ref DayZAIHitComponent> pHitComponents, out string pHitComponent)
  37. {
  38. int weights = SumOfWeights(pHitComponents);
  39. float rnd = Math.RandomInt(0, weights);
  40. for ( int i = 0; i < pHitComponents.Count(); ++i )
  41. {
  42. DayZAIHitComponent hitComp = pHitComponents.Get(i);
  43. rnd -= hitComp.m_Weight;
  44. if (rnd <= 0)
  45. {
  46. pHitComponent = hitComp.m_Name;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. /**
  53. * @fn SumOfWeights
  54. * @brief Calculates the sum of all entries in DayZAIHitComponent array
  55. *
  56. * @param[in] pHitComponents \p array that keeps registered compoenents
  57. *
  58. * @return Sum of weights of all entries in array
  59. **/
  60. static int SumOfWeights(array<ref DayZAIHitComponent> pHitComponents)
  61. {
  62. int sum = 0;
  63. for( int i = 0; i < pHitComponents.Count(); ++i )
  64. {
  65. DayZAIHitComponent hitComp = pHitComponents.Get(i);
  66. sum += hitComp.m_Weight;
  67. }
  68. return sum;
  69. }
  70. };