View Javadoc

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