comparison API/Objects/Race.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children 44a6539fadf9
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (Race.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.
4
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9 using IBBoard.IO;
10 using IBBoard.WarFoundry.API.Factories;
11
12 namespace IBBoard.WarFoundry.API.Objects
13 {
14 public class Race : WarFoundryStagedLoadingObject
15 {
16 public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault";
17
18 private string subID;
19 private GameSystem system;
20 private string defaultArmyName = "";
21 private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat;
22 private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>();
23 private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>();
24 private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>();
25 private Dictionary<string, Category> categories = new Dictionary<string,Category>();
26 private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>();
27
28 public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory)
29 {
30 }
31
32 public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID != "" ? "_" + raceSubID : ""), raceName, creatingFactory)
33 {
34 subID = (raceSubID == null ? "" : raceSubID);
35 system = gameSystem;
36 }
37
38 public string SubID
39 {
40 get { return subID; }
41 set { subID = (value == null ? "" : value.Trim()); }
42 }
43
44 public GameSystem GameSystem
45 {
46 get { return system; }
47 set
48 {
49 if (value == null)
50 {
51 throw new ArgumentException("Game system for a race cannot be null");
52 }
53
54 system = value;
55 }
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
72 public void AddCategory(Category cat)
73 {
74 categories[cat.ID] = cat;
75 }
76
77 /// <summary>
78 /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID.
79 /// </summary>
80 /// <param name="id">
81 /// The ID of the category to get
82 /// </param>
83 /// <returns>
84 /// The <code>Category</code> with the specified ID, or null if one doesn't exist.
85 /// </returns>
86 public Category GetCategory(string id)
87 {
88 EnsureFullyLoaded();
89 Category cat = null;
90 categories.TryGetValue(id, out cat);
91
92 if (cat == null)
93 {
94 cat = GameSystem.GetCategory(id);
95 }
96
97 return cat;
98 }
99
100 public Category[] Categories
101 {
102 get
103 {
104 EnsureFullyLoaded();
105 Category[] cats;
106
107 if (!HasCategoryOverrides())
108 {
109 cats = GameSystem.Categories;
110 }
111 else
112 {
113 cats = DictionaryUtils.ToArray<string, Category>(categories);
114 }
115
116 return cats;
117 }
118 }
119
120 public bool HasCategoryOverrides()
121 {
122 EnsureFullyLoaded();
123 return categories.Count > 0;
124 }
125
126 public void AddEquipmentItem(EquipmentItem item)
127 {
128 //TODO: Throw DuplicateItemException
129 equipment.Add(item.ID, item);
130 }
131
132 public EquipmentItem GetEquipmentItem(string id)
133 {
134 EnsureFullyLoaded();
135 return DictionaryUtils.GetValue(equipment, id);
136 }
137
138 public List<EquipmentItem> GetEquipmentList()
139 {
140 EnsureFullyLoaded();
141 List<EquipmentItem> items = new List<EquipmentItem>();
142
143 foreach (EquipmentItem item in equipment.Values)
144 {
145 items.Add(item);
146 }
147
148 return items;
149 }
150
151 public void AddUnitType(UnitType type)
152 {
153 CacheUnitType(type);
154 unitTypes.Add(type.ID, type);
155 }
156
157 public UnitType[] GetUnitTypes(Category cat)
158 {
159 EnsureFullyLoaded();
160 BuildUnitTypesByCategoryCache();
161 Dictionary<string, UnitType> unitTypesDictionary;
162 unitTypesByCat.TryGetValue(cat, out unitTypesDictionary);
163 UnitType[] unitTypesArray;
164
165 if (unitTypesDictionary == null)
166 {
167 unitTypesArray = new UnitType[0];
168 }
169 else
170 {
171 unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary);
172 }
173
174 return unitTypesArray;
175 }
176
177 private void CacheUnitType(UnitType unit)
178 {
179 BuildUnitTypesByCategoryCache();
180
181 foreach (Category cat in unit.Categories)
182 {
183 Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat);
184
185 if (catUnitTypes == null)
186 {
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));
188 }
189
190 catUnitTypes.Add(unit.ID, unit);
191 }
192 }
193
194 private void BuildUnitTypesByCategoryCache()
195 {
196 if (unitTypesByCat == null)
197 {
198 DoBuildUnitTypesByCategoryCache();
199 }
200 }
201
202 private void DoBuildUnitTypesByCategoryCache()
203 {
204 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
205
206 foreach (Category category in Categories)
207 {
208 unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
209 }
210
211 foreach (UnitType unit in unitTypes.Values)
212 {
213 CacheUnitType(unit);
214 }
215 }
216
217 public UnitType GetUnitType(string id)
218 {
219 EnsureFullyLoaded();
220 return DictionaryUtils.GetValue(unitTypes, id);
221 }
222
223 public List<Ability> GetAbilityList()
224 {
225 EnsureFullyLoaded();
226 List<Ability> items = new List<Ability>();
227 items.AddRange(abilities.Values);
228 return items;
229 }
230
231 public void AddAbility(Ability newAbility)
232 {
233 //TODO: Throw DuplicateItemException
234 abilities.Add(newAbility.ID, newAbility);
235 }
236
237 public Ability GetAbility(string id)
238 {
239 EnsureFullyLoaded();
240 return DictionaryUtils.GetValue(abilities, id);
241 }
242
243 protected virtual Dictionary<string, UnitType> RaceUnitTypes
244 {
245 get { return RaceRawUnitTypes; }
246 set { RaceRawUnitTypes = value; }
247 }
248
249 protected virtual Dictionary<string, EquipmentItem> RaceEquipment
250 {
251 get { return RaceRawEquipment; }
252 set { RaceRawEquipment = value; }
253 }
254
255 protected virtual Dictionary<string, Ability> RaceAbilities
256 {
257 get { return RaceRawAbilities; }
258 set { RaceRawAbilities = value; }
259 }
260
261 protected Dictionary<string, UnitType> RaceRawUnitTypes
262 {
263 get { return unitTypes; }
264 set { unitTypes = value; }
265 }
266
267 protected Dictionary<string, EquipmentItem> RaceRawEquipment
268 {
269 get { return equipment; }
270 set { equipment = value; }
271 }
272
273 protected Dictionary<string, Ability> RaceRawAbilities
274 {
275 get { return abilities; }
276 set { abilities = value; }
277 }
278
279 public void AddUnitMemberType(UnitMemberType memberType)
280 {
281 memberTypes[memberType.ID] = memberType;
282 }
283
284 /// <summary>
285 /// Gets a unit member type by its ID.
286 /// </summary>
287 /// <param name="id">
288 /// The ID of the unit member type to get
289 /// </param>
290 /// <returns>
291 /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist.
292 /// </returns>
293 public UnitMemberType GetUnitMemberType(string id)
294 {
295 EnsureFullyLoaded();
296 return DictionaryUtils.GetValue(memberTypes, id);
297 }
298
299 public UnitMemberType[] UnitMemberTypes
300 {
301 get
302 {
303 EnsureFullyLoaded();
304 return DictionaryUtils.ToArray(memberTypes);
305 }
306 }
307 }
308 }