StartInfo.h 4.8 KB

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