attack_processor.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #include "attack_processor.h"
  2. #include "../../core/component.h"
  3. #include "../../core/world.h"
  4. #include "../../units/spawn_type.h"
  5. #include "../../units/troop_config.h"
  6. #include "../../visuals/team_colors.h"
  7. #include "../arrow_system.h"
  8. #include "../command_service.h"
  9. #include "../owner_registry.h"
  10. #include "../pathfinding.h"
  11. #include "../troop_profile_service.h"
  12. #include "combat_mode_processor.h"
  13. #include "combat_types.h"
  14. #include "combat_utils.h"
  15. #include "damage_processor.h"
  16. #include <algorithm>
  17. #include <cmath>
  18. #include <numbers>
  19. #include <optional>
  20. #include <qvectornd.h>
  21. #include <random>
  22. namespace Game::Systems::Combat {
  23. namespace {
  24. thread_local std::mt19937 gen(std::random_device{}());
  25. void stop_unit_movement(Engine::Core::Entity *unit,
  26. Engine::Core::TransformComponent *transform) {
  27. auto *movement = unit->get_component<Engine::Core::MovementComponent>();
  28. if ((movement != nullptr) && movement->has_target) {
  29. movement->has_target = false;
  30. movement->vx = 0.0F;
  31. movement->vz = 0.0F;
  32. movement->clear_path();
  33. if (transform != nullptr) {
  34. movement->target_x = transform->position.x;
  35. movement->target_y = transform->position.z;
  36. movement->goal_x = transform->position.x;
  37. movement->goal_y = transform->position.z;
  38. }
  39. }
  40. }
  41. void face_target(Engine::Core::TransformComponent *attacker_transform,
  42. Engine::Core::TransformComponent *target_transform) {
  43. if ((attacker_transform == nullptr) || (target_transform == nullptr)) {
  44. return;
  45. }
  46. float const dx =
  47. target_transform->position.x - attacker_transform->position.x;
  48. float const dz =
  49. target_transform->position.z - attacker_transform->position.z;
  50. float const yaw = std::atan2(dx, dz) * 180.0F / std::numbers::pi_v<float>;
  51. attacker_transform->desired_yaw = yaw;
  52. attacker_transform->has_desired_yaw = true;
  53. }
  54. auto is_ranged_mode(Engine::Core::AttackComponent *attack_comp) -> bool {
  55. return (attack_comp != nullptr) && attack_comp->can_ranged &&
  56. attack_comp->current_mode ==
  57. Engine::Core::AttackComponent::CombatMode::Ranged;
  58. }
  59. auto get_base_max_health(const Engine::Core::UnitComponent *unit)
  60. -> std::optional<int> {
  61. if (unit == nullptr) {
  62. return std::nullopt;
  63. }
  64. auto troop_type_opt = Game::Units::spawn_typeToTroopType(unit->spawn_type);
  65. if (!troop_type_opt) {
  66. return std::nullopt;
  67. }
  68. auto profile = Game::Systems::TroopProfileService::instance().get_profile(
  69. unit->nation_id, *troop_type_opt);
  70. return profile.combat.max_health;
  71. }
  72. auto is_high_ground_advantage(
  73. const Engine::Core::TransformComponent *high_transform,
  74. const Engine::Core::TransformComponent *low_transform) -> bool {
  75. if ((high_transform == nullptr) || (low_transform == nullptr)) {
  76. return false;
  77. }
  78. float const height_diff =
  79. high_transform->position.y - low_transform->position.y;
  80. return height_diff > Constants::kHighGroundHeightThreshold;
  81. }
  82. void process_melee_lock(Engine::Core::Entity *attacker,
  83. Engine::Core::AttackComponent *attack_comp,
  84. Engine::Core::World *world, float delta_time) {
  85. if (attack_comp == nullptr || !attack_comp->in_melee_lock) {
  86. return;
  87. }
  88. auto *lock_target = world->get_entity(attack_comp->melee_lock_target_id);
  89. if ((lock_target == nullptr) ||
  90. lock_target->has_component<Engine::Core::PendingRemovalComponent>()) {
  91. attack_comp->in_melee_lock = false;
  92. attack_comp->melee_lock_target_id = 0;
  93. return;
  94. }
  95. auto *lock_target_unit =
  96. lock_target->get_component<Engine::Core::UnitComponent>();
  97. if ((lock_target_unit == nullptr) || lock_target_unit->health <= 0) {
  98. attack_comp->in_melee_lock = false;
  99. attack_comp->melee_lock_target_id = 0;
  100. return;
  101. }
  102. auto *att_t = attacker->get_component<Engine::Core::TransformComponent>();
  103. auto *tgt_t = lock_target->get_component<Engine::Core::TransformComponent>();
  104. if ((att_t == nullptr) || (tgt_t == nullptr)) {
  105. return;
  106. }
  107. face_target(att_t, tgt_t);
  108. face_target(tgt_t, att_t);
  109. float const dx = tgt_t->position.x - att_t->position.x;
  110. float const dz = tgt_t->position.z - att_t->position.z;
  111. float const dist = std::sqrt(dx * dx + dz * dz);
  112. if (dist > Constants::kMaxMeleeSeparation) {
  113. if (!is_unit_in_hold_mode(attacker) && !is_building(attacker)) {
  114. float const pull_amount = (dist - Constants::kIdealMeleeDistance) *
  115. Constants::kMeleePullFactor * delta_time *
  116. Constants::kMeleePullSpeed;
  117. if (dist > Constants::kMinDistance) {
  118. QVector3D const direction(dx / dist, 0.0F, dz / dist);
  119. float const new_x = att_t->position.x + direction.x() * pull_amount;
  120. float const new_z = att_t->position.z + direction.z() * pull_amount;
  121. auto *pathfinder = CommandService::get_pathfinder();
  122. if (pathfinder != nullptr) {
  123. Point const new_grid = CommandService::world_to_grid(new_x, new_z);
  124. if (pathfinder->is_walkable(new_grid.x, new_grid.y)) {
  125. att_t->position.x = new_x;
  126. att_t->position.z = new_z;
  127. } else {
  128. attack_comp->in_melee_lock = false;
  129. attack_comp->melee_lock_target_id = 0;
  130. }
  131. } else {
  132. att_t->position.x = new_x;
  133. att_t->position.z = new_z;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. void sync_melee_lock_target(Engine::Core::Entity *attacker,
  140. Engine::Core::AttackComponent *attack_comp) {
  141. if (attack_comp == nullptr || !attack_comp->in_melee_lock ||
  142. attack_comp->melee_lock_target_id == 0) {
  143. return;
  144. }
  145. auto *attack_target =
  146. attacker->get_component<Engine::Core::AttackTargetComponent>();
  147. if (attack_target == nullptr) {
  148. attack_target =
  149. attacker->add_component<Engine::Core::AttackTargetComponent>();
  150. }
  151. if (attack_target != nullptr) {
  152. attack_target->target_id = attack_comp->melee_lock_target_id;
  153. attack_target->should_chase = false;
  154. }
  155. }
  156. void apply_health_bonus(Engine::Core::UnitComponent *unit_comp) {
  157. auto base_max_health_opt = get_base_max_health(unit_comp);
  158. int const base_max_health =
  159. base_max_health_opt.value_or(std::max(1, unit_comp->max_health));
  160. int const max_health_bonus = static_cast<int>(
  161. static_cast<float>(base_max_health) * Constants::kHealthMultiplierHold);
  162. if (unit_comp->max_health < max_health_bonus) {
  163. int const safe_max_health = std::max(1, unit_comp->max_health);
  164. int const health_percentage = (unit_comp->health * 100) / safe_max_health;
  165. unit_comp->max_health = max_health_bonus;
  166. unit_comp->health = (max_health_bonus * health_percentage) / 100;
  167. }
  168. }
  169. void apply_hold_mode_bonuses(Engine::Core::Entity *attacker,
  170. Engine::Core::UnitComponent *unit_comp,
  171. float &range, int &damage) {
  172. auto *hold_mode = attacker->get_component<Engine::Core::HoldModeComponent>();
  173. if ((hold_mode == nullptr) || !hold_mode->active) {
  174. return;
  175. }
  176. if (unit_comp->spawn_type == Game::Units::SpawnType::Archer) {
  177. range *= Constants::kRangeMultiplierHold;
  178. damage = static_cast<int>(static_cast<float>(damage) *
  179. Constants::kDamageMultiplierArcherHold);
  180. apply_health_bonus(unit_comp);
  181. } else if (unit_comp->spawn_type == Game::Units::SpawnType::Spearman) {
  182. range *= Constants::kRangeMultiplierSpearmanHold;
  183. damage = static_cast<int>(static_cast<float>(damage) *
  184. Constants::kDamageMultiplierSpearmanHold);
  185. apply_health_bonus(unit_comp);
  186. } else {
  187. damage = static_cast<int>(static_cast<float>(damage) *
  188. Constants::kDamageMultiplierDefaultHold);
  189. }
  190. }
  191. void apply_high_ground_defense_bonuses(Engine::Core::Entity *attacker,
  192. Engine::Core::Entity *target,
  193. Engine::Core::UnitComponent *target_unit,
  194. int &damage) {
  195. if (target_unit == nullptr) {
  196. return;
  197. }
  198. if (target_unit->spawn_type != Game::Units::SpawnType::Archer &&
  199. target_unit->spawn_type != Game::Units::SpawnType::Spearman) {
  200. return;
  201. }
  202. auto *attacker_transform =
  203. attacker->get_component<Engine::Core::TransformComponent>();
  204. auto *target_transform =
  205. target->get_component<Engine::Core::TransformComponent>();
  206. if (!is_high_ground_advantage(target_transform, attacker_transform)) {
  207. return;
  208. }
  209. damage = std::max(1, static_cast<int>(static_cast<float>(damage) *
  210. Constants::kHighGroundArmorMultiplier));
  211. auto base_max_health_opt = get_base_max_health(target_unit);
  212. if (!base_max_health_opt || *base_max_health_opt <= 0) {
  213. return;
  214. }
  215. int const max_health_bonus =
  216. static_cast<int>(static_cast<float>(*base_max_health_opt) *
  217. Constants::kHighGroundHealthMultiplier);
  218. if (target_unit->max_health < max_health_bonus) {
  219. int const safe_max_health = std::max(1, target_unit->max_health);
  220. int const health_percentage = (target_unit->health * 100) / safe_max_health;
  221. target_unit->max_health = max_health_bonus;
  222. target_unit->health = (max_health_bonus * health_percentage) / 100;
  223. }
  224. }
  225. auto calculate_tactical_damage_multiplier(
  226. Engine::Core::Entity *attacker, Engine::Core::Entity *target,
  227. Engine::Core::UnitComponent *attacker_unit,
  228. Engine::Core::UnitComponent *target_unit) -> float {
  229. float multiplier = 1.0F;
  230. if (attacker_unit->spawn_type == Game::Units::SpawnType::Spearman) {
  231. if (target_unit->spawn_type == Game::Units::SpawnType::HorseArcher ||
  232. target_unit->spawn_type == Game::Units::SpawnType::HorseSpearman ||
  233. target_unit->spawn_type == Game::Units::SpawnType::MountedKnight) {
  234. multiplier *= Constants::kSpearmanVsCavalryMultiplier;
  235. }
  236. }
  237. if (attacker_unit->spawn_type == Game::Units::SpawnType::Archer ||
  238. attacker_unit->spawn_type == Game::Units::SpawnType::HorseArcher) {
  239. if (target->has_component<Engine::Core::ElephantComponent>()) {
  240. multiplier *= Constants::kArcherVsElephantMultiplier;
  241. }
  242. auto *attacker_transform =
  243. attacker->get_component<Engine::Core::TransformComponent>();
  244. auto *target_transform =
  245. target->get_component<Engine::Core::TransformComponent>();
  246. if (is_high_ground_advantage(attacker_transform, target_transform)) {
  247. multiplier *= Constants::kArcherHighGroundMultiplier;
  248. }
  249. } else if (attacker_unit->spawn_type == Game::Units::SpawnType::Spearman) {
  250. auto *attacker_transform =
  251. attacker->get_component<Engine::Core::TransformComponent>();
  252. auto *target_transform =
  253. target->get_component<Engine::Core::TransformComponent>();
  254. if (is_high_ground_advantage(attacker_transform, target_transform)) {
  255. multiplier *= Constants::kSpearmanHighGroundMultiplier;
  256. }
  257. }
  258. return multiplier;
  259. }
  260. void spawn_arrows(Engine::Core::Entity *attacker, Engine::Core::Entity *target,
  261. ArrowSystem *arrow_sys) {
  262. if (arrow_sys == nullptr) {
  263. return;
  264. }
  265. auto *att_t = attacker->get_component<Engine::Core::TransformComponent>();
  266. auto *tgt_t = target->get_component<Engine::Core::TransformComponent>();
  267. auto *att_u = attacker->get_component<Engine::Core::UnitComponent>();
  268. if ((att_t == nullptr) || (tgt_t == nullptr)) {
  269. return;
  270. }
  271. QVector3D const a_pos(att_t->position.x, att_t->position.y,
  272. att_t->position.z);
  273. QVector3D const t_pos(tgt_t->position.x, tgt_t->position.y,
  274. tgt_t->position.z);
  275. QVector3D const dir = (t_pos - a_pos).normalized();
  276. QVector3D const color =
  277. (att_u != nullptr) ? Game::Visuals::team_colorForOwner(att_u->owner_id)
  278. : QVector3D(0.8F, 0.9F, 1.0F);
  279. int arrow_count = 1;
  280. if (att_u != nullptr) {
  281. int const troop_size =
  282. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  283. att_u->spawn_type);
  284. int const max_arrows = std::max(2, (troop_size * 2) / 3);
  285. static thread_local std::mt19937 arrow_gen(std::random_device{}());
  286. std::uniform_int_distribution<> dist(max_arrows / 2, max_arrows);
  287. arrow_count = dist(arrow_gen);
  288. }
  289. for (int i = 0; i < arrow_count; ++i) {
  290. static thread_local std::mt19937 spread_gen(std::random_device{}());
  291. std::uniform_real_distribution<float> spread_dist(
  292. Constants::kArrowSpreadMin, Constants::kArrowSpreadMax);
  293. QVector3D const perpendicular(-dir.z(), 0.0F, dir.x());
  294. QVector3D const up_vector(0.0F, 1.0F, 0.0F);
  295. float const lateral_offset = spread_dist(spread_gen);
  296. float const vertical_offset =
  297. spread_dist(spread_gen) * Constants::kArrowVerticalSpreadFactor;
  298. float const depth_offset =
  299. spread_dist(spread_gen) * Constants::kArrowDepthSpreadFactor;
  300. QVector3D const start_offset =
  301. perpendicular * lateral_offset + up_vector * vertical_offset;
  302. QVector3D const end_offset = perpendicular * lateral_offset +
  303. up_vector * vertical_offset +
  304. dir * depth_offset;
  305. QVector3D const start =
  306. a_pos + QVector3D(0.0F, Constants::kArrowStartHeight, 0.0F) +
  307. dir * Constants::kArrowStartOffset + start_offset;
  308. QVector3D const end = t_pos +
  309. QVector3D(Constants::kArrowTargetOffset,
  310. Constants::kArrowTargetOffset, 0.0F) +
  311. end_offset;
  312. arrow_sys->spawn_arrow(start, end, color, Constants::kArrowSpeed);
  313. }
  314. }
  315. void initiate_melee_combat(Engine::Core::Entity *attacker,
  316. Engine::Core::Entity *target,
  317. Engine::Core::AttackComponent *attack_comp,
  318. Engine::Core::World *world) {
  319. attack_comp->in_melee_lock = true;
  320. attack_comp->melee_lock_target_id = target->get_id();
  321. auto *combat_state =
  322. attacker->get_component<Engine::Core::CombatStateComponent>();
  323. if (combat_state == nullptr) {
  324. combat_state =
  325. attacker->add_component<Engine::Core::CombatStateComponent>();
  326. }
  327. if (combat_state != nullptr && combat_state->animation_state ==
  328. Engine::Core::CombatAnimationState::Idle) {
  329. combat_state->animation_state = Engine::Core::CombatAnimationState::Advance;
  330. combat_state->state_time = 0.0F;
  331. combat_state->state_duration =
  332. Engine::Core::CombatStateComponent::kAdvanceDuration;
  333. std::uniform_real_distribution<float> offset_dist(0.0F, 0.15F);
  334. combat_state->attack_offset = offset_dist(gen);
  335. std::uniform_int_distribution<int> variant_dist(
  336. 0, Engine::Core::CombatStateComponent::kMaxAttackVariants - 1);
  337. combat_state->attack_variant = static_cast<std::uint8_t>(variant_dist(gen));
  338. }
  339. auto *target_atk = target->get_component<Engine::Core::AttackComponent>();
  340. if (target_atk != nullptr) {
  341. target_atk->in_melee_lock = true;
  342. target_atk->melee_lock_target_id = attacker->get_id();
  343. }
  344. auto *att_t = attacker->get_component<Engine::Core::TransformComponent>();
  345. auto *tgt_t = target->get_component<Engine::Core::TransformComponent>();
  346. if ((att_t != nullptr) && (tgt_t != nullptr)) {
  347. face_target(att_t, tgt_t);
  348. face_target(tgt_t, att_t);
  349. float const dx = tgt_t->position.x - att_t->position.x;
  350. float const dz = tgt_t->position.z - att_t->position.z;
  351. float const dist = std::sqrt(dx * dx + dz * dz);
  352. if (dist > Constants::kIdealMeleeDistance + 0.1F) {
  353. float const move_amount = (dist - Constants::kIdealMeleeDistance) *
  354. Constants::kMoveAmountFactor;
  355. if (dist > Constants::kMinDistance) {
  356. QVector3D const direction(dx / dist, 0.0F, dz / dist);
  357. if (!is_unit_in_hold_mode(attacker) && !is_building(attacker)) {
  358. att_t->position.x += direction.x() * move_amount;
  359. att_t->position.z += direction.z() * move_amount;
  360. }
  361. if (!is_unit_in_hold_mode(target) && !is_building(target)) {
  362. tgt_t->position.x -= direction.x() * move_amount;
  363. tgt_t->position.z -= direction.z() * move_amount;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. } // namespace
  370. void process_attacks(Engine::Core::World *world, float delta_time) {
  371. auto units = world->get_entities_with<Engine::Core::UnitComponent>();
  372. auto *arrow_sys = world->get_system<ArrowSystem>();
  373. for (auto *attacker : units) {
  374. if (attacker->has_component<Engine::Core::PendingRemovalComponent>()) {
  375. continue;
  376. }
  377. auto *attacker_unit =
  378. attacker->get_component<Engine::Core::UnitComponent>();
  379. auto *attacker_transform =
  380. attacker->get_component<Engine::Core::TransformComponent>();
  381. auto *attacker_atk =
  382. attacker->get_component<Engine::Core::AttackComponent>();
  383. if ((attacker_unit == nullptr) || (attacker_transform == nullptr)) {
  384. continue;
  385. }
  386. if (attacker_unit->health <= 0) {
  387. continue;
  388. }
  389. process_melee_lock(attacker, attacker_atk, world, delta_time);
  390. sync_melee_lock_target(attacker, attacker_atk);
  391. float range = 2.0F;
  392. int damage = 10;
  393. float cooldown = 1.0F;
  394. float *t_accum = nullptr;
  395. float tmp_accum = 0.0F;
  396. if (attacker_atk != nullptr) {
  397. update_combat_mode(attacker, world, attacker_atk);
  398. range = attacker_atk->get_current_range();
  399. damage = attacker_atk->get_current_damage();
  400. cooldown = attacker_atk->get_current_cooldown();
  401. apply_hold_mode_bonuses(attacker, attacker_unit, range, damage);
  402. attacker_atk->time_since_last += delta_time;
  403. t_accum = &attacker_atk->time_since_last;
  404. } else {
  405. tmp_accum += delta_time;
  406. t_accum = &tmp_accum;
  407. }
  408. if (*t_accum < cooldown) {
  409. continue;
  410. }
  411. auto *attack_target =
  412. attacker->get_component<Engine::Core::AttackTargetComponent>();
  413. Engine::Core::Entity *best_target = nullptr;
  414. if ((attack_target != nullptr) && attack_target->target_id != 0) {
  415. auto *target = world->get_entity(attack_target->target_id);
  416. if ((target != nullptr) &&
  417. !target->has_component<Engine::Core::PendingRemovalComponent>()) {
  418. auto *target_unit =
  419. target->get_component<Engine::Core::UnitComponent>();
  420. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  421. bool const is_ally = owner_registry.are_allies(attacker_unit->owner_id,
  422. target_unit->owner_id);
  423. if ((target_unit != nullptr) && target_unit->health > 0 &&
  424. target_unit->owner_id != attacker_unit->owner_id && !is_ally) {
  425. bool const ranged_unit = is_ranged_mode(attacker_atk);
  426. if (is_in_range(attacker, target, range)) {
  427. best_target = target;
  428. if (ranged_unit) {
  429. stop_unit_movement(attacker, attacker_transform);
  430. }
  431. face_target(
  432. attacker_transform,
  433. target->get_component<Engine::Core::TransformComponent>());
  434. } else if (attack_target->should_chase) {
  435. auto *hold_mode =
  436. attacker->get_component<Engine::Core::HoldModeComponent>();
  437. if ((hold_mode != nullptr) && hold_mode->active) {
  438. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  439. continue;
  440. }
  441. auto *guard_mode =
  442. attacker->get_component<Engine::Core::GuardModeComponent>();
  443. if ((guard_mode != nullptr) && guard_mode->active) {
  444. float guard_x = guard_mode->guard_position_x;
  445. float guard_z = guard_mode->guard_position_z;
  446. if (guard_mode->guarded_entity_id != 0) {
  447. auto *guarded_entity =
  448. world->get_entity(guard_mode->guarded_entity_id);
  449. if (guarded_entity != nullptr) {
  450. auto *guarded_transform =
  451. guarded_entity
  452. ->get_component<Engine::Core::TransformComponent>();
  453. if (guarded_transform != nullptr) {
  454. guard_x = guarded_transform->position.x;
  455. guard_z = guarded_transform->position.z;
  456. }
  457. }
  458. }
  459. auto *target_transform =
  460. target->get_component<Engine::Core::TransformComponent>();
  461. if (target_transform != nullptr) {
  462. float const dx = target_transform->position.x - guard_x;
  463. float const dz = target_transform->position.z - guard_z;
  464. float const dist_sq = dx * dx + dz * dz;
  465. float const guard_radius_sq =
  466. guard_mode->guard_radius * guard_mode->guard_radius;
  467. if (dist_sq > guard_radius_sq) {
  468. attacker
  469. ->remove_component<Engine::Core::AttackTargetComponent>();
  470. continue;
  471. }
  472. }
  473. }
  474. if (ranged_unit && is_in_range(attacker, target, range)) {
  475. stop_unit_movement(attacker, attacker_transform);
  476. best_target = target;
  477. } else {
  478. auto *target_transform =
  479. target->get_component<Engine::Core::TransformComponent>();
  480. if ((target_transform != nullptr) &&
  481. (attacker_transform != nullptr)) {
  482. QVector3D const attacker_pos(attacker_transform->position.x,
  483. 0.0F,
  484. attacker_transform->position.z);
  485. QVector3D const target_pos(target_transform->position.x, 0.0F,
  486. target_transform->position.z);
  487. QVector3D desired_pos = target_pos;
  488. bool hold_position = false;
  489. bool const target_is_building =
  490. target->has_component<Engine::Core::BuildingComponent>();
  491. if (target_is_building) {
  492. float const scale_x = target_transform->scale.x;
  493. float const scale_z = target_transform->scale.z;
  494. float const target_radius = std::max(scale_x, scale_z) * 0.5F;
  495. QVector3D direction = target_pos - attacker_pos;
  496. float const distance_sq = direction.lengthSquared();
  497. if (distance_sq > 0.000001F) {
  498. float const distance = std::sqrt(distance_sq);
  499. direction /= distance;
  500. float const desired_distance =
  501. target_radius + std::max(range - 0.2F, 0.2F);
  502. if (distance > desired_distance + 0.15F) {
  503. desired_pos = target_pos - direction * desired_distance;
  504. } else {
  505. hold_position = true;
  506. }
  507. }
  508. } else if (ranged_unit) {
  509. QVector3D direction = target_pos - attacker_pos;
  510. float const distance_sq = direction.lengthSquared();
  511. if (distance_sq > 0.000001F) {
  512. float const distance = std::sqrt(distance_sq);
  513. direction /= distance;
  514. float const optimal_range =
  515. range * Constants::kOptimalRangeFactor;
  516. if (distance >
  517. optimal_range + Constants::kOptimalRangeBuffer) {
  518. desired_pos = target_pos - direction * optimal_range;
  519. } else {
  520. hold_position = true;
  521. }
  522. }
  523. }
  524. auto *movement =
  525. attacker->get_component<Engine::Core::MovementComponent>();
  526. if (movement == nullptr) {
  527. movement =
  528. attacker
  529. ->add_component<Engine::Core::MovementComponent>();
  530. }
  531. if (movement != nullptr) {
  532. if (hold_position) {
  533. movement->has_target = false;
  534. movement->vx = 0.0F;
  535. movement->vz = 0.0F;
  536. movement->clear_path();
  537. movement->target_x = attacker_transform->position.x;
  538. movement->target_y = attacker_transform->position.z;
  539. movement->goal_x = attacker_transform->position.x;
  540. movement->goal_y = attacker_transform->position.z;
  541. } else {
  542. QVector3D planned_target(movement->target_x, 0.0F,
  543. movement->target_y);
  544. if (!movement->path.empty()) {
  545. const auto &final_node = movement->path.back();
  546. planned_target =
  547. QVector3D(final_node.first, 0.0F, final_node.second);
  548. }
  549. float const diff_sq =
  550. (planned_target - desired_pos).lengthSquared();
  551. bool need_new_command = !movement->path_pending;
  552. float const threshold = Constants::kNewCommandThreshold *
  553. Constants::kNewCommandThreshold;
  554. if (movement->has_target && diff_sq <= threshold) {
  555. need_new_command = false;
  556. }
  557. if (need_new_command) {
  558. CommandService::MoveOptions options;
  559. options.clear_attack_intent = false;
  560. options.allow_direct_fallback = true;
  561. std::vector<Engine::Core::EntityID> const unit_ids = {
  562. attacker->get_id()};
  563. std::vector<QVector3D> const move_targets = {desired_pos};
  564. CommandService::move_units(*world, unit_ids, move_targets,
  565. options);
  566. }
  567. }
  568. }
  569. if (is_in_range(attacker, target, range)) {
  570. best_target = target;
  571. }
  572. }
  573. }
  574. } else {
  575. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  576. }
  577. } else {
  578. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  579. }
  580. } else {
  581. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  582. }
  583. }
  584. if ((best_target == nullptr) && (attack_target == nullptr)) {
  585. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  586. for (auto *target : units) {
  587. if (target == attacker) {
  588. continue;
  589. }
  590. auto *target_unit =
  591. target->get_component<Engine::Core::UnitComponent>();
  592. if ((target_unit == nullptr) || target_unit->health <= 0) {
  593. continue;
  594. }
  595. if (target_unit->owner_id == attacker_unit->owner_id) {
  596. continue;
  597. }
  598. if (owner_registry.are_allies(attacker_unit->owner_id,
  599. target_unit->owner_id)) {
  600. continue;
  601. }
  602. if (target->has_component<Engine::Core::BuildingComponent>()) {
  603. continue;
  604. }
  605. if (is_in_range(attacker, target, range)) {
  606. best_target = target;
  607. break;
  608. }
  609. }
  610. }
  611. if (best_target != nullptr) {
  612. if (!attacker->has_component<Engine::Core::AttackTargetComponent>()) {
  613. auto *new_target =
  614. attacker->add_component<Engine::Core::AttackTargetComponent>();
  615. new_target->target_id = best_target->get_id();
  616. new_target->should_chase = false;
  617. } else {
  618. auto *existing_target =
  619. attacker->get_component<Engine::Core::AttackTargetComponent>();
  620. if (existing_target->target_id != best_target->get_id()) {
  621. existing_target->target_id = best_target->get_id();
  622. existing_target->should_chase = false;
  623. }
  624. }
  625. bool const ranged_unit = is_ranged_mode(attacker_atk);
  626. if (ranged_unit) {
  627. stop_unit_movement(attacker, attacker_transform);
  628. }
  629. face_target(
  630. attacker_transform,
  631. best_target->get_component<Engine::Core::TransformComponent>());
  632. auto *att_u = attacker->get_component<Engine::Core::UnitComponent>();
  633. bool const should_show_arrow_vfx =
  634. (att_u != nullptr &&
  635. att_u->spawn_type != Game::Units::SpawnType::Catapult &&
  636. att_u->spawn_type != Game::Units::SpawnType::Ballista);
  637. if (should_show_arrow_vfx &&
  638. ((attacker_atk == nullptr) ||
  639. attacker_atk->current_mode !=
  640. Engine::Core::AttackComponent::CombatMode::Melee)) {
  641. spawn_arrows(attacker, best_target, arrow_sys);
  642. }
  643. if ((attacker_atk != nullptr) &&
  644. attacker_atk->current_mode ==
  645. Engine::Core::AttackComponent::CombatMode::Melee) {
  646. if (is_unit_in_hold_mode(attacker)) {
  647. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  648. attacker_atk->in_melee_lock = false;
  649. attacker_atk->melee_lock_target_id = 0;
  650. continue;
  651. }
  652. initiate_melee_combat(attacker, best_target, attacker_atk, world);
  653. }
  654. auto *target_unit =
  655. best_target->get_component<Engine::Core::UnitComponent>();
  656. if (target_unit != nullptr) {
  657. float const tactical_multiplier = calculate_tactical_damage_multiplier(
  658. attacker, best_target, attacker_unit, target_unit);
  659. damage =
  660. static_cast<int>(static_cast<float>(damage) * tactical_multiplier);
  661. apply_high_ground_defense_bonuses(attacker, best_target, target_unit,
  662. damage);
  663. }
  664. deal_damage(world, best_target, damage, attacker->get_id());
  665. *t_accum = 0.0F;
  666. auto *guard_mode =
  667. attacker->get_component<Engine::Core::GuardModeComponent>();
  668. if ((guard_mode != nullptr) && guard_mode->active) {
  669. guard_mode->returning_to_guard_position = false;
  670. }
  671. } else {
  672. if ((attack_target == nullptr) &&
  673. attacker->has_component<Engine::Core::AttackTargetComponent>()) {
  674. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  675. }
  676. auto *guard_mode =
  677. attacker->get_component<Engine::Core::GuardModeComponent>();
  678. if ((guard_mode != nullptr) && guard_mode->active &&
  679. !guard_mode->returning_to_guard_position) {
  680. float guard_x = guard_mode->guard_position_x;
  681. float guard_z = guard_mode->guard_position_z;
  682. if (guard_mode->guarded_entity_id != 0) {
  683. auto *guarded_entity =
  684. world->get_entity(guard_mode->guarded_entity_id);
  685. if (guarded_entity != nullptr) {
  686. auto *guarded_transform =
  687. guarded_entity
  688. ->get_component<Engine::Core::TransformComponent>();
  689. if (guarded_transform != nullptr) {
  690. guard_x = guarded_transform->position.x;
  691. guard_z = guarded_transform->position.z;
  692. }
  693. }
  694. }
  695. float const dx = guard_x - attacker_transform->position.x;
  696. float const dz = guard_z - attacker_transform->position.z;
  697. float const dist_sq = dx * dx + dz * dz;
  698. float const kReturnThresholdSq =
  699. Engine::Core::Defaults::kGuardReturnThreshold *
  700. Engine::Core::Defaults::kGuardReturnThreshold;
  701. if (dist_sq > kReturnThresholdSq) {
  702. guard_mode->returning_to_guard_position = true;
  703. CommandService::MoveOptions options;
  704. options.clear_attack_intent = true;
  705. options.allow_direct_fallback = true;
  706. std::vector<Engine::Core::EntityID> const unit_ids = {
  707. attacker->get_id()};
  708. std::vector<QVector3D> const move_targets = {
  709. QVector3D(guard_x, 0.0F, guard_z)};
  710. CommandService::move_units(*world, unit_ids, move_targets, options);
  711. }
  712. }
  713. }
  714. }
  715. }
  716. } // namespace Game::Systems::Combat