ai_system.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_behaviorRegistry.register_behavior(std::make_unique<AI::RetreatBehavior>());
  24. m_behaviorRegistry.register_behavior(std::make_unique<AI::DefendBehavior>());
  25. m_behaviorRegistry.register_behavior(
  26. std::make_unique<AI::ProductionBehavior>());
  27. m_behaviorRegistry.register_behavior(std::make_unique<AI::BuilderBehavior>());
  28. m_behaviorRegistry.register_behavior(std::make_unique<AI::ExpandBehavior>());
  29. m_behaviorRegistry.register_behavior(std::make_unique<AI::AttackBehavior>());
  30. m_behaviorRegistry.register_behavior(std::make_unique<AI::GatherBehavior>());
  31. m_buildingAttackedSubscription = Engine::Core::ScopedEventSubscription<
  32. Engine::Core::BuildingAttackedEvent>(
  33. [this](const Engine::Core::BuildingAttackedEvent &event) {
  34. this->on_building_attacked(event);
  35. });
  36. initialize_ai_players();
  37. }
  38. void AISystem::reinitialize() {
  39. m_aiInstances.clear();
  40. initialize_ai_players();
  41. }
  42. void AISystem::initialize_ai_players() {
  43. auto &registry = OwnerRegistry::instance();
  44. const auto &ai_owner_ids = registry.get_ai_owner_ids();
  45. if (ai_owner_ids.empty()) {
  46. return;
  47. }
  48. for (uint32_t const player_id : ai_owner_ids) {
  49. int const team_id = registry.get_owner_team(player_id);
  50. AIInstance instance;
  51. instance.context.player_id = player_id;
  52. instance.context.state = AI::AIState::Idle;
  53. instance.worker = std::make_unique<AI::AIWorker>(m_reasoner, m_executor,
  54. m_behaviorRegistry);
  55. m_aiInstances.push_back(std::move(instance));
  56. }
  57. }
  58. AISystem::~AISystem() = default;
  59. void AISystem::set_ai_strategy(int player_id, AI::AIStrategy strategy,
  60. float aggression, float defense,
  61. float harassment) {
  62. for (auto &ai : m_aiInstances) {
  63. if (ai.context.player_id == player_id) {
  64. ai.context.strategy_config =
  65. AI::AIStrategyFactory::create_config(strategy);
  66. AI::AIStrategyFactory::apply_personality(ai.context.strategy_config,
  67. aggression, defense, harassment);
  68. break;
  69. }
  70. }
  71. }
  72. void AISystem::update(Engine::Core::World *world, float delta_time) {
  73. if (world == nullptr) {
  74. return;
  75. }
  76. m_total_game_time += delta_time;
  77. m_commandFilter.update(m_total_game_time);
  78. process_results(*world);
  79. for (auto &ai : m_aiInstances) {
  80. ai.update_timer += delta_time;
  81. if (ai.update_timer < m_update_interval) {
  82. continue;
  83. }
  84. if (ai.worker->busy()) {
  85. continue;
  86. }
  87. AI::AISnapshot snapshot = Game::Systems::AI::AISnapshotBuilder::build(
  88. *world, ai.context.player_id);
  89. snapshot.game_time = m_total_game_time;
  90. AI::AIJob job;
  91. job.snapshot = std::move(snapshot);
  92. job.context = ai.context;
  93. job.delta_time = ai.update_timer;
  94. if (ai.worker->try_submit(std::move(job))) {
  95. ai.update_timer = 0.0F;
  96. }
  97. }
  98. }
  99. void AISystem::process_results(Engine::Core::World &world) {
  100. for (auto &ai : m_aiInstances) {
  101. std::queue<AI::AIResult> results;
  102. ai.worker->drain_results(results);
  103. while (!results.empty()) {
  104. auto &result = results.front();
  105. ai.context = result.context;
  106. auto filtered_commands =
  107. m_commandFilter.filter(result.commands, m_total_game_time);
  108. Game::Systems::AI::AICommandApplier::apply(world, ai.context.player_id,
  109. filtered_commands);
  110. results.pop();
  111. }
  112. }
  113. }
  114. void AISystem::on_building_attacked(
  115. const Engine::Core::BuildingAttackedEvent &event) {
  116. for (auto &ai : m_aiInstances) {
  117. if (ai.context.player_id == event.owner_id) {
  118. ai.context.buildings_under_attack[event.buildingId] = m_total_game_time;
  119. if (event.buildingId == ai.context.primary_barracks) {
  120. ai.context.barracks_under_threat = true;
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. } // namespace Game::Systems