The nearest to the end point walkable node can not be found

Hello!
I’m making a RTS, where the battlefield is divided on hexes. So units can move simultaniously, but there can not be two of them on one hex (node).
So I’ve implemented the BlockManager and NodeBlocker, as in the TurnBased Example, and use its Traversal Provider. Everything works good, when Unit2 is on the way of Unit1, the path is being built around it, as it should. But if the end point is on the same node as Unit2, (which is unwalkable for Unit1), than I get an error message “Error: The node closest to the end point could not be traversed”.
But I want to get the path to the nearest available (walkable) node! As it should be, according to the documentation:
“By default, a search for the closest walkable nodes to the start and end nodes will be carried out…”

Example of requesting path:

Path p = ABPath.Construct(start, end, null);
p.traversalProvider = new TacticBlockManager.TraversalProvider(TacticBlockManager.Instance, TacticBlockManager.BlockMode.OnlySelector,
TacticBlockManager.Instance.Obstacles.Where(x => x != myBlocker).ToList());
p.nnConstraint = NNConstraint.Default;
return StartPath(p, null);

Please, help me to request the path correctly.

Hi

Due to having been designed a very long time before the ITraversalProvider was created, the search for the nearest node unfortunately does not have access to the ITraversalProvider. Easiest method to get this to work is to set (p as ABPath).calculatePartial = true;
You could instead create a custom NNConstraint class which uses the ITraversalProvider, but that is a few more lines of code.

Unfortunately, the first solution doesn’t work at all, I still get the same error message((
Can you give me an example of the second one, please? What exactly should I modify in the NNConstraint class?

Something like this

public class MyNNConstraint : PathNNConstraint {
	public ITraversalProvider provider;
	public Path path;
	public override bool Suitable (GraphNode node) {
		return base.Suitable(node) && provider.CanTraverse(path, node);
	}
}
var nn = new MyNNConstraint();
nn.path = yourPathHere;
nn.provider = yourProviderHere;
yourPathHere.nnConstraint = nn;

It works as expected, thank you very much!!

1 Like