ai_snapshot_builder.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "ai_snapshot_builder.h"
  2. #include "../../core/component.h"
  3. #include "../../core/world.h"
  4. #include "systems/ai_system/ai_types.h"
  5. #include <utility>
  6. namespace Game::Systems::AI {
  7. AISnapshot AISnapshotBuilder::build(const Engine::Core::World &world,
  8. int aiOwnerId) {
  9. AISnapshot snapshot;
  10. snapshot.player_id = aiOwnerId;
  11. auto friendlies = world.getUnitsOwnedBy(aiOwnerId);
  12. snapshot.friendlies.reserve(friendlies.size());
  13. int skipped_no_ai = 0;
  14. int skipped_no_unit = 0;
  15. int skipped_dead = 0;
  16. int added = 0;
  17. for (auto *entity : friendlies) {
  18. if (!entity->hasComponent<Engine::Core::AIControlledComponent>()) {
  19. skipped_no_ai++;
  20. continue;
  21. }
  22. auto *unit = entity->getComponent<Engine::Core::UnitComponent>();
  23. if (unit == nullptr) {
  24. skipped_no_unit++;
  25. continue;
  26. }
  27. if (unit->health <= 0) {
  28. skipped_dead++;
  29. continue;
  30. }
  31. EntitySnapshot data;
  32. data.id = entity->getId();
  33. data.spawn_type = unit->spawn_type;
  34. data.owner_id = unit->owner_id;
  35. data.health = unit->health;
  36. data.max_health = unit->max_health;
  37. data.isBuilding = entity->hasComponent<Engine::Core::BuildingComponent>();
  38. if (auto *transform =
  39. entity->getComponent<Engine::Core::TransformComponent>()) {
  40. data.posX = transform->position.x;
  41. data.posY = 0.0F;
  42. data.posZ = transform->position.z;
  43. }
  44. if (auto *movement =
  45. entity->getComponent<Engine::Core::MovementComponent>()) {
  46. data.movement.hasComponent = true;
  47. data.movement.hasTarget = movement->hasTarget;
  48. }
  49. if (auto *production =
  50. entity->getComponent<Engine::Core::ProductionComponent>()) {
  51. data.production.hasComponent = true;
  52. data.production.inProgress = production->inProgress;
  53. data.production.buildTime = production->buildTime;
  54. data.production.timeRemaining = production->timeRemaining;
  55. data.production.producedCount = production->producedCount;
  56. data.production.maxUnits = production->maxUnits;
  57. data.production.product_type = production->product_type;
  58. data.production.rallySet = production->rallySet;
  59. data.production.rallyX = production->rallyX;
  60. data.production.rallyZ = production->rallyZ;
  61. data.production.queueSize =
  62. static_cast<int>(production->productionQueue.size());
  63. }
  64. snapshot.friendlies.push_back(std::move(data));
  65. added++;
  66. }
  67. auto enemies = world.getEnemyUnits(aiOwnerId);
  68. snapshot.visibleEnemies.reserve(enemies.size());
  69. for (auto *entity : enemies) {
  70. auto *unit = entity->getComponent<Engine::Core::UnitComponent>();
  71. if ((unit == nullptr) || unit->health <= 0) {
  72. continue;
  73. }
  74. auto *transform = entity->getComponent<Engine::Core::TransformComponent>();
  75. if (transform == nullptr) {
  76. continue;
  77. }
  78. ContactSnapshot contact;
  79. contact.id = entity->getId();
  80. contact.isBuilding =
  81. entity->hasComponent<Engine::Core::BuildingComponent>();
  82. contact.posX = transform->position.x;
  83. contact.posY = 0.0F;
  84. contact.posZ = transform->position.z;
  85. contact.health = unit->health;
  86. contact.max_health = unit->max_health;
  87. contact.spawn_type = unit->spawn_type;
  88. snapshot.visibleEnemies.push_back(std::move(contact));
  89. }
  90. return snapshot;
  91. }
  92. } // namespace Game::Systems::AI