Save/Load and Main Menu Submenus

Redid all of the property panel UI prefabs (oof)
Implemented saving and loading of the game state
Moved sector generation to sector constructor (along with loading from save file)
Entity settings now separate from player settings
Player settings (incl. saved run) now statically accessible from game manager
Added animations and submenus to the main menu
Unified main menu settings and properties panel styles for prefab reusability
This commit is contained in:
Reinier Vens 2021-03-14 03:56:49 +00:00
commit b6828a975a
63 changed files with 2007 additions and 1158 deletions

View file

@ -14,7 +14,6 @@ public abstract class Entity
{
public Zone Zone;
public MegaCorporation Faction;
public PlayerSettings PlayerSettings;
public EquippableItem Hull;
public float3 Position;
@ -79,6 +78,8 @@ public abstract class Entity
}
}
public EntitySettings Settings { get; }
public bool OverrideShutdown { get; set; }
public bool Active
@ -155,8 +156,9 @@ public abstract class Entity
VisibleHostiles.Clear();
}
public Entity(ItemManager itemManager, Zone zone, EquippableItem hull)
public Entity(ItemManager itemManager, Zone zone, EquippableItem hull, EntitySettings settings)
{
Settings = settings;
ItemManager = itemManager;
Zone = zone;
Hull = hull;
@ -997,7 +999,7 @@ public class EquippedItem
var temp = Temperature;
ThermalPerformance = Data.Performance(temp);
DurabilityPerformance = EquippableItem.Durability / Data.Durability;
var performanceThreshold = Entity.PlayerSettings?.ShutdownPerformance ?? _itemManager.GameplaySettings.DefaultShutdownPerformance;
var performanceThreshold = Entity.Settings.ShutdownPerformance;
Wear = (1 - pow(Data.Performance(temp),
(1 - pow(_itemManager.CompoundQuality(EquippableItem), _itemManager.GameplaySettings.QualityWearExponent)) *
_itemManager.GameplaySettings.ThermalWearExponent)

View file

@ -13,8 +13,17 @@ public static class EntitySerializer
{
EntityPack pack;
if (entity is OrbitalEntity orbital)
pack = new OrbitalEntityPack {Orbit = orbital.OrbitData};
else pack = new ShipPack();
pack = new OrbitalEntityPack
{
Orbit = orbital.OrbitData
};
else if (entity is Ship ship)
pack = new ShipPack
{
Position = ship.Position,
Direction = ship.Direction
};
else throw new ArgumentException("Attempted to pack an instance of abstract class Entity!");
// Filter item behavior collections by those with any persistent behaviors
// For each item create an object containing the item position and a list of persistent behaviors
@ -37,7 +46,6 @@ public static class EntitySerializer
pack.Armor = entity.Armor;
pack.Temperature = entity.Temperature;
pack.Conductivity = entity.HullConductivity;
var hullData = entity.ItemManager.GetData(entity.Hull) as HullData;
pack.Children = entity.Children.Select(Pack).ToArray();
return pack;
}
@ -51,30 +59,30 @@ public static class EntitySerializer
_ => null
};
}
public static Ship Unpack(ItemManager itemManager, Zone zone, ShipPack pack, bool instantiate = false)
private static Ship Unpack(ItemManager itemManager, Zone zone, ShipPack pack, bool instantiate = false)
{
var entity = new Ship(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull);
var entity = new Ship(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, pack.Settings);
Restore(itemManager, zone, pack, entity, instantiate);
return entity;
}
public static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, OrbitalEntityPack pack, bool instantiate = false)
private static OrbitalEntity Unpack(ItemManager itemManager, Zone zone, OrbitalEntityPack pack, bool instantiate = false)
{
var entity = new OrbitalEntity(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, pack.Orbit);
var entity = new OrbitalEntity(itemManager, zone, instantiate ? (EquippableItem) itemManager.Instantiate(pack.Hull) : pack.Hull, pack.Orbit, pack.Settings);
Restore(itemManager, zone, pack, entity, instantiate);
return entity;
}
public static void Restore(ItemManager itemManager, Zone zone, EntityPack pack, Entity entity, bool instantiate = false)
private static void Restore(ItemManager itemManager, Zone zone, EntityPack pack, Entity entity, bool instantiate = false)
{
entity.Name = pack.Name;
entity.Children = pack.Children.Select(c =>
{
var child = Unpack(itemManager, zone, c, instantiate);
child.Parent = entity;
return (Entity) child;
return child;
}).ToList();
foreach (var (position, item) in pack.Equipment) entity.TryEquip(instantiate ? (EquippableItem) itemManager.Instantiate(item) : item, position);
foreach (var (position, item) in pack.CargoBays) entity.TryEquip(instantiate ? (EquippableItem) itemManager.Instantiate(item) : item, position);
@ -120,12 +128,19 @@ public static class EntitySerializer
}
[MessagePackObject]
public class ShipPack : EntityPack { }
public class ShipPack : EntityPack
{
[Key(15)]
public float3 Position;
[Key(16)]
public float2 Direction;
}
[MessagePackObject]
public class OrbitalEntityPack : EntityPack
{
[Key(14)]
[Key(15)]
public Guid Orbit;
}
@ -173,51 +188,56 @@ public class EntityPack
[Key(13)]
public EntityPack[] Children;
[Key(14)]
public EntitySettings Settings;
private int _price;
public int Price(ItemManager itemManager)
{
if (_price == 0)
if (_price != 0) return _price;
var hullData = itemManager.GetData(Hull);
_price = hullData.Price;
foreach (var (_, item) in Equipment)
{
var hullData = itemManager.GetData(Hull);
_price = hullData.Price;
var itemData = itemManager.GetData(item);
_price += itemData.Price;
}
foreach (var (_, item) in CargoBays)
{
var itemData = itemManager.GetData(item);
_price += itemData.Price;
}
foreach (var (_, item) in DockingBays)
{
var itemData = itemManager.GetData(item);
_price += itemData.Price;
}
foreach (var (_, item) in Equipment)
foreach (var t in CargoContents)
{
foreach (var (_, item) in t)
{
var itemData = itemManager.GetData(item);
_price += itemData.Price;
if (item is SimpleCommodity s)
_price += itemData.Price * s.Quantity;
else
_price += itemData.Price;
}
foreach (var (_, item) in CargoBays)
}
foreach (var t in DockingBayContents)
{
foreach (var (_, item) in t)
{
var itemData = itemManager.GetData(item);
_price += itemData.Price;
if (item is SimpleCommodity s)
_price += itemData.Price * s.Quantity;
else
_price += itemData.Price;
}
foreach (var (_, item) in DockingBays)
{
var itemData = itemManager.GetData(item);
_price += itemData.Price;
}
for (var bayIndex = 0; bayIndex < CargoContents.Length; bayIndex++)
foreach (var (_, item) in CargoContents[bayIndex])
{
var itemData = itemManager.GetData(item);
if (item is SimpleCommodity s)
_price += itemData.Price * s.Quantity;
else
_price += itemData.Price;
}
for (var bayIndex = 0; bayIndex < DockingBayContents.Length; bayIndex++)
foreach (var (_, item) in DockingBayContents[bayIndex])
{
var itemData = itemManager.GetData(item);
if (item is SimpleCommodity s)
_price += itemData.Price * s.Quantity;
else
_price += itemData.Price;
}
}
return _price;

View file

@ -0,0 +1,9 @@
using System;
using MessagePack;
[MessagePackObject, Serializable]
public class EntitySettings
{
[Key(0)]
public float ShutdownPerformance = .1f;
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 26d8ea7e19574688bc73286fbc929359
timeCreated: 1615691660

View file

@ -13,7 +13,7 @@ public class OrbitalEntity : Entity
{
public Guid OrbitData;
public OrbitalEntity(ItemManager itemManager, Zone zone, EquippableItem hull, Guid orbit) : base(itemManager, zone, hull)
public OrbitalEntity(ItemManager itemManager, Zone zone, EquippableItem hull, Guid orbit, EntitySettings settings) : base(itemManager, zone, hull, settings)
{
OrbitData = orbit;
}

View file

@ -4,9 +4,6 @@ using MessagePack;
[MessagePackObject, Serializable]
public class PlayerSettings
{
[Key(0)]
public string Name;
[Key(1)]
public float ShutdownPerformance = .25f;
[Key(0)] public string Name = "Anonymous";
[Key(1)] public SavedGame CurrentRun;
}

View file

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MessagePack;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[MessagePackObject]
public class SavedGame
{
[Key(0)]
public SavedZone[] Zones;
[Key(1)]
public Guid[] Factions;
[Key(2)]
public Dictionary<int, int> HomeZones;
[Key(3)]
public Dictionary<int, int> BossZones;
[Key(4)]
public int Entrance;
[Key(5)]
public int Exit;
[Key(6)]
public int CurrentZone;
[Key(7)]
public int CurrentZoneEntity;
public SavedGame() { }
public SavedGame(Sector sector)
{
Factions = sector.HomeZones.Keys.Select(f => f.ID).ToArray();
HomeZones = sector.HomeZones.ToDictionary(
x => Array.IndexOf(Factions, x.Key.ID),
x => Array.IndexOf(sector.Zones, x.Value));
BossZones = sector.BossZones.ToDictionary(
x => Array.IndexOf(Factions, x.Key.ID),
x => Array.IndexOf(sector.Zones, x.Value));
Zones = sector.Zones.Select(zone=> new SavedZone
{
Name = zone.Name,
Position = zone.Position,
AdjacentZones = zone.AdjacentZones.Select(az=> Array.IndexOf(sector.Zones, az)).ToArray(),
Factions = zone.Factions.Select(f=> Array.IndexOf(Factions, f.ID)).ToArray(),
Contents = zone.Contents.Pack(),
Owner = Array.IndexOf(Factions, zone.Owner.ID)
}).ToArray();
Entrance = Array.IndexOf(sector.Zones, sector.Entrance);
Exit = Array.IndexOf(sector.Zones, sector.Exit);
}
}
[MessagePackObject]
public class SavedZone
{
[Key(0)]
public string Name;
[Key(1)]
public float2 Position;
[Key(2)]
public int[] AdjacentZones;
[Key(3)]
public int[] Factions;
[Key(4)]
public int Owner;
[Key(5)]
public ZonePack Contents;
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8996ce0796b84c50afbadabb5edc4c6c
timeCreated: 1615670570

View file

@ -2,17 +2,20 @@ using System;
using System.Collections.Generic;
using System.Linq;
using DataStructures.ViliWonka.Heap;
using Unity.Mathematics;
using MIConvexHull;
using JM.LinqFaster;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using Random = Unity.Mathematics.Random;
public class Sector
{
public List<SectorZone> Zones = new List<SectorZone>();
public Dictionary<MegaCorporation, SectorZone> HomeZones = new Dictionary<MegaCorporation, SectorZone>();
public Dictionary<MegaCorporation, SectorZone> BossZones = new Dictionary<MegaCorporation, SectorZone>();
public SectorZone Entrance;
public SectorZone Exit;
public SectorZone[] Zones { get; }
public SectorZone Entrance { get; }
public SectorZone Exit { get; }
private SectorZone[] _exitPath;
@ -24,6 +27,188 @@ public class Sector
}
}
public Sector(DatabaseCache database, SavedGame savedGame)
{
var factions = savedGame.Factions.Select(database.Get<MegaCorporation>).ToArray();
Zones = savedGame.Zones.Select(zone =>
{
return new SectorZone
{
Name = zone.Name,
Position = zone.Position
};
}).ToArray();
for (var i = 0; i < Zones.Length; i++)
{
Zones[i].AdjacentZones = savedGame.Zones[i].AdjacentZones.Select(azi => Zones[azi]).ToList();
Zones[i].Factions = savedGame.Zones[i].Factions.Select(mi => factions[mi]).ToArray();
Zones[i].Owner = factions[savedGame.Zones[i].Owner];
}
HomeZones = savedGame.HomeZones.ToDictionary(
x => factions[x.Key],
x => Zones[x.Value]);
BossZones = savedGame.BossZones.ToDictionary(
x => factions[x.Key],
x => Zones[x.Value]);
Entrance = Zones[savedGame.Entrance];
Exit = Zones[savedGame.Exit];
CalculateDistanceMatrix();
}
public Sector(SectorGenerationSettings settings, MegaCorporation[] factions, ref Random random, float minLineSeparation = .01f)
{
var outputSamples = WeightedSampleElimination.GeneratePoints(settings.ZoneCount,
settings.CloudDensity,
v => (.2f - lengthsq(v - float2(.5f))) * 4);
Zones = outputSamples.Select(v => new SectorZone {Position = v}).ToArray();
#region Link Placement
// 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-5f);
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));
}
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(settings.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 > settings.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)));
}
}
}
#endregion
CalculateDistanceMatrix();
// Entrance is the most isolated zone (highest total distance to all other zones
Entrance = Zones.MaxBy(z => z.Isolation);
// Exit is the zone furthest from the exit
Exit = Zones.MaxBy(z => Exit.Distance[z]);
// 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));
// Choose some megas to have bosses placed based on whether a boss hull is assigned
var bossMegas = factions
.Where(m => m.BossHull != Guid.Empty)
.Take(settings.BossCount)
.ToArray();
// 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 =>
Exit.Distance[z] * Entrance.Distance[z] *
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])));
}
// Place remaining headquarters away from existing megas while also maximizing territory
foreach (var mega in factions.Where(m=>!bossMegas.Contains(m)))
{
HomeZones[mega] = Zones.MaxBy(z =>
pow(ConnectedRegion(z, mega.InfluenceDistance).Count, HomeZones.Count) *
Exit.Distance[z] * Entrance.Distance[z] *
HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])) *
BossZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
}
// Assign faction presence
foreach (var zone in Zones)
{
// Factions are present in all zones within their sphere of influence
zone.Factions = factions
.Where(m => zone.Distance[HomeZones[m]] <= m.InfluenceDistance)
.ToArray();
// Owner of a zone is the faction with the nearest headquarters
var nearestFaction = factions.MinBy(m => zone.Distance[HomeZones[m]]);
if (zone.Distance[HomeZones[nearestFaction]] <= nearestFaction.InfluenceDistance)
zone.Owner = nearestFaction;
}
// Generate zone name using the owner's name generator, otherwise assign catalogue ID
foreach (var zone in Zones)
zone.Name = zone.Owner?.NameGenerator.NextName ?? $"EAC-{random.NextInt(99999).ToString()}";
}
// Cache distance matrix and calculate isolation for every zone (used extensively for placing stuff)
private void CalculateDistanceMatrix()
{
foreach (var zone in Zones)
{
zone.Distance = ConnectedRegionDistance(zone);
zone.Isolation = zone.Distance.Sum(x => x.Value);
}
}
class DijkstraVertex
{
public float Cost;
@ -31,26 +216,6 @@ public class Sector
public SectorZone Zone;
}
// public HashSet<SectorZone> ConnectedRegion(SectorZone v, SectorZone ignoreZone = null)
// {
// var members = new HashSet<SectorZone> {v};
// while (true)
// {
// var lastCount = members.Count;
// // 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
// foreach (var zone in members.ToArray())
// foreach(var adjacentZone in zone.AdjacentZones)
// if (!members.Contains(adjacentZone) && !(zone == v && ignoreZone == adjacentZone))
// members.Add(adjacentZone);
// // If we have stopped finding neighbors, stop traversing
// if (members.Count == lastCount)
// return members;
// }
// }
//public int TotalDistance(SectorZone v) => ConnectedRegionDistance(v).Sum(x => x.distance);
public Dictionary<SectorZone, int> ConnectedRegionDistance(SectorZone v)
{
var members = new Dictionary<SectorZone, int> {{v, 0}};
@ -75,11 +240,11 @@ public class Sector
public HashSet<SectorZone> ConnectedRegion(
SectorZone v,
SectorZone ignoreLink1,
SectorZone ignoreLink2,
SectorZone ignoreLinkSource,
SectorZone ignoreLinkTarget,
int maxDistance = int.MaxValue)
{
return ConnectedRegion(v, x => x.Item1 != ignoreLink1 || x.Item2 != ignoreLink2, maxDistance);
return ConnectedRegion(v, x => x.Item1 != ignoreLinkSource || x.Item2 != ignoreLinkTarget, maxDistance);
}
public HashSet<SectorZone> ConnectedRegion(
@ -99,7 +264,7 @@ public class Sector
public HashSet<SectorZone> ConnectedRegion(
SectorZone v,
Predicate<(SectorZone,SectorZone)> linkFilter,
Predicate<(SectorZone source, SectorZone target)> linkFilter,
int maxDistance = int.MaxValue)
{
var visited = new HashSet<SectorZone> {v};
@ -160,7 +325,7 @@ public class SectorZone
public List<SectorZone> AdjacentZones = new List<SectorZone>();
public int Isolation;
public Dictionary<SectorZone, int> Distance;
public MegaCorporation[] Megas;
//public List<MegaCorporation> Megas;
public MegaCorporation[] Factions;
public MegaCorporation Owner;
public Zone Contents;
}

View file

@ -1,163 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DataStructures.ViliWonka.Heap;
using MIConvexHull;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using Random = Unity.Mathematics.Random;
public static class SectorGenerator
{
public static Sector GenerateSector(SectorGenerationSettings settings, MegaCorporation[] megas, ref Random random, float minLineSeparation = .01f)
{
var outputSamples = WeightedSampleElimination.GeneratePoints(settings.ZoneCount,
settings.CloudDensity,
v => (.2f - lengthsq(v - float2(.5f))) * 4);
var sector = new Sector();
foreach (var v in outputSamples) sector.Zones.Add(new SectorZone {Position = v});
#region Link Placement
// Create a delaunay triangulation to connect adjacent sectors
var triangulation = DelaunayTriangulation<Vertex2<SectorZone>, Cell2<SectorZone>>
.Create(sector.Zones.Select(z => new Vertex2<SectorZone>(z.Position, z)).ToList(), 1e-5f);
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));
}
foreach (var link in links.ToArray())
{
foreach (var zone in sector.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(settings.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 > settings.LinkDensity * links.Count)
{
var link = heap.PopObj();
if(sector.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)));
}
}
}
#endregion
// Cache distance set and calculate isolation for every zone (used extensively for placing stuff)
foreach (var zone in sector.Zones)
{
zone.Distance = sector.ConnectedRegionDistance(zone);
zone.Isolation = zone.Distance.Sum(x => x.Value);
}
// Exit is the most isolated zone (highest total distance to all other zones
sector.Exit = sector.Zones.MaxBy(z => z.Isolation);
// Entrance is the zone furthest from the exit
sector.Entrance = sector.Zones.MaxBy(z => sector.Exit.Distance[z]);
// Store exit path in hash set for querying
var exitPathSet = new HashSet<SectorZone>(sector.ExitPath);
// 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 = sector.ExitPath
.Where(z => z.AdjacentZones.Count > 2 && !sector.ConnectedRegion(sector.Entrance, z).Contains(sector.Exit));
// Choose some megas to have bosses placed based on whether a boss hull is assigned
var bossMegas = megas
.Where(m => m.BossHull != Guid.Empty)
.Take(settings.BossCount)
.ToArray();
// Place boss zones along the critical path as far apart from each other as possible
foreach (var mega in bossMegas)
{
sector.BossZones[mega] = chokePoints.MaxBy(z =>
sector.Exit.Distance[z] * sector.Entrance.Distance[z] *
sector.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)
{
sector.HomeZones[mega] = sector
.ConnectedRegion(sector.BossZones[mega], mega.InfluenceDistance)
.MaxBy(z =>
sector.ConnectedRegion(z, mega.InfluenceDistance).Count *
sector.HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
}
// Place remaining headquarters away from existing megas while also maximizing territory
foreach (var mega in megas.Where(m=>!bossMegas.Contains(m)))
{
sector.HomeZones[mega] = sector.Zones.MaxBy(z =>
pow(sector.ConnectedRegion(z, mega.InfluenceDistance).Count, sector.HomeZones.Count) *
sector.Exit.Distance[z] * sector.Entrance.Distance[z] *
sector.HomeZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])) *
sector.BossZones.Values.Aggregate(1f, (i, os) => i * sqrt(os.Distance[z])));
}
// Assign faction presence
foreach (var zone in sector.Zones)
{
// All megas for are present in zones within their sphere of influence
zone.Megas = megas
.Where(m => zone.Distance[sector.HomeZones[m]] <= m.InfluenceDistance)
.ToArray();
// Owner of a zone is the one with the nearest headquarters
var nearestMega = megas.MinBy(m => zone.Distance[sector.HomeZones[m]]);
if (zone.Distance[sector.HomeZones[nearestMega]] <= nearestMega.InfluenceDistance)
zone.Owner = nearestMega;
}
// Generate zone name using the owner's name generator, otherwise assign catalogue ID
foreach (var zone in sector.Zones)
zone.Name = zone.Owner?.NameGenerator.NextName ?? $"EAC-{random.NextInt(99999).ToString()}";
return sector;
}
}

View file

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 6436908b41be47d98391eabca4e57167
timeCreated: 1615343010

View file

@ -128,7 +128,7 @@ public class Ship : Entity
thruster.Item.Online.Skip(1).Subscribe(b => RecalculateClockwiseTorque());
}
public Ship(ItemManager itemManager, Zone zone, EquippableItem hull) : base(itemManager, zone, hull)
public Ship(ItemManager itemManager, Zone zone, EquippableItem hull, EntitySettings settings) : base(itemManager, zone, hull, settings)
{
void CheckForThruster(EquippedItem item)
{