View Javadoc

1   package SteeringStuff;
2   
3   import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Raycasting;
4   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
5   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AutoTraceRay;
6   import java.util.HashMap;
7   import java.util.LinkedList;
8   import java.util.concurrent.Future;
9   
10  /**
11   * This class provides the rays of the bot in the navigation layer. The rays could be changed more times (actually two steerings use rays).
12   * @author Marki
13   */
14  public class RaycastingManager {
15      
16      private UT2004Bot botself;
17      public Raycasting raycasting;
18  
19      private static boolean fastTrace = false;
20      private static boolean floorCorrection = false;
21      private static boolean traceActor = false;
22  
23      private HashMap<SteeringType, LinkedList<SteeringRay>> raysMap = new HashMap<SteeringType, LinkedList<SteeringRay>>();
24      private HashMap<SteeringType, IRaysFlagChanged> rayFlagChangedListeners = new HashMap<SteeringType, IRaysFlagChanged>();
25      private HashMap<SteeringType, HashMap<String, Future<AutoTraceRay>>> rayFutures = new HashMap<SteeringType, HashMap<String, Future<AutoTraceRay>>>();
26  
27      /** Creates the new RaycastingManager of the botself with the raycasting module.*/
28      public RaycastingManager(UT2004Bot botself, Raycasting raycasting) {
29          this.botself = botself;
30          this.raycasting = raycasting;
31      }
32  
33      /** Adds new ISteeringPropertiesChangedListener - he will listen for changes in some steering properties.*/
34      public void addRayFlagChangedListener(SteeringType type, IRaysFlagChanged listener) {
35          rayFlagChangedListeners.put(type, listener);
36      }
37  
38      /** Notify the listneres, that the steering properties were changed.*/
39      public void notifyRayFlagChangedListeners() {
40          for (IRaysFlagChanged listener : rayFlagChangedListeners.values()) {
41              if (SteeringManager.DEBUG) System.out.println("Flag ray changed. We tell to "+listener);
42              listener.flagRaysChanged();
43          }
44      }
45  
46      /**Adds rays of the type. The rays are in the rayList. The listener will be added.*/
47      public void addRays(SteeringType type, LinkedList<SteeringRay> rayList, IRaysFlagChanged listener) {
48          raysMap.put(type, rayList);
49          addRayFlagChangedListener(type, listener);
50          prepareRays();        
51      }
52  
53      /**The rays of the type will be removed.*/
54      public void removeRays(SteeringType type) {
55          if (SteeringManager.DEBUG) System.out.println("We remove rays for "+type);
56          raysMap.remove(type);
57          rayFlagChangedListeners.remove(type);
58          prepareRays();
59      }
60  
61      /**Returns the futureRays of the type.*/
62      public HashMap<String, Future<AutoTraceRay>> getMyFutureRays(SteeringType type) {
63          return rayFutures.get(type);
64      }
65  
66      /**Returns whether the rays are ready (the flag of getAllRaysInitialized).*/
67      public boolean raysAreReady() {
68          return raycasting.getAllRaysInitialized().getFlag();
69      }
70  
71      //Prepares rays for the bot, removes any old rays and sets new ones.
72      private void prepareRays() {
73          raycasting.clear();
74          rayFutures.clear();
75          
76          //botself.getAct().act(new RemoveRay("All"));
77          
78          for(SteeringType type : raysMap.keySet()) {
79              LinkedList<SteeringRay> rayList = raysMap.get(type);
80              if (SteeringManager.DEBUG) System.out.println("We prepare rays for "+type);
81              HashMap<String, Future<AutoTraceRay>> fr = new HashMap<String, Future<AutoTraceRay>>();
82              for(SteeringRay ray : rayList) {
83                  Future<AutoTraceRay> future = raycasting.createRay(ray.id, ray.direction, ray.length, fastTrace, floorCorrection, traceActor);
84                  //System.out.println("Ray "+ray.id);
85                  fr.put(ray.id, future);
86              }
87              rayFutures.put(type, fr);
88          }
89          
90          raycasting.endRayInitSequence();
91          notifyRayFlagChangedListeners();
92      }
93  }