ai_tactical.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "ai_tactical.h"
  2. #include "../../units/troop_type.h"
  3. #include "../nation_registry.h"
  4. #include "ai_utils.h"
  5. #include "systems/ai_system/ai_types.h"
  6. #include "units/spawn_type.h"
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <limits>
  10. #include <string>
  11. #include <vector>
  12. namespace Game::Systems::AI {
  13. auto TacticalUtils::assessEngagement(
  14. const std::vector<const EntitySnapshot *> &friendlies,
  15. const std::vector<const ContactSnapshot *> &enemies,
  16. float min_force_ratio) -> TacticalUtils::EngagementAssessment {
  17. EngagementAssessment result;
  18. if (friendlies.empty() || enemies.empty()) {
  19. result.should_engage = false;
  20. return result;
  21. }
  22. result.friendly_count = static_cast<int>(friendlies.size());
  23. result.enemy_count = static_cast<int>(enemies.size());
  24. float total_friendly_health = 0.0F;
  25. float total_enemy_health = 0.0F;
  26. int valid_friendlies = 0;
  27. int valid_enemies = 0;
  28. for (const auto *unit : friendlies) {
  29. if (unit->max_health > 0) {
  30. total_friendly_health += static_cast<float>(unit->health) /
  31. static_cast<float>(unit->max_health);
  32. ++valid_friendlies;
  33. }
  34. }
  35. for (const auto *enemy : enemies) {
  36. if (enemy->max_health > 0) {
  37. total_enemy_health += static_cast<float>(enemy->health) /
  38. static_cast<float>(enemy->max_health);
  39. ++valid_enemies;
  40. }
  41. }
  42. result.avg_friendly_health =
  43. valid_friendlies > 0 ? (total_friendly_health / valid_friendlies) : 1.0F;
  44. result.avg_enemy_health =
  45. valid_enemies > 0 ? (total_enemy_health / valid_enemies) : 1.0F;
  46. float const friendly_strength =
  47. static_cast<float>(result.friendly_count) * result.avg_friendly_health;
  48. float const enemy_strength =
  49. static_cast<float>(result.enemy_count) * result.avg_enemy_health;
  50. if (enemy_strength < 0.01F) {
  51. result.force_ratio = 10.0F;
  52. } else {
  53. result.force_ratio = friendly_strength / enemy_strength;
  54. }
  55. result.confidence_level =
  56. std::clamp((result.force_ratio - 0.5F) / 1.5F, 0.0F, 1.0F);
  57. result.should_engage = (result.force_ratio >= min_force_ratio);
  58. return result;
  59. }
  60. auto TacticalUtils::selectFocusFireTarget(
  61. const std::vector<const EntitySnapshot *> &,
  62. const std::vector<const ContactSnapshot *> &enemies, float group_center_x,
  63. float group_center_y, float group_center_z, const AIContext &context,
  64. Engine::Core::EntityID currentTarget) -> TacticalUtils::TargetScore {
  65. TargetScore best_target;
  66. best_target.score = -std::numeric_limits<float>::infinity();
  67. if (enemies.empty()) {
  68. return best_target;
  69. }
  70. for (const auto *enemy : enemies) {
  71. float score = 0.0F;
  72. float const dist = distance(enemy->posX, enemy->posY, enemy->posZ,
  73. group_center_x, group_center_y, group_center_z);
  74. score -= dist * 0.5F;
  75. if (enemy->max_health > 0) {
  76. float const health_ratio = static_cast<float>(enemy->health) /
  77. static_cast<float>(enemy->max_health);
  78. if (health_ratio < 0.5F) {
  79. score += 8.0F * (1.0F - health_ratio);
  80. }
  81. if (health_ratio < 0.25F) {
  82. score += 12.0F;
  83. }
  84. }
  85. float const type_priority = getUnitTypePriority(
  86. Game::Units::spawn_typeToString(enemy->spawn_type), context.nation);
  87. score += type_priority * 3.0F;
  88. if (!enemy->is_building) {
  89. score += 5.0F;
  90. }
  91. if (currentTarget != 0 && enemy->id == currentTarget) {
  92. score += 10.0F;
  93. }
  94. bool const isolated = isTargetIsolated(*enemy, enemies, 8.0F);
  95. if (isolated) {
  96. score += 6.0F;
  97. }
  98. if (context.primaryBarracks != 0) {
  99. float const dist_to_base =
  100. distance(enemy->posX, enemy->posY, enemy->posZ, context.base_pos_x,
  101. context.base_pos_y, context.base_pos_z);
  102. if (dist_to_base < 16.0F) {
  103. score += (16.0F - dist_to_base) * 0.8F;
  104. }
  105. }
  106. if (context.state == AIState::Attacking && !enemy->is_building) {
  107. score += 3.0F;
  108. }
  109. if (score > best_target.score) {
  110. best_target.target_id = enemy->id;
  111. best_target.score = score;
  112. best_target.distance_to_group = dist;
  113. best_target.is_low_health =
  114. (enemy->max_health > 0 && enemy->health < enemy->max_health / 2);
  115. best_target.is_isolated = isolated;
  116. }
  117. }
  118. return best_target;
  119. }
  120. auto TacticalUtils::calculateForceStrength(
  121. const std::vector<const EntitySnapshot *> &units) -> float {
  122. float strength = 0.0F;
  123. for (const auto *unit : units) {
  124. if (unit->max_health > 0) {
  125. float const health_ratio = static_cast<float>(unit->health) /
  126. static_cast<float>(unit->max_health);
  127. strength += health_ratio;
  128. } else {
  129. strength += 1.0F;
  130. }
  131. }
  132. return strength;
  133. }
  134. auto TacticalUtils::calculateForceStrength(
  135. const std::vector<const ContactSnapshot *> &units) -> float {
  136. float strength = 0.0F;
  137. for (const auto *unit : units) {
  138. if (unit->max_health > 0) {
  139. float const health_ratio = static_cast<float>(unit->health) /
  140. static_cast<float>(unit->max_health);
  141. strength += health_ratio;
  142. } else {
  143. strength += 1.0F;
  144. }
  145. }
  146. return strength;
  147. }
  148. auto TacticalUtils::isTargetIsolated(
  149. const ContactSnapshot &target,
  150. const std::vector<const ContactSnapshot *> &allEnemies,
  151. float isolation_radius) -> bool {
  152. const float isolation_radius_sq = isolation_radius * isolation_radius;
  153. int nearby_allies = 0;
  154. for (const auto *enemy : allEnemies) {
  155. if (enemy->id == target.id) {
  156. continue;
  157. }
  158. float const dist_sq =
  159. distance_squared(target.posX, target.posY, target.posZ, enemy->posX,
  160. enemy->posY, enemy->posZ);
  161. if (dist_sq <= isolation_radius_sq) {
  162. ++nearby_allies;
  163. }
  164. }
  165. return (nearby_allies <= 1);
  166. }
  167. auto TacticalUtils::getUnitTypePriority(const std::string &unit_type,
  168. const Game::Systems::Nation *nation)
  169. -> float {
  170. if (nation != nullptr) {
  171. auto troop_type = Game::Units::troop_typeFromString(unit_type);
  172. if (nation->is_ranged_unit(troop_type)) {
  173. return 3.0F;
  174. }
  175. if (nation->isMeleeUnit(troop_type)) {
  176. return 2.0F;
  177. }
  178. }
  179. if (unit_type == "worker" || unit_type == "villager") {
  180. return 1.0F;
  181. }
  182. auto spawn_type = Game::Units::spawn_typeFromString(unit_type);
  183. if (spawn_type && Game::Units::is_building_spawn(*spawn_type)) {
  184. return 0.5F;
  185. }
  186. if (unit_type == "base") {
  187. return 0.5F;
  188. }
  189. return 1.5F;
  190. }
  191. } // namespace Game::Systems::AI