diff --git a/src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs b/src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs new file mode 100644 index 0000000..841f087 --- /dev/null +++ b/src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs @@ -0,0 +1,42 @@ +// ----------------------------------------------------------------- +// +// Copyright (c) | Jose L. Nunez de Caceres et al. +// https://linkedin.com/in/nunezdecaceres +// +// All Rights Reserved. +// +// Licensed under the MIT License. See LICENSE in the project root for license information. +// +// ----------------------------------------------------------------- + +namespace Fibula.Communications.Packets.Outgoing +{ + using Fibula.Common.Contracts.Enumerations; + using Fibula.Communications.Contracts.Abstractions; + using Fibula.Communications.Contracts.Enumerations; + + /// + /// Class that represents a player's modes packet. + /// + public class PlayerModesPacket : IOutboundPacket + { + /// + /// Initializes a new instance of the class. + /// + /// The chase mode. + public PlayerModesPacket(ChaseMode chaseMode) + { + this.ChaseMode = chaseMode; + } + + /// + /// Gets the type of this packet. + /// + public OutgoingPacketType PacketType => OutgoingPacketType.PlayerModes; + + /// + /// Gets the chase mode to set. + /// + public ChaseMode ChaseMode { get; } + } +} diff --git a/src/Fibula.Mechanics/Game.cs b/src/Fibula.Mechanics/Game.cs index 5073136..933a5f7 100644 --- a/src/Fibula.Mechanics/Game.cs +++ b/src/Fibula.Mechanics/Game.cs @@ -614,7 +614,7 @@ public void SetCombatantModes(ICombatant combatant, FightMode fightMode, ChaseMo return; } - var changeModesOp = new ChangeModesOperation(combatant.Id, fightMode, chaseMode, safeModeOn); + var changeModesOp = new ChangeModesOperation(combatant, fightMode, chaseMode, safeModeOn); this.DispatchOperation(changeModesOp); } diff --git a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs index 1a2746b..0cbf1b1 100644 --- a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs +++ b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs @@ -11,8 +11,13 @@ namespace Fibula.Mechanics.Operations { + using System.Collections.Generic; using Fibula.Common.Contracts.Enumerations; + using Fibula.Communications.Packets.Outgoing; + using Fibula.Creatures.Contracts.Abstractions; + using Fibula.Map.Contracts.Extensions; using Fibula.Mechanics.Contracts.Abstractions; + using Fibula.Mechanics.Notifications; /// /// Class that represents a change modes operation. @@ -22,18 +27,24 @@ public class ChangeModesOperation : Operation /// /// Initializes a new instance of the class. /// - /// The id of the creature setting the modes. + /// The creature which is changing mode. /// The fight mode to set. /// The chase mode to set. /// A value indicating whether the safety mode is on. - public ChangeModesOperation(uint requestorId, FightMode fightMode, ChaseMode chaseMode, bool safeModeOn) - : base(requestorId) + public ChangeModesOperation(ICreature creature, FightMode fightMode, ChaseMode chaseMode, bool safeModeOn) + : base(creature.Id) { + this.Creature = creature; this.FightMode = fightMode; this.ChaseMode = chaseMode; this.IsSafeModeOn = safeModeOn; } + /// + /// Gets a reference to the creature turning. + /// + public ICreature Creature { get; } + /// /// Gets the fight mode to set. /// @@ -55,7 +66,7 @@ public ChangeModesOperation(uint requestorId, FightMode fightMode, ChaseMode cha /// A reference to the operation context. protected override void Execute(IOperationContext context) { - var onCreature = this.GetRequestor(context.CreatureFinder); + ICreature onCreature = this.GetRequestor(context.CreatureFinder); if (onCreature == null || !(onCreature is ICombatant combatantCreature)) { @@ -77,6 +88,8 @@ protected override void Execute(IOperationContext context) } /* combatantCreature.SafeMode = this.IsSafeModeOn; */ + + this.SendNotification(context, new GenericNotification(() => context.Map.PlayersThatCanSee(this.Creature.Location), new PlayerModesPacket(this.ChaseMode))); } } } diff --git a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs index 1de2479..78434ff 100644 --- a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs +++ b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs @@ -119,6 +119,7 @@ public static void AddProtocol772GameServerComponents(this IServiceCollection se { OutgoingPacketType.CancelAttack, typeof(PlayerCancelAttackPacketWriter) }, { OutgoingPacketType.CancelWalk, typeof(PlayerCancelWalkPacketWriter) }, { OutgoingPacketType.WorldLight, typeof(WorldLightPacketWriter) }, + { OutgoingPacketType.PlayerModes, typeof(PlayerModesPacketWriter) }, }; foreach (var (packetType, type) in packetWritersToAdd) diff --git a/src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs b/src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs new file mode 100644 index 0000000..d03fb20 --- /dev/null +++ b/src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs @@ -0,0 +1,47 @@ +// ----------------------------------------------------------------- +// +// Copyright (c) | Jose L. Nunez de Caceres et al. +// https://linkedin.com/in/nunezdecaceres +// +// All Rights Reserved. +// +// Licensed under the MIT License. See LICENSE in the project root for license information. +// +// ----------------------------------------------------------------- + +namespace Fibula.Protocol.V772.PacketWriters +{ + using Fibula.Communications; + using Fibula.Communications.Contracts.Abstractions; + using Fibula.Communications.Packets.Outgoing; + using Fibula.Protocol.V772.Extensions; + using Serilog; + + /// + /// Class that represents a player mode packet writer for the game server. + /// + public class PlayerModesPacketWriter : BasePacketWriter + { + /// + /// Initializes a new instance of the class. + /// + /// A reference to the logger in use. + public PlayerModesPacketWriter(ILogger logger) + : base(logger) + { + } + + /// + public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) + { + if (!(packet is PlayerModesPacket playerModePacket)) + { + this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); + + return; + } + + message.AddByte(playerModePacket.PacketType.ToByte()); + } + } +}