CMap.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 CGCreature;
  22. class CQuest;
  23. class CGTownInstance;
  24. class IModableArt;
  25. class IQuestObject;
  26. class CInputStream;
  27. class CMapEditManager;
  28. class JsonSerializeFormat;
  29. class IGameSettings;
  30. class GameSettings;
  31. struct TeleportChannel;
  32. enum class EGameSettings;
  33. /// The rumor struct consists of a rumor name and text.
  34. struct DLL_LINKAGE Rumor
  35. {
  36. std::string name;
  37. MetaString text;
  38. Rumor() = default;
  39. ~Rumor() = default;
  40. template <typename Handler>
  41. void serialize(Handler & h)
  42. {
  43. h & name;
  44. h & text;
  45. }
  46. void serializeJson(JsonSerializeFormat & handler);
  47. };
  48. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  49. class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
  50. {
  51. friend class CSerializer;
  52. std::unique_ptr<GameSettings> gameSettings;
  53. /// Central lists of items in game. Position of item in the vectors below is their (instance) id.
  54. std::vector< std::shared_ptr<CGObjectInstance> > objects;
  55. /// All quests that are currently present on map
  56. std::vector<std::shared_ptr<CQuest>> quests;
  57. /// All artifacts that exists on map, whether on map, in hero inventory, or stored in some object
  58. std::vector<std::shared_ptr<CArtifactInstance>> artInstances;
  59. /// All heroes that are currently free for recruitment in taverns and are not present on map
  60. std::vector<std::shared_ptr<CGHeroInstance> > heroesPool;
  61. /// Precomputed indices of all towns on map
  62. std::vector<ObjectInstanceID> towns;
  63. /// Precomputed indices of all towns on map. Does not includes heroes in prisons
  64. std::vector<ObjectInstanceID> heroesOnMap;
  65. public:
  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 addNewQuestInstance(std::shared_ptr<CQuest> quest);
  90. void clearQuestInstance(const CQuest * quest);
  91. void generateUniqueInstanceName(CGObjectInstance * target);
  92. ///Use only this method when creating new map object instances
  93. void addNewObject(std::shared_ptr<CGObjectInstance> obj);
  94. void moveObject(ObjectInstanceID target, const int3 & dst);
  95. /// Remove objects and shifts object indicies.
  96. /// Only for use in map editor / RMG
  97. std::shared_ptr<CGObjectInstance> removeObject(ObjectInstanceID oldObject);
  98. /// Replaced map object with specified ID with new object
  99. /// Old object must exist and will be removed from map
  100. /// Returns pointer to old object, which can be manipulated or dropped
  101. std::shared_ptr<CGObjectInstance> replaceObject(ObjectInstanceID oldObject, const std::shared_ptr<CGObjectInstance> & newObject);
  102. /// Erases object from map without shifting indices
  103. /// Returns pointer to old object, which can be manipulated or dropped
  104. std::shared_ptr<CGObjectInstance> eraseObject(ObjectInstanceID oldObject);
  105. void heroAddedToMap(const CGHeroInstance * hero);
  106. void heroRemovedFromMap(const CGHeroInstance * hero);
  107. void townAddedToMap(const CGTownInstance * town);
  108. void townRemovedFromMap(const CGTownInstance * town);
  109. void addToHeroPool(std::shared_ptr<CGHeroInstance> hero);
  110. std::shared_ptr<CGHeroInstance> tryTakeFromHeroPool(HeroTypeID hero);
  111. CGHeroInstance * tryGetFromHeroPool(HeroTypeID hero);
  112. std::vector<HeroTypeID> getHeroesInPool() const;
  113. CGObjectInstance * getObject(ObjectInstanceID obj);
  114. const CGObjectInstance * getObject(ObjectInstanceID obj) const;
  115. template<typename ObjectType = CGObjectInstance>
  116. std::vector<const ObjectType *> getObjects() const
  117. {
  118. std::vector<const ObjectType *> result;
  119. for (const auto & object : objects)
  120. {
  121. auto casted = dynamic_cast<const ObjectType*>(object.get());
  122. if (casted)
  123. result.push_back(casted);
  124. }
  125. return result;
  126. }
  127. template<typename ObjectType = CGObjectInstance>
  128. std::vector<ObjectType *> getObjects()
  129. {
  130. std::vector<ObjectType *> result;
  131. for (const auto & object : objects)
  132. {
  133. auto casted = dynamic_cast<ObjectType*>(object.get());
  134. if (casted)
  135. result.push_back(casted);
  136. }
  137. return result;
  138. }
  139. bool isWaterMap() const;
  140. bool calculateWaterContent();
  141. void banWaterArtifacts();
  142. void banWaterHeroes();
  143. void banHero(const HeroTypeID& id);
  144. void unbanHero(const HeroTypeID & id);
  145. void banWaterSpells();
  146. void banWaterSkills();
  147. void banWaterContent();
  148. /// Gets object of specified type on requested position
  149. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  150. /// Returns pointer to hero of specified type if hero is present on map
  151. CGHeroInstance * getHero(HeroTypeID heroId);
  152. /// Returns ID's of all heroes that are currently present on map
  153. /// All garrisoned heroes are included from this list
  154. /// All prisons are excluded from this list
  155. const std::vector<ObjectInstanceID> & getHeroesOnMap();
  156. /// Returns ID's of all towns present on map
  157. const std::vector<ObjectInstanceID> & getAllTowns();
  158. /// Sets the victory/loss condition objectives ??
  159. void checkForObjectives();
  160. void resolveQuestIdentifiers();
  161. void reindexObjects();
  162. std::vector<Rumor> rumors;
  163. std::set<SpellID> allowedSpells;
  164. std::set<ArtifactID> allowedArtifact;
  165. std::set<SecondarySkill> allowedAbilities;
  166. std::vector<CMapEvent> events;
  167. int3 grailPos;
  168. int grailRadius;
  169. //Helper lists
  170. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  171. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  172. std::map<si32, ObjectInstanceID> questIdentifierToId;
  173. std::unique_ptr<CMapEditManager> editManager;
  174. boost::multi_array<int3, 3> guardingCreaturePositions;
  175. std::map<std::string, std::shared_ptr<CGObjectInstance> > instanceNames;
  176. bool waterMap;
  177. ui8 obeliskCount = 0; //how many obelisks are on map
  178. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  179. std::vector<ArtifactID> townMerchantArtifacts;
  180. std::vector<TradeItemBuy> townUniversitySkills;
  181. void overrideGameSettings(const JsonNode & input);
  182. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  183. const IGameSettings & getSettings() const;
  184. void postInitialize();
  185. private:
  186. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  187. boost::multi_array<TerrainTile, 3> terrain;
  188. si32 uidCounter; //TODO: initialize when loading an old map
  189. public:
  190. template <typename Handler>
  191. void serialize(Handler &h)
  192. {
  193. h & static_cast<CMapHeader&>(*this);
  194. h & triggeredEvents; //from CMapHeader
  195. h & rumors;
  196. h & allowedSpells;
  197. h & allowedAbilities;
  198. h & allowedArtifact;
  199. h & events;
  200. h & grailPos;
  201. h & artInstances;
  202. h & quests;
  203. //TODO: viccondetails
  204. h & terrain;
  205. h & guardingCreaturePositions;
  206. h & objects;
  207. h & heroesOnMap;
  208. h & teleportChannels;
  209. h & towns;
  210. h & artInstances;
  211. // static members
  212. h & obeliskCount;
  213. h & obelisksVisited;
  214. h & townMerchantArtifacts;
  215. h & townUniversitySkills;
  216. h & instanceNames;
  217. h & *gameSettings;
  218. }
  219. };
  220. inline bool CMap::isInTheMap(const int3 & pos) const
  221. {
  222. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  223. return
  224. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  225. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  226. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  227. }
  228. inline TerrainTile & CMap::getTile(const int3 & tile)
  229. {
  230. assert(isInTheMap(tile));
  231. return terrain[tile.z][tile.x][tile.y];
  232. }
  233. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  234. {
  235. assert(isInTheMap(tile));
  236. return terrain[tile.z][tile.x][tile.y];
  237. }
  238. VCMI_LIB_NAMESPACE_END