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.

Here’s a small example of what that would look like:

package
{
	import be.dauntless.astar.core.IAstarTile;
	import be.dauntless.astar.core.IMap;
	import be.dauntless.astar.core.PathRequest;
	import be.dauntless.astar.core.basic2d.Map;
	import be.dauntless.astar.core.basic2d.BasicTile;
 
	/**
	 * @author Jeroen
	 */
	public class TreeRequest extends PathRequest 
	{
		public function TreeRequest(start : IAstarTile, end : IAstarTile, map : IMap, priority : uint = 10)
		{
			super(start, end, map, priority);
 
			this.setHeuristic(Map.NO_HEURISTIC);
 
		}
 
 
		override public function isTarget(p:IAstarTile):Boolean
		{
			return (p as BasicTile).hasTree();
		}
	}
}

This TreeRequest assumes that you have a hasTree() method in the tiles you’re using (BasicTile in the example)

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)

8 Responses to “PathRequest”


  1. 1 frank

    i don´t understand your buildings-override…

    which data i must give to the class for the building????

  2. 2 Dauntless

    For the Buildings example, you have to override the isTarget so that it returns TRUE if the given tile is part of the building you are looking for, and FALSE if it’s not.

    In the example, I had a Building class with an ‘occupies’ method that does exactly that. It checks if the given point is part of the building.

    You could also check it right away. If you know your building takes up the spaces ‘x=2->4’ and ‘y = 3’, you could simply check like this (if you use the BasicTile class):

    override public function isTarget(p:IAstarTile):Boolean
    {
    var pos:Point = (p as BasicTile).getPosition();
    return pos.x >= 2 && pos.x <=4 && pos.y = 3; } But that's a lot less dynamic than using a Building class.

  3. 3 frank

    hmm.. ok.. thanks for your answer, but clear is it no really for me..

    the data for my matrix come from the server, because every player can set furnitures in his room for himself… i cannot hardcode anything… the “building” in this case furnitures must be set dynamically..

    at the moment i´d done the matrix for A* directly in the Array like your datamap -array.. if the program loaded a furniture.. e.g. a chair, it looks, where the chair collides with a 16×16 grid on the roof of the room.. these points are registered as 1 in the matrix..

    anyway…

    if i try to register a variable like
    public var test:Building

    the compiler throws me an error….

  4. 4 Mateusz

    Hej, thanks for great library.
    Can You show me example of TreeRequest?

  5. 5 Dauntless

    I’ve added a small example. I made it in notepad (don’t have FDT at the moment) so let me know if I made an error 🙂

  6. 6 Mateusz

    Thank You! It’s work great.

  7. 7 Mateusz

    PathRequest don’t have setHeuristic method. Does this mean that i must set global heuristic to map?

  8. 8 Dauntless

    My bad, I was looking at my local copy, which is v1.4. I made this change specifically for this situation. I’ve uploaded v1.4 to the main page. It’s still a beta since the documentation isn’t complete, but it should work 🙂 (Some other things might have been moved around also). If you will only be using NO_HEURISTIC, you can of course set it on your map and use v1.3.1. You could even create two maps, one with NO_HEURISTIC and one with the default, but the v1.4 way is much cleaner design :).

  1. 1 auf das ende eines requests warten? - Flashforum

Leave a Reply

*