fix(core): Converts mobile moving packet (#357)

- [X] Converts mobile moving packet
This commit is contained in:
Kamron Batman 2020-12-24 17:13:55 -08:00 committed by GitHub
parent c2143584bc
commit 10a6e17640
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 170 deletions

View file

@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
@ -38,5 +39,17 @@ namespace Server.Network
writer.Write((ushort)length);
writer.Seek(length, SeekOrigin.Begin);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitializePackets(this Span<byte> buffer, int chunkLength)
{
var index = 0;
while (index < buffer.Length)
{
buffer[index] = 0;
index += chunkLength;
}
}
}
}

View file

@ -17,41 +17,6 @@ using System.Threading;
namespace Server.Network
{
public sealed class DeathAnimation : Packet
{
public DeathAnimation(Serial killed, Serial corpse) : base(0xAF, 13)
{
Stream.Write(killed);
Stream.Write(corpse);
Stream.Write(0);
}
}
public sealed class MobileMoving : Packet
{
public MobileMoving(Mobile m, int noto, bool stygianAbyss) : base(0x77, 17)
{
var loc = m.Location;
var hue = m.Hue;
if (m.SolidHueOverride >= 0)
{
hue = m.SolidHueOverride;
}
Stream.Write(m.Serial);
Stream.Write((short)m.Body);
Stream.Write((short)loc.m_X);
Stream.Write((short)loc.m_Y);
Stream.Write((sbyte)loc.m_Z);
Stream.Write((byte)m.Direction);
Stream.Write((short)hue);
Stream.Write((byte)m.GetPacketFlags(stygianAbyss));
Stream.Write((byte)noto);
}
}
public sealed class MobileHits : Packet
{
public MobileHits(Mobile m) : base(0xA1, 9)

View file

@ -15,6 +15,7 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
namespace Server.Network
{
@ -22,6 +23,8 @@ namespace Server.Network
{
public const int BondedStatusPacketLength = 11;
public const int DeathAnimationPacketLength = 13;
public const int MobileMovingPacketLength = 17;
public const int MobileMovingPacketCacheLength = MobileMovingPacketLength * 8 * 2; // 8 notoriety, 2 client versions
public static void CreateBondedStatus(ref Span<byte> buffer, Serial serial, bool bonded)
{
@ -72,5 +75,65 @@ namespace Server.Network
CreateDeathAnimation(ref span, killed, corpse);
ns.Send(span);
}
public static void CreateMobileMoving(ref Span<byte> buffer, Mobile m, int noto, bool stygianAbyss)
{
var loc = m.Location;
var hue = m.SolidHueOverride >= 0 ? m.SolidHueOverride : m.Hue;
var writer = new SpanWriter(buffer);
writer.Write((byte)0x77); // Packet ID
writer.Write(m.Serial);
writer.Write((short)m.Body);
writer.Write((short)loc.m_X);
writer.Write((short)loc.m_Y);
writer.Write((sbyte)loc.m_Z);
writer.Write((byte)m.Direction);
writer.Write((short)hue);
writer.Write((byte)m.GetPacketFlags(stygianAbyss));
writer.Write((byte)noto);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendMobileMoving(this NetState ns, Mobile source, Mobile target) =>
ns.SendMobileMoving(target, Notoriety.Compute(source, target));
public static void SendMobileMoving(this NetState ns, Mobile target, int noto)
{
if (ns == null)
{
return;
}
Span<byte> span = stackalloc byte[MobileMovingPacketLength];
CreateMobileMoving(ref span, target, noto, ns.StygianAbyss);
ns.Send(span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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, Span<byte> cache, Mobile target, int noto)
{
if (ns == null)
{
return;
}
var stygianAbyss = ns.StygianAbyss;
var startIndex = (noto * 2 + (stygianAbyss ? 1 : 0)) * MobileMovingPacketLength;
var buffer = cache.Slice(startIndex, MobileMovingPacketLength);
// Packet not created yet
if (buffer[0] == 0)
{
CreateMobileMoving(ref buffer, target, noto, stygianAbyss);
}
ns.Send(buffer);
}
}
}