fix: Removes GetObjectsInRange and fixes boat planks closing (#1579)

## BREAKING CHANGE

- Deletes `map.GetObjectsInRange` and `map.GetObejctsInBounds`

### Notes

Developers are expected to enumerate mobiles and items separately now using `map.GetMobilesInRange` and `map.GetItemsInRange`. This helps keep the code streamlined so we don't have to maintain multiple copies of ref struct enumerators that do the same thing.


### Fixes

- [X] Fixes bug with planks closing
- [X] Fixes issue with iterating items/mobiles from a null map
This commit is contained in:
Kamron Batman 2023-11-03 13:45:18 -07:00 committed by GitHub
parent cb1638591b
commit 977fdc2c5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 543 additions and 461 deletions

View file

@ -2445,15 +2445,6 @@ public class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropertyListEnt
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool GetSaveFlag(SaveFlag flags, SaveFlag toGet) => (flags & toGet) != 0;
public IPooledEnumerable<IEntity> GetObjectsInRange(int range)
{
var map = m_Map;
return map == null
? PooledEnumeration.NullEnumerable<IEntity>.Instance
: map.GetObjectsInRange(m_Parent == null ? m_Location : GetWorldLocation(), range);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Map.ItemAtEnumerable<Item> GetItemsAt() =>
m_Map == null ? Map.ItemAtEnumerable<Item>.Empty : m_Map.GetItemsAt(m_Parent == null ? m_Location : GetWorldLocation());

View file

@ -21,6 +21,9 @@ namespace Server;
public partial class Map
{
private static ValueLinkList<Item> _emptyItemLinkList = new();
public static ref readonly ValueLinkList<Item> EmptyItemLinkList => ref _emptyItemLinkList;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemAtEnumerable<Item> GetItemsAt(Point3D p) => GetItemsAt<Item>(p);
@ -110,7 +113,15 @@ public partial class Map
{
_started = false;
_location = loc;
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Items;
if (map == null)
{
_linkList = ref EmptyItemLinkList;
}
else
{
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Items;
}
_version = 0;
_current = null;
}

View file

@ -21,6 +21,9 @@ namespace Server;
public partial class Map
{
private static ValueLinkList<Mobile> _emptyMobileLinkList = new();
public static ref readonly ValueLinkList<Mobile> EmptyMobileLinkList => ref _emptyMobileLinkList;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MobileAtEnumerable<Mobile> GetMobilesAt(Point3D p) => GetMobilesAt<Mobile>(p);
@ -110,7 +113,14 @@ public partial class Map
{
_started = false;
_location = loc;
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Mobiles;
if (map == null)
{
_linkList = ref EmptyMobileLinkList;
}
else
{
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Mobiles;
}
_version = 0;
_current = null;
}

View file

@ -865,25 +865,15 @@ public sealed partial class Map : IComparable<Map>, ISpanFormattable, ISpanParsa
public IPooledEnumerable<StaticTile[]> GetMultiTilesAt(int x, int y) =>
PooledEnumeration.GetMultiTiles(this, new Rectangle2D(x, y, 1, 1));
public IPooledEnumerable<IEntity> GetObjectsInRange(Point3D p) => GetObjectsInRange(p, Core.GlobalMaxUpdateRange);
public IPooledEnumerable<IEntity> GetObjectsInRange(Point3D p, int range) =>
GetObjectsInBounds(new Rectangle2D(p.m_X - range, p.m_Y - range, range * 2 + 1, range * 2 + 1));
public IPooledEnumerable<IEntity> GetObjectsInBounds(Rectangle2D bounds) =>
PooledEnumeration.GetEntities(this, bounds);
public bool CanFit(
Point3D p, int height, bool checkBlocksFit = false, bool checkMobiles = true,
bool requireSurface = true
) =>
CanFit(p.m_X, p.m_Y, p.m_Z, height, checkBlocksFit, checkMobiles, requireSurface);
) => CanFit(p.m_X, p.m_Y, p.m_Z, height, checkBlocksFit, checkMobiles, requireSurface);
public bool CanFit(
Point2D p, int z, int height, bool checkBlocksFit = false, bool checkMobiles = true,
bool requireSurface = true
) =>
CanFit(p.m_X, p.m_Y, z, height, checkBlocksFit, checkMobiles, requireSurface);
) => CanFit(p.m_X, p.m_Y, z, height, checkBlocksFit, checkMobiles, requireSurface);
public bool CanFit(
int x, int y, int z, int height, bool checkBlocksFit = false, bool checkMobiles = true,

View file

@ -17,7 +17,6 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Server.Collections;
using Server.Items;
namespace Server;
@ -32,32 +31,13 @@ public static class PooledEnumeration
static PooledEnumeration()
{
EntitySelector = SelectEntities;
MultiSelector = SelectMultis;
MultiTileSelector = SelectMultiTiles;
}
public static Selector<IEntity> EntitySelector { get; set; }
public static Selector<Mobile> MobileSelector { get; set; }
public static Selector<BaseMulti> MultiSelector { get; set; }
public static Selector<StaticTile[]> MultiTileSelector { get; set; }
public static IEnumerable<IEntity> SelectEntities(Map.Sector s, Rectangle2D bounds)
{
var entities = new List<IEntity>(s.Mobiles.Count + s.Items.Count);
foreach (var mob in s.Mobiles)
{
entities.Add(mob);
}
foreach (var item in s.Items)
{
entities.Add(item);
}
return entities;
}
public static IEnumerable<BaseMulti> SelectMultis(Map.Sector s, Rectangle2D bounds)
{
var entities = new List<BaseMulti>(s.Multis.Count);
@ -126,9 +106,6 @@ public static class PooledEnumeration
}
}
public static PooledEnumerable<IEntity> GetEntities(Map map, Rectangle2D bounds) =>
PooledEnumerable<IEntity>.Instantiate(map, bounds, EntitySelector ?? SelectEntities);
public static PooledEnumerable<BaseMulti> GetMultis(Map map, Rectangle2D bounds) =>
PooledEnumerable<BaseMulti>.Instantiate(map, bounds, MultiSelector ?? SelectMultis);

View file

@ -4342,25 +4342,25 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
{
using var moveQueue = PooledRefQueue<IEntity>.Create(2048);
using var moveClientQueue = PooledRefQueue<Mobile>.Create(2048);
var eable = m_Map.GetObjectsInRange(m_Location, Core.GlobalMaxUpdateRange);
foreach (var o in eable)
foreach (var mob in m_Map.GetMobilesInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (o == this)
if (mob == this)
{
continue;
}
if (o is Mobile mob)
if (mob.NetState != null)
{
if (mob.NetState != null)
{
moveClientQueue.Enqueue(mob);
}
moveQueue.Enqueue(mob);
moveClientQueue.Enqueue(mob);
}
else if (o is Item item && item.HandlesOnMovement)
moveQueue.Enqueue(mob);
}
foreach (var item in m_Map.GetItemsInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (item.HandlesOnMovement)
{
moveQueue.Enqueue(item);
}
@ -5561,44 +5561,27 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
if (m_Map != null)
{
var eable = m_Map.GetObjectsInRange(m_Location, range);
foreach (var o in eable)
foreach (var heard in m_Map.GetMobilesInRange(m_Location, range))
{
if (o is Mobile heard)
if (!heard.CanSee(this) || !NoSpeechLOS && heard.Player && !heard.InLOS(this))
{
if (!heard.CanSee(this) || !NoSpeechLOS && heard.Player && !heard.InLOS(this))
{
continue;
}
if (heard.m_NetState != null)
{
hears.Add(heard);
}
if (heard.HandlesOnSpeech(this))
{
onSpeech.Add(heard);
}
for (var i = 0; i < heard.Items.Count; ++i)
{
var item = heard.Items[i];
if (item.HandlesOnSpeech)
{
onSpeech.Add(item);
}
if (item is Container container)
{
AddSpeechItemsFrom(onSpeech, container);
}
}
continue;
}
else if (o is Item item)
if (heard.m_NetState != null)
{
hears.Add(heard);
}
if (heard.HandlesOnSpeech(this))
{
onSpeech.Add(heard);
}
for (var i = 0; i < heard.Items.Count; ++i)
{
var item = heard.Items[i];
if (item.HandlesOnSpeech)
{
onSpeech.Add(item);
@ -5611,6 +5594,19 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
}
}
foreach (var item in m_Map.GetItemsInRange(m_Location, range))
{
if (item.HandlesOnSpeech)
{
onSpeech.Add(item);
}
if (item is Container container)
{
AddSpeechItemsFrom(onSpeech, container);
}
}
object mutateContext = null;
var mutatedText = text;
SpeechEventArgs mutatedArgs = null;
@ -6856,23 +6852,19 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
return;
}
var eable = m_Map.GetObjectsInRange(m_Location, Core.GlobalMaxUpdateRange);
foreach (var o in eable)
foreach (var m in m_Map.GetMobilesInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (o is Mobile m)
if (m != this && Utility.InUpdateRange(m_Location, m.m_Location))
{
if (m != this && Utility.InUpdateRange(m_Location, m.m_Location))
{
m_NetState.SendRemoveEntity(m.Serial);
}
m_NetState.SendRemoveEntity(m.Serial);
}
else if (o is Item item)
}
foreach (var item in m_Map.GetItemsInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (InRange(item.Location, item.GetUpdateRange(this)))
{
if (InRange(item.Location, item.GetUpdateRange(this)))
{
m_NetState.SendRemoveEntity(item.Serial);
}
m_NetState.SendRemoveEntity(item.Serial);
}
}
}
@ -6923,36 +6915,32 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
return;
}
var eable = m_Map.GetObjectsInRange(m_Location, Core.GlobalMaxUpdateRange);
foreach (var o in eable)
foreach (var m in m_Map.GetMobilesInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (o is Item item)
if (CanSee(m) && Utility.InUpdateRange(m_Location, m.m_Location))
{
if (CanSee(item) && InRange(item.Location, item.GetUpdateRange(this)))
ns.SendMobileIncoming(this, m);
if (ns.StygianAbyss)
{
item.SendInfoTo(ns);
ns.SendMobileHealthbar(m, Healthbar.Poison);
ns.SendMobileHealthbar(m, Healthbar.Yellow);
}
if (m.IsDeadBondedPet)
{
ns.SendBondedStatus(m.Serial, true);
}
m.SendOPLPacketTo(ns);
}
else if (o is Mobile m)
}
foreach (var item in m_Map.GetItemsInRange(m_Location, Core.GlobalMaxUpdateRange))
{
if (CanSee(item) && InRange(item.Location, item.GetUpdateRange(this)))
{
if (CanSee(m) && Utility.InUpdateRange(m_Location, m.m_Location))
{
ns.SendMobileIncoming(this, m);
if (ns.StygianAbyss)
{
ns.SendMobileHealthbar(m, Healthbar.Poison);
ns.SendMobileHealthbar(m, Healthbar.Yellow);
}
if (m.IsDeadBondedPet)
{
ns.SendBondedStatus(m.Serial, true);
}
m.SendOPLPacketTo(ns);
}
item.SendInfoTo(ns);
}
}
}
@ -7310,67 +7298,70 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
if (ourState != null)
{
// We are attached to a client, so it's a bit more complex. We need to send new items and people to ourself, and ourself to other clients
foreach (var o in map.GetObjectsInRange(newLocation, Core.GlobalMaxUpdateRange))
foreach (var m in map.GetMobilesInRange(newLocation, Core.GlobalMaxUpdateRange))
{
if (o is Item item)
if (m == this)
{
var range = item.GetUpdateRange(this);
var loc = item.Location;
if (!Utility.InRange(oldLocation, loc, range) && Utility.InRange(newLocation, loc, range) &&
CanSee(item))
{
item.SendInfoTo(ourState);
}
continue;
}
else if (o != this && o is Mobile m)
if (!Utility.InUpdateRange(newLocation, m.m_Location))
{
if (!Utility.InUpdateRange(newLocation, m.m_Location))
continue;
}
var inOldRange = Utility.InUpdateRange(oldLocation, m.m_Location);
var ns = m.m_NetState;
if (ns != null &&
(isTeleport && (!ns.HighSeas || !NoMoveHS) || !inOldRange) && m.CanSee(this))
{
ns.SendMobileIncoming(m, this);
if (ns.StygianAbyss)
{
continue;
ns.SendMobileHealthbar(this, Healthbar.Poison);
ns.SendMobileHealthbar(this, Healthbar.Yellow);
}
var inOldRange = Utility.InUpdateRange(oldLocation, m.m_Location);
var ns = m.m_NetState;
if (ns != null &&
(isTeleport && (!ns.HighSeas || !NoMoveHS) || !inOldRange) && m.CanSee(this))
if (IsDeadBondedPet)
{
ns.SendMobileIncoming(m, this);
if (ns.StygianAbyss)
{
ns.SendMobileHealthbar(this, Healthbar.Poison);
ns.SendMobileHealthbar(this, Healthbar.Yellow);
}
if (IsDeadBondedPet)
{
ns.SendBondedStatus(Serial, true);
}
SendOPLPacketTo(ns);
ns.SendBondedStatus(Serial, true);
}
if (inOldRange || !CanSee(m))
{
continue;
}
SendOPLPacketTo(ns);
}
ourState.SendMobileIncoming(this, m);
if (inOldRange || !CanSee(m))
{
continue;
}
if (ourState.StygianAbyss)
{
ourState.SendMobileHealthbar(m, Healthbar.Poison);
ourState.SendMobileHealthbar(m, Healthbar.Yellow);
}
ourState.SendMobileIncoming(this, m);
if (m.IsDeadBondedPet)
{
ourState.SendBondedStatus(m.Serial, true);
}
if (ourState.StygianAbyss)
{
ourState.SendMobileHealthbar(m, Healthbar.Poison);
ourState.SendMobileHealthbar(m, Healthbar.Yellow);
}
m.SendOPLPacketTo(ourState);
if (m.IsDeadBondedPet)
{
ourState.SendBondedStatus(m.Serial, true);
}
m.SendOPLPacketTo(ourState);
}
foreach (var item in map.GetItemsInRange(newLocation, Core.GlobalMaxUpdateRange))
{
var range = item.GetUpdateRange(this);
var loc = item.Location;
if (!Utility.InRange(oldLocation, loc, range) && Utility.InRange(newLocation, loc, range) &&
CanSee(item))
{
item.SendInfoTo(ourState);
}
}
}
@ -8098,10 +8089,6 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
public Map.ItemBoundsEnumerable<T> GetItemsInRange<T>(int range) where T : Item =>
m_Map == null ? Map.ItemBoundsEnumerable<T>.Empty : m_Map.GetItemsInRange<T>(m_Location, range);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IPooledEnumerable<IEntity> GetObjectsInRange(int range) =>
m_Map?.GetObjectsInRange(m_Location, range) ?? PooledEnumeration.NullEnumerable<IEntity>.Instance;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Map.MobileAtEnumerable<Mobile> GetMobilesInRange() => GetMobilesInRange<Mobile>();

View file

@ -1762,7 +1762,7 @@ public static class Utility
span == default || span.IsEmpty || span.IsWhiteSpace();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool InTypeList(this Item item, Type[] types) => item.GetType().InTypeList(types);
public static bool InTypeList<T>(this T obj, Type[] types) => obj.GetType().InTypeList(types);
public static bool InTypeList(this Type t, Type[] types)
{

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Server;
using Server.Collections;
using Server.Multis;
using Server.Multis.Boats;
using Server.Network;
@ -37,9 +38,12 @@ public class BoatPacketTests : IClassFixture<ServerFixture>
beholder.Map = Map.Felucca;
beholder.CanSeeEntities.Add(item1);
var list = new List<IEntity> { item1, beholder };
using var list = PooledRefList<IEntity>.Create();
list.Add(beholder);
list.Add(item1);
var notContained = new List<IEntity> { item2 };
var boat = new TestBoat((Serial)0x3000, list, notContained)
var boat = new TestBoat((Serial)0x3000, notContained)
{
Location = new Point3D(10, 20, 15),
Facing = Direction.Right,
@ -52,7 +56,7 @@ public class BoatPacketTests : IClassFixture<ServerFixture>
ns.ProtocolChanges = ProtocolChanges.HighSeas;
var expected = new MoveBoatHS(beholder, boat, d, speed, list, xOffset, yOffset).Compile();
ns.SendMoveBoatHS(beholder, boat, d, speed, boat.GetMovingEntities(true), xOffset, yOffset);
ns.SendMoveBoatHS(boat, list, d, speed, xOffset, yOffset);
var result = ns.SendPipe.Reader.AvailableToRead();
AssertThat.Equal(result, expected);
@ -77,15 +81,16 @@ public class BoatPacketTests : IClassFixture<ServerFixture>
var beholder = new MockedMobile((Serial)0x100);
beholder.DefaultMobileInit();
beholder.Location = new Point3D(10, 20, 15);
beholder.Map = Map.Felucca;
beholder.CanSeeEntities.Add(item1);
var list = new List<IEntity> { item1, beholder };
var notContained = new List<IEntity> { item2 };
var boat = new TestBoat((Serial)0x3000, list, notContained)
var boat = new TestBoat((Serial)0x3000, notContained)
{
Location = new Point3D(10, 20, 15),
Facing = Direction.Right
Facing = Direction.Right,
Map = Map.Felucca
};
beholder.CanSeeEntities.Add(boat);
@ -103,21 +108,16 @@ public class BoatPacketTests : IClassFixture<ServerFixture>
private class TestBoat : BaseBoat
{
private readonly List<IEntity> _list;
private readonly List<IEntity> _notContainedList;
public TestBoat(Serial serial, List<IEntity> list, List<IEntity> notContainedList) : base(serial)
{
_list = list;
_notContainedList = notContainedList;
}
public TestBoat(Serial serial, List<IEntity> notContainedList) : base(serial) => _notContainedList = notContainedList;
public override MultiComponentList Components { get; } = new(new List<MultiTileEntry>());
public override bool Contains(int x, int y) => !_notContainedList.Any(e => e.X == x && e.Y == y);
public override MovingEntitiesEnumerable GetMovingEntities(bool includeBoat = false) =>
new(this, true, new PooledEnumeration.PooledEnumerable<IEntity>(_list));
new(this, true, new Rectangle2D(new Point2D(9, 19), new Point2D(13, 22)));
}
private class MockedMobile : Mobile

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using Server;
using Server.Collections;
using Server.Items;
using Server.Multis;
using Server.Network;
@ -11,7 +12,7 @@ namespace UOContent.Tests
{
public MoveBoatHS(
Mobile beholder, BaseBoat boat, Direction d,
int speed, List<IEntity> ents, int xOffset,
int speed, PooledRefList<IEntity> ents, int xOffset,
int yOffset
) : base(0xF6)
{
@ -30,11 +31,6 @@ namespace UOContent.Tests
foreach (var ent in ents)
{
if (!beholder.CanSee(ent))
{
continue;
}
Stream.Write(ent.Serial);
Stream.Write((short)(ent.X + xOffset));
Stream.Write((short)(ent.Y + yOffset));

View file

@ -43,22 +43,29 @@ namespace Server.Commands.Generic
return;
}
var eable = map.GetObjectsInBounds(rect);
var objs = new List<object>();
foreach (var obj in eable)
if (mobiles)
{
if (!mobiles && obj is Mobile || !items && obj is Item)
foreach (var m in map.GetMobilesInBounds(rect))
{
continue;
}
if (BaseCommand.IsAccessible(from, obj) && ext.IsValid(obj))
{
objs.Add(obj);
if (BaseCommand.IsAccessible(from, m) && ext.IsValid(m))
{
objs.Add(m);
}
}
}
if (items)
{
foreach (var item in map.GetItemsInBounds(rect))
{
if (BaseCommand.IsAccessible(from, item) && ext.IsValid(item))
{
objs.Add(item);
}
}
}
ext.Filter(objs);
RunCommand(from, objs, command, args);

View file

@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using Server.Collections;
using Server.Items;
using Server.Multis;
@ -68,38 +68,42 @@ namespace Server.Commands
var multis = (type & WipeType.Multis) != 0;
var items = (type & WipeType.Items) != 0;
var toDelete = new List<IEntity>();
var rect = new Rectangle2D(start.X, start.Y, end.X - start.X + 1, end.Y - start.Y + 1);
IPooledEnumerable<IEntity> eable;
using var toDelete = PooledRefQueue<IEntity>.Create();
if (!items && !multis || !mobiles)
if (mobiles)
{
return;
}
eable = map.GetObjectsInBounds(rect);
foreach (var obj in eable)
{
if (items && obj is Item && !(obj is BaseMulti or HouseSign))
foreach (var mobile in map.GetMobilesInBounds(rect))
{
toDelete.Add(obj);
}
else if (multis && obj is BaseMulti)
{
toDelete.Add(obj);
}
else if (obj is Mobile mobile && !mobile.Player)
{
toDelete.Add(mobile);
if (!mobile.Player)
{
toDelete.Enqueue(mobile);
}
}
}
for (var i = 0; i < toDelete.Count; ++i)
if (items || multis)
{
toDelete[i].Delete();
foreach (var item in map.GetItemsInBounds(rect))
{
if (item is BaseMulti)
{
if (multis)
{
toDelete.Enqueue(item);
}
}
else if (items && item is not HouseSign)
{
toDelete.Enqueue(item);
}
}
}
while (toDelete.Count > 0)
{
toDelete.Dequeue().Delete();
}
}
}

View file

@ -255,25 +255,25 @@ namespace Server.Factions
public static bool IsNearType(Mobile mob, Type type, int range)
{
var mobs = type.IsSubclassOf(typeof(Mobile));
var items = type.IsSubclassOf(typeof(Item));
if (!(items || mobs))
if (type.IsAssignableTo(typeof(Mobile)))
{
return false;
foreach (var obj in mob.Map.GetMobilesInRange(mob.Location, range))
{
if (type.IsInstanceOfType(obj))
{
return true;
}
}
}
var eable = mob.Map.GetObjectsInRange(mob.Location, range);
foreach (var obj in eable)
if (type.IsAssignableTo(typeof(Item)))
{
if (!mobs && obj is Mobile || !items && obj is Item)
foreach (var item in mob.Map.GetItemsInRange(mob.Location, range))
{
continue;
}
if (type.IsInstanceOfType(obj))
{
return true;
if (type.IsInstanceOfType(item))
{
return true;
}
}
}
@ -282,17 +282,44 @@ namespace Server.Factions
public static bool IsNearType(Mobile mob, Type[] types, int range)
{
var eable = mob.GetObjectsInRange(range);
foreach (var obj in eable)
bool mobs = false;
bool items = false;
for (var i = 0; !(mobs && items) && i < types.Length; i++)
{
for (int i = 0; i < types.Length; i++)
var type = types[i];
if (type.IsAssignableTo(typeof(Mobile)))
{
if (types[i].IsInstanceOfType(obj))
mobs = true;
}
if (type.IsAssignableTo(typeof(Item)))
{
items = true;
}
}
if (mobs)
{
foreach (var m in mob.Map.GetMobilesInRange(mob.Location, range))
{
if (m.InTypeList(types))
{
return true;
}
}
}
if (items)
{
foreach (var item in mob.Map.GetItemsInRange(mob.Location, range))
{
if (item.InTypeList(types))
{
return true;
}
}
}
return false;
}

View file

@ -81,43 +81,9 @@ namespace Server.Movement
var checkMobs = (m as BaseCreature)?.Controlled == false && (xForward != _goal.X || yForward != _goal.Y);
foreach (var entity in map.GetObjectsInRange(loc, 1))
if (checkMobs)
{
if (entity is Item item)
{
if (ignoreMovableImpassables && item.Movable && item.ItemData.ImpassableSurface)
{
continue;
}
if (!item.ItemData[reqFlags] || item.ItemID > TileData.MaxItemValue || item.Parent != null)
{
continue;
}
if (item is BaseMulti)
{
continue;
}
if (item.AtPoint(xStart, yStart))
{
itemsStart.Add(item);
}
else if (item.AtPoint(xForward, yForward))
{
itemsForward.Add(item);
}
else if (checkDiagonals && item.AtPoint(xLeft, yLeft))
{
itemsLeft.Add(item);
}
else if (checkDiagonals && item.AtPoint(xRight, yRight))
{
itemsRight.Add(item);
}
}
else if (checkMobs && entity is Mobile mob)
foreach (var mob in map.GetMobilesInRange(loc, 1))
{
if (mob.AtPoint(xForward, yForward))
{
@ -134,6 +100,41 @@ namespace Server.Movement
}
}
foreach (var item in map.GetItemsInRange(loc, 1))
{
if (ignoreMovableImpassables && item.Movable && item.ItemData.ImpassableSurface)
{
continue;
}
if (!item.ItemData[reqFlags] || item.ItemID > TileData.MaxItemValue || item.Parent != null)
{
continue;
}
if (item is BaseMulti)
{
continue;
}
if (item.AtPoint(xStart, yStart))
{
itemsStart.Add(item);
}
else if (item.AtPoint(xForward, yForward))
{
itemsForward.Add(item);
}
else if (checkDiagonals && item.AtPoint(xLeft, yLeft))
{
itemsLeft.Add(item);
}
else if (checkDiagonals && item.AtPoint(xRight, yRight))
{
itemsRight.Add(item);
}
}
GetStartZ(m, map, loc, itemsStart, out var startZ, out var startTop);
var moveIsOk = Check(map, m, itemsForward, mobsForward, xForward, yForward, startTop, startZ, out newZ);

View file

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ModernUO.Serialization;
using Server.Collections;
using Server.Targeting;
namespace Server.Items;

View file

@ -142,29 +142,27 @@ public abstract partial class BaseExplosionPotion : BasePotion
alchemyBonus = (int)(from.Skills.Alchemy.Value / (Core.AOS ? 5 : 10));
}
var eable = map.GetObjectsInRange(loc, ExplosionRange);
using var queue = PooledRefQueue<IEntity>.Create();
var toDamage = 0;
foreach (var entity in eable)
foreach (var mobile in map.GetMobilesInRange(loc, ExplosionRange))
{
if (entity == this)
if (from == null || SpellHelper.ValidIndirectTarget(from, mobile) && from.CanBeHarmful(mobile, false))
{
continue;
++toDamage;
queue.Enqueue(mobile);
}
}
if (entity is Mobile mobile)
if (LeveledExplosion)
{
foreach (var item in map.GetItemsInRange<BaseExplosionPotion>(loc, ExplosionRange))
{
if (from == null || SpellHelper.ValidIndirectTarget(from, mobile) && from.CanBeHarmful(mobile, false))
if (item != this)
{
++toDamage;
queue.Enqueue(entity);
queue.Enqueue(item);
}
}
else if (LeveledExplosion && entity is BaseExplosionPotion)
{
queue.Enqueue(entity);
}
}
var min = Scale(from, MinDamage);

View file

@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Server.Collections;
using Server.Engines.Spawners;
using Server.Items;
using Server.Multis.Boats;
using Server.Network;
namespace Server.Multis
{
@ -731,12 +733,9 @@ namespace Server.Multis
return DryDockResult.Items;
}
using var ents = GetMovingEntities();
var enumerator = ents.GetEnumerator();
if (enumerator.MoveNext())
foreach (var ent in GetMovingEntities())
{
return enumerator.Current is Mobile ? DryDockResult.Mobiles : DryDockResult.Items;
return ent is Mobile ? DryDockResult.Mobiles : DryDockResult.Items;
}
return DryDockResult.Valid;
@ -1794,7 +1793,16 @@ namespace Server.Multis
}
else
{
using var eable = GetMovingEntities(true);
// We use a list because GetMovingEntities uses Map.GetItemsInRange which isn't safe for
// moving items while iterating.
using var list = PooledRefList<IEntity>.Create(64);
foreach (var e in GetMovingEntities(true))
{
list.Add(e);
}
Span<byte> moveBoatPacket = stackalloc byte[BoatPackets.GetMoveBoatHSPacketLength(list.Count)]
.InitializePacket();
// Packet must be sent before actual locations are changed
foreach (var ns in Map.GetClientsInRange(Location, GetMaxUpdateRange()))
@ -1803,47 +1811,69 @@ namespace Server.Multis
if (ns.HighSeas && m.CanSee(this) && m.InRange(Location, GetUpdateRange(m)))
{
ns.SendMoveBoatHS(m, this, d, clientSpeed, eable, xOffset, yOffset);
eable.Reset();
ns.SendMoveBoatHSUsingCache(moveBoatPacket, this, list, d, clientSpeed, xOffset, yOffset);
}
}
foreach (var e in eable)
for (var i = 0; i < list.Count; i++)
{
var e = list[i];
if (e is Item item)
{
item.NoMoveHS = true;
if (item is not (Server.Items.TillerMan or Server.Items.Hold or Plank))
// We want to enable smooth movement for boat parts, but they are ACTUALLY moved when the boat moves
if (item is not Server.Items.TillerMan and not Server.Items.Hold and not Plank)
{
item.NoMoveHS = true;
item.Location = new Point3D(item.X + xOffset, item.Y + yOffset, item.Z);
item.NoMoveHS = false;
}
}
else if (e is Mobile m)
{
m.NoMoveHS = true;
m.Location = new Point3D(m.X + xOffset, m.Y + yOffset, m.Z);
m.NoMoveHS = false;
}
}
if (TillerMan != null)
{
TillerMan.NoMoveHS = true;
}
if (SPlank != null)
{
SPlank.NoMoveHS = true;
}
if (PPlank != null)
{
PPlank.NoMoveHS = true;
}
if (Hold != null)
{
Hold.NoMoveHS = true;
}
NoMoveHS = true;
Location = new Point3D(X + xOffset, Y + yOffset, Z);
eable.Reset();
foreach (var e in eable)
{
if (e is Item item)
{
item.NoMoveHS = false;
}
else if (e is Mobile mobile)
{
mobile.NoMoveHS = false;
}
}
NoMoveHS = false;
if (TillerMan != null)
{
TillerMan.NoMoveHS = false;
}
if (SPlank != null)
{
SPlank.NoMoveHS = false;
}
if (PPlank != null)
{
PPlank.NoMoveHS = false;
}
if (Hold != null)
{
Hold.NoMoveHS = false;
}
}
return true;
@ -1851,8 +1881,16 @@ namespace Server.Multis
public void Teleport(int xOffset, int yOffset, int zOffset)
{
using var queue = PooledRefQueue<IEntity>.Create(64);
foreach (var e in GetMovingEntities())
{
queue.Enqueue(e);
}
while (queue.Count > 0)
{
var e = queue.Dequeue();
if (e is Item item)
{
item.Location = new Point3D(item.X + xOffset, item.Y + yOffset, item.Z + zOffset);
@ -1872,93 +1910,95 @@ namespace Server.Multis
if (map == null || map == Map.Internal)
{
return new MovingEntitiesEnumerable(this, includeBoat, null);
return new MovingEntitiesEnumerable(this, includeBoat, Rectangle2D.Empty);
}
var mcl = Components;
var eable = map.GetObjectsInBounds(new Rectangle2D(X + mcl.Min.X, Y + mcl.Min.Y, mcl.Width, mcl.Height));
return new MovingEntitiesEnumerable(this, includeBoat, eable);
var rect = new Rectangle2D(X + mcl.Min.X, Y + mcl.Min.Y, mcl.Width, mcl.Height);
return new MovingEntitiesEnumerable(this, includeBoat, rect);
}
public ref struct MovingEntitiesEnumerable
{
private readonly IPooledEnumerable<IEntity> _entities;
private readonly IEnumerator<IEntity> _enumerator;
private readonly bool _includeBoat;
private BaseBoat _boat;
private readonly BaseBoat _boat;
private readonly Rectangle2D _bounds;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MovingEntitiesEnumerable(BaseBoat boat, bool includeBoat, IPooledEnumerable<IEntity> entities)
public MovingEntitiesEnumerable(BaseBoat boat, bool includeBoat, Rectangle2D bounds)
{
_entities = entities;
_enumerator = entities?.GetEnumerator();
_bounds = bounds;
_boat = boat;
_includeBoat = includeBoat;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MovingEntitiesEnumerator GetEnumerator() => new(_boat, _includeBoat, _enumerator);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
_entities?.Dispose();
_enumerator?.Dispose();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
_enumerator?.Reset();
}
public MovingEntitiesEnumerator GetEnumerator() => new(_boat, _includeBoat, _bounds);
}
public ref struct MovingEntitiesEnumerator
{
private readonly IEnumerator<IEntity> _enumerator;
private IEntity? _current;
private readonly bool _includeBoat;
private readonly BaseBoat _boat;
private bool _iterateMobiles;
private bool _iterateItems;
private Map.MobileEnumerator<Mobile> _mobiles;
private Map.ItemEnumerator<Item> _items;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MovingEntitiesEnumerator(BaseBoat boat, bool includeBoat, IEnumerator<IEntity> enumerator = null)
public MovingEntitiesEnumerator(BaseBoat boat, bool includeBoat, Rectangle2D bounds)
{
_enumerator = enumerator;
_current = default;
_includeBoat = includeBoat;
_boat = boat;
_mobiles = boat.Map.GetMobilesInBounds(bounds).GetEnumerator();
_items = boat.Map.GetItemsInBounds(bounds).GetEnumerator();
_iterateMobiles = true;
_iterateItems = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
IEntity current;
while (_enumerator?.MoveNext() == true)
if (_iterateMobiles)
{
current = _enumerator.Current;
// Skip the boat, effects, spawners, or parts of the boat
if (current == _boat || current is EffectItem or BaseSpawner ||
!_includeBoat && current is Server.Items.TillerMan or Server.Items.Hold or Plank)
while (_mobiles.MoveNext())
{
continue;
}
if (current is Item item)
{
// TODO: Remove visible check and use something better, like check for spawners, or other things in the ocean we shouldn't pick up on accident
if (_boat!.Contains(item) && item.Visible && item.Z >= _boat.Z)
var current = _mobiles.Current;
if (_boat.Contains(current))
{
_current = current;
return true;
}
}
else if (current is Mobile m && _boat!.Contains(m))
_iterateMobiles = false;
}
if (_iterateItems)
{
while (_items.MoveNext())
{
_current = current;
return true;
var current = _items.Current;
// Skip the boat, effects, spawners, or parts of the boat
if (current == _boat || current is EffectItem or BaseSpawner ||
!_includeBoat && (current == _boat.TillerMan || current == _boat.SPlank || current == _boat.PPlank || current == _boat.Hold))
{
continue;
}
// TODO: Remove visible check and use something better, like check for spawners, or other things in the ocean we shouldn't pick up on accident
if (_boat!.Contains(current) && current.Visible && current.Z >= _boat.Z)
{
_current = current;
return true;
}
}
_iterateItems = false;
}
return false;

View file

@ -16,87 +16,110 @@
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using Server.Collections;
using Server.Network;
namespace Server.Multis.Boats
namespace Server.Multis.Boats;
public static class BoatPackets
{
public static class BoatPackets
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetMoveBoatHSPacketLength(int entityCount) => 18 + Math.Min(entityCount, 0xFFFF) * 10;
private static void CreateMoveBoatHS(
Span<byte> buffer, BaseBoat boat, PooledRefList<IEntity> entities,
Direction d, int speed, int xOffset, int yOffset
)
{
public static void SendMoveBoatHS(this NetState ns, Mobile beholder, BaseBoat boat,
Direction d, int speed, BaseBoat.MovingEntitiesEnumerable ents, int xOffset, int yOffset)
// Already initialized, so we don't have to create it again.
if (buffer[0] != 0)
{
if (ns?.HighSeas != true)
{
return;
}
const int minLength = 68; // 18 + 5 * 10
var writer = new SpanWriter(stackalloc byte[minLength], true);
writer.Write((byte)0xF6); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write(boat.Serial);
writer.Write((byte)speed);
writer.Write((byte)d);
writer.Write((byte)boat.Facing);
writer.Write((short)(boat.X + xOffset));
writer.Write((short)(boat.Y + yOffset));
writer.Write((short)boat.Z);
writer.Seek(2, SeekOrigin.Current); // count
var count = 0;
foreach (var ent in ents)
{
// If we assume that the entities list contains everything a player can see,
// then this can be removed and the packet can be written once and copied to improve performance
if (!beholder.CanSee(ent))
{
continue;
}
writer.Write(ent.Serial);
writer.Write((short)(ent.X + xOffset));
writer.Write((short)(ent.Y + yOffset));
writer.Write((short)ent.Z);
++count;
}
writer.Seek(16, SeekOrigin.Begin);
writer.Write((short)count);
writer.WritePacketLength();
ns.Send(writer.Span);
return;
}
public static void SendDisplayBoatHS(this NetState ns, Mobile beholder, BaseBoat boat)
var count = Math.Min(entities.Count, 0xFFFF);
var writer = new SpanWriter(buffer);
writer.Write((byte)0xF6); // Packet ID
writer.Seek(2, SeekOrigin.Current); // Length
writer.Write(boat.Serial);
writer.Write((byte)speed);
writer.Write((byte)d);
writer.Write((byte)boat.Facing);
writer.Write((short)(boat.X + xOffset));
writer.Write((short)(boat.Y + yOffset));
writer.Write((short)boat.Z);
writer.Write((short)count);
for (var i = 0; i < count; i++)
{
if (ns?.HighSeas != true)
{
return;
}
var ent = entities[i];
var minLength = PacketContainerBuilder.MinPacketLength
+ OutgoingEntityPackets.MaxWorldEntityPacketLength
* 5; // Minimum of boat, hold, planks, and the player
using var builder = new PacketContainerBuilder(stackalloc byte[minLength]);
Span<byte> buffer = builder.GetSpan(OutgoingEntityPackets.MaxWorldEntityPacketLength);
foreach (var entity in boat.GetMovingEntities(true))
{
if (!beholder.CanSee(entity))
{
continue;
}
buffer.InitializePacket();
var bytesWritten = OutgoingEntityPackets.CreateWorldEntity(buffer, entity, true);
builder.Advance(bytesWritten);
}
ns.Send(builder.Finalize());
writer.Write(ent.Serial);
writer.Write((short)(ent.X + xOffset));
writer.Write((short)(ent.Y + yOffset));
writer.Write((short)ent.Z);
}
writer.WritePacketLength();
}
public static void SendMoveBoatHS(this NetState ns, BaseBoat boat,
PooledRefList<IEntity> entities, Direction d, int speed, int xOffset, int yOffset)
{
if (ns.CannotSendPackets())
{
return;
}
Span<byte> moveBoatPacket = stackalloc byte[GetMoveBoatHSPacketLength(entities.Count)]
.InitializePacket();
CreateMoveBoatHS(moveBoatPacket, boat, entities, d, speed, xOffset, yOffset);
ns.Send(moveBoatPacket);
}
public static void SendMoveBoatHSUsingCache(this NetState ns, Span<byte> cache, BaseBoat boat,
PooledRefList<IEntity> entities, Direction d, int speed, int xOffset, int yOffset)
{
if (ns.CannotSendPackets())
{
return;
}
CreateMoveBoatHS(cache, boat, entities, d, speed, xOffset, yOffset);
ns.Send(cache);
}
public static void SendDisplayBoatHS(this NetState ns, Mobile beholder, BaseBoat boat)
{
if (ns?.HighSeas != true || ns.CannotSendPackets())
{
return;
}
const int minLength = PacketContainerBuilder.MinPacketLength
+ OutgoingEntityPackets.MaxWorldEntityPacketLength
* 5; // Minimum of boat, hold, planks, and the player
using var builder = new PacketContainerBuilder(stackalloc byte[minLength]);
Span<byte> buffer = builder.GetSpan(OutgoingEntityPackets.MaxWorldEntityPacketLength);
foreach (var entity in boat.GetMovingEntities(true))
{
if (!beholder.CanSee(entity))
{
continue;
}
buffer.InitializePacket();
var bytesWritten = OutgoingEntityPackets.CreateWorldEntity(buffer, entity, true);
builder.Advance(bytesWritten);
}
ns.Send(builder.Finalize());
}
}

View file

@ -231,17 +231,20 @@ namespace Server.Items
return false;
}
var eable = GetObjectsInRange(0);
foreach (var obj in eable)
foreach (var item in GetItemsAt())
{
if (obj == this)
if (item != this)
{
return true;
return false;
}
}
return false;
foreach (var m in GetMobilesAt())
{
return false;
}
return true;
}
public void Close()

View file

@ -751,29 +751,47 @@ namespace Server.Multis
vendor = false;
rentalContract = false;
var eable = map.GetObjectsInRange(location, 0);
foreach (var entity in eable)
foreach (var m in map.GetMobilesAt(location))
{
if ((location.Z - entity.Z).Abs() <= 16)
if ((location.Z - m.Z).Abs() <= 16 && m is PlayerVendor or PlayerBarkeeper)
{
if (entity is PlayerVendor or PlayerBarkeeper or PlayerVendorPlaceholder)
vendor = true;
return;
}
}
foreach (var item in map.GetItemsAt(location))
{
if ((location.Z - item.Z).Abs() <= 16)
{
if (item is PlayerVendorPlaceholder)
{
vendor = true;
break;
return;
}
if (entity is VendorRentalContract)
if (item is VendorRentalContract)
{
rentalContract = true;
break;
return;
}
}
}
}
public List<Mobile> AvailableVendorsFor(Mobile m) =>
PlayerVendors.Where(vendor => vendor.CanInteractWith(m, false)).ToList<Mobile>();
public List<Mobile> AvailableVendorsFor(Mobile m)
{
List<Mobile> list = new List<Mobile>();
foreach (PlayerVendor vendor in PlayerVendors)
{
if (vendor.CanInteractWith(m, false))
{
list.Add(vendor);
}
}
return list;
}
public bool AreThereAvailableVendorsFor(Mobile m)
{

View file

@ -27,7 +27,7 @@ namespace Server.Network
public static void SendBatchEntities(this NetState ns, IEnumerable<IEntity> entities, int estimatedCount)
{
if (ns?.HighSeas != true)
if (ns?.HighSeas != true || ns.CannotSendPackets())
{
return;
}

View file

@ -1,4 +1,4 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.10.1"
"version": "0.10.2"
}