forked from arturino009/ExpeditionIcons
/
PathPlannerRunner.cs
111 lines (98 loc) · 3.67 KB
/
PathPlannerRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ExileCore;
using ExpeditionIcons.PathPlannerData;
namespace ExpeditionIcons;
public class PathPlannerRunner
{
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
public bool IsRunning => _task is { IsCompleted: false };
private PathPlanner _pathPlanner;
private ExpeditionEnvironment _environment;
private BestValue[] BestValues;
private readonly ConditionalWeakTable<List<Vector2>, PathPlanner.DetailedLootScore> _lootCache = [];
public PathPlanner.DetailedLootScore CurrentBestPath
{
get
{
if (BestValues?.Where(x => x != null).MaxBy(x => x.Score)?.Path is not { } bestPath)
{
return null;
}
if (_lootCache.TryGetValue(bestPath, out var existingScore))
{
return existingScore;
}
return _pathPlanner is { } pathPlanner &&
_environment is { } environment
? _lootCache.GetValue(bestPath, p => pathPlanner.GetDetailedScore(p, environment))
: null;
}
}
public double CurrentBestScore => BestValues?.Max(x => x?.Score ?? 0) ?? 0;
private Task _task;
public void Start(PlannerSettings settings, ExpeditionEnvironment environment, SoundController soundController)
{
_task = Run(settings, environment, soundController);
}
private async Task Run(PlannerSettings settings, ExpeditionEnvironment environment, SoundController soundController)
{
_environment = environment;
_pathPlanner = new PathPlanner(settings);
_pathPlanner.Init(environment);
var threadCount = Math.Max(settings.SearchThreads.Value, 1);
BestValues = new BestValue[threadCount];
var tasks = new List<Task>();
for (int i = 0; i < threadCount; i++)
{
var ii = i;
tasks.Add(Task.Run(() =>
{
try
{
var p = new PathPlanner(settings);
var sw = Stopwatch.StartNew();
var iterationSw = Stopwatch.StartNew();
p.Init(environment);
foreach (var bestPath in p.GetBestPathSeries(environment))
{
BestValues[ii] = new BestValue(bestPath.Points, bestPath.Score, (BestValues[ii]?.Iteration ?? 0) + 1, iterationSw.Elapsed.TotalMilliseconds);
iterationSw.Restart();
if (sw.Elapsed.TotalSeconds >= settings.MaximumGenerationTimeSeconds.Value ||
_cts.IsCancellationRequested)
{
return;
}
}
}
catch (Exception ex)
{
DebugWindow.LogError($"Expedition search thread failed: {ex}");
}
}));
}
try
{
await Task.WhenAll(tasks);
}
finally
{
DebugWindow.LogMsg("ExpeditionIcons PathPlanner finished.");
if (settings.PlaySoundOnFinish)
{
soundController.PlaySound("attention");
}
_ = CurrentBestPath;
_environment = null;
_pathPlanner = null;
}
}
public void Stop() => _cts.Cancel();
}
public record BestValue(List<Vector2> Path, double Score, int Iteration, double LastGenerationTime);