ai_system.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "ai_system.h"
  2. #include "../core/world.h"
  3. #include "ai_system/ai_strategy.h"
  4. #include "ai_system/behaviors/attack_behavior.h"
  5. #include "ai_system/behaviors/builder_behavior.h"
  6. #include "ai_system/behaviors/defend_behavior.h"
  7. #include "ai_system/behaviors/expand_behavior.h"
  8. #include "ai_system/behaviors/gather_behavior.h"
  9. #include "ai_system/behaviors/production_behavior.h"
  10. #include "ai_system/behaviors/retreat_behavior.h"
  11. #include "core/event_manager.h"
  12. #include "owner_registry.h"
  13. #include "systems/ai_system/ai_command_applier.h"
  14. #include "systems/ai_system/ai_snapshot_builder.h"
  15. #include "systems/ai_system/ai_types.h"
  16. #include "systems/ai_system/ai_worker.h"
  17. #include <cstdint>
  18. #include <memory>
  19. #include <queue>
  20. #include <utility>
  21. namespace Game::Systems {
  22. AISystem::AISystem() {
  23. m_behavior_registry.register_behavior(
  24. std::make_unique<AI::RetreatBehavior>());
  25. m_behavior_registry.register_behavior(std::make_unique<AI::DefendBehavior>());
  26. m_behavior_registry.register_behavior(
  27. std::make_unique<AI::ProductionBehavior>());
  28. m_behavior_registry.register_behavior(
  29. std::make_unique<AI::BuilderBehavior>());
  30. m_behavior_registry.register_behavior(std::make_unique<AI::ExpandBehavior>());
  31. m_behavior_registry.register_behavior(std::make_unique<AI::AttackBehavior>());
  32. m_behavior_registry.register_behavior(std::make_unique<AI::GatherBehavior>());
  33. m_building_attacked_subscription = Engine::Core::ScopedEventSubscription<
  34. Engine::Core::BuildingAttackedEvent>(
  35. [this](const Engine::Core::BuildingAttackedEvent &event) {
  36. this->on_building_attacked(event);
  37. });
  38. initialize_ai_players();
  39. }
  40. void AISystem::reinitialize() {
  41. m_ai_instances.clear();
  42. initialize_ai_players();
  43. }
  44. void AISystem::initialize_ai_players() {
  45. auto &registry = OwnerRegistry::instance();
  46. const auto &ai_owner_ids = registry.get_ai_owner_ids();
  47. if (ai_owner_ids.empty()) {
  48. return;
  49. }
  50. for (uint32_t const player_id : ai_owner_ids) {
  51. int const team_id = registry.get_owner_team(player_id);
  52. AIInstance instance;
  53. instance.context.player_id = player_id;
  54. instance.context.state = AI::AIState::Idle;
  55. instance.worker = std::make_unique<AI::AIWorker>(m_reasoner, m_executor,
  56. m_behavior_registry);
  57. m_ai_instances.push_back(std::move(instance));
  58. }
  59. }
  60. AISystem::~AISystem() = default;
  61. void AISystem::set_ai_strategy(int player_id, AI::AIStrategy strategy,
  62. float aggression, float defense,
  63. float harassment) {
  64. for (auto &ai : m_ai_instances) {
  65. if (ai.context.player_id == player_id) {
  66. ai.context.strategy_config =
  67. AI::AIStrategyFactory::create_config(strategy);
  68. AI::AIStrategyFactory::apply_personality(ai.context.strategy_config,
  69. aggression, defense, harassment);
  70. break;
  71. }
  72. }
  73. }
  74. void AISystem::update(Engine::Core::World *world, float delta_time) {
  75. if (world == nullptr) {
  76. return;
  77. }
  78. m_total_game_time += delta_time;
  79. m_command_filter.update(m_total_game_time);
  80. process_results(*world);
  81. for (auto &ai : m_ai_instances) {
  82. ai.update_timer += delta_time;
  83. if (ai.update_timer < m_update_interval) {
  84. continue;
  85. }
  86. if (ai.worker->busy()) {
  87. continue;
  88. }
  89. AI::AISnapshot snapshot = Game::Systems::AI::AISnapshotBuilder::build(
  90. *world, ai.context.player_id);
  91. snapshot.game_time = m_total_game_time;
  92. AI::AIJob job;
  93. job.snapshot = std::move(snapshot);
  94. job.context = ai.context;
  95. job.delta_time = ai.update_timer;
  96. if (ai.worker->try_submit(std::move(job))) {
  97. ai.update_timer = 0.0F;
  98. }
  99. }
  100. }
  101. void AISystem::process_results(Engine::Core::World &world) {
  102. for (auto &ai : m_ai_instances) {
  103. std::queue<AI::AIResult> results;
  104. ai.worker->drain_results(results);
  105. while (!results.empty()) {
  106. auto &result = results.front();
  107. ai.context = result.context;
  108. auto filtered_commands =
  109. m_command_filter.filter(result.commands, m_total_game_time);
  110. Game::Systems::AI::AICommandApplier::apply(world, ai.context.player_id,
  111. filtered_commands);
  112. results.pop();
  113. }
  114. }
  115. }
  116. void AISystem::on_building_attacked(
  117. const Engine::Core::BuildingAttackedEvent &event) {
  118. for (auto &ai : m_ai_instances) {
  119. if (ai.context.player_id == event.owner_id) {
  120. ai.context.buildings_under_attack[event.building_id] = m_total_game_time;
  121. if (event.building_id == ai.context.primary_barracks) {
  122. ai.context.barracks_under_threat = true;
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. } // namespace Game::Systems