Skip to content

Commit 322c705

Browse files
FlamefireFlow86
authored andcommitted
Add test for shuffling HQ positions
1 parent b60d26c commit 322c705

4 files changed

Lines changed: 57 additions & 24 deletions

File tree

libs/common/include/helpers/containerUtils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ size_t count_if(const T& container, T_Predicate&& predicate)
128128
return static_cast<std::make_unsigned_t<decltype(result)>>(result);
129129
}
130130

131+
/// Sort collection with optional predicate
132+
template<class T, class T_Predicate = std::less<>>
133+
void sort(T& container, T_Predicate&& predicate = T_Predicate{})
134+
{
135+
std::sort(begin(container), end(container), std::forward<T_Predicate>(predicate));
136+
}
137+
131138
/// Remove duplicate values from the given sorted container
132139
template<class T>
133140
void makeUniqueSorted(T& container)
@@ -139,13 +146,13 @@ void makeUniqueSorted(T& container)
139146
template<class T>
140147
void makeUnique(T& container)
141148
{
142-
std::sort(begin(container), end(container));
149+
sort(container);
143150
makeUniqueSorted(container);
144151
}
145152
template<class T, class T_Predicate>
146153
void makeUnique(T& container, T_Predicate&& predicate)
147154
{
148-
std::sort(begin(container), end(container), std::forward<T_Predicate>(predicate));
155+
sort(container, std::forward<T_Predicate>(predicate));
149156
makeUniqueSorted(container);
150157
}
151158
/// Remove duplicate values from the given container without changing the order

libs/s25main/languages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Loader.h"
77
#include "RttrConfig.h"
88
#include "files.h"
9+
#include "helpers/containerUtils.h"
910
#include "mygettext/mygettext.h"
1011
#include "libsiedler2/ArchivItem_Ini.h"
1112
#include "libsiedler2/ArchivItem_Text.h"

libs/s25main/world/MapLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class MapLoader
5151
/// Return false if there was an error (e.g. invalid start position)
5252
bool PlaceHQs(bool addStartWares = true);
5353

54-
/// Return the position of the players HQ (only valid after successful load)
55-
MapPoint GetHQPos(unsigned player) const { return hqPositions_[player]; }
54+
/// Return the (original/unshuffled) position of the players HQ (only valid after successful load)
55+
MapPoint GetOriginalHQPos(unsigned player) const { return hqPositions_[player]; }
5656

5757
static void InitShadows(World& world);
5858
static void SetMapExplored(World& world);

tests/s25Main/integration/testWorld.cpp

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ struct UninitializedWorldCreator
4040
bool operator()(GameWorldBase&) { return true; }
4141
};
4242

43-
struct LoadWorldFromFileCreator : MapTestFixture
44-
{
45-
std::vector<MapPoint> hqs;
46-
47-
explicit LoadWorldFromFileCreator(MapExtent) {}
48-
bool operator()(GameWorldBase& world)
49-
{
50-
MapLoader loader(world);
51-
BOOST_TEST_REQUIRE(loader.Load(testMapPath));
52-
for(unsigned i = 0; i < world.GetNumPlayers(); i++)
53-
hqs.push_back(loader.GetHQPos(i));
54-
return true;
55-
}
56-
};
5743
struct LoadWorldAndS2MapCreator : MapTestFixture
5844
{
5945
libsiedler2::ArchivItem_Map map;
@@ -72,7 +58,6 @@ struct LoadWorldAndS2MapCreator : MapTestFixture
7258
};
7359

7460
using WorldLoadedWithS2MapFixture = WorldFixture<LoadWorldAndS2MapCreator>;
75-
using WorldLoaded1PFixture = WorldFixture<LoadWorldFromFileCreator, 1>;
7661
using WorldFixtureEmpty1P = WorldFixture<CreateEmptyWorld, 1>;
7762
} // namespace
7863

@@ -133,12 +118,52 @@ BOOST_FIXTURE_TEST_CASE(SameBQasInS2, WorldLoadedWithS2MapFixture)
133118
}
134119
}
135120

136-
BOOST_FIXTURE_TEST_CASE(HQPlacement, WorldLoaded1PFixture)
121+
BOOST_AUTO_TEST_CASE(HQPlacement)
137122
{
138-
GamePlayer& player = world.GetPlayer(0);
139-
BOOST_TEST_REQUIRE(player.isUsed());
140-
BOOST_TEST_REQUIRE(worldCreator.hqs[0].isValid());
141-
BOOST_TEST_REQUIRE(world.GetNO(worldCreator.hqs[0])->GetGOT() == GO_Type::NobHq);
123+
constexpr auto numPlayers = 4u;
124+
125+
std::vector<MapPoint> hqsOriginalMap, hqsShuffledMap, hqsOriginalWorld, hqsShuffledWorld;
126+
for(const bool randStartPos : {false, true})
127+
{
128+
BOOST_TEST_INFO_SCOPE("Random: " << randStartPos);
129+
WorldFixtureBase fixture(numPlayers);
130+
fixture.ggs.randomStartPosition = randStartPos;
131+
auto& world = fixture.world;
132+
MapLoader loader(world);
133+
BOOST_TEST_REQUIRE(loader.Load(testMapPath));
134+
auto& hqsMap = randStartPos ? hqsShuffledMap : hqsOriginalMap;
135+
auto& hqsWorld = randStartPos ? hqsShuffledWorld : hqsOriginalWorld;
136+
hqsWorld.resize(world.GetNumPlayers());
137+
for(unsigned i = 0; i < world.GetNumPlayers(); i++)
138+
{
139+
BOOST_TEST_INFO_SCOPE("Player: " << i);
140+
GamePlayer& player = world.GetPlayer(i);
141+
BOOST_TEST_REQUIRE(player.isUsed());
142+
143+
auto hqPos = loader.GetOriginalHQPos(i);
144+
BOOST_TEST_REQUIRE(hqPos.isValid());
145+
BOOST_TEST(!helpers::contains(hqsMap, hqPos)); // Unique position
146+
hqsMap.push_back(hqPos);
147+
BOOST_TEST(world.GetNO(hqPos)->GetGOT() == GO_Type::NobHq);
148+
149+
hqPos = world.GetPlayer(i).GetHQPos();
150+
BOOST_TEST_REQUIRE(hqPos.isValid());
151+
BOOST_TEST(!helpers::contains(hqsWorld, hqPos)); // Unique position
152+
hqsWorld[i] = hqPos;
153+
}
154+
}
155+
BOOST_TEST(hqsOriginalMap.size() == numPlayers);
156+
BOOST_TEST(hqsShuffledMap.size() == numPlayers);
157+
// The loader stores the HQ positions read from the map
158+
BOOST_TEST(hqsShuffledMap == hqsOriginalMap);
159+
// When shuffled the positions should have changed
160+
BOOST_TEST(hqsShuffledWorld != hqsOriginalWorld);
161+
helpers::sort(hqsOriginalMap, MapPointLess{});
162+
helpers::sort(hqsShuffledWorld, MapPointLess{});
163+
helpers::sort(hqsOriginalWorld, MapPointLess{});
164+
// But the same positions should be used just in different order
165+
BOOST_TEST(hqsOriginalMap == hqsOriginalWorld);
166+
BOOST_TEST(hqsShuffledWorld == hqsOriginalWorld);
142167
}
143168

144169
BOOST_FIXTURE_TEST_CASE(CloseHarborSpots, WorldFixture<UninitializedWorldCreator>)

0 commit comments

Comments
 (0)