View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric;
2   
3   import java.util.concurrent.Future;
4   import java.util.logging.Logger;
5   
6   import javax.vecmath.Vector3d;
7   
8   import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
9   import cz.cuni.amis.pogamut.base.communication.exception.CommunicationException;
10  import cz.cuni.amis.pogamut.ut2004.agent.navigation.navmesh.LevelGeometryModule;
11  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
12  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AutoTraceRay;
14  import cz.cuni.amis.utils.flag.Flag;
15  
16  /**
17   * Support for creating rays used for raycasting (see {@link AutoTraceRay} that is being utilized).
18   * <p><p>
19   * It is designed to be initialized inside {@link IUT2004BotController#initializeController(UT2004Bot)} method call
20   * and may be used since {@link IUT2004BotController#botInitialized(cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo, cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ConfigChange, cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.InitedMessage)}
21   * is called.
22   * @author ik
23   */
24  public class Raycasting extends SensomotoricModule<UT2004Bot> {
25  
26  	RaycastingUT2004 rayUT2004;	
27  	RaycastingBSP rayBSP;
28  	
29      public Flag<Boolean> getAllRaysInitialized() {
30          return rayUT2004.allRaysInitialized;
31      }
32      
33      public Raycasting(UT2004Bot bot) {
34      	this(bot, null);
35      }
36  
37      public Raycasting(UT2004Bot bot, LevelGeometryModule levelGeometryModule) {
38          this(bot, levelGeometryModule, null);
39      }
40      
41      public Raycasting(UT2004Bot bot, LevelGeometryModule levelGeometryModule, Logger log) {
42          super(bot, log);
43          
44          rayUT2004 = new RaycastingUT2004(bot, log);
45          rayBSP = new RaycastingBSP(bot, levelGeometryModule, log);
46          
47          cleanUp();
48      }
49      
50      @Override
51      protected void cleanUp() {
52      	super.cleanUp();
53      }
54  
55      /**
56       * Deletes all previous rays and makes this instance ready for setting up
57       * new rays.
58       */
59      public void clear() throws CommunicationException {
60      	rayBSP.clear();
61      	rayUT2004.clear();        
62      }
63  
64      /**
65       * Once all rays were initialized using createRay(...) methods, call this
66       * method to start listening for response from UT.
67       */
68      public void endRayInitSequence() {
69          rayUT2004.endRayInitSequence();
70      }
71  
72      /**
73       * Initializes ray usind AddRay command and returns future that waits for
74       * the first AutoTraceRay message corresponding to this ray.
75       * @param Id
76       * User set Id of the ray, so the ray can be identified.
77       * 
78       * @param Direction
79       * Vector direction of the ray (it will be relative - added to
80       * the vector, where the bot is looking, also takes into
81       * account angle of the floor the bot is standing on).
82       * 
83       * @param Length
84       * Specifies the length of the ray (in UT units).
85       * 
86       * @param FastTrace
87       * True if we want to use FastTrace function instead of Trace
88       * function (a bit faster but less information provided - just
89       * information if we hit something or not).
90       * 
91       * @param FloorCorrection
92       * If we should correct ray directions accoring floor normal. Note: Has issue - we can't set set rays up or down when correction is active.
93  	 * 
94       * @param TraceActors
95       * If we want to trace also actors – bots, monsters, players,
96       * items. False if we want to trace just level geometry.
97  	 * 
98       * @return
99       */
100     public Future<AutoTraceRay> createRay(String Id, Vector3d Direction, int Length, boolean FastTrace, boolean FloorCorrection, boolean TraceActors) throws CommunicationException {
101        if (!TraceActors && rayBSP.isUsable()) {
102    		   return rayBSP.createRay(Id, Direction, Length, FloorCorrection);
103        } else {
104     	   return rayUT2004.createRay(Id, Direction, Length, FastTrace, FloorCorrection, TraceActors);
105        }
106     }
107 
108     /**
109      * Creates ray with system generated id. Note that the ray is not initialized immediately - we have to wait for GB2004 to 
110      * confirm us. Therefore you will not receive actual instance of {@link AutoTraceRay} but its {@link Future}.
111      * Use method {@link Future#isDone()} to check whether the ray was initialized and method {@link Future#get()} to obtain the ray instance.
112      * 
113      * @param Direction
114      * Vector direction of the ray (it will be relative - added to
115      * the vector, where the bot is looking, also takes into
116      * account angle of the floor the bot is standing on).
117 	 * 
118      * @param Length
119      * Specifies the length of the ray (in UT units).
120      * 
121      * @param FastTrace
122      * True if we want to use FastTrace function instead of Trace
123      * function (a bit faster but less information provided - just
124      * information if we hit something or not).
125      * 
126      * @param FloorCorrection
127      * If we should correct ray directions according floor normal. Note: Has issue - we can't set set rays up or down when correction is active.
128      * 
129      * @param TraceActors
130      * If we want to trace also actors, bots, monsters, players,
131      * items. False if we want to trace just level geometry.
132 	 * 
133      * @return ray's future - use method {@link Future#isDone()} to check whether the ray was initialized and method {@link Future#get()} to obtain the ray instance
134      * @throws cz.cuni.amis.pogamut.base.communication.exceptions.CommunicationException
135      */
136     public Future<AutoTraceRay> createRay(Vector3d Direction, int Length, boolean FastTrace, boolean FloorCorrection, boolean TraceActors) throws CommunicationException {
137     	 if (!TraceActors && rayBSP.isUsable()) {
138    		   return rayBSP.createRay(Direction, Length, FloorCorrection);
139          } else {
140         	 return rayUT2004.createRay(Direction, Length, FastTrace, FloorCorrection, TraceActors);
141          }
142     }
143 
144     /**
145      * Returns a ray of specified id. If the ray of the specified id does not exist
146      * or was not initialized yet then it returns null.
147      * <p><p>
148      * Note that the {@link AutoTraceRay} instance is self updating - once obtained you may use it every
149      * logic cycle to obtain current readings from the ray.
150      * 
151      * @param rayID
152      * @return
153      */
154     public AutoTraceRay getRay(String rayID) {
155         AutoTraceRay r1 = rayUT2004.getRay(rayID);
156         if(r1 != null) return r1;
157         else return rayBSP.getRay(rayID);      
158     }
159 
160     /**
161      * Sets {@link Raycasting#allRaysInitialized} flag to true if all rays has been initialized.
162      */
163     protected void checkIfAllInited() {
164         rayUT2004.checkIfAllInited();
165     }
166     
167 }
168