StartInfo.h 6.1 KB

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