From 171141123a4d0e0f1a7269df5dead65212d7e4a7 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Thu, 5 Nov 2026 21:58:38 +0000 Subject: [PATCH 1/9] Fill large free land areas of random maps with mountains Especially on large maps, random maps contain big empty areas. Repeatedly start a new mountain at the node furthest away from water and mountains until no node exceeds the free land to keep. Mountains shrink with each iteration and keep a margin to obstacles, so passages stay open. The free land to keep is based on the mountain distance setting, so it remains possible to place headquarters at the desired distance. Mountains are raised relative to the surrounding land and textured directly to avoid cliffs. Textured nodes are treated as obstacles which leaves islands (and player start positions on water maps) untouched. See #1530 Co-Authored-By: Claude Opus 5.5 --- libs/s25main/mapGenerator/Mountains.cpp | 154 ++++++++++ libs/s25main/mapGenerator/Mountains.h | 99 ++++++ libs/s25main/mapGenerator/RandomMap.cpp | 33 ++ libs/s25main/mapGenerator/RandomMap.h | 2 + tests/s25Main/mapGenerator/testMountains.cpp | 304 +++++++++++++++++++ tests/s25Main/mapGenerator/testRandomMap.cpp | 60 ++++ 6 files changed, 652 insertions(+) create mode 100644 libs/s25main/mapGenerator/Mountains.cpp create mode 100644 libs/s25main/mapGenerator/Mountains.h create mode 100644 tests/s25Main/mapGenerator/testMountains.cpp diff --git a/libs/s25main/mapGenerator/Mountains.cpp b/libs/s25main/mapGenerator/Mountains.cpp new file mode 100644 index 0000000000..a36ee3113c --- /dev/null +++ b/libs/s25main/mapGenerator/Mountains.cpp @@ -0,0 +1,154 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "mapGenerator/Mountains.h" +#include "helpers/containerUtils.h" +#include "mapGenerator/Algorithms.h" +#include "mapGenerator/TextureHelper.h" +#include +#include +#include +#include + +namespace rttr::mapGenerator { + +namespace { + /// Size of each mountain relative to the previous one + constexpr double sizeDecay = 0.9; + /// Radius of the brush collecting nodes around a node of a growing mountain + constexpr unsigned brushRadius = 2; + /// Chance of the brush to collect a node - values below 100 create irregular shapes + constexpr unsigned brushDensity = 75; + + /// Updates the distances after the specified nodes stopped being free land + void MarkAsOccupied(NodeMapBase& freeLand, const Mountain& mountain) + { + std::queue queue; + for(const MapPoint& pt : mountain) + { + freeLand[pt] = 0; + queue.push(pt); + } + + // UpdateDistances only fills unset values so it can't be used to lower existing distances + while(!queue.empty()) + { + const MapPoint current = queue.front(); + queue.pop(); + + const unsigned distance = freeLand[current] + 1; + for(const MapPoint& neighbor : freeLand.GetNeighbours(current)) + { + if(distance < freeLand[neighbor]) + { + freeLand[neighbor] = distance; + queue.push(neighbor); + } + } + } + } +} // namespace + +unsigned GetMountainSize(const MountainSettings& settings, unsigned index) +{ + const auto size = static_cast(settings.maxSize * std::pow(sizeDecay, index)); + return std::max(size, settings.minSize); +} + +NodeMapBase GetFreeLandDistances(const Map& map, unsigned mountainLevel) +{ + const auto& textures = map.getTextures(); + return DistancesTo(map.size, [&map, &textures, mountainLevel](const MapPoint& pt) { + return map.z[pt] == map.height.minimum || map.z[pt] >= mountainLevel || textures[pt].rsu || textures[pt].lsd; + }); +} + +Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, + unsigned margin) +{ + const auto priority = [&freeLand, ¢er](const MapPoint& pt) { + return static_cast(freeLand[pt]) - freeLand.CalcDistance(center, pt); + }; + const auto compare = [&priority](const MapPoint& lhs, const MapPoint& rhs) { + return priority(lhs) < priority(rhs); + }; + + std::priority_queue, decltype(compare)> queue(compare); + Mountain mountain{center}; + queue.push(center); + + while(!queue.empty() && mountain.size() < size) + { + const MapPoint current = queue.top(); + queue.pop(); + + for(const MapPoint& pt : freeLand.GetPointsInRadius(current, brushRadius)) + { + if(mountain.size() < size && freeLand[pt] > margin && rnd.ByChance(brushDensity) + && mountain.insert(pt).second) + { + queue.push(pt); + } + } + } + + return mountain; +} + +std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings) +{ + std::vector mountains; + auto freeLand = GetFreeLandDistances(map, mountainLevel); + const auto maxDistance = [&freeLand] { return *std::max_element(freeLand.begin(), freeLand.end()); }; + + for(unsigned distance = maxDistance(); distance > settings.minFreeLand; distance = maxDistance()) + { + const auto isFreest = [&freeLand, distance](const MapPoint& pt) { return freeLand[pt] == distance; }; + const MapPoint center = rnd.RandomItem(SelectPoints(isFreest, map.size)); + + // A larger margin than required varies the gaps. It must stay below the distance to include the center. + const unsigned maxMargin = std::min(settings.minFreeLand + settings.minFreeLand / 2, distance - 1); + const unsigned margin = rnd.RandomValue(settings.minFreeLand, maxMargin); + const unsigned size = GetMountainSize(settings, static_cast(mountains.size())); + + mountains.push_back(GrowMountain(freeLand, rnd, center, size, margin)); + MarkAsOccupied(freeLand, mountains.back()); + } + + return mountains; +} + +void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) +{ + const auto isFoot = [&map, &mountain](const MapPoint& pt) { + return helpers::contains_if(map.z.GetNeighbours(pt), + [&mountain](const MapPoint& pt) { return !helpers::contains(mountain, pt); }); + }; + const auto footDistance = Distances(map.size, mountain, 0, isFoot); + const ValueRange range(0u, GetMaximum(footDistance, mountain)); + const auto textures = map.textureMap.FindAll(IsMinableMountain); + + for(const MapPoint& pt : mountain) + { + const unsigned elevation = rnd.RandomValue(footDistance[pt], footDistance[pt] + 1u); + map.z[pt] = static_cast(std::min(map.z[pt] + elevation, map.height.maximum)); + map.textureMap.Set(pt, textures[MapValueToIndex(footDistance[pt], range, textures.size())]); + } +} + +std::vector CreateMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings) +{ + auto mountains = FindMountains(map, rnd, mountainLevel, settings); + + for(const Mountain& mountain : mountains) + { + RaiseMountain(map, rnd, mountain); + } + + return mountains; +} + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/Mountains.h b/libs/s25main/mapGenerator/Mountains.h new file mode 100644 index 0000000000..4a1c9c869e --- /dev/null +++ b/libs/s25main/mapGenerator/Mountains.h @@ -0,0 +1,99 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "mapGenerator/Map.h" +#include "mapGenerator/RandomUtility.h" +#include +#include + +namespace rttr::mapGenerator { + +using Mountain = std::set; + +struct MountainSettings +{ + /// Land up to this distance to water or mountains stays free. Larger areas are filled up with mountains. + unsigned minFreeLand; + /// Number of nodes of the first mountain. Each following mountain is smaller than the previous one. + unsigned maxSize; + /// Lower limit for the number of nodes of a mountain (unless there isn't sufficient free land). + unsigned minSize; +}; + +/** + * Computes the desired number of nodes of a mountain. + * + * @param settings size limits for mountains + * @param index index of the mountain - the first mountain has index 0 + * + * @returns the desired size which decreases with the index but never drops below the minimum size. + */ +unsigned GetMountainSize(const MountainSettings& settings, unsigned index); + +/** + * Computes for each node the distance to the closest node which is not free land. Nodes are not considered free land + * if they are water, reach the mountain level or already have a texture (e.g. rivers and islands). + * + * @param map map to compute the distances for + * @param mountainLevel minimum z-value of mountains + * + * @returns the distance of each node to the closest node which isn't free land. + */ +NodeMapBase GetFreeLandDistances(const Map& map, unsigned mountainLevel); + +/** + * Grows a mountain around the specified center. Nodes close to the center and far away from other obstacles are + * preferred. Skipping nodes by chance creates an irregular shape. + * + * @param freeLand distances to the closest node which isn't free land + * @param rnd random number generator + * @param center center of the mountain which is always part of the mountain + * @param size maximum number of nodes of the mountain + * @param margin only nodes with a distance greater than this value become part of the mountain + * + * @returns all nodes of the new mountain. + */ +Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, + unsigned margin); + +/** + * Selects the nodes of new mountains until no node is further away from water or mountains than the minimum of free + * land to keep. Every mountain starts at the node with the largest distance to water and mountains. As nodes close to + * obstacles are never selected, passages between areas of the map stay open. + * + * @param map map to search for free land (is not modified) + * @param rnd random number generator + * @param mountainLevel minimum z-value of mountains + * @param settings settings of the new mountains + * + * @returns the nodes of each new mountain. All mountains are disjoint. + */ +std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings); + +/** + * Elevates the nodes of the mountain relative to the surrounding land and applies mountain textures to them. + * + * @param map reference to the map to place the mountain on (manipulates textures and z-values of the map) + * @param rnd random number generator + * @param mountain nodes of the mountain + */ +void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain); + +/** + * Reduces the size of free land by adding new mountains to the map. + * + * @param map reference to the map to place the mountains on (manipulates textures and z-values of the map) + * @param rnd random number generator + * @param mountainLevel minimum z-value of mountains + * @param settings settings of the new mountains + * + * @returns the nodes of each new mountain. + */ +std::vector CreateMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings); + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/RandomMap.cpp b/libs/s25main/mapGenerator/RandomMap.cpp index 50535154b7..a22de0c3fc 100644 --- a/libs/s25main/mapGenerator/RandomMap.cpp +++ b/libs/s25main/mapGenerator/RandomMap.cpp @@ -6,6 +6,7 @@ #include "mapGenerator/Harbors.h" #include "mapGenerator/HeadQuarters.h" #include "mapGenerator/Islands.h" +#include "mapGenerator/Mountains.h" #include "mapGenerator/Resources.h" #include "mapGenerator/Terrain.h" #include "mapGenerator/TextureHelper.h" @@ -78,6 +79,21 @@ unsigned GetIslandSize(const MapExtent& size) return 1200; } +unsigned GetMaxMountainSize(const MapExtent& size) +{ + const unsigned combinedSize = size.x * size.y; + if(combinedSize <= 64 * 64) + return 150; + else if(combinedSize <= 128 * 128) + return 300; + else if(combinedSize <= 256 * 256) + return 600; + else if(combinedSize <= 512 * 512) + return 1000; + else + return 1500; +} + unsigned GetSmoothRadius(const MapExtent& size) { const unsigned combinedSize = size.x * size.y; @@ -179,6 +195,17 @@ void RandomMap::CreateFreeIslands(unsigned waterNodes) } } +void RandomMap::AddMountains(unsigned mountainLevel) +{ + // The distance players want to keep to mountains must remain available for their headquarters + const auto mountainDistance = static_cast(settings_.mountainDistance); + const auto minFreeLand = rnd_.RandomValue(mountainDistance, mountainDistance + 5u); + const auto maxMountainSize = GetMaxMountainSize(map_.size); + const auto maxSize = rnd_.RandomValue(maxMountainSize / 2, maxMountainSize); + + CreateMountains(map_, rnd_, mountainLevel, MountainSettings{minFreeLand, maxSize, maxMountainSize / 4}); +} + void RandomMap::CreateMixedMap() { const auto center = rnd_.Point(map_.size); @@ -203,6 +230,8 @@ void RandomMap::CreateMixedMap() CreateFreeIslands(waterNodes); + AddMountains(mountainLevel); + texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); PlaceHarbors(map_, rivers); @@ -247,6 +276,8 @@ void RandomMap::CreateWaterMap() const auto rivers = CreateRivers(center); + AddMountains(mountainLevel); + texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); PlaceHarbors(map_, rivers); @@ -271,6 +302,8 @@ void RandomMap::CreateLandMap() const auto mountainLevel = LimitFor(map_.z, land, static_cast(1)) + 1; CreateRivers(); + AddMountains(mountainLevel); + texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); PlaceHeadquarters(map_, rnd_, map_.players, settings_.mountainDistance); diff --git a/libs/s25main/mapGenerator/RandomMap.h b/libs/s25main/mapGenerator/RandomMap.h index 97d7835dff..f5ccf7b49b 100644 --- a/libs/s25main/mapGenerator/RandomMap.h +++ b/libs/s25main/mapGenerator/RandomMap.h @@ -17,6 +17,7 @@ unsigned GetMaximumHeight(const MapExtent& size); unsigned GetCoastline(const MapExtent& size); unsigned GetIslandRadius(const MapExtent& size); unsigned GetIslandSize(const MapExtent& size); +unsigned GetMaxMountainSize(const MapExtent& size); unsigned GetSmoothRadius(const MapExtent& size); unsigned GetSmoothIterations(const MapExtent& size); @@ -32,6 +33,7 @@ class RandomMap std::vector CreateRivers(MapPoint source = MapPoint::Invalid()); void CreateFreeIslands(unsigned waterNodes); + void AddMountains(unsigned mountainLevel); void CreateMixedMap(); void CreateLandMap(); void CreateWaterMap(); diff --git a/tests/s25Main/mapGenerator/testMountains.cpp b/tests/s25Main/mapGenerator/testMountains.cpp new file mode 100644 index 0000000000..f5191af433 --- /dev/null +++ b/tests/s25Main/mapGenerator/testMountains.cpp @@ -0,0 +1,304 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "PointOutput.h" +#include "mapGenFixtures.h" +#include "mapGenerator/Algorithms.h" +#include "mapGenerator/Mountains.h" +#include "mapGenerator/TextureHelper.h" +#include +#include + +using namespace rttr::mapGenerator; + +namespace { +constexpr uint8_t landHeight = 10; +constexpr unsigned mountainLevel = 30; +constexpr MountainSettings settings{3, 100, 25}; + +struct MountainFixture : MapGenFixture +{ + /// Creates a map covered by land except for a single row of water + Map createLandMap(const MapExtent size) const + { + Map map = createMap(size); + map.z.Resize(size, landHeight); + RTTR_FOREACH_PT(MapPoint, size) + { + if(pt.y == 0) + map.z[pt] = map.height.minimum; + } + return map; + } +}; + +unsigned maxDistance(const NodeMapBase& distances) +{ + return *std::max_element(distances.begin(), distances.end()); +} +} // namespace + +BOOST_FIXTURE_TEST_SUITE(MountainsTests, MountainFixture) + +BOOST_AUTO_TEST_CASE(GetMountainSize_starts_with_maximum_size) +{ + BOOST_TEST(GetMountainSize(settings, 0) == settings.maxSize); +} + +BOOST_AUTO_TEST_CASE(GetMountainSize_decreases_with_index) +{ + BOOST_TEST(GetMountainSize(settings, 1) < GetMountainSize(settings, 0)); + BOOST_TEST(GetMountainSize(settings, 2) < GetMountainSize(settings, 1)); +} + +BOOST_AUTO_TEST_CASE(GetMountainSize_never_drops_below_minimum_size) +{ + BOOST_TEST(GetMountainSize(settings, 1000) == settings.minSize); +} + +BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_water) +{ + const Map map = createLandMap(MapExtent(16, 16)); + const auto distances = GetFreeLandDistances(map, mountainLevel); + + BOOST_TEST(distances[MapPoint(5, 0)] == 0u); + BOOST_TEST(distances[MapPoint(5, 1)] == 1u); + BOOST_TEST(distances[MapPoint(5, 8)] == 8u); +} + +BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_nodes_at_mountain_level) +{ + Map map = createLandMap(MapExtent(16, 16)); + const MapPoint mountain(8, 8); + map.z[mountain] = mountainLevel; + + const auto distances = GetFreeLandDistances(map, mountainLevel); + + BOOST_TEST(distances[mountain] == 0u); + BOOST_TEST(distances[MapPoint(9, 8)] == 1u); +} + +BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_textured_nodes) +{ + Map map = createLandMap(MapExtent(16, 16)); + const MapPoint river(8, 8); + map.textureMap.Set(river, map.textureMap.Find(IsWater)); + + const auto distances = GetFreeLandDistances(map, mountainLevel); + + BOOST_TEST(distances[river] == 0u); +} + +BOOST_AUTO_TEST_CASE(GrowMountain_contains_center_even_if_size_is_one) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const auto distances = GetFreeLandDistances(map, mountainLevel); + const MapPoint center(16, 16); + RandomUtility rnd(0); + + const auto mountain = GrowMountain(distances, rnd, center, 1, 3); + + BOOST_TEST(mountain.size() == 1u); + BOOST_TEST(mountain.count(center) == 1u); +} + +BOOST_AUTO_TEST_CASE(GrowMountain_reaches_requested_size_on_sufficient_land) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const auto distances = GetFreeLandDistances(map, mountainLevel); + RandomUtility rnd(0); + + const auto mountain = GrowMountain(distances, rnd, MapPoint(16, 16), 50, 3); + + BOOST_TEST(mountain.size() == 50u); +} + +BOOST_AUTO_TEST_CASE(GrowMountain_keeps_margin_to_obstacles) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const auto distances = GetFreeLandDistances(map, mountainLevel); + const unsigned margin = 6; + RandomUtility rnd(0); + + const auto mountain = GrowMountain(distances, rnd, MapPoint(16, 16), 1000, margin); + + BOOST_TEST_REQUIRE(mountain.size() > 1u); + for(const MapPoint& pt : mountain) + { + BOOST_TEST_REQUIRE(distances[pt] > margin, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(FindMountains_does_not_change_map) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const auto heightsBefore = map.z; + RandomUtility rnd(0); + + const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + + BOOST_TEST_REQUIRE(!mountains.empty()); + RTTR_FOREACH_PT(MapPoint, map.size) + { + BOOST_TEST_REQUIRE(map.z[pt] == heightsBefore[pt], "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(FindMountains_finds_nothing_without_enough_free_land) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const auto freeLand = maxDistance(GetFreeLandDistances(map, mountainLevel)); + RandomUtility rnd(0); + + const MountainSettings limit{freeLand, settings.maxSize, settings.minSize}; + + BOOST_TEST(FindMountains(map, rnd, mountainLevel, limit).empty()); +} + +BOOST_AUTO_TEST_CASE(FindMountains_finds_nothing_on_water_only) +{ + Map map = createMap(MapExtent(16, 16)); + map.z.Resize(map.size, map.height.minimum); + RandomUtility rnd(0); + + BOOST_TEST(FindMountains(map, rnd, mountainLevel, settings).empty()); +} + +BOOST_AUTO_TEST_CASE(FindMountains_covers_map_without_any_obstacles) +{ + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, landHeight); + RandomUtility rnd(0); + + BOOST_TEST(!FindMountains(map, rnd, mountainLevel, settings).empty()); +} + +BOOST_AUTO_TEST_CASE(FindMountains_returns_disjoint_mountains) +{ + const Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + + BOOST_TEST_REQUIRE(mountains.size() > 1u); + Mountain visited; + for(const Mountain& mountain : mountains) + { + for(const MapPoint& pt : mountain) + { + BOOST_TEST_REQUIRE(visited.insert(pt).second, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(FindMountains_respects_size_of_each_mountain) +{ + const Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + + BOOST_TEST_REQUIRE(mountains.size() > 1u); + for(unsigned index = 0; index < mountains.size(); index++) + { + BOOST_TEST_REQUIRE(mountains[index].size() <= GetMountainSize(settings, index), "Mountain " << index); + } +} + +BOOST_AUTO_TEST_CASE(FindMountains_keeps_free_land_around_obstacles) +{ + const Map map = createLandMap(MapExtent(64, 64)); + const auto distances = GetFreeLandDistances(map, mountainLevel); + RandomUtility rnd(0); + + for(const Mountain& mountain : FindMountains(map, rnd, mountainLevel, settings)) + { + for(const MapPoint& pt : mountain) + { + BOOST_TEST_REQUIRE(distances[pt] > settings.minFreeLand, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(RaiseMountain_elevates_center_above_foot_and_keeps_surroundings) +{ + Map map = createLandMap(MapExtent(32, 32)); + const MapPoint center(16, 16); + const auto points = map.z.GetPointsInRadiusWithCenter(center, 4); + const Mountain mountain(points.begin(), points.end()); + const MapPoint outside(16, 26); + RandomUtility rnd(0); + + RaiseMountain(map, rnd, mountain); + + BOOST_TEST(map.z[center] > landHeight + 1); + BOOST_TEST(map.z[outside] == landHeight); + for(const MapPoint& pt : mountain) + { + BOOST_TEST_REQUIRE(map.z[pt] >= landHeight, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(RaiseMountain_applies_minable_textures_to_all_nodes) +{ + Map map = createLandMap(MapExtent(32, 32)); + const auto points = map.z.GetPointsInRadiusWithCenter(MapPoint(16, 16), 4); + const Mountain mountain(points.begin(), points.end()); + const auto minable = map.textureMap.FindAll(IsMinableMountain); + RandomUtility rnd(0); + + RaiseMountain(map, rnd, mountain); + + for(const MapPoint& pt : mountain) + { + const auto& textures = map.getTextures()[pt]; + BOOST_TEST_REQUIRE(helpers::contains(minable, textures.rsu), "At point " << pt); + BOOST_TEST_REQUIRE(helpers::contains(minable, textures.lsd), "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(RaiseMountain_does_not_exceed_maximum_height) +{ + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, map.height.maximum - 1); + const auto points = map.z.GetPointsInRadiusWithCenter(MapPoint(16, 16), 6); + RandomUtility rnd(0); + + RaiseMountain(map, rnd, Mountain(points.begin(), points.end())); + + RTTR_FOREACH_PT(MapPoint, map.size) + { + BOOST_TEST_REQUIRE(map.z[pt] <= map.height.maximum, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(CreateMountains_leaves_no_free_land_beyond_limit) +{ + Map map = createLandMap(MapExtent(64, 64)); + BOOST_TEST_REQUIRE(maxDistance(GetFreeLandDistances(map, mountainLevel)) > settings.minFreeLand); + RandomUtility rnd(0); + + const auto mountains = CreateMountains(map, rnd, mountainLevel, settings); + + BOOST_TEST_REQUIRE(!mountains.empty()); + BOOST_TEST(maxDistance(GetFreeLandDistances(map, mountainLevel)) <= settings.minFreeLand); +} + +BOOST_AUTO_TEST_CASE(FindMountains_is_deterministic_for_same_seed) +{ + const Map map = createLandMap(MapExtent(48, 48)); + RandomUtility rnd1(42); + RandomUtility rnd2(42); + + const auto mountains1 = FindMountains(map, rnd1, mountainLevel, settings); + const auto mountains2 = FindMountains(map, rnd2, mountainLevel, settings); + + BOOST_TEST_REQUIRE(mountains1.size() == mountains2.size()); + for(unsigned index = 0; index < mountains1.size(); index++) + { + BOOST_TEST_REQUIRE((mountains1[index] == mountains2[index]), "Mountain " << index); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index 7f6aa1083a..cb539050be 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -5,7 +5,9 @@ #include "PointOutput.h" #include "commonDefines.h" #include "lua/GameDataLoader.h" +#include "mapGenerator/Algorithms.h" #include "mapGenerator/RandomMap.h" +#include "mapGenerator/TextureHelper.h" #include "gameData/MaxPlayers.h" #include "libsiedler2/ArchivItem_Map.h" #include "libsiedler2/ArchivItem_Map_Header.h" @@ -144,6 +146,64 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_map_with_max_players) ValidateMap(map, settings.size, settings.numPlayers); } +BOOST_AUTO_TEST_CASE(GetMaxMountainSize_grows_with_map_size) +{ + BOOST_TEST(GetMaxMountainSize(MapExtent(64, 64)) < GetMaxMountainSize(MapExtent(128, 128))); + BOOST_TEST(GetMaxMountainSize(MapExtent(128, 128)) < GetMaxMountainSize(MapExtent(256, 256))); + BOOST_TEST(GetMaxMountainSize(MapExtent(256, 256)) < GetMaxMountainSize(MapExtent(512, 512))); + BOOST_TEST(GetMaxMountainSize(MapExtent(512, 512)) < GetMaxMountainSize(MapExtent(1024, 1024))); +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + // Smaller maps do not contain enough free land to require additional mountains + settings.size = MapExtent(256, 256); + settings.mountainDistance = MountainDistance::Close; + + // Maximum of the randomized free land to keep plus the transition textures added at the foot of mountains + const unsigned maxFreeLand = static_cast(settings.mountainDistance) + 5 + 3; + + for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed}) + { + settings.style = style; + RandomUtility rnd(0); + + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + const auto distances = DistancesTo(map.size, [&map](const MapPoint& pt) { + return map.textureMap.Any(pt, [](const TerrainDesc& t) { return IsWater(t) || IsMountainOrSnowOrLava(t); }); + }); + + BOOST_TEST(*std::max_element(distances.begin(), distances.end()) <= maxFreeLand); + } +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_maps_for_all_styles_and_mountain_distances) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(96, 96); + settings.numPlayers = 4; + + for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed, MapStyle::Water}) + { + for(const MountainDistance distance : {MountainDistance::Close, MountainDistance::VeryFar}) + { + settings.style = style; + settings.mountainDistance = distance; + RandomUtility rnd(1); + + Map map = GenerateRandomMap(rnd, worldDesc, settings); + ValidateMap(map, settings.size, settings.numPlayers); + } + } +} + BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_mapfile_with_max_players) { rttr::test::TmpFolder tmpFolder; From a2a7244a46bc85f9fc732d1dd20d48d13e1aa2ad Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Thu, 5 Nov 2026 22:21:55 +0000 Subject: [PATCH 2/9] Add setting for the amount of mountains to the map generator The new slider controls the minimum free land to keep. The default of 50% keeps the desired HQ distance to mountains available, 0% disables additional mountains and higher values reduce the free land down to the space required by headquarters. See #1530 Co-Authored-By: Claude Opus 5.5 --- libs/s25main/ingameWindows/iwMapGenerator.cpp | 7 +- libs/s25main/ingameWindows/iwMapGenerator.h | 2 + libs/s25main/mapGenerator/MapSettings.h | 1 + libs/s25main/mapGenerator/RandomMap.cpp | 16 ++++- libs/s25main/mapGenerator/RandomMap.h | 12 ++++ tests/s25Main/UI/testIngameWindow.cpp | 4 ++ tests/s25Main/mapGenerator/testRandomMap.cpp | 68 +++++++++++++++++++ 7 files changed, 106 insertions(+), 4 deletions(-) diff --git a/libs/s25main/ingameWindows/iwMapGenerator.cpp b/libs/s25main/ingameWindows/iwMapGenerator.cpp index 506800d66a..14247bf508 100644 --- a/libs/s25main/ingameWindows/iwMapGenerator.cpp +++ b/libs/s25main/ingameWindows/iwMapGenerator.cpp @@ -19,7 +19,7 @@ using namespace rttr::mapGenerator; iwMapGenerator::iwMapGenerator(MapSettings& settings) - : IngameWindow(CGI_MAP_GENERATOR, IngameWindow::posLastOrCenter, Extent(270, 520), _("Map Generator"), + : IngameWindow(CGI_MAP_GENERATOR, IngameWindow::posLastOrCenter, Extent(270, 550), _("Map Generator"), LOADER.GetImageN("resource", 41), true, CloseBehavior::Custom), mapSettings(settings) { @@ -109,6 +109,9 @@ iwMapGenerator::iwMapGenerator(MapSettings& settings) AddText(ID_txtTrees, curPos, _("Trees:"), COLOR_YELLOW, FontStyle{}, NormalFont); AddProgress(ID_pgTrees, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); curPos.y += 30; + AddText(ID_txtMountains, curPos, _("Mountains:"), COLOR_YELLOW, FontStyle{}, NormalFont); + AddProgress(ID_pgMountains, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); + curPos.y += 30; AddText(ID_txtStonePiles, curPos, _("Stone piles:"), COLOR_YELLOW, FontStyle{}, NormalFont); AddProgress(ID_pgStonePiles, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); @@ -140,6 +143,7 @@ void iwMapGenerator::Apply() mapSettings.ratioGranite = GetCtrl(ID_pgGraniteRatio)->GetPosition(); mapSettings.rivers = GetCtrl(ID_pgRivers)->GetPosition(); mapSettings.trees = GetCtrl(ID_pgTrees)->GetPosition(); + mapSettings.mountains = GetCtrl(ID_pgMountains)->GetPosition(); mapSettings.stonePiles = GetCtrl(ID_pgStonePiles)->GetPosition(); switch(GetCtrl(ID_cbMountainDist)->GetSelection().value()) @@ -181,6 +185,7 @@ void iwMapGenerator::Reset() GetCtrl(ID_pgGraniteRatio)->SetPosition(mapSettings.ratioGranite); GetCtrl(ID_pgRivers)->SetPosition(mapSettings.rivers); GetCtrl(ID_pgTrees)->SetPosition(mapSettings.trees); + GetCtrl(ID_pgMountains)->SetPosition(mapSettings.mountains); GetCtrl(ID_pgStonePiles)->SetPosition(mapSettings.stonePiles); auto* combo = GetCtrl(ID_cbMountainDist); diff --git a/libs/s25main/ingameWindows/iwMapGenerator.h b/libs/s25main/ingameWindows/iwMapGenerator.h index 6297e5ed58..f0e0cbfcf3 100644 --- a/libs/s25main/ingameWindows/iwMapGenerator.h +++ b/libs/s25main/ingameWindows/iwMapGenerator.h @@ -23,6 +23,7 @@ class iwMapGenerator : public IngameWindow ID_txtRivers, ID_txtMountainDist, ID_txtTrees, + ID_txtMountains, ID_txtStonePiles, ID_txtIslands, ID_cbNumPlayers, @@ -40,6 +41,7 @@ class iwMapGenerator : public IngameWindow ID_pgRivers, ID_cbMountainDist, ID_pgTrees, + ID_pgMountains, ID_pgStonePiles, ID_cbIslands }; diff --git a/libs/s25main/mapGenerator/MapSettings.h b/libs/s25main/mapGenerator/MapSettings.h index 4558255f60..4514676d56 100644 --- a/libs/s25main/mapGenerator/MapSettings.h +++ b/libs/s25main/mapGenerator/MapSettings.h @@ -50,6 +50,7 @@ struct MapSettings unsigned short ratioGranite = 15; unsigned short rivers = 15; unsigned short trees = 40; + unsigned short mountains = 50; unsigned short stonePiles = 5; IslandAmount islands = IslandAmount::Few; MountainDistance mountainDistance = MountainDistance::Normal; diff --git a/libs/s25main/mapGenerator/RandomMap.cpp b/libs/s25main/mapGenerator/RandomMap.cpp index a22de0c3fc..d40bfc8dba 100644 --- a/libs/s25main/mapGenerator/RandomMap.cpp +++ b/libs/s25main/mapGenerator/RandomMap.cpp @@ -15,6 +15,7 @@ #include "lua/GameDataLoader.h" #include "libsiedler2/libsiedler2.h" +#include #include namespace rttr::mapGenerator { @@ -94,6 +95,13 @@ unsigned GetMaxMountainSize(const MapExtent& size) return 1500; } +unsigned GetMinFreeLand(unsigned mountainDistance, unsigned mountains) +{ + constexpr unsigned minFreeLand = 4; // Headquarters and the transition textures around mountains need space + const unsigned freeLandPercentage = 2 * (100 - std::min(mountains, 100u)); + return std::max(minFreeLand, mountainDistance * freeLandPercentage / 100); +} + unsigned GetSmoothRadius(const MapExtent& size) { const unsigned combinedSize = size.x * size.y; @@ -197,9 +205,11 @@ void RandomMap::CreateFreeIslands(unsigned waterNodes) void RandomMap::AddMountains(unsigned mountainLevel) { - // The distance players want to keep to mountains must remain available for their headquarters - const auto mountainDistance = static_cast(settings_.mountainDistance); - const auto minFreeLand = rnd_.RandomValue(mountainDistance, mountainDistance + 5u); + if(settings_.mountains == 0) + return; + + const auto freeLand = GetMinFreeLand(static_cast(settings_.mountainDistance), settings_.mountains); + const auto minFreeLand = rnd_.RandomValue(freeLand, freeLand + 5u); const auto maxMountainSize = GetMaxMountainSize(map_.size); const auto maxSize = rnd_.RandomValue(maxMountainSize / 2, maxMountainSize); diff --git a/libs/s25main/mapGenerator/RandomMap.h b/libs/s25main/mapGenerator/RandomMap.h index f5ccf7b49b..47d4c88e32 100644 --- a/libs/s25main/mapGenerator/RandomMap.h +++ b/libs/s25main/mapGenerator/RandomMap.h @@ -18,6 +18,18 @@ unsigned GetCoastline(const MapExtent& size); unsigned GetIslandRadius(const MapExtent& size); unsigned GetIslandSize(const MapExtent& size); unsigned GetMaxMountainSize(const MapExtent& size); + +/** + * Computes the amount of free land to keep around water and mountains. + * + * @param mountainDistance desired distance of headquarters to mountains + * @param mountains desired amount of mountains in percentage (between 0 and 100) - the default of 50 keeps the desired + * distance available while higher values reduce the free land down to the space required by headquarters + * + * @returns the distance to water and mountains up to which land stays free. + */ +unsigned GetMinFreeLand(unsigned mountainDistance, unsigned mountains); + unsigned GetSmoothRadius(const MapExtent& size); unsigned GetSmoothIterations(const MapExtent& size); diff --git a/tests/s25Main/UI/testIngameWindow.cpp b/tests/s25Main/UI/testIngameWindow.cpp index 8ef7fc6e48..7e46635ce5 100644 --- a/tests/s25Main/UI/testIngameWindow.cpp +++ b/tests/s25Main/UI/testIngameWindow.cpp @@ -81,6 +81,7 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) const auto expectedGraniteRatio = rttr::test::randomValue(0u, 100u); const auto expectedRivers = rttr::test::randomValue(0u, 100u); const auto expectedTrees = rttr::test::randomValue(0u, 100u); + const auto expectedMountains = rttr::test::randomValue(0u, 100u); const auto expectedStonePiles = rttr::test::randomValue(0u, 100u); rttr::mapGenerator::MapSettings settings; @@ -116,6 +117,8 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) wnd.GetCtrl(iwMapGenerator::ID_pgRivers)->SetPosition(expectedRivers); BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgTrees)->GetPosition() == settings.trees); wnd.GetCtrl(iwMapGenerator::ID_pgTrees)->SetPosition(expectedTrees); + BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgMountains)->GetPosition() == settings.mountains); + wnd.GetCtrl(iwMapGenerator::ID_pgMountains)->SetPosition(expectedMountains); BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgStonePiles)->GetPosition() == settings.stonePiles); wnd.GetCtrl(iwMapGenerator::ID_pgStonePiles)->SetPosition(expectedStonePiles); wnd.Msg_ButtonClick(iwMapGenerator::ID_btApply); @@ -133,6 +136,7 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) BOOST_TEST(settings.ratioGranite == expectedGraniteRatio); BOOST_TEST(settings.rivers == expectedRivers); BOOST_TEST(settings.trees == expectedTrees); + BOOST_TEST(settings.mountains == expectedMountains); BOOST_TEST(settings.stonePiles == expectedStonePiles); } diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index cb539050be..a3997d73e6 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -154,6 +154,74 @@ BOOST_AUTO_TEST_CASE(GetMaxMountainSize_grows_with_map_size) BOOST_TEST(GetMaxMountainSize(MapExtent(512, 512)) < GetMaxMountainSize(MapExtent(1024, 1024))); } +BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_mountain_distance_for_default_amount) +{ + const MapSettings settings; + + BOOST_TEST(GetMinFreeLand(15, settings.mountains) == 15u); +} + +BOOST_AUTO_TEST_CASE(GetMinFreeLand_decreases_with_amount_of_mountains) +{ + BOOST_TEST(GetMinFreeLand(30, 25) > GetMinFreeLand(30, 50)); + BOOST_TEST(GetMinFreeLand(30, 50) > GetMinFreeLand(30, 75)); +} + +BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_space_for_headquarters) +{ + BOOST_TEST(GetMinFreeLand(30, 100) == 4u); + BOOST_TEST(GetMinFreeLand(5, 90) == 4u); + BOOST_TEST(GetMinFreeLand(30, 1000) == 4u); +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_more_mountains_for_higher_amount) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(256, 256); + settings.style = MapStyle::Mixed; + + const auto countMountainNodes = [&worldDesc, &settings](const unsigned short mountains) { + settings.mountains = mountains; + RandomUtility rnd(0); + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + return SelectPoints([&map](const MapPoint& pt) { return map.textureMap.Any(pt, IsMinableMountain); }, map.size) + .size(); + }; + + const auto noMountains = countMountainNodes(0); + const auto defaultMountains = countMountainNodes(50); + const auto maxMountains = countMountainNodes(100); + + BOOST_TEST(noMountains < defaultMountains); + BOOST_TEST(defaultMountains < maxMountains); +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_maps_for_maximum_amount_of_mountains) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(128, 128); + settings.mountains = 100; + + for(const unsigned numPlayers : {2u, MAX_PLAYERS}) + { + for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed, MapStyle::Water}) + { + settings.numPlayers = numPlayers; + settings.style = style; + RandomUtility rnd(1); + + Map map = GenerateRandomMap(rnd, worldDesc, settings); + ValidateMap(map, settings.size, settings.numPlayers); + } + } +} + BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) { WorldDescription worldDesc; From e55db471a4dd23987ff32e8409fc4ed6050b6d91 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Fri, 13 Nov 2026 11:55:02 +0000 Subject: [PATCH 3/9] Add deserts, swamps and special objects to the map generator Free land of random maps is no longer filled with mountains only. Each area found by the mountain search becomes a mountain, a desert hill or a swamp. The new "Desert" and "Swamp" sliders set the proportions together with "Mountains", and the sum of the three controls how much free land is filled. Types are assigned by a running budget so the proportions hold even for a handful of areas, and the first area is always a mountain to keep mines available. - Desert hills are low, smooth hills of desert (wasteland or ice in the other landscapes) with bones around the center. - Swamps (stony lava or ice floes) are flat and half the size of a mountain. Land above a swamp descends to it by one level per node at most, without lowering the banks of rivers or other landscapes. - Deserts are always separated from lush land by steppe and savannah. This is applied after all other texturing as coast transitions may replace textures set before. - Trees, stone piles, animals, fish and harbors keep away from deserts and swamps. The new "Special Objects" checkbox places a few scenes on buildable land, depending on the map size. A scene combines a theme (castle ruin, tower ruin, graveyard, camp, stone circle) with a random layout (overgrown patch, avenue, crag with pond, trail). The ground of a scene blends into the land with one ring per transition texture, hills are smooth and the scene stays free from random trees and stone piles. Co-Authored-By: Claude Fable 5.2 --- libs/s25main/ingameWindows/iwMapGenerator.cpp | 18 +- libs/s25main/ingameWindows/iwMapGenerator.h | 7 +- libs/s25main/mapGenerator/FreeLand.cpp | 293 ++++++ libs/s25main/mapGenerator/FreeLand.h | 113 +++ libs/s25main/mapGenerator/Harbors.cpp | 5 +- libs/s25main/mapGenerator/Map.h | 5 + libs/s25main/mapGenerator/MapSettings.h | 7 +- libs/s25main/mapGenerator/Mountains.cpp | 31 +- libs/s25main/mapGenerator/Mountains.h | 27 + libs/s25main/mapGenerator/RandomMap.cpp | 46 +- libs/s25main/mapGenerator/RandomMap.h | 8 +- libs/s25main/mapGenerator/Resources.cpp | 18 +- libs/s25main/mapGenerator/SpecialObjects.cpp | 886 ++++++++++++++++++ libs/s25main/mapGenerator/SpecialObjects.h | 119 +++ libs/s25main/mapGenerator/TextureHelper.cpp | 5 + libs/s25main/mapGenerator/TextureHelper.h | 4 + tests/s25Main/UI/testIngameWindow.cpp | 13 + tests/s25Main/mapGenerator/testFreeLand.cpp | 719 ++++++++++++++ tests/s25Main/mapGenerator/testRandomMap.cpp | 178 +++- .../mapGenerator/testSpecialObjects.cpp | 542 +++++++++++ 20 files changed, 3008 insertions(+), 36 deletions(-) create mode 100644 libs/s25main/mapGenerator/FreeLand.cpp create mode 100644 libs/s25main/mapGenerator/FreeLand.h create mode 100644 libs/s25main/mapGenerator/SpecialObjects.cpp create mode 100644 libs/s25main/mapGenerator/SpecialObjects.h create mode 100644 tests/s25Main/mapGenerator/testFreeLand.cpp create mode 100644 tests/s25Main/mapGenerator/testSpecialObjects.cpp diff --git a/libs/s25main/ingameWindows/iwMapGenerator.cpp b/libs/s25main/ingameWindows/iwMapGenerator.cpp index 14247bf508..c4df20210a 100644 --- a/libs/s25main/ingameWindows/iwMapGenerator.cpp +++ b/libs/s25main/ingameWindows/iwMapGenerator.cpp @@ -4,6 +4,7 @@ #include "iwMapGenerator.h" #include "Loader.h" +#include "controls/ctrlCheck.h" #include "controls/ctrlComboBox.h" #include "controls/ctrlProgress.h" #include "helpers/containerUtils.h" @@ -19,7 +20,7 @@ using namespace rttr::mapGenerator; iwMapGenerator::iwMapGenerator(MapSettings& settings) - : IngameWindow(CGI_MAP_GENERATOR, IngameWindow::posLastOrCenter, Extent(270, 550), _("Map Generator"), + : IngameWindow(CGI_MAP_GENERATOR, IngameWindow::posLastOrCenter, Extent(270, 645), _("Map Generator"), LOADER.GetImageN("resource", 41), true, CloseBehavior::Custom), mapSettings(settings) { @@ -112,10 +113,19 @@ iwMapGenerator::iwMapGenerator(MapSettings& settings) AddText(ID_txtMountains, curPos, _("Mountains:"), COLOR_YELLOW, FontStyle{}, NormalFont); AddProgress(ID_pgMountains, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); curPos.y += 30; + AddText(ID_txtDesert, curPos, _("Desert:"), COLOR_YELLOW, FontStyle{}, NormalFont); + AddProgress(ID_pgDesert, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); + curPos.y += 30; + AddText(ID_txtSwamp, curPos, _("Swamp:"), COLOR_YELLOW, FontStyle{}, NormalFont); + AddProgress(ID_pgSwamp, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); + curPos.y += 30; AddText(ID_txtStonePiles, curPos, _("Stone piles:"), COLOR_YELLOW, FontStyle{}, NormalFont); AddProgress(ID_pgStonePiles, DrawPoint(pgrOffset, curPos.y - 5), progressSize, TextureColor::Grey, 139, 138, 100); curPos.y += 25; + AddCheckBox(ID_cbSpecialObjects, curPos, comboSize, TextureColor::Grey, _("Special Objects"), NormalFont, false); + + curPos.y += 35; AddTextButton(ID_btBack, curPos, buttonSize, TextureColor::Red1, _("Back"), NormalFont); AddTextButton(ID_btApply, DrawPoint(130, curPos.y), buttonSize, TextureColor::Green2, _("Apply"), NormalFont); @@ -144,7 +154,10 @@ void iwMapGenerator::Apply() mapSettings.rivers = GetCtrl(ID_pgRivers)->GetPosition(); mapSettings.trees = GetCtrl(ID_pgTrees)->GetPosition(); mapSettings.mountains = GetCtrl(ID_pgMountains)->GetPosition(); + mapSettings.desert = GetCtrl(ID_pgDesert)->GetPosition(); + mapSettings.swamp = GetCtrl(ID_pgSwamp)->GetPosition(); mapSettings.stonePiles = GetCtrl(ID_pgStonePiles)->GetPosition(); + mapSettings.specialObjects = GetCtrl(ID_cbSpecialObjects)->isChecked(); switch(GetCtrl(ID_cbMountainDist)->GetSelection().value()) { @@ -186,7 +199,10 @@ void iwMapGenerator::Reset() GetCtrl(ID_pgRivers)->SetPosition(mapSettings.rivers); GetCtrl(ID_pgTrees)->SetPosition(mapSettings.trees); GetCtrl(ID_pgMountains)->SetPosition(mapSettings.mountains); + GetCtrl(ID_pgDesert)->SetPosition(mapSettings.desert); + GetCtrl(ID_pgSwamp)->SetPosition(mapSettings.swamp); GetCtrl(ID_pgStonePiles)->SetPosition(mapSettings.stonePiles); + GetCtrl(ID_cbSpecialObjects)->setChecked(mapSettings.specialObjects); auto* combo = GetCtrl(ID_cbMountainDist); switch(mapSettings.mountainDistance) diff --git a/libs/s25main/ingameWindows/iwMapGenerator.h b/libs/s25main/ingameWindows/iwMapGenerator.h index f0e0cbfcf3..876f589768 100644 --- a/libs/s25main/ingameWindows/iwMapGenerator.h +++ b/libs/s25main/ingameWindows/iwMapGenerator.h @@ -24,6 +24,8 @@ class iwMapGenerator : public IngameWindow ID_txtMountainDist, ID_txtTrees, ID_txtMountains, + ID_txtDesert, + ID_txtSwamp, ID_txtStonePiles, ID_txtIslands, ID_cbNumPlayers, @@ -42,8 +44,11 @@ class iwMapGenerator : public IngameWindow ID_cbMountainDist, ID_pgTrees, ID_pgMountains, + ID_pgDesert, + ID_pgSwamp, ID_pgStonePiles, - ID_cbIslands + ID_cbIslands, + ID_cbSpecialObjects }; /** diff --git a/libs/s25main/mapGenerator/FreeLand.cpp b/libs/s25main/mapGenerator/FreeLand.cpp new file mode 100644 index 0000000000..00338e8a2c --- /dev/null +++ b/libs/s25main/mapGenerator/FreeLand.cpp @@ -0,0 +1,293 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "mapGenerator/FreeLand.h" +#include "helpers/containerUtils.h" +#include "mapGenerator/Algorithms.h" +#include "mapGenerator/TextureHelper.h" +#include "libsiedler2/enumTypes.h" +#include +#include +#include +#include + +namespace rttr::mapGenerator { + +namespace { + /// Maximum elevation of the center of a desert hill + constexpr unsigned maxDesertElevation = 4; + /// Number of rings of transition textures around the desert + constexpr unsigned desertTransitions = 2; + /// Chance in percentage for bones on a node around the center of a desert hill + constexpr unsigned boneChance = 12; + /// Chance in percentage for a dead tree on a node next to a swamp + constexpr unsigned deadTreeChance = 33; + /// Number of passes to close gaps of an area + constexpr unsigned gapIterations = 2; + /// Number of neighbors within an area required for a node to be considered a gap of the area + constexpr unsigned minGapNeighbors = 4; + + void PlaceDecoration(Map& map, const MapPoint& pt, uint8_t index) + { + map.objectTypes[pt] = OT_Decoration; + map.objectInfos[pt] = index; + } +} // namespace + +Mountain CloseGaps(const MapExtent& size, const Mountain& area) +{ + NodeMapBase neighbors; + neighbors.Resize(size); + Mountain closedArea = area; + + for(unsigned i = 0; i < gapIterations; i++) + { + std::set candidates; + for(const MapPoint& pt : closedArea) + { + for(const MapPoint& neighbor : neighbors.GetNeighbours(pt)) + { + if(!helpers::contains(closedArea, neighbor)) + candidates.insert(neighbor); + } + } + + std::vector gaps; + for(const MapPoint& candidate : candidates) + { + const auto inArea = [&closedArea](const MapPoint& pt) { return helpers::contains(closedArea, pt); }; + if(helpers::count_if(neighbors.GetNeighbours(candidate), inArea) >= minGapNeighbors) + gaps.push_back(candidate); + } + + if(gaps.empty()) + break; + closedArea.insert(gaps.begin(), gaps.end()); + } + + return closedArea; +} + +LandFill SelectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned) +{ + const uint64_t totalAmount = std::accumulate(amounts.begin(), amounts.end(), uint64_t(0)); + const uint64_t totalAssigned = std::accumulate(assigned.begin(), assigned.end(), uint64_t(0)); + + if(totalAmount == 0 || (totalAssigned == 0 && amounts[static_cast(LandFill::Mountain)] > 0)) + return LandFill::Mountain; + + // A type is behind if its share of assigned nodes doesn't exceed its share of the amounts. This always applies to + // at least one of the types. + std::vector candidates; + unsigned candidateAmount = 0; + for(unsigned i = 0; i < numLandFills; i++) + { + if(amounts[i] > 0 && assigned[i] * totalAmount <= totalAssigned * amounts[i]) + { + candidates.push_back(i); + candidateAmount += amounts[i]; + } + } + + if(candidates.size() == 1) + return static_cast(candidates.front()); + + unsigned value = rnd.RandomValue(1u, candidateAmount); + for(const unsigned candidate : candidates) + { + if(value <= amounts[candidate]) + return static_cast(candidate); + value -= amounts[candidate]; + } + + return static_cast(candidates.back()); +} + +void RaiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area) +{ + if(area.empty()) + return; + + const auto footDistance = GetFootDistances(map.size, area); + const unsigned maxDistance = GetMaximum(footDistance, area); + // Small hills get less transitions to still contain some desert + const unsigned transitions = std::min(desertTransitions, maxDistance); + + const auto desert = map.textureMap.Find(IsCoastTerrain); + auto transitionTextures = map.textureMap.FindAll(IsBuildableCoast); + map.textureMap.Sort(transitionTextures, ByHumidity); + + // Neighboring nodes share triangles. Texturing from the foot to the center lets the inner texture win which results + // in one row of triangles per transition and nodes of the desert which are entirely covered by desert. + std::vector nodes(area.begin(), area.end()); + std::stable_sort(nodes.begin(), nodes.end(), [&footDistance](const MapPoint& lhs, const MapPoint& rhs) { + return footDistance[lhs] < footDistance[rhs]; + }); + + for(const MapPoint& pt : nodes) + { + const unsigned distance = footDistance[pt]; + const unsigned elevation = std::min((distance + 1) / 2, maxDesertElevation); + map.z[pt] = static_cast(std::min(map.z[pt] + elevation, map.height.maximum)); + + if(distance >= transitions) + { + map.textureMap.Set(pt, desert); + map.barren.insert(pt); + } else + { + // The driest texture is next to the desert + const auto maxIndex = static_cast(transitionTextures.size() - 1); + const unsigned index = std::min(transitions - 1 - distance, maxIndex); + map.textureMap.Set(pt, transitionTextures[index]); + } + } + + bool hasBones = false; + for(const MapPoint& pt : area) + { + const unsigned distance = footDistance[pt]; + const bool isCenter = distance == maxDistance; + const bool isAroundCenter = distance >= transitions && distance * 3 >= maxDistance * 2; + + if((isCenter && !hasBones) || (isAroundCenter && rnd.ByChance(boneChance))) + { + PlaceDecoration(map, pt, rnd.ByChance(50) ? OI_Bone1 : OI_Bone2); + hasBones = true; + } + } +} + +void CreateSwamp(Map& map, RandomUtility& rnd, const Mountain& area) +{ + if(area.empty()) + return; + + const auto swamp = map.textureMap.Find(IsImpassable); + const uint8_t level = map.z[*std::min_element( + area.begin(), area.end(), [&map](const MapPoint& lhs, const MapPoint& rhs) { return map.z[lhs] < map.z[rhs]; })]; + + // Other landscapes (rivers, mountains, deserts, water) are textured already or located at sea level. Rivers follow + // the height of the land. Lowering their banks would leave the water on top of a ridge. + const auto obstacleDistances = DistancesTo(map.size, [&map](const MapPoint& pt) { + if(map.z[pt] == map.height.minimum) + return true; + const auto triangles = GetTriangles(pt, map.size); + return helpers::contains_if(triangles, + [&map](const Triangle& t) { return static_cast(map.textureMap.Get(t)); }); + }); + + for(const MapPoint& pt : area) + { + map.z[pt] = level; + map.textureMap.Set(pt, swamp); + map.barren.insert(pt); + } + + const auto distances = DistancesTo(area, map.size); + + RTTR_FOREACH_PT(MapPoint, map.size) + { + const unsigned distance = distances[pt]; + if(distance == 0) + continue; + + // The land descends to the swamp by one level per node at most. Other landscapes and the land next to them + // keep their height. From there the land may be lowered by one more level with each node. + const unsigned maxLowering = obstacleDistances[pt] > 0 ? obstacleDistances[pt] - 1 : 0; + const unsigned target = std::min(map.z[pt], level + distance); + map.z[pt] = static_cast(std::max(target, map.z[pt] > maxLowering ? map.z[pt] - maxLowering : 0u)); + + if(distance == 1 && map.objectTypes[pt] == libsiedler2::OT_Empty && rnd.ByChance(deadTreeChance)) + { + PlaceDecoration(map, pt, rnd.ByChance(50) ? OI_DeadTree : OI_DeadTreeTrunk); + } + } +} + +void SmoothDesertTransitions(Map& map) +{ + auto& textures = map.textureMap; + const auto desert = textures.Find(IsCoastTerrain); + std::vector desertNodes; + for(const MapPoint& pt : map.barren) + { + if(textures.All(pt, IsCoastTerrain)) + desertNodes.push_back(pt); + } + if(desertNodes.empty()) + return; + + // Land from dry to lush: each ring around the desert may be one step more lush than the previous one + auto ramp = textures.FindAll(IsBuildableLand); + textures.Sort(ramp, [](const TerrainDesc& terrain) { return std::make_pair(terrain.humidity, terrain.s2Id); }); + const auto exceeds = [&textures, &ramp](const MapPoint& pt, unsigned limit) { + for(const Triangle& triangle : GetTriangles(pt, textures.textures_.GetSize())) + { + const auto it = std::find(ramp.begin(), ramp.end(), textures.Get(triangle)); + if(it != ramp.end() && static_cast(it - ramp.begin()) > limit) + return true; + } + return false; + }; + const auto isWalkableLand = [](const TerrainDesc& terrain) { + return terrain.kind == TerrainKind::Land && terrain.Is(ETerrain::Walkable); + }; + + const auto distances = DistancesTo(desertNodes, map.size); + const unsigned rings = std::min(3, static_cast(ramp.size())); + + // Neighboring nodes share triangles: painting from the outside to the inside keeps one row for each transition + for(unsigned ring = rings; ring > 0; ring--) + { + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(distances[pt] == ring && !helpers::contains(map.barren, pt) && textures.All(pt, isWalkableLand) + && exceeds(pt, ring - 1)) + textures.Set(pt, ramp[ring - 1]); + } + } + for(const MapPoint& pt : desertNodes) + textures.Set(pt, desert); +} + +std::vector FillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings, const LandFillAmounts& amounts) +{ + std::vector types; + LandFillAmounts assigned{}; + + // The type is required before the area is known as it has an impact on the size + const auto getSize = [&](unsigned index) { + const LandFill type = SelectLandFill(rnd, amounts, assigned); + unsigned size = GetMountainSize(settings, index); + if(type == LandFill::Swamp) + size = std::max(size / 2, std::min(size, settings.minSize)); + + types.push_back(type); + assigned[static_cast(type)] += size; + return size; + }; + + const auto areas = FindMountains(map, rnd, mountainLevel, settings.minFreeLand, getSize); + + std::vector filledAreas; + for(unsigned i = 0; i < areas.size(); i++) + { + // Gaps would end up as pits within desert hills and as tiny islands within swamps + const Mountain area = types[i] == LandFill::Mountain ? areas[i] : CloseGaps(map.size, areas[i]); + + switch(types[i]) + { + case LandFill::Mountain: RaiseMountain(map, rnd, area); break; + case LandFill::Desert: RaiseDesertHill(map, rnd, area); break; + case LandFill::Swamp: CreateSwamp(map, rnd, area); break; + } + filledAreas.push_back(FilledArea{types[i], area}); + } + + return filledAreas; +} + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/FreeLand.h b/libs/s25main/mapGenerator/FreeLand.h new file mode 100644 index 0000000000..ab41f04f89 --- /dev/null +++ b/libs/s25main/mapGenerator/FreeLand.h @@ -0,0 +1,113 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "mapGenerator/Map.h" +#include "mapGenerator/Mountains.h" +#include "mapGenerator/RandomUtility.h" +#include +#include + +namespace rttr::mapGenerator { + +/// Types of landscape used to fill up large areas of free land +enum class LandFill +{ + Mountain, + Desert, + Swamp +}; + +/// Number of different types of landscape to fill free land with +constexpr unsigned numLandFills = 3; + +/// Desired amount of each type of landscape - indexed by LandFill. Only the proportions matter. +using LandFillAmounts = std::array; + +struct FilledArea +{ + LandFill type; + Mountain nodes; +}; + +/// Object type of decorative objects +constexpr uint8_t OT_Decoration = 0xC8; +/// Object indices of the decorative objects used for deserts and swamps +constexpr uint8_t OI_DeadTreeTrunk = 0x05; +constexpr uint8_t OI_DeadTree = 0x06; +constexpr uint8_t OI_Bone1 = 0x07; +constexpr uint8_t OI_Bone2 = 0x08; + +/** + * Closes gaps of an area which has been grown with an irregular shape. Nodes which are mostly surrounded by the area + * become part of it. + * + * @param size size of the map + * @param area nodes of the area + * + * @returns the nodes of the area including its gaps. + */ +Mountain CloseGaps(const MapExtent& size, const Mountain& area); + +/** + * Selects the type of landscape for the next area. Types which are below their desired share of nodes are chosen by + * chance according to the desired amounts. This keeps the proportions even for a low number of areas. The first area + * always becomes a mountain (if desired at all) since mountains are required for mining. + * + * @param rnd random number generator + * @param amounts desired amount of each type of landscape + * @param assigned number of nodes assigned to each type of landscape so far (indexed by LandFill) + * + * @returns the type of landscape for the next area. A mountain if all amounts are zero. + */ +LandFill SelectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned); + +/** + * Turns the area into a desert hill. The hill is lower than a mountain and rises smoothly towards its center. The outer + * nodes get buildable transition textures while the inner nodes are covered by desert (or the equivalent texture of the + * landscape). Triangles are shared between nodes with the texture closer to the center taking precedence. Bones are + * placed around the center of the hill. + * + * @param map reference to the map to place the hill on (manipulates textures, objects and z-values of the map) + * @param rnd random number generator + * @param area nodes of the desert hill + */ +void RaiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area); + +/** + * Turns the area into a swamp (or the equivalent texture of the landscape). All nodes of the swamp are lowered to the + * same height and the surrounding land is lowered to descend to the swamp by one level per node at most. Dead trees are + * placed on the land right next to the swamp. + * + * @param map reference to the map to place the swamp on (manipulates textures, objects and z-values of the map) + * @param rnd random number generator + * @param area nodes of the swamp + */ +void CreateSwamp(Map& map, RandomUtility& rnd, const Mountain& area); + +/** + * Makes sure deserts never touch lush land: the nodes around a desert get the driest land texture followed by one ring + * for each of the next textures unless the land is dry anyway. This function has to be called once all textures are + * in place as texturing coasts may replace the transitions applied by desert hills. + * + * @param map reference to the map to update the textures for + */ +void SmoothDesertTransitions(Map& map); + +/** + * Reduces the size of free land by adding mountains, desert hills and swamps to the map. + * + * @param map reference to the map to fill (manipulates textures, objects and z-values of the map) + * @param rnd random number generator + * @param mountainLevel minimum z-value of mountains + * @param settings settings for the size of the new areas - swamps only get half the size as they are impassable + * @param amounts desired amount of each type of landscape + * + * @returns the type and the nodes of each new area. Gaps of desert hills and swamps are closed. + */ +std::vector FillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, + const MountainSettings& settings, const LandFillAmounts& amounts); + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/Harbors.cpp b/libs/s25main/mapGenerator/Harbors.cpp index f373231d98..a1df7fa096 100644 --- a/libs/s25main/mapGenerator/Harbors.cpp +++ b/libs/s25main/mapGenerator/Harbors.cpp @@ -41,7 +41,10 @@ std::vector> FindCoastlines(const Map& map, const std::vec std::set visited; const auto isCoast = [&map](const MapPoint& pt) { - const auto allWater = [&map](const MapPoint& p) { return map.textureMap.All(p, IsWater); }; + // Swamps may consist of water textures but can't be used by ships + const auto allWater = [&map](const MapPoint& p) { + return map.textureMap.All(p, IsWater) && !helpers::contains(map.barren, p); + }; return map.textureMap.Any(pt, IsLand) && map.textureMap.Any(pt, IsWater) && helpers::contains_if(map.getTextures().GetNeighbours(pt), allWater); }; diff --git a/libs/s25main/mapGenerator/Map.h b/libs/s25main/mapGenerator/Map.h index 5ad3da5c54..76463e2c31 100644 --- a/libs/s25main/mapGenerator/Map.h +++ b/libs/s25main/mapGenerator/Map.h @@ -12,6 +12,7 @@ #include "libsiedler2/archives.h" #include +#include #include namespace rttr::mapGenerator { @@ -29,6 +30,10 @@ class Map NodeMapBase animals; std::vector harbors; std::vector hqPositions; + /// Nodes of deserts and swamps which get neither trees, stone piles, animals nor fish + std::set barren; + /// Nodes of special scenes which get neither trees nor stone piles + std::set reserved; TextureMap textureMap; const std::string name; diff --git a/libs/s25main/mapGenerator/MapSettings.h b/libs/s25main/mapGenerator/MapSettings.h index 4514676d56..016010a618 100644 --- a/libs/s25main/mapGenerator/MapSettings.h +++ b/libs/s25main/mapGenerator/MapSettings.h @@ -48,10 +48,13 @@ struct MapSettings unsigned short ratioIron = 36; unsigned short ratioCoal = 40; unsigned short ratioGranite = 15; - unsigned short rivers = 15; + unsigned short rivers = 21; unsigned short trees = 40; - unsigned short mountains = 50; + unsigned short mountains = 25; + unsigned short desert = 22; + unsigned short swamp = 18; unsigned short stonePiles = 5; + bool specialObjects = true; IslandAmount islands = IslandAmount::Few; MountainDistance mountainDistance = MountainDistance::Normal; DescIdx type = DescIdx(0); diff --git a/libs/s25main/mapGenerator/Mountains.cpp b/libs/s25main/mapGenerator/Mountains.cpp index a36ee3113c..293c0d44f4 100644 --- a/libs/s25main/mapGenerator/Mountains.cpp +++ b/libs/s25main/mapGenerator/Mountains.cpp @@ -98,20 +98,27 @@ Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings) +{ + return FindMountains(map, rnd, mountainLevel, settings.minFreeLand, + [&settings](unsigned index) { return GetMountainSize(settings, index); }); +} + +std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, + const std::function& getSize) { std::vector mountains; auto freeLand = GetFreeLandDistances(map, mountainLevel); const auto maxDistance = [&freeLand] { return *std::max_element(freeLand.begin(), freeLand.end()); }; - for(unsigned distance = maxDistance(); distance > settings.minFreeLand; distance = maxDistance()) + for(unsigned distance = maxDistance(); distance > minFreeLand; distance = maxDistance()) { const auto isFreest = [&freeLand, distance](const MapPoint& pt) { return freeLand[pt] == distance; }; const MapPoint center = rnd.RandomItem(SelectPoints(isFreest, map.size)); // A larger margin than required varies the gaps. It must stay below the distance to include the center. - const unsigned maxMargin = std::min(settings.minFreeLand + settings.minFreeLand / 2, distance - 1); - const unsigned margin = rnd.RandomValue(settings.minFreeLand, maxMargin); - const unsigned size = GetMountainSize(settings, static_cast(mountains.size())); + const unsigned maxMargin = std::min(minFreeLand + minFreeLand / 2, distance - 1); + const unsigned margin = rnd.RandomValue(minFreeLand, maxMargin); + const unsigned size = getSize(static_cast(mountains.size())); mountains.push_back(GrowMountain(freeLand, rnd, center, size, margin)); MarkAsOccupied(freeLand, mountains.back()); @@ -120,13 +127,21 @@ std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned return mountains; } -void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) +NodeMapBase GetFootDistances(const MapExtent& size, const Mountain& mountain) { - const auto isFoot = [&map, &mountain](const MapPoint& pt) { - return helpers::contains_if(map.z.GetNeighbours(pt), + NodeMapBase neighbors; + neighbors.Resize(size); + + const auto isFoot = [&neighbors, &mountain](const MapPoint& pt) { + return helpers::contains_if(neighbors.GetNeighbours(pt), [&mountain](const MapPoint& pt) { return !helpers::contains(mountain, pt); }); }; - const auto footDistance = Distances(map.size, mountain, 0, isFoot); + return Distances(size, mountain, 0, isFoot); +} + +void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) +{ + const auto footDistance = GetFootDistances(map.size, mountain); const ValueRange range(0u, GetMaximum(footDistance, mountain)); const auto textures = map.textureMap.FindAll(IsMinableMountain); diff --git a/libs/s25main/mapGenerator/Mountains.h b/libs/s25main/mapGenerator/Mountains.h index 4a1c9c869e..221f6e0d98 100644 --- a/libs/s25main/mapGenerator/Mountains.h +++ b/libs/s25main/mapGenerator/Mountains.h @@ -6,6 +6,7 @@ #include "mapGenerator/Map.h" #include "mapGenerator/RandomUtility.h" +#include #include #include @@ -74,6 +75,32 @@ Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings); +/** + * Selects the nodes of new mountains just like the overload above but asks for the size of each mountain right before + * growing it. This allows callers to vary the size, e.g. depending on what the nodes are going to be used for. + * + * @param map map to search for free land (is not modified) + * @param rnd random number generator + * @param mountainLevel minimum z-value of mountains + * @param minFreeLand land up to this distance to water or mountains stays free + * @param getSize returns the desired number of nodes for the mountain with the specified index + * + * @returns the nodes of each new mountain. All mountains are disjoint. + */ +std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, + const std::function& getSize); + +/** + * Computes the distance of each node of the mountain to the foot of the mountain. The foot consists of all nodes of the + * mountain with at least one neighbor which isn't part of the mountain. + * + * @param size size of the map + * @param mountain nodes of the mountain + * + * @returns the distance to the foot for each node of the mountain and zero for all other nodes. + */ +NodeMapBase GetFootDistances(const MapExtent& size, const Mountain& mountain); + /** * Elevates the nodes of the mountain relative to the surrounding land and applies mountain textures to them. * diff --git a/libs/s25main/mapGenerator/RandomMap.cpp b/libs/s25main/mapGenerator/RandomMap.cpp index d40bfc8dba..6d491a621b 100644 --- a/libs/s25main/mapGenerator/RandomMap.cpp +++ b/libs/s25main/mapGenerator/RandomMap.cpp @@ -3,11 +3,13 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "mapGenerator/RandomMap.h" +#include "mapGenerator/FreeLand.h" #include "mapGenerator/Harbors.h" #include "mapGenerator/HeadQuarters.h" #include "mapGenerator/Islands.h" #include "mapGenerator/Mountains.h" #include "mapGenerator/Resources.h" +#include "mapGenerator/SpecialObjects.h" #include "mapGenerator/Terrain.h" #include "mapGenerator/TextureHelper.h" #include "mapGenerator/Textures.h" @@ -163,6 +165,9 @@ void RandomMap::Create(const MapSettings& settings) case MapStyle::Land: CreateLandMap(); break; } + if(settings_.specialObjects) + AddSpecialScenes(map_, rnd_); + AddObjects(map_, rnd_, settings_); AddResources(map_, rnd_, settings_); AddAnimals(map_, rnd_); @@ -203,17 +208,38 @@ void RandomMap::CreateFreeIslands(unsigned waterNodes) } } -void RandomMap::AddMountains(unsigned mountainLevel) +void RandomMap::FillFreeLand(unsigned mountainLevel) { - if(settings_.mountains == 0) + const LandFillAmounts amounts{settings_.mountains, settings_.desert, settings_.swamp}; + const unsigned totalAmount = settings_.mountains + settings_.desert + settings_.swamp; + if(totalAmount == 0) return; - const auto freeLand = GetMinFreeLand(static_cast(settings_.mountainDistance), settings_.mountains); + const auto freeLand = GetMinFreeLand(static_cast(settings_.mountainDistance), totalAmount); const auto minFreeLand = rnd_.RandomValue(freeLand, freeLand + 5u); const auto maxMountainSize = GetMaxMountainSize(map_.size); const auto maxSize = rnd_.RandomValue(maxMountainSize / 2, maxMountainSize); - CreateMountains(map_, rnd_, mountainLevel, MountainSettings{minFreeLand, maxSize, maxMountainSize / 4}); + rttr::mapGenerator::FillFreeLand(map_, rnd_, mountainLevel, + MountainSettings{minFreeLand, maxSize, maxMountainSize / 4}, amounts); +} + +void RandomMap::AddTextures(unsigned mountainLevel) +{ + const NodeMapBase textures = map_.getTextures(); + + texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); + + for(const MapPoint& pt : map_.barren) + { + for(const Triangle& triangle : GetTriangles(pt, map_.size)) + { + const TexturePair& pair = textures[triangle.position]; + map_.textureMap.Set(triangle, triangle.rsu ? pair.rsu : pair.lsd); + } + } + + SmoothDesertTransitions(map_); } void RandomMap::CreateMixedMap() @@ -240,9 +266,9 @@ void RandomMap::CreateMixedMap() CreateFreeIslands(waterNodes); - AddMountains(mountainLevel); + FillFreeLand(mountainLevel); - texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); + AddTextures(mountainLevel); PlaceHarbors(map_, rivers); PlaceHeadquarters(map_, rnd_, map_.players, settings_.mountainDistance); @@ -286,9 +312,9 @@ void RandomMap::CreateWaterMap() const auto rivers = CreateRivers(center); - AddMountains(mountainLevel); + FillFreeLand(mountainLevel); - texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); + AddTextures(mountainLevel); PlaceHarbors(map_, rivers); @@ -312,9 +338,9 @@ void RandomMap::CreateLandMap() const auto mountainLevel = LimitFor(map_.z, land, static_cast(1)) + 1; CreateRivers(); - AddMountains(mountainLevel); + FillFreeLand(mountainLevel); - texturizer_.AddTextures(mountainLevel, GetCoastline(map_.size)); + AddTextures(mountainLevel); PlaceHeadquarters(map_, rnd_, map_.players, settings_.mountainDistance); } diff --git a/libs/s25main/mapGenerator/RandomMap.h b/libs/s25main/mapGenerator/RandomMap.h index 47d4c88e32..01c5c26703 100644 --- a/libs/s25main/mapGenerator/RandomMap.h +++ b/libs/s25main/mapGenerator/RandomMap.h @@ -23,8 +23,9 @@ unsigned GetMaxMountainSize(const MapExtent& size); * Computes the amount of free land to keep around water and mountains. * * @param mountainDistance desired distance of headquarters to mountains - * @param mountains desired amount of mountains in percentage (between 0 and 100) - the default of 50 keeps the desired - * distance available while higher values reduce the free land down to the space required by headquarters + * @param mountains desired amount of mountains, deserts and swamps in percentage (between 0 and 100) - an amount of + * 50 keeps the desired distance available while higher values reduce the free land down to the space required by + * headquarters * * @returns the distance to water and mountains up to which land stays free. */ @@ -45,7 +46,8 @@ class RandomMap std::vector CreateRivers(MapPoint source = MapPoint::Invalid()); void CreateFreeIslands(unsigned waterNodes); - void AddMountains(unsigned mountainLevel); + void FillFreeLand(unsigned mountainLevel); + void AddTextures(unsigned mountainLevel); void CreateMixedMap(); void CreateLandMap(); void CreateWaterMap(); diff --git a/libs/s25main/mapGenerator/Resources.cpp b/libs/s25main/mapGenerator/Resources.cpp index a46bd94573..fb163b89bd 100644 --- a/libs/s25main/mapGenerator/Resources.cpp +++ b/libs/s25main/mapGenerator/Resources.cpp @@ -66,12 +66,22 @@ void AddObjects(Map& map, RandomUtility& rnd, const MapSettings& settings) { auto suroundingArea = map.getTextures().GetPointsInRadiusWithCenter(pt, 5); excludedArea.insert(suroundingArea.begin(), suroundingArea.end()); + // Remove the decoration of deserts and swamps as well + for(const MapPoint& excluded : suroundingArea) + { + map.objectTypes[excluded] = libsiedler2::OT_Empty; + map.objectInfos[excluded] = libsiedler2::OI_Empty; + } } else if(map.textureMap.Any(pt, IsSnowOrLava)) { excludedArea.insert(pt); } } + // Deserts and swamps come with their own objects + excludedArea.insert(map.barren.begin(), map.barren.end()); + excludedArea.insert(map.reserved.begin(), map.reserved.end()); + const auto distanceToExcludedArea = DistancesTo(excludedArea, map.size); // 1) compute maximum water distance until mountain area @@ -140,6 +150,10 @@ void AddObjects(Map& map, RandomUtility& rnd, const MapSettings& settings) RTTR_FOREACH_PT(MapPoint, map.size) { + // Keep objects which are already part of the landscape + if(map.objectTypes[pt] != libsiedler2::OT_Empty) + continue; + const bool canPlaceStonePile = waterDistance[pt] > 0 && distanceToExcludedArea[pt] > 0; if(canPlaceStonePile && rnd.ByChance(settings.stonePiles)) { @@ -233,7 +247,7 @@ void AddResources(Map& map, RandomUtility& rnd, const MapSettings& settings) } } } - } else if(textures.All(pt, IsWater)) + } else if(textures.All(pt, IsWater) && !helpers::contains(map.barren, pt)) { resources[pt] = libsiedler2::R_Fish; } else @@ -251,7 +265,7 @@ void AddAnimals(Map& map, RandomUtility& rnd) const auto& textures = map.textureMap; RTTR_FOREACH_PT(MapPoint, map.size) { - if(rnd.ByChance(3)) + if(rnd.ByChance(3) && !helpers::contains(map.barren, pt)) { if(textures.All(pt, IsWater)) { diff --git a/libs/s25main/mapGenerator/SpecialObjects.cpp b/libs/s25main/mapGenerator/SpecialObjects.cpp new file mode 100644 index 0000000000..b1e393fba7 --- /dev/null +++ b/libs/s25main/mapGenerator/SpecialObjects.cpp @@ -0,0 +1,886 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "mapGenerator/SpecialObjects.h" +#include "helpers/EnumRange.h" +#include "helpers/containerUtils.h" +#include "helpers/mathFuncs.h" +#include "mapGenerator/Algorithms.h" +#include "mapGenerator/FreeLand.h" +#include "mapGenerator/Resources.h" +#include "mapGenerator/TextureHelper.h" +#include "gameTypes/Direction.h" +#include "libsiedler2/enumTypes.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace rttr::mapGenerator { + +namespace { + /// Minimum distance of the center of a scene to headquarters and harbors. Objects close to them are removed. + constexpr unsigned minPlayerDistance = sceneRadius + 6; + /// Radius of the ground in the middle of a scene which is leveled before hills are added + constexpr unsigned levelRadius = 3; + constexpr double pi = 3.14159265358979323846; + /// Texture indices which are the same for all landscapes + constexpr uint8_t flowerTextureId = 0x0F; // meadow covered by flowers + constexpr uint8_t rockTextureId = 0x22; // flat rock + + using Objects = std::vector; + const Objects mushrooms{0x00, 0x01, 0x22}; + const Objects stones{0x02, 0x03, 0x04}; + const Objects pebbles{0x25, 0x26, 0x27}; + const Objects deadTrees{OI_DeadTreeTrunk, OI_DeadTree, 0x1F, 0x20}; + const Objects bones{OI_Bone1, OI_Bone2}; + const Objects flowers{0x09}; + const Objects bushes{0x0A, 0x10, 0x11, 0x12, 0x28, 0x29, 0x2A}; + const Objects shrubs{0x0E, 0x0F, 0x13, 0x14}; + const Objects standingStones{0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E}; + const Objects cacti{0x0C, 0x0D}; + /// Bushes and shrubs of all kinds for hedges and thickets + const Objects undergrowth{0x0A, 0x10, 0x11, 0x12, 0x28, 0x29, 0x2A, 0x0E, 0x0F, 0x13, 0x14}; + /// Mostly flowers mixed with small plants and mushrooms for colorful beds and drifts + const Objects plants{0x09, 0x09, 0x09, 0x09, 0x09, 0x2A, 0x29, 0x0E, 0x13, 0x14, 0x00, 0x01, 0x22}; + /// Landscape index of the winter world which has no cacti + constexpr uint8_t winterLandscapeId = 2; + + /// Ground of a patch - either blended into the surrounding land or painted on top of it + enum class Ground + { + None, + /// Driest land which looks like trampled ground + Dry, + /// Meadow covered by flowers + Lush, + /// Flat rock with a rim of mountain meadow + Rock + }; + + /// Textures painted on top of the land in ascending priority + enum class Overlay : uint8_t + { + None, + RockRim, + Rock, + Sand, + Water + }; + + /// Objects and ground making up the character of a type of scene + struct Theme + { + /// Special object in the center + Objects main; + /// Larger objects used for clusters and along paths + Objects props; + /// Small objects scattered on the ground + Objects litter; + /// Ground right around the special object + Ground core; + /// Ground of the wider surroundings + Ground surround; + bool hill; + bool flowers; + bool granite; + bool deadVegetation; + }; + + Objects Join(Objects lhs, const Objects& rhs) + { + lhs.insert(lhs.end(), rhs.begin(), rhs.end()); + return lhs; + } + + Theme MakeTheme(SceneType type, bool isWinter) + { + // Dry ground gets cacti unless it's frozen + const Objects dryPlants = isWinter ? pebbles : Join(pebbles, cacti); + + switch(type) + { + case SceneType::CastleRuin: + return {{OI_CastleRuin}, stones, pebbles, Ground::Rock, Ground::Lush, true, true, true, false}; + case SceneType::RuinGrove: + return {{OI_TowerRuin, OI_GuardhouseRuin}, + stones, + mushrooms, + Ground::Lush, + Ground::Lush, + true, + true, + false, + false}; + case SceneType::Graveyard: + return {{OI_Cross}, {OI_Cross}, Join(bones, mushrooms), Ground::Dry, Ground::Dry, false, false, + false, true}; + case SceneType::Camp: + return {{OI_Tent}, dryPlants, dryPlants, Ground::Dry, Ground::None, false, true, false, false}; + case SceneType::StoneCircle: + return {{OI_Gate, OI_OpenGate}, + standingStones, + mushrooms, + Ground::Rock, + Ground::Lush, + true, + true, + false, + false}; + } + throw std::invalid_argument("invalid type of scene"); + } + + /// Area of irregular shape around a node + struct Blob + { + MapPoint center; + double radius; + /// Phases of the waves deforming the outline - a circle if amplitude is zero + double amplitude, phase1, phase2; + }; + + /// Helper which collects the ground, the hills and the objects of a scene + class SceneBuilder + { + Map& map_; + RandomUtility& rnd_; + const MapPoint center_; + const Theme theme_; + /// Two kinds of trees - the first one dominates + const Tree tree_, otherTree_; + /// Buildable land textures from dry to lush used for smooth transitions + std::vector> ramp_; + /// Nodes with a desired index into the ramp in the order they were requested + std::vector> ground_; + /// Requests for the ground around the special object which take precedence over all others + std::vector> coreGround_; + NodeMapBase overlay_; + /// Nodes which have to stay free from objects + std::set blocked_; + + static unsigned Mod6(int dir) { return static_cast((dir % 6 + 6) % 6); } + + std::pair Planar(const MapPoint& from, const MapPoint& to) const + { + const auto delta = [](int value, int size) { + if(value > size / 2) + return value - size; + return value < -size / 2 ? value + size : value; + }; + const int dx = delta(static_cast(to.x) - from.x, map_.size.x); + const int dy = delta(static_cast(to.y) - from.y, map_.size.y); + // Odd rows are shifted to the right by half a node + return {dx + 0.5 * ((to.y & 1) - (from.y & 1)), dy * 0.866}; + } + + /// Index into the ramp of the land texture of the node + int RampIndex(const MapPoint& pt) const + { + const auto it = std::find(ramp_.begin(), ramp_.end(), map_.getTextures()[pt].rsu); + return it != ramp_.end() ? static_cast(it - ramp_.begin()) : static_cast(ramp_.size() / 2); + } + + int Target(Ground ground) const { return ground == Ground::Dry ? 0 : static_cast(ramp_.size()) - 1; } + + DescIdx OverlayTexture(Overlay overlay) const + { + const auto isRock = [](const TerrainDesc& t) { + return t.Is(ETerrain::Buildable) && t.s2Id == rockTextureId; + }; + switch(overlay) + { + case Overlay::RockRim: return map_.textureMap.Find(IsBuildableMountain); + case Overlay::Rock: return map_.textureMap.Find(isRock); + case Overlay::Sand: return map_.textureMap.Find(IsCoastTerrain); + default: return map_.textureMap.Find(IsShipableWater); + } + } + + public: + SceneBuilder(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type) + : map_(map), rnd_(rnd), center_(center), + theme_(MakeTheme(type, map.textureMap.GetLandscapeId() == winterLandscapeId)), + tree_(rnd.RandomItem(CreateTrees(map.textureMap))), + otherTree_(rnd.RandomItem(CreateTrees(map.textureMap))) + { + const auto area = map.z.GetPointsInRadiusWithCenter(center, sceneRadius); + map.reserved.insert(area.begin(), area.end()); + overlay_.Resize(map.size, Overlay::None); + + // Flowers are the most lush texture. The index makes the order independent from the sort algorithm. + ramp_ = map.textureMap.FindAll(IsBuildableLand); + map.textureMap.Sort(ramp_, [](const TerrainDesc& terrain) { + return std::make_tuple(terrain.humidity, terrain.s2Id == flowerTextureId, terrain.s2Id); + }); + + // Level the middle of the scene and reduce the slopes towards the surrounding land + const int level = map.z[center]; + for(const MapPoint& pt : area) + { + const int tolerance = std::max(0, static_cast(Distance(pt)) - static_cast(levelRadius)); + map.z[pt] = + static_cast(helpers::clamp(map.z[pt], std::max(level - tolerance, MinZ()), + std::min(level + tolerance, map.height.maximum))); + } + } + + const MapPoint& Center() const { return center_; } + const Theme& GetTheme() const { return theme_; } + RandomUtility& Rnd() { return rnd_; } + int MinZ() const { return map_.height.minimum + 1; } + unsigned Distance(const MapPoint& pt) const { return map_.z.CalcDistance(center_, pt); } + unsigned Distance(const MapPoint& from, const MapPoint& to) const { return map_.z.CalcDistance(from, to); } + bool IsWater(const MapPoint& pt) const { return overlay_[pt] == Overlay::Water; } + + /// Moves from the node into the direction which is counted clockwise starting in the west + MapPoint Step(MapPoint pt, int dir, unsigned steps = 1) const + { + for(unsigned i = 0; i < steps; i++) + pt = map_.z.GetNeighbour(pt, Direction(Mod6(dir))); + return pt; + } + + /// All nodes with exactly the specified distance to the center in circular order starting in the west + std::vector Ring(unsigned radius) const + { + std::vector ring; + MapPoint pt = Step(center_, 0, radius); + for(int dir = 2; dir < 8; dir++) + { + for(unsigned i = 0; i < radius; i++) + { + ring.push_back(pt); + pt = Step(pt, dir); + } + } + return ring; + } + + std::vector Area(const MapPoint& pt, unsigned radius) const + { + auto area = map_.z.GetPointsInRadiusWithCenter(pt, radius); + helpers::erase_if(area, [this](const MapPoint& p) { return Distance(p) > sceneRadius; }); + return area; + } + + double Angle(const MapPoint& from, const MapPoint& to) const + { + const auto planar = Planar(from, to); + return std::atan2(planar.second, planar.first); + } + + double RandomAngle() { return rnd_.RandomDouble(-pi, pi); } + + Blob MakeBlob(const MapPoint& center, double radius, bool irregular = true) + { + return Blob{center, radius, irregular ? 1. : 0., RandomAngle(), RandomAngle()}; + } + + /// Relative position within a blob: 0 at the center, 1 at the outline and above 1 outside + double Eval(const Blob& blob, const MapPoint& pt) const + { + const double angle = Angle(blob.center, pt); + const double deformation = + 0.22 * std::sin(2 * angle + blob.phase1) + 0.13 * std::sin(3 * angle + blob.phase2); + return Distance(blob.center, pt) / (blob.radius * (1. + blob.amplitude * deformation)); + } + + std::vector Nodes(const Blob& blob, double limit = 1.) const + { + auto nodes = Area(blob.center, static_cast(blob.radius * 1.4) + 1); + helpers::erase_if(nodes, [this, &blob, limit](const MapPoint& pt) { return Eval(blob, pt) >= limit; }); + return nodes; + } + + /// Requests the ground for the blob. Later requests take precedence where grounds meet - except for the + /// ground around the special object which always wins. + void Blend(const Blob& blob, Ground ground, bool isCore = false) + { + if(ground == Ground::None) + return; + if(ground == Ground::Rock) + { + // Rock never touches the land directly but is enclosed by a rim + // Only the ground of the special object itself reaches the middle of the scene + const auto isAllowed = [this, isCore](const MapPoint& pt) { return isCore || Distance(pt) > 1; }; + for(const MapPoint& pt : Nodes(blob, 0.75)) + { + if(!isAllowed(pt)) + continue; + SetOverlay(pt, Overlay::Rock); + for(const MapPoint& neighbor : Area(pt, 1)) + { + if(isAllowed(neighbor)) + SetOverlay(neighbor, Overlay::RockRim); + } + } + return; + } + + // Only the inner part gets the ground - the transition to the surrounding land is added in the end + for(const MapPoint& pt : Nodes(blob, 0.75)) + (isCore ? coreGround_ : ground_).emplace_back(pt, Target(ground)); + } + + void SetOverlay(const MapPoint& pt, Overlay overlay) + { + if(Distance(pt) < sceneRadius - 1 && overlay > overlay_[pt]) + overlay_[pt] = overlay; + } + + /// Raises (or lowers) a smooth hill around the node + void Bump(const MapPoint& top, int height, double radius) + { + for(const MapPoint& pt : Area(top, static_cast(radius))) + { + const double position = Distance(top, pt) / radius; + if(position >= 1.) + continue; + const int elevation = static_cast(std::lround(height * 0.5 * (1. + std::cos(pi * position)))); + map_.z[pt] = static_cast(std::clamp(map_.z[pt] + elevation, MinZ(), map_.height.maximum)); + } + } + + void Block(const MapPoint& pt) { blocked_.insert(pt); } + + bool CanPlace(const MapPoint& pt) const + { + return Distance(pt) <= sceneRadius && map_.objectTypes[pt] == libsiedler2::OT_Empty + && !helpers::contains(blocked_, pt) && !IsWater(pt); + } + + bool Chance(unsigned chance) { return chance >= 100 || rnd_.ByChance(chance); } + + void Place(const MapPoint& pt, const Objects& objects) + { + if(!CanPlace(pt)) + return; + map_.objectTypes[pt] = OT_Decoration; + map_.objectInfos[pt] = rnd_.RandomItem(objects); + } + + void PlaceTree(const MapPoint& pt) + { + if(!CanPlace(pt)) + return; + const Tree& tree = rnd_.ByChance(75) ? tree_ : otherTree_; + map_.objectTypes[pt] = tree.type; + map_.objectInfos[pt] = tree.index + rnd_.RandomValue(0, 7); + } + + void PlaceGranite(const MapPoint& pt) + { + if(!CanPlace(pt)) + return; + map_.objectTypes[pt] = rnd_.ByChance(50) ? libsiedler2::OT_Stone1 : libsiedler2::OT_Stone2; + map_.objectInfos[pt] = static_cast(rnd_.RandomValue(2, 6)); + } + + /// Trees mixed with some undergrowth or dead trees for gloomy themes + void PlaceVegetation(const MapPoint& pt) + { + if(theme_.deadVegetation) + Place(pt, rnd_.ByChance(80) ? deadTrees : mushrooms); + else if(rnd_.ByChance(85)) + PlaceTree(pt); + else + Place(pt, undergrowth); + } + + /// Trees of a single kind for rows or dead trees for gloomy themes + void PlaceRowTree(const MapPoint& pt) + { + if(theme_.deadVegetation) + Place(pt, deadTrees); + else if(CanPlace(pt)) + { + map_.objectTypes[pt] = tree_.type; + map_.objectInfos[pt] = tree_.index + rnd_.RandomValue(0, 7); + } + } + + /// Large rocks or the props of the theme + void PlaceRock(const MapPoint& pt) + { + if(theme_.granite && rnd_.ByChance(60)) + PlaceGranite(pt); + else + Place(pt, theme_.granite ? stones : theme_.props); + } + + /// Flowers mixed with other small plants or the litter of the theme + void PlaceGroundCover(const MapPoint& pt) { Place(pt, theme_.flowers ? plants : theme_.litter); } + + /// Calls the function by chance for the nodes of a sector around the center + template + void ForSector(double angle, double halfWidth, unsigned minDistance, unsigned maxDistance, unsigned chance, + T_Place&& place) + { + for(const MapPoint& pt : Area(center_, maxDistance)) + { + if(Distance(pt) < minDistance) + continue; + double difference = std::fabs(Angle(center_, pt) - angle); + difference = std::min(difference, 2 * pi - difference); + if(difference <= halfWidth && Chance(chance)) + place(pt); + } + } + + /// Calls the function by chance for the nodes of a blob + template + void ForBlob(const Blob& blob, double limit, unsigned chance, T_Place&& place) + { + for(const MapPoint& pt : Nodes(blob, limit)) + { + if(Chance(chance)) + place(pt); + } + } + + /// Places the special object and the objects which make the type of scene recognizable in any style + void PlaceCore(SceneType type) + { + map_.objectTypes[center_] = OT_Decoration; + map_.objectInfos[center_] = rnd_.RandomItem(theme_.main); + Block(center_); + const auto innerRing = Ring(1); + const auto ring = Ring(2); + + switch(type) + { + case SceneType::CastleRuin: + // The ruin covers some of its neighbors + for(const int dir : {0, 1, 2}) + Block(Step(center_, dir)); + break; + case SceneType::Graveyard: + // Three rows of graves + for(const unsigned index : {0u, 3u}) + Place(innerRing[index], theme_.main); + for(const unsigned index : {2u, 3u, 4u, 8u, 9u, 10u}) + Place(ring[index], theme_.main); + break; + case SceneType::Camp: Place(innerRing[rnd_.Index(innerRing.size())], {OI_PileOfWood}); break; + case SceneType::StoneCircle: + for(unsigned i = 0; i < ring.size(); i += 2) + Place(ring[i], standingStones); + break; + default: break; + } + } + + /// Applies the collected ground to the map and makes sure the special object stands on even ground + void Finish() + { + FlattenForCastleBuilding(map_.z, center_); + + // Ponds are flat + const auto area = map_.z.GetPointsInRadiusWithCenter(center_, sceneRadius); + int waterLevel = map_.height.maximum; + for(const MapPoint& pt : area) + { + if(IsWater(pt)) + waterLevel = std::min(waterLevel, map_.z[pt]); + } + for(const MapPoint& pt : area) + { + if(IsWater(pt)) + map_.z[pt] = static_cast(waterLevel); + } + + ground_.insert(ground_.end(), coreGround_.begin(), coreGround_.end()); + PaintGround(area); + for(const Overlay overlay : {Overlay::RockRim, Overlay::Rock, Overlay::Sand, Overlay::Water}) + { + for(const MapPoint& pt : area) + { + if(overlay_[pt] == overlay) + map_.textureMap.Set(pt, OverlayTexture(overlay)); + } + } + } + + private: + /** + * Paints the requested grounds and adds transitions: the textures of neighboring nodes never differ by more + * than one step of the ramp from dry to lush land. A patch of dry ground within meadow is therefore surrounded + * by one ring for each texture in between - just like coasts. Sand counts as one step below the driest land. + * The land around the scene, the shores and later requests keep their texture where requests are in conflict. + */ + void PaintGround(const std::vector& area) + { + struct Anchor + { + MapPoint pt; + int index; + bool isBorder = false; + }; + std::vector anchors; + // All kinds of meadow look alike next to dry land: the land around the scene only asks for the + // transitions up to the first of them. Otherwise shores close to the border would get none at all. + const int firstMeadow = std::min(2, static_cast(ramp_.size()) - 1); + const auto limit = [this, &anchors, firstMeadow](const MapPoint& pt, int index) { + int lower = -100; + int upper = 100; + for(const Anchor& anchor : anchors) + { + const int distance = static_cast(Distance(anchor.pt, pt)); + lower = std::max(lower, + (anchor.isBorder ? std::min(anchor.index, firstMeadow) : anchor.index) - distance); + upper = std::min(upper, anchor.index + distance); + } + // Anchors may contradict each other if the land around the scene has no transitions itself + return lower > upper ? (lower + upper) / 2 : std::clamp(index, lower, upper); + }; + + std::set visited; + std::vector painted; + const auto addAnchor = [&](const MapPoint& pt, int index, bool paint) { + if(!visited.insert(pt).second) + return; + const Anchor anchor{pt, limit(pt, index)}; + if(paint && anchor.index != RampIndex(pt)) + painted.push_back(anchor); + anchors.push_back(anchor); + }; + + // The land right outside the scene is not painted + for(const MapPoint& pt : Ring(sceneRadius + 1)) + { + visited.insert(pt); + anchors.push_back(Anchor{pt, RampIndex(pt), true}); + } + for(const MapPoint& pt : area) + { + if(overlay_[pt] == Overlay::Sand || overlay_[pt] == Overlay::Water) + addAnchor(pt, -1, false); + } + for(auto it = ground_.rbegin(); it != ground_.rend(); ++it) + { + if(Distance(it->first) <= sceneRadius) + addAnchor(it->first, it->second, true); + } + // All other nodes keep their texture unless a transition is required + for(const MapPoint& pt : area) + { + if(visited.count(pt)) + continue; + const int index = limit(pt, RampIndex(pt)); + if(index != RampIndex(pt)) + painted.push_back(Anchor{pt, index}); + } + + // Neighboring nodes share triangles: the texture which differs most from the land is painted last. Thus + // patches keep their size and each transition gets a row of triangles. + std::vector> order; + for(const Anchor& anchor : painted) + order.emplace_back(std::abs(anchor.index - RampIndex(anchor.pt)), anchor); + std::stable_sort(order.begin(), order.end(), + [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; }); + for(const auto& entry : order) + { + const int index = std::clamp(entry.second.index, 0, static_cast(ramp_.size()) - 1); + map_.textureMap.Set(entry.second.pt, ramp_[index]); + } + } + }; + + /// Irregular patch with a cluster of rocks, a clump of trees and a drift of flowers in different directions + void CreateOvergrown(SceneBuilder& scene) + { + const Theme& theme = scene.GetTheme(); + const MapPoint center = scene.Center(); + + scene.Blend(scene.MakeBlob(center, 5.2), theme.surround); + scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 2.0 : 2.8), theme.core, true); + if(theme.hill) + scene.Bump(center, 2, 5.); + + // A path leads through the gap between the rocks and the trees + const int pathDir = static_cast(scene.Rnd().RandomValue(0, 5)); + for(unsigned i = 1; i < sceneRadius; i++) + { + const MapPoint pt = scene.Step(scene.Step(center, pathDir, i), pathDir + 1, i / 3); + scene.Block(pt); + if(theme.surround != Ground::Dry && i > 1) + scene.Blend(scene.MakeBlob(pt, 1.7, false), Ground::Dry); + } + + const double pathAngle = scene.Angle(center, scene.Step(center, pathDir, 3)); + const double rockAngle = pathAngle + scene.Rnd().RandomDouble(1.2, 1.9); + const double treeAngle = pathAngle - scene.Rnd().RandomDouble(1.2, 1.9); + const auto wrap = [](double angle) { return std::atan2(std::sin(angle), std::cos(angle)); }; + + scene.ForSector(wrap(rockAngle), 0.65, 2, 5, 55, [&scene](const MapPoint& pt) { scene.PlaceRock(pt); }); + scene.ForSector(wrap(treeAngle), 0.8, 3, sceneRadius, 70, + [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); + scene.ForSector(wrap(pathAngle + pi), 0.9, 2, 5, 50, + [&scene](const MapPoint& pt) { scene.PlaceGroundCover(pt); }); + scene.ForSector(0., pi, 2, 6, 7, [&scene](const MapPoint& pt) { scene.Place(pt, undergrowth); }); + scene.ForSector(0., pi, 1, 4, 10, [&scene, &theme](const MapPoint& pt) { scene.Place(pt, theme.litter); }); + } + + /// Straight road between two rows of trees leading to a court which is enclosed by a hedge + void CreateAvenue(SceneBuilder& scene) + { + const Theme& theme = scene.GetTheme(); + const MapPoint center = scene.Center(); + const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); + const Ground paving = theme.core == Ground::Lush ? Ground::Dry : theme.core; + + scene.Blend(scene.MakeBlob(center, 4.6), theme.surround); + scene.Blend(scene.MakeBlob(center, paving == Ground::Rock ? 2.9 : 3.1, false), paving, true); + scene.Bump(center, theme.hill ? 2 : 1, 5.); + + std::vector road; + for(unsigned i = 1; i <= sceneRadius; i++) + { + road.push_back(scene.Step(center, dir, i)); + scene.Block(road.back()); + if(paving == Ground::Rock) + scene.SetOverlay(road.back(), Overlay::RockRim); + else + scene.Blend(scene.MakeBlob(road.back(), 1.1, false), Ground::Dry); + } + const auto nearRoad = [&scene, &road](const MapPoint& pt) { + return helpers::contains_if(road, [&](const MapPoint& node) { return scene.Distance(node, pt) <= 1; }); + }; + + // Trees on both sides of the road outside the court + for(unsigned i = 3; i < road.size(); i++) + { + scene.PlaceRowTree(scene.Step(road[i], dir + 2)); + scene.PlaceRowTree(scene.Step(road[i], dir + 4)); + } + + // Beds within the court and a hedge around it + for(const MapPoint& pt : scene.Ring(2)) + { + if(!nearRoad(pt)) + scene.PlaceGroundCover(pt); + } + const auto hedge = scene.Ring(3); + for(unsigned i = 0; i < hedge.size(); i++) + { + if(nearRoad(hedge[i])) + continue; + if(theme.deadVegetation) + { + if(i % 2 == 0) + scene.Place(hedge[i], deadTrees); + } else + scene.Place(hedge[i], undergrowth); + } + // Rocks or props mark the corners + const auto outerRing = scene.Ring(4); + for(unsigned i = 0; i < outerRing.size(); i += 4) + { + if(!nearRoad(outerRing[i])) + scene.PlaceRock(outerRing[i]); + } + } + + /// Rocky hill behind the special object, a pond in front of it and flowers and trees on its sides + void CreateCrag(SceneBuilder& scene) + { + const Theme& theme = scene.GetTheme(); + const MapPoint center = scene.Center(); + const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); + const int side = scene.Rnd().ByChance(50) ? 2 : 4; + const MapPoint crag = scene.Step(center, dir, 3); + const MapPoint pond = scene.Step(center, dir + 3, 3); + + scene.Blend(scene.MakeBlob(center, 3.2), theme.surround); + scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 1.6 : 2.2), theme.core, true); + + const Blob cragBlob = scene.MakeBlob(crag, 2.9); + scene.Blend(cragBlob, Ground::Rock); + scene.Bump(crag, 4, 4.4); + scene.ForBlob(cragBlob, 0.95, 55, [&scene](const MapPoint& pt) { + if(scene.Rnd().ByChance(65)) + scene.PlaceGranite(pt); + else + scene.Place(pt, stones); + }); + scene.ForBlob(scene.MakeBlob(scene.Step(center, dir, 6), 2.2), 1., 60, + [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); + + // Pond with a sandy shore and reed at its far side. It stays close to the middle of the scene to leave space + // for the transition from sand to the surrounding land. + scene.Bump(pond, -1, 3.); + std::vector water{pond, scene.Step(pond, dir + 3)}; + for(const MapPoint& pt : scene.Area(pond, 1)) + { + if(scene.Distance(pt) >= 3 && scene.Rnd().ByChance(60) && !helpers::contains(water, pt)) + water.push_back(pt); + } + for(const MapPoint& pt : water) + { + scene.SetOverlay(pt, Overlay::Water); + scene.Block(pt); + } + for(const MapPoint& pt : water) + { + for(const MapPoint& neighbor : scene.Area(pt, 1)) + { + if(scene.IsWater(neighbor)) + continue; + scene.SetOverlay(neighbor, Overlay::Sand); + if(scene.Distance(neighbor) > scene.Distance(pond) && scene.Rnd().ByChance(50)) + scene.Place(neighbor, shrubs); + } + } + + const Blob drift = scene.MakeBlob(scene.Step(center, dir + side, 4), 2.5); + scene.Blend(drift, Ground::Lush); + scene.ForBlob(drift, 0.85, 55, [&scene, &theme](const MapPoint& pt) { + if(theme.flowers) + scene.Place(pt, plants); + else + scene.Place(pt, theme.props); + }); + scene.ForBlob(scene.MakeBlob(scene.Step(center, dir - side, 5), 2.1), 1., 65, + [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); + scene.ForSector(0., pi, 1, 3, 12, [&scene, &theme](const MapPoint& pt) { scene.Place(pt, theme.litter); }); + } + + /// Curved trail lined by remains which leads to the special object + void CreateTrail(SceneBuilder& scene) + { + const Theme& theme = scene.GetTheme(); + const MapPoint center = scene.Center(); + const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); + const int turn = scene.Rnd().ByChance(50) ? 1 : -1; + + scene.Blend(scene.MakeBlob(center, 3.4), theme.surround); + scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 1.9 : 2.6), theme.core, true); + if(theme.hill) + scene.Bump(center, 2, 4.5); + + MapPoint pt = center; + std::vector> trail; + for(const int bend : {0, 0, 1, 1, 1, 2, 2}) + { + pt = scene.Step(pt, dir + turn * bend); + trail.emplace_back(pt, dir + turn * bend); + scene.Block(pt); + } + for(const auto& node : trail) + scene.Blend(scene.MakeBlob(node.first, 1.7, false), Ground::Dry); + + // Remains alternate on both sides of the trail + for(unsigned i = 1; i < trail.size(); i++) + { + const int left = trail[i].second + 2; + const int right = trail[i].second + 4; + scene.Place(scene.Step(trail[i].first, i % 2 ? left : right), theme.props); + if(scene.Rnd().ByChance(45)) + scene.Place(scene.Step(trail[i].first, i % 2 ? right : left), theme.litter); + } + scene.PlaceRock(scene.Step(trail.back().first, trail.back().second + 1)); + + // Clumps of trees behind the special object and inside of the bend + scene.ForBlob(scene.MakeBlob(scene.Step(center, dir + 3, 4), 2.4), 1., 65, + [&scene](const MapPoint& p) { scene.PlaceVegetation(p); }); + scene.ForBlob(scene.MakeBlob(scene.Step(trail[3].first, trail[3].second + turn * 2, 3), 1.9), 1., 60, + [&scene](const MapPoint& p) { scene.PlaceVegetation(p); }); + scene.ForSector(0., pi, 1, 3, 30, [&scene](const MapPoint& p) { scene.PlaceGroundCover(p); }); + scene.ForSector(0., pi, 2, 6, 8, [&scene](const MapPoint& p) { scene.Place(p, undergrowth); }); + } +} // namespace + +bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo) +{ + if(objectType != OT_Decoration) + return false; + return objectInfo == OI_Gate || objectInfo == OI_OpenGate || objectInfo == OI_Tent + || objectInfo == OI_GuardhouseRuin || objectInfo == OI_TowerRuin || objectInfo == OI_CastleRuin + || objectInfo == OI_Cross; +} + +unsigned GetNumSpecialScenes(const MapExtent& size) +{ + return helpers::clamp((size.x + size.y) / 96u, 1u, 6u); +} + +std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigned number) +{ + const auto isUnsuitable = [&map](const MapPoint& pt) { + return !map.textureMap.All(pt, IsBuildableLand) || map.objectTypes[pt] != libsiedler2::OT_Empty + || helpers::contains(map.barren, pt) || helpers::contains(map.reserved, pt); + }; + const auto obstacleDistance = DistancesTo(map.size, isUnsuitable); + + std::vector players = map.hqPositions; + for(const Triangle& harbor : map.harbors) + players.push_back(harbor.position); + const auto playerDistance = DistancesTo(players, map.size); + + // One node more than required leaves a gap between the scene and its surroundings + auto candidates = SelectPoints( + [&](const MapPoint& pt) { + return obstacleDistance[pt] > sceneRadius + 1 && playerDistance[pt] >= minPlayerDistance; + }, + map.size); + + const unsigned minSceneDistance = std::max(4 * sceneRadius, (map.size.x + map.size.y) / 16u); + std::vector sites; + + while(sites.size() < number && !candidates.empty()) + { + const MapPoint site = rnd.RandomItem(candidates); + sites.push_back(site); + helpers::erase_if(candidates, [&map, &site, minSceneDistance](const MapPoint& pt) { + return map.z.CalcDistance(site, pt) < minSceneDistance; + }); + } + + return sites; +} + +void CreateScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style) +{ + SceneBuilder scene(map, rnd, center, type); + scene.PlaceCore(type); + + switch(style) + { + case SceneStyle::Overgrown: CreateOvergrown(scene); break; + case SceneStyle::Avenue: CreateAvenue(scene); break; + case SceneStyle::Crag: CreateCrag(scene); break; + case SceneStyle::Trail: CreateTrail(scene); break; + } + + scene.Finish(); +} + +std::vector AddSpecialScenes(Map& map, RandomUtility& rnd) +{ + // Each type is used once before any type is repeated + std::vector types; + for(const SceneType type : helpers::EnumRange()) + types.push_back(type); + for(size_t i = types.size(); i > 1; i--) + std::swap(types[i - 1], types[rnd.Index(i)]); + + std::vector scenes; + for(const MapPoint& site : FindSceneSites(map, rnd, GetNumSpecialScenes(map.size))) + { + const SceneType type = types[scenes.size() % types.size()]; + const auto style = + static_cast(rnd.RandomValue(0u, static_cast(maxEnumValue(SceneStyle{})))); + CreateScene(map, rnd, site, type, style); + scenes.push_back(Scene{type, style, site}); + } + + return scenes; +} + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/SpecialObjects.h b/libs/s25main/mapGenerator/SpecialObjects.h new file mode 100644 index 0000000000..3dc6ff08f4 --- /dev/null +++ b/libs/s25main/mapGenerator/SpecialObjects.h @@ -0,0 +1,119 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "mapGenerator/Map.h" +#include "mapGenerator/RandomUtility.h" +#include + +namespace rttr::mapGenerator { + +/// Themes of sceneries built around a special object +enum class SceneType +{ + /// Castle ruin on rocky ground between flowers and rocks + CastleRuin, + /// Ruin of a tower or guardhouse between flowers and trees + RuinGrove, + /// Rows of crosses on barren ground between dead trees and bones + Graveyard, + /// Single tent with a pile of wood on trampled ground + Camp, + /// Gate within a circle of standing stones + StoneCircle +}; +constexpr auto maxEnumValue(SceneType) +{ + return SceneType::StoneCircle; +} + +/// Layouts arranging the surroundings of a special object +enum class SceneStyle +{ + /// Irregular patch with a cluster of rocks, a clump of trees and a drift of flowers in different directions + Overgrown, + /// Straight road between two rows of trees leading to a court which is enclosed by a hedge + Avenue, + /// Rocky hill behind the special object, a pond in front of it and flowers and trees on its sides + Crag, + /// Curved trail lined by remains which leads to the special object + Trail +}; +constexpr auto maxEnumValue(SceneStyle) +{ + return SceneStyle::Trail; +} + +struct Scene +{ + SceneType type; + SceneStyle style; + MapPoint center; +}; + +/// Radius around the center of a scene which is covered by the scene +constexpr unsigned sceneRadius = 7; + +/// Indices of special objects (object type OT_Decoration) +constexpr uint8_t OI_Gate = 0x16; +constexpr uint8_t OI_OpenGate = 0x17; +constexpr uint8_t OI_Tent = 0x2C; +constexpr uint8_t OI_GuardhouseRuin = 0x2D; +constexpr uint8_t OI_TowerRuin = 0x2E; +constexpr uint8_t OI_CastleRuin = 0x2F; +constexpr uint8_t OI_Cross = 0x30; +constexpr uint8_t OI_PileOfWood = 0x32; + +/// Checks whether the object is the centerpiece of a scene +bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo); + +/** + * Computes the number of special scenes for a map. Only a few scenes are placed to keep them special. + * + * @param size size of the map + * + * @returns the desired number of scenes which grows with the size of the map. + */ +unsigned GetNumSpecialScenes(const MapExtent& size); + +/** + * Searches for places for special scenes. A scene requires buildable land without any objects and keeps a distance to + * headquarters, harbors and other scenes. + * + * @param map map to search for places (is not modified) + * @param rnd random number generator + * @param number desired number of scenes + * + * @returns the center of each scene - less than desired if the map doesn't provide enough suitable places. + */ +std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigned number); + +/** + * Creates a scene around the specified center. The theme defines the special object, the objects around it and the + * ground while the style defines how they are arranged. The ground blends into the surrounding land using intermediate + * textures and hills rise smoothly. The nodes of the scene are reserved to keep them free from random trees and stone + * piles. + * + * @param map reference to the map to place the scene on (manipulates textures, objects and z-values of the map) + * @param rnd random number generator + * @param center center of the scene which gets the special object + * @param type theme of the scene + * @param style layout of the scene + */ +void CreateScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style); + +/** + * Places a few special scenes on the map. The types of the scenes are mixed up and each scene gets a random + * style. This function has to be called once + * textures, headquarters and harbors are in place but before any other objects are added. + * + * @param map reference to the map to place the scenes on (manipulates textures, objects and z-values of the map) + * @param rnd random number generator + * + * @returns the type and center of each new scene. + */ +std::vector AddSpecialScenes(Map& map, RandomUtility& rnd); + +} // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/TextureHelper.cpp b/libs/s25main/mapGenerator/TextureHelper.cpp index bdce06bca0..eebdb560f3 100644 --- a/libs/s25main/mapGenerator/TextureHelper.cpp +++ b/libs/s25main/mapGenerator/TextureHelper.cpp @@ -21,6 +21,11 @@ bool IsSwamp(const TerrainDesc& terrain) return !terrain.Is(ETerrain::Walkable) && terrain.humidity > 0; } +bool IsImpassable(const TerrainDesc& terrain) +{ + return terrain.flags == ETerrain::Unwalkable; +} + bool IsLand(const TerrainDesc& terrain) { return terrain.kind == TerrainKind::Land; diff --git a/libs/s25main/mapGenerator/TextureHelper.h b/libs/s25main/mapGenerator/TextureHelper.h index 29c7cf9940..6afa3be79d 100644 --- a/libs/s25main/mapGenerator/TextureHelper.h +++ b/libs/s25main/mapGenerator/TextureHelper.h @@ -14,6 +14,10 @@ bool IsWater(const TerrainDesc& terrain); bool IsSwamp(const TerrainDesc& terrain); +/// Terrain which can neither be entered by settlers nor by ships. The first match of each landscape is the swamp or its +/// equivalent (stony lava, ice floes). +bool IsImpassable(const TerrainDesc& terrain); + bool IsLand(const TerrainDesc& terrain); bool IsShipableWater(const TerrainDesc& terrain); diff --git a/tests/s25Main/UI/testIngameWindow.cpp b/tests/s25Main/UI/testIngameWindow.cpp index 7e46635ce5..e77e1d1db5 100644 --- a/tests/s25Main/UI/testIngameWindow.cpp +++ b/tests/s25Main/UI/testIngameWindow.cpp @@ -9,6 +9,7 @@ #include "Settings.h" #include "WindowManager.h" #include "controls/ctrlButton.h" +#include "controls/ctrlCheck.h" #include "controls/ctrlComboBox.h" #include "controls/ctrlEdit.h" #include "controls/ctrlOptionGroup.h" @@ -82,7 +83,10 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) const auto expectedRivers = rttr::test::randomValue(0u, 100u); const auto expectedTrees = rttr::test::randomValue(0u, 100u); const auto expectedMountains = rttr::test::randomValue(0u, 100u); + const auto expectedDesert = rttr::test::randomValue(0u, 100u); + const auto expectedSwamp = rttr::test::randomValue(0u, 100u); const auto expectedStonePiles = rttr::test::randomValue(0u, 100u); + const bool expectedSpecialObjects = rttr::test::randomValue(0u, 1u) == 1u; rttr::mapGenerator::MapSettings settings; iwMapGenerator wnd(settings); @@ -119,8 +123,14 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) wnd.GetCtrl(iwMapGenerator::ID_pgTrees)->SetPosition(expectedTrees); BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgMountains)->GetPosition() == settings.mountains); wnd.GetCtrl(iwMapGenerator::ID_pgMountains)->SetPosition(expectedMountains); + BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgDesert)->GetPosition() == settings.desert); + wnd.GetCtrl(iwMapGenerator::ID_pgDesert)->SetPosition(expectedDesert); + BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgSwamp)->GetPosition() == settings.swamp); + wnd.GetCtrl(iwMapGenerator::ID_pgSwamp)->SetPosition(expectedSwamp); BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_pgStonePiles)->GetPosition() == settings.stonePiles); wnd.GetCtrl(iwMapGenerator::ID_pgStonePiles)->SetPosition(expectedStonePiles); + BOOST_TEST(wnd.GetCtrl(iwMapGenerator::ID_cbSpecialObjects)->isChecked() == settings.specialObjects); + wnd.GetCtrl(iwMapGenerator::ID_cbSpecialObjects)->setChecked(expectedSpecialObjects); wnd.Msg_ButtonClick(iwMapGenerator::ID_btApply); BOOST_TEST(wnd.ShouldBeClosed()); @@ -137,7 +147,10 @@ BOOST_AUTO_TEST_CASE(IwMapGenerator) BOOST_TEST(settings.rivers == expectedRivers); BOOST_TEST(settings.trees == expectedTrees); BOOST_TEST(settings.mountains == expectedMountains); + BOOST_TEST(settings.desert == expectedDesert); + BOOST_TEST(settings.swamp == expectedSwamp); BOOST_TEST(settings.stonePiles == expectedStonePiles); + BOOST_TEST(settings.specialObjects == expectedSpecialObjects); } BOOST_AUTO_TEST_CASE(ConnectWindow) diff --git a/tests/s25Main/mapGenerator/testFreeLand.cpp b/tests/s25Main/mapGenerator/testFreeLand.cpp new file mode 100644 index 0000000000..50eae5d27b --- /dev/null +++ b/tests/s25Main/mapGenerator/testFreeLand.cpp @@ -0,0 +1,719 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "PointOutput.h" +#include "mapGenFixtures.h" +#include "mapGenerator/Algorithms.h" +#include "mapGenerator/FreeLand.h" +#include "mapGenerator/Resources.h" +#include "mapGenerator/TextureHelper.h" +#include "libsiedler2/enumTypes.h" +#include +#include + +using namespace rttr::mapGenerator; + +namespace { +constexpr uint8_t landHeight = 10; +constexpr unsigned mountainLevel = 30; +constexpr MountainSettings settings{3, 100, 25}; +constexpr unsigned numLandscapes = 3; + +unsigned idx(LandFill type) +{ + return static_cast(type); +} + +struct FreeLandFixture : MapGenFixture +{ + /// Creates a map covered by land except for a single row of water + Map createLandMap(const MapExtent size) const + { + Map map = createMap(size); + map.z.Resize(size, landHeight); + RTTR_FOREACH_PT(MapPoint, size) + { + if(pt.y == 0) + map.z[pt] = map.height.minimum; + } + return map; + } + + /// Creates a map with a slope from the west to the center of the map + Map createSlopedMap(const MapExtent size) const + { + Map map = createMap(size); + map.z.Resize(size, landHeight); + RTTR_FOREACH_PT(MapPoint, size) + { + map.z[pt] = static_cast(landHeight + std::min(pt.x, size.x - pt.x)); + } + return map; + } + + static Mountain createArea(const Map& map, const MapPoint& center, unsigned radius) + { + const auto points = map.z.GetPointsInRadiusWithCenter(center, radius); + return Mountain(points.begin(), points.end()); + } +}; + +template +unsigned countObjects(const Map& map, T_Predicate&& predicate) +{ + unsigned count = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] != libsiedler2::OT_Empty && predicate(pt)) + count++; + } + return count; +} +} // namespace + +BOOST_FIXTURE_TEST_SUITE(FreeLandTests, FreeLandFixture) + +BOOST_AUTO_TEST_CASE(SelectLandFill_returns_mountain_without_any_amounts) +{ + RandomUtility rnd(0); + + BOOST_TEST((SelectLandFill(rnd, {0, 0, 0}, {0, 0, 0}) == LandFill::Mountain)); +} + +BOOST_AUTO_TEST_CASE(SelectLandFill_starts_with_mountain_if_desired) +{ + for(unsigned seed = 0; seed < 20; seed++) + { + RandomUtility rnd(seed); + BOOST_TEST_REQUIRE((SelectLandFill(rnd, {1, 100, 100}, {0, 0, 0}) == LandFill::Mountain)); + } +} + +BOOST_AUTO_TEST_CASE(SelectLandFill_returns_only_desired_types) +{ + RandomUtility rnd(0); + LandFillAmounts assigned{}; + + for(unsigned i = 0; i < 50; i++) + { + const LandFill type = SelectLandFill(rnd, {0, 30, 0}, assigned); + BOOST_TEST_REQUIRE((type == LandFill::Desert)); + assigned[idx(type)] += 10; + } +} + +BOOST_AUTO_TEST_CASE(SelectLandFill_keeps_proportions_of_amounts) +{ + const LandFillAmounts amounts{50, 30, 20}; + + for(unsigned seed = 0; seed < 10; seed++) + { + RandomUtility rnd(seed); + LandFillAmounts assigned{}; + + // Even a low number of areas of decreasing size has to match the proportions roughly + unsigned total = 0; + for(unsigned size = 100; size > 40; size -= 5) + { + assigned[idx(SelectLandFill(rnd, amounts, assigned))] += size; + total += size; + } + + for(unsigned i = 0; i < numLandFills; i++) + { + const int share = static_cast(assigned[i] * 100 / total); + BOOST_TEST_REQUIRE(std::abs(share - static_cast(amounts[i])) <= 15, "Type " << i << ": " << share); + } + } +} + +BOOST_AUTO_TEST_CASE(SelectLandFill_varies_with_seed) +{ + const LandFillAmounts amounts{0, 50, 50}; + unsigned deserts = 0; + + for(unsigned seed = 0; seed < 50; seed++) + { + RandomUtility rnd(seed); + if(SelectLandFill(rnd, amounts, {0, 0, 0}) == LandFill::Desert) + deserts++; + } + + BOOST_TEST(deserts > 0u); + BOOST_TEST(deserts < 50u); +} + +BOOST_AUTO_TEST_CASE(Swamp_and_desert_textures_exist_for_all_landscapes) +{ + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + const Map map = createMap(MapExtent(8, 8)); + + const auto swamp = map.textureMap.Find(IsImpassable); + const auto desert = map.textureMap.Find(IsCoastTerrain); + + // The swamp and its equivalents share the same index in all landscapes + BOOST_TEST(map.textureMap.GetTextureId(swamp) == 3u); + BOOST_TEST(!worldDesc.get(swamp).Is(ETerrain::Walkable)); + BOOST_TEST(!worldDesc.get(swamp).Is(ETerrain::Shippable)); + BOOST_TEST(worldDesc.get(desert).Is(ETerrain::Walkable)); + BOOST_TEST(!worldDesc.get(desert).Is(ETerrain::Buildable)); + BOOST_TEST(map.textureMap.FindAll(IsBuildableCoast).size() >= 2u); + } +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_is_lower_than_mountain_and_keeps_surroundings) +{ + Map hill = createLandMap(MapExtent(48, 48)); + Map mountain = createLandMap(MapExtent(48, 48)); + const MapPoint center(24, 24); + const Mountain area = createArea(hill, center, 10); + RandomUtility rnd(0); + + RaiseDesertHill(hill, rnd, area); + RaiseMountain(mountain, rnd, area); + + BOOST_TEST(hill.z[center] > landHeight); + BOOST_TEST(hill.z[center] < mountain.z[center]); + RTTR_FOREACH_PT(MapPoint, hill.size) + { + if(!helpers::contains(area, pt)) + BOOST_TEST_REQUIRE(hill.z[pt] == (pt.y == 0 ? hill.height.minimum : landHeight), "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_rises_smoothly) +{ + Map map = createLandMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(24, 24), 10); + RandomUtility rnd(0); + + RaiseDesertHill(map, rnd, area); + + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(pt.y <= 1 || pt.y + 1 >= map.size.y) // skip the water + continue; + for(const MapPoint& neighbor : map.z.GetNeighbours(pt)) + { + const int difference = static_cast(map.z[pt]) - static_cast(map.z[neighbor]); + BOOST_TEST_REQUIRE(std::abs(difference) <= 1, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_applies_transitions_from_buildable_land_to_desert) +{ + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + Map map = createLandMap(MapExtent(48, 48)); + const MapPoint center(24, 24); + const Mountain area = createArea(map, center, 8); + const auto footDistance = GetFootDistances(map.size, area); + RandomUtility rnd(0); + + RaiseDesertHill(map, rnd, area); + + auto transitions = map.textureMap.FindAll(IsBuildableCoast); + map.textureMap.Sort(transitions, ByHumidity); + const auto count = [&map](DescIdx texture) { + return helpers::count_if(map.getTextures(), [texture](const TexturePair& pair) { + return pair.rsu == texture || pair.lsd == texture; + }); + }; + + for(const MapPoint& pt : area) + { + if(footDistance[pt] < 2) + { + BOOST_TEST_REQUIRE(map.textureMap.Any(pt, IsBuildableLand), "At point " << pt); + BOOST_TEST_REQUIRE(!helpers::contains(map.barren, pt), "At point " << pt); + } else + { + BOOST_TEST_REQUIRE(map.textureMap.All(pt, IsCoastTerrain), "At point " << pt); + BOOST_TEST_REQUIRE(helpers::contains(map.barren, pt), "At point " << pt); + } + } + // The two driest buildable textures surround the desert + BOOST_TEST(count(transitions[0]) > 0); + BOOST_TEST(count(transitions[1]) > 0); + BOOST_TEST(count(transitions[2]) == 0); + BOOST_TEST(map.textureMap.All(center, IsCoastTerrain)); + } +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_places_bones_around_center_only) +{ + for(unsigned seed = 0; seed < 10; seed++) + { + Map map = createLandMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(24, 24), 9); + const auto footDistance = GetFootDistances(map.size, area); + const unsigned maxDistance = GetMaximum(footDistance, area); + RandomUtility rnd(seed); + + RaiseDesertHill(map, rnd, area); + + BOOST_TEST_REQUIRE(countObjects(map, [](const MapPoint&) { return true; }) > 0u); + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] == libsiedler2::OT_Empty) + continue; + BOOST_TEST_REQUIRE(map.objectTypes[pt] == OT_Decoration, "At point " << pt); + BOOST_TEST_REQUIRE((map.objectInfos[pt] == OI_Bone1 || map.objectInfos[pt] == OI_Bone2), "At point " << pt); + BOOST_TEST_REQUIRE(helpers::contains(area, pt), "At point " << pt); + BOOST_TEST_REQUIRE(footDistance[pt] * 3 >= maxDistance * 2, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_supports_tiny_areas) +{ + Map map = createLandMap(MapExtent(16, 16)); + const MapPoint center(8, 8); + RandomUtility rnd(0); + + RaiseDesertHill(map, rnd, Mountain{center}); + RaiseDesertHill(map, rnd, Mountain{}); + + BOOST_TEST(map.textureMap.All(center, IsCoastTerrain)); + BOOST_TEST(map.objectTypes[center] == OT_Decoration); +} + +BOOST_AUTO_TEST_CASE(RaiseDesertHill_does_not_exceed_maximum_height) +{ + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, map.height.maximum - 1); + RandomUtility rnd(0); + + RaiseDesertHill(map, rnd, createArea(map, MapPoint(16, 16), 8)); + + RTTR_FOREACH_PT(MapPoint, map.size) + { + BOOST_TEST_REQUIRE(map.z[pt] <= map.height.maximum, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_is_entirely_flat_and_impassable) +{ + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + Map map = createSlopedMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(18, 24), 6); + const uint8_t lowest = map.z[MapPoint(12, 24)]; + RandomUtility rnd(0); + + CreateSwamp(map, rnd, area); + + for(const MapPoint& pt : area) + { + BOOST_TEST_REQUIRE(map.z[pt] == lowest, "At point " << pt); + BOOST_TEST_REQUIRE(map.textureMap.All(pt, IsImpassable), "At point " << pt); + BOOST_TEST_REQUIRE(helpers::contains(map.barren, pt), "At point " << pt); + } + BOOST_TEST(map.barren.size() == area.size()); + } +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_lowers_shore_towards_swamp) +{ + Map map = createSlopedMap(MapExtent(48, 48)); + const Map original = createSlopedMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(18, 24), 6); + const uint8_t level = original.z[MapPoint(12, 24)]; + const auto distances = DistancesTo(area, map.size); + RandomUtility rnd(0); + + CreateSwamp(map, rnd, area); + + unsigned lowered = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(distances[pt] == 0) + continue; + // The land never rises by more than one level per node - no matter how far away from the swamp + BOOST_TEST_REQUIRE(map.z[pt] == std::min(original.z[pt], level + distances[pt]), "At point " << pt); + if(map.z[pt] < original.z[pt]) + lowered++; + } + BOOST_TEST(lowered > 0u); +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_avoids_steep_slopes_on_steep_land) +{ + Map map = createMap(MapExtent(48, 48)); + map.z.Resize(map.size, landHeight); + RTTR_FOREACH_PT(MapPoint, map.size) + { + // Ridge in the middle of the map which is three times as steep as the shore of a swamp + map.z[pt] = static_cast(landHeight + 3 * std::min(pt.x, map.size.x - pt.x)); + } + const Mountain area = createArea(map, MapPoint(16, 24), 5); + RandomUtility rnd(0); + + CreateSwamp(map, rnd, area); + + for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(MapPoint(16, 24), 10)) + { + // The land above the swamp is lowered while the land below keeps its slope + if(pt.x < 16) + continue; + for(const MapPoint& neighbor : map.z.GetNeighbours(pt)) + { + if(neighbor.x <= pt.x) // look uphill only + continue; + const int difference = static_cast(map.z[neighbor]) - static_cast(map.z[pt]); + BOOST_TEST_REQUIRE(difference <= 1, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_never_lowers_banks_below_rivers) +{ + // Rivers follow the height of the land instead of the sea level + Map map = createSlopedMap(MapExtent(48, 48)); + const auto water = map.textureMap.Find(IsWater); + std::vector river; + for(unsigned y = 0; y < map.size.y; y++) + { + river.push_back(MapPoint(27, y)); + map.textureMap.Set(river.back(), water); + } + const Map original = map; + const auto riverDistances = DistancesTo(river, map.size); + RandomUtility rnd(0); + + // The river passes the upper side of the swamp at a distance of three nodes + const Mountain area = createArea(map, MapPoint(18, 24), 6); + CreateSwamp(map, rnd, area); + + unsigned lowered = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(helpers::contains(area, pt)) + continue; + if(riverDistances[pt] <= 1) + BOOST_TEST_REQUIRE(map.z[pt] == original.z[pt], "At point " << pt); + else + { + // The land is lowered by one more level with each node further away from the river + BOOST_TEST_REQUIRE(original.z[pt] - map.z[pt] < static_cast(riverDistances[pt]), "At point " << pt); + if(map.z[pt] < original.z[pt]) + lowered++; + } + } + BOOST_TEST(lowered > 0u); +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_keeps_shape_of_other_landscapes) +{ + Map map = createSlopedMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(18, 24), 6); + const MapPoint mountain(26, 24); + const uint8_t height = map.z[mountain]; + map.textureMap.Set(mountain, map.textureMap.Find(IsMinableMountain)); + RandomUtility rnd(0); + + CreateSwamp(map, rnd, area); + + BOOST_TEST(map.z[mountain] == height); +} + +BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_separates_desert_from_lush_land) +{ + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + Map map = createLandMap(MapExtent(48, 48)); + auto ramp = map.textureMap.FindAll(IsBuildableLand); + map.textureMap.Sort(ramp, ByHumidity); + const auto lush = ramp.back(); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, lush); + } + const MapPoint center(24, 24); + const Mountain area = createArea(map, center, 6); + RandomUtility rnd(0); + RaiseDesertHill(map, rnd, area); + // Texturing coasts may replace the transitions of the hill + for(const MapPoint& pt : area) + { + if(!helpers::contains(map.barren, pt)) + map.textureMap.Set(pt, lush); + } + for(const MapPoint& pt : map.barren) + map.textureMap.Set(pt, map.textureMap.Find(IsCoastTerrain)); + + SmoothDesertTransitions(map); + + const auto humidityOf = [this, &map](const Triangle& triangle) { + return worldDesc.get(map.textureMap.Get(triangle)).humidity; + }; + RTTR_FOREACH_PT(MapPoint, map.size) + { + uint8_t driest = 100; + uint8_t wettest = 0; + for(const Triangle& triangle : GetTriangles(pt, map.size)) + { + driest = std::min(driest, humidityOf(triangle)); + wettest = std::max(wettest, humidityOf(triangle)); + } + // Desert (0) meets steppe (30) only, steppe meets savannah (60) and savannah meets meadow (95) + BOOST_TEST_REQUIRE((driest > 0 || wettest <= 30), "At point " << pt); + BOOST_TEST_REQUIRE((driest > 30 || wettest <= 60), "At point " << pt); + BOOST_TEST_REQUIRE((driest > 60 || wettest <= 95), "At point " << pt); + } + BOOST_TEST(map.textureMap.All(center, IsCoastTerrain)); + for(const MapPoint& pt : map.barren) + BOOST_TEST_REQUIRE(map.textureMap.All(pt, IsCoastTerrain), "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_keeps_maps_without_deserts) +{ + Map map = createLandMap(MapExtent(32, 32)); + const auto land = map.textureMap.Find(IsBuildableLand); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, land); + } + RandomUtility rnd(0); + CreateSwamp(map, rnd, createArea(map, MapPoint(16, 16), 4)); + const auto before = map.getTextures(); + + SmoothDesertTransitions(map); + + RTTR_FOREACH_PT(MapPoint, map.size) + { + BOOST_TEST_REQUIRE((map.getTextures()[pt].rsu == before[pt].rsu), "At point " << pt); + BOOST_TEST_REQUIRE((map.getTextures()[pt].lsd == before[pt].lsd), "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(CreateSwamp_places_dead_trees_on_border_to_free_land_only) +{ + Map map = createLandMap(MapExtent(48, 48)); + const Mountain area = createArea(map, MapPoint(24, 24), 6); + const auto distances = DistancesTo(area, map.size); + RandomUtility rnd(0); + + CreateSwamp(map, rnd, area); + + BOOST_TEST_REQUIRE(countObjects(map, [](const MapPoint&) { return true; }) > 0u); + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] == libsiedler2::OT_Empty) + continue; + BOOST_TEST_REQUIRE(map.objectTypes[pt] == OT_Decoration, "At point " << pt); + BOOST_TEST_REQUIRE((map.objectInfos[pt] == OI_DeadTree || map.objectInfos[pt] == OI_DeadTreeTrunk), + "At point " << pt); + BOOST_TEST_REQUIRE(distances[pt] == 1u, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(CloseGaps_adds_enclosed_nodes_only) +{ + const Map map = createLandMap(MapExtent(32, 32)); + const MapPoint center(16, 16); + const Mountain closed = createArea(map, center, 5); + Mountain area = closed; + area.erase(center); + area.erase(MapPoint(13, 15)); + + BOOST_TEST((CloseGaps(map.size, area) == closed)); + BOOST_TEST((CloseGaps(map.size, closed) == closed)); +} + +BOOST_AUTO_TEST_CASE(FillFreeLand_creates_all_desired_types_on_disjoint_areas) +{ + Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + const auto areas = FillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50}); + + BOOST_TEST_REQUIRE(areas.size() >= 3u); + BOOST_TEST((areas.front().type == LandFill::Mountain)); + + Mountain visited; + std::array count{}; + for(const FilledArea& area : areas) + { + count[idx(area.type)]++; + for(const MapPoint& pt : area.nodes) + { + BOOST_TEST_REQUIRE(visited.insert(pt).second, "At point " << pt); + } + } + BOOST_TEST(count[idx(LandFill::Mountain)] > 0u); + BOOST_TEST(count[idx(LandFill::Desert)] > 0u); + BOOST_TEST(count[idx(LandFill::Swamp)] > 0u); +} + +BOOST_AUTO_TEST_CASE(FillFreeLand_applies_textures_matching_the_type) +{ + Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + for(const FilledArea& area : FillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50})) + { + const auto all = [&map, &area](bool (*predicate)(const TerrainDesc&)) { + return std::all_of(area.nodes.begin(), area.nodes.end(), + [&](const MapPoint& pt) { return map.textureMap.All(pt, predicate); }); + }; + const auto any = [&map, &area](bool (*predicate)(const TerrainDesc&)) { + return std::any_of(area.nodes.begin(), area.nodes.end(), + [&](const MapPoint& pt) { return map.textureMap.All(pt, predicate); }); + }; + + switch(area.type) + { + case LandFill::Mountain: BOOST_TEST(all(IsMinableMountain)); break; + case LandFill::Desert: BOOST_TEST(any(IsCoastTerrain)); break; + case LandFill::Swamp: BOOST_TEST(all(IsImpassable)); break; + } + } +} + +BOOST_AUTO_TEST_CASE(FillFreeLand_creates_only_mountains_by_default) +{ + Map map = createLandMap(MapExtent(64, 64)); + Map expected = createLandMap(MapExtent(64, 64)); + RandomUtility rnd1(7); + RandomUtility rnd2(7); + + const auto areas = FillFreeLand(map, rnd1, mountainLevel, settings, {50, 0, 0}); + const auto mountains = CreateMountains(expected, rnd2, mountainLevel, settings); + + BOOST_TEST_REQUIRE(areas.size() == mountains.size()); + for(unsigned i = 0; i < areas.size(); i++) + { + BOOST_TEST_REQUIRE((areas[i].type == LandFill::Mountain)); + BOOST_TEST_REQUIRE((areas[i].nodes == mountains[i]), "Area " << i); + } + RTTR_FOREACH_PT(MapPoint, map.size) + { + BOOST_TEST_REQUIRE(map.z[pt] == expected.z[pt], "At point " << pt); + } + BOOST_TEST(map.barren.empty()); +} + +BOOST_AUTO_TEST_CASE(FillFreeLand_limits_swamps_to_half_the_size) +{ + Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + const auto areas = FillFreeLand(map, rnd, mountainLevel, settings, {0, 0, 50}); + + BOOST_TEST_REQUIRE(!areas.empty()); + for(const FilledArea& area : areas) + { + BOOST_TEST_REQUIRE((area.type == LandFill::Swamp)); + } + // Closing gaps adds a few nodes to the desired size + BOOST_TEST(areas.front().nodes.size() >= settings.maxSize / 2); + BOOST_TEST(areas.front().nodes.size() < settings.maxSize * 3 / 4); +} + +BOOST_AUTO_TEST_CASE(FillFreeLand_leaves_no_free_land_beyond_limit) +{ + Map map = createLandMap(MapExtent(64, 64)); + RandomUtility rnd(0); + + FillFreeLand(map, rnd, mountainLevel, settings, {20, 40, 40}); + + const auto distances = GetFreeLandDistances(map, mountainLevel); + BOOST_TEST(*std::max_element(distances.begin(), distances.end()) <= settings.minFreeLand); +} + +BOOST_AUTO_TEST_CASE(AddObjects_keeps_decoration_and_places_nothing_on_barren_nodes) +{ + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, landHeight); + const auto land = map.textureMap.Find(IsBuildableLand); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, land); + } + const Mountain swamp = createArea(map, MapPoint(8, 16), 4); + const Mountain hill = createArea(map, MapPoint(22, 16), 5); + RandomUtility rnd(0); + CreateSwamp(map, rnd, swamp); + RaiseDesertHill(map, rnd, hill); + const auto typesBefore = map.objectTypes; + const auto infosBefore = map.objectInfos; + + MapSettings mapSettings; + mapSettings.trees = 100; + mapSettings.stonePiles = 20; + AddObjects(map, rnd, mapSettings); + + BOOST_TEST_REQUIRE(countObjects(map, [](const MapPoint&) { return true; }) > 100u); + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(typesBefore[pt] != libsiedler2::OT_Empty) + { + BOOST_TEST_REQUIRE(map.objectTypes[pt] == typesBefore[pt], "At point " << pt); + BOOST_TEST_REQUIRE(map.objectInfos[pt] == infosBefore[pt], "At point " << pt); + } else if(helpers::contains(map.barren, pt)) + { + BOOST_TEST_REQUIRE(map.objectTypes[pt] == libsiedler2::OT_Empty, "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(AddObjects_removes_decoration_around_headquarters) +{ + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, landHeight); + const auto land = map.textureMap.Find(IsBuildableLand); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, land); + } + const MapPoint hq(16, 16); + RandomUtility rnd(0); + CreateSwamp(map, rnd, createArea(map, MapPoint(9, 16), 4)); + map.hqPositions.push_back(hq); + + AddObjects(map, rnd, MapSettings()); + + for(const MapPoint& pt : map.z.GetPointsInRadius(hq, 5)) + { + BOOST_TEST_REQUIRE(map.objectTypes[pt] == libsiedler2::OT_Empty, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(AddResources_and_AddAnimals_skip_swamps_made_of_water) +{ + // Winter world uses ice floes (water) as equivalent for swamps + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + Map map = createMap(MapExtent(32, 32)); + map.z.Resize(map.size, landHeight); + const auto land = map.textureMap.Find(IsBuildableLand); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, land); + } + const Mountain swamp = createArea(map, MapPoint(16, 16), 8); + RandomUtility rnd(0); + CreateSwamp(map, rnd, swamp); + + AddResources(map, rnd, MapSettings()); + AddAnimals(map, rnd); + + for(const MapPoint& pt : swamp) + { + BOOST_TEST_REQUIRE(map.resources[pt] != libsiedler2::R_Fish, "At point " << pt); + BOOST_TEST_REQUIRE((map.animals[pt] == libsiedler2::Animal::None), "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index a3997d73e6..2f344fcf33 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -7,6 +7,7 @@ #include "lua/GameDataLoader.h" #include "mapGenerator/Algorithms.h" #include "mapGenerator/RandomMap.h" +#include "mapGenerator/SpecialObjects.h" #include "mapGenerator/TextureHelper.h" #include "gameData/MaxPlayers.h" #include "libsiedler2/ArchivItem_Map.h" @@ -57,7 +58,10 @@ static void ValidateMap(const Map& map, const MapExtent& size, unsigned players) for(unsigned index = 0; index < players; index++) { BOOST_TEST_REQUIRE(map.hqPositions[index].isValid()); - BOOST_TEST_CONTEXT("Player " << index) { validateHqPos(map.hqPositions[index]); } + BOOST_TEST_CONTEXT("Player " << index) + { + validateHqPos(map.hqPositions[index]); + } } } @@ -154,11 +158,9 @@ BOOST_AUTO_TEST_CASE(GetMaxMountainSize_grows_with_map_size) BOOST_TEST(GetMaxMountainSize(MapExtent(512, 512)) < GetMaxMountainSize(MapExtent(1024, 1024))); } -BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_mountain_distance_for_default_amount) +BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_mountain_distance_for_medium_amount) { - const MapSettings settings; - - BOOST_TEST(GetMinFreeLand(15, settings.mountains) == 15u); + BOOST_TEST(GetMinFreeLand(15, 50) == 15u); } BOOST_AUTO_TEST_CASE(GetMinFreeLand_decreases_with_amount_of_mountains) @@ -232,8 +234,10 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) settings.size = MapExtent(256, 256); settings.mountainDistance = MountainDistance::Close; - // Maximum of the randomized free land to keep plus the transition textures added at the foot of mountains - const unsigned maxFreeLand = static_cast(settings.mountainDistance) + 5 + 3; + // Maximum of the randomized free land to keep plus the transition textures added at the foot of mountains and + // desert hills + const unsigned amount = settings.mountains + settings.desert + settings.swamp; + const unsigned maxFreeLand = GetMinFreeLand(static_cast(settings.mountainDistance), amount) + 5 + 3; for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed}) { @@ -242,7 +246,9 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) const Map map = GenerateRandomMap(rnd, worldDesc, settings); const auto distances = DistancesTo(map.size, [&map](const MapPoint& pt) { - return map.textureMap.Any(pt, [](const TerrainDesc& t) { return IsWater(t) || IsMountainOrSnowOrLava(t); }); + return helpers::contains(map.barren, pt) || map.textureMap.Any(pt, [](const TerrainDesc& t) { + return IsWater(t) || IsMountainOrSnowOrLava(t); + }); }); BOOST_TEST(*std::max_element(distances.begin(), distances.end()) <= maxFreeLand); @@ -272,6 +278,162 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_maps_for_all_styles_and_mou } } +BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_no_deserts_and_swamps_if_disabled) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + settings.size = MapExtent(256, 256); + settings.mountains = 50; + settings.desert = 0; + settings.swamp = 0; + RandomUtility rnd(0); + + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + + BOOST_TEST(map.barren.empty()); +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_deserts_and_swamps_for_all_landscapes) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(256, 256); + settings.style = MapStyle::Land; + settings.mountainDistance = MountainDistance::Close; + settings.desert = 50; + settings.swamp = 50; + + for(unsigned landscape = 0; landscape < worldDesc.landscapes.size(); landscape++) + { + settings.type = DescIdx(landscape); + RandomUtility rnd(0); + + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + ValidateMap(map, settings.size, settings.numPlayers); + + const auto count = [&map](bool (*predicate)(const TerrainDesc&)) { + return helpers::count_if(map.barren, [&](const MapPoint& pt) { return map.textureMap.All(pt, predicate); }); + }; + BOOST_TEST_CONTEXT("Landscape " << landscape) + { + BOOST_TEST(count(IsImpassable) > 0); + BOOST_TEST(count(IsCoastTerrain) > 0); + BOOST_TEST(helpers::count(map.objectTypes, 0xC8) > 0); + for(const MapPoint& pt : map.barren) + { + if(!map.textureMap.All(pt, IsImpassable)) + continue; + // Swamps are flat and neither used for fishing nor for harbors + BOOST_TEST_REQUIRE(map.resources[pt] != libsiedler2::R_Fish, "At point " << pt); + for(const MapPoint& neighbor : map.z.GetNeighbours(pt)) + { + if(helpers::contains(map.barren, neighbor)) + BOOST_TEST_REQUIRE(map.z[pt] == map.z[neighbor], "At point " << pt); + } + } + for(const Triangle& harbor : map.harbors) + { + for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(harbor.position, 2)) + BOOST_TEST_REQUIRE(!helpers::contains(map.barren, pt), "At point " << pt); + } + } + } +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_maps_with_deserts_and_swamps_for_all_styles) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(128, 128); + settings.mountains = 100; + settings.desert = 100; + settings.swamp = 100; + + for(unsigned landscape = 0; landscape < worldDesc.landscapes.size(); landscape++) + { + for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed, MapStyle::Water}) + { + for(const unsigned numPlayers : {2u, MAX_PLAYERS}) + { + settings.type = DescIdx(landscape); + settings.style = style; + settings.numPlayers = numPlayers; + RandomUtility rnd(1); + + Map map = GenerateRandomMap(rnd, worldDesc, settings); + ValidateMap(map, settings.size, settings.numPlayers); + } + } + } +} + +static unsigned countSpecialObjects(const Map& map) +{ + unsigned count = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(IsSpecialObject(map.objectTypes[pt], map.objectInfos[pt])) + count++; + } + return count; +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_no_special_objects_if_disabled) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + settings.size = MapExtent(128, 128); + settings.specialObjects = false; + RandomUtility rnd(0); + + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + + BOOST_TEST(countSpecialObjects(map) == 0u); + BOOST_TEST(map.reserved.empty()); +} + +BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_a_few_special_objects_if_enabled) +{ + WorldDescription worldDesc; + loadGameData(worldDesc); + MapSettings settings; + + settings.size = MapExtent(192, 192); + settings.numPlayers = 4; + settings.specialObjects = true; + settings.trees = 100; + settings.stonePiles = 20; + + for(unsigned landscape = 0; landscape < worldDesc.landscapes.size(); landscape++) + { + for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed, MapStyle::Water}) + { + settings.type = DescIdx(landscape); + settings.style = style; + RandomUtility rnd(2); + + const Map map = GenerateRandomMap(rnd, worldDesc, settings); + ValidateMap(map, settings.size, settings.numPlayers); + + BOOST_TEST_CONTEXT("Landscape " << landscape << " style " << static_cast(style)) + { + // The graveyard contains more than one special object + BOOST_TEST(countSpecialObjects(map) > 0u); + BOOST_TEST(countSpecialObjects(map) <= 16 * GetNumSpecialScenes(map.size)); + BOOST_TEST(map.reserved.size() % 169 == 0u); + BOOST_TEST(map.reserved.size() > 0u); + BOOST_TEST(map.reserved.size() <= 169 * GetNumSpecialScenes(map.size)); + } + } + } +} + BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_mapfile_with_max_players) { rttr::test::TmpFolder tmpFolder; diff --git a/tests/s25Main/mapGenerator/testSpecialObjects.cpp b/tests/s25Main/mapGenerator/testSpecialObjects.cpp new file mode 100644 index 0000000000..4c87f84bd1 --- /dev/null +++ b/tests/s25Main/mapGenerator/testSpecialObjects.cpp @@ -0,0 +1,542 @@ +// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "PointOutput.h" +#include "helpers/EnumRange.h" +#include "mapGenFixtures.h" +#include "mapGenerator/FreeLand.h" +#include "mapGenerator/Resources.h" +#include "mapGenerator/SpecialObjects.h" +#include "mapGenerator/TextureHelper.h" +#include "gameTypes/Direction.h" +#include "libsiedler2/enumTypes.h" +#include +#include +#include + +using namespace rttr::mapGenerator; + +namespace { +constexpr uint8_t landHeight = 10; +constexpr unsigned numLandscapes = 3; + +struct SpecialObjectsFixture : MapGenFixture +{ + static bool isMeadow(const TerrainDesc& terrain) { return IsBuildableLand(terrain) && terrain.s2Id == 0x08; } + + static bool isLushMeadow(const TerrainDesc& terrain) { return IsBuildableLand(terrain) && terrain.s2Id == 0x0A; } + + /// Creates a flat map which is entirely covered by meadow + Map createLandMap(const MapExtent size, bool lush = false) const + { + Map map = createMap(size); + map.z.Resize(size, landHeight); + const auto land = map.textureMap.Find(lush ? isLushMeadow : isMeadow); + RTTR_FOREACH_PT(MapPoint, size) + { + map.textureMap.Set(pt, land); + } + return map; + } +}; + +unsigned countObjects(const Map& map) +{ + unsigned count = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] != libsiedler2::OT_Empty) + count++; + } + return count; +} +} // namespace + +BOOST_FIXTURE_TEST_SUITE(SpecialObjectsTests, SpecialObjectsFixture) + +BOOST_AUTO_TEST_CASE(GetNumSpecialScenes_returns_a_few_scenes_depending_on_map_size) +{ + BOOST_TEST(GetNumSpecialScenes(MapExtent(32, 32)) == 1u); + BOOST_TEST(GetNumSpecialScenes(MapExtent(64, 64)) < GetNumSpecialScenes(MapExtent(128, 128))); + BOOST_TEST(GetNumSpecialScenes(MapExtent(128, 128)) < GetNumSpecialScenes(MapExtent(256, 256))); + BOOST_TEST(GetNumSpecialScenes(MapExtent(1024, 1024)) <= 6u); +} + +BOOST_AUTO_TEST_CASE(FindSceneSites_returns_requested_number_of_distant_sites) +{ + const Map map = createLandMap(MapExtent(96, 96)); + RandomUtility rnd(0); + + const auto sites = FindSceneSites(map, rnd, 4); + + BOOST_TEST_REQUIRE(sites.size() == 4u); + for(unsigned i = 0; i < sites.size(); i++) + { + for(unsigned j = i + 1; j < sites.size(); j++) + BOOST_TEST(map.z.CalcDistance(sites[i], sites[j]) >= 4 * sceneRadius); + } +} + +BOOST_AUTO_TEST_CASE(FindSceneSites_finds_nothing_without_buildable_land) +{ + Map map = createMap(MapExtent(32, 32)); + const auto water = map.textureMap.Find(IsWater); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.textureMap.Set(pt, water); + } + RandomUtility rnd(0); + + BOOST_TEST(FindSceneSites(map, rnd, 3).empty()); +} + +BOOST_AUTO_TEST_CASE(FindSceneSites_keeps_distance_to_obstacles_and_players) +{ + Map map = createLandMap(MapExtent(64, 64)); + const MapPoint hq(10, 10); + const MapPoint tree(40, 40); + const MapPoint swamp(20, 45); + map.hqPositions.push_back(hq); + map.harbors.push_back(Triangle(true, MapPoint(50, 12))); + map.objectTypes[tree] = libsiedler2::OT_TreeOrPalm; + map.textureMap.Set(swamp, map.textureMap.Find(IsImpassable)); + + for(unsigned seed = 0; seed < 30; seed++) + { + RandomUtility rnd(seed); + for(const MapPoint& site : FindSceneSites(map, rnd, 3)) + { + // Objects around headquarters and harbors are removed within a radius of 5 + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, hq) > sceneRadius + 5, "At point " << site); + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, MapPoint(50, 12)) > sceneRadius + 5, "At point " << site); + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, tree) > sceneRadius, "At point " << site); + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, swamp) > sceneRadius + 1, "At point " << site); + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_surrounds_special_object_by_decoration_within_scene) +{ + for(unsigned i = 0; i < numLandscapes; i++) + { + landscape = DescIdx(i); + for(const SceneType type : helpers::EnumRange()) + { + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, type, style); + + BOOST_TEST_CONTEXT("Landscape " << i << " scene " << static_cast(type) << " style " + << static_cast(style) << " seed " << seed) + { + BOOST_TEST(IsSpecialObject(map.objectTypes[center], map.objectInfos[center])); + BOOST_TEST(countObjects(map) >= 15u); + BOOST_TEST(map.reserved.size() == 169u); + RTTR_FOREACH_PT(MapPoint, map.size) + { + const bool inScene = map.z.CalcDistance(center, pt) <= sceneRadius; + BOOST_TEST_REQUIRE(helpers::contains(map.reserved, pt) == inScene, "At point " << pt); + if(!inScene) + { + // The surrounding land is left untouched + BOOST_TEST_REQUIRE(map.objectTypes[pt] == libsiedler2::OT_Empty, "At point " << pt); + BOOST_TEST_REQUIRE(map.z[pt] == landHeight, "At point " << pt); + } + // Transitions may reach the triangles right next to the scene + if(map.z.CalcDistance(center, pt) > sceneRadius + 1) + BOOST_TEST_REQUIRE(map.textureMap.All(pt, isMeadow), "At point " << pt); + if(map.textureMap.All(pt, IsWater)) + BOOST_TEST_REQUIRE(map.objectTypes[pt] == libsiedler2::OT_Empty, "At point " << pt); + // Hills and hollows are smooth + for(const MapPoint& neighbor : map.z.GetNeighbours(pt)) + { + const int difference = static_cast(map.z[pt]) - static_cast(map.z[neighbor]); + BOOST_TEST_REQUIRE(std::abs(difference) <= 2, "At point " << pt); + } + } + } + } + } + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_keeps_footprint_of_castle_ruin_free_and_even) +{ + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 10; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, SceneType::CastleRuin, style); + + BOOST_TEST(map.objectInfos[center] == OI_CastleRuin); + for(const Direction dir : {Direction::West, Direction::NorthWest, Direction::NorthEast}) + { + // The ruin covers some of its neighbors + BOOST_TEST_REQUIRE(map.objectTypes[map.z.GetNeighbour(center, dir)] == libsiedler2::OT_Empty); + } + for(const MapPoint& pt : map.z.GetNeighbours(center)) + BOOST_TEST_REQUIRE(map.z[pt] == map.z[center], "At point " << pt); + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_blends_ground_into_surrounding_land) +{ + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(0); + const auto isFlowers = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0F; }; + + CreateScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Overgrown); + + // Flowers in the middle fade out via the textures between flowers and meadow + BOOST_TEST(map.textureMap.All(center, isFlowers)); + std::set textures; + RTTR_FOREACH_PT(MapPoint, map.size) + { + textures.insert(map.textureMap.GetTextureId(map.getTextures()[pt].rsu)); + if(map.textureMap.Any(pt, isFlowers)) + { + // Flowers never touch the untouched meadow directly + for(const MapPoint& neighbor : map.z.GetPointsInRadius(pt, 2)) + BOOST_TEST_REQUIRE(map.z.CalcDistance(center, neighbor) <= sceneRadius, "At point " << pt); + } + } + BOOST_TEST(textures.count(0x09) == 1u); + BOOST_TEST(textures.count(0x0A) == 1u); +} + +BOOST_AUTO_TEST_CASE(CreateScene_uses_all_transition_textures_between_dry_ground_and_meadow) +{ + const auto isSteppe = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0E; }; + const auto isSavannah = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x00; }; + + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, SceneType::Camp, style); + + BOOST_TEST(map.textureMap.All(center, isSteppe)); + unsigned savannah = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.textureMap.Any(pt, isSavannah)) + savannah++; + // Steppe is always separated from meadow by savannah + if(map.textureMap.Any(pt, isSteppe)) + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isMeadow), "At point " << pt); + } + BOOST_TEST(savannah > 6u); + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_surrounds_pond_by_transitions_from_sand_to_meadow) +{ + const auto isSteppe = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0E; }; + const auto isSavannah = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x00; }; + + for(const SceneType type : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, type, SceneStyle::Crag); + + BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " seed " << seed) + { + unsigned water = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.textureMap.All(pt, IsWater)) + water++; + // Water is enclosed by sand, sand by steppe and steppe by savannah + if(map.textureMap.Any(pt, IsWater)) + BOOST_TEST_REQUIRE( + map.textureMap.All(pt, [](const TerrainDesc& t) { return IsWater(t) || IsCoastTerrain(t); }), + "At point " << pt); + if(map.textureMap.Any(pt, IsCoastTerrain)) + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isMeadow), "At point " << pt); + if(map.textureMap.Any(pt, isSteppe)) + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isMeadow), "At point " << pt); + if(map.textureMap.Any(pt, IsCoastTerrain)) + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isSavannah), "At point " << pt); + } + BOOST_TEST(water >= 2u); + } + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_adds_transitions_within_lush_meadow) +{ + // The most lush meadow is several textures away from sand but looks like any other meadow + const auto isGreen = [](const TerrainDesc& terrain) { return IsBuildableLand(terrain) && terrain.humidity > 90; }; + const auto isSteppe = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0E; }; + const auto isSavannah = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x00; }; + + for(const SceneType type : helpers::EnumRange()) + { + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + Map map = createLandMap(MapExtent(40, 40), true); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, type, style); + + BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " style " << static_cast(style) + << " seed " << seed) + { + RTTR_FOREACH_PT(MapPoint, map.size) + { + // Sand meets steppe, steppe meets savannah and only savannah meets meadow + if(map.textureMap.Any(pt, IsCoastTerrain)) + { + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isGreen), "At point " << pt); + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isSavannah), "At point " << pt); + } + if(map.textureMap.Any(pt, isSteppe)) + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isGreen), "At point " << pt); + } + } + } + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_adds_rim_around_rock) +{ + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(0); + const auto isRock = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x22; }; + + CreateScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Overgrown); + + BOOST_TEST(map.textureMap.All(center, isRock)); + BOOST_TEST(map.z[center] > landHeight); + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.textureMap.Any(pt, isRock)) + BOOST_TEST_REQUIRE(map.textureMap.All(pt, IsBuildableMountain), "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_raises_crag_next_to_pond) +{ + for(unsigned seed = 0; seed < 10; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Crag); + + unsigned pond = 0; + unsigned rocks = 0; + uint8_t top = 0; + uint8_t waterLevel = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + top = std::max(top, map.z[pt]); + if(map.objectTypes[pt] == libsiedler2::OT_Stone1 || map.objectTypes[pt] == libsiedler2::OT_Stone2) + rocks++; + if(map.textureMap.All(pt, IsWater)) + { + BOOST_TEST_REQUIRE((pond == 0 || map.z[pt] == waterLevel), "At point " << pt); + waterLevel = map.z[pt]; + pond++; + } + } + BOOST_TEST(pond >= 2u); + BOOST_TEST(rocks >= 2u); + BOOST_TEST(top >= landHeight + 3); + BOOST_TEST(waterLevel < landHeight); + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_lines_road_of_avenue_with_trees) +{ + for(unsigned seed = 0; seed < 10; seed++) + { + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Avenue); + + // One of the six directions is a free road with trees on both sides + unsigned roads = 0; + for(const Direction dir : helpers::EnumRange()) + { + MapPoint pt = center; + bool isRoad = true; + for(unsigned i = 1; i <= sceneRadius; i++) + { + pt = map.z.GetNeighbour(pt, dir); + isRoad &= map.objectTypes[pt] == libsiedler2::OT_Empty; + } + if(isRoad) + roads++; + } + BOOST_TEST(roads >= 1u); + unsigned trees = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] == libsiedler2::OT_TreeOrPalm || map.objectTypes[pt] == libsiedler2::OT_Palm + || map.objectTypes[pt] == libsiedler2::OT_Palm + 1) + trees++; + } + BOOST_TEST(trees == 8u); + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_lines_trail_with_remains) +{ + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(0); + + CreateScene(map, rnd, center, SceneType::StoneCircle, SceneStyle::Trail); + + unsigned stones = 0; + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(map.objectTypes[pt] == OT_Decoration && map.objectInfos[pt] >= 0x18 && map.objectInfos[pt] <= 0x1E) + stones++; + } + // Circle around the gate and the stones along the trail + BOOST_TEST(stones >= 6u + 5u); + BOOST_TEST((map.objectInfos[center] == OI_Gate || map.objectInfos[center] == OI_OpenGate)); +} + +BOOST_AUTO_TEST_CASE(CreateScene_places_single_tent_and_rows_of_graves_in_any_style) +{ + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + Map camp = createLandMap(MapExtent(40, 40)); + Map graveyard = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + RandomUtility rnd(seed); + + CreateScene(camp, rnd, center, SceneType::Camp, style); + CreateScene(graveyard, rnd, center, SceneType::Graveyard, style); + + unsigned tents = 0; + unsigned graves = 0; + RTTR_FOREACH_PT(MapPoint, camp.size) + { + if(camp.objectTypes[pt] == OT_Decoration && camp.objectInfos[pt] == OI_Tent) + tents++; + if(graveyard.objectTypes[pt] == OT_Decoration && graveyard.objectInfos[pt] == OI_Cross) + graves++; + } + BOOST_TEST(tents == 1u); + BOOST_TEST(graves >= 7u); + } + } +} + +BOOST_AUTO_TEST_CASE(CreateScene_levels_ground_and_smooths_transition) +{ + Map map = createLandMap(MapExtent(48, 48)); + RTTR_FOREACH_PT(MapPoint, map.size) + { + map.z[pt] = static_cast(landHeight + pt.x / 2); + } + const MapPoint center(24, 24); + const uint8_t level = map.z[center]; + RandomUtility rnd(0); + + CreateScene(map, rnd, center, SceneType::Camp, SceneStyle::Trail); + + for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(center, sceneRadius)) + { + const unsigned distance = map.z.CalcDistance(center, pt); + const int difference = static_cast(map.z[pt]) - static_cast(level); + const int tolerance = distance <= 3 ? 0 : static_cast(distance) - 3; + BOOST_TEST_REQUIRE(std::abs(difference) <= tolerance, "At point " << pt); + } +} + +BOOST_AUTO_TEST_CASE(AddSpecialScenes_mixes_up_types_of_scenes) +{ + Map map = createLandMap(MapExtent(256, 256)); + RandomUtility rnd(0); + + const auto scenes = AddSpecialScenes(map, rnd); + + BOOST_TEST_REQUIRE(scenes.size() == GetNumSpecialScenes(map.size)); + std::set types; + for(const Scene& scene : scenes) + { + types.insert(scene.type); + BOOST_TEST(IsSpecialObject(map.objectTypes[scene.center], map.objectInfos[scene.center])); + } + BOOST_TEST(types.size() == 5u); + BOOST_TEST(map.reserved.size() == 169 * scenes.size()); +} + +BOOST_AUTO_TEST_CASE(AddSpecialScenes_varies_with_seed) +{ + Map map1 = createLandMap(MapExtent(64, 64)); + Map map2 = createLandMap(MapExtent(64, 64)); + RandomUtility rnd1(1); + RandomUtility rnd2(2); + + const auto scenes1 = AddSpecialScenes(map1, rnd1); + const auto scenes2 = AddSpecialScenes(map2, rnd2); + + BOOST_TEST_REQUIRE(scenes1.size() == 1u); + BOOST_TEST_REQUIRE(scenes2.size() == 1u); + BOOST_TEST(scenes1.front().center != scenes2.front().center); +} + +BOOST_AUTO_TEST_CASE(AddObjects_keeps_scenes_free_from_trees_and_stone_piles) +{ + Map map = createLandMap(MapExtent(48, 48)); + RandomUtility rnd(0); + const auto scenes = AddSpecialScenes(map, rnd); + BOOST_TEST_REQUIRE(!scenes.empty()); + const auto typesBefore = map.objectTypes; + const auto infosBefore = map.objectInfos; + + MapSettings settings; + settings.trees = 100; + settings.stonePiles = 20; + AddObjects(map, rnd, settings); + + BOOST_TEST(countObjects(map) > 500u); + for(const MapPoint& pt : map.reserved) + { + BOOST_TEST_REQUIRE(map.objectTypes[pt] == typesBefore[pt], "At point " << pt); + BOOST_TEST_REQUIRE(map.objectInfos[pt] == infosBefore[pt], "At point " << pt); + } +} + +BOOST_AUTO_TEST_SUITE_END() From fe57b0c2d95107d04590f9ae61f2cf3ecf66b354 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Fri, 13 Nov 2026 12:49:08 +0000 Subject: [PATCH 4/9] Let special scenes adapt to obstacles and fix seed dependent tests The random distributions of the standard library differ between platforms, so maps generated from a fixed seed differ as well. Three tests only passed for the maps generated on macOS. Special scenes required buildable land eight nodes around their center which many maps don't provide: maps often ended up without any scene. Now only the middle of a scene has to be free. Water, mountains, other landscapes and objects in the outer part are left untouched and simply cut the scene. - Test the limit of free land with mountains only as the foot of desert hills is buildable and counts as free land. - Check that flowers don't touch the surrounding meadow instead of relying on the size of the patch. Co-Authored-By: Claude Fable 5.2 --- libs/s25main/mapGenerator/SpecialObjects.cpp | 29 +++++--- libs/s25main/mapGenerator/SpecialObjects.h | 11 +-- tests/s25Main/mapGenerator/testFreeLand.cpp | 1 + tests/s25Main/mapGenerator/testRandomMap.cpp | 5 ++ .../mapGenerator/testSpecialObjects.cpp | 71 +++++++++++++++++-- 5 files changed, 97 insertions(+), 20 deletions(-) diff --git a/libs/s25main/mapGenerator/SpecialObjects.cpp b/libs/s25main/mapGenerator/SpecialObjects.cpp index b1e393fba7..c7255d7c32 100644 --- a/libs/s25main/mapGenerator/SpecialObjects.cpp +++ b/libs/s25main/mapGenerator/SpecialObjects.cpp @@ -163,6 +163,9 @@ namespace { NodeMapBase overlay_; /// Nodes which have to stay free from objects std::set blocked_; + /// Nodes of the scene which may be changed: buildable land without objects. Only the middle of a scene is + /// known to be free. Water, mountains and other landscapes further out are left untouched and cut the scene. + std::set free_; static unsigned Mod6(int dir) { return static_cast((dir % 6 + 6) % 6); } @@ -210,6 +213,12 @@ namespace { otherTree_(rnd.RandomItem(CreateTrees(map.textureMap))) { const auto area = map.z.GetPointsInRadiusWithCenter(center, sceneRadius); + for(const MapPoint& pt : area) + { + if(map.textureMap.All(pt, IsBuildableLand) && map.objectTypes[pt] == libsiedler2::OT_Empty + && !helpers::contains(map.barren, pt)) + free_.insert(pt); + } map.reserved.insert(area.begin(), area.end()); overlay_.Resize(map.size, Overlay::None); @@ -221,7 +230,7 @@ namespace { // Level the middle of the scene and reduce the slopes towards the surrounding land const int level = map.z[center]; - for(const MapPoint& pt : area) + for(const MapPoint& pt : free_) { const int tolerance = std::max(0, static_cast(Distance(pt)) - static_cast(levelRadius)); map.z[pt] = @@ -237,6 +246,7 @@ namespace { unsigned Distance(const MapPoint& pt) const { return map_.z.CalcDistance(center_, pt); } unsigned Distance(const MapPoint& from, const MapPoint& to) const { return map_.z.CalcDistance(from, to); } bool IsWater(const MapPoint& pt) const { return overlay_[pt] == Overlay::Water; } + bool IsFree(const MapPoint& pt) const { return helpers::contains(free_, pt); } /// Moves from the node into the direction which is counted clockwise starting in the west MapPoint Step(MapPoint pt, int dir, unsigned steps = 1) const @@ -265,7 +275,7 @@ namespace { std::vector Area(const MapPoint& pt, unsigned radius) const { auto area = map_.z.GetPointsInRadiusWithCenter(pt, radius); - helpers::erase_if(area, [this](const MapPoint& p) { return Distance(p) > sceneRadius; }); + helpers::erase_if(area, [this](const MapPoint& p) { return !IsFree(p); }); return area; } @@ -330,7 +340,7 @@ namespace { void SetOverlay(const MapPoint& pt, Overlay overlay) { - if(Distance(pt) < sceneRadius - 1 && overlay > overlay_[pt]) + if(IsFree(pt) && Distance(pt) < sceneRadius - 1 && overlay > overlay_[pt]) overlay_[pt] = overlay; } @@ -351,8 +361,8 @@ namespace { bool CanPlace(const MapPoint& pt) const { - return Distance(pt) <= sceneRadius && map_.objectTypes[pt] == libsiedler2::OT_Empty - && !helpers::contains(blocked_, pt) && !IsWater(pt); + return IsFree(pt) && map_.objectTypes[pt] == libsiedler2::OT_Empty && !helpers::contains(blocked_, pt) + && !IsWater(pt); } bool Chance(unsigned chance) { return chance >= 100 || rnd_.ByChance(chance); } @@ -564,13 +574,13 @@ namespace { } for(auto it = ground_.rbegin(); it != ground_.rend(); ++it) { - if(Distance(it->first) <= sceneRadius) + if(IsFree(it->first)) addAnchor(it->first, it->second, true); } // All other nodes keep their texture unless a transition is required for(const MapPoint& pt : area) { - if(visited.count(pt)) + if(visited.count(pt) || !IsFree(pt)) continue; const int index = limit(pt, RampIndex(pt)); if(index != RampIndex(pt)) @@ -823,10 +833,11 @@ std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigne players.push_back(harbor.position); const auto playerDistance = DistancesTo(players, map.size); - // One node more than required leaves a gap between the scene and its surroundings + // Only the middle of a scene requires free land - one node more leaves a gap to the surroundings. Obstacles + // further out cut the scene which allows scenes on maps with little free land. auto candidates = SelectPoints( [&](const MapPoint& pt) { - return obstacleDistance[pt] > sceneRadius + 1 && playerDistance[pt] >= minPlayerDistance; + return obstacleDistance[pt] > sceneCoreRadius + 1 && playerDistance[pt] >= minPlayerDistance; }, map.size); diff --git a/libs/s25main/mapGenerator/SpecialObjects.h b/libs/s25main/mapGenerator/SpecialObjects.h index 3dc6ff08f4..47c9da3dd5 100644 --- a/libs/s25main/mapGenerator/SpecialObjects.h +++ b/libs/s25main/mapGenerator/SpecialObjects.h @@ -55,6 +55,8 @@ struct Scene /// Radius around the center of a scene which is covered by the scene constexpr unsigned sceneRadius = 7; +/// Radius around the center of a scene which has to be buildable land without any objects +constexpr unsigned sceneCoreRadius = 4; /// Indices of special objects (object type OT_Decoration) constexpr uint8_t OI_Gate = 0x16; @@ -79,8 +81,8 @@ bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo); unsigned GetNumSpecialScenes(const MapExtent& size); /** - * Searches for places for special scenes. A scene requires buildable land without any objects and keeps a distance to - * headquarters, harbors and other scenes. + * Searches for places for special scenes. The middle of a scene requires buildable land without any objects and keeps + * a distance to headquarters, harbors and other scenes. * * @param map map to search for places (is not modified) * @param rnd random number generator @@ -93,8 +95,9 @@ std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigne /** * Creates a scene around the specified center. The theme defines the special object, the objects around it and the * ground while the style defines how they are arranged. The ground blends into the surrounding land using intermediate - * textures and hills rise smoothly. The nodes of the scene are reserved to keep them free from random trees and stone - * piles. + * textures and hills rise smoothly. Only buildable land without objects is changed: water, mountains and other + * landscapes in the outer part of a scene are left untouched. The nodes of the scene are reserved to keep them free + * from random trees and stone piles. * * @param map reference to the map to place the scene on (manipulates textures, objects and z-values of the map) * @param rnd random number generator diff --git a/tests/s25Main/mapGenerator/testFreeLand.cpp b/tests/s25Main/mapGenerator/testFreeLand.cpp index 50eae5d27b..aee1a0f274 100644 --- a/tests/s25Main/mapGenerator/testFreeLand.cpp +++ b/tests/s25Main/mapGenerator/testFreeLand.cpp @@ -11,6 +11,7 @@ #include "libsiedler2/enumTypes.h" #include #include +#include using namespace rttr::mapGenerator; diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index 2f344fcf33..b9a1009f60 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -233,6 +233,11 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) // Smaller maps do not contain enough free land to require additional mountains settings.size = MapExtent(256, 256); settings.mountainDistance = MountainDistance::Close; + // The foot of desert hills is buildable and therefore counts as free land + settings.mountains = 50; + settings.desert = 0; + settings.swamp = 0; + settings.specialObjects = false; // Maximum of the randomized free land to keep plus the transition textures added at the foot of mountains and // desert hills diff --git a/tests/s25Main/mapGenerator/testSpecialObjects.cpp b/tests/s25Main/mapGenerator/testSpecialObjects.cpp index 4c87f84bd1..fb962b5229 100644 --- a/tests/s25Main/mapGenerator/testSpecialObjects.cpp +++ b/tests/s25Main/mapGenerator/testSpecialObjects.cpp @@ -13,6 +13,7 @@ #include "libsiedler2/enumTypes.h" #include #include +#include #include using namespace rttr::mapGenerator; @@ -110,8 +111,8 @@ BOOST_AUTO_TEST_CASE(FindSceneSites_keeps_distance_to_obstacles_and_players) // Objects around headquarters and harbors are removed within a radius of 5 BOOST_TEST_REQUIRE(map.z.CalcDistance(site, hq) > sceneRadius + 5, "At point " << site); BOOST_TEST_REQUIRE(map.z.CalcDistance(site, MapPoint(50, 12)) > sceneRadius + 5, "At point " << site); - BOOST_TEST_REQUIRE(map.z.CalcDistance(site, tree) > sceneRadius, "At point " << site); - BOOST_TEST_REQUIRE(map.z.CalcDistance(site, swamp) > sceneRadius + 1, "At point " << site); + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, tree) > sceneCoreRadius + 1, "At point " << site); + BOOST_TEST_REQUIRE(map.z.CalcDistance(site, swamp) > sceneCoreRadius + 1, "At point " << site); } } } @@ -207,12 +208,9 @@ BOOST_AUTO_TEST_CASE(CreateScene_blends_ground_into_surrounding_land) RTTR_FOREACH_PT(MapPoint, map.size) { textures.insert(map.textureMap.GetTextureId(map.getTextures()[pt].rsu)); + // Flowers never touch the meadow of the surrounding land directly if(map.textureMap.Any(pt, isFlowers)) - { - // Flowers never touch the untouched meadow directly - for(const MapPoint& neighbor : map.z.GetPointsInRadius(pt, 2)) - BOOST_TEST_REQUIRE(map.z.CalcDistance(center, neighbor) <= sceneRadius, "At point " << pt); - } + BOOST_TEST_REQUIRE(!map.textureMap.Any(pt, isMeadow), "At point " << pt); } BOOST_TEST(textures.count(0x09) == 1u); BOOST_TEST(textures.count(0x0A) == 1u); @@ -327,6 +325,65 @@ BOOST_AUTO_TEST_CASE(CreateScene_adds_transitions_within_lush_meadow) } } +BOOST_AUTO_TEST_CASE(CreateScene_leaves_other_landscapes_untouched) +{ + for(const SceneType type : helpers::EnumRange()) + { + for(const SceneStyle style : helpers::EnumRange()) + { + for(unsigned seed = 0; seed < 5; seed++) + { + // Water, a mountain and a tree are located in the outer part of the scene + Map map = createLandMap(MapExtent(40, 40)); + const MapPoint center(20, 20); + const auto water = map.textureMap.Find(IsWater); + RTTR_FOREACH_PT(MapPoint, map.size) + { + if(pt.x >= 26) + { + map.textureMap.Set(pt, water); + map.z[pt] = map.height.minimum; + } + } + const MapPoint mountain(15, 15); + const MapPoint tree(17, 26); + map.textureMap.Set(mountain, map.textureMap.Find(IsMinableMountain)); + map.z[mountain] = landHeight + 8; + map.objectTypes[tree] = libsiedler2::OT_TreeOrPalm; + map.objectInfos[tree] = libsiedler2::OI_TreeOrPalm_Begin; + const Map original = map; + RandomUtility rnd(seed); + + CreateScene(map, rnd, center, type, style); + + BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " style " << static_cast(style) + << " seed " << seed) + { + BOOST_TEST(IsSpecialObject(map.objectTypes[center], map.objectInfos[center])); + BOOST_TEST(map.objectTypes[tree] == original.objectTypes[tree]); + BOOST_TEST(map.objectInfos[tree] == original.objectInfos[tree]); + const auto isLand = [this](DescIdx texture) { + return IsBuildableLand(worldDesc.get(texture)); + }; + RTTR_FOREACH_PT(MapPoint, map.size) + { + // Only triangles of buildable land are painted + const TexturePair& before = original.getTextures()[pt]; + const TexturePair& after = map.getTextures()[pt]; + BOOST_TEST_REQUIRE((isLand(before.rsu) || after.rsu == before.rsu), "At point " << pt); + BOOST_TEST_REQUIRE((isLand(before.lsd) || after.lsd == before.lsd), "At point " << pt); + // Nodes which are part of other landscapes keep their height and get no objects + if(original.textureMap.All(pt, IsBuildableLand)) + continue; + BOOST_TEST_REQUIRE(map.z[pt] == original.z[pt], "At point " << pt); + BOOST_TEST_REQUIRE(map.objectTypes[pt] == original.objectTypes[pt], "At point " << pt); + } + } + } + } + } +} + BOOST_AUTO_TEST_CASE(CreateScene_adds_rim_around_rock) { Map map = createLandMap(MapExtent(40, 40)); From d2f5e0e470ea31448b6a1df299deba2d7131d2ea Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Fri, 13 Nov 2026 13:09:25 +0000 Subject: [PATCH 5/9] Fix formatting of random map test Restore the single-line test context which a newer clang-format had wrapped. The CI checks formatting with clang-format 10. Co-Authored-By: Claude Fable 5.2 --- tests/s25Main/mapGenerator/testRandomMap.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index b9a1009f60..0e8470a89c 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -58,10 +58,7 @@ static void ValidateMap(const Map& map, const MapExtent& size, unsigned players) for(unsigned index = 0; index < players; index++) { BOOST_TEST_REQUIRE(map.hqPositions[index].isValid()); - BOOST_TEST_CONTEXT("Player " << index) - { - validateHqPos(map.hqPositions[index]); - } + BOOST_TEST_CONTEXT("Player " << index) { validateHqPos(map.hqPositions[index]); } } } From 80f2639776ea7cc6fb7254e311cd5da0a882da04 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Fri, 13 Nov 2026 13:35:31 +0000 Subject: [PATCH 6/9] Test harbors next to swamps with a dedicated map The random map test only checked harbors if the generated map contained any, which left the check uncovered. Test the search for coastlines directly with a lake and a swamp made of water instead. Co-Authored-By: Claude Fable 5.2 --- tests/s25Main/mapGenerator/testHarbors.cpp | 28 ++++++++++++++++++++ tests/s25Main/mapGenerator/testRandomMap.cpp | 7 +---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/s25Main/mapGenerator/testHarbors.cpp b/tests/s25Main/mapGenerator/testHarbors.cpp index f4570ad700..818585b28c 100644 --- a/tests/s25Main/mapGenerator/testHarbors.cpp +++ b/tests/s25Main/mapGenerator/testHarbors.cpp @@ -66,6 +66,34 @@ BOOST_AUTO_TEST_CASE(PlaceHarborPosition_applies_buildable_terrain_around_positi } } +BOOST_AUTO_TEST_CASE(FindCoastlines_ignores_swamps_made_of_water) +{ + // Some landscapes use water textures for swamps which can't be used by ships + Map map = createMap(MapExtent(32, 32)); + map.getTextures().Resize(map.size, TexturePair(map.textureMap.Find(IsBuildableLand))); + const auto water = map.textureMap.Find(IsWater); + const MapPoint lake(8, 16); + const MapPoint swamp(24, 16); + const unsigned radius = 4; + + for(const MapPoint& center : {lake, swamp}) + { + for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(center, radius)) + map.textureMap.Set(pt, water); + } + const auto swampNodes = map.z.GetPointsInRadiusWithCenter(swamp, radius); + map.barren.insert(swampNodes.begin(), swampNodes.end()); + + const auto coasts = FindCoastlines(map, {}); + + BOOST_TEST_REQUIRE(coasts.size() == 1u); + BOOST_TEST_REQUIRE(!coasts.front().empty()); + for(const MapPoint& pt : coasts.front()) + { + BOOST_TEST(map.z.CalcDistance(pt, lake) == radius + 1); + } +} + BOOST_AUTO_TEST_CASE(PlaceHarbors_places_no_harbors_on_coast_below_minimum_size) { Map map = createMap(MapExtent(6, 8)); diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index 0e8470a89c..33e7dd7777 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -328,7 +328,7 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_deserts_and_swamps_for_all_landsc { if(!map.textureMap.All(pt, IsImpassable)) continue; - // Swamps are flat and neither used for fishing nor for harbors + // Swamps are flat and not used for fishing BOOST_TEST_REQUIRE(map.resources[pt] != libsiedler2::R_Fish, "At point " << pt); for(const MapPoint& neighbor : map.z.GetNeighbours(pt)) { @@ -336,11 +336,6 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_deserts_and_swamps_for_all_landsc BOOST_TEST_REQUIRE(map.z[pt] == map.z[neighbor], "At point " << pt); } } - for(const Triangle& harbor : map.harbors) - { - for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(harbor.position, 2)) - BOOST_TEST_REQUIRE(!helpers::contains(map.barren, pt), "At point " << pt); - } } } } From ae678a06ba75c797b087056816a13b3bcbad97ff Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Fri, 13 Nov 2026 15:01:43 +0000 Subject: [PATCH 7/9] Sort painted nodes of special scenes in place Avoids copying the nodes into a second vector just for sorting, which clang-tidy reported as inefficient vector operation. Co-Authored-By: Claude Fable 5.2 --- libs/s25main/mapGenerator/SpecialObjects.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/s25main/mapGenerator/SpecialObjects.cpp b/libs/s25main/mapGenerator/SpecialObjects.cpp index c7255d7c32..0a9c3d356e 100644 --- a/libs/s25main/mapGenerator/SpecialObjects.cpp +++ b/libs/s25main/mapGenerator/SpecialObjects.cpp @@ -589,15 +589,13 @@ namespace { // Neighboring nodes share triangles: the texture which differs most from the land is painted last. Thus // patches keep their size and each transition gets a row of triangles. - std::vector> order; + const auto change = [this](const Anchor& anchor) { return std::abs(anchor.index - RampIndex(anchor.pt)); }; + std::stable_sort(painted.begin(), painted.end(), + [&change](const Anchor& lhs, const Anchor& rhs) { return change(lhs) < change(rhs); }); for(const Anchor& anchor : painted) - order.emplace_back(std::abs(anchor.index - RampIndex(anchor.pt)), anchor); - std::stable_sort(order.begin(), order.end(), - [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; }); - for(const auto& entry : order) { - const int index = std::clamp(entry.second.index, 0, static_cast(ramp_.size()) - 1); - map_.textureMap.Set(entry.second.pt, ramp_[index]); + const int index = std::clamp(anchor.index, 0, static_cast(ramp_.size()) - 1); + map_.textureMap.Set(anchor.pt, ramp_[index]); } } }; From e9e8b38e561bd6a4119e5dd10074e715e65ea6e9 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Mon, 16 Nov 2026 20:31:34 +0000 Subject: [PATCH 8/9] added build-xcode folder to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8cdabf2eb4..b2f7e881fd 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ opencode.json /S2 /build +/build-xcode /external/boost* /win32/*.aps /data/RTTR/languages/*.mo From f92da66c63a6b8175448d465c1ad6cab18ed1a91 Mon Sep 17 00:00:00 2001 From: Timm Hoffmeister Date: Mon, 16 Nov 2026 21:07:40 +0000 Subject: [PATCH 9/9] Address review comments on random map generator - Use lowerCamelCase for all new functions and methods (free functions, RandomMap members, SceneBuilder methods) and the matching test names - Use maxEnumValue/EnumArray for LandFill instead of numLandFills and a std::array indexed by casts - Shorten docstrings and drop @param lines that add no information Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J1xFd2ZsbRhtWeVV21cLbw --- libs/s25main/mapGenerator/FreeLand.cpp | 63 +-- libs/s25main/mapGenerator/FreeLand.h | 84 +-- libs/s25main/mapGenerator/Mountains.cpp | 34 +- libs/s25main/mapGenerator/Mountains.h | 100 +--- libs/s25main/mapGenerator/RandomMap.cpp | 30 +- libs/s25main/mapGenerator/RandomMap.h | 18 +- libs/s25main/mapGenerator/SpecialObjects.cpp | 497 +++++++++--------- libs/s25main/mapGenerator/SpecialObjects.h | 52 +- libs/s25main/mapGenerator/TextureHelper.cpp | 2 +- libs/s25main/mapGenerator/TextureHelper.h | 5 +- tests/s25Main/mapGenerator/testFreeLand.cpp | 157 +++--- tests/s25Main/mapGenerator/testMountains.cpp | 104 ++-- tests/s25Main/mapGenerator/testRandomMap.cpp | 40 +- .../mapGenerator/testSpecialObjects.cpp | 98 ++-- 14 files changed, 575 insertions(+), 709 deletions(-) diff --git a/libs/s25main/mapGenerator/FreeLand.cpp b/libs/s25main/mapGenerator/FreeLand.cpp index 00338e8a2c..3ac5902986 100644 --- a/libs/s25main/mapGenerator/FreeLand.cpp +++ b/libs/s25main/mapGenerator/FreeLand.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "mapGenerator/FreeLand.h" +#include "helpers/EnumRange.h" #include "helpers/containerUtils.h" #include "mapGenerator/Algorithms.h" #include "mapGenerator/TextureHelper.h" @@ -28,14 +29,14 @@ namespace { /// Number of neighbors within an area required for a node to be considered a gap of the area constexpr unsigned minGapNeighbors = 4; - void PlaceDecoration(Map& map, const MapPoint& pt, uint8_t index) + void placeDecoration(Map& map, const MapPoint& pt, uint8_t index) { map.objectTypes[pt] = OT_Decoration; map.objectInfos[pt] = index; } } // namespace -Mountain CloseGaps(const MapExtent& size, const Mountain& area) +Mountain closeGaps(const MapExtent& size, const Mountain& area) { NodeMapBase neighbors; neighbors.Resize(size); @@ -69,47 +70,47 @@ Mountain CloseGaps(const MapExtent& size, const Mountain& area) return closedArea; } -LandFill SelectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned) +LandFill selectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned) { const uint64_t totalAmount = std::accumulate(amounts.begin(), amounts.end(), uint64_t(0)); const uint64_t totalAssigned = std::accumulate(assigned.begin(), assigned.end(), uint64_t(0)); - if(totalAmount == 0 || (totalAssigned == 0 && amounts[static_cast(LandFill::Mountain)] > 0)) + if(totalAmount == 0 || (totalAssigned == 0 && amounts[LandFill::Mountain] > 0)) return LandFill::Mountain; - // A type is behind if its share of assigned nodes doesn't exceed its share of the amounts. This always applies to - // at least one of the types. - std::vector candidates; + // A type is behind if its share of assigned nodes doesn't exceed its share of the amounts. + // This always applies to at least one of the types. + std::vector candidates; unsigned candidateAmount = 0; - for(unsigned i = 0; i < numLandFills; i++) + for(const LandFill type : helpers::EnumRange()) { - if(amounts[i] > 0 && assigned[i] * totalAmount <= totalAssigned * amounts[i]) + if(amounts[type] > 0 && assigned[type] * totalAmount <= totalAssigned * amounts[type]) { - candidates.push_back(i); - candidateAmount += amounts[i]; + candidates.push_back(type); + candidateAmount += amounts[type]; } } if(candidates.size() == 1) - return static_cast(candidates.front()); + return candidates.front(); unsigned value = rnd.RandomValue(1u, candidateAmount); - for(const unsigned candidate : candidates) + for(const LandFill candidate : candidates) { if(value <= amounts[candidate]) - return static_cast(candidate); + return candidate; value -= amounts[candidate]; } - return static_cast(candidates.back()); + return candidates.back(); } -void RaiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area) +void raiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area) { if(area.empty()) return; - const auto footDistance = GetFootDistances(map.size, area); + const auto footDistance = getFootDistances(map.size, area); const unsigned maxDistance = GetMaximum(footDistance, area); // Small hills get less transitions to still contain some desert const unsigned transitions = std::min(desertTransitions, maxDistance); @@ -153,18 +154,18 @@ void RaiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area) if((isCenter && !hasBones) || (isAroundCenter && rnd.ByChance(boneChance))) { - PlaceDecoration(map, pt, rnd.ByChance(50) ? OI_Bone1 : OI_Bone2); + placeDecoration(map, pt, rnd.ByChance(50) ? OI_Bone1 : OI_Bone2); hasBones = true; } } } -void CreateSwamp(Map& map, RandomUtility& rnd, const Mountain& area) +void createSwamp(Map& map, RandomUtility& rnd, const Mountain& area) { if(area.empty()) return; - const auto swamp = map.textureMap.Find(IsImpassable); + const auto swamp = map.textureMap.Find(isImpassable); const uint8_t level = map.z[*std::min_element( area.begin(), area.end(), [&map](const MapPoint& lhs, const MapPoint& rhs) { return map.z[lhs] < map.z[rhs]; })]; @@ -201,12 +202,12 @@ void CreateSwamp(Map& map, RandomUtility& rnd, const Mountain& area) if(distance == 1 && map.objectTypes[pt] == libsiedler2::OT_Empty && rnd.ByChance(deadTreeChance)) { - PlaceDecoration(map, pt, rnd.ByChance(50) ? OI_DeadTree : OI_DeadTreeTrunk); + placeDecoration(map, pt, rnd.ByChance(50) ? OI_DeadTree : OI_DeadTreeTrunk); } } } -void SmoothDesertTransitions(Map& map) +void smoothDesertTransitions(Map& map) { auto& textures = map.textureMap; const auto desert = textures.Find(IsCoastTerrain); @@ -252,7 +253,7 @@ void SmoothDesertTransitions(Map& map) textures.Set(pt, desert); } -std::vector FillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, +std::vector fillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings, const LandFillAmounts& amounts) { std::vector types; @@ -260,29 +261,29 @@ std::vector FillFreeLand(Map& map, RandomUtility& rnd, unsigned moun // The type is required before the area is known as it has an impact on the size const auto getSize = [&](unsigned index) { - const LandFill type = SelectLandFill(rnd, amounts, assigned); - unsigned size = GetMountainSize(settings, index); + const LandFill type = selectLandFill(rnd, amounts, assigned); + unsigned size = getMountainSize(settings, index); if(type == LandFill::Swamp) size = std::max(size / 2, std::min(size, settings.minSize)); types.push_back(type); - assigned[static_cast(type)] += size; + assigned[type] += size; return size; }; - const auto areas = FindMountains(map, rnd, mountainLevel, settings.minFreeLand, getSize); + const auto areas = findMountains(map, rnd, mountainLevel, settings.minFreeLand, getSize); std::vector filledAreas; for(unsigned i = 0; i < areas.size(); i++) { // Gaps would end up as pits within desert hills and as tiny islands within swamps - const Mountain area = types[i] == LandFill::Mountain ? areas[i] : CloseGaps(map.size, areas[i]); + const Mountain area = types[i] == LandFill::Mountain ? areas[i] : closeGaps(map.size, areas[i]); switch(types[i]) { - case LandFill::Mountain: RaiseMountain(map, rnd, area); break; - case LandFill::Desert: RaiseDesertHill(map, rnd, area); break; - case LandFill::Swamp: CreateSwamp(map, rnd, area); break; + case LandFill::Mountain: raiseMountain(map, rnd, area); break; + case LandFill::Desert: raiseDesertHill(map, rnd, area); break; + case LandFill::Swamp: createSwamp(map, rnd, area); break; } filledAreas.push_back(FilledArea{types[i], area}); } diff --git a/libs/s25main/mapGenerator/FreeLand.h b/libs/s25main/mapGenerator/FreeLand.h index ab41f04f89..e5df92f698 100644 --- a/libs/s25main/mapGenerator/FreeLand.h +++ b/libs/s25main/mapGenerator/FreeLand.h @@ -4,10 +4,10 @@ #pragma once +#include "helpers/EnumArray.h" #include "mapGenerator/Map.h" #include "mapGenerator/Mountains.h" #include "mapGenerator/RandomUtility.h" -#include #include namespace rttr::mapGenerator { @@ -19,12 +19,13 @@ enum class LandFill Desert, Swamp }; +constexpr auto maxEnumValue(LandFill) +{ + return LandFill::Swamp; +} -/// Number of different types of landscape to fill free land with -constexpr unsigned numLandFills = 3; - -/// Desired amount of each type of landscape - indexed by LandFill. Only the proportions matter. -using LandFillAmounts = std::array; +/// Desired amount of each type of landscape. Only the proportions matter. +using LandFillAmounts = helpers::EnumArray; struct FilledArea { @@ -40,74 +41,35 @@ constexpr uint8_t OI_DeadTree = 0x06; constexpr uint8_t OI_Bone1 = 0x07; constexpr uint8_t OI_Bone2 = 0x08; -/** - * Closes gaps of an area which has been grown with an irregular shape. Nodes which are mostly surrounded by the area - * become part of it. - * - * @param size size of the map - * @param area nodes of the area - * - * @returns the nodes of the area including its gaps. - */ -Mountain CloseGaps(const MapExtent& size, const Mountain& area); +/// Returns the area extended by all nodes which are mostly surrounded by it. +Mountain closeGaps(const MapExtent& size, const Mountain& area); /** - * Selects the type of landscape for the next area. Types which are below their desired share of nodes are chosen by - * chance according to the desired amounts. This keeps the proportions even for a low number of areas. The first area - * always becomes a mountain (if desired at all) since mountains are required for mining. + * Selects the type of landscape for the next area from the types which are below their desired share. + * The first area is always a mountain (if desired at all) since mountains are required for mining. * - * @param rnd random number generator - * @param amounts desired amount of each type of landscape - * @param assigned number of nodes assigned to each type of landscape so far (indexed by LandFill) - * - * @returns the type of landscape for the next area. A mountain if all amounts are zero. + * @param assigned number of nodes assigned to each type so far */ -LandFill SelectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned); +LandFill selectLandFill(RandomUtility& rnd, const LandFillAmounts& amounts, const LandFillAmounts& assigned); -/** - * Turns the area into a desert hill. The hill is lower than a mountain and rises smoothly towards its center. The outer - * nodes get buildable transition textures while the inner nodes are covered by desert (or the equivalent texture of the - * landscape). Triangles are shared between nodes with the texture closer to the center taking precedence. Bones are - * placed around the center of the hill. - * - * @param map reference to the map to place the hill on (manipulates textures, objects and z-values of the map) - * @param rnd random number generator - * @param area nodes of the desert hill - */ -void RaiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area); +/// Turns the area into a desert hill with buildable transition textures at its foot and some bones around its center. +void raiseDesertHill(Map& map, RandomUtility& rnd, const Mountain& area); -/** - * Turns the area into a swamp (or the equivalent texture of the landscape). All nodes of the swamp are lowered to the - * same height and the surrounding land is lowered to descend to the swamp by one level per node at most. Dead trees are - * placed on the land right next to the swamp. - * - * @param map reference to the map to place the swamp on (manipulates textures, objects and z-values of the map) - * @param rnd random number generator - * @param area nodes of the swamp - */ -void CreateSwamp(Map& map, RandomUtility& rnd, const Mountain& area); +/// Turns the area into a flat swamp surrounded by gently descending land and some dead trees. +void createSwamp(Map& map, RandomUtility& rnd, const Mountain& area); -/** - * Makes sure deserts never touch lush land: the nodes around a desert get the driest land texture followed by one ring - * for each of the next textures unless the land is dry anyway. This function has to be called once all textures are - * in place as texturing coasts may replace the transitions applied by desert hills. - * - * @param map reference to the map to update the textures for - */ -void SmoothDesertTransitions(Map& map); +/// Surrounds deserts by rings of increasingly lush land. Must be called once all textures are in place. +void smoothDesertTransitions(Map& map); /** - * Reduces the size of free land by adding mountains, desert hills and swamps to the map. + * Fills large areas of free land with mountains, desert hills and swamps. * - * @param map reference to the map to fill (manipulates textures, objects and z-values of the map) - * @param rnd random number generator * @param mountainLevel minimum z-value of mountains - * @param settings settings for the size of the new areas - swamps only get half the size as they are impassable - * @param amounts desired amount of each type of landscape + * @param settings size of the new areas - swamps only get half the size as they are impassable * - * @returns the type and the nodes of each new area. Gaps of desert hills and swamps are closed. + * @returns the type and the nodes of each new area. */ -std::vector FillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, +std::vector fillFreeLand(Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings, const LandFillAmounts& amounts); } // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/Mountains.cpp b/libs/s25main/mapGenerator/Mountains.cpp index 293c0d44f4..2cb523b17c 100644 --- a/libs/s25main/mapGenerator/Mountains.cpp +++ b/libs/s25main/mapGenerator/Mountains.cpp @@ -22,7 +22,7 @@ namespace { constexpr unsigned brushDensity = 75; /// Updates the distances after the specified nodes stopped being free land - void MarkAsOccupied(NodeMapBase& freeLand, const Mountain& mountain) + void markAsOccupied(NodeMapBase& freeLand, const Mountain& mountain) { std::queue queue; for(const MapPoint& pt : mountain) @@ -50,13 +50,13 @@ namespace { } } // namespace -unsigned GetMountainSize(const MountainSettings& settings, unsigned index) +unsigned getMountainSize(const MountainSettings& settings, unsigned index) { const auto size = static_cast(settings.maxSize * std::pow(sizeDecay, index)); return std::max(size, settings.minSize); } -NodeMapBase GetFreeLandDistances(const Map& map, unsigned mountainLevel) +NodeMapBase getFreeLandDistances(const Map& map, unsigned mountainLevel) { const auto& textures = map.getTextures(); return DistancesTo(map.size, [&map, &textures, mountainLevel](const MapPoint& pt) { @@ -64,7 +64,7 @@ NodeMapBase GetFreeLandDistances(const Map& map, unsigned mountainLeve }); } -Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, +Mountain growMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, unsigned margin) { const auto priority = [&freeLand, ¢er](const MapPoint& pt) { @@ -96,18 +96,18 @@ Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, return mountain; } -std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, +std::vector findMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings) { - return FindMountains(map, rnd, mountainLevel, settings.minFreeLand, - [&settings](unsigned index) { return GetMountainSize(settings, index); }); + return findMountains(map, rnd, mountainLevel, settings.minFreeLand, + [&settings](unsigned index) { return getMountainSize(settings, index); }); } -std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, +std::vector findMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, const std::function& getSize) { std::vector mountains; - auto freeLand = GetFreeLandDistances(map, mountainLevel); + auto freeLand = getFreeLandDistances(map, mountainLevel); const auto maxDistance = [&freeLand] { return *std::max_element(freeLand.begin(), freeLand.end()); }; for(unsigned distance = maxDistance(); distance > minFreeLand; distance = maxDistance()) @@ -120,14 +120,14 @@ std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned const unsigned margin = rnd.RandomValue(minFreeLand, maxMargin); const unsigned size = getSize(static_cast(mountains.size())); - mountains.push_back(GrowMountain(freeLand, rnd, center, size, margin)); - MarkAsOccupied(freeLand, mountains.back()); + mountains.push_back(growMountain(freeLand, rnd, center, size, margin)); + markAsOccupied(freeLand, mountains.back()); } return mountains; } -NodeMapBase GetFootDistances(const MapExtent& size, const Mountain& mountain) +NodeMapBase getFootDistances(const MapExtent& size, const Mountain& mountain) { NodeMapBase neighbors; neighbors.Resize(size); @@ -139,9 +139,9 @@ NodeMapBase GetFootDistances(const MapExtent& size, const Mountain& mo return Distances(size, mountain, 0, isFoot); } -void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) +void raiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) { - const auto footDistance = GetFootDistances(map.size, mountain); + const auto footDistance = getFootDistances(map.size, mountain); const ValueRange range(0u, GetMaximum(footDistance, mountain)); const auto textures = map.textureMap.FindAll(IsMinableMountain); @@ -153,14 +153,14 @@ void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain) } } -std::vector CreateMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, +std::vector createMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings) { - auto mountains = FindMountains(map, rnd, mountainLevel, settings); + auto mountains = findMountains(map, rnd, mountainLevel, settings); for(const Mountain& mountain : mountains) { - RaiseMountain(map, rnd, mountain); + raiseMountain(map, rnd, mountain); } return mountains; diff --git a/libs/s25main/mapGenerator/Mountains.h b/libs/s25main/mapGenerator/Mountains.h index 221f6e0d98..14107ea5f6 100644 --- a/libs/s25main/mapGenerator/Mountains.h +++ b/libs/s25main/mapGenerator/Mountains.h @@ -24,103 +24,43 @@ struct MountainSettings unsigned minSize; }; -/** - * Computes the desired number of nodes of a mountain. - * - * @param settings size limits for mountains - * @param index index of the mountain - the first mountain has index 0 - * - * @returns the desired size which decreases with the index but never drops below the minimum size. - */ -unsigned GetMountainSize(const MountainSettings& settings, unsigned index); +/// Desired number of nodes of the mountain with the given index (0 = first). Decreases with the index down to minSize. +unsigned getMountainSize(const MountainSettings& settings, unsigned index); -/** - * Computes for each node the distance to the closest node which is not free land. Nodes are not considered free land - * if they are water, reach the mountain level or already have a texture (e.g. rivers and islands). - * - * @param map map to compute the distances for - * @param mountainLevel minimum z-value of mountains - * - * @returns the distance of each node to the closest node which isn't free land. - */ -NodeMapBase GetFreeLandDistances(const Map& map, unsigned mountainLevel); +/// Distance of each node to the closest node which isn't free land, i.e. water, mountain or already textured. +NodeMapBase getFreeLandDistances(const Map& map, unsigned mountainLevel); /** - * Grows a mountain around the specified center. Nodes close to the center and far away from other obstacles are - * preferred. Skipping nodes by chance creates an irregular shape. + * Grows an irregular mountain around the center preferring nodes far away from obstacles. * * @param freeLand distances to the closest node which isn't free land - * @param rnd random number generator - * @param center center of the mountain which is always part of the mountain - * @param size maximum number of nodes of the mountain - * @param margin only nodes with a distance greater than this value become part of the mountain - * - * @returns all nodes of the new mountain. + * @param size maximum number of nodes + * @param margin only nodes with a larger distance to obstacles become part of the mountain */ -Mountain GrowMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, +Mountain growMountain(const NodeMapBase& freeLand, RandomUtility& rnd, const MapPoint& center, unsigned size, unsigned margin); /** - * Selects the nodes of new mountains until no node is further away from water or mountains than the minimum of free - * land to keep. Every mountain starts at the node with the largest distance to water and mountains. As nodes close to - * obstacles are never selected, passages between areas of the map stay open. - * - * @param map map to search for free land (is not modified) - * @param rnd random number generator - * @param mountainLevel minimum z-value of mountains - * @param settings settings of the new mountains + * Selects the nodes of new mountains until no node is further away from obstacles than settings.minFreeLand. + * Nodes close to obstacles are never selected so passages stay open. Does not modify the map. * - * @returns the nodes of each new mountain. All mountains are disjoint. + * @returns the nodes of each mountain. All mountains are disjoint. */ -std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, +std::vector findMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings); -/** - * Selects the nodes of new mountains just like the overload above but asks for the size of each mountain right before - * growing it. This allows callers to vary the size, e.g. depending on what the nodes are going to be used for. - * - * @param map map to search for free land (is not modified) - * @param rnd random number generator - * @param mountainLevel minimum z-value of mountains - * @param minFreeLand land up to this distance to water or mountains stays free - * @param getSize returns the desired number of nodes for the mountain with the specified index - * - * @returns the nodes of each new mountain. All mountains are disjoint. - */ -std::vector FindMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, +/// Same as above but queries the size of each mountain (by its index) right before growing it. +std::vector findMountains(const Map& map, RandomUtility& rnd, unsigned mountainLevel, unsigned minFreeLand, const std::function& getSize); -/** - * Computes the distance of each node of the mountain to the foot of the mountain. The foot consists of all nodes of the - * mountain with at least one neighbor which isn't part of the mountain. - * - * @param size size of the map - * @param mountain nodes of the mountain - * - * @returns the distance to the foot for each node of the mountain and zero for all other nodes. - */ -NodeMapBase GetFootDistances(const MapExtent& size, const Mountain& mountain); +/// Distance of each node of the mountain to its foot (its outermost nodes). Zero for nodes outside the mountain. +NodeMapBase getFootDistances(const MapExtent& size, const Mountain& mountain); -/** - * Elevates the nodes of the mountain relative to the surrounding land and applies mountain textures to them. - * - * @param map reference to the map to place the mountain on (manipulates textures and z-values of the map) - * @param rnd random number generator - * @param mountain nodes of the mountain - */ -void RaiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain); +/// Elevates the nodes of the mountain and applies mountain textures to them. +void raiseMountain(Map& map, RandomUtility& rnd, const Mountain& mountain); -/** - * Reduces the size of free land by adding new mountains to the map. - * - * @param map reference to the map to place the mountains on (manipulates textures and z-values of the map) - * @param rnd random number generator - * @param mountainLevel minimum z-value of mountains - * @param settings settings of the new mountains - * - * @returns the nodes of each new mountain. - */ -std::vector CreateMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, +/// Fills large areas of free land with mountains and returns their nodes. +std::vector createMountains(Map& map, RandomUtility& rnd, unsigned mountainLevel, const MountainSettings& settings); } // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/RandomMap.cpp b/libs/s25main/mapGenerator/RandomMap.cpp index 6d491a621b..bb6a35d864 100644 --- a/libs/s25main/mapGenerator/RandomMap.cpp +++ b/libs/s25main/mapGenerator/RandomMap.cpp @@ -82,7 +82,7 @@ unsigned GetIslandSize(const MapExtent& size) return 1200; } -unsigned GetMaxMountainSize(const MapExtent& size) +unsigned getMaxMountainSize(const MapExtent& size) { const unsigned combinedSize = size.x * size.y; if(combinedSize <= 64 * 64) @@ -97,7 +97,7 @@ unsigned GetMaxMountainSize(const MapExtent& size) return 1500; } -unsigned GetMinFreeLand(unsigned mountainDistance, unsigned mountains) +unsigned getMinFreeLand(unsigned mountainDistance, unsigned mountains) { constexpr unsigned minFreeLand = 4; // Headquarters and the transition textures around mountains need space const unsigned freeLandPercentage = 2 * (100 - std::min(mountains, 100u)); @@ -166,7 +166,7 @@ void RandomMap::Create(const MapSettings& settings) } if(settings_.specialObjects) - AddSpecialScenes(map_, rnd_); + addSpecialScenes(map_, rnd_); AddObjects(map_, rnd_, settings_); AddResources(map_, rnd_, settings_); @@ -208,23 +208,23 @@ void RandomMap::CreateFreeIslands(unsigned waterNodes) } } -void RandomMap::FillFreeLand(unsigned mountainLevel) +void RandomMap::fillFreeLand(unsigned mountainLevel) { const LandFillAmounts amounts{settings_.mountains, settings_.desert, settings_.swamp}; const unsigned totalAmount = settings_.mountains + settings_.desert + settings_.swamp; if(totalAmount == 0) return; - const auto freeLand = GetMinFreeLand(static_cast(settings_.mountainDistance), totalAmount); + const auto freeLand = getMinFreeLand(static_cast(settings_.mountainDistance), totalAmount); const auto minFreeLand = rnd_.RandomValue(freeLand, freeLand + 5u); - const auto maxMountainSize = GetMaxMountainSize(map_.size); + const auto maxMountainSize = getMaxMountainSize(map_.size); const auto maxSize = rnd_.RandomValue(maxMountainSize / 2, maxMountainSize); - rttr::mapGenerator::FillFreeLand(map_, rnd_, mountainLevel, + rttr::mapGenerator::fillFreeLand(map_, rnd_, mountainLevel, MountainSettings{minFreeLand, maxSize, maxMountainSize / 4}, amounts); } -void RandomMap::AddTextures(unsigned mountainLevel) +void RandomMap::addTextures(unsigned mountainLevel) { const NodeMapBase textures = map_.getTextures(); @@ -239,7 +239,7 @@ void RandomMap::AddTextures(unsigned mountainLevel) } } - SmoothDesertTransitions(map_); + smoothDesertTransitions(map_); } void RandomMap::CreateMixedMap() @@ -266,9 +266,9 @@ void RandomMap::CreateMixedMap() CreateFreeIslands(waterNodes); - FillFreeLand(mountainLevel); + fillFreeLand(mountainLevel); - AddTextures(mountainLevel); + addTextures(mountainLevel); PlaceHarbors(map_, rivers); PlaceHeadquarters(map_, rnd_, map_.players, settings_.mountainDistance); @@ -312,9 +312,9 @@ void RandomMap::CreateWaterMap() const auto rivers = CreateRivers(center); - FillFreeLand(mountainLevel); + fillFreeLand(mountainLevel); - AddTextures(mountainLevel); + addTextures(mountainLevel); PlaceHarbors(map_, rivers); @@ -338,9 +338,9 @@ void RandomMap::CreateLandMap() const auto mountainLevel = LimitFor(map_.z, land, static_cast(1)) + 1; CreateRivers(); - FillFreeLand(mountainLevel); + fillFreeLand(mountainLevel); - AddTextures(mountainLevel); + addTextures(mountainLevel); PlaceHeadquarters(map_, rnd_, map_.players, settings_.mountainDistance); } diff --git a/libs/s25main/mapGenerator/RandomMap.h b/libs/s25main/mapGenerator/RandomMap.h index 01c5c26703..2d7e6d1b33 100644 --- a/libs/s25main/mapGenerator/RandomMap.h +++ b/libs/s25main/mapGenerator/RandomMap.h @@ -17,19 +17,15 @@ unsigned GetMaximumHeight(const MapExtent& size); unsigned GetCoastline(const MapExtent& size); unsigned GetIslandRadius(const MapExtent& size); unsigned GetIslandSize(const MapExtent& size); -unsigned GetMaxMountainSize(const MapExtent& size); +unsigned getMaxMountainSize(const MapExtent& size); /** - * Computes the amount of free land to keep around water and mountains. + * Computes the distance to water and mountains up to which land stays free. * - * @param mountainDistance desired distance of headquarters to mountains - * @param mountains desired amount of mountains, deserts and swamps in percentage (between 0 and 100) - an amount of - * 50 keeps the desired distance available while higher values reduce the free land down to the space required by - * headquarters - * - * @returns the distance to water and mountains up to which land stays free. + * @param mountains desired amount of mountains, deserts and swamps in percent. 50 keeps the mountain distance while + * higher values reduce it down to the space required by headquarters. */ -unsigned GetMinFreeLand(unsigned mountainDistance, unsigned mountains); +unsigned getMinFreeLand(unsigned mountainDistance, unsigned mountains); unsigned GetSmoothRadius(const MapExtent& size); unsigned GetSmoothIterations(const MapExtent& size); @@ -46,8 +42,8 @@ class RandomMap std::vector CreateRivers(MapPoint source = MapPoint::Invalid()); void CreateFreeIslands(unsigned waterNodes); - void FillFreeLand(unsigned mountainLevel); - void AddTextures(unsigned mountainLevel); + void fillFreeLand(unsigned mountainLevel); + void addTextures(unsigned mountainLevel); void CreateMixedMap(); void CreateLandMap(); void CreateWaterMap(); diff --git a/libs/s25main/mapGenerator/SpecialObjects.cpp b/libs/s25main/mapGenerator/SpecialObjects.cpp index 0a9c3d356e..69bb3fe51f 100644 --- a/libs/s25main/mapGenerator/SpecialObjects.cpp +++ b/libs/s25main/mapGenerator/SpecialObjects.cpp @@ -92,16 +92,16 @@ namespace { bool deadVegetation; }; - Objects Join(Objects lhs, const Objects& rhs) + Objects join(Objects lhs, const Objects& rhs) { lhs.insert(lhs.end(), rhs.begin(), rhs.end()); return lhs; } - Theme MakeTheme(SceneType type, bool isWinter) + Theme makeTheme(SceneType type, bool isWinter) { // Dry ground gets cacti unless it's frozen - const Objects dryPlants = isWinter ? pebbles : Join(pebbles, cacti); + const Objects dryPlants = isWinter ? pebbles : join(pebbles, cacti); switch(type) { @@ -118,7 +118,7 @@ namespace { false, false}; case SceneType::Graveyard: - return {{OI_Cross}, {OI_Cross}, Join(bones, mushrooms), Ground::Dry, Ground::Dry, false, false, + return {{OI_Cross}, {OI_Cross}, join(bones, mushrooms), Ground::Dry, Ground::Dry, false, false, false, true}; case SceneType::Camp: return {{OI_Tent}, dryPlants, dryPlants, Ground::Dry, Ground::None, false, true, false, false}; @@ -167,9 +167,9 @@ namespace { /// known to be free. Water, mountains and other landscapes further out are left untouched and cut the scene. std::set free_; - static unsigned Mod6(int dir) { return static_cast((dir % 6 + 6) % 6); } + static unsigned mod6(int dir) { return static_cast((dir % 6 + 6) % 6); } - std::pair Planar(const MapPoint& from, const MapPoint& to) const + std::pair planar(const MapPoint& from, const MapPoint& to) const { const auto delta = [](int value, int size) { if(value > size / 2) @@ -183,15 +183,15 @@ namespace { } /// Index into the ramp of the land texture of the node - int RampIndex(const MapPoint& pt) const + int rampIndex(const MapPoint& pt) const { const auto it = std::find(ramp_.begin(), ramp_.end(), map_.getTextures()[pt].rsu); return it != ramp_.end() ? static_cast(it - ramp_.begin()) : static_cast(ramp_.size() / 2); } - int Target(Ground ground) const { return ground == Ground::Dry ? 0 : static_cast(ramp_.size()) - 1; } + int target(Ground ground) const { return ground == Ground::Dry ? 0 : static_cast(ramp_.size()) - 1; } - DescIdx OverlayTexture(Overlay overlay) const + DescIdx overlayTexture(Overlay overlay) const { const auto isRock = [](const TerrainDesc& t) { return t.Is(ETerrain::Buildable) && t.s2Id == rockTextureId; @@ -208,18 +208,18 @@ namespace { public: SceneBuilder(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type) : map_(map), rnd_(rnd), center_(center), - theme_(MakeTheme(type, map.textureMap.GetLandscapeId() == winterLandscapeId)), + theme_(makeTheme(type, map.textureMap.GetLandscapeId() == winterLandscapeId)), tree_(rnd.RandomItem(CreateTrees(map.textureMap))), otherTree_(rnd.RandomItem(CreateTrees(map.textureMap))) { - const auto area = map.z.GetPointsInRadiusWithCenter(center, sceneRadius); - for(const MapPoint& pt : area) + const auto sceneArea = map.z.GetPointsInRadiusWithCenter(center, sceneRadius); + for(const MapPoint& pt : sceneArea) { if(map.textureMap.All(pt, IsBuildableLand) && map.objectTypes[pt] == libsiedler2::OT_Empty && !helpers::contains(map.barren, pt)) free_.insert(pt); } - map.reserved.insert(area.begin(), area.end()); + map.reserved.insert(sceneArea.begin(), sceneArea.end()); overlay_.Resize(map.size, Overlay::None); // Flowers are the most lush texture. The index makes the order independent from the sort algorithm. @@ -232,85 +232,85 @@ namespace { const int level = map.z[center]; for(const MapPoint& pt : free_) { - const int tolerance = std::max(0, static_cast(Distance(pt)) - static_cast(levelRadius)); + const int tolerance = std::max(0, static_cast(distance(pt)) - static_cast(levelRadius)); map.z[pt] = - static_cast(helpers::clamp(map.z[pt], std::max(level - tolerance, MinZ()), + static_cast(helpers::clamp(map.z[pt], std::max(level - tolerance, minZ()), std::min(level + tolerance, map.height.maximum))); } } - const MapPoint& Center() const { return center_; } - const Theme& GetTheme() const { return theme_; } - RandomUtility& Rnd() { return rnd_; } - int MinZ() const { return map_.height.minimum + 1; } - unsigned Distance(const MapPoint& pt) const { return map_.z.CalcDistance(center_, pt); } - unsigned Distance(const MapPoint& from, const MapPoint& to) const { return map_.z.CalcDistance(from, to); } - bool IsWater(const MapPoint& pt) const { return overlay_[pt] == Overlay::Water; } - bool IsFree(const MapPoint& pt) const { return helpers::contains(free_, pt); } + const MapPoint& center() const { return center_; } + const Theme& getTheme() const { return theme_; } + RandomUtility& rnd() { return rnd_; } + int minZ() const { return map_.height.minimum + 1; } + unsigned distance(const MapPoint& pt) const { return map_.z.CalcDistance(center_, pt); } + unsigned distance(const MapPoint& from, const MapPoint& to) const { return map_.z.CalcDistance(from, to); } + bool isWater(const MapPoint& pt) const { return overlay_[pt] == Overlay::Water; } + bool isFree(const MapPoint& pt) const { return helpers::contains(free_, pt); } /// Moves from the node into the direction which is counted clockwise starting in the west - MapPoint Step(MapPoint pt, int dir, unsigned steps = 1) const + MapPoint step(MapPoint pt, int dir, unsigned steps = 1) const { for(unsigned i = 0; i < steps; i++) - pt = map_.z.GetNeighbour(pt, Direction(Mod6(dir))); + pt = map_.z.GetNeighbour(pt, Direction(mod6(dir))); return pt; } /// All nodes with exactly the specified distance to the center in circular order starting in the west - std::vector Ring(unsigned radius) const + std::vector ring(unsigned radius) const { - std::vector ring; - MapPoint pt = Step(center_, 0, radius); + std::vector result; + MapPoint pt = step(center_, 0, radius); for(int dir = 2; dir < 8; dir++) { for(unsigned i = 0; i < radius; i++) { - ring.push_back(pt); - pt = Step(pt, dir); + result.push_back(pt); + pt = step(pt, dir); } } - return ring; + return result; } - std::vector Area(const MapPoint& pt, unsigned radius) const + std::vector area(const MapPoint& pt, unsigned radius) const { - auto area = map_.z.GetPointsInRadiusWithCenter(pt, radius); - helpers::erase_if(area, [this](const MapPoint& p) { return !IsFree(p); }); - return area; + auto result = map_.z.GetPointsInRadiusWithCenter(pt, radius); + helpers::erase_if(result, [this](const MapPoint& p) { return !isFree(p); }); + return result; } - double Angle(const MapPoint& from, const MapPoint& to) const + double angle(const MapPoint& from, const MapPoint& to) const { - const auto planar = Planar(from, to); - return std::atan2(planar.second, planar.first); + const auto [x, y] = planar(from, to); + return std::atan2(y, x); } - double RandomAngle() { return rnd_.RandomDouble(-pi, pi); } + double randomAngle() { return rnd_.RandomDouble(-pi, pi); } - Blob MakeBlob(const MapPoint& center, double radius, bool irregular = true) + Blob makeBlob(const MapPoint& center, double radius, bool irregular = true) { - return Blob{center, radius, irregular ? 1. : 0., RandomAngle(), RandomAngle()}; + return Blob{center, radius, irregular ? 1. : 0., randomAngle(), randomAngle()}; } /// Relative position within a blob: 0 at the center, 1 at the outline and above 1 outside - double Eval(const Blob& blob, const MapPoint& pt) const + double eval(const Blob& blob, const MapPoint& pt) const { - const double angle = Angle(blob.center, pt); + const double theta = angle(blob.center, pt); const double deformation = - 0.22 * std::sin(2 * angle + blob.phase1) + 0.13 * std::sin(3 * angle + blob.phase2); - return Distance(blob.center, pt) / (blob.radius * (1. + blob.amplitude * deformation)); + 0.22 * std::sin(2 * theta + blob.phase1) + 0.13 * std::sin(3 * theta + blob.phase2); + return distance(blob.center, pt) / (blob.radius * (1. + blob.amplitude * deformation)); } - std::vector Nodes(const Blob& blob, double limit = 1.) const + std::vector nodes(const Blob& blob, double limit = 1.) const { - auto nodes = Area(blob.center, static_cast(blob.radius * 1.4) + 1); - helpers::erase_if(nodes, [this, &blob, limit](const MapPoint& pt) { return Eval(blob, pt) >= limit; }); - return nodes; + auto result = area(blob.center, static_cast(blob.radius * 1.4) + 1); + helpers::erase_if(result, [this, &blob, limit](const MapPoint& pt) { return eval(blob, pt) >= limit; }); + return result; } /// Requests the ground for the blob. Later requests take precedence where grounds meet - except for the /// ground around the special object which always wins. - void Blend(const Blob& blob, Ground ground, bool isCore = false) + void blend(const Blob& blob, Ground ground, bool isCore = false) { if(ground == Ground::None) return; @@ -318,97 +318,97 @@ namespace { { // Rock never touches the land directly but is enclosed by a rim // Only the ground of the special object itself reaches the middle of the scene - const auto isAllowed = [this, isCore](const MapPoint& pt) { return isCore || Distance(pt) > 1; }; - for(const MapPoint& pt : Nodes(blob, 0.75)) + const auto isAllowed = [this, isCore](const MapPoint& pt) { return isCore || distance(pt) > 1; }; + for(const MapPoint& pt : nodes(blob, 0.75)) { if(!isAllowed(pt)) continue; - SetOverlay(pt, Overlay::Rock); - for(const MapPoint& neighbor : Area(pt, 1)) + setOverlay(pt, Overlay::Rock); + for(const MapPoint& neighbor : area(pt, 1)) { if(isAllowed(neighbor)) - SetOverlay(neighbor, Overlay::RockRim); + setOverlay(neighbor, Overlay::RockRim); } } return; } // Only the inner part gets the ground - the transition to the surrounding land is added in the end - for(const MapPoint& pt : Nodes(blob, 0.75)) - (isCore ? coreGround_ : ground_).emplace_back(pt, Target(ground)); + for(const MapPoint& pt : nodes(blob, 0.75)) + (isCore ? coreGround_ : ground_).emplace_back(pt, target(ground)); } - void SetOverlay(const MapPoint& pt, Overlay overlay) + void setOverlay(const MapPoint& pt, Overlay overlay) { - if(IsFree(pt) && Distance(pt) < sceneRadius - 1 && overlay > overlay_[pt]) + if(isFree(pt) && distance(pt) < sceneRadius - 1 && overlay > overlay_[pt]) overlay_[pt] = overlay; } /// Raises (or lowers) a smooth hill around the node - void Bump(const MapPoint& top, int height, double radius) + void bump(const MapPoint& top, int height, double radius) { - for(const MapPoint& pt : Area(top, static_cast(radius))) + for(const MapPoint& pt : area(top, static_cast(radius))) { - const double position = Distance(top, pt) / radius; + const double position = distance(top, pt) / radius; if(position >= 1.) continue; const int elevation = static_cast(std::lround(height * 0.5 * (1. + std::cos(pi * position)))); - map_.z[pt] = static_cast(std::clamp(map_.z[pt] + elevation, MinZ(), map_.height.maximum)); + map_.z[pt] = static_cast(std::clamp(map_.z[pt] + elevation, minZ(), map_.height.maximum)); } } - void Block(const MapPoint& pt) { blocked_.insert(pt); } + void block(const MapPoint& pt) { blocked_.insert(pt); } - bool CanPlace(const MapPoint& pt) const + bool canPlace(const MapPoint& pt) const { - return IsFree(pt) && map_.objectTypes[pt] == libsiedler2::OT_Empty && !helpers::contains(blocked_, pt) - && !IsWater(pt); + return isFree(pt) && map_.objectTypes[pt] == libsiedler2::OT_Empty && !helpers::contains(blocked_, pt) + && !isWater(pt); } - bool Chance(unsigned chance) { return chance >= 100 || rnd_.ByChance(chance); } + bool chance(unsigned percentage) { return percentage >= 100 || rnd_.ByChance(percentage); } - void Place(const MapPoint& pt, const Objects& objects) + void place(const MapPoint& pt, const Objects& objects) { - if(!CanPlace(pt)) + if(!canPlace(pt)) return; map_.objectTypes[pt] = OT_Decoration; map_.objectInfos[pt] = rnd_.RandomItem(objects); } - void PlaceTree(const MapPoint& pt) + void placeTree(const MapPoint& pt) { - if(!CanPlace(pt)) + if(!canPlace(pt)) return; const Tree& tree = rnd_.ByChance(75) ? tree_ : otherTree_; map_.objectTypes[pt] = tree.type; map_.objectInfos[pt] = tree.index + rnd_.RandomValue(0, 7); } - void PlaceGranite(const MapPoint& pt) + void placeGranite(const MapPoint& pt) { - if(!CanPlace(pt)) + if(!canPlace(pt)) return; map_.objectTypes[pt] = rnd_.ByChance(50) ? libsiedler2::OT_Stone1 : libsiedler2::OT_Stone2; map_.objectInfos[pt] = static_cast(rnd_.RandomValue(2, 6)); } /// Trees mixed with some undergrowth or dead trees for gloomy themes - void PlaceVegetation(const MapPoint& pt) + void placeVegetation(const MapPoint& pt) { if(theme_.deadVegetation) - Place(pt, rnd_.ByChance(80) ? deadTrees : mushrooms); + place(pt, rnd_.ByChance(80) ? deadTrees : mushrooms); else if(rnd_.ByChance(85)) - PlaceTree(pt); + placeTree(pt); else - Place(pt, undergrowth); + place(pt, undergrowth); } /// Trees of a single kind for rows or dead trees for gloomy themes - void PlaceRowTree(const MapPoint& pt) + void placeRowTree(const MapPoint& pt) { if(theme_.deadVegetation) - Place(pt, deadTrees); - else if(CanPlace(pt)) + place(pt, deadTrees); + else if(canPlace(pt)) { map_.objectTypes[pt] = tree_.type; map_.objectInfos[pt] = tree_.index + rnd_.RandomValue(0, 7); @@ -416,115 +416,112 @@ namespace { } /// Large rocks or the props of the theme - void PlaceRock(const MapPoint& pt) + void placeRock(const MapPoint& pt) { if(theme_.granite && rnd_.ByChance(60)) - PlaceGranite(pt); + placeGranite(pt); else - Place(pt, theme_.granite ? stones : theme_.props); + place(pt, theme_.granite ? stones : theme_.props); } /// Flowers mixed with other small plants or the litter of the theme - void PlaceGroundCover(const MapPoint& pt) { Place(pt, theme_.flowers ? plants : theme_.litter); } + void placeGroundCover(const MapPoint& pt) { place(pt, theme_.flowers ? plants : theme_.litter); } /// Calls the function by chance for the nodes of a sector around the center template - void ForSector(double angle, double halfWidth, unsigned minDistance, unsigned maxDistance, unsigned chance, - T_Place&& place) + void forSector(double direction, double halfWidth, unsigned minDistance, unsigned maxDistance, + unsigned percentage, T_Place&& action) { - for(const MapPoint& pt : Area(center_, maxDistance)) + for(const MapPoint& pt : area(center_, maxDistance)) { - if(Distance(pt) < minDistance) + if(distance(pt) < minDistance) continue; - double difference = std::fabs(Angle(center_, pt) - angle); + double difference = std::fabs(angle(center_, pt) - direction); difference = std::min(difference, 2 * pi - difference); - if(difference <= halfWidth && Chance(chance)) - place(pt); + if(difference <= halfWidth && chance(percentage)) + action(pt); } } /// Calls the function by chance for the nodes of a blob template - void ForBlob(const Blob& blob, double limit, unsigned chance, T_Place&& place) + void forBlob(const Blob& blob, double limit, unsigned percentage, T_Place&& action) { - for(const MapPoint& pt : Nodes(blob, limit)) + for(const MapPoint& pt : nodes(blob, limit)) { - if(Chance(chance)) - place(pt); + if(chance(percentage)) + action(pt); } } /// Places the special object and the objects which make the type of scene recognizable in any style - void PlaceCore(SceneType type) + void placeCore(SceneType type) { map_.objectTypes[center_] = OT_Decoration; map_.objectInfos[center_] = rnd_.RandomItem(theme_.main); - Block(center_); - const auto innerRing = Ring(1); - const auto ring = Ring(2); + block(center_); + const auto innerRing = ring(1); + const auto outerRing = ring(2); switch(type) { case SceneType::CastleRuin: // The ruin covers some of its neighbors for(const int dir : {0, 1, 2}) - Block(Step(center_, dir)); + block(step(center_, dir)); break; case SceneType::Graveyard: // Three rows of graves for(const unsigned index : {0u, 3u}) - Place(innerRing[index], theme_.main); + place(innerRing[index], theme_.main); for(const unsigned index : {2u, 3u, 4u, 8u, 9u, 10u}) - Place(ring[index], theme_.main); + place(outerRing[index], theme_.main); break; - case SceneType::Camp: Place(innerRing[rnd_.Index(innerRing.size())], {OI_PileOfWood}); break; + case SceneType::Camp: place(innerRing[rnd_.Index(innerRing.size())], {OI_PileOfWood}); break; case SceneType::StoneCircle: - for(unsigned i = 0; i < ring.size(); i += 2) - Place(ring[i], standingStones); + for(unsigned i = 0; i < outerRing.size(); i += 2) + place(outerRing[i], standingStones); break; default: break; } } /// Applies the collected ground to the map and makes sure the special object stands on even ground - void Finish() + void finish() { FlattenForCastleBuilding(map_.z, center_); // Ponds are flat - const auto area = map_.z.GetPointsInRadiusWithCenter(center_, sceneRadius); + const auto sceneArea = map_.z.GetPointsInRadiusWithCenter(center_, sceneRadius); int waterLevel = map_.height.maximum; - for(const MapPoint& pt : area) + for(const MapPoint& pt : sceneArea) { - if(IsWater(pt)) + if(isWater(pt)) waterLevel = std::min(waterLevel, map_.z[pt]); } - for(const MapPoint& pt : area) + for(const MapPoint& pt : sceneArea) { - if(IsWater(pt)) + if(isWater(pt)) map_.z[pt] = static_cast(waterLevel); } ground_.insert(ground_.end(), coreGround_.begin(), coreGround_.end()); - PaintGround(area); + paintGround(sceneArea); for(const Overlay overlay : {Overlay::RockRim, Overlay::Rock, Overlay::Sand, Overlay::Water}) { - for(const MapPoint& pt : area) + for(const MapPoint& pt : sceneArea) { if(overlay_[pt] == overlay) - map_.textureMap.Set(pt, OverlayTexture(overlay)); + map_.textureMap.Set(pt, overlayTexture(overlay)); } } } private: - /** - * Paints the requested grounds and adds transitions: the textures of neighboring nodes never differ by more - * than one step of the ramp from dry to lush land. A patch of dry ground within meadow is therefore surrounded - * by one ring for each texture in between - just like coasts. Sand counts as one step below the driest land. - * The land around the scene, the shores and later requests keep their texture where requests are in conflict. - */ - void PaintGround(const std::vector& area) + /// Paints the requested grounds with transitions: neighboring nodes differ by at most one step of the ramp. + /// Sand counts as one step below the driest land. On conflicts the surrounding land, shores and later requests + /// win. + void paintGround(const std::vector& sceneArea) { struct Anchor { @@ -541,10 +538,10 @@ namespace { int upper = 100; for(const Anchor& anchor : anchors) { - const int distance = static_cast(Distance(anchor.pt, pt)); - lower = std::max(lower, - (anchor.isBorder ? std::min(anchor.index, firstMeadow) : anchor.index) - distance); - upper = std::min(upper, anchor.index + distance); + const int dist = static_cast(distance(anchor.pt, pt)); + lower = + std::max(lower, (anchor.isBorder ? std::min(anchor.index, firstMeadow) : anchor.index) - dist); + upper = std::min(upper, anchor.index + dist); } // Anchors may contradict each other if the land around the scene has no transitions itself return lower > upper ? (lower + upper) / 2 : std::clamp(index, lower, upper); @@ -556,40 +553,40 @@ namespace { if(!visited.insert(pt).second) return; const Anchor anchor{pt, limit(pt, index)}; - if(paint && anchor.index != RampIndex(pt)) + if(paint && anchor.index != rampIndex(pt)) painted.push_back(anchor); anchors.push_back(anchor); }; // The land right outside the scene is not painted - for(const MapPoint& pt : Ring(sceneRadius + 1)) + for(const MapPoint& pt : ring(sceneRadius + 1)) { visited.insert(pt); - anchors.push_back(Anchor{pt, RampIndex(pt), true}); + anchors.push_back(Anchor{pt, rampIndex(pt), true}); } - for(const MapPoint& pt : area) + for(const MapPoint& pt : sceneArea) { if(overlay_[pt] == Overlay::Sand || overlay_[pt] == Overlay::Water) addAnchor(pt, -1, false); } for(auto it = ground_.rbegin(); it != ground_.rend(); ++it) { - if(IsFree(it->first)) + if(isFree(it->first)) addAnchor(it->first, it->second, true); } // All other nodes keep their texture unless a transition is required - for(const MapPoint& pt : area) + for(const MapPoint& pt : sceneArea) { - if(visited.count(pt) || !IsFree(pt)) + if(visited.count(pt) || !isFree(pt)) continue; - const int index = limit(pt, RampIndex(pt)); - if(index != RampIndex(pt)) + const int index = limit(pt, rampIndex(pt)); + if(index != rampIndex(pt)) painted.push_back(Anchor{pt, index}); } // Neighboring nodes share triangles: the texture which differs most from the land is painted last. Thus // patches keep their size and each transition gets a row of triangles. - const auto change = [this](const Anchor& anchor) { return std::abs(anchor.index - RampIndex(anchor.pt)); }; + const auto change = [this](const Anchor& anchor) { return std::abs(anchor.index - rampIndex(anchor.pt)); }; std::stable_sort(painted.begin(), painted.end(), [&change](const Anchor& lhs, const Anchor& rhs) { return change(lhs) < change(rhs); }); for(const Anchor& anchor : painted) @@ -601,80 +598,80 @@ namespace { }; /// Irregular patch with a cluster of rocks, a clump of trees and a drift of flowers in different directions - void CreateOvergrown(SceneBuilder& scene) + void createOvergrown(SceneBuilder& scene) { - const Theme& theme = scene.GetTheme(); - const MapPoint center = scene.Center(); + const Theme& theme = scene.getTheme(); + const MapPoint center = scene.center(); - scene.Blend(scene.MakeBlob(center, 5.2), theme.surround); - scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 2.0 : 2.8), theme.core, true); + scene.blend(scene.makeBlob(center, 5.2), theme.surround); + scene.blend(scene.makeBlob(center, theme.core == Ground::Rock ? 2.0 : 2.8), theme.core, true); if(theme.hill) - scene.Bump(center, 2, 5.); + scene.bump(center, 2, 5.); // A path leads through the gap between the rocks and the trees - const int pathDir = static_cast(scene.Rnd().RandomValue(0, 5)); + const int pathDir = static_cast(scene.rnd().RandomValue(0, 5)); for(unsigned i = 1; i < sceneRadius; i++) { - const MapPoint pt = scene.Step(scene.Step(center, pathDir, i), pathDir + 1, i / 3); - scene.Block(pt); + const MapPoint pt = scene.step(scene.step(center, pathDir, i), pathDir + 1, i / 3); + scene.block(pt); if(theme.surround != Ground::Dry && i > 1) - scene.Blend(scene.MakeBlob(pt, 1.7, false), Ground::Dry); + scene.blend(scene.makeBlob(pt, 1.7, false), Ground::Dry); } - const double pathAngle = scene.Angle(center, scene.Step(center, pathDir, 3)); - const double rockAngle = pathAngle + scene.Rnd().RandomDouble(1.2, 1.9); - const double treeAngle = pathAngle - scene.Rnd().RandomDouble(1.2, 1.9); + const double pathAngle = scene.angle(center, scene.step(center, pathDir, 3)); + const double rockAngle = pathAngle + scene.rnd().RandomDouble(1.2, 1.9); + const double treeAngle = pathAngle - scene.rnd().RandomDouble(1.2, 1.9); const auto wrap = [](double angle) { return std::atan2(std::sin(angle), std::cos(angle)); }; - scene.ForSector(wrap(rockAngle), 0.65, 2, 5, 55, [&scene](const MapPoint& pt) { scene.PlaceRock(pt); }); - scene.ForSector(wrap(treeAngle), 0.8, 3, sceneRadius, 70, - [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); - scene.ForSector(wrap(pathAngle + pi), 0.9, 2, 5, 50, - [&scene](const MapPoint& pt) { scene.PlaceGroundCover(pt); }); - scene.ForSector(0., pi, 2, 6, 7, [&scene](const MapPoint& pt) { scene.Place(pt, undergrowth); }); - scene.ForSector(0., pi, 1, 4, 10, [&scene, &theme](const MapPoint& pt) { scene.Place(pt, theme.litter); }); + scene.forSector(wrap(rockAngle), 0.65, 2, 5, 55, [&scene](const MapPoint& pt) { scene.placeRock(pt); }); + scene.forSector(wrap(treeAngle), 0.8, 3, sceneRadius, 70, + [&scene](const MapPoint& pt) { scene.placeVegetation(pt); }); + scene.forSector(wrap(pathAngle + pi), 0.9, 2, 5, 50, + [&scene](const MapPoint& pt) { scene.placeGroundCover(pt); }); + scene.forSector(0., pi, 2, 6, 7, [&scene](const MapPoint& pt) { scene.place(pt, undergrowth); }); + scene.forSector(0., pi, 1, 4, 10, [&scene, &theme](const MapPoint& pt) { scene.place(pt, theme.litter); }); } /// Straight road between two rows of trees leading to a court which is enclosed by a hedge - void CreateAvenue(SceneBuilder& scene) + void createAvenue(SceneBuilder& scene) { - const Theme& theme = scene.GetTheme(); - const MapPoint center = scene.Center(); - const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); + const Theme& theme = scene.getTheme(); + const MapPoint center = scene.center(); + const int dir = static_cast(scene.rnd().RandomValue(0, 5)); const Ground paving = theme.core == Ground::Lush ? Ground::Dry : theme.core; - scene.Blend(scene.MakeBlob(center, 4.6), theme.surround); - scene.Blend(scene.MakeBlob(center, paving == Ground::Rock ? 2.9 : 3.1, false), paving, true); - scene.Bump(center, theme.hill ? 2 : 1, 5.); + scene.blend(scene.makeBlob(center, 4.6), theme.surround); + scene.blend(scene.makeBlob(center, paving == Ground::Rock ? 2.9 : 3.1, false), paving, true); + scene.bump(center, theme.hill ? 2 : 1, 5.); std::vector road; for(unsigned i = 1; i <= sceneRadius; i++) { - road.push_back(scene.Step(center, dir, i)); - scene.Block(road.back()); + road.push_back(scene.step(center, dir, i)); + scene.block(road.back()); if(paving == Ground::Rock) - scene.SetOverlay(road.back(), Overlay::RockRim); + scene.setOverlay(road.back(), Overlay::RockRim); else - scene.Blend(scene.MakeBlob(road.back(), 1.1, false), Ground::Dry); + scene.blend(scene.makeBlob(road.back(), 1.1, false), Ground::Dry); } const auto nearRoad = [&scene, &road](const MapPoint& pt) { - return helpers::contains_if(road, [&](const MapPoint& node) { return scene.Distance(node, pt) <= 1; }); + return helpers::contains_if(road, [&](const MapPoint& node) { return scene.distance(node, pt) <= 1; }); }; // Trees on both sides of the road outside the court for(unsigned i = 3; i < road.size(); i++) { - scene.PlaceRowTree(scene.Step(road[i], dir + 2)); - scene.PlaceRowTree(scene.Step(road[i], dir + 4)); + scene.placeRowTree(scene.step(road[i], dir + 2)); + scene.placeRowTree(scene.step(road[i], dir + 4)); } // Beds within the court and a hedge around it - for(const MapPoint& pt : scene.Ring(2)) + for(const MapPoint& pt : scene.ring(2)) { if(!nearRoad(pt)) - scene.PlaceGroundCover(pt); + scene.placeGroundCover(pt); } - const auto hedge = scene.Ring(3); + const auto hedge = scene.ring(3); for(unsigned i = 0; i < hedge.size(); i++) { if(nearRoad(hedge[i])) @@ -682,129 +679,129 @@ namespace { if(theme.deadVegetation) { if(i % 2 == 0) - scene.Place(hedge[i], deadTrees); + scene.place(hedge[i], deadTrees); } else - scene.Place(hedge[i], undergrowth); + scene.place(hedge[i], undergrowth); } // Rocks or props mark the corners - const auto outerRing = scene.Ring(4); + const auto outerRing = scene.ring(4); for(unsigned i = 0; i < outerRing.size(); i += 4) { if(!nearRoad(outerRing[i])) - scene.PlaceRock(outerRing[i]); + scene.placeRock(outerRing[i]); } } /// Rocky hill behind the special object, a pond in front of it and flowers and trees on its sides - void CreateCrag(SceneBuilder& scene) + void createCrag(SceneBuilder& scene) { - const Theme& theme = scene.GetTheme(); - const MapPoint center = scene.Center(); - const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); - const int side = scene.Rnd().ByChance(50) ? 2 : 4; - const MapPoint crag = scene.Step(center, dir, 3); - const MapPoint pond = scene.Step(center, dir + 3, 3); - - scene.Blend(scene.MakeBlob(center, 3.2), theme.surround); - scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 1.6 : 2.2), theme.core, true); - - const Blob cragBlob = scene.MakeBlob(crag, 2.9); - scene.Blend(cragBlob, Ground::Rock); - scene.Bump(crag, 4, 4.4); - scene.ForBlob(cragBlob, 0.95, 55, [&scene](const MapPoint& pt) { - if(scene.Rnd().ByChance(65)) - scene.PlaceGranite(pt); + const Theme& theme = scene.getTheme(); + const MapPoint center = scene.center(); + const int dir = static_cast(scene.rnd().RandomValue(0, 5)); + const int side = scene.rnd().ByChance(50) ? 2 : 4; + const MapPoint crag = scene.step(center, dir, 3); + const MapPoint pond = scene.step(center, dir + 3, 3); + + scene.blend(scene.makeBlob(center, 3.2), theme.surround); + scene.blend(scene.makeBlob(center, theme.core == Ground::Rock ? 1.6 : 2.2), theme.core, true); + + const Blob cragBlob = scene.makeBlob(crag, 2.9); + scene.blend(cragBlob, Ground::Rock); + scene.bump(crag, 4, 4.4); + scene.forBlob(cragBlob, 0.95, 55, [&scene](const MapPoint& pt) { + if(scene.rnd().ByChance(65)) + scene.placeGranite(pt); else - scene.Place(pt, stones); + scene.place(pt, stones); }); - scene.ForBlob(scene.MakeBlob(scene.Step(center, dir, 6), 2.2), 1., 60, - [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); + scene.forBlob(scene.makeBlob(scene.step(center, dir, 6), 2.2), 1., 60, + [&scene](const MapPoint& pt) { scene.placeVegetation(pt); }); // Pond with a sandy shore and reed at its far side. It stays close to the middle of the scene to leave space // for the transition from sand to the surrounding land. - scene.Bump(pond, -1, 3.); - std::vector water{pond, scene.Step(pond, dir + 3)}; - for(const MapPoint& pt : scene.Area(pond, 1)) + scene.bump(pond, -1, 3.); + std::vector water{pond, scene.step(pond, dir + 3)}; + for(const MapPoint& pt : scene.area(pond, 1)) { - if(scene.Distance(pt) >= 3 && scene.Rnd().ByChance(60) && !helpers::contains(water, pt)) + if(scene.distance(pt) >= 3 && scene.rnd().ByChance(60) && !helpers::contains(water, pt)) water.push_back(pt); } for(const MapPoint& pt : water) { - scene.SetOverlay(pt, Overlay::Water); - scene.Block(pt); + scene.setOverlay(pt, Overlay::Water); + scene.block(pt); } for(const MapPoint& pt : water) { - for(const MapPoint& neighbor : scene.Area(pt, 1)) + for(const MapPoint& neighbor : scene.area(pt, 1)) { - if(scene.IsWater(neighbor)) + if(scene.isWater(neighbor)) continue; - scene.SetOverlay(neighbor, Overlay::Sand); - if(scene.Distance(neighbor) > scene.Distance(pond) && scene.Rnd().ByChance(50)) - scene.Place(neighbor, shrubs); + scene.setOverlay(neighbor, Overlay::Sand); + if(scene.distance(neighbor) > scene.distance(pond) && scene.rnd().ByChance(50)) + scene.place(neighbor, shrubs); } } - const Blob drift = scene.MakeBlob(scene.Step(center, dir + side, 4), 2.5); - scene.Blend(drift, Ground::Lush); - scene.ForBlob(drift, 0.85, 55, [&scene, &theme](const MapPoint& pt) { + const Blob drift = scene.makeBlob(scene.step(center, dir + side, 4), 2.5); + scene.blend(drift, Ground::Lush); + scene.forBlob(drift, 0.85, 55, [&scene, &theme](const MapPoint& pt) { if(theme.flowers) - scene.Place(pt, plants); + scene.place(pt, plants); else - scene.Place(pt, theme.props); + scene.place(pt, theme.props); }); - scene.ForBlob(scene.MakeBlob(scene.Step(center, dir - side, 5), 2.1), 1., 65, - [&scene](const MapPoint& pt) { scene.PlaceVegetation(pt); }); - scene.ForSector(0., pi, 1, 3, 12, [&scene, &theme](const MapPoint& pt) { scene.Place(pt, theme.litter); }); + scene.forBlob(scene.makeBlob(scene.step(center, dir - side, 5), 2.1), 1., 65, + [&scene](const MapPoint& pt) { scene.placeVegetation(pt); }); + scene.forSector(0., pi, 1, 3, 12, [&scene, &theme](const MapPoint& pt) { scene.place(pt, theme.litter); }); } /// Curved trail lined by remains which leads to the special object - void CreateTrail(SceneBuilder& scene) + void createTrail(SceneBuilder& scene) { - const Theme& theme = scene.GetTheme(); - const MapPoint center = scene.Center(); - const int dir = static_cast(scene.Rnd().RandomValue(0, 5)); - const int turn = scene.Rnd().ByChance(50) ? 1 : -1; + const Theme& theme = scene.getTheme(); + const MapPoint center = scene.center(); + const int dir = static_cast(scene.rnd().RandomValue(0, 5)); + const int turn = scene.rnd().ByChance(50) ? 1 : -1; - scene.Blend(scene.MakeBlob(center, 3.4), theme.surround); - scene.Blend(scene.MakeBlob(center, theme.core == Ground::Rock ? 1.9 : 2.6), theme.core, true); + scene.blend(scene.makeBlob(center, 3.4), theme.surround); + scene.blend(scene.makeBlob(center, theme.core == Ground::Rock ? 1.9 : 2.6), theme.core, true); if(theme.hill) - scene.Bump(center, 2, 4.5); + scene.bump(center, 2, 4.5); MapPoint pt = center; std::vector> trail; for(const int bend : {0, 0, 1, 1, 1, 2, 2}) { - pt = scene.Step(pt, dir + turn * bend); + pt = scene.step(pt, dir + turn * bend); trail.emplace_back(pt, dir + turn * bend); - scene.Block(pt); + scene.block(pt); } for(const auto& node : trail) - scene.Blend(scene.MakeBlob(node.first, 1.7, false), Ground::Dry); + scene.blend(scene.makeBlob(node.first, 1.7, false), Ground::Dry); // Remains alternate on both sides of the trail for(unsigned i = 1; i < trail.size(); i++) { const int left = trail[i].second + 2; const int right = trail[i].second + 4; - scene.Place(scene.Step(trail[i].first, i % 2 ? left : right), theme.props); - if(scene.Rnd().ByChance(45)) - scene.Place(scene.Step(trail[i].first, i % 2 ? right : left), theme.litter); + scene.place(scene.step(trail[i].first, i % 2 ? left : right), theme.props); + if(scene.rnd().ByChance(45)) + scene.place(scene.step(trail[i].first, i % 2 ? right : left), theme.litter); } - scene.PlaceRock(scene.Step(trail.back().first, trail.back().second + 1)); + scene.placeRock(scene.step(trail.back().first, trail.back().second + 1)); // Clumps of trees behind the special object and inside of the bend - scene.ForBlob(scene.MakeBlob(scene.Step(center, dir + 3, 4), 2.4), 1., 65, - [&scene](const MapPoint& p) { scene.PlaceVegetation(p); }); - scene.ForBlob(scene.MakeBlob(scene.Step(trail[3].first, trail[3].second + turn * 2, 3), 1.9), 1., 60, - [&scene](const MapPoint& p) { scene.PlaceVegetation(p); }); - scene.ForSector(0., pi, 1, 3, 30, [&scene](const MapPoint& p) { scene.PlaceGroundCover(p); }); - scene.ForSector(0., pi, 2, 6, 8, [&scene](const MapPoint& p) { scene.Place(p, undergrowth); }); + scene.forBlob(scene.makeBlob(scene.step(center, dir + 3, 4), 2.4), 1., 65, + [&scene](const MapPoint& p) { scene.placeVegetation(p); }); + scene.forBlob(scene.makeBlob(scene.step(trail[3].first, trail[3].second + turn * 2, 3), 1.9), 1., 60, + [&scene](const MapPoint& p) { scene.placeVegetation(p); }); + scene.forSector(0., pi, 1, 3, 30, [&scene](const MapPoint& p) { scene.placeGroundCover(p); }); + scene.forSector(0., pi, 2, 6, 8, [&scene](const MapPoint& p) { scene.place(p, undergrowth); }); } } // namespace -bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo) +bool isSpecialObject(uint8_t objectType, uint8_t objectInfo) { if(objectType != OT_Decoration) return false; @@ -813,12 +810,12 @@ bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo) || objectInfo == OI_Cross; } -unsigned GetNumSpecialScenes(const MapExtent& size) +unsigned getNumSpecialScenes(const MapExtent& size) { return helpers::clamp((size.x + size.y) / 96u, 1u, 6u); } -std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigned number) +std::vector findSceneSites(const Map& map, RandomUtility& rnd, unsigned number) { const auto isUnsuitable = [&map](const MapPoint& pt) { return !map.textureMap.All(pt, IsBuildableLand) || map.objectTypes[pt] != libsiedler2::OT_Empty @@ -854,23 +851,23 @@ std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigne return sites; } -void CreateScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style) +void createScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style) { SceneBuilder scene(map, rnd, center, type); - scene.PlaceCore(type); + scene.placeCore(type); switch(style) { - case SceneStyle::Overgrown: CreateOvergrown(scene); break; - case SceneStyle::Avenue: CreateAvenue(scene); break; - case SceneStyle::Crag: CreateCrag(scene); break; - case SceneStyle::Trail: CreateTrail(scene); break; + case SceneStyle::Overgrown: createOvergrown(scene); break; + case SceneStyle::Avenue: createAvenue(scene); break; + case SceneStyle::Crag: createCrag(scene); break; + case SceneStyle::Trail: createTrail(scene); break; } - scene.Finish(); + scene.finish(); } -std::vector AddSpecialScenes(Map& map, RandomUtility& rnd) +std::vector addSpecialScenes(Map& map, RandomUtility& rnd) { // Each type is used once before any type is repeated std::vector types; @@ -880,12 +877,12 @@ std::vector AddSpecialScenes(Map& map, RandomUtility& rnd) std::swap(types[i - 1], types[rnd.Index(i)]); std::vector scenes; - for(const MapPoint& site : FindSceneSites(map, rnd, GetNumSpecialScenes(map.size))) + for(const MapPoint& site : findSceneSites(map, rnd, getNumSpecialScenes(map.size))) { const SceneType type = types[scenes.size() % types.size()]; const auto style = static_cast(rnd.RandomValue(0u, static_cast(maxEnumValue(SceneStyle{})))); - CreateScene(map, rnd, site, type, style); + createScene(map, rnd, site, type, style); scenes.push_back(Scene{type, style, site}); } diff --git a/libs/s25main/mapGenerator/SpecialObjects.h b/libs/s25main/mapGenerator/SpecialObjects.h index 47c9da3dd5..a7ba919827 100644 --- a/libs/s25main/mapGenerator/SpecialObjects.h +++ b/libs/s25main/mapGenerator/SpecialObjects.h @@ -69,54 +69,28 @@ constexpr uint8_t OI_Cross = 0x30; constexpr uint8_t OI_PileOfWood = 0x32; /// Checks whether the object is the centerpiece of a scene -bool IsSpecialObject(uint8_t objectType, uint8_t objectInfo); +bool isSpecialObject(uint8_t objectType, uint8_t objectInfo); -/** - * Computes the number of special scenes for a map. Only a few scenes are placed to keep them special. - * - * @param size size of the map - * - * @returns the desired number of scenes which grows with the size of the map. - */ -unsigned GetNumSpecialScenes(const MapExtent& size); +/// Desired number of scenes for the map size. Only a few are placed to keep them special. +unsigned getNumSpecialScenes(const MapExtent& size); /** - * Searches for places for special scenes. The middle of a scene requires buildable land without any objects and keeps - * a distance to headquarters, harbors and other scenes. - * - * @param map map to search for places (is not modified) - * @param rnd random number generator - * @param number desired number of scenes - * - * @returns the center of each scene - less than desired if the map doesn't provide enough suitable places. + * Searches for up to `number` centers of scenes. The middle of a scene needs buildable land without objects and keeps + * a distance to headquarters, harbors and other scenes. Does not modify the map. */ -std::vector FindSceneSites(const Map& map, RandomUtility& rnd, unsigned number); +std::vector findSceneSites(const Map& map, RandomUtility& rnd, unsigned number); /** - * Creates a scene around the specified center. The theme defines the special object, the objects around it and the - * ground while the style defines how they are arranged. The ground blends into the surrounding land using intermediate - * textures and hills rise smoothly. Only buildable land without objects is changed: water, mountains and other - * landscapes in the outer part of a scene are left untouched. The nodes of the scene are reserved to keep them free - * from random trees and stone piles. - * - * @param map reference to the map to place the scene on (manipulates textures, objects and z-values of the map) - * @param rnd random number generator - * @param center center of the scene which gets the special object - * @param type theme of the scene - * @param style layout of the scene + * Creates a scene around the center: the type defines the objects and the ground, the style their arrangement. + * Only buildable land without objects is changed. The nodes of the scene are reserved to keep them free from trees and + * stone piles. */ -void CreateScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style); +void createScene(Map& map, RandomUtility& rnd, const MapPoint& center, SceneType type, SceneStyle style); /** - * Places a few special scenes on the map. The types of the scenes are mixed up and each scene gets a random - * style. This function has to be called once - * textures, headquarters and harbors are in place but before any other objects are added. - * - * @param map reference to the map to place the scenes on (manipulates textures, objects and z-values of the map) - * @param rnd random number generator - * - * @returns the type and center of each new scene. + * Places a few scenes of mixed types and random styles on the map. + * Must be called once textures, headquarters and harbors are in place but before any other objects are added. */ -std::vector AddSpecialScenes(Map& map, RandomUtility& rnd); +std::vector addSpecialScenes(Map& map, RandomUtility& rnd); } // namespace rttr::mapGenerator diff --git a/libs/s25main/mapGenerator/TextureHelper.cpp b/libs/s25main/mapGenerator/TextureHelper.cpp index eebdb560f3..b559df842d 100644 --- a/libs/s25main/mapGenerator/TextureHelper.cpp +++ b/libs/s25main/mapGenerator/TextureHelper.cpp @@ -21,7 +21,7 @@ bool IsSwamp(const TerrainDesc& terrain) return !terrain.Is(ETerrain::Walkable) && terrain.humidity > 0; } -bool IsImpassable(const TerrainDesc& terrain) +bool isImpassable(const TerrainDesc& terrain) { return terrain.flags == ETerrain::Unwalkable; } diff --git a/libs/s25main/mapGenerator/TextureHelper.h b/libs/s25main/mapGenerator/TextureHelper.h index 6afa3be79d..3b036bed4e 100644 --- a/libs/s25main/mapGenerator/TextureHelper.h +++ b/libs/s25main/mapGenerator/TextureHelper.h @@ -14,9 +14,8 @@ bool IsWater(const TerrainDesc& terrain); bool IsSwamp(const TerrainDesc& terrain); -/// Terrain which can neither be entered by settlers nor by ships. The first match of each landscape is the swamp or its -/// equivalent (stony lava, ice floes). -bool IsImpassable(const TerrainDesc& terrain); +/// Terrain which can neither be entered by settlers nor by ships, e.g. swamp +bool isImpassable(const TerrainDesc& terrain); bool IsLand(const TerrainDesc& terrain); diff --git a/tests/s25Main/mapGenerator/testFreeLand.cpp b/tests/s25Main/mapGenerator/testFreeLand.cpp index aee1a0f274..45c2bbdbfe 100644 --- a/tests/s25Main/mapGenerator/testFreeLand.cpp +++ b/tests/s25Main/mapGenerator/testFreeLand.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "PointOutput.h" +#include "helpers/EnumRange.h" #include "mapGenFixtures.h" #include "mapGenerator/Algorithms.h" #include "mapGenerator/FreeLand.h" @@ -21,11 +22,6 @@ constexpr unsigned mountainLevel = 30; constexpr MountainSettings settings{3, 100, 25}; constexpr unsigned numLandscapes = 3; -unsigned idx(LandFill type) -{ - return static_cast(type); -} - struct FreeLandFixture : MapGenFixture { /// Creates a map covered by land except for a single row of water @@ -75,36 +71,36 @@ unsigned countObjects(const Map& map, T_Predicate&& predicate) BOOST_FIXTURE_TEST_SUITE(FreeLandTests, FreeLandFixture) -BOOST_AUTO_TEST_CASE(SelectLandFill_returns_mountain_without_any_amounts) +BOOST_AUTO_TEST_CASE(selectLandFill_returns_mountain_without_any_amounts) { RandomUtility rnd(0); - BOOST_TEST((SelectLandFill(rnd, {0, 0, 0}, {0, 0, 0}) == LandFill::Mountain)); + BOOST_TEST((selectLandFill(rnd, {0, 0, 0}, {0, 0, 0}) == LandFill::Mountain)); } -BOOST_AUTO_TEST_CASE(SelectLandFill_starts_with_mountain_if_desired) +BOOST_AUTO_TEST_CASE(selectLandFill_starts_with_mountain_if_desired) { for(unsigned seed = 0; seed < 20; seed++) { RandomUtility rnd(seed); - BOOST_TEST_REQUIRE((SelectLandFill(rnd, {1, 100, 100}, {0, 0, 0}) == LandFill::Mountain)); + BOOST_TEST_REQUIRE((selectLandFill(rnd, {1, 100, 100}, {0, 0, 0}) == LandFill::Mountain)); } } -BOOST_AUTO_TEST_CASE(SelectLandFill_returns_only_desired_types) +BOOST_AUTO_TEST_CASE(selectLandFill_returns_only_desired_types) { RandomUtility rnd(0); LandFillAmounts assigned{}; for(unsigned i = 0; i < 50; i++) { - const LandFill type = SelectLandFill(rnd, {0, 30, 0}, assigned); + const LandFill type = selectLandFill(rnd, {0, 30, 0}, assigned); BOOST_TEST_REQUIRE((type == LandFill::Desert)); - assigned[idx(type)] += 10; + assigned[type] += 10; } } -BOOST_AUTO_TEST_CASE(SelectLandFill_keeps_proportions_of_amounts) +BOOST_AUTO_TEST_CASE(selectLandFill_keeps_proportions_of_amounts) { const LandFillAmounts amounts{50, 30, 20}; @@ -117,19 +113,20 @@ BOOST_AUTO_TEST_CASE(SelectLandFill_keeps_proportions_of_amounts) unsigned total = 0; for(unsigned size = 100; size > 40; size -= 5) { - assigned[idx(SelectLandFill(rnd, amounts, assigned))] += size; + assigned[selectLandFill(rnd, amounts, assigned)] += size; total += size; } - for(unsigned i = 0; i < numLandFills; i++) + for(const LandFill type : helpers::EnumRange()) { - const int share = static_cast(assigned[i] * 100 / total); - BOOST_TEST_REQUIRE(std::abs(share - static_cast(amounts[i])) <= 15, "Type " << i << ": " << share); + const int share = static_cast(assigned[type] * 100 / total); + BOOST_TEST_REQUIRE(std::abs(share - static_cast(amounts[type])) <= 15, + "Type " << rttr::enum_cast(type) << ": " << share); } } } -BOOST_AUTO_TEST_CASE(SelectLandFill_varies_with_seed) +BOOST_AUTO_TEST_CASE(selectLandFill_varies_with_seed) { const LandFillAmounts amounts{0, 50, 50}; unsigned deserts = 0; @@ -137,7 +134,7 @@ BOOST_AUTO_TEST_CASE(SelectLandFill_varies_with_seed) for(unsigned seed = 0; seed < 50; seed++) { RandomUtility rnd(seed); - if(SelectLandFill(rnd, amounts, {0, 0, 0}) == LandFill::Desert) + if(selectLandFill(rnd, amounts, {0, 0, 0}) == LandFill::Desert) deserts++; } @@ -152,7 +149,7 @@ BOOST_AUTO_TEST_CASE(Swamp_and_desert_textures_exist_for_all_landscapes) landscape = DescIdx(i); const Map map = createMap(MapExtent(8, 8)); - const auto swamp = map.textureMap.Find(IsImpassable); + const auto swamp = map.textureMap.Find(isImpassable); const auto desert = map.textureMap.Find(IsCoastTerrain); // The swamp and its equivalents share the same index in all landscapes @@ -165,7 +162,7 @@ BOOST_AUTO_TEST_CASE(Swamp_and_desert_textures_exist_for_all_landscapes) } } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_is_lower_than_mountain_and_keeps_surroundings) +BOOST_AUTO_TEST_CASE(raiseDesertHill_is_lower_than_mountain_and_keeps_surroundings) { Map hill = createLandMap(MapExtent(48, 48)); Map mountain = createLandMap(MapExtent(48, 48)); @@ -173,8 +170,8 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_is_lower_than_mountain_and_keeps_surroundin const Mountain area = createArea(hill, center, 10); RandomUtility rnd(0); - RaiseDesertHill(hill, rnd, area); - RaiseMountain(mountain, rnd, area); + raiseDesertHill(hill, rnd, area); + raiseMountain(mountain, rnd, area); BOOST_TEST(hill.z[center] > landHeight); BOOST_TEST(hill.z[center] < mountain.z[center]); @@ -185,13 +182,13 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_is_lower_than_mountain_and_keeps_surroundin } } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_rises_smoothly) +BOOST_AUTO_TEST_CASE(raiseDesertHill_rises_smoothly) { Map map = createLandMap(MapExtent(48, 48)); const Mountain area = createArea(map, MapPoint(24, 24), 10); RandomUtility rnd(0); - RaiseDesertHill(map, rnd, area); + raiseDesertHill(map, rnd, area); RTTR_FOREACH_PT(MapPoint, map.size) { @@ -205,7 +202,7 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_rises_smoothly) } } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_applies_transitions_from_buildable_land_to_desert) +BOOST_AUTO_TEST_CASE(raiseDesertHill_applies_transitions_from_buildable_land_to_desert) { for(unsigned i = 0; i < numLandscapes; i++) { @@ -213,10 +210,10 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_applies_transitions_from_buildable_land_to_ Map map = createLandMap(MapExtent(48, 48)); const MapPoint center(24, 24); const Mountain area = createArea(map, center, 8); - const auto footDistance = GetFootDistances(map.size, area); + const auto footDistance = getFootDistances(map.size, area); RandomUtility rnd(0); - RaiseDesertHill(map, rnd, area); + raiseDesertHill(map, rnd, area); auto transitions = map.textureMap.FindAll(IsBuildableCoast); map.textureMap.Sort(transitions, ByHumidity); @@ -246,17 +243,17 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_applies_transitions_from_buildable_land_to_ } } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_places_bones_around_center_only) +BOOST_AUTO_TEST_CASE(raiseDesertHill_places_bones_around_center_only) { for(unsigned seed = 0; seed < 10; seed++) { Map map = createLandMap(MapExtent(48, 48)); const Mountain area = createArea(map, MapPoint(24, 24), 9); - const auto footDistance = GetFootDistances(map.size, area); + const auto footDistance = getFootDistances(map.size, area); const unsigned maxDistance = GetMaximum(footDistance, area); RandomUtility rnd(seed); - RaiseDesertHill(map, rnd, area); + raiseDesertHill(map, rnd, area); BOOST_TEST_REQUIRE(countObjects(map, [](const MapPoint&) { return true; }) > 0u); RTTR_FOREACH_PT(MapPoint, map.size) @@ -271,26 +268,26 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_places_bones_around_center_only) } } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_supports_tiny_areas) +BOOST_AUTO_TEST_CASE(raiseDesertHill_supports_tiny_areas) { Map map = createLandMap(MapExtent(16, 16)); const MapPoint center(8, 8); RandomUtility rnd(0); - RaiseDesertHill(map, rnd, Mountain{center}); - RaiseDesertHill(map, rnd, Mountain{}); + raiseDesertHill(map, rnd, Mountain{center}); + raiseDesertHill(map, rnd, Mountain{}); BOOST_TEST(map.textureMap.All(center, IsCoastTerrain)); BOOST_TEST(map.objectTypes[center] == OT_Decoration); } -BOOST_AUTO_TEST_CASE(RaiseDesertHill_does_not_exceed_maximum_height) +BOOST_AUTO_TEST_CASE(raiseDesertHill_does_not_exceed_maximum_height) { Map map = createMap(MapExtent(32, 32)); map.z.Resize(map.size, map.height.maximum - 1); RandomUtility rnd(0); - RaiseDesertHill(map, rnd, createArea(map, MapPoint(16, 16), 8)); + raiseDesertHill(map, rnd, createArea(map, MapPoint(16, 16), 8)); RTTR_FOREACH_PT(MapPoint, map.size) { @@ -298,7 +295,7 @@ BOOST_AUTO_TEST_CASE(RaiseDesertHill_does_not_exceed_maximum_height) } } -BOOST_AUTO_TEST_CASE(CreateSwamp_is_entirely_flat_and_impassable) +BOOST_AUTO_TEST_CASE(createSwamp_is_entirely_flat_and_impassable) { for(unsigned i = 0; i < numLandscapes; i++) { @@ -308,19 +305,19 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_is_entirely_flat_and_impassable) const uint8_t lowest = map.z[MapPoint(12, 24)]; RandomUtility rnd(0); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); for(const MapPoint& pt : area) { BOOST_TEST_REQUIRE(map.z[pt] == lowest, "At point " << pt); - BOOST_TEST_REQUIRE(map.textureMap.All(pt, IsImpassable), "At point " << pt); + BOOST_TEST_REQUIRE(map.textureMap.All(pt, isImpassable), "At point " << pt); BOOST_TEST_REQUIRE(helpers::contains(map.barren, pt), "At point " << pt); } BOOST_TEST(map.barren.size() == area.size()); } } -BOOST_AUTO_TEST_CASE(CreateSwamp_lowers_shore_towards_swamp) +BOOST_AUTO_TEST_CASE(createSwamp_lowers_shore_towards_swamp) { Map map = createSlopedMap(MapExtent(48, 48)); const Map original = createSlopedMap(MapExtent(48, 48)); @@ -329,7 +326,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_lowers_shore_towards_swamp) const auto distances = DistancesTo(area, map.size); RandomUtility rnd(0); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); unsigned lowered = 0; RTTR_FOREACH_PT(MapPoint, map.size) @@ -344,7 +341,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_lowers_shore_towards_swamp) BOOST_TEST(lowered > 0u); } -BOOST_AUTO_TEST_CASE(CreateSwamp_avoids_steep_slopes_on_steep_land) +BOOST_AUTO_TEST_CASE(createSwamp_avoids_steep_slopes_on_steep_land) { Map map = createMap(MapExtent(48, 48)); map.z.Resize(map.size, landHeight); @@ -356,7 +353,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_avoids_steep_slopes_on_steep_land) const Mountain area = createArea(map, MapPoint(16, 24), 5); RandomUtility rnd(0); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(MapPoint(16, 24), 10)) { @@ -373,7 +370,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_avoids_steep_slopes_on_steep_land) } } -BOOST_AUTO_TEST_CASE(CreateSwamp_never_lowers_banks_below_rivers) +BOOST_AUTO_TEST_CASE(createSwamp_never_lowers_banks_below_rivers) { // Rivers follow the height of the land instead of the sea level Map map = createSlopedMap(MapExtent(48, 48)); @@ -390,7 +387,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_never_lowers_banks_below_rivers) // The river passes the upper side of the swamp at a distance of three nodes const Mountain area = createArea(map, MapPoint(18, 24), 6); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); unsigned lowered = 0; RTTR_FOREACH_PT(MapPoint, map.size) @@ -410,7 +407,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_never_lowers_banks_below_rivers) BOOST_TEST(lowered > 0u); } -BOOST_AUTO_TEST_CASE(CreateSwamp_keeps_shape_of_other_landscapes) +BOOST_AUTO_TEST_CASE(createSwamp_keeps_shape_of_other_landscapes) { Map map = createSlopedMap(MapExtent(48, 48)); const Mountain area = createArea(map, MapPoint(18, 24), 6); @@ -419,12 +416,12 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_keeps_shape_of_other_landscapes) map.textureMap.Set(mountain, map.textureMap.Find(IsMinableMountain)); RandomUtility rnd(0); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); BOOST_TEST(map.z[mountain] == height); } -BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_separates_desert_from_lush_land) +BOOST_AUTO_TEST_CASE(smoothDesertTransitions_separates_desert_from_lush_land) { for(unsigned i = 0; i < numLandscapes; i++) { @@ -440,7 +437,7 @@ BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_separates_desert_from_lush_land) const MapPoint center(24, 24); const Mountain area = createArea(map, center, 6); RandomUtility rnd(0); - RaiseDesertHill(map, rnd, area); + raiseDesertHill(map, rnd, area); // Texturing coasts may replace the transitions of the hill for(const MapPoint& pt : area) { @@ -450,7 +447,7 @@ BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_separates_desert_from_lush_land) for(const MapPoint& pt : map.barren) map.textureMap.Set(pt, map.textureMap.Find(IsCoastTerrain)); - SmoothDesertTransitions(map); + smoothDesertTransitions(map); const auto humidityOf = [this, &map](const Triangle& triangle) { return worldDesc.get(map.textureMap.Get(triangle)).humidity; @@ -475,7 +472,7 @@ BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_separates_desert_from_lush_land) } } -BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_keeps_maps_without_deserts) +BOOST_AUTO_TEST_CASE(smoothDesertTransitions_keeps_maps_without_deserts) { Map map = createLandMap(MapExtent(32, 32)); const auto land = map.textureMap.Find(IsBuildableLand); @@ -484,10 +481,10 @@ BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_keeps_maps_without_deserts) map.textureMap.Set(pt, land); } RandomUtility rnd(0); - CreateSwamp(map, rnd, createArea(map, MapPoint(16, 16), 4)); + createSwamp(map, rnd, createArea(map, MapPoint(16, 16), 4)); const auto before = map.getTextures(); - SmoothDesertTransitions(map); + smoothDesertTransitions(map); RTTR_FOREACH_PT(MapPoint, map.size) { @@ -496,14 +493,14 @@ BOOST_AUTO_TEST_CASE(SmoothDesertTransitions_keeps_maps_without_deserts) } } -BOOST_AUTO_TEST_CASE(CreateSwamp_places_dead_trees_on_border_to_free_land_only) +BOOST_AUTO_TEST_CASE(createSwamp_places_dead_trees_on_border_to_free_land_only) { Map map = createLandMap(MapExtent(48, 48)); const Mountain area = createArea(map, MapPoint(24, 24), 6); const auto distances = DistancesTo(area, map.size); RandomUtility rnd(0); - CreateSwamp(map, rnd, area); + createSwamp(map, rnd, area); BOOST_TEST_REQUIRE(countObjects(map, [](const MapPoint&) { return true; }) > 0u); RTTR_FOREACH_PT(MapPoint, map.size) @@ -517,7 +514,7 @@ BOOST_AUTO_TEST_CASE(CreateSwamp_places_dead_trees_on_border_to_free_land_only) } } -BOOST_AUTO_TEST_CASE(CloseGaps_adds_enclosed_nodes_only) +BOOST_AUTO_TEST_CASE(closeGaps_adds_enclosed_nodes_only) { const Map map = createLandMap(MapExtent(32, 32)); const MapPoint center(16, 16); @@ -526,41 +523,41 @@ BOOST_AUTO_TEST_CASE(CloseGaps_adds_enclosed_nodes_only) area.erase(center); area.erase(MapPoint(13, 15)); - BOOST_TEST((CloseGaps(map.size, area) == closed)); - BOOST_TEST((CloseGaps(map.size, closed) == closed)); + BOOST_TEST((closeGaps(map.size, area) == closed)); + BOOST_TEST((closeGaps(map.size, closed) == closed)); } -BOOST_AUTO_TEST_CASE(FillFreeLand_creates_all_desired_types_on_disjoint_areas) +BOOST_AUTO_TEST_CASE(fillFreeLand_creates_all_desired_types_on_disjoint_areas) { Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - const auto areas = FillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50}); + const auto areas = fillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50}); BOOST_TEST_REQUIRE(areas.size() >= 3u); BOOST_TEST((areas.front().type == LandFill::Mountain)); Mountain visited; - std::array count{}; + helpers::EnumArray count{}; for(const FilledArea& area : areas) { - count[idx(area.type)]++; + count[area.type]++; for(const MapPoint& pt : area.nodes) { BOOST_TEST_REQUIRE(visited.insert(pt).second, "At point " << pt); } } - BOOST_TEST(count[idx(LandFill::Mountain)] > 0u); - BOOST_TEST(count[idx(LandFill::Desert)] > 0u); - BOOST_TEST(count[idx(LandFill::Swamp)] > 0u); + BOOST_TEST(count[LandFill::Mountain] > 0u); + BOOST_TEST(count[LandFill::Desert] > 0u); + BOOST_TEST(count[LandFill::Swamp] > 0u); } -BOOST_AUTO_TEST_CASE(FillFreeLand_applies_textures_matching_the_type) +BOOST_AUTO_TEST_CASE(fillFreeLand_applies_textures_matching_the_type) { Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - for(const FilledArea& area : FillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50})) + for(const FilledArea& area : fillFreeLand(map, rnd, mountainLevel, settings, {50, 50, 50})) { const auto all = [&map, &area](bool (*predicate)(const TerrainDesc&)) { return std::all_of(area.nodes.begin(), area.nodes.end(), @@ -575,20 +572,20 @@ BOOST_AUTO_TEST_CASE(FillFreeLand_applies_textures_matching_the_type) { case LandFill::Mountain: BOOST_TEST(all(IsMinableMountain)); break; case LandFill::Desert: BOOST_TEST(any(IsCoastTerrain)); break; - case LandFill::Swamp: BOOST_TEST(all(IsImpassable)); break; + case LandFill::Swamp: BOOST_TEST(all(isImpassable)); break; } } } -BOOST_AUTO_TEST_CASE(FillFreeLand_creates_only_mountains_by_default) +BOOST_AUTO_TEST_CASE(fillFreeLand_creates_only_mountains_by_default) { Map map = createLandMap(MapExtent(64, 64)); Map expected = createLandMap(MapExtent(64, 64)); RandomUtility rnd1(7); RandomUtility rnd2(7); - const auto areas = FillFreeLand(map, rnd1, mountainLevel, settings, {50, 0, 0}); - const auto mountains = CreateMountains(expected, rnd2, mountainLevel, settings); + const auto areas = fillFreeLand(map, rnd1, mountainLevel, settings, {50, 0, 0}); + const auto mountains = createMountains(expected, rnd2, mountainLevel, settings); BOOST_TEST_REQUIRE(areas.size() == mountains.size()); for(unsigned i = 0; i < areas.size(); i++) @@ -603,12 +600,12 @@ BOOST_AUTO_TEST_CASE(FillFreeLand_creates_only_mountains_by_default) BOOST_TEST(map.barren.empty()); } -BOOST_AUTO_TEST_CASE(FillFreeLand_limits_swamps_to_half_the_size) +BOOST_AUTO_TEST_CASE(fillFreeLand_limits_swamps_to_half_the_size) { Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - const auto areas = FillFreeLand(map, rnd, mountainLevel, settings, {0, 0, 50}); + const auto areas = fillFreeLand(map, rnd, mountainLevel, settings, {0, 0, 50}); BOOST_TEST_REQUIRE(!areas.empty()); for(const FilledArea& area : areas) @@ -620,14 +617,14 @@ BOOST_AUTO_TEST_CASE(FillFreeLand_limits_swamps_to_half_the_size) BOOST_TEST(areas.front().nodes.size() < settings.maxSize * 3 / 4); } -BOOST_AUTO_TEST_CASE(FillFreeLand_leaves_no_free_land_beyond_limit) +BOOST_AUTO_TEST_CASE(fillFreeLand_leaves_no_free_land_beyond_limit) { Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - FillFreeLand(map, rnd, mountainLevel, settings, {20, 40, 40}); + fillFreeLand(map, rnd, mountainLevel, settings, {20, 40, 40}); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); BOOST_TEST(*std::max_element(distances.begin(), distances.end()) <= settings.minFreeLand); } @@ -643,8 +640,8 @@ BOOST_AUTO_TEST_CASE(AddObjects_keeps_decoration_and_places_nothing_on_barren_no const Mountain swamp = createArea(map, MapPoint(8, 16), 4); const Mountain hill = createArea(map, MapPoint(22, 16), 5); RandomUtility rnd(0); - CreateSwamp(map, rnd, swamp); - RaiseDesertHill(map, rnd, hill); + createSwamp(map, rnd, swamp); + raiseDesertHill(map, rnd, hill); const auto typesBefore = map.objectTypes; const auto infosBefore = map.objectInfos; @@ -678,7 +675,7 @@ BOOST_AUTO_TEST_CASE(AddObjects_removes_decoration_around_headquarters) } const MapPoint hq(16, 16); RandomUtility rnd(0); - CreateSwamp(map, rnd, createArea(map, MapPoint(9, 16), 4)); + createSwamp(map, rnd, createArea(map, MapPoint(9, 16), 4)); map.hqPositions.push_back(hq); AddObjects(map, rnd, MapSettings()); @@ -704,7 +701,7 @@ BOOST_AUTO_TEST_CASE(AddResources_and_AddAnimals_skip_swamps_made_of_water) } const Mountain swamp = createArea(map, MapPoint(16, 16), 8); RandomUtility rnd(0); - CreateSwamp(map, rnd, swamp); + createSwamp(map, rnd, swamp); AddResources(map, rnd, MapSettings()); AddAnimals(map, rnd); diff --git a/tests/s25Main/mapGenerator/testMountains.cpp b/tests/s25Main/mapGenerator/testMountains.cpp index f5191af433..8de7a9f05e 100644 --- a/tests/s25Main/mapGenerator/testMountains.cpp +++ b/tests/s25Main/mapGenerator/testMountains.cpp @@ -41,87 +41,87 @@ unsigned maxDistance(const NodeMapBase& distances) BOOST_FIXTURE_TEST_SUITE(MountainsTests, MountainFixture) -BOOST_AUTO_TEST_CASE(GetMountainSize_starts_with_maximum_size) +BOOST_AUTO_TEST_CASE(getMountainSize_starts_with_maximum_size) { - BOOST_TEST(GetMountainSize(settings, 0) == settings.maxSize); + BOOST_TEST(getMountainSize(settings, 0) == settings.maxSize); } -BOOST_AUTO_TEST_CASE(GetMountainSize_decreases_with_index) +BOOST_AUTO_TEST_CASE(getMountainSize_decreases_with_index) { - BOOST_TEST(GetMountainSize(settings, 1) < GetMountainSize(settings, 0)); - BOOST_TEST(GetMountainSize(settings, 2) < GetMountainSize(settings, 1)); + BOOST_TEST(getMountainSize(settings, 1) < getMountainSize(settings, 0)); + BOOST_TEST(getMountainSize(settings, 2) < getMountainSize(settings, 1)); } -BOOST_AUTO_TEST_CASE(GetMountainSize_never_drops_below_minimum_size) +BOOST_AUTO_TEST_CASE(getMountainSize_never_drops_below_minimum_size) { - BOOST_TEST(GetMountainSize(settings, 1000) == settings.minSize); + BOOST_TEST(getMountainSize(settings, 1000) == settings.minSize); } -BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_water) +BOOST_AUTO_TEST_CASE(getFreeLandDistances_is_zero_for_water) { const Map map = createLandMap(MapExtent(16, 16)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); BOOST_TEST(distances[MapPoint(5, 0)] == 0u); BOOST_TEST(distances[MapPoint(5, 1)] == 1u); BOOST_TEST(distances[MapPoint(5, 8)] == 8u); } -BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_nodes_at_mountain_level) +BOOST_AUTO_TEST_CASE(getFreeLandDistances_is_zero_for_nodes_at_mountain_level) { Map map = createLandMap(MapExtent(16, 16)); const MapPoint mountain(8, 8); map.z[mountain] = mountainLevel; - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); BOOST_TEST(distances[mountain] == 0u); BOOST_TEST(distances[MapPoint(9, 8)] == 1u); } -BOOST_AUTO_TEST_CASE(GetFreeLandDistances_is_zero_for_textured_nodes) +BOOST_AUTO_TEST_CASE(getFreeLandDistances_is_zero_for_textured_nodes) { Map map = createLandMap(MapExtent(16, 16)); const MapPoint river(8, 8); map.textureMap.Set(river, map.textureMap.Find(IsWater)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); BOOST_TEST(distances[river] == 0u); } -BOOST_AUTO_TEST_CASE(GrowMountain_contains_center_even_if_size_is_one) +BOOST_AUTO_TEST_CASE(growMountain_contains_center_even_if_size_is_one) { const Map map = createLandMap(MapExtent(32, 32)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); const MapPoint center(16, 16); RandomUtility rnd(0); - const auto mountain = GrowMountain(distances, rnd, center, 1, 3); + const auto mountain = growMountain(distances, rnd, center, 1, 3); BOOST_TEST(mountain.size() == 1u); BOOST_TEST(mountain.count(center) == 1u); } -BOOST_AUTO_TEST_CASE(GrowMountain_reaches_requested_size_on_sufficient_land) +BOOST_AUTO_TEST_CASE(growMountain_reaches_requested_size_on_sufficient_land) { const Map map = createLandMap(MapExtent(32, 32)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); RandomUtility rnd(0); - const auto mountain = GrowMountain(distances, rnd, MapPoint(16, 16), 50, 3); + const auto mountain = growMountain(distances, rnd, MapPoint(16, 16), 50, 3); BOOST_TEST(mountain.size() == 50u); } -BOOST_AUTO_TEST_CASE(GrowMountain_keeps_margin_to_obstacles) +BOOST_AUTO_TEST_CASE(growMountain_keeps_margin_to_obstacles) { const Map map = createLandMap(MapExtent(32, 32)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); const unsigned margin = 6; RandomUtility rnd(0); - const auto mountain = GrowMountain(distances, rnd, MapPoint(16, 16), 1000, margin); + const auto mountain = growMountain(distances, rnd, MapPoint(16, 16), 1000, margin); BOOST_TEST_REQUIRE(mountain.size() > 1u); for(const MapPoint& pt : mountain) @@ -130,13 +130,13 @@ BOOST_AUTO_TEST_CASE(GrowMountain_keeps_margin_to_obstacles) } } -BOOST_AUTO_TEST_CASE(FindMountains_does_not_change_map) +BOOST_AUTO_TEST_CASE(findMountains_does_not_change_map) { const Map map = createLandMap(MapExtent(32, 32)); const auto heightsBefore = map.z; RandomUtility rnd(0); - const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + const auto mountains = findMountains(map, rnd, mountainLevel, settings); BOOST_TEST_REQUIRE(!mountains.empty()); RTTR_FOREACH_PT(MapPoint, map.size) @@ -145,41 +145,41 @@ BOOST_AUTO_TEST_CASE(FindMountains_does_not_change_map) } } -BOOST_AUTO_TEST_CASE(FindMountains_finds_nothing_without_enough_free_land) +BOOST_AUTO_TEST_CASE(findMountains_finds_nothing_without_enough_free_land) { const Map map = createLandMap(MapExtent(32, 32)); - const auto freeLand = maxDistance(GetFreeLandDistances(map, mountainLevel)); + const auto freeLand = maxDistance(getFreeLandDistances(map, mountainLevel)); RandomUtility rnd(0); const MountainSettings limit{freeLand, settings.maxSize, settings.minSize}; - BOOST_TEST(FindMountains(map, rnd, mountainLevel, limit).empty()); + BOOST_TEST(findMountains(map, rnd, mountainLevel, limit).empty()); } -BOOST_AUTO_TEST_CASE(FindMountains_finds_nothing_on_water_only) +BOOST_AUTO_TEST_CASE(findMountains_finds_nothing_on_water_only) { Map map = createMap(MapExtent(16, 16)); map.z.Resize(map.size, map.height.minimum); RandomUtility rnd(0); - BOOST_TEST(FindMountains(map, rnd, mountainLevel, settings).empty()); + BOOST_TEST(findMountains(map, rnd, mountainLevel, settings).empty()); } -BOOST_AUTO_TEST_CASE(FindMountains_covers_map_without_any_obstacles) +BOOST_AUTO_TEST_CASE(findMountains_covers_map_without_any_obstacles) { Map map = createMap(MapExtent(32, 32)); map.z.Resize(map.size, landHeight); RandomUtility rnd(0); - BOOST_TEST(!FindMountains(map, rnd, mountainLevel, settings).empty()); + BOOST_TEST(!findMountains(map, rnd, mountainLevel, settings).empty()); } -BOOST_AUTO_TEST_CASE(FindMountains_returns_disjoint_mountains) +BOOST_AUTO_TEST_CASE(findMountains_returns_disjoint_mountains) { const Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + const auto mountains = findMountains(map, rnd, mountainLevel, settings); BOOST_TEST_REQUIRE(mountains.size() > 1u); Mountain visited; @@ -192,27 +192,27 @@ BOOST_AUTO_TEST_CASE(FindMountains_returns_disjoint_mountains) } } -BOOST_AUTO_TEST_CASE(FindMountains_respects_size_of_each_mountain) +BOOST_AUTO_TEST_CASE(findMountains_respects_size_of_each_mountain) { const Map map = createLandMap(MapExtent(64, 64)); RandomUtility rnd(0); - const auto mountains = FindMountains(map, rnd, mountainLevel, settings); + const auto mountains = findMountains(map, rnd, mountainLevel, settings); BOOST_TEST_REQUIRE(mountains.size() > 1u); for(unsigned index = 0; index < mountains.size(); index++) { - BOOST_TEST_REQUIRE(mountains[index].size() <= GetMountainSize(settings, index), "Mountain " << index); + BOOST_TEST_REQUIRE(mountains[index].size() <= getMountainSize(settings, index), "Mountain " << index); } } -BOOST_AUTO_TEST_CASE(FindMountains_keeps_free_land_around_obstacles) +BOOST_AUTO_TEST_CASE(findMountains_keeps_free_land_around_obstacles) { const Map map = createLandMap(MapExtent(64, 64)); - const auto distances = GetFreeLandDistances(map, mountainLevel); + const auto distances = getFreeLandDistances(map, mountainLevel); RandomUtility rnd(0); - for(const Mountain& mountain : FindMountains(map, rnd, mountainLevel, settings)) + for(const Mountain& mountain : findMountains(map, rnd, mountainLevel, settings)) { for(const MapPoint& pt : mountain) { @@ -221,7 +221,7 @@ BOOST_AUTO_TEST_CASE(FindMountains_keeps_free_land_around_obstacles) } } -BOOST_AUTO_TEST_CASE(RaiseMountain_elevates_center_above_foot_and_keeps_surroundings) +BOOST_AUTO_TEST_CASE(raiseMountain_elevates_center_above_foot_and_keeps_surroundings) { Map map = createLandMap(MapExtent(32, 32)); const MapPoint center(16, 16); @@ -230,7 +230,7 @@ BOOST_AUTO_TEST_CASE(RaiseMountain_elevates_center_above_foot_and_keeps_surround const MapPoint outside(16, 26); RandomUtility rnd(0); - RaiseMountain(map, rnd, mountain); + raiseMountain(map, rnd, mountain); BOOST_TEST(map.z[center] > landHeight + 1); BOOST_TEST(map.z[outside] == landHeight); @@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(RaiseMountain_elevates_center_above_foot_and_keeps_surround } } -BOOST_AUTO_TEST_CASE(RaiseMountain_applies_minable_textures_to_all_nodes) +BOOST_AUTO_TEST_CASE(raiseMountain_applies_minable_textures_to_all_nodes) { Map map = createLandMap(MapExtent(32, 32)); const auto points = map.z.GetPointsInRadiusWithCenter(MapPoint(16, 16), 4); @@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(RaiseMountain_applies_minable_textures_to_all_nodes) const auto minable = map.textureMap.FindAll(IsMinableMountain); RandomUtility rnd(0); - RaiseMountain(map, rnd, mountain); + raiseMountain(map, rnd, mountain); for(const MapPoint& pt : mountain) { @@ -258,14 +258,14 @@ BOOST_AUTO_TEST_CASE(RaiseMountain_applies_minable_textures_to_all_nodes) } } -BOOST_AUTO_TEST_CASE(RaiseMountain_does_not_exceed_maximum_height) +BOOST_AUTO_TEST_CASE(raiseMountain_does_not_exceed_maximum_height) { Map map = createMap(MapExtent(32, 32)); map.z.Resize(map.size, map.height.maximum - 1); const auto points = map.z.GetPointsInRadiusWithCenter(MapPoint(16, 16), 6); RandomUtility rnd(0); - RaiseMountain(map, rnd, Mountain(points.begin(), points.end())); + raiseMountain(map, rnd, Mountain(points.begin(), points.end())); RTTR_FOREACH_PT(MapPoint, map.size) { @@ -273,26 +273,26 @@ BOOST_AUTO_TEST_CASE(RaiseMountain_does_not_exceed_maximum_height) } } -BOOST_AUTO_TEST_CASE(CreateMountains_leaves_no_free_land_beyond_limit) +BOOST_AUTO_TEST_CASE(createMountains_leaves_no_free_land_beyond_limit) { Map map = createLandMap(MapExtent(64, 64)); - BOOST_TEST_REQUIRE(maxDistance(GetFreeLandDistances(map, mountainLevel)) > settings.minFreeLand); + BOOST_TEST_REQUIRE(maxDistance(getFreeLandDistances(map, mountainLevel)) > settings.minFreeLand); RandomUtility rnd(0); - const auto mountains = CreateMountains(map, rnd, mountainLevel, settings); + const auto mountains = createMountains(map, rnd, mountainLevel, settings); BOOST_TEST_REQUIRE(!mountains.empty()); - BOOST_TEST(maxDistance(GetFreeLandDistances(map, mountainLevel)) <= settings.minFreeLand); + BOOST_TEST(maxDistance(getFreeLandDistances(map, mountainLevel)) <= settings.minFreeLand); } -BOOST_AUTO_TEST_CASE(FindMountains_is_deterministic_for_same_seed) +BOOST_AUTO_TEST_CASE(findMountains_is_deterministic_for_same_seed) { const Map map = createLandMap(MapExtent(48, 48)); RandomUtility rnd1(42); RandomUtility rnd2(42); - const auto mountains1 = FindMountains(map, rnd1, mountainLevel, settings); - const auto mountains2 = FindMountains(map, rnd2, mountainLevel, settings); + const auto mountains1 = findMountains(map, rnd1, mountainLevel, settings); + const auto mountains2 = findMountains(map, rnd2, mountainLevel, settings); BOOST_TEST_REQUIRE(mountains1.size() == mountains2.size()); for(unsigned index = 0; index < mountains1.size(); index++) diff --git a/tests/s25Main/mapGenerator/testRandomMap.cpp b/tests/s25Main/mapGenerator/testRandomMap.cpp index 33e7dd7777..50ee95a9bb 100644 --- a/tests/s25Main/mapGenerator/testRandomMap.cpp +++ b/tests/s25Main/mapGenerator/testRandomMap.cpp @@ -147,30 +147,30 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_returns_valid_map_with_max_players) ValidateMap(map, settings.size, settings.numPlayers); } -BOOST_AUTO_TEST_CASE(GetMaxMountainSize_grows_with_map_size) +BOOST_AUTO_TEST_CASE(getMaxMountainSize_grows_with_map_size) { - BOOST_TEST(GetMaxMountainSize(MapExtent(64, 64)) < GetMaxMountainSize(MapExtent(128, 128))); - BOOST_TEST(GetMaxMountainSize(MapExtent(128, 128)) < GetMaxMountainSize(MapExtent(256, 256))); - BOOST_TEST(GetMaxMountainSize(MapExtent(256, 256)) < GetMaxMountainSize(MapExtent(512, 512))); - BOOST_TEST(GetMaxMountainSize(MapExtent(512, 512)) < GetMaxMountainSize(MapExtent(1024, 1024))); + BOOST_TEST(getMaxMountainSize(MapExtent(64, 64)) < getMaxMountainSize(MapExtent(128, 128))); + BOOST_TEST(getMaxMountainSize(MapExtent(128, 128)) < getMaxMountainSize(MapExtent(256, 256))); + BOOST_TEST(getMaxMountainSize(MapExtent(256, 256)) < getMaxMountainSize(MapExtent(512, 512))); + BOOST_TEST(getMaxMountainSize(MapExtent(512, 512)) < getMaxMountainSize(MapExtent(1024, 1024))); } -BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_mountain_distance_for_medium_amount) +BOOST_AUTO_TEST_CASE(getMinFreeLand_keeps_mountain_distance_for_medium_amount) { - BOOST_TEST(GetMinFreeLand(15, 50) == 15u); + BOOST_TEST(getMinFreeLand(15, 50) == 15u); } -BOOST_AUTO_TEST_CASE(GetMinFreeLand_decreases_with_amount_of_mountains) +BOOST_AUTO_TEST_CASE(getMinFreeLand_decreases_with_amount_of_mountains) { - BOOST_TEST(GetMinFreeLand(30, 25) > GetMinFreeLand(30, 50)); - BOOST_TEST(GetMinFreeLand(30, 50) > GetMinFreeLand(30, 75)); + BOOST_TEST(getMinFreeLand(30, 25) > getMinFreeLand(30, 50)); + BOOST_TEST(getMinFreeLand(30, 50) > getMinFreeLand(30, 75)); } -BOOST_AUTO_TEST_CASE(GetMinFreeLand_keeps_space_for_headquarters) +BOOST_AUTO_TEST_CASE(getMinFreeLand_keeps_space_for_headquarters) { - BOOST_TEST(GetMinFreeLand(30, 100) == 4u); - BOOST_TEST(GetMinFreeLand(5, 90) == 4u); - BOOST_TEST(GetMinFreeLand(30, 1000) == 4u); + BOOST_TEST(getMinFreeLand(30, 100) == 4u); + BOOST_TEST(getMinFreeLand(5, 90) == 4u); + BOOST_TEST(getMinFreeLand(30, 1000) == 4u); } BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_more_mountains_for_higher_amount) @@ -239,7 +239,7 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_leaves_no_large_free_land) // Maximum of the randomized free land to keep plus the transition textures added at the foot of mountains and // desert hills const unsigned amount = settings.mountains + settings.desert + settings.swamp; - const unsigned maxFreeLand = GetMinFreeLand(static_cast(settings.mountainDistance), amount) + 5 + 3; + const unsigned maxFreeLand = getMinFreeLand(static_cast(settings.mountainDistance), amount) + 5 + 3; for(const MapStyle style : {MapStyle::Land, MapStyle::Mixed}) { @@ -321,12 +321,12 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_deserts_and_swamps_for_all_landsc }; BOOST_TEST_CONTEXT("Landscape " << landscape) { - BOOST_TEST(count(IsImpassable) > 0); + BOOST_TEST(count(isImpassable) > 0); BOOST_TEST(count(IsCoastTerrain) > 0); BOOST_TEST(helpers::count(map.objectTypes, 0xC8) > 0); for(const MapPoint& pt : map.barren) { - if(!map.textureMap.All(pt, IsImpassable)) + if(!map.textureMap.All(pt, isImpassable)) continue; // Swamps are flat and not used for fishing BOOST_TEST_REQUIRE(map.resources[pt] != libsiedler2::R_Fish, "At point " << pt); @@ -374,7 +374,7 @@ static unsigned countSpecialObjects(const Map& map) unsigned count = 0; RTTR_FOREACH_PT(MapPoint, map.size) { - if(IsSpecialObject(map.objectTypes[pt], map.objectInfos[pt])) + if(isSpecialObject(map.objectTypes[pt], map.objectInfos[pt])) count++; } return count; @@ -422,10 +422,10 @@ BOOST_AUTO_TEST_CASE(GenerateRandomMap_creates_a_few_special_objects_if_enabled) { // The graveyard contains more than one special object BOOST_TEST(countSpecialObjects(map) > 0u); - BOOST_TEST(countSpecialObjects(map) <= 16 * GetNumSpecialScenes(map.size)); + BOOST_TEST(countSpecialObjects(map) <= 16 * getNumSpecialScenes(map.size)); BOOST_TEST(map.reserved.size() % 169 == 0u); BOOST_TEST(map.reserved.size() > 0u); - BOOST_TEST(map.reserved.size() <= 169 * GetNumSpecialScenes(map.size)); + BOOST_TEST(map.reserved.size() <= 169 * getNumSpecialScenes(map.size)); } } } diff --git a/tests/s25Main/mapGenerator/testSpecialObjects.cpp b/tests/s25Main/mapGenerator/testSpecialObjects.cpp index fb962b5229..1877dd8ceb 100644 --- a/tests/s25Main/mapGenerator/testSpecialObjects.cpp +++ b/tests/s25Main/mapGenerator/testSpecialObjects.cpp @@ -56,20 +56,20 @@ unsigned countObjects(const Map& map) BOOST_FIXTURE_TEST_SUITE(SpecialObjectsTests, SpecialObjectsFixture) -BOOST_AUTO_TEST_CASE(GetNumSpecialScenes_returns_a_few_scenes_depending_on_map_size) +BOOST_AUTO_TEST_CASE(getNumSpecialScenes_returns_a_few_scenes_depending_on_map_size) { - BOOST_TEST(GetNumSpecialScenes(MapExtent(32, 32)) == 1u); - BOOST_TEST(GetNumSpecialScenes(MapExtent(64, 64)) < GetNumSpecialScenes(MapExtent(128, 128))); - BOOST_TEST(GetNumSpecialScenes(MapExtent(128, 128)) < GetNumSpecialScenes(MapExtent(256, 256))); - BOOST_TEST(GetNumSpecialScenes(MapExtent(1024, 1024)) <= 6u); + BOOST_TEST(getNumSpecialScenes(MapExtent(32, 32)) == 1u); + BOOST_TEST(getNumSpecialScenes(MapExtent(64, 64)) < getNumSpecialScenes(MapExtent(128, 128))); + BOOST_TEST(getNumSpecialScenes(MapExtent(128, 128)) < getNumSpecialScenes(MapExtent(256, 256))); + BOOST_TEST(getNumSpecialScenes(MapExtent(1024, 1024)) <= 6u); } -BOOST_AUTO_TEST_CASE(FindSceneSites_returns_requested_number_of_distant_sites) +BOOST_AUTO_TEST_CASE(findSceneSites_returns_requested_number_of_distant_sites) { const Map map = createLandMap(MapExtent(96, 96)); RandomUtility rnd(0); - const auto sites = FindSceneSites(map, rnd, 4); + const auto sites = findSceneSites(map, rnd, 4); BOOST_TEST_REQUIRE(sites.size() == 4u); for(unsigned i = 0; i < sites.size(); i++) @@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE(FindSceneSites_returns_requested_number_of_distant_sites) } } -BOOST_AUTO_TEST_CASE(FindSceneSites_finds_nothing_without_buildable_land) +BOOST_AUTO_TEST_CASE(findSceneSites_finds_nothing_without_buildable_land) { Map map = createMap(MapExtent(32, 32)); const auto water = map.textureMap.Find(IsWater); @@ -89,10 +89,10 @@ BOOST_AUTO_TEST_CASE(FindSceneSites_finds_nothing_without_buildable_land) } RandomUtility rnd(0); - BOOST_TEST(FindSceneSites(map, rnd, 3).empty()); + BOOST_TEST(findSceneSites(map, rnd, 3).empty()); } -BOOST_AUTO_TEST_CASE(FindSceneSites_keeps_distance_to_obstacles_and_players) +BOOST_AUTO_TEST_CASE(findSceneSites_keeps_distance_to_obstacles_and_players) { Map map = createLandMap(MapExtent(64, 64)); const MapPoint hq(10, 10); @@ -101,12 +101,12 @@ BOOST_AUTO_TEST_CASE(FindSceneSites_keeps_distance_to_obstacles_and_players) map.hqPositions.push_back(hq); map.harbors.push_back(Triangle(true, MapPoint(50, 12))); map.objectTypes[tree] = libsiedler2::OT_TreeOrPalm; - map.textureMap.Set(swamp, map.textureMap.Find(IsImpassable)); + map.textureMap.Set(swamp, map.textureMap.Find(isImpassable)); for(unsigned seed = 0; seed < 30; seed++) { RandomUtility rnd(seed); - for(const MapPoint& site : FindSceneSites(map, rnd, 3)) + for(const MapPoint& site : findSceneSites(map, rnd, 3)) { // Objects around headquarters and harbors are removed within a radius of 5 BOOST_TEST_REQUIRE(map.z.CalcDistance(site, hq) > sceneRadius + 5, "At point " << site); @@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE(FindSceneSites_keeps_distance_to_obstacles_and_players) } } -BOOST_AUTO_TEST_CASE(CreateScene_surrounds_special_object_by_decoration_within_scene) +BOOST_AUTO_TEST_CASE(createScene_surrounds_special_object_by_decoration_within_scene) { for(unsigned i = 0; i < numLandscapes; i++) { @@ -132,12 +132,12 @@ BOOST_AUTO_TEST_CASE(CreateScene_surrounds_special_object_by_decoration_within_s const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, type, style); + createScene(map, rnd, center, type, style); BOOST_TEST_CONTEXT("Landscape " << i << " scene " << static_cast(type) << " style " << static_cast(style) << " seed " << seed) { - BOOST_TEST(IsSpecialObject(map.objectTypes[center], map.objectInfos[center])); + BOOST_TEST(isSpecialObject(map.objectTypes[center], map.objectInfos[center])); BOOST_TEST(countObjects(map) >= 15u); BOOST_TEST(map.reserved.size() == 169u); RTTR_FOREACH_PT(MapPoint, map.size) @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_surrounds_special_object_by_decoration_within_s } } -BOOST_AUTO_TEST_CASE(CreateScene_keeps_footprint_of_castle_ruin_free_and_even) +BOOST_AUTO_TEST_CASE(createScene_keeps_footprint_of_castle_ruin_free_and_even) { for(const SceneStyle style : helpers::EnumRange()) { @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_keeps_footprint_of_castle_ruin_free_and_even) const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, SceneType::CastleRuin, style); + createScene(map, rnd, center, SceneType::CastleRuin, style); BOOST_TEST(map.objectInfos[center] == OI_CastleRuin); for(const Direction dir : {Direction::West, Direction::NorthWest, Direction::NorthEast}) @@ -193,14 +193,14 @@ BOOST_AUTO_TEST_CASE(CreateScene_keeps_footprint_of_castle_ruin_free_and_even) } } -BOOST_AUTO_TEST_CASE(CreateScene_blends_ground_into_surrounding_land) +BOOST_AUTO_TEST_CASE(createScene_blends_ground_into_surrounding_land) { Map map = createLandMap(MapExtent(40, 40)); const MapPoint center(20, 20); RandomUtility rnd(0); const auto isFlowers = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0F; }; - CreateScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Overgrown); + createScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Overgrown); // Flowers in the middle fade out via the textures between flowers and meadow BOOST_TEST(map.textureMap.All(center, isFlowers)); @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_blends_ground_into_surrounding_land) BOOST_TEST(textures.count(0x0A) == 1u); } -BOOST_AUTO_TEST_CASE(CreateScene_uses_all_transition_textures_between_dry_ground_and_meadow) +BOOST_AUTO_TEST_CASE(createScene_uses_all_transition_textures_between_dry_ground_and_meadow) { const auto isSteppe = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0E; }; const auto isSavannah = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x00; }; @@ -229,7 +229,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_uses_all_transition_textures_between_dry_ground const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, SceneType::Camp, style); + createScene(map, rnd, center, SceneType::Camp, style); BOOST_TEST(map.textureMap.All(center, isSteppe)); unsigned savannah = 0; @@ -246,7 +246,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_uses_all_transition_textures_between_dry_ground } } -BOOST_AUTO_TEST_CASE(CreateScene_surrounds_pond_by_transitions_from_sand_to_meadow) +BOOST_AUTO_TEST_CASE(createScene_surrounds_pond_by_transitions_from_sand_to_meadow) { const auto isSteppe = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x0E; }; const auto isSavannah = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x00; }; @@ -259,7 +259,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_surrounds_pond_by_transitions_from_sand_to_mead const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, type, SceneStyle::Crag); + createScene(map, rnd, center, type, SceneStyle::Crag); BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " seed " << seed) { @@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_surrounds_pond_by_transitions_from_sand_to_mead } } -BOOST_AUTO_TEST_CASE(CreateScene_adds_transitions_within_lush_meadow) +BOOST_AUTO_TEST_CASE(createScene_adds_transitions_within_lush_meadow) { // The most lush meadow is several textures away from sand but looks like any other meadow const auto isGreen = [](const TerrainDesc& terrain) { return IsBuildableLand(terrain) && terrain.humidity > 90; }; @@ -303,7 +303,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_adds_transitions_within_lush_meadow) const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, type, style); + createScene(map, rnd, center, type, style); BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " style " << static_cast(style) << " seed " << seed) @@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_adds_transitions_within_lush_meadow) } } -BOOST_AUTO_TEST_CASE(CreateScene_leaves_other_landscapes_untouched) +BOOST_AUTO_TEST_CASE(createScene_leaves_other_landscapes_untouched) { for(const SceneType type : helpers::EnumRange()) { @@ -354,12 +354,12 @@ BOOST_AUTO_TEST_CASE(CreateScene_leaves_other_landscapes_untouched) const Map original = map; RandomUtility rnd(seed); - CreateScene(map, rnd, center, type, style); + createScene(map, rnd, center, type, style); BOOST_TEST_CONTEXT("Scene " << static_cast(type) << " style " << static_cast(style) << " seed " << seed) { - BOOST_TEST(IsSpecialObject(map.objectTypes[center], map.objectInfos[center])); + BOOST_TEST(isSpecialObject(map.objectTypes[center], map.objectInfos[center])); BOOST_TEST(map.objectTypes[tree] == original.objectTypes[tree]); BOOST_TEST(map.objectInfos[tree] == original.objectInfos[tree]); const auto isLand = [this](DescIdx texture) { @@ -384,14 +384,14 @@ BOOST_AUTO_TEST_CASE(CreateScene_leaves_other_landscapes_untouched) } } -BOOST_AUTO_TEST_CASE(CreateScene_adds_rim_around_rock) +BOOST_AUTO_TEST_CASE(createScene_adds_rim_around_rock) { Map map = createLandMap(MapExtent(40, 40)); const MapPoint center(20, 20); RandomUtility rnd(0); const auto isRock = [](const TerrainDesc& terrain) { return terrain.s2Id == 0x22; }; - CreateScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Overgrown); + createScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Overgrown); BOOST_TEST(map.textureMap.All(center, isRock)); BOOST_TEST(map.z[center] > landHeight); @@ -402,7 +402,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_adds_rim_around_rock) } } -BOOST_AUTO_TEST_CASE(CreateScene_raises_crag_next_to_pond) +BOOST_AUTO_TEST_CASE(createScene_raises_crag_next_to_pond) { for(unsigned seed = 0; seed < 10; seed++) { @@ -410,7 +410,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_raises_crag_next_to_pond) const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Crag); + createScene(map, rnd, center, SceneType::RuinGrove, SceneStyle::Crag); unsigned pond = 0; unsigned rocks = 0; @@ -435,7 +435,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_raises_crag_next_to_pond) } } -BOOST_AUTO_TEST_CASE(CreateScene_lines_road_of_avenue_with_trees) +BOOST_AUTO_TEST_CASE(createScene_lines_road_of_avenue_with_trees) { for(unsigned seed = 0; seed < 10; seed++) { @@ -443,7 +443,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_lines_road_of_avenue_with_trees) const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Avenue); + createScene(map, rnd, center, SceneType::CastleRuin, SceneStyle::Avenue); // One of the six directions is a free road with trees on both sides unsigned roads = 0; @@ -471,13 +471,13 @@ BOOST_AUTO_TEST_CASE(CreateScene_lines_road_of_avenue_with_trees) } } -BOOST_AUTO_TEST_CASE(CreateScene_lines_trail_with_remains) +BOOST_AUTO_TEST_CASE(createScene_lines_trail_with_remains) { Map map = createLandMap(MapExtent(40, 40)); const MapPoint center(20, 20); RandomUtility rnd(0); - CreateScene(map, rnd, center, SceneType::StoneCircle, SceneStyle::Trail); + createScene(map, rnd, center, SceneType::StoneCircle, SceneStyle::Trail); unsigned stones = 0; RTTR_FOREACH_PT(MapPoint, map.size) @@ -490,7 +490,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_lines_trail_with_remains) BOOST_TEST((map.objectInfos[center] == OI_Gate || map.objectInfos[center] == OI_OpenGate)); } -BOOST_AUTO_TEST_CASE(CreateScene_places_single_tent_and_rows_of_graves_in_any_style) +BOOST_AUTO_TEST_CASE(createScene_places_single_tent_and_rows_of_graves_in_any_style) { for(const SceneStyle style : helpers::EnumRange()) { @@ -501,8 +501,8 @@ BOOST_AUTO_TEST_CASE(CreateScene_places_single_tent_and_rows_of_graves_in_any_st const MapPoint center(20, 20); RandomUtility rnd(seed); - CreateScene(camp, rnd, center, SceneType::Camp, style); - CreateScene(graveyard, rnd, center, SceneType::Graveyard, style); + createScene(camp, rnd, center, SceneType::Camp, style); + createScene(graveyard, rnd, center, SceneType::Graveyard, style); unsigned tents = 0; unsigned graves = 0; @@ -519,7 +519,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_places_single_tent_and_rows_of_graves_in_any_st } } -BOOST_AUTO_TEST_CASE(CreateScene_levels_ground_and_smooths_transition) +BOOST_AUTO_TEST_CASE(createScene_levels_ground_and_smooths_transition) { Map map = createLandMap(MapExtent(48, 48)); RTTR_FOREACH_PT(MapPoint, map.size) @@ -530,7 +530,7 @@ BOOST_AUTO_TEST_CASE(CreateScene_levels_ground_and_smooths_transition) const uint8_t level = map.z[center]; RandomUtility rnd(0); - CreateScene(map, rnd, center, SceneType::Camp, SceneStyle::Trail); + createScene(map, rnd, center, SceneType::Camp, SceneStyle::Trail); for(const MapPoint& pt : map.z.GetPointsInRadiusWithCenter(center, sceneRadius)) { @@ -541,33 +541,33 @@ BOOST_AUTO_TEST_CASE(CreateScene_levels_ground_and_smooths_transition) } } -BOOST_AUTO_TEST_CASE(AddSpecialScenes_mixes_up_types_of_scenes) +BOOST_AUTO_TEST_CASE(addSpecialScenes_mixes_up_types_of_scenes) { Map map = createLandMap(MapExtent(256, 256)); RandomUtility rnd(0); - const auto scenes = AddSpecialScenes(map, rnd); + const auto scenes = addSpecialScenes(map, rnd); - BOOST_TEST_REQUIRE(scenes.size() == GetNumSpecialScenes(map.size)); + BOOST_TEST_REQUIRE(scenes.size() == getNumSpecialScenes(map.size)); std::set types; for(const Scene& scene : scenes) { types.insert(scene.type); - BOOST_TEST(IsSpecialObject(map.objectTypes[scene.center], map.objectInfos[scene.center])); + BOOST_TEST(isSpecialObject(map.objectTypes[scene.center], map.objectInfos[scene.center])); } BOOST_TEST(types.size() == 5u); BOOST_TEST(map.reserved.size() == 169 * scenes.size()); } -BOOST_AUTO_TEST_CASE(AddSpecialScenes_varies_with_seed) +BOOST_AUTO_TEST_CASE(addSpecialScenes_varies_with_seed) { Map map1 = createLandMap(MapExtent(64, 64)); Map map2 = createLandMap(MapExtent(64, 64)); RandomUtility rnd1(1); RandomUtility rnd2(2); - const auto scenes1 = AddSpecialScenes(map1, rnd1); - const auto scenes2 = AddSpecialScenes(map2, rnd2); + const auto scenes1 = addSpecialScenes(map1, rnd1); + const auto scenes2 = addSpecialScenes(map2, rnd2); BOOST_TEST_REQUIRE(scenes1.size() == 1u); BOOST_TEST_REQUIRE(scenes2.size() == 1u); @@ -578,7 +578,7 @@ BOOST_AUTO_TEST_CASE(AddObjects_keeps_scenes_free_from_trees_and_stone_piles) { Map map = createLandMap(MapExtent(48, 48)); RandomUtility rnd(0); - const auto scenes = AddSpecialScenes(map, rnd); + const auto scenes = addSpecialScenes(map, rnd); BOOST_TEST_REQUIRE(!scenes.empty()); const auto typesBefore = map.objectTypes; const auto infosBefore = map.objectInfos;