CMap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #include "CMapDefines.h"
  20. class CArtifactInstance;
  21. class CGObjectInstance;
  22. class CGHeroInstance;
  23. class CCommanderInstance;
  24. class CGCreature;
  25. class CQuest;
  26. class CGTownInstance;
  27. class IModableArt;
  28. class IQuestObject;
  29. class CInputStream;
  30. class CMapEditManager;
  31. /// The hero name struct consists of the hero id and the hero name.
  32. struct DLL_LINKAGE SHeroName
  33. {
  34. SHeroName();
  35. int heroId;
  36. std::string heroName;
  37. template <typename Handler>
  38. void serialize(Handler & h, const int version)
  39. {
  40. h & heroId & heroName;
  41. }
  42. };
  43. /// The player info constains data about which factions are allowed, AI tactical settings,
  44. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  45. struct DLL_LINKAGE PlayerInfo
  46. {
  47. PlayerInfo();
  48. /// Gets the default faction id or -1 for a random faction.
  49. si8 defaultCastle() const;
  50. /// Gets the default hero id or -1 for a random hero.
  51. si8 defaultHero() const;
  52. bool canAnyonePlay() const;
  53. bool hasCustomMainHero() const;
  54. bool canHumanPlay;
  55. bool canComputerPlay;
  56. EAiTactic::EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  57. std::set<TFaction> allowedFactions;
  58. bool isFactionRandom;
  59. si32 mainCustomHeroPortrait; /// The default value is -1.
  60. std::string mainCustomHeroName;
  61. si32 mainCustomHeroId; /// ID of custom hero (only if portrait and hero name are set, otherwise unpredicted value), -1 if none (not always -1)
  62. std::vector<SHeroName> heroesNames; /// list of placed heroes on the map
  63. bool hasMainTown; /// The default value is false.
  64. bool generateHeroAtMainTown; /// The default value is false.
  65. int3 posOfMainTown;
  66. TeamID team; /// The default value NO_TEAM
  67. bool hasRandomHero; /// Player has a random hero
  68. bool generateHero; /// Unused.
  69. si32 p7; /// Unknown and unused.
  70. /// Unused. Count of hero placeholders containing hero type.
  71. /// WARNING: powerPlaceholders sometimes gives false 0 (eg. even if there is one placeholder), maybe different meaning ???
  72. ui8 powerPlaceholders;
  73. template <typename Handler>
  74. void serialize(Handler & h, const int version)
  75. {
  76. h & p7 & hasRandomHero & mainCustomHeroId & canHumanPlay & canComputerPlay & aiTactic & allowedFactions & isFactionRandom &
  77. mainCustomHeroPortrait & mainCustomHeroName & heroesNames & hasMainTown & generateHeroAtMainTown &
  78. posOfMainTown & team & generateHero;
  79. }
  80. };
  81. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  82. struct DLL_LINKAGE EventCondition
  83. {
  84. enum EWinLoseType {
  85. HAVE_ARTIFACT, // type - required artifact
  86. HAVE_CREATURES, // type - creatures to collect, value - amount to collect
  87. HAVE_RESOURCES, // type - resource ID, value - amount to collect
  88. HAVE_BUILDING, // position - town, optional, type - building to build
  89. CONTROL, // position - position of object, optional, type - type of object
  90. DESTROY, // position - position of object, optional, type - type of object
  91. TRANSPORT, // position - where artifact should be transported, type - type of artifact
  92. DAYS_PASSED, // value - number of days from start of the game
  93. IS_HUMAN, // value - 0 = player is AI, 1 = player is human
  94. DAYS_WITHOUT_TOWN, // value - how long player can live without town, 0=instakill
  95. STANDARD_WIN, // normal defeat all enemies condition
  96. CONST_VALUE // condition that always evaluates to "value" (0 = false, 1 = true)
  97. };
  98. EventCondition(EWinLoseType condition = STANDARD_WIN);
  99. EventCondition(EWinLoseType condition, si32 value, si32 objectType, int3 position = int3(-1, -1, -1));
  100. const CGObjectInstance * object; // object that was at specified position on start
  101. si32 value;
  102. si32 objectType;
  103. int3 position;
  104. EWinLoseType condition;
  105. template <typename Handler>
  106. void serialize(Handler & h, const int version)
  107. {
  108. h & object;
  109. h & value;
  110. h & objectType;
  111. h & position;
  112. h & condition;
  113. }
  114. };
  115. typedef LogicalExpression<EventCondition> EventExpression;
  116. struct DLL_LINKAGE EventEffect
  117. {
  118. enum EType
  119. {
  120. VICTORY,
  121. DEFEAT
  122. };
  123. /// effect type, using EType enum
  124. si8 type;
  125. /// message that will be sent to other players
  126. std::string toOtherMessage;
  127. template <typename Handler>
  128. void serialize(Handler & h, const int version)
  129. {
  130. h & type & toOtherMessage;
  131. }
  132. };
  133. struct DLL_LINKAGE TriggeredEvent
  134. {
  135. /// base condition that must be evaluated
  136. EventExpression trigger;
  137. /// string identifier read from config file (e.g. captureKreelah)
  138. std::string identifier;
  139. /// string-description, for use in UI (capture town to win)
  140. std::string description;
  141. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  142. std::string onFulfill;
  143. /// Effect of this event. TODO: refactor into something more flexible
  144. EventEffect effect;
  145. template <typename Handler>
  146. void serialize(Handler & h, const int version)
  147. {
  148. h & identifier;
  149. h & trigger;
  150. h & description;
  151. h & onFulfill & effect;
  152. }
  153. };
  154. /// The rumor struct consists of a rumor name and text.
  155. struct DLL_LINKAGE Rumor
  156. {
  157. std::string name;
  158. std::string text;
  159. template <typename Handler>
  160. void serialize(Handler & h, const int version)
  161. {
  162. h & name & text;
  163. }
  164. };
  165. /// The disposed hero struct describes which hero can be hired from which player.
  166. struct DLL_LINKAGE DisposedHero
  167. {
  168. DisposedHero();
  169. ui32 heroId;
  170. ui16 portrait; /// The portrait id of the hero, 0xFF is default.
  171. std::string name;
  172. ui8 players; /// Who can hire this hero (bitfield).
  173. template <typename Handler>
  174. void serialize(Handler & h, const int version)
  175. {
  176. h & heroId & portrait & name & players;
  177. }
  178. };
  179. namespace EMapFormat
  180. {
  181. enum EMapFormat: ui8
  182. {
  183. INVALID = 0,
  184. // HEX DEC
  185. ROE = 0x0e, // 14
  186. AB = 0x15, // 21
  187. SOD = 0x1c, // 28
  188. // HOTA = 0x1e ... 0x20 // 28 ... 30
  189. WOG = 0x33, // 51
  190. VCMI = 0xF0
  191. };
  192. }
  193. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  194. class DLL_LINKAGE CMapHeader
  195. {
  196. void setupEvents();
  197. public:
  198. static const int MAP_SIZE_SMALL;
  199. static const int MAP_SIZE_MIDDLE;
  200. static const int MAP_SIZE_LARGE;
  201. static const int MAP_SIZE_XLARGE;
  202. CMapHeader();
  203. virtual ~CMapHeader();
  204. EMapFormat::EMapFormat version; /// The default value is EMapFormat::SOD.
  205. si32 height; /// The default value is 72.
  206. si32 width; /// The default value is 72.
  207. bool twoLevel; /// The default value is true.
  208. std::string name;
  209. std::string description;
  210. ui8 difficulty; /// The default value is 1 representing a normal map difficulty.
  211. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  212. /// maximum level for heroes. This is the default value.
  213. ui8 levelLimit;
  214. std::string victoryMessage;
  215. std::string defeatMessage;
  216. ui16 victoryIconIndex;
  217. ui16 defeatIconIndex;
  218. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  219. ui8 howManyTeams;
  220. std::vector<bool> allowedHeroes;
  221. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  222. /// "main quests" of the map that describe victory and loss conditions
  223. std::vector<TriggeredEvent> triggeredEvents;
  224. template <typename Handler>
  225. void serialize(Handler & h, const int Version)
  226. {
  227. h & version & name & description & width & height & twoLevel & difficulty & levelLimit & areAnyPlayers;
  228. h & players & howManyTeams & allowedHeroes & triggeredEvents;
  229. h & victoryMessage & victoryIconIndex & defeatMessage & defeatIconIndex;
  230. }
  231. };
  232. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  233. class DLL_LINKAGE CMap : public CMapHeader
  234. {
  235. public:
  236. CMap();
  237. ~CMap();
  238. void initTerrain();
  239. CMapEditManager * getEditManager();
  240. TerrainTile & getTile(const int3 & tile);
  241. const TerrainTile & getTile(const int3 & tile) const;
  242. bool isCoastalTile(const int3 & pos) const;
  243. bool isInTheMap(const int3 & pos) const;
  244. bool isWaterTile(const int3 & pos) const;
  245. bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
  246. int3 guardingCreaturePosition (int3 pos) const;
  247. void addBlockVisTiles(CGObjectInstance * obj);
  248. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  249. void calculateGuardingGreaturePositions();
  250. void addNewArtifactInstance(CArtifactInstance * art);
  251. void eraseArtifactInstance(CArtifactInstance * art);
  252. void addQuest(CGObjectInstance * quest);
  253. void addNewObject(CGObjectInstance * obj);
  254. /// Gets object of specified type on requested position
  255. const CGObjectInstance * getObjectiveObjectFrom(int3 pos, Obj::EObj type);
  256. CGHeroInstance * getHero(int heroId);
  257. /// Sets the victory/loss condition objectives ??
  258. void checkForObjectives();
  259. ui32 checksum;
  260. std::vector<Rumor> rumors;
  261. std::vector<DisposedHero> disposedHeroes;
  262. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  263. std::vector<bool> allowedSpell;
  264. std::vector<bool> allowedArtifact;
  265. std::vector<bool> allowedAbilities;
  266. std::list<CMapEvent> events;
  267. int3 grailPos;
  268. int grailRadius;
  269. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  270. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  271. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  272. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  273. std::vector< ConstTransitivePtr<CQuest> > quests;
  274. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  275. //Helper lists
  276. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  277. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  278. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  279. std::map<si32, ObjectInstanceID> questIdentifierToId;
  280. std::unique_ptr<CMapEditManager> editManager;
  281. int3 ***guardingCreaturePositions;
  282. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  283. private:
  284. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  285. TerrainTile*** terrain;
  286. public:
  287. template <typename Handler>
  288. void serialize(Handler &h, const int formatVersion)
  289. {
  290. h & static_cast<CMapHeader&>(*this);
  291. h & rumors & allowedSpell & allowedAbilities & allowedArtifact & events & grailPos;
  292. h & artInstances & quests & allHeroes;
  293. h & questIdentifierToId;
  294. //TODO: viccondetails
  295. int level = twoLevel ? 2 : 1;
  296. if(h.saving)
  297. {
  298. // Save terrain
  299. for(int i = 0; i < width ; ++i)
  300. {
  301. for(int j = 0; j < height ; ++j)
  302. {
  303. for(int k = 0; k < level; ++k)
  304. {
  305. h & terrain[i][j][k];
  306. h & guardingCreaturePositions[i][j][k];
  307. }
  308. }
  309. }
  310. }
  311. else
  312. {
  313. // Load terrain
  314. terrain = new TerrainTile**[width];
  315. guardingCreaturePositions = new int3**[width];
  316. for(int i = 0; i < width; ++i)
  317. {
  318. terrain[i] = new TerrainTile*[height];
  319. guardingCreaturePositions[i] = new int3*[height];
  320. for(int j = 0; j < height; ++j)
  321. {
  322. terrain[i][j] = new TerrainTile[level];
  323. guardingCreaturePositions[i][j] = new int3[level];
  324. }
  325. }
  326. for(int i = 0; i < width ; ++i)
  327. {
  328. for(int j = 0; j < height ; ++j)
  329. {
  330. for(int k = 0; k < level; ++k)
  331. {
  332. h & terrain[i][j][k];
  333. h & guardingCreaturePositions[i][j][k];
  334. }
  335. }
  336. }
  337. }
  338. h & objects;
  339. h & heroesOnMap & teleportChannels & towns & artInstances;
  340. // static members
  341. h & CGKeys::playerKeyMap;
  342. h & CGMagi::eyelist;
  343. h & CGObelisk::obeliskCount & CGObelisk::visited;
  344. h & CGTownInstance::merchantArtifacts;
  345. h & CGTownInstance::universitySkills;
  346. if(version >= 759)
  347. {
  348. h & instanceNames;
  349. }
  350. }
  351. };