StartInfo.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, const int version)
  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, const int version)
  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. enum class EStartMode : int32_t
  87. {
  88. NEW_GAME,
  89. LOAD_GAME,
  90. CAMPAIGN,
  91. INVALID = 255
  92. };
  93. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  94. struct DLL_LINKAGE StartInfo
  95. {
  96. EStartMode mode;
  97. ui8 difficulty; //0=easy; 4=impossible
  98. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  99. TPlayerInfos playerInfos; //color indexed
  100. ui32 seedToBeUsed; //0 if not sure (client requests server to decide, will be send in reply pack)
  101. ui32 seedPostInit; //so we know that game is correctly synced at the start; 0 if not known yet
  102. ui32 mapfileChecksum; //0 if not relevant
  103. std::string startTimeIso8601;
  104. std::string fileURI;
  105. SimturnsInfo simturnsInfo;
  106. TurnTimerInfo turnTimerInfo;
  107. ExtraOptionsInfo extraOptionsInfo;
  108. std::string mapname; // empty for random map, otherwise name of the map or savegame
  109. bool createRandomMap() const { return mapGenOptions != nullptr; }
  110. std::shared_ptr<CMapGenOptions> mapGenOptions;
  111. std::shared_ptr<CampaignState> campState;
  112. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  113. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  114. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  115. // TODO: Must be client-side
  116. std::string getCampaignName() const;
  117. template <typename Handler>
  118. void serialize(Handler &h, const int version)
  119. {
  120. h & mode;
  121. h & difficulty;
  122. h & playerInfos;
  123. h & seedToBeUsed;
  124. h & seedPostInit;
  125. h & mapfileChecksum;
  126. h & startTimeIso8601;
  127. h & fileURI;
  128. h & simturnsInfo;
  129. h & turnTimerInfo;
  130. if(version >= 832)
  131. h & extraOptionsInfo;
  132. else
  133. extraOptionsInfo = ExtraOptionsInfo();
  134. h & mapname;
  135. h & mapGenOptions;
  136. h & campState;
  137. }
  138. StartInfo() : mode(EStartMode::INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0),
  139. mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI("")
  140. {
  141. }
  142. };
  143. struct ClientPlayer
  144. {
  145. int connection;
  146. std::string name;
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & connection;
  150. h & name;
  151. }
  152. };
  153. struct DLL_LINKAGE LobbyState
  154. {
  155. std::shared_ptr<StartInfo> si;
  156. std::shared_ptr<CMapInfo> mi;
  157. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  158. int hostClientId;
  159. // TODO: Campaign-only and we don't really need either of them.
  160. // Before start both go into CCampaignState that is part of StartInfo
  161. CampaignScenarioID campaignMap;
  162. int campaignBonus;
  163. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  164. template <typename Handler> void serialize(Handler &h, const int version)
  165. {
  166. h & si;
  167. h & mi;
  168. h & playerNames;
  169. h & hostClientId;
  170. h & campaignMap;
  171. h & campaignBonus;
  172. }
  173. };
  174. struct DLL_LINKAGE LobbyInfo : public LobbyState
  175. {
  176. std::string uuid;
  177. LobbyInfo() {}
  178. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  179. bool isClientHost(int clientId) const;
  180. bool isPlayerHost(const PlayerColor & color) const;
  181. std::set<PlayerColor> getAllClientPlayers(int clientId) const;
  182. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  183. // Helpers for lobby state access
  184. std::set<PlayerColor> clientHumanColors(int clientId);
  185. PlayerColor clientFirstColor(int clientId) const;
  186. bool isClientColor(int clientId, const PlayerColor & color) const;
  187. ui8 clientFirstId(int clientId) const; // Used by chat only!
  188. PlayerInfo & getPlayerInfo(PlayerColor color);
  189. TeamID getPlayerTeamId(const PlayerColor & color);
  190. };
  191. VCMI_LIB_NAMESPACE_END