CMap.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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. struct TeleportChannel;
  30. /// The rumor struct consists of a rumor name and text.
  31. struct DLL_LINKAGE Rumor
  32. {
  33. std::string name;
  34. MetaString text;
  35. Rumor() = default;
  36. ~Rumor() = default;
  37. template <typename Handler>
  38. void serialize(Handler & h)
  39. {
  40. h & name;
  41. h & text;
  42. }
  43. void serializeJson(JsonSerializeFormat & handler);
  44. };
  45. /// The disposed hero struct describes which hero can be hired from which player.
  46. struct DLL_LINKAGE DisposedHero
  47. {
  48. DisposedHero();
  49. HeroTypeID heroId;
  50. HeroTypeID portrait; /// The portrait id of the hero, -1 is default.
  51. std::string name;
  52. std::set<PlayerColor> players; /// Who can hire this hero (bitfield).
  53. template <typename Handler>
  54. void serialize(Handler & h)
  55. {
  56. h & heroId;
  57. h & portrait;
  58. h & name;
  59. h & players;
  60. }
  61. };
  62. /// The map contains the map header, the tiles of the terrain, objects, heroes, towns, rumors...
  63. class DLL_LINKAGE CMap : public CMapHeader, public GameCallbackHolder
  64. {
  65. public:
  66. explicit CMap(IGameCallback *cb);
  67. ~CMap();
  68. void initTerrain();
  69. CMapEditManager * getEditManager();
  70. TerrainTile & getTile(const int3 & tile);
  71. const TerrainTile & getTile(const int3 & tile) const;
  72. bool isCoastalTile(const int3 & pos) const;
  73. bool isInTheMap(const int3 & pos) const;
  74. bool isWaterTile(const int3 & pos) const;
  75. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  76. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  77. int3 guardingCreaturePosition (int3 pos) const;
  78. void addBlockVisTiles(CGObjectInstance * obj);
  79. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  80. void calculateGuardingGreaturePositions();
  81. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  82. void eraseArtifactInstance(CArtifactInstance * art);
  83. void addNewQuestInstance(CQuest * quest);
  84. void removeQuestInstance(CQuest * quest);
  85. void setUniqueInstanceName(CGObjectInstance * obj);
  86. ///Use only this method when creating new map object instances
  87. void addNewObject(CGObjectInstance * obj);
  88. void moveObject(CGObjectInstance * obj, const int3 & dst);
  89. void removeObject(CGObjectInstance * obj);
  90. bool isWaterMap() const;
  91. bool calculateWaterContent();
  92. void banWaterArtifacts();
  93. void banWaterHeroes();
  94. void banHero(const HeroTypeID& id);
  95. void unbanHero(const HeroTypeID & id);
  96. void banWaterSpells();
  97. void banWaterSkills();
  98. void banWaterContent();
  99. /// Gets object of specified type on requested position
  100. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  101. CGHeroInstance * getHero(HeroTypeID heroId);
  102. /// Sets the victory/loss condition objectives ??
  103. void checkForObjectives();
  104. void resetStaticData();
  105. void resolveQuestIdentifiers();
  106. void reindexObjects();
  107. ui32 checksum;
  108. std::vector<Rumor> rumors;
  109. std::vector<DisposedHero> disposedHeroes;
  110. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  111. std::set<SpellID> allowedSpells;
  112. std::set<ArtifactID> allowedArtifact;
  113. std::set<SecondarySkill> allowedAbilities;
  114. std::vector<CMapEvent> events;
  115. int3 grailPos;
  116. int grailRadius;
  117. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  118. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  119. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  120. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  121. std::vector< ConstTransitivePtr<CQuest> > quests;
  122. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  123. //Helper lists
  124. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  125. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  126. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  127. std::map<si32, ObjectInstanceID> questIdentifierToId;
  128. std::unique_ptr<CMapEditManager> editManager;
  129. boost::multi_array<int3, 3> guardingCreaturePositions;
  130. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  131. bool waterMap;
  132. ui8 obeliskCount = 0; //how many obelisks are on map
  133. std::map<TeamID, ui8> obelisksVisited; //map: team_id => how many obelisks has been visited
  134. std::vector<const CArtifact *> townMerchantArtifacts;
  135. std::vector<TradeItemBuy> townUniversitySkills;
  136. private:
  137. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  138. boost::multi_array<TerrainTile, 3> terrain;
  139. si32 uidCounter; //TODO: initialize when loading an old map
  140. public:
  141. template <typename Handler>
  142. void serialize(Handler &h)
  143. {
  144. h & static_cast<CMapHeader&>(*this);
  145. h & triggeredEvents; //from CMapHeader
  146. h & rumors;
  147. h & allowedSpells;
  148. h & allowedAbilities;
  149. h & allowedArtifact;
  150. h & events;
  151. h & grailPos;
  152. h & artInstances;
  153. h & quests;
  154. h & allHeroes;
  155. if (h.version < Handler::Version::DESTROYED_OBJECTS)
  156. {
  157. // old save compatibility
  158. //FIXME: remove this field after save-breaking change
  159. h & questIdentifierToId;
  160. resolveQuestIdentifiers();
  161. }
  162. //TODO: viccondetails
  163. h & terrain;
  164. h & guardingCreaturePositions;
  165. h & objects;
  166. h & heroesOnMap;
  167. h & teleportChannels;
  168. h & towns;
  169. h & artInstances;
  170. // static members
  171. h & obeliskCount;
  172. h & obelisksVisited;
  173. h & townMerchantArtifacts;
  174. h & townUniversitySkills;
  175. h & instanceNames;
  176. }
  177. };
  178. VCMI_LIB_NAMESPACE_END