Skip to content

Commit

Permalink
Add FleeGoal
Browse files Browse the repository at this point in the history
A goal which can be configured to run away to a possible safepoint if X mobs attack.
  • Loading branch information
lionaneesh committed Dec 11, 2024
1 parent 946035f commit eda5c45
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
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>();
}
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 @@ public sealed class CombatGoal : GoapGoal, IGoapEventListener
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 sealed class CombatGoal : GoapGoal, IGoapEventListener
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 @@ public CombatGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
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 @@ public override void Update()
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)
{
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)
{
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

0 comments on commit eda5c45

Please sign in to comment.