CMap.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "../ConstTransitivePtr.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 CGCreature;
  23. class CQuest;
  24. class CGTownInstance;
  25. class IModableArt;
  26. class IQuestObject;
  27. class CInputStream;
  28. class CMapEditManager;
  29. class JsonSerializeFormat;
  30. class IGameSettings;
  31. class GameSettings;
  32. struct TeleportChannel;
  33. enum class EGameSettings;
  34. /// The rumor struct consists of a rumor name and text.
  35. struct DLL_LINKAGE Rumor
  36. {
  37. std::string name;
  38. MetaString text;
  39. Rumor() = default;
  40. ~Rumor() = default;
  41. template <typename Handler>
  42. void serialize(Handler & h)
  43. {
  44. h & name;
  45. h & text;
  46. }
  47. void serializeJson(JsonSerializeFormat & handler);
  48. };
  49. /// The disposed hero struct describes which hero can be hired from which player.
  50. struct DLL_LINKAGE DisposedHero
  51. {
  52. DisposedHero();
  53. HeroTypeID heroId;
  54. HeroTypeID portrait; /// The portrait id of the hero, -1 is default.
  55. std::string name;
  56. std::set<PlayerColor> players; /// Who can hire this hero (bitfield).
  57. template <typename Handler>
  58. void serialize(Handler & h)
  59. {
  60. h & heroId;
  61. h & portrait;
  62. h & name;
  63. h & players;
  64. }
  65. };
  66. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  67. class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
  68. {
  69. std::unique_ptr<GameSettings> gameSettings;
  70. public:
  71. explicit CMap(IGameCallback *cb);
  72. ~CMap();
  73. void initTerrain();
  74. CMapEditManager * getEditManager();
  75. inline TerrainTile & getTile(const int3 & tile);
  76. inline const TerrainTile & getTile(const int3 & tile) const;
  77. bool isCoastalTile(const int3 & pos) const;
  78. inline bool isInTheMap(const int3 & pos) const;
  79. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  80. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  81. int3 guardingCreaturePosition (int3 pos) const;
  82. void addBlockVisTiles(CGObjectInstance * obj);
  83. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  84. void calculateGuardingGreaturePositions();
  85. void addNewArtifactInstance(CArtifactSet & artSet);
  86. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  87. void eraseArtifactInstance(CArtifactInstance * art);
  88. void moveArtifactInstance(CArtifactSet & srcSet, const ArtifactPosition & srcSlot, CArtifactSet & dstSet, const ArtifactPosition & dstSlot);
  89. void putArtifactInstance(CArtifactSet & set, CArtifactInstance * art, const ArtifactPosition & slot);
  90. void removeArtifactInstance(CArtifactSet & set, const ArtifactPosition & slot);
  91. void addNewQuestInstance(CQuest * quest);
  92. void removeQuestInstance(CQuest * quest);
  93. void setUniqueInstanceName(CGObjectInstance * obj);
  94. ///Use only this method when creating new map object instances
  95. void addNewObject(CGObjectInstance * obj);
  96. void moveObject(CGObjectInstance * obj, const int3 & dst);
  97. void removeObject(CGObjectInstance * obj);
  98. bool isWaterMap() const;
  99. bool calculateWaterContent();
  100. void banWaterArtifacts();
  101. void banWaterHeroes();
  102. void banHero(const HeroTypeID& id);
  103. void unbanHero(const HeroTypeID & id);
  104. void banWaterSpells();
  105. void banWaterSkills();
  106. void banWaterContent();
  107. /// Gets object of specified type on requested position
  108. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  109. CGHeroInstance * getHero(HeroTypeID heroId);
  110. /// Sets the victory/loss condition objectives ??
  111. void checkForObjectives();
  112. void resetStaticData();
  113. void resolveQuestIdentifiers();
  114. void reindexObjects();
  115. std::vector<Rumor> rumors;
  116. std::vector<DisposedHero> disposedHeroes;
  117. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  118. std::set<SpellID> allowedSpells;
  119. std::set<ArtifactID> allowedArtifact;
  120. std::set<SecondarySkill> allowedAbilities;
  121. std::vector<CMapEvent> events;
  122. int3 grailPos;
  123. int grailRadius;
  124. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  125. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  126. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  127. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  128. std::vector< ConstTransitivePtr<CQuest> > quests;
  129. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  130. //Helper lists
  131. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  132. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  133. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  134. std::map<si32, ObjectInstanceID> questIdentifierToId;
  135. std::unique_ptr<CMapEditManager> editManager;
  136. boost::multi_array<int3, 3> guardingCreaturePositions;
  137. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  138. bool waterMap;
  139. ui8 obeliskCount = 0; //how many obelisks are on map
  140. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  141. std::vector<ArtifactID> townMerchantArtifacts;
  142. std::vector<TradeItemBuy> townUniversitySkills;
  143. void overrideGameSettings(const JsonNode & input);
  144. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  145. const IGameSettings & getSettings() const;
  146. private:
  147. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  148. boost::multi_array<TerrainTile, 3> terrain;
  149. si32 uidCounter; //TODO: initialize when loading an old map
  150. public:
  151. template <typename Handler>
  152. void serialize(Handler &h)
  153. {
  154. h & static_cast<CMapHeader&>(*this);
  155. h & triggeredEvents; //from CMapHeader
  156. h & rumors;
  157. h & allowedSpells;
  158. h & allowedAbilities;
  159. h & allowedArtifact;
  160. h & events;
  161. h & grailPos;
  162. h & artInstances;
  163. h & quests;
  164. h & allHeroes;
  165. //TODO: viccondetails
  166. h & terrain;
  167. h & guardingCreaturePositions;
  168. h & objects;
  169. h & heroesOnMap;
  170. h & teleportChannels;
  171. h & towns;
  172. h & artInstances;
  173. // static members
  174. h & obeliskCount;
  175. h & obelisksVisited;
  176. if (h.version < Handler::Version::REMOVE_VLC_POINTERS)
  177. {
  178. int32_t size = 0;
  179. h & size;
  180. for (int32_t i = 0; i < size; ++i)
  181. {
  182. bool isNull = false;
  183. ArtifactID artifact;
  184. h & isNull;
  185. if (!isNull)
  186. h & artifact;
  187. townMerchantArtifacts.push_back(artifact);
  188. }
  189. }
  190. else
  191. {
  192. h & townMerchantArtifacts;
  193. }
  194. h & townUniversitySkills;
  195. h & instanceNames;
  196. if (h.version >= Handler::Version::PER_MAP_GAME_SETTINGS)
  197. h & *gameSettings;
  198. }
  199. };
  200. inline bool CMap::isInTheMap(const int3 & pos) const
  201. {
  202. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  203. return
  204. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  205. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  206. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  207. }
  208. inline TerrainTile & CMap::getTile(const int3 & tile)
  209. {
  210. assert(isInTheMap(tile));
  211. return terrain[tile.z][tile.x][tile.y];
  212. }
  213. inline const TerrainTile & CMap::getTile(const int3 & tile) const
  214. {
  215. assert(isInTheMap(tile));
  216. return terrain[tile.z][tile.x][tile.y];
  217. }
  218. VCMI_LIB_NAMESPACE_END