CMap.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. friend class CSerializer;
  53. std::unique_ptr<GameSettings> gameSettings;
  54. std::vector< std::shared_ptr<CQuest> > quests;
  55. std::vector< std::shared_ptr<CArtifactInstance> > artInstances;
  56. public:
  57. explicit CMap(IGameCallback *cb);
  58. ~CMap();
  59. void initTerrain();
  60. CMapEditManager * getEditManager();
  61. inline TerrainTile & getTile(const int3 & tile);
  62. inline const TerrainTile & getTile(const int3 & tile) const;
  63. bool isCoastalTile(const int3 & pos) const;
  64. inline bool isInTheMap(const int3 & pos) const;
  65. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  66. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  67. int3 guardingCreaturePosition (int3 pos) const;
  68. void addBlockVisTiles(CGObjectInstance * obj);
  69. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  70. void calculateGuardingGreaturePositions();
  71. CArtifactInstance * createScroll(const SpellID & spellId);
  72. CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  73. CArtifactInstance * createSingleArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  74. CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID);
  75. const CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID) const;
  76. void eraseArtifactInstance(const ArtifactInstanceID art);
  77. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  78. void putArtifactInstance(CArtifactSet & set, const ArtifactInstanceID art, const ArtifactPosition & slot);
  79. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  80. void addNewQuestInstance(std::shared_ptr<CQuest> quest);
  81. void removeQuestInstance(std::shared_ptr<CQuest> quest);
  82. void clearQuestInstance(const CQuest * quest);
  83. void setUniqueInstanceName(CGObjectInstance * obj);
  84. ///Use only this method when creating new map object instances
  85. void addNewObject(CGObjectInstance * obj);
  86. void moveObject(CGObjectInstance * obj, const int3 & dst);
  87. void removeObject(CGObjectInstance * obj);
  88. bool isWaterMap() const;
  89. bool calculateWaterContent();
  90. void banWaterArtifacts();
  91. void banWaterHeroes();
  92. void banHero(const HeroTypeID& id);
  93. void unbanHero(const HeroTypeID & id);
  94. void banWaterSpells();
  95. void banWaterSkills();
  96. void banWaterContent();
  97. /// Gets object of specified type on requested position
  98. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  99. CGHeroInstance * getHero(HeroTypeID heroId);
  100. /// Sets the victory/loss condition objectives ??
  101. void checkForObjectives();
  102. void resetStaticData();
  103. void resolveQuestIdentifiers();
  104. void reindexObjects();
  105. std::vector<Rumor> rumors;
  106. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  107. std::set<SpellID> allowedSpells;
  108. std::set<ArtifactID> allowedArtifact;
  109. std::set<SecondarySkill> allowedAbilities;
  110. std::vector<CMapEvent> events;
  111. int3 grailPos;
  112. int grailRadius;
  113. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  114. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  115. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  116. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  117. //Helper lists
  118. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  119. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  120. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  121. std::map<si32, ObjectInstanceID> questIdentifierToId;
  122. std::unique_ptr<CMapEditManager> editManager;
  123. boost::multi_array<int3, 3> guardingCreaturePositions;
  124. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  125. bool waterMap;
  126. ui8 obeliskCount = 0; //how many obelisks are on map
  127. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  128. std::vector<ArtifactID> townMerchantArtifacts;
  129. std::vector<TradeItemBuy> townUniversitySkills;
  130. void overrideGameSettings(const JsonNode & input);
  131. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  132. const IGameSettings & getSettings() const;
  133. private:
  134. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  135. boost::multi_array<TerrainTile, 3> terrain;
  136. si32 uidCounter; //TODO: initialize when loading an old map
  137. public:
  138. template <typename Handler>
  139. void serialize(Handler &h)
  140. {
  141. h & static_cast<CMapHeader&>(*this);
  142. h & triggeredEvents; //from CMapHeader
  143. h & rumors;
  144. h & allowedSpells;
  145. h & allowedAbilities;
  146. h & allowedArtifact;
  147. h & events;
  148. h & grailPos;
  149. h & artInstances;
  150. h & quests;
  151. h & allHeroes;
  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 & obeliskCount;
  162. h & obelisksVisited;
  163. h & townMerchantArtifacts;
  164. h & townUniversitySkills;
  165. h & instanceNames;
  166. h & *gameSettings;
  167. }
  168. };
  169. inline bool CMap::isInTheMap(const int3 & pos) const
  170. {
  171. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  172. return
  173. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  174. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  175. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  176. }
  177. inline TerrainTile & CMap::getTile(const int3 & tile)
  178. {
  179. assert(isInTheMap(tile));
  180. return terrain[tile.z][tile.x][tile.y];
  181. }
  182. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  183. {
  184. assert(isInTheMap(tile));
  185. return terrain[tile.z][tile.x][tile.y];
  186. }
  187. VCMI_LIB_NAMESPACE_END