StartInfo.h 4.7 KB

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