CMap.h 14 KB

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