capture_system.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 deltaTime) {
  19. processBarrackCapture(world, deltaTime);
  20. }
  21. auto CaptureSystem::countNearbyTroops(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->getEntitiesWith<Engine::Core::UnitComponent>();
  26. for (auto *e : entities) {
  27. auto *unit = e->getComponent<Engine::Core::UnitComponent>();
  28. auto *transform = e->getComponent<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 individuals_per_unit =
  43. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  44. unit->spawn_type);
  45. total_troops += individuals_per_unit;
  46. }
  47. }
  48. return total_troops;
  49. }
  50. void CaptureSystem::transferBarrackOwnership(Engine::Core::World *,
  51. Engine::Core::Entity *barrack,
  52. int newOwnerId) {
  53. auto *unit = barrack->getComponent<Engine::Core::UnitComponent>();
  54. auto *renderable = barrack->getComponent<Engine::Core::RenderableComponent>();
  55. auto *transform = barrack->getComponent<Engine::Core::TransformComponent>();
  56. auto *prod = barrack->getComponent<Engine::Core::ProductionComponent>();
  57. if ((unit == nullptr) || (renderable == nullptr) || (transform == nullptr)) {
  58. return;
  59. }
  60. int const previous_owner_id = unit->owner_id;
  61. unit->owner_id = newOwnerId;
  62. auto &nation_registry = NationRegistry::instance();
  63. if (!Game::Core::isNeutralOwner(newOwnerId)) {
  64. if (const auto *nation = nation_registry.getNationForPlayer(newOwnerId)) {
  65. unit->nation_id = nation->id;
  66. } else {
  67. unit->nation_id = nation_registry.default_nation_id();
  68. }
  69. } else {
  70. unit->nation_id.clear();
  71. }
  72. QVector3D const tc = Game::Visuals::team_colorForOwner(newOwnerId);
  73. renderable->color[0] = tc.x();
  74. renderable->color[1] = tc.y();
  75. renderable->color[2] = tc.z();
  76. Game::Systems::BuildingCollisionRegistry::instance().updateBuildingOwner(
  77. barrack->getId(), newOwnerId);
  78. if (!Game::Core::isNeutralOwner(newOwnerId) && (prod == nullptr)) {
  79. prod = barrack->addComponent<Engine::Core::ProductionComponent>();
  80. if (prod != nullptr) {
  81. prod->product_type = Game::Units::TroopType::Archer;
  82. prod->maxUnits = 150;
  83. prod->inProgress = false;
  84. prod->timeRemaining = 0.0F;
  85. prod->producedCount = 0;
  86. prod->rallyX = transform->position.x + 4.0F;
  87. prod->rallyZ = transform->position.z + 2.0F;
  88. prod->rallySet = true;
  89. const auto profile = TroopProfileService::instance().get_profile(
  90. unit->nation_id, prod->product_type);
  91. prod->buildTime = profile.production.build_time;
  92. prod->villagerCost = profile.individuals_per_unit;
  93. }
  94. } else if (Game::Core::isNeutralOwner(newOwnerId) && (prod != nullptr)) {
  95. barrack->removeComponent<Engine::Core::ProductionComponent>();
  96. } else if (prod != nullptr) {
  97. const auto profile = TroopProfileService::instance().get_profile(
  98. unit->nation_id, prod->product_type);
  99. prod->buildTime = profile.production.build_time;
  100. prod->villagerCost = profile.individuals_per_unit;
  101. }
  102. Engine::Core::EventManager::instance().publish(
  103. Engine::Core::BarrackCapturedEvent(barrack->getId(), previous_owner_id,
  104. newOwnerId));
  105. }
  106. void CaptureSystem::processBarrackCapture(Engine::Core::World *world,
  107. float deltaTime) {
  108. constexpr float capture_radius = 8.0F;
  109. constexpr int troop_advantage_multiplier = 3;
  110. auto barracks = world->getEntitiesWith<Engine::Core::BuildingComponent>();
  111. for (auto *barrack : barracks) {
  112. auto *unit = barrack->getComponent<Engine::Core::UnitComponent>();
  113. auto *transform = barrack->getComponent<Engine::Core::TransformComponent>();
  114. if ((unit == nullptr) || (transform == nullptr)) {
  115. continue;
  116. }
  117. if (unit->spawn_type != Game::Units::SpawnType::Barracks) {
  118. continue;
  119. }
  120. auto *capture = barrack->getComponent<Engine::Core::CaptureComponent>();
  121. if (capture == nullptr) {
  122. capture = barrack->addComponent<Engine::Core::CaptureComponent>();
  123. }
  124. float const barrack_x = transform->position.x;
  125. float const barrack_z = transform->position.z;
  126. int const barrack_owner_id = unit->owner_id;
  127. int max_enemy_troops = 0;
  128. int capturing_player_id = -1;
  129. auto entities = world->getEntitiesWith<Engine::Core::UnitComponent>();
  130. std::vector<int> player_ids;
  131. for (auto *e : entities) {
  132. auto *u = e->getComponent<Engine::Core::UnitComponent>();
  133. if ((u != nullptr) && u->owner_id != barrack_owner_id &&
  134. !Game::Core::isNeutralOwner(u->owner_id)) {
  135. if (std::find(player_ids.begin(), player_ids.end(), u->owner_id) ==
  136. player_ids.end()) {
  137. player_ids.push_back(u->owner_id);
  138. }
  139. }
  140. }
  141. for (int const player_id : player_ids) {
  142. int const troop_count = countNearbyTroops(world, barrack_x, barrack_z,
  143. player_id, capture_radius);
  144. if (troop_count > max_enemy_troops) {
  145. max_enemy_troops = troop_count;
  146. capturing_player_id = player_id;
  147. }
  148. }
  149. int defender_troops = 0;
  150. if (!Game::Core::isNeutralOwner(barrack_owner_id)) {
  151. defender_troops = countNearbyTroops(world, barrack_x, barrack_z,
  152. barrack_owner_id, capture_radius);
  153. }
  154. bool const can_capture =
  155. max_enemy_troops >= (defender_troops * troop_advantage_multiplier);
  156. if (can_capture && capturing_player_id != -1) {
  157. if (capture->capturing_player_id != capturing_player_id) {
  158. capture->capturing_player_id = capturing_player_id;
  159. capture->captureProgress = 0.0F;
  160. }
  161. capture->isBeingCaptured = true;
  162. capture->captureProgress += deltaTime;
  163. if (capture->captureProgress >= capture->requiredTime) {
  164. transferBarrackOwnership(world, barrack, capturing_player_id);
  165. capture->captureProgress = 0.0F;
  166. capture->isBeingCaptured = false;
  167. capture->capturing_player_id = -1;
  168. }
  169. } else {
  170. if (capture->isBeingCaptured) {
  171. capture->captureProgress -= deltaTime * 2.0F;
  172. if (capture->captureProgress <= 0.0F) {
  173. capture->captureProgress = 0.0F;
  174. capture->isBeingCaptured = false;
  175. capture->capturing_player_id = -1;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. } // namespace Game::Systems