View Javadoc

1   /*
2    * Copyright (C) 2013 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package nl.tudelft.goal.ut3.environment;
18  
19  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
20  import cz.cuni.amis.pogamut.ut3.communication.messages.UT3ItemType;
21  import eis.iilang.Function;
22  import eis.iilang.Identifier;
23  import eis.iilang.Numeral;
24  import eis.iilang.Percept;
25  import nl.tudelft.goal.unreal.messages.UnrealIdOrLocation;
26  import static nl.tudelft.goal.ut3.environment.AbstractEnvironmentTests.testBot;
27  import nl.tudelft.goal.ut3.messages.FireMode;
28  import nl.tudelft.goal.ut3.messages.SelectorList;
29  import nl.tudelft.goal.ut3.selector.WorldObject;
30  import org.junit.Test;
31  import static org.junit.Assert.*;
32  
33  /**
34   * Test for all the UT3 self percepts.
35   * 
36   * @author Evers
37   */
38  public class SelfPerceptTests extends AbstractEnvironmentTests {
39  
40          /**
41           * Check the self-percept.
42           */
43          @Test
44          public void selfTest() {
45                  for(Percept p : startupPercepts) {
46                          if(p.getName().equals("self")) {
47                                  assertTrue(p.getParameters().size() == 3);
48                                  // traverse all parameters
49                                 assertEquals(new Identifier(BOT_NAME), p.getParameters().get(1));
50                                 assertEquals(new Identifier(BOT_TEAM.name().toLowerCase()), p.getParameters().get(2));
51                          }
52                  }
53          }
54          
55          /**
56           * Check the orientation percept of the bot while not moving.
57           *  The percepts are wrong if the location equals position 0.0,0.0,0.0.
58           */
59          @Test
60          public void orientationStandStillTest() {
61                   for(Percept p : startupPercepts) {
62                          if(p.getName().equals("orientation")) {
63                                  // traverse all parameters
64                                  assertTrue(p.getParameters().size() == 3);
65                                  Function noneLocation = new Function("location", new Numeral(0.0), new Numeral(0.0), new Numeral(0.0));
66                                  assertFalse(p.getParameters().get(0).equals(noneLocation));
67                          }
68                  }                                               
69          }
70          
71          // TODO(Optional): Create dynamic test for velocity, let the bot run to the flag and measure a velocity > 0
72          
73          /**
74           * Check the status percept.
75           */
76          @Test
77          public void statusTest() {
78                    for(Percept p : startupPercepts) {
79                          if(p.getName().equals("status")) {
80                                  // The OLD UT2004 environment got 4 parameters (Health, Armor, Adrenaline and Combo)
81                                  //  but the new environment does not have Combo and adrenaline so only Health and Armor are sent and adrenaline = 0 and combo = null.
82                                  assertTrue(p.getParameters().size() == 4);
83                                  assertEquals(new Numeral(100), p.getParameters().get(0));
84                                  assertEquals(new Numeral(0), p.getParameters().get(1));
85                          }
86                  }                 
87          }
88          
89          /**
90           * Check the score percept.
91           */
92          @Test
93          public void scoreTest() {
94                  
95                  // Score must be 0,0,0
96                  for(Percept p : startupPercepts) {
97                          if(p.getName().equals("score")) {
98                                  assertTrue(p.getParameters().size() == 3);
99                                  assertEquals(new Numeral(0), p.getParameters().get(0));
100                                 assertEquals(new Numeral(0), p.getParameters().get(1));
101                                 assertEquals(new Numeral(0), p.getParameters().get(2));
102                         }
103                 }   
104         }
105         
106         /**
107          * Check the currentWeapon percept.
108          */
109         @Test
110         public void currentWeaponTest() {
111                 for(Percept p : startupPercepts) {
112                         if(p.getName().equals("currentWeapon")) {
113                                 assertTrue(p.getParameters().size() == 2);
114                                 assertEquals(new Identifier("enforcer"), p.getParameters().get(0));
115                                 assertEquals(new Identifier(FireMode.NONE.name().toLowerCase()), p.getParameters().get(1));
116                         }
117                 }
118         }
119         
120         /**
121          * Check the weapon percept.
122          */
123         @Test
124         public void weaponTest() {
125                 boolean enforcer = false, impact_hammer = false;
126                 
127                 for(Percept p : startupPercepts) {
128                         if(p.getName().equals("weapon")) {
129                                 assertTrue(p.getParameters().size() == 3);      // WeaponType, Ammo, AltAmmo
130                                 
131                                 // Enforcer
132                                 if(p.getParameters().get(0).equals(new Identifier("enforcer"))) {
133                                         enforcer = true;
134                                         assertEquals(new Numeral(50), p.getParameters().get(1));
135                                         assertEquals(new Numeral(50), p.getParameters().get(2));
136                                 }
137                                 // Impact hammer, no ammo
138                                 else if(p.getParameters().get(0).equals(new Identifier("impact_hammer"))) {
139                                         impact_hammer = true;
140                                         assertEquals(new Numeral(0), p.getParameters().get(1));
141                                         assertEquals(new Numeral(0), p.getParameters().get(2));                                        
142                                 }
143                                 else {
144                                         // We got some different weapons, not particulary wrong but strange
145                                         
146                                 }
147                         }
148                 }      
149                 assertTrue(enforcer);
150                 assertTrue(impact_hammer);
151         }
152         
153         /**
154          * Test ammo and health while shooting itself.
155          * @throws InterruptedException 
156          */
157         @Test
158         public void testSuicideAttempt() throws InterruptedException
159         {
160                 testBot.getWeaponPrefs().clearAllPrefs();
161                 testBot.getWeaponPrefs().addGeneralPref(UT3ItemType.ROCKET_LAUNCHER, true);
162                 testBot.navigate(new UnrealIdOrLocation(UnrealId.get("UTWeaponLocker_Content_0")));
163                 Thread.sleep(3000);
164                 
165                 SelectorList selectorList = new SelectorList();
166                 selectorList.add(new WorldObject(UnrealId.get("UTWeaponLocker_Content_0")));
167                 testBot.shoot(selectorList);      // near suicide attempt
168                 
169                 Thread.sleep(3000);
170                 assertTrue(testBot.getInfo().getHealth() < 100);
171                 assertEquals(17, testBot.getWeaponry().getAmmo(UT3ItemType.ROCKET_LAUNCHER));
172         }
173 }