@tamb/gamegrid
    Preparing search index...

    Function bfsNextStep

    • First step of a shortest walk from from to to. Returns null when the target is the start cell or no walk exists. Greedy axis steps are not used: beside a long wall they step toward the target and then reverse.

      Parameters

      • matrix: Cell[][]

        Board the walk is planned on.

      • isWalkable: IsWalkable

        Which cells may be entered. Also blocks cells the caller wants skipped.

      • from: Coord

        Start coordinate.

      • to: Coord

        Target coordinate.

      Returns Direction | null

      The first direction, or null.

      import { bfsNextStep, walkableUnlessTypes } from "@tamb/gamegrid/npc";

      const matrix = [
      [{ type: "wall" }, { type: "wall" }, { type: "wall" }],
      [{ type: "wall" }, { type: "floor" }, { type: "floor" }],
      [{ type: "wall" }, { type: "wall" }, { type: "wall" }],
      ];
      const isWalkable = walkableUnlessTypes(["wall"]);
      const toward = bfsNextStep(matrix, isWalkable, [1, 1], [2, 1]);
      // "right"
      import { bfsNextStep, walkableUnlessTypes } from "@tamb/gamegrid/npc";

      const matrix = [
      [{ type: "floor" }, { type: "floor" }, { type: "floor" }, { type: "wall" }, { type: "floor" }],
      [{ type: "floor" }, { type: "wall" }, { type: "wall" }, { type: "wall" }, { type: "floor" }],
      [{ type: "floor" }, { type: "floor" }, { type: "floor" }, { type: "floor" }, { type: "floor" }],
      ];
      const isWalkable = walkableUnlessTypes(["wall"]);
      const toward = bfsNextStep(matrix, isWalkable, [0, 0], [4, 0]);
      // "down". The cells to the right dead-end, so the first step goes around.