nation_registry.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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::getMeleeTroops() const -> std::vector<const TroopType *> {
  16. std::vector<const TroopType *> result;
  17. for (const auto &troop : availableTroops) {
  18. if (troop.isMelee) {
  19. result.push_back(&troop);
  20. }
  21. }
  22. return result;
  23. }
  24. auto Nation::getRangedTroops() const -> std::vector<const TroopType *> {
  25. std::vector<const TroopType *> result;
  26. for (const auto &troop : availableTroops) {
  27. if (!troop.isMelee) {
  28. result.push_back(&troop);
  29. }
  30. }
  31. return result;
  32. }
  33. auto Nation::getTroop(Game::Units::TroopType unit_type) const
  34. -> const TroopType * {
  35. for (const auto &troop : availableTroops) {
  36. if (troop.unit_type == unit_type) {
  37. return &troop;
  38. }
  39. }
  40. return nullptr;
  41. }
  42. auto Nation::getBestMeleeTroop() const -> const TroopType * {
  43. auto melee = getMeleeTroops();
  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::getBestRangedTroop() const -> const TroopType * {
  54. auto ranged = getRangedTroops();
  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::isMeleeUnit(Game::Units::TroopType unit_type) const -> bool {
  65. const auto *troop = getTroop(unit_type);
  66. return troop != nullptr && troop->isMelee;
  67. }
  68. auto Nation::is_ranged_unit(Game::Units::TroopType unit_type) const -> bool {
  69. const auto *troop = getTroop(unit_type);
  70. return troop != nullptr && !troop->isMelee;
  71. }
  72. auto NationRegistry::instance() -> NationRegistry & {
  73. static NationRegistry inst;
  74. return inst;
  75. }
  76. void NationRegistry::registerNation(Nation nation) {
  77. auto it = m_nationIndex.find(nation.id);
  78. if (it != m_nationIndex.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_nationIndex[m_nations.back().id] = index;
  85. }
  86. auto NationRegistry::getNation(const std::string &nationId) const
  87. -> const Nation * {
  88. auto it = m_nationIndex.find(nationId);
  89. if (it == m_nationIndex.end()) {
  90. return nullptr;
  91. }
  92. return &m_nations[it->second];
  93. }
  94. auto NationRegistry::getNationForPlayer(int player_id) const -> const Nation * {
  95. auto it = m_playerNations.find(player_id);
  96. if (it != m_playerNations.end()) {
  97. const auto *nation = getNation(it->second);
  98. return nation;
  99. }
  100. const auto *nation = getNation(m_defaultNation);
  101. if (nation == nullptr) {
  102. }
  103. return nation;
  104. }
  105. void NationRegistry::setPlayerNation(int player_id,
  106. const std::string &nationId) {
  107. m_playerNations[player_id] = nationId;
  108. }
  109. void NationRegistry::initializeDefaults() {
  110. if (m_initialized) {
  111. return;
  112. }
  113. clear();
  114. Game::Units::TroopCatalogLoader::load_default_catalog();
  115. auto nations = NationLoader::load_default_nations();
  116. if (nations.empty()) {
  117. Nation kingdom_of_iron;
  118. kingdom_of_iron.id = "kingdom_of_iron";
  119. kingdom_of_iron.displayName = "Kingdom of Iron";
  120. kingdom_of_iron.primaryBuilding = "barracks";
  121. kingdom_of_iron.formation_type = FormationType::Roman;
  122. auto appendTroop = [&kingdom_of_iron](Game::Units::TroopType type) {
  123. TroopType troop_entry;
  124. troop_entry.unit_type = type;
  125. const auto &troop_class =
  126. Game::Units::TroopCatalog::instance().get_class_or_fallback(type);
  127. troop_entry.displayName = troop_class.display_name;
  128. troop_entry.isMelee = troop_class.production.is_melee;
  129. troop_entry.cost = troop_class.production.cost;
  130. troop_entry.buildTime = troop_class.production.build_time;
  131. troop_entry.priority = troop_class.production.priority;
  132. kingdom_of_iron.availableTroops.push_back(std::move(troop_entry));
  133. };
  134. appendTroop(Game::Units::TroopType::Archer);
  135. appendTroop(Game::Units::TroopType::Swordsman);
  136. appendTroop(Game::Units::TroopType::Spearman);
  137. appendTroop(Game::Units::TroopType::MountedKnight);
  138. registerNation(std::move(kingdom_of_iron));
  139. m_defaultNation = "kingdom_of_iron";
  140. } else {
  141. const std::string desired_default = "kingdom_of_iron";
  142. std::string fallback_default = nations.front().id;
  143. for (auto &nation : nations) {
  144. if (nation.id == desired_default) {
  145. fallback_default = nation.id;
  146. }
  147. registerNation(std::move(nation));
  148. }
  149. m_defaultNation = fallback_default;
  150. }
  151. TroopProfileService::instance().clear();
  152. m_initialized = true;
  153. }
  154. void NationRegistry::clear() {
  155. m_nations.clear();
  156. m_nationIndex.clear();
  157. m_playerNations.clear();
  158. m_initialized = false;
  159. }
  160. void NationRegistry::clearPlayerAssignments() { m_playerNations.clear(); }
  161. } // namespace Game::Systems