1 package cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.UUID;
6 import java.util.Map.Entry;
7 import java.util.concurrent.Future;
8 import java.util.concurrent.TimeUnit;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12 import javax.vecmath.Vector3d;
13
14 import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
15 import cz.cuni.amis.pogamut.base.communication.exception.CommunicationException;
16 import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdateResult;
17 import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdatedEvent;
18 import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdateResult.Result;
19 import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdateResult.WorldObjectUpdateResult;
20 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
21 import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectFuture;
22 import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
23 import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
24 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
25 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddRay;
26 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.RemoveRay;
27 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AutoTraceRay;
28 import cz.cuni.amis.utils.flag.Flag;
29
30
31
32
33
34
35
36
37
38 public class Raycasting extends SensomotoricModule<UT2004Bot> {
39
40 Map<String, WorldObjectFuture<AutoTraceRay>> rayFutures = new HashMap<String, WorldObjectFuture<AutoTraceRay>>();
41 String idSuffix = null;
42 int counter = 0;
43 int alreadyInitialized = 0;
44 Flag<Boolean> allRaysInitialized = new Flag<Boolean>(false);
45 boolean listening = false;
46
47 public Flag<Boolean> getAllRaysInitialized() {
48 return allRaysInitialized.getImmutable();
49 }
50
51 public Raycasting(UT2004Bot bot) {
52 this(bot, null);
53 }
54
55 public Raycasting(UT2004Bot bot, Logger log) {
56 super(bot, log);
57 idSuffix = "_" + bot.getName() + UUID.randomUUID().toString();
58
59 cleanUp();
60 }
61
62 @Override
63 protected void cleanUp() {
64 super.cleanUp();
65 for (Entry<String, WorldObjectFuture<AutoTraceRay>> entry : rayFutures.entrySet()) {
66 final AutoTraceRay ray = entry.getValue().get(0, TimeUnit.MILLISECONDS);
67 if (ray != null) {
68
69 agent.getWorldView().notifyImmediately(
70 new IWorldObjectUpdatedEvent() {
71 @Override
72 public WorldObjectId getId() {
73 return ray.getId();
74 }
75 @Override
76 public IWorldObjectUpdateResult<IWorldObject> update(IWorldObject obj) {
77 return new WorldObjectUpdateResult<IWorldObject>(Result.DESTROYED, obj);
78 }
79 @Override
80 public long getSimTime() {
81 return ray.getSimTime();
82 }
83 }
84 );
85 }
86 entry.getValue().cancel(false);
87 }
88 rayFutures.clear();
89 allRaysInitialized.setFlag(false);
90 alreadyInitialized = 0;
91 listening = false;
92 }
93
94
95
96
97
98 public void clear() throws CommunicationException {
99 act.act(new RemoveRay("All"));
100 cleanUp();
101 }
102
103
104
105
106
107 public void endRayInitSequence() {
108 listening = true;
109 checkIfAllInited();
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public Future<AutoTraceRay> createRay(
141 String Id, Vector3d Direction, int Length, boolean FastTrace, boolean FloorCorrection, boolean TraceActors) throws CommunicationException {
142 AddRay addRay = new AddRay(Id, Direction, Length, FastTrace, FloorCorrection, TraceActors);
143
144
145 WorldObjectFuture future = new WorldObjectFuture<AutoTraceRay>(worldView, Id, AutoTraceRay.class) {
146
147 @Override
148 protected void customObjectEncounteredHook(AutoTraceRay obj) {
149 alreadyInitialized++;
150 checkIfAllInited();
151 }
152 };
153 rayFutures.put(Id, future);
154
155
156 act.act(addRay);
157 return future;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public Future<AutoTraceRay> createRay(
189 Vector3d Direction, int Length, boolean FastTrace, boolean FloorCorrection, boolean TraceActors) throws CommunicationException {
190 String id = counter++ + idSuffix;
191 return createRay(id, Direction, Length, FastTrace, FloorCorrection, TraceActors);
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public AutoTraceRay getRay(String rayID) {
205 try {
206 Future<AutoTraceRay> ray = rayFutures.get(rayID);
207 if (ray == null) return null;
208 if (ray.isDone()) return ray.get();
209 else return null;
210 } catch (Exception ex) {
211 if (log.isLoggable(Level.SEVERE)) log.severe(ex.getMessage());
212 return null;
213 }
214 }
215
216
217
218
219 protected void checkIfAllInited() {
220 if (listening && rayFutures.size() == alreadyInitialized) {
221 allRaysInitialized.setFlag(true);
222 }
223 }
224
225
226 }
227