CMap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "CMapHeader.h"
  12. #include "../mapObjects/MiscObjects.h" // To serialize static props
  13. #include "../mapObjects/CQuest.h" // To serialize static props
  14. #include "../mapObjects/CGTownInstance.h" // To serialize static props
  15. #include "CMapDefines.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. std::string text;
  35. Rumor() = default;
  36. ~Rumor() = default;
  37. template <typename Handler>
  38. void serialize(Handler & h, const int version)
  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. ui32 heroId;
  50. ui32 portrait; /// The portrait id of the hero, -1 is default.
  51. std::string name;
  52. ui8 players; /// Who can hire this hero (bitfield).
  53. template <typename Handler>
  54. void serialize(Handler & h, const int version)
  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
  64. {
  65. public:
  66. CMap();
  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(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. /// Gets object of specified type on requested position
  91. const CGObjectInstance * getObjectiveObjectFrom(const int3 & pos, Obj::EObj type);
  92. CGHeroInstance * getHero(int heroId);
  93. /// Sets the victory/loss condition objectives ??
  94. void checkForObjectives();
  95. void resetStaticData();
  96. ui32 checksum;
  97. std::vector<Rumor> rumors;
  98. std::vector<DisposedHero> disposedHeroes;
  99. std::vector<ConstTransitivePtr<CGHeroInstance> > predefinedHeroes;
  100. std::vector<bool> allowedSpell;
  101. std::vector<bool> allowedArtifact;
  102. std::vector<bool> allowedAbilities;
  103. std::list<CMapEvent> events;
  104. int3 grailPos;
  105. int grailRadius;
  106. //Central lists of items in game. Position of item in the vectors below is their (instance) id.
  107. std::vector< ConstTransitivePtr<CGObjectInstance> > objects;
  108. std::vector< ConstTransitivePtr<CGTownInstance> > towns;
  109. std::vector< ConstTransitivePtr<CArtifactInstance> > artInstances;
  110. std::vector< ConstTransitivePtr<CQuest> > quests;
  111. std::vector< ConstTransitivePtr<CGHeroInstance> > allHeroes; //indexed by [hero_type_id]; on map, disposed, prisons, etc.
  112. //Helper lists
  113. std::vector< ConstTransitivePtr<CGHeroInstance> > heroesOnMap;
  114. std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > teleportChannels;
  115. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  116. std::map<si32, ObjectInstanceID> questIdentifierToId;
  117. std::unique_ptr<CMapEditManager> editManager;
  118. int3 ***guardingCreaturePositions;
  119. std::map<std::string, ConstTransitivePtr<CGObjectInstance> > instanceNames;
  120. private:
  121. /// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
  122. TerrainTile*** terrain;
  123. si32 uidCounter; //TODO: initialize when loading an old map
  124. public:
  125. template <typename Handler>
  126. void serialize(Handler &h, const int formatVersion)
  127. {
  128. h & static_cast<CMapHeader&>(*this);
  129. h & triggeredEvents; //from CMapHeader
  130. h & rumors;
  131. h & allowedSpell;
  132. h & allowedAbilities;
  133. h & allowedArtifact;
  134. h & events;
  135. h & grailPos;
  136. h & artInstances;
  137. h & quests;
  138. h & allHeroes;
  139. h & questIdentifierToId;
  140. //TODO: viccondetails
  141. const int level = levels();
  142. if(h.saving)
  143. {
  144. // Save terrain
  145. for(int z = 0; z < level; ++z)
  146. {
  147. for(int x = 0; x < width; ++x)
  148. {
  149. for(int y = 0; y < height; ++y)
  150. {
  151. h & terrain[z][x][y];
  152. h & guardingCreaturePositions[z][x][y];
  153. }
  154. }
  155. }
  156. }
  157. else
  158. {
  159. // Load terrain
  160. terrain = new TerrainTile**[level];
  161. guardingCreaturePositions = new int3**[level];
  162. for(int z = 0; z < level; ++z)
  163. {
  164. terrain[z] = new TerrainTile*[width];
  165. guardingCreaturePositions[z] = new int3*[width];
  166. for(int x = 0; x < width; ++x)
  167. {
  168. terrain[z][x] = new TerrainTile[height];
  169. guardingCreaturePositions[z][x] = new int3[height];
  170. }
  171. }
  172. for(int z = 0; z < level; ++z)
  173. {
  174. for(int x = 0; x < width; ++x)
  175. {
  176. for(int y = 0; y < height; ++y)
  177. {
  178. h & terrain[z][x][y];
  179. h & guardingCreaturePositions[z][x][y];
  180. }
  181. }
  182. }
  183. }
  184. h & objects;
  185. h & heroesOnMap;
  186. h & teleportChannels;
  187. h & towns;
  188. h & artInstances;
  189. // static members
  190. h & CGKeys::playerKeyMap;
  191. h & CGMagi::eyelist;
  192. h & CGObelisk::obeliskCount;
  193. h & CGObelisk::visited;
  194. h & CGTownInstance::merchantArtifacts;
  195. h & CGTownInstance::universitySkills;
  196. h & instanceNames;
  197. }
  198. };
  199. VCMI_LIB_NAMESPACE_END