Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions documentation/manual-test-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
//

using System.Threading.Tasks;
using NosCore.GameObject.Ecs;
using NosCore.GameObject.Ecs.Interfaces;

namespace NosCore.GameObject.Services.MateService
{
public interface IMateAttackService
{
// The creature's own basic attack, gated by its BasicCooldown and BasicRange.
Task<bool> BasicAttackAsync(MateComponentBundle mate, IAliveEntity target);
Task<bool> BasicAttackAsync(INonPlayableEntity attacker, IAliveEntity target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface IMateControlService

Task<bool> TalkAsync(PlayerComponentBundle owner, long mateTransportId, string? message);

Task<bool> AttackAsync(PlayerComponentBundle owner, long mateTransportId, long castId, IAliveEntity target);
Task<bool> AttackAsync(PlayerComponentBundle owner, long mateTransportId, IAliveEntity target);
}
}
17 changes: 9 additions & 8 deletions src/NosCore.GameObject/Services/MateService/MateAttackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,34 +21,35 @@ public sealed class MateAttackService(IBattleService battleService, IHeuristic d
private const short BasicAttackKey = 0;
private const int MinimumCooldownMs = 200;

public async Task<bool> BasicAttackAsync(MateComponentBundle mate, IAliveEntity target)
public async Task<bool> 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<SkillCooldownComponent>(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))
{
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;
}
Expand Down
57 changes: 6 additions & 51 deletions src/NosCore.GameObject/Services/MateService/MateControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<bool> MoveAsync(PlayerComponentBundle owner, long mateTransportId, short x, short y)
{
if (!TryGetOutMate(owner, mateTransportId, out var mate, out var entity))
Expand Down Expand Up @@ -106,59 +98,22 @@ public async Task<bool> TalkAsync(PlayerComponentBundle owner, long mateTranspor
return true;
}

public async Task<bool> AttackAsync(PlayerComponentBundle owner, long mateTransportId, long castId,
IAliveEntity target)
public async Task<bool> 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<bool> StrikeAsync(IAliveEntity entity, short vNum, ConcurrentDictionary<short, Instant> 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.
Expand Down
2 changes: 1 addition & 1 deletion src/NosCore.PacketHandlers/Mates/SuCtlPacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion test/NosCore.EndToEnd.Tests/Infrastructure/WorldData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down
16 changes: 16 additions & 0 deletions test/NosCore.EndToEnd.Tests/Scenarios/PetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HitRequest> _hits = new();

[TestInitialize]
public async Task SetupAsync()
{
World = await TestWorld.CreateAsync();
_session = await World.CreateSessionAsync();
_map = _session.Character.MapInstance;

World.Inject(new List<SkillDto>
{
new() { SkillVNum = PetSkill, CastId = 0, Cooldown = 80, Range = 3, Effect = 791, AttackAnimation = 12, SkillType = 5 },
});
World.Inject(new List<NpcMonsterSkillDto>
{
new() { NpcMonsterSkillId = 1, NpcMonsterVNum = PetVNum, SkillVNum = PetSkill },
});
World.Inject(new List<BCardDto>
{
new() { SkillVNum = PetSkill, Type = BCardType.CardType.Buff, SubType = 11, FirstData = 100, SecondData = 1 },
});
World.UseReal<INpcCombatCatalog, NpcCombatCatalog>();
World.UseReal<ISkillResolver, SkillResolver>();
World.Freeze<Mock<ITargetResolver>>()
.Setup(r => r.Resolve(It.IsAny<IAliveEntity>(), It.IsAny<IAliveEntity>(), It.IsAny<SkillInfo>()))
.Returns<IAliveEntity, IAliveEntity, SkillInfo>((_, target, _) => new[] { target });
World.Freeze<Mock<IHitQueue>>()
.Setup(q => q.EnqueueAsync(It.IsAny<HitRequest>()))
.Callback<HitRequest>(_hits.Add)
.ReturnsAsync(new HitOutcome(HitStatus.Landed, 10, SuPacketHitMode.SuccessAttack, false));
World.Freeze<Mock<IBattleStatsProvider>>()
.Setup(s => s.GetStats(It.IsAny<IAliveEntity>()))
.Returns(new CombatStats());
World.UseReal<IBattleService, GameObject.Services.BattleService.BattleService>();

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<bool> ThePetSwingsAtTheMonsterAsync() =>
World.Create<MateAttackService>().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<SuPacket>().Single());
}
}
}
Loading