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