CMap.h 13 KB

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