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