diff options
author | Shauren <shauren.trinity@gmail.com> | 2022-02-11 21:58:32 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2022-02-11 21:58:32 +0100 |
commit | 8fd05dbc931aa557177f666729b5654b2908b46e (patch) | |
tree | b1eab2a0b65a703a196056dba4f22f5252c5b9de | |
parent | d057e057d36f6052972b90eb1f4bd03263f4dab5 (diff) |
Core/Misc: Cleanup TaxiPathGraph includes (don't leak boost::graph outside)
-rw-r--r-- | src/server/game/Entities/Taxi/TaxiPathGraph.cpp | 195 | ||||
-rw-r--r-- | src/server/game/Entities/Taxi/TaxiPathGraph.h | 39 | ||||
-rw-r--r-- | src/server/game/Handlers/TaxiHandler.cpp | 4 | ||||
-rw-r--r-- | src/server/game/World/World.cpp | 2 |
4 files changed, 107 insertions, 133 deletions
diff --git a/src/server/game/Entities/Taxi/TaxiPathGraph.cpp b/src/server/game/Entities/Taxi/TaxiPathGraph.cpp index 75a17bbc5a2..39bdb65b2b2 100644 --- a/src/server/game/Entities/Taxi/TaxiPathGraph.cpp +++ b/src/server/game/Entities/Taxi/TaxiPathGraph.cpp @@ -16,69 +16,63 @@ */ #include "TaxiPathGraph.h" +#include "DB2Stores.h" #include "ObjectMgr.h" #include "Player.h" -#include "DB2Stores.h" -#include "Config.h" -#include "Util.h" +#include <boost/graph/adjacency_list.hpp> #include <boost/graph/depth_first_search.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> #include <boost/property_map/transform_value_property_map.hpp> -TaxiPathGraph& TaxiPathGraph::Instance() +namespace { - static TaxiPathGraph instance; - return instance; -} - -void TaxiPathGraph::Initialize() +struct EdgeCost { - if (boost::num_vertices(m_graph) > 0) - return; + TaxiNodesEntry const* To; + uint32 Distance; + uint32 EvaluateDistance(Player const* player) const + { + uint32 requireFlag = (player->GetTeam() == ALLIANCE) ? TAXI_NODE_FLAG_ALLIANCE : TAXI_NODE_FLAG_HORDE; + if (!(To->Flags & requireFlag)) + return std::numeric_limits<uint16>::max(); - std::vector<std::pair<edge, EdgeCost>> edges; + if (PlayerConditionEntry const* condition = sPlayerConditionStore.LookupEntry(To->ConditionID)) + if (!sConditionMgr->IsPlayerMeetingCondition(player, condition)) + return std::numeric_limits<uint16>::max(); - // Initialize here - for (TaxiPathEntry const* path : sTaxiPathStore) - { - TaxiNodesEntry const* from = sTaxiNodesStore.LookupEntry(path->FromTaxiNode); - TaxiNodesEntry const* to = sTaxiNodesStore.LookupEntry(path->ToTaxiNode); - if (from && to && from->Flags & (TAXI_NODE_FLAG_ALLIANCE | TAXI_NODE_FLAG_HORDE) && to->Flags & (TAXI_NODE_FLAG_ALLIANCE | TAXI_NODE_FLAG_HORDE)) - AddVerticeAndEdgeFromNodeInfo(from, to, path->ID, edges); + return Distance; } +}; - // create graph - m_graph = Graph(m_nodesByVertex.size()); - WeightMap weightmap = boost::get(boost::edge_weight, m_graph); +typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::property<boost::vertex_index_t, uint32>, boost::property<boost::edge_weight_t, EdgeCost>> Graph; +typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap; +typedef Graph::vertex_descriptor vertex_descriptor; +typedef Graph::edge_descriptor edge_descriptor; +typedef std::pair<vertex_descriptor, vertex_descriptor> edge; - for (std::size_t j = 0; j < edges.size(); ++j) - { - edge_descriptor e = boost::add_edge(edges[j].first.first, edges[j].first.second, m_graph).first; - weightmap[e] = edges[j].second; - } -} +Graph m_graph; +std::vector<TaxiNodesEntry const*> m_nodesByVertex; +std::unordered_map<uint32, vertex_descriptor> m_verticesByNode; -uint32 TaxiPathGraph::GetNodeIDFromVertexID(vertex_descriptor vertexID) +void GetTaxiMapPosition(DBCPosition3D const& position, int32 mapId, DBCPosition2D* uiMapPosition, int32* uiMapId) { - if (vertexID < m_nodesByVertex.size()) - return m_nodesByVertex[vertexID]->ID; - - return std::numeric_limits<uint32>::max(); + if (!DB2Manager::GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UI_MAP_SYSTEM_ADVENTURE, false, uiMapId, uiMapPosition)) + DB2Manager::GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UI_MAP_SYSTEM_TAXI, false, uiMapId, uiMapPosition); } -TaxiPathGraph::vertex_descriptor TaxiPathGraph::GetVertexIDFromNodeID(TaxiNodesEntry const* node) +vertex_descriptor CreateVertexFromFromNodeInfoIfNeeded(TaxiNodesEntry const* node) { auto itr = m_verticesByNode.find(node->ID); - return itr != m_verticesByNode.end() ? itr->second : std::numeric_limits<vertex_descriptor>::max(); -} + if (itr == m_verticesByNode.end()) + { + itr = m_verticesByNode.emplace(node->ID, m_nodesByVertex.size()).first; + m_nodesByVertex.push_back(node); + } -void GetTaxiMapPosition(DBCPosition3D const& position, int32 mapId, DBCPosition2D* uiMapPosition, int32* uiMapId) -{ - if (!DB2Manager::GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UI_MAP_SYSTEM_ADVENTURE, false, uiMapId, uiMapPosition)) - DB2Manager::GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UI_MAP_SYSTEM_TAXI, false, uiMapId, uiMapPosition); + return itr->second; } -void TaxiPathGraph::AddVerticeAndEdgeFromNodeInfo(TaxiNodesEntry const* from, TaxiNodesEntry const* to, uint32 pathId, std::vector<std::pair<edge, EdgeCost>>& edges) +void AddVerticeAndEdgeFromNodeInfo(TaxiNodesEntry const* from, TaxiNodesEntry const* to, uint32 pathId, std::vector<std::pair<edge, EdgeCost>>& edges) { if (from != to) { @@ -89,7 +83,7 @@ void TaxiPathGraph::AddVerticeAndEdgeFromNodeInfo(TaxiNodesEntry const* from, Ta TaxiPathNodeList const& nodes = sTaxiPathNodesByPath[pathId]; if (nodes.size() < 2) { - edges.push_back(std::make_pair(edge(fromVertexID, toVertexID), EdgeCost{ to, 0xFFFF })); + edges.emplace_back(edge(fromVertexID, toVertexID), EdgeCost{ to, 0xFFFF }); return; } @@ -124,7 +118,72 @@ void TaxiPathGraph::AddVerticeAndEdgeFromNodeInfo(TaxiNodesEntry const* from, Ta if (dist > 0xFFFF) dist = 0xFFFF; - edges.push_back(std::make_pair(edge(fromVertexID, toVertexID), EdgeCost{ to, dist })); + edges.emplace_back(edge(fromVertexID, toVertexID), EdgeCost{ to, dist }); + } +} + +vertex_descriptor GetVertexIDFromNodeID(TaxiNodesEntry const* node) +{ + auto itr = m_verticesByNode.find(node->ID); + return itr != m_verticesByNode.end() ? itr->second : std::numeric_limits<vertex_descriptor>::max(); +} + +uint32 GetNodeIDFromVertexID(vertex_descriptor vertexID) +{ + if (vertexID < m_nodesByVertex.size()) + return m_nodesByVertex[vertexID]->ID; + + return std::numeric_limits<uint32>::max(); +} + +template<typename T> +struct DiscoverVertexVisitor : public boost::base_visitor<DiscoverVertexVisitor<T>> +{ + using event_filter = boost::on_discover_vertex; + + DiscoverVertexVisitor(T&& func) : _func(std::forward<T>(func)) { } + + template <class Vertex, class Graph> + void operator()(Vertex v, Graph& /*g*/) + { + _func(v); + } + +private: + T _func; +}; + +template<typename T> +auto make_discover_vertex_dfs_visitor(T&& t) +{ + return boost::make_dfs_visitor(DiscoverVertexVisitor<T>(std::forward<T>(t))); +} +} + +void TaxiPathGraph::Initialize() +{ + if (boost::num_vertices(m_graph) > 0) + return; + + std::vector<std::pair<edge, EdgeCost>> edges; + + // Initialize here + for (TaxiPathEntry const* path : sTaxiPathStore) + { + TaxiNodesEntry const* from = sTaxiNodesStore.LookupEntry(path->FromTaxiNode); + TaxiNodesEntry const* to = sTaxiNodesStore.LookupEntry(path->ToTaxiNode); + if (from && to && from->Flags & (TAXI_NODE_FLAG_ALLIANCE | TAXI_NODE_FLAG_HORDE) && to->Flags & (TAXI_NODE_FLAG_ALLIANCE | TAXI_NODE_FLAG_HORDE)) + AddVerticeAndEdgeFromNodeInfo(from, to, path->ID, edges); + } + + // create graph + m_graph = Graph(m_nodesByVertex.size()); + WeightMap weightmap = boost::get(boost::edge_weight, m_graph); + + for (std::size_t j = 0; j < edges.size(); ++j) + { + edge_descriptor e = boost::add_edge(edges[j].first.first, edges[j].first.second, m_graph).first; + weightmap[e] = edges[j].second; } } @@ -177,61 +236,13 @@ std::size_t TaxiPathGraph::GetCompleteNodeRoute(TaxiNodesEntry const* from, Taxi return shortestPath.size(); } -template<typename T> -struct DiscoverVertexVisitor : public boost::base_visitor<DiscoverVertexVisitor<T>> -{ - using event_filter = boost::on_discover_vertex; - - DiscoverVertexVisitor(T&& func) : _func(std::forward<T>(func)) { } - - template <class Vertex, class Graph> - void operator()(Vertex v, Graph& /*g*/) - { - _func(v); - } - -private: - T _func; -}; - -template<typename T> -inline auto make_discover_vertex_dfs_visitor(T&& t) -{ - return boost::make_dfs_visitor(DiscoverVertexVisitor<T>(std::forward<T>(t))); -} - void TaxiPathGraph::GetReachableNodesMask(TaxiNodesEntry const* from, TaxiMask* mask) { boost::vector_property_map<boost::default_color_type> color(boost::num_vertices(m_graph)); std::fill(color.storage_begin(), color.storage_end(), boost::white_color); - boost::depth_first_visit(m_graph, GetVertexIDFromNodeID(from), make_discover_vertex_dfs_visitor([this, mask](vertex_descriptor vertex) + boost::depth_first_visit(m_graph, GetVertexIDFromNodeID(from), make_discover_vertex_dfs_visitor([mask](vertex_descriptor vertex) { if (TaxiNodesEntry const* taxiNode = sTaxiNodesStore.LookupEntry(GetNodeIDFromVertexID(vertex))) (*mask)[(taxiNode->ID - 1) / 8] |= 1 << ((taxiNode->ID - 1) % 8); }), color); } - -TaxiPathGraph::vertex_descriptor TaxiPathGraph::CreateVertexFromFromNodeInfoIfNeeded(TaxiNodesEntry const* node) -{ - auto itr = m_verticesByNode.find(node->ID); - if (itr == m_verticesByNode.end()) - { - itr = m_verticesByNode.emplace(node->ID, m_nodesByVertex.size()).first; - m_nodesByVertex.push_back(node); - } - - return itr->second; -} - -uint32 TaxiPathGraph::EdgeCost::EvaluateDistance(Player const* player) const -{ - uint32 requireFlag = (player->GetTeam() == ALLIANCE) ? TAXI_NODE_FLAG_ALLIANCE : TAXI_NODE_FLAG_HORDE; - if (!(To->Flags & requireFlag)) - return std::numeric_limits<uint16>::max(); - - if (PlayerConditionEntry const* condition = sPlayerConditionStore.LookupEntry(To->ConditionID)) - if (!sConditionMgr->IsPlayerMeetingCondition(player, condition)) - return std::numeric_limits<uint16>::max(); - - return Distance; -} diff --git a/src/server/game/Entities/Taxi/TaxiPathGraph.h b/src/server/game/Entities/Taxi/TaxiPathGraph.h index f228d0c37b5..a100ec2916d 100644 --- a/src/server/game/Entities/Taxi/TaxiPathGraph.h +++ b/src/server/game/Entities/Taxi/TaxiPathGraph.h @@ -18,54 +18,17 @@ #ifndef TAXIPATHGRAPH_HPP #define TAXIPATHGRAPH_HPP -#include "Position.h" -#include "Define.h" #include "DBCEnums.h" -#include <boost/graph/adjacency_list.hpp> -#include <unordered_map> #include <vector> class Player; struct TaxiNodesEntry; -class TC_GAME_API TaxiPathGraph +namespace TaxiPathGraph { -public: - static TaxiPathGraph& Instance(); - void Initialize(); std::size_t GetCompleteNodeRoute(TaxiNodesEntry const* from, TaxiNodesEntry const* to, Player const* player, std::vector<uint32>& shortestPath); void GetReachableNodesMask(TaxiNodesEntry const* from, TaxiMask* mask); - -private: - struct EdgeCost - { - TaxiNodesEntry const* To; - uint32 Distance; - uint32 EvaluateDistance(Player const* player) const; - }; - typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::property<boost::vertex_index_t, uint32>, boost::property<boost::edge_weight_t, EdgeCost>> Graph; - typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap; - typedef Graph::vertex_descriptor vertex_descriptor; - typedef Graph::edge_descriptor edge_descriptor; - typedef std::pair<vertex_descriptor, vertex_descriptor> edge; - - TaxiPathGraph() { } - ~TaxiPathGraph() { } - - void AddVerticeAndEdgeFromNodeInfo(TaxiNodesEntry const* from, TaxiNodesEntry const* to, uint32 pathId, std::vector<std::pair<edge, EdgeCost>>& edges); - vertex_descriptor GetVertexIDFromNodeID(TaxiNodesEntry const* node); - uint32 GetNodeIDFromVertexID(vertex_descriptor vertexID); - vertex_descriptor CreateVertexFromFromNodeInfoIfNeeded(TaxiNodesEntry const* node); - - Graph m_graph; - std::vector<TaxiNodesEntry const*> m_nodesByVertex; - std::unordered_map<uint32, vertex_descriptor> m_verticesByNode; - - TaxiPathGraph(TaxiPathGraph const&) = delete; - TaxiPathGraph& operator=(TaxiPathGraph const&) = delete; }; -#define sTaxiPathGraph TaxiPathGraph::Instance() - #endif /* TAXIPATHGRAPH_HPP */ diff --git a/src/server/game/Handlers/TaxiHandler.cpp b/src/server/game/Handlers/TaxiHandler.cpp index 45569e8ad2e..127fd5754c1 100644 --- a/src/server/game/Handlers/TaxiHandler.cpp +++ b/src/server/game/Handlers/TaxiHandler.cpp @@ -111,7 +111,7 @@ void WorldSession::SendTaxiMenu(Creature* unit) TaxiMask reachableNodes; std::fill(reachableNodes.begin(), reachableNodes.end(), 0); - sTaxiPathGraph.GetReachableNodesMask(sTaxiNodesStore.LookupEntry(curloc), &reachableNodes); + TaxiPathGraph::GetReachableNodesMask(sTaxiNodesStore.LookupEntry(curloc), &reachableNodes); for (std::size_t i = 0; i < TaxiMaskSize; ++i) { @@ -216,7 +216,7 @@ void WorldSession::HandleActivateTaxiOpcode(WorldPackets::Taxi::ActivateTaxi& ac } std::vector<uint32> nodes; - sTaxiPathGraph.GetCompleteNodeRoute(from, to, GetPlayer(), nodes); + TaxiPathGraph::GetCompleteNodeRoute(from, to, GetPlayer(), nodes); GetPlayer()->ActivateTaxiPathTo(nodes, unit, 0, preferredMountDisplay); } diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index bbc979e7403..e9edf05026c 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -1763,7 +1763,7 @@ void World::SetInitialWorldSettings() LoadGameTables(m_dataPath); //Load weighted graph on taxi nodes path - sTaxiPathGraph.Initialize(); + TaxiPathGraph::Initialize(); // Load IP Location Database sIPLocation->Load(); |