StartInfo.cpp 4.5 KB

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