Przeglądaj źródła

Merge pull request #5550 from vcmi/non_euclidean_roads

Curved roads
DjWarmonger 7 miesięcy temu
rodzic
commit
a3d1159512
3 zmienionych plików z 26 dodań i 2 usunięć
  1. 23 1
      lib/rmg/RmgPath.cpp
  2. 2 0
      lib/rmg/RmgPath.h
  3. 1 1
      lib/rmg/modificators/RoadPlacer.cpp

+ 23 - 1
lib/rmg/RmgPath.cpp

@@ -190,13 +190,35 @@ const Area & Path::getPathArea() const
 	return dPath;
 }
 
+float Path::nonEuclideanCostFunction(const int3& src, const int3& dst, const int3& center)
+{
+	float R = 30.0f; // radius of the zone
+	float W = 10.0f;// width of the transition area
+	float A = 0.7f; // sine bias
+
+    // Euclidean distance:
+    float d = src.dist2d(dst);
+    
+    // Distance from dst to the zone center:
+    float r = dst.dist2d(center);
+    
+    // Compute normalized offset inside the zone:
+    // (R - W) is the inner edge, R is the outer edge.
+    float t = std::clamp((r - (R - W)) / W, 0.0f, 1.0f);
+    
+    // Use sine bias: lowest cost in the middle (t=0.5), higher near edges.
+    float bias = 1.0f + A * std::sin(M_PI * t);
+    
+    return d * bias;
+}
+
 Path::MoveCostFunction Path::createCurvedCostFunction(const Area & border)
 {
 	// Capture by value to ensure the Area object persists
 	return [border = border](const int3& src, const int3& dst) -> float
 	{
+		float ret = nonEuclideanCostFunction(src, dst, border.getCenterOfMass());
 		// Route main roads far from border
-		float ret = dst.dist2d(src);
 		float dist = border.distanceSqr(dst);
 
 		if(dist > 1.0f)

+ 2 - 0
lib/rmg/RmgPath.h

@@ -18,6 +18,7 @@ VCMI_LIB_NAMESPACE_BEGIN
 
 namespace rmg
 {
+
 class Path
 {
 public:
@@ -44,6 +45,7 @@ public:
 	
 	static Path invalid();
 	static MoveCostFunction createCurvedCostFunction(const Area & border);
+	static float nonEuclideanCostFunction(const int3& src, const int3& dst, const int3& center);
 	
 private:
 	

+ 1 - 1
lib/rmg/modificators/RoadPlacer.cpp

@@ -93,7 +93,7 @@ bool RoadPlacer::createRoad(const int3 & destination)
 			}
 		}
 
-		float ret = dst.dist2d(src);
+		float ret = rmg::Path::nonEuclideanCostFunction(src, dst, border.getCenterOfMass());
 
 		if (visitableTiles.contains(src) || visitableTiles.contains(dst))
 		{