### 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}");
}
```
288 lines
9.4 KiB
C#
288 lines
9.4 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2025 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: Map.ClientEnumerator.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using Server.Collections;
|
|
using Server.Network;
|
|
|
|
namespace Server;
|
|
|
|
public partial class Map
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientAtEnumerable GetClientsAt(Point3D p) => GetClientsAt(new Point2D(p.X, p.Y));
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientAtEnumerable GetClientsAt(int x, int y) => GetClientsAt(new Point2D(x, y));
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientAtEnumerable GetClientsAt(Point2D p) => new(this, p);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInRange(Point3D p) => GetClientsInRange(p, Core.GlobalMaxUpdateRange);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInRange(Point3D p, int range) =>
|
|
GetClientsInRange(p.m_X, p.m_Y, range);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInRange(Point2D p) => GetClientsInRange(p, Core.GlobalMaxUpdateRange);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInRange(Point2D p, int range) =>
|
|
GetClientsInRange(p.m_X, p.m_Y, range);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInRange(int x, int y, int range)
|
|
{
|
|
var clampedRange = Math.Max(0, range);
|
|
var edge = clampedRange * 2 + 1;
|
|
return GetClientsInBounds(new Rectangle2D(x - clampedRange, y - clampedRange, edge, edge));
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerable GetClientsInBounds(Rectangle2D bounds, bool makeBoundsInclusive = false) =>
|
|
new(this, bounds, makeBoundsInclusive);
|
|
|
|
public ref struct ClientAtEnumerable
|
|
{
|
|
public static ClientAtEnumerable Empty
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => new();
|
|
}
|
|
|
|
private readonly Map _map;
|
|
private readonly Point2D _location;
|
|
|
|
public ClientAtEnumerable(Map map, Point2D loc)
|
|
{
|
|
_map = map;
|
|
_location = loc;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientAtEnumerator GetEnumerator() => new(_map, _location);
|
|
}
|
|
|
|
public ref struct ClientAtEnumerator
|
|
{
|
|
private readonly Map _map;
|
|
private bool _started;
|
|
private Point2D _location;
|
|
private ref readonly ValueLinkList<NetState> _linkList;
|
|
private int _version;
|
|
private NetState _current;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientAtEnumerator(Map map, Point2D loc)
|
|
{
|
|
_map = map;
|
|
_started = false;
|
|
_location = loc;
|
|
|
|
if (map != null)
|
|
{
|
|
_linkList = ref map.GetSector(loc.m_X, loc.m_Y).Clients;
|
|
}
|
|
|
|
_version = 0;
|
|
_current = null;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool MoveNext()
|
|
{
|
|
if (_map == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ref var loc = ref _location;
|
|
NetState current;
|
|
Mobile m;
|
|
|
|
if (!_started)
|
|
{
|
|
current = _linkList._first;
|
|
_started = true;
|
|
_version = _linkList.Version;
|
|
|
|
m = current?.Mobile;
|
|
if (m?.Deleted == false && m.X == loc.m_X && m.Y == loc.m_Y)
|
|
{
|
|
_current = current;
|
|
return true;
|
|
}
|
|
}
|
|
else if (_linkList.Version != _version)
|
|
{
|
|
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
|
}
|
|
else
|
|
{
|
|
current = _current;
|
|
}
|
|
|
|
while (current != null)
|
|
{
|
|
current = current.Next;
|
|
|
|
m = current?.Mobile;
|
|
if (m?.Deleted == false && m.X == loc.m_X && m.Y == loc.m_Y)
|
|
{
|
|
_current = current;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public NetState Current
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => _current;
|
|
}
|
|
}
|
|
|
|
public ref struct ClientBoundsEnumerable
|
|
{
|
|
public static ClientBoundsEnumerable Empty
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => new(null, Rectangle2D.Empty, false);
|
|
}
|
|
|
|
private readonly Map _map;
|
|
private readonly Rectangle2D _bounds;
|
|
private readonly bool _makeBoundsInclusive;
|
|
|
|
public ClientBoundsEnumerable(Map map, Rectangle2D bounds, bool makeBoundsInclusive)
|
|
{
|
|
_map = map;
|
|
_bounds = bounds;
|
|
_makeBoundsInclusive = makeBoundsInclusive;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerator GetEnumerator() => new(_map, _bounds, _makeBoundsInclusive);
|
|
}
|
|
|
|
public ref struct ClientBoundsEnumerator
|
|
{
|
|
private readonly Map _map;
|
|
private readonly int _sectorStartX;
|
|
private readonly int _sectorEndX;
|
|
private readonly int _sectorEndY;
|
|
private Rectangle2D _bounds;
|
|
|
|
private int _currentSectorX;
|
|
private int _currentSectorY;
|
|
|
|
private ref readonly ValueLinkList<NetState> _linkList;
|
|
private int _currentVersion;
|
|
private NetState _current;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ClientBoundsEnumerator(Map map, Rectangle2D bounds, bool makeBoundsInclusive)
|
|
{
|
|
_map = map;
|
|
_bounds = bounds;
|
|
|
|
if (makeBoundsInclusive)
|
|
{
|
|
++bounds.Width;
|
|
++bounds.Height;
|
|
}
|
|
|
|
_bounds = bounds;
|
|
|
|
if (map != null)
|
|
{
|
|
map.CalculateSectors(bounds, out _sectorStartX, out var _sectorStartY, out _sectorEndX, out _sectorEndY);
|
|
|
|
// We start the X sector one short because it gets incremented immediately in MoveNext()
|
|
_currentSectorX = _sectorStartX - 1;
|
|
_currentSectorY = _sectorStartY;
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool MoveNext()
|
|
{
|
|
var map = _map;
|
|
|
|
if (map == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!Unsafe.IsNullRef(in _linkList) && _linkList.Version != _currentVersion)
|
|
{
|
|
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
|
}
|
|
|
|
NetState current = _current;
|
|
ref Rectangle2D bounds = ref _bounds;
|
|
var currentSectorX = _currentSectorX;
|
|
var currentSectorY = _currentSectorY;
|
|
var sectorEndX = _sectorEndX;
|
|
var sectorEndY = _sectorEndY;
|
|
|
|
while (true)
|
|
{
|
|
current = current?.Next;
|
|
|
|
while (current == null)
|
|
{
|
|
// Move to next sector
|
|
if (currentSectorX < sectorEndX)
|
|
{
|
|
_currentSectorX = ++currentSectorX;
|
|
}
|
|
else if (currentSectorY < sectorEndY)
|
|
{
|
|
_currentSectorX = currentSectorX = _sectorStartX;
|
|
_currentSectorY = ++currentSectorY;
|
|
}
|
|
else
|
|
{
|
|
// Ran out of sectors
|
|
return false;
|
|
}
|
|
|
|
_linkList = ref map.GetRealSector(currentSectorX, currentSectorY).Clients;
|
|
_currentVersion = _linkList.Version;
|
|
current = _linkList._first;
|
|
}
|
|
|
|
var m = current.Mobile;
|
|
if (m?.Deleted == false && bounds.Contains(m.Location))
|
|
{
|
|
_current = current;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public NetState Current
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => _current;
|
|
}
|
|
}
|
|
}
|