Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Objects/UnitType.cs @ 34:b28be912adab
Re #32 - Migrate to schema
* Remove use of Categories (now just has a single category)
* Fix infinite loop of trying to load files by adding a "is loading" flag
* Fix invalid setting of MinSize to MaxNumber when we're testing if MinSize > MaxSize
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 15 Mar 2009 16:07:52 +0000 |
parents | 306558904c2a |
children | 548cfc776f54 |
rev | line source |
---|---|
15 | 1 // This file (UnitType.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
0 | 5 using System; |
6 using System.Collections; | |
7 using System.Collections.Generic; | |
8 using System.Xml; | |
9 using IBBoard.Logging; | |
10 using IBBoard.WarFoundry.API.Requirements; | |
11 | |
12 namespace IBBoard.WarFoundry.API.Objects | |
13 { | |
14 /// <summary> | |
15 /// Summary description for Unit. | |
16 /// </summary> | |
17 public class UnitType : WarFoundryObject | |
18 { | |
34 | 19 protected Category mainCat; |
0 | 20 protected Race race; |
21 protected int min, max, baseSize = 0; | |
22 protected int minSize, maxSize; | |
23 protected double baseUnitCost; | |
24 protected double costPerTrooper; | |
25 protected Stats stats; | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
26 protected List<UnitRequirement> requirements = new List<UnitRequirement>(); |
0 | 27 protected Hashtable equipment = new Hashtable(); |
28 protected Hashtable equipmentExclusionGroups = new Hashtable(); | |
29 protected ArrayList equipmentKeyOrder = new ArrayList(); | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
30 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
31 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
32 public UnitType(string id, string typeName, Race parentRace) : base(id, typeName) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
33 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
34 race = parentRace; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
35 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
36 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
37 [Obsolete("Use three parameter constructor and setters")] |
0 | 38 public UnitType(string id, string typeName, string mainCategoryID, string[] allCategoryIDs, int minNum, int maxNum, int minimumSize, int maximumSize, double unitCost, double trooperCost, Stats unitStats, UnitRequirement[] unitRequirements, Race parentRace) : base(id, typeName) |
39 { | |
40 race = parentRace; | |
34 | 41 mainCat = race.GetCategory(mainCategoryID); |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
42 MinNumber = minNum; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
43 MaxNumber = maxNum; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
44 MinSize = minimumSize; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
45 MaxSize = maximumSize; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
46 BaseUnitCost = unitCost; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
47 CostPerTrooper = trooperCost; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
48 UnitStats = unitStats; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
49 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
50 foreach (UnitRequirement requirement in requirements) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
51 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
52 AddRequirement(requirement); |
0 | 53 } |
54 } | |
55 | |
56 public Race Race | |
57 { | |
58 get { return race; } | |
59 } | |
60 | |
61 public virtual Category MainCategory | |
62 { | |
63 get | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
64 { |
0 | 65 return mainCat; |
66 } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
67 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
68 { |
34 | 69 mainCat = value; |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
70 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
71 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
72 |
0 | 73 public int MinSize |
74 { | |
75 get { return minSize; } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
76 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
77 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
78 minSize = (value >= 0 ? value : 0); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
79 CheckMinimumSize(); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
80 } |
0 | 81 } |
82 | |
83 public int MaxSize | |
84 { | |
85 get { return maxSize; } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
86 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
87 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
88 maxSize = (value >= -1 ? value : -1); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
89 CheckMinimumSize(); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
90 } |
0 | 91 } |
92 | |
93 public int BaseSize | |
94 { | |
95 get { return baseSize; } | |
96 } | |
97 | |
98 public int MinNumber | |
99 { | |
100 get { return min; } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
101 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
102 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
103 min = (value >= 0 ? value : 0); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
104 CheckMinimumNumber(); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
105 } |
0 | 106 } |
107 | |
108 public int MaxNumber | |
109 { | |
110 get { return max; } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
111 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
112 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
113 max = (value >= -1 ? value : -1); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
114 CheckMinimumNumber(); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
115 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
116 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
117 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
118 private void CheckMinimumNumber() |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
119 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
120 if (MinNumber > MaxNumber && MaxNumber!=-1) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
121 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
122 MinNumber = MaxNumber; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
123 LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", Name, ID); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
124 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
125 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
126 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
127 private void CheckMinimumSize() |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
128 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
129 if (MinSize > MaxSize && MaxSize!=-1) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
130 { |
34 | 131 MinSize = MaxSize; |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
132 LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", Name, ID); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
133 } |
0 | 134 } |
135 | |
136 public double BaseUnitCost | |
137 { | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
138 get { return baseUnitCost; } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
139 set { baseUnitCost = (value >= 0 ? value : 0); } |
0 | 140 } |
141 | |
142 public double CostPerTrooper | |
143 { | |
144 get { return costPerTrooper; } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
145 set { costPerTrooper = (value >= 0 ? value : 0); } |
0 | 146 } |
147 | |
148 protected override string DefaultName() | |
149 { | |
150 throw new InvalidOperationException("Unit type with id "+id+" did not have a name specified"); | |
151 } | |
152 | |
153 public Stats UnitStats | |
154 { | |
155 get | |
156 { | |
157 return stats; | |
158 } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
159 set |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
160 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
161 if (value!=null) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
162 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
163 stats = value; |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
164 } |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
165 } |
0 | 166 } |
167 | |
168 public UnitEquipmentItem GetEquipmentItem(string id) | |
169 { | |
170 return (UnitEquipmentItem)equipment[id]; | |
171 } | |
172 | |
173 public UnitEquipmentItem[] GetEquipmentItems() | |
174 { | |
175 UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count]; | |
176 int i = 0; | |
177 | |
178 foreach (string itemID in equipmentKeyOrder) | |
179 { | |
180 items[i++] = (UnitEquipmentItem)equipment[itemID]; | |
181 } | |
182 | |
183 return items; | |
184 } | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
185 |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
186 public void AddRequirement(UnitRequirement requirement) |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
187 { |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
188 requirements.Add(requirement); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
189 } |
0 | 190 |
191 public List<FailedUnitRequirement> CanAddToArmy(Army army) | |
192 { | |
193 List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); | |
194 | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
195 if (requirements!=null && requirements.Count > 0) |
0 | 196 { |
197 foreach (UnitRequirement requirement in requirements) | |
198 { | |
199 FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanAddToWarFoundryObject(army); | |
200 | |
201 if (failure!=null) | |
202 { | |
203 failures.Add(failure); | |
204 } | |
205 } | |
206 } | |
207 | |
208 return failures; | |
209 } | |
210 | |
211 public List<FailedUnitRequirement> CanRemoveFromArmy(Army army) | |
212 { | |
213 List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>(); | |
214 | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
215 if (requirements!=null && requirements.Count > 0) |
0 | 216 { |
217 foreach (UnitRequirement requirement in requirements) | |
218 { | |
219 FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanRemoveFromWarFoundryObject(army); | |
220 | |
221 if (failure!=null) | |
222 { | |
223 failures.Add(failure); | |
224 } | |
225 } | |
226 } | |
227 | |
228 return failures; | |
229 } | |
230 | |
231 public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group) | |
232 { | |
233 ArrayList list = (ArrayList)equipmentExclusionGroups[group]; | |
234 | |
235 if (list == null) | |
236 { | |
237 return new UnitEquipmentItem[0]; | |
238 } | |
239 else | |
240 { | |
241 return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem)); | |
242 } | |
243 } | |
244 } | |
245 } |