CMapDefines.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Obj topVisitableId(bool excludeTop = false) const;
  84. CGObjectInstance * 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<CGObjectInstance *> visitableObjects;
  109. std::vector<CGObjectInstance *> 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. h & visitableObjects;
  121. h & blockingObjects;
  122. }
  123. };
  124. inline bool TerrainTile::hasFavorableWinds() const
  125. {
  126. return extTileFlags & 128;
  127. }
  128. inline bool TerrainTile::isWater() const
  129. {
  130. return getTerrain()->isWater();
  131. }
  132. inline bool TerrainTile::isLand() const
  133. {
  134. return getTerrain()->isLand();
  135. }
  136. inline bool TerrainTile::visitable() const
  137. {
  138. return !visitableObjects.empty();
  139. }
  140. inline bool TerrainTile::blocked() const
  141. {
  142. return !blockingObjects.empty();
  143. }
  144. inline bool TerrainTile::hasRiver() const
  145. {
  146. return getRiverID() != RiverId::NO_RIVER;
  147. }
  148. inline bool TerrainTile::hasRoad() const
  149. {
  150. return getRoadID() != RoadId::NO_ROAD;
  151. }
  152. inline const TerrainType * TerrainTile::getTerrain() const
  153. {
  154. return terrainType.toEntity(LIBRARY);
  155. }
  156. inline const RiverType * TerrainTile::getRiver() const
  157. {
  158. return riverType.toEntity(LIBRARY);
  159. }
  160. inline const RoadType * TerrainTile::getRoad() const
  161. {
  162. return roadType.toEntity(LIBRARY);
  163. }
  164. inline TerrainId TerrainTile::getTerrainID() const
  165. {
  166. return terrainType;
  167. }
  168. inline RiverId TerrainTile::getRiverID() const
  169. {
  170. return riverType;
  171. }
  172. inline RoadId TerrainTile::getRoadID() const
  173. {
  174. return roadType;
  175. }
  176. inline bool TerrainTile::entrableTerrain() const
  177. {
  178. return entrableTerrain(true, true);
  179. }
  180. inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  181. {
  182. const TerrainType * terrainFrom = from->getTerrain();
  183. return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
  184. }
  185. inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  186. {
  187. const TerrainType * terrain = getTerrain();
  188. return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
  189. }
  190. VCMI_LIB_NAMESPACE_END