From fca8b77a21fb0cfc2aca1f10a87c5629b72cf8cb Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 12 Aug 2024 14:12:49 -0700 Subject: [PATCH] Move GetAdjacentSeasons to PathFinder --- MultiversalDiplomacy/Adjudicate/PathFinder.cs | 59 +++++++++++++++++-- MultiversalDiplomacy/Model/Season.cs | 51 ---------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/MultiversalDiplomacy/Adjudicate/PathFinder.cs b/MultiversalDiplomacy/Adjudicate/PathFinder.cs index c01b391..95e1319 100644 --- a/MultiversalDiplomacy/Adjudicate/PathFinder.cs +++ b/MultiversalDiplomacy/Adjudicate/PathFinder.cs @@ -60,7 +60,7 @@ public static class PathFinder (Location currentLocation, Season currentSeason) = toVisit.Dequeue(); visited.Add((currentLocation, currentSeason)); - var adjacents = GetAdjacentPoints(currentLocation, currentSeason); + var adjacents = GetAdjacentPoints(world, currentLocation, currentSeason); foreach ((Location adjLocation, Season adjSeason) in adjacents) { // If the destination is adjacent, then a path exists. @@ -81,11 +81,11 @@ public static class PathFinder return false; } - private static List<(Location, Season)> GetAdjacentPoints(Location location, Season season) + private static List<(Location, Season)> GetAdjacentPoints(World world, Location location, Season season) { - List<(Location, Season)> adjacentPoints = new(); + List<(Location, Season)> adjacentPoints = []; List adjacentLocations = location.Adjacents.ToList(); - List adjacentSeasons = season.GetAdjacentSeasons().ToList(); + List adjacentSeasons = GetAdjacentSeasons(world, season).ToList(); foreach (Location adjacentLocation in adjacentLocations) { @@ -105,4 +105,55 @@ public static class PathFinder return adjacentPoints; } + + /// + /// Returns all seasons that are adjacent to a season. + /// + public static IEnumerable GetAdjacentSeasons(World world, Season season) + { + List adjacents = []; + + // The immediate past and all immediate futures are adjacent. + if (season.Past != null) adjacents.Add(season.Past); + adjacents.AddRange(season.Futures); + + // Find all adjacent timelines by finding all timelines that branched off of this season's + // timeline, i.e. all futures of this season's past that have different timelines. Also + // include any timelines that branched off of the timeline this timeline branched off from. + List adjacentTimelineRoots = new(); + Season? current; + for (current = season; + current?.Past?.Timeline != null && current.Past.Timeline == current.Timeline; + current = current.Past) + { + adjacentTimelineRoots.AddRange( + current.Futures.Where(s => s.Timeline != current.Timeline)); + } + + // At the end of the for loop, if this season is part of the first timeline, then current + // is the root season (current.past == null); if this season is in a branched timeline, + // then current is the branch timeline's root season (current.past.timeline != + // current.timeline). There are co-branches if this season is in a branched timeline, since + // the first timeline by definition cannot have co-branches. + if (current?.Past != null) + { + IEnumerable cobranchRoots = current.Past.Futures + .Where(s => s.Timeline != current.Timeline && s.Timeline != current.Past.Timeline); + adjacentTimelineRoots.AddRange(cobranchRoots); + } + + // Walk up all alternate timelines to find seasons within one turn of this season. + foreach (Season timelineRoot in adjacentTimelineRoots) + { + for (Season? branchSeason = timelineRoot; + branchSeason != null && branchSeason.Turn <= season.Turn + 1; + branchSeason = branchSeason.Futures + .FirstOrDefault(s => s!.Timeline == branchSeason.Timeline, null)) + { + if (branchSeason.Turn >= season.Turn - 1) adjacents.Add(branchSeason); + } + } + + return adjacents; + } } \ No newline at end of file diff --git a/MultiversalDiplomacy/Model/Season.cs b/MultiversalDiplomacy/Model/Season.cs index 5d3f539..8db781f 100644 --- a/MultiversalDiplomacy/Model/Season.cs +++ b/MultiversalDiplomacy/Model/Season.cs @@ -126,55 +126,4 @@ public class Season // Both branched off of the same point || thisRoot.Past == otherRoot.Past; } - - /// - /// Returns all seasons that are adjacent to this season. - /// - public IEnumerable GetAdjacentSeasons() - { - List adjacents = []; - - // The immediate past and all immediate futures are adjacent. - if (this.Past != null) adjacents.Add(this.Past); - adjacents.AddRange(this.FutureList); - - // Find all adjacent timelines by finding all timelines that branched off of this season's - // timeline, i.e. all futures of this season's past that have different timelines. Also - // include any timelines that branched off of the timeline this timeline branched off from. - List adjacentTimelineRoots = new(); - Season? current; - for (current = this; - current?.Past?.Timeline != null && current.Past.Timeline == current.Timeline; - current = current.Past) - { - adjacentTimelineRoots.AddRange( - current.FutureList.Where(s => s.Timeline != current.Timeline)); - } - - // At the end of the for loop, if this season is part of the first timeline, then current - // is the root season (current.past == null); if this season is in a branched timeline, - // then current is the branch timeline's root season (current.past.timeline != - // current.timeline). There are co-branches if this season is in a branched timeline, since - // the first timeline by definition cannot have co-branches. - if (current?.Past != null) - { - IEnumerable cobranchRoots = current.Past.FutureList - .Where(s => s.Timeline != current.Timeline && s.Timeline != current.Past.Timeline); - adjacentTimelineRoots.AddRange(cobranchRoots); - } - - // Walk up all alternate timelines to find seasons within one turn of this season. - foreach (Season timelineRoot in adjacentTimelineRoots) - { - for (Season? branchSeason = timelineRoot; - branchSeason != null && branchSeason.Turn <= this.Turn + 1; - branchSeason = branchSeason.FutureList - .FirstOrDefault(s => s!.Timeline == branchSeason.Timeline, null)) - { - if (branchSeason.Turn >= this.Turn - 1) adjacents.Add(branchSeason); - } - } - - return adjacents; - } }