StartInfo.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "VCMI_Lib.h"
  14. #include "entities/faction/CFaction.h"
  15. #include "entities/faction/CTownHandler.h"
  16. #include "entities/hero/CHeroHandler.h"
  17. #include "rmg/CMapGenOptions.h"
  18. #include "mapping/CMapInfo.h"
  19. #include "campaign/CampaignState.h"
  20. #include "mapping/CMapHeader.h"
  21. #include "mapping/CMapService.h"
  22. #include "modding/ModIncompatibility.h"
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. PlayerSettings::PlayerSettings()
  25. : bonus(PlayerStartingBonus::RANDOM), color(0), compOnly(false)
  26. {
  27. }
  28. FactionID PlayerSettings::getCastleValidated() const
  29. {
  30. if (!castle.isValid())
  31. return FactionID(0);
  32. if (castle.getNum() < VLC->townh->size() && castle.toEntity(VLC)->hasTown())
  33. return castle;
  34. return FactionID(0);
  35. }
  36. HeroTypeID PlayerSettings::getHeroValidated() const
  37. {
  38. if (!hero.isValid())
  39. return HeroTypeID(0);
  40. if (hero.getNum() < VLC->heroh->size())
  41. return hero;
  42. return HeroTypeID(0);
  43. }
  44. bool PlayerSettings::isControlledByAI() const
  45. {
  46. return connectedPlayerIDs.empty();
  47. }
  48. bool PlayerSettings::isControlledByHuman() const
  49. {
  50. return !connectedPlayerIDs.empty();
  51. }
  52. PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no)
  53. {
  54. if(playerInfos.find(no) != playerInfos.end())
  55. return playerInfos[no];
  56. logGlobal->error("Cannot find info about player %s. Throwing...", no.toString());
  57. throw std::runtime_error("Cannot find info about player");
  58. }
  59. const PlayerSettings & StartInfo::getIthPlayersSettings(const PlayerColor & no) const
  60. {
  61. return const_cast<StartInfo &>(*this).getIthPlayersSettings(no);
  62. }
  63. PlayerSettings * StartInfo::getPlayersSettings(const ui8 connectedPlayerId)
  64. {
  65. for(auto & elem : playerInfos)
  66. {
  67. if(vstd::contains(elem.second.connectedPlayerIDs, connectedPlayerId))
  68. return &elem.second;
  69. }
  70. return nullptr;
  71. }
  72. std::string StartInfo::getCampaignName() const
  73. {
  74. if(!campState->getNameTranslated().empty())
  75. return campState->getNameTranslated();
  76. else
  77. return VLC->generaltexth->allTexts[508];
  78. }
  79. bool StartInfo::isSteadwickFallCampaignMission() const
  80. {
  81. if (!campState)
  82. return false;
  83. if (campState->getFilename() != "DATA/EVIL1")
  84. return false;
  85. if (campState->currentScenario() != CampaignScenarioID(2))
  86. return false;
  87. return true;
  88. }
  89. void LobbyInfo::verifyStateBeforeStart(bool ignoreNoHuman) const
  90. {
  91. if(!mi || !mi->mapHeader)
  92. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.529"));
  93. auto missingMods = CMapService::verifyMapHeaderMods(*mi->mapHeader);
  94. ModIncompatibility::ModList modList;
  95. for(const auto & m : missingMods)
  96. modList.push_back(m.second.name);
  97. if(!modList.empty())
  98. throw ModIncompatibility(modList);
  99. //there must be at least one human player before game can be started
  100. std::map<PlayerColor, PlayerSettings>::const_iterator i;
  101. for(i = si->playerInfos.cbegin(); i != si->playerInfos.cend(); i++)
  102. if(i->second.isControlledByHuman())
  103. break;
  104. if(i == si->playerInfos.cend() && !ignoreNoHuman)
  105. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.530"));
  106. if(si->mapGenOptions && si->mode == EStartMode::NEW_GAME)
  107. {
  108. if(!si->mapGenOptions->checkOptions())
  109. throw std::domain_error(VLC->generaltexth->translate("core.genrltxt.751"));
  110. }
  111. }
  112. bool LobbyInfo::isClientHost(int clientId) const
  113. {
  114. return clientId == hostClientId;
  115. }
  116. bool LobbyInfo::isPlayerHost(const PlayerColor & color) const
  117. {
  118. return vstd::contains(getAllClientPlayers(hostClientId), color);
  119. }
  120. std::set<PlayerColor> LobbyInfo::getAllClientPlayers(int clientId) const
  121. {
  122. std::set<PlayerColor> players;
  123. for(auto & elem : si->playerInfos)
  124. {
  125. if(isClientHost(clientId) && elem.second.isControlledByAI())
  126. players.insert(elem.first);
  127. for(ui8 id : elem.second.connectedPlayerIDs)
  128. {
  129. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  130. players.insert(elem.first);
  131. }
  132. }
  133. if(isClientHost(clientId))
  134. players.insert(PlayerColor::NEUTRAL);
  135. return players;
  136. }
  137. std::vector<ui8> LobbyInfo::getConnectedPlayerIdsForClient(int clientId) const
  138. {
  139. std::vector<ui8> ids;
  140. for(const auto & pair : playerNames)
  141. {
  142. if(pair.second.connection == clientId)
  143. {
  144. for(auto & elem : si->playerInfos)
  145. {
  146. if(vstd::contains(elem.second.connectedPlayerIDs, pair.first))
  147. ids.push_back(pair.first);
  148. }
  149. }
  150. }
  151. return ids;
  152. }
  153. std::set<PlayerColor> LobbyInfo::clientHumanColors(int clientId)
  154. {
  155. std::set<PlayerColor> players;
  156. for(auto & elem : si->playerInfos)
  157. {
  158. for(ui8 id : elem.second.connectedPlayerIDs)
  159. {
  160. if(vstd::contains(getConnectedPlayerIdsForClient(clientId), id))
  161. {
  162. players.insert(elem.first);
  163. }
  164. }
  165. }
  166. return players;
  167. }
  168. PlayerColor LobbyInfo::clientFirstColor(int clientId) const
  169. {
  170. for(auto & pair : si->playerInfos)
  171. {
  172. if(isClientColor(clientId, pair.first))
  173. return pair.first;
  174. }
  175. return PlayerColor::CANNOT_DETERMINE;
  176. }
  177. bool LobbyInfo::isClientColor(int clientId, const PlayerColor & color) const
  178. {
  179. if(si->playerInfos.find(color) != si->playerInfos.end())
  180. {
  181. for(ui8 id : si->playerInfos.find(color)->second.connectedPlayerIDs)
  182. {
  183. if(playerNames.find(id) != playerNames.end())
  184. {
  185. if(playerNames.find(id)->second.connection == clientId)
  186. return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. ui8 LobbyInfo::clientFirstId(int clientId) const
  193. {
  194. for(const auto & pair : playerNames)
  195. {
  196. if(pair.second.connection == clientId)
  197. return pair.first;
  198. }
  199. return 0;
  200. }
  201. PlayerInfo & LobbyInfo::getPlayerInfo(PlayerColor color)
  202. {
  203. return mi->mapHeader->players[color.getNum()];
  204. }
  205. TeamID LobbyInfo::getPlayerTeamId(const PlayerColor & color)
  206. {
  207. if(color.isValidPlayer())
  208. return getPlayerInfo(color).team;
  209. else
  210. return TeamID::NO_TEAM;
  211. }
  212. VCMI_LIB_NAMESPACE_END