CMapDefines.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "../VCMI_Lib.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. if (h.version >= Handler::Version::EVENTS_PLAYER_SET)
  47. {
  48. h & players;
  49. }
  50. else
  51. {
  52. ui8 playersMask = 0;
  53. h & playersMask;
  54. for (int i = 0; i < 8; ++i)
  55. if ((playersMask & (1 << i)) != 0)
  56. players.insert(PlayerColor(i));
  57. }
  58. h & humanAffected;
  59. h & computerAffected;
  60. h & firstOccurrence;
  61. h & nextOccurrence;
  62. if(h.version >= Handler::Version::EVENT_OBJECTS_DELETION)
  63. {
  64. h & deletedObjectsInstances;
  65. }
  66. }
  67. virtual void serializeJson(JsonSerializeFormat & handler);
  68. };
  69. /// The castle event builds/adds buildings/creatures for a specific town.
  70. class DLL_LINKAGE CCastleEvent: public CMapEvent
  71. {
  72. public:
  73. CCastleEvent() = default;
  74. std::set<BuildingID> buildings;
  75. std::vector<si32> creatures;
  76. template <typename Handler>
  77. void serialize(Handler & h)
  78. {
  79. h & static_cast<CMapEvent &>(*this);
  80. h & buildings;
  81. h & creatures;
  82. }
  83. void serializeJson(JsonSerializeFormat & handler) override;
  84. };
  85. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  86. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  87. struct DLL_LINKAGE TerrainTile
  88. {
  89. TerrainTile();
  90. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  91. inline bool entrableTerrain() const;
  92. inline bool entrableTerrain(const TerrainTile * from) const;
  93. inline bool entrableTerrain(bool allowLand, bool allowSea) const;
  94. /// Checks for blocking objects and terraint type (water / land).
  95. bool isClear(const TerrainTile * from = nullptr) const;
  96. /// Gets the ID of the top visitable object or -1 if there is none.
  97. Obj topVisitableId(bool excludeTop = false) const;
  98. CGObjectInstance * topVisitableObj(bool excludeTop = false) const;
  99. inline bool isWater() const;
  100. inline bool isLand() const;
  101. EDiggingStatus getDiggingStatus(bool excludeTop = true) const;
  102. inline bool hasFavorableWinds() const;
  103. inline bool visitable() const;
  104. inline bool blocked() const;
  105. inline const TerrainType * getTerrain() const;
  106. inline const RiverType * getRiver() const;
  107. inline const RoadType * getRoad() const;
  108. inline TerrainId getTerrainID() const;
  109. inline RiverId getRiverID() const;
  110. inline RoadId getRoadID() const;
  111. inline bool hasRiver() const;
  112. inline bool hasRoad() const;
  113. TerrainId terrainType;
  114. RiverId riverType;
  115. RoadId roadType;
  116. ui8 terView;
  117. ui8 riverDir;
  118. ui8 roadDir;
  119. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  120. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favorable Winds effect
  121. ui8 extTileFlags;
  122. std::vector<CGObjectInstance *> visitableObjects;
  123. std::vector<CGObjectInstance *> blockingObjects;
  124. template <typename Handler>
  125. void serialize(Handler & h)
  126. {
  127. if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
  128. {
  129. h & terrainType;
  130. }
  131. else
  132. {
  133. bool isNull = false;
  134. h & isNull;
  135. if (!isNull)
  136. h & terrainType;
  137. }
  138. h & terView;
  139. if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
  140. {
  141. h & riverType;
  142. }
  143. else
  144. {
  145. bool isNull = false;
  146. h & isNull;
  147. if (!isNull)
  148. h & riverType;
  149. }
  150. h & riverDir;
  151. if (h.version >= Handler::Version::REMOVE_VLC_POINTERS)
  152. {
  153. h & roadType;
  154. }
  155. else
  156. {
  157. bool isNull = false;
  158. h & isNull;
  159. if (!isNull)
  160. h & roadType;
  161. }
  162. h & roadDir;
  163. h & extTileFlags;
  164. if (h.version < Handler::Version::REMOVE_VLC_POINTERS)
  165. {
  166. bool unused = false;
  167. h & unused;
  168. h & unused;
  169. }
  170. h & visitableObjects;
  171. h & blockingObjects;
  172. }
  173. };
  174. inline bool TerrainTile::hasFavorableWinds() const
  175. {
  176. return extTileFlags & 128;
  177. }
  178. inline bool TerrainTile::isWater() const
  179. {
  180. return getTerrain()->isWater();
  181. }
  182. inline bool TerrainTile::isLand() const
  183. {
  184. return getTerrain()->isLand();
  185. }
  186. inline bool TerrainTile::visitable() const
  187. {
  188. return !visitableObjects.empty();
  189. }
  190. inline bool TerrainTile::blocked() const
  191. {
  192. return !blockingObjects.empty();
  193. }
  194. inline bool TerrainTile::hasRiver() const
  195. {
  196. return getRiverID() != RiverId::NO_RIVER;
  197. }
  198. inline bool TerrainTile::hasRoad() const
  199. {
  200. return getRoadID() != RoadId::NO_ROAD;
  201. }
  202. inline const TerrainType * TerrainTile::getTerrain() const
  203. {
  204. return terrainType.toEntity(VLC);
  205. }
  206. inline const RiverType * TerrainTile::getRiver() const
  207. {
  208. return riverType.toEntity(VLC);
  209. }
  210. inline const RoadType * TerrainTile::getRoad() const
  211. {
  212. return roadType.toEntity(VLC);
  213. }
  214. inline TerrainId TerrainTile::getTerrainID() const
  215. {
  216. return terrainType;
  217. }
  218. inline RiverId TerrainTile::getRiverID() const
  219. {
  220. return riverType;
  221. }
  222. inline RoadId TerrainTile::getRoadID() const
  223. {
  224. return roadType;
  225. }
  226. inline bool TerrainTile::entrableTerrain() const
  227. {
  228. return entrableTerrain(true, true);
  229. }
  230. inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  231. {
  232. const TerrainType * terrainFrom = from->getTerrain();
  233. return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
  234. }
  235. inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  236. {
  237. const TerrainType * terrain = getTerrain();
  238. return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
  239. }
  240. VCMI_LIB_NAMESPACE_END