CMap.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 "../mapObjects/MiscObjects.h" // To serialize static props
  13. #include "../mapObjects/CQuest.h" // To serialize static props
  14. #include "../mapObjects/CGTownInstance.h" // To serialize static props
  15. #include "../ResourceSet.h"
  16. #include "../int3.h"
  17. #include "../GameConstants.h"
  18. #include "../LogicalExpression.h"
  19. class CArtifactInstance;
  20. class CGObjectInstance;
  21. class CGHeroInstance;
  22. class CCommanderInstance;
  23. class CGCreature;
  24. class CQuest;
  25. class CGTownInstance;
  26. class IModableArt;
  27. class IQuestObject;
  28. class CInputStream;
  29. class CMapEditManager;
  30. /// The hero name struct consists of the hero id and the hero name.
  31. struct DLL_LINKAGE SHeroName
  32. {
  33. SHeroName();
  34. int heroId;
  35. std::string heroName;
  36. template <typename Handler>
  37. void serialize(Handler & h, const int version)
  38. {
  39. h & heroId & heroName;
  40. }
  41. };
  42. namespace EAiTactic
  43. {
  44. enum EAiTactic
  45. {
  46. NONE = -1,
  47. RANDOM,
  48. WARRIOR,
  49. BUILDER,
  50. EXPLORER
  51. };
  52. }
  53. /// The player info constains data about which factions are allowed, AI tactical settings,
  54. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  55. struct DLL_LINKAGE PlayerInfo
  56. {
  57. PlayerInfo();
  58. /// Gets the default faction id or -1 for a random faction.
  59. si8 defaultCastle() const;
  60. /// Gets the default hero id or -1 for a random hero.
  61. si8 defaultHero() const;
  62. bool canAnyonePlay() const;
  63. bool hasCustomMainHero() const;
  64. bool canHumanPlay;
  65. bool canComputerPlay;
  66. EAiTactic::EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  67. std::set<TFaction> allowedFactions;
  68. bool isFactionRandom;
  69. si32 mainCustomHeroPortrait; /// The default value is -1.
  70. std::string mainCustomHeroName;
  71. si32 mainCustomHeroId; /// ID of custom hero (only if portrait and hero name are set, otherwise unpredicted value), -1 if none (not always -1)
  72. std::vector<SHeroName> heroesNames; /// list of placed heroes on the map
  73. bool hasMainTown; /// The default value is false.
  74. bool generateHeroAtMainTown; /// The default value is false.
  75. int3 posOfMainTown;
  76. TeamID team; /// The default value is 255 representing that the player belongs to no team.
  77. bool hasRandomHero; /// Player has a random hero
  78. bool generateHero; /// Unused.
  79. si32 p7; /// Unknown and unused.
  80. /// Unused. Count of hero placeholders containing hero type.
  81. /// WARNING: powerPlaceholders sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning ???
  82. ui8 powerPlaceholders;
  83. template <typename Handler>
  84. void serialize(Handler & h, const int version)
  85. {
  86. h & p7 & hasRandomHero & mainCustomHeroId & canHumanPlay & canComputerPlay & aiTactic & allowedFactions & isFactionRandom &
  87. mainCustomHeroPortrait & mainCustomHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  88. posOfMainTown & team & generateHero;
  89. }
  90. };
  91. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  92. struct DLL_LINKAGE EventCondition
  93. {
  94. enum EWinLoseType {
  95. HAVE_ARTIFACT, // type - required artifact
  96. HAVE_CREATURES, // type - creatures to collect, value - amount to collect
  97. HAVE_RESOURCES, // type - resource ID, value - amount to collect
  98. HAVE_BUILDING, // position - town, optional, type - building to build
  99. CONTROL, // position - position of object, optional, type - type of object
  100. DESTROY, // position - position of object, optional, type - type of object
  101. TRANSPORT, // position - where artifact should be transported, type - type of artifact
  102. DAYS_PASSED, // value - number of days from start of the game
  103. IS_HUMAN, // value - 0 = player is AI, 1 = player is human
  104. DAYS_WITHOUT_TOWN, // value - how long player can live without town, 0=instakill
  105. STANDARD_WIN, // normal defeat all enemies condition
  106. CONST_VALUE // condition that always evaluates to "value" (0 = false, 1 = true)
  107. };
  108. EventCondition(EWinLoseType condition = STANDARD_WIN);
  109. EventCondition(EWinLoseType condition, si32 value, si32 objectType, int3 position = int3(-1, -1, -1));
  110. const CGObjectInstance * object; // object that was at specified position on start
  111. si32 value;
  112. si32 objectType;
  113. int3 position;
  114. EWinLoseType condition;
  115. template <typename Handler>
  116. void serialize(Handler & h, const int version)
  117. {
  118. h & object & value & objectType & position & condition;
  119. }
  120. };
  121. typedef LogicalExpression<EventCondition> EventExpression;
  122. struct DLL_LINKAGE EventEffect
  123. {
  124. enum EType
  125. {
  126. VICTORY,
  127. DEFEAT
  128. };
  129. /// effect type, using EType enum
  130. si8 type;
  131. /// message that will be sent to other players
  132. std::string toOtherMessage;
  133. template <typename Handler>
  134. void serialize(Handler & h, const int version)
  135. {
  136. h & type & toOtherMessage;
  137. }
  138. };
  139. struct DLL_LINKAGE TriggeredEvent
  140. {
  141. /// base condition that must be evaluated
  142. EventExpression trigger;
  143. /// string identifier read from config file (e.g. captureKreelah)
  144. std::string identifier;
  145. /// string-description, for use in UI (capture town to win)
  146. std::string description;
  147. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  148. std::string onFulfill;
  149. /// Effect of this event. TODO: refactor into something more flexible
  150. EventEffect effect;
  151. template <typename Handler>
  152. void serialize(Handler & h, const int version)
  153. {
  154. h & identifier & trigger & description & onFulfill & effect;
  155. }
  156. };
  157. /// The rumor struct consists of a rumor name and text.
  158. struct DLL_LINKAGE Rumor
  159. {
  160. std::string name;
  161. std::string text;
  162. template <typename Handler>
  163. void serialize(Handler & h, const int version)
  164. {
  165. h & name & text;
  166. }
  167. };
  168. /// The disposed hero struct describes which hero can be hired from which player.
  169. struct DLL_LINKAGE DisposedHero
  170. {
  171. DisposedHero();
  172. ui32 heroId;
  173. ui16 portrait; /// The portrait id of the hero, 0xFF is default.
  174. std::string name;
  175. ui8 players; /// Who can hire this hero (bitfield).
  176. template <typename Handler>
  177. void serialize(Handler & h, const int version)
  178. {
  179. h & heroId & portrait & name & players;
  180. }
  181. };
  182. /// The map event is an event which e.g. gives or takes resources of a specific
  183. /// amount to/from players and can appear regularly or once a time.
  184. class DLL_LINKAGE CMapEvent
  185. {
  186. public:
  187. CMapEvent();
  188. bool earlierThan(const CMapEvent & other) const;
  189. bool earlierThanOrEqual(const CMapEvent & other) const;
  190. std::string name;
  191. std::string message;
  192. TResources resources;
  193. ui8 players; // affected players, bit field?
  194. ui8 humanAffected;
  195. ui8 computerAffected;
  196. ui32 firstOccurence;
  197. ui32 nextOccurence; /// specifies after how many days the event will occur the next time; 0 if event occurs only one time
  198. template <typename Handler>
  199. void serialize(Handler & h, const int version)
  200. {
  201. h & name & message & resources
  202. & players & humanAffected & computerAffected & firstOccurence & nextOccurence;
  203. }
  204. };
  205. /// The castle event builds/adds buildings/creatures for a specific town.
  206. class DLL_LINKAGE CCastleEvent: public CMapEvent
  207. {
  208. public:
  209. CCastleEvent();
  210. std::set<BuildingID> buildings;
  211. std::vector<si32> creatures;
  212. CGTownInstance * town;
  213. template <typename Handler>
  214. void serialize(Handler & h, const int version)
  215. {
  216. h & static_cast<CMapEvent &>(*this);
  217. h & buildings & creatures;
  218. }
  219. };
  220. namespace ERiverType
  221. {
  222. enum ERiverType
  223. {
  224. NO_RIVER, CLEAR_RIVER, ICY_RIVER, MUDDY_RIVER, LAVA_RIVER
  225. };
  226. }
  227. namespace ERoadType
  228. {
  229. enum ERoadType
  230. {
  231. NO_ROAD, DIRT_ROAD, GRAVEL_ROAD, COBBLESTONE_ROAD
  232. };
  233. }
  234. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  235. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  236. struct DLL_LINKAGE TerrainTile
  237. {
  238. TerrainTile();
  239. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  240. bool entrableTerrain(const TerrainTile * from = nullptr) const;
  241. bool entrableTerrain(bool allowLand, bool allowSea) const;
  242. /// Checks for blocking objects and terraint type (water / land).
  243. bool isClear(const TerrainTile * from = nullptr) const;
  244. /// Gets the ID of the top visitable object or -1 if there is none.
  245. Obj topVisitableId(bool excludeTop = false) const;
  246. CGObjectInstance * topVisitableObj(bool excludeTop = false) const;
  247. bool isWater() const;
  248. bool isCoastal() const;
  249. bool hasFavourableWinds() const;
  250. ETerrainType terType;
  251. ui8 terView;
  252. ERiverType::ERiverType riverType;
  253. ui8 riverDir;
  254. ERoadType::ERoadType roadType;
  255. ui8 roadDir;
  256. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  257. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  258. ui8 extTileFlags;
  259. bool visitable;
  260. bool blocked;
  261. std::vector<CGObjectInstance *> visitableObjects;
  262. std::vector<CGObjectInstance *> blockingObjects;
  263. template <typename Handler>
  264. void serialize(Handler & h, const int version)
  265. {
  266. h & terType & terView & riverType & riverDir & roadType &roadDir & extTileFlags;
  267. h & visitable & blocked;
  268. h & visitableObjects & blockingObjects;
  269. }
  270. };
  271. namespace EMapFormat
  272. {
  273. enum EMapFormat
  274. {
  275. INVALID = 0,
  276. // HEX DEC
  277. ROE = 0x0e, // 14
  278. AB = 0x15, // 21
  279. SOD = 0x1c, // 28
  280. // HOTA = 0x1e ... 0x20 // 28 ... 30
  281. WOG = 0x33 // 51
  282. };
  283. }
  284. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  285. class DLL_LINKAGE CMapHeader
  286. {
  287. void setupEvents();
  288. public:
  289. static const int MAP_SIZE_SMALL;
  290. static const int MAP_SIZE_MIDDLE;
  291. static const int MAP_SIZE_LARGE;
  292. static const int MAP_SIZE_XLARGE;
  293. CMapHeader();
  294. virtual ~CMapHeader();
  295. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  296. si32 height; /// The default value is 72.
  297. si32 width; /// The default value is 72.
  298. bool twoLevel; /// The default value is true.
  299. std::string name;
  300. std::string description;
  301. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  302. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  303. /// maximum level for heroes. This is the default value.
  304. ui8 levelLimit;
  305. std::string victoryMessage;
  306. std::string defeatMessage;
  307. ui16 victoryIconIndex;
  308. ui16 defeatIconIndex;
  309. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  310. ui8 howManyTeams;
  311. std::vector<bool> allowedHeroes;
  312. std::vector<ui16> placeholdedHeroes;
  313. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  314. /// "main quests" of the map that describe victory and loss conditions
  315. std::vector<TriggeredEvent> triggeredEvents;
  316. template <typename Handler>
  317. void serialize(Handler & h, const int Version)
  318. {
  319. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  320. h & players & howManyTeams & allowedHeroes & triggeredEvents;
  321. h & victoryMessage & victoryIconIndex & defeatMessage & defeatIconIndex;
  322. }
  323. };
  324. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  325. class DLL_LINKAGE CMap : public CMapHeader
  326. {
  327. public:
  328. CMap();
  329. ~CMap();
  330. void initTerrain();
  331. CMapEditManager * getEditManager();
  332. TerrainTile & getTile(const int3 & tile);
  333. const TerrainTile & getTile(const int3 & tile) const;
  334. bool isInTheMap(const int3 & pos) const;
  335. bool isWaterTile(const int3 & pos) const;
  336. bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
  337. int3 guardingCreaturePosition (int3 pos) const;
  338. void addBlockVisTiles(CGObjectInstance * obj);
  339. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  340. void calculateGuardingGreaturePositions();
  341. void addNewArtifactInstance(CArtifactInstance * art);
  342. void eraseArtifactInstance(CArtifactInstance * art);
  343. void addQuest(CGObjectInstance * quest);
  344. /// Gets object of specified type on requested position
  345. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, Obj::EObj type);
  346. CGHeroInstance * getHero(int heroId);
  347. /// Sets the victory/loss condition objectives ??
  348. void checkForObjectives();
  349. ui32 checksum;
  350. std::vector<Rumor> rumors;
  351. std::vector<DisposedHero> disposedHeroes;
  352. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  353. std::vector<bool> allowedSpell;
  354. std::vector<bool> allowedArtifact;
  355. std::vector<bool> allowedAbilities;
  356. std::list<CMapEvent> events;
  357. int3 grailPos;
  358. int grailRadious;
  359. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  360. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  361. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  362. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  363. std::vector< ConstTransitivePtr<CQuest> > quests;
  364. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  365. //Helper lists
  366. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  367. std::map<TeleportChannelID, shared_ptr<TeleportChannel> > teleportChannels;
  368. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  369. std::map<si32, ObjectInstanceID> questIdentifierToId;
  370. unique_ptr<CMapEditManager> editManager;
  371. int3 ***guardingCreaturePositions;
  372. private:
  373. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  374. TerrainTile*** terrain;
  375. public:
  376. template <typename Handler>
  377. void serialize(Handler &h, const int formatVersion)
  378. {
  379. h & static_cast<CMapHeader&>(*this);
  380. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  381. h & artInstances & quests & allHeroes;
  382. h & questIdentifierToId;
  383. //TODO: viccondetails
  384. int level = twoLevel ? 2 : 1;
  385. if(h.saving)
  386. {
  387. // Save terrain
  388. for(int i = 0; i < width ; ++i)
  389. {
  390. for(int j = 0; j < height ; ++j)
  391. {
  392. for(int k = 0; k < level; ++k)
  393. {
  394. h & terrain[i][j][k];
  395. h & guardingCreaturePositions[i][j][k];
  396. }
  397. }
  398. }
  399. }
  400. else
  401. {
  402. // Load terrain
  403. terrain = new TerrainTile**[width];
  404. guardingCreaturePositions = new int3**[width];
  405. for(int i = 0; i < width; ++i)
  406. {
  407. terrain[i] = new TerrainTile*[height];
  408. guardingCreaturePositions[i] = new int3*[height];
  409. for(int j = 0; j < height; ++j)
  410. {
  411. terrain[i][j] = new TerrainTile[level];
  412. guardingCreaturePositions[i][j] = new int3[level];
  413. }
  414. }
  415. for(int i = 0; i < width ; ++i)
  416. {
  417. for(int j = 0; j < height ; ++j)
  418. {
  419. for(int k = 0; k < level; ++k)
  420. {
  421. h & terrain[i][j][k];
  422. h & guardingCreaturePositions[i][j][k];
  423. }
  424. }
  425. }
  426. }
  427. h & objects;
  428. h & heroesOnMap & teleportChannels & towns & artInstances;
  429. // static members
  430. h & CGKeys::playerKeyMap;
  431. h & CGMagi::eyelist;
  432. h & CGObelisk::obeliskCount & CGObelisk::visited;
  433. h & CGTownInstance::merchantArtifacts;
  434. h & CGTownInstance::universitySkills;
  435. }
  436. };