CMap.h 6.1 KB

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