CMap.h 14 KB

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