StartInfo.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * StartInfo.h, 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. #pragma once
  11. #include "vstd/DateUtils.h"
  12. #include "GameConstants.h"
  13. #include "TurnTimerInfo.h"
  14. #include "campaign/CampaignConstants.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CMapGenOptions;
  17. class CampaignState;
  18. class CMapInfo;
  19. struct PlayerInfo;
  20. class PlayerColor;
  21. struct DLL_LINKAGE SimturnsInfo
  22. {
  23. /// Minimal number of turns that must be played simultaneously even if contact has been detected
  24. int requiredTurns = 0;
  25. /// Maximum number of turns that might be played simultaneously unless contact is detected
  26. int optionalTurns = 0;
  27. /// If set to true, human and 1 AI can act at the same time
  28. bool allowHumanWithAI = false;
  29. template <typename Handler>
  30. void serialize(Handler &h, const int version)
  31. {
  32. h & requiredTurns;
  33. h & optionalTurns;
  34. h & allowHumanWithAI;
  35. }
  36. };
  37. enum class PlayerStartingBonus : int8_t
  38. {
  39. RANDOM = -1,
  40. ARTIFACT = 0,
  41. GOLD = 1,
  42. RESOURCE = 2
  43. };
  44. /// Struct which describes the name, the color, the starting bonus of a player
  45. struct DLL_LINKAGE PlayerSettings
  46. {
  47. enum { PLAYER_AI = 0 }; // for use in playerID
  48. PlayerStartingBonus bonus;
  49. FactionID castle;
  50. HeroTypeID hero;
  51. HeroTypeID heroPortrait; //-1 if default, else ID
  52. std::string heroNameTextId;
  53. PlayerColor color; //from 0 -
  54. enum EHandicap {NO_HANDICAP, MILD, SEVERE};
  55. EHandicap handicap;//0-no, 1-mild, 2-severe
  56. std::string name;
  57. std::set<ui8> connectedPlayerIDs; //Empty - AI, or connectrd player ids
  58. bool compOnly; //true if this player is a computer only player; required for RMG
  59. template <typename Handler>
  60. void serialize(Handler &h, const int version)
  61. {
  62. h & castle;
  63. h & hero;
  64. h & heroPortrait;
  65. h & heroNameTextId;
  66. h & bonus;
  67. h & color;
  68. h & handicap;
  69. h & name;
  70. h & connectedPlayerIDs;
  71. h & compOnly;
  72. }
  73. PlayerSettings();
  74. bool isControlledByAI() const;
  75. bool isControlledByHuman() const;
  76. FactionID getCastleValidated() const;
  77. HeroTypeID getHeroValidated() const;
  78. };
  79. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  80. struct DLL_LINKAGE StartInfo
  81. {
  82. enum EMode {NEW_GAME, LOAD_GAME, CAMPAIGN, INVALID = 255};
  83. EMode mode;
  84. ui8 difficulty; //0=easy; 4=impossible
  85. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  86. TPlayerInfos playerInfos; //color indexed
  87. ui32 seedToBeUsed; //0 if not sure (client requests server to decide, will be send in reply pack)
  88. ui32 seedPostInit; //so we know that game is correctly synced at the start; 0 if not known yet
  89. ui32 mapfileChecksum; //0 if not relevant
  90. std::string startTimeIso8601;
  91. std::string fileURI;
  92. SimturnsInfo simturnsInfo;
  93. TurnTimerInfo turnTimerInfo;
  94. std::string mapname; // empty for random map, otherwise name of the map or savegame
  95. bool createRandomMap() const { return mapGenOptions != nullptr; }
  96. std::shared_ptr<CMapGenOptions> mapGenOptions;
  97. std::shared_ptr<CampaignState> campState;
  98. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  99. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  100. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  101. // TODO: Must be client-side
  102. std::string getCampaignName() const;
  103. template <typename Handler>
  104. void serialize(Handler &h, const int version)
  105. {
  106. h & mode;
  107. h & difficulty;
  108. h & playerInfos;
  109. h & seedToBeUsed;
  110. h & seedPostInit;
  111. h & mapfileChecksum;
  112. h & startTimeIso8601;
  113. h & fileURI;
  114. h & simturnsInfo;
  115. h & turnTimerInfo;
  116. h & mapname;
  117. h & mapGenOptions;
  118. h & campState;
  119. }
  120. StartInfo() : mode(INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0),
  121. mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI("")
  122. {
  123. }
  124. };
  125. struct ClientPlayer
  126. {
  127. int connection;
  128. std::string name;
  129. template <typename Handler> void serialize(Handler &h, const int version)
  130. {
  131. h & connection;
  132. h & name;
  133. }
  134. };
  135. struct DLL_LINKAGE LobbyState
  136. {
  137. std::shared_ptr<StartInfo> si;
  138. std::shared_ptr<CMapInfo> mi;
  139. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  140. int hostClientId;
  141. // TODO: Campaign-only and we don't really need either of them.
  142. // Before start both go into CCampaignState that is part of StartInfo
  143. CampaignScenarioID campaignMap;
  144. int campaignBonus;
  145. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  146. template <typename Handler> void serialize(Handler &h, const int version)
  147. {
  148. h & si;
  149. h & mi;
  150. h & playerNames;
  151. h & hostClientId;
  152. h & campaignMap;
  153. h & campaignBonus;
  154. }
  155. };
  156. struct DLL_LINKAGE LobbyInfo : public LobbyState
  157. {
  158. boost::mutex stateMutex;
  159. std::string uuid;
  160. LobbyInfo() {}
  161. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  162. bool isClientHost(int clientId) const;
  163. std::set<PlayerColor> getAllClientPlayers(int clientId);
  164. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  165. // Helpers for lobby state access
  166. std::set<PlayerColor> clientHumanColors(int clientId);
  167. PlayerColor clientFirstColor(int clientId) const;
  168. bool isClientColor(int clientId, const PlayerColor & color) const;
  169. ui8 clientFirstId(int clientId) const; // Used by chat only!
  170. PlayerInfo & getPlayerInfo(PlayerColor color);
  171. TeamID getPlayerTeamId(const PlayerColor & color);
  172. };
  173. VCMI_LIB_NAMESPACE_END