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