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

Merged
merged 6 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion Core/GOAP/GoapAgentState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

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

namespace Core.GOAP;

public sealed class GoapAgentState
Expand All @@ -9,4 +11,5 @@ public sealed class GoapAgentState
public int ConsumableCorpseCount { get; set; }
public int LastCombatKillCount { get; set; }
public bool Gathering { get; set; }
public LinkedList<Vector3> safeLocations = new LinkedList<Vector3>();
}
Copy link
Owner

@Xian55 Xian55 Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my humble opinion, making safeLocations as a LinkedList<Vector3> seems like an overkill.

In my humble opinion, a safe spot would be a location what the player was already stepped on in the recent past.

While the player is outside of combat, periodically X seconds or X milliseconds, a location can be marked as safe.

Upon entering the FleeGoal the goal execution would be really simple, from a Stack collection just pops safe locations until leaves combat.

There should be some guard clause that the safeLocations should contain about Y seconds worth of time in order to make sure that do not run out of safeLocations this can be checked in the CanRun().

Basically safeLocations.Count > 0

Essentially while the safeLocations has content we do not need to reach for the Navigation Component.

It should be a good idea to find out, after entering combat with a mob, how many seconds does it take till the enemy exit combat.

36 changes: 35 additions & 1 deletion 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 All @@ -21,6 +22,7 @@
private readonly CastingHandler castingHandler;
private readonly IMountHandler mountHandler;
private readonly CombatLog combatLog;
private readonly GoapAgentState goapAgentState;

private float lastDirection;
private float lastMinDistance;
Expand All @@ -29,7 +31,7 @@
public CombatGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
Wait wait, PlayerReader playerReader, StopMoving stopMoving, AddonBits bits,
ClassConfiguration classConfiguration, ClassConfiguration classConfig,
CastingHandler castingHandler, CombatLog combatLog,
CastingHandler castingHandler, CombatLog combatLog, GoapAgentState state,
IMountHandler mountHandler)
: base(nameof(CombatGoal))
{
Expand All @@ -46,6 +48,8 @@
this.mountHandler = mountHandler;
this.classConfig = classConfig;

this.goapAgentState = state;

AddPrecondition(GoapKey.incombat, true);
AddPrecondition(GoapKey.hastarget, true);
AddPrecondition(GoapKey.targetisalive, true);
Expand Down Expand Up @@ -162,6 +166,36 @@
stopMoving.Stop();
FindNewTarget();
}
else
{
// dont save too close points
logger.LogInformation("Target(s) Dead -- trying saving safe pos " + playerReader.MapPosNoZ.ToString());
bool foundClosePoint = false;
if (this.goapAgentState.safeLocations == null)
{
return;
}
for (LinkedListNode<Vector3> point = this.goapAgentState.safeLocations.Last; point != null; point = point.Previous)

Check warning on line 178 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 178 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
{
Vector2 p1 = new Vector2(point.Value.X, point.Value.Y);
Vector2 p2 = new Vector2(playerReader.MapPos.X, playerReader.MapPos.Y);
if (Vector2.Distance(p1, p2) <= 0.1)
{
foundClosePoint = true;
break;
}
}
if (!foundClosePoint)
{
Console.WriteLine("Saving safepos: " + playerReader.MapPosNoZ.ToString());
this.goapAgentState.safeLocations.AddLast(playerReader.MapPosNoZ);
if (this.goapAgentState.safeLocations.Count > 100)
{
this.goapAgentState.safeLocations.RemoveFirst();
}
}
input.PressClearTarget();
}
}
}

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

using Microsoft.Extensions.Logging;

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

namespace Core.Goals;

public sealed class FleeGoal : GoapGoal
{
public override float Cost => 4f;
private readonly ILogger<CombatGoal> logger;
private readonly ConfigurableInput input;
private readonly ClassConfiguration classConfig;
private readonly Wait wait;
private readonly PlayerReader playerReader;
private readonly Navigation playerNavigation;
private readonly AddonBits bits;
private readonly StopMoving stopMoving;
private readonly CastingHandler castingHandler;
private readonly IMountHandler mountHandler;
private readonly CombatLog combatLog;
private readonly GoapAgentState goapAgentState;
public int MOB_COUNT = 1;
public bool runningAway;

public FleeGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
Wait wait, PlayerReader playerReader, StopMoving stopMoving, AddonBits bits,
ClassConfiguration classConfiguration, Navigation playerNavigation, GoapAgentState state,
ClassConfiguration classConfig, CastingHandler castingHandler, CombatLog combatLog,
IMountHandler mountHandler)
: base(nameof(CombatGoal))
{
this.logger = logger;
this.input = input;

this.runningAway = false;
this.wait = wait;
this.playerReader = playerReader;
this.playerNavigation = playerNavigation;
this.bits = bits;
this.combatLog = combatLog;

this.stopMoving = stopMoving;
this.castingHandler = castingHandler;
this.mountHandler = mountHandler;
this.classConfig = classConfig;
this.goapAgentState = state;

AddPrecondition(GoapKey.incombat, true);
//AddPrecondition(GoapKey.hastarget, true);
//AddPrecondition(GoapKey.targetisalive, true);
//AddPrecondition(GoapKey.targethostile, true);
//AddPrecondition(GoapKey.targettargetsus, true);
//AddPrecondition(GoapKey.incombatrange, true);

//AddEffect(GoapKey.producedcorpse, true);
//AddEffect(GoapKey.targetisalive, false);
//AddEffect(GoapKey.hastarget, false);

Keys = classConfiguration.Combat.Sequence;
}

private void ResetCooldowns()
{
ReadOnlySpan<KeyAction> span = Keys;
for (int i = 0; i < span.Length; i++)
{
KeyAction keyAction = span[i];
if (keyAction.ResetOnNewTarget)
{
keyAction.ResetCooldown();
keyAction.ResetCharges();
}
}
}

public override void OnEnter()
{
if (mountHandler.IsMounted())
{
mountHandler.Dismount();
}

this.runningAway = false;
playerNavigation.Stop();
playerNavigation.SetWayPoints(stackalloc Vector3[] { });
playerNavigation.ResetStuckParameters();
}

public override void OnExit()
{
if (combatLog.DamageTakenCount() > 0 && !bits.Target())
{
stopMoving.Stop();
}
// clearing
this.runningAway = false;
playerNavigation.Stop();
playerNavigation.SetWayPoints(stackalloc Vector3[] { });
playerNavigation.ResetStuckParameters();
}

public override void Update()
{
wait.Update();
if (this.goapAgentState.safeLocations.Count >= MOB_COUNT && this.runningAway == false)
{

bool foundPoint = false;
logger.LogInformation("Flee Goal Activated. Current Pos: " + playerReader.MapPos.ToString() + ",Safe Spots: " + goapAgentState.safeLocations.Count);
if (goapAgentState.safeLocations == null)
{
return;
}
for (LinkedListNode<Vector3> point = goapAgentState.safeLocations.Last; point != null; point = point.Previous)

Check warning on line 118 in Core/Goals/FleeGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 118 in Core/Goals/FleeGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 118 in Core/Goals/FleeGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 118 in Core/Goals/FleeGoal.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
{
Vector2 p1 = new Vector2(point.Value.X, point.Value.Y);
Vector2 p2 = new Vector2(playerReader.MapPos.X, playerReader.MapPos.Y);
if (Vector2.Distance(p1, p2) >= 2.2)
{
// select the point far enough to lose the current mobs.
input.PressClearTarget();
playerNavigation.Stop();
playerNavigation.ResetStuckParameters();
playerNavigation.SetWayPoints(stackalloc Vector3[] { (Vector3)(point.Value) });
playerNavigation.Update();
Console.WriteLine("Found point " + point.Value.ToString());
foundPoint = true;
this.runningAway = true;
break;
}
}
if (foundPoint)
{
logger.LogInformation("Running away to the last safe point!");
return;
}
else
{
logger.LogInformation("Cant run away, figting!");
}
}
}
}
1 change: 1 addition & 0 deletions Core/GoalsFactory/GoalFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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