ai_reasoner.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "ai_reasoner.h"
  2. #include "../../core/ownership_constants.h"
  3. #include "../../game_config.h"
  4. #include "../nation_registry.h"
  5. #include "ai_utils.h"
  6. #include "systems/ai_system/ai_types.h"
  7. #include "units/spawn_type.h"
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <limits>
  11. namespace Game::Systems::AI {
  12. void AIReasoner::update_context(const AISnapshot &snapshot, AIContext &ctx) {
  13. if (ctx.nation == nullptr) {
  14. ctx.nation =
  15. Game::Systems::NationRegistry::instance().get_nation_for_player(
  16. ctx.player_id);
  17. }
  18. cleanup_dead_units(snapshot, ctx);
  19. int previous_unit_count = ctx.total_units;
  20. ctx.military_units.clear();
  21. ctx.buildings.clear();
  22. ctx.primary_barracks = 0;
  23. ctx.total_units = 0;
  24. ctx.idle_units = 0;
  25. ctx.combat_units = 0;
  26. ctx.melee_count = 0;
  27. ctx.ranged_count = 0;
  28. ctx.builder_count = 0;
  29. ctx.damaged_units_count = 0;
  30. ctx.average_health = 1.0F;
  31. ctx.rally_x = 0.0F;
  32. ctx.rally_z = 0.0F;
  33. ctx.barracks_under_threat = false;
  34. ctx.nearby_threat_count = 0;
  35. ctx.closest_threat_distance = std::numeric_limits<float>::infinity();
  36. ctx.base_pos_x = 0.0F;
  37. ctx.base_pos_y = 0.0F;
  38. ctx.base_pos_z = 0.0F;
  39. ctx.visible_enemy_count = 0;
  40. ctx.enemy_buildings_count = 0;
  41. ctx.neutral_barracks_count = 0;
  42. ctx.average_enemy_distance = 0.0F;
  43. ctx.home_count = 0;
  44. ctx.defense_tower_count = 0;
  45. ctx.barracks_count = 0;
  46. ctx.max_troops_per_player =
  47. Game::GameConfig::instance().get_max_troops_per_player();
  48. constexpr float attack_record_timeout = 10.0F;
  49. auto it = ctx.buildings_under_attack.begin();
  50. while (it != ctx.buildings_under_attack.end()) {
  51. bool still_exists = false;
  52. for (const auto &entity : snapshot.friendly_units) {
  53. if (entity.id == it->first && entity.is_building) {
  54. still_exists = true;
  55. break;
  56. }
  57. }
  58. if (!still_exists ||
  59. (snapshot.game_time - it->second) > attack_record_timeout) {
  60. it = ctx.buildings_under_attack.erase(it);
  61. } else {
  62. ++it;
  63. }
  64. }
  65. float total_health_ratio = 0.0F;
  66. for (const auto &entity : snapshot.friendly_units) {
  67. if (entity.is_building) {
  68. ctx.buildings.push_back(entity.id);
  69. if (entity.spawn_type == Game::Units::SpawnType::Home) {
  70. ctx.home_count++;
  71. } else if (entity.spawn_type == Game::Units::SpawnType::DefenseTower) {
  72. ctx.defense_tower_count++;
  73. } else if (entity.spawn_type == Game::Units::SpawnType::Barracks) {
  74. ctx.barracks_count++;
  75. }
  76. if (entity.spawn_type == Game::Units::SpawnType::Barracks &&
  77. ctx.primary_barracks == 0) {
  78. ctx.primary_barracks = entity.id;
  79. ctx.rally_x = entity.pos_x - 5.0F;
  80. ctx.rally_z = entity.pos_z;
  81. ctx.base_pos_x = entity.pos_x;
  82. ctx.base_pos_y = entity.pos_y;
  83. ctx.base_pos_z = entity.pos_z;
  84. }
  85. continue;
  86. }
  87. ctx.military_units.push_back(entity.id);
  88. ctx.total_units++;
  89. if (ctx.nation != nullptr) {
  90. auto troop_type_opt =
  91. Game::Units::spawn_typeToTroopType(entity.spawn_type);
  92. if (troop_type_opt) {
  93. auto troop_type = *troop_type_opt;
  94. if (troop_type == Game::Units::TroopType::Builder) {
  95. ctx.builder_count++;
  96. } else if (ctx.nation->is_ranged_unit(troop_type)) {
  97. ctx.ranged_count++;
  98. } else if (ctx.nation->is_melee_unit(troop_type)) {
  99. ctx.melee_count++;
  100. }
  101. }
  102. }
  103. if (!entity.movement.has_component || !entity.movement.has_target) {
  104. ctx.idle_units++;
  105. } else {
  106. ctx.combat_units++;
  107. }
  108. if (entity.max_health > 0) {
  109. float const health_ratio = static_cast<float>(entity.health) /
  110. static_cast<float>(entity.max_health);
  111. total_health_ratio += health_ratio;
  112. if (health_ratio < 0.5F) {
  113. ctx.damaged_units_count++;
  114. }
  115. }
  116. }
  117. ctx.average_health =
  118. (ctx.total_units > 0)
  119. ? (total_health_ratio / static_cast<float>(ctx.total_units))
  120. : 1.0F;
  121. ctx.visible_enemy_count = static_cast<int>(snapshot.visible_enemies.size());
  122. float total_enemy_dist = 0.0F;
  123. for (const auto &enemy : snapshot.visible_enemies) {
  124. if (enemy.is_building) {
  125. ctx.enemy_buildings_count++;
  126. if (enemy.spawn_type == Game::Units::SpawnType::Barracks &&
  127. Game::Core::isNeutralOwner(enemy.owner_id)) {
  128. ctx.neutral_barracks_count++;
  129. }
  130. }
  131. if (ctx.primary_barracks != 0) {
  132. float const dist =
  133. distance(enemy.pos_x, enemy.pos_y, enemy.pos_z, ctx.base_pos_x,
  134. ctx.base_pos_y, ctx.base_pos_z);
  135. total_enemy_dist += dist;
  136. }
  137. }
  138. ctx.average_enemy_distance =
  139. (ctx.visible_enemy_count > 0)
  140. ? (total_enemy_dist / static_cast<float>(ctx.visible_enemy_count))
  141. : 1000.0F;
  142. if (ctx.primary_barracks != 0) {
  143. constexpr float defend_radius = 40.0F;
  144. constexpr float critical_radius = 20.0F;
  145. const float defend_radius_sq = defend_radius * defend_radius;
  146. const float critical_radius_sq = critical_radius * critical_radius;
  147. for (const auto &enemy : snapshot.visible_enemies) {
  148. float const dist_sq =
  149. distance_squared(enemy.pos_x, enemy.pos_y, enemy.pos_z,
  150. ctx.base_pos_x, ctx.base_pos_y, ctx.base_pos_z);
  151. if (dist_sq <= defend_radius_sq) {
  152. ctx.barracks_under_threat = true;
  153. ctx.nearby_threat_count++;
  154. float const dist = std::sqrt(std::max(dist_sq, 0.0F));
  155. ctx.closest_threat_distance =
  156. std::min(ctx.closest_threat_distance, dist);
  157. }
  158. }
  159. if (!ctx.barracks_under_threat) {
  160. ctx.closest_threat_distance = std::numeric_limits<float>::infinity();
  161. }
  162. }
  163. if (ctx.total_units != previous_unit_count || ctx.combat_units > 0) {
  164. ctx.consecutive_no_progress_cycles = 0;
  165. ctx.last_meaningful_action_time = snapshot.game_time;
  166. } else if (ctx.idle_units > 0 || ctx.visible_enemy_count > 0) {
  167. ctx.consecutive_no_progress_cycles++;
  168. }
  169. if (ctx.last_meaningful_action_time == 0.0F) {
  170. ctx.last_meaningful_action_time = snapshot.game_time;
  171. }
  172. ctx.last_total_units = ctx.total_units;
  173. }
  174. void AIReasoner::update_state_machine(const AISnapshot &snapshot,
  175. AIContext &ctx, float delta_time) {
  176. ctx.state_timer += delta_time;
  177. ctx.decision_timer += delta_time;
  178. constexpr float min_state_duration = 3.0F;
  179. constexpr float max_no_progress_duration = 3.0F;
  180. bool deadlock_detected = false;
  181. if (ctx.state_timer > ctx.max_state_duration) {
  182. deadlock_detected = true;
  183. }
  184. float time_since_progress =
  185. snapshot.game_time - ctx.last_meaningful_action_time;
  186. if (time_since_progress >= max_no_progress_duration && ctx.idle_units > 0) {
  187. deadlock_detected = true;
  188. }
  189. AIState previous_state = ctx.state;
  190. if ((ctx.barracks_under_threat || !ctx.buildings_under_attack.empty()) &&
  191. ctx.state != AIState::Defending) {
  192. ctx.state = AIState::Defending;
  193. }
  194. else if (ctx.visible_enemy_count > 0 && ctx.average_enemy_distance < 50.0F &&
  195. (ctx.state == AIState::Gathering || ctx.state == AIState::Idle)) {
  196. ctx.state = AIState::Defending;
  197. }
  198. if (deadlock_detected && ctx.state != AIState::Defending) {
  199. if (ctx.state == AIState::Idle && ctx.total_units > 0) {
  200. ctx.state = AIState::Gathering;
  201. } else if (ctx.state == AIState::Gathering) {
  202. if (ctx.visible_enemy_count > 0) {
  203. ctx.state = AIState::Attacking;
  204. } else {
  205. ctx.state = AIState::Idle;
  206. }
  207. } else if (ctx.state == AIState::Attacking) {
  208. ctx.assigned_units.clear();
  209. if (ctx.average_health < 0.5F) {
  210. ctx.state = AIState::Defending;
  211. } else {
  212. ctx.state = AIState::Idle;
  213. }
  214. }
  215. ctx.consecutive_no_progress_cycles = 0;
  216. ctx.debug_info.deadlock_recoveries++;
  217. }
  218. if (ctx.decision_timer < 2.0F) {
  219. if (ctx.state != previous_state) {
  220. ctx.state_timer = 0.0F;
  221. ctx.debug_info.state_transitions++;
  222. }
  223. return;
  224. }
  225. ctx.decision_timer = 0.0F;
  226. ctx.debug_info.total_decisions_made++;
  227. previous_state = ctx.state;
  228. if (ctx.state_timer < min_state_duration &&
  229. ((!ctx.barracks_under_threat && ctx.buildings_under_attack.empty()) ||
  230. ctx.state == AIState::Defending)) {
  231. return;
  232. }
  233. switch (ctx.state) {
  234. case AIState::Idle:
  235. if (ctx.idle_units >= 1) {
  236. ctx.state = AIState::Gathering;
  237. } else if (ctx.average_health <
  238. (0.40F * ctx.strategy_config.defense_modifier) &&
  239. ctx.total_units > 0) {
  240. ctx.state = AIState::Defending;
  241. } else if (ctx.neutral_barracks_count > 0 &&
  242. ctx.total_units >=
  243. static_cast<int>(3.0F /
  244. ctx.strategy_config.expansion_priority) &&
  245. ctx.strategy_config.expansion_priority > 0.8F) {
  246. ctx.state = AIState::Expanding;
  247. } else if (ctx.total_units >= 1 && ctx.visible_enemy_count > 0) {
  248. if (ctx.total_units >=
  249. static_cast<int>(2.0F / ctx.strategy_config.min_attack_force) ||
  250. ctx.barracks_under_threat) {
  251. ctx.state = AIState::Attacking;
  252. }
  253. }
  254. break;
  255. case AIState::Gathering: {
  256. const auto &strategy = ctx.strategy_config;
  257. const int MIN_UNITS_FOR_REACTIVE_ATTACK =
  258. static_cast<int>(2.0F / strategy.min_attack_force);
  259. const int MIN_UNITS_FOR_PROACTIVE_ATTACK =
  260. static_cast<int>(4.0F * strategy.min_attack_force);
  261. const int MIN_UNITS_FOR_EXPANSION =
  262. static_cast<int>(3.0F / strategy.expansion_priority);
  263. if (ctx.total_units < 1) {
  264. ctx.state = AIState::Idle;
  265. } else if (ctx.average_health < (0.40F * strategy.defense_modifier)) {
  266. ctx.state = AIState::Defending;
  267. } else if (ctx.neutral_barracks_count > 0 &&
  268. ctx.total_units >= MIN_UNITS_FOR_EXPANSION &&
  269. strategy.expansion_priority > 0.8F) {
  270. ctx.state = AIState::Expanding;
  271. } else if (ctx.visible_enemy_count > 0 &&
  272. ctx.total_units >= MIN_UNITS_FOR_REACTIVE_ATTACK) {
  273. ctx.state = AIState::Attacking;
  274. } else if (ctx.total_units >= MIN_UNITS_FOR_PROACTIVE_ATTACK &&
  275. strategy.aggression_modifier > 0.8F) {
  276. ctx.state = AIState::Attacking;
  277. }
  278. } break;
  279. case AIState::Attacking:
  280. if (ctx.average_health < ctx.strategy_config.retreat_threshold) {
  281. ctx.state = AIState::Retreating;
  282. } else if (ctx.total_units == 0) {
  283. ctx.state = AIState::Idle;
  284. } else if (ctx.visible_enemy_count == 0 && ctx.state_timer > 15.0F) {
  285. ctx.state = AIState::Idle;
  286. } else if (ctx.average_health <
  287. (0.50F * ctx.strategy_config.defense_modifier) &&
  288. ctx.damaged_units_count * 2 > ctx.total_units) {
  289. if (!ctx.barracks_under_threat) {
  290. ctx.state = AIState::Defending;
  291. }
  292. }
  293. break;
  294. case AIState::Defending:
  295. if (ctx.barracks_under_threat || !ctx.buildings_under_attack.empty()) {
  296. } else if (ctx.total_units >=
  297. static_cast<int>(4.0F *
  298. ctx.strategy_config.min_attack_force) &&
  299. ctx.average_health >
  300. (0.65F / ctx.strategy_config.defense_modifier)) {
  301. ctx.state = AIState::Attacking;
  302. } else if (ctx.average_health >
  303. (0.80F / ctx.strategy_config.defense_modifier) &&
  304. ctx.visible_enemy_count == 0) {
  305. ctx.state = AIState::Idle;
  306. } else if (ctx.total_units < 2 && !ctx.barracks_under_threat) {
  307. ctx.state = AIState::Idle;
  308. }
  309. break;
  310. case AIState::Retreating:
  311. if (ctx.state_timer > 6.0F && ctx.average_health > 0.55F) {
  312. ctx.state = AIState::Defending;
  313. } else if (ctx.state_timer > 12.0F) {
  314. ctx.state = AIState::Idle;
  315. ctx.assigned_units.clear();
  316. } else if (ctx.average_health > 0.70F && ctx.state_timer > 3.0F) {
  317. ctx.state = AIState::Defending;
  318. }
  319. break;
  320. case AIState::Expanding:
  321. if (ctx.neutral_barracks_count == 0) {
  322. if (ctx.visible_enemy_count > 0) {
  323. ctx.state = AIState::Attacking;
  324. } else {
  325. ctx.state = AIState::Gathering;
  326. }
  327. } else if (ctx.total_units < 2) {
  328. ctx.state = AIState::Gathering;
  329. } else if (ctx.barracks_under_threat ||
  330. !ctx.buildings_under_attack.empty()) {
  331. ctx.state = AIState::Defending;
  332. } else if (ctx.average_health < 0.40F) {
  333. ctx.state = AIState::Defending;
  334. }
  335. break;
  336. }
  337. if (ctx.state != previous_state) {
  338. ctx.state_timer = 0.0F;
  339. ctx.consecutive_no_progress_cycles = 0;
  340. ctx.debug_info.state_transitions++;
  341. }
  342. }
  343. void AIReasoner::validate_state(AIContext &ctx) {
  344. constexpr size_t MAX_ASSIGNMENT_MULTIPLIER = 2;
  345. constexpr int MAX_NO_PROGRESS_CYCLES = 50;
  346. constexpr float MAX_STATE_TIMER = 1000.0F;
  347. constexpr float MAX_DECISION_TIMER = 100.0F;
  348. if (ctx.total_units == 0 && ctx.state != AIState::Idle) {
  349. ctx.state = AIState::Idle;
  350. ctx.state_timer = 0.0F;
  351. ctx.consecutive_no_progress_cycles = 0;
  352. }
  353. if (ctx.primary_barracks == 0 && ctx.buildings.empty()) {
  354. if (ctx.state == AIState::Defending && !ctx.barracks_under_threat) {
  355. ctx.state = AIState::Idle;
  356. ctx.state_timer = 0.0F;
  357. }
  358. }
  359. if (ctx.state_timer > MAX_STATE_TIMER) {
  360. ctx.state_timer = ctx.max_state_duration;
  361. }
  362. if (ctx.decision_timer > MAX_DECISION_TIMER) {
  363. ctx.decision_timer = 0.0F;
  364. }
  365. size_t max_expected_assignments =
  366. static_cast<size_t>(ctx.total_units) * MAX_ASSIGNMENT_MULTIPLIER;
  367. if (ctx.assigned_units.size() > max_expected_assignments) {
  368. ctx.assigned_units.clear();
  369. }
  370. if (ctx.consecutive_no_progress_cycles > MAX_NO_PROGRESS_CYCLES) {
  371. ctx.consecutive_no_progress_cycles = 0;
  372. ctx.state = AIState::Idle;
  373. ctx.assigned_units.clear();
  374. }
  375. }
  376. } // namespace Game::Systems::AI