From a74a0eeb294e049e872a47da22078ae294fe266f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=82ngelo=20Tadeucci?= Date: Wed, 2 Apr 2025 01:28:52 -0300 Subject: [PATCH] Fix Npc Attacking No more spazzing out npcs attack you! --- .../Manager/Field/AgentNavigation.cs | 17 ++++++++++++++--- .../Actor/ActorStateComponent/MovementState.cs | 12 +----------- .../MovementState.SkillCast.cs | 15 +++------------ .../MovementStateStates/MovementState.Walk.cs | 4 +--- .../MovementState.SkillCastTask.cs | 2 -- .../Model/Field/Actor/FieldNpc.cs | 2 +- Maple2.Server.Game/Packets/NpcControlPacket.cs | 2 -- 7 files changed, 20 insertions(+), 34 deletions(-) diff --git a/Maple2.Server.Game/Manager/Field/AgentNavigation.cs b/Maple2.Server.Game/Manager/Field/AgentNavigation.cs index 86121694a..12b536879 100644 --- a/Maple2.Server.Game/Manager/Field/AgentNavigation.cs +++ b/Maple2.Server.Game/Manager/Field/AgentNavigation.cs @@ -170,6 +170,17 @@ public bool UpdatePosition() { return true; } + public bool UpdatePosition(Vector3 position) { + if (!field.FindNearestPoly(position, out _, out RcVec3f rcPosition)) { + Logger.Error("Failed to find valid position from {Source} => {Position}", npc.Position, rcPosition); + return false; + } + + agent.npos = rcPosition; + npc.Position = DotRecastHelper.FromNavMeshSpace(rcPosition); + return true; + } + public Vector3 GetAgentPosition() { return DotRecastHelper.FromNavMeshSpace(agent.npos); } @@ -229,7 +240,7 @@ public Vector3 GetRandomPatrolPoint() { return DotRecastHelper.FromNavMeshSpace(randomPt); } - public Vector3 FindClosestPoint(Vector3 point, int maxDistance, Vector3 fallback) { + public Vector3 FindRandomPointAround(Vector3 point, int maxDistance, Vector3 fallback) { if (!field.FindNearestPoly(point, out long closest, out RcVec3f position)) { return fallback; } @@ -244,8 +255,8 @@ public Vector3 FindClosestPoint(Vector3 point, int maxDistance, Vector3 fallback return DotRecastHelper.FromNavMeshSpace(randomPt); } - public Vector3 FindClosestPoint(Vector3 point, int maxDistance) { - return FindClosestPoint(point, maxDistance, DotRecastHelper.FromNavMeshSpace(agent.npos)); + public Vector3 FindRandomPointAround(Vector3 point, int maxDistance) { + return FindRandomPointAround(point, maxDistance, DotRecastHelper.FromNavMeshSpace(agent.npos)); } public bool PathTo(Vector3 goal) { diff --git a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs index 62eca3e80..1115aa043 100644 --- a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs +++ b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs @@ -149,14 +149,6 @@ private void Idle(string sequence = "") { } } - public void Died() { - SetState(ActorState.Dead); - - UpdateControl(); - - Velocity = new Vector3(0, 0, 0); - } - public void StateRegenEvent(string keyName) { switch (keyName) { case "end": @@ -196,9 +188,7 @@ public void Update(long tickCount) { Velocity = new Vector3(0, 0, 0); - if (actor.Stats.Values[BasicAttribute.Health].Current == 0) { - SetState(ActorState.Dead); - + if (actor.IsDead) { return; } diff --git a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs index 38d3296a5..6911216fe 100644 --- a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs +++ b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs @@ -1,6 +1,4 @@ - -using Maple2.Model.Common; -using Maple2.Model.Metadata; +using Maple2.Model.Metadata; using Maple2.Server.Game.Model.Skill; using System.Numerics; using static Maple2.Server.Game.Model.ActorStateComponent.TaskState; @@ -29,7 +27,7 @@ public void StateSkillEvent(string keyName) { actor.AppendDebugMessage(actor.AnimationState.PlayingSequence.Name); } - if (castSkill.Motion.AttackPoints.ContainsKey(keyName)) { + if (castSkill.Motion.AttackPoints.TryGetValue(keyName, out byte point)) { var targets = new List(); // TODO: change BattleState to track a list of targets for multi projectile skills & use that instead @@ -37,8 +35,6 @@ public void StateSkillEvent(string keyName) { targets.Add(actor.BattleState.Target); } - byte point = castSkill.Motion.AttackPoints[keyName]; - if (point != 0xFF) { actor.SkillState.SkillCastAttack(castSkill, point, targets); } else { @@ -126,12 +122,7 @@ private void StateSkillCastMoveUpdate(long tickCount, long tickDelta, float delt castMoveLastTick = castMoveTick; } - int searchRadius = Math.Max((int) Math.Ceiling(offset.Length()), 0) + 10; - - Vector3 lastPosition = actor.Position; - - actor.Navigation!.UpdatePosition(); - actor.Position = actor.Navigation.FindClosestPoint(newPosition, searchRadius, actor.Position); + actor.Navigation!.UpdatePosition(newPosition); float timeStep = 0; diff --git a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs index 243a276a1..4f619f6bf 100644 --- a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs +++ b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs @@ -69,10 +69,8 @@ public bool IsMovingToTarget() { private void StateWalkDirectionUpdate(long tickCount, long tickDelta, float delta) { Vector3 newPosition = actor.Position + delta * Speed * walkDirection; - int searchRadius = Math.Max((int) (delta * Speed * 1.1f), 10); - actor.Navigation!.UpdatePosition(); - actor.Position = actor.Navigation.FindClosestPoint(newPosition, searchRadius); + actor.Navigation!.UpdatePosition(newPosition); Velocity = Speed * walkDirection; diff --git a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs index 48ee5c626..3ef9422b0 100644 --- a/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs +++ b/Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs @@ -120,7 +120,5 @@ private void SkillCast(NpcSkillCastTask task, int id, short level, long uid, byt SetState(ActorState.PcSkill); stateSequence = sequence; - - return; } } diff --git a/Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs b/Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs index 350f6b08e..4fcdde04f 100644 --- a/Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs +++ b/Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs @@ -151,7 +151,7 @@ public override void Update(long tickCount) { } } - bool isSpawning = MovementState.State == ActorState.Spawn || MovementState.State == ActorState.Regen; + bool isSpawning = MovementState.State is ActorState.Spawn or ActorState.Regen; if (!isSpawning) { BattleState.Update(tickCount); diff --git a/Maple2.Server.Game/Packets/NpcControlPacket.cs b/Maple2.Server.Game/Packets/NpcControlPacket.cs index fd8861c70..61d7778c2 100644 --- a/Maple2.Server.Game/Packets/NpcControlPacket.cs +++ b/Maple2.Server.Game/Packets/NpcControlPacket.cs @@ -4,8 +4,6 @@ using Maple2.Server.Core.Constants; using Maple2.Server.Core.Packets; using Maple2.Server.Game.Model; -using Maple2.Server.Game.Model.State; -using Maple2.Tools.Extensions; using System.Numerics; namespace Maple2.Server.Game.Packets;