Browse Source

Satisfy SonarCloud

Tomasz Zieliński 1 year ago
parent
commit
5a979e42e2
2 changed files with 37 additions and 32 deletions
  1. 34 32
      lib/rmg/modificators/RoadPlacer.cpp
  2. 3 0
      lib/rmg/modificators/RoadPlacer.h

+ 34 - 32
lib/rmg/modificators/RoadPlacer.cpp

@@ -73,9 +73,7 @@ bool RoadPlacer::createRoad(const int3 & destination)
 	rmg::Path path(searchArea);
 	path.connect(roads);
 
-	const float VISITABLE_PENALTY = 1.33f;
-
-	auto simpleRoutig = [this, &border, &VISITABLE_PENALTY](const int3& src, const int3& dst)
+	auto simpleRoutig = [this, &border](const int3& src, const int3& dst)
 	{
 		if(areaIsolated().contains(dst))
 		{
@@ -101,44 +99,48 @@ bool RoadPlacer::createRoad(const int3 & destination)
 	auto res = path.search(destination, true, simpleRoutig);
 	if(!res.valid())
 	{
-		auto desperateRoutig = [this, &VISITABLE_PENALTY](const int3& src, const int3& dst) -> float
+		res = createRoadDesperate(path, destination);
+		if (!res.valid())
 		{
-			//Do not allow connections straight up through object not visitable from top
-			if(std::abs((src - dst).y) == 1)
+			logGlobal->warn("Failed to create road to node %s", destination.toString());
+			return false;
+		}
+	}
+	roads.unite(res.getPathArea());
+	return true;
+	
+}
+
+rmg::Path RoadPlacer::createRoadDesperate(rmg::Path & path, const int3 & destination)
+{
+	auto desperateRoutig = [this](const int3& src, const int3& dst) -> float
+	{
+		//Do not allow connections straight up through object not visitable from top
+		if(std::abs((src - dst).y) == 1)
+		{
+			if(areaIsolated().contains(dst) || areaIsolated().contains(src))
 			{
-				if(areaIsolated().contains(dst) || areaIsolated().contains(src))
-				{
-					return 1e12;
-				}
+				return 1e12;
 			}
-			else
+		}
+		else
+		{
+			if(areaIsolated().contains(dst))
 			{
-				if(areaIsolated().contains(dst))
-				{
-					return 1e6;
-				}
+				return 1e6;
 			}
+		}
 
-			float weight = dst.dist2dSQ(src);
-			auto ret =  weight * weight; // Still prefer straight paths
-
-			if (visitableTiles.contains(src) || visitableTiles.contains(dst))
-			{
-				ret *= VISITABLE_PENALTY;
-			}
-			return ret;
-		};
-		res = path.search(destination, false, desperateRoutig);
+		float weight = dst.dist2dSQ(src);
+		auto ret =  weight * weight; // Still prefer straight paths
 
-		if(!res.valid())
+		if (visitableTiles.contains(src) || visitableTiles.contains(dst))
 		{
-			logGlobal->warn("Failed to create road to node %s", destination.toString());
-			return false;
+			ret *= VISITABLE_PENALTY;
 		}
-	}
-	roads.unite(res.getPathArea());
-	return true;
-	
+		return ret;
+	};
+	return path.search(destination, false, desperateRoutig);
 }
 
 void RoadPlacer::drawRoads(bool secondary)

+ 3 - 0
lib/rmg/modificators/RoadPlacer.h

@@ -13,6 +13,8 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
+const float VISITABLE_PENALTY = 1.33f;
+
 class RoadPlacer: public Modificator
 {
 public:
@@ -34,6 +36,7 @@ public:
 	
 protected:
 	bool createRoad(const int3 & dst);
+	rmg::Path createRoadDesperate(rmg::Path & path, const int3 & destination);
 	void drawRoads(bool secondary = false); //actually updates tiles
 
 protected: