CMap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 (nullptr 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 = nullptr) 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 = nullptr) 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;
  234. h & visitable & blocked;
  235. h & visitableObjects & blockingObjects;
  236. }
  237. };
  238. namespace EMapFormat
  239. {
  240. enum EMapFormat
  241. {
  242. INVALID = 0,
  243. // HEX DEC
  244. ROE = 0x0e, // 14
  245. AB = 0x15, // 21
  246. SOD = 0x1c, // 28
  247. WOG = 0x33 // 51
  248. };
  249. }
  250. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  251. class DLL_LINKAGE CMapHeader
  252. {
  253. public:
  254. static const int MAP_SIZE_SMALL;
  255. static const int MAP_SIZE_MIDDLE;
  256. static const int MAP_SIZE_LARGE;
  257. static const int MAP_SIZE_XLARGE;
  258. CMapHeader();
  259. virtual ~CMapHeader();
  260. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  261. si32 height; /// The default value is 72.
  262. si32 width; /// The default value is 72.
  263. bool twoLevel; /// The default value is true.
  264. std::string name;
  265. std::string description;
  266. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  267. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  268. /// maximum level for heroes. This is the default value.
  269. ui8 levelLimit;
  270. LossCondition lossCondition; /// The default value is lose all your towns and heroes.
  271. VictoryCondition victoryCondition; /// The default value is defeat all enemies.
  272. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  273. ui8 howManyTeams;
  274. std::vector<bool> allowedHeroes;
  275. std::vector<ui16> placeholdedHeroes;
  276. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  277. template <typename Handler>
  278. void serialize(Handler & h, const int Version)
  279. {
  280. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  281. h & players & lossCondition & victoryCondition & howManyTeams & allowedHeroes;
  282. }
  283. };
  284. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  285. class DLL_LINKAGE CMap : public CMapHeader
  286. {
  287. public:
  288. CMap();
  289. ~CMap();
  290. void initTerrain();
  291. CMapEditManager * getEditManager();
  292. TerrainTile & getTile(const int3 & tile);
  293. const TerrainTile & getTile(const int3 & tile) const;
  294. bool isInTheMap(const int3 & pos) const;
  295. bool isWaterTile(const int3 & pos) const;
  296. void addBlockVisTiles(CGObjectInstance * obj);
  297. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  298. void addNewArtifactInstance(CArtifactInstance * art);
  299. void eraseArtifactInstance(CArtifactInstance * art);
  300. void addQuest(CGObjectInstance * quest);
  301. /// Gets the topmost object or the lowermost object depending on the flag lookForHero from the specified position.
  302. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, bool lookForHero);
  303. CGHeroInstance * getHero(int heroId);
  304. /// Sets the victory/loss condition objectives ??
  305. void checkForObjectives();
  306. ui32 checksum;
  307. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  308. std::vector<Rumor> rumors;
  309. std::vector<DisposedHero> disposedHeroes;
  310. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  311. std::vector<ConstTransitivePtr<CGDefInfo> > customDefs;
  312. std::vector<bool> allowedSpell;
  313. std::vector<bool> allowedArtifact;
  314. std::vector<bool> allowedAbilities;
  315. std::list<CMapEvent> events;
  316. int3 grailPos;
  317. int grailRadious;
  318. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  319. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  320. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  321. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  322. std::vector< ConstTransitivePtr<CQuest> > quests;
  323. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  324. //Helper lists
  325. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  326. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  327. bmap<si32, ObjectInstanceID> questIdentifierToId;
  328. unique_ptr<CMapEditManager> editManager;
  329. private:
  330. TerrainTile*** terrain;
  331. public:
  332. template <typename Handler>
  333. void serialize(Handler &h, const int formatVersion)
  334. {
  335. h & static_cast<CMapHeader&>(*this);
  336. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  337. h & artInstances & quests & allHeroes;
  338. h & questIdentifierToId;
  339. //TODO: viccondetails
  340. if(h.saving)
  341. {
  342. // Save terrain
  343. for(int i = 0; i < width ; ++i)
  344. {
  345. for(int j = 0; j < height ; ++j)
  346. {
  347. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  348. {
  349. h & terrain[i][j][k];
  350. }
  351. }
  352. }
  353. }
  354. else
  355. {
  356. // Load terrain
  357. terrain = new TerrainTile**[width];
  358. for(int ii = 0; ii < width; ++ii)
  359. {
  360. terrain[ii] = new TerrainTile*[height];
  361. for(int jj = 0; jj < height; ++jj)
  362. {
  363. terrain[ii][jj] = new TerrainTile[twoLevel ? 2 : 1];
  364. }
  365. }
  366. for(int i = 0; i < width ; ++i)
  367. {
  368. for(int j = 0; j < height ; ++j)
  369. {
  370. for(int k = 0; k < (twoLevel ? 2 : 1); ++k)
  371. {
  372. h & terrain[i][j][k];
  373. }
  374. }
  375. }
  376. }
  377. h & customDefs & objects;
  378. h & heroesOnMap & towns & artInstances;
  379. // static members
  380. h & CGTeleport::objs;
  381. h & CGTeleport::gates;
  382. h & CGKeys::playerKeyMap;
  383. h & CGMagi::eyelist;
  384. h & CGObelisk::obeliskCount & CGObelisk::visited;
  385. h & CGTownInstance::merchantArtifacts;
  386. h & CGTownInstance::universitySkills;
  387. }
  388. };