diff options
Diffstat (limited to 'game/network')
| -rw-r--r-- | game/network/network.cpp | 54 | ||||
| -rw-r--r-- | game/network/network.h | 14 | ||||
| -rw-r--r-- | game/network/rail.cpp | 16 | ||||
| -rw-r--r-- | game/network/rail.h | 1 |
4 files changed, 78 insertions, 7 deletions
diff --git a/game/network/network.cpp b/game/network/network.cpp index 70c4e21..ae4865a 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -179,10 +179,13 @@ Network::genDef(const GlobalPosition3D & start, const GlobalPosition3D & end, An const auto flatStart = start.xy(), flatEnd = end.xy(); const auto centre = find_arc_centre(flatStart, dir, flatEnd); - if (centre.second) { // right hand arc + if (centre.second > 0.1F) { return {GenCurveDef {end, start, centre.first}}; } - return {GenCurveDef {start, end, centre.first}}; + if (centre.second < -0.1F) { + return {GenCurveDef {start, end, centre.first}}; + } + return {GenStraightDef {start, end}}; } GenLinksDef @@ -219,13 +222,38 @@ Network::genDef(const GlobalPosition3D & start, const GlobalPosition3D & end, An return genDef(start, joint, startDir) + genDef(end, joint, endDir); } +namespace { + struct MergeEq { + bool + operator()(GenCurveDef & lhs, const GenCurveDef & rhs) const + { + if (distance(std::get<2>(lhs), std::get<2>(rhs)) < 100.F) { // LHS.centre near RHS.centre + if (std::get<1>(lhs) == std::get<0>(rhs)) { // LHS.end == RHS.start + std::get<1>(lhs) = std::get<1>(rhs); + } + else if (std::get<0>(lhs) == std::get<1>(rhs)) { // LHS.start == RHS.end + std::get<0>(lhs) = std::get<0>(rhs); + } + else { + return false; + } + std::get<2>(lhs) = midpoint(std::get<2>(lhs), std::get<2>(rhs)); + return true; + } + return false; + } + + bool + operator()(const auto &, const auto &) const + { + return false; + } + }; +} + Link::Collection Network::create(const GeoData * geoData, const CreationDefinition & def) { - // TODO - // Where to make a straight to join because angles align? - // Where to drop part of S curve pair if a single curve works? - const auto linkDefs = [&def]() -> GenLinksDef { if (!def.fromEnd.direction && !def.toEnd.direction) { // No specific directions at either end, straight link @@ -251,8 +279,20 @@ Network::create(const GeoData * geoData, const CreationDefinition & def) def); }); }; + // Merge adjacent pairs where possible + auto linkDefsGen = geoData ? splitDefs() : linkDefs(); + if (!linkDefsGen.empty()) { + for (auto lhsIter = linkDefsGen.begin(), rhsIter = lhsIter + 1; rhsIter != linkDefsGen.end();) { + if (std::visit(MergeEq {}, *lhsIter, *rhsIter)) { + rhsIter = linkDefsGen.erase(rhsIter); + } + else { + lhsIter = rhsIter++; + } + } + } Link::Collection links; - std::ranges::transform(geoData ? splitDefs() : linkDefs(), std::back_inserter(links), [this](const auto & def) { + std::ranges::transform(linkDefsGen, std::back_inserter(links), [this](const auto & def) { return std::visit( [this](const auto & typedDef) { return this->create(typedDef); diff --git a/game/network/network.h b/game/network/network.h index d3370b3..7bfd2ce 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -35,6 +35,19 @@ struct CreationDefinition { CreationDefinitionEnd toEnd; }; +struct SnapPoint : CreationDefinitionEnd { + template<typename... CDE> + requires std::is_constructible_v<CreationDefinitionEnd, CDE...> + explicit SnapPoint(GlobalPosition3D snapPosition, CDE &&... cde) : + CreationDefinitionEnd {std::forward<CDE>(cde)...}, snapPosition(snapPosition) + { + } + + GlobalPosition3D snapPosition; +}; + +using SnapPoints = std::vector<SnapPoint>; + class Network { public: using LinkEnd = std::pair<Link::Ptr, unsigned char>; @@ -55,6 +68,7 @@ public: [[nodiscard]] static Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &); [[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0; + [[nodiscard]] virtual SnapPoints getSnapPoints() const = 0; [[nodiscard]] Link::Collection create(const GeoData *, const CreationDefinition &); [[nodiscard]] Link::Collection createChain(const GeoData *, std::span<const GlobalPosition3D>); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 342a2ad..45d55d6 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -137,3 +137,19 @@ RailLinks::getBaseWidth() const static constexpr auto BASE_WIDTH = 5'700; return BASE_WIDTH; } + +SnapPoints +RailLinks::getSnapPoints() const +{ + static constexpr auto EXTENSION_SNAP_DIST = 1'200.F; + SnapPoints out; + for (const auto & link : links) { + for (const auto & end : link->ends) { + // Link end node; directionless, suitable crossings + out.emplace_back(end.node->pos, end.node->pos, std::nullopt); + // Link end; with direction, suitable for continuing/joining + out.emplace_back(end.node->pos - ((sincos(end.dir) * EXTENSION_SNAP_DIST) || 0.F), end.node->pos, end.dir); + } + } + return out; +} diff --git a/game/network/rail.h b/game/network/rail.h index 15b9ae4..48b1410 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -78,6 +78,7 @@ public: [[nodiscard]] const Surface * getBaseSurface() const override; [[nodiscard]] RelativeDistance getBaseWidth() const override; + [[nodiscard]] SnapPoints getSnapPoints() const override; private: void tick(TickDuration elapsed) override; |
