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:
parent
cb1638591b
commit
977fdc2c5a
21 changed files with 543 additions and 461 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue