CMapDefines.h 5.4 KB

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