CMap.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. /**
  28. * The hero name struct consists of the hero id and the hero name.
  29. */
  30. struct DLL_LINKAGE SHeroName
  31. {
  32. /**
  33. * Default c-tor.
  34. */
  35. SHeroName();
  36. /** the id of the hero */
  37. int heroId;
  38. /** the name of the hero */
  39. std::string heroName;
  40. /**
  41. * Serialize method.
  42. */
  43. template <typename Handler>
  44. void serialize(Handler & h, const int version)
  45. {
  46. h & heroId & heroName;
  47. }
  48. };
  49. namespace EAiTactic
  50. {
  51. enum EAiTactic
  52. {
  53. NONE = -1,
  54. RANDOM,
  55. WARRIOR,
  56. BUILDER,
  57. EXPLORER
  58. };
  59. }
  60. /**
  61. * The player info constains data about which factions are allowed, AI tactical settings,
  62. * the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  63. */
  64. struct DLL_LINKAGE PlayerInfo
  65. {
  66. /**
  67. * Default constructor.
  68. */
  69. PlayerInfo();
  70. /**
  71. * Gets the default faction id or -1 for a random faction.
  72. *
  73. * @return the default faction id or -1 for a random faction
  74. */
  75. si8 defaultCastle() const;
  76. /**
  77. * Gets -1 for random hero.
  78. *
  79. * @return -1 for random hero
  80. */
  81. si8 defaultHero() const;
  82. /** True if the player can be played by a human. */
  83. bool canHumanPlay;
  84. /** True if th player can be played by the computer */
  85. bool canComputerPlay;
  86. /** Defines the tactical setting of the AI. The default value is EAiTactic::RANDOM. */
  87. EAiTactic::EAiTactic aiTactic;
  88. /** A list of unique IDs of allowed factions. */
  89. std::set<TFaction> allowedFactions;
  90. /** Unused. True if the faction should be chosen randomly. */
  91. bool isFactionRandom;
  92. /** Specifies the ID of the main hero with chosen portrait. The default value is -1. */
  93. si32 mainHeroPortrait;
  94. /** The name of the main hero. */
  95. std::string mainHeroName;
  96. /** The list of renamed heroes. */
  97. std::vector<SHeroName> heroesNames;
  98. /** True if the player has a main town. The default value is true. */
  99. bool hasMainTown;
  100. /** True if the main hero should be generated at the main town. The default value is true. */
  101. bool generateHeroAtMainTown;
  102. /** The position of the main town. */
  103. int3 posOfMainTown;
  104. /** The team id to which the player belongs to. The default value is 255 representing that the player belongs to no team. */
  105. ui8 team;
  106. /** Unused. True if a hero should be generated. */
  107. bool generateHero;
  108. /** Unknown and unused. */
  109. si32 p7;
  110. /** Player has a (custom?) hero */
  111. bool hasHero;
  112. /** ID of custom hero, -1 if none */
  113. si32 customHeroID;
  114. /**
  115. * Unused. Count of hero placeholders containing hero type.
  116. * WARNING: powerPlaceholders sometimes gives false 0 (eg. even if there is one placeholder),
  117. * maybe different meaning ???
  118. */
  119. ui8 powerPlaceholders;
  120. /**
  121. * Serialize method.
  122. */
  123. template <typename Handler>
  124. void serialize(Handler & h, const int version)
  125. {
  126. h & p7 & hasHero & customHeroID & canHumanPlay & canComputerPlay & aiTactic & allowedFactions & isFactionRandom &
  127. mainHeroPortrait & mainHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  128. posOfMainTown & team & generateHero;
  129. }
  130. };
  131. /**
  132. * The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  133. */
  134. struct DLL_LINKAGE LossCondition
  135. {
  136. /**
  137. * Default constructor.
  138. */
  139. LossCondition();
  140. /** specifies the condition type */
  141. ELossConditionType::ELossConditionType typeOfLossCon;
  142. /** the position of an object which mustn't be lost */
  143. int3 pos;
  144. /** time limit in days, -1 if not used */
  145. si32 timeLimit;
  146. /** set during map parsing: hero/town (depending on typeOfLossCon); nullptr if not used */
  147. const CGObjectInstance * obj;
  148. /**
  149. * Serialize method.
  150. */
  151. template <typename Handler>
  152. void serialize(Handler & h, const int version)
  153. {
  154. h & typeOfLossCon & pos & timeLimit & obj;
  155. }
  156. };
  157. /**
  158. * The victory condition describes the condition to win the game. (e.g. defeat all enemy heroes/castles,
  159. * receive a specific artifact, ...)
  160. */
  161. struct DLL_LINKAGE VictoryCondition
  162. {
  163. /**
  164. * Default constructor.
  165. */
  166. VictoryCondition();
  167. /** specifies the condition type */
  168. EVictoryConditionType::EVictoryConditionType condition;
  169. /** true if a normal victory is allowed (defeat all enemy towns, heroes) */
  170. bool allowNormalVictory;
  171. /** true if this victory condition also applies to the AI */
  172. bool appliesToAI;
  173. /** 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); monster pos (7); destination pos(8) */
  174. int3 pos;
  175. /** artifact ID (0); monster ID (1); resource ID (2); needed fort level in upgraded town (3); artifact ID (8) */
  176. si32 objectId;
  177. /** needed count for creatures (1) / resource (2); upgraded town hall level (3); */
  178. si32 count;
  179. /** object of specific monster / city / hero instance (NULL if not used); set during map parsing */
  180. const CGObjectInstance * obj;
  181. /**
  182. * Serialize method.
  183. */
  184. template <typename Handler>
  185. void serialize(Handler & h, const int version)
  186. {
  187. h & condition & allowNormalVictory & appliesToAI & pos & objectId & count & obj;
  188. }
  189. };
  190. /**
  191. * The rumor struct consists of a rumor name and text.
  192. */
  193. struct DLL_LINKAGE Rumor
  194. {
  195. /** the name of the rumor */
  196. std::string name;
  197. /** the content of the rumor */
  198. std::string text;
  199. /**
  200. * Serialize method.
  201. */
  202. template <typename Handler>
  203. void serialize(Handler & h, const int version)
  204. {
  205. h & name & text;
  206. }
  207. };
  208. /**
  209. * The disposed hero struct describes which hero can be hired from which player.
  210. */
  211. struct DLL_LINKAGE DisposedHero
  212. {
  213. /**
  214. * Default c-tor.
  215. */
  216. DisposedHero();
  217. /** the id of the hero */
  218. ui32 heroId;
  219. /** the portrait id of the hero, 0xFF is default */
  220. ui16 portrait;
  221. /** the name of the hero */
  222. std::string name;
  223. /** who can hire this hero (bitfield) */
  224. ui8 players;
  225. /**
  226. * Serialize method.
  227. */
  228. template <typename Handler>
  229. void serialize(Handler & h, const int version)
  230. {
  231. h & heroId & portrait & name & players;
  232. }
  233. };
  234. /// Class which manages map events.
  235. /**
  236. * The map event is an event which gives or takes resources for a specific
  237. * amount of players and can appear regularly or once a time.
  238. */
  239. class DLL_LINKAGE CMapEvent
  240. {
  241. public:
  242. /**
  243. * Default c-tor.
  244. */
  245. CMapEvent();
  246. /**
  247. * Returns true if this map event occurs earlier than the other map event for the first time.
  248. *
  249. * @param other the other map event to compare with
  250. * @return true if this event occurs earlier than the other map event, false if not
  251. */
  252. bool earlierThan(const CMapEvent & other) const;
  253. /**
  254. * Returns true if this map event occurs earlier than or at the same day than the other map event for the first time.
  255. *
  256. * @param other the other map event to compare with
  257. * @return true if this event occurs earlier than or at the same day than the other map event, false if not
  258. */
  259. bool earlierThanOrEqual(const CMapEvent & other) const;
  260. /** the name of the event */
  261. std::string name;
  262. /** the message to display */
  263. std::string message;
  264. /** gained or taken resources */
  265. TResources resources;
  266. /** affected players */
  267. ui8 players;
  268. /** affected humans */
  269. ui8 humanAffected;
  270. /** affacted computer players */
  271. ui8 computerAffected;
  272. /** the day counted continously when the event happens */
  273. ui32 firstOccurence;
  274. /** specifies after how many days the event will occur the next time; 0 if event occurs only one time */
  275. ui32 nextOccurence;
  276. /**
  277. * Serialize method.
  278. */
  279. template <typename Handler>
  280. void serialize(Handler & h, const int version)
  281. {
  282. h & name & message & resources
  283. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  284. }
  285. };
  286. /**
  287. * The castle event builds/adds buildings/creatures for a specific town.
  288. */
  289. class DLL_LINKAGE CCastleEvent: public CMapEvent
  290. {
  291. public:
  292. /**
  293. * Default c-tor.
  294. */
  295. CCastleEvent();
  296. /** build specific buildings */
  297. std::set<si32> buildings;
  298. /** additional creatures in i-th level dwelling */
  299. std::vector<si32> creatures;
  300. /** owner of this event */
  301. CGTownInstance * town;
  302. /**
  303. * Serialize method.
  304. */
  305. template <typename Handler>
  306. void serialize(Handler & h, const int version)
  307. {
  308. h & static_cast<CMapEvent &>(*this);
  309. h & buildings & creatures;
  310. }
  311. };
  312. namespace ETerrainType
  313. {
  314. enum ETerrainType
  315. {
  316. BORDER = -1, DIRT, SAND, GRASS, SNOW, SWAMP,
  317. ROUGH, SUBTERRANEAN, LAVA, WATER, ROCK
  318. };
  319. }
  320. namespace ERiverType
  321. {
  322. enum ERiverType
  323. {
  324. NO_RIVER, CLEAR_RIVER, ICY_RIVER, MUDDY_RIVER, LAVA_RIVER
  325. };
  326. }
  327. namespace ERoadType
  328. {
  329. enum ERoadType
  330. {
  331. NO_ROAD, DIRT_ROAD, GRAVEL_ROAD, COBBLESTONE_ROAD
  332. };
  333. }
  334. /**
  335. * The terrain tile describes the terrain type and the visual representation of the terrain.
  336. * Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  337. */
  338. struct DLL_LINKAGE TerrainTile
  339. {
  340. /**
  341. * Default c-tor.
  342. */
  343. TerrainTile();
  344. /**
  345. * Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  346. *
  347. * @param from
  348. * @return
  349. */
  350. bool entrableTerrain(const TerrainTile * from = NULL) const;
  351. /**
  352. * Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  353. *
  354. * @param allowLand
  355. * @param allowSea
  356. * @return
  357. */
  358. bool entrableTerrain(bool allowLand, bool allowSea) const;
  359. /**
  360. * Checks for blocking objects and terraint type (water / land).
  361. *
  362. * @param from
  363. * @return
  364. */
  365. bool isClear(const TerrainTile * from = NULL) const;
  366. /**
  367. * Gets the ID of the top visitable object or -1 if there is none.
  368. *
  369. * @return the ID of the top visitable object or -1 if there is none
  370. */
  371. int topVisitableId() const;
  372. /**
  373. * Gets true if the terrain type is water.
  374. *
  375. * @return true if the terrain type is water
  376. */
  377. bool isWater() const;
  378. /**
  379. * Gets true if the terrain tile is coastal.
  380. *
  381. * @return true if the terrain tile is coastal
  382. */
  383. bool isCoastal() const;
  384. /**
  385. * Gets true if the terrain tile has favourable winds.
  386. *
  387. * @return true if the terrain tile has favourable winds
  388. */
  389. bool hasFavourableWinds() const;
  390. /** the type of terrain */
  391. ETerrainType::ETerrainType terType;
  392. /** the visual representation of the terrain */
  393. ui8 terView;
  394. /** the type of the river. 0 if there is no river */
  395. ERiverType::ERiverType riverType;
  396. /** the direction of the river */
  397. ui8 riverDir;
  398. /** the type of the road. 0 if there is no river */
  399. ERoadType::ERoadType roadType;
  400. /** the direction of the road */
  401. ui8 roadDir;
  402. /**
  403. * first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  404. * 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  405. */
  406. ui8 extTileFlags;
  407. /** true if it is visitable, false if not */
  408. bool visitable;
  409. /** true if it is blocked, false if not */
  410. bool blocked;
  411. /** pointers to objects which the hero can visit while being on this tile */
  412. std::vector<CGObjectInstance *> visitableObjects;
  413. /** pointers to objects that are blocking this tile */
  414. std::vector<CGObjectInstance *> blockingObjects;
  415. /**
  416. * Serialize method.
  417. */
  418. template <typename Handler>
  419. void serialize(Handler & h, const int version)
  420. {
  421. h & terType & terView & riverType & riverDir & roadType &roadDir & extTileFlags & blocked;
  422. if(!h.saving)
  423. {
  424. visitable = false;
  425. //these flags (and obj vectors) will be restored in map serialization
  426. }
  427. }
  428. };
  429. namespace EMapFormat
  430. {
  431. enum EMapFormat
  432. {
  433. INVALID, WOG=0x33, AB=0x15, ROE=0x0e, SOD=0x1c
  434. };
  435. }
  436. /**
  437. * The map header holds information about loss/victory condition,
  438. * map format, version, players, height, width,...
  439. */
  440. class DLL_LINKAGE CMapHeader
  441. {
  442. public:
  443. /**
  444. * Default constructor.
  445. */
  446. CMapHeader();
  447. /**
  448. * D-tor.
  449. */
  450. virtual ~CMapHeader();
  451. /** The version of the map. The default value is EMapFormat::SOD. */
  452. EMapFormat::EMapFormat version;
  453. /** The height of the map. The default value is 72. */
  454. si32 height;
  455. /** The width of the map. The default value is 72. */
  456. si32 width;
  457. /** Specifies if the map has two levels. The default value is true. */
  458. bool twoLevel;
  459. /** The name of the map. */
  460. std::string name;
  461. /** The description of the map. */
  462. std::string description;
  463. /**
  464. * Specifies the difficulty of the map ranging from 0 easy to 4 impossible.
  465. * The default value is 1 representing a normal map difficulty.
  466. */
  467. ui8 difficulty;
  468. /**
  469. * Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  470. * maximum level for heroes.
  471. */
  472. ui8 levelLimit;
  473. /** Specifies the loss condition. The default value is lose all your towns and heroes. */
  474. LossCondition lossCondition;
  475. /** Specifies the victory condition. The default value is defeat all enemies. */
  476. VictoryCondition victoryCondition;
  477. /** A list containing information about players. */
  478. std::vector<PlayerInfo> players;
  479. /** The number of teams. */
  480. ui8 howManyTeams;
  481. /**
  482. * A list of allowed heroes. The index is the hero id and the value is either 0 for not allowed or 1 for allowed.
  483. * The default value is a list of default allowed heroes. See CHeroHandler::getDefaultAllowedHeroes for more info.
  484. */
  485. std::vector<ui8> allowedHeroes;
  486. /** A list of placeholded heroes. The index is the id of a hero type. */
  487. std::vector<ui16> placeholdedHeroes;
  488. /** Unused. True if there are any playable players on the map. */
  489. bool areAnyPlayers;
  490. /**
  491. * Serialize method.
  492. */
  493. template <typename Handler>
  494. void serialize(Handler & h, const int Version)
  495. {
  496. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  497. h & players & lossCondition & victoryCondition & howManyTeams & allowedHeroes;
  498. }
  499. };
  500. /**
  501. * The map contains the map header, the tiles of the terrain, objects,
  502. * heroes, towns, rumors...
  503. */
  504. class DLL_LINKAGE CMap : public CMapHeader
  505. {
  506. public:
  507. /**
  508. * Default constructor.
  509. */
  510. CMap();
  511. /**
  512. * Destructor.
  513. */
  514. ~CMap();
  515. /**
  516. * Erases an artifact instance.
  517. *
  518. * @param art the artifact to erase
  519. */
  520. void eraseArtifactInstance(CArtifactInstance * art);
  521. /**
  522. * Gets the topmost object or the lowermost object depending on the flag
  523. * lookForHero from the specified position.
  524. *
  525. * @param pos the position of the tile
  526. * @param lookForHero true if you want to get the lowermost object, false if
  527. * you want to get the topmost object
  528. * @return the object at the given position and level
  529. */
  530. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, bool lookForHero);
  531. /**
  532. * Sets the victory/loss condition objectives.
  533. */
  534. void checkForObjectives();
  535. /**
  536. * Adds an visitable/blocking object to a terrain tile.
  537. *
  538. * @param obj the visitable/blocking object to add to a tile
  539. */
  540. void addBlockVisTiles(CGObjectInstance * obj);
  541. /**
  542. * Removes an visitable/blocking object from a terrain tile.
  543. *
  544. * @param obj the visitable/blocking object to remove from a tile
  545. * @param total
  546. */
  547. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  548. /**
  549. * Gets the terrain tile of the specified position.
  550. *
  551. * @param tile the position of the tile
  552. * @return the terrain tile of the specified position
  553. */
  554. TerrainTile & getTile(const int3 & tile);
  555. /**
  556. * Gets the terrain tile as a const of the specified position.
  557. *
  558. * @param tile the position of the tile
  559. * @return the terrain tile as a const of the specified position
  560. */
  561. const TerrainTile & getTile(const int3 & tile) const;
  562. /**
  563. * Gets the hero with the given id.
  564. * @param heroId the hero id
  565. * @return the hero with the given id
  566. */
  567. CGHeroInstance * getHero(int heroId);
  568. /**
  569. * Validates if the position is in the bounds of the map.
  570. *
  571. * @param pos the position to test
  572. * @return true if the position is in the bounds of the map
  573. */
  574. bool isInTheMap(const int3 & pos) const;
  575. /**
  576. * Validates if the tile at the given position is a water terrain type.
  577. *
  578. * @param pos the position to test
  579. * @return true if the tile at the given position is a water terrain type
  580. */
  581. bool isWaterTile(const int3 & pos) const;
  582. /**
  583. * Adds the specified artifact instance to the list of artifacts of this map.
  584. *
  585. * @param art the artifact which should be added to the list of artifacts
  586. */
  587. void addNewArtifactInstance(CArtifactInstance * art);
  588. /** the checksum of the map */
  589. ui32 checksum;
  590. /** a 3-dimensional array of terrain tiles, access is as follows: x, y, level */
  591. TerrainTile*** terrain;
  592. /** list of rumors */
  593. std::vector<Rumor> rumors;
  594. /** list of disposed heroes */
  595. std::vector<DisposedHero> disposedHeroes;
  596. /** list of predefined heroes */
  597. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  598. /** list of .def files with definitions from .h3m (may be custom) */
  599. std::vector<ConstTransitivePtr<CGDefInfo> > customDefs;
  600. /** list of allowed spells, index is the spell id */
  601. std::vector<ui8> allowedSpell;
  602. /** list of allowed artifacts, index is the artifact id */
  603. std::vector<ui8> allowedArtifact;
  604. /** list of allowed abilities, index is the ability id */
  605. std::vector<ui8> allowedAbilities;
  606. /** list of map events */
  607. std::list<ConstTransitivePtr<CMapEvent> > events;
  608. /** specifies the position of the grail */
  609. int3 grailPos;
  610. /** specifies the radius of the grail */
  611. int grailRadious;
  612. /** list of objects */
  613. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  614. /** list of heroes */
  615. std::vector< ConstTransitivePtr<CGHeroInstance> > heroes;
  616. /** list of towns */
  617. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  618. /** list of artifacts */
  619. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  620. /** list of quests */
  621. std::vector< ConstTransitivePtr<CQuest> > quests;
  622. /** associative list to identify which hero/creature id belongs to which object id(index for objects) */
  623. bmap<si32, si32> questIdentifierToId;
  624. /**
  625. * Serialize method.
  626. */
  627. template <typename Handler>
  628. void serialize(Handler &h, const int formatVersion)
  629. {
  630. h & static_cast<CMapHeader&>(*this);
  631. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  632. h & artInstances & quests;
  633. h & questIdentifierToId;
  634. //TODO: viccondetails
  635. if(h.saving)
  636. {
  637. // Save terrain
  638. for(int i = 0; i < width ; ++i)
  639. {
  640. for(int j = 0; j < height ; ++j)
  641. {
  642. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  643. {
  644. h & terrain[i][j][k];
  645. }
  646. }
  647. }
  648. }
  649. else
  650. {
  651. // Load terrain
  652. terrain = new TerrainTile**[width];
  653. for(int ii = 0; ii < width; ++ii)
  654. {
  655. terrain[ii] = new TerrainTile*[height];
  656. for(int jj = 0; jj < height; ++jj)
  657. {
  658. terrain[ii][jj] = new TerrainTile[twoLevel ? 2 : 1];
  659. }
  660. }
  661. for(int i = 0; i < width ; ++i)
  662. {
  663. for(int j = 0; j < height ; ++j)
  664. {
  665. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  666. {
  667. h & terrain[i][j][k];
  668. }
  669. }
  670. }
  671. }
  672. h & customDefs & objects;
  673. // static members
  674. h & CGTeleport::objs;
  675. h & CGTeleport::gates;
  676. h & CGKeys::playerKeyMap;
  677. h & CGMagi::eyelist;
  678. h & CGObelisk::obeliskCount & CGObelisk::visited;
  679. h & CGTownInstance::merchantArtifacts;
  680. h & CGTownInstance::universitySkills;
  681. if(!h.saving)
  682. {
  683. for(ui32 i = 0; i < objects.size(); ++i)
  684. {
  685. if(!objects[i]) continue;
  686. switch (objects[i]->ID)
  687. {
  688. case Obj::HERO:
  689. heroes.push_back (static_cast<CGHeroInstance*>(+objects[i]));
  690. break;
  691. case Obj::TOWN:
  692. towns.push_back (static_cast<CGTownInstance*>(+objects[i]));
  693. break;
  694. }
  695. // recreate blockvis map
  696. addBlockVisTiles(objects[i]);
  697. }
  698. // if hero is visiting/garrisoned in town set appropriate pointers
  699. for(ui32 i = 0; i < heroes.size(); ++i)
  700. {
  701. int3 vistile = heroes[i]->pos;
  702. vistile.x++;
  703. for(ui32 j = 0; j < towns.size(); ++j)
  704. {
  705. // hero stands on the town entrance
  706. if(vistile == towns[j]->pos)
  707. {
  708. if(heroes[i]->inTownGarrison)
  709. {
  710. towns[j]->garrisonHero = heroes[i];
  711. removeBlockVisTiles(heroes[i]);
  712. }
  713. else
  714. {
  715. towns[j]->visitingHero = heroes[i];
  716. }
  717. heroes[i]->visitedTown = towns[j];
  718. break;
  719. }
  720. }
  721. vistile.x -= 2; //manifest pos
  722. const TerrainTile & t = getTile(vistile);
  723. if(t.terType != ETerrainType::WATER) continue;
  724. //hero stands on the water - he must be in the boat
  725. for(ui32 j = 0; j < t.visitableObjects.size(); ++j)
  726. {
  727. if(t.visitableObjects[j]->ID == Obj::BOAT)
  728. {
  729. CGBoat * b = static_cast<CGBoat *>(t.visitableObjects[j]);
  730. heroes[i]->boat = b;
  731. b->hero = heroes[i];
  732. removeBlockVisTiles(b);
  733. break;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. };