A* with Node Weights

For Example 8-16, we will be adding weighs to our nodes. We’ll do this by simply adding in some grass tiles to the tile map we have been using in the previous examples. By doing this, we can change the A* search result in a path avoiding the grass tiles has a lower total node value sum than one that travels over the grass tiles.

We can add to the weight of each open node by simply giving it a number higher than 1. We have created our tile sheet to make this very simple. The third tile (or tile index 2) is a grass tile. With astar.as, as long as a tile has a node value greater than 0, it is considered a movable tile. If a path can be made through the maze and the total value of the path, taking the node values into account, is the lowest, the path will cross these nodes with higher values. To demonstrate this, we will now add some grass tiles to the tile map. The changes for Example 8-16 are below. Notice that we are also removing the diagonal movement from Example 8-15, but it is not mandatory that you do so. We will look at that in Example 8-17:

//Example 8-16 changes to example 8-15 tile map
 var mapRows=15;
 var mapCols=15;
 var tileMap=[

Get HTML5 Canvas, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.