CMap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 "../CObjectHandler.h"
  13. #include "../ResourceSet.h"
  14. #include "../int3.h"
  15. #include "../GameConstants.h"
  16. class CArtifactInstance;
  17. class CGDefInfo;
  18. class CGObjectInstance;
  19. class CGHeroInstance;
  20. class CCommanderInstance;
  21. class CGCreature;
  22. class CQuest;
  23. class CGTownInstance;
  24. class IModableArt;
  25. class IQuestObject;
  26. class CInputStream;
  27. class CMapEditManager;
  28. /// The hero name struct consists of the hero id and the hero name.
  29. struct DLL_LINKAGE SHeroName
  30. {
  31. SHeroName();
  32. int heroId;
  33. std::string heroName;
  34. template <typename Handler>
  35. void serialize(Handler & h, const int version)
  36. {
  37. h & heroId & heroName;
  38. }
  39. };
  40. namespace EAiTactic
  41. {
  42. enum EAiTactic
  43. {
  44. NONE = -1,
  45. RANDOM,
  46. WARRIOR,
  47. BUILDER,
  48. EXPLORER
  49. };
  50. }
  51. /// The player info constains data about which factions are allowed, AI tactical settings,
  52. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  53. struct DLL_LINKAGE PlayerInfo
  54. {
  55. PlayerInfo();
  56. /// Gets the default faction id or -1 for a random faction.
  57. si8 defaultCastle() const;
  58. /// Gets the default hero id or -1 for a random hero.
  59. si8 defaultHero() const;
  60. bool canAnyonePlay() const;
  61. bool canHumanPlay;
  62. bool canComputerPlay;
  63. EAiTactic::EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  64. std::set<TFaction> allowedFactions;
  65. bool isFactionRandom;
  66. si32 mainHeroPortrait; /// The default value is -1.
  67. std::string mainHeroName;
  68. std::vector<SHeroName> heroesNames; /// List of renamed heroes.
  69. bool hasMainTown; /// The default value is false.
  70. bool generateHeroAtMainTown; /// The default value is false.
  71. int3 posOfMainTown;
  72. TeamID team; /// The default value is 255 representing that the player belongs to no team.
  73. bool generateHero; /// Unused.
  74. si32 p7; /// Unknown and unused.
  75. bool hasHero; /// Player has a (custom?) hero
  76. si32 customHeroID; /// ID of custom hero, -1 if none
  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 & hasHero & customHeroID & canHumanPlay & canComputerPlay & aiTactic & allowedFactions & isFactionRandom &
  84. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  85. posOfMainTown & team & generateHero;
  86. }
  87. };
  88. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  89. struct DLL_LINKAGE LossCondition
  90. {
  91. LossCondition();
  92. ELossConditionType::ELossConditionType typeOfLossCon;
  93. int3 pos; /// the position of an object which mustn't be lost
  94. si32 timeLimit; /// time limit in days, -1 if not used
  95. const CGObjectInstance * obj;
  96. template <typename Handler>
  97. void serialize(Handler & h, const int version)
  98. {
  99. h & typeOfLossCon & pos & timeLimit & obj;
  100. }
  101. };
  102. /// The victory condition describes the condition to win the game. (e.g. defeat all enemy heroes/castles,
  103. /// receive a specific artifact, ...)
  104. struct DLL_LINKAGE VictoryCondition
  105. {
  106. VictoryCondition();
  107. EVictoryConditionType::EVictoryConditionType condition;
  108. bool allowNormalVictory; /// true if a normal victory is allowed (defeat all enemy towns, heroes)
  109. bool appliesToAI;
  110. /// pos of city to upgrade (3); pos of town to build grail, {-1,-1,-1} if not relevant (4); hero pos (5); town pos(6);
  111. /// monster pos (7); destination pos(8)
  112. int3 pos;
  113. /// artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8)
  114. si32 objectId;
  115. /// needed count for creatures (1) / resource (2); upgraded town hall level (3);
  116. si32 count;
  117. /// object of specific monster / city / hero instance (NULL if not used); set during map parsing
  118. const CGObjectInstance * obj;
  119. template <typename Handler>
  120. void serialize(Handler & h, const int version)
  121. {
  122. h & condition & allowNormalVictory & appliesToAI & pos & objectId & count & obj;
  123. }
  124. };
  125. /// The rumor struct consists of a rumor name and text.
  126. struct DLL_LINKAGE Rumor
  127. {
  128. std::string name;
  129. std::string text;
  130. template <typename Handler>
  131. void serialize(Handler & h, const int version)
  132. {
  133. h & name & text;
  134. }
  135. };
  136. /// The disposed hero struct describes which hero can be hired from which player.
  137. struct DLL_LINKAGE DisposedHero
  138. {
  139. DisposedHero();
  140. ui32 heroId;
  141. ui16 portrait; /// The portrait id of the hero, 0xFF is default.
  142. std::string name;
  143. ui8 players; /// Who can hire this hero (bitfield).
  144. template <typename Handler>
  145. void serialize(Handler & h, const int version)
  146. {
  147. h & heroId & portrait & name & players;
  148. }
  149. };
  150. /// The map event is an event which e.g. gives or takes resources of a specific
  151. /// amount to/from players and can appear regularly or once a time.
  152. class DLL_LINKAGE CMapEvent
  153. {
  154. public:
  155. CMapEvent();
  156. bool earlierThan(const CMapEvent & other) const;
  157. bool earlierThanOrEqual(const CMapEvent & other) const;
  158. std::string name;
  159. std::string message;
  160. TResources resources;
  161. ui8 players; // affected players, bit field?
  162. ui8 humanAffected;
  163. ui8 computerAffected;
  164. ui32 firstOccurence;
  165. ui32 nextOccurence; /// specifies after how many days the event will occur the next time; 0 if event occurs only one time
  166. template <typename Handler>
  167. void serialize(Handler & h, const int version)
  168. {
  169. h & name & message & resources
  170. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  171. }
  172. };
  173. /// The castle event builds/adds buildings/creatures for a specific town.
  174. class DLL_LINKAGE CCastleEvent: public CMapEvent
  175. {
  176. public:
  177. CCastleEvent();
  178. std::set<BuildingID> buildings;
  179. std::vector<si32> creatures;
  180. CGTownInstance * town;
  181. template <typename Handler>
  182. void serialize(Handler & h, const int version)
  183. {
  184. h & static_cast<CMapEvent &>(*this);
  185. h & buildings & creatures;
  186. }
  187. };
  188. namespace ERiverType
  189. {
  190. enum ERiverType
  191. {
  192. NO_RIVER, CLEAR_RIVER, ICY_RIVER, MUDDY_RIVER, LAVA_RIVER
  193. };
  194. }
  195. namespace ERoadType
  196. {
  197. enum ERoadType
  198. {
  199. NO_ROAD, DIRT_ROAD, GRAVEL_ROAD, COBBLESTONE_ROAD
  200. };
  201. }
  202. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  203. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  204. struct DLL_LINKAGE TerrainTile
  205. {
  206. TerrainTile();
  207. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  208. bool entrableTerrain(const TerrainTile * from = NULL) const;
  209. bool entrableTerrain(bool allowLand, bool allowSea) const;
  210. /// Checks for blocking objects and terraint type (water / land).
  211. bool isClear(const TerrainTile * from = NULL) const;
  212. /// Gets the ID of the top visitable object or -1 if there is none.
  213. int topVisitableId() const;
  214. bool isWater() const;
  215. bool isCoastal() const;
  216. bool hasFavourableWinds() const;
  217. ETerrainType terType;
  218. ui8 terView;
  219. ERiverType::ERiverType riverType;
  220. ui8 riverDir;
  221. ERoadType::ERoadType roadType;
  222. ui8 roadDir;
  223. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  224. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  225. ui8 extTileFlags;
  226. bool visitable;
  227. bool blocked;
  228. std::vector<CGObjectInstance *> visitableObjects;
  229. std::vector<CGObjectInstance *> blockingObjects;
  230. template <typename Handler>
  231. void serialize(Handler & h, const int version)
  232. {
  233. h & terType & terView & riverType & riverDir & roadType &roadDir & extTileFlags & blocked;
  234. if(!h.saving)
  235. {
  236. visitable = false;
  237. //these flags (and obj vectors) will be restored in map serialization
  238. }
  239. }
  240. };
  241. namespace EMapFormat
  242. {
  243. enum EMapFormat
  244. {
  245. INVALID, WOG=0x33, AB=0x15, ROE=0x0e, SOD=0x1c
  246. };
  247. }
  248. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  249. class DLL_LINKAGE CMapHeader
  250. {
  251. public:
  252. CMapHeader();
  253. virtual ~CMapHeader();
  254. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  255. si32 height; /// The default value is 72.
  256. si32 width; /// The default value is 72.
  257. bool twoLevel; /// The default value is true.
  258. std::string name;
  259. std::string description;
  260. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  261. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  262. /// maximum level for heroes. This is the default value.
  263. ui8 levelLimit;
  264. LossCondition lossCondition; /// The default value is lose all your towns and heroes.
  265. VictoryCondition victoryCondition; /// The default value is defeat all enemies.
  266. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  267. ui8 howManyTeams;
  268. std::vector<bool> allowedHeroes;
  269. std::vector<ui16> placeholdedHeroes;
  270. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  271. template <typename Handler>
  272. void serialize(Handler & h, const int Version)
  273. {
  274. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  275. h & players & lossCondition & victoryCondition & howManyTeams & allowedHeroes;
  276. }
  277. };
  278. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  279. class DLL_LINKAGE CMap : public CMapHeader
  280. {
  281. public:
  282. CMap();
  283. ~CMap();
  284. void initTerrain();
  285. CMapEditManager * getEditManager();
  286. TerrainTile & getTile(const int3 & tile);
  287. const TerrainTile & getTile(const int3 & tile) const;
  288. bool isInTheMap(const int3 & pos) const;
  289. bool isWaterTile(const int3 & pos) const;
  290. void addBlockVisTiles(CGObjectInstance * obj);
  291. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  292. void addNewArtifactInstance(CArtifactInstance * art);
  293. void eraseArtifactInstance(CArtifactInstance * art);
  294. void addQuest(CGObjectInstance * quest);
  295. /// Gets the topmost object or the lowermost object depending on the flag lookForHero from the specified position.
  296. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, bool lookForHero);
  297. CGHeroInstance * getHero(int heroId);
  298. /// Sets the victory/loss condition objectives ??
  299. void checkForObjectives();
  300. ui32 checksum;
  301. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  302. std::vector<Rumor> rumors;
  303. std::vector<DisposedHero> disposedHeroes;
  304. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  305. std::vector<ConstTransitivePtr<CGDefInfo> > customDefs;
  306. std::vector<bool> allowedSpell;
  307. std::vector<bool> allowedArtifact;
  308. std::vector<bool> allowedAbilities;
  309. std::list<CMapEvent> events;
  310. int3 grailPos;
  311. int grailRadious;
  312. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  313. std::vector< ConstTransitivePtr<CGHeroInstance> > heroes;
  314. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  315. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  316. std::vector< ConstTransitivePtr<CQuest> > quests;
  317. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  318. bmap<si32, ObjectInstanceID> questIdentifierToId;
  319. unique_ptr<CMapEditManager> editManager;
  320. private:
  321. void getTileRangeCheck(const int3 & tile) const;
  322. TerrainTile*** terrain;
  323. public:
  324. template <typename Handler>
  325. void serialize(Handler &h, const int formatVersion)
  326. {
  327. h & static_cast<CMapHeader&>(*this);
  328. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  329. h & artInstances & quests;
  330. h & questIdentifierToId;
  331. //TODO: viccondetails
  332. if(h.saving)
  333. {
  334. // Save terrain
  335. for(int i = 0; i < width ; ++i)
  336. {
  337. for(int j = 0; j < height ; ++j)
  338. {
  339. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  340. {
  341. h & terrain[i][j][k];
  342. }
  343. }
  344. }
  345. }
  346. else
  347. {
  348. // Load terrain
  349. terrain = new TerrainTile**[width];
  350. for(int ii = 0; ii < width; ++ii)
  351. {
  352. terrain[ii] = new TerrainTile*[height];
  353. for(int jj = 0; jj < height; ++jj)
  354. {
  355. terrain[ii][jj] = new TerrainTile[twoLevel ? 2 : 1];
  356. }
  357. }
  358. for(int i = 0; i < width ; ++i)
  359. {
  360. for(int j = 0; j < height ; ++j)
  361. {
  362. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  363. {
  364. h & terrain[i][j][k];
  365. }
  366. }
  367. }
  368. }
  369. h & customDefs & objects;
  370. // static members
  371. h & CGTeleport::objs;
  372. h & CGTeleport::gates;
  373. h & CGKeys::playerKeyMap;
  374. h & CGMagi::eyelist;
  375. h & CGObelisk::obeliskCount & CGObelisk::visited;
  376. h & CGTownInstance::merchantArtifacts;
  377. h & CGTownInstance::universitySkills;
  378. if(!h.saving)
  379. {
  380. for(ui32 i = 0; i < objects.size(); ++i)
  381. {
  382. if(!objects[i]) continue;
  383. switch (objects[i]->ID)
  384. {
  385. case Obj::HERO:
  386. heroes.push_back (static_cast<CGHeroInstance*>(+objects[i]));
  387. break;
  388. case Obj::TOWN:
  389. towns.push_back (static_cast<CGTownInstance*>(+objects[i]));
  390. break;
  391. }
  392. // recreate blockvis map
  393. addBlockVisTiles(objects[i]);
  394. }
  395. // if hero is visiting/garrisoned in town set appropriate pointers
  396. for(ui32 i = 0; i < heroes.size(); ++i)
  397. {
  398. int3 vistile = heroes[i]->pos;
  399. vistile.x++;
  400. for(ui32 j = 0; j < towns.size(); ++j)
  401. {
  402. // hero stands on the town entrance
  403. if(vistile == towns[j]->pos)
  404. {
  405. if(heroes[i]->inTownGarrison)
  406. {
  407. towns[j]->garrisonHero = heroes[i];
  408. removeBlockVisTiles(heroes[i]);
  409. }
  410. else
  411. {
  412. towns[j]->visitingHero = heroes[i];
  413. }
  414. heroes[i]->visitedTown = towns[j];
  415. break;
  416. }
  417. }
  418. vistile.x -= 2; //manifest pos
  419. const TerrainTile & t = getTile(vistile);
  420. if(t.terType != ETerrainType::WATER) continue;
  421. //hero stands on the water - he must be in the boat
  422. for(ui32 j = 0; j < t.visitableObjects.size(); ++j)
  423. {
  424. if(t.visitableObjects[j]->ID == Obj::BOAT)
  425. {
  426. CGBoat * b = static_cast<CGBoat *>(t.visitableObjects[j]);
  427. heroes[i]->boat = b;
  428. b->hero = heroes[i];
  429. removeBlockVisTiles(b);
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. };