From dd80098076cda9c103b44356ca07c2c8bc7698d7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 14 Feb 2021 13:14:32 +0000 Subject: Join links in rail graph --- game/network/link.h | 5 +++++ game/network/rail.cpp | 17 +++++++++++++++++ game/network/rail.h | 5 ++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/game/network/link.h b/game/network/link.h index 287ba14..c9fbedb 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -6,6 +6,7 @@ #include #include #include +#include // Generic network node // something that can be travelled to @@ -24,6 +25,8 @@ using NodePtr = std::shared_ptr; // Generic network link // something that can be travelled along // it joins 2 nodes +class Link; +using LinkPtr = std::shared_ptr; class Link { public: using End = std::pair; @@ -35,6 +38,8 @@ public: std::array ends; float length; + using Next = std::pair; + std::array, 2> nexts; }; bool operator<(const glm::vec3 & a, const glm::vec3 & b); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index fd2ea08..a66bf65 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -15,6 +15,23 @@ RailLinks::RailLinks() : texture {Texture::cachedTexture.get("rails.jpg")} { } void RailLinks::tick(TickDuration) { } +void +RailLinks::joinLinks(LinkPtr l) const +{ + for (const auto & ol : links.objects) { + if (l != ol) { + for (const auto oe : {0, 1}) { + for (const auto te : {0, 1}) { + if (l->ends[te].first == ol->ends[oe].first) { + l->nexts[te].emplace_back(ol.get(), oe); + ol->nexts[oe].emplace_back(l.get(), te); + } + } + } + } + } +} + void RailLinks::render(const Shader & shader) const { diff --git a/game/network/rail.h b/game/network/rail.h index 5fea713..a501f32 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -61,7 +61,9 @@ public: { const auto node1 = *nodes.insert(std::make_shared(a)).first; const auto node2 = *nodes.insert(std::make_shared(b)).first; - return links.create(node1, node2, std::forward(params)...); + auto l = links.create(node1, node2, std::forward(params)...); + joinLinks(l); + return l; } private: @@ -70,6 +72,7 @@ private: Nodes nodes; void render(const Shader &) const override; void tick(TickDuration elapsed) override; + void joinLinks(LinkPtr) const; std::shared_ptr texture; }; -- cgit v1.2.3