CMap.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  221. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  222. struct DLL_LINKAGE TerrainTile
  223. {
  224. TerrainTile();
  225. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  226. bool entrableTerrain(const TerrainTile * from = nullptr) const;
  227. bool entrableTerrain(bool allowLand, bool allowSea) const;
  228. /// Checks for blocking objects and terraint type (water / land).
  229. bool isClear(const TerrainTile * from = nullptr) const;
  230. /// Gets the ID of the top visitable object or -1 if there is none.
  231. Obj topVisitableId(bool excludeTop = false) const;
  232. CGObjectInstance * topVisitableObj(bool excludeTop = false) const;
  233. bool isWater() const;
  234. bool isCoastal() const;
  235. EDiggingStatus getDiggingStatus(const bool excludeTop = true) const;
  236. bool hasFavourableWinds() const;
  237. ETerrainType terType;
  238. ui8 terView;
  239. ERiverType::ERiverType riverType;
  240. ui8 riverDir;
  241. ERoadType::ERoadType roadType;
  242. ui8 roadDir;
  243. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  244. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favourable Winds effect
  245. ui8 extTileFlags;
  246. bool visitable;
  247. bool blocked;
  248. std::vector<CGObjectInstance *> visitableObjects;
  249. std::vector<CGObjectInstance *> blockingObjects;
  250. template <typename Handler>
  251. void serialize(Handler & h, const int version)
  252. {
  253. h & terType & terView & riverType & riverDir & roadType &roadDir & extTileFlags;
  254. h & visitable & blocked;
  255. h & visitableObjects & blockingObjects;
  256. }
  257. };
  258. namespace EMapFormat
  259. {
  260. enum EMapFormat
  261. {
  262. INVALID = 0,
  263. // HEX DEC
  264. ROE = 0x0e, // 14
  265. AB = 0x15, // 21
  266. SOD = 0x1c, // 28
  267. // HOTA = 0x1e ... 0x20 // 28 ... 30
  268. WOG = 0x33 // 51
  269. };
  270. }
  271. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  272. class DLL_LINKAGE CMapHeader
  273. {
  274. void setupEvents();
  275. public:
  276. static const int MAP_SIZE_SMALL;
  277. static const int MAP_SIZE_MIDDLE;
  278. static const int MAP_SIZE_LARGE;
  279. static const int MAP_SIZE_XLARGE;
  280. CMapHeader();
  281. virtual ~CMapHeader();
  282. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  283. si32 height; /// The default value is 72.
  284. si32 width; /// The default value is 72.
  285. bool twoLevel; /// The default value is true.
  286. std::string name;
  287. std::string description;
  288. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  289. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  290. /// maximum level for heroes. This is the default value.
  291. ui8 levelLimit;
  292. std::string victoryMessage;
  293. std::string defeatMessage;
  294. ui16 victoryIconIndex;
  295. ui16 defeatIconIndex;
  296. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  297. ui8 howManyTeams;
  298. std::vector<bool> allowedHeroes;
  299. std::vector<ui16> placeholdedHeroes;
  300. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  301. /// "main quests" of the map that describe victory and loss conditions
  302. std::vector<TriggeredEvent> triggeredEvents;
  303. template <typename Handler>
  304. void serialize(Handler & h, const int Version)
  305. {
  306. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  307. h & players & howManyTeams & allowedHeroes & triggeredEvents;
  308. h & victoryMessage & victoryIconIndex & defeatMessage & defeatIconIndex;
  309. }
  310. };
  311. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  312. class DLL_LINKAGE CMap : public CMapHeader
  313. {
  314. public:
  315. CMap();
  316. ~CMap();
  317. void initTerrain();
  318. CMapEditManager * getEditManager();
  319. TerrainTile & getTile(const int3 & tile);
  320. const TerrainTile & getTile(const int3 & tile) const;
  321. bool isInTheMap(const int3 & pos) const;
  322. bool isWaterTile(const int3 & pos) const;
  323. bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
  324. int3 guardingCreaturePosition (int3 pos) const;
  325. void addBlockVisTiles(CGObjectInstance * obj);
  326. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  327. void calculateGuardingGreaturePositions();
  328. void addNewArtifactInstance(CArtifactInstance * art);
  329. void eraseArtifactInstance(CArtifactInstance * art);
  330. void addQuest(CGObjectInstance * quest);
  331. /// Gets object of specified type on requested position
  332. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, Obj::EObj type);
  333. CGHeroInstance * getHero(int heroId);
  334. /// Sets the victory/loss condition objectives ??
  335. void checkForObjectives();
  336. ui32 checksum;
  337. std::vector<Rumor> rumors;
  338. std::vector<DisposedHero> disposedHeroes;
  339. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  340. std::vector<bool> allowedSpell;
  341. std::vector<bool> allowedArtifact;
  342. std::vector<bool> allowedAbilities;
  343. std::list<CMapEvent> events;
  344. int3 grailPos;
  345. int grailRadious;
  346. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  347. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  348. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  349. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  350. std::vector< ConstTransitivePtr<CQuest> > quests;
  351. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  352. //Helper lists
  353. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  354. std::map<TeleportChannelID, shared_ptr<TeleportChannel> > teleportChannels;
  355. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  356. std::map<si32, ObjectInstanceID> questIdentifierToId;
  357. unique_ptr<CMapEditManager> editManager;
  358. int3 ***guardingCreaturePositions;
  359. private:
  360. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  361. TerrainTile*** terrain;
  362. public:
  363. template <typename Handler>
  364. void serialize(Handler &h, const int formatVersion)
  365. {
  366. h & static_cast<CMapHeader&>(*this);
  367. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  368. h & artInstances & quests & allHeroes;
  369. h & questIdentifierToId;
  370. //TODO: viccondetails
  371. int level = twoLevel ? 2 : 1;
  372. if(h.saving)
  373. {
  374. // Save terrain
  375. for(int i = 0; i < width ; ++i)
  376. {
  377. for(int j = 0; j < height ; ++j)
  378. {
  379. for(int k = 0; k < level; ++k)
  380. {
  381. h & terrain[i][j][k];
  382. h & guardingCreaturePositions[i][j][k];
  383. }
  384. }
  385. }
  386. }
  387. else
  388. {
  389. // Load terrain
  390. terrain = new TerrainTile**[width];
  391. guardingCreaturePositions = new int3**[width];
  392. for(int i = 0; i < width; ++i)
  393. {
  394. terrain[i] = new TerrainTile*[height];
  395. guardingCreaturePositions[i] = new int3*[height];
  396. for(int j = 0; j < height; ++j)
  397. {
  398. terrain[i][j] = new TerrainTile[level];
  399. guardingCreaturePositions[i][j] = new int3[level];
  400. }
  401. }
  402. for(int i = 0; i < width ; ++i)
  403. {
  404. for(int j = 0; j < height ; ++j)
  405. {
  406. for(int k = 0; k < level; ++k)
  407. {
  408. h & terrain[i][j][k];
  409. h & guardingCreaturePositions[i][j][k];
  410. }
  411. }
  412. }
  413. }
  414. h & objects;
  415. h & heroesOnMap & teleportChannels & towns & artInstances;
  416. // static members
  417. h & CGKeys::playerKeyMap;
  418. h & CGMagi::eyelist;
  419. h & CGObelisk::obeliskCount & CGObelisk::visited;
  420. h & CGTownInstance::merchantArtifacts;
  421. h & CGTownInstance::universitySkills;
  422. }
  423. };