command_controller.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. #include "command_controller.h"
  2. #include "../../game/core/component.h"
  3. #include "../../game/core/entity.h"
  4. #include "../../game/core/world.h"
  5. #include "../../game/systems/command_service.h"
  6. #include "../../game/systems/formation_planner.h"
  7. #include "../../game/systems/picking_service.h"
  8. #include "../../game/systems/production_service.h"
  9. #include "../../game/systems/selection_system.h"
  10. #include "../../game/systems/troop_profile_service.h"
  11. #include "../../render/gl/camera.h"
  12. #include "../utils/movement_utils.h"
  13. #include "game/game_config.h"
  14. #include "units/spawn_type.h"
  15. #include <QPointF>
  16. #include <cmath>
  17. #include <numbers>
  18. #include <qglobal.h>
  19. #include <qobject.h>
  20. #include <qtmetamacros.h>
  21. #include <qvectornd.h>
  22. #include <vector>
  23. namespace App::Controllers {
  24. CommandController::CommandController(
  25. Engine::Core::World *world,
  26. Game::Systems::SelectionSystem *selection_system,
  27. Game::Systems::PickingService *picking_service, QObject *parent)
  28. : QObject(parent), m_world(world), m_selection_system(selection_system),
  29. m_picking_service(picking_service) {}
  30. auto CommandController::on_attack_click(qreal sx, qreal sy, int viewport_width,
  31. int viewport_height,
  32. void *camera) -> CommandResult {
  33. CommandResult result;
  34. if ((m_selection_system == nullptr) || (m_picking_service == nullptr) ||
  35. (camera == nullptr) || (m_world == nullptr)) {
  36. result.reset_cursor_to_normal = true;
  37. return result;
  38. }
  39. const auto &selected = m_selection_system->get_selected_units();
  40. if (selected.empty()) {
  41. result.reset_cursor_to_normal = true;
  42. return result;
  43. }
  44. auto *cam = static_cast<Render::GL::Camera *>(camera);
  45. Engine::Core::EntityID const target_id =
  46. Game::Systems::PickingService::pick_unit_first(
  47. float(sx), float(sy), *m_world, *cam, viewport_width, viewport_height,
  48. 0);
  49. if (target_id == 0) {
  50. result.reset_cursor_to_normal = true;
  51. return result;
  52. }
  53. auto *target_entity = m_world->get_entity(target_id);
  54. if (target_entity == nullptr) {
  55. return result;
  56. }
  57. auto *target_unit =
  58. target_entity->get_component<Engine::Core::UnitComponent>();
  59. if (target_unit == nullptr) {
  60. return result;
  61. }
  62. Game::Systems::CommandService::attack_target(*m_world, selected, target_id,
  63. true);
  64. emit attack_target_selected();
  65. result.input_consumed = true;
  66. result.reset_cursor_to_normal = true;
  67. return result;
  68. }
  69. auto CommandController::on_stop_command() -> CommandResult {
  70. CommandResult result;
  71. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  72. return result;
  73. }
  74. const auto &selected = m_selection_system->get_selected_units();
  75. if (selected.empty()) {
  76. return result;
  77. }
  78. for (auto id : selected) {
  79. auto *entity = m_world->get_entity(id);
  80. if (entity == nullptr) {
  81. continue;
  82. }
  83. reset_movement(entity);
  84. entity->remove_component<Engine::Core::AttackTargetComponent>();
  85. if (auto *patrol = entity->get_component<Engine::Core::PatrolComponent>()) {
  86. patrol->patrolling = false;
  87. patrol->waypoints.clear();
  88. }
  89. auto *hold_mode = entity->get_component<Engine::Core::HoldModeComponent>();
  90. if ((hold_mode != nullptr) && hold_mode->active) {
  91. hold_mode->active = false;
  92. hold_mode->exit_cooldown = hold_mode->stand_up_duration;
  93. emit hold_mode_changed(false);
  94. }
  95. auto *formation_mode =
  96. entity->get_component<Engine::Core::FormationModeComponent>();
  97. if ((formation_mode != nullptr) && formation_mode->active) {
  98. formation_mode->active = false;
  99. emit formation_mode_changed(false);
  100. }
  101. }
  102. result.input_consumed = true;
  103. result.reset_cursor_to_normal = true;
  104. return result;
  105. }
  106. auto CommandController::on_hold_command() -> CommandResult {
  107. CommandResult result;
  108. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  109. return result;
  110. }
  111. const auto &selected = m_selection_system->get_selected_units();
  112. if (selected.empty()) {
  113. return result;
  114. }
  115. int eligible_count = 0;
  116. int hold_active_count = 0;
  117. for (auto id : selected) {
  118. auto *entity = m_world->get_entity(id);
  119. if (entity == nullptr) {
  120. continue;
  121. }
  122. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  123. if (unit == nullptr) {
  124. continue;
  125. }
  126. if (!Game::Units::can_use_hold_mode(unit->spawn_type)) {
  127. continue;
  128. }
  129. eligible_count++;
  130. auto *hold_mode = entity->get_component<Engine::Core::HoldModeComponent>();
  131. if ((hold_mode != nullptr) && hold_mode->active) {
  132. hold_active_count++;
  133. }
  134. }
  135. if (eligible_count == 0) {
  136. return result;
  137. }
  138. const bool should_enable_hold = (hold_active_count < eligible_count);
  139. for (auto id : selected) {
  140. auto *entity = m_world->get_entity(id);
  141. if (entity == nullptr) {
  142. continue;
  143. }
  144. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  145. if (unit == nullptr) {
  146. continue;
  147. }
  148. if (!Game::Units::can_use_hold_mode(unit->spawn_type)) {
  149. continue;
  150. }
  151. auto *hold_mode = entity->get_component<Engine::Core::HoldModeComponent>();
  152. if (should_enable_hold) {
  153. reset_movement(entity);
  154. entity->remove_component<Engine::Core::AttackTargetComponent>();
  155. auto *attack_comp =
  156. entity->get_component<Engine::Core::AttackComponent>();
  157. if (attack_comp != nullptr) {
  158. attack_comp->in_melee_lock = false;
  159. attack_comp->melee_lock_target_id = 0;
  160. }
  161. if (auto *patrol =
  162. entity->get_component<Engine::Core::PatrolComponent>()) {
  163. patrol->patrolling = false;
  164. patrol->waypoints.clear();
  165. }
  166. if (hold_mode == nullptr) {
  167. hold_mode = entity->add_component<Engine::Core::HoldModeComponent>();
  168. }
  169. hold_mode->active = true;
  170. hold_mode->exit_cooldown = 0.0F;
  171. auto *movement = entity->get_component<Engine::Core::MovementComponent>();
  172. if (movement != nullptr) {
  173. movement->has_target = false;
  174. movement->path.clear();
  175. movement->path_pending = false;
  176. movement->vx = 0.0F;
  177. movement->vz = 0.0F;
  178. }
  179. } else {
  180. if ((hold_mode != nullptr) && hold_mode->active) {
  181. hold_mode->active = false;
  182. hold_mode->exit_cooldown = hold_mode->stand_up_duration;
  183. }
  184. }
  185. }
  186. emit hold_mode_changed(should_enable_hold);
  187. result.input_consumed = true;
  188. result.reset_cursor_to_normal = true;
  189. return result;
  190. }
  191. auto CommandController::on_patrol_click(qreal sx, qreal sy, int viewport_width,
  192. int viewport_height,
  193. void *camera) -> CommandResult {
  194. CommandResult result;
  195. if ((m_selection_system == nullptr) || (m_world == nullptr) ||
  196. (m_picking_service == nullptr) || (camera == nullptr)) {
  197. if (m_has_patrol_first_waypoint) {
  198. clear_patrol_first_waypoint();
  199. result.reset_cursor_to_normal = true;
  200. }
  201. return result;
  202. }
  203. const auto &selected = m_selection_system->get_selected_units();
  204. if (selected.empty()) {
  205. if (m_has_patrol_first_waypoint) {
  206. clear_patrol_first_waypoint();
  207. result.reset_cursor_to_normal = true;
  208. }
  209. return result;
  210. }
  211. auto *cam = static_cast<Render::GL::Camera *>(camera);
  212. QVector3D hit;
  213. if (!Game::Systems::PickingService::screen_to_ground(
  214. QPointF(sx, sy), *cam, viewport_width, viewport_height, hit)) {
  215. if (m_has_patrol_first_waypoint) {
  216. clear_patrol_first_waypoint();
  217. result.reset_cursor_to_normal = true;
  218. }
  219. return result;
  220. }
  221. if (!m_has_patrol_first_waypoint) {
  222. m_has_patrol_first_waypoint = true;
  223. m_patrol_first_waypoint = hit;
  224. result.input_consumed = true;
  225. return result;
  226. }
  227. QVector3D const second_waypoint = hit;
  228. for (auto id : selected) {
  229. auto *entity = m_world->get_entity(id);
  230. if (entity == nullptr) {
  231. continue;
  232. }
  233. auto *building = entity->get_component<Engine::Core::BuildingComponent>();
  234. if (building != nullptr) {
  235. continue;
  236. }
  237. auto *patrol = entity->get_component<Engine::Core::PatrolComponent>();
  238. if (patrol == nullptr) {
  239. patrol = entity->add_component<Engine::Core::PatrolComponent>();
  240. }
  241. if (patrol != nullptr) {
  242. patrol->waypoints.clear();
  243. patrol->waypoints.emplace_back(m_patrol_first_waypoint.x(),
  244. m_patrol_first_waypoint.z());
  245. patrol->waypoints.emplace_back(second_waypoint.x(), second_waypoint.z());
  246. patrol->current_waypoint = 0;
  247. patrol->patrolling = true;
  248. }
  249. reset_movement(entity);
  250. entity->remove_component<Engine::Core::AttackTargetComponent>();
  251. }
  252. clear_patrol_first_waypoint();
  253. result.input_consumed = true;
  254. result.reset_cursor_to_normal = true;
  255. return result;
  256. }
  257. auto CommandController::set_rally_at_screen(
  258. qreal sx, qreal sy, int viewport_width, int viewport_height, void *camera,
  259. int local_owner_id) -> CommandResult {
  260. CommandResult result;
  261. if ((m_world == nullptr) || (m_selection_system == nullptr) ||
  262. (m_picking_service == nullptr) || (camera == nullptr)) {
  263. return result;
  264. }
  265. auto *cam = static_cast<Render::GL::Camera *>(camera);
  266. QVector3D hit;
  267. if (!Game::Systems::PickingService::screen_to_ground(
  268. QPointF(sx, sy), *cam, viewport_width, viewport_height, hit)) {
  269. return result;
  270. }
  271. Game::Systems::ProductionService::set_rally_for_first_selected_barracks(
  272. *m_world, m_selection_system->get_selected_units(), local_owner_id,
  273. hit.x(), hit.z());
  274. result.input_consumed = true;
  275. return result;
  276. }
  277. void CommandController::recruit_near_selected(const QString &unit_type,
  278. int local_owner_id) {
  279. if ((m_world == nullptr) || (m_selection_system == nullptr)) {
  280. return;
  281. }
  282. const auto &sel = m_selection_system->get_selected_units();
  283. if (sel.empty()) {
  284. return;
  285. }
  286. auto result = Game::Systems::ProductionService::
  287. start_production_for_first_selected_barracks(
  288. *m_world, sel, local_owner_id, unit_type.toStdString());
  289. if (result == Game::Systems::ProductionResult::GlobalTroopLimitReached) {
  290. emit troop_limit_reached();
  291. }
  292. }
  293. void CommandController::reset_movement(Engine::Core::Entity *entity) {
  294. App::Utils::reset_movement(entity);
  295. }
  296. void CommandController::reset_transient_state() {
  297. m_has_patrol_first_waypoint = false;
  298. m_patrol_first_waypoint = QVector3D();
  299. if (!m_is_placing_formation) {
  300. m_formation_placement_position = QVector3D();
  301. m_formation_placement_angle = 0.0F;
  302. m_formation_units.clear();
  303. return;
  304. }
  305. for (const auto id : m_formation_units) {
  306. auto *entity = m_world != nullptr ? m_world->get_entity(id) : nullptr;
  307. if (entity == nullptr) {
  308. continue;
  309. }
  310. auto *formation_mode =
  311. entity->get_component<Engine::Core::FormationModeComponent>();
  312. if (formation_mode != nullptr) {
  313. formation_mode->active = false;
  314. }
  315. }
  316. m_is_placing_formation = false;
  317. m_formation_placement_position = QVector3D();
  318. m_formation_placement_angle = 0.0F;
  319. m_formation_units.clear();
  320. emit formation_placement_ended();
  321. emit formation_mode_changed(false);
  322. }
  323. auto CommandController::any_selected_in_hold_mode() const -> bool {
  324. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  325. return false;
  326. }
  327. const auto &selected = m_selection_system->get_selected_units();
  328. for (Engine::Core::EntityID const entity_id : selected) {
  329. Engine::Core::Entity *entity = m_world->get_entity(entity_id);
  330. if (entity == nullptr) {
  331. continue;
  332. }
  333. auto *hold_mode = entity->get_component<Engine::Core::HoldModeComponent>();
  334. if ((hold_mode != nullptr) && hold_mode->active) {
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. auto CommandController::any_selected_in_guard_mode() const -> bool {
  341. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  342. return false;
  343. }
  344. const auto &selected = m_selection_system->get_selected_units();
  345. for (Engine::Core::EntityID const entity_id : selected) {
  346. Engine::Core::Entity *entity = m_world->get_entity(entity_id);
  347. if (entity == nullptr) {
  348. continue;
  349. }
  350. auto *guard_mode =
  351. entity->get_component<Engine::Core::GuardModeComponent>();
  352. if ((guard_mode != nullptr) && guard_mode->active) {
  353. return true;
  354. }
  355. }
  356. return false;
  357. }
  358. auto CommandController::on_guard_command() -> CommandResult {
  359. CommandResult result;
  360. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  361. return result;
  362. }
  363. const auto &selected = m_selection_system->get_selected_units();
  364. if (selected.empty()) {
  365. return result;
  366. }
  367. int eligible_count = 0;
  368. int guard_active_count = 0;
  369. for (auto id : selected) {
  370. auto *entity = m_world->get_entity(id);
  371. if (entity == nullptr) {
  372. continue;
  373. }
  374. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  375. if (unit == nullptr) {
  376. continue;
  377. }
  378. if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
  379. continue;
  380. }
  381. eligible_count++;
  382. auto *guard_mode =
  383. entity->get_component<Engine::Core::GuardModeComponent>();
  384. if ((guard_mode != nullptr) && guard_mode->active) {
  385. guard_active_count++;
  386. }
  387. }
  388. if (eligible_count == 0) {
  389. return result;
  390. }
  391. const bool should_enable_guard = (guard_active_count < eligible_count);
  392. for (auto id : selected) {
  393. auto *entity = m_world->get_entity(id);
  394. if (entity == nullptr) {
  395. continue;
  396. }
  397. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  398. if (unit == nullptr) {
  399. continue;
  400. }
  401. if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
  402. continue;
  403. }
  404. auto *guard_mode =
  405. entity->get_component<Engine::Core::GuardModeComponent>();
  406. if (should_enable_guard) {
  407. if (guard_mode == nullptr) {
  408. guard_mode = entity->add_component<Engine::Core::GuardModeComponent>();
  409. }
  410. guard_mode->active = true;
  411. guard_mode->returning_to_guard_position = false;
  412. auto *transform =
  413. entity->get_component<Engine::Core::TransformComponent>();
  414. if (transform != nullptr) {
  415. guard_mode->guard_position_x = transform->position.x;
  416. guard_mode->guard_position_z = transform->position.z;
  417. guard_mode->has_guard_target = true;
  418. guard_mode->guarded_entity_id = 0;
  419. }
  420. auto *hold_mode =
  421. entity->get_component<Engine::Core::HoldModeComponent>();
  422. if ((hold_mode != nullptr) && hold_mode->active) {
  423. hold_mode->active = false;
  424. }
  425. if (auto *patrol =
  426. entity->get_component<Engine::Core::PatrolComponent>()) {
  427. patrol->patrolling = false;
  428. patrol->waypoints.clear();
  429. }
  430. } else {
  431. if ((guard_mode != nullptr) && guard_mode->active) {
  432. guard_mode->active = false;
  433. guard_mode->guarded_entity_id = 0;
  434. guard_mode->guard_position_x = 0.0F;
  435. guard_mode->guard_position_z = 0.0F;
  436. guard_mode->returning_to_guard_position = false;
  437. guard_mode->has_guard_target = false;
  438. }
  439. }
  440. }
  441. emit guard_mode_changed(should_enable_guard);
  442. result.input_consumed = true;
  443. result.reset_cursor_to_normal = true;
  444. return result;
  445. }
  446. auto CommandController::on_guard_click(qreal sx, qreal sy, int viewport_width,
  447. int viewport_height,
  448. void *camera) -> CommandResult {
  449. CommandResult result;
  450. if ((m_selection_system == nullptr) || (m_picking_service == nullptr) ||
  451. (camera == nullptr) || (m_world == nullptr)) {
  452. result.reset_cursor_to_normal = true;
  453. return result;
  454. }
  455. const auto &selected = m_selection_system->get_selected_units();
  456. if (selected.empty()) {
  457. result.reset_cursor_to_normal = true;
  458. return result;
  459. }
  460. auto *cam = static_cast<Render::GL::Camera *>(camera);
  461. QVector3D hit;
  462. if (!Game::Systems::PickingService::screen_to_ground(
  463. QPointF(sx, sy), *cam, viewport_width, viewport_height, hit)) {
  464. result.reset_cursor_to_normal = true;
  465. return result;
  466. }
  467. for (auto id : selected) {
  468. auto *entity = m_world->get_entity(id);
  469. if (entity == nullptr) {
  470. continue;
  471. }
  472. auto *building = entity->get_component<Engine::Core::BuildingComponent>();
  473. if (building != nullptr) {
  474. continue;
  475. }
  476. auto *guard_mode =
  477. entity->get_component<Engine::Core::GuardModeComponent>();
  478. if (guard_mode == nullptr) {
  479. guard_mode = entity->add_component<Engine::Core::GuardModeComponent>();
  480. }
  481. guard_mode->active = true;
  482. guard_mode->guarded_entity_id = 0;
  483. guard_mode->guard_position_x = hit.x();
  484. guard_mode->guard_position_z = hit.z();
  485. guard_mode->returning_to_guard_position = false;
  486. guard_mode->has_guard_target = true;
  487. auto *hold_mode = entity->get_component<Engine::Core::HoldModeComponent>();
  488. if ((hold_mode != nullptr) && hold_mode->active) {
  489. hold_mode->active = false;
  490. }
  491. if (auto *patrol = entity->get_component<Engine::Core::PatrolComponent>()) {
  492. patrol->patrolling = false;
  493. patrol->waypoints.clear();
  494. }
  495. reset_movement(entity);
  496. entity->remove_component<Engine::Core::AttackTargetComponent>();
  497. }
  498. emit guard_mode_changed(true);
  499. result.input_consumed = true;
  500. result.reset_cursor_to_normal = true;
  501. return result;
  502. }
  503. auto CommandController::on_formation_command() -> CommandResult {
  504. CommandResult result;
  505. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  506. return result;
  507. }
  508. const auto &selected = m_selection_system->get_selected_units();
  509. if (selected.size() <= 1) {
  510. return result;
  511. }
  512. int eligible_count = 0;
  513. int formation_active_count = 0;
  514. for (auto id : selected) {
  515. auto *entity = m_world->get_entity(id);
  516. if (entity == nullptr) {
  517. continue;
  518. }
  519. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  520. if (unit == nullptr) {
  521. continue;
  522. }
  523. if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
  524. continue;
  525. }
  526. eligible_count++;
  527. auto *formation_mode =
  528. entity->get_component<Engine::Core::FormationModeComponent>();
  529. if ((formation_mode != nullptr) && formation_mode->active) {
  530. formation_active_count++;
  531. }
  532. }
  533. if (eligible_count <= 1) {
  534. return result;
  535. }
  536. const bool should_enable_formation =
  537. (formation_active_count < eligible_count);
  538. for (auto id : selected) {
  539. auto *entity = m_world->get_entity(id);
  540. if (entity == nullptr) {
  541. continue;
  542. }
  543. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  544. if (unit == nullptr) {
  545. continue;
  546. }
  547. if (unit->spawn_type == Game::Units::SpawnType::Barracks) {
  548. continue;
  549. }
  550. auto *formation_mode =
  551. entity->get_component<Engine::Core::FormationModeComponent>();
  552. if (should_enable_formation) {
  553. if (formation_mode == nullptr) {
  554. formation_mode =
  555. entity->add_component<Engine::Core::FormationModeComponent>();
  556. }
  557. formation_mode->active = true;
  558. auto *hold_mode =
  559. entity->get_component<Engine::Core::HoldModeComponent>();
  560. if ((hold_mode != nullptr) && hold_mode->active) {
  561. hold_mode->active = false;
  562. }
  563. auto *guard_mode =
  564. entity->get_component<Engine::Core::GuardModeComponent>();
  565. if ((guard_mode != nullptr) && guard_mode->active) {
  566. guard_mode->active = false;
  567. }
  568. if (auto *patrol =
  569. entity->get_component<Engine::Core::PatrolComponent>()) {
  570. patrol->patrolling = false;
  571. patrol->waypoints.clear();
  572. }
  573. } else {
  574. if ((formation_mode != nullptr) && formation_mode->active) {
  575. formation_mode->active = false;
  576. }
  577. }
  578. }
  579. if (should_enable_formation) {
  580. QVector3D center(0.0F, 0.0F, 0.0F);
  581. int valid_count = 0;
  582. m_formation_units.clear();
  583. for (auto id : selected) {
  584. auto *entity = m_world->get_entity(id);
  585. if (entity == nullptr) {
  586. continue;
  587. }
  588. auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  589. if (unit == nullptr ||
  590. unit->spawn_type == Game::Units::SpawnType::Barracks) {
  591. continue;
  592. }
  593. m_formation_units.push_back(id);
  594. auto *transform =
  595. entity->get_component<Engine::Core::TransformComponent>();
  596. if (transform != nullptr) {
  597. center.setX(center.x() + transform->position.x);
  598. center.setY(center.y() + transform->position.y);
  599. center.setZ(center.z() + transform->position.z);
  600. valid_count++;
  601. }
  602. }
  603. if (valid_count > 0) {
  604. center.setX(center.x() / static_cast<float>(valid_count));
  605. center.setY(center.y() / static_cast<float>(valid_count));
  606. center.setZ(center.z() / static_cast<float>(valid_count));
  607. m_is_placing_formation = true;
  608. m_formation_placement_position = center;
  609. m_formation_placement_angle = 0.0F;
  610. emit formation_placement_started();
  611. emit formation_placement_updated(m_formation_placement_position,
  612. m_formation_placement_angle);
  613. }
  614. }
  615. result.input_consumed = true;
  616. result.reset_cursor_to_normal = false;
  617. return result;
  618. }
  619. bool CommandController::any_selected_in_formation_mode() const {
  620. if ((m_selection_system == nullptr) || (m_world == nullptr)) {
  621. return false;
  622. }
  623. const auto &selected = m_selection_system->get_selected_units();
  624. for (auto id : selected) {
  625. auto *entity = m_world->get_entity(id);
  626. if (entity == nullptr) {
  627. continue;
  628. }
  629. auto *formation_mode =
  630. entity->get_component<Engine::Core::FormationModeComponent>();
  631. if ((formation_mode != nullptr) && formation_mode->active) {
  632. return true;
  633. }
  634. }
  635. return false;
  636. }
  637. void CommandController::update_formation_placement(const QVector3D &position) {
  638. if (!m_is_placing_formation) {
  639. return;
  640. }
  641. m_formation_placement_position = position;
  642. emit formation_placement_updated(m_formation_placement_position,
  643. m_formation_placement_angle);
  644. }
  645. void CommandController::update_formation_rotation(float angle_degrees) {
  646. if (!m_is_placing_formation) {
  647. return;
  648. }
  649. m_formation_placement_angle = angle_degrees;
  650. emit formation_placement_updated(m_formation_placement_position,
  651. m_formation_placement_angle);
  652. }
  653. void CommandController::confirm_formation_placement() {
  654. if (!m_is_placing_formation || m_formation_units.empty()) {
  655. cancel_formation_placement();
  656. return;
  657. }
  658. auto formation_result =
  659. Game::Systems::FormationPlanner::get_formation_with_facing(
  660. *m_world, m_formation_units, m_formation_placement_position,
  661. Game::GameConfig::instance().gameplay().formation_spacing_default);
  662. float const angle_rad =
  663. m_formation_placement_angle * std::numbers::pi_v<float> / 180.0F;
  664. float const cos_a = std::cos(angle_rad);
  665. float const sin_a = std::sin(angle_rad);
  666. for (size_t i = 0; i < m_formation_units.size(); ++i) {
  667. if (i < formation_result.positions.size()) {
  668. QVector3D &pos = formation_result.positions[i];
  669. float const dx = pos.x() - m_formation_placement_position.x();
  670. float const dz = pos.z() - m_formation_placement_position.z();
  671. float const rotated_x = dx * cos_a - dz * sin_a;
  672. float const rotated_z = dx * sin_a + dz * cos_a;
  673. pos.setX(m_formation_placement_position.x() + rotated_x);
  674. pos.setZ(m_formation_placement_position.z() + rotated_z);
  675. }
  676. auto *entity = m_world->get_entity(m_formation_units[i]);
  677. if (entity == nullptr) {
  678. continue;
  679. }
  680. auto *transform = entity->get_component<Engine::Core::TransformComponent>();
  681. if (transform != nullptr) {
  682. float unit_facing = (i < formation_result.facing_angles.size())
  683. ? formation_result.facing_angles[i]
  684. : 0.0F;
  685. transform->desired_yaw = unit_facing + m_formation_placement_angle;
  686. transform->has_desired_yaw = true;
  687. }
  688. }
  689. Game::Systems::CommandService::MoveOptions opts;
  690. opts.group_move = m_formation_units.size() > 1;
  691. opts.clear_attack_intent = true;
  692. Game::Systems::CommandService::move_units(*m_world, m_formation_units,
  693. formation_result.positions, opts);
  694. m_is_placing_formation = false;
  695. m_formation_units.clear();
  696. emit formation_placement_ended();
  697. emit formation_mode_changed(true);
  698. }
  699. void CommandController::cancel_formation_placement() {
  700. if (!m_is_placing_formation) {
  701. return;
  702. }
  703. for (auto id : m_formation_units) {
  704. auto *entity = m_world->get_entity(id);
  705. if (entity == nullptr) {
  706. continue;
  707. }
  708. auto *formation_mode =
  709. entity->get_component<Engine::Core::FormationModeComponent>();
  710. if (formation_mode != nullptr) {
  711. formation_mode->active = false;
  712. }
  713. }
  714. m_is_placing_formation = false;
  715. m_formation_units.clear();
  716. emit formation_placement_ended();
  717. emit formation_mode_changed(false);
  718. }
  719. auto CommandController::on_run_command() -> CommandResult {
  720. CommandResult result;
  721. if (m_selection_system == nullptr || m_world == nullptr) {
  722. return result;
  723. }
  724. const auto &selected = m_selection_system->get_selected_units();
  725. if (selected.empty()) {
  726. return result;
  727. }
  728. struct UnitRunState {
  729. Engine::Core::Entity *entity;
  730. Engine::Core::StaminaComponent *stamina;
  731. Game::Systems::NationID nation_id;
  732. Game::Units::SpawnType spawn_type;
  733. };
  734. std::vector<UnitRunState> eligible_units;
  735. eligible_units.reserve(selected.size());
  736. int run_active_count = 0;
  737. for (const auto id : selected) {
  738. auto *entity = m_world->get_entity(id);
  739. if (entity == nullptr) {
  740. continue;
  741. }
  742. const auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  743. if (unit == nullptr || !Game::Units::can_use_run_mode(unit->spawn_type)) {
  744. continue;
  745. }
  746. auto *stamina = entity->get_component<Engine::Core::StaminaComponent>();
  747. const bool is_active = stamina != nullptr && stamina->run_requested;
  748. run_active_count += is_active ? 1 : 0;
  749. eligible_units.push_back(
  750. {entity, stamina, unit->nation_id, unit->spawn_type});
  751. }
  752. if (eligible_units.empty()) {
  753. return result;
  754. }
  755. const bool should_enable_run =
  756. run_active_count < static_cast<int>(eligible_units.size());
  757. for (auto &[entity, stamina, nation_id, spawn_type] : eligible_units) {
  758. if (should_enable_run) {
  759. if (stamina == nullptr) {
  760. stamina = entity->add_component<Engine::Core::StaminaComponent>();
  761. const auto troop_type = Game::Units::spawn_typeToTroopType(spawn_type);
  762. if (troop_type.has_value()) {
  763. const auto profile =
  764. Game::Systems::TroopProfileService::instance().get_profile(
  765. nation_id, *troop_type);
  766. stamina->initialize_from_stats(profile.combat.max_stamina,
  767. profile.combat.stamina_regen_rate,
  768. profile.combat.stamina_depletion_rate);
  769. }
  770. }
  771. stamina->run_requested = true;
  772. } else if (stamina != nullptr) {
  773. stamina->run_requested = false;
  774. stamina->is_running = false;
  775. }
  776. }
  777. emit run_mode_changed(should_enable_run);
  778. result.input_consumed = true;
  779. result.reset_cursor_to_normal = true;
  780. return result;
  781. }
  782. auto CommandController::any_selected_in_run_mode() const -> bool {
  783. if (m_selection_system == nullptr || m_world == nullptr) {
  784. return false;
  785. }
  786. for (const auto id : m_selection_system->get_selected_units()) {
  787. const auto *entity = m_world->get_entity(id);
  788. if (entity == nullptr) {
  789. continue;
  790. }
  791. const auto *stamina =
  792. entity->get_component<Engine::Core::StaminaComponent>();
  793. if (stamina != nullptr && stamina->run_requested) {
  794. return true;
  795. }
  796. }
  797. return false;
  798. }
  799. void CommandController::enable_run_mode_for_selected() {
  800. if (m_selection_system == nullptr || m_world == nullptr) {
  801. return;
  802. }
  803. const auto &selected = m_selection_system->get_selected_units();
  804. if (selected.empty()) {
  805. return;
  806. }
  807. for (const auto id : selected) {
  808. auto *entity = m_world->get_entity(id);
  809. if (entity == nullptr) {
  810. continue;
  811. }
  812. const auto *unit = entity->get_component<Engine::Core::UnitComponent>();
  813. if (unit == nullptr || !Game::Units::can_use_run_mode(unit->spawn_type)) {
  814. continue;
  815. }
  816. auto *stamina = entity->get_component<Engine::Core::StaminaComponent>();
  817. if (stamina == nullptr) {
  818. stamina = entity->add_component<Engine::Core::StaminaComponent>();
  819. const auto troop_type =
  820. Game::Units::spawn_typeToTroopType(unit->spawn_type);
  821. if (troop_type.has_value()) {
  822. const auto profile =
  823. Game::Systems::TroopProfileService::instance().get_profile(
  824. unit->nation_id, *troop_type);
  825. stamina->initialize_from_stats(profile.combat.max_stamina,
  826. profile.combat.stamina_regen_rate,
  827. profile.combat.stamina_depletion_rate);
  828. }
  829. }
  830. stamina->run_requested = true;
  831. }
  832. emit run_mode_changed(true);
  833. }
  834. void CommandController::disable_run_mode_for_selected() {
  835. if (m_selection_system == nullptr || m_world == nullptr) {
  836. return;
  837. }
  838. const auto &selected = m_selection_system->get_selected_units();
  839. if (selected.empty()) {
  840. return;
  841. }
  842. for (const auto id : selected) {
  843. auto *entity = m_world->get_entity(id);
  844. if (entity == nullptr) {
  845. continue;
  846. }
  847. auto *stamina = entity->get_component<Engine::Core::StaminaComponent>();
  848. if (stamina != nullptr) {
  849. stamina->run_requested = false;
  850. stamina->is_running = false;
  851. }
  852. }
  853. emit run_mode_changed(false);
  854. }
  855. } // namespace App::Controllers