I have a custom graph generator based off Point Graph that creates the graph I use. I’d like the pathfinder to ignore the normal “distance”/“connection” costs and rely entirely on the ITraversalProvider I wrote when computing paths.
I attempted to make this happen by setting cost = 0 whenever I add a connection in the custom graph generator. However, this doesn’t seem to work, as the ABPaths I ask for still seem to put some weight on going “straight” in addition to minimizing the custom traversal costs.
I know it’s putting “some weight” on going straight because if I use really small costs in my ITraversalProvider (1 vs 2 for cheap vs expensive nodes), it ignores my costs and just goes straight. If I use really large costs (1 vs 20000 for cheap vs expensive nodes), it obeys my custom costs.
Using really large costs seems like a hack that won’t always work and will be ugly to maintain & be unreadable. Is there some way to actually set the connection costs to 0 or tell ABPath to ignore everything but my ITraversalProvider?
Here is the custom graph generator code:
public class HexGraph : PointGraph
{
/// <summary>
/// Scans world hexes and ensures their existence in hex graph for pathfinder
/// </summary>
/// <param name="statusCallback"></param>
/// <returns></returns>
protected override IEnumerable<Progress> ScanInternal()
{
int hexradius = World.instance.hexRadius;
List<PointNode> pointNodes = new List<PointNode>();
Dictionary<Int3, PointNode> nodeDictionary = new Dictionary<Int3, PointNode>();
for (int x = -hexradius; x <= hexradius; x++)
{
for (int y = Mathf.Max(-hexradius, -x - hexradius); y <= Mathf.Min(hexradius, -x + hexradius); y++)
{
int z = -x - y;
PointNode pn = new PointNode(active);
Int3 pos = new Int3(new Vector3(x, y, z));
pn.SetPosition(pos);
pn.Walkable = true;
pointNodes.Add(pn);
nodeDictionary[pos] = pn;
}
}
nodes = pointNodes.ToArray();
nodeCount = nodes.Length;
List<Connection> connections = new List<Connection>();
foreach (PointNode pn in nodes)
{
connections.Clear();
foreach (Vector3i v in HexNeighbors.neighbours)
{
Int3 offset = v.ToInt3();
Int3 pos = pn.position + offset;
if (nodeDictionary.ContainsKey(pos))
{
connections.Add(new Connection { node = nodeDictionary[pos], cost = 0 });
}
}
pn.connections = connections.ToArray();
}
yield return new Progress(1.0f, "Done");
}
}
Here is the ITraversalProvider’s GetTraversalCost fucntion
public uint GetTraversalCost(Path path, GraphNode node)
{
var tile = TileOfNode(node);
if (tile.TerrainType == TerrainType.Hills || tile.TerrainType == TerrainType.Wetland)
{
return 2;
}
return 1;
}