diff --git a/Assets/Map/Graph.cs b/Assets/Map/Graph.cs index 8ec4233..075ab8b 100644 --- a/Assets/Map/Graph.cs +++ b/Assets/Map/Graph.cs @@ -7,16 +7,34 @@ namespace Assets.Map public class Graph { public List Vertices { get; internal set; } = new List(); - public ISet<(int, int)> Edges { get; internal set; } = new HashSet<(int, int)>(); + public Dictionary> _edges = new Dictionary>(); + + public IEnumerable<(int, int)> Edges + { + get + { + return _edges.SelectMany(pair => pair.Value.Where(value => value > pair.Key).Select(value => (pair.Key, value))); + } + + set + { + _edges = new Dictionary>(); + + foreach (var (a, b) in value) + AddEdge(a, b); + } + } public void AddEdge(int a, int b) { - Edges.Add(a < b ? (a, b) : (b, a)); - } - - public IEnumerable<(int, int)> EdgesOf(int a) - { - return Edges.Where(x => x.Item1 == a || x.Item2 == a); + if (!_edges.ContainsKey(a)) + _edges[a] = new List(); + + if (!_edges.ContainsKey(b)) + _edges[b] = new List(); + + _edges[a].Add(b); + _edges[b].Add(a); } public Graph Morph(Func morph) @@ -30,8 +48,9 @@ namespace Assets.Map public IEnumerable Neighbours(int vertex) { - foreach (var (a, b) in this.EdgesOf(vertex)) - yield return a == vertex ? b : a; + if (!_edges.ContainsKey(vertex)) return Enumerable.Empty(); + + return _edges[vertex]; } } } \ No newline at end of file diff --git a/Assets/Map/LocationGenerator.cs b/Assets/Map/LocationGenerator.cs index f5e86d6..8bbceda 100644 --- a/Assets/Map/LocationGenerator.cs +++ b/Assets/Map/LocationGenerator.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using Assets.Voronoi; using Random = System.Random; @@ -12,7 +11,6 @@ namespace Assets.Map private readonly Graph _locations; private readonly List<(int, Location)> _queue = new List<(int, Location)>(); - private readonly Dictionary> _blacklist = new Dictionary>(); private readonly Random _random; @@ -20,7 +18,6 @@ namespace Assets.Map public Graph Result => _locations; public bool Done => _queue.Count == 0; - public LocationGenerator(List locations, Graph sites, Random random = null) { @@ -32,11 +29,7 @@ namespace Assets.Map public void SetLocation(int vertex, Location location) { - _sites.Vertices[vertex].Location = location; - location.Sites.Add(_sites.Vertices[vertex]); - - FixBoundaryPoints(vertex, location); - FixBoundaryEdges(vertex, location); + location.AddSite(_sites.Vertices[vertex]); foreach (var neighbour in _sites.Neighbours(vertex)) if (_sites.Vertices[neighbour].Location == null) @@ -75,24 +68,5 @@ namespace Assets.Map { } } - - private void FixBoundaryPoints(int vertex, Location location) - { - var a = location.BoundaryPoints; - var b = _sites.Vertices[vertex].Site.Vertices; - - if (!_blacklist.ContainsKey(location)) _blacklist[location] = new List(); - _blacklist[location].AddRange(a.Intersect(b)); - - location.BoundaryPoints = a.Union(b).Except(_blacklist[location]).ToList(); - } - - private void FixBoundaryEdges(int vertex, Location location) - { - var a = location.BoundaryEdges; - var b = _sites.Vertices[vertex].Site.Edges; - - location.BoundaryEdges = a.Union(b).Except(a.Intersect(b)).ToList(); - } } } \ No newline at end of file diff --git a/Assets/Map/LocationSite.cs b/Assets/Map/LocationSite.cs index 551fa62..bb2a695 100644 --- a/Assets/Map/LocationSite.cs +++ b/Assets/Map/LocationSite.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Linq; using Assets.Common; using Assets.Voronoi; using UnityEngine; @@ -8,10 +10,30 @@ namespace Assets.Map public class Location { public List Sites = new List(); - public List BoundaryPoints = new List(); + + public IEnumerable Points => Sites.SelectMany(site => site.Site.Vertices); + public IEnumerable BoundaryPoints => BoundaryEdges.SelectMany(edge => new [] { edge.Item1, edge.Item2 }).Distinct(); + public IEnumerable InsidePoints => Points.Except(BoundaryPoints); + public List<(int, int)> BoundaryEdges = new List<(int, int)>(); public LocationType Type; + + public void AddSite(LocationSite site) + { + site.Location = this; + Sites.Add(site); + + FixBoundaryEdges(site); + } + + private void FixBoundaryEdges(LocationSite site) + { + var a = BoundaryEdges; + var b = site.Site.Edges; + + BoundaryEdges = a.Union(b).Except(a.Intersect(b)).ToList(); + } } public class LocationSite