diff --git a/OpenSim/Data/IRegionData.cs b/OpenSim/Data/IRegionData.cs index 42533c6668..46dc4fbe8a 100644 --- a/OpenSim/Data/IRegionData.cs +++ b/OpenSim/Data/IRegionData.cs @@ -79,4 +79,26 @@ namespace OpenSim.Data Authenticate = 256, // Require authentication Hyperlink = 512 // Record represents a HG link } + + public class RegionDataDistanceCompare : IComparer + { + private Vector2 m_origin; + + public RegionDataDistanceCompare(int x, int y) + { + m_origin = new Vector2(x, y); + } + + public int Compare(RegionData regionA, RegionData regionB) + { + Vector2 vectorA = new Vector2(regionA.posX, regionA.posY); + Vector2 vectorB = new Vector2(regionB.posX, regionB.posY); + return Math.Sign(VectorDistance(m_origin, vectorA) - VectorDistance(m_origin, vectorB)); + } + + private float VectorDistance(Vector2 x, Vector2 y) + { + return (x - y).Length(); + } + } } diff --git a/OpenSim/Data/MSSQL/MSSQLRegionData.cs b/OpenSim/Data/MSSQL/MSSQLRegionData.cs index 9656be14a9..cdf8ec09fb 100644 --- a/OpenSim/Data/MSSQL/MSSQLRegionData.cs +++ b/OpenSim/Data/MSSQL/MSSQLRegionData.cs @@ -315,8 +315,11 @@ namespace OpenSim.Data.MSSQL public List GetFallbackRegions(UUID scopeID, int x, int y) { - // TODO: distance-sort results - return Get((int)RegionFlags.FallbackRegion, scopeID); + List regions = Get((int)RegionFlags.FallbackRegion, scopeID); + RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y); + regions.Sort(distanceComparer); + + return regions; } public List GetHyperlinks(UUID scopeID) diff --git a/OpenSim/Data/MySQL/MySQLRegionData.cs b/OpenSim/Data/MySQL/MySQLRegionData.cs index aec37e2bbd..878b8e8056 100644 --- a/OpenSim/Data/MySQL/MySQLRegionData.cs +++ b/OpenSim/Data/MySQL/MySQLRegionData.cs @@ -289,8 +289,10 @@ namespace OpenSim.Data.MySQL public List GetFallbackRegions(UUID scopeID, int x, int y) { - // TODO: distance-sort results - return Get((int)RegionFlags.FallbackRegion, scopeID); + List regions = Get((int)RegionFlags.FallbackRegion, scopeID); + RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y); + regions.Sort(distanceComparer); + return regions; } public List GetHyperlinks(UUID scopeID)