CMap.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 isWaterTile(const int3 & pos) const;
  74. inline bool isInTheMap(const int3 & pos) const
  75. {
  76. // Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
  77. return
  78. static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
  79. static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
  80. static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
  81. }
  82. bool canMoveBetween(const int3 &src, const int3 &dst) const;
  83. bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
  84. int3 guardingCreaturePosition (int3 pos) const;
  85. void addBlockVisTiles(CGObjectInstance * obj);
  86. void removeBlockVisTiles(CGObjectInstance * obj, bool total = false);
  87. void calculateGuardingGreaturePositions();
  88. void addNewArtifactInstance(ConstTransitivePtr<CArtifactInstance> art);
  89. void eraseArtifactInstance(CArtifactInstance * art);
  90. void addNewQuestInstance(CQuest * quest);
  91. void removeQuestInstance(CQuest * quest);
  92. void setUniqueInstanceName(CGObjectInstance * obj);
  93. ///Use only this method when creating new map object instances
  94. void addNewObject(CGObjectInstance * obj);
  95. void moveObject(CGObjectInstance * obj, const int3 & dst);
  96. void removeObject(CGObjectInstance * obj);
  97. bool isWaterMap() const;
  98. bool calculateWaterContent();
  99. void banWaterArtifacts();
  100. void banWaterHeroes();
  101. void banHero(const HeroTypeID& id);
  102. void unbanHero(const HeroTypeID & id);
  103. void banWaterSpells();
  104. void banWaterSkills();
  105. void banWaterContent();
  106. /// Gets object of specified type on requested position
  107. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj type);
  108. CGHeroInstance * getHero(HeroTypeID heroId);
  109. /// Sets the victory/loss condition objectives ??
  110. void checkForObjectives();
  111. void resetStaticData();
  112. void resolveQuestIdentifiers();
  113. void reindexObjects();
  114. ui32 checksum;
  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<const CArtifact *> townMerchantArtifacts;
  142. std::vector<TradeItemBuy> townUniversitySkills;
  143. private:
  144. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  145. boost::multi_array<TerrainTile, 3> terrain;
  146. si32 uidCounter; //TODO: initialize when loading an old map
  147. public:
  148. template <typename Handler>
  149. void serialize(Handler &h)
  150. {
  151. h & static_cast<CMapHeader&>(*this);
  152. h & triggeredEvents; //from CMapHeader
  153. h & rumors;
  154. h & allowedSpells;
  155. h & allowedAbilities;
  156. h & allowedArtifact;
  157. h & events;
  158. h & grailPos;
  159. h & artInstances;
  160. h & quests;
  161. h & allHeroes;
  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