ai_system.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "ai_system.h"
  2. #include "../core/world.h"
  3. #include "ai_system/behaviors/attack_behavior.h"
  4. #include "ai_system/behaviors/defend_behavior.h"
  5. #include "ai_system/behaviors/gather_behavior.h"
  6. #include "ai_system/behaviors/production_behavior.h"
  7. #include "ai_system/behaviors/retreat_behavior.h"
  8. #include "core/event_manager.h"
  9. #include "owner_registry.h"
  10. #include "systems/ai_system/ai_command_applier.h"
  11. #include "systems/ai_system/ai_snapshot_builder.h"
  12. #include "systems/ai_system/ai_types.h"
  13. #include "systems/ai_system/ai_worker.h"
  14. #include <cstdint>
  15. #include <memory>
  16. #include <queue>
  17. #include <utility>
  18. namespace Game::Systems {
  19. AISystem::AISystem() {
  20. m_behaviorRegistry.registerBehavior(std::make_unique<AI::RetreatBehavior>());
  21. m_behaviorRegistry.registerBehavior(std::make_unique<AI::DefendBehavior>());
  22. m_behaviorRegistry.registerBehavior(
  23. std::make_unique<AI::ProductionBehavior>());
  24. m_behaviorRegistry.registerBehavior(std::make_unique<AI::AttackBehavior>());
  25. m_behaviorRegistry.registerBehavior(std::make_unique<AI::GatherBehavior>());
  26. m_buildingAttackedSubscription = Engine::Core::ScopedEventSubscription<
  27. Engine::Core::BuildingAttackedEvent>(
  28. [this](const Engine::Core::BuildingAttackedEvent &event) {
  29. this->onBuildingAttacked(event);
  30. });
  31. initializeAIPlayers();
  32. }
  33. void AISystem::reinitialize() {
  34. m_aiInstances.clear();
  35. initializeAIPlayers();
  36. }
  37. void AISystem::initializeAIPlayers() {
  38. auto &registry = OwnerRegistry::instance();
  39. const auto &ai_owner_ids = registry.getAIOwnerIds();
  40. if (ai_owner_ids.empty()) {
  41. return;
  42. }
  43. for (uint32_t const player_id : ai_owner_ids) {
  44. int const team_id = registry.getOwnerTeam(player_id);
  45. AIInstance instance;
  46. instance.context.player_id = player_id;
  47. instance.context.state = AI::AIState::Idle;
  48. instance.worker = std::make_unique<AI::AIWorker>(m_reasoner, m_executor,
  49. m_behaviorRegistry);
  50. m_aiInstances.push_back(std::move(instance));
  51. }
  52. }
  53. AISystem::~AISystem() = default;
  54. void AISystem::update(Engine::Core::World *world, float deltaTime) {
  55. if (world == nullptr) {
  56. return;
  57. }
  58. m_totalGameTime += deltaTime;
  59. m_commandFilter.update(m_totalGameTime);
  60. processResults(*world);
  61. for (auto &ai : m_aiInstances) {
  62. ai.updateTimer += deltaTime;
  63. if (ai.updateTimer < 0.3F) {
  64. continue;
  65. }
  66. if (ai.worker->busy()) {
  67. continue;
  68. }
  69. AI::AISnapshot snapshot = Game::Systems::AI::AISnapshotBuilder::build(
  70. *world, ai.context.player_id);
  71. snapshot.gameTime = m_totalGameTime;
  72. AI::AIJob job;
  73. job.snapshot = std::move(snapshot);
  74. job.context = ai.context;
  75. job.deltaTime = ai.updateTimer;
  76. if (ai.worker->trySubmit(std::move(job))) {
  77. ai.updateTimer = 0.0F;
  78. }
  79. }
  80. }
  81. void AISystem::processResults(Engine::Core::World &world) {
  82. for (auto &ai : m_aiInstances) {
  83. std::queue<AI::AIResult> results;
  84. ai.worker->drainResults(results);
  85. while (!results.empty()) {
  86. auto &result = results.front();
  87. ai.context = result.context;
  88. auto filtered_commands =
  89. m_commandFilter.filter(result.commands, m_totalGameTime);
  90. Game::Systems::AI::AICommandApplier::apply(world, ai.context.player_id,
  91. filtered_commands);
  92. results.pop();
  93. }
  94. }
  95. }
  96. void AISystem::onBuildingAttacked(
  97. const Engine::Core::BuildingAttackedEvent &event) {
  98. for (auto &ai : m_aiInstances) {
  99. if (ai.context.player_id == event.owner_id) {
  100. ai.context.buildingsUnderAttack[event.buildingId] = m_totalGameTime;
  101. if (event.buildingId == ai.context.primaryBarracks) {
  102. ai.context.barracksUnderThreat = true;
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. } // namespace Game::Systems