view api/Objects/Army.cs @ 259:35dc06030355

Re #270: Add multiple categories to API * Add "category" attribute to army schema * Add method to define category when adding unit to army * Make army loading check new attribute and assign to correct category or main category TODO: Write out category of unit to army file
author IBBoard <dev@ibboard.co.uk>
date Wed, 19 May 2010 20:07:07 +0000
parents 7f13ffcb8765
children 92d10b06ab0f
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 IBBoard.WarFoundry.API.Requirements;
13 using ICSharpCode.SharpZipLib.Zip;
15 namespace IBBoard.WarFoundry.API.Objects
16 {
17 /// <summary>
18 /// Summary description for Army.
19 /// </summary>
20 public class Army : WarFoundryObject, 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 [Obsolete("Use Points instead")]
160 public double PointsTotal
161 {
162 get { return TotalPoints; }
163 }
165 public double Points
166 {
167 get { return TotalPoints; }
168 }
170 public void AddUnit(Unit unit)
171 {
172 Category category = unit.UnitType.MainCategory;
173 AddUnit(unit, category);
174 }
176 public void AddUnit(Unit unit, Category category)
177 {
178 ArmyCategory armyCat = GetCategory(category);
179 armyCat.AddUnit(unit);
180 }
182 public void RemoveUnit(Unit unit)
183 {
184 unit.Category.RemoveUnit(unit);
185 }
187 public Unit[] GetUnits(Category cat)
188 {
189 return GetUnits(this.GetCategory(cat));
190 }
192 public Unit[] GetUnits(ArmyCategory cat)
193 {
194 return cat.GetUnits();
195 }
197 public Unit[] GetUnits()
198 {
199 List<Unit> fullList = new List<Unit>();
201 foreach(ArmyCategory cat in Categories)
202 {
203 fullList.AddRange(cat.GetUnits());
204 }
206 return fullList.ToArray();
207 }
209 public int MaxPoints
210 {
211 get { return maxPoints; }
212 set
213 {
214 if (value > 0)
215 {
216 maxPoints = value;
217 }
218 }
219 }
221 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
222 {
223 if (obj is ArmyCategory)
224 {
225 double points = 0;
227 foreach (ArmyCategory cat in Categories)
228 {
229 points+= cat.Points;
230 }
232 TotalPoints = points;
233 }
234 }
236 public List<FailedUnitRequirement> CanAddUnit(Unit unit)
237 {
238 return CanAddUnitType(unit.UnitType);
239 }
241 public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType)
242 {
243 return unitType.CanAddToArmy(this);
244 }
246 public List<FailedUnitRequirement> CanRemoveUnit(Unit unit)
247 {
248 return CanRemoveUnitType(unit.UnitType);
249 }
251 public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType)
252 {
253 return unitType.CanRemoveFromArmy(this);
254 }
256 public int GetUnitTypeCount(UnitType unitType)
257 {
258 int count = 0;
260 foreach (ArmyCategory cat in Categories)
261 {
262 count+= cat.GetUnitTypeCount(unitType);
263 }
265 return count;
266 }
268 private void Army_UnitAdded(WarFoundryObject val)
269 {
270 OnUnitAdded((Unit)val);
271 }
273 private void Army_UnitRemoved(WarFoundryObject val)
274 {
275 OnUnitRemoved((Unit)val);
276 }
278 private void Army_FailedRequirement(List<FailedUnitRequirement> val)
279 {
280 OnFailedRequirement(val);
281 }
282 }
283 }