1 package cz.cuni.amis.pogamut.emohawk.agent.module.sensomotoric;
2
3 import java.util.HashMap;
4
5 import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
6 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
7 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
8 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
9 import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
10 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
11 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
12 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SetEmoticon;
13 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
14 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
15
16
17
18
19
20 public class Emoticons extends SensomotoricModule<UT2004Bot> {
21
22
23
24
25 private Self self = null;
26
27
28 private static double DEFAULT_DURATION = 5;
29
30
31 private static EmoticonBubbleType DEFAULT_BUBBLE = EmoticonBubbleType.BUBBLE_THOUGHT_CENTER;
32
33 HashMap<UnrealId, EmoticonInfo> playersEmoticons = new HashMap();
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public EmoticonType[] getCurrentEmoticonTypes() {
49 return getCurrentEmoticon().getEmoticonTypes();
50 }
51
52
53
54
55
56 public Emoticon getCurrentEmoticon() {
57 if (self == null) return new Emoticon(EmoticonType.NONE, EmoticonBubbleType.NONE);
58 return new Emoticon(EmoticonType.get(self.getEmotLeft()), EmoticonType.get(self.getEmotCenter()), EmoticonType.get(self.getEmotRight()), EmoticonBubbleType.get(self.getBubble()));
59 }
60
61
62
63
64
65
66 public Emoticon getPlayerEmoticon(Player player) {
67 return new Emoticon(EmoticonType.get(player.getEmotLeft()), EmoticonType.get(player.getEmotCenter()), EmoticonType.get(player.getEmotRight()), EmoticonBubbleType.get(player.getBubble()));
68 }
69
70
71
72
73
74
75
76
77 public EmoticonInfo getPlayerEmoticonInfo(UnrealId playerId) {
78 if (playersEmoticons.containsKey(playerId))
79 return playersEmoticons.get(playerId);
80 else
81 return new EmoticonInfo(new Emoticon(EmoticonType.NONE, EmoticonType.NONE, EmoticonType.NONE, EmoticonBubbleType.NONE), 0);
82 }
83
84
85
86
87
88 public void setEmoticon(Emoticon emoticon) {
89 if (emoticon.getEmoticonCount() == 3)
90 setTripleEmoticon(emoticon.getLeftEmoticon(), emoticon.getCenterEmoticon(), emoticon.getRightEmoticon(), DEFAULT_DURATION, emoticon.getBubble());
91 else if (emoticon.getEmoticonCount() == 2)
92 setDoubleEmoticon(emoticon.getLeftEmoticon(), emoticon.getRightEmoticon(), DEFAULT_DURATION, emoticon.getBubble());
93 else
94 setCenterEmoticonType(emoticon.getCenterEmoticon(), DEFAULT_DURATION, emoticon.getBubble());
95 }
96
97
98
99
100
101
102 public void setEmoticon(Emoticon emoticon, double duration) {
103 if (emoticon.getEmoticonCount() == 3)
104 setTripleEmoticon(emoticon.getLeftEmoticon(), emoticon.getCenterEmoticon(), emoticon.getRightEmoticon(), duration, emoticon.getBubble());
105 else if (emoticon.getEmoticonCount() == 2)
106 setDoubleEmoticon(emoticon.getLeftEmoticon(), emoticon.getRightEmoticon(), duration, emoticon.getBubble());
107 else
108 setCenterEmoticonType(emoticon.getCenterEmoticon(), duration, emoticon.getBubble());
109 }
110
111
112
113
114
115 public EmoticonType getCurrentLeftEmoticonType() {
116 if (self == null) return EmoticonType.NONE;
117
118
119
120
121 return EmoticonType.NONE;
122 }
123
124
125
126
127
128 public EmoticonType getCurrentCenterEmoticonType() {
129 if (self == null) return EmoticonType.NONE;
130
131 if (EmoticonType.has(self.getEmotCenter()))
132 return EmoticonType.get(self.getEmotCenter());
133 return EmoticonType.NONE;
134 }
135
136
137
138
139
140 public EmoticonType getCurrentRightEmoticonType() {
141 if (self == null) return EmoticonType.NONE;
142 if (EmoticonType.has(self.getEmotRight()))
143 return EmoticonType.get(self.getEmotRight());
144
145 return EmoticonType.NONE;
146 }
147
148
149
150
151
152 public EmoticonBubbleType getCurrentBubbleType() {
153 if (self == null) return EmoticonBubbleType.NONE;
154 if (EmoticonBubbleType.has(self.getBubble())) {
155 return EmoticonBubbleType.get(self.getBubble());
156 }
157 return EmoticonBubbleType.NONE;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public void setEmoticons(double duration, EmoticonBubbleType bubble, EmoticonType... emoticons) {
178 clearEmoticons();
179 if (emoticons == null || emoticons.length == 0) return;
180 switch(emoticons.length) {
181 case 1:
182 setCenterEmoticonType(emoticons[0], duration, bubble);
183 return;
184 case 2:
185 setDoubleEmoticon(emoticons[0], emoticons[1], duration, bubble);
186 return;
187 case 3:
188 setTripleEmoticon(emoticons[0], emoticons[1], emoticons[3], duration, bubble);
189 return;
190 default:
191 log.warning("Unsupported number of emoticons to show (" + emoticons.length + "), cannot show more than 3, discarding the rest.");
192 setTripleEmoticon(emoticons[0], emoticons[1], emoticons[3], duration, bubble);
193 return;
194 }
195 }
196
197
198
199
200 public void clearEmoticons() {
201 setTripleEmoticon(EmoticonType.NONE, EmoticonType.NONE, EmoticonType.NONE, 0, EmoticonBubbleType.NONE);
202 }
203
204
205
206
207
208
209
210
211 @Deprecated
212 public void setCenterEmoticonType(EmoticonType type) {
213 setCenterEmoticonType(type, DEFAULT_DURATION, DEFAULT_BUBBLE);
214 }
215
216
217
218
219
220
221
222
223
224 @Deprecated
225 public void setCenterEmoticonType(EmoticonType type, double duration) {
226 setCenterEmoticonType(type, duration, DEFAULT_BUBBLE);
227 }
228
229
230
231
232
233
234
235
236
237
238 @Deprecated
239 public void setCenterEmoticonType(EmoticonType centerEmoticon, double duration, EmoticonBubbleType bubble) {
240 this.act.act(new SetEmoticon(centerEmoticon.id, 1, null, 0, null, 0, bubble.id, 1, duration));
241 }
242
243
244
245
246
247
248
249
250
251 @Deprecated
252 public void setDoubleEmoticon(EmoticonType left, EmoticonType right) {
253 setDoubleEmoticon(left, right, DEFAULT_DURATION);
254 }
255
256
257
258
259
260
261
262
263
264
265 @Deprecated
266 public void setDoubleEmoticon(EmoticonType left, EmoticonType right, double duration) {
267 setDoubleEmoticon(left, right, duration, DEFAULT_BUBBLE);
268 }
269
270
271
272
273
274
275
276
277
278
279
280 @Deprecated
281 public void setDoubleEmoticon(EmoticonType left, EmoticonType right, double duration, EmoticonBubbleType bubble) {
282 this.act.act(new SetEmoticon(null, 0, left.id, 1, right.id, 1, bubble.id, 1, duration));
283 }
284
285
286
287
288
289
290
291
292
293
294 @Deprecated
295 public void setTripleEmoticon(EmoticonType left, EmoticonType center, EmoticonType right) {
296 setTripleEmoticon(left, center, right, DEFAULT_DURATION);
297 }
298
299
300
301
302
303
304
305
306
307
308
309 @Deprecated
310 public void setTripleEmoticon(EmoticonType left, EmoticonType center, EmoticonType right, double duration) {
311 setTripleEmoticon(left, center, right, duration, DEFAULT_BUBBLE);
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 @Deprecated
326 public void setTripleEmoticon(EmoticonType left, EmoticonType center, EmoticonType right, double duration, EmoticonBubbleType bubble) {
327 this.act.act(new SetEmoticon(center.id, 1, left.id, 1, right.id, 1, bubble.id, 1, duration));
328 }
329
330
331
332
333 IWorldObjectListener<Self> storeSelfListener = new IWorldObjectListener<Self>() {
334 @Override
335 public void notify(IWorldObjectEvent<Self> event) {
336 if (self == null) {
337 self = event.getObject();
338 worldView.removeObjectListener(Self.class, this);
339 storeSelfListener = null;
340 }
341 }
342 };
343
344
345
346
347
348 IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playersListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
349 @Override
350 public void notify(WorldObjectUpdatedEvent<Player> event) {
351 Player upPl = event.getObject();
352 Emoticon actualEmot = getPlayerEmoticon(upPl);
353 if (playersEmoticons.containsKey(upPl.getId())) {
354 Emoticon previousEmot = playersEmoticons.get(upPl.getId()).getEmoticon();
355 if (!previousEmot.equals(actualEmot) ) {
356 playersEmoticons.put(upPl.getId(), new EmoticonInfo(actualEmot, event.getSimTime()));
357
358 worldView.notify(new NewEmoticonEvent(upPl.getId(), actualEmot, event.getSimTime()));
359 }
360 } else {
361 playersEmoticons.put(upPl.getId(), new EmoticonInfo(actualEmot, event.getSimTime()));
362
363 worldView.notify(new NewEmoticonEvent(upPl.getId(), actualEmot, event.getSimTime()));
364 }
365 }
366 };
367
368
369
370
371
372 public Emoticons(UT2004Bot agent) {
373 super(agent);
374 agent.getWorldView().addObjectListener(Self.class, storeSelfListener);
375 agent.getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playersListener);
376 }
377
378 }