rot.js icon indicating copy to clipboard operation
rot.js copied to clipboard

Allow path 'weighting' within AStar

Open jaycrossler opened this issue 10 years ago • 1 comments

Added callback function to allow path 'weighting' within AStar so that longer routes through some cells are preferred over shorter routes through other difficult cells (for example, routing around a forest or lake).

Example usage:

    function tile_traversability_weight (x, y) {
        var cell = game.tile_details(x, y);

        var weight = 0;
        if (cell.name == 'plains') weight += 2;
        if (cell.name == 'mountains') weight += 6;
        if (cell.name == 'forest') weight += 3;
        if (cell.name == 'lake') weight += 4;
        if (cell.density == 'medium') weight += 2;
        if (cell.density == 'large') weight += 4;
        if (cell.has('path')) weight -= 1;
        if (cell.has('road')) weight -= 2;
        if (cell.has('rail')) weight -= 4;
        if (cell.has('river')) weight += 2;

        return Math.max(0, weight);
    }  
    astar.compute(from_x, from_y, pathCallback, tile_traversability_weight);

jaycrossler avatar Nov 11 '15 15:11 jaycrossler

I would suggest storing the weightingCallback as a private property, not passing it to _add and calling this._weigtingCallback(x, y) instead.

ondras avatar Nov 13 '15 11:11 ondras