# How to add traversal cost to AIPath with ITraversalProvider?

I am making a game similar to RimWorld where anything that is on a particular tile, including that tile, can affect movement speed. Everything has a MoveSpeedMultiplier (0 is unwalkable, 2 is twice the walking speed) that adds up to the final value. I will try to concisely explain what I’m doing just to make sure this is the way it needs to be done.

Calculate total MoveSpeedMultiplier for 2 scenarios:

1. Dirt tile (0.9) + Food (0.9) on the ground = 0.9*0.9 = 0.81.
2. Shallow water tile (0.2) + floating baggage (0.6) = 0.2*0.6 = 0.12.

Then, these added-up values are calculated into TraversalCost (using 1000 as the base value for traversing a single tile).

1. 1000 / 0.81 = 1235 (1.2 times more expensive to walk over)
2. 1000 / 0.12 = 8334 (8.3 times more expensive to walk over)

Then, these TraversalCost values are assigned to NodeCost in every single node data (my own class).
Finally, I assign these costs via my custom TraversalProvider, like so:
https://hastebin.com/usiricixex.csharp

Now I want my characters to take into account the TraversalCost when calculating a path from their location to some other location. I use AIPath, but I couldn’t get it to work with the custom traversal provider.

I got it to work using seeker:
https://hastebin.com/fowesozabe.csharp

1. Is there any way to make custom traversal providers work with AIPath, or do I have to use Seeker like shown in the example above? If I do use seeker, do I manually have to apply all the modifiers that I use?

2. I use multithreading. To pathfind across a 400x400 map, this method takes about a second to calculate. Am I doing everything as optimally as I should be, or is there any part of this that I can improve?

This is a crucial part of my game. Massive gratitude for your time and an amazing asset.

Hey @aron_granberg, it’s been a while since this question was posted. I’d appreciate an answer since further development completely depends on this.

Hi

If you want a custom ITraversalProvider for the AIPath script you currently need to either pass the path via ai.SetPath and disable the AIPath’s own path calculation code, or you can subclass the AIPath and override the SearchPath method (this is probably the easiest).

Note that connections between grid nodes also have a cost which is not modified by the ITraversalProvider.

1. Especially with high movement costs the pathfinder may have to search a large part of the graph. There are options for precalculating some info to improve the performance, but those are generally only applicable to static maps where things don’t change much.

How can I achieve this precalculation? My map changes not very often, and only in specific spots. I think this would be useful for me.