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

@ -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());
}
}