I found that RandomPath doesn’t use nnConstraint.graph to constrain nodes search. It seemed counterintuitive to me. But this fix seems to work:
added:
if (nnConstraint.graphMask == -1 || nnConstraint.graphMask.Contains((int)node.GraphIndex))
public override void OnVisitNode (uint pathNode, uint hScore, uint gScore) {
// This method may be called multiple times without checking if the path is complete yet.
if (CompleteState != PathCompleteState.NotCalculated) return;
if (gScore >= searchLength) {
if (gScore <= searchLength+spread) {
nodesEvaluatedRep++;
// Use reservoir sampling to pick a node from the ones with the highest G score
if (rnd.NextDouble() <= 1.0f/nodesEvaluatedRep) {
var node = pathHandler.GetNode(pathNode);
if (nnConstraint.graphMask == -1 || nnConstraint.graphMask.Contains((int)node.GraphIndex))
{
chosenPathNodeIndex = pathNode;
chosenPathNodeGScore = gScore;
}
}
} else {
// If no node was in the valid range of G scores, then fall back to picking one right outside valid range
if (chosenPathNodeIndex == uint.MaxValue) {
chosenPathNodeIndex = pathNode;
chosenPathNodeGScore = gScore;
}
OnFoundEndNode(chosenPathNodeIndex, 0, chosenPathNodeGScore);
}
} else if (gScore > maxGScore) {
maxGScore = gScore;
maxGScorePathNodeIndex = pathNode;
}
}
Is it ok or is there better solution?