CMap.h 13 KB

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