StartInfo.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. struct 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. h & connectedPlayerIDs;
  53. h & team;
  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. typedef std::map<PlayerColor, PlayerSettings> TPlayerInfos;
  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.get() != nullptr; }
  74. std::shared_ptr<CMapGenOptions> mapGenOptions;
  75. std::shared_ptr<CCampaignState> campState;
  76. PlayerSettings & getIthPlayersSettings(PlayerColor no);
  77. const PlayerSettings & getIthPlayersSettings(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(0), 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. int campaignMap;
  119. int campaignBonus;
  120. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(-1), 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, PlayerColor color) const;
  145. ui8 clientFirstId(int clientId) const; // Used by chat only!
  146. PlayerInfo & getPlayerInfo(int color);
  147. TeamID getPlayerTeamId(PlayerColor color);
  148. };