From 1cde77293e993c3a7329ea8e1fa013c8d7b898e5 Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Sun, 8 Aug 2010 17:51:43 +0200 Subject: Thank you, Marck00, for a patch that implemented region distance sorting for fallback regions. Applied with changes. --- OpenSim/Data/IRegionData.cs | 22 ++++++++++++++++++++++ OpenSim/Data/MSSQL/MSSQLRegionData.cs | 7 +++++-- OpenSim/Data/MySQL/MySQLRegionData.cs | 6 ++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/OpenSim/Data/IRegionData.cs b/OpenSim/Data/IRegionData.cs index 42533c6..46dc4fb 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 9656be1..cdf8ec0 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 aec37e2..878b8e8 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) -- cgit v1.1