1 package nl.tudelft.pogamut.ut2004.agent.module.shooting;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import nl.tudelft.pogamut.ut2004.agent.module.shooting.util.FocusProvider;
7 import nl.tudelft.pogamut.ut2004.agent.module.shooting.weapon.GenericWeaponShooting;
8 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
9 import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
10 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
11 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
12 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weapon;
13 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weaponry;
14 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.AgentInfo;
15 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPref;
16 import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.WeaponPrefs;
17 import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004Navigation;
18 import cz.cuni.amis.pogamut.ut2004.bot.command.ImprovedShooting;
19 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
20 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
21 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
22 import cz.cuni.amis.utils.SafeEquals;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @SuppressWarnings("rawtypes")
55 public class WeaponryShooting extends SensorModule<UT2004Bot> {
56
57
58
59
60 private class EndMessageListener implements IWorldEventListener<EndMessage> {
61
62
63
64
65
66
67 public EndMessageListener(IWorldView worldView) {
68 worldView.addEventListener(EndMessage.class, this);
69 }
70
71 @Override
72 public void notify(EndMessage event) {
73 shoot();
74 }
75 }
76
77
78
79
80 protected ILocated currentTarget = null;
81
82
83
84
85 protected WeaponPref currentWeaponPref = null;
86
87
88
89 protected WeaponShooting currentWeaponShooting = null;
90
91 @SuppressWarnings("unused")
92 private EndMessageListener endMessageListener;
93
94
95
96
97 protected FocusProvider focus = new FocusProvider();
98
99
100
101 protected AgentInfo info;
102
103
104
105
106 protected ILocated lastTarget = null;
107
108
109
110
111
112 protected WeaponPref lastWeaponPref = null;
113
114
115
116
117 protected WeaponShooting lastWeaponShooting = null;
118
119
120
121
122 protected ImprovedShooting shoot;
123
124
125
126 protected WeaponPrefs weaponPrefs;
127
128
129
130
131 protected Weaponry weaponry;
132
133
134
135 protected Map<ItemType, WeaponShooting> weaponShootings = new HashMap<ItemType, WeaponShooting>();
136
137
138
139
140
141
142
143
144
145
146 public WeaponryShooting(UT2004Bot bot, AgentInfo info, Weaponry weaponry, WeaponPrefs weaponPrefs,
147 ImprovedShooting shoot) {
148 super(bot);
149 this.info = info;
150 this.weaponry = weaponry;
151 this.weaponPrefs = weaponPrefs;
152 this.shoot = shoot;
153
154 endMessageListener = new EndMessageListener(worldView);
155
156 }
157
158
159
160
161
162
163
164
165 public WeaponShooting addWeaponShooting(WeaponShooting weaponShooting) {
166 return weaponShootings.put(weaponShooting.getWeaponType(), weaponShooting);
167 }
168
169
170
171
172
173
174
175
176
177 public WeaponShooting removeWeaponShooting(ItemType weapon) {
178 return weaponShootings.remove(weapon);
179 }
180
181
182
183
184
185 public ILocated getFocus() {
186 return focus;
187 }
188
189
190
191
192 public ILocated getLastTarget() {
193 return lastTarget;
194
195 }
196
197
198
199
200 public ILocated getTarget() {
201 return currentTarget;
202 }
203
204
205
206
207 public WeaponPref getWeaponPref() {
208 return currentWeaponPref;
209 }
210
211
212
213
214 public WeaponShooting getWeaponShooting() {
215 return currentWeaponShooting;
216 }
217
218
219
220
221
222
223
224
225
226 private WeaponShooting selectWeaponShooting(Weapon weapon) {
227 if (weapon == null) {
228 return null;
229 }
230
231 if (!weaponShootings.containsKey(weapon.getType())) {
232 GenericWeaponShooting genericWeaponShooting = new GenericWeaponShooting(agent, info, shoot, weaponry,
233 weapon.getType());
234 weaponShootings.put(weapon.getType(), genericWeaponShooting);
235 return genericWeaponShooting;
236 }
237
238 return weaponShootings.get(weapon.getType());
239 }
240
241
242
243
244 private void shoot() {
245 lastWeaponShooting = currentWeaponShooting;
246
247 WeaponPref suggested = weaponPrefs.getWeaponPreference(currentTarget);
248
249 if (shoot.mayChangeWeapon()) {
250 lastWeaponPref = currentWeaponPref;
251 currentWeaponPref = suggested;
252 }
253
254
255
256
257 if (currentWeaponPref != null) {
258 currentWeaponShooting = selectWeaponShooting(weaponry.getWeapon(currentWeaponPref.getWeapon()));
259 } else {
260 currentWeaponShooting = selectWeaponShooting(weaponry.getCurrentWeapon());
261 currentWeaponPref = new WeaponPref(weaponry.getCurrentWeapon().getType());
262 }
263
264
265 if (lastWeaponShooting != null && lastWeaponShooting != currentWeaponShooting) {
266 lastWeaponShooting.stopShoot();
267 focus.clearFocus();
268 }
269
270 if (currentWeaponShooting == null) {
271 return;
272 }
273
274 if (lastWeaponShooting != currentWeaponShooting) {
275 currentWeaponShooting.shoot(currentWeaponPref, currentTarget);
276 focus.setFocus(currentWeaponShooting.getFocus());
277 }
278
279 else if (!currentWeaponPref.equals(lastWeaponPref) || !SafeEquals.equals(lastTarget, currentTarget)) {
280 currentWeaponShooting.shoot(currentWeaponPref, currentTarget);
281 }
282
283 }
284
285
286
287
288
289
290
291 public void shoot(ILocated target) {
292 if (this.currentTarget != target) {
293 this.lastTarget = this.currentTarget;
294 this.currentTarget = target;
295 }
296 }
297
298
299
300
301
302 public void stopShoot() {
303 if (this.currentTarget != null) {
304 this.lastTarget = this.currentTarget;
305 this.currentTarget = null;
306 }
307 this.currentWeaponShooting.stopShoot();
308 }
309 }