mwg API
    Preparing search index...

    Class Pathfinder

    Routes across the map.

    A* for "how do I get to that one place", which is what a monster chasing the player needs. A Dijkstra map for "which way is the player from anywhere", which is what a dozen monsters chasing the player need, computed once for the whole level rather than once per monster.

    import { Pathfinder, Level, WALL, FLOOR } from '@datamoc/mw_games/roguelike';

    const level = new Level(20, 20, [WALL, FLOOR], 1);
    const pathfinder = new Pathfinder(level);

    const route = pathfinder.find({ x: 0, y: 0 }, { x: 10, y: 10 }); // one monster's own route

    const distances = pathfinder.distanceMap({ x: 10, y: 10 }); // every monster reads the same map
    Index
    • Autoexplore: the path to the nearest passable, reachable cell not yet explored - "walk towards whatever is unseen" rather than a chosen destination. A breadth-first flood from from stops at the first unexplored cell it reaches, which is nearest by construction; a Dijkstra map from a single target cannot answer this, since there is no one target until the search itself finds one.

      Parameters

      Returns Step[]

      the steps to walk there, or [] when everything reachable is already explored

    • Distance from every reachable cell to to, in steps.

      One pass gives every creature on the map its direction: each reads the value under itself and moves to whichever neighbour is lower. That is a Dijkstra map, and it is the difference between one flood fill per turn and one per monster per turn.

      Parameters

      Returns Int32Array

    • The shortest route from one cell to another.

      Parameters

      Returns Step[]

      the cells to walk through, starting with the one after from and ending on to. Empty when there is no route (including when to is blocked), so a caller should exclude the target itself from blocked if it means to walk into it.