using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpriteCompiler.AI
{
///
/// Depth-first search with a cutoff
///
public class DepthLimitedSearch : DepthFirstSearch
where T : ISearchNode
where C : IPathCost, new()
{
protected readonly INodeLimiter limit;
public DepthLimitedSearch(AbstractAISearch search, INodeLimiter limit)
: base(search)
{
this.limit = limit;
}
public override IEnumerable Search(ISearchProblem problem, S initialState)
{
// Save the old node expander
var oldExpander = search.Expander;
// Wrap the expander with a depth-limied expanded in order to
// terminate the search
var expander = new DepthLimitedNodeExpander(oldExpander, limit);
search.Expander = expander;
// Run the search
var solution = base.Search(problem, initialState);
// Restore the old expander
search.Expander = oldExpander;
// Check to see we failed and if the reason for failing was not reaching the cutoff depth.
if (search.IsFailure(solution) && expander.CutoffOccured)
{
return null;
}
return solution;
}
public override IEnumerable ExtendSearch(ISearchProblem problem)
{
return base.ExtendSearch(problem);
}
public bool IsCutoff(IEnumerable result)
{
return result == null;
}
}
}