diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-10-22 19:06:14 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-10-22 19:06:14 +0100 |
commit | 873ef1436e233b5b4c542f35838a7cd73432ede6 (patch) | |
tree | ad5b0f0bba71c5da5791471216855a1af221c26e /game | |
parent | Move -Wold-style-cast to gcc only list, clang triggers in some SDL headers (diff) | |
download | ilt-873ef1436e233b5b4c542f35838a7cd73432ede6.tar.bz2 ilt-873ef1436e233b5b4c542f35838a7cd73432ede6.tar.xz ilt-873ef1436e233b5b4c542f35838a7cd73432ede6.zip |
Tidy network node insertion/searching
Diffstat (limited to 'game')
-rw-r--r-- | game/network/network.cpp | 17 | ||||
-rw-r--r-- | game/network/network.h | 9 | ||||
-rw-r--r-- | game/network/rail.cpp | 10 |
3 files changed, 22 insertions, 14 deletions
diff --git a/game/network/network.cpp b/game/network/network.cpp index d78f672..ad501b5 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -19,14 +19,15 @@ Network::nodeAt(glm::vec3 pos) return newNodeAt(pos).first; } -std::pair<Node::Ptr, bool> +Network::NodeInsertion Network::newNodeAt(glm::vec3 pos) { - const auto [n, i] = candidateNodeAt(pos); - if (!i) { - nodes.insert(n); + if (const auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { + return {*nodes.insert(std::move(n)).first, i}; + } + else { + return {n, NodeIs::InNetwork}; } - return {n, !i}; } Node::Ptr @@ -38,13 +39,13 @@ Network::findNodeAt(glm::vec3 pos) const return {}; } -std::pair<Node::Ptr, bool> +Network::NodeInsertion Network::candidateNodeAt(glm::vec3 pos) const { if (const auto n = nodes.find(pos); n != nodes.end()) { - return {*n, true}; + return {*n, NodeIs::InNetwork}; } - return {std::make_shared<Node>(pos), false}; + return {std::make_shared<Node>(pos), NodeIs::NotInNetwork}; } Node::Ptr diff --git a/game/network/network.h b/game/network/network.h index b95dfdc..25f0200 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -23,8 +23,10 @@ public: [[nodiscard]] Node::Ptr findNodeAt(glm::vec3) const; [[nodiscard]] Node::Ptr nodeAt(glm::vec3); - [[nodiscard]] std::pair<Node::Ptr, bool> newNodeAt(glm::vec3); - [[nodiscard]] std::pair<Node::Ptr, bool> candidateNodeAt(glm::vec3) const; + enum class NodeIs { InNetwork, NotInNetwork }; + using NodeInsertion = std::pair<Node::Ptr, NodeIs>; + [[nodiscard]] NodeInsertion newNodeAt(glm::vec3); + [[nodiscard]] NodeInsertion candidateNodeAt(glm::vec3) const; [[nodiscard]] virtual Link::Ptr intersectRayLinks(const Ray &) const = 0; [[nodiscard]] virtual Node::Ptr intersectRayNodes(const Ray &) const; @@ -85,4 +87,7 @@ public: Link::CCollection addExtend(glm::vec3, glm::vec3) override; void render(const Shader &) const override; + +protected: + Link::CCollection addJoins(); }; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 635ee6e..95b6d24 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -30,11 +30,11 @@ std::shared_ptr<RailLink> RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); - if (node1ins.second && node2ins.second) { + if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) { // Both nodes are new, direct link, easy return addLink<RailLinkStraight>(start, end); } - if (node1ins.second && !node2ins.second) { + if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::InNetwork) { // node1 is new, node2 exists, but we build from existing outwards std::swap(node1ins, node2ins); std::swap(start, end); @@ -56,7 +56,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) return addLink<RailLinkStraight>(start, end); } const glm::vec2 flatStart {!start}, flatEnd {!end}; - if (!node2ins.second) { + if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.z + ((end.z - start.z) * (sm / (sm + em))); @@ -128,7 +128,9 @@ round_sleepers(const float v) return round_frac(v, sleepers); } -RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : RailLinkStraight(a, b, b->pos - a->pos) { } +RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : RailLinkStraight(a, b, b->pos - a->pos) +{ +} RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const glm::vec3 & diff) : Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) |