StartInfo.h 4.9 KB

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