StartInfo.h 4.7 KB

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