StartInfo.h 5.7 KB

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