Skip to content

Astar overrides cost of tiles next to the starting point #371

@eeetem

Description

@eeetem

Describe the bug
When setting the initial values of tiles next to the starting point they're not assigned an open state which often leads to them being overriden other nearby tiles.

This is the code that initiates the costs at the starting point

// Add connecting nodes if traversable
                if (node.Traversable)
                {
                    // Calculate the Costs
                    node.CurrentCost = from.CurrentCost + from.DistanceTo(node) * node.TraversalCostMultiplier;
                    node.EstimatedCost = from.CurrentCost + node.DistanceTo(to);
                    // Enqueue
                    open.Enqueue(node);
                }

And this is the code that iteratively sets the costs

  // Adds a previously not "seen" node into the Queue
                if (connected.State == NodeState.Unconsidered)
                {
                    connected.Parent = current;
                    connected.CurrentCost =
                        current.CurrentCost + current.DistanceTo(connected) * connected.TraversalCostMultiplier;
                    connected.EstimatedCost = connected.CurrentCost + connected.DistanceTo(to);
                    connected.State = NodeState.Open;
                    queue.Enqueue(connected);
                }
                else if (current != connected)
                {
                    // Updating the cost of the node if the current way is cheaper than the previous
                    var newCCost = current.CurrentCost + current.DistanceTo(connected);
                    var newTCost = newCCost + current.EstimatedCost;
                    if (newTCost < connected.TotalCost)
                    {
                        connected.Parent = current;
                        connected.CurrentCost = newCCost;
                    }
                }

The solution is a one line fix, altough it also might make more sense to move both of those code snipets into a sningle function

node.State = NodeState.Open;
i was considering doing a pull request however i do not have the time to write tests for this to fit in wth the guidelnes(since astar does not have any to begin with)

To Reproduce
There's no tests so it's rather difficult as i've only discovered it myself when using the code as part of a bigger project

Expected behavior
The iterative code that sets the costs does NOT override costs of tiles if the cost is higher - which it does as seen in the snipped above UNLESS the tile is unconsidered at which point it assumes the cost is 0 and overrides the cost without checking. The issue here is that all initial tiles start out as unconsidered rather than as open.

Actual behavior
In scenarios where one of the corner nodes is evaluated first
image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions