Unify tunnel build mode into a single Tunnel button

This commit is contained in:
2026-07-21 21:12:06 +02:00
parent 4ca5b332cd
commit 9b63af6ccb
14 changed files with 522 additions and 63 deletions

View File

@@ -9,6 +9,7 @@ add_files(
BeltSystemTest.cpp
SurfaceMaskTest.cpp
BeltDragPathTest.cpp
TunnelCompletionTest.cpp
BuildingTest.cpp
BuildingConfigTest.cpp
ShipTest.cpp

View File

@@ -0,0 +1,171 @@
#include "catch.hpp"
#include <map>
#include <optional>
#include <utility>
#include <QPoint>
#include <QVector2D>
#include "BuildingType.h"
#include "Rotation.h"
#include "TunnelCompletion.h"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
namespace
{
using TunnelMap = std::map<std::pair<int, int>, TunnelTileInfo>;
TunnelLookup makeLookup(const TunnelMap& tunnels)
{
return [&tunnels](QPoint tile) -> std::optional<TunnelTileInfo>
{
const TunnelMap::const_iterator it = tunnels.find({tile.x(), tile.y()});
if (it == tunnels.end()) { return std::nullopt; }
return it->second;
};
}
// Cursor at the centre of a tile.
QVector2D tileCentre(QPoint tile)
{
return QVector2D(static_cast<float>(tile.x()) + 0.5f,
static_cast<float>(tile.y()) + 0.5f);
}
} // namespace
// ---------------------------------------------------------------------------
// No match
// ---------------------------------------------------------------------------
TEST_CASE("No tunnels: ghost defaults to a Tunnel Entry with no partner")
{
const TunnelMap tunnels;
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 10, tileCentre(QPoint(5, 5)));
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(!result.partnerTile.has_value());
}
// ---------------------------------------------------------------------------
// Entry-completion: an existing exit ahead keeps the ghost an entry
// ---------------------------------------------------------------------------
TEST_CASE("Existing exit ahead completes as an entry")
{
TunnelMap tunnels;
tunnels[{8, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::East};
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 10, tileCentre(QPoint(5, 5)));
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(result.partnerTile.has_value());
REQUIRE(*result.partnerTile == QPoint(8, 5));
}
// ---------------------------------------------------------------------------
// Exit-completion: an existing entry behind flips the ghost to an exit
// ---------------------------------------------------------------------------
TEST_CASE("Existing entry behind completes as an exit")
{
TunnelMap tunnels;
tunnels[{2, 5}] = TunnelTileInfo{BuildingType::TunnelEntry, Rotation::East};
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 10, tileCentre(QPoint(5, 5)));
REQUIRE(result.resolvedType == BuildingType::TunnelExit);
REQUIRE(result.partnerTile.has_value());
REQUIRE(*result.partnerTile == QPoint(2, 5));
}
// ---------------------------------------------------------------------------
// Both matches: the sub-tile cursor position breaks the tie
// ---------------------------------------------------------------------------
TEST_CASE("Both matches resolve to the partner nearer the sub-tile cursor")
{
TunnelMap tunnels;
tunnels[{3, 5}] = TunnelTileInfo{BuildingType::TunnelEntry, Rotation::East}; // behind
tunnels[{7, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::East}; // ahead
const TunnelLookup lookup = makeLookup(tunnels);
SECTION("cursor near the left edge completes the entry (ghost becomes exit)")
{
const QVector2D cursor(5.1f, 5.5f);
const TunnelCompletion result =
resolveTunnelCompletion(lookup, QPoint(5, 5), Rotation::East, 10, cursor);
REQUIRE(result.resolvedType == BuildingType::TunnelExit);
REQUIRE(*result.partnerTile == QPoint(3, 5));
}
SECTION("cursor near the right edge completes the exit (ghost stays entry)")
{
const QVector2D cursor(5.9f, 5.5f);
const TunnelCompletion result =
resolveTunnelCompletion(lookup, QPoint(5, 5), Rotation::East, 10, cursor);
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(*result.partnerTile == QPoint(7, 5));
}
}
// ---------------------------------------------------------------------------
// A same-direction tunnel between the ghost and a candidate blocks the pairing
// ---------------------------------------------------------------------------
TEST_CASE("A same-direction entry between the ghost and an exit blocks completion")
{
TunnelMap tunnels;
tunnels[{7, 5}] = TunnelTileInfo{BuildingType::TunnelEntry, Rotation::East}; // blocker
tunnels[{9, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::East};
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 10, tileCentre(QPoint(5, 5)));
// The forward scan stops at the same-direction entry, which is not an exit, so no
// entry-completion. Nothing behind, so no exit-completion either.
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(!result.partnerTile.has_value());
}
// ---------------------------------------------------------------------------
// Differently-facing tunnels are skipped by the scan
// ---------------------------------------------------------------------------
TEST_CASE("A differently-facing tunnel ahead is skipped, not treated as a match")
{
TunnelMap tunnels;
tunnels[{6, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::North}; // skipped
tunnels[{8, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::East}; // match
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 10, tileCentre(QPoint(5, 5)));
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(*result.partnerTile == QPoint(8, 5));
}
// ---------------------------------------------------------------------------
// Distance limit
// ---------------------------------------------------------------------------
TEST_CASE("A candidate beyond the max distance is not matched")
{
TunnelMap tunnels;
tunnels[{8, 5}] = TunnelTileInfo{BuildingType::TunnelExit, Rotation::East}; // distance 3
const TunnelCompletion result = resolveTunnelCompletion(
makeLookup(tunnels), QPoint(5, 5), Rotation::East, 2, tileCentre(QPoint(5, 5)));
REQUIRE(result.resolvedType == BuildingType::TunnelEntry);
REQUIRE(!result.partnerTile.has_value());
}