0
|
1 // Race.cs
|
|
2 //
|
|
3 // Copyright (C) 2008 IBBoard
|
|
4 //
|
|
5 // This library is free software; you can redistribute it and/or
|
|
6 // modify it under the terms of the GNU Lesser General Public
|
|
7 // License as published by the Free Software Foundation; either
|
|
8 // version 2.1 of the License, or (at your option) any later version.
|
|
9 //
|
|
10 // This library is distributed in the hope that it will be useful,
|
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 // Lesser General Public License for more details.
|
|
14 //
|
|
15 // You should have received a copy of the GNU Lesser General Public
|
|
16 // License along with this library; if not, write to the Free Software
|
|
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 //
|
|
19 //
|
|
20
|
|
21 using System;
|
|
22 using System.Collections.Generic;
|
|
23 using System.IO;
|
|
24 using System.Xml;
|
|
25 using IBBoard.IO;
|
|
26 using IBBoard.WarFoundry.API.Factories;
|
|
27
|
|
28 namespace IBBoard.WarFoundry.API.Objects
|
|
29 {
|
|
30 public class Race : WarFoundryObject
|
|
31 {
|
|
32 public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault";
|
|
33
|
|
34 private string subID;
|
|
35 private string systemID;
|
|
36 private GameSystem system;
|
|
37 private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat;
|
|
38 private Dictionary<string, UnitType> unitTypes;
|
|
39 private Dictionary<string, EquipmentItem> equipment;
|
|
40 private Dictionary<string, Ability> abilities;
|
|
41 private Category[] cats;
|
|
42 private FileInfo sourceFile;
|
|
43
|
|
44 public Race(string raceID, string raceName, string gameSystemID) : this(raceID, "", raceName, gameSystemID)
|
|
45 {
|
|
46 }
|
|
47
|
|
48 public Race(string raceID, string raceSubID, string raceName, string gameSystemID) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName)
|
|
49 {
|
|
50 subID = (raceSubID == null ? "" : raceSubID);
|
|
51 systemID = gameSystemID;
|
|
52 }
|
|
53
|
|
54 public string SubID
|
|
55 {
|
|
56 get { return subID; }
|
|
57 set { subID = (value == null ? "" : value.Trim()); }
|
|
58 }
|
|
59
|
|
60 public FileInfo SourceFile
|
|
61 {
|
|
62 get { return sourceFile; }
|
|
63 set { sourceFile = value; }
|
|
64 }
|
|
65
|
|
66 public GameSystem GameSystem
|
|
67 {
|
|
68 get
|
|
69 {
|
|
70 if (system == null)
|
|
71 {
|
|
72 system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
|
|
73 }
|
|
74
|
|
75 return system;
|
|
76 }
|
|
77 set
|
|
78 {
|
|
79 if (value == null)
|
|
80 {
|
|
81 throw new ArgumentException("Game system for a race cannot be null");
|
|
82 }
|
|
83
|
|
84 system = value;
|
|
85 }
|
|
86 }
|
|
87
|
|
88 /// <summary>
|
|
89 /// 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.
|
|
90 /// </summary>
|
|
91 /// <param name="id">
|
|
92 /// The ID of the category to get
|
|
93 /// </param>
|
|
94 /// <returns>
|
|
95 /// The <code>Category</code> with the specified ID, or null if one doesn't exist.
|
|
96 /// </returns>
|
|
97 public Category GetCategory(string id)
|
|
98 {
|
|
99 Category[] categories = RaceCategories;
|
|
100 Category cat = null;
|
|
101
|
|
102 for (int i = 0; i<categories.Length; i++)
|
|
103 {
|
|
104 if (categories[i].ID == id)
|
|
105 {
|
|
106 cat = categories[i];
|
|
107 break;
|
|
108 }
|
|
109 }
|
|
110
|
|
111 return cat;
|
|
112 }
|
|
113
|
|
114 /// <summary>
|
|
115 /// Gets a category based on its index within the list. Ordering is defined by the game system definition or by the race's overrides.
|
|
116 /// </summary>
|
|
117 /// <param name="index">
|
|
118 /// A <see cref="System.Int32"/>
|
|
119 /// </param>
|
|
120 /// <returns>
|
|
121 /// A <see cref="Category"/>
|
|
122 /// </returns>
|
|
123 public virtual Category GetCategory(int index)
|
|
124 {
|
|
125 if (cats == null)
|
|
126 {
|
|
127 return GameSystem.GetCategory(index);
|
|
128 }
|
|
129 else
|
|
130 {
|
|
131 return Categories[index];
|
|
132 }
|
|
133 }
|
|
134
|
|
135 public Category[] Categories
|
|
136 {
|
|
137 get
|
|
138 {
|
|
139 return RaceCategories;
|
|
140 }
|
|
141
|
|
142 set
|
|
143 {
|
|
144 RaceOverrideCategories = value;
|
|
145 }
|
|
146 }
|
|
147
|
|
148 /*private virtual Category[] GetCategories()
|
|
149 {
|
|
150 if (cats==null)
|
|
151 {
|
|
152 return GameSystem.Categories;
|
|
153 }
|
|
154 else
|
|
155 {
|
|
156 return cats;
|
|
157 }
|
|
158 }*/
|
|
159
|
|
160 public void SetEquipmentItems(Dictionary<string, EquipmentItem> items)
|
|
161 {
|
|
162 equipment = items;
|
|
163 }
|
|
164
|
|
165 public virtual EquipmentItem GetEquipmentItem(string id)
|
|
166 {
|
|
167 return (EquipmentItem)equipment[id];
|
|
168 }
|
|
169
|
|
170 public List<EquipmentItem> GetEquipmentList()
|
|
171 {
|
|
172 List<EquipmentItem> items = new List<EquipmentItem>();
|
|
173
|
|
174 foreach (EquipmentItem item in equipment.Values)
|
|
175 {
|
|
176 items.Add(item);
|
|
177 }
|
|
178
|
|
179 return items;
|
|
180 }
|
|
181
|
|
182 public virtual bool HasCategoryOverrides()
|
|
183 {
|
|
184 return cats!=null;
|
|
185 }
|
|
186
|
|
187 public void SetUnitTypes(Dictionary<string, UnitType> types)
|
|
188 {
|
|
189 unitTypes = types;
|
|
190 }
|
|
191
|
|
192 public UnitType[] GetUnitTypes(Category cat)
|
|
193 {
|
|
194 if (unitTypesByCat==null)
|
|
195 {
|
|
196 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
|
|
197
|
|
198 foreach (Category category in RaceCategories)
|
|
199 {
|
|
200 unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
|
|
201 }
|
|
202
|
|
203 Dictionary<string,UnitType> catUnitTypes;
|
|
204
|
|
205 foreach (UnitType unit in unitTypes.Values)
|
|
206 {
|
|
207 unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes);
|
|
208
|
|
209 if (catUnitTypes == null)
|
|
210 {
|
|
211 throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name));
|
|
212 }
|
|
213
|
|
214 catUnitTypes.Add(unit.ID, unit);
|
|
215 }
|
|
216 }
|
|
217
|
|
218 ICollection<UnitType> col = unitTypesByCat[cat].Values;
|
|
219 UnitType[] toRet = new UnitType[col.Count];
|
|
220 int i = 0;
|
|
221
|
|
222 foreach (UnitType type in col)
|
|
223 {
|
|
224 toRet[i++] = type;
|
|
225 }
|
|
226
|
|
227 return toRet;
|
|
228 }
|
|
229
|
|
230 public UnitType GetUnitType(string id)
|
|
231 {
|
|
232 return (UnitType)unitTypes[id];
|
|
233 }
|
|
234
|
|
235 public List<Ability> GetAbilityList()
|
|
236 {
|
|
237 List<Ability> items = new List<Ability>();
|
|
238
|
|
239 foreach (Ability ability in abilities.Values)
|
|
240 {
|
|
241 items.Add(ability);
|
|
242 }
|
|
243
|
|
244 return items;
|
|
245 }
|
|
246
|
|
247 public void SetAbilities(Dictionary<string, Ability> newAbilities)
|
|
248 {
|
|
249 abilities = newAbilities;
|
|
250 }
|
|
251
|
|
252 public Ability GetAbility(string id)
|
|
253 {
|
|
254 Ability ability = null;
|
|
255 abilities.TryGetValue(id, out ability);
|
|
256 return ability;
|
|
257 }
|
|
258
|
|
259 protected Category[] RaceCategories
|
|
260 {
|
|
261 get
|
|
262 {
|
|
263 Category[] cats = RaceOverrideCategories;
|
|
264
|
|
265 if (cats == null)
|
|
266 {
|
|
267 //No overrides, so load system categories
|
|
268 cats = GameSystem.Categories;
|
|
269 }
|
|
270
|
|
271 return cats;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 protected virtual Category[] RaceOverrideCategories
|
|
276 {
|
|
277 get
|
|
278 {
|
|
279 return RaceRawOverrideCategories;
|
|
280 }
|
|
281 set
|
|
282 {
|
|
283 RaceRawOverrideCategories = value;
|
|
284 }
|
|
285 }
|
|
286
|
|
287 protected Category[] RaceRawOverrideCategories
|
|
288 {
|
|
289 get { return cats; }
|
|
290 set
|
|
291 {
|
|
292 if (value!=null && value.Length>0)
|
|
293 {
|
|
294 cats = value;
|
|
295 }
|
|
296 else
|
|
297 {
|
|
298 cats = null;
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302
|
|
303 protected virtual Dictionary<string, UnitType> RaceUnitTypes
|
|
304 {
|
|
305 get { return RaceRawUnitTypes; }
|
|
306 set { RaceRawUnitTypes = value; }
|
|
307 }
|
|
308
|
|
309 protected virtual Dictionary<string, EquipmentItem> RaceEquipment
|
|
310 {
|
|
311 get { return RaceRawEquipment; }
|
|
312 set { RaceRawEquipment = value; }
|
|
313 }
|
|
314
|
|
315 protected virtual Dictionary<string, Ability> RaceAbilities
|
|
316 {
|
|
317 get { return RaceRawAbilities; }
|
|
318 set { RaceRawAbilities = value; }
|
|
319 }
|
|
320
|
|
321 protected Dictionary<string, UnitType> RaceRawUnitTypes
|
|
322 {
|
|
323 get { return unitTypes; }
|
|
324 set { unitTypes = value; }
|
|
325 }
|
|
326
|
|
327 protected Dictionary<string, EquipmentItem> RaceRawEquipment
|
|
328 {
|
|
329 get { return equipment; }
|
|
330 set { equipment = value; }
|
|
331 }
|
|
332
|
|
333 protected Dictionary<string, Ability> RaceRawAbilities
|
|
334 {
|
|
335 get { return abilities; }
|
|
336 set { abilities = value; }
|
|
337 }
|
|
338 }
|
|
339 }
|