searchStep method

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

Implementation

@override
void searchStep(GridDijkstraState 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 (point == state.target) {
    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;

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