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
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 public void strafeRight(double distance, UnrealId focusId) {
125 if (self == null) {
126 self = agent.getWorldView().getSingle(Self.class);
127 }
128 if (self != null) {
129 Location startLoc = self.getLocation();
130 Location directionVector = self.getRotation().toLocation();
131 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(-distance);
132
133 agent.getAct().act(
134 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusTarget(focusId));
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148 public void strafeRight(double distance, ILocated focusLocation) {
149 if (self == null) {
150 self = agent.getWorldView().getSingle(Self.class);
151 }
152 if (self != null) {
153 Location startLoc = self.getLocation();
154 Location directionVector = self.getRotation().toLocation();
155 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(-distance);
156
157 agent.getAct().act(
158 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusLocation(focusLocation.getLocation()));
159 }
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 public void strafeRight(double distance) {
174 if (self == null) {
175 self = agent.getWorldView().getSingle(Self.class);
176 }
177 if (self != null) {
178 Location startLoc = self.getLocation();
179 Location directionVector = self.getRotation().toLocation();
180 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(-distance);
181
182 agent.getAct().act(
183 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusLocation(
184 startLoc.add(directionVector.getNormalized().scale(
185 FOCUS_DISTANCE))));
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199 public void strafeLeft(double distance, UnrealId focusId) {
200 if (self == null) {
201 self = agent.getWorldView().getSingle(Self.class);
202 }
203 if (self != null) {
204 Location startLoc = self.getLocation();
205 Location directionVector = self.getRotation().toLocation();
206 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(distance);
207
208 agent.getAct().act(
209 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusTarget(focusId));
210 }
211 }
212
213
214
215
216
217
218
219
220
221
222
223 public void strafeLeft(double distance, ILocated focusLocation) {
224 if (self == null) {
225 self = agent.getWorldView().getSingle(Self.class);
226 }
227 if (self != null) {
228 Location startLoc = self.getLocation();
229 Location directionVector = self.getRotation().toLocation();
230 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(distance);
231
232 agent.getAct().act(
233 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusLocation(focusLocation.getLocation()));
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 public void strafeLeft(double distance) {
249 if (self == null) {
250 self = agent.getWorldView().getSingle(Self.class);
251 }
252 if (self != null) {
253 Location startLoc = self.getLocation();
254 Location directionVector = self.getRotation().toLocation();
255 Location targetVec = directionVector.cross(new Location(0, 0, 1)).getNormalized().scale(distance);
256
257 agent.getAct().act(
258 new Move().setFirstLocation(startLoc.add(targetVec)).setFocusLocation(
259 startLoc.add(directionVector.getNormalized().scale(
260 FOCUS_DISTANCE))));
261 }
262 }
263
264
265
266
267
268
269
270
271
272
273 public void strafeTo(ILocated location, ILocated focusLocation) {
274 Move move = new Move().setFirstLocation(location.getLocation()).setFocusLocation(focusLocation.getLocation());
275 agent.getAct().act(move);
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 public void strafeTo(ILocated location, UnrealId focus) {
295 Move move = new Move().setFirstLocation(location.getLocation()).setFocusTarget(focus);
296
297
298
299
300
301
302
303
304
305 agent.getAct().act(move);
306 }
307
308
309
310
311
312
313
314
315
316
317 public void doubleJump() {
318 Jump jump = new Jump();
319 jump.setDoubleJump(true);
320
321
322 jump.setForce((double) 680);
323
324 agent.getAct().act(jump);
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 public void generalJump(boolean doubleJump, double secondJumpDelay, double jumpZ) {
342 Jump jump = new Jump();
343 jump.setDoubleJump(doubleJump);
344 if (doubleJump) {
345 jump.setDelay(secondJumpDelay);
346 }
347 jump.setForce(jumpZ);
348 agent.getAct().act(jump);
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 public void doubleJump(double secondJumpDelay, double jumpZ) {
365 Jump jump = new Jump();
366 jump.setDoubleJump(true);
367 jump.setDelay(secondJumpDelay);
368 jump.setForce(jumpZ);
369 agent.getAct().act(jump);
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383 public void dodge(Location direction, boolean bDouble) {
384 direction = direction.getNormalized();
385 double alpha = Math.acos(self.getRotation().toLocation().getNormalized().dot(direction.getNormalized()));
386 double orientation = self.getRotation().toLocation().cross(new Location(0, 0, 1)).dot(direction);
387 if (orientation > 0) {
388 alpha = -alpha;
389 }
390
391 Matrix3d rot = Rotation.constructXYRot(alpha);
392 direction = new Location(1, 0, 0).mul(rot);
393
394 act.act(new Dodge().setDirection(direction).setDouble(bDouble));
395 }
396
397
398
399
400
401
402
403 public void dodgeLeft(ILocated inFrontOfTheBot, boolean bDouble) {
404 ILocated bot = self.getLocation();
405 Location direction = new Location(inFrontOfTheBot.getLocation().x - bot.getLocation().x, inFrontOfTheBot.getLocation().y - bot.getLocation().y, 0);
406 direction = direction.getNormalized();
407
408 double x = direction.getX();
409 double y = direction.getY();
410
411 direction = new Location(-y, x, 0);
412
413 double alpha = Math.acos(self.getRotation().toLocation().getNormalized().dot(direction.getNormalized()));
414 double orientation = self.getRotation().toLocation().cross(new Location(0, 0, 1)).dot(direction);
415 if (orientation > 0) {
416 alpha = -alpha;
417 }
418
419 Matrix3d rot = Rotation.constructXYRot(alpha);
420 direction = new Location(-1, 0, 0).mul(rot);
421
422 this.act.act(new Dodge().setDirection(direction).setDouble(bDouble));
423 }
424
425
426
427
428
429
430
431 public void dodgeRight(ILocated inFrontOfTheBot, boolean bDouble) {
432 ILocated bot = self.getLocation();
433 Location direction = new Location(inFrontOfTheBot.getLocation().x - bot.getLocation().x, inFrontOfTheBot.getLocation().y - bot.getLocation().y, 0);
434 direction = direction.getNormalized();
435
436 double x = direction.getX();
437 double y = direction.getY();
438
439 direction = new Location(-y, x, 0);
440
441 double alpha = Math.acos(self.getRotation().toLocation().getNormalized().dot(direction.getNormalized()));
442
443 double orientation = self.getRotation().toLocation().cross(new Location(0, 0, 1)).dot(direction);
444 if (orientation > 0) {
445 alpha = -alpha;
446 }
447
448 Matrix3d rot = Rotation.constructXYRot(alpha);
449 direction = new Location(1, 0, 0).mul(rot);
450
451 this.act.act(new Dodge().setDirection(direction).setDouble(bDouble));
452 }
453
454
455
456
457
458
459
460 public void dodgeBack(ILocated inFrontOfTheBot, boolean bDouble) {
461 ILocated bot = self.getLocation();
462 Location direction = new Location(bot.getLocation().x - inFrontOfTheBot.getLocation().x, bot.getLocation().y - inFrontOfTheBot.getLocation().y, 0);
463 direction = direction.getNormalized();
464
465 double alpha = Math.acos(self.getRotation().toLocation().getNormalized().dot(direction.getNormalized()));
466
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 dodgeTo(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 alpha = Math.acos(self.getRotation().toLocation().getNormalized().dot(direction.getNormalized()));
490
491 double orientation = self.getRotation().toLocation().cross(new Location(0, 0, 1)).dot(direction);
492 if (orientation > 0) {
493 alpha = -alpha;
494 }
495
496 Matrix3d rot = Rotation.constructXYRot(alpha);
497 direction = new Location(1, 0, 0).mul(rot);
498
499 this.act.act(new Dodge().setDirection(direction).setDouble(bDouble));
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515 public void dodgeRelative(Location direction, boolean bDouble) {
516 agent.getAct().act(new Dodge().setDirection(direction).setDouble(bDouble));
517 }
518
519
520
521
522
523
524
525
526
527
528 public void setSpeed(double speedMultiplier) {
529 Configuration configure = new Configuration();
530 configure.setSpeedMultiplier(speedMultiplier);
531 agent.getAct().act(configure);
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548 public void setRotationSpeed(Rotation newRotationRate) {
549 Configuration configure = new Configuration();
550 configure.setRotationRate(newRotationRate);
551 agent.getAct().act(configure);
552 }
553
554
555
556
557
558
559
560 public AdvancedLocomotion(UT2004Bot agent, Logger log) {
561 super(agent, log);
562 this.selfListener = new SelfListener(agent.getWorldView());
563 }
564
565 @Override
566 public void jump() {
567 super.jump();
568 }
569
570
571
572
573
574
575
576
577
578
579
580 public void jump(double jumpZ) {
581 Jump jump = new Jump();
582 jump.setForce(jumpZ);
583 agent.getAct().act(jump);
584 }
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600 public void jump(boolean doubleJump, double secondJumpDelay, double jumpZ) {
601 Jump jump = new Jump();
602 jump.setDoubleJump(doubleJump);
603 jump.setDelay(secondJumpDelay);
604 jump.setForce(jumpZ);
605 agent.getAct().act(jump);
606 }
607
608 @Override
609 public void moveTo(ILocated location) {
610 super.moveTo(location);
611 }
612
613 @Override
614 public void setRun() {
615 super.setRun();
616 }
617
618 @Override
619 public void setWalk() {
620 super.setWalk();
621 }
622
623 @Override
624 public void stopMovement() {
625 super.stopMovement();
626 }
627
628 @Override
629 public void turnHorizontal(int amount) {
630 super.turnHorizontal(amount);
631 }
632
633 @Override
634 public void turnTo(ILocated location) {
635 super.turnTo(location);
636 }
637
638 @Override
639 public void turnTo(Player player) {
640 super.turnTo(player);
641 }
642
643 @Override
644 public void turnTo(Item item) {
645 super.turnTo(item);
646 }
647
648 @Override
649 public void turnVertical(int amount) {
650 super.turnVertical(amount);
651 }
652 }