Skip to content

Commit

Permalink
dirty hoard search logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukkales committed Apr 10, 2024
1 parent 9babbc7 commit 2f8cceb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 42 deletions.
104 changes: 82 additions & 22 deletions HoardFarm/Service/HoardFarmService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Timers;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Utility;
using ECommons.GameHelpers;
using HoardFarm.IPC;
using HoardFarm.Model;
using HoardFarm.Tasks;
using Lumina.Excel.GeneratedSheets;
Expand All @@ -23,9 +27,10 @@ public class HoardFarmService : IDisposable
private bool hoardAvailable;
private bool intuitionUsed;
private bool movingToHoard;
private bool searchMode;
private Vector3 hoardPosition = Vector3.Zero;
// private Collection<uint> chestIds = [];
// private Collection<uint> visitedChestIds = [];
private readonly List<uint> chestIds = [];
private readonly List<uint> visitedChestIds = [];
private readonly string hoardFoundMessage;
private readonly string senseHoardMessage;
private readonly string noHoardMessage;
Expand Down Expand Up @@ -94,10 +99,61 @@ private void Reset()
hoardPosition = Vector3.Zero;
movingToHoard = false;
hoardAvailable = false;
searchMode = false;
}

private unsafe bool SearchLogic()
{
HoardModeStatus = "Searching";
chestIds.Clear();
var chests = ObjectTable.Where(e => ChestIDs.Contains(e.DataId)).Select(e => e.ObjectId).Distinct().ToList();
chests.ForEach(e =>
{
if (!visitedChestIds.Contains(e))
{
chestIds.Add(e);
}
});

if (!TaskManager.IsBusy)
{
var nextChest = chestIds.MinBy(id => ObjectTable.First(e => e.ObjectId == id).Position.Distance(Player.GameObject->Position));
visitedChestIds.Add(nextChest);

if (!Concealment)
{
Enqueue(new UsePomanderTask(Pomander.Concealment), "Use Concealment");
}

Enqueue(new PathfindTask(ObjectTable.First(e => e.ObjectId == nextChest).Position, true), 60 * 1000, "Searching " + nextChest);
}

FindHoardPosition();
if (hoardPosition != Vector3.Zero)
{
NavmeshIPC.PathStop();
TaskManager.Abort();
return true;
}

return false;
}

private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
if (!NavmeshIPC.NavIsReady())
{
HoardModeStatus = "Waiting Navmesh";
return;
}
if (searchMode && hoardPosition == Vector3.Zero)
{
if (!SearchLogic())
{
return;
}
}

if (!TaskManager.IsBusy && hoardModeActive)
{
if (CheckDone())
Expand All @@ -106,7 +162,7 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
HoardModeStatus = "Ending";
TaskManager.Abort();
EnqueueImmediate(new LeaveDutyTask());
EnqueueImmediate(new LeaveDutyTask(), "Leave Duty");
}
EnqueueImmediate(() =>
{
Expand All @@ -118,38 +174,37 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
if (!InHoH && !InRubySea && NotBusy() && !KyuseiInteractable())
{
HoardModeStatus = "Moving to HoH";
Enqueue(new MoveToHoHTask());
Enqueue(new MoveToHoHTask(), "Move to HoH");
}

if (InRubySea && NotBusy() && KyuseiInteractable())
{
HoardModeStatus = "Entering HoH";
Enqueue(new EnterHeavenOnHigh());
Enqueue(new EnterHeavenOnHigh(), "Enter HoH");
}

if (InHoH && NotBusy())
{
if (!intuitionUsed)
{
Enqueue(new UsePomanderTask(Pomander.Intuition));
Enqueue(new UsePomanderTask(Pomander.Intuition), "Use Intuition");
intuitionUsed = true;
}
else
{
if (hoardAvailable)
{
if (hoardPosition == Vector3.Zero &&
ObjectTable.TryGetFirst(gameObject => gameObject.DataId == 2007542, out var hoard))
{
hoardPosition = hoard.Position;
}
FindHoardPosition();

if (hoardPosition != Vector3.Zero)
{
if (!movingToHoard)
{
Enqueue(new UsePomanderTask(Pomander.Concealment));
Enqueue(new PathfindTask(hoardPosition, true, 1.5f), 60 * 1000);
if (!Concealment)
{
Enqueue(new UsePomanderTask(Pomander.Concealment), "Use Concealment");
}
Enqueue(new PathfindTask(hoardPosition, true, 1.5f), 60 * 1000, "Move to Hoard");
movingToHoard = true;
HoardModeStatus = "Move to Hoard";
}
Expand All @@ -158,20 +213,16 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
if (!hoardFound)
{
HoardModeStatus = "Unreachable";
Enqueue(new LeaveDutyTask());
Enqueue(() =>
{
SessionRuns++;
return true;
});
Enqueue(new UsePomanderTask(Pomander.Concealment), "Use Concealment");
searchMode = true;
return;
}
}

if (hoardFound)
{
HoardModeStatus = "Leaving";
Enqueue(new LeaveDutyTask());
Enqueue(new LeaveDutyTask(), "Leave Duty");
Enqueue(() =>
{
SessionRuns++;
Expand All @@ -183,7 +234,7 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
else
{
HoardModeStatus = "Leaving";
Enqueue(new LeaveDutyTask());
Enqueue(new LeaveDutyTask(), "Leave Duty");
Enqueue(() =>
{
SessionRuns++;
Expand All @@ -197,6 +248,15 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
SessionTime++;
}

private void FindHoardPosition()
{
if (hoardPosition == Vector3.Zero &&
ObjectTable.TryGetFirst(gameObject => gameObject.DataId == AccursedHoardId, out var hoard))
{
hoardPosition = hoard.Position;
}
}

private bool CheckDone()
{
switch (Config.StopAfterMode)
Expand Down
7 changes: 0 additions & 7 deletions HoardFarm/Tasks/LeaveDutyTask.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using Dalamud.Game.ClientState.Conditions;
using ECommons.Automation;
using ECommons.DalamudServices;
using ECommons.GameHelpers;
using ECommons.Throttlers;
using FFXIVClientStructs.FFXIV.Client.Game;
using FFXIVClientStructs.FFXIV.Client.Game.Control;
using FFXIVClientStructs.FFXIV.Client.Game.Object;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
using FFXIVClientStructs.FFXIV.Component.GUI;
using HoardFarm.IPC;
using ValueType = FFXIVClientStructs.FFXIV.Component.GUI.ValueType;

namespace HoardFarm.Tasks;
Expand Down
2 changes: 1 addition & 1 deletion HoardFarm/Tasks/PathfindTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PathfindTask(Vector3 targetPosition, bool sprint = false, float tol
{
public unsafe bool? Run()
{
if (targetPosition.Distance2D(Player.GameObject->Position) <= toleranceDistance)
if (targetPosition.Distance(Player.GameObject->Position) <= toleranceDistance)
{
NavmeshIPC.PathStop();
return true;
Expand Down
6 changes: 5 additions & 1 deletion HoardFarm/Utils/DataIDs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.Collections.Generic;
using System.Numerics;

namespace HoardFarm.Utils;

Expand All @@ -17,5 +18,8 @@ public static class DataIDs
public const uint ConfirmPartyKoMessageId = 10483;

public const uint VanishStatusId = 1496;
public const uint AccursedHoardId = 2007542;

public static readonly HashSet<uint> ChestIDs = [2007357, 2007358];

}
14 changes: 3 additions & 11 deletions HoardFarm/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Memory;
using Dalamud.Utility;
using ECommons.DalamudServices;
using ECommons.GameFunctions;
using ECommons.GameHelpers;
using ECommons.Reflection;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
using FFXIVClientStructs.FFXIV.Component.GUI;
using ValueType = FFXIVClientStructs.FFXIV.Component.GUI.ValueType;

namespace HoardFarm.Utils;

public static class Utils
{
public static float SprintCD => Player.Status.FirstOrDefault(s => s.StatusId == 50)?.RemainingTime ?? 0;
public static bool Concealment => (Player.Status.FirstOrDefault(s => s.StatusId == VanishStatusId)?.RemainingTime ?? 0) > 0;
public static bool InHoH => Player.Territory == HoHMapId11 || Player.Territory == HoHMapId21;
public static bool InRubySea => Player.Territory == RubySeaMapId;
public static unsafe bool IsMoving() => AgentMap.Instance()->IsPlayerMoving == 1;

public static float Distance2D(this Vector3 v, Vector3 v2)
{
return new Vector2(v.X - v2.X, v.Z - v2.Z).Length();
}

public static float Distance(this Vector3 v, Vector3 v2) => new Vector2(v.X - v2.X, v.Z - v2.Z).Length();
public static bool PluginInstalled(string name) => DalamudReflector.TryGetDalamudPlugin(name, out _, false, true);

public static bool NotBusy()
Expand Down Expand Up @@ -74,7 +66,7 @@ public static unsafe bool KyuseiInteractable()
{
if (ObjectTable.TryGetFirst(e => e.DataId == KyuseiDataId, out var npc))
{
return npc.Position.Distance2D(Player.GameObject->Position) < 3f;
return npc.Position.Distance(Player.GameObject->Position) < 3f;
}
return false;
}
Expand Down

0 comments on commit 2f8cceb

Please sign in to comment.