StartInfo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * StartInfo.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "StartInfo.h"
  12. #include "texts/CGeneralTextHandler.h"
  13. #include "GameLibrary.h"
  14. #include "entities/faction/CFaction.h"
  15. #include "entities/faction/CTownHandler.h"
  16. #include "entities/hero/CHeroHandler.h"
  17. #include "rmg/CMapGenOptions.h"
  18. #include "mapping/CMapInfo.h"
  19. #include "campaign/CampaignState.h"
  20. #include "mapping/CMapHeader.h"
  21. #include "mapping/CMapService.h"
  22. #include "modding/ModIncompatibility.h"
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. PlayerSettings::PlayerSettings()
  25. : bonus(PlayerStartingBonus::RANDOM), color(0), compOnly(false)
  26. {
  27. }
  28. FactionID PlayerSettings::getCastleValidated() const
  29. {
  30. if (!castle.isValid())
  31. return FactionID(0);
  32. if (castle.getNum() < LIBRARY->townh->size() && castle.toEntity(LIBRARY)->hasTown())
  33. return castle;
  34. return FactionID(0);
  35. }
  36. HeroTypeID PlayerSettings::getHeroValidated() const
  37. {
  38. if (!hero.isValid())
  39. return HeroTypeID(0);
  40. if (hero.getNum() < LIBRARY->heroh->size())
  41. return hero;
  42. return HeroTypeID(0);
  43. }
  44. bool PlayerSettings::isControlledByAI() const
  45. {
  46. return connectedPlayerIDs.empty();
  47. }
  48. bool PlayerSettings::isControlledByHuman() const
  49. {
  50. return !connectedPlayerIDs.empty();
  51. }
  52. PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no)
  53. {
  54. if(playerInfos.find(no) != playerInfos.end())
  55. return playerInfos[no];
  56. logGlobal->error("Cannot find info about player %s. Throwing...", no.toString());
  57. throw std::runtime_error("Cannot find info about player");
  58. }
  59. const PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no) const
  60. {
  61. return const_cast<StartInfo &>(*this).getIthPlayersSettings(no);
  62. }
  63. PlayerSettings * StartInfo::getPlayersSettings(const ui8 connectedPlayerId)
  64. {
  65. for(auto & elem : playerInfos)
  66. {
  67. if(vstd::contains(elem.second.connectedPlayerIDs, connectedPlayerId))
  68. return &elem.second;
  69. }
  70. return nullptr;
  71. }
  72. std::string StartInfo::getCampaignName() const
  73. {
  74. if(!campState->getNameTranslated().empty())
  75. return campState->getNameTranslated();
  76. else
  77. return LIBRARY->generaltexth->allTexts[508];
  78. }
  79. bool StartInfo::restrictedGarrisonsForAI() const
  80. {
  81. return campState && campState->restrictedGarrisonsForAI();
  82. }
  83. void LobbyInfo::verifyStateBeforeStart(bool ignoreNoHuman) const
  84. {
  85. if(!mi || !mi->mapHeader)
  86. throw std::domain_error(LIBRARY->generaltexth->translate("core.genrltxt.529"));
  87. auto missingMods = CMapService::verifyMapHeaderMods(*mi->mapHeader);
  88. ModIncompatibility::ModList modList;
  89. for(const auto & m : missingMods)
  90. modList.push_back(m.second.name);
  91. if(!modList.empty())
  92. throw ModIncompatibility(modList);
  93. //there must be at least one human player before game can be started
  94. std::map<PlayerColor, PlayerSettings>::const_iterator i;
  95. for(i = si->playerInfos.cbegin(); i != si->playerInfos.cend(); i++)
  96. if(i->second.isControlledByHuman())
  97. break;
  98. if(i == si->playerInfos.cend() && !ignoreNoHuman)
  99. throw std::domain_error(LIBRARY->generaltexth->translate("core.genrltxt.530"));
  100. if(si->mapGenOptions && si->mode == EStartMode::NEW_GAME)
  101. {
  102. if(!si->mapGenOptions->checkOptions())
  103. throw std::domain_error(LIBRARY->generaltexth->translate("core.genrltxt.751"));
  104. }
  105. }
  106. bool LobbyInfo::isClientHost(int clientId) const
  107. {
  108. return clientId == hostClientId;
  109. }
  110. bool LobbyInfo::isPlayerHost(const PlayerColor & color) const
  111. {
  112. return vstd::contains(getAllClientPlayers(hostClientId), color);
  113. }
  114. std::set<PlayerColor> LobbyInfo::getAllClientPlayers(int clientId) const
  115. {
  116. std::set<PlayerColor> players;
  117. for(auto & elem : si->playerInfos)
  118. {
  119. if(isClientHost(clientId) && elem.second.isControlledByAI())
  120. players.insert(elem.first);
  121. for(ui8 id : elem.second.connectedPlayerIDs)
  122. {
  123. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  124. players.insert(elem.first);
  125. }
  126. }
  127. if(isClientHost(clientId))
  128. players.insert(PlayerColor::NEUTRAL);
  129. return players;
  130. }
  131. std::vector<ui8> LobbyInfo::getConnectedPlayerIdsForClient(int clientId) const
  132. {
  133. std::vector<ui8> ids;
  134. for(const auto & pair : playerNames)
  135. {
  136. if(pair.second.connection == clientId)
  137. {
  138. for(auto & elem : si->playerInfos)
  139. {
  140. if(vstd::contains(elem.second.connectedPlayerIDs, pair.first))
  141. ids.push_back(pair.first);
  142. }
  143. }
  144. }
  145. return ids;
  146. }
  147. std::set<PlayerColor> LobbyInfo::clientHumanColors(int clientId)
  148. {
  149. std::set<PlayerColor> players;
  150. for(auto & elem : si->playerInfos)
  151. {
  152. for(ui8 id : elem.second.connectedPlayerIDs)
  153. {
  154. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  155. {
  156. players.insert(elem.first);
  157. }
  158. }
  159. }
  160. return players;
  161. }
  162. PlayerColor LobbyInfo::clientFirstColor(int clientId) const
  163. {
  164. for(auto & pair : si->playerInfos)
  165. {
  166. if(isClientColor(clientId, pair.first))
  167. return pair.first;
  168. }
  169. return PlayerColor::CANNOT_DETERMINE;
  170. }
  171. bool LobbyInfo::isClientColor(int clientId, const PlayerColor & color) const
  172. {
  173. if(si->playerInfos.find(color) != si->playerInfos.end())
  174. {
  175. for(ui8 id : si->playerInfos.find(color)->second.connectedPlayerIDs)
  176. {
  177. if(playerNames.find(id) != playerNames.end())
  178. {
  179. if(playerNames.find(id)->second.connection == clientId)
  180. return true;
  181. }
  182. }
  183. }
  184. return false;
  185. }
  186. ui8 LobbyInfo::clientFirstId(int clientId) const
  187. {
  188. for(const auto & pair : playerNames)
  189. {
  190. if(pair.second.connection == clientId)
  191. return pair.first;
  192. }
  193. return 0;
  194. }
  195. PlayerInfo & LobbyInfo::getPlayerInfo(PlayerColor color)
  196. {
  197. return mi->mapHeader->players[color.getNum()];
  198. }
  199. TeamID LobbyInfo::getPlayerTeamId(const PlayerColor & color)
  200. {
  201. if(color.isValidPlayer())
  202. return getPlayerInfo(color).team;
  203. else
  204. return TeamID::NO_TEAM;
  205. }
  206. VCMI_LIB_NAMESPACE_END