/// <summary> Use kruskals to find the minimum spanning tree plus a few extra edges. </summary> /// <param name="basePaths"> All possible paths. </param> /// <param name="objects"> All rooms. </param> /// <returns> The final set of paths for this floor. </returns> private List <FloorPath> Kruskals(List <FloorPath> basePaths, List <Room> objects) { List <FloorPath> paths = new List <FloorPath>(); List <FloorPath> extraPaths = new List <FloorPath>(); DisjointSets <Room> disjointSets = new DisjointSets <Room>(objects); basePaths.Sort((x, y) => x.Distance.CompareTo(y.Distance)); foreach (FloorPath path in basePaths) { if (!disjointSets.IsSameSet(path.Start, path.End)) { disjointSets.Merge(path.Start, path.End); paths.Add(path); } else { extraPaths.Add(path); } } int center = (int)(extraPaths.Count * .75f); if (center + 2 < extraPaths.Count) { FloorPath p1 = extraPaths[center]; FloorPath p2 = extraPaths[center + 1]; FloorPath p3 = extraPaths[center + 2]; extraPaths.Remove(p1); extraPaths.Remove(p2); extraPaths.Remove(p3); extraPaths.Insert(0, p1); extraPaths.Insert(1, p2); extraPaths.Insert(2, p3); } List <FloorPath> finalPaths = new List <FloorPath>(); foreach (FloorPath e in paths) { finalPaths.Add(e); } int numToAdd = (int)(extraPaths.Count * this.edgeAmount); for (int i = 0; i < numToAdd; i++) { finalPaths.Add(extraPaths[i]); } return(finalPaths); }
private void TryAddPath(Room r1, Room r2, List <FloorPath> basePaths, Floor floor, MiddleLayer upperLayer, MiddleLayer lowerLayer) { if (!CheckPathBlocked(r1.Position, r2.Position, r1, r2, 0, floor, upperLayer, lowerLayer)) { bool duplicate = false; foreach (FloorPath e in basePaths) { if ((e.Start == r1 || e.End == r1) && (e.Start == r2 || e.End == r2)) { duplicate = true; } } if (!duplicate) { List <Edge> edges = FindPath( r1.Position, r2.Position, r1, r2, new List <Edge>(), 0, floor, upperLayer, lowerLayer); float dist = 0; foreach (Edge e in edges) { dist += e.Distance; } FloorPath path = new FloorPath { Start = r1, End = r2, Distance = dist, Width = this.pathWidth, Edges = edges.ToArray() }; basePaths.Add(path); } } }