StartInfo.cpp 4.9 KB

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