ai_system.cpp 3.6 KB

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