### 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
129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2025 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: WalkRandomLogic.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 Server.Engines.Spawners;
|
|
|
|
namespace Server.Mobiles;
|
|
|
|
public abstract partial class BaseAI
|
|
{
|
|
public virtual void WalkRandom(int chanceToNotMove, int chanceToDir, int steps)
|
|
{
|
|
if (Mobile.Deleted || Mobile.DisallowAllMoves || chanceToNotMove <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var maxSteps = Math.Min(steps, 3);
|
|
|
|
for (var i = 0; i < maxSteps; i++)
|
|
{
|
|
if (Utility.Random(1 + chanceToNotMove) == 0)
|
|
{
|
|
DoMove(GetRandomDirection(chanceToDir));
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual void WalkRandomInHome(int chanceToNotMove, int chanceToDir, int steps)
|
|
{
|
|
if (Mobile.Deleted || Mobile.DisallowAllMoves)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Mobile.Home == Point3D.Zero)
|
|
{
|
|
WalkRandomNoHome(chanceToNotMove, chanceToDir, steps);
|
|
}
|
|
else
|
|
{
|
|
WalkRandomWithHome(chanceToNotMove, chanceToDir, steps);
|
|
}
|
|
}
|
|
|
|
private void WalkRandomNoHome(int chanceToNotMove, int chanceToDir, int steps)
|
|
{
|
|
if (Mobile.Spawner is RegionSpawner rs)
|
|
{
|
|
var region = rs.SpawnRegion;
|
|
|
|
if (Mobile.Region.AcceptsSpawnsFrom(region))
|
|
{
|
|
Mobile.WalkRegion = region;
|
|
|
|
WalkRandom(chanceToNotMove, chanceToDir, steps);
|
|
|
|
Mobile.WalkRegion = null;
|
|
}
|
|
else if (region.GoLocation != Point3D.Zero && Utility.RandomBool())
|
|
{
|
|
DoMove(Mobile.GetDirectionTo(region.GoLocation));
|
|
}
|
|
else
|
|
{
|
|
WalkRandom(chanceToNotMove, chanceToDir, 1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
WalkRandom(chanceToNotMove, chanceToDir, steps);
|
|
}
|
|
}
|
|
|
|
private void WalkRandomWithHome(int chanceToNotMove, int chanceToDir, int steps)
|
|
{
|
|
if (Mobile.RangeHome == 0)
|
|
{
|
|
if (Mobile.Location != Mobile.Home)
|
|
{
|
|
DoMove(Mobile.GetDirectionTo(Mobile.Home));
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
for (var i = 0; i < steps; i++)
|
|
{
|
|
var currDist = (int)Mobile.GetDistanceToSqrt(Mobile.Home);
|
|
|
|
if (currDist > Mobile.RangeHome)
|
|
{
|
|
DoMove(Mobile.GetDirectionTo(Mobile.Home));
|
|
}
|
|
else if (currDist < Mobile.RangeHome * 2 / 3 || Utility.Random(10) <= 5)
|
|
{
|
|
WalkRandom(chanceToNotMove, chanceToDir, 1);
|
|
}
|
|
else
|
|
{
|
|
DoMove(Mobile.GetDirectionTo(Mobile.Home));
|
|
}
|
|
}
|
|
}
|
|
|
|
private Direction GetRandomDirection(int chanceToDir)
|
|
{
|
|
var randomMove = Utility.Random(8 * (chanceToDir + 1));
|
|
|
|
if (randomMove < 8)
|
|
{
|
|
return (Direction)randomMove;
|
|
}
|
|
|
|
return Mobile.Direction;
|
|
}
|
|
}
|