StartInfo.cpp 5.5 KB

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