From 58e2411513db1183b821fcf8bd41a858d1ca9acb Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 10:44:40 +0000 Subject: [PATCH 1/3] Wait out a skill's cast time before it hits Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014gSmkTnTjQfc83H72tpiYm --- .../Ecs/Components/SkillCooldownComponent.cs | 3 +- .../Ecs/Interfaces/INonPlayableEntity.cs | 2 + src/NosCore.GameObject/Ecs/MapWorld.cs | 6 +-- .../Services/BattleService/BattleService.cs | 41 ++++++++++++++++ .../BattleService/CastDelayService.cs | 16 +++++++ .../BattleService/ICastDelayService.cs | 15 ++++++ .../Services/BattleService/Model/SkillInfo.cs | 5 +- .../Services/BattleService/MonsterAi.cs | 33 ++++++++++++- .../Services/BattleService/SkillResolver.cs | 4 +- .../AreaAndTargetListSkillTests.cs | 1 + .../BattleServiceLifetimeTests.cs | 1 + .../BattleService/BattleServiceTests.cs | 47 +++++++++++++++++++ .../Services/BattleService/MonsterAiTests.cs | 35 ++++++++++++++ 13 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 src/NosCore.GameObject/Services/BattleService/CastDelayService.cs create mode 100644 src/NosCore.GameObject/Services/BattleService/ICastDelayService.cs diff --git a/src/NosCore.GameObject/Ecs/Components/SkillCooldownComponent.cs b/src/NosCore.GameObject/Ecs/Components/SkillCooldownComponent.cs index 29a4ac4b54..02f4a8ceaf 100644 --- a/src/NosCore.GameObject/Ecs/Components/SkillCooldownComponent.cs +++ b/src/NosCore.GameObject/Ecs/Components/SkillCooldownComponent.cs @@ -11,4 +11,5 @@ namespace NosCore.GameObject.Ecs.Components; // Per-skill cooldown tracker for non-player entities (monsters, pets, NPCs). Characters // keep cooldown state on their CharacterSkill instances, so they don't need this. -public record struct SkillCooldownComponent(ConcurrentDictionary NextUsableAt); +// CastingUntil holds the entity still while a skill with a cast time winds up. +public record struct SkillCooldownComponent(ConcurrentDictionary NextUsableAt, Instant CastingUntil); diff --git a/src/NosCore.GameObject/Ecs/Interfaces/INonPlayableEntity.cs b/src/NosCore.GameObject/Ecs/Interfaces/INonPlayableEntity.cs index 36b4e6b262..cf38fdbaf9 100644 --- a/src/NosCore.GameObject/Ecs/Interfaces/INonPlayableEntity.cs +++ b/src/NosCore.GameObject/Ecs/Interfaces/INonPlayableEntity.cs @@ -27,6 +27,8 @@ public interface INonPlayableEntity : IAliveEntity CachedPath? ChasePath { get; set; } + Instant CastingUntil { get; set; } + new bool IsAlive { get; set; } new byte Speed { get; set; } diff --git a/src/NosCore.GameObject/Ecs/MapWorld.cs b/src/NosCore.GameObject/Ecs/MapWorld.cs index d8c923bc1f..98961ea1df 100644 --- a/src/NosCore.GameObject/Ecs/MapWorld.cs +++ b/src/NosCore.GameObject/Ecs/MapWorld.cs @@ -263,7 +263,7 @@ public Entity CreateMonster( new BuffStateComponent(new ConcurrentDictionary()), new AggroComponent(VisualType.Object, 0, 0, Instant.MinValue), new ChasePathComponent(null), - new SkillCooldownComponent(new ConcurrentDictionary()), + new SkillCooldownComponent(new ConcurrentDictionary(), Instant.MinValue), new HitListComponent(new ConcurrentDictionary()), new RespawnComponent(null) ); @@ -310,7 +310,7 @@ public Entity CreateNpc( new BuffStateComponent(new ConcurrentDictionary()), new AggroComponent(VisualType.Object, 0, 0, Instant.MinValue), new ChasePathComponent(null), - new SkillCooldownComponent(new ConcurrentDictionary()), + new SkillCooldownComponent(new ConcurrentDictionary(), Instant.MinValue), new HitListComponent(new ConcurrentDictionary()), new RespawnComponent(null) ); @@ -344,7 +344,7 @@ public Entity CreateMate( new BuffStateComponent(new ConcurrentDictionary()), new AggroComponent(VisualType.Object, 0, 0, Instant.MinValue), new ChasePathComponent(null), - new SkillCooldownComponent(new ConcurrentDictionary()), + new SkillCooldownComponent(new ConcurrentDictionary(), Instant.MinValue), new MateStateComponent(mate, mate.CharacterId), new HitListComponent(new ConcurrentDictionary()) ); diff --git a/src/NosCore.GameObject/Services/BattleService/BattleService.cs b/src/NosCore.GameObject/Services/BattleService/BattleService.cs index c05b7c94f4..4764a4f511 100644 --- a/src/NosCore.GameObject/Services/BattleService/BattleService.cs +++ b/src/NosCore.GameObject/Services/BattleService/BattleService.cs @@ -41,6 +41,7 @@ public sealed class BattleService( ISessionRegistry sessionRegistry, IClock clock, ICaptureService captureService, + ICastDelayService castDelayService, ILogger logger) : IBattleService, ISingletonService { public async Task Hit(IAliveEntity origin, IAliveEntity target, HitArguments arguments) @@ -75,6 +76,12 @@ public async Task Hit(IAliveEntity origin, IAliveEntity target, HitArgumen return true; } + if (!await CastAsync(origin, target, skill)) + { + await CancelAsync(origin, target); + return false; + } + var targets = targetResolver.Resolve(origin, target, skill); if (skill.HitsSeveral && arguments.ListedTargets is { Count: > 0 } listed) { @@ -106,6 +113,12 @@ public async Task HitArea(IAliveEntity origin, short mapX, short mapY, Hit return false; } + if (!await CastAsync(origin, null, skill)) + { + await CancelAsync(origin, null); + return false; + } + var presentation = new HitPresentation(mapX, mapY, true); var targets = targetResolver.ResolveArea(origin, mapInstance, mapX, mapY, skill); for (var i = 0; i < targets.Count; i++) @@ -249,6 +262,34 @@ private static Task BroadcastHitAsync(IAliveEntity origin, IAliveEntity target, // Where su draws the blow from and whether it is drawn as caught in an area. private readonly record struct HitPresentation(short X, short Y, bool InArea); + // The client sends the skill as soon as it is used and leaves the wind-up to the server: + // ct starts the cast animation, and the hit resolves once the cast time has passed and + // both sides can still fight. + private async Task CastAsync(IAliveEntity origin, IAliveEntity? target, SkillInfo skill) + { + if (skill.CastTime <= 0) + { + return true; + } + + if (origin.MapInstance != null) + { + await origin.MapInstance.SendPacketAsync(new CtPacket + { + VisualType = origin.VisualType, + VisualId = origin.VisualId, + TargetVisualType = (target ?? origin).VisualType, + TargetId = (target ?? origin).VisualId, + CastAnimationId = skill.CastAnimation, + CastEffect = skill.CastEffect, + SkillId = skill.SkillVnum + }); + } + + await castDelayService.WaitAsync(Duration.FromMilliseconds(skill.CastTime * 100)); + return target == null ? CanCast(origin) : CanAttack(origin, target); + } + private void ScheduleCooldownReset(IAliveEntity origin, SkillInfo skill) { if (origin is not PlayerComponentBundle character) return; diff --git a/src/NosCore.GameObject/Services/BattleService/CastDelayService.cs b/src/NosCore.GameObject/Services/BattleService/CastDelayService.cs new file mode 100644 index 0000000000..073f301c1e --- /dev/null +++ b/src/NosCore.GameObject/Services/BattleService/CastDelayService.cs @@ -0,0 +1,16 @@ +// __ _ __ __ ___ __ ___ ___ +// | \| |/__\ /' _/ / _//__\| _ \ __| +// | | ' | \/ |`._`.| \_| \/ | v / _| +// |_|\__|\__/ |___/ \__/\__/|_|_\___| +// + +using System.Threading.Tasks; +using NodaTime; +using NosCore.GameObject.Infastructure; + +namespace NosCore.GameObject.Services.BattleService; + +public sealed class CastDelayService : ICastDelayService, ISingletonService +{ + public Task WaitAsync(Duration castTime) => Task.Delay(castTime.ToTimeSpan()); +} diff --git a/src/NosCore.GameObject/Services/BattleService/ICastDelayService.cs b/src/NosCore.GameObject/Services/BattleService/ICastDelayService.cs new file mode 100644 index 0000000000..e2ee6d1e51 --- /dev/null +++ b/src/NosCore.GameObject/Services/BattleService/ICastDelayService.cs @@ -0,0 +1,15 @@ +// __ _ __ __ ___ __ ___ ___ +// | \| |/__\ /' _/ / _//__\| _ \ __| +// | | ' | \/ |`._`.| \_| \/ | v / _| +// |_|\__|\__/ |___/ \__/\__/|_|_\___| +// + +using System.Threading.Tasks; +using NodaTime; + +namespace NosCore.GameObject.Services.BattleService; + +public interface ICastDelayService +{ + Task WaitAsync(Duration castTime); +} diff --git a/src/NosCore.GameObject/Services/BattleService/Model/SkillInfo.cs b/src/NosCore.GameObject/Services/BattleService/Model/SkillInfo.cs index 177ab4d3dc..0ebad3071a 100644 --- a/src/NosCore.GameObject/Services/BattleService/Model/SkillInfo.cs +++ b/src/NosCore.GameObject/Services/BattleService/Model/SkillInfo.cs @@ -28,7 +28,10 @@ public sealed record SkillInfo( short Duration, short MpCost, IReadOnlyList BCards, - string? CellPattern = null) + string? CellPattern = null, + short CastAnimation = 0, + // Tenths of a second the caster winds up before the hit lands. + short CastTime = 0) { public bool IsAoe => HitType is TargetHitType.SingleAoeTargetHit or TargetHitType.AoeTargetHit diff --git a/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs b/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs index f244d86056..bfe7413939 100644 --- a/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs +++ b/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs @@ -100,6 +100,8 @@ public async Task TickAsync(INonPlayableEntity entity) { if (!entity.IsAlive || entity.NpcMonster == null) return false; + if (entity.CastingUntil > clock.GetCurrentInstant()) return true; + var target = CurrentTarget(entity); if (target == null && entity.NpcMonster.IsHostile) { @@ -359,7 +361,8 @@ private static bool SameGroup(NpcMonsterDto entity, NpcMonsterDto? other) SkillVnum: dto.SkillVNum, CastId: dto.CastId, SkillRange: Math.Max(1, (int)dto.Range), - CooldownMs: Math.Max(200, dto.Cooldown * 100)); + CooldownMs: Math.Max(200, dto.Cooldown * 100), + CastTimeMs: dto.CastTime * 100); } return null; } @@ -378,6 +381,20 @@ private async Task AttackAsync(INonPlayableEntity entity, IAliveEntity target, C return; } + if (skill.CastTimeMs > 0) + { + // The life loop must not wait out the cast: the monster stands still until it ends + // and the hit resolves on its own. + if (cooldowns != null) + { + cooldowns.Value.NextUsableAt[skill.SkillVnum] = now.Plus(Duration.FromMilliseconds(skill.CooldownMs)); + } + + entity.CastingUntil = now.Plus(Duration.FromMilliseconds(skill.CastTimeMs)); + _ = HitAfterCastAsync(entity, target, skill); + return; + } + var accepted = await battleService.Hit(entity, target, new HitArguments { SkillId = skill.CastId }); if (accepted && cooldowns != null) @@ -386,6 +403,18 @@ private async Task AttackAsync(INonPlayableEntity entity, IAliveEntity target, C } } + private async Task HitAfterCastAsync(INonPlayableEntity entity, IAliveEntity target, ChosenSkill skill) + { + try + { + await battleService.Hit(entity, target, new HitArguments { SkillId = skill.CastId }); + } + catch (Exception ex) + { + logger.LogError(ex, "Cast of skill {Skill} by {VisualId} failed", skill.SkillVnum, entity.VisualId); + } + } + // Walks a cached JPS path toward the goal (a target's cell, or the spawn cell to go // home), re-planning when the goal changed or the path ran out. Consumes `Speed / 2` // cells per tick: the 400ms life-loop cadence is already the rate limit. The path @@ -439,5 +468,5 @@ private async Task StepAlongPathAsync(INonPlayableEntity entity, short goa return true; } - private sealed record ChosenSkill(short SkillVnum, long CastId, int SkillRange, int CooldownMs); + private sealed record ChosenSkill(short SkillVnum, long CastId, int SkillRange, int CooldownMs, int CastTimeMs = 0); } diff --git a/src/NosCore.GameObject/Services/BattleService/SkillResolver.cs b/src/NosCore.GameObject/Services/BattleService/SkillResolver.cs index 096662889f..d9177ff0b7 100644 --- a/src/NosCore.GameObject/Services/BattleService/SkillResolver.cs +++ b/src/NosCore.GameObject/Services/BattleService/SkillResolver.cs @@ -149,6 +149,8 @@ private SkillInfo BuildInfo(SkillDto main, SkillDto? upgrade, long castId) Duration: main.Duration, MpCost: main.MpCost, BCards: _catalog.GetSkillBCards(main.SkillVNum), - CellPattern: main.CellPattern); + CellPattern: main.CellPattern, + CastAnimation: main.CastAnimation, + CastTime: main.CastTime); } } diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/AreaAndTargetListSkillTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/AreaAndTargetListSkillTests.cs index 4c8dfda648..76d4937a7a 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/AreaAndTargetListSkillTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/AreaAndTargetListSkillTests.cs @@ -71,6 +71,7 @@ public async Task SetupAsync() new Mock().Object, NodaTime.SystemClock.Instance, new Mock().Object, + new Mock().Object, NullLogger.Instance); _origin = new FakeEntity { Hp = 100, VisualId = 1 }; } diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceLifetimeTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceLifetimeTests.cs index 20dd958b90..daf0f71203 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceLifetimeTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceLifetimeTests.cs @@ -71,6 +71,7 @@ public async Task ACastSkillIsReadyAgainOnceItsCooldownHasRunOut() var service = new GameObject.Services.BattleService.BattleService( skillResolver.Object, targetResolver.Object, hitQueue.Object, new Mock().Object, World.SessionRegistry, clock, new Mock().Object, + new Mock().Object, NullLogger.Instance); Assert.IsTrue(await service.Hit(session.Character, monster, new HitArguments { SkillId = 7 })); diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceTests.cs index 7a19db7cb9..24666742bd 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/BattleServiceTests.cs @@ -34,6 +34,7 @@ public class BattleServiceTests private Mock _targetResolver; private Mock _hitQueue; private Mock _bus; + private Mock _castDelay; private GameObject.Services.BattleService.BattleService _service; [TestInitialize] @@ -43,6 +44,8 @@ public void Setup() _targetResolver = new Mock(); _hitQueue = new Mock(); _bus = new Mock(); + _castDelay = new Mock(); + _castDelay.Setup(d => d.WaitAsync(It.IsAny())).Returns(Task.CompletedTask); _service = new GameObject.Services.BattleService.BattleService( _skillResolver.Object, _targetResolver.Object, @@ -51,9 +54,53 @@ public void Setup() new Mock().Object, NodaTime.SystemClock.Instance, new Mock().Object, + _castDelay.Object, NullLogger.Instance); } + [TestMethod] + public async Task ASkillWithACastTimeHitsOnlyOnceTheCastEnds() + { + var origin = new FakeEntity { Hp = 100 }; + var target = new FakeEntity { Hp = 100, VisualType = VisualType.Monster }; + SetupLanding(origin, target, MakeSkill(TargetHitType.SingleTargetHit) with { CastTime = 10 }); + var cast = new TaskCompletionSource(); + _castDelay.Setup(d => d.WaitAsync(NodaTime.Duration.FromSeconds(1))).Returns(cast.Task); + + var hit = _service.Hit(origin, target, new HitArguments { SkillId = 1 }); + _hitQueue.Verify(q => q.EnqueueAsync(It.IsAny()), Times.Never); + + cast.SetResult(); + Assert.IsTrue(await hit); + _hitQueue.Verify(q => q.EnqueueAsync(It.IsAny()), Times.Once); + } + + [TestMethod] + public async Task ACastWhoseTargetDiesMeanwhileLandsNothing() + { + var origin = new FakeEntity { Hp = 100 }; + var target = new FakeEntity { Hp = 100, VisualType = VisualType.Monster }; + SetupLanding(origin, target, MakeSkill(TargetHitType.SingleTargetHit) with { CastTime = 10 }); + _castDelay.Setup(d => d.WaitAsync(It.IsAny())) + .Callback(() => target.Hp = 0) + .Returns(Task.CompletedTask); + + Assert.IsFalse(await _service.Hit(origin, target, new HitArguments { SkillId = 1 })); + _hitQueue.Verify(q => q.EnqueueAsync(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task ASkillWithoutACastTimeDoesNotWait() + { + var origin = new FakeEntity { Hp = 100 }; + var target = new FakeEntity { Hp = 100, VisualType = VisualType.Monster }; + SetupLanding(origin, target, MakeSkill(TargetHitType.SingleTargetHit)); + + await _service.Hit(origin, target, new HitArguments { SkillId = 1 }); + + _castDelay.Verify(d => d.WaitAsync(It.IsAny()), Times.Never); + } + [TestMethod] public async Task CannotAttackWhenAttackerIsDead() { diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs index 5892657045..b1a7e7cb5c 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs @@ -344,6 +344,41 @@ public async Task AGroupMemberIgnoresAFightBeyondItsNoticeRange() } // The shared fixture map declares 8 rows but ships 6, which the pathfinder walks off. + [TestMethod] + public async Task AMonsterWindingUpASkillStandsStillWithoutHoldingUpTheLoop() + { + var catalog = new Mock(); + catalog.Setup(c => c.GetSkills(1)).Returns(new List + { + new() { NpcMonsterVNum = 1, SkillVNum = 50 } + }); + var random = new Mock(); + random.Setup(r => r.Next(0, 10)).Returns(9); + var ai = new MonsterAi(_battle.Object, _aggro, + World.Create(), World.SessionRegistry, + World.DistanceCalculator, catalog.Object, random.Object, World.Clock, + new Dictionary { [50] = new() { SkillVNum = 50, CastId = 3, Range = 2, Cooldown = 100, CastTime = 10 } }, + NullLogger.Instance); + var cast = new TaskCompletionSource(); + _battle.Setup(b => b.Hit(It.IsAny(), It.IsAny(), It.Is(a => a.SkillId == 3))) + .Returns(cast.Task); + var monster = SpawnMonster(4, 1); + PlacePlayer(4, 3); + + Assert.IsTrue(await ai.TickAsync(monster)); + PlacePlayer(4, 6); + await ai.TickAsync(monster); + + Assert.AreEqual((short)1, monster.PositionY); + _battle.Verify(b => b.Hit(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + World.Clock.Advance(Duration.FromMilliseconds(1000)); + await ai.TickAsync(monster); + cast.SetResult(true); + + Assert.AreNotEqual((short)1, monster.PositionY); + } + private static GameObject.Map.Map TestMap() { var data = new List { 8, 0, 8, 0 }; From fbdb1044ca483ad8e06470af923eb04f0b888be7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 10:51:11 +0000 Subject: [PATCH 2/3] Add cast time and debuff resistance to the manual test plan Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014gSmkTnTjQfc83H72tpiYm --- documentation/manual-test-plan.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/documentation/manual-test-plan.md b/documentation/manual-test-plan.md index 40c499d1af..55b029f1a2 100644 --- a/documentation/manual-test-plan.md +++ b/documentation/manual-test-plan.md @@ -175,6 +175,24 @@ distance the walk check uses. is refused from two cells further away - [ ] Cast while walking: the hit lands and you are not pulled back to an old cell +### Cast time + +A skill's cast time (Skill.dat, tenths of a second) is waited out on the server, after a +`ct` that shows the wind-up. + +- [ ] Cast Slingshot (203, 1 s) at a monster: a cast bar and charging effect appear over the + character, and the damage lands about a second later, not at once +- [ ] A second character next to you sees the same wind-up +- [ ] Kill the target with another character during the wind-up: the cast ends without a hit +- [ ] A monster with a cast-time skill stops chasing while it winds up, then resumes + +### Debuff resistance + +- [ ] Let a monster whose skill causes Blackout hit you: Blackout lands on every hit that + connects +- [ ] Wear the Monkey Hat ("high chance of protection from Blackouts") and repeat: Blackout + lands only now and then (about one hit in ten) + ### Area skills and bystanders - [ ] Stand a second character next to a monster and cast an area skill at the monster: the From d918734625f1cd7297e64bbde6a9c97679107ca9 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 10:55:06 +0000 Subject: [PATCH 3/3] Keep a cast to its map and hold the monster until its hit resolves Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014gSmkTnTjQfc83H72tpiYm --- .../Services/BattleService/BattleService.cs | 8 +++++--- .../Services/BattleService/MonsterAi.cs | 13 ++++++++++--- .../Services/BattleService/MonsterAiTests.cs | 6 ++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/NosCore.GameObject/Services/BattleService/BattleService.cs b/src/NosCore.GameObject/Services/BattleService/BattleService.cs index 4764a4f511..6328653491 100644 --- a/src/NosCore.GameObject/Services/BattleService/BattleService.cs +++ b/src/NosCore.GameObject/Services/BattleService/BattleService.cs @@ -272,9 +272,10 @@ private async Task CastAsync(IAliveEntity origin, IAliveEntity? target, Sk return true; } - if (origin.MapInstance != null) + var map = origin.MapInstance; + if (map != null) { - await origin.MapInstance.SendPacketAsync(new CtPacket + await map.SendPacketAsync(new CtPacket { VisualType = origin.VisualType, VisualId = origin.VisualId, @@ -287,7 +288,8 @@ await origin.MapInstance.SendPacketAsync(new CtPacket } await castDelayService.WaitAsync(Duration.FromMilliseconds(skill.CastTime * 100)); - return target == null ? CanCast(origin) : CanAttack(origin, target); + return ReferenceEquals(origin.MapInstance, map) + && (target == null ? CanCast(origin) : CanAttack(origin, target)); } private void ScheduleCooldownReset(IAliveEntity origin, SkillInfo skill) diff --git a/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs b/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs index bfe7413939..f22b7f4d9f 100644 --- a/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs +++ b/src/NosCore.GameObject/Services/BattleService/MonsterAi.cs @@ -383,14 +383,14 @@ private async Task AttackAsync(INonPlayableEntity entity, IAliveEntity target, C if (skill.CastTimeMs > 0) { - // The life loop must not wait out the cast: the monster stands still until it ends - // and the hit resolves on its own. + // The life loop must not wait out the cast: the monster stands still until the hit + // has resolved on its own. if (cooldowns != null) { cooldowns.Value.NextUsableAt[skill.SkillVnum] = now.Plus(Duration.FromMilliseconds(skill.CooldownMs)); } - entity.CastingUntil = now.Plus(Duration.FromMilliseconds(skill.CastTimeMs)); + entity.CastingUntil = Instant.MaxValue; _ = HitAfterCastAsync(entity, target, skill); return; } @@ -413,6 +413,13 @@ private async Task HitAfterCastAsync(INonPlayableEntity entity, IAliveEntity tar { logger.LogError(ex, "Cast of skill {Skill} by {VisualId} failed", skill.SkillVnum, entity.VisualId); } + finally + { + if (entity.MapInstance?.EcsWorld.IsAlive(entity.Handle) == true) + { + entity.CastingUntil = Instant.MinValue; + } + } } // Walks a cached JPS path toward the goal (a target's cell, or the spawn cell to go diff --git a/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs b/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs index b1a7e7cb5c..643593f8c1 100644 --- a/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs +++ b/test/NosCore.GameObject.Tests/Services/BattleService/MonsterAiTests.cs @@ -345,7 +345,7 @@ public async Task AGroupMemberIgnoresAFightBeyondItsNoticeRange() // The shared fixture map declares 8 rows but ships 6, which the pathfinder walks off. [TestMethod] - public async Task AMonsterWindingUpASkillStandsStillWithoutHoldingUpTheLoop() + public async Task AMonsterWindingUpASkillStandsStillUntilItsHitResolvesWithoutHoldingUpTheLoop() { var catalog = new Mock(); catalog.Setup(c => c.GetSkills(1)).Returns(new List @@ -374,8 +374,10 @@ public async Task AMonsterWindingUpASkillStandsStillWithoutHoldingUpTheLoop() World.Clock.Advance(Duration.FromMilliseconds(1000)); await ai.TickAsync(monster); - cast.SetResult(true); + Assert.AreEqual((short)1, monster.PositionY, "the hit has not resolved yet"); + cast.SetResult(true); + await ai.TickAsync(monster); Assert.AreNotEqual((short)1, monster.PositionY); }