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. auto AISnapshotBuilder::build(const Engine::Core::World &world,
  8. int aiOwnerId) -> AISnapshot {
  9. AISnapshot snapshot;
  10. snapshot.player_id = aiOwnerId;
  11. auto friendlies = world.get_units_owned_by(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->has_component<Engine::Core::AIControlledComponent>()) {
  19. skipped_no_ai++;
  20. continue;
  21. }
  22. auto *unit = entity->get_component<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->get_id();
  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->has_component<Engine::Core::BuildingComponent>();
  38. if (auto *transform =
  39. entity->get_component<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->get_component<Engine::Core::MovementComponent>()) {
  46. data.movement.has_component = true;
  47. data.movement.has_target = movement->has_target;
  48. }
  49. if (auto *production =
  50. entity->get_component<Engine::Core::ProductionComponent>()) {
  51. data.production.has_component = true;
  52. data.production.in_progress = production->in_progress;
  53. data.production.build_time = production->build_time;
  54. data.production.time_remaining = production->time_remaining;
  55. data.production.produced_count = production->produced_count;
  56. data.production.max_units = production->max_units;
  57. data.production.product_type = production->product_type;
  58. data.production.rally_set = production->rally_set;
  59. data.production.rally_x = production->rally_x;
  60. data.production.rally_z = production->rally_z;
  61. data.production.queue_size =
  62. static_cast<int>(production->production_queue.size());
  63. }
  64. snapshot.friendlies.push_back(std::move(data));
  65. added++;
  66. }
  67. auto enemies = world.get_enemy_units(aiOwnerId);
  68. snapshot.visibleEnemies.reserve(enemies.size());
  69. for (auto *entity : enemies) {
  70. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  71. if ((unit == nullptr) || unit->health <= 0) {
  72. continue;
  73. }
  74. auto *transform = entity->get_component<Engine::Core::TransformComponent>();
  75. if (transform == nullptr) {
  76. continue;
  77. }
  78. ContactSnapshot contact;
  79. contact.id = entity->get_id();
  80. contact.isBuilding =
  81. entity->has_component<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