forked from exApiTools/BetterSanctum
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPathFinder.cs
More file actions
237 lines (203 loc) · 7.84 KB
/
PathFinder.cs
File metadata and controls
237 lines (203 loc) · 7.84 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System.Collections.Generic;
using System.Linq;
using ExileCore;
namespace PathfindSanctum;
/// <summary>
/// Handles Dijkstra Pathfinding logic for Sanctum, calculating optimal routes based on room weights.
/// </summary>
public class PathFinder(
Graphics graphics,
PathfindSanctumSettings settings,
SanctumStateTracker sanctumStateTracker,
WeightCalculator weightCalculator
)
{
private double[,] roomWeights;
private readonly Dictionary<(int, int), string> debugTexts = [];
private readonly Dictionary<(int, int), string> displayTexts = [];
private List<(int, int)> foundBestPath;
#region Path Calculation
public void CreateRoomWeightMap()
{
var roomsByLayer = sanctumStateTracker.roomsByLayer;
roomWeights = new double[roomsByLayer.Count, roomsByLayer.Max(x => x.Count)];
for (var layer = 0; layer < roomsByLayer.Count; layer++)
{
for (var room = 0; room < roomsByLayer[layer].Count; room++)
{
var sanctumRoom = roomsByLayer[layer][room];
if (sanctumRoom == null)
continue;
var stateTrackerRoom = sanctumStateTracker.GetRoom(layer, room);
var (weight, debug, display) = weightCalculator.CalculateRoomWeight(stateTrackerRoom, sanctumStateTracker);
roomWeights[layer, room] = weight;
debugTexts[(layer, room)] = debug;
displayTexts[(layer, room)] = display;
}
}
}
public List<(int, int)> FindBestPath()
{
int numLayers = sanctumStateTracker.roomLayout.Length;
var startNode = (7, 0);
var bestPath = new Dictionary<(int, int), List<(int, int)>>
{
{
startNode,
new List<(int, int)> { startNode }
}
};
var maxCost = new Dictionary<(int, int), double>();
// Initialize maxCost for all valid rooms
for (int i = 0; i < roomWeights.GetLength(0); i++)
{
for (int j = 0; j < roomWeights.GetLength(1); j++)
{
maxCost[(i, j)] = double.MinValue;
}
}
maxCost[startNode] = roomWeights[startNode.Item1, startNode.Item2];
var queue = new SortedSet<(int, int)>(
Comparer<(int, int)>.Create(
(a, b) =>
{
double costA = maxCost[a];
double costB = maxCost[b];
if (costA != costB)
{
// Reverse comparison to prioritize higher weights
return costB.CompareTo(costA);
}
// If costs are equal, break the tie by comparing the nodes
return a.CompareTo(b);
}
)
)
{
startNode
};
while (queue.Any())
{
var currentRoom = queue.First();
queue.Remove(currentRoom);
foreach (var neighbor in GetNeighbors(currentRoom, sanctumStateTracker.roomLayout))
{
double neighborCost =
maxCost[currentRoom] + roomWeights[neighbor.Item1, neighbor.Item2];
if (neighborCost > maxCost[neighbor])
{
queue.Remove(neighbor);
maxCost[neighbor] = neighborCost;
queue.Add(neighbor);
bestPath[neighbor] = new List<(int, int)>(bestPath[currentRoom]) { neighbor };
}
}
}
var groupedPaths = bestPath.GroupBy(pair => pair.Value.Count());
var maxCountGroup = groupedPaths.OrderByDescending(group => group.Key).FirstOrDefault();
var path = maxCountGroup
?.OrderByDescending(pair => maxCost.GetValueOrDefault(pair.Key, double.MinValue))
.FirstOrDefault()
.Value;
if (sanctumStateTracker.PlayerLayerIndex != -1 && sanctumStateTracker.PlayerRoomIndex != -1)
{
path = bestPath.TryGetValue(
(sanctumStateTracker.PlayerLayerIndex, sanctumStateTracker.PlayerRoomIndex),
out var specificPath
)
? specificPath
: new List<(int, int)>();
}
foundBestPath = path ?? new List<(int, int)>();
return foundBestPath;
}
private static IEnumerable<(int, int)> GetNeighbors(
(int, int) currentRoom,
byte[][][] connections
)
{
int currentLayerIndex = currentRoom.Item1;
int currentRoomIndex = currentRoom.Item2;
int previousLayerIndex = currentLayerIndex - 1;
if (currentLayerIndex == 0)
{
yield break; // No neighbors to yield
}
byte[][] previousLayer = connections[previousLayerIndex];
for (
int previousLayerRoomIndex = 0;
previousLayerRoomIndex < previousLayer.Length;
previousLayerRoomIndex++
)
{
var previousLayerRoom = previousLayer[previousLayerRoomIndex];
if (previousLayerRoom.Contains((byte)currentRoomIndex))
{
yield return (previousLayerIndex, previousLayerRoomIndex);
}
}
}
#endregion
#region Visualization
public void DrawInfo()
{
if(!settings.DebugEnable.Value && displayTexts.Count == 0)
return;
var roomsByLayer = sanctumStateTracker.roomsByLayer;
for (var layer = 0; layer < roomsByLayer.Count; layer++)
{
for (var room = 0; room < roomsByLayer[layer].Count; room++)
{
var sanctumRoom = sanctumStateTracker.GetRoom(layer, room);
if (sanctumRoom == null)
continue;
var pos = sanctumRoom.Position;
// DebugWindow.LogMsg($"{layer}, {room}: {pos}");
if (settings.DebugEnable.Value)
{
var debugText = debugTexts.TryGetValue((layer, room), out var text)
? text
: string.Empty;
var displayText = $"Weight: {roomWeights[layer, room]:F0}\n{debugText}";
graphics.DrawTextWithBackground(
displayText,
new System.Numerics.Vector2(pos.X, pos.Y),
settings.TextColor,
settings.BackgroundColor
);
} else
{
var infoText = displayTexts.TryGetValue((layer, room), out var text)
? text
: string.Empty;
graphics.DrawTextWithBackground(
infoText,
new System.Numerics.Vector2(pos.X, pos.Y),
settings.TextColor,
settings.BackgroundColor
);
}
}
}
}
public void DrawBestPath()
{
if (this.foundBestPath == null)
return;
foreach (var room in this.foundBestPath)
{
if (
room.Item1 == sanctumStateTracker.PlayerLayerIndex
&& room.Item2 == sanctumStateTracker.PlayerRoomIndex
)
continue;
var sanctumRoom = sanctumStateTracker.roomsByLayer[room.Item1][room.Item2];
graphics.DrawFrame(
sanctumRoom.GetClientRect(),
settings.BestPathColor,
settings.FrameThickness
);
}
}
#endregion
}