ai_snapshot_builder.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 ai_owner_id) -> AISnapshot {
  9. AISnapshot snapshot;
  10. snapshot.player_id = ai_owner_id;
  11. auto friendlies = world.get_units_owned_by(ai_owner_id);
  12. snapshot.friendly_units.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.is_building = entity->has_component<Engine::Core::BuildingComponent>();
  38. if (auto *transform =
  39. entity->get_component<Engine::Core::TransformComponent>()) {
  40. data.pos_x = transform->position.x;
  41. data.pos_y = 0.0F;
  42. data.pos_z = 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. if (auto *builder_prod =
  65. entity->get_component<Engine::Core::BuilderProductionComponent>()) {
  66. data.builder_production.has_component = true;
  67. data.builder_production.has_construction_site =
  68. builder_prod->has_construction_site;
  69. data.builder_production.in_progress = builder_prod->in_progress;
  70. data.builder_production.at_construction_site =
  71. builder_prod->at_construction_site;
  72. }
  73. snapshot.friendly_units.push_back(std::move(data));
  74. added++;
  75. }
  76. auto enemies = world.get_enemy_units(ai_owner_id);
  77. snapshot.visible_enemies.reserve(enemies.size());
  78. for (auto *entity : enemies) {
  79. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  80. if ((unit == nullptr) || unit->health <= 0) {
  81. continue;
  82. }
  83. auto *transform = entity->get_component<Engine::Core::TransformComponent>();
  84. if (transform == nullptr) {
  85. continue;
  86. }
  87. ContactSnapshot contact;
  88. contact.id = entity->get_id();
  89. contact.owner_id = unit->owner_id;
  90. contact.is_building =
  91. entity->has_component<Engine::Core::BuildingComponent>();
  92. contact.pos_x = transform->position.x;
  93. contact.pos_y = 0.0F;
  94. contact.pos_z = transform->position.z;
  95. contact.health = unit->health;
  96. contact.max_health = unit->max_health;
  97. contact.spawn_type = unit->spawn_type;
  98. snapshot.visible_enemies.push_back(std::move(contact));
  99. }
  100. return snapshot;
  101. }
  102. } // namespace Game::Systems::AI