浏览代码

Inlined methods commonly used by pathfinder for better optimization

Ivan Savenko 9 月之前
父节点
当前提交
ea368c5176
共有 5 个文件被更改,包括 132 次插入134 次删除
  1. 0 30
      lib/TerrainHandler.cpp
  2. 30 0
      lib/TerrainHandler.h
  3. 1 94
      lib/mapping/CMap.cpp
  4. 22 9
      lib/mapping/CMap.h
  5. 79 1
      lib/mapping/CMapDefines.h

+ 0 - 30
lib/TerrainHandler.cpp

@@ -151,36 +151,6 @@ std::vector<JsonNode> TerrainTypeHandler::loadLegacyData()
 	return result;
 	return result;
 }
 }
 
 
-bool TerrainType::isLand() const
-{
-	return !isWater();
-}
-
-bool TerrainType::isWater() const
-{
-	return passabilityType & PassabilityType::WATER;
-}
-
-bool TerrainType::isRock() const
-{
-	return passabilityType & PassabilityType::ROCK;
-}
-
-bool TerrainType::isPassable() const
-{
-	return !isRock();
-}
-
-bool TerrainType::isSurface() const
-{
-	return passabilityType & PassabilityType::SURFACE;
-}
-
-bool TerrainType::isUnderground() const
-{
-	return passabilityType & PassabilityType::SUBTERRANEAN;
-}
-
 bool TerrainType::isTransitionRequired() const
 bool TerrainType::isTransitionRequired() const
 {
 {
 	return transitionRequired;
 	return transitionRequired;

+ 30 - 0
lib/TerrainHandler.h

@@ -112,4 +112,34 @@ public:
 	std::vector<JsonNode> loadLegacyData() override;
 	std::vector<JsonNode> loadLegacyData() override;
 };
 };
 
 
+inline bool TerrainType::isLand() const
+{
+	return !isWater();
+}
+
+inline bool TerrainType::isWater() const
+{
+	return passabilityType & PassabilityType::WATER;
+}
+
+inline bool TerrainType::isRock() const
+{
+	return passabilityType & PassabilityType::ROCK;
+}
+
+inline bool TerrainType::isPassable() const
+{
+	return !isRock();
+}
+
+inline bool TerrainType::isSurface() const
+{
+	return passabilityType & PassabilityType::SURFACE;
+}
+
+inline bool TerrainType::isUnderground() const
+{
+	return passabilityType & PassabilityType::SUBTERRANEAN;
+}
+
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END

+ 1 - 94
lib/mapping/CMap.cpp

@@ -143,17 +143,6 @@ TerrainTile::TerrainTile():
 {
 {
 }
 }
 
 
-bool TerrainTile::entrableTerrain(const TerrainTile * from) const
-{
-	return entrableTerrain(from ? from->isLand() : true, from ? from->isWater() : true);
-}
-
-bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
-{
-	return getTerrain()->isPassable()
-			&& ((allowSea && isWater())  ||  (allowLand && isLand()));
-}
-
 bool TerrainTile::isClear(const TerrainTile * from) const
 bool TerrainTile::isClear(const TerrainTile * from) const
 {
 {
 	return entrableTerrain(from) && !blocked();
 	return entrableTerrain(from) && !blocked();
@@ -187,72 +176,6 @@ EDiggingStatus TerrainTile::getDiggingStatus(const bool excludeTop) const
 		return EDiggingStatus::CAN_DIG;
 		return EDiggingStatus::CAN_DIG;
 }
 }
 
 
-bool TerrainTile::hasFavorableWinds() const
-{
-	return extTileFlags & 128;
-}
-
-bool TerrainTile::isWater() const
-{
-	return getTerrain()->isWater();
-}
-
-bool TerrainTile::isLand() const
-{
-	return getTerrain()->isLand();
-}
-
-bool TerrainTile::visitable() const
-{
-	return !visitableObjects.empty();
-}
-
-bool TerrainTile::blocked() const
-{
-	return !blockingObjects.empty();
-}
-
-bool TerrainTile::hasRiver() const
-{
-	return getRiverID() != RiverId::NO_RIVER;
-}
-
-bool TerrainTile::hasRoad() const
-{
-	return getRoadID() != RoadId::NO_ROAD;
-}
-
-const TerrainType * TerrainTile::getTerrain() const
-{
-	return terrainType.toEntity(VLC);
-}
-
-const RiverType * TerrainTile::getRiver() const
-{
-	return riverType.toEntity(VLC);
-}
-
-const RoadType * TerrainTile::getRoad() const
-{
-	return roadType.toEntity(VLC);
-}
-
-TerrainId TerrainTile::getTerrainID() const
-{
-	return terrainType;
-}
-
-RiverId TerrainTile::getRiverID() const
-{
-	return riverType;
-}
-
-RoadId TerrainTile::getRoadID() const
-{
-	return roadType;
-}
-
-
 CMap::CMap(IGameCallback * cb)
 CMap::CMap(IGameCallback * cb)
 	: GameCallbackHolder(cb)
 	: GameCallbackHolder(cb)
 	, checksum(0)
 	, checksum(0)
@@ -365,7 +288,7 @@ bool CMap::isCoastalTile(const int3 & pos) const
 		return false;
 		return false;
 	}
 	}
 
 
-	if(isWaterTile(pos))
+	if(getTile(pos).isWater())
 		return false;
 		return false;
 
 
 	for(const auto & dir : dirs)
 	for(const auto & dir : dirs)
@@ -382,22 +305,6 @@ bool CMap::isCoastalTile(const int3 & pos) const
 	return false;
 	return false;
 }
 }
 
 
-TerrainTile & CMap::getTile(const int3 & tile)
-{
-	assert(isInTheMap(tile));
-	return terrain[tile.z][tile.x][tile.y];
-}
-
-const TerrainTile & CMap::getTile(const int3 & tile) const
-{
-	assert(isInTheMap(tile));
-	return terrain[tile.z][tile.x][tile.y];
-}
-
-bool CMap::isWaterTile(const int3 &pos) const
-{
-	return isInTheMap(pos) && getTile(pos).isWater();
-}
 bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
 bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
 {
 {
 	const TerrainTile * dstTile = &getTile(dst);
 	const TerrainTile * dstTile = &getTile(dst);

+ 22 - 9
lib/mapping/CMap.h

@@ -89,15 +89,7 @@ public:
 	TerrainTile & getTile(const int3 & tile);
 	TerrainTile & getTile(const int3 & tile);
 	const TerrainTile & getTile(const int3 & tile) const;
 	const TerrainTile & getTile(const int3 & tile) const;
 	bool isCoastalTile(const int3 & pos) const;
 	bool isCoastalTile(const int3 & pos) const;
-	bool isWaterTile(const int3 & pos) const;
-	inline bool isInTheMap(const int3 & pos) const
-	{
-		// Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
-		return
-			static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
-			static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
-			static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
-	}
+	bool isInTheMap(const int3 & pos) const;
 
 
 	bool canMoveBetween(const int3 &src, const int3 &dst) const;
 	bool canMoveBetween(const int3 &src, const int3 &dst) const;
 	bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
 	bool checkForVisitableDir(const int3 & src, const TerrainTile * pom, const int3 & dst) const;
@@ -250,4 +242,25 @@ public:
 	}
 	}
 };
 };
 
 
+inline bool CMap::isInTheMap(const int3 & pos) const
+{
+	// Check whether coord < 0 is done implicitly. Negative signed int overflows to unsigned number larger than all signed ints.
+	return
+		static_cast<uint32_t>(pos.x) < static_cast<uint32_t>(width) &&
+		static_cast<uint32_t>(pos.y) < static_cast<uint32_t>(height) &&
+		static_cast<uint32_t>(pos.z) <= (twoLevel ? 1 : 0);
+}
+
+inline TerrainTile & CMap::getTile(const int3 & tile)
+{
+	assert(isInTheMap(tile));
+	return terrain[tile.z][tile.x][tile.y];
+}
+
+inline const TerrainTile & CMap::getTile(const int3 & tile) const
+{
+	assert(isInTheMap(tile));
+	return terrain[tile.z][tile.x][tile.y];
+}
+
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END

+ 79 - 1
lib/mapping/CMapDefines.h

@@ -12,7 +12,8 @@
 
 
 #include "../ResourceSet.h"
 #include "../ResourceSet.h"
 #include "../texts/MetaString.h"
 #include "../texts/MetaString.h"
-#include "../int3.h"
+#include "../VCMI_Lib.h"
+#include "../TerrainHandler.h"
 
 
 VCMI_LIB_NAMESPACE_BEGIN
 VCMI_LIB_NAMESPACE_BEGIN
 
 
@@ -193,4 +194,81 @@ struct DLL_LINKAGE TerrainTile
 	}
 	}
 };
 };
 
 
+inline bool TerrainTile::hasFavorableWinds() const
+{
+	return extTileFlags & 128;
+}
+
+inline bool TerrainTile::isWater() const
+{
+	return getTerrain()->isWater();
+}
+
+inline bool TerrainTile::isLand() const
+{
+	return getTerrain()->isLand();
+}
+
+inline bool TerrainTile::visitable() const
+{
+	return !visitableObjects.empty();
+}
+
+inline bool TerrainTile::blocked() const
+{
+	return !blockingObjects.empty();
+}
+
+inline bool TerrainTile::hasRiver() const
+{
+	return getRiverID() != RiverId::NO_RIVER;
+}
+
+inline bool TerrainTile::hasRoad() const
+{
+	return getRoadID() != RoadId::NO_ROAD;
+}
+
+inline const TerrainType * TerrainTile::getTerrain() const
+{
+	return terrainType.toEntity(VLC);
+}
+
+inline const RiverType * TerrainTile::getRiver() const
+{
+	return riverType.toEntity(VLC);
+}
+
+inline const RoadType * TerrainTile::getRoad() const
+{
+	return roadType.toEntity(VLC);
+}
+
+inline TerrainId TerrainTile::getTerrainID() const
+{
+	return terrainType;
+}
+
+inline RiverId TerrainTile::getRiverID() const
+{
+	return riverType;
+}
+
+inline RoadId TerrainTile::getRoadID() const
+{
+	return roadType;
+}
+
+inline bool TerrainTile::entrableTerrain(const TerrainTile * from) const
+{
+	const TerrainType * terrainFrom = from->getTerrain();
+	return entrableTerrain(terrainFrom->isLand(), terrainFrom->isWater());
+}
+
+inline bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
+{
+	const TerrainType * terrain = getTerrain();
+	return terrain->isPassable() && ((allowSea && terrain->isWater()) || (allowLand && terrain->isLand()));
+}
+
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END