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