Own regular sector topology in daemon
This commit is contained in:
parent
c78fdb0361
commit
0bad9ea78e
6 changed files with 406 additions and 17 deletions
257
Aetheria.State.Daemon/AetheriaDaemonRegularTopologyGenerator.cs
Normal file
257
Aetheria.State.Daemon/AetheriaDaemonRegularTopologyGenerator.cs
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
public sealed record AetheriaDaemonRegularTopologySettings(
|
||||
int ZoneCount,
|
||||
float LinkDensity,
|
||||
int MegaCount,
|
||||
int BossCount)
|
||||
{
|
||||
public static AetheriaDaemonRegularTopologySettings Fossil { get; } =
|
||||
new(196, 0.5f, 12, 3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daemon-owned port of the fossil non-tutorial Galaxy constructor. It owns
|
||||
/// regular-sector corporation selection, entrance/exit, boss chokepoints,
|
||||
/// full-radius influence, discovery, and names. Zone contents consume this
|
||||
/// graph and cannot revise it.
|
||||
/// </summary>
|
||||
public static class AetheriaDaemonRegularTopologyGenerator
|
||||
{
|
||||
public static AetheriaDaemonTutorialTopology GenerateFossil(
|
||||
IReadOnlyList<AetheriaDaemonTutorialFactionInput> availableFactions,
|
||||
uint seed,
|
||||
AetheriaDaemonRegularTopologySettings? settings = null)
|
||||
{
|
||||
settings ??= AetheriaDaemonRegularTopologySettings.Fossil;
|
||||
if (settings.ZoneCount < 3)
|
||||
throw new ArgumentOutOfRangeException(nameof(settings), "Regular topology requires at least three zones.");
|
||||
if (settings.MegaCount < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(settings), "Regular topology requires at least one corporation.");
|
||||
if (availableFactions == null || availableFactions.Count == 0)
|
||||
throw new InvalidDataException("Regular topology requires typed corporations.");
|
||||
|
||||
var random = new CultMath.Random(seed);
|
||||
var factions = availableFactions
|
||||
.OrderBy(_ => random.NextFloat())
|
||||
.Take(settings.MegaCount)
|
||||
.ToArray();
|
||||
var points = AetheriaDaemonTutorialTopologyGenerator.GeneratePoints(
|
||||
settings.ZoneCount, ref random, MainCloudDensity);
|
||||
var links = AetheriaDaemonTutorialTopologyGenerator.DelaunayLinks(points);
|
||||
AetheriaDaemonTutorialTopologyGenerator.PruneLinks(
|
||||
points, links, settings.LinkDensity, MainCloudDensity);
|
||||
var distances = AetheriaDaemonTutorialTopologyGenerator.BuildDistances(points.Length, links);
|
||||
|
||||
var exit = AetheriaDaemonTutorialTopologyGenerator.MaxZone(points.Length, zone =>
|
||||
Enumerable.Range(0, points.Length).Sum(other => distances[zone, other]));
|
||||
var entrance = AetheriaDaemonTutorialTopologyGenerator.MaxZone(points.Length, zone =>
|
||||
distances[exit, zone]);
|
||||
var bossZones = PlaceBosses(
|
||||
factions, settings.BossCount, entrance, exit, links, distances);
|
||||
var homes = PlaceHomes(factions, entrance, exit, bossZones, distances);
|
||||
|
||||
var zoneFactions = new string[points.Length][];
|
||||
var owners = new string[points.Length];
|
||||
for (var zoneIndex = 0; zoneIndex < points.Length; zoneIndex++)
|
||||
{
|
||||
zoneFactions[zoneIndex] = factions
|
||||
.Where(faction => distances[zoneIndex, homes[faction.CorporationKey]] <= faction.InfluenceDistance)
|
||||
.Select(faction => faction.CorporationKey)
|
||||
.ToArray();
|
||||
owners[zoneIndex] = factions
|
||||
.Where(faction => distances[zoneIndex, homes[faction.CorporationKey]] <= faction.InfluenceDistance)
|
||||
.OrderBy(faction => distances[zoneIndex, homes[faction.CorporationKey]])
|
||||
.ThenBy(faction => Array.IndexOf(factions, faction))
|
||||
.Select(faction => faction.CorporationKey)
|
||||
.FirstOrDefault() ?? "";
|
||||
}
|
||||
|
||||
var discovered = new[] { entrance }
|
||||
.Concat(AetheriaDaemonTutorialTopologyGenerator.Neighbors(entrance, links))
|
||||
.Distinct()
|
||||
.OrderBy(index => index)
|
||||
.ToArray();
|
||||
var nameSeed = (uint)random.NextInt(1, int.MaxValue);
|
||||
var zoneNames = AetheriaDaemonTutorialTopologyGenerator.GenerateNames(
|
||||
factions, owners, nameSeed, order: 4);
|
||||
var zones = Enumerable.Range(0, points.Length)
|
||||
.Select(index => new AetheriaDaemonTutorialZoneTopology(
|
||||
index,
|
||||
zoneNames[index],
|
||||
points[index].X,
|
||||
points[index].Y,
|
||||
AetheriaDaemonTutorialTopologyGenerator.Neighbors(index, links).OrderBy(value => value).ToArray(),
|
||||
zoneFactions[index],
|
||||
owners[index]))
|
||||
.ToArray();
|
||||
return new AetheriaDaemonTutorialTopology(
|
||||
seed, 91.29439f, entrance, discovered, homes, zones)
|
||||
{
|
||||
ExitZoneIndex = exit,
|
||||
BossZoneByFactionKey = bossZones
|
||||
};
|
||||
}
|
||||
|
||||
public static IReadOnlyList<AetheriaDaemonTutorialFactionInput> ResolveFossilFactions(
|
||||
GameCult.Aetheria.State.Verse.AetheriaRuntimeCatalogSnapshot catalog)
|
||||
{
|
||||
if (catalog == null) throw new ArgumentNullException(nameof(catalog));
|
||||
return (catalog.Corporations ?? Array.Empty<GameCult.Aetheria.State.Verse.AetheriaRuntimeCorporation>())
|
||||
.Where(corporation => corporation != null && !string.IsNullOrWhiteSpace(corporation.CorporationKey))
|
||||
.Select(corporation =>
|
||||
{
|
||||
var names = catalog.FindNameFile(corporation.GeonameFileKey);
|
||||
if (names == null || names.Names.Count == 0)
|
||||
throw new InvalidDataException(
|
||||
$"Regular-sector corporation '{corporation.Name}' has no typed geoname corpus.");
|
||||
return new AetheriaDaemonTutorialFactionInput(
|
||||
corporation.CorporationKey,
|
||||
corporation.Name ?? "",
|
||||
corporation.ShortName ?? "",
|
||||
corporation.InfluenceDistance)
|
||||
{
|
||||
TrainingNames = names.Names,
|
||||
BossHullItemKey = corporation.BossHullItemKey ?? ""
|
||||
};
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public static float MainCloudDensity(float x, float y)
|
||||
{
|
||||
var point = new CultMath.float2(x + 91.29439f, y + 91.29439f);
|
||||
var frequency = 1f;
|
||||
var amplitude = 0.5f;
|
||||
var sum = 0f;
|
||||
for (var octave = 0; octave < 10; octave++)
|
||||
{
|
||||
var noise = MathF.Abs(CultMath.math.simplex_noise(point * frequency));
|
||||
sum += (octave < 4 ? 1f - noise : noise) * amplitude;
|
||||
frequency *= 2f;
|
||||
amplitude *= 0.7f;
|
||||
}
|
||||
return MathF.Pow(sum + 0.3f, 10f) * 0.01f;
|
||||
}
|
||||
|
||||
private static Dictionary<string, int> PlaceBosses(
|
||||
IReadOnlyList<AetheriaDaemonTutorialFactionInput> factions,
|
||||
int bossCount,
|
||||
int entrance,
|
||||
int exit,
|
||||
IReadOnlySet<AetheriaDaemonTutorialTopologyGenerator.Edge> links,
|
||||
int[,] distances)
|
||||
{
|
||||
var path = ShortestPath(entrance, exit, links);
|
||||
var chokepoints = path
|
||||
.Where(zone => AetheriaDaemonTutorialTopologyGenerator.Neighbors(zone, links).Count() > 2)
|
||||
.Where(zone => !ConnectedWithoutZone(entrance, exit, zone, links, distances.GetLength(0)))
|
||||
.ToArray();
|
||||
var bossZones = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
foreach (var faction in factions
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value.BossHullItemKey))
|
||||
.Take(Math.Max(0, bossCount)))
|
||||
{
|
||||
var candidates = chokepoints.Length == 0 ? path : chokepoints;
|
||||
bossZones[faction.CorporationKey] = AetheriaDaemonTutorialTopologyGenerator.MaxZone(candidates, zone =>
|
||||
{
|
||||
var score = (double)distances[exit, zone] * distances[entrance, zone];
|
||||
foreach (var occupied in bossZones.Values)
|
||||
score *= distances[occupied, zone];
|
||||
return score;
|
||||
});
|
||||
}
|
||||
return bossZones;
|
||||
}
|
||||
|
||||
private static Dictionary<string, int> PlaceHomes(
|
||||
IReadOnlyList<AetheriaDaemonTutorialFactionInput> factions,
|
||||
int entrance,
|
||||
int exit,
|
||||
IReadOnlyDictionary<string, int> bossZones,
|
||||
int[,] distances)
|
||||
{
|
||||
var homes = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
foreach (var faction in factions.Where(value => bossZones.ContainsKey(value.CorporationKey)))
|
||||
{
|
||||
var candidates = Enumerable.Range(0, distances.GetLength(0))
|
||||
.Where(zone => distances[zone, bossZones[faction.CorporationKey]] <= faction.InfluenceDistance);
|
||||
homes[faction.CorporationKey] = AetheriaDaemonTutorialTopologyGenerator.MaxZone(candidates, zone =>
|
||||
{
|
||||
var score = (double)AetheriaDaemonTutorialTopologyGenerator.RegionSize(
|
||||
zone, faction.InfluenceDistance, distances);
|
||||
foreach (var occupied in homes.Values)
|
||||
score *= Math.Sqrt(distances[occupied, zone]);
|
||||
return score;
|
||||
});
|
||||
}
|
||||
foreach (var faction in factions.Where(value => !homes.ContainsKey(value.CorporationKey)))
|
||||
{
|
||||
homes[faction.CorporationKey] = AetheriaDaemonTutorialTopologyGenerator.MaxZone(
|
||||
distances.GetLength(0), zone =>
|
||||
{
|
||||
var score = Math.Pow(
|
||||
AetheriaDaemonTutorialTopologyGenerator.RegionSize(
|
||||
zone, faction.InfluenceDistance, distances), homes.Count);
|
||||
score *= distances[exit, zone] * distances[entrance, zone];
|
||||
foreach (var occupied in homes.Values)
|
||||
score *= Math.Sqrt(distances[occupied, zone]);
|
||||
foreach (var boss in bossZones.Values)
|
||||
score *= Math.Sqrt(distances[boss, zone]);
|
||||
return score;
|
||||
});
|
||||
}
|
||||
return homes;
|
||||
}
|
||||
|
||||
private static int[] ShortestPath(
|
||||
int source,
|
||||
int target,
|
||||
IReadOnlySet<AetheriaDaemonTutorialTopologyGenerator.Edge> links)
|
||||
{
|
||||
var queue = new Queue<int>();
|
||||
var parents = new Dictionary<int, int>();
|
||||
queue.Enqueue(source);
|
||||
parents[source] = -1;
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
if (current == target) break;
|
||||
foreach (var neighbor in AetheriaDaemonTutorialTopologyGenerator.Neighbors(current, links).OrderBy(value => value))
|
||||
{
|
||||
if (parents.ContainsKey(neighbor)) continue;
|
||||
parents[neighbor] = current;
|
||||
queue.Enqueue(neighbor);
|
||||
}
|
||||
}
|
||||
var path = new List<int>();
|
||||
for (var cursor = target; cursor >= 0; cursor = parents[cursor])
|
||||
path.Add(cursor);
|
||||
path.Reverse();
|
||||
return path.ToArray();
|
||||
}
|
||||
|
||||
private static bool ConnectedWithoutZone(
|
||||
int source,
|
||||
int target,
|
||||
int ignored,
|
||||
IReadOnlySet<AetheriaDaemonTutorialTopologyGenerator.Edge> links,
|
||||
int zoneCount)
|
||||
{
|
||||
if (source == ignored || target == ignored) return false;
|
||||
var queue = new Queue<int>();
|
||||
var visited = new bool[zoneCount];
|
||||
queue.Enqueue(source);
|
||||
visited[source] = true;
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
foreach (var neighbor in AetheriaDaemonTutorialTopologyGenerator.Neighbors(current, links))
|
||||
{
|
||||
if (neighbor == ignored || visited[neighbor]) continue;
|
||||
if (neighbor == target) return true;
|
||||
visited[neighbor] = true;
|
||||
queue.Enqueue(neighbor);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ public sealed record AetheriaDaemonTutorialFactionInput(
|
|||
int InfluenceDistance)
|
||||
{
|
||||
public IReadOnlyList<string> TrainingNames { get; init; } = Array.Empty<string>();
|
||||
public string BossHullItemKey { get; init; } = "";
|
||||
}
|
||||
|
||||
public sealed record AetheriaDaemonTutorialTopologySettings(
|
||||
|
|
@ -43,7 +44,12 @@ public sealed record AetheriaDaemonTutorialTopology(
|
|||
int EntranceZoneIndex,
|
||||
IReadOnlyList<int> DiscoveredZoneIndices,
|
||||
IReadOnlyDictionary<string, int> HomeZoneByFactionKey,
|
||||
IReadOnlyList<AetheriaDaemonTutorialZoneTopology> Zones);
|
||||
IReadOnlyList<AetheriaDaemonTutorialZoneTopology> Zones)
|
||||
{
|
||||
public int ExitZoneIndex { get; init; } = -1;
|
||||
public IReadOnlyDictionary<string, int> BossZoneByFactionKey { get; init; } =
|
||||
new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daemon-owned migration of the fossil tutorial galaxy topology. This organ
|
||||
|
|
@ -52,7 +58,7 @@ public sealed record AetheriaDaemonTutorialTopology(
|
|||
/// </summary>
|
||||
public static class AetheriaDaemonTutorialTopologyGenerator
|
||||
{
|
||||
private const int CandidateMultiplier = 8;
|
||||
internal const int CandidateMultiplier = 8;
|
||||
|
||||
public static AetheriaDaemonTutorialTopology GenerateFossil(
|
||||
IReadOnlyList<AetheriaDaemonTutorialFactionInput> factions,
|
||||
|
|
@ -252,10 +258,11 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
return new TutorialRoles(protagonist, antagonist, buffer, neutrals, quest, all);
|
||||
}
|
||||
|
||||
private static string[] GenerateNames(
|
||||
internal static string[] GenerateNames(
|
||||
IReadOnlyList<AetheriaDaemonTutorialFactionInput> factions,
|
||||
IReadOnlyList<string> ownerKeys,
|
||||
uint seed)
|
||||
uint seed,
|
||||
int order = 3)
|
||||
{
|
||||
var generators = new Dictionary<string, TutorialMarkovNameGenerator>(StringComparer.Ordinal);
|
||||
foreach (var faction in factions)
|
||||
|
|
@ -265,7 +272,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
generators[faction.CorporationKey] = new TutorialMarkovNameGenerator(
|
||||
seed,
|
||||
faction.TrainingNames,
|
||||
order: 3,
|
||||
order: order,
|
||||
minimumLength: 5,
|
||||
maximumLength: 10);
|
||||
}
|
||||
|
|
@ -290,7 +297,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
return output;
|
||||
}
|
||||
|
||||
private static Point[] GeneratePoints(
|
||||
internal static Point[] GeneratePoints(
|
||||
int count,
|
||||
ref CultMath.Random random,
|
||||
Func<float, float, float> density)
|
||||
|
|
@ -359,7 +366,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
return MathF.Pow(1f - distance / dMax, 5f + 6f * density);
|
||||
}
|
||||
|
||||
private static HashSet<Edge> DelaunayLinks(IReadOnlyList<Point> points)
|
||||
internal static HashSet<Edge> DelaunayLinks(IReadOnlyList<Point> points)
|
||||
{
|
||||
var vertices = points.Concat(new[]
|
||||
{
|
||||
|
|
@ -423,7 +430,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
yield return new Edge(triangle.C, triangle.A);
|
||||
}
|
||||
|
||||
private static void PruneLinks(
|
||||
internal static void PruneLinks(
|
||||
IReadOnlyList<Point> points,
|
||||
HashSet<Edge> links,
|
||||
float linkDensity,
|
||||
|
|
@ -541,7 +548,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
.ToArray();
|
||||
}
|
||||
|
||||
private static int[,] BuildDistances(int zoneCount, IReadOnlySet<Edge> links)
|
||||
internal static int[,] BuildDistances(int zoneCount, IReadOnlySet<Edge> links)
|
||||
{
|
||||
var output = new int[zoneCount, zoneCount];
|
||||
for (var source = 0; source < zoneCount; source++)
|
||||
|
|
@ -568,7 +575,7 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
return output;
|
||||
}
|
||||
|
||||
private static bool IsConnectedWithout(
|
||||
internal static bool IsConnectedWithout(
|
||||
int zoneCount,
|
||||
IReadOnlySet<Edge> links,
|
||||
int source,
|
||||
|
|
@ -598,20 +605,20 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
return false;
|
||||
}
|
||||
|
||||
private static IEnumerable<int> Neighbors(int zone, IEnumerable<Edge> links)
|
||||
internal static IEnumerable<int> Neighbors(int zone, IEnumerable<Edge> links)
|
||||
{
|
||||
return links.Where(edge => edge.Contains(zone)).Select(edge => edge.Other(zone));
|
||||
}
|
||||
|
||||
private static int RegionSize(int zone, int maxDistance, int[,] distances)
|
||||
internal static int RegionSize(int zone, int maxDistance, int[,] distances)
|
||||
{
|
||||
return Enumerable.Range(0, distances.GetLength(0)).Count(other => distances[zone, other] <= maxDistance);
|
||||
}
|
||||
|
||||
private static int MaxZone(int zoneCount, Func<int, double> score) =>
|
||||
internal static int MaxZone(int zoneCount, Func<int, double> score) =>
|
||||
MaxZone(Enumerable.Range(0, zoneCount), score);
|
||||
|
||||
private static int MaxZone(IEnumerable<int> zones, Func<int, double> score) =>
|
||||
internal static int MaxZone(IEnumerable<int> zones, Func<int, double> score) =>
|
||||
zones.Select(zone => new { Zone = zone, Score = score(zone) })
|
||||
.OrderByDescending(value => value.Score)
|
||||
.ThenBy(value => value.Zone)
|
||||
|
|
@ -629,9 +636,9 @@ public static class AetheriaDaemonTutorialTopologyGenerator
|
|||
|
||||
private static float Saturate(float value) => value < 0 ? 0 : value > 1 ? 1 : value;
|
||||
|
||||
private readonly record struct Point(float X, float Y);
|
||||
internal readonly record struct Point(float X, float Y);
|
||||
private readonly record struct Triangle(int A, int B, int C);
|
||||
private readonly record struct Edge
|
||||
internal readonly record struct Edge
|
||||
{
|
||||
public Edge(int a, int b)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue