From ee3b20762e03e5ed8cbdf64f35feaf2c983a8282 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 31 Dec 2022 15:13:35 +0100 Subject: [PATCH] fix(game): create npc instead of monster in InResponder --- Core/NosSmooth.Game/Data/Entities/Npc.cs | 6 +++ .../PacketHandlers/Map/InResponder.cs | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/Core/NosSmooth.Game/Data/Entities/Npc.cs b/Core/NosSmooth.Game/Data/Entities/Npc.cs index cf177aca5c162fc9b0db068757e841bc408eb141..8ea0f4d5eb6ab5a0587e548fa4c113558991825d 100644 --- a/Core/NosSmooth.Game/Data/Entities/Npc.cs +++ b/Core/NosSmooth.Game/Data/Entities/Npc.cs @@ -4,6 +4,7 @@ // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using NosSmooth.Data.Abstractions.Infos; using NosSmooth.Game.Data.Info; using NosSmooth.Packets.Enums; @@ -14,6 +15,11 @@ namespace NosSmooth.Game.Data.Entities; /// public class Npc : ILivingEntity { + /// + /// Gets or sets the monster info. + /// + public IMonsterInfo? NpcInfo { get; set; } + /// /// Gets the VNum of the npc. /// diff --git a/Core/NosSmooth.Game/PacketHandlers/Map/InResponder.cs b/Core/NosSmooth.Game/PacketHandlers/Map/InResponder.cs index bb7a0eff225639571f1988e0040d9d6e1422e593..4bfcf6ec295e384cfb181d236c690ea566211c3b 100644 --- a/Core/NosSmooth.Game/PacketHandlers/Map/InResponder.cs +++ b/Core/NosSmooth.Game/PacketHandlers/Map/InResponder.cs @@ -15,6 +15,7 @@ using NosSmooth.Game.Data.Social; using NosSmooth.Game.Events.Core; using NosSmooth.Game.Events.Entities; using NosSmooth.Game.Helpers; +using NosSmooth.Packets.Enums; using NosSmooth.Packets.Server.Entities; using NosSmooth.Packets.Server.Maps; using Remora.Results; @@ -83,6 +84,11 @@ public class InResponder : IPacketResponder } if (packet.NonPlayerSubPacket is not null) { + if (packet.EntityType == EntityType.Npc) + { + return await CreateNpc(packet, packet.NonPlayerSubPacket, ct); + } + return await CreateMonster(packet, packet.NonPlayerSubPacket, ct); } @@ -163,6 +169,42 @@ public class InResponder : IPacketResponder }; } + private async Task CreateNpc + (InPacket packet, InNonPlayerSubPacket nonPlayerSubPacket, CancellationToken ct) + { + if (packet.VNum is null) + { + throw new Exception("The vnum from the in packet cannot be null for monsters."); + } + + var monsterInfoResult = await _infoService.GetMonsterInfoAsync(packet.VNum.Value, ct); + if (!monsterInfoResult.IsDefined(out var monsterInfo)) + { + _logger.LogWarning + ( + "Could not obtain a monster info for vnum {vnum}: {error}", + packet.VNum.Value, + monsterInfoResult.ToFullString() + ); + } + + return new Npc + { + VNum = packet.VNum.Value, + NpcInfo = monsterInfo, + Id = packet.EntityId, + Direction = packet.Direction, + Faction = nonPlayerSubPacket.Faction, + Hp = new Health { Percentage = nonPlayerSubPacket.HpPercentage }, + Mp = new Health { Percentage = nonPlayerSubPacket.MpPercentage }, + Name = nonPlayerSubPacket.Name?.Name, + Position = new Position(packet.PositionX, packet.PositionY), + IsInvisible = nonPlayerSubPacket.IsInvisible, + Level = monsterInfo?.Level ?? null, + IsSitting = nonPlayerSubPacket.IsSitting + }; + } + private async Task CreateMonster (InPacket packet, InNonPlayerSubPacket nonPlayerSubPacket, CancellationToken ct) {