feat: Add CanSpawnMobile overload with props Z-range support. (#2293)
### Summary - Adds CanSpawnMobile(x, y, minZ, maxZ, canSwim, cantWalk, out spawnZ) overload for finding spawn surfaces within a Z range - Adds CanSpawnItem(x, y, minZ, maxZ, out spawnZ) for item spawning with Surface+Impassable support (tables, furniture) - Uses bitmask optimization inspired by Item.DropToWorld's m_OpenSlots pattern for O(1) surface/blocker checks - HomeRange spawners now use surface detection to set proper Z bounds ### Key Changes Map.cs: - CanSpawnMobile with Z-range finds lowest valid surface for mobiles - CanSpawnItem with Z-range finds lowest valid surface for items (including tables) - CanFitItem for point-check item placement on Surface+Impassable tiles - Bitmask approach eliminates nested loops and stackalloc arrays Spawners: - Simplified GetSpawnPosition using new Z-range methods - HomeRange setter detects surface below spawner for proper Z bounds - Consistent handling for mobiles and items ### Bug Fixes - Water tiles (Impassable | Wet) no longer block swimming mobs - Items can now spawn on tables/furniture (Surface+Impassable) ### Test Plan - Run dotnet test - 631 tests pass - Manual testing: multi-story spawning, water mobs, item spawning on tables - Verify HomeRange spawner movement shifts bounds correctly
This commit is contained in:
parent
ebaf104935
commit
6d51b33cf8
15 changed files with 1604 additions and 214 deletions
327
Projects/Server.Tests/Tests/Maps/CanFitItemTests.cs
Normal file
327
Projects/Server.Tests/Tests/Maps/CanFitItemTests.cs
Normal file
|
|
@ -0,0 +1,327 @@
|
|||
using Server.Items;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests.Tests.Maps;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for CanFitItem which allows Surface+Impassable tiles (tables, furniture) as valid surfaces.
|
||||
/// </summary>
|
||||
[Collection("Sequential Server Tests")]
|
||||
public class CanFitItemTests
|
||||
{
|
||||
private readonly ServerFixture _fixture;
|
||||
|
||||
public CanFitItemTests(ServerFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
}
|
||||
|
||||
private void SkipIfNoTileData()
|
||||
{
|
||||
Skip.If(!ServerFixture.TileDataLoaded, "TileData not loaded - client files required");
|
||||
}
|
||||
|
||||
#region Basic Validation Tests
|
||||
|
||||
[Fact]
|
||||
public void CanFitItem_InternalMapReturnsFalse()
|
||||
{
|
||||
var result = Map.Internal.CanFitItem(100, 100, 0, 1);
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanFitItem_InvalidCoordinatesReturnsFalse()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
|
||||
Assert.False(map.CanFitItem(-1, 100, 0, 1));
|
||||
Assert.False(map.CanFitItem(100, -1, 0, 1));
|
||||
Assert.False(map.CanFitItem(map.Width + 1, 100, 0, 1));
|
||||
Assert.False(map.CanFitItem(100, map.Height + 1, 0, 1));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Land Surface Tests
|
||||
|
||||
private const int TestLandX = 1500;
|
||||
private const int TestLandY = 1600;
|
||||
|
||||
[Fact]
|
||||
public void CanFitItem_LandSurfaceReturnsTrue()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var avgZ = map.GetAverageZ(TestLandX, TestLandY);
|
||||
|
||||
var result = map.CanFitItem(TestLandX, TestLandY, avgZ, 1);
|
||||
|
||||
Assert.True(result, $"Expected land at Z={avgZ} to be valid surface for items");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanFitItem_AboveLandWithNoSurfaceReturnsFalse()
|
||||
{
|
||||
var map = Map.Felucca;
|
||||
var avgZ = map.GetAverageZ(TestLandX, TestLandY);
|
||||
|
||||
// Try to place item 50 units above land with no surface there
|
||||
var result = map.CanFitItem(TestLandX, TestLandY, avgZ + 50, 1);
|
||||
|
||||
Assert.False(result, "No surface 50 units above land");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Surface + Impassable Tests (Tables, Furniture)
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_SurfaceImpassableMultiIsValidSurface()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
Skip.If(ServerFixture.SurfaceImpassableTileId == 0, "No Surface+Impassable tile found in TileData");
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1700;
|
||||
const int y = 1700;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Get the actual tile height from TileData
|
||||
var tileData = TileData.ItemTable[ServerFixture.SurfaceImpassableTileId];
|
||||
var tileHeight = tileData.CalcHeight;
|
||||
const int tileZ = 10;
|
||||
|
||||
// Create a multi with a Surface+Impassable tile (like a table) at Z=10
|
||||
multi = CreateSurfaceImpassableMulti(map, new Point3D(x, y, 0), surfaceZ: tileZ);
|
||||
|
||||
// CanFitItem should treat Surface+Impassable as a valid surface
|
||||
var surfaceTop = tileZ + tileHeight;
|
||||
var result = map.CanFitItem(x, y, surfaceTop, 1);
|
||||
|
||||
Assert.True(result, $"Surface+Impassable tile should be valid surface for items at Z={surfaceTop}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_CanFitDoesNotAllowSurfaceImpassable()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
Skip.If(ServerFixture.SurfaceImpassableTileId == 0, "No Surface+Impassable tile found in TileData");
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1750;
|
||||
const int y = 1750;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Get the actual tile height from TileData
|
||||
var tileData = TileData.ItemTable[ServerFixture.SurfaceImpassableTileId];
|
||||
var tileHeight = tileData.CalcHeight;
|
||||
const int tileZ = 10;
|
||||
|
||||
// Create a multi with a Surface+Impassable tile at Z=10
|
||||
multi = CreateSurfaceImpassableMulti(map, new Point3D(x, y, 0), surfaceZ: tileZ);
|
||||
|
||||
var surfaceTop = tileZ + tileHeight;
|
||||
|
||||
// Regular CanFit should NOT treat Surface+Impassable as valid (for comparison)
|
||||
var canFitResult = map.CanFit(x, y, surfaceTop, 1, requireSurface: true);
|
||||
|
||||
// This test documents the difference between CanFit and CanFitItem
|
||||
// CanFit requires surface && !impassable
|
||||
Assert.False(canFitResult, "CanFit should NOT allow Surface+Impassable as valid surface");
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region World Item Surface Tests
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_NonMovableWorldItemSurfaceWorks()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1800;
|
||||
const int y = 1800;
|
||||
|
||||
Item surfaceItem = null;
|
||||
try
|
||||
{
|
||||
// Create a non-movable surface item (like a placed table)
|
||||
surfaceItem = new Item(ServerFixture.SurfaceTileId)
|
||||
{
|
||||
Movable = false
|
||||
};
|
||||
surfaceItem.MoveToWorld(new Point3D(x, y, 10), map);
|
||||
|
||||
var itemTop = 10 + surfaceItem.ItemData.CalcHeight;
|
||||
var result = map.CanFitItem(x, y, itemTop, 1);
|
||||
|
||||
Assert.True(result, $"Non-movable surface item should be valid surface at Z={itemTop}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
surfaceItem?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_MovableWorldItemNotValidSurface()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1850;
|
||||
const int y = 1850;
|
||||
|
||||
Item movableItem = null;
|
||||
try
|
||||
{
|
||||
// Create a movable surface item
|
||||
movableItem = new Item(ServerFixture.SurfaceTileId)
|
||||
{
|
||||
Movable = true
|
||||
};
|
||||
movableItem.MoveToWorld(new Point3D(x, y, 10), map);
|
||||
|
||||
var itemTop = 10 + movableItem.ItemData.CalcHeight;
|
||||
// Try placing at item top - should fail unless there's land there too
|
||||
var avgZ = map.GetAverageZ(x, y);
|
||||
|
||||
// If avgZ != itemTop, there's no other surface, so it should fail
|
||||
if (avgZ != itemTop)
|
||||
{
|
||||
var result = map.CanFitItem(x, y, itemTop, 1);
|
||||
Assert.False(result, "Movable item should not count as valid surface");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
movableItem?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Blocking Tests
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_ImpassableTileBlocksPlacement()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1900;
|
||||
const int y = 1900;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Create a multi with an impassable blocker at Z=10
|
||||
multi = CreateImpassableMulti(map, new Point3D(x, y, 0), blockerZ: 10);
|
||||
|
||||
// Try to place item at Z=10 (inside the blocker)
|
||||
var result = map.CanFitItem(x, y, 10, 5);
|
||||
|
||||
Assert.False(result, "Should not be able to place item inside impassable tile");
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void CanFitItem_CanPlaceOnTopOfImpassable()
|
||||
{
|
||||
SkipIfNoTileData();
|
||||
Skip.If(ServerFixture.SurfaceImpassableTileId == 0, "No Surface+Impassable tile found in TileData");
|
||||
|
||||
var map = Map.Felucca;
|
||||
const int x = 1950;
|
||||
const int y = 1950;
|
||||
|
||||
TestMulti multi = null;
|
||||
try
|
||||
{
|
||||
// Get the actual tile height from TileData
|
||||
var tileData = TileData.ItemTable[ServerFixture.SurfaceImpassableTileId];
|
||||
var tileHeight = tileData.CalcHeight;
|
||||
const int tileZ = 10;
|
||||
|
||||
// Create a Surface+Impassable tile at Z=10
|
||||
multi = CreateSurfaceImpassableMulti(map, new Point3D(x, y, 0), surfaceZ: tileZ);
|
||||
|
||||
// Place item on top should work
|
||||
var surfaceTop = tileZ + tileHeight;
|
||||
var result = map.CanFitItem(x, y, surfaceTop, 1);
|
||||
|
||||
Assert.True(result, $"Should be able to place item on top of Surface+Impassable tile at Z={surfaceTop}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
multi?.Delete();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Methods and Classes
|
||||
|
||||
private TestMulti CreateSurfaceImpassableMulti(Map map, Point3D location, int surfaceZ)
|
||||
{
|
||||
// Use SurfaceImpassableTileId - a tile that has both Surface and Impassable flags
|
||||
var multi = new TestMulti(new MultiComponentList(
|
||||
[
|
||||
new MultiTileEntry(
|
||||
ServerFixture.SurfaceImpassableTileId,
|
||||
0, 0, (short)surfaceZ,
|
||||
TileFlag.Surface | TileFlag.Impassable
|
||||
)
|
||||
]));
|
||||
multi.MoveToWorld(location, map);
|
||||
return multi;
|
||||
}
|
||||
|
||||
private TestMulti CreateImpassableMulti(Map map, Point3D location, int blockerZ)
|
||||
{
|
||||
var multi = new TestMulti(new MultiComponentList(
|
||||
[
|
||||
new MultiTileEntry(
|
||||
ServerFixture.ImpassableTileId,
|
||||
0, 0, (short)blockerZ,
|
||||
TileFlag.Impassable
|
||||
)
|
||||
]));
|
||||
multi.MoveToWorld(location, map);
|
||||
return multi;
|
||||
}
|
||||
|
||||
private class TestMulti : BaseMulti
|
||||
{
|
||||
private readonly MultiComponentList _components;
|
||||
|
||||
public TestMulti(MultiComponentList components) : base(0x1)
|
||||
{
|
||||
_components = components ?? MultiComponentList.Empty;
|
||||
}
|
||||
|
||||
public override MultiComponentList Components => _components;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue