StartInfo.h 6.3 KB

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