nation_registry.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "nation_registry.h"
  2. #include "systems/formation_system.h"
  3. #include "systems/nation_loader.h"
  4. #include "systems/troop_profile_service.h"
  5. #include "units/troop_catalog.h"
  6. #include "units/troop_catalog_loader.h"
  7. #include "units/troop_type.h"
  8. #include <QDebug>
  9. #include <algorithm>
  10. #include <cstddef>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. namespace Game::Systems {
  15. auto Nation::get_melee_troops() const -> std::vector<const TroopType *> {
  16. std::vector<const TroopType *> result;
  17. for (const auto &troop : available_troops) {
  18. if (troop.is_melee) {
  19. result.push_back(&troop);
  20. }
  21. }
  22. return result;
  23. }
  24. auto Nation::get_ranged_troops() const -> std::vector<const TroopType *> {
  25. std::vector<const TroopType *> result;
  26. for (const auto &troop : available_troops) {
  27. if (!troop.is_melee) {
  28. result.push_back(&troop);
  29. }
  30. }
  31. return result;
  32. }
  33. auto Nation::get_troop(Game::Units::TroopType unit_type) const
  34. -> const TroopType * {
  35. for (const auto &troop : available_troops) {
  36. if (troop.unit_type == unit_type) {
  37. return &troop;
  38. }
  39. }
  40. return nullptr;
  41. }
  42. auto Nation::get_best_melee_troop() const -> const TroopType * {
  43. auto melee = get_melee_troops();
  44. if (melee.empty()) {
  45. return nullptr;
  46. }
  47. auto it = std::max_element(melee.begin(), melee.end(),
  48. [](const TroopType *a, const TroopType *b) {
  49. return a->priority < b->priority;
  50. });
  51. return *it;
  52. }
  53. auto Nation::get_best_ranged_troop() const -> const TroopType * {
  54. auto ranged = get_ranged_troops();
  55. if (ranged.empty()) {
  56. return nullptr;
  57. }
  58. auto it = std::max_element(ranged.begin(), ranged.end(),
  59. [](const TroopType *a, const TroopType *b) {
  60. return a->priority < b->priority;
  61. });
  62. return *it;
  63. }
  64. auto Nation::is_melee_unit(Game::Units::TroopType unit_type) const -> bool {
  65. const auto *troop = get_troop(unit_type);
  66. return troop != nullptr && troop->is_melee;
  67. }
  68. auto Nation::is_ranged_unit(Game::Units::TroopType unit_type) const -> bool {
  69. const auto *troop = get_troop(unit_type);
  70. return troop != nullptr && !troop->is_melee;
  71. }
  72. auto NationRegistry::instance() -> NationRegistry & {
  73. static NationRegistry inst;
  74. return inst;
  75. }
  76. void NationRegistry::register_nation(Nation nation) {
  77. auto it = m_nation_index.find(nation.id);
  78. if (it != m_nation_index.end()) {
  79. m_nations[it->second] = std::move(nation);
  80. return;
  81. }
  82. size_t const index = m_nations.size();
  83. m_nations.push_back(std::move(nation));
  84. m_nation_index[m_nations.back().id] = index;
  85. }
  86. auto NationRegistry::get_nation(NationID nation_id) const -> const Nation * {
  87. auto it = m_nation_index.find(nation_id);
  88. if (it == m_nation_index.end()) {
  89. return nullptr;
  90. }
  91. return &m_nations[it->second];
  92. }
  93. auto NationRegistry::get_nation_for_player(int player_id) const
  94. -> const Nation * {
  95. auto it = m_player_nations.find(player_id);
  96. if (it != m_player_nations.end()) {
  97. const auto *nation = get_nation(it->second);
  98. return nation;
  99. }
  100. const auto *nation = get_nation(m_default_nation);
  101. if (nation == nullptr) {
  102. }
  103. return nation;
  104. }
  105. void NationRegistry::set_player_nation(int player_id, NationID nation_id) {
  106. m_player_nations[player_id] = nation_id;
  107. }
  108. void NationRegistry::initialize_defaults() {
  109. if (m_initialized) {
  110. return;
  111. }
  112. clear();
  113. Game::Units::TroopCatalogLoader::load_default_catalog();
  114. auto nations = NationLoader::load_default_nations();
  115. if (nations.empty()) {
  116. Nation roman;
  117. roman.id = NationID::RomanRepublic;
  118. roman.display_name = "Roman Republic";
  119. roman.primary_building = Game::Units::BuildingType::Barracks;
  120. roman.formation_type = FormationType::Roman;
  121. auto append_troop = [&roman](Game::Units::TroopType type) {
  122. TroopType troop_entry;
  123. troop_entry.unit_type = type;
  124. const auto &troop_class =
  125. Game::Units::TroopCatalog::instance().get_class_or_fallback(type);
  126. troop_entry.display_name = troop_class.display_name;
  127. troop_entry.is_melee = troop_class.production.is_melee;
  128. troop_entry.cost = troop_class.production.cost;
  129. troop_entry.build_time = troop_class.production.build_time;
  130. troop_entry.priority = troop_class.production.priority;
  131. roman.available_troops.push_back(std::move(troop_entry));
  132. };
  133. append_troop(Game::Units::TroopType::Archer);
  134. append_troop(Game::Units::TroopType::Swordsman);
  135. append_troop(Game::Units::TroopType::Spearman);
  136. append_troop(Game::Units::TroopType::MountedKnight);
  137. register_nation(std::move(roman));
  138. m_default_nation = NationID::RomanRepublic;
  139. } else {
  140. NationID fallback_default = nations.front().id;
  141. for (auto &nation : nations) {
  142. register_nation(std::move(nation));
  143. }
  144. m_default_nation = fallback_default;
  145. }
  146. TroopProfileService::instance().clear();
  147. m_initialized = true;
  148. }
  149. void NationRegistry::clear() {
  150. m_nations.clear();
  151. m_nation_index.clear();
  152. m_player_nations.clear();
  153. m_initialized = false;
  154. }
  155. void NationRegistry::clear_player_assignments() { m_player_nations.clear(); }
  156. } // namespace Game::Systems