2
0

CMap.h 13 KB

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