View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.bot.command;
2   
3   import java.util.logging.Logger;
4   
5   import javax.vecmath.Vector3d;
6   
7   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
8   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddRay;
10  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Configuration;
11  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.FastTrace;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.RemoveRay;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Trace;
14  
15  /**
16   * This command module provides basic ray tracing control. Note that you will
17   * need to register listeners in your code in order to receive answers to
18   * commands issued by this module.
19   * 
20   * For fastTrace register listener for FastTraceResponse() class. For trace
21   * register listener for TraceResponse() class. For autoTracing rays register
22   * listener for AutoTraceRay() class.
23   * 
24   * @author Knight
25   */
26  public class SimpleRayCasting extends BotCommands {
27  
28  	/**
29  	 * Sends a ray from actual bot location to desired location. To receive
30  	 * response register listener for FastTraceResponse() messages and check Id.
31  	 * 
32  	 * This ray collides only with level geometry (not with dynamic items -
33  	 * other players, items).
34  	 * 
35  	 * @param id
36  	 *            user determined id of the ray (use to match response)
37  	 * @param to
38  	 *            end location of this ray
39  	 */
40  	public void fastTrace(String id, ILocated to) {
41  		agent.getAct().act(new FastTrace().setId(id).setTo(to.getLocation()));
42  	}
43  
44  	/**
45  	 * Sends a ray from desired location to desired location. To receive
46  	 * response register listener for FastTraceResponse() messages and check Id.
47  	 * 
48  	 * This ray collides only with level geometry (not with dynamic items -
49  	 * other players, items).
50  	 * 
51  	 * @param id
52  	 *            user determined id of the ray (use to match response)
53  	 * @param from
54  	 *            start location of this ray
55  	 * @param to
56  	 *            end location of this ray
57  	 */
58  	public void fastTrace(String id, ILocated from, ILocated to) {
59  		agent.getAct().act(
60  				new FastTrace().setId(id).setFrom(from.getLocation()).setTo(
61  						to.getLocation()));
62  	}
63  
64  	/**
65  	 * Sends a ray from actual bot location to desired location. To receive
66  	 * response register listener for TraceResponse() messages and check Id.
67  	 * 
68  	 * Normaly this ray collides only with level geometry. If bTraceActors is
69  	 * set to true it will collide also with dynamic items - other players and
70  	 * items or vehicles in the map.
71  	 * 
72  	 * @param id
73  	 *            user determined id of the ray (use to match response)
74  	 * @param to
75  	 *            end location of this ray
76  	 */
77  	public void trace(String id, ILocated to, boolean bTraceActors) {
78  		agent.getAct().act(
79  				new Trace().setId(id).setTo(to.getLocation()).setTraceActors(
80  						bTraceActors));
81  	}
82  
83  	/**
84  	 * Sends a ray from desired location to desired location. To receive
85  	 * response register listener for TraceResponse() messages and check Id.
86  	 * 
87  	 * Normaly this ray collides only with level geometry. If bTraceActors is
88  	 * set to true it will collide also with dynamic items - other players and
89  	 * items or vehicles in the map.
90  	 * 
91  	 * @param id
92  	 *            user determined id of the ray (use to match response)
93  	 * @param from
94  	 *            start location of this ray
95  	 * @param to
96  	 *            end location of this ray
97  	 */
98  	public void trace(String id, ILocated from, ILocated to,
99  			boolean bTraceActors) {
100 		agent.getAct().act(
101 				new Trace().setId(id).setFrom(from.getLocation()).setTo(
102 						to.getLocation()).setTraceActors(bTraceActors));
103 	}
104 
105 	/**
106 	 * Adds a new ray to auto ray tracing rays set. If automatic ray tracing
107 	 * will be enabled, each ray in this set will be cast (natively in UT) each
108 	 * synchronous batch and the response will come through ATR synchronous
109 	 * GameBots messsages (in Pogamut class AutoTraceRay() class). Each ray has
110 	 * its Id used to change its parameters, its direction, lenght and three
111 	 * boolean variables. Each ray is casted from actual bot location and this
112 	 * cannot be changed.
113 	 * 
114 	 * To start automatic ray casting (each synchronous batch) use command
115 	 * enableAutoTracign() or send INIT message with proper parameters
116 	 * (AutoTrace true).
117 	 * 
118 	 * @param id
119 	 *            String id of this ray, used to change its parameters or delete
120 	 *            it
121 	 * @param direction
122 	 *            vector direction of this ray (1,0,0) for straight ahead,
123 	 *            (0,1,0) for 90 degrees right, etc. The vector does not have to
124 	 *            be normalized.
125 	 * @param length
126 	 *            length of the ray in UT units. One character in UT is
127 	 *            approximately 200 UT units long.
128 	 * @param bFastTrace
129 	 *            true if we want to use FastTrace function for this ray cast
130 	 *            (faster, but only world geometry can be traced)
131 	 * @param bFloorCorrection
132 	 *            if true the rays direction will be adjusted according to floor
133 	 *            normal. So if we will climb a hill, the rays will adjust to
134 	 *            the steepnes of the hill.
135 	 * @param bTraceActors
136 	 *            if true and bFastTrace set to true, also dynamic items in the
137 	 *            map will be traced (other players and items)
138 	 */
139 	public void addAutoTraceRay(String id, Vector3d direction, int length,
140 			boolean bFastTrace, boolean bFloorCorrection, boolean bTraceActors) {
141 		AddRay newRay = new AddRay();
142 		newRay.setId(id).setDirection(direction).setLength(length)
143 				.setFastTrace(bFastTrace).setFloorCorrection(bFloorCorrection)
144 				.setTraceActors(bTraceActors);
145 		agent.getAct().act(newRay);
146 	}
147 
148 	/**
149 	 * To change the ray it is sufficient to call addAutoTraceRay function with
150 	 * proper Id. This function is here to tell this information.
151 	 * 
152 	 * @param id
153 	 *            String id of this ray, used to change its parameters or delete
154 	 *            it
155 	 * @param direction
156 	 *            vector direction of this ray (1,0,0) for straight ahead,
157 	 *            (0,1,0) for 90 degrees right, etc. The vector does not have to
158 	 *            be normalized.
159 	 * @param length
160 	 *            length of the ray in UT units. One character in UT is
161 	 *            approximately 200 UT units long.
162 	 * @param bFastTrace
163 	 *            true if we want to use FastTrace function for this ray cast
164 	 *            (faster, but only world geometry can be traced)
165 	 * @param bFloorCorrection
166 	 *            if true the rays direction will be adjusted according to floor
167 	 *            normal. So if we will climb a hill, the rays will adjust to
168 	 *            the steepnes of the hill.
169 	 * @param bTraceActors
170 	 *            if true and bFastTrace set to true, also dynamic items in the
171 	 *            map will be traced (other players and items)
172 	 */
173 	public void changeAutoTraceRay(String id, Vector3d direction, int length,
174 			boolean bFastTrace, boolean bFloorCorrection, boolean bTraceActors) {
175 		this.addAutoTraceRay(id, direction, length, bFastTrace,
176 				bFloorCorrection, bTraceActors);
177 	}
178 
179 	/**
180 	 * This command removes all existing auto trace rays and adds a default auto
181 	 * trace rays set. This set consists of three rays: first ray with Id
182 	 * StraghtAhead (direction (1,0,0), length 250 UT units), second ray with Id
183 	 * 45toLeft (direction (1,-1,0), length 200 UT units) and third ray with Id
184 	 * 45toRight (direction (1,1,0), length 200 UT units). All rays will be set
185 	 * with FastTrace to false, TraceActors to false and FloorCorrection to
186 	 * false.
187 	 * 
188 	 * To start automatic ray casting (each synchronous batch) use command
189 	 * enableAutoTracign().
190 	 * 
191 	 */
192 	public void addDefaultAutoTraceRays() {
193 		agent.getAct().act(new AddRay().setId("Default"));
194 	}
195 
196 	/**
197 	 * Remove one autoTraceRay with desired id from the list. The ray will be
198 	 * deleted And no longer visualized in the environment.
199 	 * 
200 	 * @param id
201 	 *            id of the ray we want to delete
202 	 */
203 	public void removeAutoTraceRay(String id) {
204 		agent.getAct().act(new RemoveRay().setId(id));
205 	}
206 
207 	/**
208 	 * Removes all autoTraceRays. The rays will be deleted and no longer
209 	 * visualized in the environment.
210 	 * 
211 	 */
212 	public void removeAllAutoTraceRays() {
213 		agent.getAct().act(new RemoveRay().setId("All"));
214 	}
215 
216 	/**
217 	 * Enables auto trace rays. This means that each time GameBots synchronous
218 	 * batch arrives, the rays will be casted and the results will be a part of
219 	 * this synchronous batch (ATR messages, in Pogamut AutoTraceRay() class).
220 	 * To get these results register listener to AutoTraceRays() class.
221 	 * 
222 	 */
223 	public void enableAutoTracing() {
224 		agent.getAct().act(new Configuration().setAutoTrace(true));
225 	}
226 
227 	/**
228 	 * Disables auto trace rays. This means that auto trace rays messages
229 	 * (AutoTraceRay() class) will no longer be a part of GameBots synchronous
230 	 * batch and the rays won't be casted. However the rays won't be deleted.
231 	 * 
232 	 */
233 	public void disableAutoTracing() {
234 		agent.getAct().act(new Configuration().setAutoTrace(false));
235 	}
236 
237 	/**
238 	 * Constructor. Setups the command module based on given agent and logger.
239 	 * 
240 	 * @param agent
241 	 *            AbstractUT2004Bot we will send commands for
242 	 * @param log
243 	 *            Logger to be used for logging runtime/debug info.
244 	 */
245 	public SimpleRayCasting(UT2004Bot agent, Logger log) {
246 		super(agent, log);
247 	}
248 }