ModernUO/Projects/Server.Tests/Tests/Maps/MobileEnumeratorTests.cs
Kamron Batman f2ce860c18
feat: Adds Map.GetXByDistance (for tracking skill). Fixes negative range checks. (#2252)
### Summary

Adds `XInRangeByDistance` and `XInBoundsByDistance` methods to `Map.cs`:

**Item Distance Enumeration:**
```cs
ItemDistanceEnumerable<Item> GetItemsInRangeByDistance(Point3D p);
ItemDistanceEnumerable<Item> GetItemsInRangeByDistance(Point3D p, int range);
ItemDistanceEnumerable<T> GetItemsInRangeByDistance<T>(Point3D p) where T : Item;
ItemDistanceEnumerable<T> GetItemsInRangeByDistance<T>(Point3D p, int range) where T : Item;
ItemDistanceEnumerable<Item> GetItemsInRangeByDistance(Point2D p);
ItemDistanceEnumerable<Item> GetItemsInRangeByDistance(Point2D p, int range);
ItemDistanceEnumerable<T> GetItemsInRangeByDistance<T>(Point2D p) where T : Item;
ItemDistanceEnumerable<T> GetItemsInRangeByDistance<T>(Point2D p, int range) where T : Item;
ItemDistanceEnumerable<Item> GetItemsInRangeByDistance(int x, int y, int range);
ItemDistanceEnumerable<T> GetItemsInRangeByDistance<T>(int x, int y, int range) where T : Item;
ItemDistanceEnumerable<Item> GetItemsInBoundsByDistance(Rectangle2D bounds, , bool makeBoundsInclusive = false);
ItemDistanceEnumerable<T> GetItemsInBoundsByDistance<T>(Rectangle2D bounds, bool makeBoundsInclusive = false) where T : Item;
```

**Mobile Distance Enumeration:**
```cs
MobileDistanceEnumerable<Mobile> GetMobilesInRangeByDistance(Point3D p);
MobileDistanceEnumerable<Mobile> GetMobilesInRangeByDistance(Point3D p, int range);
MobileDistanceEnumerable<T> GetMobilesInRangeByDistance<T>(Point3D p) where T : Mobile;
MobileDistanceEnumerable<T> GetMobilesInRangeByDistance<T>(Point3D p, int range) where T : Mobile;
MobileDistanceEnumerable<Mobile> GetMobilesInRangeByDistance(Point2D p);
MobileDistanceEnumerable<Mobile> GetMobilesInRangeByDistance(Point2D p, int range);
MobileDistanceEnumerable<T> GetMobilesInRangeByDistance<T>(Point2D p) where T : Mobile;
MobileDistanceEnumerable<T> GetMobilesInRangeByDistance<T>(Point2D p, int range) where T : Mobile;
MobileDistanceEnumerable<Mobile> GetMobilesInRangeByDistance(int x, int y, int range);
MobileDistanceEnumerable<T> GetMobilesInRangeByDistance<T>(int x, int y, int range) where T : Mobile;
MobileDistanceEnumerable<Mobile> GetMobilesInBoundsByDistance(Rectangle2D bounds, bool makeBoundsInclusive = false);
MobileDistanceEnumerable<T> GetMobilesInBoundsByDistance<T>(Rectangle2D bounds, bool makeBoundsInclusive = false) where T : Mobile;
```

**Client Distance Enumeration:**
```cs
ClientDistanceEnumerable GetClientsInRangeByDistance(Point3D p);
ClientDistanceEnumerable GetClientsInRangeByDistance(Point3D p, int range);
ClientDistanceEnumerable GetClientsInRangeByDistance(Point2D p);
ClientDistanceEnumerable GetClientsInRangeByDistance(Point2D p, int range);
ClientDistanceEnumerable GetClientsInRangeByDistance(int x, int y, int range);
ClientDistanceEnumerable GetClientsInBoundsByDistance(Rectangle2D bounds, bool makeBoundsInclusive = false);
```

**Example Usage:**

How to use `minDistance` to terminate early when all subsequent mobiles in the iteration will be at an increasing min distance.

```csharp
var playerLocation = player.Location;
const int maxRange = 100;
const int maxMobiles = 12;

var closestMobiles = new SortedSet<Mobile>(Comparer<Mobile>.Create((x, y) =>
{
    var distX = x.GetDistanceToSqrt(playerLocation);
    var distY = y.GetDistanceToSqrt(playerLocation);

    int result = distX.CompareTo(distY);
    if (result == 0)
    {
        result = (x?.Serial ?? Serial.MinusOne).CompareTo(y?.Serial ?? Serial.MinusOne);
    }
    return result;
}));

int lastMinDistance = 0;

foreach (var (mobile, minDistance) in map.GetMobilesInRangeByDistance(playerLocation, maxRange))
{
    // Stop if we have enough and distance starts increasing
    if (closestMobiles.Count >= maxMobiles && minDistance > lastMinDistance)
    {
        break;
    }

    closestMobiles.Add(mobile);
    lastMinDistance = minDistance;
}

// Results are already ordered by proximity
foreach (var mobile in closestMobiles)
{
    var actualDistance = mobile.GetDistanceToSqrt(playerLocation);
    Console.WriteLine($"{mobile.Name}: ActualDist={actualDistance:F2}");
}
```
2025-11-28 10:57:54 -08:00

263 lines
7 KiB
C#

using System;
using System.Collections.Generic;
using Xunit;
namespace Server.Tests.Tests.Maps;
[Collection("Sequential Server Tests")]
public class MobileEnumeratorTests
{
[Fact]
public void MobileEnumerator_FiltersByBoundsAndOrder()
{
var map = Map.Felucca;
var rect = new Rectangle2D(100, 100, 32, 32);
var mobiles = new Mobile[3];
try
{
mobiles[0] = CreateMobile(map, new Point3D(105, 105, 0));
mobiles[1] = CreateMobile(map, new Point3D(130, 130, 0));
mobiles[2] = CreateMobile(map, new Point3D(90, 90, 0));
var found = new List<Mobile>();
foreach (var m in map.GetMobilesInBounds<Mobile>(rect))
{
found.Add(m);
}
Assert.Equal(2, found.Count);
Assert.All(found, m => Assert.True(rect.Contains(m.Location)));
Assert.Equal(new[] { mobiles[0], mobiles[1] }, found);
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_DeletedMobilesAreSkipped()
{
var map = Map.Felucca;
var rect = new Rectangle2D(200, 200, 16, 16);
var mobiles = new Mobile[3];
try
{
mobiles[0] = CreateMobile(map, new Point3D(205, 205, 0));
mobiles[1] = CreateMobile(map, new Point3D(206, 205, 0));
mobiles[2] = CreateMobile(map, new Point3D(207, 205, 0));
mobiles[1].Delete();
var found = new List<Mobile>();
foreach (var m in map.GetMobilesInBounds<Mobile>(rect))
{
found.Add(m);
}
Assert.Equal(new[] { mobiles[0], mobiles[2] }, found);
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_RespectsMakeBoundsInclusiveFlag()
{
var map = Map.Felucca;
var rect = new Rectangle2D(300, 300, 1, 1);
var mobiles = new Mobile[1];
try
{
mobiles[0] = CreateMobile(map, new Point3D(301, 301, 0));
var enumerator = map.GetMobilesInBounds<Mobile>(rect, makeBoundsInclusive: true).GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal(mobiles[0], enumerator.Current);
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_MapNullYieldsEmpty()
{
var enumerator = new Map.MobileEnumerator<Mobile>(null, Rectangle2D.Empty, false);
Assert.False(enumerator.MoveNext());
}
[Fact]
public void MobileEnumerator_ThrowsOnVersionChange()
{
var map = Map.Felucca;
var rect = new Rectangle2D(400, 400, 16, 16);
var mobiles = new[]
{
CreateMobile(map, new Point3D(405, 405, 0)),
CreateMobile(map, new Point3D(406, 405, 0))
};
try
{
var enumerator = map.GetMobilesInBounds<Mobile>(rect).GetEnumerator();
Assert.True(enumerator.MoveNext());
mobiles[1].Delete();
var exceptionThrown = false;
try
{
enumerator.MoveNext();
}
catch (InvalidOperationException)
{
exceptionThrown = true;
}
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_StepsAcrossSectors()
{
var map = Map.Felucca;
var rect = new Rectangle2D(500, 500, Map.SectorSize * 2, Map.SectorSize * 2);
var mobiles = new[]
{
CreateMobile(map, new Point3D(rect.X + 1, rect.Y + 1, 0)),
CreateMobile(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + 1, 0)),
CreateMobile(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + Map.SectorSize + 1, 0))
};
try
{
var result = new List<Mobile>();
foreach (var m in map.GetMobilesInBounds<Mobile>(rect))
{
result.Add(m);
}
Assert.Equal(mobiles, result);
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_MapBoundsAreClamped()
{
var map = Map.Felucca;
var width = map.Width;
var height = map.Height;
var rect = new Rectangle2D(width - Map.SectorSize - 2, height - Map.SectorSize - 2, Map.SectorSize * 2, Map.SectorSize * 2);
var mobiles = new[]
{
CreateMobile(map, new Point3D(width - 2, height - 2, 0))
};
try
{
var enumerator = map.GetMobilesInBounds<Mobile>(rect).GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal(mobiles[0], enumerator.Current);
Assert.False(enumerator.MoveNext());
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_ZeroRangeReturnsOnlyCenter()
{
var map = Map.Felucca;
var center = new Point3D(700, 700, 0);
const int range = 0;
var mobiles = new Mobile[2];
try
{
mobiles[0] = CreateMobile(map, center); // Exact center
mobiles[1] = CreateMobile(map, new Point3D(701, 700, 0)); // 1 tile away
var found = new List<Mobile>();
foreach (var mobile in map.GetMobilesInRange<Mobile>(center, range))
{
found.Add(mobile);
}
Assert.Single(found);
Assert.Equal(mobiles[0], found[0]);
}
finally
{
DeleteAll(mobiles);
}
}
[Fact]
public void MobileEnumerator_NegativeRangeCreates1x1Bounds()
{
var map = Map.Felucca;
var center = new Point3D(750, 750, 0);
const int range = -5;
var mobiles = new Mobile[2];
try
{
mobiles[0] = CreateMobile(map, center);
mobiles[1] = CreateMobile(map, new Point3D(751, 750, 0)); // 1 tile away
var found = new List<Mobile>();
foreach (var mobile in map.GetMobilesInRange<Mobile>(center, range))
{
found.Add(mobile);
}
// With negative range creating a 1x1 bounds, only exact center matches
Assert.Single(found);
Assert.Equal(mobiles[0], found[0]);
}
finally
{
DeleteAll(mobiles);
}
}
private static Mobile CreateMobile(Map map, Point3D location)
{
var mobile = new Mobile((Serial)Utility.RandomMinMax(0x100u, 0xFFFu));
mobile.DefaultMobileInit();
mobile.MoveToWorld(location, map);
return mobile;
}
private static void DeleteAll(Mobile[] mobiles)
{
for (var i = 0; i < mobiles.Length; i++)
{
mobiles[i]?.Delete();
}
}
}