StartInfo.cpp 4.8 KB

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