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  
22  
23  
24  	public UT2004AStar(UT2004Bot bot) {
25  		this(new UT2004PFMap(bot.getWorldView()));
26  	}
27  	
28  	
29  
30  
31  
32  	public UT2004AStar(UT2004Bot bot, IPFMapView<NavPoint> view) {
33  		this(new UT2004PFMap(bot.getWorldView()), view);
34  	}
35  	
36  	
37  
38  
39  
40  	public UT2004AStar(UT2004PFMap map) {
41  		this(map, new IPFMapView.DefaultView());
42  	}
43  	
44  	
45  
46  
47  
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  
60  
61  
62  
63  
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  
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 
102 
103 
104 
105 
106 
107 	public synchronized AStarResult<NavPoint> findPath(NavPoint from, NavPoint to) {		
108 		return findPath(new UT2004PFGoal(from, to));
109 	}
110 	
111 	
112 
113 
114 	public void mapChanged() {
115 		getMap().mapChanged();
116 	}
117 
118 }