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

CombatGoal update to check if multiple mobs hit the player and run back to last safe spot. #632

Closed
wants to merge 3 commits into from
Closed
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
106 changes: 93 additions & 13 deletions Core/Goals/CombatGoal.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
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 System.Drawing;
using static System.MathF;
using SixLabors.ImageSharp.Formats;

namespace Core.Goals;

public sealed class CombatGoal : GoapGoal, IGoapEventListener
{
public override float Cost => 4f;
public DateTime LastSafeLocationTime = new DateTime();

Check warning on line 20 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 20 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)
LinkedList<Vector3> safeLocations = new LinkedList<Vector3>();

private bool runningAway;

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

View workflow job for this annotation

GitHub Actions / build

The field 'CombatGoal.runningAway' is never used

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

View workflow job for this annotation

GitHub Actions / build

The field 'CombatGoal.runningAway' is never used
private readonly ILogger<CombatGoal> logger;
private readonly ConfigurableInput input;
private readonly ClassConfiguration classConfig;
private readonly Wait wait;
private readonly Navigation playerNavigation;
private readonly PlayerReader playerReader;
private readonly AddonBits bits;
private readonly StopMoving stopMoving;
private readonly CastingHandler castingHandler;
private readonly IMountHandler mountHandler;
private readonly CombatLog combatLog;

private const float minAngleToTurn = PI / 35f; // 5.14 degree
private const float minAngleToStopBeforeTurn = PI / 2f; // 90 degree
private float lastDirection;
private float lastMinDistance;
private float lastMaxDistance;

public CombatGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
Wait wait, PlayerReader playerReader, StopMoving stopMoving, AddonBits bits,
ClassConfiguration classConfiguration, ClassConfiguration classConfig,
Navigation playerNavigation, ClassConfiguration classConfiguration, ClassConfiguration classConfig,
CastingHandler castingHandler, CombatLog combatLog,
IMountHandler mountHandler)
: base(nameof(CombatGoal))
Expand All @@ -40,15 +52,15 @@
this.playerReader = playerReader;
this.bits = bits;
this.combatLog = combatLog;

this.playerNavigation = playerNavigation;
this.stopMoving = stopMoving;
this.castingHandler = castingHandler;
this.mountHandler = mountHandler;
this.classConfig = classConfig;

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

Choose a reason for hiding this comment

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

Take a loook at this.

Expand Down Expand Up @@ -106,12 +118,7 @@
public override void Update()
{
wait.Update();

if (MathF.Abs(lastDirection - playerReader.Direction) > MathF.PI / 2)
{
logger.LogInformation("Turning too fast!");
stopMoving.Stop();
}
playerNavigation.Update();

lastDirection = playerReader.Direction;
lastMinDistance = playerReader.MinRange();
Expand All @@ -123,6 +130,55 @@
return;
}

if ((MathF.Abs(lastDirection - playerReader.Direction) > MathF.PI / 2))
{
logger.LogInformation("Turning too fast!");
stopMoving.Stop();

}
if (bits.Target() && !bits.Target_Alive())
{
input.PressClearTarget();
return;
}

if (combatLog.DamageTaken.Count > 1 || (playerReader.HealthPercent() < 50 && playerReader.ManaPercent() < 20) || (playerReader.HealthPercent() < 30))
{
// multiple mobs hitting us
// bail
Console.WriteLine("Multiple mob hits! OR HP and mana low");
Console.WriteLine(safeLocations.Count);
if (safeLocations.Count >= 1)
{
bool foundPoint = false;
Console.WriteLine("Current Pos: " + playerReader.MapPos.ToString());
Console.WriteLine("Safe Spots: " + safeLocations.Count);
for (LinkedListNode<Vector3> point = safeLocations.Last; point != null; point = point.Previous)

Check warning on line 156 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 156 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 156 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 156 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) >= 1.8)
{
// select the point far enough to lose the current mobs.
input.PressClearTarget();
playerNavigation.Stop();
playerNavigation.StuckResetTimeout = 500;

Check failure on line 165 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

'Navigation' does not contain a definition for 'StuckResetTimeout' and no accessible extension method 'StuckResetTimeout' accepting a first argument of type 'Navigation' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 165 in Core/Goals/CombatGoal.cs

View workflow job for this annotation

GitHub Actions / build

'Navigation' does not contain a definition for 'StuckResetTimeout' and no accessible extension method 'StuckResetTimeout' accepting a first argument of type 'Navigation' could be found (are you missing a using directive or an assembly reference?)
playerNavigation.ResetStuckParameters();
playerNavigation.SetWayPoints(stackalloc Vector3[] { (Vector3)(point.Value) });
playerNavigation.Update();
Console.WriteLine("Found point " + point.Value.ToString());
foundPoint = true;
break;
}
}
if (foundPoint)
{
Console.WriteLine("Running away to the last safe point!");
return;
}
}
}

if (bits.Target())
{
if (classConfig.AutoPetAttack &&
Expand Down Expand Up @@ -161,12 +217,36 @@
{
stopMoving.Stop();
FindNewTarget();
} else
{
if (LastSafeLocationTime == DateTime.MinValue)
{
LastSafeLocationTime = DateTime.UtcNow;
safeLocations.AddLast(playerReader.MapPos);
}
else
{
if ((DateTime.UtcNow - LastSafeLocationTime).TotalMilliseconds > 7_000 && !bits.Combat())
{
safeLocations.AddLast(playerReader.MapPos);
LastSafeLocationTime = DateTime.UtcNow;
if (safeLocations.Count > 100)
{
safeLocations.RemoveFirst();
}
}
}
Console.WriteLine("Target Dead2 -- saving safe pos " + playerReader.MapPos.ToString());
safeLocations.AddLast(playerReader.MapPos);
logger.LogWarning("---- Target dead, clearing");
input.PressClearTarget();
}
}
}

private void FindNewTarget()
{
playerNavigation.Stop();
if (playerReader.PetTarget() && combatLog.DeadGuid.Value != playerReader.PetTargetGuid)
{
ResetCooldowns();
Expand All @@ -177,7 +257,7 @@

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

Expand Down Expand Up @@ -216,4 +296,4 @@
{
return PointEstimator.GetPoint(playerReader.MapPos, playerReader.Direction, distance);
}
}
}
Loading