CMap.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "CMapDefines.h"
  12. #include "CMapHeader.h"
  13. #include "../ConstTransitivePtr.h"
  14. #include "../GameCallbackHolder.h"
  15. #include "../networkPacks/TradeItem.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CArtifactInstance;
  18. class CArtifactSet;
  19. class CGObjectInstance;
  20. class CGHeroInstance;
  21. class CCommanderInstance;
  22. class CGCreature;
  23. class CQuest;
  24. class CGTownInstance;
  25. class IModableArt;
  26. class IQuestObject;
  27. class CInputStream;
  28. class CMapEditManager;
  29. class JsonSerializeFormat;
  30. class IGameSettings;
  31. class GameSettings;
  32. struct TeleportChannel;
  33. enum class EGameSettings;
  34. /// The rumor struct consists of a rumor name and text.
  35. struct DLL_LINKAGE Rumor
  36. {
  37. std::string name;
  38. MetaString text;
  39. Rumor() = default;
  40. ~Rumor() = default;
  41. template <typename Handler>
  42. void serialize(Handler & h)
  43. {
  44. h & name;
  45. h & text;
  46. }
  47. void serializeJson(JsonSerializeFormat & handler);
  48. };
  49. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  50. class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
  51. {
  52. std::unique_ptr<GameSettings> gameSettings;
  53. public:
  54. explicit CMap(IGameCallback *cb);
  55. ~CMap();
  56. void initTerrain();
  57. CMapEditManager * getEditManager();
  58. inline TerrainTile & getTile(const int3 & tile);
  59. inline const TerrainTile & getTile(const int3 & tile) const;
  60. bool isCoastalTile(const int3 & pos) const;
  61. inline bool isInTheMap(const int3 & pos) const;
  62. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  63. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  64. int3 guardingCreaturePosition (int3 pos) const;
  65. void addBlockVisTiles(CGObjectInstance * obj);
  66. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  67. void calculateGuardingGreaturePositions();
  68. void addNewArtifactInstance(CArtifactSet & artSet);
  69. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  70. void eraseArtifactInstance(CArtifactInstance * art);
  71. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  72. void putArtifactInstance(CArtifactSet & set, CArtifactInstance * art, const ArtifactPosition & slot);
  73. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  74. void addNewQuestInstance(CQuest * quest);
  75. void removeQuestInstance(CQuest * quest);
  76. void setUniqueInstanceName(CGObjectInstance * obj);
  77. ///Use only this method when creating new map object instances
  78. void addNewObject(CGObjectInstance * obj);
  79. void moveObject(CGObjectInstance * obj, const int3 & dst);
  80. void removeObject(CGObjectInstance * obj);
  81. bool isWaterMap() const;
  82. bool calculateWaterContent();
  83. void banWaterArtifacts();
  84. void banWaterHeroes();
  85. void banHero(const HeroTypeID& id);
  86. void unbanHero(const HeroTypeID & id);
  87. void banWaterSpells();
  88. void banWaterSkills();
  89. void banWaterContent();
  90. /// Gets object of specified type on requested position
  91. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  92. CGHeroInstance * getHero(HeroTypeID heroId);
  93. /// Sets the victory/loss condition objectives ??
  94. void checkForObjectives();
  95. void resetStaticData();
  96. void resolveQuestIdentifiers();
  97. void reindexObjects();
  98. std::vector<Rumor> rumors;
  99. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  100. std::set<SpellID> allowedSpells;
  101. std::set<ArtifactID> allowedArtifact;
  102. std::set<SecondarySkill> allowedAbilities;
  103. std::vector<CMapEvent> events;
  104. int3 grailPos;
  105. int grailRadius;
  106. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  107. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  108. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  109. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  110. std::vector< ConstTransitivePtr<CQuest> > quests;
  111. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  112. //Helper lists
  113. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  114. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  115. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  116. std::map<si32, ObjectInstanceID> questIdentifierToId;
  117. std::unique_ptr<CMapEditManager> editManager;
  118. boost::multi_array<int3, 3> guardingCreaturePositions;
  119. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  120. bool waterMap;
  121. ui8 obeliskCount = 0; //how many obelisks are on map
  122. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  123. std::vector<ArtifactID> townMerchantArtifacts;
  124. std::vector<TradeItemBuy> townUniversitySkills;
  125. void overrideGameSettings(const JsonNode & input);
  126. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  127. const IGameSettings & getSettings() const;
  128. private:
  129. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  130. boost::multi_array<TerrainTile, 3> terrain;
  131. si32 uidCounter; //TODO: initialize when loading an old map
  132. public:
  133. template <typename Handler>
  134. void serialize(Handler &h)
  135. {
  136. h & static_cast<CMapHeader&>(*this);
  137. h & triggeredEvents; //from CMapHeader
  138. h & rumors;
  139. h & allowedSpells;
  140. h & allowedAbilities;
  141. h & allowedArtifact;
  142. h & events;
  143. h & grailPos;
  144. h & artInstances;
  145. h & quests;
  146. h & allHeroes;
  147. //TODO: viccondetails
  148. h & terrain;
  149. h & guardingCreaturePositions;
  150. h & objects;
  151. h & heroesOnMap;
  152. h & teleportChannels;
  153. h & towns;
  154. h & artInstances;
  155. // static members
  156. h & obeliskCount;
  157. h & obelisksVisited;
  158. if (h.version < Handler::Version::REMOVE_VLC_POINTERS)
  159. {
  160. int32_t size = 0;
  161. h & size;
  162. for (int32_t i = 0; i < size; ++i)
  163. {
  164. bool isNull = false;
  165. ArtifactID artifact;
  166. h & isNull;
  167. if (!isNull)
  168. h & artifact;
  169. townMerchantArtifacts.push_back(artifact);
  170. }
  171. }
  172. else
  173. {
  174. h & townMerchantArtifacts;
  175. }
  176. h & townUniversitySkills;
  177. h & instanceNames;
  178. if (h.version >= Handler::Version::PER_MAP_GAME_SETTINGS)
  179. h & *gameSettings;
  180. }
  181. };
  182. inline bool CMap::isInTheMap(const int3 & pos) const
  183. {
  184. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  185. return
  186. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  187. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  188. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  189. }
  190. inline TerrainTile & CMap::getTile(const int3 & tile)
  191. {
  192. assert(isInTheMap(tile));
  193. return terrain[tile.z][tile.x][tile.y];
  194. }
  195. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  196. {
  197. assert(isInTheMap(tile));
  198. return terrain[tile.z][tile.x][tile.y];
  199. }
  200. VCMI_LIB_NAMESPACE_END