fix(core): Converts healthbar packets (#367)

- [X] Converts healthbar packets
This commit is contained in:
Kamron Batman 2020-12-27 23:22:21 -08:00 • committed by GitHub
parent 26243a72a7
commit 8f8b650ab5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 174 additions and 166 deletions

View file

@ -133,6 +133,47 @@ namespace Server.Tests.Network
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected); AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
} }
[Theory]
[InlineData("None")]
[InlineData("Lesser")]
[InlineData("Lethal")]
public void TestHealthbarPoison(string pName)
{
var p = Poison.GetPoison(pName);
var m = new Mobile(0x1);
m.DefaultMobileInit();
m.Poison = p;
var expected = new HealthbarPoison(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Poison);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public void TestYellowBar(bool isBlessed, bool isYellowHealth)
{
var m = new Mobile(0x1);
m.DefaultMobileInit();
m.Blessed = isBlessed;
m.YellowHealthbar = isYellowHealth;
var expected = new HealthbarYellow(m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileHealthbar(m, Healthbar.Yellow);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact] [Fact]
public void TestMobileStatusCompact() public void TestMobileStatusCompact()
{ {
@ -279,51 +320,6 @@ namespace Server.Tests.Network
AssertThat.Equal(data, expectedData); AssertThat.Equal(data, expectedData);
} }
[Theory, InlineData("None"), InlineData("Lesser"), InlineData("Lethal")]
public void TestHealthbarPoison(string pName)
{
var p = Poison.GetPoison(pName);
var m = new Mobile(0x1);
m.DefaultMobileInit();
m.Poison = p;
var data = new HealthbarPoison(m).Compile();
Span<byte> expectedData = stackalloc byte[12];
var pos = 0;
expectedData.Write(ref pos, (byte)0x17); // Packet ID
expectedData.Write(ref pos, (ushort)12); // Length
expectedData.Write(ref pos, m.Serial);
expectedData.Write(ref pos, 0x10001); // Show Bar?, Poison Bar
expectedData.Write(ref pos, (byte)((p?.Level ?? -1) + 1));
AssertThat.Equal(data, expectedData);
Assert.Equal(p?.Level, m.Poison?.Level);
}
[Theory, InlineData(false, false), InlineData(true, false), InlineData(false, true), InlineData(true, true)]
public void TestYellowBar(bool isBlessed, bool isYellowHealth)
{
var m = new Mobile(0x1);
m.DefaultMobileInit();
m.Blessed = isBlessed;
m.YellowHealthbar = isYellowHealth;
var data = new HealthbarYellow(m).Compile();
Span<byte> expectedData = stackalloc byte[12];
var pos = 0;
expectedData.Write(ref pos, (byte)0x17); // Packet ID
expectedData.Write(ref pos, (ushort)12); // Length
expectedData.Write(ref pos, m.Serial);
expectedData.Write(ref pos, 0x10002); // Show Bar?, Yellow Bar
expectedData.Write(ref pos, isBlessed || isYellowHealth);
AssertThat.Equal(data, expectedData);
}
[Fact] [Fact]
public void TestMobileUpdate() public void TestMobileUpdate()
{ {

View file

@ -174,4 +174,50 @@ namespace Server.Tests.Network
Stream.Write((byte)delay); Stream.Write((byte)delay);
} }
} }
public sealed class HealthbarPoison : Packet
{
public HealthbarPoison(Mobile m) : base(0x17)
{
EnsureCapacity(12);
Stream.Write(m.Serial);
Stream.Write((short)1); // Show Bar?
Stream.Write((short)1); // Poison Bar
var p = m.Poison;
if (p != null)
{
Stream.Write((byte)(p.Level + 1));
}
else
{
Stream.Write((byte)0);
}
}
}
public sealed class HealthbarYellow : Packet
{
public HealthbarYellow(Mobile m) : base(0x17)
{
EnsureCapacity(12);
Stream.Write(m.Serial);
Stream.Write((short)1);
Stream.Write((short)2);
if (m.Blessed || m.YellowHealthbar)
{
Stream.Write((byte)1);
}
else
{
Stream.Write((byte)0);
}
}
}
} }

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using Server.Buffers; using Server.Buffers;
namespace Server namespace Server

View file

@ -13,8 +13,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/ *************************************************************************/
using System;
namespace Server namespace Server
{ {
[NoSort, Parsable, PropertyObject] [NoSort, Parsable, PropertyObject]

View file

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
namespace Server.Guilds namespace Server.Guilds

View file

@ -5,7 +5,6 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using Server.Accounting; using Server.Accounting;
using Server.Buffers; using Server.Buffers;
using Server.ContextMenus; using Server.ContextMenus;
@ -345,6 +344,13 @@ namespace Server
Attributes = 0x0000001C Attributes = 0x0000001C
} }
public enum Healthbar
{
Normal,
Poison,
Yellow
}
public enum AccessLevel public enum AccessLevel
{ {
Player, Player,
@ -2978,17 +2984,6 @@ namespace Server
sendIncoming = true; sendIncoming = true;
} }
/*if ((delta & MobileDelta.Hue) != 0)
{
sendNonlocalIncoming = true;
sendUpdate = true;
}
else if ((delta & (MobileDelta.Direction | MobileDelta.Body)) != 0)
{
sendNonlocalMoving = true;
sendUpdate = true;
}
else*/
if ((delta & (MobileDelta.Flags | MobileDelta.Noto)) != 0) if ((delta & (MobileDelta.Flags | MobileDelta.Noto)) != 0)
{ {
sendMoving = true; sendMoving = true;
@ -3066,12 +3061,12 @@ namespace Server
{ {
if (sendHealthbarPoison) if (sendHealthbarPoison)
{ {
ourState.Send(new HealthbarPoison(m)); ourState.SendMobileHealthbar(m, Healthbar.Poison);
} }
if (sendHealthbarYellow) if (sendHealthbarYellow)
{ {
ourState.Send(new HealthbarYellow(m)); ourState.SendMobileHealthbar(m, Healthbar.Yellow);
} }
} }
@ -3164,6 +3159,12 @@ namespace Server
var eable = m.Map.GetClientsInRange(m.m_Location); var eable = m.Map.GetClientsInRange(m.m_Location);
Span<byte> hbpBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength];
hbpBuffer.InitializePacket();
Span<byte> hbyBuffer = stackalloc byte[OutgoingMobilePackets.MobileHealthbarPacketLength];
hbyBuffer.InitializePacket();
Span<byte> deadBuffer = stackalloc byte[OutgoingMobilePackets.BondedStatusPacketLength]; Span<byte> deadBuffer = stackalloc byte[OutgoingMobilePackets.BondedStatusPacketLength];
deadBuffer.InitializePacket(); deadBuffer.InitializePacket();
@ -3216,14 +3217,20 @@ namespace Server
{ {
if (sendHealthbarPoison) if (sendHealthbarPoison)
{ {
hbpPacket ??= Packet.Acquire(new HealthbarPoison(m)); if (hbpBuffer[0] == 0)
{
OutgoingMobilePackets.CreateMobileHealthbar(hbpBuffer, m, Healthbar.Poison);
}
state.Send(hbpPacket); state.Send(hbpPacket);
} }
if (sendHealthbarYellow) if (sendHealthbarYellow)
{ {
hbyPacket ??= Packet.Acquire(new HealthbarYellow(m)); if (hbyBuffer[0] == 0)
{
OutgoingMobilePackets.CreateMobileHealthbar(hbyBuffer, m, Healthbar.Yellow);
}
state.Send(hbyPacket); state.Send(hbyPacket);
} }
@ -7150,15 +7157,8 @@ namespace Server
if (ns.StygianAbyss) if (ns.StygianAbyss)
{ {
if (m.Poisoned) ns.SendMobileHealthbar(m, Healthbar.Poison);
{ ns.SendMobileHealthbar(m, Healthbar.Yellow);
ns.Send(new HealthbarPoison(m));
}
if (m.Blessed || m.YellowHealthbar)
{
ns.Send(new HealthbarYellow(m));
}
} }
if (m.IsDeadBondedPet) if (m.IsDeadBondedPet)
@ -7562,27 +7562,25 @@ namespace Server
} }
var inOldRange = Utility.InUpdateRange(oldLocation, m.m_Location); var inOldRange = Utility.InUpdateRange(oldLocation, m.m_Location);
var ns = m.m_NetState;
if (m.m_NetState != null && if (ns != null &&
(isTeleport && (!m.m_NetState.HighSeas || !NoMoveHS) || !inOldRange) && m.CanSee(this)) (isTeleport && (!ns.HighSeas || !NoMoveHS) || !inOldRange) && m.CanSee(this))
{ {
m.m_NetState.Send(new MobileIncoming(m.m_NetState, m, this)); ns.Send(new MobileIncoming(m.m_NetState, m, this));
if (m.m_NetState.StygianAbyss) if (ns.StygianAbyss)
{ {
// if (m_Poison != null) ns.SendMobileHealthbar(this, Healthbar.Poison);
m.m_NetState.Send(new HealthbarPoison(this)); ns.SendMobileHealthbar(this, Healthbar.Yellow);
// if (m_Blessed || m_YellowHealthbar)
m.m_NetState.Send(new HealthbarYellow(this));
} }
if (IsDeadBondedPet) if (IsDeadBondedPet)
{ {
m.m_NetState.SendBondedStatus(Serial, true); ns.SendBondedStatus(Serial, true);
} }
SendOPLPacketTo(m.m_NetState); SendOPLPacketTo(ns);
} }
if (inOldRange || !CanSee(m)) if (inOldRange || !CanSee(m))
@ -7594,11 +7592,8 @@ namespace Server
if (ourState.StygianAbyss) if (ourState.StygianAbyss)
{ {
// if (m.Poisoned) ourState.SendMobileHealthbar(m, Healthbar.Poison);
ourState.Send(new HealthbarPoison(m)); ourState.SendMobileHealthbar(m, Healthbar.Yellow);
// if (m.Blessed || m.YellowHealthbar)
ourState.Send(new HealthbarYellow(m));
} }
if (m.IsDeadBondedPet) if (m.IsDeadBondedPet)
@ -7626,11 +7621,8 @@ namespace Server
if (ns.StygianAbyss) if (ns.StygianAbyss)
{ {
// if (m_Poison != null) ns.SendMobileHealthbar(this, Healthbar.Poison);
ns.Send(new HealthbarPoison(this)); ns.SendMobileHealthbar(this, Healthbar.Yellow);
// if (m_Blessed || m_YellowHealthbar)
ns.Send(new HealthbarYellow(this));
} }
if (IsDeadBondedPet) if (IsDeadBondedPet)
@ -7707,15 +7699,8 @@ namespace Server
if (state.StygianAbyss) if (state.StygianAbyss)
{ {
if (m_Poison != null) state.SendMobileHealthbar(this, Healthbar.Poison);
{ state.SendMobileHealthbar(this, Healthbar.Yellow);
state.Send(new HealthbarPoison(this));
}
if (m_Blessed || m_YellowHealthbar)
{
state.Send(new HealthbarYellow(this));
}
} }
if (IsDeadBondedPet) if (IsDeadBondedPet)

View file

@ -259,52 +259,6 @@ namespace Server.Network
} }
} }
public sealed class HealthbarPoison : Packet
{
public HealthbarPoison(Mobile m) : base(0x17)
{
EnsureCapacity(12);
Stream.Write(m.Serial);
Stream.Write((short)1); // Show Bar?
Stream.Write((short)1); // Poison Bar
var p = m.Poison;
if (p != null)
{
Stream.Write((byte)(p.Level + 1));
}
else
{
Stream.Write((byte)0);
}
}
}
public sealed class HealthbarYellow : Packet
{
public HealthbarYellow(Mobile m) : base(0x17)
{
EnsureCapacity(12);
Stream.Write(m.Serial);
Stream.Write((short)1);
Stream.Write((short)2);
if (m.Blessed || m.YellowHealthbar)
{
Stream.Write((byte)1);
}
else
{
Stream.Write((byte)0);
}
}
}
public sealed class MobileUpdate : Packet public sealed class MobileUpdate : Packet
{ {
public MobileUpdate(Mobile m, bool stygianAbyss) : base(0x20, 19) public MobileUpdate(Mobile m, bool stygianAbyss) : base(0x20, 19)

View file

@ -30,6 +30,7 @@ namespace Server.Network
public const int MobileAttributesPacketLength = 17; public const int MobileAttributesPacketLength = 17;
public const int MobileAnimationPacketLength = 14; public const int MobileAnimationPacketLength = 14;
public const int NewMobileAnimationPacketLength = 10; public const int NewMobileAnimationPacketLength = 10;
public const int MobileHealthbarPacketLength = 12;
public static void CreateBondedStatus(Span<byte> buffer, Serial serial, bool bonded) public static void CreateBondedStatus(Span<byte> buffer, Serial serial, bool bonded)
{ {
@ -327,5 +328,51 @@ namespace Server.Network
CreateNewMobileAnimation(span, mobile, action, frameCount, delay); CreateNewMobileAnimation(span, mobile, action, frameCount, delay);
ns.Send(span); ns.Send(span);
} }
public static void SendMobileHealthbar(this NetState ns, Mobile m, Healthbar healthbar)
{
if (ns == null)
{
return;
}
Span<byte> span = stackalloc byte[MobileHealthbarPacketLength];
CreateMobileHealthbar(span, m, healthbar);
ns.Send(span);
}
public static void CreateMobileHealthbar(Span<byte> buffer, Mobile m, Healthbar healthbar)
{
switch (healthbar)
{
case Healthbar.Poison:
{
CreateMobileHealthbar(buffer, m.Serial, Healthbar.Poison, m.Poison?.Level + 1 ?? 0);
break;
}
case Healthbar.Yellow:
{
CreateMobileHealthbar(buffer, m.Serial, Healthbar.Yellow, m.Blessed || m.YellowHealthbar ? 1 : 0);
break;
}
default:
{
Console.WriteLine("Packets: Invalid Healthbar {0} in {1}", healthbar, nameof(CreateMobileHealthbar));
CreateMobileHealthbar(buffer, m.Serial, Healthbar.Normal, 0);
break;
}
}
}
public static void CreateMobileHealthbar(Span<byte> buffer, Serial serial, Healthbar healthbar, int level)
{
var writer = new SpanWriter(buffer);
writer.Write((byte)0x17); // Packet ID
writer.Write((ushort)12);
writer.Write(serial);
writer.Write((short)1); // Show bar
writer.Write((short)healthbar);
writer.Write((byte)level); // 0 is off for that bar type
}
} }
} }

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using Server.Buffers; using Server.Buffers;
namespace Server.Commands.Generic namespace Server.Commands.Generic

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Server.Commands.Generic; using Server.Commands.Generic;

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Server.Items; using Server.Items;

View file

@ -1,5 +1,3 @@
using System;
namespace Server.Engines.Chat namespace Server.Engines.Chat
{ {
public static class ChatActionHandlers public static class ChatActionHandlers

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using Server.Commands; using Server.Commands;

View file

@ -1,4 +1,3 @@
using System.Linq;
using Server.Guilds; using Server.Guilds;
using Server.Prompts; using Server.Prompts;

View file

@ -1,4 +1,3 @@
using System;
using Server.Gumps; using Server.Gumps;
using Server.Misc; using Server.Misc;
using Server.Mobiles; using Server.Mobiles;

View file

@ -1,5 +1,3 @@
using System;
namespace Server.Items namespace Server.Items
{ {
public enum HeadType public enum HeadType

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using Server.Buffers; using Server.Buffers;
using Server.ContextMenus; using Server.ContextMenus;
using Server.Gumps; using Server.Gumps;

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using Server.Buffers; using Server.Buffers;
using Server.Mobiles; using Server.Mobiles;
using Server.Network; using Server.Network;

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using Server.Buffers; using Server.Buffers;
namespace Server.Misc namespace Server.Misc

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Server.ContextMenus; using Server.ContextMenus;
using Server.Gumps; using Server.Gumps;

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Text;
using Server.Buffers; using Server.Buffers;
using Server.Items; using Server.Items;
using Server.Mobiles; using Server.Mobiles;