StartInfo.h 5.6 KB

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