2021-03-12 05:12:01 +00:00
|
|
|
using System;
|
2021-03-10 09:18:07 +00:00
|
|
|
using System.Collections.Generic;
|
2021-03-11 01:36:08 +00:00
|
|
|
using System.Linq;
|
2021-03-15 11:40:46 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-12 05:12:01 +00:00
|
|
|
using DataStructures.ViliWonka.Heap;
|
2021-03-14 03:56:49 +00:00
|
|
|
using MIConvexHull;
|
2021-03-11 01:36:08 +00:00
|
|
|
using JM.LinqFaster;
|
2021-04-09 14:01:34 +01:00
|
|
|
using UniRx;
|
2021-03-14 03:56:49 +00:00
|
|
|
using Unity.Mathematics;
|
2021-03-11 01:36:08 +00:00
|
|
|
using static Unity.Mathematics.math;
|
2021-03-14 03:56:49 +00:00
|
|
|
using Random = Unity.Mathematics.Random;
|
2021-03-10 09:18:07 +00:00
|
|
|
|
|
|
|
|
public class Sector
|
|
|
|
|
{
|
2021-03-20 04:20:17 +00:00
|
|
|
public Dictionary<Faction, SectorZone> HomeZones = new Dictionary<Faction, SectorZone>();
|
|
|
|
|
public Dictionary<Faction, SectorZone> BossZones = new Dictionary<Faction, SectorZone>();
|
2021-04-09 14:01:34 +01:00
|
|
|
public HashSet<SectorZone> DiscoveredZones = new HashSet<SectorZone>();
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
public SectorBackgroundSettings Background { get; }
|
|
|
|
|
public NameGeneratorSettings NameGeneratorSettings { get; }
|
2021-03-20 04:20:17 +00:00
|
|
|
public Faction[] Factions { get; }
|
2021-03-14 03:56:49 +00:00
|
|
|
public SectorZone[] Zones { get; }
|
|
|
|
|
public SectorZone Entrance { get; }
|
|
|
|
|
public SectorZone Exit { get; }
|
2021-03-12 05:12:01 +00:00
|
|
|
|
2021-03-18 00:36:34 +00:00
|
|
|
private HashSet<Guid> _containedFactions;
|
2021-03-12 05:12:01 +00:00
|
|
|
private SectorZone[] _exitPath;
|
2021-03-20 04:20:17 +00:00
|
|
|
private Dictionary<Faction, MarkovNameGenerator> _nameGenerators = new Dictionary<Faction, MarkovNameGenerator>();
|
2021-03-12 05:12:01 +00:00
|
|
|
|
|
|
|
|
public SectorZone[] ExitPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
if(Entrance!=null && Exit != null)
|
|
|
|
|
return _exitPath ??= FindPath(Entrance, Exit);
|
|
|
|
|
return null;
|
2021-03-12 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 04:07:11 +01:00
|
|
|
public Sector(CultCache cultCache, SavedGame savedGame)
|
2021-03-14 03:56:49 +00:00
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
Background = savedGame.Background;
|
2021-03-31 04:07:11 +01:00
|
|
|
Factions = savedGame.Factions.Select(cultCache.Get<Faction>).ToArray();
|
2021-03-14 03:56:49 +00:00
|
|
|
Zones = savedGame.Zones.Select(zone =>
|
|
|
|
|
{
|
|
|
|
|
return new SectorZone
|
|
|
|
|
{
|
|
|
|
|
Name = zone.Name,
|
2021-03-15 11:40:46 +00:00
|
|
|
Position = zone.Position,
|
|
|
|
|
PackedContents = zone.Contents
|
2021-03-14 03:56:49 +00:00
|
|
|
};
|
|
|
|
|
}).ToArray();
|
2021-04-09 14:01:34 +01:00
|
|
|
foreach (var i in savedGame.DiscoveredZones) DiscoveredZones.Add(Zones[i]);
|
2021-03-14 03:56:49 +00:00
|
|
|
for (var i = 0; i < Zones.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Zones[i].AdjacentZones = savedGame.Zones[i].AdjacentZones.Select(azi => Zones[azi]).ToList();
|
2021-03-15 11:40:46 +00:00
|
|
|
Zones[i].Factions = savedGame.Zones[i].Factions.Select(mi => Factions[mi]).ToArray();
|
|
|
|
|
Zones[i].Owner = savedGame.Zones[i].Owner < 0 ? null : Factions[savedGame.Zones[i].Owner];
|
2021-03-14 03:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HomeZones = savedGame.HomeZones.ToDictionary(
|
2021-03-15 11:40:46 +00:00
|
|
|
x => Factions[x.Key],
|
2021-03-14 03:56:49 +00:00
|
|
|
x => Zones[x.Value]);
|
|
|
|
|
|
|
|
|
|
BossZones = savedGame.BossZones.ToDictionary(
|
2021-03-15 11:40:46 +00:00
|
|
|
x => Factions[x.Key],
|
2021-03-14 03:56:49 +00:00
|
|
|
x => Zones[x.Value]);
|
|
|
|
|
|
|
|
|
|
Entrance = Zones[savedGame.Entrance];
|
2021-04-27 16:44:44 +01:00
|
|
|
if(savedGame.Exit != -1)
|
|
|
|
|
Exit = Zones[savedGame.Exit];
|
2021-03-14 03:56:49 +00:00
|
|
|
|
|
|
|
|
CalculateDistanceMatrix();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
public Sector(
|
|
|
|
|
SectorGenerationSettings settings,
|
|
|
|
|
SectorBackgroundSettings background,
|
|
|
|
|
NameGeneratorSettings nameGeneratorSettings,
|
|
|
|
|
CultCache cache,
|
|
|
|
|
Action<string> progressCallback = null,
|
|
|
|
|
uint seed = 0)
|
2021-03-14 03:56:49 +00:00
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
Background = background;
|
|
|
|
|
var factions = cache.GetAll<Faction>();
|
2021-03-15 11:40:46 +00:00
|
|
|
var random = new Random(seed == 0 ? (uint) (DateTime.Now.Ticks % uint.MaxValue) : seed);
|
2021-04-22 02:53:06 +01:00
|
|
|
Factions = factions.OrderBy(x => random.NextFloat()).Take(settings.MegaCount).ToArray();
|
2021-03-15 11:40:46 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
Zones = GenerateZones(settings.ZoneCount, ref random, progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
GenerateLinks(settings.LinkDensity, progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
CalculateDistanceMatrix(progressCallback);
|
|
|
|
|
|
|
|
|
|
// Exit is the most isolated zone (highest total distance to all other zones)
|
|
|
|
|
Exit = Zones.MaxBy(z => z.Isolation);
|
2021-03-15 11:40:46 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
// Entrance is the zone furthest from the exit
|
|
|
|
|
Entrance = Zones.MaxBy(z => Exit.Distance[z]);
|
|
|
|
|
|
|
|
|
|
DiscoveredZones.Add(Entrance);
|
|
|
|
|
foreach(var z in Entrance.AdjacentZones) DiscoveredZones.Add(z);
|
|
|
|
|
|
|
|
|
|
PlaceFactionsMain(settings.BossCount, progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
CalculateFactionInfluence(progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
GenerateNames(cache, nameGeneratorSettings, ref random, progressCallback);
|
2021-03-18 22:48:33 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
progressCallback?.Invoke("Done!");
|
|
|
|
|
if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Sector(
|
|
|
|
|
TutorialGenerationSettings settings,
|
|
|
|
|
SectorBackgroundSettings background,
|
|
|
|
|
NameGeneratorSettings nameGeneratorSettings,
|
|
|
|
|
CultCache cache,
|
|
|
|
|
Action<string> progressCallback = null,
|
|
|
|
|
uint seed = 0)
|
|
|
|
|
{
|
2021-04-27 16:44:44 +01:00
|
|
|
Faction ResolveFaction(string name) => cache.GetAll<Faction>().FirstOrDefault(f => f.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
Background = background;
|
|
|
|
|
var random = new Random(seed == 0 ? (uint) (DateTime.Now.Ticks % uint.MaxValue) : seed);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
var factions = new List<Faction>();
|
2021-04-27 16:44:44 +01:00
|
|
|
|
|
|
|
|
var protagonistFaction = ResolveFaction(settings.ProtagonistFaction);
|
2021-04-22 02:53:06 +01:00
|
|
|
factions.Add(protagonistFaction);
|
|
|
|
|
|
2021-04-27 16:44:44 +01:00
|
|
|
var antagonistFaction = ResolveFaction(settings.AntagonistFaction);
|
2021-04-22 02:53:06 +01:00
|
|
|
factions.Add(antagonistFaction);
|
|
|
|
|
|
2021-04-27 16:44:44 +01:00
|
|
|
var bufferFaction = ResolveFaction(settings.BufferFaction);
|
2021-04-22 02:53:06 +01:00
|
|
|
factions.Add(bufferFaction);
|
|
|
|
|
|
2021-04-27 16:44:44 +01:00
|
|
|
var questFaction = ResolveFaction(settings.QuestFaction);
|
2021-04-22 02:53:06 +01:00
|
|
|
factions.Add(questFaction);
|
|
|
|
|
|
|
|
|
|
var neutralFactions = settings.NeutralFactions
|
2021-04-27 16:44:44 +01:00
|
|
|
.Select(ResolveFaction)
|
2021-04-22 02:53:06 +01:00
|
|
|
.ToArray();
|
|
|
|
|
factions.AddRange(neutralFactions);
|
|
|
|
|
|
|
|
|
|
Factions = factions.ToArray();
|
|
|
|
|
foreach (var faction in Factions) faction.InfluenceDistance = (faction.InfluenceDistance + 1) / 2;
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
Zones = GenerateZones(settings.ZoneCount, ref random, progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
GenerateLinks(settings.LinkDensity, progressCallback);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
CalculateDistanceMatrix(progressCallback);
|
|
|
|
|
|
|
|
|
|
HomeZones[protagonistFaction] = Zones
|
|
|
|
|
.MaxBy(z => ConnectedRegion(z, protagonistFaction.InfluenceDistance).Count);
|
|
|
|
|
|
|
|
|
|
HomeZones[antagonistFaction] = Zones
|
|
|
|
|
.MaxBy(z => ConnectedRegion(z, antagonistFaction.InfluenceDistance).Count * z.Distance[HomeZones[protagonistFaction]]);
|
|
|
|
|
|
|
|
|
|
HomeZones[protagonistFaction] = Zones
|
|
|
|
|
.MaxBy(z => ConnectedRegion(z, protagonistFaction.InfluenceDistance).Count * sqrt(z.Distance[HomeZones[antagonistFaction]]));
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
// var antagonistRegion = ConnectedRegion(HomeZones[antagonistFaction], antagonistFaction.InfluenceDistance);
|
|
|
|
|
// var protagonistRegion = ConnectedRegion(HomeZones[protagonistFaction], protagonistFaction.InfluenceDistance);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
// Place the buffer faction in a zone where it has equal distance to the pro/antagonist HQs and where it can control the most territory
|
|
|
|
|
var bufferDistance = Zones.Min(z => abs(z.Distance[HomeZones[antagonistFaction]] - z.Distance[HomeZones[protagonistFaction]]));
|
|
|
|
|
var potentialBufferZones = Zones
|
|
|
|
|
.Where(z => abs(z.Distance[HomeZones[antagonistFaction]] - z.Distance[HomeZones[protagonistFaction]]) == bufferDistance);
|
|
|
|
|
HomeZones[bufferFaction] = potentialBufferZones.MaxBy(z => ConnectedRegion(z, bufferFaction.InfluenceDistance).Count);
|
2021-03-18 22:48:33 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
// Place neutral headquarters away from existing factions while also maximizing territory
|
|
|
|
|
foreach (var faction in neutralFactions)
|
|
|
|
|
{
|
|
|
|
|
HomeZones[faction] = Zones.MaxBy(z =>
|
|
|
|
|
ConnectedRegion(z, faction.InfluenceDistance).Count *
|
|
|
|
|
HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CalculateFactionInfluence(progressCallback);
|
|
|
|
|
|
|
|
|
|
var potentialQuestZones = Zones
|
|
|
|
|
.Where(z => z.Factions.Contains(antagonistFaction) && z.Factions.Contains(bufferFaction));
|
|
|
|
|
HomeZones[questFaction] = potentialQuestZones
|
|
|
|
|
.MaxBy(z=>z.Distance[HomeZones[antagonistFaction]] * ConnectedRegion(z, questFaction.InfluenceDistance).Count);
|
|
|
|
|
|
|
|
|
|
CalculateFactionInfluence(progressCallback);
|
|
|
|
|
|
2021-04-30 11:50:47 +01:00
|
|
|
Entrance = Zones.Where(z => z.Owner == null).MinBy(z => z.Distance[HomeZones[protagonistFaction]]);
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-09 14:01:34 +01:00
|
|
|
DiscoveredZones.Add(Entrance);
|
|
|
|
|
foreach(var z in Entrance.AdjacentZones) DiscoveredZones.Add(z);
|
2021-04-22 02:53:06 +01:00
|
|
|
|
|
|
|
|
CalculateFactionInfluence(progressCallback);
|
|
|
|
|
|
|
|
|
|
GenerateNames(cache, nameGeneratorSettings, ref random, progressCallback);
|
|
|
|
|
|
|
|
|
|
progressCallback?.Invoke("Done!");
|
|
|
|
|
if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SectorZone[] GenerateZones(int zoneCount, ref Random random, Action<string> progressCallback = null)
|
|
|
|
|
{
|
|
|
|
|
var outputSamples = WeightedSampleElimination.GeneratePoints(zoneCount,
|
|
|
|
|
ref random,
|
|
|
|
|
Background.CloudDensity,
|
|
|
|
|
v => (.2f - lengthsq(v - float2(.5f))) * 4,
|
|
|
|
|
progressCallback);
|
|
|
|
|
return outputSamples.Select(v => new SectorZone {Position = v}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaceFactionsMain(int bossCount, Action<string> progressCallback = null)
|
|
|
|
|
{
|
|
|
|
|
progressCallback?.Invoke("Finding Chokepoints");
|
|
|
|
|
if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
2021-04-09 14:01:34 +01:00
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Find all zones on the exit path where removing that zone would disconnect the entrance from the exit
|
|
|
|
|
// Disregard "corridor" zones with only two adjacent zones
|
|
|
|
|
var chokePoints = ExitPath
|
|
|
|
|
.Where(z => z.AdjacentZones.Count > 2 && !ConnectedRegion(Entrance, z).Contains(Exit));
|
2021-04-22 02:53:06 +01:00
|
|
|
|
2021-03-15 11:40:46 +00:00
|
|
|
progressCallback?.Invoke("Placing Factions");
|
2021-04-22 02:53:06 +01:00
|
|
|
if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Choose some megas to have bosses placed based on whether a boss hull is assigned
|
2021-03-15 11:40:46 +00:00
|
|
|
var bossMegas = Factions
|
2021-03-14 03:56:49 +00:00
|
|
|
.Where(m => m.BossHull != Guid.Empty)
|
2021-04-22 02:53:06 +01:00
|
|
|
.Take(bossCount)
|
2021-03-14 03:56:49 +00:00
|
|
|
.ToArray();
|
2021-04-22 02:53:06 +01:00
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Place boss zones along the critical path as far apart from each other as possible
|
|
|
|
|
foreach (var mega in bossMegas)
|
|
|
|
|
{
|
|
|
|
|
BossZones[mega] = chokePoints.MaxBy(z =>
|
2021-04-22 02:53:06 +01:00
|
|
|
Exit.Distance[z] * Entrance.Distance[z] *
|
2021-03-14 03:56:49 +00:00
|
|
|
BossZones.Values.Aggregate(1, (i, os) => i * os.Distance[z]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Place boss mega headquarters such that their sphere of influence encompasses their boss zone
|
|
|
|
|
// While occupying as much territory as possible
|
|
|
|
|
foreach (var mega in bossMegas)
|
|
|
|
|
{
|
|
|
|
|
HomeZones[mega] = ConnectedRegion(BossZones[mega], mega.InfluenceDistance)
|
|
|
|
|
.MaxBy(z =>
|
|
|
|
|
ConnectedRegion(z, mega.InfluenceDistance).Count *
|
|
|
|
|
HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
|
|
|
|
|
}
|
2021-04-22 02:53:06 +01:00
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Place remaining headquarters away from existing megas while also maximizing territory
|
2021-04-22 02:53:06 +01:00
|
|
|
foreach (var mega in Factions.Where(m => !bossMegas.Contains(m)))
|
2021-03-14 03:56:49 +00:00
|
|
|
{
|
|
|
|
|
HomeZones[mega] = Zones.MaxBy(z =>
|
|
|
|
|
pow(ConnectedRegion(z, mega.InfluenceDistance).Count, HomeZones.Count) *
|
2021-04-22 02:53:06 +01:00
|
|
|
Exit.Distance[z] * Entrance.Distance[z] *
|
|
|
|
|
HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])) *
|
2021-03-14 03:56:49 +00:00
|
|
|
BossZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
|
|
|
|
|
}
|
2021-04-22 02:53:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CalculateFactionInfluence(Action<string> progressCallback = null)
|
|
|
|
|
{
|
2021-03-15 11:40:46 +00:00
|
|
|
progressCallback?.Invoke("Calculating Faction Influence");
|
2021-04-22 02:53:06 +01:00
|
|
|
if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
2021-03-14 03:56:49 +00:00
|
|
|
|
|
|
|
|
// Assign faction presence
|
|
|
|
|
foreach (var zone in Zones)
|
|
|
|
|
{
|
|
|
|
|
// Factions are present in all zones within their sphere of influence
|
2021-03-15 11:40:46 +00:00
|
|
|
zone.Factions = Factions
|
2021-04-22 02:53:06 +01:00
|
|
|
.Where(f => HomeZones.ContainsKey(f))
|
|
|
|
|
.Where(f => zone.Distance[HomeZones[f]] <= f.InfluenceDistance)
|
2021-03-14 03:56:49 +00:00
|
|
|
.ToArray();
|
2021-04-22 02:53:06 +01:00
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Owner of a zone is the faction with the nearest headquarters
|
2021-04-22 02:53:06 +01:00
|
|
|
var nearestFaction = Factions
|
|
|
|
|
.Where(f => HomeZones.ContainsKey(f))
|
|
|
|
|
.MinBy(f => (float)zone.Distance[HomeZones[f]]);
|
2021-03-14 03:56:49 +00:00
|
|
|
if (zone.Distance[HomeZones[nearestFaction]] <= nearestFaction.InfluenceDistance)
|
|
|
|
|
zone.Owner = nearestFaction;
|
|
|
|
|
}
|
2021-04-22 02:53:06 +01:00
|
|
|
}
|
2021-03-14 03:56:49 +00:00
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
private void GenerateNames(CultCache cache,
|
|
|
|
|
NameGeneratorSettings nameGeneratorSettings,
|
|
|
|
|
ref Random random,
|
|
|
|
|
Action<string> progressCallback = null)
|
|
|
|
|
{
|
2021-03-15 11:40:46 +00:00
|
|
|
for (var i = 0; i < Factions.Length; i++)
|
|
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
progressCallback?.Invoke($"Feeding Markov Chains: {i + 1} / {Factions.Length}");
|
2021-03-15 11:40:46 +00:00
|
|
|
//if(progressCallback!=null) Thread.Sleep(250); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
var faction = Factions[i];
|
2021-04-22 02:53:06 +01:00
|
|
|
_nameGenerators[faction] = new MarkovNameGenerator(ref random, cache.Get<NameFile>(faction.GeonameFile).Names, nameGeneratorSettings);
|
2021-03-15 11:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-14 03:56:49 +00:00
|
|
|
// Generate zone name using the owner's name generator, otherwise assign catalogue ID
|
2021-03-15 11:40:46 +00:00
|
|
|
foreach (var zone in Zones)
|
|
|
|
|
{
|
|
|
|
|
if (zone.Owner != null)
|
|
|
|
|
{
|
|
|
|
|
zone.Name = _nameGenerators[zone.Owner].NextName.Trim();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-18 22:48:33 +00:00
|
|
|
zone.Name = $"EAC-{random.NextInt(9999).ToString()}";
|
2021-03-15 11:40:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
private void GenerateLinks(float linkDensity, Action<string> progressCallback = null)
|
2021-03-15 11:40:46 +00:00
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
progressCallback?.Invoke("Triangulating Zone Positions");
|
|
|
|
|
if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
|
|
|
|
|
// Create a delaunay triangulation to connect adjacent sectors
|
|
|
|
|
var triangulation = DelaunayTriangulation<Vertex2<SectorZone>, Cell2<SectorZone>>
|
|
|
|
|
.Create(Zones.Select(z => new Vertex2<SectorZone>(z.Position, z)).ToList(), 1e-7f);
|
|
|
|
|
var links = new HashSet<(SectorZone, SectorZone)>();
|
|
|
|
|
foreach (var cell in triangulation.Cells)
|
|
|
|
|
{
|
|
|
|
|
if (!links.Contains((cell.Vertices[0].StoredObject, cell.Vertices[1].StoredObject)) &&
|
|
|
|
|
!links.Contains((cell.Vertices[1].StoredObject, cell.Vertices[0].StoredObject)))
|
|
|
|
|
links.Add((cell.Vertices[0].StoredObject, cell.Vertices[1].StoredObject));
|
|
|
|
|
if (!links.Contains((cell.Vertices[1].StoredObject, cell.Vertices[2].StoredObject)) &&
|
|
|
|
|
!links.Contains((cell.Vertices[2].StoredObject, cell.Vertices[1].StoredObject)))
|
|
|
|
|
links.Add((cell.Vertices[1].StoredObject, cell.Vertices[2].StoredObject));
|
|
|
|
|
if (!links.Contains((cell.Vertices[0].StoredObject, cell.Vertices[2].StoredObject)) &&
|
|
|
|
|
!links.Contains((cell.Vertices[2].StoredObject, cell.Vertices[0].StoredObject)))
|
|
|
|
|
links.Add((cell.Vertices[0].StoredObject, cell.Vertices[2].StoredObject));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressCallback?.Invoke("Eliminating Zone Links");
|
|
|
|
|
if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
|
|
|
|
// foreach (var link in links.ToArray())
|
|
|
|
|
// {
|
|
|
|
|
// foreach (var zone in Zones)
|
|
|
|
|
// {
|
|
|
|
|
// if(zone != link.Item1 && zone != link.Item2)
|
|
|
|
|
// if (AetheriaMath.FindDistanceToSegment(zone.Position, link.Item1.Position, link.Item2.Position, out _) < minLineSeparation)
|
|
|
|
|
// links.Remove(link);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
foreach (var link in links)
|
|
|
|
|
{
|
|
|
|
|
link.Item1.AdjacentZones.Add(link.Item2);
|
|
|
|
|
link.Item2.AdjacentZones.Add(link.Item1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float LinkWeight((SectorZone, SectorZone) link)
|
|
|
|
|
{
|
|
|
|
|
return 1 / saturate(Background.CloudDensity((link.Item1.Position + link.Item2.Position) / 2)) *
|
|
|
|
|
lengthsq(link.Item1.Position - link.Item2.Position) *
|
|
|
|
|
(link.Item1.AdjacentZones.Count - 1) * (link.Item2.AdjacentZones.Count - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var heap = new MaxHeap<(SectorZone, SectorZone)>(links.Count);
|
|
|
|
|
foreach (var link in links) heap.PushObj(link, LinkWeight(link));
|
|
|
|
|
while (heap.Count > linkDensity * links.Count)
|
|
|
|
|
{
|
|
|
|
|
var link = heap.PopObj();
|
|
|
|
|
if (ConnectedRegion(link.Item1, link.Item1, link.Item2).Contains(link.Item2))
|
|
|
|
|
{
|
|
|
|
|
link.Item1.AdjacentZones.Remove(link.Item2);
|
|
|
|
|
link.Item2.AdjacentZones.Remove(link.Item1);
|
|
|
|
|
foreach (var secondary in link.Item1.AdjacentZones)
|
|
|
|
|
{
|
|
|
|
|
heap.SetValue((link.Item1, secondary), LinkWeight((link.Item1, secondary)));
|
|
|
|
|
heap.SetValue((secondary, link.Item1), LinkWeight((secondary, link.Item1)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var secondary in link.Item2.AdjacentZones)
|
|
|
|
|
{
|
|
|
|
|
heap.SetValue((link.Item2, secondary), LinkWeight((link.Item2, secondary)));
|
|
|
|
|
heap.SetValue((secondary, link.Item2), LinkWeight((secondary, link.Item2)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-14 03:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cache distance matrix and calculate isolation for every zone (used extensively for placing stuff)
|
2021-04-22 02:53:06 +01:00
|
|
|
private void CalculateDistanceMatrix(Action<string> progressCallback = null)
|
2021-03-14 03:56:49 +00:00
|
|
|
{
|
2021-04-22 02:53:06 +01:00
|
|
|
progressCallback?.Invoke("Calculating Distance Matrix");
|
|
|
|
|
if(progressCallback!=null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao
|
2021-03-14 03:56:49 +00:00
|
|
|
foreach (var zone in Zones)
|
|
|
|
|
{
|
|
|
|
|
zone.Distance = ConnectedRegionDistance(zone);
|
|
|
|
|
zone.Isolation = zone.Distance.Sum(x => x.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 02:53:06 +01:00
|
|
|
public bool ContainsFaction(Guid factionID)
|
|
|
|
|
{
|
|
|
|
|
_containedFactions ??= new HashSet<Guid>(Factions.Select(f => f.ID));
|
|
|
|
|
return _containedFactions.Contains(factionID);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 01:36:08 +00:00
|
|
|
class DijkstraVertex
|
|
|
|
|
{
|
|
|
|
|
public float Cost;
|
|
|
|
|
public DijkstraVertex Parent;
|
|
|
|
|
public SectorZone Zone;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 05:12:01 +00:00
|
|
|
public Dictionary<SectorZone, int> ConnectedRegionDistance(SectorZone v)
|
2021-03-11 01:36:08 +00:00
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
var members = new Dictionary<SectorZone, int> {{v, 0}};
|
|
|
|
|
int cost = 1;
|
2021-03-11 01:36:08 +00:00
|
|
|
while (true)
|
|
|
|
|
{
|
2021-03-11 11:18:21 +00:00
|
|
|
var lastCount = members.Count;
|
2021-03-11 01:36:08 +00:00
|
|
|
// For each member, add all vertices that are connected to it but are not already a member
|
|
|
|
|
// Also, if there is a zone being ignored, do not traverse across it
|
2021-03-12 05:12:01 +00:00
|
|
|
foreach (var member in members.Keys.ToArray())
|
|
|
|
|
foreach (var adjacentZone in member.AdjacentZones)
|
|
|
|
|
{
|
|
|
|
|
if(!members.ContainsKey(adjacentZone))
|
|
|
|
|
members.Add(adjacentZone, cost);
|
|
|
|
|
}
|
2021-03-11 01:36:08 +00:00
|
|
|
// If we have stopped finding neighbors, stop traversing
|
|
|
|
|
if (members.Count == lastCount)
|
|
|
|
|
return members;
|
2021-03-12 05:12:01 +00:00
|
|
|
cost++;
|
2021-03-11 01:36:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 11:18:21 +00:00
|
|
|
|
2021-03-12 05:12:01 +00:00
|
|
|
public HashSet<SectorZone> ConnectedRegion(
|
|
|
|
|
SectorZone v,
|
2021-03-14 03:56:49 +00:00
|
|
|
SectorZone ignoreLinkSource,
|
|
|
|
|
SectorZone ignoreLinkTarget,
|
2021-03-12 05:12:01 +00:00
|
|
|
int maxDistance = int.MaxValue)
|
|
|
|
|
{
|
2021-03-14 03:56:49 +00:00
|
|
|
return ConnectedRegion(v, x => x.Item1 != ignoreLinkSource || x.Item2 != ignoreLinkTarget, maxDistance);
|
2021-03-12 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HashSet<SectorZone> ConnectedRegion(
|
|
|
|
|
SectorZone v,
|
|
|
|
|
SectorZone ignoreZone,
|
|
|
|
|
int maxDistance = int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
return ConnectedRegion(v, x => x.Item2 != ignoreZone, maxDistance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HashSet<SectorZone> ConnectedRegion(
|
|
|
|
|
SectorZone v,
|
|
|
|
|
int maxDistance = int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
return ConnectedRegion(v, x => true, maxDistance);
|
|
|
|
|
}
|
2021-03-11 11:18:21 +00:00
|
|
|
|
2021-03-12 05:12:01 +00:00
|
|
|
public HashSet<SectorZone> ConnectedRegion(
|
|
|
|
|
SectorZone v,
|
2021-03-14 03:56:49 +00:00
|
|
|
Predicate<(SectorZone source, SectorZone target)> linkFilter,
|
2021-03-12 05:12:01 +00:00
|
|
|
int maxDistance = int.MaxValue)
|
2021-03-11 11:18:21 +00:00
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
var visited = new HashSet<SectorZone> {v};
|
2021-03-11 11:18:21 +00:00
|
|
|
int cost = 1;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
var lastCount = visited.Count;
|
|
|
|
|
// For each member, add all nodes that are connected to it but have not been visited
|
2021-03-11 11:18:21 +00:00
|
|
|
// Also, if there is a zone being ignored, do not traverse across it
|
2021-03-12 05:12:01 +00:00
|
|
|
foreach (var zone in visited.ToArray())
|
|
|
|
|
foreach (var adjacentZone in zone.AdjacentZones)
|
2021-03-11 11:18:21 +00:00
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
if(!visited.Contains(adjacentZone) && linkFilter((zone, adjacentZone)))
|
|
|
|
|
visited.Add(adjacentZone);
|
2021-03-11 11:18:21 +00:00
|
|
|
}
|
|
|
|
|
// If we have stopped finding neighbors, stop traversing
|
2021-03-12 05:12:01 +00:00
|
|
|
if (visited.Count == lastCount || cost == maxDistance)
|
|
|
|
|
return visited;
|
2021-03-11 11:18:21 +00:00
|
|
|
cost++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 01:36:08 +00:00
|
|
|
|
2021-03-12 05:12:01 +00:00
|
|
|
public SectorZone[] FindPath(SectorZone source, SectorZone target, bool bestFirst = false)
|
2021-03-11 01:36:08 +00:00
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
MinHeap<DijkstraVertex> unsearchedNodes = new MinHeap<DijkstraVertex>();
|
|
|
|
|
unsearchedNodes.PushObj(new DijkstraVertex{Zone = source}, 0);
|
|
|
|
|
//SortedList<float,DijkstraVertex> members = new SortedList<float,DijkstraVertex>{{0, new DijkstraVertex{Zone = source}}};
|
|
|
|
|
var searched = new HashSet<SectorZone>();
|
2021-03-11 01:36:08 +00:00
|
|
|
while (true)
|
|
|
|
|
{
|
2021-03-12 05:12:01 +00:00
|
|
|
if(unsearchedNodes.Count == 0) return null; // No nodes left unsearched
|
|
|
|
|
var s = unsearchedNodes.PopObj(); // Lowest cost unsearched node
|
2021-03-11 01:36:08 +00:00
|
|
|
if (s.Zone == target) // We found the path
|
|
|
|
|
{
|
|
|
|
|
Stack<DijkstraVertex> path = new Stack<DijkstraVertex>(); // Since we start at the end, use a LIFO collection
|
|
|
|
|
path.Push(s);
|
|
|
|
|
while(path.Peek().Parent!=null) // Keep pushing until we reach the start, which has no parent
|
|
|
|
|
path.Push(path.Peek().Parent);
|
2021-03-12 05:12:01 +00:00
|
|
|
return path.Select(dv => dv.Zone).ToArray();
|
2021-03-11 01:36:08 +00:00
|
|
|
}
|
|
|
|
|
// For each adjacent star (filter already visited stars unless heuristic is in use)
|
2021-03-12 05:12:01 +00:00
|
|
|
IEnumerable<SectorZone> zonesToSearch = s.Zone.AdjacentZones;
|
|
|
|
|
if (!bestFirst) zonesToSearch = zonesToSearch.Where(z => !searched.Contains(z));
|
|
|
|
|
foreach (var dijkstraStar in zonesToSearch
|
2021-03-11 01:36:08 +00:00
|
|
|
// Cost is parent cost plus distance squared
|
2021-03-12 05:12:01 +00:00
|
|
|
.Select(zone => new DijkstraVertex {Parent = s, Zone = zone, Cost = s.Cost + lengthsq(s.Zone.Position - zone.Position)}))
|
2021-03-11 01:36:08 +00:00
|
|
|
// Add new member to list, sorted by cost plus optional heuristic
|
2021-03-12 05:12:01 +00:00
|
|
|
unsearchedNodes.PushObj(dijkstraStar, bestFirst ? dijkstraStar.Cost + lengthsq(dijkstraStar.Zone.Position - target.Position) : dijkstraStar.Cost);
|
|
|
|
|
searched.Add(s.Zone);
|
2021-03-11 01:36:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-10 09:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SectorZone
|
|
|
|
|
{
|
|
|
|
|
public string Name;
|
|
|
|
|
public float2 Position;
|
|
|
|
|
public List<SectorZone> AdjacentZones = new List<SectorZone>();
|
2021-03-11 11:18:21 +00:00
|
|
|
public int Isolation;
|
|
|
|
|
public Dictionary<SectorZone, int> Distance;
|
2021-03-20 04:20:17 +00:00
|
|
|
public Faction[] Factions;
|
|
|
|
|
public Faction Owner;
|
2021-03-14 03:56:49 +00:00
|
|
|
public Zone Contents;
|
2021-03-15 11:40:46 +00:00
|
|
|
public ZonePack PackedContents;
|
2021-03-10 09:18:07 +00:00
|
|
|
}
|