StartInfo.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 DLL_LINKAGE Handicap {
  60. TResources startBonus = TResources();
  61. int percentIncome = 100;
  62. int percentGrowth = 100;
  63. template <typename Handler>
  64. void serialize(Handler &h)
  65. {
  66. h & startBonus;
  67. h & percentIncome;
  68. h & percentGrowth;
  69. }
  70. };
  71. /// Struct which describes the name, the color, the starting bonus of a player
  72. struct DLL_LINKAGE PlayerSettings
  73. {
  74. enum { PLAYER_AI = 0 }; // for use in playerID
  75. PlayerStartingBonus bonus;
  76. FactionID castle;
  77. HeroTypeID hero;
  78. HeroTypeID heroPortrait; //-1 if default, else ID
  79. std::string heroNameTextId;
  80. PlayerColor color; //from 0 -
  81. Handicap handicap;
  82. std::string name;
  83. std::set<ui8> connectedPlayerIDs; //Empty - AI, or connectrd player ids
  84. bool compOnly; //true if this player is a computer only player; required for RMG
  85. template <typename Handler>
  86. void serialize(Handler &h)
  87. {
  88. h & castle;
  89. h & hero;
  90. h & heroPortrait;
  91. h & heroNameTextId;
  92. h & bonus;
  93. h & color;
  94. if (h.version >= Handler::Version::PLAYER_HANDICAP)
  95. h & handicap;
  96. else
  97. {
  98. enum EHandicap {NO_HANDICAP, MILD, SEVERE};
  99. EHandicap handicapLegacy;
  100. h & handicapLegacy;
  101. }
  102. h & name;
  103. h & connectedPlayerIDs;
  104. h & compOnly;
  105. }
  106. PlayerSettings();
  107. bool isControlledByAI() const;
  108. bool isControlledByHuman() const;
  109. FactionID getCastleValidated() const;
  110. HeroTypeID getHeroValidated() const;
  111. };
  112. enum class EStartMode : int32_t
  113. {
  114. NEW_GAME,
  115. LOAD_GAME,
  116. CAMPAIGN,
  117. INVALID = 255
  118. };
  119. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  120. struct DLL_LINKAGE StartInfo : public Serializeable
  121. {
  122. EStartMode mode;
  123. ui8 difficulty; //0=easy; 4=impossible
  124. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  125. TPlayerInfos playerInfos; //color indexed
  126. std::string startTimeIso8601;
  127. std::string fileURI;
  128. SimturnsInfo simturnsInfo;
  129. TurnTimerInfo turnTimerInfo;
  130. ExtraOptionsInfo extraOptionsInfo;
  131. std::string mapname; // empty for random map, otherwise name of the map or savegame
  132. bool createRandomMap() const { return mapGenOptions != nullptr; }
  133. std::shared_ptr<CMapGenOptions> mapGenOptions;
  134. std::shared_ptr<CampaignState> campState;
  135. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  136. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  137. PlayerSettings * getPlayersSettings(const ui8 connectedPlayerId);
  138. // TODO: Must be client-side
  139. std::string getCampaignName() const;
  140. /// Controls hardcoded check for "Steadwick's Fall" scenario from "Dungeon and Devils" campaign
  141. bool isSteadwickFallCampaignMission() const;
  142. template <typename Handler>
  143. void serialize(Handler &h)
  144. {
  145. h & mode;
  146. h & difficulty;
  147. h & playerInfos;
  148. if (h.version < Handler::Version::REMOVE_LIB_RNG)
  149. {
  150. uint32_t oldSeeds = 0;
  151. h & oldSeeds;
  152. h & oldSeeds;
  153. h & oldSeeds;
  154. }
  155. h & startTimeIso8601;
  156. h & fileURI;
  157. h & simturnsInfo;
  158. h & turnTimerInfo;
  159. if(h.version >= Handler::Version::HAS_EXTRA_OPTIONS)
  160. h & extraOptionsInfo;
  161. else
  162. extraOptionsInfo = ExtraOptionsInfo();
  163. h & mapname;
  164. h & mapGenOptions;
  165. h & campState;
  166. }
  167. StartInfo()
  168. : mode(EStartMode::INVALID)
  169. , difficulty(1)
  170. , startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr)))
  171. {
  172. }
  173. };
  174. struct ClientPlayer
  175. {
  176. int connection;
  177. std::string name;
  178. template <typename Handler> void serialize(Handler &h)
  179. {
  180. h & connection;
  181. h & name;
  182. }
  183. };
  184. struct DLL_LINKAGE LobbyState
  185. {
  186. std::shared_ptr<StartInfo> si;
  187. std::shared_ptr<CMapInfo> mi;
  188. std::map<ui8, ClientPlayer> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  189. int hostClientId;
  190. // TODO: Campaign-only and we don't really need either of them.
  191. // Before start both go into CCampaignState that is part of StartInfo
  192. CampaignScenarioID campaignMap;
  193. int campaignBonus;
  194. LobbyState() : si(new StartInfo()), hostClientId(-1), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  195. template <typename Handler> void serialize(Handler &h)
  196. {
  197. h & si;
  198. h & mi;
  199. h & playerNames;
  200. h & hostClientId;
  201. h & campaignMap;
  202. h & campaignBonus;
  203. }
  204. };
  205. struct DLL_LINKAGE LobbyInfo : public LobbyState
  206. {
  207. std::string uuid;
  208. LobbyInfo() {}
  209. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  210. bool isClientHost(int clientId) const;
  211. bool isPlayerHost(const PlayerColor & color) const;
  212. std::set<PlayerColor> getAllClientPlayers(int clientId) const;
  213. std::vector<ui8> getConnectedPlayerIdsForClient(int clientId) const;
  214. // Helpers for lobby state access
  215. std::set<PlayerColor> clientHumanColors(int clientId);
  216. PlayerColor clientFirstColor(int clientId) const;
  217. bool isClientColor(int clientId, const PlayerColor & color) const;
  218. ui8 clientFirstId(int clientId) const; // Used by chat only!
  219. PlayerInfo & getPlayerInfo(PlayerColor color);
  220. TeamID getPlayerTeamId(const PlayerColor & color);
  221. };
  222. VCMI_LIB_NAMESPACE_END