1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package nl.tudelft.goal.ut2004.agent;
22
23 import java.lang.reflect.Method;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.logging.Level;
30
31 import nl.tudelft.goal.unreal.actions.Action;
32 import nl.tudelft.goal.unreal.actions.ActionQueue;
33 import nl.tudelft.goal.unreal.messages.BotParameters;
34 import nl.tudelft.goal.unreal.messages.None;
35 import nl.tudelft.goal.unreal.messages.Parameters;
36 import nl.tudelft.goal.unreal.messages.Percept;
37 import nl.tudelft.goal.unreal.messages.UnrealIdOrLocation;
38 import nl.tudelft.goal.unreal.util.Selector;
39 import nl.tudelft.goal.ut2004.actions.Chat;
40 import nl.tudelft.goal.ut2004.actions.DropWeapon;
41 import nl.tudelft.goal.ut2004.actions.Look;
42 import nl.tudelft.goal.ut2004.actions.Navigate;
43 import nl.tudelft.goal.ut2004.actions.Prefer;
44 import nl.tudelft.goal.ut2004.actions.Respawn;
45 import nl.tudelft.goal.ut2004.actions.Shoot;
46 import nl.tudelft.goal.ut2004.actions.Stop;
47 import nl.tudelft.goal.unreal.floydwarshall.SharedFloydWarshallMap;
48 import nl.tudelft.goal.ut2004.messages.Combo;
49 import nl.tudelft.goal.ut2004.messages.FireMode;
50 import nl.tudelft.goal.ut2004.messages.FlagState;
51 import nl.tudelft.goal.ut2004.messages.Scope;
52 import nl.tudelft.goal.ut2004.messages.SelectorList;
53 import nl.tudelft.goal.ut2004.messages.WeaponPrefList;
54 import nl.tudelft.goal.ut2004.selector.ContextSelector;
55 import nl.tudelft.goal.ut2004.selector.NearestEnemy;
56 import nl.tudelft.goal.ut2004.util.Team;
57 import nl.tudelft.pogamut.unreal.agent.module.sensor.Projectiles;
58 import nl.tudelft.pogamut.unreal.agent.module.shooting.WeaponryShooting;
59 import nl.tudelft.pogamut.unreal.agent.module.shooting.util.FocusProvider;
60 import nl.tudelft.pogamut.unreal.agent.module.shooting.util.OrderedFocusProvider;
61 import nl.tudelft.pogamut.ut2004.agent.module.sensor.UT2004Projectiles;
62 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.AssaultRifleShooting;
63 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.BioRifleShooting;
64 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.FlakCannonShooting;
65 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.LigthningGunShooting;
66 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.LinkGunShooting;
67 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.MinigunShooting;
68 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.RocketLauncherShooting;
69 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.ShieldGunShooting;
70 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.ShockRifleShooting;
71 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.SniperRifleShooting;
72 import cz.cuni.amis.pogamut.base.agent.navigation.IPathPlanner;
73 import cz.cuni.amis.pogamut.base.communication.worldview.listener.annotation.EventListener;
74 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
75 import cz.cuni.amis.pogamut.base.utils.math.DistanceUtils;
76 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
77 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
78 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
79 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weapon;
80 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPref;
81 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004AStarPathPlanner;
82 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004GetBackToNavGraph;
83 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004Navigation;
84 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004PathExecutor;
85 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004RunStraight;
86 import cz.cuni.amis.pogamut.ut2004.agent.navigation.loquenavigator.LoqueNavigator;
87 import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004DistanceStuckDetector;
88 import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004PositionStuckDetector;
89 import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004TimeStuckDetector;
90 import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
91 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
92 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotModuleController;
93 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
94 import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemType;
95 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.GetPath;
96 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
97 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotKilled;
98 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
99 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
100 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
101 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.PathList;
102 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
103 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.PlayerKilled;
104 import cz.cuni.amis.pogamut.ut2004.utils.UT2004BotRunner;
105 import cz.cuni.amis.utils.exception.PogamutException;
106 import eis.eis2java.annotation.AsAction;
107 import eis.eis2java.annotation.AsPercept;
108 import eis.eis2java.translation.Filter.Type;
109 import eis.eis2java.util.AllPerceptsModule;
110 import eis.eis2java.util.AllPerceptsProvider;
111 import eis.exceptions.EntityException;
112 import eis.exceptions.PerceiveException;
113
114 @SuppressWarnings("rawtypes")
115 public class UT2004BotBehavior extends UT2004BotModuleController<UT2004Bot>
116 implements AllPerceptsProvider {
117
118 protected List<ContextSelector> targetSelector = new ArrayList<ContextSelector>();
119 protected List<ContextSelector> lookSelector = new ArrayList<ContextSelector>();
120 protected Projectiles projectiles;
121 protected WeaponryShooting weaponShooting;
122 protected FocusProvider lookFocus = new FocusProvider();
123 protected OrderedFocusProvider focus = new OrderedFocusProvider();
124
125 protected AllPerceptsModule percepts;
126
127
128
129 protected BotParameters parameters;
130
131
132
133
134
135
136
137
138 private static final int ACTION_QUEUE_SIZE = 8;
139
140
141
142
143
144 private ActionQueue actions = new ActionQueue(ACTION_QUEUE_SIZE);
145
146 protected long logicIteration;
147 protected long actionCount;
148
149 @Override
150 public void initializeController(UT2004Bot bot) {
151 super.initializeController(bot);
152
153
154 UT2004AgentParameters parameters = bot.getParams();
155 if ((parameters instanceof BotParameters)) {
156 this.parameters = (BotParameters) parameters;
157 } else {
158 log.warning("Provided parameters were not a subclass of UnrealGoalParameters, using defaults.");
159 this.parameters = new BotParameters();
160 }
161 Parameters defaults = BotParameters.getDefaults();
162 this.parameters.assignDefaults(defaults);
163
164 }
165
166
167
168
169 protected void initializeModules(UT2004Bot bot) {
170 super.initializeModules(bot);
171
172 projectiles = new UT2004Projectiles(bot, info);
173 weaponShooting = new WeaponryShooting(bot, info, weaponry, weaponPrefs,
174 shoot);
175
176
177 try {
178 percepts = new AllPerceptsModule(this);
179 } catch (EntityException e) {
180 throw new PogamutException("Could not create percept module", e);
181 }
182
183 initializeWeaponShootings();
184 }
185
186
187
188
189 protected void initializeWeaponShootings() {
190 weaponShooting.addWeaponShooting(new LinkGunShooting(bot, info, shoot,
191 weaponry));
192 weaponShooting.addWeaponShooting(new ShockRifleShooting(bot, info,
193 shoot, weaponry, projectiles));
194 weaponShooting.addWeaponShooting(new MinigunShooting(bot, info, shoot,
195 weaponry));
196 weaponShooting.addWeaponShooting(new FlakCannonShooting(bot, info,
197 shoot, weaponry));
198 weaponShooting.addWeaponShooting(new ShieldGunShooting(bot, info,
199 shoot, weaponry, projectiles, senses));
200 weaponShooting.addWeaponShooting(new BioRifleShooting(bot, info, shoot,
201 weaponry));
202 weaponShooting.addWeaponShooting(new AssaultRifleShooting(bot, info,
203 shoot, weaponry));
204 weaponShooting.addWeaponShooting(new RocketLauncherShooting(bot, info,
205 shoot, weaponry));
206 weaponShooting.addWeaponShooting(new LigthningGunShooting(bot, info,
207 shoot, weaponry));
208 weaponShooting.addWeaponShooting(new SniperRifleShooting(bot, info,
209 shoot, weaponry));
210 }
211
212
213
214
215
216
217
218
219
220
221
222 protected SharedFloydWarshallMap sfwMap;
223
224
225
226
227
228
229
230
231
232
233
234 @Override
235 protected void initializePathFinding(UT2004Bot bot) {
236 pathPlanner = new UT2004AStarPathPlanner(bot);
237 sfwMap = new SharedFloydWarshallMap(bot);
238 pathExecutor = new UT2004PathExecutor<ILocated>(
239 bot, info,move, new LoqueNavigator<ILocated>(bot, info,move,bot.getLog()),
240 bot.getLog());
241
242
243
244
245
246
247 pathExecutor.addStuckDetector(new UT2004TimeStuckDetector(bot, 3000,
248 100000));
249 pathExecutor.addStuckDetector(new UT2004PositionStuckDetector(bot));
250 pathExecutor.addStuckDetector(new UT2004DistanceStuckDetector(bot));
251
252 getBackToNavGraph = new UT2004GetBackToNavGraph(bot, info, move);
253 runStraight = new UT2004RunStraight(bot, info, move);
254 navigation = new UT2004Navigation(bot, pathExecutor, sfwMap,
255 getBackToNavGraph, runStraight);
256 }
257
258
259
260
261
262
263 @Override
264 public Initialize getInitializeCommand() {
265 assert parameters != null;
266
267
268 Initialize init = super.getInitializeCommand();
269 init.setDesiredSkill(parameters.getSkill());
270 init.setSkin(parameters.getSkin().getUnrealName());
271 init.setTeam(parameters.getTeam());
272 init.setShouldLeadTarget(parameters.shouldLeadTarget());
273
274 init.setLocation(parameters.getInitialLocation());
275 init.setRotation(parameters.getInitialRotation());
276
277 log.setLevel(this.parameters.getLogLevel());
278
279 return init;
280
281 }
282
283
284
285
286
287 @Override
288 public void finishControllerInitialization() {
289
290
291
292
293 focus.add(weaponShooting.getFocus());
294 focus.add(lookFocus);
295 navigation.setFocus(focus);
296
297
298 if (navBuilder.isUsed()) {
299 log.info("Navigation graph has been altered by 'navBuilder', triggering recomputation of Floyd-Warshall path matrix...");
300 Level oldLevel = sfwMap.getLog().getLevel();
301 sfwMap.getLog().setLevel(Level.FINER);
302 sfwMap.refreshPathMatrix();
303 sfwMap.getLog().setLevel(oldLevel);
304 }
305 }
306
307
308
309
310
311
312 @Override
313 public void beforeFirstLogic() {
314 targetSelector.add(new NearestEnemy().setContext(this));
315 lookSelector.add(new NearestEnemy().setContext(this));
316
317 weaponPrefs.addGeneralPref(UT2004ItemType.SHOCK_RIFLE, false);
318 weaponPrefs.addGeneralPref(UT2004ItemType.ROCKET_LAUNCHER, true);
319 weaponPrefs.addGeneralPref(UT2004ItemType.FLAK_CANNON, true);
320 weaponPrefs.addGeneralPref(UT2004ItemType.SNIPER_RIFLE, true);
321 weaponPrefs.addGeneralPref(UT2004ItemType.LIGHTNING_GUN, true);
322 weaponPrefs.addGeneralPref(UT2004ItemType.MINIGUN, true);
323 weaponPrefs.addGeneralPref(UT2004ItemType.LINK_GUN, true);
324 weaponPrefs.addGeneralPref(UT2004ItemType.BIO_RIFLE, false);
325 weaponPrefs.addGeneralPref(UT2004ItemType.ASSAULT_RIFLE, true);
326 weaponPrefs.addGeneralPref(UT2004ItemType.ASSAULT_RIFLE, false);
327 weaponPrefs.addGeneralPref(UT2004ItemType.SHIELD_GUN, false);
328 weaponPrefs.addGeneralPref(UT2004ItemType.SHIELD_GUN, true);
329
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 @Override
360 public void logic() {
361 super.logic();
362
363
364 for (Action action : actions.drain()) {
365 action.execute();
366 actionCount++;
367 }
368
369
370
371 ILocated shootSelected = null;
372 for (Selector<ILocated> selector : targetSelector) {
373 shootSelected = selector.select(players.getVisiblePlayers()
374 .values());
375 if (shootSelected != null) {
376 break;
377 }
378 }
379 weaponShooting.shoot(shootSelected);
380
381
382
383 ILocated lookSelected = null;
384 for (Selector<ILocated> selector : lookSelector) {
385 lookSelected = selector
386 .select(players.getVisiblePlayers().values());
387 if (lookSelected != null) {
388 break;
389 }
390 }
391 lookFocus.setFocus(lookSelected);
392
393
394 if (!navigation.isNavigating()) {
395
396 if (focus.getLocation() != null) {
397 move.turnTo(focus.getLocation());
398 }
399
400 else {
401 move.turnHorizontal(30);
402 }
403 }
404
405 logicIteration++;
406
407
408 try {
409 percepts.updatePercepts();
410 } catch (PerceiveException e) {
411 throw new PogamutException("Could not update percepts", e);
412 }
413
414 }
415
416
417
418
419
420
421
422
423
424 public void addAction(Action action) throws InterruptedException {
425 actions.put(action);
426 }
427
428
429
430
431
432
433 public Map<Method, Object> getAllPercepts() {
434 return percepts.getAllPercepts();
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 @AsAction(name = "navigate")
461 public void navigate(final UnrealIdOrLocation destination)
462 throws InterruptedException {
463 log.fine(String.format("called navigate to %s", destination));
464
465 addAction(new Navigate() {
466
467 @Override
468 public void execute() {
469
470 ILocated object = destination.toILocated(world, info);
471
472 if (object == null) {
473 log.warning(String
474 .format("failed to navigate to %s. The object associated with this Id was not located in the world. Halting.",
475 destination));
476 navigation.stopNavigation();
477 return;
478 }
479
480 log.fine(String.format("executed navigate to %s", destination));
481 navigation.navigate(object);
482 }
483 });
484
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541 @AsAction(name = "stop")
542 public void stop() throws InterruptedException {
543 log.fine("called stop");
544
545 addAction(new Stop() {
546 @Override
547 public void execute() {
548 log.info("executed stop");
549 navigation.stopNavigation();
550 }
551 });
552 }
553
554
555
556
557
558
559
560
561
562 @AsAction(name = "respawn")
563 public void respawn() throws InterruptedException {
564 log.fine("called respawn");
565 addAction(new Respawn() {
566 @Override
567 public void execute() {
568 log.info("executed respawn");
569 bot.respawn();
570 }
571 });
572 }
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602 @AsAction(name = "combo")
603 public void combo(final Combo combo) throws InterruptedException {
604 log.fine("called combo %s", combo);
605
606 addAction(new nl.tudelft.goal.ut2004.actions.Combo() {
607 @Override
608 public void execute() {
609
610 if (info.isAdrenalineSufficient()) {
611 log.info("executed combo %s", combo);
612 body.getAction().startCombo(combo.toString());
613 } else {
614 log.warning("combo %s failed, insufficient adrenaline",
615 combo);
616 }
617 }
618 });
619 }
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638 @AsAction(name = "dropWeapon")
639 public void dropWeapon() throws InterruptedException {
640 log.fine("called drop");
641
642 addAction(new DropWeapon() {
643 @Override
644 public void execute() {
645 Weapon weapon = weaponry.getCurrentWeapon();
646
647 body.getAction().throwWeapon();
648
649 if (weapon == null) {
650 log.warning(String
651 .format("Could not drop weapon. Not holding a weapon."));
652 } else if (weapon.getType() == UT2004ItemType.SHIELD_GUN
653 || weapon.getType() == UT2004ItemType.TRANSLOCATOR) {
654 log.warning(String.format("Could not drop weapon %s",
655 weapon));
656 } else {
657 log.info("executed drop %s", weapon);
658 }
659 }
660 });
661 }
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 @AsAction(name = "path")
697 public Percept path(UnrealIdOrLocation from, UnrealIdOrLocation to) {
698
699 ILocated toObject = to.toILocated(world, info);
700 ILocated fromObject = from.toILocated(world, info);
701
702 if (fromObject == null) {
703 throw new PogamutException(
704 String.format(
705 "Failed to compute path from %s to %s. The start was not located in the world.",
706 from, to), this);
707 }
708 if (toObject == null) {
709 throw new PogamutException(
710 String.format(
711 "Failed to compute path from %s to %s. The destination was not located in the world.",
712 from, to), this);
713 }
714
715
716 log.info(String.format("executed path from %s to %s", from, to));
717
718
719
720
721
722
723
724 Location fromLocation = fromObject.getLocation();
725 Location toLocation = toObject.getLocation();
726
727 NavPoint fromNav = DistanceUtils.getNearest(world
728 .getAll(NavPoint.class).values(), fromLocation);
729 NavPoint toNav = DistanceUtils.getNearest(world.getAll(NavPoint.class)
730 .values(), toLocation);
731
732 double distance = sfwMap.getDistance(fromNav, toNav);
733 List<NavPoint> navPoints = sfwMap.getPath(fromNav, toNav);
734 List<UnrealId> unrealIds = new ArrayList<UnrealId>(navPoints.size());
735 for (NavPoint n : navPoints) {
736 unrealIds.add(n.getId());
737 }
738 return new Percept(from, to, distance, unrealIds);
739 }
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771 @AsAction(name = "shoot")
772 public void shoot(final SelectorList targets) throws InterruptedException {
773 log.fine(String.format("called shoot %s", targets));
774
775 addAction(new Shoot() {
776
777 @Override
778 public void execute() {
779 log.info(String.format("executed shoot %s ", targetSelector));
780
781 targetSelector = targets.setContext(UT2004BotBehavior.this);
782 }
783 });
784
785 }
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 @AsAction(name = "stopShooting")
801 public void stopShooting() throws InterruptedException {
802 shoot(new SelectorList());
803 }
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841 @AsAction(name = "prefer")
842 public void prefer(final WeaponPrefList weaponList)
843 throws InterruptedException {
844 log.fine(String.format("called prefer %s", weaponList));
845
846 addAction(new Prefer() {
847
848 @Override
849 public void execute() {
850 weaponPrefs.clearAllPrefs();
851
852 for (WeaponPref pref : weaponList) {
853 weaponPrefs.addGeneralPref(pref.getWeapon(),
854 pref.isPrimary());
855 }
856
857 log.info(String.format("executed prefer %s", weaponList));
858 }
859 });
860 }
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892 @AsAction(name = "look")
893 public void look(final SelectorList targets) throws InterruptedException {
894 log.fine(String.format("called look %s", targets));
895
896 addAction(new Look() {
897
898 @Override
899 public void execute() {
900 log.info(String.format("executed look %s", targets));
901
902 lookSelector = targets.setContext(UT2004BotBehavior.this);
903 }
904 });
905 }
906
907
908
909
910
911
912
913
914
915
916
917
918 @AsAction(name = "skip")
919 public void skip() {
920
921 }
922
923
924
925
926
927
928
929
930 @AsAction(name = "chat")
931 public void chat(final Scope scope, final String message)
932 throws InterruptedException {
933 log.fine(String.format("called chat: %s", message));
934
935 addAction(new Chat() {
936 @Override
937 public void execute() {
938 log.info(String.format("executed chat: %s", message));
939 switch (scope) {
940 case GLOBAL:
941 body.getCommunication().sendGlobalTextMessage(message);
942 break;
943 case TEAM:
944 body.getCommunication().sendTeamTextMessage(message);
945 break;
946
947
948
949
950
951
952
953
954
955 }
956
957 }
958 });
959 }
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984 @AsPercept(name = "logic", filter = Type.ON_CHANGE)
985 @Deprecated
986 public Percept logicIteration() {
987 return new Percept(logicIteration);
988 }
989
990 @AsPercept(name = "actionCount", filter = Type.ON_CHANGE)
991 @Deprecated
992 public Percept actionCount() {
993 return new Percept(actionCount);
994 }
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 @AsPercept(name = "self", filter = Type.ON_CHANGE)
1015 public Percept self() {
1016 return new Percept(info.getId(), info.getName(), Team.valueOf(info
1017 .getTeam()));
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038 @AsPercept(name = "orientation", filter = Type.ON_CHANGE)
1039 public Percept orientation() {
1040 return new Percept(info.getLocation(), info.getRotation(),
1041 info.getVelocity());
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064 @AsPercept(name = "status", filter = Type.ON_CHANGE)
1065 public Percept status() {
1066 return new Percept(info.getHealth(), info.getArmor(),
1067 info.getAdrenaline(), Combo.parseCombo(info.getSelf()
1068 .getCombo()));
1069 }
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096 @AsPercept(name = "score", filter = Type.ON_CHANGE)
1097 public Percept score() {
1098 return new Percept(info.getKills(), info.getDeaths(),
1099 info.getSuicides());
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 @AsPercept(name = "currentWeapon", filter = Type.ON_CHANGE)
1124 public Percept currentWeapon() {
1125 final Weapon weapon = weaponry.getCurrentWeapon();
1126
1127 if (weapon == null) {
1128 return new Percept(new None(), FireMode.NONE);
1129 }
1130
1131 return new Percept(weapon.getType(), FireMode.valueOf(
1132 info.isPrimaryShooting(), info.isSecondaryShooting()));
1133 }
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 @AsPercept(name = "weapon", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1163 public Collection<Percept> weapon() {
1164 Collection<Weapon> weapons = weaponry.getWeapons().values();
1165 Collection<Percept> percepts = new ArrayList<Percept>(weapons.size());
1166
1167 for (Weapon w : weapons) {
1168 if (w.getType() == UT2004ItemType.SHIELD_GUN) {
1169
1170
1171
1172 percepts.add(new Percept(w.getType(), 1, w.getSecondaryAmmo()));
1173 } else {
1174 percepts.add(new Percept(w.getType(), w.getPrimaryAmmo(), w
1175 .getSecondaryAmmo()));
1176 }
1177 }
1178
1179 return percepts;
1180 }
1181
1182
1183
1184
1185 private List<Percept> fragged = new LinkedList<Percept>();
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 private void fraggedEvent(final long time, final UnrealId killer,
1196 final UnrealId victem, final String damageType) {
1197 fragged.add(new Percept(time, killer, victem, WeaponDamage
1198 .weaponForDamage(damageType)));
1199 }
1200
1201
1202
1203
1204
1205
1206 @EventListener(eventClass = BotKilled.class)
1207 public void msgBotKilled(BotKilled msg) {
1208 fraggedEvent(msg.getSimTime(), msg.getKiller(), info.getId(),
1209 msg.getDamageType());
1210 }
1211
1212
1213
1214
1215
1216
1217
1218 @EventListener(eventClass = PlayerKilled.class)
1219 public void msgPlayerKilled(PlayerKilled msg) {
1220 fraggedEvent(msg.getSimTime(), msg.getKiller(), msg.getId(),
1221 msg.getDamageType());
1222 }
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245 @AsPercept(name = "fragged", multiplePercepts = true, filter = Type.ALWAYS, event = true)
1246 public List<Percept> fragged() {
1247 ArrayList<Percept> percepts = new ArrayList<Percept>(fragged);
1248 fragged.clear();
1249 return percepts;
1250 }
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 @AsPercept(name = "navigation", filter = Type.ON_CHANGE)
1278 public Percept navigation() {
1279 ILocated currentTarget = navigation.getCurrentTarget();
1280
1281
1282 if (currentTarget == null) {
1283 return new Percept(navigation.getState().getFlag(), new None());
1284 }
1285
1286
1287 if (currentTarget instanceof IWorldObject) {
1288 IWorldObject targetObject = (IWorldObject) navigation
1289 .getCurrentTarget();
1290 return new Percept(navigation.getState().getFlag(),
1291 targetObject.getId());
1292 }
1293
1294
1295 return new Percept(navigation.getState().getFlag(),
1296 currentTarget.getLocation());
1297 }
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320 @AsPercept(name = "navPoint", multiplePercepts = true, filter = Type.ONCE)
1321 public Collection<Percept> navPoint() {
1322 Collection<NavPoint> navPoints = world.getAll(NavPoint.class).values();
1323 List<Percept> percepts = new ArrayList<Percept>(navPoints.size());
1324
1325 for (NavPoint p : navPoints) {
1326 percepts.add(new Percept(p.getId(), p.getLocation(), p
1327 .getOutgoingEdges().keySet()));
1328 }
1329
1330 return percepts;
1331 }
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366 @AsPercept(name = "pickup", multiplePercepts = true, filter = Type.ONCE)
1367 public Collection<Percept> pickup() {
1368 Collection<Item> pickups = items.getKnownPickups().values();
1369 Collection<Percept> percepts = new ArrayList<Percept>(pickups.size());
1370
1371 for (Item item : pickups) {
1372
1373
1374
1375 if (!item.isDropped() && item.getNavPoint() != null) {
1376 percepts.add(new Percept(item.getNavPointId(), item.getType()
1377 .getCategory(), item.getType()));
1378 }
1379 }
1380 return percepts;
1381 }
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399 @AsPercept(name = "pickup", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1400 public Collection<Percept> visiblePickup() {
1401 Collection<NavPoint> navPoints = world.getAll(NavPoint.class).values();
1402 List<Percept> percepts = new ArrayList<Percept>(navPoints.size());
1403
1404 for (NavPoint p : navPoints) {
1405 if (p.isVisible()) {
1406 percepts.add(new Percept(p.getId()));
1407 }
1408 }
1409
1410 return percepts;
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432 @AsPercept(name = "base", multiplePercepts = true, filter = Type.ONCE)
1433 public Collection<Percept> base() {
1434
1435 Collection<FlagInfo> flags = game.getAllCTFFlagsCollection();
1436 Collection<Percept> percepts = new ArrayList<Percept>(flags.size());
1437 Collection<NavPoint> navPoints = world.getAll(NavPoint.class).values();
1438
1439 for (FlagInfo flag : flags) {
1440 Team team = Team.valueOf(flag.getTeam());
1441 NavPoint nav = DistanceUtils.getNearest(navPoints,
1442 game.getFlagBase(team.id()));
1443 percepts.add(new Percept(team, nav.getId()));
1444 }
1445
1446 return percepts;
1447
1448 }
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473 @AsPercept(name = "game", filter = Type.ON_CHANGE)
1474 public Percept game() {
1475 return new Percept(game.getGameType(), game.getMapName(),
1476 game.getTeamScoreLimit(), game.getRemainingTime());
1477 }
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506 @AsPercept(name = "teamScore", filter = Type.ON_CHANGE)
1507 public Percept teamScore() {
1508 return new Percept(game.getTeamScore(info.getTeam()),
1509 game.getTeamScore(1 - info.getTeam()));
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539 @AsPercept(name = "flagState", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1540 public Collection<Percept> flagState() {
1541
1542 Collection<FlagInfo> flags = game.getAllCTFFlagsCollection();
1543 Collection<Percept> percepts = new ArrayList<Percept>(flags.size());
1544
1545 for (FlagInfo flag : flags) {
1546 percepts.add(new Percept(Team.valueOf(flag.getTeam()), FlagState
1547 .valueOfIgnoreCase(flag.getState())));
1548 }
1549
1550 return percepts;
1551
1552 }
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590 @AsPercept(name = "item", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1591 public Collection<Percept> item() {
1592 Collection<Item> visibleItems = items.getVisibleItems().values();
1593 Collection<Percept> percepts = new ArrayList<Percept>(
1594 visibleItems.size());
1595
1596 for (Item item : visibleItems) {
1597
1598
1599 if (item.isDropped() || item.getNavPointId() == null) {
1600 percepts.add(new Percept(item.getId(), item.getType()
1601 .getCategory(), item.getType(), item.getLocation()));
1602 } else {
1603 percepts.add(new Percept(item.getId(), item.getType()
1604 .getCategory(), item.getType(), item.getNavPointId()));
1605 }
1606 }
1607
1608 return percepts;
1609 }
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641 @AsPercept(name = "flag", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1642 public Collection<Percept> flag() {
1643
1644 Collection<FlagInfo> flags = game.getAllCTFFlagsCollection();
1645 Collection<Percept> percepts = new ArrayList<Percept>(flags.size());
1646
1647 for (FlagInfo flag : flags) {
1648 if (flag.isVisible())
1649 percepts.add(new Percept(Team.valueOf(flag.getTeam()), flag
1650 .getHolder(), flag.getLocation()));
1651 }
1652
1653 return percepts;
1654 }
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680 @AsPercept(name = "bot", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
1681 public Collection<Percept> bot() {
1682 Collection<Player> visible = players.getVisiblePlayers().values();
1683 Collection<Percept> wrapped = new ArrayList<Percept>(visible.size());
1684
1685 for (Player p : visible) {
1686 wrapped.add(new Percept(p.getId(), p.getName(), Team.valueOf(p
1687 .getTeam()), p.getLocation(), UT2004ItemType.getItemType(p
1688 .getWeapon()), FireMode.valueOf(p.getFiring())));
1689 }
1690
1691 return wrapped;
1692 }
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713 @AsPercept(name = "udamage", filter = Type.ON_CHANGE)
1714 public Percept udamage() {
1715 return new Percept(info.hasUDamage() ? info.getRemainingUDamageTime()
1716 : 0);
1717 }
1718 }