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.base.communication.command.IAct;
20  import cz.cuni.amis.pogamut.base.utils.Pogamut;
21  import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
22  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
23  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController;
24  import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
25  import cz.cuniz.amis.pogamut.ut3.utils.UT3BotRunner;
26  import eis.eis2java.handlers.ActionHandler;
27  import eis.eis2java.handlers.AllPerceptPerceptHandler;
28  import eis.eis2java.handlers.DefaultActionHandler;
29  import eis.eis2java.handlers.PerceptHandler;
30  import eis.eis2java.translation.Translator;
31  import eis.eis2java.util.AllPerceptsProvider;
32  import eis.exceptions.PerceiveException;
33  import eis.iilang.Percept;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.logging.Level;
38  
39  import nl.tudelft.goal.unreal.messages.BotParameters;
40  import nl.tudelft.goal.unreal.messages.Configuration;
41  import nl.tudelft.goal.unreal.translators.LocationTranslator;
42  import nl.tudelft.goal.unreal.translators.NoneTranslator;
43  import nl.tudelft.goal.unreal.translators.PerceptTranslator;
44  import nl.tudelft.goal.unreal.translators.RotationTranslator;
45  import nl.tudelft.goal.unreal.translators.TeamTranslator;
46  import nl.tudelft.goal.unreal.translators.UnrealIdTranslator;
47  import nl.tudelft.goal.unreal.translators.VelocityTranslator;
48  import nl.tudelft.goal.ut2004.util.Team;
49  import nl.tudelft.goal.ut3.agent.UT3BotBehavior;
50  import nl.tudelft.goal.ut3.translators.CategoryTranslator;
51  import nl.tudelft.goal.ut3.translators.FireModeTranslator;
52  import nl.tudelft.goal.ut3.translators.FlagStateTranslator;
53  import nl.tudelft.goal.ut3.translators.GameTypeTranslator;
54  import nl.tudelft.goal.ut3.translators.ItemTypeTranslator;
55  import nl.tudelft.goal.ut3.translators.NavigationStateTranslator;
56  import nl.tudelft.goal.ut3.translators.SelectorListTranslator;
57  import nl.tudelft.goal.ut3.translators.SelectorTranslator;
58  import nl.tudelft.goal.ut3.translators.UT3ItemTypeTranslator;
59  import nl.tudelft.goal.unreal.translators.UnrealIdOrLocationTranslator;
60  import nl.tudelft.goal.ut3.translators.WeaponPrefListTranslator;
61  import nl.tudelft.goal.ut3.translators.WeaponPrefTranslator;
62  
63  import org.junit.AfterClass;
64  import org.junit.BeforeClass;
65  import org.junit.Test;
66  
67  /**
68   * Test-suite to provide a full environment test.
69   * 
70   * @author Evers
71   */
72  @SuppressWarnings("rawtypes")
73  public class AbstractEnvironmentTests {
74  
75  	public static final String BOT_NAME = "TESTBOT";
76  	public static final String MAP_NAME = "CTF-GOALTest";
77  	public static final Team BOT_TEAM = Team.RED;
78  	
79  	protected static UT3BotRunner<UT2004Bot, UT2004BotParameters> runner;
80  	protected static List<UT2004Bot> agents;
81  	protected static List<PerceptHandler> perceptHandlers;
82  	protected List<ActionHandler> actionHandlers;
83  
84  	protected static UT3BotBehavior testBot;
85  
86  	protected static List<Percept> startupPercepts;
87  
88  	/**
89  	 * Register all the required translators for the GOAL environment.
90  	 */
91  	private static void registerTranslators() {
92  		Translator translator = Translator.getInstance();
93  
94  
95  		/*
96  		 * To translate from Parameter2Java we are given an UnrealId. However we
97  		 * can not access the agents memory during translation. To work around
98  		 * this we store everything we have send to any agent. Hence the same
99  		 * object has to be used for both directions.
100 		 */
101 		
102 		/*
103 		 * Translators provided by the BaseUnrealEnvironment.
104 		 * 
105 		 * Please list these in lexical order.
106 		 */
107 		LocationTranslator locationTranslator = new LocationTranslator();
108 		translator.registerJava2ParameterTranslator(locationTranslator);
109 		translator.registerParameter2JavaTranslator(locationTranslator);
110 		
111 		NoneTranslator noneTranslator = new NoneTranslator();
112 		translator.registerJava2ParameterTranslator(noneTranslator);
113 
114 		PerceptTranslator perceptTranslator = new PerceptTranslator();
115 		translator.registerJava2ParameterTranslator(perceptTranslator);
116 
117 		RotationTranslator rotationTranslator = new RotationTranslator();
118 		translator.registerJava2ParameterTranslator(rotationTranslator);
119 		translator.registerParameter2JavaTranslator(rotationTranslator);
120 
121 		TeamTranslator teamTranslator = new TeamTranslator();
122 		translator.registerJava2ParameterTranslator(teamTranslator);
123 		translator.registerParameter2JavaTranslator(teamTranslator);
124 
125 		UnrealIdTranslator unrealIdTranslator = new UnrealIdTranslator();
126 		translator.registerJava2ParameterTranslator(unrealIdTranslator);
127 		translator.registerParameter2JavaTranslator(unrealIdTranslator);
128 
129 		VelocityTranslator velocityTranslator = new VelocityTranslator();
130 		translator.registerJava2ParameterTranslator(velocityTranslator);
131 		translator.registerParameter2JavaTranslator(velocityTranslator);
132 
133 		/*
134 		 * Translators provided by the UT3 environment.
135 		 * 
136 		 * Please list these in lexical order.
137 		 */
138 		CategoryTranslator ut3ItemTypeTranslator = new CategoryTranslator();
139 		translator.registerJava2ParameterTranslator(ut3ItemTypeTranslator);
140 
141 		UT3ItemTypeTranslator ut3CategoryTranslator = new UT3ItemTypeTranslator();
142 		translator.registerJava2ParameterTranslator(ut3CategoryTranslator);
143 		translator.registerParameter2JavaTranslator(ut3CategoryTranslator);
144 
145 		CategoryTranslator categoryTranslator = new CategoryTranslator();
146 		translator.registerJava2ParameterTranslator(categoryTranslator);
147 		translator.registerParameter2JavaTranslator(categoryTranslator);
148 
149 		ItemTypeTranslator itemTypeTranslator = new ItemTypeTranslator();
150 		translator.registerJava2ParameterTranslator(itemTypeTranslator);
151 
152 		FireModeTranslator fireModeTranslator = new FireModeTranslator();
153 		translator.registerJava2ParameterTranslator(fireModeTranslator);
154 		translator.registerParameter2JavaTranslator(fireModeTranslator);
155 
156 		FlagStateTranslator flagStateTranslator = new FlagStateTranslator();
157 		translator.registerJava2ParameterTranslator(flagStateTranslator);
158 
159 		GameTypeTranslator gameTypeTranslator = new GameTypeTranslator();
160 		translator.registerJava2ParameterTranslator(gameTypeTranslator);
161 
162 		NavigationStateTranslator navigationStateTranslator = new NavigationStateTranslator();
163 		translator.registerJava2ParameterTranslator(navigationStateTranslator);
164 
165 		SelectorListTranslator selectorListTranslator = new SelectorListTranslator();
166 		translator.registerParameter2JavaTranslator(selectorListTranslator);
167 
168 		SelectorTranslator selectorTranslator = new SelectorTranslator();
169 		translator.registerParameter2JavaTranslator(selectorTranslator);
170 
171 		UnrealIdOrLocationTranslator unrealIdOrLocationTranslator = new UnrealIdOrLocationTranslator();
172 		translator.registerParameter2JavaTranslator(unrealIdOrLocationTranslator);
173 
174 		WeaponPrefListTranslator weaponPrefListTranslator = new WeaponPrefListTranslator();
175 		translator.registerParameter2JavaTranslator(weaponPrefListTranslator);
176 
177 		WeaponPrefTranslator weaponPrefTranslator = new WeaponPrefTranslator();
178 		translator.registerParameter2JavaTranslator(weaponPrefTranslator);
179 	}
180 
181 	@BeforeClass
182 	public static void setUpClass() throws Exception {
183 		// Setup environment
184 		// 1. Register translators
185 		registerTranslators();
186 
187 		// 2. Configure parameters
188 		BotParameters parameters = new BotParameters();
189 
190 		parameters.setAgentId(BOT_NAME);
191 		parameters.setTeam(BOT_TEAM);
192 		parameters.setLogLevel(Level.INFO);
193 
194 		// 3. Start environment                
195                 runner = new UT3BotRunner<UT2004Bot, UT2004BotParameters>(
196                         UT3BotBehavior.class, Configuration.DEFAULT_BOT_NAME, Configuration.LOCAL_HOST, Configuration.BOT_SERVER_PORT);
197 		runner.setConsoleLogging(true);
198 		agents = runner.startAgents(parameters);
199 
200 		// 4. Register percept handlers
201 		perceptHandlers = new ArrayList<PerceptHandler>(agents.size());
202 
203 		for (UT2004Bot agent : agents) {
204 			PerceptHandler handler = new AllPerceptPerceptHandler(
205 					(AllPerceptsProvider) agent.getController());
206 			perceptHandlers.add(handler);
207 		}
208 
209 		// 5. Register action handlers
210 		List<ActionHandler> actionHandlers = new ArrayList<ActionHandler>(
211 				agents.size());
212 
213 		for (UT2004Bot agent : agents) {
214 			ActionHandler handler = new DefaultActionHandler(
215 					agent.getController());
216 			actionHandlers.add(handler);
217 		}
218 
219 		// 6. Set testbot to the UT3 bot controller
220 		testBot = ((UT3BotBehavior) agents.get(0).getController());
221 
222 		/*
223 		 * if(!testBot.getGame().getMapName().equals(MAP_NAME)) { throw new
224 		 * Exception("Error, tests only supported on: " + MAP_NAME); }
225 		 */
226 		// 7. Catch all startup percepts to provide send-once tests
227 		Thread.sleep(1000);
228 		startupPercepts = new ArrayList<Percept>();
229 		for (PerceptHandler handler : perceptHandlers) {
230 			startupPercepts.addAll(handler.getAllPercepts());
231 		}
232 	}
233 
234 	public static List<Percept> getPercepts() throws PerceiveException {
235 		List<Percept> perceptList = new ArrayList<Percept>();
236 		for (PerceptHandler handler : perceptHandlers) {
237 			perceptList.addAll(handler.getAllPercepts());
238 		}
239 		return perceptList;
240 	}
241 }