TerrainTile.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * TerrainTile.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 "../GameLibrary.h"
  12. #include "../TerrainHandler.h"
  13. #include "../mapObjects/CGObjectInstance.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class TerrainType;
  16. class RiverType;
  17. class RoadType;
  18. class CGObjectInstance;
  19. /// The terrain tile describes the terrain type and the visual representation of the terrain.
  20. /// Furthermore the struct defines whether the tile is visitable or/and blocked and which objects reside in it.
  21. struct DLL_LINKAGE TerrainTile
  22. {
  23. TerrainTile();
  24. /// Gets true if the terrain is not a rock. If from is water/land, same type is also required.
  25. inline bool entrableTerrain() const;
  26. inline bool entrableTerrain(const TerrainTile * from) const;
  27. inline bool entrableTerrain(bool allowLand, bool allowSea) const;
  28. /// Checks for blocking objects and terraint type (water / land).
  29. bool isClear(const TerrainTile * from = nullptr) const;
  30. /// Gets the ID of the top visitable object or -1 if there is none.
  31. ObjectInstanceID topVisitableObj(bool excludeTop = false) const;
  32. inline bool isWater() const;
  33. inline bool isLand() const;
  34. EDiggingStatus getDiggingStatus(bool excludeTop = true) const;
  35. inline bool hasFavorableWinds() const;
  36. inline bool visitable() const;
  37. inline bool blocked() const;
  38. inline const TerrainType * getTerrain() const;
  39. inline const RiverType * getRiver() const;
  40. inline const RoadType * getRoad() const;
  41. inline TerrainId getTerrainID() const;
  42. inline RiverId getRiverID() const;
  43. inline RoadId getRoadID() const;
  44. inline bool hasRiver() const;
  45. inline bool hasRoad() const;
  46. TerrainId terrainType;
  47. RiverId riverType;
  48. RoadId roadType;
  49. ui8 terView;
  50. ui8 riverDir;
  51. ui8 roadDir;
  52. /// first two bits - how to rotate terrain graphic (next two - river graphic, next two - road);
  53. /// 7th bit - whether tile is coastal (allows disembarking if land or block movement if water); 8th bit - Favorable Winds effect
  54. ui8 extTileFlags;
  55. std::vector<ObjectInstanceID> visitableObjects;
  56. std::vector<ObjectInstanceID> blockingObjects;
  57. template<typename Handler>
  58. void serialize(Handler & h)
  59. {
  60. h & terrainType;
  61. h & terView;
  62. h & riverType;
  63. h & riverDir;
  64. h & roadType;
  65. h & roadDir;
  66. h & extTileFlags;
  67. if(h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  68. {
  69. h & visitableObjects;
  70. h & blockingObjects;
  71. }
  72. else
  73. {
  74. std::vector<std::shared_ptr<CGObjectInstance>> objectPtrs;
  75. h & objectPtrs;
  76. for(const auto & ptr : objectPtrs)
  77. visitableObjects.push_back(ptr->id);
  78. h & objectPtrs;
  79. for(const auto & ptr : objectPtrs)
  80. blockingObjects.push_back(ptr->id);
  81. }
  82. }
  83. };
  84. inline bool TerrainTile::hasFavorableWinds() const
  85. {
  86. return extTileFlags & 128;
  87. }
  88. inline bool TerrainTile::isWater() const
  89. {
  90. return getTerrain()->isWater();
  91. }
  92. inline bool TerrainTile::isLand() const
  93. {
  94. return getTerrain()->isLand();
  95. }
  96. inline bool TerrainTile::visitable() const
  97. {
  98. return !visitableObjects.empty();
  99. }
  100. inline bool TerrainTile::blocked() const
  101. {
  102. return !blockingObjects.empty();
  103. }
  104. inline bool TerrainTile::hasRiver() const
  105. {
  106. return getRiverID() != RiverId::NO_RIVER;
  107. }
  108. inline bool TerrainTile::hasRoad() const
  109. {
  110. return getRoadID() != RoadId::NO_ROAD;
  111. }
  112. inline const TerrainType * TerrainTile::getTerrain() const
  113. {
  114. return terrainType.toEntity(LIBRARY);
  115. }
  116. inline const RiverType * TerrainTile::getRiver() const
  117. {
  118. return riverType.toEntity(LIBRARY);
  119. }
  120. inline const RoadType * TerrainTile::getRoad() const
  121. {
  122. return roadType.toEntity(LIBRARY);
  123. }
  124. inline TerrainId TerrainTile::getTerrainID() const
  125. {
  126. return terrainType;
  127. }
  128. inline RiverId TerrainTile::getRiverID() const
  129. {
  130. return riverType;
  131. }
  132. inline RoadId TerrainTile::getRoadID() const
  133. {
  134. return roadType;
  135. }
  136. inline bool TerrainTile::entrableTerrain() const
  137. {
  138. return entrableTerrain(true, true);
  139. }
  140. inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
  141. {
  142. const TerrainType * terrainFrom = from->getTerrain();
  143. return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
  144. }
  145. inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  146. {
  147. const TerrainType * terrain = getTerrain();
  148. return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
  149. }
  150. VCMI_LIB_NAMESPACE_END