fix(core): Converts boat packets (#416)

- [X] Converts boat packets
- [X] Makes a packet container builder for packet 0xF7
- [X] Generalizes world item packet so it works for mobiles too

Notes:
- This PR doesn't address proper smooth movement for boats.
This commit is contained in:
Kamron Batman 2021-01-18 08:38:32 -08:00 • committed by GitHub
parent b4b1b0b122
commit 08f05afd01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 619 additions and 220 deletions

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using Moq;
using Server;
using Server.Items;
using Server.Multis;
using Server.Multis.Boats;
using Server.Network;
using Server.Tests;
using Server.Tests.Network;
using Xunit;
namespace UOContent.Tests
{
public class BoatPacketTests : IClassFixture<ServerFixture>
{
[Theory]
[InlineData(Direction.West, 10, 100, 200)]
public void TestMoveBoatHS(Direction d, int speed, int xOffset, int yOffset)
{
var boat = new Mock<BaseBoat>((Serial)0x2);
boat.Object.Location = new Point3D(10, 20, 15);
boat.Object.Facing = Direction.Right;
// Item on the boat
var item1 = new Mock<BaseWeapon>((Serial)0x2);
item1.Setup(m => m.ItemID).Returns(0x13B9);
item1.Object.Location = new Point3D(10, 20, 15);
// Item not on the boat
var item2 = new Mock<BaseWeapon>((Serial)0x2);
item2.Setup(m => m.ItemID).Returns(0x13B9);
item2.Object.Location = new Point3D(100, 200, 15);
var beholder = new Mock<Mobile>((Serial)0x1024u);
beholder.Object.Location = new Point3D(10, 20, 15);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item1.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item2.Object))).Returns(false);
var list = new List<IEntity>(4) { item1.Object, item2.Object, boat.Object, beholder.Object };
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = ProtocolChanges.HighSeas;
var expected = new MoveBoatHS(beholder.Object, boat.Object, d, speed, list, xOffset, yOffset).Compile();
ns.SendMoveBoatHS(beholder.Object, boat.Object, d, speed, list, xOffset, yOffset);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
public void TestDisplayBoatHS()
{
var item1 = new Item((Serial)0x1000) { ItemID = 0x13B9, Location = new Point3D(11, 21, 16), Map = Map.Felucca };
var item2 = new Item((Serial)0x2000) { ItemID = 0x13B9, Location = new Point3D(100, 200, 16), Map = Map.Felucca };
var beholder = new Mock<Mobile>((Serial)0x100);
beholder.Object.DefaultMobileInit();
beholder.Object.Location = new Point3D(10, 20, 15);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == beholder.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item1))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item2))).Returns(false);
var boat = new Mock<BaseBoat>((Serial)0x3000);
boat.Setup(b => b.GetMovingEntities()).Returns(() => new List<IEntity>{ item1, beholder.Object });
boat.Setup(b => b.Location).Returns(new Point3D(10, 20, 15));
boat.Object.Facing = Direction.Right;
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat.Object))).Returns(true);
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = ProtocolChanges.HighSeas;
var expected = new DisplayBoatHS(beholder.Object, boat.Object).Compile();
ns.SendDisplayBoatHS(beholder.Object, boat.Object);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
}
}

View file

@ -0,0 +1,145 @@
using System.Collections.Generic;
using System.IO;
using Server;
using Server.Collections;
using Server.Items;
using Server.Multis;
using Server.Network;
namespace UOContent.Tests
{
public sealed class MoveBoatHS : Packet
{
public MoveBoatHS(
Mobile beholder, BaseBoat boat, Direction d,
int speed, List<IEntity> ents, int xOffset,
int yOffset
) : base(0xF6)
{
EnsureCapacity(3 + 15 + ents.Count * 10);
Stream.Write(boat.Serial);
Stream.Write((byte)speed);
Stream.Write((byte)d);
Stream.Write((byte)boat.Facing);
Stream.Write((short)(boat.X + xOffset));
Stream.Write((short)(boat.Y + yOffset));
Stream.Write((short)boat.Z);
Stream.Write((short)0); // count placeholder
var count = 0;
foreach (var ent in ents)
{
if (!beholder.CanSee(ent))
{
continue;
}
Stream.Write(ent.Serial);
Stream.Write((short)(ent.X + xOffset));
Stream.Write((short)(ent.Y + yOffset));
Stream.Write((short)ent.Z);
++count;
}
Stream.Seek(16, SeekOrigin.Begin);
Stream.Write((short)count);
}
}
public sealed class DisplayBoatHS : Packet
{
public DisplayBoatHS(Mobile beholder, BaseBoat boat) : base(0xF7)
{
var ents = boat.GetMovingEntities();
ents.AddNotNull(boat.TillerMan);
ents.AddNotNull(boat.Hold);
ents.AddNotNull(boat.PPlank);
ents.AddNotNull(boat.SPlank);
ents.Add(boat);
EnsureCapacity(3 + 2 + ents.Count * 26);
Stream.Write((short)0); // count placeholder
var count = 0;
foreach (var ent in ents)
{
if (!beholder.CanSee(ent))
{
continue;
}
// Embedded WorldItemHS packets
Stream.Write((byte)0xF3);
Stream.Write((short)0x1);
if (ent is BaseMulti bm)
{
Stream.Write((byte)0x02);
Stream.Write(bm.Serial);
// TODO: Mask no longer needed, merge with Item case?
Stream.Write((ushort)(bm.ItemID & 0x3FFF));
Stream.Write((byte)0);
Stream.Write((short)bm.Amount);
Stream.Write((short)bm.Amount);
Stream.Write((short)(bm.X & 0x7FFF));
Stream.Write((short)(bm.Y & 0x3FFF));
Stream.Write((sbyte)bm.Z);
Stream.Write((byte)bm.Light);
Stream.Write((short)bm.Hue);
Stream.Write((byte)bm.GetPacketFlags());
}
else if (ent is Mobile m)
{
Stream.Write((byte)0x01);
Stream.Write(m.Serial);
Stream.Write((short)m.Body);
Stream.Write((byte)0);
Stream.Write((short)1);
Stream.Write((short)1);
Stream.Write((short)(m.X & 0x7FFF));
Stream.Write((short)(m.Y & 0x3FFF));
Stream.Write((sbyte)m.Z);
Stream.Write((byte)m.Direction);
Stream.Write((short)m.Hue);
Stream.Write((byte)m.GetPacketFlags(true));
}
else if (ent is Item item)
{
Stream.Write((byte)0x00);
Stream.Write(item.Serial);
Stream.Write((ushort)(item.ItemID & 0xFFFF));
Stream.Write((byte)0);
Stream.Write((short)item.Amount);
Stream.Write((short)item.Amount);
Stream.Write((short)(item.X & 0x7FFF));
Stream.Write((short)(item.Y & 0x3FFF));
Stream.Write((sbyte)item.Z);
Stream.Write((byte)item.Light);
Stream.Write((short)item.Hue);
Stream.Write((byte)item.GetPacketFlags());
}
Stream.Write((short)0x00);
++count;
}
Stream.Seek(3, SeekOrigin.Begin);
Stream.Write((short)count);
}
}
}