StartInfo.h 4.7 KB

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