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