CMap.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "../GameCallbackHolder.h"
  14. #include "../networkPacks/TradeItem.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CArtifactInstance;
  17. class CArtifactSet;
  18. class CGObjectInstance;
  19. class CGHeroInstance;
  20. class CCommanderInstance;
  21. class CGameState;
  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. /// All artifacts that exists on map, whether on map, in hero inventory, or stored in some object
  55. std::vector<std::shared_ptr<CArtifactInstance>> artInstances;
  56. /// All heroes that are currently free for recruitment in taverns and are not present on map
  57. std::vector<std::shared_ptr<CGHeroInstance> > heroesPool;
  58. /// Precomputed indices of all towns on map
  59. std::vector<ObjectInstanceID> towns;
  60. /// Precomputed indices of all towns on map. Does not includes heroes in prisons
  61. std::vector<ObjectInstanceID> heroesOnMap;
  62. public:
  63. /// Central lists of items in game. Position of item in the vectors below is their (instance) id.
  64. /// TODO: make private
  65. std::vector<std::shared_ptr<CGObjectInstance>> objects;
  66. explicit CMap(IGameCallback *cb);
  67. ~CMap();
  68. void initTerrain();
  69. CMapEditManager * getEditManager();
  70. inline TerrainTile & getTile(const int3 & tile);
  71. inline const TerrainTile & getTile(const int3 & tile) const;
  72. bool isCoastalTile(const int3 & pos) const;
  73. inline bool isInTheMap(const int3 & pos) const;
  74. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  75. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  76. int3 guardingCreaturePosition (int3 pos) const;
  77. void addBlockVisTiles(CGObjectInstance * obj);
  78. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  79. void calculateGuardingGreaturePositions();
  80. CArtifactInstance * createScroll(const SpellID & spellId);
  81. CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  82. CArtifactInstance * createSingleArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  83. CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID);
  84. const CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID) const;
  85. void eraseArtifactInstance(const ArtifactInstanceID art);
  86. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  87. void putArtifactInstance(CArtifactSet & set, const ArtifactInstanceID art, const ArtifactPosition & slot);
  88. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  89. void generateUniqueInstanceName(CGObjectInstance * target);
  90. ///Use only this method when creating new map object instances
  91. void addNewObject(std::shared_ptr<CGObjectInstance> obj);
  92. void moveObject(ObjectInstanceID target, const int3 & dst);
  93. /// Remove objects and shifts object indicies.
  94. /// Only for use in map editor / RMG
  95. std::shared_ptr<CGObjectInstance> removeObject(ObjectInstanceID oldObject);
  96. /// Replaced map object with specified ID with new object
  97. /// Old object must exist and will be removed from map
  98. /// Returns pointer to old object, which can be manipulated or dropped
  99. std::shared_ptr<CGObjectInstance> replaceObject(ObjectInstanceID oldObject, const std::shared_ptr<CGObjectInstance> & newObject);
  100. /// Erases object from map without shifting indices
  101. /// Returns pointer to old object, which can be manipulated or dropped
  102. std::shared_ptr<CGObjectInstance> eraseObject(ObjectInstanceID oldObject);
  103. void heroAddedToMap(const CGHeroInstance * hero);
  104. void heroRemovedFromMap(const CGHeroInstance * hero);
  105. void townAddedToMap(const CGTownInstance * town);
  106. void townRemovedFromMap(const CGTownInstance * town);
  107. void addToHeroPool(std::shared_ptr<CGHeroInstance> hero);
  108. std::shared_ptr<CGHeroInstance> tryTakeFromHeroPool(HeroTypeID hero);
  109. CGHeroInstance * tryGetFromHeroPool(HeroTypeID hero);
  110. std::vector<HeroTypeID> getHeroesInPool() const;
  111. CGObjectInstance * getObject(ObjectInstanceID obj);
  112. const CGObjectInstance * getObject(ObjectInstanceID obj) const;
  113. void attachToBonusSystem(CGameState * gs);
  114. template<typename ObjectType = CGObjectInstance>
  115. std::vector<const ObjectType *> getObjects() const
  116. {
  117. std::vector<const ObjectType *> result;
  118. for (const auto & object : objects)
  119. {
  120. auto casted = dynamic_cast<const ObjectType*>(object.get());
  121. if (casted)
  122. result.push_back(casted);
  123. }
  124. return result;
  125. }
  126. template<typename ObjectType = CGObjectInstance>
  127. std::vector<ObjectType *> getObjects()
  128. {
  129. std::vector<ObjectType *> result;
  130. for (const auto & object : objects)
  131. {
  132. auto casted = dynamic_cast<ObjectType*>(object.get());
  133. if (casted)
  134. result.push_back(casted);
  135. }
  136. return result;
  137. }
  138. std::vector<CArtifactInstance *> getArtifacts()
  139. {
  140. std::vector<CArtifactInstance *> result;
  141. for (const auto & art : artInstances)
  142. if (art)
  143. result.push_back(art.get());
  144. return result;
  145. }
  146. bool isWaterMap() const;
  147. bool calculateWaterContent();
  148. void banWaterArtifacts();
  149. void banWaterHeroes();
  150. void banHero(const HeroTypeID& id);
  151. void unbanHero(const HeroTypeID & id);
  152. void banWaterSpells();
  153. void banWaterSkills();
  154. void banWaterContent();
  155. /// Gets object of specified type on requested position
  156. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  157. /// Returns pointer to hero of specified type if hero is present on map
  158. CGHeroInstance * getHero(HeroTypeID heroId);
  159. /// Returns ID's of all heroes that are currently present on map
  160. /// Includes all garrisoned and imprisoned heroes
  161. const std::vector<ObjectInstanceID> & getHeroesOnMap();
  162. /// Returns ID's of all towns present on map
  163. const std::vector<ObjectInstanceID> & getAllTowns();
  164. /// Sets the victory/loss condition objectives ??
  165. void checkForObjectives();
  166. void resolveQuestIdentifiers();
  167. void reindexObjects();
  168. std::vector<Rumor> rumors;
  169. std::set<SpellID> allowedSpells;
  170. std::set<ArtifactID> allowedArtifact;
  171. std::set<SecondarySkill> allowedAbilities;
  172. std::vector<CMapEvent> events;
  173. int3 grailPos;
  174. int grailRadius;
  175. //Helper lists
  176. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  177. std::unique_ptr<CMapEditManager> editManager;
  178. boost::multi_array<int3, 3> guardingCreaturePositions;
  179. std::map<std::string, std::shared_ptr<CGObjectInstance> > instanceNames;
  180. bool waterMap;
  181. ui8 obeliskCount = 0; //how many obelisks are on map
  182. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  183. std::vector<ArtifactID> townMerchantArtifacts;
  184. std::vector<TradeItemBuy> townUniversitySkills;
  185. void overrideGameSettings(const JsonNode & input);
  186. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  187. const IGameSettings & getSettings() const;
  188. void postInitialize();
  189. private:
  190. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  191. boost::multi_array<TerrainTile, 3> terrain;
  192. si32 uidCounter; //TODO: initialize when loading an old map
  193. public:
  194. template <typename Handler>
  195. void serialize(Handler &h)
  196. {
  197. h & static_cast<CMapHeader&>(*this);
  198. h & triggeredEvents; //from CMapHeader
  199. h & rumors;
  200. h & allowedSpells;
  201. h & allowedAbilities;
  202. h & allowedArtifact;
  203. h & events;
  204. h & grailPos;
  205. h & artInstances;
  206. //TODO: viccondetails
  207. h & terrain;
  208. h & guardingCreaturePositions;
  209. h & objects;
  210. h & heroesOnMap;
  211. h & heroesPool;
  212. h & teleportChannels;
  213. h & towns;
  214. h & artInstances;
  215. // static members
  216. h & obeliskCount;
  217. h & obelisksVisited;
  218. h & townMerchantArtifacts;
  219. h & townUniversitySkills;
  220. h & instanceNames;
  221. h & *gameSettings;
  222. }
  223. };
  224. inline bool CMap::isInTheMap(const int3 & pos) const
  225. {
  226. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  227. return
  228. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  229. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  230. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  231. }
  232. inline TerrainTile & CMap::getTile(const int3 & tile)
  233. {
  234. assert(isInTheMap(tile));
  235. return terrain[tile.z][tile.x][tile.y];
  236. }
  237. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  238. {
  239. assert(isInTheMap(tile));
  240. return terrain[tile.z][tile.x][tile.y];
  241. }
  242. VCMI_LIB_NAMESPACE_END