view API/Objects/Army.cs @ 366:4993e6f7c509

* Remove reference to unused namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 May 2011 15:40:05 +0000
parents 65fccdd51aec
children 1a70ca80ef41
line source
1 // This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
5 using System;
6 using System.IO;
7 using System.Collections.Generic;
8 using System.Text;
9 using System.Xml;
10 using IBBoard.WarFoundry.API;
11 using IBBoard.WarFoundry.API.Factories;
12 using ICSharpCode.SharpZipLib.Zip;
13 using IBBoard.WarFoundry.API.Objects.Requirement;
15 namespace IBBoard.WarFoundry.API.Objects
16 {
17 /// <summary>
18 /// Summary description for Army.
19 /// </summary>
20 public class Army : WarFoundryLoadedObject, ICostedWarFoundryObject
21 {
22 //private GameSystem system;
23 private Race armyRace;
24 private int maxPoints;
25 private double pointsTotal;
26 private Dictionary<Category, ArmyCategory> categories;
28 public event ObjectAddDelegate UnitAdded;
29 public event ObjectRemoveDelegate UnitRemoved;
30 public event FailedUnitRequirementDelegate FailedRequirement;
31 public event DoubleValChangedDelegate PointsValueChanged;
32 private DoubleValChangedDelegate PointsValueChangedMethod;
34 public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null)
35 {
36 }
38 public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName)
39 {
40 armyRace = race;
41 Name = armyName;
42 maxPoints = maxArmyPoints;
43 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
44 }
46 public ArmyCategory GetCategory(Category cat)
47 {
48 ArmyCategory armyCat = null;
49 ArmyCategories.TryGetValue(cat, out armyCat);
50 return armyCat;
51 }
53 private Dictionary<Category, ArmyCategory> ArmyCategories
54 {
55 get
56 {
57 if (categories==null)
58 {
59 categories = new Dictionary<Category, ArmyCategory>();
60 Category[] raceCats = Race.Categories;
61 ArmyCategory cat;
62 int raceCatCount = raceCats.Length;
64 for (int i = 0; i < raceCatCount; i++)
65 {
66 Category raceCat = raceCats[i];
67 cat = new ArmyCategory(this, raceCat);
68 categories[raceCat] = cat;
69 cat.PointsValueChanged+= PointsValueChangedMethod;
70 cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
71 cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
72 cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement);
73 }
74 }
76 return categories;
77 }
78 }
80 public ArmyCategory[] Categories
81 {
82 get
83 {
84 return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
85 }
86 }
88 public Race Race
89 {
90 get { return armyRace; }
91 }
93 public GameSystem GameSystem
94 {
95 get { return (armyRace!=null ? armyRace.GameSystem : null); }
96 }
98 protected void OnUnitAdded(Unit unit)
99 {
100 OnUnitAdded(unit, null);
101 }
103 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
104 {
105 if (UnitAdded != null)
106 {
107 UnitAdded(unit);
108 }
110 OnFailedRequirement(failedReqs);
111 }
113 protected void OnUnitRemoved(Unit unit)
114 {
115 OnUnitRemoved(unit, null);
116 }
118 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
119 {
120 if (UnitRemoved!=null)
121 {
122 UnitRemoved(unit);
123 }
125 OnFailedRequirement(failedReqs);
126 }
128 protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs)
129 {
130 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
131 {
132 FailedRequirement(failedReqs);
133 }
134 }
136 private void OnPointsValueChanged(double oldValue, double newValue)
137 {
138 if (PointsValueChanged!=null)
139 {
140 PointsValueChanged(this, oldValue, newValue);
141 }
142 }
144 private double TotalPoints
145 {
146 get { return pointsTotal; }
147 set
148 {
149 double oldPoints = pointsTotal;
150 pointsTotal = value;
152 if (oldPoints!=pointsTotal)
153 {
154 OnPointsValueChanged(oldPoints, pointsTotal);
155 }
156 }
157 }
159 public double Points
160 {
161 get { return TotalPoints; }
162 }
164 public void AddUnit(Unit unit)
165 {
166 Category category = unit.UnitType.MainCategory;
167 AddUnit(unit, category);
168 }
170 public void AddUnit(Unit unit, Category category)
171 {
172 ArmyCategory armyCat = GetCategory(category);
173 armyCat.AddUnit(unit);
174 }
176 public void RemoveUnit(Unit unit)
177 {
178 unit.Category.RemoveUnit(unit);
179 }
181 public Unit[] GetUnits(Category cat)
182 {
183 return GetUnits(this.GetCategory(cat));
184 }
186 public Unit[] GetUnits(ArmyCategory cat)
187 {
188 return cat.GetUnits();
189 }
191 public Unit[] GetUnits()
192 {
193 List<Unit> fullList = new List<Unit>();
195 foreach(ArmyCategory cat in Categories)
196 {
197 fullList.AddRange(cat.GetUnits());
198 }
200 return fullList.ToArray();
201 }
203 public int MaxPoints
204 {
205 get { return maxPoints; }
206 set
207 {
208 if (value > 0)
209 {
210 maxPoints = value;
211 }
212 }
213 }
215 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
216 {
217 if (obj is ArmyCategory)
218 {
219 double points = 0;
221 foreach (ArmyCategory cat in Categories)
222 {
223 points+= cat.Points;
224 }
226 TotalPoints = points;
227 }
228 }
230 [Obsolete]
231 public List<FailedUnitRequirement> CanAddUnit(Unit unit)
232 {
233 return CanAddUnitType(unit.UnitType);
234 }
236 [Obsolete]
237 public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType)
238 {
239 return unitType.CanAddToArmy(this);
240 }
242 [Obsolete]
243 public List<FailedUnitRequirement> CanRemoveUnit(Unit unit)
244 {
245 return CanRemoveUnitType(unit.UnitType);
246 }
248 [Obsolete]
249 public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType)
250 {
251 return unitType.CanRemoveFromArmy(this);
252 }
254 public int GetUnitTypeCount(UnitType unitType)
255 {
256 int count = 0;
258 foreach (ArmyCategory cat in Categories)
259 {
260 count+= cat.GetUnitTypeCount(unitType);
261 }
263 return count;
264 }
266 private void Army_UnitAdded(WarFoundryObject val)
267 {
268 OnUnitAdded((Unit)val);
269 }
271 private void Army_UnitRemoved(WarFoundryObject val)
272 {
273 OnUnitRemoved((Unit)val);
274 }
276 private void Army_FailedRequirement(List<FailedUnitRequirement> val)
277 {
278 OnFailedRequirement(val);
279 }
281 public ICollection<IRequirement> GetRequirements ()
282 {
283 return Race.GetRequirements();
284 }
285 }
286 }