CMap.h 13 KB

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