CMap.h 14 KB

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