Optimizations

This commit is contained in:
Sebastian Bularca
2026-04-20 09:51:08 +02:00
parent 2b48af8d3e
commit e7595bdc89
7 changed files with 152 additions and 48 deletions

View File

@@ -8,8 +8,8 @@ namespace Jovian.EncounterSystem {
/// construction; rolling and advancement are O(1).
/// </summary>
public class QuestProgress {
private readonly HashSet<string> resolvedIds = new();
private readonly Dictionary<string, IEncounter> predecessorOf = new();
private readonly HashSet<string> resolvedIds;
private readonly Dictionary<string, IEncounter> predecessorOf;
public event Action<IEncounter> QuestStarted;
public event Action<IEncounter, IEncounter> QuestAdvanced;
@@ -18,9 +18,26 @@ namespace Jovian.EncounterSystem {
public IReadOnlyCollection<string> ResolvedIds => resolvedIds;
public QuestProgress(EncountersCollection encountersCollection) {
var capacity = EstimateEncounterCount(encountersCollection);
resolvedIds = new HashSet<string>(capacity);
predecessorOf = new Dictionary<string, IEncounter>(capacity);
IndexQuests(encountersCollection);
}
private static int EstimateEncounterCount(EncountersCollection collection) {
if(collection?.encounterTables == null) {
return 0;
}
var total = 0;
for(int i = 0; i < collection.encounterTables.Length; i++) {
var table = collection.encounterTables[i];
if(table?.encounters != null) {
total += table.encounters.Count;
}
}
return total;
}
public bool IsGated(IEncounter encounter) {
var id = encounter?.EncounterDefinition?.internalId;
if(string.IsNullOrEmpty(id)) {
@@ -77,13 +94,15 @@ namespace Jovian.EncounterSystem {
return;
}
foreach(var table in collection.encounterTables) {
for(int t = 0; t < collection.encounterTables.Length; t++) {
var table = collection.encounterTables[t];
if(table?.encounters == null) {
continue;
}
foreach(var encounter in table.encounters) {
if(encounter?.EncounterDefinition.Kind is not QuestKind questKind) {
for(int i = 0; i < table.encounters.Count; i++) {
var encounter = table.encounters[i];
if(encounter?.EncounterDefinition?.Kind is not QuestKind questKind) {
continue;
}