comparison api/Objects/Race.cs @ 283:cd657faa0c05

* Merge branch into trunk (branch contained some fixes that should have been made on trunk) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Wed, 20 Oct 2010 19:26:57 +0000
parents 3b395ab8784d
children 92d10b06ab0f
comparison
equal deleted inserted replaced
282:cd082d7bd4d9 283:cd657faa0c05
10 using IBBoard.WarFoundry.API.Factories; 10 using IBBoard.WarFoundry.API.Factories;
11 11
12 namespace IBBoard.WarFoundry.API.Objects 12 namespace IBBoard.WarFoundry.API.Objects
13 { 13 {
14 public class Race : WarFoundryStagedLoadingObject 14 public class Race : WarFoundryStagedLoadingObject
15 { 15 {
16 public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; 16 public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault";
17 17
18 private string subID; 18 private string subID;
19 private GameSystem system; 19 private GameSystem system;
20 private string defaultArmyName = "";
20 private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; 21 private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat;
21 private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>(); 22 private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>();
22 private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>(); 23 private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>();
23 private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>(); 24 private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>();
24 private Dictionary<string, Category> categories = new Dictionary<string,Category>(); 25 private Dictionary<string, Category> categories = new Dictionary<string,Category>();
25 private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>(); 26 private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>();
26 27
27 public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory) 28 public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory)
28 { 29 {
29 } 30 }
30 31
31 public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName, creatingFactory) 32 public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID != "" ? "_" + raceSubID : ""), raceName, creatingFactory)
32 { 33 {
33 subID = (raceSubID == null ? "" : raceSubID); 34 subID = (raceSubID == null ? "" : raceSubID);
34 system = gameSystem; 35 system = gameSystem;
35 } 36 }
36 37
47 { 48 {
48 if (value == null) 49 if (value == null)
49 { 50 {
50 throw new ArgumentException("Game system for a race cannot be null"); 51 throw new ArgumentException("Game system for a race cannot be null");
51 } 52 }
52 53
53 system = value; 54 system = value;
54 } 55 }
55 } 56 }
56 57
58 public string ArmyDefaultName
59 {
60 get { return defaultArmyName; }
61 set
62 {
63 if (value == null)
64 {
65 throw new ArgumentException("No default army name");
66 }
67
68 defaultArmyName = value;
69 }
70 }
71
57 public void AddCategory(Category cat) 72 public void AddCategory(Category cat)
58 { 73 {
59 categories[cat.ID] = cat; 74 categories[cat.ID] = cat;
60 } 75 }
61 76
64 /// </summary> 79 /// </summary>
65 /// <param name="id"> 80 /// <param name="id">
66 /// The ID of the category to get 81 /// The ID of the category to get
67 /// </param> 82 /// </param>
68 /// <returns> 83 /// <returns>
69 /// The <code>Category</code> with the specified ID, or null if one doesn't exist. 84 /// The <code>Category</code> with the specified ID, or null if one doesn't exist.
70 /// </returns> 85 /// </returns>
71 public Category GetCategory(string id) 86 public Category GetCategory(string id)
72 { 87 {
73 EnsureFullyLoaded(); 88 EnsureFullyLoaded();
74 Category cat = null; 89 Category cat = null;
75 categories.TryGetValue(id, out cat); 90 categories.TryGetValue(id, out cat);
76 91
77 if (cat == null) 92 if (cat == null)
78 { 93 {
79 cat = GameSystem.GetCategory(id); 94 cat = GameSystem.GetCategory(id);
80 } 95 }
81 96
82 return cat; 97 return cat;
83 } 98 }
84 99
85 public Category[] Categories 100 public Category[] Categories
86 { 101 {
87 get 102 get
88 { 103 {
89 EnsureFullyLoaded(); 104 EnsureFullyLoaded();
90 Category[] cats; 105 Category[] cats;
91 106
92 if (!HasCategoryOverrides()) 107 if (!HasCategoryOverrides())
93 { 108 {
94 cats = GameSystem.Categories; 109 cats = GameSystem.Categories;
95 } 110 }
96 else 111 else
97 { 112 {
98 cats = DictionaryUtils.ToArray<string, Category>(categories); 113 cats = DictionaryUtils.ToArray<string, Category>(categories);
99 } 114 }
100 115
101 return cats; 116 return cats;
102 } 117 }
103 } 118 }
104 119
105 public bool HasCategoryOverrides() 120 public bool HasCategoryOverrides()
117 public EquipmentItem GetEquipmentItem(string id) 132 public EquipmentItem GetEquipmentItem(string id)
118 { 133 {
119 EnsureFullyLoaded(); 134 EnsureFullyLoaded();
120 return DictionaryUtils.GetValue(equipment, id); 135 return DictionaryUtils.GetValue(equipment, id);
121 } 136 }
122 137
123 public List<EquipmentItem> GetEquipmentList() 138 public List<EquipmentItem> GetEquipmentList()
124 { 139 {
125 EnsureFullyLoaded(); 140 EnsureFullyLoaded();
126 List<EquipmentItem> items = new List<EquipmentItem>(); 141 List<EquipmentItem> items = new List<EquipmentItem>();
127 142
128 foreach (EquipmentItem item in equipment.Values) 143 foreach (EquipmentItem item in equipment.Values)
129 { 144 {
130 items.Add(item); 145 items.Add(item);
131 } 146 }
132 147
133 return items; 148 return items;
134 } 149 }
135 150
136 public void AddUnitType(UnitType type) 151 public void AddUnitType(UnitType type)
137 { 152 {
138 CacheUnitType(type); 153 CacheUnitType(type);
139 unitTypes.Add(type.ID, type); 154 unitTypes.Add(type.ID, type);
140 } 155 }
141 156
142 public UnitType[] GetUnitTypes(Category cat) 157 public UnitType[] GetUnitTypes(Category cat)
143 { 158 {
144 EnsureFullyLoaded(); 159 EnsureFullyLoaded();
145 BuildUnitTypesByCategoryCache(); 160 BuildUnitTypesByCategoryCache();
146 Dictionary<string, UnitType> unitTypesDictionary; 161 Dictionary<string, UnitType> unitTypesDictionary;
147 unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); 162 unitTypesByCat.TryGetValue(cat, out unitTypesDictionary);
148 UnitType[] unitTypesArray; 163 UnitType[] unitTypesArray;
149 164
150 if (unitTypesDictionary == null) 165 if (unitTypesDictionary == null)
151 { 166 {
152 unitTypesArray = new UnitType[0]; 167 unitTypesArray = new UnitType[0];
153 } 168 }
154 else 169 else
155 { 170 {
156 unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary); 171 unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary);
157 } 172 }
158 173
159 return unitTypesArray; 174 return unitTypesArray;
160 } 175 }
161 176
162 private void CacheUnitType(UnitType unit) 177 private void CacheUnitType(UnitType unit)
163 { 178 {
164 BuildUnitTypesByCategoryCache(); 179 BuildUnitTypesByCategoryCache();
165 180
166 foreach (Category cat in unit.Categories) 181 foreach (Category cat in unit.Categories)
167 { 182 {
168 Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat); 183 Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat);
169 184
170 if (catUnitTypes == null) 185 if (catUnitTypes == null)
171 { 186 {
172 throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID)); 187 throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID));
173 } 188 }
174 189
175 catUnitTypes.Add(unit.ID, unit); 190 catUnitTypes.Add(unit.ID, unit);
176 } 191 }
177 } 192 }
178 193
179 private void BuildUnitTypesByCategoryCache() 194 private void BuildUnitTypesByCategoryCache()
180 { 195 {
181 if (unitTypesByCat == null) 196 if (unitTypesByCat == null)
182 { 197 {
183 DoBuildUnitTypesByCategoryCache(); 198 DoBuildUnitTypesByCategoryCache();
185 } 200 }
186 201
187 private void DoBuildUnitTypesByCategoryCache() 202 private void DoBuildUnitTypesByCategoryCache()
188 { 203 {
189 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); 204 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
190 205
191 foreach (Category category in Categories) 206 foreach (Category category in Categories)
192 { 207 {
193 unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); 208 unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
194 } 209 }
195 210
196 foreach (UnitType unit in unitTypes.Values) 211 foreach (UnitType unit in unitTypes.Values)
197 { 212 {
198 CacheUnitType(unit); 213 CacheUnitType(unit);
199 } 214 }
200 } 215 }
202 public UnitType GetUnitType(string id) 217 public UnitType GetUnitType(string id)
203 { 218 {
204 EnsureFullyLoaded(); 219 EnsureFullyLoaded();
205 return DictionaryUtils.GetValue(unitTypes, id); 220 return DictionaryUtils.GetValue(unitTypes, id);
206 } 221 }
207 222
208 public List<Ability> GetAbilityList() 223 public List<Ability> GetAbilityList()
209 { 224 {
210 EnsureFullyLoaded(); 225 EnsureFullyLoaded();
211 List<Ability> items = new List<Ability>(); 226 List<Ability> items = new List<Ability>();
212 items.AddRange(abilities.Values); 227 items.AddRange(abilities.Values);
213 return items; 228 return items;
214 } 229 }
215 230
216 public void AddAbility(Ability newAbility) 231 public void AddAbility(Ability newAbility)
217 { 232 {
218 //TODO: Throw DuplicateItemException 233 //TODO: Throw DuplicateItemException
219 abilities.Add(newAbility.ID, newAbility); 234 abilities.Add(newAbility.ID, newAbility);
220 } 235 }
225 foreach (Ability ability in newAbilities.Values) 240 foreach (Ability ability in newAbilities.Values)
226 { 241 {
227 AddAbility(ability); 242 AddAbility(ability);
228 } 243 }
229 } 244 }
230 245
231 public Ability GetAbility(string id) 246 public Ability GetAbility(string id)
232 { 247 {
233 EnsureFullyLoaded(); 248 EnsureFullyLoaded();
234 return DictionaryUtils.GetValue(abilities, id); 249 return DictionaryUtils.GetValue(abilities, id);
235 } 250 }
236 251
237 protected virtual Dictionary<string, UnitType> RaceUnitTypes 252 protected virtual Dictionary<string, UnitType> RaceUnitTypes
238 { 253 {
239 get { return RaceRawUnitTypes; } 254 get { return RaceRawUnitTypes; }
240 set { RaceRawUnitTypes = value; } 255 set { RaceRawUnitTypes = value; }
241 } 256 }
242 257
243 protected virtual Dictionary<string, EquipmentItem> RaceEquipment 258 protected virtual Dictionary<string, EquipmentItem> RaceEquipment
244 { 259 {
245 get { return RaceRawEquipment; } 260 get { return RaceRawEquipment; }
246 set { RaceRawEquipment = value; } 261 set { RaceRawEquipment = value; }
247 } 262 }
248 263
249 protected virtual Dictionary<string, Ability> RaceAbilities 264 protected virtual Dictionary<string, Ability> RaceAbilities
250 { 265 {
251 get { return RaceRawAbilities; } 266 get { return RaceRawAbilities; }
252 set { RaceRawAbilities = value; } 267 set { RaceRawAbilities = value; }
253 } 268 }
254 269
255 protected Dictionary<string, UnitType> RaceRawUnitTypes 270 protected Dictionary<string, UnitType> RaceRawUnitTypes
256 { 271 {
257 get { return unitTypes; } 272 get { return unitTypes; }
258 set { unitTypes = value; } 273 set { unitTypes = value; }
259 } 274 }
260 275
261 protected Dictionary<string, EquipmentItem> RaceRawEquipment 276 protected Dictionary<string, EquipmentItem> RaceRawEquipment
262 { 277 {
263 get { return equipment; } 278 get { return equipment; }
264 set { equipment = value; } 279 set { equipment = value; }
265 } 280 }
266 281
267 protected Dictionary<string, Ability> RaceRawAbilities 282 protected Dictionary<string, Ability> RaceRawAbilities
268 { 283 {
269 get { return abilities; } 284 get { return abilities; }
270 set { abilities = value; } 285 set { abilities = value; }
271 } 286 }
272 287
273 public void AddUnitMemberType(UnitMemberType memberType) 288 public void AddUnitMemberType(UnitMemberType memberType)
274 { 289 {
275 memberTypes[memberType.ID] = memberType; 290 memberTypes[memberType.ID] = memberType;
276 } 291 }
277 292
280 /// </summary> 295 /// </summary>
281 /// <param name="id"> 296 /// <param name="id">
282 /// The ID of the unit member type to get 297 /// The ID of the unit member type to get
283 /// </param> 298 /// </param>
284 /// <returns> 299 /// <returns>
285 /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist. 300 /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist.
286 /// </returns> 301 /// </returns>
287 public UnitMemberType GetUnitMemberType(string id) 302 public UnitMemberType GetUnitMemberType(string id)
288 { 303 {
289 EnsureFullyLoaded(); 304 EnsureFullyLoaded();
290 return DictionaryUtils.GetValue(memberTypes, id); 305 return DictionaryUtils.GetValue(memberTypes, id);
291 } 306 }
292 307
293 public UnitMemberType[] UnitMemberTypes 308 public UnitMemberType[] UnitMemberTypes
294 { 309 {
295 get 310 get
296 { 311 {
297 EnsureFullyLoaded(); 312 EnsureFullyLoaded();
298 return DictionaryUtils.ToArray(memberTypes); 313 return DictionaryUtils.ToArray(memberTypes);
299 } 314 }
300 } 315 }
301 } 316 }