CMap.h 12 KB

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