View Javadoc

1   package nl.tudelft.goal.unreal.floydwarshall;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.logging.Level;
6   import java.util.logging.Logger;
7   
8   import cz.cuni.amis.pogamut.base.agent.IGhostAgent;
9   import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
10  import cz.cuni.amis.pogamut.base.agent.navigation.IPathFuture;
11  import cz.cuni.amis.pogamut.base.agent.navigation.IPathPlanner;
12  import cz.cuni.amis.pogamut.base.agent.navigation.impl.PrecomputedPathFuture;
13  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
15  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPointNeighbourLink;
16  import cz.cuni.amis.pogamut.ut2004.communication.translator.shared.events.MapPointListObtained;
17  import cz.cuni.amis.utils.collections.MyCollections;
18  
19  /**
20   * Wrapper for a FloyWarschallMap shared by multiple agents.
21   *
22   * @author M.P. Korstanje
23   */
24  public class SharedFloydWarshallMap extends SensorModule<IGhostAgent> implements IPathPlanner<NavPoint> {
25  
26      /**
27       * Prohibited edges.
28       */
29      protected int badEdgeFlag = 0;
30      /**
31       * Our map
32       */
33      protected FloydWarshallMap sharedMap = null;
34      private IWorldEventListener<MapPointListObtained> mapListener = new IWorldEventListener<MapPointListObtained>() {
35          @Override
36          public void notify(MapPointListObtained event) {
37              if (log.isLoggable(Level.INFO)) {
38                  log.info("Map point list obtained.");
39              }
40              sharedMap = FloydWarshallMapCache.getInstance().createMap(event, badEdgeFlag, log);
41          }
42      };
43  
44      public SharedFloydWarshallMap(IGhostAgent bot) {
45          this(bot, null);
46      }
47  
48      public SharedFloydWarshallMap(IGhostAgent bot, Logger log) {
49          this(bot, FloydWarshallMap.BAD_EDGE_FLAG, log);
50      }
51  
52      public SharedFloydWarshallMap(IGhostAgent bot, int badEdgeFlag, Logger log) {
53          super(bot, log);
54          this.badEdgeFlag = badEdgeFlag;
55          worldView.addEventListener(MapPointListObtained.class, mapListener);
56      }
57  
58      /**
59       * Transforms a list of navpoints owned by the (bot owning the) shared map
60       * to navpoints that we own. This is important with regards to visibility.
61       *
62       * @param shared
63       * @return
64       */
65      private List<NavPoint> clean(List<NavPoint> shared) {
66  
67          //Empty path.
68          if (shared == null) {
69              return null;
70          }
71  
72          List<NavPoint> clean = new ArrayList<NavPoint>(shared.size());
73  
74          for (NavPoint navpoint : shared) {
75              clean.add(clean(navpoint));
76          }
77  
78          return clean;
79      }
80  
81      /**
82       * Transforms a navpoint owned by the (bot owning the) shared map to
83       * navpoints that we own. This is important with regards to visibility.
84       *
85       * @param shared
86       * @return
87       */
88      private NavPoint clean(NavPoint shared) {
89          return worldView.get(shared.getId(), NavPoint.class);
90      }
91  
92      /**
93       * Returns path between navpoints 'from' -> 'to'. The path begins with
94       * 'from' and ends with 'to'. If such path does not exist, it returns
95       * zero-sized path.
96       * <p>
97       * <p>
98       * Throws exception if object is disabled, see
99       * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object is
100      * enabled by default.
101      *
102      * @param from
103      * @param to
104      * @return
105      */
106     @Override
107     public IPathFuture<NavPoint> computePath(NavPoint from, NavPoint to) {
108         return new PrecomputedPathFuture<NavPoint>(from, to, getPath(from, to));
109     }
110 
111     /**
112      * Whether navpoint 'to' is reachable from the navpoint 'from'.
113      * <p>
114      * <p>
115      * Throws exception if object is disabled, see
116      * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object is
117      * enabled by default.
118      *
119      * @param from
120      * @param to
121      * @return
122      */
123     public boolean reachable(NavPoint from, NavPoint to) {
124         return sharedMap.reachable(from, to);
125     }
126 
127     /**
128      * Calculate's distance between two nav points (using pathfinding).
129      * <p>
130      * <p>
131      * Throws exception if object is disabled, see
132      * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object is
133      * enabled by default.
134      *
135      * @return Distance or POSITIVE_INFINITY if there's no path.
136      */
137     public float getDistance(NavPoint from, NavPoint to) {
138         return sharedMap.getDistance(from, to);
139     }
140 
141     /**
142      * Returns path between navpoints 'from' -> 'to'. The path begins with
143      * 'from' and ends with 'to'. If such path doesn't exist, returns null.
144      * <p>
145      * <p>
146      * Throws exception if object is disabled, see
147      * {@link FloydWarshallMap#setEnabled(boolean)}. Note that the object is
148      * enabled by default.
149      *
150      * @param from
151      * @param to
152      * @return
153      */
154     public List<NavPoint> getPath(NavPoint from, NavPoint to) {
155         return clean(sharedMap.getPath(from, to));
156     }
157 
158     /**
159      * Checks whether the edge is usable.
160      *
161      * @param from Starting nav point.
162      * @param edge NeighNav object representing the edge.
163      * @return boolean
164      */
165     public boolean checkLink(NavPointNeighbourLink edge) {
166         return sharedMap.checkLink(edge);
167 
168     }
169 
170     /**
171      * Hook where to perform clean up of data structures of the module.
172      */
173     @Override
174     protected void cleanUp() {
175         super.cleanUp();
176         sharedMap = null;
177     }
178 
179     public void refreshPathMatrix() {
180         List<NavPoint> navPoints = MyCollections.asList(agent.getWorldView().getAll(NavPoint.class).values());
181         sharedMap.refreshPathMatrix(navPoints);
182     }
183 }