searchStep method

  1. @override
void searchStep(
  1. GridAstarState state
)
override

Implementation

@override
void searchStep(GridAstarState state) {
  // If there is no open node, we are stuck.
  if (state.open.isEmpty) {
    state.status = Status.failure;
    return;
  }

  GridPoint point = state.open.removeFirst();
  point.set(state.grid, GridNode.closed);

  // If the target node is reached, we are done.
  if (state.target.get(state.grid) == GridNode.closed) {
    state.status = Status.success;
    return;
  }

  for (var (i, j) in state.dirs) {
    GridPoint newPoint = GridPoint(point.x + i, point.y + j);
    if (state.isUntraversable(newPoint)) continue;

    GridNode newNode = newPoint.get(state.grid);
    if (newNode == GridNode.closed) continue;

    int newGCost = state.gCost[point]! + 1; // Assuming uniform cost
    if (newGCost >= (state.gCost[newPoint] ?? double.infinity)) continue;

    state.parents[newPoint] = point;
    state.gCost[newPoint] = newGCost;
    state.fCost[newPoint] =
        newGCost + state.heuristic(newPoint, state.target);

    if (newNode != GridNode.open) {
      state.open.add(newPoint);
      newPoint.set(state.grid, GridNode.open);
    }
  }
}