Skip to content

Commit

Permalink
Refactor: GoapAgentState: no longer needed
Browse files Browse the repository at this point in the history
Refactor: SafeSpotCollector: stores the spots.

More todos
  • Loading branch information
Xian55 committed Dec 12, 2024
1 parent f063ae9 commit 46a178d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
7 changes: 1 addition & 6 deletions Core/GOAP/GoapAgentState.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Numerics;

namespace Core.GOAP;
namespace Core.GOAP;

public sealed class GoapAgentState
{
Expand All @@ -11,6 +8,4 @@ public sealed class GoapAgentState
public int ConsumableCorpseCount { get; set; }
public int LastCombatKillCount { get; set; }
public bool Gathering { get; set; }

public Stack<Vector3> SafeLocations { get; } = new();
}
15 changes: 7 additions & 8 deletions Core/Goals/FleeGoal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public sealed class FleeGoal : GoapGoal, IRouteProvider
private readonly Navigation navigation;
private readonly AddonBits bits;
private readonly CombatLog combatLog;
private readonly GoapAgentState goapAgentState;

private readonly SafeSpotCollector safeSpotCollector;

Expand All @@ -30,7 +29,7 @@ public sealed class FleeGoal : GoapGoal, IRouteProvider

public FleeGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
Wait wait, PlayerReader playerReader, AddonBits bits,
ClassConfiguration classConfiguration, Navigation playerNavigation, GoapAgentState state,
ClassConfiguration classConfiguration, Navigation playerNavigation,
ClassConfiguration classConfig, CombatLog combatLog,
SafeSpotCollector safeSpotCollector)
: base(nameof(FleeGoal))
Expand All @@ -45,7 +44,6 @@ public FleeGoal(ILogger<CombatGoal> logger, ConfigurableInput input,
this.combatLog = combatLog;

this.classConfig = classConfig;
this.goapAgentState = state;

AddPrecondition(GoapKey.incombat, true);

Expand Down Expand Up @@ -81,18 +79,19 @@ public Vector3 NextMapPoint()
public override bool CanRun()
{
return
goapAgentState.SafeLocations.Count > 0 &&
safeSpotCollector.Locations.Count > 0 &&
combatLog.DamageTakenCount() > MOB_COUNT;
}

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

goapAgentState.SafeLocations.CopyTo(MapPoints, 0);
safeSpotCollector.Locations.CopyTo(MapPoints, 0);

navigation.SetWayPoints(MapPoints.AsSpan(0, count));
navigation.ResetStuckParameters();
Expand All @@ -101,7 +100,7 @@ public override void OnEnter()
public override void OnExit()
{
// TODO: there might be better options here to dont clear all of them
goapAgentState.SafeLocations.Clear();
safeSpotCollector.Locations.Clear();

navigation.Stop();
navigation.StopMovement();
Expand Down
12 changes: 7 additions & 5 deletions Core/GoalsComponent/SafeSpotCollector.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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 GoapAgentState state;
private readonly AddonBits bits;

private readonly Timer timer;

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

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

timer = new(Update, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
Expand All @@ -35,10 +36,11 @@ public void Update(object? obj)
if (bits.Combat())
return;

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

state.SafeLocations.Push(playerReader.MapPosNoZ);
// TODO: might be a good idea to check distance to last location
Locations.Push(playerReader.MapPosNoZ);
}
}

0 comments on commit 46a178d

Please sign in to comment.