CMap.h 5.7 KB

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