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

@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server.Collections;
using Server.Items;
using Server.Multis.Boats;
using Server.Network;
namespace Server.Multis
@ -1737,11 +1738,10 @@ namespace Server.Multis
else
{
var toMove = GetMovingEntities();
SafeAdd(TillerMan, toMove);
SafeAdd(Hold, toMove);
SafeAdd(PPlank, toMove);
SafeAdd(SPlank, toMove);
toMove.AddNotNull(TillerMan);
toMove.AddNotNull(Hold);
toMove.AddNotNull(PPlank);
toMove.AddNotNull(SPlank);
// Packet must be sent before actual locations are changed
foreach (var ns in Map.GetClientsInRange(Location, GetMaxUpdateRange()))
@ -1750,7 +1750,7 @@ namespace Server.Multis
if (ns.HighSeas && m.CanSee(this) && m.InRange(Location, GetUpdateRange(m)))
{
ns.Send(new MoveBoatHS(m, this, d, clientSpeed, toMove, xOffset, yOffset));
ns.SendMoveBoatHS(m, this, d, clientSpeed, toMove, xOffset, yOffset);
}
}
@ -1793,14 +1793,6 @@ namespace Server.Multis
return true;
}
private static void SafeAdd(Item item, List<IEntity> toMove)
{
if (item != null)
{
toMove.Add(item);
}
}
public void Teleport(int xOffset, int yOffset, int zOffset)
{
var toMove = GetMovingEntities();
@ -1822,7 +1814,7 @@ namespace Server.Multis
Location = new Point3D(X + xOffset, Y + yOffset, Z + zOffset);
}
public List<IEntity> GetMovingEntities()
public virtual List<IEntity> GetMovingEntities()
{
var list = new List<IEntity>();
@ -1835,7 +1827,9 @@ namespace Server.Multis
var mcl = Components;
foreach (var o in map.GetObjectsInBounds(new Rectangle2D(X + mcl.Min.X, Y + mcl.Min.Y, mcl.Width, mcl.Height)))
var eable = map.GetObjectsInBounds(new Rectangle2D(X + mcl.Min.X, Y + mcl.Min.Y, mcl.Width, mcl.Height));
foreach (var o in eable)
{
if (o == this || o is TillerMan || o is Hold || o is Plank)
{
@ -1858,6 +1852,8 @@ namespace Server.Multis
}
}
eable.Free();
return list;
}
@ -2089,141 +2085,5 @@ namespace Server.Multis
return base.GetWorldPacketFor( state );
}
*/
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();
SafeAdd(boat.TillerMan, ents);
SafeAdd(boat.Hold, ents);
SafeAdd(boat.PPlank, ents);
SafeAdd(boat.SPlank, ents);
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);
}
}
}
}

View file

@ -0,0 +1,88 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BoatPackets.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server.Collections;
using Server.Network;
namespace Server.Multis.Boats
{
public static class BoatPackets
{
public static void SendMoveBoatHS(this NetState ns, Mobile beholder, BaseBoat boat,
Direction d, int speed, List<IEntity> ents, int xOffset, int yOffset)
{
if (ns?.HighSeas != true)
{
return;
}
var maxLength = 18 + ents.Count * 10;
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0xF6); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write(boat.Serial);
writer.Write((byte)speed);
writer.Write((byte)d);
writer.Write((byte)boat.Facing);
writer.Write((short)(boat.X + xOffset));
writer.Write((short)(boat.Y + yOffset));
writer.Write((short)boat.Z);
writer.Seek(2, SeekOrigin.Current); // count
var count = 0;
foreach (var ent in ents)
{
if (!beholder.CanSee(ent))
{
continue;
}
writer.Write(ent.Serial);
writer.Write((short)(ent.X + xOffset));
writer.Write((short)(ent.Y + yOffset));
writer.Write((short)ent.Z);
++count;
}
writer.Seek(16, SeekOrigin.Begin);
writer.Write((short)count);
writer.WritePacketLength();
ns.Send(writer.Span);
}
public static void SendDisplayBoatHS(this NetState ns, Mobile beholder, BaseBoat boat)
{
var ents = boat.GetMovingEntities();
ents.AddNotNull(boat.TillerMan);
ents.AddNotNull(boat.Hold);
ents.AddNotNull(boat.PPlank);
ents.AddNotNull(boat.SPlank);
ents.Add(boat);
var eable = ents.Where(beholder.CanSee);
ns.SendBatchEntities(eable, ents.Count);
}
}
}