CMap.h 15 KB

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