using System.Collections.Generic; using UnityEngine; /// /// Utility functions for building and querying puzzle step dependency graphs. /// public static class PuzzleGraphUtility { /// /// Builds a dependency graph: for each step, lists the steps it depends on. /// /// All puzzle steps. /// Dictionary mapping each step to the list of steps it depends on. public static Dictionary> BuildDependencyGraph(IEnumerable steps) { var graph = new Dictionary>(); foreach (var step in steps) { graph[step] = new List(); } foreach (var step in steps) { foreach (var unlocked in step.unlocks) { if (!graph.ContainsKey(unlocked)) graph[unlocked] = new List(); graph[unlocked].Add(step); } } return graph; } /// /// Finds initial steps (no dependencies). /// /// Dependency graph. /// List of steps with no dependencies. public static List FindInitialSteps(Dictionary> graph) { var initial = new List(); foreach (var kvp in graph) { if (kvp.Value.Count == 0) initial.Add(kvp.Key); } return initial; } /// /// Finds all steps unlocked by a given step. /// /// The step to check unlocks for. /// All puzzle steps. /// List of steps unlocked by the given step. public static List FindUnlocks(PuzzleStepSO step, IEnumerable allSteps) { var unlocks = new List(); foreach (var s in allSteps) { if (s.unlocks.Contains(step)) unlocks.Add(s); } return unlocks; } }