Skip to content

Commit

Permalink
Update Combat Flee to use the Navigation GoalComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
lionaneesh authored Dec 5, 2024
1 parent bea1bca commit 852710d
Showing 1 changed file with 58 additions and 66 deletions.
124 changes: 58 additions & 66 deletions Core/Goals/CombatGoal.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Core.GOAP;
using Core.GOAP;

using Microsoft.Extensions.Logging;

using SharpDX.WIC;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading;
using static System.MathF;
Expand All @@ -15,9 +16,9 @@ public sealed class CombatGoal : GoapGoal, IGoapEventListener
{
public override float Cost => 4f;
public DateTime LastSafeLocationTime = new DateTime();

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

View workflow job for this annotation

GitHub Actions / build

Member 'LastSafeLocationTime' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)

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

View workflow job for this annotation

GitHub Actions / build

Member 'LastSafeLocationTime' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)
Queue safeLocations = new Queue();

LinkedList<Vector3> safeLocations = new LinkedList<Vector3>();

private bool runningAway;
private readonly ILogger<CombatGoal> logger;
private readonly ConfigurableInput input;
private readonly ClassConfiguration classConfig;
Expand All @@ -44,13 +45,14 @@ public CombatGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
{
this.logger = logger;
this.input = input;
this.runningAway = false;

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

playerNavigation.OnWayPointReached += Flee_SafePointReached;
this.stopMoving = stopMoving;
this.castingHandler = castingHandler;
this.mountHandler = mountHandler;
Expand Down Expand Up @@ -116,38 +118,42 @@ public override void OnExit()
public override void Update()
{
wait.Update();
if (runningAway)
{
if (!bits.Combat())
{
runningAway = false;
Console.WriteLine("NO longer in combat. We are safe!");
safeLocations.AddLast(playerReader.MapPos);
playerNavigation.Stop();
}
else
{
Console.WriteLine("Still running!");
input.PressClearTarget();
playerNavigation.Update();
return;
}
}
if (combatLog.DamageTaken.Count > 1)
{
// multiple mobs hitting us
// bail
Console.WriteLine("Multiple mob hits!");
Console.WriteLine(safeLocations.Count);
CancellationToken token = new CancellationToken();

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

View workflow job for this annotation

GitHub Actions / build

The variable 'token' is assigned but its value is never used

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

View workflow job for this annotation

GitHub Actions / build

The variable 'token' is assigned but its value is never used
if (safeLocations.Count > 1)
if (safeLocations.Count >= 1)
{
float heading = DirectionCalculator.CalculateMapHeading(playerReader.MapPos, (Vector3)safeLocations.ToArray()[1]);
safeLocations.Dequeue();
safeLocations.Dequeue();
runningAway = true;
Console.WriteLine("Current Pos: " + playerReader.MapPos.ToString());
Console.WriteLine("Last safe: " + safeLocations.Last().ToString());
playerNavigation.SetWayPoints(stackalloc Vector3[] { (Vector3)(safeLocations.Last()) });
safeLocations.RemoveLast();
input.PressClearTarget();
Console.WriteLine("Running away!");
float diff1 = Abs(Tau + heading - playerReader.Direction) % Tau;
float diff2 = Abs(heading - playerReader.Direction - Tau) % Tau;
float diff = Min(diff1, diff2);
if (diff > minAngleToTurn)
{
if (diff > minAngleToStopBeforeTurn)
{
stopMoving.Stop();
}

ConsoleKey directionKey = (Tau + heading - playerReader.Direction) % Tau < PI
? input.TurnLeftKey : input.TurnRightKey;
float result = (Tau + heading - playerReader.Direction) % Tau;
float amount = result > PI ? Tau - result : result;
int duration = (int)(amount * 1000 / PI);
input.PressFixed(directionKey, duration, token);
input.PressFixed(ConsoleKey.W, 11_000, token);
}
Console.WriteLine("Running away to the last safe point!");
} else
{
Console.WriteLine("No safe points to run, just fight!");
}
}
if (MathF.Abs(lastDirection - playerReader.Direction) > MathF.PI / 2)
Expand All @@ -170,25 +176,6 @@ public override void Update()
{
if (bits.Target_Dead())
{
Console.WriteLine("Target Dead -- saving safe pos");
if (LastSafeLocationTime == DateTime.MinValue)
{
LastSafeLocationTime = DateTime.UtcNow;
safeLocations.Enqueue(playerReader.MapPos);
}
else
{
if ((DateTime.UtcNow - LastSafeLocationTime).TotalMilliseconds > 5_000 && !bits.Combat())
{
safeLocations.Enqueue(playerReader.MapPos);
LastSafeLocationTime = DateTime.UtcNow;
if (safeLocations.Count > 5)
{
safeLocations.Dequeue();
}
}
}
logger.LogWarning("---- Target dead, clearing");
input.PressClearTarget();
}
if (classConfig.AutoPetAttack &&
Expand Down Expand Up @@ -229,30 +216,35 @@ public override void Update()
FindNewTarget();
} else
{
Console.WriteLine("Target Dead -- saving safe pos");
Console.WriteLine(bits.Combat());
if (LastSafeLocationTime == DateTime.MinValue)
{
LastSafeLocationTime = DateTime.UtcNow;
}
else
{
if ((DateTime.UtcNow - LastSafeLocationTime).TotalMilliseconds > 4_000)
{
safeLocations.Enqueue(playerReader.MapPos);
LastSafeLocationTime = DateTime.UtcNow;
if (safeLocations.Count > 5)
{
safeLocations.Dequeue();
}
}
}
Console.WriteLine("Target Dead2 -- saving safe pos " + playerReader.MapPos.ToString());
safeLocations.AddLast(playerReader.MapPos);
logger.LogWarning("---- Target dead, clearing");
input.PressClearTarget();
}
}
}

private void Flee_SafePointReached()
{
Console.WriteLine("Safepoint reached, checking if combat cleared?");
if (!bits.Combat()) {
Console.WriteLine("We are safe!");
runningAway = false;
} else
{
Console.WriteLine("Still not safe, run to next safepoint!");
if (safeLocations.Count >= 1)
{
playerNavigation.SetWayPoints(stackalloc Vector3[] { (Vector3)(safeLocations.Last()) });
input.PressClearTarget();
} else
{
Console.WriteLine("No more safepoints, Fight back!");
runningAway = false;
playerNavigation.Stop();
}
}
}
private void FindNewTarget()
{
if (playerReader.PetTarget() && combatLog.DeadGuid.Value != playerReader.PetTargetGuid)
Expand All @@ -265,7 +257,7 @@ private void FindNewTarget()

if (!bits.Target_Dead())
{
logger.LogWarning("---- New targe from Pet target!");
logger.LogWarning("---- New target from Pet target!");
return;
}

Expand Down

0 comments on commit 852710d

Please sign in to comment.