CMap.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. TerrainTile & getTile(const int3 & tile);
  76. const TerrainTile & getTile(const int3 & tile) const;
  77. bool isCoastalTile(const int3 & pos) const;
  78. bool isWaterTile(const int3 & pos) const;
  79. inline bool isInTheMap(const int3 & pos) const
  80. {
  81. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  82. return
  83. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  84. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  85. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  86. }
  87. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  88. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  89. int3 guardingCreaturePosition (int3 pos) const;
  90. void addBlockVisTiles(CGObjectInstance * obj);
  91. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  92. void calculateGuardingGreaturePositions();
  93. void addNewArtifactInstance(CArtifactSet & artSet);
  94. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  95. void eraseArtifactInstance(CArtifactInstance * art);
  96. void addNewQuestInstance(CQuest * quest);
  97. void removeQuestInstance(CQuest * quest);
  98. void setUniqueInstanceName(CGObjectInstance * obj);
  99. ///Use only this method when creating new map object instances
  100. void addNewObject(CGObjectInstance * obj);
  101. void moveObject(CGObjectInstance * obj, const int3 & dst);
  102. void removeObject(CGObjectInstance * obj);
  103. bool isWaterMap() const;
  104. bool calculateWaterContent();
  105. void banWaterArtifacts();
  106. void banWaterHeroes();
  107. void banHero(const HeroTypeID& id);
  108. void unbanHero(const HeroTypeID & id);
  109. void banWaterSpells();
  110. void banWaterSkills();
  111. void banWaterContent();
  112. /// Gets object of specified type on requested position
  113. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  114. const CGObjectInstance * getObjectFrom(const int3 & pos);
  115. CGHeroInstance * getHero(HeroTypeID heroId);
  116. /// Sets the victory/loss condition objectives ??
  117. void checkForObjectives();
  118. void resetStaticData();
  119. void resolveQuestIdentifiers();
  120. void reindexObjects();
  121. ui32 checksum;
  122. std::vector<Rumor> rumors;
  123. std::vector<DisposedHero> disposedHeroes;
  124. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  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< ConstTransitivePtr<CGObjectInstance> > objects;
  133. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  134. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  135. std::vector< ConstTransitivePtr<CQuest> > quests;
  136. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  137. //Helper lists
  138. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  139. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  140. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  141. std::map<si32, ObjectInstanceID> questIdentifierToId;
  142. std::unique_ptr<CMapEditManager> editManager;
  143. boost::multi_array<int3, 3> guardingCreaturePositions;
  144. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  145. bool waterMap;
  146. ui8 obeliskCount = 0; //how many obelisks are on map
  147. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  148. std::vector<const CArtifact *> townMerchantArtifacts;
  149. std::vector<TradeItemBuy> townUniversitySkills;
  150. void overrideGameSettings(const JsonNode & input);
  151. void overrideGameSetting(EGameSettings option, const JsonNode & input);
  152. const IGameSettings & getSettings() const;
  153. private:
  154. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  155. boost::multi_array<TerrainTile, 3> terrain;
  156. si32 uidCounter; //TODO: initialize when loading an old map
  157. public:
  158. template <typename Handler>
  159. void serialize(Handler &h)
  160. {
  161. h & static_cast<CMapHeader&>(*this);
  162. h & triggeredEvents; //from CMapHeader
  163. h & rumors;
  164. h & allowedSpells;
  165. h & allowedAbilities;
  166. h & allowedArtifact;
  167. h & events;
  168. h & grailPos;
  169. h & artInstances;
  170. h & quests;
  171. h & allHeroes;
  172. //TODO: viccondetails
  173. h & terrain;
  174. h & guardingCreaturePositions;
  175. h & objects;
  176. h & heroesOnMap;
  177. h & teleportChannels;
  178. h & towns;
  179. h & artInstances;
  180. // static members
  181. h & obeliskCount;
  182. h & obelisksVisited;
  183. h & townMerchantArtifacts;
  184. h & townUniversitySkills;
  185. h & instanceNames;
  186. if (h.version >= Handler::Version::PER_MAP_GAME_SETTINGS)
  187. h & *gameSettings;
  188. }
  189. };
  190. VCMI_LIB_NAMESPACE_END