fix(core): Converts mobile status packets (#368)

- [X] Combines compact/extended statuses
This commit is contained in:
Kamron Batman 2020-12-28 01:13:02 -08:00 committed by GitHub
parent 8f8b650ab5
commit 3d98e6e193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 621 additions and 537 deletions

View file

@ -324,16 +324,16 @@ namespace Server.Network
state.Send(new MobileUpdate(m, state.StygianAbyss));
state.Send(new MobileIncoming(state, m, m));
// state.Send( new MobileAttributes( m ) );
state.Send(new MobileStatus(m, m));
state.SendMobileStatus(m);
state.SendSetWarMode(m.Warmode);
m.SendEverything();
state.SendSupportedFeature();
state.Send(new MobileUpdate(m, state.StygianAbyss));
// state.Send( new MobileAttributes( m ) );
state.Send(new MobileStatus(m, m));
state.SendMobileStatus(m);
state.SendSetWarMode(m.Warmode);
state.Send(new MobileIncoming(state, m, m));

View file

@ -17,248 +17,6 @@ using System.Threading;
namespace Server.Network
{
public sealed class MobileStatusCompact : Packet
{
public MobileStatusCompact(bool canBeRenamed, Mobile m) : base(0x11)
{
EnsureCapacity(43);
Stream.Write(m.Serial);
Stream.WriteAsciiFixed(m.Name ?? "", 30);
AttributeNormalizer.WriteReverse(Stream, m.Hits, m.HitsMax);
Stream.Write(canBeRenamed);
Stream.Write((byte)0); // type
}
}
public sealed class MobileStatusExtended : Packet
{
public MobileStatusExtended(Mobile m) : this(m, m.NetState)
{
}
public MobileStatusExtended(Mobile m, NetState ns) : base(0x11)
{
var name = m.Name ?? "";
int type;
if (Core.HS && ns?.ExtendedStatus == true)
{
type = 6;
EnsureCapacity(121);
}
else if (Core.ML && ns?.SupportsExpansion(Expansion.ML) == true)
{
type = 5;
EnsureCapacity(91);
}
else
{
type = Core.AOS ? 4 : 3;
EnsureCapacity(88);
}
Stream.Write(m.Serial);
Stream.WriteAsciiFixed(name, 30);
Stream.Write((short)m.Hits);
Stream.Write((short)m.HitsMax);
Stream.Write(m.CanBeRenamedBy(m));
Stream.Write((byte)type);
Stream.Write(m.Female);
Stream.Write((short)m.Str);
Stream.Write((short)m.Dex);
Stream.Write((short)m.Int);
Stream.Write((short)m.Stam);
Stream.Write((short)m.StamMax);
Stream.Write((short)m.Mana);
Stream.Write((short)m.ManaMax);
Stream.Write(m.TotalGold);
Stream.Write((short)(Core.AOS ? m.PhysicalResistance : (int)(m.ArmorRating + 0.5)));
Stream.Write((short)(Mobile.BodyWeight + m.TotalWeight));
if (type >= 5)
{
Stream.Write((short)m.MaxWeight);
Stream.Write((byte)(m.Race.RaceID + 1)); // Would be 0x00 if it's a non-ML enabled account but...
}
Stream.Write((short)m.StatCap);
Stream.Write((byte)m.Followers);
Stream.Write((byte)m.FollowersMax);
if (type >= 4)
{
Stream.Write((short)m.FireResistance); // Fire
Stream.Write((short)m.ColdResistance); // Cold
Stream.Write((short)m.PoisonResistance); // Poison
Stream.Write((short)m.EnergyResistance); // Energy
Stream.Write((short)m.Luck); // Luck
var weapon = m.Weapon;
if (weapon != null)
{
weapon.GetStatusDamage(m, out var min, out var max);
Stream.Write((short)min); // Damage min
Stream.Write((short)max); // Damage max
}
else
{
Stream.Write((short)0); // Damage min
Stream.Write((short)0); // Damage max
}
Stream.Write(m.TithingPoints);
}
if (type >= 6)
{
for (var i = 0; i < 15; ++i)
{
Stream.Write((short)m.GetAOSStatus(i));
}
}
}
}
public sealed class MobileStatus : Packet
{
public MobileStatus(Mobile beholder, Mobile beheld) : this(beholder, beheld, beheld.NetState)
{
}
public MobileStatus(Mobile beholder, Mobile beheld, NetState ns) : base(0x11)
{
var name = beheld.Name ?? "";
int type;
if (beholder != beheld)
{
type = 0;
EnsureCapacity(43);
}
else if (Core.HS && ns?.ExtendedStatus == true)
{
type = 6;
EnsureCapacity(121);
}
else if (Core.ML && ns?.SupportsExpansion(Expansion.ML) == true)
{
type = 5;
EnsureCapacity(91);
}
else
{
type = Core.AOS ? 4 : 3;
EnsureCapacity(88);
}
Stream.Write(beheld.Serial);
Stream.WriteAsciiFixed(name, 30);
if (beholder == beheld)
{
WriteAttr(beheld.Hits, beheld.HitsMax);
}
else
{
WriteAttrNorm(beheld.Hits, beheld.HitsMax);
}
Stream.Write(beheld.CanBeRenamedBy(beholder));
Stream.Write((byte)type);
if (type <= 0)
{
return;
}
Stream.Write(beheld.Female);
Stream.Write((short)beheld.Str);
Stream.Write((short)beheld.Dex);
Stream.Write((short)beheld.Int);
WriteAttr(beheld.Stam, beheld.StamMax);
WriteAttr(beheld.Mana, beheld.ManaMax);
Stream.Write(beheld.TotalGold);
Stream.Write((short)(Core.AOS ? beheld.PhysicalResistance : (int)(beheld.ArmorRating + 0.5)));
Stream.Write((short)(Mobile.BodyWeight + beheld.TotalWeight));
if (type >= 5)
{
Stream.Write((short)beheld.MaxWeight);
Stream.Write((byte)(beheld.Race.RaceID + 1)); // Would be 0x00 if it's a non-ML enabled account but...
}
Stream.Write((short)beheld.StatCap);
Stream.Write((byte)beheld.Followers);
Stream.Write((byte)beheld.FollowersMax);
if (type >= 4)
{
Stream.Write((short)beheld.FireResistance); // Fire
Stream.Write((short)beheld.ColdResistance); // Cold
Stream.Write((short)beheld.PoisonResistance); // Poison
Stream.Write((short)beheld.EnergyResistance); // Energy
Stream.Write((short)beheld.Luck); // Luck
var weapon = beheld.Weapon;
if (weapon != null)
{
weapon.GetStatusDamage(beheld, out var min, out var max);
Stream.Write((short)min); // Damage min
Stream.Write((short)max); // Damage max
}
else
{
Stream.Write((short)0); // Damage min
Stream.Write((short)0); // Damage max
}
Stream.Write(beheld.TithingPoints);
}
if (type >= 6)
{
for (var i = 0; i < 15; ++i)
{
Stream.Write((short)beheld.GetAOSStatus(i));
}
}
}
private void WriteAttr(int current, int maximum)
{
Stream.Write((short)current);
Stream.Write((short)maximum);
}
private void WriteAttrNorm(int current, int maximum)
{
AttributeNormalizer.WriteReverse(Stream, current, maximum);
}
}
public sealed class MobileUpdate : Packet
{
public MobileUpdate(Mobile m, bool stygianAbyss) : base(0x20, 19)

View file

@ -15,6 +15,7 @@
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
namespace Server.Network
@ -31,6 +32,8 @@ namespace Server.Network
public const int MobileAnimationPacketLength = 14;
public const int NewMobileAnimationPacketLength = 10;
public const int MobileHealthbarPacketLength = 12;
public const int MobileStatusCompactLength = 43;
public const int MobileStatusMaxLength = 121;
public static void CreateBondedStatus(Span<byte> buffer, Serial serial, bool bonded)
{
@ -143,7 +146,9 @@ namespace Server.Network
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteAttribute(this ref SpanWriter writer, int max, int cur, bool normalize = false, bool reverse = false)
private static void WriteAttribute(
this ref SpanWriter writer, int max, int cur, bool normalize = false, bool reverse = false
)
{
if (normalize && max != 0)
{
@ -374,5 +379,139 @@ namespace Server.Network
writer.Write((short)healthbar);
writer.Write((byte)level); // 0 is off for that bar type
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CreateMobileStatusCompact(Span<byte> buffer, Mobile m, bool canBeRenamed) =>
CreateMobileStatus(buffer, null, m, 0, canBeRenamed);
public static void SendMobileStatusCompact(this NetState ns, Mobile m, bool canBeRenamed)
{
if (ns == null)
{
return;
}
Span<byte> buffer = stackalloc byte[MobileStatusCompactLength];
CreateMobileStatusCompact(buffer, m, canBeRenamed);
ns.Send(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendMobileStatus(this NetState ns, Mobile m) => ns.SendMobileStatus(m, m);
public static void SendMobileStatus(this NetState ns, Mobile beholder, Mobile beheld)
{
if (ns == null || beheld == null)
{
return;
}
Span<byte> buffer = stackalloc byte[MobileStatusMaxLength];
int version;
if (beholder != beheld)
{
version = 0;
}
else if (Core.HS && ns.ExtendedStatus)
{
version = 6;
}
else if (Core.ML && ns.SupportsExpansion(Expansion.ML))
{
version = 5;
}
else
{
version = Core.AOS ? 4 : 3;
}
var length = CreateMobileStatus(buffer, beholder, beheld, version, beheld.CanBeRenamedBy(beholder));
ns.Send(buffer.Slice(0, length));
}
public static int CreateMobileStatus(
Span<byte> buffer, Mobile beholder, Mobile beheld, int version, bool canBeRenamed
)
{
var name = beheld.Name ?? "";
var writer = new SpanWriter(buffer);
writer.Write((byte)0x11); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write(beheld.Serial);
writer.WriteAscii(name, 30);
writer.WriteAttribute(beheld.HitsMax, beheld.Hits, version == 0, true);
writer.Write(canBeRenamed);
writer.Write((byte)version);
if (version <= 0)
{
writer.WritePacketLength();
return writer.Position;
}
writer.Write(beheld.Female);
writer.Write((short)beheld.Str);
writer.Write((short)beheld.Dex);
writer.Write((short)beheld.Int);
writer.Write((short)beheld.Stam);
writer.Write((short)beheld.StamMax);
writer.Write((short)beheld.Mana);
writer.Write((short)beheld.ManaMax);
writer.Write(beheld.TotalGold);
writer.Write((short)(Core.AOS ? beheld.PhysicalResistance : (int)(beheld.ArmorRating + 0.5)));
writer.Write((short)(Mobile.BodyWeight + beheld.TotalWeight));
if (version >= 5)
{
writer.Write((short)beheld.MaxWeight);
writer.Write((byte)(beheld.Race?.RaceID + 1 ?? 0)); // Would be 0x00 if it's a non-ML enabled account but...
}
writer.Write((short)beheld.StatCap);
writer.Write((byte)beheld.Followers);
writer.Write((byte)beheld.FollowersMax);
if (version >= 4)
{
writer.Write((short)beheld.FireResistance); // Fire
writer.Write((short)beheld.ColdResistance); // Cold
writer.Write((short)beheld.PoisonResistance); // Poison
writer.Write((short)beheld.EnergyResistance); // Energy
writer.Write((short)beheld.Luck); // Luck
var weapon = beheld.Weapon;
int min = 0, max = 0;
weapon?.GetStatusDamage(beheld, out min, out max);
writer.Write((short)min); // Damage min
writer.Write((short)max); // Damage max
writer.Write(beheld.TithingPoints);
}
if (version >= 6)
{
for (var i = 0; i < 15; ++i)
{
writer.Write((short)beheld.GetAOSStatus(i));
}
}
writer.WritePacketLength();
return writer.Position;
}
}
}