StartInfo.cpp 6.3 KB

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