StartInfo.h 4.9 KB

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