CMap.h 14 KB

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