CMap.h 14 KB

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