Skip to content

Should AStarPathPlanning.get_distance() return 0.0 when no path exists? #158

Description

@ToB213

Currently, AStarPathPlanning.get_distance() returns 0.0 when no path exists between two entities.

When the destination is unreachable, get_path() returns an empty list:

return []

However, get_distance() initializes the distance to 0.0 and sums the distances along the returned path:

path: list[EntityID] = self.get_path(from_entity_id, to_entity_id)
distance: float = 0.0
for i in range(len(path) - 1):
    distance += self.distance(path[i], path[i + 1])
return distance

As a result, get_distance() returns 0.0 when get_path() returns an empty list.

This makes an unreachable destination indistinguishable from being at the destination itself (a true zero-distance case). It can also cause issues such as selecting an unreachable target as the nearest one when selecting targets based on path distance.

Would it be better for get_distance() to return float("inf") when no path exists?

Alternatively, if returning 0.0 for unreachable destinations is intentional, should this behavior be explicitly documented?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions