fix: Fixes searching multis/clients. Adds missing map enumeration tests (#2278)
> [!IMPORTANT] > **Dev Note:** This is an **important** patch as the bug could lead to major issues like: > * Multis/Players disappearing from view or not being counted during game logic. > * World processes (e.g., area checks, targeting) failing to detect entities correctly. > * General stability and correctness concerns for core map functionality. > > **Important Breaking Change**: Multis now properly use the map link list. This means modifying a multi while iterating will cause the server to crash. The crash _is expected_. Please modify/fix code accordingly to create a list using `PooledRefQueue` or `PooledRefList` instead of moving/deleting multis while inside the foreach. ### Summary * Fixes a bug where deleting/moving a multi (boat/house) in some circumstances can use undefined behavior due to unsafe changes to List<BaseMulti> * Fixes a bug where Multis may not be considered while searching due to a bug causing the sector search to end early.
This commit is contained in:
parent
5aed018232
commit
d3fdb180b3
9 changed files with 1923 additions and 571 deletions
|
|
@ -1,503 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2023 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: BaseMulti.SectorMultiLinkList.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;
|
||||
|
||||
namespace Server.Items;
|
||||
|
||||
// Adds support for the specific value link list on sectors for multis, separate from items
|
||||
public partial class BaseMulti
|
||||
{
|
||||
// Sectors, specifically for multis
|
||||
public BaseMulti SectorMultiNext { get; set; }
|
||||
public BaseMulti SectorMultiPrevious { get; set; }
|
||||
public bool OnSectorMultiLinkList { get; set; }
|
||||
}
|
||||
|
||||
public struct SectorMultiValueLinkList
|
||||
{
|
||||
public int Count { get; internal set; }
|
||||
internal BaseMulti _first;
|
||||
internal BaseMulti _last;
|
||||
|
||||
public int Version { get; private set; }
|
||||
|
||||
public void Remove(BaseMulti node)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to remove a node that is not on the list.");
|
||||
}
|
||||
|
||||
if (node.SectorMultiPrevious == null)
|
||||
{
|
||||
// If SectorMultiPrevious is null, then it is the first element.
|
||||
if (_first != node)
|
||||
{
|
||||
throw new ArgumentException("Attempted to remove a node that is not on the list.");
|
||||
}
|
||||
|
||||
if (_first == _last)
|
||||
{
|
||||
_last = null;
|
||||
_first = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_first = node.SectorMultiNext;
|
||||
}
|
||||
|
||||
if (node.SectorMultiNext != null)
|
||||
{
|
||||
node.SectorMultiNext.SectorMultiPrevious = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
node.SectorMultiPrevious.SectorMultiNext = node.SectorMultiNext;
|
||||
|
||||
// If SectorMultiNext is null, then it is the last element.
|
||||
if (node.SectorMultiNext == null)
|
||||
{
|
||||
_last = node.SectorMultiPrevious;
|
||||
}
|
||||
else
|
||||
{
|
||||
node.SectorMultiNext.SectorMultiPrevious = node.SectorMultiPrevious;
|
||||
}
|
||||
}
|
||||
|
||||
node.SectorMultiNext = null;
|
||||
node.SectorMultiPrevious = null;
|
||||
node.OnSectorMultiLinkList = false;
|
||||
Count--;
|
||||
Version++;
|
||||
|
||||
if (Count < 0)
|
||||
{
|
||||
throw new Exception("Count is negative!");
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all entries before this node, not including this node.
|
||||
public void RemoveAllBefore(BaseMulti e)
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!e.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to remove nodes before a node that is not on the list.");
|
||||
}
|
||||
|
||||
if (e.SectorMultiPrevious == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var current = e.SectorMultiPrevious;
|
||||
e.SectorMultiPrevious = null;
|
||||
|
||||
while (current != null)
|
||||
{
|
||||
var SectorMultiPrevious = current.SectorMultiPrevious;
|
||||
|
||||
current.OnSectorMultiLinkList = false;
|
||||
current.SectorMultiNext = null;
|
||||
current.SectorMultiPrevious = null;
|
||||
Count--;
|
||||
|
||||
if (Count < 0)
|
||||
{
|
||||
throw new Exception("Count is negative!");
|
||||
}
|
||||
|
||||
current = SectorMultiPrevious;
|
||||
}
|
||||
|
||||
_first = e;
|
||||
Version++;
|
||||
}
|
||||
|
||||
// Remove all entries after this node, not including this node.
|
||||
public void RemoveAllAfter(BaseMulti e)
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!e.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to remove nodes after a node that is not on the list.");
|
||||
}
|
||||
|
||||
if (e.SectorMultiNext == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var current = e.SectorMultiNext;
|
||||
e.SectorMultiNext = null;
|
||||
|
||||
while (current != null)
|
||||
{
|
||||
var SectorMultiNext = current.SectorMultiNext;
|
||||
|
||||
current.OnSectorMultiLinkList = false;
|
||||
current.SectorMultiNext = null;
|
||||
current.SectorMultiPrevious = null;
|
||||
Count--;
|
||||
|
||||
if (Count < 0)
|
||||
{
|
||||
throw new Exception("Count is negative!");
|
||||
}
|
||||
|
||||
current = SectorMultiNext;
|
||||
}
|
||||
|
||||
_last = e;
|
||||
Version++;
|
||||
}
|
||||
|
||||
public void AddLast(BaseMulti e)
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to add a node that is already on a list.");
|
||||
}
|
||||
|
||||
if (_last != null)
|
||||
{
|
||||
AddAfter(_last, e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_first = e;
|
||||
_last = e;
|
||||
Count = 1;
|
||||
Version++;
|
||||
e.OnSectorMultiLinkList = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddFirst(BaseMulti e)
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to add a node that is already on a list.");
|
||||
}
|
||||
|
||||
if (_first != null)
|
||||
{
|
||||
AddBefore(_first, e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_first = e;
|
||||
_last = e;
|
||||
Count = 1;
|
||||
Version++;
|
||||
e.OnSectorMultiLinkList = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddBefore(BaseMulti existing, BaseMulti node)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(existing);
|
||||
|
||||
if (!existing.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException($"Argument '{nameof(existing)}' must be a node on a list.");
|
||||
}
|
||||
|
||||
if (node.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to add a node that is already on a list.");
|
||||
}
|
||||
|
||||
node.SectorMultiNext = existing;
|
||||
node.SectorMultiPrevious = existing.SectorMultiPrevious;
|
||||
|
||||
if (existing.SectorMultiPrevious != null)
|
||||
{
|
||||
existing.SectorMultiPrevious.SectorMultiNext = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
_first = node;
|
||||
}
|
||||
|
||||
existing.SectorMultiPrevious = node;
|
||||
node.OnSectorMultiLinkList = true;
|
||||
Count++;
|
||||
Version++;
|
||||
}
|
||||
|
||||
public void AddAfter(BaseMulti existing, BaseMulti node)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(existing);
|
||||
|
||||
if (!existing.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException($"Argument '{nameof(existing)}' must be a node on a list.");
|
||||
}
|
||||
|
||||
if (node.OnSectorMultiLinkList)
|
||||
{
|
||||
throw new ArgumentException("Attempted to add a node that is already on a list.");
|
||||
}
|
||||
|
||||
node.SectorMultiPrevious = existing;
|
||||
node.SectorMultiNext = existing.SectorMultiNext;
|
||||
|
||||
if (existing.SectorMultiNext != null)
|
||||
{
|
||||
existing.SectorMultiNext.SectorMultiPrevious = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
_last = node;
|
||||
}
|
||||
|
||||
existing.SectorMultiNext = node;
|
||||
node.OnSectorMultiLinkList = true;
|
||||
Count++;
|
||||
Version++;
|
||||
}
|
||||
|
||||
public void RemoveAll()
|
||||
{
|
||||
var current = _first;
|
||||
while (current != null)
|
||||
{
|
||||
var SectorMultiNext = current.SectorMultiNext;
|
||||
|
||||
current.OnSectorMultiLinkList = false;
|
||||
current.SectorMultiNext = null;
|
||||
current.SectorMultiPrevious = null;
|
||||
current = SectorMultiNext;
|
||||
}
|
||||
|
||||
_first = null;
|
||||
_last = null;
|
||||
Count = 0;
|
||||
Version++;
|
||||
}
|
||||
|
||||
public void AddLast(ref SectorMultiValueLinkList otherList, BaseMulti start, BaseMulti end)
|
||||
{
|
||||
// Should we check if start and end actually exist on the other list?
|
||||
if (otherList.Count == 0 || otherList.Count == 1 && (start != end || otherList._first != start))
|
||||
{
|
||||
throw new ArgumentException("Attempted to add nodes that are not on the specified linklist.");
|
||||
}
|
||||
|
||||
if (start.SectorMultiPrevious != null)
|
||||
{
|
||||
start.SectorMultiPrevious.SectorMultiNext = end.SectorMultiNext;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start is first
|
||||
otherList._first = end.SectorMultiNext;
|
||||
}
|
||||
|
||||
if (end.SectorMultiNext != null)
|
||||
{
|
||||
end.SectorMultiNext.SectorMultiPrevious = start.SectorMultiPrevious;
|
||||
}
|
||||
else
|
||||
{
|
||||
otherList._last = start.SectorMultiPrevious;
|
||||
}
|
||||
|
||||
var count = 1;
|
||||
var current = start;
|
||||
|
||||
// Assume start and end are in the right order, or bad things happen (crash).
|
||||
while (current != end)
|
||||
{
|
||||
count++;
|
||||
current = current.SectorMultiNext;
|
||||
}
|
||||
|
||||
otherList.Count -= count;
|
||||
|
||||
if (otherList.Count < 0)
|
||||
{
|
||||
throw new Exception("Count is negative!");
|
||||
}
|
||||
|
||||
if (_last != null)
|
||||
{
|
||||
_last.SectorMultiNext = start;
|
||||
start.SectorMultiPrevious = _last;
|
||||
}
|
||||
else
|
||||
{
|
||||
_first = start;
|
||||
}
|
||||
|
||||
_last = end;
|
||||
Count += count;
|
||||
Version++;
|
||||
}
|
||||
|
||||
public BaseMulti[] ToArray()
|
||||
{
|
||||
var arr = new BaseMulti[Count];
|
||||
|
||||
var index = 0;
|
||||
foreach (var t in this)
|
||||
{
|
||||
arr[index++] = t;
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
public ref struct SectorMultiValueListEnumerator
|
||||
{
|
||||
private bool _started;
|
||||
private BaseMulti _current;
|
||||
private ref readonly SectorMultiValueLinkList _linkList;
|
||||
private int _version;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public SectorMultiValueListEnumerator(in SectorMultiValueLinkList linkList)
|
||||
{
|
||||
_linkList = ref linkList;
|
||||
_started = false;
|
||||
_current = null;
|
||||
_version = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (!_started)
|
||||
{
|
||||
_current = _linkList._first;
|
||||
_started = true;
|
||||
_version = _linkList.Version;
|
||||
}
|
||||
else if (_linkList.Version != _version)
|
||||
{
|
||||
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
_current = _current.SectorMultiNext;
|
||||
}
|
||||
|
||||
return _current != null;
|
||||
}
|
||||
|
||||
public BaseMulti Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _current;
|
||||
}
|
||||
}
|
||||
|
||||
public ref struct DescendingSectorMultiValueListEnumerator
|
||||
{
|
||||
private bool _started;
|
||||
private BaseMulti _current;
|
||||
private ref readonly SectorMultiValueLinkList _linkList;
|
||||
private int _version;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public DescendingSectorMultiValueListEnumerator(in SectorMultiValueLinkList linkList)
|
||||
{
|
||||
_linkList = ref linkList;
|
||||
_started = false;
|
||||
_current = null;
|
||||
_version = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (!_started)
|
||||
{
|
||||
_current = _linkList._last;
|
||||
_started = true;
|
||||
_version = _linkList.Version;
|
||||
}
|
||||
else if (_linkList.Version != _version)
|
||||
{
|
||||
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
_current = _current.SectorMultiPrevious;
|
||||
}
|
||||
|
||||
return _current != null;
|
||||
}
|
||||
|
||||
public BaseMulti Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _current;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public DescendingSectorMultiValueListEnumerator GetEnumerator() => this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SectorMultiValueLinkListExt
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static SectorMultiValueLinkList.SectorMultiValueListEnumerator GetEnumerator(this in SectorMultiValueLinkList linkList)
|
||||
=> new(in linkList);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static SectorMultiValueLinkList.DescendingSectorMultiValueListEnumerator ByDescending(this in SectorMultiValueLinkList linkList)
|
||||
=> new(in linkList);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2023 - ModernUO Development Team *
|
||||
* Copyright 2019-2025 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Map.ClientEnumerator.cs *
|
||||
* *
|
||||
|
|
@ -76,6 +76,7 @@ public partial class Map
|
|||
|
||||
public ref struct ClientAtEnumerator
|
||||
{
|
||||
private readonly Map _map;
|
||||
private bool _started;
|
||||
private Point2D _location;
|
||||
private ref readonly ValueLinkList<NetState> _linkList;
|
||||
|
|
@ -85,9 +86,15 @@ public partial class Map
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ClientAtEnumerator(Map map, Point2D loc)
|
||||
{
|
||||
_map = map;
|
||||
_started = false;
|
||||
_location = loc;
|
||||
_linkList = ref map.GetSector(loc.m_X, loc.m_Y).Clients;
|
||||
|
||||
if (map != null)
|
||||
{
|
||||
_linkList = ref map.GetSector(loc.m_X, loc.m_Y).Clients;
|
||||
}
|
||||
|
||||
_version = 0;
|
||||
_current = null;
|
||||
}
|
||||
|
|
@ -95,6 +102,11 @@ public partial class Map
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ref var loc = ref _location;
|
||||
NetState current;
|
||||
Mobile m;
|
||||
|
|
@ -105,7 +117,7 @@ public partial class Map
|
|||
_started = true;
|
||||
_version = _linkList.Version;
|
||||
|
||||
m = current.Mobile;
|
||||
m = current?.Mobile;
|
||||
if (m?.Deleted == false && m.X == loc.m_X && m.Y == loc.m_Y)
|
||||
{
|
||||
_current = current;
|
||||
|
|
@ -125,7 +137,7 @@ public partial class Map
|
|||
{
|
||||
current = current.Next;
|
||||
|
||||
m = current.Mobile;
|
||||
m = current?.Mobile;
|
||||
if (m?.Deleted == false && m.X == loc.m_X && m.Y == loc.m_Y)
|
||||
{
|
||||
_current = current;
|
||||
|
|
@ -163,10 +175,10 @@ public partial class Map
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MobileEnumerator GetEnumerator() => new(_map, _bounds, _makeBoundsInclusive);
|
||||
public ClientBoundsEnumerator GetEnumerator() => new(_map, _bounds, _makeBoundsInclusive);
|
||||
}
|
||||
|
||||
public ref struct MobileEnumerator
|
||||
public ref struct ClientBoundsEnumerator
|
||||
{
|
||||
private readonly Map _map;
|
||||
private readonly int _sectorStartX;
|
||||
|
|
@ -182,7 +194,7 @@ public partial class Map
|
|||
private NetState _current;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MobileEnumerator(Map map, Rectangle2D bounds, bool makeBoundsInclusive)
|
||||
public ClientBoundsEnumerator(Map map, Rectangle2D bounds, bool makeBoundsInclusive)
|
||||
{
|
||||
_map = map;
|
||||
_bounds = bounds;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2023 - ModernUO Development Team *
|
||||
* Copyright 2019-2025 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Map.MultiEnumerator.cs *
|
||||
* *
|
||||
|
|
@ -17,15 +17,13 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Server.Collections;
|
||||
using Server.Items;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public partial class Map
|
||||
{
|
||||
private static SectorMultiValueLinkList _emptyMultiLinkList = new();
|
||||
public static ref readonly SectorMultiValueLinkList EmptyMultiLinkList => ref _emptyMultiLinkList;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MultiSectorEnumerable<BaseMulti> GetMultisInSector(Point3D p) => GetMultisInSector<BaseMulti>(p);
|
||||
|
||||
|
|
@ -83,6 +81,8 @@ public partial class Map
|
|||
public MultiBoundsEnumerable<T> GetMultisInBounds<T>(Rectangle2D bounds, bool makeBoundsInclusive = false) where T : BaseMulti =>
|
||||
new(this, bounds, makeBoundsInclusive);
|
||||
|
||||
private static readonly HashSet<Serial> _sharedDupes = [];
|
||||
|
||||
public ref struct MultiSectorEnumerable<T>(Map map, Point2D loc) where T : BaseMulti
|
||||
{
|
||||
public static MultiSectorEnumerable<T> Empty
|
||||
|
|
@ -98,27 +98,42 @@ public partial class Map
|
|||
public ref struct MultiSectorEnumerator<T> where T : BaseMulti
|
||||
{
|
||||
private readonly Span<BaseMulti> _list;
|
||||
private readonly int _version;
|
||||
private readonly Sector _sector;
|
||||
private int _index;
|
||||
private T _current;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MultiSectorEnumerator(Map map, Point2D loc)
|
||||
{
|
||||
_list = map == null
|
||||
? Span<BaseMulti>.Empty
|
||||
: CollectionsMarshal.AsSpan(map.GetSector(loc.m_X, loc.m_Y).Multis);
|
||||
if (map == null)
|
||||
{
|
||||
_list = Span<BaseMulti>.Empty;
|
||||
_sector = null;
|
||||
_version = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_sector = map.GetSector(loc.m_X, loc.m_Y);
|
||||
_list = CollectionsMarshal.AsSpan(_sector.Multis);
|
||||
_version = _sector.MultisVersion;
|
||||
}
|
||||
|
||||
_index = 0;
|
||||
_index = -1;
|
||||
_current = null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
while ((uint)_index < (uint)_list.Length)
|
||||
if (_sector != null && _version != _sector.MultisVersion)
|
||||
{
|
||||
var current = _list[_index++];
|
||||
if (current is T { Deleted: false } o)
|
||||
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
|
||||
while (++_index < _list.Length)
|
||||
{
|
||||
if (_list[_index] is T { Deleted: false } o)
|
||||
{
|
||||
_current = o;
|
||||
return true;
|
||||
|
|
@ -160,24 +175,27 @@ public partial class Map
|
|||
|
||||
public ref struct MultiBoundsEnumerator<T> where T : BaseMulti
|
||||
{
|
||||
private readonly Map _map;
|
||||
private readonly int _sectorStartX;
|
||||
private readonly int _sectorEndX;
|
||||
private readonly int _sectorEndY;
|
||||
private Map _map;
|
||||
private int _sectorStartX;
|
||||
private int _sectorEndX;
|
||||
private int _sectorEndY;
|
||||
private Rectangle2D _bounds;
|
||||
|
||||
private int _currentSectorX;
|
||||
private int _currentSectorY;
|
||||
|
||||
private Span<BaseMulti> _list;
|
||||
private Span<BaseMulti> _currentList;
|
||||
private int _currentIndex;
|
||||
private int _currentVersion;
|
||||
private Sector _currentSector;
|
||||
private T _current;
|
||||
private int _index;
|
||||
|
||||
private HashSet<Serial> _dupes;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MultiBoundsEnumerator(Map map, Rectangle2D bounds, bool makeBoundsInclusive)
|
||||
{
|
||||
_sharedDupes.Clear();
|
||||
|
||||
_map = map;
|
||||
_bounds = bounds;
|
||||
|
||||
|
|
@ -196,62 +214,78 @@ public partial class Map
|
|||
// We start the X sector one short because it gets incremented immediately in MoveNext()
|
||||
_currentSectorX = _sectorStartX - 1;
|
||||
_currentSectorY = _sectorStartY;
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
_currentList = default;
|
||||
_currentIndex = -1;
|
||||
_currentVersion = 0;
|
||||
_currentSector = null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private bool GetMulti()
|
||||
public bool MoveNext()
|
||||
{
|
||||
ref Rectangle2D bounds = ref _bounds;
|
||||
var map = _map;
|
||||
|
||||
while ((uint)_index < (uint)_list.Length)
|
||||
if (map == null)
|
||||
{
|
||||
var current = _list[_index++];
|
||||
_dupes ??= new HashSet<Serial>();
|
||||
|
||||
if (current is T { Deleted: false } o && bounds.Contains(o.Location) && !_dupes.Contains(o.Serial))
|
||||
{
|
||||
_dupes.Add(o.Serial);
|
||||
_current = o;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private bool GetSector()
|
||||
{
|
||||
ref Rectangle2D bounds = ref _bounds;
|
||||
var currentSectorX = _currentSectorX;
|
||||
var currentSectorY = _currentSectorY;
|
||||
var sectorEndX = _sectorEndX;
|
||||
var sectorEndY = _sectorEndY;
|
||||
|
||||
// Move to next sector
|
||||
if (currentSectorX < sectorEndX)
|
||||
while (true)
|
||||
{
|
||||
_currentSectorX = ++currentSectorX;
|
||||
}
|
||||
else if (currentSectorY < sectorEndY)
|
||||
{
|
||||
_currentSectorX = currentSectorX = _sectorStartX;
|
||||
_currentSectorY = ++currentSectorY;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ran out of sectors
|
||||
return false;
|
||||
}
|
||||
// Try to advance in the current list
|
||||
if (_currentList.Length > 0)
|
||||
{
|
||||
if (_currentVersion != _currentSector.MultisVersion)
|
||||
{
|
||||
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
|
||||
_list = CollectionsMarshal.AsSpan(_map.GetRealSector(currentSectorX, currentSectorY).Multis);
|
||||
return GetMulti();
|
||||
while (++_currentIndex < _currentList.Length)
|
||||
{
|
||||
var item = _currentList[_currentIndex];
|
||||
if (item is T { Deleted: false } o && bounds.Contains(o.Location))
|
||||
{
|
||||
// Multis can span multiple sectors, so we need to deduplicate
|
||||
if (_sharedDupes.Add(o.Serial))
|
||||
{
|
||||
_current = o;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
_currentSector = map.GetRealSector(currentSectorX, currentSectorY);
|
||||
_currentList = CollectionsMarshal.AsSpan(_currentSector.Multis);
|
||||
_currentVersion = _currentSector.MultisVersion;
|
||||
_currentIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => _map != null && (GetMulti() || GetSector());
|
||||
|
||||
public T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2023 - ModernUO Development Team *
|
||||
* Copyright 2019-2025 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Map.cs *
|
||||
* *
|
||||
|
|
@ -1355,11 +1355,13 @@ public sealed partial class Map : IComparable<Map>, ISpanFormattable, ISpanParsa
|
|||
{
|
||||
// TODO: Can we avoid this?
|
||||
private static readonly List<Region> m_DefaultRectList = new();
|
||||
private static readonly List<BaseMulti> m_DefaultMultiList = new();
|
||||
private bool m_Active;
|
||||
private ValueLinkList<NetState> _clients;
|
||||
private ValueLinkList<Item> _items;
|
||||
private ValueLinkList<Mobile> _mobiles;
|
||||
private List<BaseMulti> _multis = new();
|
||||
private List<BaseMulti> _multis;
|
||||
private int _multisVersion;
|
||||
private List<Region> _regions;
|
||||
|
||||
public Sector(int x, int y, Map owner)
|
||||
|
|
@ -1372,9 +1374,11 @@ public sealed partial class Map : IComparable<Map>, ISpanFormattable, ISpanParsa
|
|||
|
||||
public List<Region> Regions => _regions ?? m_DefaultRectList;
|
||||
|
||||
internal List<BaseMulti> Multis => _multis;
|
||||
internal List<BaseMulti> Multis => _multis ?? m_DefaultMultiList;
|
||||
|
||||
internal ref ValueLinkList<Mobile> Mobiles => ref _mobiles;
|
||||
internal int MultisVersion => _multisVersion;
|
||||
|
||||
internal ref readonly ValueLinkList<Mobile> Mobiles => ref _mobiles;
|
||||
|
||||
internal ref readonly ValueLinkList<Item> Items => ref _items;
|
||||
|
||||
|
|
@ -1503,12 +1507,17 @@ public sealed partial class Map : IComparable<Map>, ISpanFormattable, ISpanParsa
|
|||
|
||||
public void OnMultiEnter(BaseMulti multi)
|
||||
{
|
||||
_multis ??= new List<BaseMulti>();
|
||||
_multis.Add(multi);
|
||||
_multisVersion++;
|
||||
}
|
||||
|
||||
public void OnMultiLeave(BaseMulti multi)
|
||||
{
|
||||
_multis.Remove(multi);
|
||||
if (_multis?.Remove(multi) == true)
|
||||
{
|
||||
_multisVersion++;
|
||||
}
|
||||
}
|
||||
|
||||
public void Activate()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue