CMap.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /// All quests that are currently present on map
  54. std::vector<std::shared_ptr<CQuest>> quests;
  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 towns on map. Does not includes heroes in prisons
  62. std::vector<ObjectInstanceID> heroesOnMap;
  63. public:
  64. explicit CMap(IGameCallback *cb);
  65. ~CMap();
  66. void initTerrain();
  67. CMapEditManager * getEditManager();
  68. inline TerrainTile & getTile(const int3 & tile);
  69. inline const TerrainTile & getTile(const int3 & tile) const;
  70. bool isCoastalTile(const int3 & pos) const;
  71. inline bool isInTheMap(const int3 & pos) const;
  72. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  73. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  74. int3 guardingCreaturePosition (int3 pos) const;
  75. void addBlockVisTiles(CGObjectInstance * obj);
  76. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  77. void calculateGuardingGreaturePositions();
  78. CArtifactInstance * createScroll(const SpellID & spellId);
  79. CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  80. CArtifactInstance * createSingleArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  81. CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID);
  82. const CArtifactInstance * getArtifactInstance(const ArtifactInstanceID & artifactID) const;
  83. void eraseArtifactInstance(const ArtifactInstanceID art);
  84. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  85. void putArtifactInstance(CArtifactSet & set, const ArtifactInstanceID art, const ArtifactPosition & slot);
  86. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  87. void addNewQuestInstance(std::shared_ptr<CQuest> quest);
  88. void clearQuestInstance(const CQuest * quest);
  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. bool isWaterMap() const;
  112. bool calculateWaterContent();
  113. void banWaterArtifacts();
  114. void banWaterHeroes();
  115. void banHero(const HeroTypeID& id);
  116. void unbanHero(const HeroTypeID & id);
  117. void banWaterSpells();
  118. void banWaterSkills();
  119. void banWaterContent();
  120. /// Gets object of specified type on requested position
  121. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  122. /// Returns pointer to hero of specified type if hero is present on map
  123. CGHeroInstance * getHero(HeroTypeID heroId);
  124. /// Returns ID's of all heroes that are currently present on map
  125. /// All garrisoned heroes are included from this list
  126. /// All prisons are excluded from this list
  127. const std::vector<ObjectInstanceID> & getHeroesOnMap();
  128. /// Returns ID's of all towns present on map
  129. const std::vector<ObjectInstanceID> & getAllTowns();
  130. /// Sets the victory/loss condition objectives ??
  131. void checkForObjectives();
  132. void resolveQuestIdentifiers();
  133. void reindexObjects();
  134. std::vector<Rumor> rumors;
  135. std::set<SpellID> allowedSpells;
  136. std::set<ArtifactID> allowedArtifact;
  137. std::set<SecondarySkill> allowedAbilities;
  138. std::vector<CMapEvent> events;
  139. int3 grailPos;
  140. int grailRadius;
  141. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  142. std::vector< std::shared_ptr<CGObjectInstance> > objects;
  143. //Helper lists
  144. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  145. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  146. std::map<si32, ObjectInstanceID> questIdentifierToId;
  147. std::unique_ptr<CMapEditManager> editManager;
  148. boost::multi_array<int3, 3> guardingCreaturePositions;
  149. std::map<std::string, std::shared_ptr<CGObjectInstance> > instanceNames;
  150. bool waterMap;
  151. ui8 obeliskCount = 0; //how many obelisks are on map
  152. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  153. std::vector<ArtifactID> townMerchantArtifacts;
  154. std::vector<TradeItemBuy> townUniversitySkills;
  155. void overrideGameSettings(const JsonNode & input);
  156. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  157. const IGameSettings & getSettings() const;
  158. void postInitialize();
  159. private:
  160. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  161. boost::multi_array<TerrainTile, 3> terrain;
  162. si32 uidCounter; //TODO: initialize when loading an old map
  163. public:
  164. template <typename Handler>
  165. void serialize(Handler &h)
  166. {
  167. h & static_cast<CMapHeader&>(*this);
  168. h & triggeredEvents; //from CMapHeader
  169. h & rumors;
  170. h & allowedSpells;
  171. h & allowedAbilities;
  172. h & allowedArtifact;
  173. h & events;
  174. h & grailPos;
  175. h & artInstances;
  176. h & quests;
  177. //TODO: viccondetails
  178. h & terrain;
  179. h & guardingCreaturePositions;
  180. h & objects;
  181. h & heroesOnMap;
  182. h & teleportChannels;
  183. h & towns;
  184. h & artInstances;
  185. // static members
  186. h & obeliskCount;
  187. h & obelisksVisited;
  188. h & townMerchantArtifacts;
  189. h & townUniversitySkills;
  190. h & instanceNames;
  191. h & *gameSettings;
  192. }
  193. };
  194. inline bool CMap::isInTheMap(const int3 & pos) const
  195. {
  196. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  197. return
  198. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  199. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  200. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  201. }
  202. inline TerrainTile & CMap::getTile(const int3 & tile)
  203. {
  204. assert(isInTheMap(tile));
  205. return terrain[tile.z][tile.x][tile.y];
  206. }
  207. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  208. {
  209. assert(isInTheMap(tile));
  210. return terrain[tile.z][tile.x][tile.y];
  211. }
  212. VCMI_LIB_NAMESPACE_END