From 7d502f167d553e5ddb549bc0412cac998c746570 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Sat, 18 Jun 2022 23:56:15 -0400 Subject: [PATCH] Core/Misc: Cleanup TaxiPathGraph Port From (https://github.com/TrinityCore/TrinityCore/commit/8fd05dbc931aa557177f666729b5654b2908b46e) --- Source/Game/Entities/Taxi/TaxiPathGraph.cs | 116 ++++++++++----------- Source/Game/Globals/Global.cs | 1 - Source/Game/Handlers/TaxiHandler.cs | 4 +- Source/Game/Server/WorldManager.cs | 2 +- 4 files changed, 60 insertions(+), 63 deletions(-) diff --git a/Source/Game/Entities/Taxi/TaxiPathGraph.cs b/Source/Game/Entities/Taxi/TaxiPathGraph.cs index c13e7e0bc..9043ea75d 100644 --- a/Source/Game/Entities/Taxi/TaxiPathGraph.cs +++ b/Source/Game/Entities/Taxi/TaxiPathGraph.cs @@ -25,59 +25,30 @@ using System.Numerics; namespace Game.Entities { - public class TaxiPathGraph : Singleton + public class TaxiPathGraph { - EdgeWeightedDigraph m_graph; - List m_nodesByVertex = new(); - Dictionary m_verticesByNode = new(); + static EdgeWeightedDigraph m_graph; + static List m_nodesByVertex = new(); + static Dictionary m_verticesByNode = new(); - TaxiPathGraph() { } - - public void Initialize() - { - if (m_graph != null) - return; - - List, uint>> edges = new(); - - // Initialize here - foreach (TaxiPathRecord path in CliDB.TaxiPathStorage.Values) - { - TaxiNodesRecord from = CliDB.TaxiNodesStorage.LookupByKey(path.FromTaxiNode); - TaxiNodesRecord to = CliDB.TaxiNodesStorage.LookupByKey(path.ToTaxiNode); - if (from != null && to != null && from.Flags.HasAnyFlag(TaxiNodeFlags.Alliance | TaxiNodeFlags.Horde) && to.Flags.HasAnyFlag(TaxiNodeFlags.Alliance | TaxiNodeFlags.Horde)) - AddVerticeAndEdgeFromNodeInfo(from, to, path.Id, edges); - } - - // create graph - m_graph = new EdgeWeightedDigraph(m_nodesByVertex.Count); - - for (int j = 0; j < edges.Count; ++j) - { - m_graph.AddEdge(new DirectedEdge(edges[j].Item1.Item1, edges[j].Item1.Item2, edges[j].Item2)); - } - } - - uint GetNodeIDFromVertexID(uint vertexID) - { - if (vertexID < m_nodesByVertex.Count) - return m_nodesByVertex[(int)vertexID].Id; - - return uint.MaxValue; - } - - uint GetVertexIDFromNodeID(TaxiNodesRecord node) - { - return m_verticesByNode.ContainsKey(node.Id) ? m_verticesByNode[node.Id] : uint.MaxValue; - } - - void GetTaxiMapPosition(Vector3 position, int mapId, out Vector2 uiMapPosition, out int uiMapId) + static void GetTaxiMapPosition(Vector3 position, int mapId, out Vector2 uiMapPosition, out int uiMapId) { if (!Global.DB2Mgr.GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UiMapSystem.Adventure, false, out uiMapId, out uiMapPosition)) Global.DB2Mgr.GetUiMapPosition(position.X, position.Y, position.Z, mapId, 0, 0, 0, UiMapSystem.Taxi, false, out uiMapId, out uiMapPosition); } - void AddVerticeAndEdgeFromNodeInfo(TaxiNodesRecord from, TaxiNodesRecord to, uint pathId, List, uint>> edges) + static uint CreateVertexFromFromNodeInfoIfNeeded(TaxiNodesRecord node) + { + if (!m_verticesByNode.ContainsKey(node.Id)) + { + m_verticesByNode.Add(node.Id, (uint)m_nodesByVertex.Count); + m_nodesByVertex.Add(node); + } + + return m_verticesByNode[node.Id]; + } + + static void AddVerticeAndEdgeFromNodeInfo(TaxiNodesRecord from, TaxiNodesRecord to, uint pathId, List, uint>> edges) { if (from.Id != to.Id) { @@ -125,7 +96,45 @@ namespace Game.Entities } } - public int GetCompleteNodeRoute(TaxiNodesRecord from, TaxiNodesRecord to, Player player, List shortestPath) + static uint GetVertexIDFromNodeID(TaxiNodesRecord node) + { + return m_verticesByNode.ContainsKey(node.Id) ? m_verticesByNode[node.Id] : uint.MaxValue; + } + + static uint GetNodeIDFromVertexID(uint vertexID) + { + if (vertexID < m_nodesByVertex.Count) + return m_nodesByVertex[(int)vertexID].Id; + + return uint.MaxValue; + } + + public static void Initialize() + { + if (m_graph != null) + return; + + List, uint>> edges = new(); + + // Initialize here + foreach (TaxiPathRecord path in CliDB.TaxiPathStorage.Values) + { + TaxiNodesRecord from = CliDB.TaxiNodesStorage.LookupByKey(path.FromTaxiNode); + TaxiNodesRecord to = CliDB.TaxiNodesStorage.LookupByKey(path.ToTaxiNode); + if (from != null && to != null && from.Flags.HasAnyFlag(TaxiNodeFlags.Alliance | TaxiNodeFlags.Horde) && to.Flags.HasAnyFlag(TaxiNodeFlags.Alliance | TaxiNodeFlags.Horde)) + AddVerticeAndEdgeFromNodeInfo(from, to, path.Id, edges); + } + + // create graph + m_graph = new EdgeWeightedDigraph(m_nodesByVertex.Count); + + for (int j = 0; j < edges.Count; ++j) + { + m_graph.AddEdge(new DirectedEdge(edges[j].Item1.Item1, edges[j].Item1.Item2, edges[j].Item2)); + } + } + + public static int GetCompleteNodeRoute(TaxiNodesRecord from, TaxiNodesRecord to, Player player, List shortestPath) { /* Information about node algorithm from client @@ -172,7 +181,7 @@ namespace Game.Entities } //todo test me - public void GetReachableNodesMask(TaxiNodesRecord from, byte[] mask) + public static void GetReachableNodesMask(TaxiNodesRecord from, byte[] mask) { DepthFirstSearch depthFirst = new(m_graph, GetVertexIDFromNodeID(from), vertex => { @@ -181,17 +190,6 @@ namespace Game.Entities mask[(taxiNode.Id - 1) / 8] |= (byte)(1 << (int)((taxiNode.Id - 1) % 8)); }); } - - uint CreateVertexFromFromNodeInfoIfNeeded(TaxiNodesRecord node) - { - if (!m_verticesByNode.ContainsKey(node.Id)) - { - m_verticesByNode.Add(node.Id, (uint)m_nodesByVertex.Count); - m_nodesByVertex.Add(node); - } - - return m_verticesByNode[node.Id]; - } } } diff --git a/Source/Game/Globals/Global.cs b/Source/Game/Globals/Global.cs index ad7c40863..472345ffe 100644 --- a/Source/Game/Globals/Global.cs +++ b/Source/Game/Globals/Global.cs @@ -73,7 +73,6 @@ public static class Global public static WaypointManager WaypointMgr { get { return WaypointManager.Instance; } } public static TransportManager TransportMgr { get { return TransportManager.Instance; } } public static InstanceSaveManager InstanceSaveMgr { get { return InstanceSaveManager.Instance; } } - public static TaxiPathGraph TaxiPathGraph { get { return TaxiPathGraph.Instance; } } public static ScenarioManager ScenarioMgr { get { return ScenarioManager.Instance; } } //PVP diff --git a/Source/Game/Handlers/TaxiHandler.cs b/Source/Game/Handlers/TaxiHandler.cs index b2962e555..42520e865 100644 --- a/Source/Game/Handlers/TaxiHandler.cs +++ b/Source/Game/Handlers/TaxiHandler.cs @@ -113,7 +113,7 @@ namespace Game GetPlayer().m_taxi.AppendTaximaskTo(data, lastTaxiCheaterState); byte[] reachableNodes = new byte[CliDB.TaxiNodesMask.Length]; - Global.TaxiPathGraph.GetReachableNodesMask(CliDB.TaxiNodesStorage.LookupByKey(curloc), reachableNodes); + TaxiPathGraph.GetReachableNodesMask(CliDB.TaxiNodesStorage.LookupByKey(curloc), reachableNodes); for (var i = 0; i < reachableNodes.Length; ++i) { data.CanLandNodes[i] &= reachableNodes[i]; @@ -221,7 +221,7 @@ namespace Game } List nodes = new(); - Global.TaxiPathGraph.GetCompleteNodeRoute(from, to, GetPlayer(), nodes); + TaxiPathGraph.GetCompleteNodeRoute(from, to, GetPlayer(), nodes); GetPlayer().ActivateTaxiPathTo(nodes, unit, 0, preferredMountDisplay); } diff --git a/Source/Game/Server/WorldManager.cs b/Source/Game/Server/WorldManager.cs index ef936851e..0caef01e2 100644 --- a/Source/Game/Server/WorldManager.cs +++ b/Source/Game/Server/WorldManager.cs @@ -443,7 +443,7 @@ namespace Game CliDB.LoadGameTables(_dataPath); //Load weighted graph on taxi nodes path - Global.TaxiPathGraph.Initialize(); + TaxiPathGraph.Initialize(); MultiMap mapData = new(); foreach (MapRecord mapEntry in CliDB.MapStorage.Values)