Skip to content

Commit

Permalink
safety improvements - new search logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukkales committed Apr 17, 2024
1 parent 7cf66da commit b731ef7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

I try to keep this changelog up to date with the latest changes in the project.

## [1.5.0.0]
- safety and magicite usage if no concealment is available
- fixed some bugs with the new retainers
- improved search logic
- timeout leave

## [1.4.0.0]
- run retainers between runs
- speed up some things to save 1-2 seconds per run
Expand Down
2 changes: 1 addition & 1 deletion HoardFarm/HoardFarm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Authors>Jukkales</Authors>
<Version>1.4.0.0</Version>
<Version>1.5.0.0</Version>
<Description>HoardFarm Dalamud Plugin</Description>
<PackageProjectUrl>https://github.com/Jukkales/HoardFarm</PackageProjectUrl>
</PropertyGroup>
Expand Down
132 changes: 100 additions & 32 deletions HoardFarm/Service/HoardFarmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
using System.Linq;
using System.Numerics;
using System.Timers;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Utility;
using ECommons.DalamudServices;
using ECommons.GameHelpers;
using ECommons.Throttlers;
using HoardFarm.IPC;
using HoardFarm.Model;
using HoardFarm.Tasks;
Expand All @@ -17,6 +20,8 @@ namespace HoardFarm.Service;

public class HoardFarmService : IDisposable
{
private record MapObject(uint ObjectId, uint DataId, Vector3 Position);

public string HoardModeStatus = "";
public string HoardModeError = "";
private bool hoardModeActive;
Expand All @@ -31,12 +36,14 @@ public class HoardFarmService : IDisposable
private bool intuitionUsed;
private bool movingToHoard;
private bool searchMode;
private bool safetyUsed;
private Vector3 hoardPosition = Vector3.Zero;
private readonly List<uint> chestIds = [];
private readonly List<uint> visitedChestIds = [];
private readonly List<uint> visitedObjectIds = [];
private readonly string hoardFoundMessage;
private readonly string senseHoardMessage;
private readonly string noHoardMessage;
private readonly Dictionary<uint, MapObject> objectPositions = new();
private DateTime runStarted;

public HoardFarmService()
{
Expand Down Expand Up @@ -100,34 +107,37 @@ private void Reset()
hoardAvailable = false;
searchMode = false;
FinishRun = false;
safetyUsed = false;
objectPositions.Clear();
runStarted = DateTime.Now;
}

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.MaxBy(id => ObjectTable.First(e => e.ObjectId == id).Position.Distance(Player.GameObject->Position));
visitedChestIds.Add(nextChest);

if (!Concealment)
if (!TaskManager.IsBusy) {
if (!objectPositions.Where(e => !visitedObjectIds.Contains(e.Value.ObjectId))
.Where(e => ChestIDs.Contains(e.Value.DataId))
.OrderBy(e => e.Value.Position.Distance(Player.GameObject->Position))
.Select(e => e.Value)
.TryGetFirst(out var next))
{
Enqueue(new UsePomanderTask(Pomander.Concealment), "Use Concealment");
if (!objectPositions.Where(e => !visitedObjectIds.Contains(e.Value.ObjectId))
.OrderBy(e => e.Value.Position.Distance(Player.GameObject->Position))
.Select(e => e.Value)
.TryGetFirst(out next))
{
// We should never reach here normally .. but "never" is still a chance > 0% ;)
LeaveDuty("Unreachable");
return true;
}
}

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

FindHoardPosition();
if (hoardPosition != Vector3.Zero)
{
Expand All @@ -148,22 +158,21 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
return;
}

SessionTime++;
Config.OverallTime++;

if (!NavmeshIPC.NavIsReady())
{
HoardModeStatus = "Waiting Navmesh";
return;
}

SessionTime++;
Config.OverallTime++;

UpdateObjectPositions();
SafetyChecks();

if (searchMode && hoardPosition == Vector3.Zero)
{
if (!SearchLogic())
{
return;
}
}

if (!TaskManager.IsBusy && hoardModeActive)
{
Expand All @@ -190,7 +199,7 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
HoardModeStatus = "Finished";
HoardMode = false;
return;
return;
}

if (CheckRetainer())
Expand Down Expand Up @@ -234,10 +243,10 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
if (!movingToHoard)
{
if (!Concealment)
{
Enqueue(new UsePomanderTask(Pomander.Concealment, false), "Use Concealment");
}
// if (!Concealment)
// {
// Enqueue(new UsePomanderTask(Pomander.Concealment, false), "Use Concealment");
// }
Enqueue(new PathfindTask(hoardPosition, true, 1.5f), 60 * 1000, "Move to Hoard");
movingToHoard = true;
HoardModeStatus = "Move to Hoard";
Expand Down Expand Up @@ -272,6 +281,65 @@ private void OnTimerUpdate(object? sender, ElapsedEventArgs e)
}
}

private void SafetyChecks()
{
if (InHoH && intuitionUsed)
{
if (DateTime.Now.Subtract(runStarted).TotalSeconds > 130 && !Svc.Condition[ConditionFlag.InCombat])
{
TaskManager.Abort();
NavmeshIPC.PathStop();
LeaveDuty("Timeout");
return;
}

if (IsMoving())
{
if (!Concealment)
{
if (CanUsePomander(Pomander.Concealment))
{
if (EzThrottler.Check("Concealment"))
{
EzThrottler.Throttle("Concealment", 2000);
new UsePomanderTask(Pomander.Concealment, false).Run();
return; // start next iteration
}
} else if (CanUsePomander(Pomander.Safety) && !safetyUsed && EzThrottler.Check("Concealment"))
{
if (EzThrottler.Check("Safety"))
{
EzThrottler.Throttle("Safety", 2000);
new UsePomanderTask(Pomander.Safety, false).Run();
safetyUsed = true;
return; // start next iteration
}
}
}
}

if (Svc.Condition[ConditionFlag.InCombat])
{
if (CanUseMagicite() && EzThrottler.Check("Magicite"))
{
EzThrottler.Throttle("Magicite", 6000);
new UseMagiciteTask().Run();
}
}

if (Svc.Condition[ConditionFlag.Unconscious])
{
LeaveDuty("Player died");
}
}
}

private void UpdateObjectPositions()
{
foreach (var gameObject in ObjectTable)
objectPositions.TryAdd(gameObject.ObjectId, new MapObject(gameObject.ObjectId, gameObject.DataId, gameObject.Position));
}

private bool CheckRetainer()
{
if (Config.DoRetainers && RetainerService.CheckRetainersDone(Config.RetainerMode == 1))
Expand Down
1 change: 1 addition & 0 deletions HoardFarm/Service/RetainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public RetainerService()
public void Dispose()
{
RetainerApi.OnRetainerPostprocessStep -= CheckRetainerPostProcess;
updateTimer.Dispose();
}

public void StartProcess()
Expand Down
8 changes: 4 additions & 4 deletions HoardFarm/Utils/TaskManagerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public static void EnqueueImmediate(BaseTask task, int timeLimitMs = 10000, stri
TaskManager.EnqueueImmediate(task.Run, timeLimitMs, name);
}

public static void EnqueueImmediate(BaseTask task, string? name = null) => Enqueue(task, 10000, name);
public static void EnqueueImmediate(BaseTask task) => Enqueue(task, 10000);
public static void EnqueueImmediate(BaseTask task, string? name = null) => EnqueueImmediate(task, 10000, name);
public static void EnqueueImmediate(BaseTask task) => EnqueueImmediate(task, 10000);

public static void EnqueueImmediate(Func<bool?> task, int timeLimitMs = 10000, string? name = null)
{
TaskManager.EnqueueImmediate(task, timeLimitMs, name);
}

public static void EnqueueImmediate(Func<bool?> task, string? name = null) => Enqueue(task, 10000, name);
public static void EnqueueImmediate(Func<bool?> task) => Enqueue(task, 10000);
public static void EnqueueImmediate(Func<bool?> task, string? name = null) => EnqueueImmediate(task, 10000, name);
public static void EnqueueImmediate(Func<bool?> task) => EnqueueImmediate(task, 10000);

public static void EnqueueWaitImmediate(int delayMS)
{
Expand Down

0 comments on commit b731ef7

Please sign in to comment.