CMap.h 6.6 KB

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