StartInfo.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. std::string fileURI;
  90. SimturnsInfo simturnsInfo;
  91. TurnTimerInfo turnTimerInfo;
  92. std::string mapname; // empty for random map, otherwise name of the map or savegame
  93. bool createRandomMap() const { return mapGenOptions != nullptr; }
  94. std::shared_ptr<CMapGenOptions> mapGenOptions;
  95. std::shared_ptr<CampaignState> campState;
  96. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  97. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  98. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  99. // TODO: Must be client-side
  100. std::string getCampaignName() const;
  101. template <typename Handler>
  102. void serialize(Handler &h, const int version)
  103. {
  104. h & mode;
  105. h & difficulty;
  106. h & playerInfos;
  107. h & seedToBeUsed;
  108. h & seedPostInit;
  109. h & mapfileChecksum;
  110. h & startTimeIso8601;
  111. h & fileURI;
  112. h & simturnsInfo;
  113. h & turnTimerInfo;
  114. h & mapname;
  115. h & mapGenOptions;
  116. h & campState;
  117. }
  118. StartInfo() : mode(INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0),
  119. mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(0))), fileURI("")
  120. {
  121. }
  122. };
  123. struct ClientPlayer
  124. {
  125. int connection;
  126. std::string name;
  127. template <typename Handler> void serialize(Handler &h, const int version)
  128. {
  129. h & connection;
  130. h & name;
  131. }
  132. };
  133. struct DLL_LINKAGE LobbyState
  134. {
  135. std::shared_ptr<StartInfo> si;
  136. std::shared_ptr<CMapInfo> mi;
  137. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  138. int hostClientId;
  139. // TODO: Campaign-only and we don't really need either of them.
  140. // Before start both go into CCampaignState that is part of StartInfo
  141. CampaignScenarioID campaignMap;
  142. int campaignBonus;
  143. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & si;
  147. h & mi;
  148. h & playerNames;
  149. h & hostClientId;
  150. h & campaignMap;
  151. h & campaignBonus;
  152. }
  153. };
  154. struct DLL_LINKAGE LobbyInfo : public LobbyState
  155. {
  156. boost::mutex stateMutex;
  157. std::string uuid;
  158. LobbyInfo() {}
  159. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  160. bool isClientHost(int clientId) const;
  161. std::set<PlayerColor> getAllClientPlayers(int clientId);
  162. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  163. // Helpers for lobby state access
  164. std::set<PlayerColor> clientHumanColors(int clientId);
  165. PlayerColor clientFirstColor(int clientId) const;
  166. bool isClientColor(int clientId, const PlayerColor & color) const;
  167. ui8 clientFirstId(int clientId) const; // Used by chat only!
  168. PlayerInfo & getPlayerInfo(int color);
  169. TeamID getPlayerTeamId(const PlayerColor & color);
  170. };
  171. VCMI_LIB_NAMESPACE_END