CMap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. typedef LogicalExpression<EventCondition> EventExpression;
  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. HOTA = 0x20, // 32
  223. WOG = 0x33, // 51
  224. VCMI = 0xF0
  225. };
  226. // Inherit from container to enable forward declaration
  227. class ModCompatibilityInfo: public std::map<TModID, CModInfo::Version>
  228. {};
  229. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  230. class DLL_LINKAGE CMapHeader
  231. {
  232. void setupEvents();
  233. public:
  234. static const int MAP_SIZE_SMALL = 36;
  235. static const int MAP_SIZE_MIDDLE = 72;
  236. static const int MAP_SIZE_LARGE = 108;
  237. static const int MAP_SIZE_XLARGE = 144;
  238. static const int MAP_SIZE_HUGE = 180;
  239. static const int MAP_SIZE_XHUGE = 216;
  240. static const int MAP_SIZE_GIANT = 252;
  241. CMapHeader();
  242. virtual ~CMapHeader() = default;
  243. ui8 levels() const;
  244. EMapFormat version; /// The default value is EMapFormat::SOD.
  245. ModCompatibilityInfo mods; /// set of mods required to play a map
  246. si32 height; /// The default value is 72.
  247. si32 width; /// The default value is 72.
  248. bool twoLevel; /// The default value is true.
  249. std::string name;
  250. std::string description;
  251. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  252. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  253. /// maximum level for heroes. This is the default value.
  254. ui8 levelLimit;
  255. std::string victoryMessage;
  256. std::string defeatMessage;
  257. ui16 victoryIconIndex;
  258. ui16 defeatIconIndex;
  259. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  260. ui8 howManyTeams;
  261. std::vector<bool> allowedHeroes;
  262. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  263. /// "main quests" of the map that describe victory and loss conditions
  264. std::vector<TriggeredEvent> triggeredEvents;
  265. template <typename Handler>
  266. void serialize(Handler & h, const int Version)
  267. {
  268. h & version;
  269. if(Version >= 821)
  270. h & mods;
  271. h & name;
  272. h & description;
  273. h & width;
  274. h & height;
  275. h & twoLevel;
  276. h & difficulty;
  277. h & levelLimit;
  278. h & areAnyPlayers;
  279. h & players;
  280. h & howManyTeams;
  281. h & allowedHeroes;
  282. //Do not serialize triggeredEvents in header as they can contain information about heroes and armies
  283. h & victoryMessage;
  284. h & victoryIconIndex;
  285. h & defeatMessage;
  286. h & defeatIconIndex;
  287. }
  288. };
  289. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  290. class DLL_LINKAGE CMap : public CMapHeader
  291. {
  292. public:
  293. CMap();
  294. ~CMap();
  295. void initTerrain();
  296. CMapEditManager * getEditManager();
  297. TerrainTile & getTile(const int3 & tile);
  298. const TerrainTile & getTile(const int3 & tile) const;
  299. bool isCoastalTile(const int3 & pos) const;
  300. bool isInTheMap(const int3 & pos) const;
  301. bool isWaterTile(const int3 & pos) const;
  302. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  303. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  304. int3 guardingCreaturePosition (int3 pos) const;
  305. void addBlockVisTiles(CGObjectInstance * obj);
  306. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  307. void calculateGuardingGreaturePositions();
  308. void addNewArtifactInstance(CArtifactInstance * art);
  309. void eraseArtifactInstance(CArtifactInstance * art);
  310. void addNewQuestInstance(CQuest * quest);
  311. void removeQuestInstance(CQuest * quest);
  312. void setUniqueInstanceName(CGObjectInstance * obj);
  313. ///Use only this method when creating new map object instances
  314. void addNewObject(CGObjectInstance * obj);
  315. void moveObject(CGObjectInstance * obj, const int3 & dst);
  316. void removeObject(CGObjectInstance * obj);
  317. /// Gets object of specified type on requested position
  318. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj::EObj type);
  319. CGHeroInstance * getHero(int heroId);
  320. /// Sets the victory/loss condition objectives ??
  321. void checkForObjectives();
  322. void resetStaticData();
  323. ui32 checksum;
  324. std::vector<Rumor> rumors;
  325. std::vector<DisposedHero> disposedHeroes;
  326. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  327. std::vector<bool> allowedSpell;
  328. std::vector<bool> allowedArtifact;
  329. std::vector<bool> allowedAbilities;
  330. std::list<CMapEvent> events;
  331. int3 grailPos;
  332. int grailRadius;
  333. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  334. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  335. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  336. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  337. std::vector< ConstTransitivePtr<CQuest> > quests;
  338. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  339. //Helper lists
  340. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  341. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  342. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  343. std::map<si32, ObjectInstanceID> questIdentifierToId;
  344. std::unique_ptr<CMapEditManager> editManager;
  345. int3 ***guardingCreaturePositions;
  346. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  347. private:
  348. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  349. TerrainTile*** terrain;
  350. si32 uidCounter; //TODO: initialize when loading an old map
  351. public:
  352. template <typename Handler>
  353. void serialize(Handler &h, const int formatVersion)
  354. {
  355. h & static_cast<CMapHeader&>(*this);
  356. h & triggeredEvents; //from CMapHeader
  357. h & rumors;
  358. h & allowedSpell;
  359. h & allowedAbilities;
  360. h & allowedArtifact;
  361. h & events;
  362. h & grailPos;
  363. h & artInstances;
  364. h & quests;
  365. h & allHeroes;
  366. h & questIdentifierToId;
  367. //TODO: viccondetails
  368. const int level = levels();
  369. if(h.saving)
  370. {
  371. // Save terrain
  372. for(int z = 0; z < level; ++z)
  373. {
  374. for(int x = 0; x < width; ++x)
  375. {
  376. for(int y = 0; y < height; ++y)
  377. {
  378. h & terrain[z][x][y];
  379. h & guardingCreaturePositions[z][x][y];
  380. }
  381. }
  382. }
  383. }
  384. else
  385. {
  386. // Load terrain
  387. terrain = new TerrainTile**[level];
  388. guardingCreaturePositions = new int3**[level];
  389. for(int z = 0; z < level; ++z)
  390. {
  391. terrain[z] = new TerrainTile*[width];
  392. guardingCreaturePositions[z] = new int3*[width];
  393. for(int x = 0; x < width; ++x)
  394. {
  395. terrain[z][x] = new TerrainTile[height];
  396. guardingCreaturePositions[z][x] = new int3[height];
  397. }
  398. }
  399. for(int z = 0; z < level; ++z)
  400. {
  401. for(int x = 0; x < width; ++x)
  402. {
  403. for(int y = 0; y < height; ++y)
  404. {
  405. h & terrain[z][x][y];
  406. h & guardingCreaturePositions[z][x][y];
  407. }
  408. }
  409. }
  410. }
  411. h & objects;
  412. h & heroesOnMap;
  413. h & teleportChannels;
  414. h & towns;
  415. h & artInstances;
  416. // static members
  417. h & CGKeys::playerKeyMap;
  418. h & CGMagi::eyelist;
  419. h & CGObelisk::obeliskCount;
  420. h & CGObelisk::visited;
  421. h & CGTownInstance::merchantArtifacts;
  422. h & CGTownInstance::universitySkills;
  423. h & instanceNames;
  424. }
  425. };
  426. VCMI_LIB_NAMESPACE_END