View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.navigation.astar;
2   
3   import java.util.List;
4   
5   import cz.cuni.amis.pathfinding.alg.astar.AStar;
6   import cz.cuni.amis.pathfinding.alg.astar.AStarResult;
7   import cz.cuni.amis.pathfinding.map.IPFMapView;
8   import cz.cuni.amis.pathfinding.map.IPFMapView.DefaultView;
9   import cz.cuni.amis.pogamut.base.agent.navigation.IPathFuture;
10  import cz.cuni.amis.pogamut.base.agent.navigation.IPathPlanner;
11  import cz.cuni.amis.pogamut.base.agent.navigation.impl.PrecomputedPathFuture;
12  import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.NavigationGraphBuilder;
13  import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004PathAutoFixer;
14  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
15  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
16  import cz.cuni.amis.utils.collections.MyCollections;
17  
18  public class UT2004AStar extends AStar<NavPoint> implements IPathPlanner<NavPoint>  {
19  
20  	/**
21  	 * AStar configured with {@link UT2004PFMap} with no agent-specific view on the map, {@link DefaultView} is used. 
22  	 * @param map
23  	 */
24  	public UT2004AStar(UT2004Bot bot) {
25  		this(new UT2004PFMap(bot.getWorldView()));
26  	}
27  	
28  	/**
29  	 * AStar configured with {@link UT2004PFMap} with no agent-specific view on the map, {@link DefaultView} is used. 
30  	 * @param map
31  	 */
32  	public UT2004AStar(UT2004Bot bot, IPFMapView<NavPoint> view) {
33  		this(new UT2004PFMap(bot.getWorldView()), view);
34  	}
35  	
36  	/**
37  	 * AStar configured with "map" with no agent-specific view on the map, {@link DefaultView} is used. 
38  	 * @param map
39  	 */
40  	public UT2004AStar(UT2004PFMap map) {
41  		this(map, new IPFMapView.DefaultView());
42  	}
43  	
44  	/**
45  	 * AStar configured with "map" and agent-specific view on the map, if "view" is null, {@link DefaultView} is going to be used. 
46  	 * @param map
47  	 * @param view may be null
48  	 */
49  	public UT2004AStar(UT2004PFMap map, IPFMapView<NavPoint> view) {
50  		super(map, view);
51  	}	
52  	
53  	@Override
54  	public UT2004PFMap getMap() {
55  		return (UT2004PFMap) super.getMap();
56  	}
57  	
58  	/**
59  	 * Uses {@link UT2004PFGoal} to define START-NODE (from) and TARGET-NODE (to) for the A-Star, using
60  	 * standard 3D-Euclidian heuristic and customized 'mapView'.
61  	 * @param from
62  	 * @param to
63  	 * @return
64  	 */
65  	public synchronized AStarResult<NavPoint> findPath(NavPoint from, NavPoint to, IPFMapView<NavPoint> mapView) {		
66  		return findPath(new UT2004PFGoal(from, to), mapView);
67  	}
68  	
69  	/**
70  	 * Using {@link #findPath(NavPoint, NavPoint)} to implement {@link IPathPlanner#computePath(Object, Object)} interface.
71  	 */
72  	@Override
73  	public IPathFuture<NavPoint> computePath(NavPoint from, NavPoint to) {
74  		if (from == null || to == null) return new PrecomputedPathFuture<NavPoint>(from, to, null);
75  		if (from == to) return new PrecomputedPathFuture<NavPoint>(from, to, MyCollections.toList(from));
76  		AStarResult<NavPoint> result = findPath(from, to);
77  		if (result == null) return new PrecomputedPathFuture<NavPoint>(from, to, null);
78  		return new PrecomputedPathFuture<NavPoint>(from, to, result.getPath());		
79  	}
80  	
81  	@Override
82  	public double getDistance(NavPoint from, NavPoint to) { 
83  		IPathFuture<NavPoint> path = computePath(from, to);
84  		if (path.isDone()) {
85  			List<NavPoint> list = path.get();
86  			if (list.size() == 0) return 0;
87  			double result = 0;
88  			NavPoint np = list.get(0);
89  			for (int i = 1; i < list.size(); ++i) {
90  				NavPoint next = list.get(i);
91  				result += np.getLocation().getDistance(next.getLocation());
92  				np = next;
93  			}
94  			return result;
95  		} else {
96  			return Double.POSITIVE_INFINITY;
97  		}
98  	}
99  	
100 	/**
101 	 * Uses {@link UT2004PFGoal} to define START-NODE (from) and TARGET-NODE (to) for the A-Star, using
102 	 * standard 3D-Euclidian heuristic.
103 	 * @param from
104 	 * @param to
105 	 * @return
106 	 */
107 	public synchronized AStarResult<NavPoint> findPath(NavPoint from, NavPoint to) {		
108 		return findPath(new UT2004PFGoal(from, to));
109 	}
110 	
111 	/**
112 	 * Call to wipe cached info about the map, e.g., whenever {@link UT2004PathAutoFixer} bans some navpoint or you use {@link NavigationGraphBuilder}, etc.
113 	 */
114 	public void mapChanged() {
115 		getMap().mapChanged();
116 	}
117 
118 }