capture_system.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "capture_system.h"
  2. #include "../core/component.h"
  3. #include "../core/event_manager.h"
  4. #include "../core/ownership_constants.h"
  5. #include "../core/world.h"
  6. #include "../systems/nation_registry.h"
  7. #include "../systems/troop_profile_service.h"
  8. #include "../units/troop_config.h"
  9. #include "../visuals/team_colors.h"
  10. #include "building_collision_registry.h"
  11. #include "units/spawn_type.h"
  12. #include "units/troop_type.h"
  13. #include <algorithm>
  14. #include <cmath>
  15. #include <qvectornd.h>
  16. #include <vector>
  17. namespace Game::Systems {
  18. void CaptureSystem::update(Engine::Core::World *world, float delta_time) {
  19. process_barrack_capture(world, delta_time);
  20. }
  21. auto CaptureSystem::count_nearby_troops(Engine::Core::World *world,
  22. float barrack_x, float barrack_z,
  23. int owner_id, float radius) -> int {
  24. int total_troops = 0;
  25. auto entities = world->get_entities_with<Engine::Core::UnitComponent>();
  26. for (auto *e : entities) {
  27. auto *unit = e->get_component<Engine::Core::UnitComponent>();
  28. auto *transform = e->get_component<Engine::Core::TransformComponent>();
  29. if ((unit == nullptr) || (transform == nullptr) || unit->health <= 0) {
  30. continue;
  31. }
  32. if (unit->owner_id != owner_id) {
  33. continue;
  34. }
  35. if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
  36. continue;
  37. }
  38. float const dx = transform->position.x - barrack_x;
  39. float const dz = transform->position.z - barrack_z;
  40. float const dist_sq = dx * dx + dz * dz;
  41. if (dist_sq <= radius * radius) {
  42. int const production_cost =
  43. Game::Units::TroopConfig::instance().getProductionCost(
  44. unit->spawn_type);
  45. total_troops += production_cost;
  46. }
  47. }
  48. return total_troops;
  49. }
  50. void CaptureSystem::transfer_barrack_ownership(Engine::Core::World *,
  51. Engine::Core::Entity *barrack,
  52. int new_owner_id) {
  53. auto *unit = barrack->get_component<Engine::Core::UnitComponent>();
  54. auto *renderable =
  55. barrack->get_component<Engine::Core::RenderableComponent>();
  56. auto *transform = barrack->get_component<Engine::Core::TransformComponent>();
  57. auto *prod = barrack->get_component<Engine::Core::ProductionComponent>();
  58. if ((unit == nullptr) || (renderable == nullptr) || (transform == nullptr)) {
  59. return;
  60. }
  61. int const previous_owner_id = unit->owner_id;
  62. unit->owner_id = new_owner_id;
  63. QVector3D const tc = Game::Visuals::team_colorForOwner(new_owner_id);
  64. renderable->color[0] = tc.x();
  65. renderable->color[1] = tc.y();
  66. renderable->color[2] = tc.z();
  67. Game::Systems::BuildingCollisionRegistry::instance().update_building_owner(
  68. barrack->get_id(), new_owner_id);
  69. if (!Game::Core::isNeutralOwner(new_owner_id) && (prod == nullptr)) {
  70. prod = barrack->add_component<Engine::Core::ProductionComponent>();
  71. if (prod != nullptr) {
  72. prod->product_type = Game::Units::TroopType::Archer;
  73. prod->max_units = 150;
  74. prod->in_progress = false;
  75. prod->time_remaining = 0.0F;
  76. prod->produced_count = 0;
  77. prod->rally_x = transform->position.x + 4.0F;
  78. prod->rally_z = transform->position.z + 2.0F;
  79. prod->rally_set = true;
  80. const auto profile = TroopProfileService::instance().get_profile(
  81. unit->nation_id, prod->product_type);
  82. prod->build_time = profile.production.build_time;
  83. prod->villager_cost = profile.production.cost;
  84. }
  85. } else if (Game::Core::isNeutralOwner(new_owner_id) && (prod != nullptr)) {
  86. barrack->remove_component<Engine::Core::ProductionComponent>();
  87. } else if (prod != nullptr) {
  88. prod->produced_count = 0;
  89. const auto profile = TroopProfileService::instance().get_profile(
  90. unit->nation_id, prod->product_type);
  91. prod->build_time = profile.production.build_time;
  92. prod->villager_cost = profile.production.cost;
  93. }
  94. Engine::Core::EventManager::instance().publish(
  95. Engine::Core::BarrackCapturedEvent(barrack->get_id(), previous_owner_id,
  96. new_owner_id));
  97. }
  98. void CaptureSystem::process_barrack_capture(Engine::Core::World *world,
  99. float delta_time) {
  100. constexpr float capture_radius = 8.0F;
  101. constexpr int troop_advantage_multiplier = 3;
  102. auto barracks = world->get_entities_with<Engine::Core::BuildingComponent>();
  103. for (auto *barrack : barracks) {
  104. auto *unit = barrack->get_component<Engine::Core::UnitComponent>();
  105. auto *transform =
  106. barrack->get_component<Engine::Core::TransformComponent>();
  107. if ((unit == nullptr) || (transform == nullptr)) {
  108. continue;
  109. }
  110. if (unit->spawn_type != Game::Units::SpawnType::Barracks) {
  111. continue;
  112. }
  113. auto *capture = barrack->get_component<Engine::Core::CaptureComponent>();
  114. if (capture == nullptr) {
  115. capture = barrack->add_component<Engine::Core::CaptureComponent>();
  116. }
  117. float const barrack_x = transform->position.x;
  118. float const barrack_z = transform->position.z;
  119. int const barrack_owner_id = unit->owner_id;
  120. int max_enemy_troops = 0;
  121. int capturing_player_id = -1;
  122. auto entities = world->get_entities_with<Engine::Core::UnitComponent>();
  123. std::vector<int> player_ids;
  124. for (auto *e : entities) {
  125. auto *u = e->get_component<Engine::Core::UnitComponent>();
  126. if ((u != nullptr) && u->owner_id != barrack_owner_id &&
  127. !Game::Core::isNeutralOwner(u->owner_id)) {
  128. if (std::find(player_ids.begin(), player_ids.end(), u->owner_id) ==
  129. player_ids.end()) {
  130. player_ids.push_back(u->owner_id);
  131. }
  132. }
  133. }
  134. for (int const player_id : player_ids) {
  135. int const troop_count = count_nearby_troops(world, barrack_x, barrack_z,
  136. player_id, capture_radius);
  137. if (troop_count > max_enemy_troops) {
  138. max_enemy_troops = troop_count;
  139. capturing_player_id = player_id;
  140. }
  141. }
  142. int defender_troops = 0;
  143. if (!Game::Core::isNeutralOwner(barrack_owner_id)) {
  144. defender_troops = count_nearby_troops(world, barrack_x, barrack_z,
  145. barrack_owner_id, capture_radius);
  146. }
  147. bool const can_capture =
  148. max_enemy_troops >= (defender_troops * troop_advantage_multiplier);
  149. if (can_capture && capturing_player_id != -1) {
  150. if (capture->capturing_player_id != capturing_player_id) {
  151. capture->capturing_player_id = capturing_player_id;
  152. capture->capture_progress = 0.0F;
  153. }
  154. capture->is_being_captured = true;
  155. capture->capture_progress += delta_time;
  156. if (capture->capture_progress >= capture->required_time) {
  157. transfer_barrack_ownership(world, barrack, capturing_player_id);
  158. capture->capture_progress = 0.0F;
  159. capture->is_being_captured = false;
  160. capture->capturing_player_id = -1;
  161. }
  162. } else {
  163. if (capture->is_being_captured) {
  164. capture->capture_progress -= delta_time * 2.0F;
  165. if (capture->capture_progress <= 0.0F) {
  166. capture->capture_progress = 0.0F;
  167. capture->is_being_captured = false;
  168. capture->capturing_player_id = -1;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. } // namespace Game::Systems