fix: Cleans up mobile status packets (#835)

* Removes Span2D for mobile moving. Instead uses pure math and simplifies the calculation.
* Cleans up the extended mobile status packet.
This commit is contained in:
Kamron Batman 2022-02-27 03:10:35 -08:00 • committed by GitHub
parent e873d2ed7e
commit 651cffa872
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 47 deletions

View file

@ -17,7 +17,6 @@ using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance;
namespace Server.Network;
@ -26,7 +25,7 @@ public static class OutgoingMobilePackets
public const int BondedStatusPacketLength = 11;
public const int DeathAnimationPacketLength = 13;
public const int MobileMovingPacketLength = 17;
public const int MobileMovingPacketCacheHeight = 16; // 8 notoriety, 2 client versions
public const int MobileMovingPacketCacheHeight = 7 * 2; // 7 notoriety, 2 client versions
public const int MobileMovingPacketCacheByteLength = MobileMovingPacketLength * MobileMovingPacketCacheHeight;
public const int AttributeMaximum = 100;
public const int MobileAttributePacketLength = 9;
@ -35,13 +34,16 @@ public static class OutgoingMobilePackets
public const int NewMobileAnimationPacketLength = 10;
public const int MobileHealthbarPacketLength = 12;
public const int MobileStatusCompactLength = 43;
public const int MobileStatusMaxLength = 121;
public const int MobileStatusLength = 70;
public const int MobileStatusAOSLength = 88;
public const int MobileStatusMLLength = 91;
public const int MobileStatusHSLength = 121;
public static bool ExtendedStatus { get; set; }
public static bool ExtendedStatus { get; private set; } = true;
public static void Initialize()
public static void Configure()
{
ExtendedStatus = ServerConfiguration.GetOrUpdateSetting("extendedStatus", false);
ExtendedStatus = ServerConfiguration.GetSetting("client.showExtendedStatus", true);
}
public static void CreateBondedStatus(Span<byte> buffer, Serial serial, bool bonded)
@ -144,12 +146,12 @@ public static class OutgoingMobilePackets
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile source, Mobile target) =>
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> cache, Mobile source, Mobile target) =>
ns.SendMobileMovingUsingCache(cache, target, Notoriety.Compute(source, target));
// Requires a buffer of 16 packets, 17bytes per packet (272 bytes).
// Requires cache to have the first byte of each packet zeroed.
public static void SendMobileMovingUsingCache(this NetState ns, Span2D<byte> cache, Mobile target, int noto)
// Requires a buffer of 14 packets, 17 bytes per packet (238 bytes).
// Requires cache to have the first byte of each packet initially zeroed.
public static void SendMobileMovingUsingCache(this NetState ns, Span<byte> cache, Mobile target, int noto)
{
if (ns.CannotSendPackets())
{
@ -157,8 +159,9 @@ public static class OutgoingMobilePackets
}
var stygianAbyss = ns.StygianAbyss;
var row = noto * 2 + (stygianAbyss ? 1 : 0);
var buffer = cache.GetRowSpan(row);
// Indexes 0-6 for pre-SA, and 7-13 for SA
var row = noto + (stygianAbyss ? 6 : -1);
var buffer = cache.Slice(row * MobileMovingPacketLength, MobileMovingPacketLength);
CreateMobileMoving(buffer, target, noto, stygianAbyss);
ns.Send(buffer);
@ -431,7 +434,7 @@ public static class OutgoingMobilePackets
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CreateMobileStatusCompact(Span<byte> buffer, Mobile m, bool canBeRenamed) =>
CreateMobileStatus(buffer, null, m, 0, canBeRenamed);
CreateMobileStatus(buffer, m, 0, canBeRenamed);
public static void SendMobileStatusCompact(this NetState ns, Mobile m, bool canBeRenamed)
{
@ -451,46 +454,50 @@ public static class OutgoingMobilePackets
public static void SendMobileStatus(this NetState ns, Mobile beholder, Mobile beheld)
{
if (ns == null || beheld == null)
if (ns.CannotSendPackets() || beheld == null)
{
return;
}
Span<byte> buffer = stackalloc byte[MobileStatusMaxLength];
int version;
int length;
if (beholder != beheld)
{
version = 0;
length = MobileStatusCompactLength;
}
else if (Core.HS && ns.ExtendedStatus)
else if (ExtendedStatus && ns.ExtendedStatus)
{
version = 6;
length = MobileStatusHSLength;
}
else if (Core.ML && ns.SupportsExpansion(Expansion.ML))
{
/*
* For the ML era, the version value must be 5 if the original UO distribution
* is used and the client is not lower than version 5
*/
version = ExtendedStatus ? 6 : 5;
version = 5;
length = MobileStatusMLLength;
}
else if (Core.AOS)
{
version = 4;
length = MobileStatusAOSLength;
}
else
{
version = Core.AOS ? 4 : 3;
version = 3;
length = MobileStatusLength;
}
var length = CreateMobileStatus(buffer, beholder, beheld, version, beheld.CanBeRenamedBy(beholder));
ns.Send(buffer[..length]);
Span<byte> buffer = stackalloc byte[length];
CreateMobileStatus(buffer, beheld, version, beheld.CanBeRenamedBy(beholder));
ns.Send(buffer);
}
public static int CreateMobileStatus(
Span<byte> buffer, Mobile beholder, Mobile beheld, int version, bool canBeRenamed
)
public static void CreateMobileStatus(Span<byte> buffer, Mobile beheld, int version, bool canBeRenamed)
{
if (buffer[0] != 0)
{
return buffer.Length;
return;
}
var name = beheld.Name ?? "";
@ -507,7 +514,7 @@ public static class OutgoingMobilePackets
if (version <= 0)
{
writer.WritePacketLength();
return writer.Position;
return;
}
writer.Write(beheld.Female);
@ -563,7 +570,6 @@ public static class OutgoingMobilePackets
}
writer.WritePacketLength();
return writer.Position;
}
public static void SendMobileUpdate(this NetState ns, Mobile m)