StartInfo.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = true;
  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. /// Struct which describes the name, the color, the starting bonus of a player
  38. struct DLL_LINKAGE PlayerSettings
  39. {
  40. enum { PLAYER_AI = 0 }; // for use in playerID
  41. enum Ebonus {
  42. NONE = -2,
  43. RANDOM = -1,
  44. ARTIFACT = 0,
  45. GOLD = 1,
  46. RESOURCE = 2
  47. };
  48. Ebonus 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. };
  77. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  78. struct DLL_LINKAGE StartInfo
  79. {
  80. enum EMode {NEW_GAME, LOAD_GAME, CAMPAIGN, INVALID = 255};
  81. EMode mode;
  82. ui8 difficulty; //0=easy; 4=impossible
  83. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  84. TPlayerInfos playerInfos; //color indexed
  85. ui32 seedToBeUsed; //0 if not sure (client requests server to decide, will be send in reply pack)
  86. ui32 seedPostInit; //so we know that game is correctly synced at the start; 0 if not known yet
  87. ui32 mapfileChecksum; //0 if not relevant
  88. std::string startTimeIso8601;
  89. SimturnsInfo simturnsInfo;
  90. TurnTimerInfo turnTimerInfo;
  91. std::string mapname; // empty for random map, otherwise name of the map or savegame
  92. bool createRandomMap() const { return mapGenOptions != nullptr; }
  93. std::shared_ptr<CMapGenOptions> mapGenOptions;
  94. std::shared_ptr<CampaignState> campState;
  95. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  96. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  97. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  98. // TODO: Must be client-side
  99. std::string getCampaignName() const;
  100. template <typename Handler>
  101. void serialize(Handler &h, const int version)
  102. {
  103. h & mode;
  104. h & difficulty;
  105. h & playerInfos;
  106. h & seedToBeUsed;
  107. h & seedPostInit;
  108. h & mapfileChecksum;
  109. h & startTimeIso8601;
  110. h & simturnsInfo;
  111. h & turnTimerInfo;
  112. h & mapname;
  113. h & mapGenOptions;
  114. h & campState;
  115. }
  116. StartInfo() : mode(INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0),
  117. mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(0)))
  118. {
  119. }
  120. };
  121. struct ClientPlayer
  122. {
  123. int connection;
  124. std::string name;
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & connection;
  128. h & name;
  129. }
  130. };
  131. struct DLL_LINKAGE LobbyState
  132. {
  133. std::shared_ptr<StartInfo> si;
  134. std::shared_ptr<CMapInfo> mi;
  135. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  136. int hostClientId;
  137. // TODO: Campaign-only and we don't really need either of them.
  138. // Before start both go into CCampaignState that is part of StartInfo
  139. CampaignScenarioID campaignMap;
  140. int campaignBonus;
  141. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & si;
  145. h & mi;
  146. h & playerNames;
  147. h & hostClientId;
  148. h & campaignMap;
  149. h & campaignBonus;
  150. }
  151. };
  152. struct DLL_LINKAGE LobbyInfo : public LobbyState
  153. {
  154. boost::mutex stateMutex;
  155. std::string uuid;
  156. LobbyInfo() {}
  157. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  158. bool isClientHost(int clientId) const;
  159. std::set<PlayerColor> getAllClientPlayers(int clientId);
  160. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  161. // Helpers for lobby state access
  162. std::set<PlayerColor> clientHumanColors(int clientId);
  163. PlayerColor clientFirstColor(int clientId) const;
  164. bool isClientColor(int clientId, const PlayerColor & color) const;
  165. ui8 clientFirstId(int clientId) const; // Used by chat only!
  166. PlayerInfo & getPlayerInfo(int color);
  167. TeamID getPlayerTeamId(const PlayerColor & color);
  168. };
  169. VCMI_LIB_NAMESPACE_END