-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCache.cs
More file actions
1199 lines (993 loc) · 45.1 KB
/
Copy pathCache.cs
File metadata and controls
1199 lines (993 loc) · 45.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using ExileCore2.PoEMemory.Elements.AtlasElements;
using GameOffsets2.Native;
using ExileMaps.Classes;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;
using ExileCore2;
using ExileCore2.Shared.Helpers;
using ExileCore2.Shared.Enums;
using ExileCore2.Shared.Interfaces;
using ExileCore2.PoEMemory.FilesInMemory;
using System.Numerics;
using ImGuiNET;
namespace ExileMaps;
public partial class ExileMapsCore
{
#region Map Cache
private static NodeStates StateOf(Node n) =>
n.IsDone ? NodeStates.Visited :
n.IsUnlocked ? NodeStates.Unlocked :
n.IsVisible ? NodeStates.Locked :
NodeStates.Hidden;
public void RefreshMapCache(bool clearCache = false)
{
cacheRefreshProgress = 0f;
if (clearCache) {
lock (mapCacheLock)
mapCache.Clear();
connectionCurves.Clear();
lastCurveRecordCount = -1;
}
RefreshRitualLine();
SeedAtlasModCatalogue();
long tSnap = Stopwatch.GetTimestamp();
List<AtlasNodeDescription> atlasNodes = [.. AtlasPanel.Descriptions];
lastDescriptionCount = atlasNodes.Count;
var points = AtlasPanel.Points.ToList();
var forwardPoints = new Dictionary<Vector2i, List<Vector2i>>(points.Count);
var reverseNeighbors = new Dictionary<Vector2i, List<Vector2i>>(points.Count);
foreach (var point in points) {
forwardPoints[point.Source] = point.Targets;
foreach (var neighbor in point.Targets) {
if (neighbor == default)
continue;
if (!reverseNeighbors.TryGetValue(neighbor, out var sources))
reverseNeighbors[neighbor] = sources = new List<Vector2i>();
sources.Add(point.Source);
}
}
PerfMonitor.Record("Cache.Snapshot", Stopwatch.GetTimestamp() - tSnap);
long tPhase = Stopwatch.GetTimestamp();
int changes = 0;
int total = atlasNodes.Count;
int processed = 0;
foreach (var node in atlasNodes) {
if (mapCache.TryGetValue(node.Coordinate, out Node cachedNode)) {
if (RefreshCachedMapNode(node, cachedNode)) changes++;
} else {
changes += CacheNewMapNode(node);
}
cacheRefreshProgress = total > 0 ? 0.8f * (++processed) / total : 0.8f;
}
PerfMonitor.Record("Cache.NodePass1", Stopwatch.GetTimestamp() - tPhase);
tPhase = Stopwatch.GetTimestamp();
processed = 0;
foreach (var node in atlasNodes) {
if (mapCache.TryGetValue(node.Coordinate, out Node cachedNode))
if (CacheMapConnections(cachedNode, forwardPoints, reverseNeighbors)) changes++;
cacheRefreshProgress = total > 0 ? 0.8f + 0.1f * (++processed) / total : 0.9f;
}
PerfMonitor.Record("Cache.NodePass2", Stopwatch.GetTimestamp() - tPhase);
bool dirty = clearCache || changes > 0;
if (dirty) {
long t0 = Stopwatch.GetTimestamp();
RecalculateWeights();
PerfMonitor.Record("Cache.WeightRecalc", Stopwatch.GetTimestamp() - t0);
}
waypointSyncPending = true;
SnapshotExpeditions();
long tc0 = Stopwatch.GetTimestamp();
List<Node> curveNodes;
lock (mapCacheLock)
curveNodes = [.. mapCache.Values];
RefreshConnectionCurves(curveNodes);
PerfMonitor.Record("Cache.ConnectionCurves", Stopwatch.GetTimestamp() - tc0);
if (dirty) mapCacheVersion++;
cacheRefreshProgress = 1f;
lastRefreshMs = Environment.TickCount64;
}
private void RecalculateWeights() {
weightsRecalcVersion++;
if (mapCache.Count == 0)
return;
lock (mapCacheLock)
foreach (var node in mapCache.Values)
node.RecalculateWeight();
}
private int CacheNewMapNode(AtlasNodeDescription node)
{
var el = node.Element;
var area = el.Area;
string mapId = area.Id.Trim();
string shortID = mapId.Replace("_NoBoss", "");
Node newNode = new()
{
IsUnlocked = el.IsUnlocked,
IsVisible = el.IsVisible,
IsVisited = el.IsVisited,
IsActive = el.IsActive,
IsCompleted = el.IsCompleted,
ParentAddress = node.Address,
Coordinates = node.Coordinate,
Name = area.Name,
Id = mapId,
MapNode = node,
ArtWidth = el.Width,
MapType = ResolveMapType(shortID, mapId)
};
newNode.ResolveSpecial();
CacheWorldPos(node, newNode);
if (!newNode.IsDone) {
try {
AddNodeContentFromIdentity(node, newNode);
AddIdBasedContent(newNode);
AddNodeBiome(node, newNode);
SetAtlasPassive(node, newNode);
AddSpecialModifiers(node, newNode);
RefreshNodeAtlasMods(node, newNode);
} catch (Exception e) {
LogError($"Error getting Content for map type {node.Address.ToString("X")}: " + e.Message);
}
}
newNode.StaticResolved = !string.IsNullOrWhiteSpace(mapId);
newNode.RecalculateWeight();
lock (mapCacheLock)
return mapCache.TryAdd(node.Coordinate, newNode) ? 1 : 0;
}
private bool RefreshCachedMapNode(AtlasNodeDescription node, Node cachedNode)
{
var el = node.Element;
bool unlocked = el.IsUnlocked, visible = el.IsVisible, visited = el.IsVisited, completed = el.IsCompleted;
bool changed = unlocked != cachedNode.IsUnlocked || visible != cachedNode.IsVisible
|| visited != cachedNode.IsVisited || completed != cachedNode.IsCompleted;
cachedNode.IsUnlocked = unlocked;
cachedNode.IsVisible = visible;
cachedNode.IsVisited = visited;
cachedNode.IsActive = el.IsActive;
cachedNode.IsCompleted = completed;
int kids = (int)el.ChildCount;
if (kids != cachedNode.ModifierChildCount) {
cachedNode.ModifierChildCount = kids;
cachedNode.SpecialModifiers.Clear();
cachedNode.ModifierDetails.Clear();
AddSpecialModifiers(node, cachedNode);
changed = true;
}
cachedNode.ParentAddress = node.Address;
cachedNode.MapNode = node;
cachedNode.ArtWidth = el.Width;
CacheWorldPos(node, cachedNode);
bool wasSpecial = cachedNode.IsSpecial;
cachedNode.ResolveSpecial();
changed |= wasSpecial != cachedNode.IsSpecial;
changed |= RefreshNodeAtlasMods(node, cachedNode);
if (cachedNode.IsDone)
return changed;
if (!cachedNode.StaticResolved) {
string fullId = el.Area.Id;
if (!string.IsNullOrWhiteSpace(fullId)) {
cachedNode.Id = fullId.Trim();
cachedNode.MapType = ResolveMapType(fullId.Trim().Replace("_NoBoss", ""), fullId);
if (string.IsNullOrWhiteSpace(cachedNode.Name))
cachedNode.Name = el.Area.Name;
cachedNode.Content.Clear();
cachedNode.Biomes.Clear();
cachedNode.SpecialModifiers.Clear();
cachedNode.ModifierDetails.Clear();
AddNodeContentFromIdentity(node, cachedNode);
AddIdBasedContent(cachedNode);
AddNodeBiome(node, cachedNode);
SetAtlasPassive(node, cachedNode);
AddSpecialModifiers(node, cachedNode);
RefreshNodeAtlasMods(node, cachedNode, force: true);
cachedNode.StaticResolved = true;
changed = true;
}
}
return changed;
}
private MapInfo ResolveMapType(string shortId, string fullId)
{
if (Settings.GameData.Maps.TryGetValue(shortId, out MapInfo mapType))
return mapType;
EnsureMapIdIndex();
if (!string.IsNullOrWhiteSpace(fullId) && mapIdIndex.TryGetValue(fullId, out var byId))
return byId;
return new MapInfo();
}
private void EnsureMapIdIndex()
{
if (mapIdIndex != null && mapIdIndexCount == Settings.GameData.Maps.Count)
return;
var idx = new Dictionary<string, MapInfo>(StringComparer.OrdinalIgnoreCase);
foreach (var m in Settings.GameData.Maps.Values)
foreach (var id in m.IDs)
if (!string.IsNullOrWhiteSpace(id) && !idx.ContainsKey(id))
idx[id] = m;
mapIdIndex = idx;
mapIdIndexCount = Settings.GameData.Maps.Count;
}
private static void CacheWorldPos(AtlasNodeDescription node, Node cachedNode)
{
if (cachedNode.HasWorldPos)
return;
try {
var d3d = node?.Description3D;
if (d3d == null)
return;
var pos = d3d.Position;
if (pos == System.Numerics.Vector3.Zero)
return;
cachedNode.WorldPos = pos;
cachedNode.HasWorldPos = true;
} catch { }
}
private bool CacheMapConnections(Node cachedNode,
Dictionary<Vector2i, List<Vector2i>> forwardPoints,
Dictionary<Vector2i, List<Vector2i>> reverseNeighbors) {
if (cachedNode.ConnectionsResolved)
return false;
bool changed = false;
if (forwardPoints.TryGetValue(cachedNode.Coordinates, out var connectionPoints)) {
int haveNeighbors = 0;
foreach (var kv in cachedNode.Neighbors)
if (kv.Value.Coordinates != default) haveNeighbors++;
int wantNeighbors = 0;
foreach (var v in connectionPoints)
if (v != default) wantNeighbors++;
if (haveNeighbors >= wantNeighbors) {
cachedNode.ConnectionsResolved = true;
return true;
}
cachedNode.NeighborCoordinates = connectionPoints;
foreach (Vector2i vector in connectionPoints)
if (mapCache.TryGetValue(vector, out Node neighborNode))
changed |= cachedNode.Neighbors.TryAdd(vector, neighborNode);
}
if (reverseNeighbors.TryGetValue(cachedNode.Coordinates, out var sources))
foreach (var source in sources)
if (mapCache.TryGetValue(source, out Node neighborNode))
changed |= cachedNode.Neighbors.TryAdd(source, neighborNode);
return changed;
}
private void AddNodeContentFromIdentity(AtlasNodeDescription node, Node toNode) {
var contentIdentity = node.Element?.ContentIdentity;
if (contentIdentity == null)
return;
foreach (var content in contentIdentity) {
var id = content?.Id;
if (string.IsNullOrEmpty(id))
continue;
var contentType = Settings.GameData.Content.TryGetValue(id, out var direct)
? direct
: Settings.GameData.Content.FirstOrDefault(x => x.Key.Replace(" ", "") == id).Value;
if (contentType != null)
toNode.Content.TryAdd(contentType.Name, contentType);
}
}
#region Atlas Modifiers
private readonly Dictionary<(string, int), string> atlasModTextCache = [];
private readonly Dictionary<string, AtlasModInfo> atlasModByKey = [];
private readonly Dictionary<string, GameStat> atlasModStats = [];
private readonly Dictionary<GameStat, int> atlasModScratch = [];
private readonly List<AtlasStatValue> atlasModRaw = [];
private readonly Dictionary<string, int> atlasModTotals = [];
private readonly List<AtlasStatValue> atlasModOrder = [];
private readonly Dictionary<StatDescriptionWrapper, HashSet<GameStat>> atlasModOwnedStats = [];
private bool atlasModDescriptionsUsable;
private AtlasNodeModReader atlasModReader;
private StatDescriptionWrapper endgameMapStatDescriptions;
private bool endgameMapStatDescriptionsFailed;
private long ritualLineAddress;
private long ritualLineStamp;
private bool atlasModsChanged;
private const string EndgameMapStatDescriptionsFile = "Metadata/StatDescriptions/endgame_map_stat_descriptions.csd";
private AtlasNodeModReader AtlasModReader =>
atlasModReader ??= new AtlasNodeModReader(GameController.Memory, GameController.Files);
private void RefreshRitualLine()
{
try {
ritualLineAddress = AtlasModReader.ReadRitualLine(AtlasPanel?.Address ?? 0);
ritualLineStamp = AtlasModReader.ReadRitualLineStamp(ritualLineAddress);
} catch (Exception e) {
ritualLineAddress = 0;
ritualLineStamp = 0;
DebugSwallow("RefreshRitualLine", e);
}
}
private bool RefreshNodeAtlasMods(AtlasNodeDescription node, Node toNode, bool force = false)
{
try {
var element = node.Element;
long el = element?.Address ?? 0;
if (el == 0)
return false;
var pin = AtlasModReader.ReadPin(el);
int fingerprint = AtlasNodeModReader.Fingerprint(pin, ritualLineStamp);
if (!force && toNode.AtlasModsBuilt && fingerprint == toNode.AtlasModFingerprint)
return false;
toNode.AtlasModFingerprint = fingerprint;
toNode.AtlasModsBuilt = true;
toNode.AtlasMods.Clear();
atlasModRaw.Clear();
AtlasModReader.Collect(pin, ritualLineAddress, atlasModRaw);
AddContentStats(element, atlasModRaw);
atlasModTotals.Clear();
atlasModOrder.Clear();
foreach (var stat in atlasModRaw) {
if (!atlasModTotals.ContainsKey(stat.Key))
atlasModOrder.Add(stat);
atlasModTotals[stat.Key] = atlasModTotals.GetValueOrDefault(stat.Key) + stat.Value;
}
foreach (var stat in atlasModOrder) {
int value = atlasModTotals[stat.Key];
if (value == 0)
continue;
var info = ResolveAtlasMod(stat.Key, stat.Stat, stat.Type);
if (info == null)
continue;
toNode.AtlasMods.Add(new AtlasModEntry(stat.Key, value, AtlasModText(info, value),
info.ScalesWithValue ? value : 1));
}
return true;
} catch (Exception e) {
DebugSwallow("RefreshNodeAtlasMods", e);
return false;
}
}
private static void AddContentStats(AtlasPanelNode element, List<AtlasStatValue> into)
{
var contents = element.Content;
if (contents == null)
return;
foreach (var content in contents) {
var stats = content?.Stats;
var values = content?.StatValues;
if (stats == null || values == null)
continue;
int n = Math.Min(stats.Count, values.Count);
for (int i = 0; i < n; i++) {
var record = stats[i];
int value = values[i];
if (record == null || value == 0 || string.IsNullOrEmpty(record.Key))
continue;
into.Add(new AtlasStatValue(record.Key, record.MatchingStat, record.Type, value));
}
}
}
private AtlasModInfo ResolveAtlasMod(string key, GameStat stat, StatType type)
{
if (atlasModByKey.TryGetValue(key, out var known))
return known;
Settings.GameData.AtlasMods.TryGetValue(key, out var stored);
string text = HashProbeValue(TranslateAtlasMod(stat, AtlasModLabelProbe) ?? stored?.Text);
if (string.IsNullOrEmpty(text)) {
if (atlasModDescriptionsUsable) {
atlasModByKey[key] = null;
return null;
}
text = PrettifyStatKey(key);
}
atlasModStats[key] = stat;
var info = stored ?? new AtlasModInfo { Key = key };
info.Text = text;
info.ScalesWithValue = type == StatType.IntValue;
atlasModByKey[key] = info;
Settings.GameData.AtlasMods[key] = info;
atlasModsChanged = true;
return info;
}
private const int AreaModDomain = 6;
private bool atlasModCatalogueSeeded;
private void SeedAtlasModCatalogue()
{
if (atlasModCatalogueSeeded)
return;
atlasModCatalogueSeeded = true;
int scanned = 0, before = Settings.GameData.AtlasMods.Count;
try {
var mods = GameController.Files.Mods?.records;
if (mods != null)
foreach (var mod in mods.Values) {
try {
if (mod == null || (int)mod.Domain != AreaModDomain)
continue;
scanned++;
SeedStatRecords(mod.StatNames);
} catch (Exception e) {
DebugSwallow("SeedAtlasModCatalogue: mod", e);
}
}
var contents = GameController.Files.EndgameMapContent?.EntriesList;
if (contents != null)
foreach (var content in contents) {
try {
SeedStatRecords(content?.Stats);
} catch (Exception e) {
DebugSwallow("SeedAtlasModCatalogue: content", e);
}
}
} catch (Exception e) {
atlasModCatalogueSeeded = false;
DebugSwallow("SeedAtlasModCatalogue", e);
}
LogMessage($"ExileMaps: atlas modifier catalogue seeded from {scanned} area mods, " +
$"{Settings.GameData.AtlasMods.Count - before} new, {Settings.GameData.AtlasMods.Count} total");
}
private void SeedStatRecords(IEnumerable<StatsDat.StatRecord> records)
{
if (records == null)
return;
foreach (var record in records)
if (record != null && !string.IsNullOrEmpty(record.Key))
ResolveAtlasMod(record.Key, record.MatchingStat, record.Type);
}
private string AtlasModText(AtlasModInfo info, int value)
{
var cacheKey = (info.Key, value);
if (atlasModTextCache.TryGetValue(cacheKey, out var cached))
return cached;
string text = (atlasModStats.TryGetValue(info.Key, out var stat)
? TranslateAtlasMod(stat, value)
: null) ?? info.Text;
atlasModTextCache[cacheKey] = text;
return text;
}
private static readonly char[] ModTextBreaks = ['\n', '\r'];
private static readonly Regex ModTagPattern = new(@"\[(?:[^\[\]|]*\|)?([^\[\]]*)\]", RegexOptions.Compiled);
private static string CollapseModText(string text) =>
ModTagPattern.Replace(
string.Join(" ", text.Split(ModTextBreaks, StringSplitOptions.RemoveEmptyEntries)), "$1").Trim();
private static bool IsUntranslated(string text) =>
string.IsNullOrWhiteSpace(text) || text.Contains("<unknown", StringComparison.OrdinalIgnoreCase);
private StatDescriptionWrapper EndgameMapStatDescriptions
{
get {
if (endgameMapStatDescriptions != null || endgameMapStatDescriptionsFailed)
return endgameMapStatDescriptions;
try {
endgameMapStatDescriptions = new StatDescriptionWrapper(
GameController.Memory, GameController.Files.FindFile, EndgameMapStatDescriptionsFile);
} catch (Exception e) {
endgameMapStatDescriptionsFailed = true;
DebugSwallow("EndgameMapStatDescriptions", e);
}
return endgameMapStatDescriptions;
}
}
private bool DescribesStat(StatDescriptionWrapper wrapper, GameStat stat)
{
if (!atlasModOwnedStats.TryGetValue(wrapper, out var owned)) {
owned = [];
var entries = wrapper.EntriesList;
if (entries != null)
foreach (var entry in entries)
if (entry?.Stats != null)
foreach (var described in entry.Stats)
owned.Add(described);
if (owned.Count == 0)
return true;
atlasModOwnedStats[wrapper] = owned;
atlasModDescriptionsUsable = true;
}
return owned.Contains(stat);
}
private string TranslateWith(StatDescriptionWrapper wrapper, GameStat stat, string label, bool gated = true)
{
if (wrapper == null)
return null;
try {
if (gated && !DescribesStat(wrapper, stat))
return null;
var text = wrapper.TranslateMod(atlasModScratch);
return IsUntranslated(text) ? null : CollapseModText(text);
} catch (Exception e) {
DebugSwallow("TranslateAtlasMod: " + label, e);
return null;
}
}
private string TranslateAtlasMod(GameStat stat, int value = 1)
{
atlasModScratch.Clear();
atlasModScratch[stat] = value;
var files = GameController.Files;
return TranslateWith(files.StatDescriptions, stat, "general")
?? TranslateWith(files.AtlasStatDescriptions, stat, "atlas")
?? TranslateWith(EndgameMapStatDescriptions, stat, "endgame")
?? TranslateWith(files.StatDescriptions, stat, "general ungated", false)
?? TranslateWith(EndgameMapStatDescriptions, stat, "endgame ungated", false)
?? TranslateWith(files.AtlasStatDescriptions, stat, "atlas ungated", false);
}
private const int AtlasModLabelProbe = 1;
private static readonly Regex AtlasModProbePattern =
new(@"(?<![\w.])" + AtlasModLabelProbe + @"(?![\d.])", RegexOptions.Compiled);
private static string HashProbeValue(string text) =>
text == null ? null : AtlasModProbePattern.Replace(text, "#");
private static string PrettifyStatKey(string key)
{
if (string.IsNullOrEmpty(key))
return "";
var words = key.Split('_', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < words.Length; i++)
if (words[i].Length > 1 && char.IsLetter(words[i][0]))
words[i] = char.ToUpperInvariant(words[i][0]) + words[i][1..];
return string.Join(" ", words);
}
#endregion
private void AddNodeBiome(AtlasNodeDescription node, Node toNode) {
var biomeId = node.Element?.Biome?.Id;
if (string.IsNullOrEmpty(biomeId))
return;
toNode.Biomes[biomeId] = new BiomeInfo { Name = biomeId };
}
private void AddIdBasedContent(Node toNode)
{
if (string.IsNullOrEmpty(toNode.Id))
return;
if (toNode.Id.Contains("ExpeditionLogbook", StringComparison.OrdinalIgnoreCase)) {
var exp = Settings.GameData.Content.TryGetValue("Expedition", out var direct)
? direct
: Settings.GameData.Content.Values.FirstOrDefault(c => c.Name == "Expedition");
if (exp != null)
toNode.Content.TryAdd(exp.Name, exp);
}
}
private static readonly PropertyInfo AtlasChildrenProp =
typeof(AtlasPanelNode).GetProperty("AtlasChildren", BindingFlags.NonPublic | BindingFlags.Instance);
private void AddSpecialModifiers(AtlasNodeDescription node, Node toNode) {
try {
var element = node?.Element;
if (element == null)
return;
IEnumerable<AtlasPanelNodeChild> children = null;
if (AtlasChildrenProp != null)
children = AtlasChildrenProp.GetValue(element) as IEnumerable<AtlasPanelNodeChild>;
if (children == null || !children.Any())
children = element.GetChildrenAs<AtlasPanelNodeChild>();
if (children == null)
return;
foreach (var child in children) {
var tt = child?.Tooltip;
string text = null;
if (tt != null) {
text = tt.TextNoTags;
if (string.IsNullOrWhiteSpace(text))
text = tt.Text;
}
if (string.IsNullOrWhiteSpace(text))
text = child?.TextNoTags;
if (string.IsNullOrWhiteSpace(text))
continue;
var lines = text.Split('\n');
var line = lines[0].Trim();
if (line.Length == 0)
continue;
if (!toNode.SpecialModifiers.Contains(line, StringComparer.OrdinalIgnoreCase))
toNode.SpecialModifiers.Add(line);
for (int i = 1; i < lines.Length; i++) {
var detail = lines[i].Trim();
if (detail.Length == 0)
continue;
if (!toNode.ModifierDetails.Contains(detail, StringComparer.OrdinalIgnoreCase))
toNode.ModifierDetails.Add(detail);
}
}
}
catch (Exception e) { DebugSwallow("AddSpecialModifiers", e); }
}
private void SetAtlasPassive(AtlasNodeDescription node, Node toNode) {
try {
var passiveId = node.Element?.AtlasEntry?.PassiveSkill?.Id;
bool completed = node.Element?.IsCompleted ?? false;
bool grantsInside = passiveId?.Contains("Inside", StringComparison.OrdinalIgnoreCase) ?? false;
bool isLeague = passiveId?.StartsWith("AtlasLeague", StringComparison.OrdinalIgnoreCase) ?? false;
string contentType = isLeague
? ContentDisplaySettings.AtlasPointTypes.FirstOrDefault(t => passiveId.Contains(t, StringComparison.OrdinalIgnoreCase))
: null;
toNode.AtlasPointType = (grantsInside || contentType == null) ? null : contentType;
toNode.GivesAtlasPoint = (grantsInside || contentType != null) && !completed;
toNode.HasAtlasQuest = (passiveId?.Contains("AtlasQuest", StringComparison.OrdinalIgnoreCase) ?? false) && !completed;
if (contentType != null && !completed) {
var apContent = ResolveAtlasPointContent(contentType);
if (apContent != null)
toNode.Content.TryAdd(apContent.Name, apContent);
}
}
catch (Exception e) { toNode.GivesAtlasPoint = false; toNode.HasAtlasQuest = false; toNode.AtlasPointType = null; DebugSwallow("SetAtlasPassive", e); }
}
private ContentInfo ResolveAtlasPointContent(string type) {
if (string.IsNullOrEmpty(type))
return null;
var types = Settings.GameData.Content;
foreach (var c in types.Values)
if (c?.Name != null && c.Name.Replace(" ", "").Equals(type, StringComparison.OrdinalIgnoreCase))
return c;
foreach (var (key, c) in types)
if ((c?.Name != null && c.Name.Contains(type, StringComparison.OrdinalIgnoreCase))
|| (key != null && key.Contains(type, StringComparison.OrdinalIgnoreCase)))
return c;
return null;
}
private void MergeDuplicateMapsByName() {
var dupeGroups = Settings.GameData.Maps
.GroupBy(kv => kv.Value.Name)
.Where(g => g.Count() > 1)
.ToList();
foreach (var group in dupeGroups) {
var keep = group.First().Value;
keep.IDs = group.SelectMany(kv => kv.Value.IDs ?? []).Distinct().ToArray();
if (string.IsNullOrEmpty(keep.ShortestId))
keep.ShortestId = keep.IDs.OrderBy(x => x.Length).FirstOrDefault();
foreach (var dup in group.Skip(1))
Settings.GameData.Maps.Remove(dup.Key);
}
}
#endregion
#region Game Data
private bool UpdateMapData(bool writeToFile = true) {
try {
MergeDuplicateMapsByName();
var endgameMaps = GameController.Files.EndgameMaps?.EntriesList;
if (endgameMaps == null || endgameMaps.Count == 0)
return false;
int added = 0, updated = 0;
foreach (var endgameMap in endgameMaps) {
var area = endgameMap?.Area;
var id = area?.Id;
var name = area?.Name;
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name))
continue;
if (id.Contains("DNT-UNUSED") || name.Contains("DNT-UNUSED"))
continue;
var shortID = id.Replace("_NoBoss", "");
var mapType = Settings.GameData.Maps.Values.FirstOrDefault(m => m.Name == name);
if (mapType != null) {
if (!mapType.IDs.Contains(id))
mapType.IDs = [.. mapType.IDs, id];
if (string.IsNullOrEmpty(mapType.ShortestId))
mapType.ShortestId = shortID;
updated++;
} else {
Settings.GameData.Maps.TryAdd(shortID, new MapInfo {
Name = name,
IDs = [id],
ShortestId = shortID });
added++;
}
}
if (writeToFile) {
var json = JsonConvert.SerializeObject(Settings.GameData.Maps, Formatting.Indented);
File.WriteAllText(Path.Combine(DirectoryFullName, defaultMapsPath), json);
}
if (writeToFile) LogMessage($"Updated Map Data from game files ({added} new, {updated} updated)");
return true;
} catch (Exception e) {
LogError("Error updating map data from game files: " + e.Message);
return false;
}
}
private static readonly Color ContentColorFallback = Color.FromArgb(255, 220, 220, 220);
internal static Color ContentDefaultColor(string id)
{
if (string.IsNullOrEmpty(id)) return ContentColorFallback;
string s = id.ToLowerInvariant();
if (s.Contains("waterbiome")) return Color.FromArgb(255, 80, 170, 230);
if (s.Contains("mountainbiome")) return Color.FromArgb(255, 170, 175, 185);
if (s.Contains("grassbiome")) return Color.FromArgb(255, 130, 210, 90);
if (s.Contains("forestbiome")) return Color.FromArgb(255, 60, 150, 70);
if (s.Contains("swampbiome")) return Color.FromArgb(255, 130, 140, 60);
if (s.Contains("desertbiome")) return Color.FromArgb(255, 220, 195, 130);
if (s.Contains("breach")) return Color.FromArgb(255, 170, 90, 230);
if (s.Contains("ritual")) return Color.FromArgb(255, 210, 40, 40);
if (s.Contains("abyss")) return Color.FromArgb(255, 120, 70, 200);
if (s.Contains("expedition")) return Color.FromArgb(255, 215, 175, 95);
if (s.Contains("delirium") || s.Contains("simulacrum")) return Color.FromArgb(255, 205, 205, 230);
if (s.Contains("incursion")) return Color.FromArgb(255, 60, 200, 190);
if (s.Contains("essence")) return Color.FromArgb(255, 100, 200, 230);
if (s.Contains("azmeri")) return Color.FromArgb(255, 80, 200, 140);
if (s.Contains("strongbox")) return Color.FromArgb(255, 230, 180, 60);
if (s.Contains("shrine")) return Color.FromArgb(255, 80, 200, 180);
if (s.Contains("stonecircle")) return Color.FromArgb(255, 220, 170, 70);
if (s.Contains("rogueexile") || s.Contains("exile")) return Color.FromArgb(255, 90, 150, 230);
if (s.Contains("headhunter")) return Color.FromArgb(255, 230, 60, 60);
if (s.Contains("ultimarum") || s.Contains("ultimatum")) return Color.FromArgb(255, 200, 150, 60);
if (s.Contains("sanctif") || s.Contains("sanctum")) return Color.FromArgb(255, 240, 210, 120);
if (s.Contains("unique")) return Color.FromArgb(255, 175, 96, 37);
if (s.Contains("itemrarity") || s.Contains("rarity") || s.Contains("rarecurrency")) return Color.FromArgb(255, 230, 215, 90);
if (s.Contains("experience")) return Color.FromArgb(255, 150, 190, 230);
if (s.Contains("corrupt")) return Color.FromArgb(255, 170, 35, 35);
if (s.Contains("irradiated")) return Color.FromArgb(255, 120, 230, 80);
if (s.Contains("boss")) return Color.FromArgb(255, 230, 70, 70);
if (s.Contains("magicmonsters")) return Color.FromArgb(255, 110, 130, 230);
if (s.Contains("giant")) return Color.FromArgb(255, 230, 140, 60);
if (s.Contains("rare")) return Color.FromArgb(255, 230, 215, 90);
if (s.Contains("trader")) return Color.FromArgb(255, 90, 160, 230);
if (s.Contains("hideout")) return Color.FromArgb(255, 170, 170, 170);
if (s.Contains("quest")) return Color.FromArgb(255, 255, 200, 40);
return ContentColorFallback;
}
private static string SplitPascalCase(string s) =>
string.IsNullOrEmpty(s) ? s
: System.Text.RegularExpressions.Regex.Replace(s, "(?<=[a-z0-9])(?=[A-Z])", " ");
private bool UpdateContentData(bool writeToFile = true) {
try {
var visuals = GameController.Files.EndgameMapContentVisualIdentity?.EntriesList;
if (visuals == null || visuals.Count == 0)
return false;
var nameLookup = new Dictionary<string, string>();
var contentEntries = GameController.Files.EndgameMapContent?.EntriesList;
if (contentEntries != null)
foreach (var ce in contentEntries) {
var cid = ce?.Id;
if (!string.IsNullOrEmpty(cid) && !string.IsNullOrEmpty(ce.Name))
nameLookup[cid] = ce.Name;
}
int added = 0, updated = 0;
foreach (var entry in visuals) {
var id = entry?.Id;
if (string.IsNullOrEmpty(id))
continue;
var icon = entry.AtlasIcon?.ToString();
var name = nameLookup.TryGetValue(id, out var nicer) ? nicer : SplitPascalCase(id);
if (Settings.GameData.Content.TryGetValue(id, out var existing)) {
existing.Name = name;
if (!string.IsNullOrEmpty(icon))
existing.AtlasIcon = icon;
updated++;
} else if (Settings.GameData.Content.TryAdd(id, new ContentInfo { Id = id, Name = name, AtlasIcon = icon })) {
added++;
}
}
if (writeToFile) {
var json = JsonConvert.SerializeObject(Settings.GameData.Content, Formatting.Indented);
File.WriteAllText(Path.Combine(DirectoryFullName, defaultContentPath), json);
}
if (writeToFile) LogMessage($"Updated Content Data from game files ({added} new, {updated} updated)");
return true;
} catch (Exception e) {
LogError("Error updating content data from game files: " + e.Message);
return false;
}
}
private bool UpdateBiomeData(bool writeToFile = true) {
try {
var biomeEntries = GameController.Files.EndgameMapBiomes?.EntriesList;
if (biomeEntries == null || biomeEntries.Count == 0)
return false;
int added = 0;
foreach (var entry in biomeEntries) {
var id = entry?.Id;
if (string.IsNullOrEmpty(id))
continue;
if (Settings.GameData.Biomes.ContainsKey(id))
continue;
if (Settings.GameData.Biomes.TryAdd(id, new BiomeInfo { Name = id })) {
added++;
}
}
if (writeToFile) {
var json = JsonConvert.SerializeObject(Settings.GameData.Biomes, Formatting.Indented);
File.WriteAllText(Path.Combine(DirectoryFullName, defaultBiomesPath), json);
}
if (writeToFile) LogMessage($"Updated Biome Data from game files ({added} new)");
return true;
} catch (Exception e) {
LogError("Error updating biome data from game files: " + e.Message);
return false;
}
}
#endregion
#region Pathfinding
private static float RouteCost(Node n, HashSet<Vector2i> done, float? extraMapCost, out bool fresh)
{
fresh = !(n.IsDone || (done != null && done.Contains(n.Coordinates)));
if (!fresh) return 0f;
if (extraMapCost == null) return 1f;
return Math.Max(0f, extraMapCost.Value - n.Weight);
}
private float? TourExtraMapCost => Settings.Tours.WeightAwareRouting ? Settings.Tours.ExtraMapCost : null;
private float? WaypointExtraMapCost => Settings.Waypoints.WeightAwareRouting ? Settings.Waypoints.ExtraMapCost : null;
private (List<Node> path, int steps, float weight) Route(Node start, Func<Node, bool> isGoal, HashSet<Vector2i> done, float? extraMapCost)
{
if (!start.IsNavigable) return (null, 0, 0f);
var best = new Dictionary<Vector2i, (float cost, int steps, float weight)> { [start.Coordinates] = (0f, 0, 0f) };
var parent = new Dictionary<Vector2i, Node> { [start.Coordinates] = null };
var pq = new PriorityQueue<Node, (float cost, int steps, float negWeight)>();
pq.Enqueue(start, (0f, 0, 0f));
static bool Better((float cost, int steps, float weight) a, (float cost, int steps, float weight) b)
=> a.cost < b.cost || (a.cost == b.cost && (a.steps < b.steps || (a.steps == b.steps && a.weight > b.weight)));
while (pq.TryDequeue(out var current, out var pri))
{
var c = current.Coordinates;
var cur = best[c];
if (Better(cur, (pri.cost, pri.steps, -pri.negWeight))) continue;
if (isGoal(current))
{
var found = new List<Node>();
for (Node n = current; n != null; n = parent[n.Coordinates]) found.Add(n);
found.Reverse();
return (found, cur.steps, cur.weight);
}
foreach (var nb in current.Neighbors.Values)
{
if (nb == null || !nb.IsNavigable) continue;
float step = RouteCost(nb, done, extraMapCost, out bool fresh);
var cand = (cur.cost + step, cur.steps + (fresh ? 1 : 0), cur.weight + (fresh ? nb.Weight : 0f));
if (best.TryGetValue(nb.Coordinates, out var old) && !Better(cand, old)) continue;
best[nb.Coordinates] = cand;
parent[nb.Coordinates] = current;
pq.Enqueue(nb, (cand.Item1, cand.Item2, -cand.Item3));
}
}
return (null, 0, 0f);
}