CMapDefines.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * CMapDefines.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 "../ResourceSet.h"
  12. #include "../texts/MetaString.h"
  13. #include "../GameLibrary.h"
  14. #include "../TerrainHandler.h"
  15. #include "../mapObjects/CGObjectInstance.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class TerrainType;
  18. class RiverType;
  19. class RoadType;
  20. class CGObjectInstance;
  21. class CGTownInstance;
  22. class JsonSerializeFormat;
  23. /// The map event is an event which e.g. gives or takes resources of a specific
  24. /// amount to/from players and can appear regularly or once a time.
  25. class DLL_LINKAGE CMapEvent
  26. {
  27. public:
  28. CMapEvent();
  29. virtual ~CMapEvent() = default;
  30. bool occursToday(int currentDay) const;
  31. bool affectsPlayer(PlayerColor player, bool isHuman) const;
  32. std::string name;
  33. MetaString message;
  34. TResources resources;
  35. std::set<PlayerColor> players;
  36. bool humanAffected;
  37. bool computerAffected;
  38. ui32 firstOccurrence;
  39. ui32 nextOccurrence; /// specifies after how many days the event will occur the next time; 0 if event occurs only one time
  40. std::vector<ObjectInstanceID> deletedObjectsInstances;
  41. template <typename Handler>
  42. void serialize(Handler & h)
  43. {
  44. h & name;
  45. h & message;
  46. h & resources;
  47. h & players;
  48. h & humanAffected;
  49. h & computerAffected;
  50. h & firstOccurrence;
  51. h & nextOccurrence;
  52. h & deletedObjectsInstances;
  53. }
  54. virtual void serializeJson(JsonSerializeFormat & handler);
  55. };
  56. /// The castle event builds/adds buildings/creatures for a specific town.
  57. class DLL_LINKAGE CCastleEvent: public CMapEvent
  58. {
  59. public:
  60. CCastleEvent() = default;
  61. std::set<BuildingID> buildings;
  62. std::vector<si32> creatures;
  63. template <typename Handler>
  64. void serialize(Handler & h)
  65. {
  66. h & static_cast<CMapEvent &>(*this);
  67. h & buildings;
  68. h & creatures;
  69. }
  70. void serializeJson(JsonSerializeFormat & handler) override;
  71. };
  72. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  73. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  74. struct DLL_LINKAGE TerrainTile
  75. {
  76. TerrainTile();
  77. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  78. inline bool entrableTerrain() const;
  79. inline bool entrableTerrain(const TerrainTile * from) const;
  80. inline bool entrableTerrain(bool allowLand, bool allowSea) const;
  81. /// Checks for blocking objects and terraint type (water / land).
  82. bool isClear(const TerrainTile * from = nullptr) const;
  83. /// Gets the ID of the top visitable object or -1 if there is none.
  84. ObjectInstanceID topVisitableObj(bool excludeTop = false) const;
  85. inline bool isWater() const;
  86. inline bool isLand() const;
  87. EDiggingStatus getDiggingStatus(bool excludeTop = true) const;
  88. inline bool hasFavorableWinds() const;
  89. inline bool visitable() const;
  90. inline bool blocked() const;
  91. inline const TerrainType * getTerrain() const;
  92. inline const RiverType * getRiver() const;
  93. inline const RoadType * getRoad() const;
  94. inline TerrainId getTerrainID() const;
  95. inline RiverId getRiverID() const;
  96. inline RoadId getRoadID() const;
  97. inline bool hasRiver() const;
  98. inline bool hasRoad() const;
  99. TerrainId terrainType;
  100. RiverId riverType;
  101. RoadId roadType;
  102. ui8 terView;
  103. ui8 riverDir;
  104. ui8 roadDir;
  105. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  106. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favorable Winds effect
  107. ui8 extTileFlags;
  108. std::vector<ObjectInstanceID> visitableObjects;
  109. std::vector<ObjectInstanceID> blockingObjects;
  110. template <typename Handler>
  111. void serialize(Handler & h)
  112. {
  113. h & terrainType;
  114. h & terView;
  115. h & riverType;
  116. h & riverDir;
  117. h & roadType;
  118. h & roadDir;
  119. h & extTileFlags;
  120. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  121. {
  122. h & visitableObjects;
  123. h & blockingObjects;
  124. }
  125. else
  126. {
  127. std::vector<std::shared_ptr<CGObjectInstance>> objectPtrs;
  128. h & objectPtrs;
  129. for (const auto & ptr : objectPtrs)
  130. visitableObjects.push_back(ptr->id);
  131. h & objectPtrs;
  132. for (const auto & ptr : objectPtrs)
  133. blockingObjects.push_back(ptr->id);
  134. }
  135. }
  136. };
  137. inline bool TerrainTile::hasFavorableWinds() const
  138. {
  139. return extTileFlags & 128;
  140. }
  141. inline bool TerrainTile::isWater() const
  142. {
  143. return getTerrain()->isWater();
  144. }
  145. inline bool TerrainTile::isLand() const
  146. {
  147. return getTerrain()->isLand();
  148. }
  149. inline bool TerrainTile::visitable() const
  150. {
  151. return !visitableObjects.empty();
  152. }
  153. inline bool TerrainTile::blocked() const
  154. {
  155. return !blockingObjects.empty();
  156. }
  157. inline bool TerrainTile::hasRiver() const
  158. {
  159. return getRiverID() != RiverId::NO_RIVER;
  160. }
  161. inline bool TerrainTile::hasRoad() const
  162. {
  163. return getRoadID() != RoadId::NO_ROAD;
  164. }
  165. inline const TerrainType * TerrainTile::getTerrain() const
  166. {
  167. return terrainType.toEntity(LIBRARY);
  168. }
  169. inline const RiverType * TerrainTile::getRiver() const
  170. {
  171. return riverType.toEntity(LIBRARY);
  172. }
  173. inline const RoadType * TerrainTile::getRoad() const
  174. {
  175. return roadType.toEntity(LIBRARY);
  176. }
  177. inline TerrainId TerrainTile::getTerrainID() const
  178. {
  179. return terrainType;
  180. }
  181. inline RiverId TerrainTile::getRiverID() const
  182. {
  183. return riverType;
  184. }
  185. inline RoadId TerrainTile::getRoadID() const
  186. {
  187. return roadType;
  188. }
  189. inline bool TerrainTile::entrableTerrain() const
  190. {
  191. return entrableTerrain(true, true);
  192. }
  193. inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  194. {
  195. const TerrainType * terrainFrom = from->getTerrain();
  196. return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
  197. }
  198. inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  199. {
  200. const TerrainType * terrain = getTerrain();
  201. return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
  202. }
  203. VCMI_LIB_NAMESPACE_END