-
Notifications
You must be signed in to change notification settings - Fork 3
/
Analyzer.LootGroups.cs
480 lines (422 loc) · 21.9 KB
/
Analyzer.LootGroups.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
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
using lib.remnant2.analyzer.Model;
using System.Text.RegularExpressions;
using Serilog;
using SerilogTimings.Extensions;
using lib.remnant2.analyzer.Enums;
namespace lib.remnant2.analyzer;
public partial class Analyzer
{
[GeneratedRegex(@"([^|,]+)|(\|)|(,)")]
private static partial Regex RegexPrerequisite();
private static void FillLootGroups(RolledWorld world)
{
int characterSlot = world.ParentCharacter.Index;
int characterIndex = world.ParentCharacter.ParentDataset.Characters.FindIndex(x => x == world.ParentCharacter) + 1;
string mode = world.IsCampaign ? "campaign" : "adventure";
ILogger logger = Log.Logger
.ForContext(Log.Category, Log.UnknownItems)
.ForContext("RemnantNotificationType", "Warning")
.ForContext("SourceContext", "Analyzer:LootGroups");
ILogger prerequisiteLogger = Log.Logger
.ForContext(Log.Category, Log.Prerequisites)
.ForContext("SourceContext", "Analyzer:LootGroups");
ILogger performanceLogger = Log.Logger
.ForContext(Log.Category, Log.Performance)
.ForContext("SourceContext", "Analyzer:LootGroups");
// Add items to locations
var operation = performanceLogger.BeginOperation($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, add items to locations");
foreach (Zone zone in world.AllZones)
{
int canopyIndex = zone.Locations.FindIndex(x => x.Name == "Ancient Canopy");
// Bloody Walt is on the move and not tied to a particular location
// Let's create a special synthetic location for him!
if (canopyIndex >= 0)
{
// The very first location is always associated with the story.
// Since there are a few IsLooted markers coming from the story,
// we access that first location further down when processing those markers.
// Here we make sure not to insert the location at the first position
// As Ancient Canopy is the first location in the zone and should remain such
zone.Locations.Insert(canopyIndex+1, new(
name: "Ancient Canopy/Luminous Vale",
category: zone.Locations[canopyIndex].Category
));
}
int sentinelsKeepIndex = zone.Locations.FindIndex(x => x.Name == "Sentinel's Keep");
int seekersRestIndex = zone.Locations.FindIndex(x => x.Name == "Seeker's Rest");
// Inject Alepsis-Taura
if (sentinelsKeepIndex >= 0)
{
zone.Locations.Insert(zone.Locations.Count, new(
name: "Alepsis-Taura",
category: zone.Locations[sentinelsKeepIndex].Category
)
{
LootedMarkers = zone.Locations[seekersRestIndex].LootedMarkers
});
}
// Populate locations with possible items
foreach (Location location in zone.Locations)
{
// Part 1 : Drop Type : Location
location.LootGroups = [];
LootGroup lg = new()
{
Type = "Location",
Items = ItemDb.GetItemsByReference("Location", location.Name),
};
if (lg.Items.Count > 0)
{
location.LootGroups.Add(lg);
}
// Part 2 : Drop Type : Event
foreach (DropReference dropReference in location.DropReferences)
{
// This reference injects Nimue into one of the two locations she appears in:
// Beatific Palace, as such it's not very useful to us, because
// it's the very same Nimue as in Nimue's retreat, so we are skipping it
if (dropReference.Name == "Quest_Injectable_GoldenHall_Nimue") continue;
Dictionary<string, string>? ev = ItemDb.Db.SingleOrDefault(x =>
x["Id"].Equals(dropReference.Name, StringComparison.InvariantCultureIgnoreCase));
if (ev == null)
{
logger.Warning($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, Event: Drop reference '{dropReference.Name}' found in the save but is absent from the database");
lg = new LootGroup
{
Items = [],
EventDropReference = dropReference.Name,
Type = "unknown event",
Name = dropReference.Name,
UnknownMarker = UnknownData.Event
};
location.LootGroups.Add(lg);
continue;
}
string type = ev["Subtype"];
string? name = null;
if (type == "overworld POI" || type == "boss" || type == "miniboss" || type == "injectable")
{
name = ev["Name"];
}
// This is not a boss-only zone so if zone is complete does not mean all items are looted
bool propagateLooted = !(
type == "location" && ev["Id"].StartsWith("Quest_RootEarth_Zone")
|| type == "dungeon"
);
lg = new LootGroup
{
Items = ItemDb.GetItemsByReference("Event", dropReference, propagateLooted).Where(x => x.Type != "challenge").ToList(),
EventDropReference = dropReference.Name,
Type = type,
Name = name
};
location.LootGroups.Add(lg);
}
// Part 3 : Drop Type : World Drop
List<LootItem> worldDrops = location.WorldDrops
.Where(x => x.Name != "Bloodmoon" && ItemDb.HasItem(x.Name))
.Select(ItemDb.GetItemById).ToList();
UnknownData unknown = UnknownData.None;
foreach (DropReference s in location.WorldDrops.Where(x => x.Name != "Bloodmoon" && !ItemDb.HasItem(x.Name)))
{
logger.Warning($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, World Drop: Drop reference {s.Name} is absent from the database (world drop {location.Name})");
Dictionary<string, string> unknownItem = new()
{
{ "Name", s.Name },
{ "Id", "unknown" },
{ "Type", "unknown" }
};
worldDrops.Add(new() { Properties = unknownItem });
unknown = UnknownData.WorldDrop;
}
if (worldDrops.Count > 0)
{
lg = new LootGroup
{
Type = "World Drop",
Items = worldDrops,
UnknownMarker = unknown,
Name = worldDrops.Count > 1 ? "Multiple" : worldDrops[0].Name
};
location.LootGroups.Add(lg);
}
// Part 4 : Drop Type : Vendor
foreach (string vendor in location.Vendors)
{
lg = new LootGroup
{
Type = "Vendor",
Name = vendor,
Items = ItemDb.GetItemsByReference("Vendor", vendor)
};
location.LootGroups.Add(lg);
}
}
}
operation.Complete();
// Process additional Looted Markers
operation = performanceLogger.BeginOperation($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, process additional looted markers");
// Since we process Looted Markers from the first zone for every location
// we do not want to display the missing loot marker message more than once per item. We will keep track of those here
HashSet<string> missingLootedMarkers = [];
foreach (Zone zone in world.AllZones)
{
// Story associated loot items are attached to the first zone location in the save,
// but we can have them elsewhere in our database so we process the looted markers from the first zone
// for each location
var firstL = zone.Locations.First();
foreach (Location location in zone.Locations)
{
foreach (LootedMarker marker in location.LootedMarkers.Union(firstL.LootedMarkers))
{
if (marker.ProfileId == "/Game/World_DLC3/Items/Weapons/RepairTool/Weapon_RepairTool.Weapon_RepairTool_C" && marker.SpawnPointTags[0] == "Reward_SingleCore")
continue;
LootItem? li = ItemDb.GetItemByProfileId(marker.ProfileId);
if (li == null)
{
if (!missingLootedMarkers.Contains(marker.ProfileId))
{
logger.Warning($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, Looted marker not found in database: {marker.ProfileId}");
missingLootedMarkers.Add(marker.ProfileId);
}
continue;
}
foreach (LootItem item in location.LootGroups.SelectMany(x => x.Items))
{
if (item.Id == li.Id)
{
// This might need to be dealt with in custom scripts
// Sometimes finished location means that the item is looted, and sometimes it does not
//item.IsLooted = item.IsLooted || marker.IsLooted;
item.IsLooted = marker.IsLooted;
}
}
}
}
}
operation.Complete();
void ProcessPrerequisitesAndScripts(Zone? zone, Location? location, LootGroup lootGroup, LootItem lootItem)
{
if (lootItem.Properties.TryGetValue("Prerequisite", out string? prerequisite) ||
CustomScripts.PrerequisitesScripts.ContainsKey(lootItem.Id))
{
bool res = CheckPrerequisites(world, lootItem, prerequisite);
if (!res)
{
lootItem.IsPrerequisiteMissing = true;
prerequisiteLogger.Information($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, Prerequisite check NEGATIVE for '{prerequisite}'");
}
else
{
prerequisiteLogger.Information($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, Prerequisite check POSITIVE for '{prerequisite}'");
}
}
if (CustomScripts.Scripts.TryGetValue(lootItem.Id, out Func<LootItemContext, bool>? func))
{
LootItemContext lic = new()
{
LootItem = lootItem,
Location = location,
LootGroup = lootGroup,
World = world,
Zone = zone
};
if (!func(lic))
{
lootGroup.Items.Remove(lootItem);
}
}
}
// Mark items that cannot be obtained because no prerequisite
// Process item custom scripts
operation = performanceLogger.BeginOperation($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, process prerequisites and scripts");
foreach (Zone zone in world.AllZones)
{
foreach (Location location in zone.Locations)
{
foreach (LootGroup lootGroup in new List<LootGroup>(location.LootGroups))
{
bool emptyBeforePrerequisitesCheck = lootGroup.Items.Count == 0;
foreach (LootItem item in new List<LootItem>(lootGroup.Items))
{
ProcessPrerequisitesAndScripts(zone, location, lootGroup, item);
}
if (lootGroup.Items.Count == 0 && !emptyBeforePrerequisitesCheck)
{
location.LootGroups.Remove(lootGroup);
}
}
}
}
operation.Complete();
// Progression items are the items that are not tied to any particular location
operation = performanceLogger.BeginOperation($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, progression items");
LootGroup progression = new()
{
Type = "Progression",
Items = ItemDb.GetItemsByReference("Progression"),
};
world.AdditionalItems.Add(progression);
foreach (LootItem item in new List<LootItem>(progression.Items))
{
ProcessPrerequisitesAndScripts(null, null, progression, item);
}
operation.Complete();
// Show items that can be obtained because the character already has the material in their inventory
operation = performanceLogger.BeginOperation($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, craftable items");
LootGroup craftable = new()
{
Type = "Craftable",
// Quest Inventory is for Wooden Box that grants Effigy Pendant
Items = GetItemsWithMaterials(world.ParentCharacter.Profile.Inventory.Union(world.QuestInventory)).ToList()
};
world.AdditionalItems.Add(craftable);
operation.Complete();
}
internal static IEnumerable<LootItem> GetItemsWithMaterials(IEnumerable<InventoryItem> inventory)
{
Dictionary<string, List<Dictionary<string, string>>> materials = ItemDb.Db.Where(x => x.ContainsKey("Material"))
.GroupBy(x => x["Material"])
.ToDictionary(x => x.Key, x => x.ToList());
Dictionary<string, List<Dictionary<string, string>>> consumables = ItemDb.Db.Where(x => x.ContainsKey("Consumable"))
.GroupBy(x => x["Consumable"])
.ToDictionary(x => x.Key, x => x.ToList());
Dictionary<string, List<Dictionary<string, string>>> combined = new[] {materials,consumables}.SelectMany(dict => dict).ToDictionary();
foreach (InventoryItem item in inventory)
{
string name = Utils.GetNameFromProfileId(item.ProfileId);
if (combined.TryGetValue(name, out List<Dictionary<string, string>>? mat))
{
foreach (Dictionary<string, string> d in mat)
{
yield return new LootItem { Properties = d };
}
}
}
}
internal static bool CheckPrerequisites(RolledWorld world, LootItem item, string? prerequisite, bool checkHave = true, bool checkCanGet = true)
{
if (!checkHave && !checkCanGet) return true;
List<string> accountAwards = world.ParentCharacter.ParentDataset.AccountAwards;
Profile profile = world.ParentCharacter.Profile;
int characterSlot = world.ParentCharacter.Index;
int characterIndex = world.ParentCharacter.ParentDataset.Characters.FindIndex(x => x == world.ParentCharacter) + 1;
string mode = world.IsCampaign ? "campaign" : "adventure";
ILogger prerequisiteLogger = Log.Logger
.ForContext(Log.Category, Log.Prerequisites)
.ForContext("SourceContext", "Analyzer:LootGroups");
bool CheckAdditionalPrerequisite(string cur)
{
if (CustomScripts.PrerequisitesScripts.TryGetValue(cur, out Func<LootItemContext, bool>? script))
{
prerequisiteLogger.Information($" Running custom prerequisite script for '{cur}'");
var li = world.AllZones
.SelectMany(x => x.Locations.Select(y => new { Zone = x, Location = y }))
.SelectMany(x => x.Location.LootGroups.Select(y => new { x.Zone, x.Location, LootGroup = y }))
.SelectMany(x =>
x.LootGroup.Items.Select(y => new { x.Zone, x.Location, x.LootGroup, LootItem = y }))
.Single(x => x.LootItem.Id == cur);
// If we already determined that a prerequisite is missing, do not check again
if (li.LootItem.IsPrerequisiteMissing)
{
prerequisiteLogger.Information($" Skip custom prerequisite script for '{cur}' since it is marked with IsPrerequisiteMissing already");
return false;
}
LootItemContext lic = new()
{
LootItem = li.LootItem,
Location = li.Location,
LootGroup = li.LootGroup,
World = world,
Zone = li.Zone
};
bool ok = script(lic);
prerequisiteLogger.Information($" Custom prerequisite script for '{cur}' returned '{ok}'. (true - ok, false - missing prerequisite detected)");
if (!ok)
{
li.LootItem.IsPrerequisiteMissing = true;
}
return ok;
}
return true;
}
if (!CheckAdditionalPrerequisite(item.Id)) return false;
if (prerequisite == null) return true;
prerequisiteLogger.Information($"Character {characterIndex} (save_{characterSlot}), mode: {mode}, Processing prerequisites for {item.Name}. '{prerequisite}'");
List<string> prerequisiteExpressionTokens = RegexPrerequisite().Matches(prerequisite)
.Select(x => x.Value.Trim()).ToList();
bool Check(string cur)
{
LootItem currentItem = ItemDb.GetItemById(cur);
if (currentItem.Type == "award")
{
if (accountAwards.Contains(cur) && checkHave)
{
prerequisiteLogger.Information($" Have '{cur}'");
return true;
}
if (world.CanGetAccountAward(cur) && checkCanGet)
{
prerequisiteLogger.Information($" Can get '{cur}'");
return true;
}
prerequisiteLogger.Information($" Do not have and/or cannot get '{cur}'. CheckHave: {checkHave}, CheckCanGet: {checkCanGet}");
return false;
}
if (currentItem.Type == "challenge" || currentItem.Type == "achievement")
{
if (world.ParentCharacter.Profile.IsObjectiveAchieved(cur) && checkHave)
{
prerequisiteLogger.Information($" Have '{currentItem.Name}'");
return true;
}
if (world.CanGetChallenge(cur) && checkCanGet)
{
prerequisiteLogger.Information($" Can get '{currentItem.Name}'");
return true;
}
prerequisiteLogger.Information($" Do not have and/or cannot get '{currentItem.Name}'. CheckHave: {checkHave}, CheckCanGet: {checkCanGet}");
return false;
}
string itemProfileId = currentItem.Properties["ProfileId"];
if (profile.Inventory.Where(y => y.Quantity is null or > 0).Select(y => y.ProfileId.ToLowerInvariant()).Contains(itemProfileId.ToLowerInvariant()) && checkHave)
{
prerequisiteLogger.Information($" Have '{cur}'");
return true;
}
if (world.QuestInventory.Where(y => y.Quantity is null or > 0).Select(y => y.ProfileId.ToLowerInvariant()).Contains(itemProfileId.ToLowerInvariant()) && checkHave)
{
prerequisiteLogger.Information($" Have '{cur}'");
return true;
}
if (world.CanGetItem(cur) && checkCanGet)
{
if (!CheckAdditionalPrerequisite(cur))
{
prerequisiteLogger.Information($" Cannot get '{cur}' due to additional prerequisites");
return false;
}
prerequisiteLogger.Information($" Can get '{cur}'");
return true;
}
prerequisiteLogger.Information($" Do not have and/or cannot get '{cur}'. CheckHave: {checkHave}, CheckCanGet: {checkCanGet}");
return false;
}
(bool, int) Term(int index) // term => word ',' term | word
{
bool left = Check(prerequisiteExpressionTokens[index++]);
if (index >= prerequisiteExpressionTokens.Count || prerequisiteExpressionTokens[index++] != ",") return (left, index);
(bool right, index) = Term(index);
return (left && right, index);
}
(bool, int) Expr(int index) // expr => term '|' expr | term
{
(bool left, index) = Term(index);
if (index >= prerequisiteExpressionTokens.Count || prerequisiteExpressionTokens[index++] != "|") return (left, index);
(bool right, index) = Expr(index);
return (left || right, index);
}
(bool res, _) = Expr(0);
return res;
}
}