0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.Text;
|
|
4 using System.Xml;
|
|
5 using IBBoard.Lang;
|
|
6
|
|
7 namespace IBBoard.WarFoundry.API.Objects
|
|
8 {
|
|
9 /// <summary>
|
|
10 /// Summary description for UnitInstance.
|
|
11 /// </summary>
|
|
12 public class Unit : WarFoundryObject
|
|
13 {
|
|
14 private UnitType type;
|
|
15 private int size;
|
|
16 private Army army;
|
|
17 private Race race;
|
|
18 private double points;
|
|
19 private ArmyCategory cat;
|
|
20 private Hashtable equipment = new Hashtable();
|
|
21 public event DoubleValChangedDelegate PointsValueChanged;
|
|
22 public event IntValChangedDelegate UnitSizeChanged;
|
|
23 public event FloatValChangedDelegate UnitEquipmentAmountChanged;
|
|
24
|
|
25 public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){}
|
|
26
|
|
27 public Unit(UnitType unitType, int startSize, Army parentArmy)
|
|
28 {
|
|
29 Army = parentArmy;
|
|
30 type = unitType;
|
|
31 Size = startSize;
|
|
32 setInitialEquipment();
|
|
33 CalcCost();
|
|
34 }
|
|
35
|
|
36 protected override string DefaultName()
|
|
37 {
|
|
38 if (type != null)
|
|
39 {
|
|
40 if (size == 1)
|
|
41 {
|
|
42 return type.Name;
|
|
43 }
|
|
44 else
|
|
45 {
|
|
46 return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name);
|
|
47 }
|
|
48 }
|
|
49 else
|
|
50 {
|
|
51 return "Unknown Unit";
|
|
52 }
|
|
53 }
|
|
54
|
|
55 private void setInitialEquipment()
|
|
56 {
|
|
57 foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems())
|
|
58 {
|
|
59 if (unitEquip.IsRequired)
|
|
60 {
|
|
61 if (CanEquipWithItem(unitEquip.ID))
|
|
62 {
|
|
63 EquipmentItem equipItem = unitEquip.EquipmentItem;
|
|
64 equipment[unitEquip.ID] = equipItem.MinNumber;
|
|
65 }
|
|
66 }
|
|
67 }
|
|
68 }
|
|
69
|
|
70 private void CalcCost()
|
|
71 {
|
|
72 String oldName = HasDefaultName() ? Name : null;
|
|
73 double oldpoints = points;
|
|
74 points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;
|
|
75 UnitEquipmentItem unitEquipItem;
|
|
76 EquipmentItem equipItem;
|
|
77 float count;
|
|
78
|
|
79 foreach (string key in equipment.Keys)
|
|
80 {
|
|
81 unitEquipItem = UnitType.GetEquipmentItem(key);
|
|
82 equipItem = unitEquipItem.EquipmentItem;
|
|
83 count = (float)equipment[key];
|
|
84
|
|
85 if (equipItem.IsRatioLimit)
|
|
86 {
|
|
87 if (unitEquipItem.RoundNumberUp)
|
|
88 {
|
|
89 points+= Math.Ceiling(size * count) * equipItem.Cost;
|
|
90 }
|
|
91 else
|
|
92 {
|
|
93 points+= Math.Floor(size * count) * equipItem.Cost;
|
|
94 }
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 if (count == -1)
|
|
99 {
|
|
100 points+= size * equipItem.Cost;
|
|
101 }
|
|
102 else
|
|
103 {
|
|
104 points+= count * equipItem.Cost;
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108
|
|
109 if (oldpoints!=points)
|
|
110 {
|
|
111 OnPointsValueChanged(oldpoints, points);
|
|
112 }
|
|
113
|
|
114 if (oldName!=null)
|
|
115 {
|
|
116 OnNameChanged(oldName, Name);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 public int AdditionalTroopers
|
|
121 {
|
|
122 get { return Math.Max(Size - type.BaseSize, 0); }
|
|
123 }
|
|
124
|
|
125 public int Size
|
|
126 {
|
|
127 get { return size; }
|
|
128 set
|
|
129 {
|
|
130 if (value!=size)
|
|
131 {
|
|
132 int oldValue = size;
|
|
133 size = (value>0 ? value : 1);
|
|
134 CalcCost();
|
|
135 OnUnitSizeChanged(oldValue, size);
|
|
136 }
|
|
137 }
|
|
138 }
|
|
139
|
|
140 public UnitType UnitType
|
|
141 {
|
|
142 get { return type; }
|
|
143 }
|
|
144
|
|
145 public Army Army
|
|
146 {
|
|
147 get { return army; }
|
|
148 set
|
|
149 {
|
|
150 army = value;
|
|
151
|
|
152 if (army == null)
|
|
153 {
|
|
154 Category = null;
|
|
155 }
|
|
156 }
|
|
157 }
|
|
158
|
|
159 public Race Race
|
|
160 {
|
|
161 get { return race; }
|
|
162 set { race = value; }
|
|
163 }
|
|
164
|
|
165 public ArmyCategory Category
|
|
166 {
|
|
167 get
|
|
168 {
|
|
169 if (cat==null)
|
|
170 {
|
|
171 if (Army!=null)
|
|
172 {
|
|
173 return Army.GetCategory(UnitType.MainCategory);
|
|
174 }
|
|
175 else
|
|
176 {
|
|
177 return null;
|
|
178 }
|
|
179 }
|
|
180 else
|
|
181 {
|
|
182 return cat;
|
|
183 }
|
|
184 }
|
|
185 set { cat = value; }
|
|
186 }
|
|
187
|
|
188 public double PointsValue
|
|
189 {
|
|
190 get
|
|
191 {
|
|
192 if (points == 0)
|
|
193 {
|
|
194 CalcCost();
|
|
195 }
|
|
196
|
|
197 return points;
|
|
198 }
|
|
199 }
|
|
200
|
|
201 public UnitEquipmentItem[] GetAllowedOptionalEquipment()
|
|
202 {
|
|
203 ArrayList list = new ArrayList();
|
|
204
|
|
205 foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems())
|
|
206 {
|
|
207 if (!item.IsRequired)
|
|
208 {
|
|
209 list.Add(item);
|
|
210 }
|
|
211 }
|
|
212
|
|
213 return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem));
|
|
214 }
|
|
215
|
|
216 public UnitEquipmentItem[] GetEquipment()
|
|
217 {
|
|
218 UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count];
|
|
219 int i = 0;
|
|
220
|
|
221 foreach (String itemID in equipment.Keys)
|
|
222 {
|
|
223 items[i++] = UnitType.GetEquipmentItem(itemID);
|
|
224 }
|
|
225
|
|
226 return items;
|
|
227 }
|
|
228
|
|
229 public UnitEquipmentItem[] GetRequiredEquipment()
|
|
230 {
|
|
231 ArrayList list = new ArrayList();
|
|
232 UnitEquipmentItem item;
|
|
233
|
|
234 foreach(String itemID in equipment.Keys)
|
|
235 {
|
|
236 item = UnitType.GetEquipmentItem(itemID);
|
|
237
|
|
238 if (item.IsRequired)
|
|
239 {
|
|
240 list.Add(item);
|
|
241 }
|
|
242 }
|
|
243
|
|
244 return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem));
|
|
245 }
|
|
246
|
|
247 public float GetEquipmentAmount(UnitEquipmentItem item)
|
|
248 {
|
|
249 return GetEquipmentAmount(item.EquipmentItem.ID);
|
|
250 }
|
|
251
|
|
252 public float GetEquipmentAmount(string equipID)
|
|
253 {
|
|
254 if (equipment.ContainsKey(equipID))
|
|
255 {
|
|
256 return (float)equipment[equipID];
|
|
257 }
|
|
258 else
|
|
259 {
|
|
260 return 0;
|
|
261 }
|
|
262 }
|
|
263
|
|
264 public void SetEquipmentAmount(string equipID, float amount)
|
|
265 {
|
|
266 UnitEquipmentItem equip = UnitType.GetEquipmentItem(equipID);
|
|
267
|
|
268 if (equip == null)
|
|
269 {
|
|
270 throw new InvalidOperationException("No such equipment ID "+equipID+" for unit "+ID);
|
|
271 }
|
|
272
|
|
273 if (equip.EquipmentItem.IsRatioLimit)
|
|
274 {
|
|
275 if (amount > 1)
|
|
276 {
|
|
277 amount = 1;
|
|
278 }
|
|
279 else if (amount < 0)
|
|
280 {
|
|
281 amount = 0;
|
|
282 }
|
|
283 //else what was passed in was okay
|
|
284 }
|
|
285 else
|
|
286 {
|
|
287 if (amount >=1 || amount == -1)
|
|
288 {
|
|
289 amount = (float)Math.Round(amount);
|
|
290 }
|
|
291 else
|
|
292 {
|
|
293 amount = 0;
|
|
294 }
|
|
295 }
|
|
296
|
|
297 float oldAmount = 0;
|
|
298
|
|
299 if (equipment.ContainsKey(equipID))
|
|
300 {
|
|
301 oldAmount = (float)equipment[equipID];
|
|
302 }
|
|
303
|
|
304 if (amount!=oldAmount)
|
|
305 {
|
|
306 if (amount > 0 || amount == -1)
|
|
307 {
|
|
308 equipment[equipID] = amount;
|
|
309 }
|
|
310 else
|
|
311 {
|
|
312 equipment.Remove(equipID);
|
|
313 }
|
|
314
|
|
315 OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
|
|
316 CalcCost();
|
|
317 }
|
|
318 }
|
|
319
|
|
320 public bool CanEquipWithItem(string equipID)
|
|
321 {
|
|
322 string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup;
|
|
323
|
|
324 if (mutex == "")
|
|
325 {
|
|
326 return true;
|
|
327 }
|
|
328
|
|
329 foreach (string itemID in equipment.Keys)
|
|
330 {
|
|
331 if (UnitType.GetEquipmentItem(itemID).MutexGroup == mutex)
|
|
332 {
|
|
333 return false;
|
|
334 }
|
|
335 }
|
|
336
|
|
337 return true;
|
|
338 }
|
|
339
|
|
340 private void OnPointsValueChanged(double oldValue, double newValue)
|
|
341 {
|
|
342 if (PointsValueChanged!=null)
|
|
343 {
|
|
344 PointsValueChanged(this, oldValue, newValue);
|
|
345 }
|
|
346 }
|
|
347
|
|
348 private void OnUnitSizeChanged(int oldValue, int newValue)
|
|
349 {
|
|
350 if (UnitSizeChanged!=null)
|
|
351 {
|
|
352 UnitSizeChanged(this, oldValue, newValue);
|
|
353 }
|
|
354 }
|
|
355
|
|
356 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, float oldValue, float newValue)
|
|
357 {
|
|
358 if (UnitEquipmentAmountChanged!=null)
|
|
359 {
|
|
360 UnitEquipmentAmountChanged(equip, oldValue, newValue);
|
|
361 }
|
|
362 }
|
|
363
|
|
364 public Stats UnitStats
|
|
365 {
|
|
366 get { return UnitType.UnitStats; }
|
|
367 }
|
|
368
|
|
369 /*public override string ToXmlString()
|
|
370 {
|
|
371 StringBuilder sb = new StringBuilder();
|
|
372 float amount;
|
|
373
|
|
374 foreach(string key in equipment.Keys)
|
|
375 {
|
|
376 amount = (float)equipment[key];
|
|
377
|
|
378 if (amount > 0 || amount == -1)
|
|
379 {
|
|
380 sb.Append("<equipItem id=\""+key+"\" amount=\""+amount+"\" />");
|
|
381 }
|
|
382 }
|
|
383
|
|
384 string equipmentString;
|
|
385
|
|
386 if (sb.Length > 0)
|
|
387 {
|
|
388 equipmentString = "<equipment>"+sb.ToString()+"</equipment>";
|
|
389 }
|
|
390 else
|
|
391 {
|
|
392 equipmentString = "";
|
|
393 }
|
|
394
|
|
395 if (equipmentString == "")
|
|
396 {
|
|
397 return "<unit id=\""+ID+"\" unitType=\""+UnitType.ID+"\" unitName=\""+name+"\" size=\""+Size+"\" />";
|
|
398 }
|
|
399 else
|
|
400 {
|
|
401 return "<unit id=\""+ID+"\" unitType=\""+UnitType.ID+"\" unitName=\""+name+"\" size=\""+Size+"\">"+equipmentString+"</unit>";
|
|
402 }
|
|
403 }*/
|
|
404 }
|
|
405 }
|