Use typed catalog keys for faction lookups

This commit is contained in:
Meta Vens 2026-06-15 17:25:40 +01:00
commit 7f87373886
13 changed files with 182 additions and 40 deletions

View file

@ -11,6 +11,7 @@ using Unity.Mathematics;
public class Faction : INamedEntry
{
public Guid ID = Guid.NewGuid();
public string FactionKey;
[Inspectable]
public string Name;
@ -33,6 +34,7 @@ public class Faction : INamedEntry
[InspectableColor]
public float3 SecondaryColor;
public Guid GeonameFile;
public string GeonameFileKey;
public Guid BossHull;
[Inspectable]
@ -40,6 +42,7 @@ public class Faction : INamedEntry
[InspectableRangedFloat(0, 1)]
public Dictionary<Guid, float> Allegiance = new Dictionary<Guid, float>();
public Dictionary<string, float> AllegianceByKey = new Dictionary<string, float>();
[InspectableSoundBank]
public uint OverworldMusic;

View file

@ -32,6 +32,7 @@ public class Galaxy
public bool IsPrelude { get; }
private HashSet<Guid> _containedFactions;
private HashSet<string> _containedFactionKeys;
private GalaxyZone[] _exitPath;
private Dictionary<Faction, MarkovNameGenerator> _nameGenerators = new Dictionary<Faction, MarkovNameGenerator>();
private readonly AetheriaRuntimeCatalogSnapshot _runtimeCatalog;
@ -243,6 +244,12 @@ public class Galaxy
{
factions[index].Allegiance[corporationId] = (float) allegiance.Weight;
}
if (!string.IsNullOrWhiteSpace(allegiance.CorporationKey) &&
factionsByLegacyId.ContainsKey(allegiance.CorporationLegacyId))
{
factions[index].AllegianceByKey[allegiance.CorporationKey] = (float) allegiance.Weight;
}
}
}
@ -254,10 +261,12 @@ public class Galaxy
return new Faction
{
ID = ParseLegacyId(corporation.LegacyId, nameof(corporation.LegacyId), corporation.Name),
FactionKey = corporation.CorporationKey,
Name = corporation.Name,
ShortName = corporation.ShortName,
Description = corporation.Description,
GeonameFile = ParseLegacyId(corporation.GeonameFileLegacyId, nameof(corporation.GeonameFileLegacyId), corporation.Name),
GeonameFileKey = corporation.GeonameFileKey,
BossHull = ParseOptionalLegacyId(corporation.BossHullLegacyId),
InfluenceDistance = corporation.InfluenceDistance,
};
@ -358,10 +367,10 @@ public class Galaxy
progressCallback?.Invoke($"Feeding Markov Chains: {i + 1} / {Factions.Length}");
//if(progressCallback!=null) Thread.Sleep(250); // Inserting Delay to make it seem like it's doing more work lmao
var faction = Factions[i];
var nameFile = _runtimeCatalog.FindNameFileByLegacyId(faction.GeonameFile.ToString());
var nameFile = _runtimeCatalog.FindNameFile(faction.GeonameFileKey);
if (nameFile == null || nameFile.Names.Count == 0)
{
throw new InvalidOperationException($"Typed catalog has no name file for faction {faction.Name} ({faction.GeonameFile}).");
throw new InvalidOperationException($"Typed catalog has no name file for faction {faction.Name} ({faction.GeonameFileKey}).");
}
_nameGenerators[faction] = new MarkovNameGenerator(ref random, nameFile.Names, nameGeneratorSettings);
@ -470,6 +479,16 @@ public class Galaxy
return _containedFactions.Contains(factionID);
}
public bool ContainsFaction(string factionKey)
{
_containedFactionKeys ??= new HashSet<string>(
Factions
.Select(f => f.FactionKey)
.Where(key => !string.IsNullOrWhiteSpace(key)),
StringComparer.OrdinalIgnoreCase);
return !string.IsNullOrWhiteSpace(factionKey) && _containedFactionKeys.Contains(factionKey);
}
public Faction ResolveFaction(Guid factionID)
{
if (factionID == Guid.Empty)
@ -486,6 +505,22 @@ public class Galaxy
return _allFactions.FirstOrDefault(f => f.ID == factionID);
}
public Faction ResolveFactionByKey(string factionKey)
{
if (string.IsNullOrWhiteSpace(factionKey))
{
return null;
}
var faction = Factions.FirstOrDefault(f => string.Equals(f.FactionKey, factionKey, StringComparison.OrdinalIgnoreCase));
if (faction != null)
{
return faction;
}
return _allFactions.FirstOrDefault(f => string.Equals(f.FactionKey, factionKey, StringComparison.OrdinalIgnoreCase));
}
class DijkstraVertex
{
public float Cost;

View file

@ -150,24 +150,24 @@ public class LoadoutGenerator
.Where(item => IsTypedCandidate(item, candidateKind))
.Where(item =>
item.Price > 0 &&
Guid.TryParse(item.ManufacturerLegacyId, out var manufacturer) &&
manufacturer != Guid.Empty &&
(Galaxy.IsPrelude || Galaxy.ContainsFaction(manufacturer) &&
(Faction == null || Faction.Allegiance.ContainsKey(manufacturer))) &&
!string.IsNullOrWhiteSpace(item.ManufacturerKey) &&
(Galaxy.IsPrelude || Galaxy.ContainsFaction(item.ManufacturerKey) &&
(Faction == null || Faction.AllegianceByKey.ContainsKey(item.ManufacturerKey))) &&
(typedFilter?.Invoke(item) ?? true))
.WeightedRandomElements(ref Random, item =>
{
if (!Guid.TryParse(item.ManufacturerLegacyId, out var manufacturer))
var manufacturerKey = item.ManufacturerKey;
if (string.IsNullOrWhiteSpace(manufacturerKey))
{
return 0;
}
var allegianceWeight = Faction == null
? 1
: manufacturer == Faction.ID
: string.Equals(manufacturerKey, Faction.FactionKey, StringComparison.OrdinalIgnoreCase)
? 1
: Faction.Allegiance.TryGetValue(manufacturer, out var allegiance)
? allegiance / ManufacturerDistancePenalty(manufacturer)
: Faction.AllegianceByKey.TryGetValue(manufacturerKey, out var allegiance)
? allegiance / ManufacturerDistancePenalty(manufacturerKey)
: 0;
return allegianceWeight *
pow(item.OccupiedCells, sizeExponent) / // Prioritize larger items
@ -196,14 +196,14 @@ public class LoadoutGenerator
}
}
private float ManufacturerDistancePenalty(Guid manufacturer)
private float ManufacturerDistancePenalty(string manufacturerKey)
{
if (Galaxy == null || Zone == null || !Galaxy.ContainsFaction(manufacturer))
if (Galaxy == null || Zone == null || !Galaxy.ContainsFaction(manufacturerKey))
{
return 1;
}
var faction = Galaxy.ResolveFaction(manufacturer);
var faction = Galaxy.ResolveFactionByKey(manufacturerKey);
return faction != null && Galaxy.HomeZones.TryGetValue(faction, out var homeZone)
? Zone.Distance[homeZone]
: 1;