CMap.h 8.0 KB

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