Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FleeGoal #636

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions Core/GOAP/GoapAgentState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace Core.GOAP;
namespace Core.GOAP;

public sealed class GoapAgentState
{
Expand Down
5 changes: 5 additions & 0 deletions Core/Goals/CombatGoal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;

using System;
using System.Collections.Generic;
using System.Numerics;

namespace Core.Goals;
Expand Down Expand Up @@ -162,6 +163,10 @@ public override void Update()
stopMoving.Stop();
FindNewTarget();
}
else
{
input.PressClearTarget();
}
}
}

Expand Down
119 changes: 119 additions & 0 deletions Core/Goals/FleeGoal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Core.GOAP;

using Microsoft.Extensions.Logging;

using System;
using System.Buffers;
using System.Numerics;

namespace Core.Goals;

public sealed class FleeGoal : GoapGoal, IRouteProvider
{
public override float Cost => 3.1f;

private readonly ILogger<CombatGoal> logger;
private readonly ConfigurableInput input;
private readonly ClassConfiguration classConfig;
private readonly Wait wait;
private readonly PlayerReader playerReader;
private readonly Navigation navigation;
private readonly AddonBits bits;
private readonly CombatLog combatLog;

private readonly SafeSpotCollector safeSpotCollector;

private Vector3[] MapPoints = [];

public int MOB_COUNT = 1;

public FleeGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
Wait wait, PlayerReader playerReader, AddonBits bits,
ClassConfiguration classConfiguration, Navigation playerNavigation,
ClassConfiguration classConfig, CombatLog combatLog,
SafeSpotCollector safeSpotCollector)
: base(nameof(FleeGoal))
{
this.logger = logger;
this.input = input;

this.wait = wait;
this.playerReader = playerReader;
this.navigation = playerNavigation;
this.bits = bits;
this.combatLog = combatLog;

this.classConfig = classConfig;

AddPrecondition(GoapKey.incombat, true);

Keys = classConfiguration.Combat.Sequence;

// this will ensure that the component is created
this.safeSpotCollector = safeSpotCollector;
}

#region IRouteProvider

public DateTime LastActive => navigation.LastActive;

public Vector3[] MapRoute() => MapPoints;

public Vector3[] PathingRoute()
{
return navigation.TotalRoute;
}

public bool HasNext()
{
return navigation.HasNext();
}

public Vector3 NextMapPoint()
{
return navigation.NextMapPoint();
}

#endregion

public override bool CanRun()
{
return
safeSpotCollector.Locations.Count > 0 &&
combatLog.DamageTakenCount() > MOB_COUNT;
}

public override void OnEnter()
{
// TODO: might have to do some pre processing like
// straightening the path like
// PathSimplify.SimplifyPath(MapPoints);
var count = safeSpotCollector.Locations.Count;
MapPoints = new Vector3[count];

safeSpotCollector.Locations.CopyTo(MapPoints, 0);

navigation.SetWayPoints(MapPoints.AsSpan(0, count));
navigation.ResetStuckParameters();
}

public override void OnExit()
{
// TODO: there might be better options here to dont clear all of them
safeSpotCollector.Locations.Clear();

navigation.Stop();
navigation.StopMovement();
}

public override void Update()
{
if (bits.Target())
{
input.PressClearTarget();
}

wait.Update();
navigation.Update();
}
}
46 changes: 46 additions & 0 deletions Core/GoalsComponent/SafeSpotCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Core.GOAP;

using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading;

namespace Core.Goals;

public sealed class SafeSpotCollector : IDisposable
{
private readonly PlayerReader playerReader;
private readonly AddonBits bits;

private readonly Timer timer;

public Stack<Vector3> Locations { get; } = new();

public SafeSpotCollector(
PlayerReader playerReader,
AddonBits bits)
{
this.playerReader = playerReader;
this.bits = bits;

timer = new(Update, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
}

public void Dispose()
{
timer.Dispose();
}

public void Update(object? obj)
{
if (bits.Combat())
return;

if (Locations.TryPeek(out var lastPos) &&
lastPos == playerReader.MapPosNoZ)
return;

// TODO: might be a good idea to check distance to last location
Locations.Push(playerReader.MapPosNoZ);
}
}
3 changes: 3 additions & 0 deletions Core/GoalsFactory/GoalFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static IServiceProvider Create(
services.AddScoped<CastingHandler>();
services.AddScoped<StuckDetector>();
services.AddScoped<CombatUtil>();
services.AddScoped<SafeSpotCollector>();

var playerReader = sp.GetRequiredService<PlayerReader>();

Expand All @@ -83,6 +84,7 @@ public static IServiceProvider Create(
{
services.AddScoped<GoapGoal, WalkToCorpseGoal>();
services.AddScoped<GoapGoal, CombatGoal>();
services.AddScoped<GoapGoal, FleeGoal>();
services.AddScoped<GoapGoal, ApproachTargetGoal>();
services.AddScoped<GoapGoal, WaitForGatheringGoal>();
ResolveFollowRouteGoal(services, classConfig);
Expand Down Expand Up @@ -136,6 +138,7 @@ public static IServiceProvider Create(
services.AddScoped<GoapGoal, WalkToCorpseGoal>();
services.AddScoped<GoapGoal, PullTargetGoal>();
services.AddScoped<GoapGoal, ApproachTargetGoal>();
services.AddScoped<GoapGoal, FleeGoal>();
services.AddScoped<GoapGoal, CombatGoal>();

if (classConfig.WrongZone.ZoneId > 0)
Expand Down