view API/Objects/GameSystem.cs @ 397:5733e3b957cc

Re #341: Default army size of 0 * Set sensible defaults in GameSystem Also: * Setup .hgignore
author IBBoard <dev@ibboard.co.uk>
date Sun, 21 Aug 2011 20:42:23 +0100
parents 50d0d3b39a0b
children 71fceea2725b
line source
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.
5 using System.Collections.Generic;
6 using IBBoard.WarFoundry.API.Factories;
7 using Col = IBBoard.Collections;
9 namespace IBBoard.WarFoundry.API.Objects
10 {
11 /// <summary>
12 /// Summary description for GameSystem.
13 /// </summary>
14 public class GameSystem : WarFoundryStagedLoadingObject
15 {
16 private static readonly int SYSTEM_DEFAULT_ARMY_SIZE = 1000;
17 private bool warnOnError = true;
18 private bool allowAllies = false;
19 private Dictionary<string, Category> categories = new Dictionary<string, Category>();
20 private Dictionary<string, SystemStats> stats = new Dictionary<string, SystemStats>();
21 private string defaultStats = "";
22 private int defaultArmySize = SYSTEM_DEFAULT_ARMY_SIZE;
24 public GameSystem(string systemID, string systemName, IWarFoundryFactory creatingFactory) : base(systemID, systemName, creatingFactory)
25 {
26 //Do nothing special
27 }
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 }
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 }
67 public void AddCategory(Category cat)
68 {
69 RawCategories[cat.ID] = cat;
70 }
72 public Category GetCategory(string id)
73 {
74 EnsureFullyLoaded();
75 Category cat = null;
76 RawCategories.TryGetValue(id, out cat);
77 return cat;
78 }
80 public void SetCategory(Category cat)
81 {
82 Category old;
83 RawCategories.TryGetValue(cat.ID, out old);
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 }
98 public void RemoveCategory(string id)
99 {
100 RawCategories.Remove(id);
101 }
103 public Category[] Categories
104 {
105 get
106 {
107 EnsureFullyLoaded();
108 return DictionaryUtils.ToArray<string, Category>(RawCategories);
109 }
110 }
112 protected Dictionary<string, Category> RawCategories
113 {
114 get { return categories; }
115 }
117 public bool WarnOnError
118 {
119 get
120 {
121 return warnOnError;
122 }
123 set { warnOnError = value; }
124 }
126 public void AddSystemStats(SystemStats sysStats)
127 {
128 stats[sysStats.ID] = sysStats;
129 }
131 public SystemStats StandardSystemStats
132 {
133 get
134 {
135 EnsureFullyLoaded();
136 return stats[defaultStats];
137 }
138 }
140 public string StandardSystemStatsID
141 {
142 get
143 {
144 EnsureFullyLoaded();
145 return defaultStats;
146 }
148 set
149 {
150 if (value != null && value.Trim().Length > 0)
151 {
152 defaultStats = value;
153 }
154 }
155 }
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 }
168 public SystemStats GetSystemStatsForID(string id)
169 {
170 EnsureFullyLoaded();
171 SystemStats statsForID;
172 stats.TryGetValue(id, out statsForID);
173 return statsForID;
174 }
176 public void SetSystemStats(SystemStats newStats)
177 {
178 SystemStats old;
179 stats.TryGetValue(newStats.ID, out old);
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 }
194 public void RemoveSystemStats(string id)
195 {
196 stats.Remove(id);
197 }
199 public Race SystemDefaultRace
200 {
201 get { return WarFoundryLoader.GetDefault().GetRace(this, Race.SYSTEM_DEFAULT_RACE_ID); }
202 }
204 public bool Matches(GameSystem otherSystem)
205 {
206 if (otherSystem == null)
207 {
208 return false;
209 }
211 return this.ID == otherSystem.ID;
212 }
214 public override bool Equals(object obj)
215 {
216 if (obj == null)
217 {
218 return false;
219 }
221 if (obj.GetType().Equals(this.GetType()))
222 {
223 GameSystem otherSystem = (GameSystem)obj;
224 if (!ID.Equals(otherSystem.ID) || !Name.Equals(otherSystem.Name) || !Col.Collections.AreEqual(RawCategories, otherSystem.RawCategories))
225 {
226 return false;
227 }
228 else
229 {
230 return true;
231 }
232 }
233 else
234 {
235 return false;
236 }
237 }
239 public override int GetHashCode()
240 {
241 return ID.GetHashCode() + Name.GetHashCode() + (RawCategories != null ? RawCategories.GetHashCode() : 0) + warnOnError.GetHashCode();
242 }
244 public bool UnitTypeMaxed(UnitType unitType, Army army)
245 {
246 return unitType.MaxNumber != WarFoundryCore.INFINITY && army.GetUnitTypeCount(unitType) >= unitType.MaxNumber;
247 }
249 public bool UnitTypeMinned(UnitType unitType, Army army)
250 {
251 return army.GetUnitTypeCount(unitType) <= unitType.MinNumber;
252 }
254 public List<EquipmentItem> GetSystemEquipmentList()
255 {
256 List<EquipmentItem> items = new List<EquipmentItem>();
257 Race defaultRace = SystemDefaultRace;
259 if (defaultRace != null)
260 {
261 items = defaultRace.GetEquipmentList();
262 }
264 return items;
265 }
267 public EquipmentItem GetSystemEquipmentItem(string id)
268 {
269 EquipmentItem item = null;
270 Race defaultRace = SystemDefaultRace;
272 if (defaultRace != null)
273 {
274 item = defaultRace.GetEquipmentItem(id);
275 }
277 return item;
278 }
280 public UnitType[] GetSystemUnitTypes(Category cat)
281 {
282 UnitType[] items = new UnitType[0];
283 Race defaultRace = SystemDefaultRace;
285 if (defaultRace != null)
286 {
287 items = defaultRace.GetUnitTypes(cat);
288 }
290 return items;
291 }
293 public UnitType GetSystemUnitType(string id)
294 {
295 UnitType unit = null;
296 Race defaultRace = SystemDefaultRace;
298 if (defaultRace != null)
299 {
300 unit = defaultRace.GetUnitType(id);
301 }
303 return unit;
304 }
306 public List<Ability> GetSystemAbilityList()
307 {
308 List<Ability> items = new List<Ability>();
309 Race defaultRace = SystemDefaultRace;
311 if (defaultRace != null)
312 {
313 items = defaultRace.GetAbilityList();
314 }
316 return items;
317 }
319 public Ability GetSystemAbility(string id)
320 {
321 Ability ability = null;
322 Race defaultRace = SystemDefaultRace;
324 if (defaultRace != null)
325 {
326 ability = defaultRace.GetAbility(id);
327 }
329 return ability;
330 }
332 public string GetPointsAbbrev(double pointTemp)
333 {
334 string abbrev = (pointTemp == 1 ? GetPreferredString(SystemPtsAbbrevSingle, SystemPtsAbbrevPlural) : GetPreferredString(SystemPtsAbbrevPlural, SystemPtsAbbrevSingle));
335 return abbrev;
336 }
338 public string GetPointsName(double pointTemp)
339 {
340 string ptsName = (pointTemp == 1 ? GetPreferredString(SystemPtsNameSingle, SystemPtsNamePlural) : GetPreferredString(SystemPtsNamePlural, SystemPtsNameSingle));
341 return ptsName;
342 }
344 private string GetPreferredString(string str1, string str2)
345 {
346 string preferred = "";
348 if (str1 != null)
349 {
350 preferred = str1;
351 }
352 else if (str2 != null)
353 {
354 preferred = str2;
355 }
357 return preferred;
358 }
359 }
360 }