Mercurial > repos > IBBoard.WarFoundry.API
comparison API/Objects/GameSystem.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 | 50d0d3b39a0b |
comparison
equal
deleted
inserted
replaced
336:3631c1493c7f | 337:3c4a6403a88c |
---|---|
1 // This file (GameSystem.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009, 2010, 2011 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.Collections.Generic; | |
6 using IBBoard.WarFoundry.API.Factories; | |
7 | |
8 namespace IBBoard.WarFoundry.API.Objects | |
9 { | |
10 /// <summary> | |
11 /// Summary description for GameSystem. | |
12 /// </summary> | |
13 public class GameSystem : WarFoundryStagedLoadingObject | |
14 { | |
15 private static int SYSTEM_DEFAULT_ARMY_SIZE = 1000; | |
16 private bool warnOnError; | |
17 private bool allowAllies; | |
18 private Dictionary<string, Category> categories = new Dictionary<string, Category>(); | |
19 private Dictionary<string, SystemStats> stats = new Dictionary<string, SystemStats>(); | |
20 private string defaultStats; | |
21 private int defaultArmySize; | |
22 | |
23 public GameSystem(string systemID, string systemName, IWarFoundryFactory creatingFactory) : base(systemID, systemName, creatingFactory) | |
24 { | |
25 stats = new Dictionary<string, SystemStats>(); | |
26 defaultStats = ""; | |
27 } | |
28 | |
29 public int SystemArmyDefaultSize | |
30 { | |
31 get { return defaultArmySize; } | |
32 set | |
33 { | |
34 if (value == 0) | |
35 { | |
36 defaultArmySize = SYSTEM_DEFAULT_ARMY_SIZE; | |
37 } | |
38 else | |
39 { | |
40 defaultArmySize = value; | |
41 } | |
42 } | |
43 } | |
44 | |
45 public string SystemPtsAbbrevSingle | |
46 { | |
47 get; set; | |
48 } | |
49 public string SystemPtsAbbrevPlural | |
50 { | |
51 get; set; | |
52 } | |
53 public string SystemPtsNameSingle | |
54 { | |
55 get; set; | |
56 } | |
57 public string SystemPtsNamePlural | |
58 { | |
59 get; set; | |
60 } | |
61 public bool AllowAllies | |
62 { | |
63 get { return allowAllies; } | |
64 set { allowAllies = value; } | |
65 } | |
66 | |
67 public void AddCategory(Category cat) | |
68 { | |
69 RawCategories[cat.ID] = cat; | |
70 } | |
71 | |
72 public Category GetCategory(string id) | |
73 { | |
74 EnsureFullyLoaded(); | |
75 Category cat = null; | |
76 RawCategories.TryGetValue(id, out cat); | |
77 return cat; | |
78 } | |
79 | |
80 public void SetCategory(Category cat) | |
81 { | |
82 Category old; | |
83 RawCategories.TryGetValue(cat.ID, out old); | |
84 | |
85 if (old == null) | |
86 { | |
87 AddCategory(cat); | |
88 } | |
89 else | |
90 { | |
91 if (!old.Equals(cat)) | |
92 { | |
93 RawCategories[old.ID] = cat; | |
94 } | |
95 } | |
96 } | |
97 | |
98 public void RemoveCategory(string id) | |
99 { | |
100 RawCategories.Remove(id); | |
101 } | |
102 | |
103 public Category[] Categories | |
104 { | |
105 get | |
106 { | |
107 EnsureFullyLoaded(); | |
108 return DictionaryUtils.ToArray<string, Category>(RawCategories); | |
109 } | |
110 } | |
111 | |
112 protected Dictionary<string, Category> RawCategories | |
113 { | |
114 get { return categories; } | |
115 } | |
116 | |
117 public bool WarnOnError | |
118 { | |
119 get | |
120 { | |
121 return warnOnError; | |
122 } | |
123 set { warnOnError = value; } | |
124 } | |
125 | |
126 public void AddSystemStats(SystemStats sysStats) | |
127 { | |
128 stats[sysStats.ID] = sysStats; | |
129 } | |
130 | |
131 public SystemStats StandardSystemStats | |
132 { | |
133 get | |
134 { | |
135 EnsureFullyLoaded(); | |
136 return stats[defaultStats]; | |
137 } | |
138 } | |
139 | |
140 public string StandardSystemStatsID | |
141 { | |
142 get | |
143 { | |
144 EnsureFullyLoaded(); | |
145 return defaultStats; | |
146 } | |
147 | |
148 set | |
149 { | |
150 if (value != null && value.Trim().Length > 0) | |
151 { | |
152 defaultStats = value; | |
153 } | |
154 } | |
155 } | |
156 | |
157 public SystemStats[] SystemStats | |
158 { | |
159 get | |
160 { | |
161 EnsureFullyLoaded(); | |
162 SystemStats[] statsArray = new SystemStats[stats.Count]; | |
163 stats.Values.CopyTo(statsArray, 0); | |
164 return statsArray; | |
165 } | |
166 } | |
167 | |
168 public SystemStats GetSystemStatsForID(string id) | |
169 { | |
170 EnsureFullyLoaded(); | |
171 SystemStats statsForID; | |
172 stats.TryGetValue(id, out statsForID); | |
173 return statsForID; | |
174 } | |
175 | |
176 public void SetSystemStats(SystemStats newStats) | |
177 { | |
178 SystemStats old; | |
179 stats.TryGetValue(newStats.ID, out old); | |
180 | |
181 if (old == null) | |
182 { | |
183 AddSystemStats(newStats); | |
184 } | |
185 else | |
186 { | |
187 if (!old.Equals(newStats)) | |
188 { | |
189 stats[old.ID] = newStats; | |
190 } | |
191 } | |
192 } | |
193 | |
194 public void RemoveSystemStats(string id) | |
195 { | |
196 stats.Remove(id); | |
197 } | |
198 | |
199 public Race SystemDefaultRace | |
200 { | |
201 get { return WarFoundryLoader.GetDefault().GetRace(this, Race.SYSTEM_DEFAULT_RACE_ID); } | |
202 } | |
203 | |
204 public bool Matches(GameSystem otherSystem) | |
205 { | |
206 if (otherSystem == null) | |
207 { | |
208 return false; | |
209 } | |
210 | |
211 return this.ID == otherSystem.ID; | |
212 } | |
213 | |
214 public override bool Equals(object obj) | |
215 { | |
216 if (obj == null) | |
217 { | |
218 return false; | |
219 } | |
220 | |
221 if (obj.GetType().Equals(this.GetType())) | |
222 { | |
223 GameSystem otherSystem = (GameSystem)obj; | |
224 | |
225 return this.ID == otherSystem.ID && this.Name == otherSystem.Name && ((this.RawCategories == null && otherSystem.RawCategories == null) || this.RawCategories.Equals(otherSystem.RawCategories)); | |
226 } | |
227 else | |
228 { | |
229 return false; | |
230 } | |
231 } | |
232 | |
233 public override int GetHashCode() | |
234 { | |
235 return ID.GetHashCode() + Name.GetHashCode() + (RawCategories != null ? RawCategories.GetHashCode() : 0) + warnOnError.GetHashCode(); | |
236 } | |
237 | |
238 public bool UnitTypeMaxed(UnitType unitType, Army army) | |
239 { | |
240 return unitType.MaxNumber != WarFoundryCore.INFINITY && army.GetUnitTypeCount(unitType) >= unitType.MaxNumber; | |
241 } | |
242 | |
243 public bool UnitTypeMinned(UnitType unitType, Army army) | |
244 { | |
245 return army.GetUnitTypeCount(unitType) <= unitType.MinNumber; | |
246 } | |
247 | |
248 public List<EquipmentItem> GetSystemEquipmentList() | |
249 { | |
250 List<EquipmentItem> items = new List<EquipmentItem>(); | |
251 Race defaultRace = SystemDefaultRace; | |
252 | |
253 if (defaultRace != null) | |
254 { | |
255 items = defaultRace.GetEquipmentList(); | |
256 } | |
257 | |
258 return items; | |
259 } | |
260 | |
261 public EquipmentItem GetSystemEquipmentItem(string id) | |
262 { | |
263 EquipmentItem item = null; | |
264 Race defaultRace = SystemDefaultRace; | |
265 | |
266 if (defaultRace != null) | |
267 { | |
268 item = defaultRace.GetEquipmentItem(id); | |
269 } | |
270 | |
271 return item; | |
272 } | |
273 | |
274 public UnitType[] GetSystemUnitTypes(Category cat) | |
275 { | |
276 UnitType[] items = new UnitType[0]; | |
277 Race defaultRace = SystemDefaultRace; | |
278 | |
279 if (defaultRace != null) | |
280 { | |
281 items = defaultRace.GetUnitTypes(cat); | |
282 } | |
283 | |
284 return items; | |
285 } | |
286 | |
287 public UnitType GetSystemUnitType(string id) | |
288 { | |
289 UnitType unit = null; | |
290 Race defaultRace = SystemDefaultRace; | |
291 | |
292 if (defaultRace != null) | |
293 { | |
294 unit = defaultRace.GetUnitType(id); | |
295 } | |
296 | |
297 return unit; | |
298 } | |
299 | |
300 public List<Ability> GetSystemAbilityList() | |
301 { | |
302 List<Ability> items = new List<Ability>(); | |
303 Race defaultRace = SystemDefaultRace; | |
304 | |
305 if (defaultRace != null) | |
306 { | |
307 items = defaultRace.GetAbilityList(); | |
308 } | |
309 | |
310 return items; | |
311 } | |
312 | |
313 public Ability GetSystemAbility(string id) | |
314 { | |
315 Ability ability = null; | |
316 Race defaultRace = SystemDefaultRace; | |
317 | |
318 if (defaultRace != null) | |
319 { | |
320 ability = defaultRace.GetAbility(id); | |
321 } | |
322 | |
323 return ability; | |
324 } | |
325 | |
326 public string GetPointsAbbrev(double pointTemp) | |
327 { | |
328 string abbrev = (pointTemp == 1 ? GetPreferredString(SystemPtsAbbrevSingle, SystemPtsAbbrevPlural) : GetPreferredString(SystemPtsAbbrevPlural, SystemPtsAbbrevSingle)); | |
329 return abbrev; | |
330 } | |
331 | |
332 public string GetPointsName(double pointTemp) | |
333 { | |
334 string ptsName = (pointTemp == 1 ? GetPreferredString(SystemPtsNameSingle, SystemPtsNamePlural) : GetPreferredString(SystemPtsNamePlural, SystemPtsNameSingle)); | |
335 return ptsName; | |
336 } | |
337 | |
338 private string GetPreferredString(string str1, string str2) | |
339 { | |
340 string preferred = ""; | |
341 | |
342 if (str1 != null) | |
343 { | |
344 preferred = str1; | |
345 } | |
346 else if (str2 != null) | |
347 { | |
348 preferred = str2; | |
349 } | |
350 | |
351 return preferred; | |
352 } | |
353 } | |
354 } |