feat: add check for line of sight for range and skill attacks

This commit is contained in:
Иванов Иван 2024-08-18 17:32:29 +02:00
parent abadf90d4a
commit 5f19cc1f76
11 changed files with 50 additions and 34 deletions

View File

@ -47,11 +47,11 @@ namespace Client.Domain.AI
if (isEnabled && worldHandler.Hero != null) if (isEnabled && worldHandler.Hero != null)
{ {
states[currentState].Execute(); states[currentState].Execute();
foreach (var transition in locator.Get(Type).Build()) foreach (var transition in locator.Get(Type).Build(worldHandler, config, asyncPathMover))
{ {
if (transition.fromStates.ContainsKey(BaseState.Type.Any) && transition.toState != currentState || transition.fromStates.ContainsKey(currentState)) if (transition.fromStates.ContainsKey(BaseState.Type.Any) && transition.toState != currentState || transition.fromStates.ContainsKey(currentState))
{ {
if (transition.predicate(worldHandler, config, states[currentState])) if (transition.predicate(states[currentState]))
{ {
states[currentState].OnLeave(); states[currentState].OnLeave();
currentState = transition.toState; currentState = transition.toState;

View File

@ -27,10 +27,7 @@ namespace Client.Domain.AI.Combat
{ {
continue; continue;
} }
if (skill.IsReadyToUse && hero.VitalStats.Mp >= skill.Cost) return skill;
{
return skill;
}
} }
} }

View File

@ -12,25 +12,25 @@ namespace Client.Domain.AI.Combat
{ {
public class TransitionBuilder : TransitionBuilderInterface public class TransitionBuilder : TransitionBuilderInterface
{ {
public List<TransitionBuilderInterface.Transition> Build() public List<TransitionBuilderInterface.Transition> Build(WorldHandler worldHandler, Config config, AsyncPathMoverInterface pathMover)
{ {
if (transitions.Count == 0) if (transitions.Count == 0)
{ {
transitions = new List<TransitionBuilderInterface.Transition>() transitions = new List<TransitionBuilderInterface.Transition>()
{ {
new(new List<BaseState.Type>{BaseState.Type.Any}, BaseState.Type.Dead, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Any}, BaseState.Type.Dead, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.VitalStats.IsDead; return worldHandler.Hero.VitalStats.IsDead;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Dead}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Dead}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return !worldHandler.Hero.VitalStats.IsDead; return !worldHandler.Hero.VitalStats.IsDead;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Idle, BaseState.Type.MoveToTarget, BaseState.Type.Rest, BaseState.Type.MoveToSpot}, BaseState.Type.FindTarget, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Idle, BaseState.Type.MoveToTarget, BaseState.Type.Rest, BaseState.Type.MoveToSpot}, BaseState.Type.FindTarget, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -45,13 +45,13 @@ namespace Client.Domain.AI.Combat
return worldHandler.Hero.AttackerIds.Count > 0; return worldHandler.Hero.AttackerIds.Count > 0;
}), }),
new(new List<BaseState.Type>{BaseState.Type.FindTarget}, BaseState.Type.MoveToTarget, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.FindTarget}, BaseState.Type.MoveToTarget, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.HasValidTarget; return worldHandler.Hero.HasValidTarget;
}), }),
new(new List<BaseState.Type>{BaseState.Type.FindTarget}, BaseState.Type.MoveToSpot, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.FindTarget}, BaseState.Type.MoveToSpot, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -59,7 +59,7 @@ namespace Client.Domain.AI.Combat
return Helper.GetMobsToAttackByConfig(worldHandler, config, worldHandler.Hero).Count == 0 return Helper.GetMobsToAttackByConfig(worldHandler, config, worldHandler.Hero).Count == 0
&& !Helper.IsOnSpot(worldHandler, config, worldHandler.Hero); && !Helper.IsOnSpot(worldHandler, config, worldHandler.Hero);
}), }),
new(new List<BaseState.Type>{BaseState.Type.MoveToSpot}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.MoveToSpot}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -70,27 +70,27 @@ namespace Client.Domain.AI.Combat
return Helper.IsOnSpot(worldHandler, config, worldHandler.Hero); return Helper.IsOnSpot(worldHandler, config, worldHandler.Hero);
}), }),
new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return !worldHandler.Hero.HasValidTarget; return !worldHandler.Hero.HasValidTarget;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Idle}, BaseState.Type.Rest, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Idle}, BaseState.Type.Rest, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
}; };
return worldHandler.Hero.AttackerIds.Count == 0 && (worldHandler.Hero.VitalStats.HpPercent < config.Combat.RestStartPercentHp return worldHandler.Hero.AttackerIds.Count == 0 && (worldHandler.Hero.VitalStats.HpPercent < config.Combat.RestStartPercentHp
|| worldHandler.Hero.VitalStats.MpPercent < config.Combat.RestStartPecentMp); || worldHandler.Hero.VitalStats.MpPercent < config.Combat.RestStartPecentMp);
}), }),
new(new List<BaseState.Type>{BaseState.Type.Rest}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Rest}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.VitalStats.HpPercent >= config.Combat.RestEndPecentHp return worldHandler.Hero.VitalStats.HpPercent >= config.Combat.RestEndPecentHp
&& worldHandler.Hero.VitalStats.MpPercent >= config.Combat.RestEndPecentMp; && worldHandler.Hero.VitalStats.MpPercent >= config.Combat.RestEndPecentMp;
}), }),
new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Attack, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Attack, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -99,6 +99,11 @@ namespace Client.Domain.AI.Combat
return false; return false;
} }
if (!pathMover.Pathfinder.HasLineOfSight(worldHandler.Hero.Transform.Position, worldHandler.Hero.Target.Transform.Position))
{
return false;
}
if (config.Combat.SpoilIsPriority) { if (config.Combat.SpoilIsPriority) {
var spoil = worldHandler.GetSkillById(config.Combat.SpoilSkillId); var spoil = worldHandler.GetSkillById(config.Combat.SpoilSkillId);
if (spoil != null && !spoil.IsReadyToUse) { if (spoil != null && !spoil.IsReadyToUse) {
@ -109,21 +114,21 @@ namespace Client.Domain.AI.Combat
var distance = worldHandler.Hero.Transform.Position.HorizontalDistance(worldHandler.Hero.Target.Transform.Position); var distance = worldHandler.Hero.Transform.Position.HorizontalDistance(worldHandler.Hero.Target.Transform.Position);
return distance < Helper.GetAttackDistanceByConfig(worldHandler, config, worldHandler.Hero, worldHandler.Hero.Target); return distance < Helper.GetAttackDistanceByConfig(worldHandler, config, worldHandler.Hero, worldHandler.Hero.Target);
}), }),
new(new List<BaseState.Type>{BaseState.Type.Attack}, BaseState.Type.Pickup, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Attack}, BaseState.Type.Pickup, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return !worldHandler.Hero.HasValidTarget; return !worldHandler.Hero.HasValidTarget;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Attack}, BaseState.Type.FindTarget, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Attack}, BaseState.Type.FindTarget, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.HasValidTarget && worldHandler.Hero.AttackerIds.Count > 0 && !worldHandler.Hero.AttackerIds.Contains(worldHandler.Hero.TargetId); return worldHandler.Hero.HasValidTarget && worldHandler.Hero.AttackerIds.Count > 0 && !worldHandler.Hero.AttackerIds.Contains(worldHandler.Hero.TargetId);
}), }),
new(new List<BaseState.Type>{BaseState.Type.Pickup}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Pickup}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }

View File

@ -12,37 +12,39 @@ namespace Client.Domain.AI.Deleveling
{ {
public class TransitionBuilder : TransitionBuilderInterface public class TransitionBuilder : TransitionBuilderInterface
{ {
public List<TransitionBuilderInterface.Transition> Build()
// todo add MoveToDropState, SweepState
public List<TransitionBuilderInterface.Transition> Build(WorldHandler worldHandler, Config config, AsyncPathMoverInterface pathMover)
{ {
if (transitions.Count == 0) if (transitions.Count == 0)
{ {
transitions = new List<TransitionBuilderInterface.Transition>() transitions = new List<TransitionBuilderInterface.Transition>()
{ {
new(new List<BaseState.Type>{BaseState.Type.Any}, BaseState.Type.Dead, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Any}, BaseState.Type.Dead, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.VitalStats.IsDead; return worldHandler.Hero.VitalStats.IsDead;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Dead}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Dead}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return !worldHandler.Hero.VitalStats.IsDead; return !worldHandler.Hero.VitalStats.IsDead;
}), }),
new(new List<BaseState.Type>{BaseState.Type.FindGuard}, BaseState.Type.MoveToTarget, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.FindGuard}, BaseState.Type.MoveToTarget, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.Target != null; return worldHandler.Hero.Target != null;
}), }),
new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Idle, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.Idle, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
return worldHandler.Hero.Target == null; return worldHandler.Hero.Target == null;
}), }),
new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.AttackGuard, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.MoveToTarget}, BaseState.Type.AttackGuard, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -55,7 +57,7 @@ namespace Client.Domain.AI.Deleveling
var expectedDistance = config.Deleveling.AttackDistance; var expectedDistance = config.Deleveling.AttackDistance;
return distance < expectedDistance; return distance < expectedDistance;
}), }),
new(new List<BaseState.Type>{BaseState.Type.AttackGuard}, BaseState.Type.FindGuard, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.AttackGuard}, BaseState.Type.FindGuard, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }
@ -68,7 +70,7 @@ namespace Client.Domain.AI.Deleveling
var expectedDistance = config.Deleveling.AttackDistance; var expectedDistance = config.Deleveling.AttackDistance;
return distance >= expectedDistance; return distance >= expectedDistance;
}), }),
new(new List<BaseState.Type>{BaseState.Type.Idle}, BaseState.Type.FindGuard, (worldHandler, config, state) => { new(new List<BaseState.Type>{BaseState.Type.Idle}, BaseState.Type.FindGuard, (state) => {
if (worldHandler.Hero == null) { if (worldHandler.Hero == null) {
return false; return false;
} }

View File

@ -48,7 +48,7 @@ namespace Client.Domain.AI.State
} }
var skill = Helper.GetSkillByConfig(worldHandler, config, hero, hero.Target); var skill = Helper.GetSkillByConfig(worldHandler, config, hero, hero.Target);
if (skill != null) if (skill != null && skill.IsReadyToUse && hero.VitalStats.Mp >= skill.Cost)
{ {
worldHandler.RequestUseSkill(skill.Id, false, false); worldHandler.RequestUseSkill(skill.Id, false, false);
} }

View File

@ -25,7 +25,8 @@ namespace Client.Domain.AI.State
{ {
return; return;
} }
if (distance >= Helper.GetAttackDistanceByConfig(worldHandler, config, hero, target)) var hasLineOfSight = asyncPathMover.Pathfinder.HasLineOfSight(hero.Transform.Position, target.Transform.Position);
if (distance >= Helper.GetAttackDistanceByConfig(worldHandler, config, hero, target) || !hasLineOfSight)
{ {
asyncPathMover.MoveAsync(target.Transform.Position); asyncPathMover.MoveAsync(target.Transform.Position);
} }

View File

@ -14,16 +14,16 @@ namespace Client.Domain.AI
{ {
public readonly Dictionary<BaseState.Type, BaseState.Type> fromStates; public readonly Dictionary<BaseState.Type, BaseState.Type> fromStates;
public readonly BaseState.Type toState; public readonly BaseState.Type toState;
public readonly Func<WorldHandler, Config, BaseState, bool> predicate; public readonly Func<BaseState, bool> predicate;
public Transition(List<BaseState.Type> fromStates, BaseState.Type toState, Func<WorldHandler, Config, BaseState, bool>? predicate = null) public Transition(List<BaseState.Type> fromStates, BaseState.Type toState, Func<BaseState, bool>? predicate = null)
{ {
this.fromStates = fromStates.ToDictionary(x => x, x => x); this.fromStates = fromStates.ToDictionary(x => x, x => x);
this.toState = toState; this.toState = toState;
this.predicate = predicate != null ? predicate : (worldHandler, config, state) => { return true; }; this.predicate = predicate != null ? predicate : (state) => { return true; };
} }
} }
List<Transition> Build(); List<Transition> Build(WorldHandler worldHandler, Config config, AsyncPathMoverInterface pathMover);
} }
} }

View File

@ -11,6 +11,7 @@ namespace Client.Domain.Service
{ {
public interface AsyncPathMoverInterface public interface AsyncPathMoverInterface
{ {
public PathfinderInterface Pathfinder { get; }
public ObservableCollection<PathSegment> Path { get; } public ObservableCollection<PathSegment> Path { get; }
public Task<bool> MoveAsync(Vector3 location); public Task<bool> MoveAsync(Vector3 location);
public Task MoveUntilReachedAsync(Vector3 location); public Task MoveUntilReachedAsync(Vector3 location);

View File

@ -11,5 +11,6 @@ namespace Client.Domain.Service
public interface PathfinderInterface public interface PathfinderInterface
{ {
public List<PathSegment> FindPath(Vector3 start, Vector3 end); public List<PathSegment> FindPath(Vector3 start, Vector3 end);
public bool HasLineOfSight(Vector3 start, Vector3 end);
} }
} }

View File

@ -27,6 +27,7 @@ namespace Client.Infrastructure.Service
private readonly int nextNodeDistanceTolerance; private readonly int nextNodeDistanceTolerance;
private CancellationTokenSource? cancellationTokenSource; private CancellationTokenSource? cancellationTokenSource;
public PathfinderInterface Pathfinder => pathfinder;
public ObservableCollection<PathSegment> Path { get; private set; } = new ObservableCollection<PathSegment>(); public ObservableCollection<PathSegment> Path { get; private set; } = new ObservableCollection<PathSegment>();
public bool IsLocked { get; private set; } = false; public bool IsLocked { get; private set; } = false;

View File

@ -17,6 +17,9 @@ namespace Client.Infrastructure.Service
[DllImport("L2JGeoDataPathFinder.dll", CallingConvention = CallingConvention.Cdecl)] [DllImport("L2JGeoDataPathFinder.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern uint ReleasePath(IntPtr arrayPtr); private static extern uint ReleasePath(IntPtr arrayPtr);
[DllImport("L2JGeoDataPathFinder.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool HasLineOfSight(string geoDataDirectory, float startX, float startY, float startZ, float endX, float endY, ushort maxPassableHeight);
public L2jGeoDataPathfinder(string geodataDirectory, ushort maxPassableHeight) public L2jGeoDataPathfinder(string geodataDirectory, ushort maxPassableHeight)
{ {
this.geodataDirectory = geodataDirectory; this.geodataDirectory = geodataDirectory;
@ -48,6 +51,11 @@ namespace Client.Infrastructure.Service
return BuildPath(nodes); return BuildPath(nodes);
} }
public bool HasLineOfSight(Vector3 start, Vector3 end)
{
return HasLineOfSight(GetGeodataFullpath(), start.X, start.Y, start.Z, end.X, end.Y, maxPassableHeight);
}
private List<PathSegment> BuildPath(List<PathNode> nodes) private List<PathSegment> BuildPath(List<PathNode> nodes)
{ {
var result = new List<PathSegment>(); var result = new List<PathSegment>();