CMap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "CMapEvent.h"
  12. #include "CMapHeader.h"
  13. #include "TerrainTile.h"
  14. #include "../mapObjects/CGObjectInstance.h"
  15. #include "../callback/GameCallbackHolder.h"
  16. #include "../networkPacks/TradeItem.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CArtifactInstance;
  19. class CArtifactSet;
  20. class CGObjectInstance;
  21. class CGHeroInstance;
  22. class CCommanderInstance;
  23. class CGameState;
  24. class CGCreature;
  25. class CQuest;
  26. class CGTownInstance;
  27. class IModableArt;
  28. class IQuestObject;
  29. class CInputStream;
  30. class CMapEditManager;
  31. class JsonSerializeFormat;
  32. class IGameSettings;
  33. class GameSettings;
  34. struct TeleportChannel;
  35. enum class EGameSettings;
  36. /// The rumor struct consists of a rumor name and text.
  37. struct DLL_LINKAGE Rumor
  38. {
  39. std::string name;
  40. MetaString text;
  41. Rumor() = default;
  42. ~Rumor() = default;
  43. template <typename Handler>
  44. void serialize(Handler & h)
  45. {
  46. h & name;
  47. h & text;
  48. }
  49. void serializeJson(JsonSerializeFormat & handler);
  50. };
  51. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  52. class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
  53. {
  54. friend class CSerializer;
  55. std::unique_ptr<GameSettings> gameSettings;
  56. /// All artifacts that exists on map, whether on map, in hero inventory, or stored in some object
  57. std::vector<std::shared_ptr<CArtifactInstance>> artInstances;
  58. /// All heroes that are currently free for recruitment in taverns and are not present on map
  59. std::vector<std::shared_ptr<CGHeroInstance> > heroesPool;
  60. /// Precomputed indices of all towns on map
  61. std::vector<ObjectInstanceID> towns;
  62. /// Precomputed indices of all heroes on map. Does not includes heroes in prisons
  63. std::vector<ObjectInstanceID> heroesOnMap;
  64. public:
  65. /// Central lists of items in game. Position of item in the vectors below is their (instance) id.
  66. /// TODO: make private
  67. std::vector<std::shared_ptr<CGObjectInstance>> objects;
  68. explicit CMap(IGameInfoCallback *cb);
  69. ~CMap();
  70. void initTerrain();
  71. CMapEditManager * getEditManager();
  72. inline TerrainTile & getTile(const int3 & tile);
  73. inline const TerrainTile & getTile(const int3 & tile) const;
  74. bool isCoastalTile(const int3 & pos) const;
  75. inline bool isInTheMap(const int3 & pos) const;
  76. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  77. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  78. int3 guardingCreaturePosition (int3 pos) const;
  79. void calculateGuardingGreaturePositions();
  80. void saveCompatibilityAddMissingArtifact(std::shared_ptr<CArtifactInstance> artifact);
  81. /// Creates instance of spell scroll artifact with provided spell
  82. CArtifactInstance * createScroll(const SpellID & spellId);
  83. /// Creates instance of requested artifact
  84. /// For combined artifact this method will also create alll required components
  85. /// For scrolls this method will also initialize its spell
  86. CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  87. /// Creates single instance of requested artifact
  88. /// Does NOT creates components for combined artifacts
  89. /// Does NOT initializes spell when spell scroll artifact is created
  90. CArtifactInstance * createArtifactComponent(const ArtifactID & artId);
  91. /// Returns pointer to requested Artifact Instance. Throws on invalid ID
  92. CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID);
  93. /// Returns pointer to requested Artifact Instance. Throws on invalid ID
  94. const CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID) const;
  95. /// Completely removes artifact instance from the game
  96. void eraseArtifactInstance(const ArtifactInstanceID art);
  97. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  98. void putArtifactInstance(CArtifactSet & set, const ArtifactInstanceID art, const ArtifactPosition & slot);
  99. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  100. /// Generates unique string identifier for provided object instance
  101. void generateUniqueInstanceName(CGObjectInstance * target);
  102. /// Generates new, unique numeric identifier that can be used for creation of a new object
  103. ObjectInstanceID allocateUniqueInstanceID();
  104. /// Adds provided object to the map
  105. /// Throws on error, for example if object is already on map
  106. void addNewObject(std::shared_ptr<CGObjectInstance> obj);
  107. /// Moves anchor position of requested object to specified coordinates and updates map state
  108. /// Throws in invalid object instance ID
  109. void moveObject(ObjectInstanceID target, const int3 & dst);
  110. /// Hides object from map without actually removing it from object list
  111. void hideObject(CGObjectInstance * obj);
  112. /// Shows previously hiiden object on map
  113. void showObject(CGObjectInstance * obj);
  114. /// Remove objects and shifts object indicies.
  115. /// Only for use in map editor / RMG
  116. std::shared_ptr<CGObjectInstance> removeObject(ObjectInstanceID oldObject);
  117. /// Replaced map object with specified ID with new object
  118. /// Old object must exist and will be removed from map
  119. /// Returns pointer to old object, which can be manipulated or dropped
  120. std::shared_ptr<CGObjectInstance> replaceObject(ObjectInstanceID oldObject, const std::shared_ptr<CGObjectInstance> & newObject);
  121. /// Erases object from map without shifting indices
  122. /// Returns pointer to old object, which can be manipulated or dropped
  123. std::shared_ptr<CGObjectInstance> eraseObject(ObjectInstanceID oldObject);
  124. void heroAddedToMap(const CGHeroInstance * hero);
  125. void heroRemovedFromMap(const CGHeroInstance * hero);
  126. void townAddedToMap(const CGTownInstance * town);
  127. void townRemovedFromMap(const CGTownInstance * town);
  128. /// Adds provided hero to map pool. Hero with same identity must not exist
  129. void addToHeroPool(std::shared_ptr<CGHeroInstance> hero);
  130. /// Attempts to take hero of specified identity from pool. Returns nullptr on failure
  131. /// Hero is removed from pool on success
  132. std::shared_ptr<CGHeroInstance> tryTakeFromHeroPool(HeroTypeID hero);
  133. /// Attempts to access hero of specified identity in pool. Returns nullptr on failure
  134. CGHeroInstance * tryGetFromHeroPool(HeroTypeID hero);
  135. /// Returns list of identities of heroes currently present in pool
  136. std::vector<HeroTypeID> getHeroesInPool() const;
  137. CGObjectInstance * getObject(ObjectInstanceID obj);
  138. const CGObjectInstance * getObject(ObjectInstanceID obj) const;
  139. void attachToBonusSystem(CGameState & gs);
  140. /// Returns all valid objects of specified class present on map
  141. template<typename ObjectType = CGObjectInstance>
  142. std::vector<const ObjectType *> getObjects() const
  143. {
  144. std::vector<const ObjectType *> result;
  145. for (const auto & object : objects)
  146. {
  147. auto casted = dynamic_cast<const ObjectType*>(object.get());
  148. if (casted)
  149. result.push_back(casted);
  150. }
  151. return result;
  152. }
  153. /// Returns all valid objects of specified class present on map
  154. template<typename ObjectType = CGObjectInstance>
  155. std::vector<ObjectType *> getObjects()
  156. {
  157. std::vector<ObjectType *> result;
  158. for (const auto & object : objects)
  159. {
  160. auto casted = dynamic_cast<ObjectType*>(object.get());
  161. if (casted)
  162. result.push_back(casted);
  163. }
  164. return result;
  165. }
  166. /// Returns all valid artifacts present on map
  167. std::vector<CArtifactInstance *> getArtifacts()
  168. {
  169. std::vector<CArtifactInstance *> result;
  170. for (const auto & art : artInstances)
  171. if (art)
  172. result.push_back(art.get());
  173. return result;
  174. }
  175. bool isWaterMap() const;
  176. bool calculateWaterContent();
  177. void banWaterArtifacts();
  178. void banWaterHeroes();
  179. void banHero(const HeroTypeID& id);
  180. void unbanHero(const HeroTypeID & id);
  181. void banWaterSpells();
  182. void banWaterSkills();
  183. void banWaterContent();
  184. /// Gets object of specified type on requested position
  185. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  186. /// Returns pointer to hero of specified type if hero is present on map
  187. CGHeroInstance * getHero(HeroTypeID heroId);
  188. const CGHeroInstance * getHero(HeroTypeID heroId) const;
  189. /// Returns ID's of all heroes that are currently present on map
  190. /// Includes all garrisoned and imprisoned heroes
  191. const std::vector<ObjectInstanceID> & getHeroesOnMap() const;
  192. /// Returns ID's of all towns present on map
  193. const std::vector<ObjectInstanceID> & getAllTowns() const;
  194. /// Sets the victory/loss condition objectives ??
  195. void checkForObjectives();
  196. void resolveQuestIdentifiers();
  197. void reindexObjects();
  198. std::vector<Rumor> rumors;
  199. std::set<SpellID> allowedSpells;
  200. std::set<ArtifactID> allowedArtifact;
  201. std::set<SecondarySkill> allowedAbilities;
  202. std::vector<CMapEvent> events;
  203. int3 grailPos;
  204. int grailRadius;
  205. //Helper lists
  206. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  207. std::unique_ptr<CMapEditManager> editManager;
  208. boost::multi_array<int3, 3> guardingCreaturePositions;
  209. std::map<std::string, std::shared_ptr<CGObjectInstance> > instanceNames;
  210. bool waterMap;
  211. ui8 obeliskCount = 0; //how many obelisks are on map
  212. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  213. std::vector<ArtifactID> townMerchantArtifacts;
  214. void overrideGameSettings(const JsonNode & input);
  215. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  216. const IGameSettings & getSettings() const;
  217. void saveCompatibilityStoreAllocatedArtifactID();
  218. void parseUidCounter();
  219. private:
  220. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  221. boost::multi_array<TerrainTile, 3> terrain;
  222. si32 uidCounter;
  223. public:
  224. template <typename Handler>
  225. void serialize(Handler &h)
  226. {
  227. h & static_cast<CMapHeader&>(*this);
  228. h & triggeredEvents; //from CMapHeader
  229. h & rumors;
  230. h & allowedSpells;
  231. h & allowedAbilities;
  232. h & allowedArtifact;
  233. h & events;
  234. h & grailPos;
  235. h & artInstances;
  236. if (!h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  237. {
  238. saveCompatibilityStoreAllocatedArtifactID();
  239. std::vector< std::shared_ptr<CQuest> > quests;
  240. h & quests;
  241. }
  242. h & heroesPool;
  243. //TODO: viccondetails
  244. h & terrain;
  245. h & guardingCreaturePositions;
  246. h & objects;
  247. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  248. h & heroesOnMap;
  249. else
  250. {
  251. std::vector<std::shared_ptr<CGObjectInstance>> objectPtrs;
  252. h & objectPtrs;
  253. for (const auto & ptr : objectPtrs)
  254. heroesOnMap.push_back(ptr->id);
  255. for (auto & ptr : heroesPool)
  256. if (vstd::contains(objects, ptr))
  257. ptr = nullptr;
  258. }
  259. h & teleportChannels;
  260. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  261. h & towns;
  262. else
  263. {
  264. std::vector<std::shared_ptr<CGObjectInstance>> objectPtrs;
  265. h & objectPtrs;
  266. for (const auto & ptr : objectPtrs)
  267. towns.push_back(ptr->id);
  268. }
  269. h & artInstances;
  270. // static members
  271. h & obeliskCount;
  272. h & obelisksVisited;
  273. h & townMerchantArtifacts;
  274. if (!h.hasFeature(Handler::Version::UNIVERSITY_CONFIG))
  275. {
  276. std::vector<TradeItemBuy> townUniversitySkills;
  277. h & townUniversitySkills;
  278. }
  279. h & instanceNames;
  280. h & *gameSettings;
  281. if (!h.hasFeature(Handler::Version::STORE_UID_COUNTER_IN_CMAP))
  282. {
  283. if (!h.saving)
  284. parseUidCounter();
  285. }
  286. else
  287. {
  288. h & uidCounter;
  289. }
  290. }
  291. };
  292. inline bool CMap::isInTheMap(const int3 & pos) const
  293. {
  294. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  295. return
  296. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  297. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  298. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  299. }
  300. inline TerrainTile & CMap::getTile(const int3 & tile)
  301. {
  302. assert(isInTheMap(tile));
  303. return terrain[tile.z][tile.x][tile.y];
  304. }
  305. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  306. {
  307. assert(isInTheMap(tile));
  308. return terrain[tile.z][tile.x][tile.y];
  309. }
  310. VCMI_LIB_NAMESPACE_END