Restricting Pathfinding to only horiz/vert or diagonal connections

Pro version user.
I’m using a single 2D Grid Graph that I want to share between all actors. The nodes are square and have 8-point connects (so, connections in horizontal, vertical, and diagonal directions between all nodes).
What I’d like to be able to do is restrict a Seeker to finding only a path that follows certain rules regarding those connections: so I could programatically specify “make a path only using diagonal connections” or “only using horizontal and vertical connections.”
Like in chess, some of my units can only travel orthogonal on the grid and some only diagonally.
Is there some way to do this? NNConstraints only seems to affect nodes themselves. I don’t want to modify or re-scan the grid every time I have to get a path - just restrict what connections a path can use when I request it. I suspect this will take some source code modification, and a pointer in the right direction would be helpful! Thanks.

Hi

Without a custom node type, you cannot do this with a single graph. However if your graph is not that large (i.e you can spare the memory), you could use multiple graphs to get this effect (and choose between them using the NNConstraint or graphMask parameter in the seeker.StartPath call).

For 4-connected nodes, you can set the grid graph’s ‘Neighbours’ field to ‘Four’.
There is no built in support for only using diagonal connections, however I think you should be able to make a quick change to the source and get that working.

In the GridGenerator.cs file, find the line which reads

	/** Which neighbours are going to be used when #neighbours=6 */
	internal static readonly int[] hexagonNeighbourIndices = { 0, 1, 2, 3, 5, 7 };

and change that to

	/** Which neighbours are going to be used when #neighbours=6 */
	internal static readonly int[] hexagonNeighbourIndices = { 4, 5, 6, 7 };

Then set ‘Neighbours’ to ‘Six’ in the grid graph settings.
I haven’t tested this, but I think it should work.

Thank you!

That code works, but the generated paths on the resulting diagonal grid will cut corners (I don’t want them to) and therefore not just follow the diagonal connections (and of course the “Cut Corners” bool option goes away on “6-connection” graphs). I took a quick look at how Cut Corners is used on 8-connections graphs in GridGenerator.cs, but it’s a bit out of my depth on how to proceed.

The other issue I’m having is that the types of paths generated for the different connection types are vastly sub-optimal for one or the other. That is, turning off heuristics works best on the diagonal-only graph, while Manhattan works best on the orthogonal graph. Is there some way to have different heuristics on each graph?

Hi

Ah, you need to avoid corner cutting. Ok, then you can do a very quick hack.
In the CalculateConnections method in the GridGraph.cs script, there is a line that reads

node.SetAllConnectionInternal(conns | diagConns);

change that to

if (neighbours == NumNeighbours.Eight) {
    // Set only 4 diagonal connections
    node.SetAllConnectionInternal(diagConns);
} else {
    // Set only 4 orthogonal connections
    node.SetAllConnectionInternal(conns);
}

and then set neighbours to ‘Eight’.

Are they suboptimal? They shouldn’t be… Can you show a screenshot?
The heuristic can be set per path request by assigning the path.heuristic field.

Unfortunately I still need to keep the regular 8-connection functionality as well, as there are units that can move both orthogonal and diagonally.
Is there some way of forcing no-cut-corners on just the 6-connection graph?

Regarding the path routes, I just mean sub-optimal in the context of this particular game. It’s turn-based, so I need to maintain the longest possible straight-lines in the path as opposed to zig-zagging. Path.heuristic should solve that though, thanks.