StartInfo.h 6.4 KB

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