CMap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. //(!!!) should be `version >= 759` here, but do not try to "fix" it
  146. if(version > 759)
  147. {
  148. h & objectSubtype;
  149. h & objectInstanceName;
  150. }
  151. if(version >= 770)
  152. {
  153. h & metaType;
  154. }
  155. }
  156. };
  157. typedef LogicalExpression<EventCondition> EventExpression;
  158. struct DLL_LINKAGE EventEffect
  159. {
  160. enum EType
  161. {
  162. VICTORY,
  163. DEFEAT
  164. };
  165. /// effect type, using EType enum
  166. si8 type;
  167. /// message that will be sent to other players
  168. std::string toOtherMessage;
  169. template <typename Handler>
  170. void serialize(Handler & h, const int version)
  171. {
  172. h & type;
  173. h & toOtherMessage;
  174. }
  175. };
  176. struct DLL_LINKAGE TriggeredEvent
  177. {
  178. /// base condition that must be evaluated
  179. EventExpression trigger;
  180. /// string identifier read from config file (e.g. captureKreelah)
  181. std::string identifier;
  182. /// string-description, for use in UI (capture town to win)
  183. std::string description;
  184. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  185. std::string onFulfill;
  186. /// Effect of this event. TODO: refactor into something more flexible
  187. EventEffect effect;
  188. template <typename Handler>
  189. void serialize(Handler & h, const int version)
  190. {
  191. h & identifier;
  192. h & trigger;
  193. h & description;
  194. h & onFulfill;
  195. h & effect;
  196. }
  197. };
  198. /// The rumor struct consists of a rumor name and text.
  199. struct DLL_LINKAGE Rumor
  200. {
  201. std::string name;
  202. std::string text;
  203. Rumor() = default;
  204. ~Rumor() = default;
  205. template <typename Handler>
  206. void serialize(Handler & h, const int version)
  207. {
  208. h & name;
  209. h & text;
  210. }
  211. void serializeJson(JsonSerializeFormat & handler);
  212. };
  213. /// The disposed hero struct describes which hero can be hired from which player.
  214. struct DLL_LINKAGE DisposedHero
  215. {
  216. DisposedHero();
  217. ui32 heroId;
  218. ui16 portrait; /// The portrait id of the hero, 0xFF is default.
  219. std::string name;
  220. ui8 players; /// Who can hire this hero (bitfield).
  221. template <typename Handler>
  222. void serialize(Handler & h, const int version)
  223. {
  224. h & heroId;
  225. h & portrait;
  226. h & name;
  227. h & players;
  228. }
  229. };
  230. namespace EMapFormat
  231. {
  232. enum EMapFormat: ui8
  233. {
  234. INVALID = 0,
  235. // HEX DEC
  236. ROE = 0x0e, // 14
  237. AB = 0x15, // 21
  238. SOD = 0x1c, // 28
  239. // HOTA = 0x1e ... 0x20 // 28 ... 30
  240. WOG = 0x33, // 51
  241. VCMI = 0xF0
  242. };
  243. }
  244. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  245. class DLL_LINKAGE CMapHeader
  246. {
  247. void setupEvents();
  248. public:
  249. static const int MAP_SIZE_SMALL;
  250. static const int MAP_SIZE_MIDDLE;
  251. static const int MAP_SIZE_LARGE;
  252. static const int MAP_SIZE_XLARGE;
  253. CMapHeader();
  254. virtual ~CMapHeader();
  255. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  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. h & name;
  280. h & description;
  281. h & width;
  282. h & height;
  283. h & twoLevel;
  284. h & difficulty;
  285. h & levelLimit;
  286. h & areAnyPlayers;
  287. h & players;
  288. h & howManyTeams;
  289. h & allowedHeroes;
  290. h & triggeredEvents;
  291. h & victoryMessage;
  292. h & victoryIconIndex;
  293. h & defeatMessage;
  294. h & defeatIconIndex;
  295. }
  296. };
  297. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  298. class DLL_LINKAGE CMap : public CMapHeader
  299. {
  300. public:
  301. CMap();
  302. ~CMap();
  303. void initTerrain();
  304. CMapEditManager * getEditManager();
  305. TerrainTile & getTile(const int3 & tile);
  306. const TerrainTile & getTile(const int3 & tile) const;
  307. bool isCoastalTile(const int3 & pos) const;
  308. bool isInTheMap(const int3 & pos) const;
  309. bool isWaterTile(const int3 & pos) const;
  310. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  311. bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
  312. int3 guardingCreaturePosition (int3 pos) const;
  313. void addBlockVisTiles(CGObjectInstance * obj);
  314. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  315. void calculateGuardingGreaturePositions();
  316. void addNewArtifactInstance(CArtifactInstance * art);
  317. void eraseArtifactInstance(CArtifactInstance * art);
  318. void addNewQuestInstance(CQuest * quest);
  319. ///Use only this method when creating new map object instances
  320. void addNewObject(CGObjectInstance * obj);
  321. /// Gets object of specified type on requested position
  322. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, Obj::EObj type);
  323. CGHeroInstance * getHero(int heroId);
  324. /// Sets the victory/loss condition objectives ??
  325. void checkForObjectives();
  326. ui32 checksum;
  327. std::vector<Rumor> rumors;
  328. std::vector<DisposedHero> disposedHeroes;
  329. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  330. std::vector<bool> allowedSpell;
  331. std::vector<bool> allowedArtifact;
  332. std::vector<bool> allowedAbilities;
  333. std::list<CMapEvent> events;
  334. int3 grailPos;
  335. int grailRadius;
  336. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  337. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  338. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  339. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  340. std::vector< ConstTransitivePtr<CQuest> > quests;
  341. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  342. //Helper lists
  343. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  344. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  345. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  346. std::map<si32, ObjectInstanceID> questIdentifierToId;
  347. std::unique_ptr<CMapEditManager> editManager;
  348. int3 ***guardingCreaturePositions;
  349. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  350. private:
  351. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  352. TerrainTile*** terrain;
  353. public:
  354. template <typename Handler>
  355. void serialize(Handler &h, const int formatVersion)
  356. {
  357. h & static_cast<CMapHeader&>(*this);
  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. int level = twoLevel ? 2 : 1;
  370. if(h.saving)
  371. {
  372. // Save terrain
  373. for(int i = 0; i < width ; ++i)
  374. {
  375. for(int j = 0; j < height ; ++j)
  376. {
  377. for(int k = 0; k < level; ++k)
  378. {
  379. h & terrain[i][j][k];
  380. h & guardingCreaturePositions[i][j][k];
  381. }
  382. }
  383. }
  384. }
  385. else
  386. {
  387. // Load terrain
  388. terrain = new TerrainTile**[width];
  389. guardingCreaturePositions = new int3**[width];
  390. for(int i = 0; i < width; ++i)
  391. {
  392. terrain[i] = new TerrainTile*[height];
  393. guardingCreaturePositions[i] = new int3*[height];
  394. for(int j = 0; j < height; ++j)
  395. {
  396. terrain[i][j] = new TerrainTile[level];
  397. guardingCreaturePositions[i][j] = new int3[level];
  398. }
  399. }
  400. for(int i = 0; i < width ; ++i)
  401. {
  402. for(int j = 0; j < height ; ++j)
  403. {
  404. for(int k = 0; k < level; ++k)
  405. {
  406. h & terrain[i][j][k];
  407. h & guardingCreaturePositions[i][j][k];
  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. if(formatVersion >= 759)
  425. {
  426. h & instanceNames;
  427. }
  428. }
  429. };