From b6e168f2022e50da37cffb86db9cb08ff9c4fa36 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 05:40:27 +0000 Subject: [PATCH] Hit with no skill on a pet's basic attack A pet's basic attack resolved cast 0, which matches any skill the pet knows, so a pet with skills of its own swung with the first of them: its reach, cooldown, animation and cards. Sending a NosMate or an escort at a monster with suctl did the same with the packet's first field, and refused to attack at all for a creature with no skills. In the packet traces every hit a pet, partner or escort lands when sent at a monster is skill 0 with animation 11, the creature's own basic cooldown and its EFF value as the effect, whatever suctl's first field holds. Both orders now go through one basic attack: no skill, no cards, reach and cooldown from the creature's data. Its skills are cast only from its skill bar. Fixes #3632 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CUfJ46iekh4rbiCyoTamtn --- documentation/manual-test-plan.md | 28 ++-- .../MateService/IMateAttackService.cs | 3 +- .../MateService/IMateControlService.cs | 2 +- .../Services/MateService/MateAttackService.cs | 17 ++- .../MateService/MateControlService.cs | 57 +------- .../Mates/SuCtlPacketHandler.cs | 2 +- .../Infrastructure/WorldData.cs | 3 +- .../Scenarios/PetTests.cs | 16 ++ .../BattleService/MateBasicAttackTests.cs | 129 ++++++++++++++++ .../BattleService/SkillResolverTests.cs | 27 ++++ .../Mates/MatePacketDeserializationTests.cs | 4 +- .../Mates/SuCtlPacketHandlerTests.cs | 138 +++++++++--------- .../Mates/UpetPacketHandlerTests.cs | 15 ++ 13 files changed, 295 insertions(+), 146 deletions(-) create mode 100644 test/NosCore.GameObject.Tests/Services/BattleService/MateBasicAttackTests.cs diff --git a/documentation/manual-test-plan.md b/documentation/manual-test-plan.md index b05b914f5a..c548f431c4 100644 --- a/documentation/manual-test-plan.md +++ b/documentation/manual-test-plan.md @@ -2045,17 +2045,23 @@ Have a pet and a partner out, and a second client on the same map watching. - [ ] Become visible again and take the pet back out with you, then make it talk: a speech bubble with your sentence shows over the pet on both screens, and the sentence is not cut at its first space -- [ ] With a pet that has skills of its own (a pet without any only sends a move order when a - monster is clicked), select a monster next to the pet and use one of the pet's skills on - it: the pet plays the skill, the damage number shows on both screens and the monster's bar - drops -- [ ] Use the same pet skill again at once: nothing happens until its cooldown is over -- [ ] Kill a monster with a pet skill: it collapses as it does from your own hits, and what falls - is what an ordinary kill of the same monster on the same map drops (drops are rolled, so - compare a few kills); the pet's share of the damage gives no experience yet, to you or the - pet -- [ ] Use a pet skill on a monster across the map: the pet does not hit it -- [ ] Use a pet skill on another player or on an NPC: nothing happens +- [ ] With the pet beside a monster, attack the monster: the pet joins in and hits it, the + damage number shows on both screens and the monster's bar drops; this works the same with + a pet that has no skill of its own (a Chicken, 333) +- [ ] The pet's `su` lines read skill 0, animation 11, the pet's own basic cooldown and its own + hit effect: 14 and 0 for a pet like vnum 709, 12 and 200 for vnum 9 +- [ ] With a pet that has skills of its own (an Otter, 683), let it fight a while without + touching its skill bar: none of its skills plays, its MP does not drop and no slot of its + bar greys out +- [ ] The pet hits no faster than its basic cooldown, however often the client sends it in +- [ ] Kill a monster with the pet's help: it collapses as it does from your own hits, and what + falls is what an ordinary kill of the same monster on the same map drops (drops are rolled, + so compare a few kills); the pet's share of the damage gives no experience yet, to you or + the pet +- [ ] Select a monster across the map: the pet does not hit it from where it stands +- [ ] Send the pet at another player or at an NPC: nothing happens +- [ ] In a time-space with an escort following you, attack a monster: the escort hits it with + skill 0 and animation 11 too ### A NosMate knocked out diff --git a/src/NosCore.GameObject/Services/MateService/IMateAttackService.cs b/src/NosCore.GameObject/Services/MateService/IMateAttackService.cs index dc72e4dedc..dcd63f7cdc 100644 --- a/src/NosCore.GameObject/Services/MateService/IMateAttackService.cs +++ b/src/NosCore.GameObject/Services/MateService/IMateAttackService.cs @@ -5,7 +5,6 @@ // using System.Threading.Tasks; -using NosCore.GameObject.Ecs; using NosCore.GameObject.Ecs.Interfaces; namespace NosCore.GameObject.Services.MateService @@ -13,6 +12,6 @@ namespace NosCore.GameObject.Services.MateService public interface IMateAttackService { // The creature's own basic attack, gated by its BasicCooldown and BasicRange. - Task BasicAttackAsync(MateComponentBundle mate, IAliveEntity target); + Task BasicAttackAsync(INonPlayableEntity attacker, IAliveEntity target); } } diff --git a/src/NosCore.GameObject/Services/MateService/IMateControlService.cs b/src/NosCore.GameObject/Services/MateService/IMateControlService.cs index 1f57e3bb12..8a0fa4d99a 100644 --- a/src/NosCore.GameObject/Services/MateService/IMateControlService.cs +++ b/src/NosCore.GameObject/Services/MateService/IMateControlService.cs @@ -16,6 +16,6 @@ public interface IMateControlService Task TalkAsync(PlayerComponentBundle owner, long mateTransportId, string? message); - Task AttackAsync(PlayerComponentBundle owner, long mateTransportId, long castId, IAliveEntity target); + Task AttackAsync(PlayerComponentBundle owner, long mateTransportId, IAliveEntity target); } } diff --git a/src/NosCore.GameObject/Services/MateService/MateAttackService.cs b/src/NosCore.GameObject/Services/MateService/MateAttackService.cs index bfd5dc1f19..e73178530e 100644 --- a/src/NosCore.GameObject/Services/MateService/MateAttackService.cs +++ b/src/NosCore.GameObject/Services/MateService/MateAttackService.cs @@ -7,7 +7,7 @@ using System; using System.Threading.Tasks; using NodaTime; -using NosCore.GameObject.Ecs; +using NosCore.GameObject.Ecs.Components; using NosCore.GameObject.Ecs.Interfaces; using NosCore.GameObject.Services.BattleService; using NosCore.PathFinder.Interfaces; @@ -21,21 +21,22 @@ public sealed class MateAttackService(IBattleService battleService, IHeuristic d private const short BasicAttackKey = 0; private const int MinimumCooldownMs = 200; - public async Task BasicAttackAsync(MateComponentBundle mate, IAliveEntity target) + public async Task BasicAttackAsync(INonPlayableEntity attacker, IAliveEntity target) { - var npcMonster = mate.NpcMonster; - if (npcMonster == null || mate.MapInstance == null || target.MapInstance != mate.MapInstance) + var npcMonster = attacker.NpcMonster; + if (npcMonster == null || attacker.MapInstance is not { } map || target.MapInstance != map + || map.EcsWorld.TryGetComponent(attacker.Handle) is not { } cooldowns) { return false; } var now = clock.GetCurrentInstant(); - if (mate.NextUsableAt.TryGetValue(BasicAttackKey, out var readyAt) && readyAt > now) + if (cooldowns.NextUsableAt.TryGetValue(BasicAttackKey, out var readyAt) && readyAt > now) { return false; } - var distance = (int)distanceCalculator.GetDistance((mate.PositionX, mate.PositionY), + var distance = (int)distanceCalculator.GetDistance((attacker.PositionX, attacker.PositionY), (target.PositionX, target.PositionY)); var targetArea = target is INonPlayableEntity { NpcMonster: { } targetMonster } ? targetMonster.BasicArea : (byte)0; if (!SkillReach.IsInReach(SkillReach.TargetedSkill, npcMonster.BasicRange, 0, distance, targetArea)) @@ -43,12 +44,12 @@ public async Task BasicAttackAsync(MateComponentBundle mate, IAliveEntity return false; } - if (!await battleService.Hit(mate, target, new HitArguments { SkillId = 0 })) + if (!await battleService.Hit(attacker, target, new HitArguments { BasicAttack = true })) { return false; } - mate.NextUsableAt[BasicAttackKey] = + cooldowns.NextUsableAt[BasicAttackKey] = now.Plus(Duration.FromMilliseconds(Math.Max(MinimumCooldownMs, npcMonster.BasicCooldown * 100))); return true; } diff --git a/src/NosCore.GameObject/Services/MateService/MateControlService.cs b/src/NosCore.GameObject/Services/MateService/MateControlService.cs index 5173a16e06..27fb423b74 100644 --- a/src/NosCore.GameObject/Services/MateService/MateControlService.cs +++ b/src/NosCore.GameObject/Services/MateService/MateControlService.cs @@ -4,9 +4,6 @@ // |_|\__|\__/ |___/ \__/\__/|_|_\___| // -using NodaTime; -using NosCore.Data.StaticEntities; -using NosCore.GameObject.Services.SkillService; using NosCore.GameObject.Ecs; using NosCore.GameObject.Ecs.Extensions; using NosCore.GameObject.Ecs.Interfaces; @@ -17,19 +14,14 @@ using NosCore.Packets.ServerPackets.Entities; using NosCore.PathFinder.Interfaces; using NosCore.Shared.Enumerations; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; using System.Threading.Tasks; namespace NosCore.GameObject.Services.MateService { - public class MateControlService(IHeuristic distanceCalculator, IBattleService battleService, - INpcCombatCatalog catalog, ISkillCatalog skills, IClock clock) : IMateControlService + public class MateControlService(IHeuristic distanceCalculator, IMateAttackService mateAttackService) + : IMateControlService { - public async Task MoveAsync(PlayerComponentBundle owner, long mateTransportId, short x, short y) { if (!TryGetOutMate(owner, mateTransportId, out var mate, out var entity)) @@ -106,59 +98,22 @@ public async Task TalkAsync(PlayerComponentBundle owner, long mateTranspor return true; } - public async Task AttackAsync(PlayerComponentBundle owner, long mateTransportId, long castId, - IAliveEntity target) + public async Task AttackAsync(PlayerComponentBundle owner, long mateTransportId, IAliveEntity target) { if (target.MapInstance != owner.MapInstance) { return false; } - if (TryGetOutMate(owner, mateTransportId, out var mate, out var entity)) + if (TryGetOutMate(owner, mateTransportId, out _, out var entity)) { - return await StrikeAsync(entity, mate.VNum, entity.NextUsableAt, castId, target); + return await mateAttackService.BasicAttackAsync(entity, target); } return mateTransportId is > 0 and <= int.MaxValue && owner.MapInstance.GetNpcById((int)mateTransportId) is { IsAlive: true } escort && escort.LeaderId == owner.CharacterId - && await StrikeAsync(escort, escort.VNum, escort.NextUsableAt, castId, target); - } - - private async Task StrikeAsync(IAliveEntity entity, short vNum, ConcurrentDictionary nextUsableAt, - long castId, IAliveEntity target) - { - // Only a skill in the creature's own roster, picked as the resolver picks it (cast 0 means its first - // skill), so reach and cooldown are checked on the skill that is actually cast. - var skill = catalog.GetSkills(vNum) - .Select(s => skills.GetSkill(s.SkillVNum)) - .FirstOrDefault(s => s != null && (s.CastId == castId || castId == 0)); - if (skill == null) - { - return false; - } - - var now = clock.GetCurrentInstant(); - if (nextUsableAt.TryGetValue(skill.SkillVNum, out var readyAt) && readyAt > now) - { - return false; - } - - var distance = (int)distanceCalculator.GetDistance((entity.PositionX, entity.PositionY), - (target.PositionX, target.PositionY)); - var targetArea = target is INonPlayableEntity { NpcMonster: { } npcMonster } ? npcMonster.BasicArea : (byte)0; - if (!SkillReach.IsInReach(skill.TargetType, skill.Range, skill.TargetRange, distance, targetArea)) - { - return false; - } - - if (!await battleService.Hit(entity, target, new HitArguments { SkillId = castId })) - { - return false; - } - - nextUsableAt[skill.SkillVNum] = now.Plus(Duration.FromMilliseconds(Math.Max(200, skill.Cooldown * 100))); - return true; + && await mateAttackService.BasicAttackAsync(escort, target); } // Trusting the id alone would let a client drive somebody else's mate, or one left in the Miniland. diff --git a/src/NosCore.PacketHandlers/Mates/SuCtlPacketHandler.cs b/src/NosCore.PacketHandlers/Mates/SuCtlPacketHandler.cs index ce1b0a7df5..73b2a160ed 100644 --- a/src/NosCore.PacketHandlers/Mates/SuCtlPacketHandler.cs +++ b/src/NosCore.PacketHandlers/Mates/SuCtlPacketHandler.cs @@ -25,7 +25,7 @@ public override Task ExecuteAsync(SuCtlPacket packet, ClientSession session) return Task.CompletedTask; } - return mateControlService.AttackAsync(session.Character, packet.MateTransportId, packet.CastId, target); + return mateControlService.AttackAsync(session.Character, packet.MateTransportId, target); } } } diff --git a/test/NosCore.EndToEnd.Tests/Infrastructure/WorldData.cs b/test/NosCore.EndToEnd.Tests/Infrastructure/WorldData.cs index 58146db15b..90c70c0dc8 100644 --- a/test/NosCore.EndToEnd.Tests/Infrastructure/WorldData.cs +++ b/test/NosCore.EndToEnd.Tests/Infrastructure/WorldData.cs @@ -146,6 +146,7 @@ public static class WorldData public const short PetSkill = 220; public const short PetSkillMp = 40; public const short PetSkillCooldown = 30; + public const short PetBasicCooldown = 20; // A partner, and a partner card whose one skill is cast on the partner itself and buffs it. public const short PartnerVNum = 13; @@ -593,7 +594,7 @@ public static async Task SeedAsync(NosCoreContext context) context.Add(new NpcMonster { NpcMonsterVNum = PetVNum, Name = "pet", Level = 1, MaxHp = 100, MaxMp = 100, HpBonus = 100, MpBonus = 100, - BasicRange = 1, BasicCooldown = 20 + BasicRange = 1, BasicCooldown = PetBasicCooldown }); await context.SaveChangesAsync(); context.Add(new NpcMonsterSkill { NpcMonsterVNum = PetVNum, SkillVNum = PetSkill }); diff --git a/test/NosCore.EndToEnd.Tests/Scenarios/PetTests.cs b/test/NosCore.EndToEnd.Tests/Scenarios/PetTests.cs index e2bb66dc63..f7b7573b87 100644 --- a/test/NosCore.EndToEnd.Tests/Scenarios/PetTests.cs +++ b/test/NosCore.EndToEnd.Tests/Scenarios/PetTests.cs @@ -146,6 +146,22 @@ public async Task APetSkillIsRefusedUntilItHasCooledDown() "the pet does not use the skill a second time while it cools down"); } + [TestMethod] + public async Task APetSentAtAMonsterHitsItWithItsBasicAttackNotItsSkill() + { + await using var alice = await EnterWithAPetAsync("Alice"); + var pet = await PetOfAsync(alice); + var sent = alice.Mark(); + + await alice.SendAsync(new SuCtlPacket { MateTransportId = pet, TargetType = VisualType.Monster, TargetId = WorldData.SturdyMonster }); + + var hit = await alice.ExpectSinceAsync(sent, HitBy(pet), "the pet's hit"); + Assert.AreEqual(0L, hit.SkillVnum, "no skill"); + Assert.AreEqual((long)WorldData.PetBasicCooldown, hit.SkillCooldown, "the pet's basic cooldown"); + Assert.AreEqual(11L, hit.AttackAnimation, "the basic attack's animation"); + Assert.AreEqual(0L, hit.SkillEffect, "the pet's own hit effect"); + } + [TestMethod] public async Task APetOnlyUsesTheSkillsOfItsOwnBar() { diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/MateBasicAttackTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/MateBasicAttackTests.cs new file mode 100644 index 0000000000..03cc4fd8fd --- /dev/null +++ b/test/NosCore.GameObject.Tests/Services/BattleService/MateBasicAttackTests.cs @@ -0,0 +1,129 @@ +// __ _ __ __ ___ __ ___ ___ +// | \| |/__\ /' _/ / _//__\| _ \ __| +// | | ' | \/ |`._`.| \_| \/ | v / _| +// |_|\__|\__/ |___/ \__/\__/|_|_\___| +// + +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using NosCore.Data.Enumerations.Buff; +using NosCore.Data.StaticEntities; +using NosCore.GameObject.Ecs; +using NosCore.GameObject.Ecs.Extensions; +using NosCore.GameObject.Ecs.Interfaces; +using NosCore.GameObject.Networking.ClientSession; +using NosCore.GameObject.Services.BattleService; +using NosCore.GameObject.Services.BattleService.Model; +using NosCore.GameObject.Services.MapInstanceGenerationService; +using NosCore.GameObject.Services.MateService; +using NosCore.Packets.Enumerations; +using NosCore.Packets.ServerPackets.Battle; +using NosCore.Shared.Enumerations; +using NosCore.Tests.Shared; + +namespace NosCore.GameObject.Tests.Services.BattleService +{ + [TestClass] + public class MateBasicAttackTests + { + private const short PetVNum = 709; + private const short PetSkill = 790; + private const short PetHitEffect = 200; + private const short PetBasicCooldown = 14; + + private TestWorld World; + private ClientSession _session; + private MapInstance _map; + private MateComponentBundle _pet; + private MonsterComponentBundle _monster; + private readonly List _hits = new(); + + [TestInitialize] + public async Task SetupAsync() + { + World = await TestWorld.CreateAsync(); + _session = await World.CreateSessionAsync(); + _map = _session.Character.MapInstance; + + World.Inject(new List + { + new() { SkillVNum = PetSkill, CastId = 0, Cooldown = 80, Range = 3, Effect = 791, AttackAnimation = 12, SkillType = 5 }, + }); + World.Inject(new List + { + new() { NpcMonsterSkillId = 1, NpcMonsterVNum = PetVNum, SkillVNum = PetSkill }, + }); + World.Inject(new List + { + new() { SkillVNum = PetSkill, Type = BCardType.CardType.Buff, SubType = 11, FirstData = 100, SecondData = 1 }, + }); + World.UseReal(); + World.UseReal(); + World.Freeze>() + .Setup(r => r.Resolve(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns((_, target, _) => new[] { target }); + World.Freeze>() + .Setup(q => q.EnqueueAsync(It.IsAny())) + .Callback(_hits.Add) + .ReturnsAsync(new HitOutcome(HitStatus.Landed, 10, SuPacketHitMode.SuccessAttack, false)); + World.Freeze>() + .Setup(s => s.GetStats(It.IsAny())) + .Returns(new CombatStats()); + World.UseReal(); + + var pet = TestMates.Pet(_session.Character.CharacterId, vnum: PetVNum); + pet.NpcMonster.BasicSkill = PetHitEffect; + pet.NpcMonster.BasicCooldown = PetBasicCooldown; + pet.NpcMonster.BasicRange = 1; + pet.NpcMonster.AttackClass = 1; + _session.Character.Mates[pet.MateTransportId] = pet; + await pet.SpawnAsync(_session.Character, _map, 5, 5); + _pet = pet.Entity.NotNull(); + + _monster = _map.SpawnMonster(new NpcMonsterDto { NpcMonsterVNum = 2, MaxHp = 1000 }, 6, 5, false); + } + + private Task ThePetSwingsAtTheMonsterAsync() => + World.Create().BasicAttackAsync(_pet, _monster); + + [TestMethod] + public async Task APetsBasicAttackIsNotItsFirstSkill() + { + Assert.IsTrue(await ThePetSwingsAtTheMonsterAsync()); + + var hit = _hits.Single(); + Assert.AreEqual((short)0, hit.Skill.SkillVnum); + Assert.AreEqual(0, hit.Skill.BCards.Count); + } + + [TestMethod] + public async Task APetsBasicHitShowsItsOwnEffectAndCooldown() + { + await ThePetSwingsAtTheMonsterAsync(); + + PacketAssert.AreEqual(new SuPacket + { + VisualType = VisualType.Npc, + VisualId = _pet.VisualId, + TargetVisualType = VisualType.Monster, + TargetId = _monster.VisualId, + SkillVnum = 0, + SkillCooldown = PetBasicCooldown, + AttackAnimation = 11, + SkillEffect = PetHitEffect, + PositionX = _pet.PositionX, + PositionY = _pet.PositionY, + TargetIsAlive = true, + HpPercentage = (byte)(_monster.Hp * 100 / _monster.MaxHp), + Damage = 10, + HitMode = SuPacketHitMode.SuccessAttack, + SkillTypeMinusOne = 0, + TargetCurrentHp = _monster.Hp, + TargetMaxHp = _monster.MaxHp, + }, _map.LastPackets.OfType().Single()); + } + } +} diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/SkillResolverTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/SkillResolverTests.cs index c9630caaae..bc9be3d4d6 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/SkillResolverTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/SkillResolverTests.cs @@ -20,6 +20,7 @@ using NosCore.Data.Enumerations.Buff; using NosCore.Data.Enumerations.I18N; using NosCore.Data.StaticEntities; +using NosCore.GameObject.Ecs.Extensions; using NosCore.GameObject.Ecs.Interfaces; using NosCore.GameObject.Services.BattleService; using NosCore.GameObject.Services.GroupService; @@ -108,6 +109,32 @@ public void AMonsterWithoutSkillsFallsBackToItsBasicAttackNotToTheSkillItsEffect Assert.AreEqual(0, resolved?.BCards.Count); } + [TestMethod] + public async Task APetsBasicAttackIsItsOwnHitNotTheSkillItKnows() + { + var world = await TestWorld.CreateAsync(); + var owner = (await world.CreateSessionAsync()).Character; + var pet = TestMates.Pet(owner.CharacterId, vnum: 709); + pet.NpcMonster.BasicSkill = 200; + pet.NpcMonster.BasicCooldown = 14; + pet.NpcMonster.BasicRange = 1; + await pet.SpawnAsync(owner, owner.MapInstance, 5, 5); + var resolver = new SkillResolver( + new NosCore.GameObject.Services.SkillService.SkillCatalog([new SkillDto { SkillVNum = 790, CastId = 0, Cooldown = 80, Range = 3, Effect = 791 }]), + new NpcCombatCatalog([new NpcMonsterSkillDto { NpcMonsterVNum = 709, SkillVNum = 790 }], new(), [Causes(790, 1, 100)]), + _cards.Object); + + var basic = resolver.ResolveBasic(pet.Entity.NotNull()); + + Assert.IsNotNull(basic); + Assert.AreEqual((short)0, basic.SkillVnum); + Assert.AreEqual((short)200, basic.Effect); + Assert.AreEqual((short)11, basic.AttackAnimation); + Assert.AreEqual((short)14, basic.Cooldown); + Assert.AreEqual((byte)1, basic.Range); + Assert.AreEqual(0, basic.BCards.Count); + } + [TestMethod] public void UnknownCasterReturnsNull() { diff --git a/test/NosCore.PacketHandlers.Tests/Mates/MatePacketDeserializationTests.cs b/test/NosCore.PacketHandlers.Tests/Mates/MatePacketDeserializationTests.cs index 8c6320ad3a..d390b9868b 100644 --- a/test/NosCore.PacketHandlers.Tests/Mates/MatePacketDeserializationTests.cs +++ b/test/NosCore.PacketHandlers.Tests/Mates/MatePacketDeserializationTests.cs @@ -52,9 +52,9 @@ public void APetsSpeechReachesItsHandler() } [TestMethod] - public void APartnerSkillReachesItsHandler() + public void AnOrderToAttackReachesItsHandler() { - var packet = new SuCtlPacket { CastId = 200, MateTransportId = 1135623, TargetType = VisualType.Monster, TargetId = 2045 }; + var packet = new SuCtlPacket { MateTransportId = 1135623, TargetType = VisualType.Monster, TargetId = 2045 }; Assert.IsInstanceOfType(RoundTrip(packet), HandledBy(typeof(SuCtlPacketHandler))); } diff --git a/test/NosCore.PacketHandlers.Tests/Mates/SuCtlPacketHandlerTests.cs b/test/NosCore.PacketHandlers.Tests/Mates/SuCtlPacketHandlerTests.cs index 4571333fa6..43aa7af375 100644 --- a/test/NosCore.PacketHandlers.Tests/Mates/SuCtlPacketHandlerTests.cs +++ b/test/NosCore.PacketHandlers.Tests/Mates/SuCtlPacketHandlerTests.cs @@ -34,14 +34,12 @@ public class SuCtlPacketHandlerTests private const int PetCastId = 3; private const int NearMonsterId = 200; private const int FarMonsterId = 201; - private const short LongReachSkillVNum = 701; private SuCtlPacketHandler _handler; private ClientSession _session; private ClientSession _otherSession; private MapInstance _map; private Mock _battleService; - private Mock _catalog; private Mate _pet; [TestInitialize] @@ -64,46 +62,56 @@ public async Task SetupAsync() _battleService = World.Freeze>(); _battleService.Setup(s => s.Hit(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(true); - _catalog = World.Freeze>(); - _catalog.Setup(c => c.GetSkills(It.IsAny())).Returns(new List + World.Inject(new List { - new() { NpcMonsterVNum = 333, SkillVNum = PetSkillVNum } + new() { SkillVNum = PetSkillVNum, CastId = PetCastId, Range = 20, TargetType = 0, Cooldown = 10 } }); - var skills = new List + World.Inject(new List { - new() { SkillVNum = PetSkillVNum, CastId = PetCastId, Range = 2, TargetType = 0, Cooldown = 10 }, - new() { SkillVNum = LongReachSkillVNum, CastId = 0, Range = 20, TargetType = 0, Cooldown = 10 } - }; - World.Inject(skills); - _handler = World.UseReal().Create(); + new() { NpcMonsterSkillId = 1, NpcMonsterVNum = 333, SkillVNum = PetSkillVNum } + }); + _handler = World.UseReal() + .UseReal() + .UseReal() + .Create(); } [TestMethod] - public async Task AnOwnPetHitsAMonsterWithItsSkill() + public async Task AnOwnPetHitsAMonsterWithItsBasicAttack() { - await new Spec("An own pet hits a monster in reach with a skill it knows, through the battle service") + await new Spec("An own pet sent at a monster in reach hits it with its basic attack, through the battle service") .GivenAsync(APetOf_IsOut, "owner") - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) - .Then(ThePetShouldHitMonster_WithCast_, NearMonsterId, PetCastId) + .WhenAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) + .Then(ThePetShouldHitMonster_WithItsBasicAttack, NearMonsterId) .ExecuteAsync(); } [TestMethod] - public async Task AnotherPlayersPetIsIgnored() + public async Task TheFirstFieldOfTheOrderPicksNoSkill() { - await new Spec("A suctl naming another player's pet does not attack") - .GivenAsync(APetOf_IsOut, "other player") - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) - .Then(NothingShouldBeHit) + await new Spec("An order whose first field matches one of the pet's skills still sends the pet in with its basic attack") + .GivenAsync(APetOf_IsOut, "owner") + .WhenAsync(TheOwnerSendsThePetAtMonster_WithFirstField_, NearMonsterId, PetCastId) + .Then(ThePetShouldHitMonster_WithItsBasicAttack, NearMonsterId) .ExecuteAsync(); } [TestMethod] - public async Task ASkillThePetDoesNotKnowIsRefused() + public async Task APetWithoutSkillsStillAttacks() { - await new Spec("A cast id outside the pet's own skills is refused, not swapped for its basic attack") - .GivenAsync(APetOf_IsOut, "owner") - .WhenAsync(TheOwnerOrdersCast_OnMonster_, 9, NearMonsterId) + await new Spec("A pet that knows no skill still hits the monster it is sent at") + .GivenAsync(APetWithoutSkillsIsOut) + .WhenAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) + .Then(ThePetShouldHitMonster_WithItsBasicAttack, NearMonsterId) + .ExecuteAsync(); + } + + [TestMethod] + public async Task AnotherPlayersPetIsIgnored() + { + await new Spec("A suctl naming another player's pet does not attack") + .GivenAsync(APetOf_IsOut, "other player") + .WhenAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) .Then(NothingShouldBeHit) .ExecuteAsync(); } @@ -111,9 +119,9 @@ public async Task ASkillThePetDoesNotKnowIsRefused() [TestMethod] public async Task AMonsterOutOfReachIsRefused() { - await new Spec("A monster beyond the skill's range is not hit") + await new Spec("A monster beyond the pet's basic range is not hit, however far its skills reach") .GivenAsync(APetOf_IsOut, "owner") - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, FarMonsterId) + .WhenAsync(TheOwnerSendsThePetAtMonster_, FarMonsterId) .Then(NothingShouldBeHit) .ExecuteAsync(); } @@ -123,7 +131,7 @@ public async Task AnUnknownMonsterIsRefused() { await new Spec("A target id that is not a monster on the map is refused") .GivenAsync(APetOf_IsOut, "owner") - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, 999) + .WhenAsync(TheOwnerSendsThePetAtMonster_, 999) .Then(NothingShouldBeHit) .ExecuteAsync(); } @@ -131,54 +139,43 @@ public async Task AnUnknownMonsterIsRefused() [TestMethod] public async Task APlayerIsNotATarget() { - await new Spec("A pet cannot be ordered at a player") + await new Spec("A pet cannot be sent at a player") .GivenAsync(APetOf_IsOut, "owner") - .WhenAsync(TheOwnerOrdersCastOnTheOtherPlayer) + .WhenAsync(TheOwnerSendsThePetAtTheOtherPlayer) .Then(NothingShouldBeHit) .ExecuteAsync(); } [TestMethod] - public async Task ASkillWaitsForItsCooldown() + public async Task AnAttackWaitsForTheBasicCooldown() { - await new Spec("A pet's skill cannot be cast again before its cooldown is over") + await new Spec("A pet cannot hit again before its basic cooldown is over") .GivenAsync(APetOf_IsOut, "owner") - .AndAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) + .AndAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) + .WhenAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) .Then(ThePetShouldHaveHit_Times, 1) .ExecuteAsync(); } [TestMethod] - public async Task ASkillIsBackAfterItsCooldown() + public async Task AnAttackIsBackAfterTheBasicCooldown() { - await new Spec("A pet's skill can be cast again once its cooldown is over") + await new Spec("A pet hits again once its basic cooldown is over") .GivenAsync(APetOf_IsOut, "owner") - .AndAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) + .AndAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) .And(_SecondsPass, 1) - .WhenAsync(TheOwnerOrdersCast_OnMonster_, PetCastId, NearMonsterId) + .WhenAsync(TheOwnerSendsThePetAtMonster_, NearMonsterId) .Then(ThePetShouldHaveHit_Times, 2) .ExecuteAsync(); } - [TestMethod] - public async Task CastZeroIsCheckedOnTheSkillTheResolverCasts() - { - await new Spec("Cast 0 is the pet's first skill, so a far monster is out of that skill's reach") - .GivenAsync(APetOf_IsOut, "owner") - .And(ThePetsSecondSkillHasCastZeroAndAMuchLongerRange) - .WhenAsync(TheOwnerOrdersCast_OnMonster_, 0, FarMonsterId) - .Then(NothingShouldBeHit) - .ExecuteAsync(); - } - [TestMethod] public async Task AnEscortHitsAMonsterItsLeaderPoints() { - await new Spec("An escort following the player hits the monster the player sends it at") + await new Spec("An escort following the player hits the monster the player sends it at with its basic attack") .Given(AnEscortLedBy_, "owner") .WhenAsync(TheOwnerSendsTheEscortAtMonster_, NearMonsterId) - .Then(ThePetShouldHaveHit_Times, 1) + .Then(TheEscortShouldHitMonster_WithItsBasicAttack, NearMonsterId) .ExecuteAsync(); } @@ -197,7 +194,7 @@ public async Task AnEscortLedByAnotherPlayerIgnoresTheOrder() private void AnEscortLedBy_(string whose) { var leader = whose == "owner" ? _session : _otherSession; - _escort = _map.SummonNpc(new NpcMonsterDto { NpcMonsterVNum = 318, MaxHp = 10, Speed = 10 }, 5, 5); + _escort = _map.SummonNpc(new NpcMonsterDto { NpcMonsterVNum = 318, MaxHp = 10, Speed = 10, BasicRange = 1 }, 5, 5); _escort.IsEscort = true; _escort.LeaderId = leader.Character.CharacterId; } @@ -205,25 +202,21 @@ private void AnEscortLedBy_(string whose) private Task TheOwnerSendsTheEscortAtMonster_(int monsterId) => _handler.ExecuteAsync(new SuCtlPacket { - CastId = PetCastId, MateTransportId = _escort.VisualId, TargetType = VisualType.Monster, TargetId = monsterId }, _session); - private void ThePetsSecondSkillHasCastZeroAndAMuchLongerRange() - { - _catalog.Setup(c => c.GetSkills(It.IsAny())).Returns(new List - { - new() { NpcMonsterVNum = 333, SkillVNum = PetSkillVNum }, - new() { NpcMonsterVNum = 333, SkillVNum = LongReachSkillVNum } - }); - } + private Task APetOf_IsOut(string whose) => SpawnAsync(whose, TestMates.Pet((whose == "owner" ? _session : _otherSession).Character.CharacterId)); + + private Task APetWithoutSkillsIsOut() => SpawnAsync("owner", TestMates.Pet(_session.Character.CharacterId, vnum: 334)); - private async Task APetOf_IsOut(string whose) + private async Task SpawnAsync(string whose, Mate pet) { var owner = whose == "owner" ? _session : _otherSession; - _pet = TestMates.Pet(owner.Character.CharacterId); + _pet = pet; + _pet.NpcMonster.BasicRange = 1; + _pet.NpcMonster.BasicCooldown = 10; owner.Character.Mates[_pet.MateTransportId] = _pet; await _pet.SpawnAsync(owner.Character, _map, 5, 5); } @@ -233,30 +226,37 @@ private void _SecondsPass(int seconds) World.Clock.Advance(Duration.FromSeconds(seconds)); } - private Task TheOwnerOrdersCast_OnMonster_(int castId, int monsterId) => + private Task TheOwnerSendsThePetAtMonster_(int monsterId) => TheOwnerSendsThePetAtMonster_WithFirstField_(monsterId, 0); + + private Task TheOwnerSendsThePetAtMonster_WithFirstField_(int monsterId, int firstField) => _handler.ExecuteAsync(new SuCtlPacket { - CastId = castId, + CastId = firstField, MateTransportId = _pet.MateTransportId, TargetType = VisualType.Monster, TargetId = monsterId }, _session); - private Task TheOwnerOrdersCastOnTheOtherPlayer() => + private Task TheOwnerSendsThePetAtTheOtherPlayer() => _handler.ExecuteAsync(new SuCtlPacket { - CastId = PetCastId, MateTransportId = _pet.MateTransportId, TargetType = VisualType.Player, TargetId = _otherSession.Character.VisualId }, _session); - private void ThePetShouldHitMonster_WithCast_(int monsterId, int castId) + private void ThePetShouldHitMonster_WithItsBasicAttack(int monsterId) => + ShouldHitWithABasicAttack(_pet.MateTransportId, monsterId); + + private void TheEscortShouldHitMonster_WithItsBasicAttack(int monsterId) => + ShouldHitWithABasicAttack(_escort.VisualId, monsterId); + + private void ShouldHitWithABasicAttack(long attackerId, int monsterId) { _battleService.Verify(s => s.Hit( - It.Is(e => e.VisualType == VisualType.Npc && e.VisualId == _pet.MateTransportId), + It.Is(e => e.VisualType == VisualType.Npc && e.VisualId == attackerId), It.Is(e => e.VisualType == VisualType.Monster && e.VisualId == monsterId), - It.Is(a => a.SkillId == castId)), Times.Once); + It.Is(a => a.BasicAttack && a.Skill == null)), Times.Once); } private void ThePetShouldHaveHit_Times(int times) diff --git a/test/NosCore.PacketHandlers.Tests/Mates/UpetPacketHandlerTests.cs b/test/NosCore.PacketHandlers.Tests/Mates/UpetPacketHandlerTests.cs index 289be88538..619f7a8e58 100644 --- a/test/NosCore.PacketHandlers.Tests/Mates/UpetPacketHandlerTests.cs +++ b/test/NosCore.PacketHandlers.Tests/Mates/UpetPacketHandlerTests.cs @@ -99,6 +99,15 @@ public async Task APetAttacksAMonsterBesideIt() .ExecuteAsync(); } + [TestMethod] + public async Task APetsBasicAttackIsNoneOfItsSkills() + { + await new Spec("A pet ordered to attack with no skill hits with its basic attack, not with the first skill it knows") + .WhenAsync(ThePetAttacks) + .Then(ThePetShouldHitTheMonsterWithItsBasicAttack) + .ExecuteAsync(); + } + [TestMethod] public async Task APetCannotAttackAgainBeforeItsBasicCooldown() { @@ -324,6 +333,12 @@ private void ThePetShouldHitTheMonsterWith_(short skill) => It.Is(e => e.VisualType == VisualType.Monster && e.VisualId == _monster.VisualId), It.Is(a => a.Skill != null && a.Skill.SkillVNum == skill)), Times.Once); + private void ThePetShouldHitTheMonsterWithItsBasicAttack() => + _battleService.Verify(b => b.Hit( + It.Is(e => e.VisualType == VisualType.Npc && e.VisualId == _pet.MateTransportId), + It.Is(e => e.VisualType == VisualType.Monster && e.VisualId == _monster.VisualId), + It.Is(a => a.BasicAttack && a.Skill == null)), Times.Once); + private List<(long OwnerId, short Slot)> ReadySlots() { var ready = new List<(long OwnerId, short Slot)>();