StartInfo.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "vstd/DateUtils.h"
  12. #include "GameConstants.h"
  13. #include "TurnTimerInfo.h"
  14. #include "ExtraOptionsInfo.h"
  15. #include "campaign/CampaignConstants.h"
  16. #include "serializer/Serializeable.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CMapGenOptions;
  19. class CampaignState;
  20. class CMapInfo;
  21. struct PlayerInfo;
  22. class PlayerColor;
  23. struct DLL_LINKAGE SimturnsInfo
  24. {
  25. /// Minimal number of turns that must be played simultaneously even if contact has been detected
  26. int requiredTurns = 0;
  27. /// Maximum number of turns that might be played simultaneously unless contact is detected
  28. int optionalTurns = 0;
  29. /// If set to true, human and 1 AI can act at the same time
  30. bool allowHumanWithAI = false;
  31. bool operator == (const SimturnsInfo & other) const
  32. {
  33. return requiredTurns == other.requiredTurns &&
  34. optionalTurns == other.optionalTurns &&
  35. allowHumanWithAI == other.allowHumanWithAI;
  36. }
  37. template <typename Handler>
  38. void serialize(Handler &h)
  39. {
  40. h & requiredTurns;
  41. h & optionalTurns;
  42. h & allowHumanWithAI;
  43. }
  44. };
  45. enum class PlayerStartingBonus : int8_t
  46. {
  47. RANDOM = -1,
  48. ARTIFACT = 0,
  49. GOLD = 1,
  50. RESOURCE = 2
  51. };
  52. /// Struct which describes the name, the color, the starting bonus of a player
  53. struct DLL_LINKAGE PlayerSettings
  54. {
  55. enum { PLAYER_AI = 0 }; // for use in playerID
  56. PlayerStartingBonus bonus;
  57. FactionID castle;
  58. HeroTypeID hero;
  59. HeroTypeID heroPortrait; //-1 if default, else ID
  60. std::string heroNameTextId;
  61. PlayerColor color; //from 0 -
  62. enum EHandicap {NO_HANDICAP, MILD, SEVERE};
  63. EHandicap handicap;//0-no, 1-mild, 2-severe
  64. std::string name;
  65. std::set<ui8> connectedPlayerIDs; //Empty - AI, or connectrd player ids
  66. bool compOnly; //true if this player is a computer only player; required for RMG
  67. template <typename Handler>
  68. void serialize(Handler &h)
  69. {
  70. h & castle;
  71. h & hero;
  72. h & heroPortrait;
  73. h & heroNameTextId;
  74. h & bonus;
  75. h & color;
  76. h & handicap;
  77. h & name;
  78. h & connectedPlayerIDs;
  79. h & compOnly;
  80. }
  81. PlayerSettings();
  82. bool isControlledByAI() const;
  83. bool isControlledByHuman() const;
  84. FactionID getCastleValidated() const;
  85. HeroTypeID getHeroValidated() const;
  86. };
  87. enum class EStartMode : int32_t
  88. {
  89. NEW_GAME,
  90. LOAD_GAME,
  91. CAMPAIGN,
  92. INVALID = 255
  93. };
  94. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  95. struct DLL_LINKAGE StartInfo : public Serializeable
  96. {
  97. EStartMode mode;
  98. ui8 difficulty; //0=easy; 4=impossible
  99. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  100. TPlayerInfos playerInfos; //color indexed
  101. ui32 seedToBeUsed; //0 if not sure (client requests server to decide, will be send in reply pack)
  102. ui32 seedPostInit; //so we know that game is correctly synced at the start; 0 if not known yet
  103. ui32 mapfileChecksum; //0 if not relevant
  104. std::string startTimeIso8601;
  105. std::string fileURI;
  106. SimturnsInfo simturnsInfo;
  107. TurnTimerInfo turnTimerInfo;
  108. ExtraOptionsInfo extraOptionsInfo;
  109. std::string mapname; // empty for random map, otherwise name of the map or savegame
  110. bool createRandomMap() const { return mapGenOptions != nullptr; }
  111. std::shared_ptr<CMapGenOptions> mapGenOptions;
  112. std::shared_ptr<CampaignState> campState;
  113. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  114. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  115. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  116. // TODO: Must be client-side
  117. std::string getCampaignName() const;
  118. /// Controls hardcoded check for "Steadwick's Fall" scenario from "Dungeon and Devils" campaign
  119. bool isSteadwickFallCampaignMission() const;
  120. template <typename Handler>
  121. void serialize(Handler &h)
  122. {
  123. h & mode;
  124. h & difficulty;
  125. h & playerInfos;
  126. h & seedToBeUsed;
  127. h & seedPostInit;
  128. h & mapfileChecksum;
  129. h & startTimeIso8601;
  130. h & fileURI;
  131. h & simturnsInfo;
  132. h & turnTimerInfo;
  133. if(h.version >= Handler::Version::HAS_EXTRA_OPTIONS)
  134. h & extraOptionsInfo;
  135. else
  136. extraOptionsInfo = ExtraOptionsInfo();
  137. h & mapname;
  138. h & mapGenOptions;
  139. h & campState;
  140. }
  141. StartInfo() : mode(EStartMode::INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0),
  142. mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI("")
  143. {
  144. }
  145. };
  146. struct ClientPlayer
  147. {
  148. int connection;
  149. std::string name;
  150. template <typename Handler> void serialize(Handler &h)
  151. {
  152. h & connection;
  153. h & name;
  154. }
  155. };
  156. struct DLL_LINKAGE LobbyState
  157. {
  158. std::shared_ptr<StartInfo> si;
  159. std::shared_ptr<CMapInfo> mi;
  160. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  161. int hostClientId;
  162. // TODO: Campaign-only and we don't really need either of them.
  163. // Before start both go into CCampaignState that is part of StartInfo
  164. CampaignScenarioID campaignMap;
  165. int campaignBonus;
  166. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  167. template <typename Handler> void serialize(Handler &h)
  168. {
  169. h & si;
  170. h & mi;
  171. h & playerNames;
  172. h & hostClientId;
  173. h & campaignMap;
  174. h & campaignBonus;
  175. }
  176. };
  177. struct DLL_LINKAGE LobbyInfo : public LobbyState
  178. {
  179. std::string uuid;
  180. LobbyInfo() {}
  181. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  182. bool isClientHost(int clientId) const;
  183. bool isPlayerHost(const PlayerColor & color) const;
  184. std::set<PlayerColor> getAllClientPlayers(int clientId) const;
  185. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  186. // Helpers for lobby state access
  187. std::set<PlayerColor> clientHumanColors(int clientId);
  188. PlayerColor clientFirstColor(int clientId) const;
  189. bool isClientColor(int clientId, const PlayerColor & color) const;
  190. ui8 clientFirstId(int clientId) const; // Used by chat only!
  191. PlayerInfo & getPlayerInfo(PlayerColor color);
  192. TeamID getPlayerTeamId(const PlayerColor & color);
  193. };
  194. VCMI_LIB_NAMESPACE_END