attack_processor.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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::k_high_ground_height_threshold;
  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::k_max_melee_separation) {
  113. if (!is_unit_in_hold_mode(attacker) && !is_building(attacker)) {
  114. float const pull_amount = (dist - Constants::k_ideal_melee_distance) *
  115. Constants::k_melee_pull_factor * delta_time *
  116. Constants::k_melee_pull_speed;
  117. if (dist > Constants::k_min_distance) {
  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 =
  161. static_cast<int>(static_cast<float>(base_max_health) *
  162. Constants::k_health_multiplier_hold);
  163. if (unit_comp->max_health < max_health_bonus) {
  164. int const safe_max_health = std::max(1, unit_comp->max_health);
  165. int const health_percentage = (unit_comp->health * 100) / safe_max_health;
  166. unit_comp->max_health = max_health_bonus;
  167. unit_comp->health = (max_health_bonus * health_percentage) / 100;
  168. }
  169. }
  170. void apply_hold_mode_bonuses(Engine::Core::Entity *attacker,
  171. Engine::Core::UnitComponent *unit_comp,
  172. float &range, int &damage) {
  173. auto *hold_mode = attacker->get_component<Engine::Core::HoldModeComponent>();
  174. if ((hold_mode == nullptr) || !hold_mode->active) {
  175. return;
  176. }
  177. if (unit_comp->spawn_type == Game::Units::SpawnType::Archer) {
  178. range *= Constants::k_range_multiplier_hold;
  179. damage = static_cast<int>(static_cast<float>(damage) *
  180. Constants::k_damage_multiplier_archer_hold);
  181. apply_health_bonus(unit_comp);
  182. } else if (unit_comp->spawn_type == Game::Units::SpawnType::Spearman) {
  183. range *= Constants::k_range_multiplier_spearman_hold;
  184. damage = static_cast<int>(static_cast<float>(damage) *
  185. Constants::k_damage_multiplier_spearman_hold);
  186. apply_health_bonus(unit_comp);
  187. } else {
  188. damage = static_cast<int>(static_cast<float>(damage) *
  189. Constants::k_damage_multiplier_default_hold);
  190. }
  191. }
  192. void apply_high_ground_defense_bonuses(Engine::Core::Entity *attacker,
  193. Engine::Core::Entity *target,
  194. Engine::Core::UnitComponent *target_unit,
  195. int &damage) {
  196. if (target_unit == nullptr) {
  197. return;
  198. }
  199. if (target_unit->spawn_type != Game::Units::SpawnType::Archer &&
  200. target_unit->spawn_type != Game::Units::SpawnType::Spearman) {
  201. return;
  202. }
  203. auto *attacker_transform =
  204. attacker->get_component<Engine::Core::TransformComponent>();
  205. auto *target_transform =
  206. target->get_component<Engine::Core::TransformComponent>();
  207. if (!is_high_ground_advantage(target_transform, attacker_transform)) {
  208. return;
  209. }
  210. damage =
  211. std::max(1, static_cast<int>(static_cast<float>(damage) *
  212. Constants::k_high_ground_armor_multiplier));
  213. auto base_max_health_opt = get_base_max_health(target_unit);
  214. if (!base_max_health_opt || *base_max_health_opt <= 0) {
  215. return;
  216. }
  217. int const max_health_bonus =
  218. static_cast<int>(static_cast<float>(*base_max_health_opt) *
  219. Constants::k_high_ground_health_multiplier);
  220. if (target_unit->max_health < max_health_bonus) {
  221. int const safe_max_health = std::max(1, target_unit->max_health);
  222. int const health_percentage = (target_unit->health * 100) / safe_max_health;
  223. target_unit->max_health = max_health_bonus;
  224. target_unit->health = (max_health_bonus * health_percentage) / 100;
  225. }
  226. }
  227. auto calculate_tactical_damage_multiplier(
  228. Engine::Core::Entity *attacker, Engine::Core::Entity *target,
  229. Engine::Core::UnitComponent *attacker_unit,
  230. Engine::Core::UnitComponent *target_unit) -> float {
  231. float multiplier = 1.0F;
  232. if (attacker_unit->spawn_type == Game::Units::SpawnType::Spearman) {
  233. if (target_unit->spawn_type == Game::Units::SpawnType::HorseArcher ||
  234. target_unit->spawn_type == Game::Units::SpawnType::HorseSpearman ||
  235. target_unit->spawn_type == Game::Units::SpawnType::MountedKnight) {
  236. multiplier *= Constants::k_spearman_vs_cavalry_multiplier;
  237. }
  238. }
  239. if (attacker_unit->spawn_type == Game::Units::SpawnType::Archer ||
  240. attacker_unit->spawn_type == Game::Units::SpawnType::HorseArcher) {
  241. if (target->has_component<Engine::Core::ElephantComponent>()) {
  242. multiplier *= Constants::k_archer_vs_elephant_multiplier;
  243. }
  244. auto *attacker_transform =
  245. attacker->get_component<Engine::Core::TransformComponent>();
  246. auto *target_transform =
  247. target->get_component<Engine::Core::TransformComponent>();
  248. if (is_high_ground_advantage(attacker_transform, target_transform)) {
  249. multiplier *= Constants::k_archer_high_ground_multiplier;
  250. }
  251. } else if (attacker_unit->spawn_type == Game::Units::SpawnType::Spearman) {
  252. auto *attacker_transform =
  253. attacker->get_component<Engine::Core::TransformComponent>();
  254. auto *target_transform =
  255. target->get_component<Engine::Core::TransformComponent>();
  256. if (is_high_ground_advantage(attacker_transform, target_transform)) {
  257. multiplier *= Constants::k_spearman_high_ground_multiplier;
  258. }
  259. }
  260. return multiplier;
  261. }
  262. void spawn_arrows(Engine::Core::Entity *attacker, Engine::Core::Entity *target,
  263. ArrowSystem *arrow_sys) {
  264. if (arrow_sys == nullptr) {
  265. return;
  266. }
  267. auto *att_t = attacker->get_component<Engine::Core::TransformComponent>();
  268. auto *tgt_t = target->get_component<Engine::Core::TransformComponent>();
  269. auto *att_u = attacker->get_component<Engine::Core::UnitComponent>();
  270. if ((att_t == nullptr) || (tgt_t == nullptr)) {
  271. return;
  272. }
  273. QVector3D const a_pos(att_t->position.x, att_t->position.y,
  274. att_t->position.z);
  275. QVector3D const t_pos(tgt_t->position.x, tgt_t->position.y,
  276. tgt_t->position.z);
  277. QVector3D const dir = (t_pos - a_pos).normalized();
  278. QVector3D const color =
  279. (att_u != nullptr) ? Game::Visuals::team_colorForOwner(att_u->owner_id)
  280. : QVector3D(0.8F, 0.9F, 1.0F);
  281. int arrow_count = 1;
  282. if (att_u != nullptr) {
  283. int const troop_size =
  284. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  285. att_u->spawn_type);
  286. int const max_arrows = std::max(2, (troop_size * 2) / 3);
  287. static thread_local std::mt19937 arrow_gen(std::random_device{}());
  288. std::uniform_int_distribution<> dist(max_arrows / 2, max_arrows);
  289. arrow_count = dist(arrow_gen);
  290. }
  291. for (int i = 0; i < arrow_count; ++i) {
  292. static thread_local std::mt19937 spread_gen(std::random_device{}());
  293. std::uniform_real_distribution<float> spread_dist(
  294. Constants::k_arrow_spread_min, Constants::k_arrow_spread_max);
  295. QVector3D const perpendicular(-dir.z(), 0.0F, dir.x());
  296. QVector3D const up_vector(0.0F, 1.0F, 0.0F);
  297. float const lateral_offset = spread_dist(spread_gen);
  298. float const vertical_offset =
  299. spread_dist(spread_gen) * Constants::k_arrow_vertical_spread_factor;
  300. float const depth_offset =
  301. spread_dist(spread_gen) * Constants::k_arrow_depth_spread_factor;
  302. QVector3D const start_offset =
  303. perpendicular * lateral_offset + up_vector * vertical_offset;
  304. QVector3D const end_offset = perpendicular * lateral_offset +
  305. up_vector * vertical_offset +
  306. dir * depth_offset;
  307. QVector3D const start =
  308. a_pos + QVector3D(0.0F, Constants::k_arrow_start_height, 0.0F) +
  309. dir * Constants::k_arrow_start_offset + start_offset;
  310. QVector3D const end = t_pos +
  311. QVector3D(Constants::k_arrow_target_offset,
  312. Constants::k_arrow_target_offset, 0.0F) +
  313. end_offset;
  314. arrow_sys->spawn_arrow(start, end, color, Constants::k_arrow_speed);
  315. }
  316. }
  317. void initiate_melee_combat(Engine::Core::Entity *attacker,
  318. Engine::Core::Entity *target,
  319. Engine::Core::AttackComponent *attack_comp,
  320. Engine::Core::World *world) {
  321. attack_comp->in_melee_lock = true;
  322. attack_comp->melee_lock_target_id = target->get_id();
  323. auto *combat_state =
  324. attacker->get_component<Engine::Core::CombatStateComponent>();
  325. if (combat_state == nullptr) {
  326. combat_state =
  327. attacker->add_component<Engine::Core::CombatStateComponent>();
  328. }
  329. if (combat_state != nullptr && combat_state->animation_state ==
  330. Engine::Core::CombatAnimationState::Idle) {
  331. combat_state->animation_state = Engine::Core::CombatAnimationState::Advance;
  332. combat_state->state_time = 0.0F;
  333. combat_state->state_duration =
  334. Engine::Core::CombatStateComponent::kAdvanceDuration;
  335. std::uniform_real_distribution<float> offset_dist(0.0F, 0.15F);
  336. combat_state->attack_offset = offset_dist(gen);
  337. std::uniform_int_distribution<int> variant_dist(
  338. 0, Engine::Core::CombatStateComponent::kMaxAttackVariants - 1);
  339. combat_state->attack_variant = static_cast<std::uint8_t>(variant_dist(gen));
  340. }
  341. auto *target_atk = target->get_component<Engine::Core::AttackComponent>();
  342. if (target_atk != nullptr) {
  343. target_atk->in_melee_lock = true;
  344. target_atk->melee_lock_target_id = attacker->get_id();
  345. }
  346. auto *att_t = attacker->get_component<Engine::Core::TransformComponent>();
  347. auto *tgt_t = target->get_component<Engine::Core::TransformComponent>();
  348. if ((att_t != nullptr) && (tgt_t != nullptr)) {
  349. face_target(att_t, tgt_t);
  350. face_target(tgt_t, att_t);
  351. float const dx = tgt_t->position.x - att_t->position.x;
  352. float const dz = tgt_t->position.z - att_t->position.z;
  353. float const dist = std::sqrt(dx * dx + dz * dz);
  354. if (dist > Constants::k_ideal_melee_distance + 0.1F) {
  355. float const move_amount = (dist - Constants::k_ideal_melee_distance) *
  356. Constants::k_move_amount_factor;
  357. if (dist > Constants::k_min_distance) {
  358. QVector3D const direction(dx / dist, 0.0F, dz / dist);
  359. if (!is_unit_in_hold_mode(attacker) && !is_building(attacker)) {
  360. att_t->position.x += direction.x() * move_amount;
  361. att_t->position.z += direction.z() * move_amount;
  362. }
  363. if (!is_unit_in_hold_mode(target) && !is_building(target)) {
  364. tgt_t->position.x -= direction.x() * move_amount;
  365. tgt_t->position.z -= direction.z() * move_amount;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. } // namespace
  372. void process_attacks(Engine::Core::World *world, float delta_time) {
  373. auto units = world->get_entities_with<Engine::Core::UnitComponent>();
  374. auto *arrow_sys = world->get_system<ArrowSystem>();
  375. for (auto *attacker : units) {
  376. if (attacker->has_component<Engine::Core::PendingRemovalComponent>()) {
  377. continue;
  378. }
  379. auto *attacker_unit =
  380. attacker->get_component<Engine::Core::UnitComponent>();
  381. auto *attacker_transform =
  382. attacker->get_component<Engine::Core::TransformComponent>();
  383. auto *attacker_atk =
  384. attacker->get_component<Engine::Core::AttackComponent>();
  385. if ((attacker_unit == nullptr) || (attacker_transform == nullptr)) {
  386. continue;
  387. }
  388. if (attacker_unit->health <= 0) {
  389. continue;
  390. }
  391. process_melee_lock(attacker, attacker_atk, world, delta_time);
  392. sync_melee_lock_target(attacker, attacker_atk);
  393. float range = 2.0F;
  394. int damage = 10;
  395. float cooldown = 1.0F;
  396. float *t_accum = nullptr;
  397. float tmp_accum = 0.0F;
  398. if (attacker_atk != nullptr) {
  399. update_combat_mode(attacker, world, attacker_atk);
  400. range = attacker_atk->get_current_range();
  401. damage = attacker_atk->get_current_damage();
  402. cooldown = attacker_atk->get_current_cooldown();
  403. apply_hold_mode_bonuses(attacker, attacker_unit, range, damage);
  404. attacker_atk->time_since_last += delta_time;
  405. t_accum = &attacker_atk->time_since_last;
  406. } else {
  407. tmp_accum += delta_time;
  408. t_accum = &tmp_accum;
  409. }
  410. if (*t_accum < cooldown) {
  411. continue;
  412. }
  413. auto *attack_target =
  414. attacker->get_component<Engine::Core::AttackTargetComponent>();
  415. Engine::Core::Entity *best_target = nullptr;
  416. if ((attack_target != nullptr) && attack_target->target_id != 0) {
  417. auto *target = world->get_entity(attack_target->target_id);
  418. if ((target != nullptr) &&
  419. !target->has_component<Engine::Core::PendingRemovalComponent>()) {
  420. auto *target_unit =
  421. target->get_component<Engine::Core::UnitComponent>();
  422. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  423. bool const is_ally = owner_registry.are_allies(attacker_unit->owner_id,
  424. target_unit->owner_id);
  425. if ((target_unit != nullptr) && target_unit->health > 0 &&
  426. target_unit->owner_id != attacker_unit->owner_id && !is_ally) {
  427. bool const ranged_unit = is_ranged_mode(attacker_atk);
  428. if (is_in_range(attacker, target, range)) {
  429. best_target = target;
  430. if (ranged_unit) {
  431. stop_unit_movement(attacker, attacker_transform);
  432. }
  433. face_target(
  434. attacker_transform,
  435. target->get_component<Engine::Core::TransformComponent>());
  436. } else if (attack_target->should_chase) {
  437. auto *hold_mode =
  438. attacker->get_component<Engine::Core::HoldModeComponent>();
  439. if ((hold_mode != nullptr) && hold_mode->active) {
  440. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  441. continue;
  442. }
  443. auto *guard_mode =
  444. attacker->get_component<Engine::Core::GuardModeComponent>();
  445. if ((guard_mode != nullptr) && guard_mode->active) {
  446. float guard_x = guard_mode->guard_position_x;
  447. float guard_z = guard_mode->guard_position_z;
  448. if (guard_mode->guarded_entity_id != 0) {
  449. auto *guarded_entity =
  450. world->get_entity(guard_mode->guarded_entity_id);
  451. if (guarded_entity != nullptr) {
  452. auto *guarded_transform =
  453. guarded_entity
  454. ->get_component<Engine::Core::TransformComponent>();
  455. if (guarded_transform != nullptr) {
  456. guard_x = guarded_transform->position.x;
  457. guard_z = guarded_transform->position.z;
  458. }
  459. }
  460. }
  461. auto *target_transform =
  462. target->get_component<Engine::Core::TransformComponent>();
  463. if (target_transform != nullptr) {
  464. float const dx = target_transform->position.x - guard_x;
  465. float const dz = target_transform->position.z - guard_z;
  466. float const dist_sq = dx * dx + dz * dz;
  467. float const guard_radius_sq =
  468. guard_mode->guard_radius * guard_mode->guard_radius;
  469. if (dist_sq > guard_radius_sq) {
  470. attacker
  471. ->remove_component<Engine::Core::AttackTargetComponent>();
  472. continue;
  473. }
  474. }
  475. }
  476. if (ranged_unit && is_in_range(attacker, target, range)) {
  477. stop_unit_movement(attacker, attacker_transform);
  478. best_target = target;
  479. } else {
  480. auto *target_transform =
  481. target->get_component<Engine::Core::TransformComponent>();
  482. if ((target_transform != nullptr) &&
  483. (attacker_transform != nullptr)) {
  484. QVector3D const attacker_pos(attacker_transform->position.x,
  485. 0.0F,
  486. attacker_transform->position.z);
  487. QVector3D const target_pos(target_transform->position.x, 0.0F,
  488. target_transform->position.z);
  489. QVector3D desired_pos = target_pos;
  490. bool hold_position = false;
  491. bool const target_is_building =
  492. target->has_component<Engine::Core::BuildingComponent>();
  493. if (target_is_building) {
  494. float const scale_x = target_transform->scale.x;
  495. float const scale_z = target_transform->scale.z;
  496. float const target_radius = std::max(scale_x, scale_z) * 0.5F;
  497. QVector3D direction = target_pos - attacker_pos;
  498. float const distance_sq = direction.lengthSquared();
  499. if (distance_sq > 0.000001F) {
  500. float const distance = std::sqrt(distance_sq);
  501. direction /= distance;
  502. float const desired_distance =
  503. target_radius + std::max(range - 0.2F, 0.2F);
  504. if (distance > desired_distance + 0.15F) {
  505. desired_pos = target_pos - direction * desired_distance;
  506. } else {
  507. hold_position = true;
  508. }
  509. }
  510. } else if (ranged_unit) {
  511. QVector3D direction = target_pos - attacker_pos;
  512. float const distance_sq = direction.lengthSquared();
  513. if (distance_sq > 0.000001F) {
  514. float const distance = std::sqrt(distance_sq);
  515. direction /= distance;
  516. float const optimal_range =
  517. range * Constants::k_optimal_range_factor;
  518. if (distance >
  519. optimal_range + Constants::k_optimal_range_buffer) {
  520. desired_pos = target_pos - direction * optimal_range;
  521. } else {
  522. hold_position = true;
  523. }
  524. }
  525. }
  526. auto *movement =
  527. attacker->get_component<Engine::Core::MovementComponent>();
  528. if (movement == nullptr) {
  529. movement =
  530. attacker
  531. ->add_component<Engine::Core::MovementComponent>();
  532. }
  533. if (movement != nullptr) {
  534. if (hold_position) {
  535. movement->has_target = false;
  536. movement->vx = 0.0F;
  537. movement->vz = 0.0F;
  538. movement->clear_path();
  539. movement->target_x = attacker_transform->position.x;
  540. movement->target_y = attacker_transform->position.z;
  541. movement->goal_x = attacker_transform->position.x;
  542. movement->goal_y = attacker_transform->position.z;
  543. } else {
  544. QVector3D planned_target(movement->target_x, 0.0F,
  545. movement->target_y);
  546. if (!movement->path.empty()) {
  547. const auto &final_node = movement->path.back();
  548. planned_target =
  549. QVector3D(final_node.first, 0.0F, final_node.second);
  550. }
  551. float const diff_sq =
  552. (planned_target - desired_pos).lengthSquared();
  553. bool need_new_command = !movement->path_pending;
  554. float const threshold = Constants::k_new_command_threshold *
  555. Constants::k_new_command_threshold;
  556. if (movement->has_target && diff_sq <= threshold) {
  557. need_new_command = false;
  558. }
  559. if (need_new_command) {
  560. CommandService::MoveOptions options;
  561. options.clear_attack_intent = false;
  562. options.allow_direct_fallback = true;
  563. std::vector<Engine::Core::EntityID> const unit_ids = {
  564. attacker->get_id()};
  565. std::vector<QVector3D> const move_targets = {desired_pos};
  566. CommandService::move_units(*world, unit_ids, move_targets,
  567. options);
  568. }
  569. }
  570. }
  571. if (is_in_range(attacker, target, range)) {
  572. best_target = target;
  573. }
  574. }
  575. }
  576. } else {
  577. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  578. }
  579. } else {
  580. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  581. }
  582. } else {
  583. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  584. }
  585. }
  586. if ((best_target == nullptr) && (attack_target == nullptr)) {
  587. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  588. for (auto *target : units) {
  589. if (target == attacker) {
  590. continue;
  591. }
  592. auto *target_unit =
  593. target->get_component<Engine::Core::UnitComponent>();
  594. if ((target_unit == nullptr) || target_unit->health <= 0) {
  595. continue;
  596. }
  597. if (target_unit->owner_id == attacker_unit->owner_id) {
  598. continue;
  599. }
  600. if (owner_registry.are_allies(attacker_unit->owner_id,
  601. target_unit->owner_id)) {
  602. continue;
  603. }
  604. if (target->has_component<Engine::Core::BuildingComponent>()) {
  605. continue;
  606. }
  607. if (is_in_range(attacker, target, range)) {
  608. best_target = target;
  609. break;
  610. }
  611. }
  612. }
  613. if (best_target != nullptr) {
  614. if (!attacker->has_component<Engine::Core::AttackTargetComponent>()) {
  615. auto *new_target =
  616. attacker->add_component<Engine::Core::AttackTargetComponent>();
  617. new_target->target_id = best_target->get_id();
  618. new_target->should_chase = false;
  619. } else {
  620. auto *existing_target =
  621. attacker->get_component<Engine::Core::AttackTargetComponent>();
  622. if (existing_target->target_id != best_target->get_id()) {
  623. existing_target->target_id = best_target->get_id();
  624. existing_target->should_chase = false;
  625. }
  626. }
  627. bool const ranged_unit = is_ranged_mode(attacker_atk);
  628. if (ranged_unit) {
  629. stop_unit_movement(attacker, attacker_transform);
  630. }
  631. face_target(
  632. attacker_transform,
  633. best_target->get_component<Engine::Core::TransformComponent>());
  634. auto *att_u = attacker->get_component<Engine::Core::UnitComponent>();
  635. bool const should_show_arrow_vfx =
  636. (att_u != nullptr &&
  637. att_u->spawn_type != Game::Units::SpawnType::Catapult &&
  638. att_u->spawn_type != Game::Units::SpawnType::Ballista);
  639. if (should_show_arrow_vfx &&
  640. ((attacker_atk == nullptr) ||
  641. attacker_atk->current_mode !=
  642. Engine::Core::AttackComponent::CombatMode::Melee)) {
  643. spawn_arrows(attacker, best_target, arrow_sys);
  644. }
  645. if ((attacker_atk != nullptr) &&
  646. attacker_atk->current_mode ==
  647. Engine::Core::AttackComponent::CombatMode::Melee) {
  648. if (is_unit_in_hold_mode(attacker)) {
  649. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  650. attacker_atk->in_melee_lock = false;
  651. attacker_atk->melee_lock_target_id = 0;
  652. continue;
  653. }
  654. initiate_melee_combat(attacker, best_target, attacker_atk, world);
  655. }
  656. auto *target_unit =
  657. best_target->get_component<Engine::Core::UnitComponent>();
  658. if (target_unit != nullptr) {
  659. float const tactical_multiplier = calculate_tactical_damage_multiplier(
  660. attacker, best_target, attacker_unit, target_unit);
  661. damage =
  662. static_cast<int>(static_cast<float>(damage) * tactical_multiplier);
  663. apply_high_ground_defense_bonuses(attacker, best_target, target_unit,
  664. damage);
  665. }
  666. deal_damage(world, best_target, damage, attacker->get_id());
  667. *t_accum = 0.0F;
  668. auto *guard_mode =
  669. attacker->get_component<Engine::Core::GuardModeComponent>();
  670. if ((guard_mode != nullptr) && guard_mode->active) {
  671. guard_mode->returning_to_guard_position = false;
  672. }
  673. } else {
  674. if ((attack_target == nullptr) &&
  675. attacker->has_component<Engine::Core::AttackTargetComponent>()) {
  676. attacker->remove_component<Engine::Core::AttackTargetComponent>();
  677. }
  678. auto *guard_mode =
  679. attacker->get_component<Engine::Core::GuardModeComponent>();
  680. if ((guard_mode != nullptr) && guard_mode->active &&
  681. !guard_mode->returning_to_guard_position) {
  682. float guard_x = guard_mode->guard_position_x;
  683. float guard_z = guard_mode->guard_position_z;
  684. if (guard_mode->guarded_entity_id != 0) {
  685. auto *guarded_entity =
  686. world->get_entity(guard_mode->guarded_entity_id);
  687. if (guarded_entity != nullptr) {
  688. auto *guarded_transform =
  689. guarded_entity
  690. ->get_component<Engine::Core::TransformComponent>();
  691. if (guarded_transform != nullptr) {
  692. guard_x = guarded_transform->position.x;
  693. guard_z = guarded_transform->position.z;
  694. }
  695. }
  696. }
  697. float const dx = guard_x - attacker_transform->position.x;
  698. float const dz = guard_z - attacker_transform->position.z;
  699. float const dist_sq = dx * dx + dz * dz;
  700. float const k_return_threshold_sq =
  701. Engine::Core::Defaults::kGuardReturnThreshold *
  702. Engine::Core::Defaults::kGuardReturnThreshold;
  703. if (dist_sq > k_return_threshold_sq) {
  704. guard_mode->returning_to_guard_position = true;
  705. CommandService::MoveOptions options;
  706. options.clear_attack_intent = true;
  707. options.allow_direct_fallback = true;
  708. std::vector<Engine::Core::EntityID> const unit_ids = {
  709. attacker->get_id()};
  710. std::vector<QVector3D> const move_targets = {
  711. QVector3D(guard_x, 0.0F, guard_z)};
  712. CommandService::move_units(*world, unit_ids, move_targets, options);
  713. }
  714. }
  715. }
  716. }
  717. }
  718. } // namespace Game::Systems::Combat