CMap.h 21 KB

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