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
492
Projects/Server.Tests/Tests/Maps/ClientEnumeratorTests.cs
Normal file
492
Projects/Server.Tests/Tests/Maps/ClientEnumeratorTests.cs
Normal file
|
|
@ -0,0 +1,492 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using Server.Accounting;
|
||||
using Server.Network;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Tests.Maps;
|
||||
|
||||
[Collection("Sequential Server Tests")]
|
||||
public class ClientEnumeratorTests
|
||||
{
|
||||
[Fact]
|
||||
public void ClientEnumerator_FiltersByBoundsAndOrder()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(100, 100, 32, 32);
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, new Point3D(105, 105, 0));
|
||||
clients[1] = CreateClientWithMobile(map, new Point3D(130, 130, 0));
|
||||
clients[2] = CreateClientWithMobile(map, new Point3D(90, 90, 0));
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsInBounds(rect))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.All(found, ns => Assert.True(rect.Contains(ns.Mobile.Location)));
|
||||
Assert.Equal(new[] { clients[0].Item1, clients[1].Item1 }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_SkipsNullMobiles()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(200, 200, 16, 16);
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, new Point3D(205, 205, 0));
|
||||
clients[1] = CreateClientWithMobile(map, new Point3D(206, 205, 0));
|
||||
clients[2] = CreateClientWithMobile(map, new Point3D(207, 205, 0));
|
||||
|
||||
// Remove the mobile from the second client
|
||||
clients[1].Item2.Delete();
|
||||
clients[1].Item1.Mobile = null;
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsInBounds(rect))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Equal(new[] { clients[0].Item1, clients[2].Item1 }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_RespectsMakeBoundsInclusiveFlag()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(300, 300, 1, 1);
|
||||
|
||||
var clients = new (NetState, Mobile)[1];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, new Point3D(301, 301, 0));
|
||||
|
||||
var enumerator = map.GetClientsInBounds(rect, makeBoundsInclusive: true).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(clients[0].Item1, enumerator.Current);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.ClientBoundsEnumerable(null, Rectangle2D.Empty, false).GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_ThrowsOnVersionChange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(400, 400, 16, 16);
|
||||
|
||||
var clients = new[]
|
||||
{
|
||||
CreateClientWithMobile(map, new Point3D(405, 405, 0)),
|
||||
CreateClientWithMobile(map, new Point3D(406, 405, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetClientsInBounds(rect).GetEnumerator();
|
||||
Assert.True(enumerator.MoveNext());
|
||||
|
||||
clients[1].Item2.Delete();
|
||||
|
||||
// Ref structs cannot be captured in lambdas, so we test the exception directly
|
||||
var exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
enumerator.MoveNext();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_StepsAcrossSectors()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(500, 500, Map.SectorSize * 2, Map.SectorSize * 2);
|
||||
|
||||
var clients = new[]
|
||||
{
|
||||
CreateClientWithMobile(map, new Point3D(rect.X + 1, rect.Y + 1, 0)),
|
||||
CreateClientWithMobile(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + 1, 0)),
|
||||
CreateClientWithMobile(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + Map.SectorSize + 1, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var result = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsInBounds(rect))
|
||||
{
|
||||
result.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { clients[0].Item1, clients[1].Item1, clients[2].Item1 }, result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_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 clients = new[]
|
||||
{
|
||||
CreateClientWithMobile(map, new Point3D(width - 2, height - 2, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetClientsInBounds(rect).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(clients[0].Item1, enumerator.Current);
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientAtEnumerator_FiltersExactLocation()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(600, 600, 0);
|
||||
var differentLocation = new Point3D(601, 600, 0);
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, location);
|
||||
clients[1] = CreateClientWithMobile(map, location);
|
||||
clients[2] = CreateClientWithMobile(map, differentLocation);
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsAt(location))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.All(found, ns =>
|
||||
{
|
||||
Assert.NotNull(ns.Mobile);
|
||||
Assert.Equal(location.X, ns.Mobile.X);
|
||||
Assert.Equal(location.Y, ns.Mobile.Y);
|
||||
});
|
||||
Assert.Contains(clients[0].Item1, found);
|
||||
Assert.Contains(clients[1].Item1, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientAtEnumerator_SkipsNullMobiles()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(650, 650, 0);
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, location);
|
||||
clients[1] = CreateClientWithMobile(map, location);
|
||||
clients[2] = CreateClientWithMobile(map, location);
|
||||
|
||||
// Remove the mobile from the second client
|
||||
clients[1].Item2.Delete();
|
||||
clients[1].Item1.Mobile = null;
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsAt(location))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Equal(new[] { clients[0].Item1, clients[2].Item1 }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientAtEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.ClientAtEnumerable(null, new Point2D(0, 0)).GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientAtEnumerator_ThrowsOnVersionChange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(750, 750, 0);
|
||||
|
||||
var clients = new[]
|
||||
{
|
||||
CreateClientWithMobile(map, location),
|
||||
CreateClientWithMobile(map, location)
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetClientsAt(location).GetEnumerator();
|
||||
Assert.True(enumerator.MoveNext());
|
||||
|
||||
clients[1].Item2.Delete();
|
||||
|
||||
// Ref structs cannot be captured in lambdas, so we test the exception directly
|
||||
var exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
enumerator.MoveNext();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientAtEnumerator_UsesDifferentPoint3DOverloads()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(800, 800, 5);
|
||||
|
||||
var clients = new (NetState, Mobile)[1];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, location);
|
||||
|
||||
// Test Point3D overload
|
||||
var found1 = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsAt(location))
|
||||
{
|
||||
found1.Add(ns);
|
||||
}
|
||||
|
||||
// Test (int, int) overload - should find the same client (Z is ignored)
|
||||
var found2 = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsAt(location.X, location.Y))
|
||||
{
|
||||
found2.Add(ns);
|
||||
}
|
||||
|
||||
// Test Point2D overload
|
||||
var found3 = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsAt(new Point2D(location.X, location.Y)))
|
||||
{
|
||||
found3.Add(ns);
|
||||
}
|
||||
|
||||
Assert.Single(found1);
|
||||
Assert.Equal(clients[0].Item1, found1[0]);
|
||||
Assert.Equal(found1, found2);
|
||||
Assert.Equal(found1, found3);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_GetClientsInRange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var center = new Point3D(900, 900, 0);
|
||||
var range = 5;
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, new Point3D(902, 902, 0)); // Within range
|
||||
clients[1] = CreateClientWithMobile(map, new Point3D(898, 898, 0)); // Within range
|
||||
clients[2] = CreateClientWithMobile(map, new Point3D(910, 910, 0)); // Outside range (906+ is outside)
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsInRange(center, range))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
// GetClientsInRange uses a bounding rectangle, not circular distance
|
||||
// Range of 5 means rectangle from (895, 895) to (905, 905)
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(clients[0].Item1, found);
|
||||
Assert.Contains(clients[1].Item1, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClientEnumerator_DeletedMobilesAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(1000, 1000, 16, 16);
|
||||
|
||||
var clients = new (NetState, Mobile)[3];
|
||||
try
|
||||
{
|
||||
clients[0] = CreateClientWithMobile(map, new Point3D(1005, 1005, 0));
|
||||
clients[1] = CreateClientWithMobile(map, new Point3D(1006, 1005, 0));
|
||||
clients[2] = CreateClientWithMobile(map, new Point3D(1007, 1005, 0));
|
||||
|
||||
// Delete the mobile (but not the NetState)
|
||||
clients[1].Item2.Delete();
|
||||
|
||||
var found = new List<NetState>();
|
||||
foreach (var ns in map.GetClientsInBounds(rect))
|
||||
{
|
||||
found.Add(ns);
|
||||
}
|
||||
|
||||
// Should skip the client whose mobile was deleted
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(clients[0].Item1, found);
|
||||
Assert.Contains(clients[2].Item1, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(clients);
|
||||
}
|
||||
}
|
||||
|
||||
private class MockAccount : IAccount
|
||||
{
|
||||
public int TotalGold { get; }
|
||||
public int TotalPlat { get; }
|
||||
public bool DepositGold(int amount) => throw new NotImplementedException();
|
||||
public bool DepositPlat(int amount) => throw new NotImplementedException();
|
||||
public bool WithdrawGold(int amount) => throw new NotImplementedException();
|
||||
public bool WithdrawPlat(int amount) => throw new NotImplementedException();
|
||||
public long GetTotalGold() => throw new NotImplementedException();
|
||||
public int CompareTo(IAccount other) => throw new NotImplementedException();
|
||||
public string Username { get; }
|
||||
public string Email { get; set; }
|
||||
public AccessLevel AccessLevel { get; set; }
|
||||
public int Length { get; }
|
||||
public int Limit { get; set; } = 6; // Default to 6 character slots
|
||||
public int Count { get; }
|
||||
|
||||
private readonly Dictionary<int, Mobile> _mobiles = new();
|
||||
public Mobile this[int index]
|
||||
{
|
||||
get => _mobiles.GetValueOrDefault(index);
|
||||
set => _mobiles[index] = value;
|
||||
}
|
||||
|
||||
public DateTime Created { get; set; }
|
||||
public Serial Serial { get; }
|
||||
public void Deserialize(IGenericReader reader) => throw new NotImplementedException();
|
||||
public byte SerializedThread { get; set; }
|
||||
public int SerializedPosition { get; set; }
|
||||
public int SerializedLength { get; set; }
|
||||
public void Serialize(IGenericWriter writer) => throw new NotImplementedException();
|
||||
public bool Deleted { get; }
|
||||
public void Delete() => throw new NotImplementedException();
|
||||
public bool TrySetUsername(string username) => throw new NotImplementedException();
|
||||
public void SetPassword(string password) => throw new NotImplementedException();
|
||||
public bool CheckPassword(string password) => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private static (NetState, Mobile) CreateClientWithMobile(Map map, Point3D location)
|
||||
{
|
||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
var ns = new NetState(socket);
|
||||
|
||||
// Assign a mock account to avoid null reference issues
|
||||
ns.Account = new MockAccount();
|
||||
|
||||
// Use a unique serial for each mobile
|
||||
var serial = World.NewMobile;
|
||||
var mobile = new Mobile(serial);
|
||||
mobile.DefaultMobileInit();
|
||||
|
||||
// Set the NetState on the mobile BEFORE moving it to the world
|
||||
// so the sector's client list gets updated properly
|
||||
ns.Mobile = mobile;
|
||||
mobile.NetState = ns;
|
||||
|
||||
mobile.MoveToWorld(location, map);
|
||||
return (ns, mobile);
|
||||
}
|
||||
|
||||
private static void DeleteAll((NetState, Mobile)[] clients)
|
||||
{
|
||||
for (var i = 0; i < clients.Length; i++)
|
||||
{
|
||||
if (clients[i].Item1 != null)
|
||||
{
|
||||
clients[i].Item1.Mobile = null;
|
||||
clients[i].Item1.Disconnect("Test cleanup");
|
||||
}
|
||||
clients[i].Item2?.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
427
Projects/Server.Tests/Tests/Maps/ItemEnumeratorTests.cs
Normal file
427
Projects/Server.Tests/Tests/Maps/ItemEnumeratorTests.cs
Normal file
|
|
@ -0,0 +1,427 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Server.Items;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Tests.Maps;
|
||||
|
||||
[Collection("Sequential Server Tests")]
|
||||
public class ItemEnumeratorTests
|
||||
{
|
||||
[Fact]
|
||||
public void ItemEnumerator_FiltersByBoundsAndOrder()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(100, 100, 32, 32);
|
||||
|
||||
var items = new Item[3];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, new Point3D(105, 105, 0));
|
||||
items[1] = CreateItem(map, new Point3D(130, 130, 0));
|
||||
items[2] = CreateItem(map, new Point3D(90, 90, 0));
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsInBounds<Item>(rect))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.All(found, item => Assert.True(rect.Contains(item.Location)));
|
||||
Assert.Equal(new[] { items[0], items[1] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_DeletedItemsAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(200, 200, 16, 16);
|
||||
|
||||
var items = new Item[3];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, new Point3D(205, 205, 0));
|
||||
items[1] = CreateItem(map, new Point3D(206, 205, 0));
|
||||
items[2] = CreateItem(map, new Point3D(207, 205, 0));
|
||||
|
||||
items[1].Delete();
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsInBounds<Item>(rect))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { items[0], items[2] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_ItemsWithParentAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(250, 250, 16, 16);
|
||||
|
||||
var items = new Item[2];
|
||||
var container = new Container(0xE75);
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, new Point3D(255, 255, 0));
|
||||
items[1] = CreateItem(map, new Point3D(256, 255, 0));
|
||||
container.MoveToWorld(new Point3D(255, 255, 0), map);
|
||||
|
||||
// Move items[1] into the container - it should be skipped
|
||||
items[1].Parent = container;
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsInBounds<Item>(rect))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
// Should only find items[0] and container, not items[1] (which has a parent)
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(items[0], found);
|
||||
Assert.Contains(container, found);
|
||||
Assert.DoesNotContain(items[1], found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
container?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_RespectsMakeBoundsInclusiveFlag()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(300, 300, 1, 1);
|
||||
|
||||
var items = new Item[1];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, new Point3D(301, 301, 0));
|
||||
|
||||
var enumerator = map.GetItemsInBounds<Item>(rect, makeBoundsInclusive: true).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(items[0], enumerator.Current);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.ItemEnumerator<Item>(null, Rectangle2D.Empty, false);
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_ThrowsOnVersionChange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(400, 400, 16, 16);
|
||||
|
||||
var items = new[]
|
||||
{
|
||||
CreateItem(map, new Point3D(405, 405, 0)),
|
||||
CreateItem(map, new Point3D(406, 405, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetItemsInBounds<Item>(rect).GetEnumerator();
|
||||
Assert.True(enumerator.MoveNext());
|
||||
|
||||
items[1].Delete();
|
||||
|
||||
// Ref structs cannot be captured in lambdas, so we test the exception directly
|
||||
var exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
enumerator.MoveNext();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_StepsAcrossSectors()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(500, 500, Map.SectorSize * 2, Map.SectorSize * 2);
|
||||
|
||||
var items = new[]
|
||||
{
|
||||
CreateItem(map, new Point3D(rect.X + 1, rect.Y + 1, 0)),
|
||||
CreateItem(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + 1, 0)),
|
||||
CreateItem(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + Map.SectorSize + 1, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var result = new List<Item>();
|
||||
foreach (var item in map.GetItemsInBounds<Item>(rect))
|
||||
{
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(items, result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemEnumerator_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 items = new[]
|
||||
{
|
||||
CreateItem(map, new Point3D(width - 2, height - 2, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetItemsInBounds<Item>(rect).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(items[0], enumerator.Current);
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_FiltersExactLocation()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(600, 600, 0);
|
||||
|
||||
var items = new Item[3];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, location);
|
||||
items[1] = CreateItem(map, location);
|
||||
items[2] = CreateItem(map, new Point3D(601, 600, 0)); // Different location
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt<Item>(location))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(items[0], found);
|
||||
Assert.Contains(items[1], found);
|
||||
Assert.DoesNotContain(items[2], found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_DeletedItemsAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(650, 650, 0);
|
||||
|
||||
var items = new Item[3];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, location);
|
||||
items[1] = CreateItem(map, location);
|
||||
items[2] = CreateItem(map, location);
|
||||
|
||||
items[1].Delete();
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt<Item>(location))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { items[0], items[2] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_ItemsWithParentAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(700, 700, 0);
|
||||
|
||||
var items = new Item[2];
|
||||
var container = new Container(0xE75);
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, location);
|
||||
items[1] = CreateItem(map, location);
|
||||
container.MoveToWorld(location, map);
|
||||
|
||||
// Move items[1] into the container - it should be skipped
|
||||
items[1].Parent = container;
|
||||
|
||||
var found = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt<Item>(location))
|
||||
{
|
||||
found.Add(item);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(items[0], found);
|
||||
Assert.Contains(container, found);
|
||||
Assert.DoesNotContain(items[1], found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
container?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.ItemAtEnumerator<Item>(null, new Point2D(0, 0));
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_ThrowsOnVersionChange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(750, 750, 0);
|
||||
|
||||
var items = new[]
|
||||
{
|
||||
CreateItem(map, location),
|
||||
CreateItem(map, location)
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetItemsAt<Item>(location).GetEnumerator();
|
||||
Assert.True(enumerator.MoveNext());
|
||||
|
||||
items[1].Delete();
|
||||
|
||||
// Ref structs cannot be captured in lambdas, so we test the exception directly
|
||||
var exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
enumerator.MoveNext();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItemAtEnumerator_UsesDifferentPoint3DOverloads()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(800, 800, 5);
|
||||
|
||||
var items = new Item[1];
|
||||
try
|
||||
{
|
||||
items[0] = CreateItem(map, location);
|
||||
|
||||
// Test Point3D overload
|
||||
var found1 = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt(location))
|
||||
{
|
||||
found1.Add(item);
|
||||
}
|
||||
|
||||
// Test (int, int) overload - should find the same item (Z is ignored)
|
||||
var found2 = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt(location.X, location.Y))
|
||||
{
|
||||
found2.Add(item);
|
||||
}
|
||||
|
||||
// Test Point2D overload
|
||||
var found3 = new List<Item>();
|
||||
foreach (var item in map.GetItemsAt(new Point2D(location.X, location.Y)))
|
||||
{
|
||||
found3.Add(item);
|
||||
}
|
||||
|
||||
Assert.Single(found1);
|
||||
Assert.Equal(items[0], found1[0]);
|
||||
Assert.Equal(found1, found2);
|
||||
Assert.Equal(found1, found3);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(items);
|
||||
}
|
||||
}
|
||||
|
||||
private static Item CreateItem(Map map, Point3D location)
|
||||
{
|
||||
var item = new Item(0x1);
|
||||
item.Movable = false;
|
||||
item.MoveToWorld(location, map);
|
||||
return item;
|
||||
}
|
||||
|
||||
private static void DeleteAll(Item[] items)
|
||||
{
|
||||
for (var i = 0; i < items.Length; i++)
|
||||
{
|
||||
items[i]?.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
206
Projects/Server.Tests/Tests/Maps/MobileEnumeratorTests.cs
Normal file
206
Projects/Server.Tests/Tests/Maps/MobileEnumeratorTests.cs
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
356
Projects/Server.Tests/Tests/Maps/MultiEnumeratorTests.cs
Normal file
356
Projects/Server.Tests/Tests/Maps/MultiEnumeratorTests.cs
Normal file
|
|
@ -0,0 +1,356 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Server.Items;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Tests.Maps;
|
||||
|
||||
[Collection("Sequential Server Tests")]
|
||||
public class MultiEnumeratorTests
|
||||
{
|
||||
[Fact]
|
||||
public void MultiEnumerator_FiltersByBoundsAndOrder()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(100, 100, 32, 32);
|
||||
|
||||
var multis = new TestMulti[3];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, new Point3D(105, 105, 0));
|
||||
multis[1] = CreateMulti(map, new Point3D(130, 130, 0));
|
||||
multis[2] = CreateMulti(map, new Point3D(90, 90, 0));
|
||||
|
||||
var found = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInBounds<BaseMulti>(rect))
|
||||
{
|
||||
found.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.All(found, multi => Assert.True(rect.Contains(multi.Location)));
|
||||
Assert.Equal(new[] { multis[0], multis[1] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_DeletedMultisAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(200, 200, 16, 16);
|
||||
|
||||
var multis = new TestMulti[3];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, new Point3D(205, 205, 0));
|
||||
multis[1] = CreateMulti(map, new Point3D(206, 205, 0));
|
||||
multis[2] = CreateMulti(map, new Point3D(207, 205, 0));
|
||||
|
||||
multis[1].Delete();
|
||||
|
||||
var found = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInBounds<BaseMulti>(rect))
|
||||
{
|
||||
found.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { multis[0], multis[2] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_RespectsMakeBoundsInclusiveFlag()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(300, 300, 1, 1);
|
||||
|
||||
var multis = new TestMulti[1];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, new Point3D(301, 301, 0));
|
||||
|
||||
var enumerator = map.GetMultisInBounds<BaseMulti>(rect, makeBoundsInclusive: true).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(multis[0], enumerator.Current);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.MultiBoundsEnumerable<BaseMulti>(null, Rectangle2D.Empty, false).GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_ThrowsOnVersionChange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(400, 400, 16, 16);
|
||||
|
||||
var multis = new[]
|
||||
{
|
||||
CreateMulti(map, new Point3D(405, 405, 0)),
|
||||
CreateMulti(map, new Point3D(406, 405, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetMultisInBounds<BaseMulti>(rect).GetEnumerator();
|
||||
Assert.True(enumerator.MoveNext());
|
||||
|
||||
multis[1].Delete();
|
||||
|
||||
// Ref structs cannot be captured in lambdas, so we test the exception directly
|
||||
var exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
enumerator.MoveNext();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Assert.IsType<InvalidOperationException>(e);
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
Assert.True(exceptionThrown, "Expected InvalidOperationException when collection version changes");
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_StepsAcrossSectors()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var rect = new Rectangle2D(500, 500, Map.SectorSize * 2, Map.SectorSize * 2);
|
||||
|
||||
var multis = new[]
|
||||
{
|
||||
CreateMulti(map, new Point3D(rect.X + 1, rect.Y + 1, 0)),
|
||||
CreateMulti(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + 1, 0)),
|
||||
CreateMulti(map, new Point3D(rect.X + Map.SectorSize + 1, rect.Y + Map.SectorSize + 1, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var result = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInBounds<BaseMulti>(rect))
|
||||
{
|
||||
result.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Equal(multis, result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_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 multis = new[]
|
||||
{
|
||||
CreateMulti(map, new Point3D(width - 2, height - 2, 0))
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var enumerator = map.GetMultisInBounds<BaseMulti>(rect).GetEnumerator();
|
||||
|
||||
Assert.True(enumerator.MoveNext());
|
||||
Assert.Equal(multis[0], enumerator.Current);
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiEnumerator_GetMultisInRange()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var center = new Point3D(600, 600, 0);
|
||||
var range = 5;
|
||||
|
||||
var multis = new TestMulti[3];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, new Point3D(602, 602, 0)); // Within range
|
||||
multis[1] = CreateMulti(map, new Point3D(598, 598, 0)); // Within range
|
||||
multis[2] = CreateMulti(map, new Point3D(610, 610, 0)); // Outside range
|
||||
|
||||
var found = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInRange<BaseMulti>(center, range))
|
||||
{
|
||||
found.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(multis[0], found);
|
||||
Assert.Contains(multis[1], found);
|
||||
Assert.DoesNotContain(multis[2], found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiSectorEnumerator_FiltersToSingleSector()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(700, 700, 0);
|
||||
|
||||
var multis = new TestMulti[2];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, location);
|
||||
multis[1] = CreateMulti(map, new Point3D(location.X + 1, location.Y, 0));
|
||||
|
||||
var found = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInSector<BaseMulti>(location))
|
||||
{
|
||||
found.Add(multi);
|
||||
}
|
||||
|
||||
// Both should be in the same sector
|
||||
Assert.Equal(2, found.Count);
|
||||
Assert.Contains(multis[0], found);
|
||||
Assert.Contains(multis[1], found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiSectorEnumerator_DeletedMultisAreSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(750, 750, 0);
|
||||
|
||||
var multis = new TestMulti[3];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, location);
|
||||
multis[1] = CreateMulti(map, new Point3D(location.X + 1, location.Y, 0));
|
||||
multis[2] = CreateMulti(map, new Point3D(location.X + 2, location.Y, 0));
|
||||
|
||||
multis[1].Delete();
|
||||
|
||||
var found = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInRange<BaseMulti>(location, 10))
|
||||
{
|
||||
found.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Equal(new[] { multis[0], multis[2] }, found);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiSectorEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.MultiSectorEnumerable<BaseMulti>(null, new Point2D(0, 0)).GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultiSectorEnumerator_UsesDifferentPointOverloads()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point3D(800, 800, 5);
|
||||
|
||||
var multis = new TestMulti[1];
|
||||
try
|
||||
{
|
||||
multis[0] = CreateMulti(map, location);
|
||||
|
||||
// Test Point3D overload
|
||||
var found1 = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInSector(location))
|
||||
{
|
||||
found1.Add(multi);
|
||||
}
|
||||
|
||||
// Test (int, int) overload
|
||||
var found2 = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInSector(location.X, location.Y))
|
||||
{
|
||||
found2.Add(multi);
|
||||
}
|
||||
|
||||
// Test Point2D overload
|
||||
var found3 = new List<BaseMulti>();
|
||||
foreach (var multi in map.GetMultisInSector(new Point2D(location.X, location.Y)))
|
||||
{
|
||||
found3.Add(multi);
|
||||
}
|
||||
|
||||
Assert.Single(found1);
|
||||
Assert.Equal(multis[0], found1[0]);
|
||||
Assert.Equal(found1, found2);
|
||||
Assert.Equal(found1, found3);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DeleteAll(multis);
|
||||
}
|
||||
}
|
||||
|
||||
private static TestMulti CreateMulti(Map map, Point3D location)
|
||||
{
|
||||
var multi = new TestMulti();
|
||||
multi.MoveToWorld(location, map);
|
||||
return multi;
|
||||
}
|
||||
|
||||
private static void DeleteAll(TestMulti[] multis)
|
||||
{
|
||||
for (var i = 0; i < multis.Length; i++)
|
||||
{
|
||||
multis[i]?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
// Test implementation of BaseMulti
|
||||
private class TestMulti : BaseMulti
|
||||
{
|
||||
public TestMulti() : base(0x1)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
319
Projects/Server.Tests/Tests/Maps/StaticTileEnumeratorTests.cs
Normal file
319
Projects/Server.Tests/Tests/Maps/StaticTileEnumeratorTests.cs
Normal file
|
|
@ -0,0 +1,319 @@
|
|||
using System.Collections.Generic;
|
||||
using Server.Items;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Tests.Maps;
|
||||
|
||||
[Collection("Sequential Server Tests")]
|
||||
public class StaticTileEnumeratorTests
|
||||
{
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_MapNullYieldsEmpty()
|
||||
{
|
||||
var enumerator = new Map.StaticTileEnumerable(null, new Point2D(0, 0)).GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_EmptyLocationYieldsEmpty()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(100, 100);
|
||||
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: true, includeMultis: false))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Since we don't have actual map files loaded, this should be empty
|
||||
Assert.Empty(tiles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_IncludeStaticsOnlyWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(200, 200);
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at the location
|
||||
multi = CreateMultiWithComponents(map, new Point3D(200, 200, 0));
|
||||
|
||||
// Get tiles with statics only (no multis)
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: true, includeMultis: false))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should not include multi tiles
|
||||
Assert.Empty(tiles);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_IncludeMultisOnlyWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(300, 300);
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at the location with components
|
||||
multi = CreateMultiWithComponents(map, new Point3D(300, 300, 0));
|
||||
|
||||
// Get tiles with multis only (no statics)
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: false, includeMultis: true))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should include multi tiles
|
||||
Assert.NotEmpty(tiles);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_IncludeBothStaticsAndMultisWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(400, 400);
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at the location
|
||||
multi = CreateMultiWithComponents(map, new Point3D(400, 400, 0));
|
||||
|
||||
// Get all tiles (statics and multis)
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: true, includeMultis: true))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should include multi tiles (statics would be empty without map files)
|
||||
Assert.NotEmpty(tiles);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_MultiTileZOffsetApplied()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(500, 500);
|
||||
var multiZ = 10;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at Z=10
|
||||
multi = CreateMultiWithComponents(map, new Point3D(500, 500, multiZ));
|
||||
|
||||
// Get multi tiles
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: false, includeMultis: true))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// All tiles should have Z offset by the multi's Z position
|
||||
Assert.NotEmpty(tiles);
|
||||
Assert.All(tiles, tile => Assert.True(tile.Z >= multiZ));
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_TileMatrixGetStaticTilesWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var x = 600;
|
||||
var y = 600;
|
||||
|
||||
// Test the TileMatrix.GetStaticTiles method
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in map.Tiles.GetStaticTiles(x, y))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Without map files loaded, should be empty
|
||||
Assert.Empty(tiles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_TileMatrixGetStaticAndMultiTilesWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var x = 700;
|
||||
var y = 700;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at the location
|
||||
multi = CreateMultiWithComponents(map, new Point3D(700, 700, 0));
|
||||
|
||||
// Test the TileMatrix.GetStaticAndMultiTiles method
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in map.Tiles.GetStaticAndMultiTiles(x, y))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should include multi tiles
|
||||
Assert.NotEmpty(tiles);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_TileMatrixGetMultiTilesWorks()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var x = 800;
|
||||
var y = 800;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi at the location
|
||||
multi = CreateMultiWithComponents(map, new Point3D(800, 800, 0));
|
||||
|
||||
// Test the TileMatrix.GetMultiTiles method
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in map.Tiles.GetMultiTiles(x, y))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should include multi tiles
|
||||
Assert.NotEmpty(tiles);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_MultipleMultisAtSameLocation()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(900, 900);
|
||||
|
||||
var multis = new TestMulti[2];
|
||||
try
|
||||
{
|
||||
// Create two multis at the same location
|
||||
multis[0] = CreateMultiWithComponents(map, new Point3D(900, 900, 0));
|
||||
multis[1] = CreateMultiWithComponents(map, new Point3D(900, 900, 5));
|
||||
|
||||
// Get all multi tiles
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: false, includeMultis: true))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should include tiles from both multis
|
||||
Assert.NotEmpty(tiles);
|
||||
// We expect at least tiles from both multis
|
||||
Assert.True(tiles.Count >= 2);
|
||||
}
|
||||
finally
|
||||
{
|
||||
multis[0]?.Delete();
|
||||
multis[1]?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_EmptyReturnsCorrectly()
|
||||
{
|
||||
var enumerator = Map.StaticTileEnumerable.Empty.GetEnumerator();
|
||||
Assert.False(enumerator.MoveNext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticTileEnumerator_DeletedMultiSkipped()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var location = new Point2D(1000, 1000);
|
||||
|
||||
var multis = new TestMulti[2];
|
||||
try
|
||||
{
|
||||
// Create two multis
|
||||
multis[0] = CreateMultiWithComponents(map, new Point3D(1000, 1000, 0));
|
||||
multis[1] = CreateMultiWithComponents(map, new Point3D(1000, 1000, 5));
|
||||
|
||||
// Delete the first multi
|
||||
multis[0].Delete();
|
||||
|
||||
// Get all multi tiles
|
||||
var tiles = new List<StaticTile>();
|
||||
foreach (var tile in new Map.StaticTileEnumerable(map, location, includeStatics: false, includeMultis: true))
|
||||
{
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
// Should only include tiles from the second multi
|
||||
Assert.NotEmpty(tiles);
|
||||
Assert.All(tiles, tile => Assert.True(tile.Z >= 5));
|
||||
}
|
||||
finally
|
||||
{
|
||||
multis[0]?.Delete();
|
||||
multis[1]?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
private static TestMulti CreateMultiWithComponents(Map map, Point3D location)
|
||||
{
|
||||
var multi = new TestMulti(World.NewItem);
|
||||
multi.MoveToWorld(location, map);
|
||||
return multi;
|
||||
}
|
||||
|
||||
private class TestMulti : BaseMulti
|
||||
{
|
||||
public TestMulti(Serial serial) : base(serial)
|
||||
{
|
||||
}
|
||||
|
||||
public override MultiComponentList Components => DefaultComponents;
|
||||
|
||||
private static readonly MultiComponentList DefaultComponents = new(
|
||||
[
|
||||
new MultiTileEntry(0x1, 0, 0, 0, 0x0),
|
||||
new MultiTileEntry(0x2, 1, 0, 0, 0x0)
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue