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.base.communication.worldview.IWorldView;
8 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
9 import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
10 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
11 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
12 import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
13 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
14 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
15 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Configuration;
16 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ContinuousMove;
17 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Dodge;
18 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Jump;
19 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Move;
20 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.TurnTo;
21 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
22 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
23 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
24
25
26
27
28
29
30
31 public class AdvancedLocomotion extends SimpleLocomotion {
32
33
34
35
36 Self self = null;
37
38
39
40
41
42
43 private class SelfListener implements IWorldObjectEventListener<Self, WorldObjectUpdatedEvent<Self>>
44 {
45 private IWorldView worldView;
46
47
48
49
50
51 public SelfListener(IWorldView worldView)
52 {
53 this.worldView = worldView;
54 worldView.addObjectListener(Self.class, WorldObjectUpdatedEvent.class, this);
55 }
56
57 @Override
58 public void notify(WorldObjectUpdatedEvent<Self> event) {
59 self = event.getObject();
60 }
61 }
62
63
64 private SelfListener selfListener = null;
65
66
67
68
69
70 private static final double FOCUS_DISTANCE = 3000;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public void moveAlong(ILocated firstLocation, ILocated secondLocation) {
92 Move moveAlong = new Move();
93
94 moveAlong.setFirstLocation(firstLocation.getLocation());
95 moveAlong.setSecondLocation(secondLocation.getLocation());
96
97 agent.getAct().act(moveAlong);
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public void moveContinuos() {
111 agent.getAct().act(new ContinuousMove());
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public void strafeRight(double distance, UnrealId focusId) {
127 if (self == null) {
128 self = agent.getWorldView().getSingle(Self.class);
129 }
130 if (self != null) {
131 Location startLoc = self.getLocation();
132 Location directionVector = self.getRotation().toLocation();
133 Location targetVec = directionVector.cross(new Location(0, 0, 1))
134 .getNormalized().scale(-distance);
135
136 agent.getAct().act(
137 new Move().setFirstLocation(startLoc.add(targetVec))
138 .setFocusTarget(focusId));
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public void strafeRight(double distance, ILocated focusLocation) {
155 if (self == null) {
156 self = agent.getWorldView().getSingle(Self.class);
157 }
158 if (self != null) {
159 Location startLoc = self.getLocation();
160 Location directionVector = self.getRotation().toLocation();
161 Location targetVec = directionVector.cross(new Location(0, 0, 1))
162 .getNormalized().scale(-distance);
163
164 agent.getAct().act(
165 new Move().setFirstLocation(startLoc.add(targetVec))
166 .setFocusLocation(focusLocation.getLocation()));
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public void strafeRight(double distance) {
183 if (self == null) {
184 self = agent.getWorldView().getSingle(Self.class);
185 }
186 if (self != null) {
187 Location startLoc = self.getLocation();
188 Location directionVector = self.getRotation().toLocation();
189 Location targetVec = directionVector.cross(new Location(0, 0, 1))
190 .getNormalized().scale(-distance);
191
192 agent.getAct().act(
193 new Move().setFirstLocation(startLoc.add(targetVec))
194 .setFocusLocation(
195 startLoc.add(directionVector
196 .getNormalized().scale(
197 FOCUS_DISTANCE))));
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public void strafeLeft(double distance, UnrealId focusId) {
214 if (self == null) {
215 self = agent.getWorldView().getSingle(Self.class);
216 }
217 if (self != null) {
218 Location startLoc = self.getLocation();
219 Location directionVector = self.getRotation().toLocation();
220 Location targetVec = directionVector.cross(new Location(0, 0, 1))
221 .getNormalized().scale(distance);
222
223 agent.getAct().act(
224 new Move().setFirstLocation(startLoc.add(targetVec))
225 .setFocusTarget(focusId));
226 }
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241 public void strafeLeft(double distance, ILocated focusLocation) {
242 if (self == null) {
243 self = agent.getWorldView().getSingle(Self.class);
244 }
245 if (self != null) {
246 Location startLoc = self.getLocation();
247 Location directionVector = self.getRotation().toLocation();
248 Location targetVec = directionVector.cross(new Location(0, 0, 1))
249 .getNormalized().scale(distance);
250
251 agent.getAct().act(
252 new Move().setFirstLocation(startLoc.add(targetVec))
253 .setFocusLocation(focusLocation.getLocation()));
254 }
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268
269 public void strafeLeft(double distance) {
270 if (self == null) {
271 self = agent.getWorldView().getSingle(Self.class);
272 }
273 if (self != null) {
274 Location startLoc = self.getLocation();
275 Location directionVector = self.getRotation().toLocation();
276 Location targetVec = directionVector.cross(new Location(0, 0, 1))
277 .getNormalized().scale(distance);
278
279 agent.getAct().act(
280 new Move().setFirstLocation(startLoc.add(targetVec))
281 .setFocusLocation(
282 startLoc.add(directionVector
283 .getNormalized().scale(
284 FOCUS_DISTANCE))));
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299 public void strafeTo(ILocated location, ILocated focusLocation) {
300 Move move = new Move().setFirstLocation(location.getLocation())
301 .setFocusLocation(focusLocation.getLocation());
302 agent.getAct().act(move);
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 public void strafeTo(ILocated location, UnrealId focus) {
324 Move move = new Move().setFirstLocation(location.getLocation())
325 .setFocusTarget(focus);
326
327
328
329
330
331
332
333
334
335 agent.getAct().act(move);
336 }
337
338
339
340
341
342
343
344
345 public void doubleJump() {
346 Jump jump = new Jump();
347 jump.setDoubleJump(true);
348
349
350 jump.setForce((double)680);
351
352 agent.getAct().act(jump);
353 }
354
355
356
357
358
359
360
361
362
363
364
365
366
367 public void generalJump(boolean doubleJump, double secondJumpDelay, double jumpZ) {
368 Jump jump = new Jump();
369 jump.setDoubleJump(doubleJump);
370 if (doubleJump) {
371 jump.setDelay(secondJumpDelay);
372 }
373 jump.setForce(jumpZ);
374 agent.getAct().act(jump);
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388 public void doubleJump(double secondJumpDelay, double jumpZ) {
389 Jump jump = new Jump();
390 jump.setDoubleJump(true);
391 jump.setDelay(secondJumpDelay);
392 jump.setForce(jumpZ);
393 agent.getAct().act(jump);
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407
408 public void dodge(Location direction, boolean bDouble) {
409 agent.getAct().act(new Dodge().setDirection(direction).setDouble(bDouble));
410 }
411
412
413
414
415
416
417 public void dodgeRight(ILocated botPosition, ILocated inFrontOfTheBot) {
418 if (botPosition == null || inFrontOfTheBot == null) return;
419 Location inFront = inFrontOfTheBot.getLocation();
420 ILocated bot = botPosition;
421 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
422 direction = direction.getNormalized();
423
424 double x = direction.getX();
425 double y = direction.getY();
426
427 direction = new Location(y, -x, 0);
428
429 direction = direction.scale(100);
430
431 dodge(direction, false);
432 }
433
434
435
436
437
438
439
440 public void dodgeRight(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
441 if (botPosition == null || inFrontOfTheBot == null) return;
442 Location inFront = inFrontOfTheBot.getLocation();
443 ILocated bot = botPosition;
444 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
445 direction = direction.getNormalized();
446
447 double x = direction.getX();
448 double y = direction.getY();
449
450 direction = new Location(y, -x, 0);
451
452 direction = direction.scale(100);
453
454 dodge(direction, bDouble);
455 }
456
457
458
459
460
461
462 public void dodgeLeft(ILocated botPosition, ILocated inFrontOfTheBot) {
463 if (botPosition == null || inFrontOfTheBot == null) return;
464 Location inFront = inFrontOfTheBot.getLocation();
465 ILocated bot = botPosition;
466 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
467 direction = direction.getNormalized();
468
469 double x = direction.getX();
470 double y = direction.getY();
471
472 direction = new Location(-y, x, 0);
473
474 direction = direction.scale(100);
475
476 dodge(direction, false);
477 }
478
479
480
481
482
483
484
485 public void dodgeLeft(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
486 if (botPosition == null || inFrontOfTheBot == null) return;
487 Location inFront = inFrontOfTheBot.getLocation();
488 ILocated bot = botPosition;
489 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
490 direction = direction.getNormalized();
491
492 double x = direction.getX();
493 double y = direction.getY();
494
495 direction = new Location(-y, x, 0);
496
497 direction = direction.scale(100);
498
499 dodge(direction, bDouble);
500 }
501
502
503
504
505
506
507 public void dodgeBack(ILocated botPosition, ILocated inFrontOfTheBot) {
508 if (botPosition == null || inFrontOfTheBot == null) return;
509 Location inFront = inFrontOfTheBot.getLocation();
510 ILocated bot = botPosition;
511 Location direction = inFront.sub(bot.getLocation()).setZ(0);
512 direction = direction.getNormalized();
513
514 double x = direction.getX();
515 double y = direction.getY();
516
517 direction = new Location(-x, -y, 0);
518
519 direction = direction.scale(100);
520
521 dodge(direction, false);
522 }
523
524
525
526
527
528
529
530 public void dodgeBack(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
531 if (botPosition == null || inFrontOfTheBot == null) return;
532 Location inFront = inFrontOfTheBot.getLocation();
533 ILocated bot = botPosition;
534 Location direction = inFront.sub(bot.getLocation()).setZ(0);
535 direction = direction.getNormalized();
536
537 double x = direction.getX();
538 double y = direction.getY();
539
540 direction = new Location(-x, -y, 0);
541
542 direction = direction.scale(100);
543
544 dodge(direction, bDouble);
545 }
546
547
548
549
550
551
552
553
554
555
556
557 public void setSpeed(double speedMultiplier) {
558 Configuration configure = new Configuration();
559 configure.setSpeedMultiplier(speedMultiplier);
560 agent.getAct().act(configure);
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578 public void setRotationSpeed(Rotation newRotationRate) {
579 Configuration configure = new Configuration();
580 configure.setRotationRate(newRotationRate);
581 agent.getAct().act(configure);
582 }
583
584
585
586
587
588
589
590
591
592 public AdvancedLocomotion(UT2004Bot agent, Logger log) {
593 super(agent, log);
594 this.selfListener = new SelfListener( agent.getWorldView() );
595 }
596
597 @Override
598 public void jump() {
599 super.jump();
600 }
601
602
603
604
605
606
607
608
609
610
611
612 public void jump(double jumpZ) {
613 Jump jump = new Jump();
614 jump.setForce(jumpZ);
615 agent.getAct().act(jump);
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630 public void jump(boolean doubleJump, double secondJumpDelay, double jumpZ) {
631 Jump jump = new Jump();
632 jump.setDoubleJump(doubleJump);
633 jump.setDelay(secondJumpDelay);
634 jump.setForce(jumpZ);
635 agent.getAct().act(jump);
636 }
637
638 @Override
639 public void moveTo(ILocated location) {
640 super.moveTo(location);
641 }
642
643 @Override
644 public void setRun() {
645 super.setRun();
646 }
647
648 @Override
649 public void setWalk() {
650 super.setWalk();
651 }
652
653 @Override
654 public void stopMovement() {
655 super.stopMovement();
656 }
657
658 @Override
659 public void turnHorizontal(int amount) {
660 super.turnHorizontal(amount);
661 }
662
663 @Override
664 public void turnTo(ILocated location) {
665 super.turnTo(location);
666 }
667
668 @Override
669 public void turnTo(Player player) {
670 super.turnTo(player);
671 }
672
673 @Override
674 public void turnTo(Item item) {
675 super.turnTo(item);
676 }
677
678 @Override
679 public void turnVertical(int amount) {
680 super.turnVertical(amount);
681 }
682
683 }