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