StartInfo.h 4.7 KB

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