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.x = y;
428 direction.y = -x;
429 direction.z = 0;
430
431 direction = direction.scale(100);
432
433 dodge(direction, false);
434 }
435
436
437
438
439
440
441
442 public void dodgeRight(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
443 if (botPosition == null || inFrontOfTheBot == null) return;
444 Location inFront = inFrontOfTheBot.getLocation();
445 ILocated bot = botPosition;
446 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
447 direction = direction.getNormalized();
448
449 double x = direction.getX();
450 double y = direction.getY();
451
452 direction.x = y;
453 direction.y = -x;
454 direction.z = 0;
455
456 direction = direction.scale(100);
457
458 dodge(direction, bDouble);
459 }
460
461
462
463
464
465
466 public void dodgeLeft(ILocated botPosition, ILocated inFrontOfTheBot) {
467 if (botPosition == null || inFrontOfTheBot == null) return;
468 Location inFront = inFrontOfTheBot.getLocation();
469 ILocated bot = botPosition;
470 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
471 direction = direction.getNormalized();
472
473 double x = direction.getX();
474 double y = direction.getY();
475
476 direction.x = -y;
477 direction.y = -x;
478 direction.z = 0;
479
480 direction = direction.scale(100);
481
482 dodge(direction, false);
483 }
484
485
486
487
488
489
490
491 public void dodgeLeft(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
492 if (botPosition == null || inFrontOfTheBot == null) return;
493 Location inFront = inFrontOfTheBot.getLocation();
494 ILocated bot = botPosition;
495 Location direction = new Location(inFront.getLocation().x - bot.getLocation().x, inFront.getLocation().y - bot.getLocation().y, 0);
496 direction = direction.getNormalized();
497
498 double x = direction.getX();
499 double y = direction.getY();
500
501 direction.x = -y;
502 direction.y = -x;
503 direction.z = 0;
504
505 direction = direction.scale(100);
506
507 dodge(direction, bDouble);
508 }
509
510
511
512
513
514
515 public void dodgeBack(ILocated botPosition, ILocated inFrontOfTheBot) {
516 if (botPosition == null || inFrontOfTheBot == null) return;
517 Location inFront = inFrontOfTheBot.getLocation();
518 ILocated bot = botPosition;
519 Location direction = inFront.sub(bot.getLocation());
520 direction.z = 0;
521 direction = direction.getNormalized();
522
523 double x = direction.getX();
524 double y = direction.getY();
525
526 direction.x = -x;
527 direction.y = -y;
528 direction.z = 0;
529
530 direction = direction.scale(100);
531
532 dodge(direction, false);
533 }
534
535
536
537
538
539
540
541 public void dodgeBack(ILocated botPosition, ILocated inFrontOfTheBot, boolean bDouble) {
542 if (botPosition == null || inFrontOfTheBot == null) return;
543 Location inFront = inFrontOfTheBot.getLocation();
544 ILocated bot = botPosition;
545 Location direction = inFront.sub(bot.getLocation());
546 direction.z = 0;
547 direction = direction.getNormalized();
548
549 double x = direction.getX();
550 double y = direction.getY();
551
552 direction.x = -x;
553 direction.y = -y;
554 direction.z = 0;
555
556 direction = direction.scale(100);
557
558 dodge(direction, bDouble);
559 }
560
561
562
563
564
565
566
567
568
569
570
571 public void setSpeed(double speedMultiplier) {
572 Configuration configure = new Configuration();
573 configure.setSpeedMultiplier(speedMultiplier);
574 agent.getAct().act(configure);
575 }
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592 public void setRotationSpeed(Rotation newRotationRate) {
593 Configuration configure = new Configuration();
594 configure.setRotationRate(newRotationRate);
595 agent.getAct().act(configure);
596 }
597
598
599
600
601
602
603
604
605
606 public AdvancedLocomotion(UT2004Bot agent, Logger log) {
607 super(agent, log);
608 this.selfListener = new SelfListener( agent.getWorldView() );
609 }
610
611 @Override
612 public void jump() {
613 super.jump();
614 }
615
616
617
618
619
620
621
622
623
624
625
626 public void jump(double jumpZ) {
627 Jump jump = new Jump();
628 jump.setForce(jumpZ);
629 agent.getAct().act(jump);
630 }
631
632
633
634
635
636
637
638
639
640
641
642
643
644 public void jump(boolean doubleJump, double secondJumpDelay, double jumpZ) {
645 Jump jump = new Jump();
646 jump.setDoubleJump(doubleJump);
647 jump.setDelay(secondJumpDelay);
648 jump.setForce(jumpZ);
649 agent.getAct().act(jump);
650 }
651
652 @Override
653 public void moveTo(ILocated location) {
654 super.moveTo(location);
655 }
656
657 @Override
658 public void setRun() {
659 super.setRun();
660 }
661
662 @Override
663 public void setWalk() {
664 super.setWalk();
665 }
666
667 @Override
668 public void stopMovement() {
669 super.stopMovement();
670 }
671
672 @Override
673 public void turnHorizontal(int amount) {
674 super.turnHorizontal(amount);
675 }
676
677 @Override
678 public void turnTo(ILocated location) {
679 super.turnTo(location);
680 }
681
682 @Override
683 public void turnTo(Player player) {
684 super.turnTo(player);
685 }
686
687 @Override
688 public void turnTo(Item item) {
689 super.turnTo(item);
690 }
691
692 @Override
693 public void turnVertical(int amount) {
694 super.turnVertical(amount);
695 }
696
697 }