The default PathRequest (found in be.dauntless.astar.core) lets you find the path from one tile on the map to another. A PathRequest requires the two tiles, the map that should be used and a priority. PathRequests with higher priorities will be handled first.

As stated on the Analyzers page, you can add Analyzers to a PathRequest. This way you can give different behaviors to different PathRequests. For example, your kangaroo-friend may be able to jump over walls, while your hero has to walk around them.

PathRequest.isTarget()

This is probably the most important method of PathRequest. It simply tells the Astar class if the given tile is the target or not. PathRequest has a simple implementation of this:

public function isTarget(tile:IAstarTile):Boolean
{
	return tile == this.getEnd();
}

Suppose you want your soldier to attack a building. The building is of course a lot bigger than your soldier and it will occupy more than one tile. Your soldier doesn’t really care what side it attacks. He just wants to know how he can start attack the building as soon as possible. One way to solve this problem is to check where your soldier is, find out which tile of the building is closest to the soldier and start a simple path search. However, you can’t tell for sure if the tile that is closest to the soldier is also the best path. If a wall is blocking that side of the building, you can only attack from another side.

You can solve this problem by extending PathRequest and overriding the isTarget() method:

package  
{
	import be.dauntless.astar.core.IAstarTile;
	import be.dauntless.astar.core.IMap;
	import be.dauntless.astar.core.PathRequest;
 
	/**
	 * @author Jeroen
	 */
	public class BuildingRequest extends PathRequest 
	{
 
		private var building:Building;
		public function BuildingRequest(start : IAstarTile, end : IAstarTile, map : IMap, building:Building, priority : uint = 10)
		{
			super(start, end, map, priority);
 
			this.building = building;
		}
		override public function isTarget(p:IAstarTile):Boolean
		{
			return building.occupies(p);
		}
	}
}

If the tile that is being visited is occupied by the building, the search has finished. You do still need a target for the request. But this can also easily be fixed: you could simply use the center of the building as a target tile, or you could still use the corner of the building that’s closest to your soldier. The difference now is that Astar will now stop with a valid solution, in stead of stopping without finding a path, because it can’t get to the given corner of the building.

What if I don’t know my target?

This is of course a common problem. Take for example a game where you want your workers to search for trees to cut down for wood. You could loop over every tree on the map, find out which one is the closest and then find a path to that tree. If you have a big map with a lot of trees you probably don’t want to do this. What you could do is disable the heuristic (Map.heuristic = NO_HEURISTIC) and extend the PathRequest with a TreeRequest that will stop if a tree is found.

Because no heuristic is used, tiles will be searched starting with the tiles that are closest to the start tile and then tiles that are further away:

No Heuristic

(The blue tile is your starting tile and it will expand its search from darker tiles to brighter tiles)

0 Responses to “PathRequest”


  1. No Comments

Leave a Reply