CMap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. ui16 portrait; /// The portrait id of the hero, 0xFF 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. HOTA1 = 0x1e, // 30
  222. HOTA2 = 0x1f, // 31
  223. HOTA3 = 0x20, // 32
  224. WOG = 0x33, // 51
  225. VCMI = 0xF0
  226. };
  227. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  228. class DLL_LINKAGE CMapHeader
  229. {
  230. void setupEvents();
  231. public:
  232. static const int MAP_SIZE_SMALL = 36;
  233. static const int MAP_SIZE_MIDDLE = 72;
  234. static const int MAP_SIZE_LARGE = 108;
  235. static const int MAP_SIZE_XLARGE = 144;
  236. static const int MAP_SIZE_HUGE = 180;
  237. static const int MAP_SIZE_XHUGE = 216;
  238. static const int MAP_SIZE_GIANT = 252;
  239. CMapHeader();
  240. virtual ~CMapHeader() = default;
  241. ui8 levels() const;
  242. EMapFormat version; /// The default value is EMapFormat::SOD.
  243. si32 height; /// The default value is 72.
  244. si32 width; /// The default value is 72.
  245. bool twoLevel; /// The default value is true.
  246. std::string name;
  247. std::string description;
  248. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  249. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  250. /// maximum level for heroes. This is the default value.
  251. ui8 levelLimit;
  252. std::string victoryMessage;
  253. std::string defeatMessage;
  254. ui16 victoryIconIndex;
  255. ui16 defeatIconIndex;
  256. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  257. ui8 howManyTeams;
  258. std::vector<bool> allowedHeroes;
  259. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  260. /// "main quests" of the map that describe victory and loss conditions
  261. std::vector<TriggeredEvent> triggeredEvents;
  262. template <typename Handler>
  263. void serialize(Handler & h, const int Version)
  264. {
  265. h & version;
  266. h & name;
  267. h & description;
  268. h & width;
  269. h & height;
  270. h & twoLevel;
  271. h & difficulty;
  272. h & levelLimit;
  273. h & areAnyPlayers;
  274. h & players;
  275. h & howManyTeams;
  276. h & allowedHeroes;
  277. //Do not serialize triggeredEvents in header as they can contain information about heroes and armies
  278. h & victoryMessage;
  279. h & victoryIconIndex;
  280. h & defeatMessage;
  281. h & defeatIconIndex;
  282. }
  283. };
  284. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  285. class DLL_LINKAGE CMap : public CMapHeader
  286. {
  287. public:
  288. CMap();
  289. ~CMap();
  290. void initTerrain();
  291. CMapEditManager * getEditManager();
  292. TerrainTile & getTile(const int3 & tile);
  293. const TerrainTile & getTile(const int3 & tile) const;
  294. bool isCoastalTile(const int3 & pos) const;
  295. bool isInTheMap(const int3 & pos) const;
  296. bool isWaterTile(const int3 & pos) const;
  297. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  298. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  299. int3 guardingCreaturePosition (int3 pos) const;
  300. void addBlockVisTiles(CGObjectInstance * obj);
  301. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  302. void calculateGuardingGreaturePositions();
  303. void addNewArtifactInstance(CArtifactInstance * art);
  304. void eraseArtifactInstance(CArtifactInstance * art);
  305. void addNewQuestInstance(CQuest * quest);
  306. void removeQuestInstance(CQuest * quest);
  307. void setUniqueInstanceName(CGObjectInstance * obj);
  308. ///Use only this method when creating new map object instances
  309. void addNewObject(CGObjectInstance * obj);
  310. void moveObject(CGObjectInstance * obj, const int3 & dst);
  311. void removeObject(CGObjectInstance * obj);
  312. /// Gets object of specified type on requested position
  313. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj::EObj type);
  314. CGHeroInstance * getHero(int heroId);
  315. /// Sets the victory/loss condition objectives ??
  316. void checkForObjectives();
  317. void resetStaticData();
  318. ui32 checksum;
  319. std::vector<Rumor> rumors;
  320. std::vector<DisposedHero> disposedHeroes;
  321. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  322. std::vector<bool> allowedSpell;
  323. std::vector<bool> allowedArtifact;
  324. std::vector<bool> allowedAbilities;
  325. std::list<CMapEvent> events;
  326. int3 grailPos;
  327. int grailRadius;
  328. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  329. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  330. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  331. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  332. std::vector< ConstTransitivePtr<CQuest> > quests;
  333. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  334. //Helper lists
  335. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  336. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  337. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  338. std::map<si32, ObjectInstanceID> questIdentifierToId;
  339. std::unique_ptr<CMapEditManager> editManager;
  340. int3 ***guardingCreaturePositions;
  341. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  342. private:
  343. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  344. TerrainTile*** terrain;
  345. si32 uidCounter; //TODO: initialize when loading an old map
  346. public:
  347. template <typename Handler>
  348. void serialize(Handler &h, const int formatVersion)
  349. {
  350. h & static_cast<CMapHeader&>(*this);
  351. h & triggeredEvents; //from CMapHeader
  352. h & rumors;
  353. h & allowedSpell;
  354. h & allowedAbilities;
  355. h & allowedArtifact;
  356. h & events;
  357. h & grailPos;
  358. h & artInstances;
  359. h & quests;
  360. h & allHeroes;
  361. h & questIdentifierToId;
  362. //TODO: viccondetails
  363. const int level = levels();
  364. if(h.saving)
  365. {
  366. // Save terrain
  367. for(int z = 0; z < level; ++z)
  368. {
  369. for(int x = 0; x < width; ++x)
  370. {
  371. for(int y = 0; y < height; ++y)
  372. {
  373. h & terrain[z][x][y];
  374. h & guardingCreaturePositions[z][x][y];
  375. }
  376. }
  377. }
  378. }
  379. else
  380. {
  381. // Load terrain
  382. terrain = new TerrainTile**[level];
  383. guardingCreaturePositions = new int3**[level];
  384. for(int z = 0; z < level; ++z)
  385. {
  386. terrain[z] = new TerrainTile*[width];
  387. guardingCreaturePositions[z] = new int3*[width];
  388. for(int x = 0; x < width; ++x)
  389. {
  390. terrain[z][x] = new TerrainTile[height];
  391. guardingCreaturePositions[z][x] = new int3[height];
  392. }
  393. }
  394. for(int z = 0; z < level; ++z)
  395. {
  396. for(int x = 0; x < width; ++x)
  397. {
  398. for(int y = 0; y < height; ++y)
  399. {
  400. h & terrain[z][x][y];
  401. h & guardingCreaturePositions[z][x][y];
  402. }
  403. }
  404. }
  405. }
  406. h & objects;
  407. h & heroesOnMap;
  408. h & teleportChannels;
  409. h & towns;
  410. h & artInstances;
  411. // static members
  412. h & CGKeys::playerKeyMap;
  413. h & CGMagi::eyelist;
  414. h & CGObelisk::obeliskCount;
  415. h & CGObelisk::visited;
  416. h & CGTownInstance::merchantArtifacts;
  417. h & CGTownInstance::universitySkills;
  418. h & instanceNames;
  419. }
  420. };
  421. VCMI_LIB_NAMESPACE_END