CMap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * CMap.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 "../ConstTransitivePtr.h"
  12. #include "../mapObjects/MiscObjects.h" // To serialize static props
  13. #include "../mapObjects/CQuest.h" // To serialize static props
  14. #include "../mapObjects/CGTownInstance.h" // To serialize static props
  15. #include "../ResourceSet.h"
  16. #include "../int3.h"
  17. #include "../GameConstants.h"
  18. #include "../LogicalExpression.h"
  19. #include "../CModHandler.h"
  20. #include "CMapDefines.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class CArtifactInstance;
  23. class CGObjectInstance;
  24. class CGHeroInstance;
  25. class CCommanderInstance;
  26. class CGCreature;
  27. class CQuest;
  28. class CGTownInstance;
  29. class IModableArt;
  30. class IQuestObject;
  31. class CInputStream;
  32. class CMapEditManager;
  33. /// The hero name struct consists of the hero id and the hero name.
  34. struct DLL_LINKAGE SHeroName
  35. {
  36. SHeroName();
  37. int heroId;
  38. std::string heroName;
  39. template <typename Handler>
  40. void serialize(Handler & h, const int version)
  41. {
  42. h & heroId;
  43. h & heroName;
  44. }
  45. };
  46. /// The player info constains data about which factions are allowed, AI tactical settings,
  47. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  48. struct DLL_LINKAGE PlayerInfo
  49. {
  50. PlayerInfo();
  51. /// Gets the default faction id or -1 for a random faction.
  52. si8 defaultCastle() const;
  53. /// Gets the default hero id or -1 for a random hero.
  54. si8 defaultHero() const;
  55. bool canAnyonePlay() const;
  56. bool hasCustomMainHero() const;
  57. bool canHumanPlay;
  58. bool canComputerPlay;
  59. EAiTactic::EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  60. std::set<FactionID> allowedFactions;
  61. bool isFactionRandom;
  62. ///main hero instance (VCMI maps only)
  63. std::string mainHeroInstance;
  64. /// Player has a random main hero
  65. bool hasRandomHero;
  66. /// The default value is -1.
  67. si32 mainCustomHeroPortrait;
  68. std::string mainCustomHeroName;
  69. /// ID of custom hero (only if portrait and hero name are set, otherwise unpredicted value), -1 if none (not always -1)
  70. si32 mainCustomHeroId;
  71. std::vector<SHeroName> heroesNames; /// list of placed heroes on the map
  72. bool hasMainTown; /// The default value is false.
  73. bool generateHeroAtMainTown; /// The default value is false.
  74. int3 posOfMainTown;
  75. TeamID team; /// The default value NO_TEAM
  76. template <typename Handler>
  77. void serialize(Handler & h, const int version)
  78. {
  79. h & hasRandomHero;
  80. h & mainCustomHeroId;
  81. h & canHumanPlay;
  82. h & canComputerPlay;
  83. h & aiTactic;
  84. h & allowedFactions;
  85. h & isFactionRandom;
  86. h & mainCustomHeroPortrait;
  87. h & mainCustomHeroName;
  88. h & heroesNames;
  89. h & hasMainTown;
  90. h & generateHeroAtMainTown;
  91. h & posOfMainTown;
  92. h & team;
  93. h & mainHeroInstance;
  94. }
  95. };
  96. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  97. struct DLL_LINKAGE EventCondition
  98. {
  99. enum EWinLoseType {
  100. //internal use, deprecated
  101. HAVE_ARTIFACT, // type - required artifact
  102. HAVE_CREATURES, // type - creatures to collect, value - amount to collect
  103. HAVE_RESOURCES, // type - resource ID, value - amount to collect
  104. HAVE_BUILDING, // position - town, optional, type - building to build
  105. CONTROL, // position - position of object, optional, type - type of object
  106. DESTROY, // position - position of object, optional, type - type of object
  107. TRANSPORT, // position - where artifact should be transported, type - type of artifact
  108. //map format version pre 1.0
  109. DAYS_PASSED, // value - number of days from start of the game
  110. IS_HUMAN, // value - 0 = player is AI, 1 = player is human
  111. DAYS_WITHOUT_TOWN, // value - how long player can live without town, 0=instakill
  112. STANDARD_WIN, // normal defeat all enemies condition
  113. CONST_VALUE, // condition that always evaluates to "value" (0 = false, 1 = true)
  114. //map format version 1.0+
  115. HAVE_0,
  116. HAVE_BUILDING_0,
  117. DESTROY_0
  118. };
  119. EventCondition(EWinLoseType condition = STANDARD_WIN);
  120. EventCondition(EWinLoseType condition, si32 value, si32 objectType, const int3 & position = int3(-1, -1, -1));
  121. const CGObjectInstance * object; // object that was at specified position or with instance name on start
  122. EMetaclass metaType;
  123. si32 value;
  124. si32 objectType;
  125. si32 objectSubtype;
  126. std::string objectInstanceName;
  127. int3 position;
  128. EWinLoseType condition;
  129. template <typename Handler>
  130. void serialize(Handler & h, const int version)
  131. {
  132. h & object;
  133. h & value;
  134. h & objectType;
  135. h & position;
  136. h & condition;
  137. h & objectSubtype;
  138. h & objectInstanceName;
  139. h & metaType;
  140. }
  141. };
  142. using EventExpression = LogicalExpression<EventCondition>;
  143. struct DLL_LINKAGE EventEffect
  144. {
  145. enum EType
  146. {
  147. VICTORY,
  148. DEFEAT
  149. };
  150. /// effect type, using EType enum
  151. si8 type;
  152. /// message that will be sent to other players
  153. std::string toOtherMessage;
  154. template <typename Handler>
  155. void serialize(Handler & h, const int version)
  156. {
  157. h & type;
  158. h & toOtherMessage;
  159. }
  160. };
  161. struct DLL_LINKAGE TriggeredEvent
  162. {
  163. /// base condition that must be evaluated
  164. EventExpression trigger;
  165. /// string identifier read from config file (e.g. captureKreelah)
  166. std::string identifier;
  167. /// string-description, for use in UI (capture town to win)
  168. std::string description;
  169. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  170. std::string onFulfill;
  171. /// Effect of this event. TODO: refactor into something more flexible
  172. EventEffect effect;
  173. template <typename Handler>
  174. void serialize(Handler & h, const int version)
  175. {
  176. h & identifier;
  177. h & trigger;
  178. h & description;
  179. h & onFulfill;
  180. h & effect;
  181. }
  182. };
  183. /// The rumor struct consists of a rumor name and text.
  184. struct DLL_LINKAGE Rumor
  185. {
  186. std::string name;
  187. std::string text;
  188. Rumor() = default;
  189. ~Rumor() = default;
  190. template <typename Handler>
  191. void serialize(Handler & h, const int version)
  192. {
  193. h & name;
  194. h & text;
  195. }
  196. void serializeJson(JsonSerializeFormat & handler);
  197. };
  198. /// The disposed hero struct describes which hero can be hired from which player.
  199. struct DLL_LINKAGE DisposedHero
  200. {
  201. DisposedHero();
  202. ui32 heroId;
  203. ui32 portrait; /// The portrait id of the hero, -1 is default.
  204. std::string name;
  205. ui8 players; /// Who can hire this hero (bitfield).
  206. template <typename Handler>
  207. void serialize(Handler & h, const int version)
  208. {
  209. h & heroId;
  210. h & portrait;
  211. h & name;
  212. h & players;
  213. }
  214. };
  215. enum class EMapFormat: uint8_t
  216. {
  217. INVALID = 0,
  218. // HEX DEC
  219. ROE = 0x0e, // 14
  220. AB = 0x15, // 21
  221. SOD = 0x1c, // 28
  222. // CHR = 0x1d, // 29 Heroes Chronicles, presumably - identical to SoD, untested
  223. HOTA = 0x20, // 32
  224. WOG = 0x33, // 51
  225. VCMI = 0x64
  226. };
  227. // Inherit from container to enable forward declaration
  228. class ModCompatibilityInfo: public std::map<TModID, CModInfo::Version>
  229. {};
  230. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  231. class DLL_LINKAGE CMapHeader
  232. {
  233. void setupEvents();
  234. public:
  235. static const int MAP_SIZE_SMALL = 36;
  236. static const int MAP_SIZE_MIDDLE = 72;
  237. static const int MAP_SIZE_LARGE = 108;
  238. static const int MAP_SIZE_XLARGE = 144;
  239. static const int MAP_SIZE_HUGE = 180;
  240. static const int MAP_SIZE_XHUGE = 216;
  241. static const int MAP_SIZE_GIANT = 252;
  242. CMapHeader();
  243. virtual ~CMapHeader() = default;
  244. ui8 levels() const;
  245. EMapFormat version; /// The default value is EMapFormat::SOD.
  246. ModCompatibilityInfo mods; /// set of mods required to play a map
  247. si32 height; /// The default value is 72.
  248. si32 width; /// The default value is 72.
  249. bool twoLevel; /// The default value is true.
  250. std::string name;
  251. std::string description;
  252. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  253. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  254. /// maximum level for heroes. This is the default value.
  255. ui8 levelLimit;
  256. std::string victoryMessage;
  257. std::string defeatMessage;
  258. ui16 victoryIconIndex;
  259. ui16 defeatIconIndex;
  260. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  261. ui8 howManyTeams;
  262. std::vector<bool> allowedHeroes;
  263. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  264. /// "main quests" of the map that describe victory and loss conditions
  265. std::vector<TriggeredEvent> triggeredEvents;
  266. template <typename Handler>
  267. void serialize(Handler & h, const int Version)
  268. {
  269. h & version;
  270. if(Version >= 821)
  271. h & mods;
  272. h & name;
  273. h & description;
  274. h & width;
  275. h & height;
  276. h & twoLevel;
  277. h & difficulty;
  278. h & levelLimit;
  279. h & areAnyPlayers;
  280. h & players;
  281. h & howManyTeams;
  282. h & allowedHeroes;
  283. //Do not serialize triggeredEvents in header as they can contain information about heroes and armies
  284. h & victoryMessage;
  285. h & victoryIconIndex;
  286. h & defeatMessage;
  287. h & defeatIconIndex;
  288. }
  289. };
  290. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  291. class DLL_LINKAGE CMap : public CMapHeader
  292. {
  293. public:
  294. CMap();
  295. ~CMap();
  296. void initTerrain();
  297. CMapEditManager * getEditManager();
  298. TerrainTile & getTile(const int3 & tile);
  299. const TerrainTile & getTile(const int3 & tile) const;
  300. bool isCoastalTile(const int3 & pos) const;
  301. bool isInTheMap(const int3 & pos) const;
  302. bool isWaterTile(const int3 & pos) const;
  303. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  304. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  305. int3 guardingCreaturePosition (int3 pos) const;
  306. void addBlockVisTiles(CGObjectInstance * obj);
  307. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  308. void calculateGuardingGreaturePositions();
  309. void addNewArtifactInstance(CArtifactInstance * art);
  310. void eraseArtifactInstance(CArtifactInstance * art);
  311. void addNewQuestInstance(CQuest * quest);
  312. void removeQuestInstance(CQuest * quest);
  313. void setUniqueInstanceName(CGObjectInstance * obj);
  314. ///Use only this method when creating new map object instances
  315. void addNewObject(CGObjectInstance * obj);
  316. void moveObject(CGObjectInstance * obj, const int3 & dst);
  317. void removeObject(CGObjectInstance * obj);
  318. /// Gets object of specified type on requested position
  319. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj::EObj type);
  320. CGHeroInstance * getHero(int heroId);
  321. /// Sets the victory/loss condition objectives ??
  322. void checkForObjectives();
  323. void resetStaticData();
  324. ui32 checksum;
  325. std::vector<Rumor> rumors;
  326. std::vector<DisposedHero> disposedHeroes;
  327. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  328. std::vector<bool> allowedSpell;
  329. std::vector<bool> allowedArtifact;
  330. std::vector<bool> allowedAbilities;
  331. std::list<CMapEvent> events;
  332. int3 grailPos;
  333. int grailRadius;
  334. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  335. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  336. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  337. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  338. std::vector< ConstTransitivePtr<CQuest> > quests;
  339. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  340. //Helper lists
  341. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  342. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  343. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  344. std::map<si32, ObjectInstanceID> questIdentifierToId;
  345. std::unique_ptr<CMapEditManager> editManager;
  346. int3 ***guardingCreaturePositions;
  347. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  348. private:
  349. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  350. TerrainTile*** terrain;
  351. si32 uidCounter; //TODO: initialize when loading an old map
  352. public:
  353. template <typename Handler>
  354. void serialize(Handler &h, const int formatVersion)
  355. {
  356. h & static_cast<CMapHeader&>(*this);
  357. h & triggeredEvents; //from CMapHeader
  358. h & rumors;
  359. h & allowedSpell;
  360. h & allowedAbilities;
  361. h & allowedArtifact;
  362. h & events;
  363. h & grailPos;
  364. h & artInstances;
  365. h & quests;
  366. h & allHeroes;
  367. h & questIdentifierToId;
  368. //TODO: viccondetails
  369. const int level = levels();
  370. if(h.saving)
  371. {
  372. // Save terrain
  373. for(int z = 0; z < level; ++z)
  374. {
  375. for(int x = 0; x < width; ++x)
  376. {
  377. for(int y = 0; y < height; ++y)
  378. {
  379. h & terrain[z][x][y];
  380. h & guardingCreaturePositions[z][x][y];
  381. }
  382. }
  383. }
  384. }
  385. else
  386. {
  387. // Load terrain
  388. terrain = new TerrainTile**[level];
  389. guardingCreaturePositions = new int3**[level];
  390. for(int z = 0; z < level; ++z)
  391. {
  392. terrain[z] = new TerrainTile*[width];
  393. guardingCreaturePositions[z] = new int3*[width];
  394. for(int x = 0; x < width; ++x)
  395. {
  396. terrain[z][x] = new TerrainTile[height];
  397. guardingCreaturePositions[z][x] = new int3[height];
  398. }
  399. }
  400. for(int z = 0; z < level; ++z)
  401. {
  402. for(int x = 0; x < width; ++x)
  403. {
  404. for(int y = 0; y < height; ++y)
  405. {
  406. h & terrain[z][x][y];
  407. h & guardingCreaturePositions[z][x][y];
  408. }
  409. }
  410. }
  411. }
  412. h & objects;
  413. h & heroesOnMap;
  414. h & teleportChannels;
  415. h & towns;
  416. h & artInstances;
  417. // static members
  418. h & CGKeys::playerKeyMap;
  419. h & CGMagi::eyelist;
  420. h & CGObelisk::obeliskCount;
  421. h & CGObelisk::visited;
  422. h & CGTownInstance::merchantArtifacts;
  423. h & CGTownInstance::universitySkills;
  424. h & instanceNames;
  425. }
  426. };
  427. VCMI_LIB_NAMESPACE_END