From d4fd7f62d1052fb8633af016e045262fa774adda Mon Sep 17 00:00:00 2001 From: Andreas Schmitt Date: Fri, 18 Jun 2021 04:24:28 +0200 Subject: [PATCH] Build a couple of cycles in the network by connecting each town to the three closest ones in its network --- src/road.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/road.cpp b/src/road.cpp index ec6453d559..e0235dae6e 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -841,8 +841,8 @@ static void PublicRoad_FoundEndNode(AyStar *aystar, OpenListNode *current) } static const int32 BASE_COST_PER_TILE = 1; // Cost for existing road or tunnel/bridge. -static const int32 COST_FOR_NEW_ROAD = 100; // Cost for building a new road. -static const int32 COST_FOR_SLOPE = 50; // Additional cost if the road heads up or down a slope. +static const int32 COST_FOR_NEW_ROAD = 10; // Cost for building a new road. +static const int32 COST_FOR_SLOPE = 5; // Additional cost if the road heads up or down a slope. /** AyStar callback for getting the cost of the current node. */ static int32 PublicRoad_CalculateG(AyStar *, AyStarNode *current, OpenListNode *parent) @@ -880,7 +880,7 @@ static int32 PublicRoad_CalculateG(AyStar *, AyStarNode *current, OpenListNode * if (distance > 1) { // We are planning to build a bridge or tunnel. Make that a bit more expensive. cost += 6 * COST_FOR_SLOPE; - cost += distance * (COST_FOR_NEW_ROAD / 2); + cost += distance * COST_FOR_NEW_ROAD; } } @@ -933,6 +933,30 @@ struct TownNetwork std::vector towns; }; +void PostProcessNetworks(const std::vector>& town_networks) +{ + for (auto network : town_networks) { + std::vector towns(network->towns); + + for (auto town_a : network->towns) { + std::sort(towns.begin(), towns.end(), [&](const TileIndex& a, const TileIndex& b) { return DistanceManhattan(a, town_a) < DistanceManhattan(b, town_a); }); + + const auto second_clostest_town = *(towns.begin() + 2); + const auto third_clostest_town = *(towns.begin() + 3); + + AyStar finder {}; + { + FindPath(finder, town_a, second_clostest_town); + finder.Clear(); + FindPath(finder, town_a, third_clostest_town); + } + finder.Free(); + + IncreaseGeneratingWorldProgress(GWP_PUBLIC_ROADS); + } + } +} + /** * Build the public road network connecting towns using AyStar. */ @@ -956,7 +980,7 @@ void GeneratePublicRoads() return; } - SetGeneratingWorldProgress(GWP_PUBLIC_ROADS, uint(towns.size())); + SetGeneratingWorldProgress(GWP_PUBLIC_ROADS, uint(towns.size() * 2)); // Create a list of networks which also contain a value indicating how many times we failed to connect to them. std::vector> networks; @@ -1106,6 +1130,8 @@ void GeneratePublicRoads() IncreaseGeneratingWorldProgress(GWP_PUBLIC_ROADS); } + + PostProcessNetworks(networks); } /* ========================================================================= */