1 package cz.cuni.amis.pogamut.ut2004.agent.module.sensor;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
9 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weapon;
10 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weaponry;
11 import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
12 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
13 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
14
15
16
17
18
19
20
21
22
23
24
25 public class WeaponPrefs {
26
27 protected List<WeaponPrefsRange> prefs = new ArrayList<WeaponPrefsRange>();
28 protected Weaponry weaponry;
29 protected UT2004Bot bot;
30 protected WeaponPrefsRange generalPrefs;
31 protected WeaponPrefs onlyGeneral;
32
33 public WeaponPrefs(Weaponry weaponry, UT2004Bot bot) {
34 this.weaponry = weaponry;
35 this.bot = bot;
36 this.generalPrefs = new WeaponPrefsRange(this, 0);
37 this.onlyGeneral = new WeaponPrefs(weaponry, bot, new WeaponPrefsRange(this, 0)) {
38 @Override
39 public WeaponPrefsRange newPrefsRange(double minDistance) {
40 throw new IllegalStateException("Can't invoke the method on 'generalOnly' preferences!");
41 }
42 @Override
43 public WeaponPrefs addGeneralPref(ItemType weapon, boolean usePrimaryMode) {
44 throw new IllegalStateException("Can't invoke the method on 'generalOnly' preferences!");
45 }
46 @Override
47 public WeaponPrefs addGeneralPref(Weapon weapon, boolean usePrimaryMode) {
48 throw new IllegalStateException("Can't invoke the method on 'generalOnly' preferences!");
49 }
50 };
51 }
52
53 protected WeaponPrefs(Weaponry weaponry, UT2004Bot bot, WeaponPrefsRange generalPrefs) {
54 this.weaponry = weaponry;
55 this.bot = bot;
56 this.generalPrefs = new WeaponPrefsRange(this, generalPrefs);
57 this.onlyGeneral = this;
58 }
59
60
61
62
63
64
65
66 public WeaponPrefs asGeneralOnly() {
67 return this.onlyGeneral;
68 }
69
70
71
72
73 public void clearAllPrefs() {
74 prefs.clear();
75 generalPrefs.clear();
76 onlyGeneral.generalPrefs.clear();
77 }
78
79
80
81
82
83
84
85
86 public WeaponPrefs addGeneralPref(ItemType weapon, boolean usePrimaryMode) {
87 generalPrefs.add(weapon, usePrimaryMode);
88 onlyGeneral.generalPrefs.add(weapon, usePrimaryMode);
89 return this;
90 }
91
92
93
94
95
96
97
98
99 public WeaponPrefs addGeneralPref(Weapon weapon, boolean usePrimaryMode) {
100 generalPrefs.add(weapon, usePrimaryMode);
101 onlyGeneral.generalPrefs.add(weapon, usePrimaryMode);
102 return this;
103 }
104
105
106
107
108
109
110
111
112 public WeaponPrefsRange newPrefsRange(double maxDistance) {
113 WeaponPrefsRange newPrefs = new WeaponPrefsRange(this, maxDistance);
114 this.prefs.add(newPrefs);
115 Collections.sort(this.prefs, new Comparator<WeaponPrefsRange>() {
116 @Override
117 public int compare(WeaponPrefsRange o1, WeaponPrefsRange o2) {
118 double diff = o1.getMaxDistance() - o2.getMaxDistance();
119 if (diff > 0) return 1;
120 if (diff < 0) return -1;
121 return 0;
122 }
123 });
124 return newPrefs;
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public WeaponPrefsRange getWeaponPreferences(double distance) {
138 if (distance < 0) {
139 return generalPrefs;
140 }
141 if (prefs.size() == 0) return generalPrefs;
142 int i = 0;
143 for (WeaponPrefsRange pref : prefs) {
144 double minDistance = pref.getMinDistance();
145 if (minDistance > distance) {
146 if (i == 0) return null;
147 return prefs.get(i-1);
148 }
149 ++i;
150 }
151 return prefs.get(prefs.size()-1);
152 }
153
154
155
156
157
158
159 protected WeaponPrefsRange getPreviousRange(WeaponPrefsRange weaponPrefsRange) {
160 if (weaponPrefsRange == generalPrefs) return null;
161 int index = prefs.indexOf(weaponPrefsRange);
162 if (index < 1) return null;
163 return prefs.get(index-1);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public WeaponPref getWeaponPreference(double distance, ItemType... forbiddenWeapons) {
181 WeaponPref pref = null;
182 if (distance >= 0 && prefs.size() != 0) {
183 WeaponPrefsRange range = getWeaponPreferences(distance);
184 pref = range.getWeaponPreference(forbiddenWeapons);
185 if (pref != null) {
186 return pref;
187 }
188 }
189 pref = generalPrefs.getWeaponPreference(forbiddenWeapons);
190 if (pref != null) return pref;
191 pref = generalPrefs.getWeaponPreference();
192 if (pref != null) return pref;
193 if (weaponry.getCurrentWeapon() != null) {
194 return new WeaponPref(weaponry.getCurrentWeapon().getType(), true);
195 }
196 return null;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 public WeaponPref getWeaponPreference(ILocated target, ItemType... forbiddenWeapons) {
211 if (target == null) {
212 return getWeaponPreference(-1, forbiddenWeapons);
213 } else {
214 return getWeaponPreference(bot.getLocation().getDistance(target.getLocation()), forbiddenWeapons);
215 }
216 }
217
218
219
220
221
222
223 public WeaponPref getWeaponPreference(ItemType... forbiddenWeapons) {
224 return getWeaponPreference(-1, forbiddenWeapons);
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 public WeaponPref getWeaponPreference(double distance, WeaponPref... forbiddenWeapons) {
242 WeaponPref pref = null;
243 if (distance >= 0 && prefs.size() != 0) {
244 WeaponPrefsRange range = getWeaponPreferences(distance);
245 pref = range.getWeaponPreference(forbiddenWeapons);
246 if (pref != null) {
247 return pref;
248 }
249 }
250 pref = generalPrefs.getWeaponPreference(forbiddenWeapons);
251 if (pref != null) return pref;
252 pref = generalPrefs.getWeaponPreference();
253 if (pref != null) return pref;
254 if (weaponry.getCurrentWeapon() != null) {
255 return new WeaponPref(weaponry.getCurrentWeapon().getType(), true);
256 }
257 return null;
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271 public WeaponPref getWeaponPreference(ILocated target, WeaponPref... forbiddenWeapons) {
272 if (target == null) {
273 return getWeaponPreference(-1, forbiddenWeapons);
274 } else {
275 return getWeaponPreference(bot.getLocation().getDistance(target.getLocation()), forbiddenWeapons);
276 }
277 }
278
279
280
281
282
283
284 public WeaponPref getWeaponPreference(WeaponPref... forbiddenWeapons) {
285 return getWeaponPreference(-1, forbiddenWeapons);
286 }
287
288
289
290
291
292
293
294
295
296
297
298 public WeaponPref getWeaponPreference(double distance) {
299 return getWeaponPreference(distance, (ItemType[])null);
300 }
301
302
303
304
305
306
307
308
309
310
311
312 public WeaponPref getWeaponPreference(ILocated target) {
313 return getWeaponPreference(target, (ItemType[])null);
314 }
315
316
317
318
319
320 public WeaponPref getWeaponPreference() {
321 return getWeaponPreference(-1, (ItemType[])null);
322 }
323
324 }