damagesystem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. class TotalDamageResult: Managed
  2. {
  3. proto native float GetDamage(string zoneName, string healthType);
  4. proto native float GetHighestDamage(string healthType);
  5. };
  6. //-----------------------------------------------------------------------------
  7. //! exposed from C++ (do not change)
  8. enum DamageType
  9. {
  10. CLOSE_COMBAT, // 0
  11. FIRE_ARM, // 1
  12. EXPLOSION,
  13. STUN,
  14. CUSTOM
  15. }
  16. //-----------------------------------------------------------------------------
  17. class DamageSystem
  18. {
  19. static proto native void CloseCombatDamage(EntityAI source, Object targetObject, int targetComponentIndex, string ammoTypeName, vector worldPos, int directDamageFlags = ProcessDirectDamageFlags.ALL_TRANSFER);
  20. static proto native void CloseCombatDamageName(EntityAI source, Object targetObject, string targetComponentName, string ammoTypeName, vector worldPos, int directDamageFlags = ProcessDirectDamageFlags.ALL_TRANSFER);
  21. static proto native void ExplosionDamage(EntityAI source, Object directHitObject, string ammoTypeName, vector worldPos, int damageType);
  22. static bool GetDamageZoneMap(EntityAI entity, out DamageZoneMap zoneMap)
  23. {
  24. string path_base;
  25. string path;
  26. if (entity.IsWeapon())
  27. {
  28. path_base = CFG_WEAPONSPATH;
  29. }
  30. else if (entity.IsMagazine())
  31. {
  32. path_base = CFG_MAGAZINESPATH;
  33. }
  34. else
  35. {
  36. path_base = CFG_VEHICLESPATH;
  37. }
  38. path_base = string.Format("%1 %2 DamageSystem DamageZones", path_base, entity.GetType());
  39. if (!GetGame().ConfigIsExisting(path_base))
  40. {
  41. return false;
  42. }
  43. else
  44. {
  45. string zone;
  46. array<string> zone_names = new array<string>;
  47. array<string> component_names;
  48. entity.GetDamageZones(zone_names);
  49. for (int i = 0; i < zone_names.Count(); i++)
  50. {
  51. component_names = new array<string>;
  52. zone = zone_names.Get(i);
  53. path = string.Format("%1 %2 componentNames ", path_base, zone);
  54. if (GetGame().ConfigIsExisting(path))
  55. {
  56. GetGame().ConfigGetTextArray(path,component_names);
  57. }
  58. zoneMap.Insert(zone,component_names);
  59. }
  60. return true;
  61. }
  62. }
  63. //! Returns damage zone to which the named component belongs
  64. static bool GetDamageZoneFromComponentName(notnull EntityAI entity, string component, out string damageZone)
  65. {
  66. DamageZoneMap zoneMap = entity.GetEntityDamageZoneMap();
  67. array<array<string>> components;
  68. components = zoneMap.GetValueArray();
  69. for (int i = 0; i < components.Count(); i++)
  70. {
  71. array<string> inner = components.Get(i);
  72. for (int j = 0; j < inner.Count(); j++)
  73. {
  74. string innerComponentName = inner.Get(j);
  75. innerComponentName.ToLower();
  76. //We don't have a component name, no need to proceed
  77. if ( innerComponentName == "" )
  78. break;
  79. if (innerComponentName == component)
  80. {
  81. damageZone = zoneMap.GetKey(i);
  82. return true;
  83. }
  84. }
  85. }
  86. damageZone = "";
  87. return false;
  88. }
  89. static bool GetComponentNamesFromDamageZone(notnull EntityAI entity, string damageZone, out array<string> componentNames)
  90. {
  91. string path;
  92. if (entity.IsWeapon())
  93. {
  94. path = CFG_WEAPONSPATH;
  95. }
  96. else if (entity.IsMagazine())
  97. {
  98. path = CFG_MAGAZINESPATH;
  99. }
  100. else
  101. {
  102. path = CFG_VEHICLESPATH;
  103. }
  104. path = string.Format("%1 %2 DamageSystem DamageZones %3 componentNames", path, entity.GetType(), damageZone);
  105. if (GetGame().ConfigIsExisting(path))
  106. {
  107. GetGame().ConfigGetTextArray(path,componentNames);
  108. return true;
  109. }
  110. return false;
  111. }
  112. static string GetDamageDisplayName(EntityAI entity, string zone)
  113. {
  114. string component_name;
  115. entity.GetEntityDamageDisplayNameMap().Find(zone.Hash(), component_name);
  116. component_name = Widget.TranslateString(component_name);
  117. return component_name;
  118. }
  119. static void ResetAllZones(EntityAI entity)
  120. {
  121. DamageZoneMap zonesMap = new DamageZoneMap();
  122. DamageSystem.GetDamageZoneMap(entity, zonesMap);
  123. array<string> zones = zonesMap.GetKeyArray();
  124. entity.SetHealth("", "Health", entity.GetMaxHealth("","Health"));
  125. entity.SetHealth("", "Shock", entity.GetMaxHealth("","Shock"));
  126. entity.SetHealth("", "Blood", entity.GetMaxHealth("","Blood"));
  127. foreach (string zone : zones)
  128. {
  129. entity.SetHealth(zone, "Health", entity.GetMaxHealth(zone,"Health"));
  130. entity.SetHealth(zone, "Shock", entity.GetMaxHealth(zone,"Shock"));
  131. entity.SetHealth(zone, "Blood", entity.GetMaxHealth(zone,"Blood"));
  132. }
  133. }
  134. }
  135. typedef map<string,ref array<string>> DamageZoneMap; //<zone_name,<components>>