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.ut3.environment;
22
23 import java.net.URI;
24 import java.rmi.RemoteException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import nl.tudelft.goal.unreal.environment.AbstractUnrealEnvironment;
29 import nl.tudelft.goal.unreal.messages.BotParameters;
30 import nl.tudelft.goal.unreal.messages.Configuration;
31 import nl.tudelft.goal.unreal.translators.LocationTranslator;
32 import nl.tudelft.goal.unreal.translators.NoneTranslator;
33 import nl.tudelft.goal.unreal.translators.PerceptTranslator;
34 import nl.tudelft.goal.unreal.translators.RotationTranslator;
35 import nl.tudelft.goal.unreal.translators.TeamTranslator;
36 import nl.tudelft.goal.unreal.translators.UnrealIdTranslator;
37 import nl.tudelft.goal.unreal.translators.VelocityTranslator;
38 import nl.tudelft.goal.ut3.translators.GroupTranslator;
39 import nl.tudelft.goal.ut3.translators.ItemTypeTranslator;
40 import nl.tudelft.goal.ut2004.visualizer.connection.AddBotCommand;
41 import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentServiceListener;
42 import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentServiceMediator;
43 import nl.tudelft.goal.ut3.agent.UT3BotBehavior;
44 import nl.tudelft.goal.ut3.server.EnvironmentControllerServer;
45 import nl.tudelft.goal.ut3.server.EnvironmentControllerServerModule;
46 import nl.tudelft.goal.ut3.translators.CategoryTranslator;
47 import nl.tudelft.goal.ut3.translators.FireModeTranslator;
48 import nl.tudelft.goal.ut3.translators.FlagStateTranslator;
49 import nl.tudelft.goal.ut3.translators.GameTypeTranslator;
50 import nl.tudelft.goal.ut3.translators.NavigationStateTranslator;
51 import nl.tudelft.goal.ut3.translators.SelectorListTranslator;
52 import nl.tudelft.goal.ut3.translators.SelectorTranslator;
53 import nl.tudelft.goal.ut3.translators.UT3GroupTranslator;
54 import nl.tudelft.goal.ut3.translators.UT3ItemTypeTranslator;
55 import nl.tudelft.goal.unreal.translators.UnrealIdOrLocationTranslator;
56 import nl.tudelft.goal.ut3.translators.WeaponPrefListTranslator;
57 import nl.tudelft.goal.ut3.translators.WeaponPrefTranslator;
58 import nl.tudelft.goal.ut3.visualizer.connection.client.RemoteVisualizer;
59 import nl.tudelft.goal.ut3.visualizer.connection.client.VisualizerServiceDefinition;
60 import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
61 import cz.cuni.amis.pogamut.base.communication.command.IAct;
62 import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
63 import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
64 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
65 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController;
66 import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
67 import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ServerFactory;
68 import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ServerModule;
69 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
70 import cz.cuni.amis.pogamut.ut2004.utils.UT2004BotRunner;
71 import cz.cuni.amis.pogamut.ut2004.utils.UT2004ServerRunner;
72 import cz.cuni.amis.pogamut.ut2004.utils.UTBotRunner;
73 import cz.cuni.amis.utils.flag.FlagListener;
74 import cz.cuniz.amis.pogamut.ut3.utils.UT3BotRunner;
75 import eis.eis2java.handlers.ActionHandler;
76 import eis.eis2java.handlers.AllPerceptPerceptHandler;
77 import eis.eis2java.handlers.DefaultActionHandler;
78 import eis.eis2java.handlers.PerceptHandler;
79 import eis.eis2java.translation.Translator;
80 import eis.eis2java.util.AllPerceptsProvider;
81 import eis.exceptions.EntityException;
82 import eis.exceptions.ManagementException;
83 import eis.iilang.Identifier;
84 import eis.iilang.Parameter;
85 import eis.iilang.ParameterList;
86
87 public class UT3Environment extends AbstractUnrealEnvironment {
88
89
90
91
92 private static final long serialVersionUID = 8240549393243585632L;
93
94
95 private ReconnectingServerDefinition<RemoteVisualizer> visualizerConnection;
96
97 @Override
98 protected void registerTranslators() {
99
100 Translator translator = Translator.getInstance();
101
102
103
104
105
106
107
108
109
110
111
112
113
114 LocationTranslator locationTranslator = new LocationTranslator();
115 translator.registerJava2ParameterTranslator(locationTranslator);
116 translator.registerParameter2JavaTranslator(locationTranslator);
117
118 NoneTranslator noneTranslator = new NoneTranslator();
119 translator.registerJava2ParameterTranslator(noneTranslator);
120
121 PerceptTranslator perceptTranslator = new PerceptTranslator();
122 translator.registerJava2ParameterTranslator(perceptTranslator);
123
124 RotationTranslator rotationTranslator = new RotationTranslator();
125 translator.registerJava2ParameterTranslator(rotationTranslator);
126 translator.registerParameter2JavaTranslator(rotationTranslator);
127
128 TeamTranslator teamTranslator = new TeamTranslator();
129 translator.registerJava2ParameterTranslator(teamTranslator);
130 translator.registerParameter2JavaTranslator(teamTranslator);
131
132 UnrealIdTranslator unrealIdTranslator = new UnrealIdTranslator();
133 translator.registerJava2ParameterTranslator(unrealIdTranslator);
134 translator.registerParameter2JavaTranslator(unrealIdTranslator);
135
136 UnrealIdOrLocationTranslator unrealIdOrLocationTranslator = new UnrealIdOrLocationTranslator();
137 translator.registerParameter2JavaTranslator(unrealIdOrLocationTranslator);
138 translator.registerJava2ParameterTranslator(unrealIdOrLocationTranslator);
139
140 VelocityTranslator velocityTranslator = new VelocityTranslator();
141 translator.registerJava2ParameterTranslator(velocityTranslator);
142 translator.registerParameter2JavaTranslator(velocityTranslator);
143
144
145
146
147
148
149 CategoryTranslator categoryTranslator = new CategoryTranslator();
150 translator.registerJava2ParameterTranslator(categoryTranslator);
151 translator.registerParameter2JavaTranslator(categoryTranslator);
152
153 FireModeTranslator fireModeTranslator = new FireModeTranslator();
154 translator.registerJava2ParameterTranslator(fireModeTranslator);
155 translator.registerParameter2JavaTranslator(fireModeTranslator);
156
157 FlagStateTranslator flagStateTranslator = new FlagStateTranslator();
158 translator.registerJava2ParameterTranslator(flagStateTranslator);
159
160 GameTypeTranslator gameTypeTranslator = new GameTypeTranslator();
161 translator.registerJava2ParameterTranslator(gameTypeTranslator);
162
163 GroupTranslator groupTranslator = new GroupTranslator();
164 translator.registerJava2ParameterTranslator(groupTranslator);
165 translator.registerParameter2JavaTranslator(groupTranslator);
166
167 ItemTypeTranslator itemTypeTranslator = new ItemTypeTranslator();
168 translator.registerJava2ParameterTranslator(itemTypeTranslator);
169 translator.registerParameter2JavaTranslator(itemTypeTranslator);
170
171 NavigationStateTranslator navigationStateTranslator = new NavigationStateTranslator();
172 translator.registerJava2ParameterTranslator(navigationStateTranslator);
173
174 SelectorListTranslator selectorListTranslator = new SelectorListTranslator();
175 translator.registerParameter2JavaTranslator(selectorListTranslator);
176
177 SelectorTranslator selectorTranslator = new SelectorTranslator();
178 translator.registerParameter2JavaTranslator(selectorTranslator);
179
180 UT3GroupTranslator ut3GroupTranslator = new UT3GroupTranslator();
181 translator.registerJava2ParameterTranslator(ut3GroupTranslator);
182 translator.registerParameter2JavaTranslator(ut3GroupTranslator);
183
184 UT3ItemTypeTranslator ut3ItemTypeTranslator = new UT3ItemTypeTranslator();
185 translator.registerJava2ParameterTranslator(ut3ItemTypeTranslator);
186 translator.registerParameter2JavaTranslator(ut3ItemTypeTranslator);
187
188 WeaponPrefListTranslator weaponPrefListTranslator = new WeaponPrefListTranslator();
189 translator.registerParameter2JavaTranslator(weaponPrefListTranslator);
190
191 WeaponPrefTranslator weaponPrefTranslator = new WeaponPrefTranslator();
192 translator.registerParameter2JavaTranslator(weaponPrefTranslator);
193 }
194
195 public static void main(String[] args) throws ManagementException {
196 HashMap<String, Parameter> map = new HashMap<String, Parameter>();
197 map.put("botNames", new ParameterList(new Identifier("Test")));
198
199 new UT3Environment().init(map);
200
201 }
202
203 @Override
204 protected Class<UT3BotBehavior> getControlerClass() {
205 return UT3BotBehavior.class;
206 }
207
208 protected UTBotRunner<UT2004Bot<IVisionWorldView, IAct, UT2004BotController>, UT2004BotParameters> getBotRunner(
209 Configuration configuration) {
210 UT3BotRunner<UT2004Bot<IVisionWorldView, IAct, UT2004BotController>, UT2004BotParameters> runner = new UT3BotRunner<UT2004Bot<IVisionWorldView, IAct, UT2004BotController>, UT2004BotParameters>(
211 getControlerClass(), configuration.getDefaultBotName(),
212 configuration.getBotServerHost(),
213 configuration.getBotServerPort());
214 return runner;
215 }
216
217 @Override
218 protected PerceptHandler createPerceptHandler(
219 @SuppressWarnings("rawtypes") UT2004BotController controller)
220 throws EntityException {
221 if (!(controller instanceof AllPerceptsProvider))
222 throw new EntityException("Expected a class that implements "
223 + AllPerceptsProvider.class.getSimpleName());
224 return new AllPerceptPerceptHandler((AllPerceptsProvider) controller);
225 }
226
227 @Override
228 protected ActionHandler createActionHandler(
229 @SuppressWarnings("rawtypes") UT2004BotController controller)
230 throws EntityException {
231 return new DefaultActionHandler(controller);
232 }
233
234 @Override
235 protected synchronized void initializeEnvironment(
236 Map<String, Parameter> parameters) throws ManagementException {
237 super.initializeEnvironment(parameters);
238
239
240 try {
241 visualizerConnection = new ReconnectingServerDefinition<RemoteVisualizer>(
242 new VisualizerServiceDefinition());
243 visualizerConnection.getServerFlag().addListener(
244 new VisualizerServiceListener());
245 } catch (RemoteException e) {
246 log.severe("Could not start connection to Visualizer: " + e);
247 }
248
249 }
250
251 @Override
252 protected UT2004ServerRunner<? extends IUT2004Server, ? extends UT2004AgentParameters> createServerRunner() {
253 UT2004ServerModule<UT2004AgentParameters> serverModule = new EnvironmentControllerServerModule<UT2004AgentParameters>();
254 UT2004ServerFactory<EnvironmentControllerServer, UT2004AgentParameters> serverFactory = new UT2004ServerFactory<EnvironmentControllerServer, UT2004AgentParameters>(
255 serverModule);
256 UT2004ServerRunner<EnvironmentControllerServer, UT2004AgentParameters> serverRunner = new UT2004ServerRunner<EnvironmentControllerServer, UT2004AgentParameters>(
257 serverFactory, "UTServer",
258 configuration.getControlServerHost(),
259 configuration.getControlServerPort());
260 return serverRunner;
261 }
262
263
264
265
266
267
268
269
270 private class VisualizerServiceListener implements
271 EnvironmentServiceListener, FlagListener<RemoteVisualizer> {
272
273 private final EnvironmentServiceMediator mediator;
274
275 public VisualizerServiceListener() throws RemoteException {
276 mediator = new EnvironmentServiceMediator(getComponentId());
277 mediator.setListener(this);
278 }
279
280 @Override
281 public void flagChanged(RemoteVisualizer visualizer) {
282 if (visualizer != null) {
283 visualizer.setEnvironment(mediator);
284 }
285 }
286
287
288
289
290
291
292
293
294 @Override
295 public void addBot(AddBotCommand command) throws ManagementException {
296
297 BotParameters parameters = new BotParameters();
298
299
300 if (command.getBotName() != null) {
301 parameters.setAgentId(command.getBotName());
302 }
303 if (command.getLogLevel() != null) {
304 parameters.setLogLevel(command.getLogLevel());
305 }
306 if (command.getShouldLeadTarget() != null) {
307 parameters.setShouldLeadTarget(command.getShouldLeadTarget());
308 }
309 if (command.getSkill() != null) {
310 parameters.setSkill(command.getSkill());
311 }
312 if (command.getSkin() != null) {
313 parameters.setSkin(command.getSkin());
314 }
315 if (command.getTeam() != null) {
316 parameters.setTeam(command.getTeam());
317 }
318
319 if (command.getRotation() != null) {
320 parameters.setInitialRotation(command.getRotation());
321 }
322
323 if (command.getLocation() != null) {
324 parameters.setInitialLocation(command.getLocation());
325 }
326
327
328
329
330
331 startAgent(parameters);
332
333 }
334
335 }
336
337 @Override
338 protected synchronized void connectEnvironment() throws ManagementException {
339 super.connectEnvironment();
340
341
342 URI visualizerUri = configuration.getVisualizer();
343 if (visualizerUri != null) {
344 log.info("Connecting to visualizer server at " + visualizerUri
345 + " .");
346
347 visualizerConnection.setUri(visualizerUri);
348 } else {
349 log.info("No address for the visualizer server was provided. The environment will not try to connect to the visualizer.");
350 }
351 }
352
353 @Override
354 protected synchronized void killEnvironment() {
355 super.killEnvironment();
356
357 visualizerConnection.stopServer();
358
359 }
360
361 }