StartInfo.cpp 4.3 KB

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