CMap.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "CMapHeader.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 "CMapDefines.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  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. class JsonSerializeFormat;
  29. struct TeleportChannel;
  30. /// The rumor struct consists of a rumor name and text.
  31. struct DLL_LINKAGE Rumor
  32. {
  33. std::string name;
  34. std::string text;
  35. Rumor() = default;
  36. ~Rumor() = default;
  37. template <typename Handler>
  38. void serialize(Handler & h, const int version)
  39. {
  40. h & name;
  41. h & text;
  42. }
  43. void serializeJson(JsonSerializeFormat & handler);
  44. };
  45. /// The disposed hero struct describes which hero can be hired from which player.
  46. struct DLL_LINKAGE DisposedHero
  47. {
  48. DisposedHero();
  49. HeroTypeID heroId;
  50. HeroTypeID portrait; /// The portrait id of the hero, -1 is default.
  51. std::string name;
  52. std::set<PlayerColor> players; /// Who can hire this hero (bitfield).
  53. template <typename Handler>
  54. void serialize(Handler & h, const int version)
  55. {
  56. h & heroId;
  57. h & portrait;
  58. h & name;
  59. h & players;
  60. }
  61. };
  62. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  63. class DLL_LINKAGE CMap : public CMapHeader
  64. {
  65. public:
  66. CMap();
  67. ~CMap();
  68. void initTerrain();
  69. CMapEditManager * getEditManager();
  70. TerrainTile & getTile(const int3 & tile);
  71. const TerrainTile & getTile(const int3 & tile) const;
  72. bool isCoastalTile(const int3 & pos) const;
  73. bool isInTheMap(const int3 & pos) const;
  74. bool isWaterTile(const int3 & pos) const;
  75. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  76. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  77. int3 guardingCreaturePosition (int3 pos) const;
  78. void addBlockVisTiles(CGObjectInstance * obj);
  79. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  80. void calculateGuardingGreaturePositions();
  81. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  82. void eraseArtifactInstance(CArtifactInstance * art);
  83. void addNewQuestInstance(CQuest * quest);
  84. void removeQuestInstance(CQuest * quest);
  85. void setUniqueInstanceName(CGObjectInstance * obj);
  86. ///Use only this method when creating new map object instances
  87. void addNewObject(CGObjectInstance * obj);
  88. void moveObject(CGObjectInstance * obj, const int3 & dst);
  89. void removeObject(CGObjectInstance * obj);
  90. bool isWaterMap() const;
  91. bool calculateWaterContent();
  92. void banWaterArtifacts();
  93. void banWaterHeroes();
  94. void banHero(const HeroTypeID& id);
  95. void banWaterSpells();
  96. void banWaterSkills();
  97. void banWaterContent();
  98. /// Gets object of specified type on requested position
  99. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  100. CGHeroInstance * getHero(int heroId);
  101. /// Sets the victory/loss condition objectives ??
  102. void checkForObjectives();
  103. void resetStaticData();
  104. ui32 checksum;
  105. std::vector<Rumor> rumors;
  106. std::vector<DisposedHero> disposedHeroes;
  107. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  108. std::vector<bool> allowedSpells;
  109. std::vector<bool> allowedArtifact;
  110. std::vector<bool> allowedAbilities;
  111. std::list<CMapEvent> events;
  112. int3 grailPos;
  113. int grailRadius;
  114. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  115. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  116. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  117. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  118. std::vector< ConstTransitivePtr<CQuest> > quests;
  119. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  120. //Helper lists
  121. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  122. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  123. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  124. std::map<si32, ObjectInstanceID> questIdentifierToId;
  125. std::unique_ptr<CMapEditManager> editManager;
  126. boost::multi_array<int3, 3> guardingCreaturePositions;
  127. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  128. bool waterMap;
  129. private:
  130. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  131. boost::multi_array<TerrainTile, 3> terrain;
  132. si32 uidCounter; //TODO: initialize when loading an old map
  133. public:
  134. template <typename Handler>
  135. void serialize(Handler &h, const int formatVersion)
  136. {
  137. h & static_cast<CMapHeader&>(*this);
  138. h & triggeredEvents; //from CMapHeader
  139. h & rumors;
  140. h & allowedSpells;
  141. h & allowedAbilities;
  142. h & allowedArtifact;
  143. h & events;
  144. h & grailPos;
  145. h & artInstances;
  146. h & quests;
  147. h & allHeroes;
  148. h & questIdentifierToId;
  149. //TODO: viccondetails
  150. h & terrain;
  151. h & guardingCreaturePositions;
  152. h & objects;
  153. h & heroesOnMap;
  154. h & teleportChannels;
  155. h & towns;
  156. h & artInstances;
  157. // static members
  158. h & CGKeys::playerKeyMap;
  159. h & CGMagi::eyelist;
  160. h & CGObelisk::obeliskCount;
  161. h & CGObelisk::visited;
  162. h & CGTownInstance::merchantArtifacts;
  163. h & CGTownInstance::universitySkills;
  164. h & instanceNames;
  165. }
  166. };
  167. VCMI_LIB_NAMESPACE_END