Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow path 'weighting' within AStar #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jaycrossler
Copy link

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);

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)
@ondras
Copy link
Owner

ondras commented Nov 13, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants