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

@ -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)
{