StartInfo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "texts/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. bool StartInfo::isSteadwickFallCampaignMission() const
  79. {
  80. if (!campState)
  81. return false;
  82. if (campState->getFilename() != "DATA/EVIL1")
  83. return false;
  84. if (campState->currentScenario() != CampaignScenarioID(2))
  85. return false;
  86. return true;
  87. }
  88. void LobbyInfo::verifyStateBeforeStart(bool ignoreNoHuman) const
  89. {
  90. if(!mi || !mi->mapHeader)
  91. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.529"));
  92. auto missingMods = CMapService::verifyMapHeaderMods(*mi->mapHeader);
  93. ModIncompatibility::ModList modList;
  94. for(const auto & m : missingMods)
  95. modList.push_back(m.second.name);
  96. if(!modList.empty())
  97. throw ModIncompatibility(modList);
  98. //there must be at least one human player before game can be started
  99. std::map<PlayerColor, PlayerSettings>::const_iterator i;
  100. for(i = si->playerInfos.cbegin(); i != si->playerInfos.cend(); i++)
  101. if(i->second.isControlledByHuman())
  102. break;
  103. if(i == si->playerInfos.cend() && !ignoreNoHuman)
  104. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.530"));
  105. if(si->mapGenOptions && si->mode == EStartMode::NEW_GAME)
  106. {
  107. if(!si->mapGenOptions->checkOptions())
  108. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.751"));
  109. }
  110. }
  111. bool LobbyInfo::isClientHost(int clientId) const
  112. {
  113. return clientId == hostClientId;
  114. }
  115. bool LobbyInfo::isPlayerHost(const PlayerColor & color) const
  116. {
  117. return vstd::contains(getAllClientPlayers(hostClientId), color);
  118. }
  119. std::set<PlayerColor> LobbyInfo::getAllClientPlayers(int clientId) const
  120. {
  121. std::set<PlayerColor> players;
  122. for(auto & elem : si->playerInfos)
  123. {
  124. if(isClientHost(clientId) && elem.second.isControlledByAI())
  125. players.insert(elem.first);
  126. for(ui8 id : elem.second.connectedPlayerIDs)
  127. {
  128. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  129. players.insert(elem.first);
  130. }
  131. }
  132. if(isClientHost(clientId))
  133. players.insert(PlayerColor::NEUTRAL);
  134. return players;
  135. }
  136. std::vector<ui8> LobbyInfo::getConnectedPlayerIdsForClient(int clientId) const
  137. {
  138. std::vector<ui8> ids;
  139. for(const auto & pair : playerNames)
  140. {
  141. if(pair.second.connection == clientId)
  142. {
  143. for(auto & elem : si->playerInfos)
  144. {
  145. if(vstd::contains(elem.second.connectedPlayerIDs, pair.first))
  146. ids.push_back(pair.first);
  147. }
  148. }
  149. }
  150. return ids;
  151. }
  152. std::set<PlayerColor> LobbyInfo::clientHumanColors(int clientId)
  153. {
  154. std::set<PlayerColor> players;
  155. for(auto & elem : si->playerInfos)
  156. {
  157. for(ui8 id : elem.second.connectedPlayerIDs)
  158. {
  159. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  160. {
  161. players.insert(elem.first);
  162. }
  163. }
  164. }
  165. return players;
  166. }
  167. PlayerColor LobbyInfo::clientFirstColor(int clientId) const
  168. {
  169. for(auto & pair : si->playerInfos)
  170. {
  171. if(isClientColor(clientId, pair.first))
  172. return pair.first;
  173. }
  174. return PlayerColor::CANNOT_DETERMINE;
  175. }
  176. bool LobbyInfo::isClientColor(int clientId, const PlayerColor & color) const
  177. {
  178. if(si->playerInfos.find(color) != si->playerInfos.end())
  179. {
  180. for(ui8 id : si->playerInfos.find(color)->second.connectedPlayerIDs)
  181. {
  182. if(playerNames.find(id) != playerNames.end())
  183. {
  184. if(playerNames.find(id)->second.connection == clientId)
  185. return true;
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. ui8 LobbyInfo::clientFirstId(int clientId) const
  192. {
  193. for(const auto & pair : playerNames)
  194. {
  195. if(pair.second.connection == clientId)
  196. return pair.first;
  197. }
  198. return 0;
  199. }
  200. PlayerInfo & LobbyInfo::getPlayerInfo(PlayerColor color)
  201. {
  202. return mi->mapHeader->players[color.getNum()];
  203. }
  204. TeamID LobbyInfo::getPlayerTeamId(const PlayerColor & color)
  205. {
  206. if(color.isValidPlayer())
  207. return getPlayerInfo(color).team;
  208. else
  209. return TeamID::NO_TEAM;
  210. }
  211. VCMI_LIB_NAMESPACE_END