1 package cz.cuni.amis.pogamut.udk.bot.command;
2
3 import java.util.logging.Logger;
4
5 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
6 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
7 import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
8 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
9 import cz.cuni.amis.pogamut.udk.communication.messages.gbcommands.Shoot;
10 import cz.cuni.amis.pogamut.udk.communication.messages.gbcommands.StopShooting;
11 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.BeginMessage;
12 import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.Player;
13
14 /**
15 * Class providing Pogamut2 UT2004 advanced shooting commands for the bot -
16 * shooting in secondary mode, grenade launcher shooting, etc.
17 *
18 * @author Michal 'Knight' Bida
19 */
20 public class AdvancedShooting extends SimpleShooting {
21
22 /** Here we store current UT time used to issue charge shoot commands */
23 private double currentTime = 0;
24 /** Time last charge shoot command was issued, -1 for command not issued */
25 private double lastChargeShootCommandTime = -1;
26 /** Delay of last charge shoot command */
27 private double lastChargeShootCommandDelay = 0;
28 /**
29 * Listener to begin message - used to get UT04 current time.
30 */
31 private IWorldEventListener<BeginMessage> myBegListener = new IWorldEventListener<BeginMessage>() {
32
33 @Override
34 public void notify(BeginMessage bm) {
35 currentTime = bm.getTime();
36
37 // Here we will stop shooting if the charge dealy is exceeded
38 if ((lastChargeShootCommandTime >= 0)
39 && ((currentTime - lastChargeShootCommandTime) > lastChargeShootCommandDelay)) {
40 agent.getAct().act(new StopShooting());
41 lastChargeShootCommandTime = -1;
42 lastChargeShootCommandDelay = 0;
43 }
44 }
45 };
46
47 /**
48 * The bot will stop shooting completely (regardless on the mode of shooting).
49 * <p><p>
50 * (issues GB STOPSHOOT command)
51 */
52 public void stopShooting() {
53 agent.getAct().act(new StopShooting());
54 }
55
56 /**
57 * Bot will start shooting his current weapon with selected mode.
58 * <p><p>
59 * (issues GB SHOOT command)
60 *
61 * @param secondaryMode
62 * If true secondary firing mode will be issued.
63 *
64 */
65 public void shootWithMode(boolean secondaryMode) {
66 Shoot shoot = new Shoot();
67 shoot.setAlt(secondaryMode);
68 agent.getAct().act(shoot);
69 }
70
71 /**
72 * Bot will start shooting his current weapon with primary firing mode at
73 * the location specified. The bot will shoot on this location even when he
74 * will turn a little bit from the direction to the location. If he turn out
75 * more then approx 15 - 30 degrees he won't be able to hit the location
76 * anymore.
77 * <p><p>
78 * (issues GB SHOOT command)
79 *
80 * @param location
81 * Location we will be shooting at.
82 *
83 * @see shootPrimary(UnrealId)
84 */
85 public void shootPrimary(ILocated location) {
86 Shoot shoot = new Shoot();
87 shoot.setLocation(location.getLocation());
88 agent.getAct().act(shoot);
89 }
90
91 /**
92 * Bot will start shooting his current weapon with primary firing mode at
93 * the target specified. The target should exist in the environment. The bot
94 * will track the target in the environment as long as other commands won't
95 * change his focus (strafe(), turnTo()..). If they will the bot will still
96 * shoot on target location until he will turn from target more then approx
97 * 15 - 30 degrees. Then he won't be able to hit the target location
98 * anymore.
99 * <p><p>
100 * (issues GB SHOOT command)
101 *
102 * @param target
103 * Object in the environment we will shoot at.
104 *
105 * @see shootPrimary(ILocated)
106 */
107 public void shootPrimary(UnrealId target) {
108 Shoot shoot = new Shoot();
109 shoot.setTarget(target);
110 agent.getAct().act(shoot);
111 }
112
113 /**
114 * Shortcut for 'shootPrimary(player.getId())', see {@link AdvancedShooting#shootPrimary(UnrealId)}.
115 * <p><p>
116 * (issues GB SHOOT command)
117 *
118 * @param target
119 * Player the bot wants to shoot at.
120 */
121 public void shootPrimary(Player target) {
122 shootPrimary(target.getId());
123 }
124
125 /**
126 * Bot will start shooting his current weapon with secondary firing mode at
127 * the location specified. The bot will shoot on this location even when he
128 * will turn a little bit from the direction to the location. If he turn out
129 * more then approx 15 - 30 degrees he won't be able to hit the location
130 * anymore.
131 * <p><p>
132 * (issues GB SHOOT command)
133 *
134 * @param location
135 * Location we will be shooting at.
136 *
137 * @see shootSecondary(UnrealId)
138 */
139 public void shootSecondary(ILocated location) {
140 Shoot shoot = new Shoot();
141 shoot.setLocation(location.getLocation());
142 shoot.setAlt(true);
143 agent.getAct().act(shoot);
144 }
145
146 /**
147 * Bot will start shooting his current weapon with secondary firing mode at
148 * the target specified. The target should exist in the environment. The bot
149 * will track the target in the environment as long as other commands won't
150 * change his focus (strafe(), turnTo()..). If they will the bot will still
151 * shoot on target location until he will turn from target more then approx
152 * 15 - 30 degrees. Then he won't be able to hit the target location
153 * anymore.
154 * <p><p>
155 * (issues GB SHOOT command)
156 *
157 * @param target
158 * Object in the environment we will shoot at.
159 *
160 * @see shootSecondary(ILocated)
161 */
162 public void shootSecondary(UnrealId target) {
163 Shoot shoot = new Shoot();
164 shoot.setTarget(target);
165 shoot.setAlt(true);
166 agent.getAct().act(shoot);
167 }
168
169 /**
170 * Shortcut for 'shootSecondary(player.getId())', see {@link AdvancedShooting#shootSecondary(UnrealId)}.
171 * <p><p>
172 * (issues GB SHOOT command)
173 *
174 * @param target
175 * Player the bot wants to shoot at.
176 */
177 public void shootSecondary(Player target) {
178 shootSecondary(target.getId());
179 }
180
181 /**
182 * This method can be used for UT2004 charging weapons. Some weapons in
183 * UT2004 feature charging firing modes. These modes works as follows. To
184 * shoot with a charging firing mode the bot has to start shooting first -
185 * this will trigger the weapon to start charging (it won't fire yet). To
186 * fire the weapon the bot needs to send STOPSHOOT command to stop charging
187 * the weapon and to release the projectile. This method does this
188 * automatically for primary firing mode of the weapon. The time of charging
189 * can be specified too (second parameter in seconds).
190 * <p><p>
191 * This method can be also used for non-charing (primary) firing mode of the
192 * weapon - then it will work as burst fire - the bot will continue firing
193 * for the amout of seconds specified.
194 * <p><p>
195 * So if the current weapon primary firing mode is charging, the bot will
196 * release the projectiles once. With normal primary firing mode the bot
197 * will fire a burst.
198 * <p><p>
199 * Note: We will shoot at location specified. The bot will continue to aim
200 * on the location in the environment (for the time of charging or bursting)
201 * as long as other commands won't change his focus (strafe(), turnTo()..).
202 * If they will the bot will still shoot on location until he will turn from
203 * it more then approx 15 - 30 degrees. Then he won't be able to hit the
204 * location anymore.
205 * <p><p>
206 * (issues GB SHOOT command)
207 *
208 * @param location
209 * Location we will be shooting at.
210 * @param chargeTime
211 * In seconds - how long we will charge the weapon (or how long
212 * will be the burst fire).
213 *
214 * @see shootPrimaryCharged(UnrealId, double)
215 */
216 public void shootPrimaryCharged(ILocated location, double chargeTime) {
217 Shoot shoot = new Shoot();
218 shoot.setLocation(location.getLocation());
219 agent.getAct().act(shoot);
220
221 // Stop shoot command will be issued after delay
222 lastChargeShootCommandTime = currentTime;
223 lastChargeShootCommandDelay = chargeTime;
224 }
225
226 /**
227 * This method can be used for UT2004 charging weapons. Some weapons in
228 * UT2004 feature charging firing modes. These modes works as follows. To
229 * shoot with a charging firing mode the bot has to start shooting first -
230 * this will trigger the weapon to start charging (it won't fire yet). To
231 * fire the weapon the bot needs to send STOPSHOOT command to stop charging
232 * the weapon and to release the projectile. This method does this
233 * automatically for primary firing mode of the weapon. The time of charging
234 * can be specified too (second parameter in seconds).
235 * <p><p>
236 * This method can be also used for non-charing (primary) firing mode of the
237 * weapon - then it will work as burst fire - the bot will continue firing
238 * for the amout of seconds specified.
239 * <p><p>
240 * So if the current weapon primary firing mode is charging, the bot will
241 * release the projectiles once. With normal primary firing mode the bot
242 * will fire a burst.
243 * <p><p>
244 * Note: The target for shooting should exist in the environment. The bot
245 * will track the target in the environment (for the time of charging or
246 * bursting) as long as other commands won't change his focus (strafe(),
247 * turnTo()..). If they will the bot will still shoot on target location
248 * until he will turn from target more then approx 15 - 30 degrees. Then he
249 * won't be able to hit the target location anymore.
250 * <p><p>
251 * (issues GB SHOOT command)
252 *
253 * @param target
254 * Object in the environment we will shoot at (basic tracking
255 * provided).
256 * @param chargeTime
257 * In seconds - how long we will charge the weapon (or how long
258 * will be the burst fire).
259 *
260 * @see shootPrimaryCharged(ILocated, double)
261 *
262 * @todo Implement somehow the charging delay.
263 */
264 public void shootPrimaryCharged(UnrealId target, double chargeTime) {
265 Shoot shoot = new Shoot();
266 shoot.setTarget(target);
267 agent.getAct().act(shoot);
268
269 // Stop shoot command will be issued after delay
270 lastChargeShootCommandTime = currentTime;
271 lastChargeShootCommandDelay = chargeTime;
272 }
273
274 /**
275 * Shortcut for 'shootPrimaryCharged(player.getId())', see {@link AdvancedShooting#shootPrimaryCharged(UnrealId, double)}.
276 * <p><p>
277 * (issues GB SHOOT command)
278 *
279 * @param target
280 * Player the bot wants to shoot at.
281 */
282 public void shootPrimaryCharged(Player target, double chargeTime) {
283 shootPrimaryCharged(target.getId(), chargeTime);
284 }
285
286 /**
287 * This method can be used for UT2004 charging weapons. Some weapons in
288 * UT2004 feature charging firing modes. These modes works as follows. To
289 * shoot with a charging firing mode the bot has to start shooting first -
290 * this will trigger the weapon to start charging (it won't fire yet). To
291 * fire the weapon the bot needs to send STOPSHOOT command to stop charging
292 * the weapon and to release the projectile. This method does this
293 * automatically for secondary firing mode of the weapon. The time of
294 * charging can be specified too (second parameter in seconds).
295 * <p><p>
296 * This method can be also used for non-charing (secondary) firing mode of
297 * the weapon - then it will work as burst fire - the bot will continue
298 * firing for the amout of seconds specified.
299 * <p><p>
300 * So if the current weapon secondary firing mode is charging, the bot will
301 * release the projectiles once. With normal secondary firing mode the bot
302 * will fire a burst.
303 * <p><p>
304 * Note: We will shoot at location specified. The bot will continue to aim
305 * on the location in the environment (for the time of charging or bursting)
306 * as long as other commands won't change his focus (strafe(), turnTo()..).
307 * If they will the bot will still shoot on location until he will turn from
308 * it more then approx 15 - 30 degrees. Then he won't be able to hit the
309 * location anymore.
310 * <p><p>
311 * (issues GB SHOOT command)
312 *
313 * @param location
314 * Location we will be shooting at.
315 * @param chargeTime
316 * In seconds - how long we will charge the weapon (or how long
317 * will be the burst fire).
318 *
319 * @see shootSecondaryCharged(UnrealId, double)
320 *
321 * @todo Implement somehow the charging delay.
322 */
323 public void shootSecondaryCharged(ILocated location, double chargeTime) {
324 Shoot shoot = new Shoot();
325 shoot.setLocation(location.getLocation());
326 shoot.setAlt(true);
327 agent.getAct().act(shoot);
328
329 // Stop shoot command will be issued after delay
330 lastChargeShootCommandTime = currentTime;
331 lastChargeShootCommandDelay = chargeTime;
332 }
333
334 /**
335 * This method can be used for UT2004 charging weapons. Some weapons in
336 * UT2004 feature charging firing modes. These modes works as follows. To
337 * shoot with a charging firing mode the bot has to start shooting first -
338 * this will trigger the weapon to start charging (it won't fire yet). To
339 * fire the weapon the bot needs to send STOPSHOOT command to stop charging
340 * the weapon and to release the projectile. This method does this
341 * automatically for secondary firing mode of the weapon. The time of
342 * charging can be specified too (second parameter in seconds).
343 * <p><p>
344 * This method can be also used for non-charing (secondary) firing mode of
345 * the weapon - then it will work as burst fire - the bot will continue
346 * firing for the amout of seconds specified.
347 * <p><p>
348 * So if the current weapon secondary firing mode is charging, the bot will
349 * release the projectiles once. With normal secondary firing mode the bot
350 * will fire a burst.
351 * <p><p>
352 * Note: The target for shooting should exist in the environment. The bot
353 * will track the target in the environment (for the time of charging or
354 * bursting) as long as other commands won't change his focus (strafe(),
355 * turnTo()..). If they will the bot will still shoot on target location
356 * until he will turn from target more then approx 15 - 30 degrees. Then he
357 * won't be able to hit the target location anymore.
358 * <p><p>
359 * (issues GB SHOOT command)
360 *
361 * @param target
362 * Object in the environment we will shoot at (basic tracking
363 * provided).
364 * @param chargeTime
365 * In seconds - how long we will charge the weapon (or how long
366 * will be the burst fire).
367 *
368 * @see shootSecondaryCharged(ILocated, double)
369 *
370 * @todo Implement somehow the charging delay.
371 */
372 public void shootSecondaryCharged(UnrealId target, double chargeTime) {
373 Shoot shoot = new Shoot();
374 shoot.setTarget(target);
375 shoot.setAlt(true);
376 agent.getAct().act(shoot);
377
378 // Stop shoot command will be issued after delay
379 lastChargeShootCommandTime = currentTime;
380 lastChargeShootCommandDelay = chargeTime;
381 }
382
383 /**
384 * Shortcut for 'shootSecondaryCharged(player.getId())', see {@link AdvancedShooting#shootSecondaryCharged(UnrealId, double)}.
385 * <p><p>
386 * (issues GB SHOOT command)
387 *
388 * @param target
389 * Player the bot wants to shoot at.
390 */
391 public void shootSecondaryCharged(Player target, double chargeTime) {
392 shootSecondaryCharged(target.getId(), chargeTime);
393 }
394
395 /**
396 * Resets the agent module so it may be reused.
397 */
398 @Override
399 protected void reset() {
400 lastChargeShootCommandTime = -1;
401 lastChargeShootCommandDelay = 0;
402 }
403
404 /**
405 * Constructor. Setups the command module based on given agent and logger.
406 *
407 * @param agent
408 * AbstractUT2004Bot we will send commands for
409 * @param log
410 * Logger to be used for logging runtime/debug info.
411 *
412 * @todo Implement somehow the charging delay.
413 */
414 public AdvancedShooting(UDKBot agent, Logger log) {
415 super(agent, log);
416 agent.getWorldView()
417 .addEventListener(BeginMessage.class, myBegListener);
418 }
419
420 @Override
421 public void shoot() {
422 super.shoot();
423 }
424
425 @Override
426 public void shoot(UnrealId target) {
427 super.shoot(target);
428 }
429
430 @Override
431 public void shoot(Player target) {
432 super.shoot(target);
433 }
434
435 @Override
436 public void stopShoot() {
437 super.stopShoot();
438 }
439
440
441 }