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