StartInfo.h 6.3 KB

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