| 1 | // This file (Unit.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 | |
|---|
| 5 | using System; |
|---|
| 6 | using System.Collections.Generic; |
|---|
| 7 | using System.Text; |
|---|
| 8 | using System.Xml; |
|---|
| 9 | using IBBoard.Lang; |
|---|
| 10 | |
|---|
| 11 | namespace IBBoard.WarFoundry.API.Objects |
|---|
| 12 | { |
|---|
| 13 | /// <summary> |
|---|
| 14 | /// Summary description for UnitInstance. |
|---|
| 15 | /// </summary> |
|---|
| 16 | public class Unit : WarFoundryObject |
|---|
| 17 | { |
|---|
| 18 | private UnitType type; |
|---|
| 19 | private int size; |
|---|
| 20 | private Army army; |
|---|
| 21 | private Unit parentUnit; |
|---|
| 22 | private double points; |
|---|
| 23 | private ArmyCategory cat; |
|---|
| 24 | private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); |
|---|
| 25 | private List<Unit> containedUnits = new List<Unit>(); |
|---|
| 26 | public event DoubleValChangedDelegate PointsValueChanged; |
|---|
| 27 | public event IntValChangedDelegate UnitSizeChanged; |
|---|
| 28 | public event DoubleValChangedDelegate UnitEquipmentAmountChanged; |
|---|
| 29 | |
|---|
| 30 | public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){} |
|---|
| 31 | |
|---|
| 32 | public Unit(UnitType unitType, int startSize, Army parentArmy) |
|---|
| 33 | { |
|---|
| 34 | Army = parentArmy; |
|---|
| 35 | type = unitType; |
|---|
| 36 | Size = startSize; |
|---|
| 37 | SetInitialEquipment(); |
|---|
| 38 | CalcCost(); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | protected override string DefaultName() |
|---|
| 42 | { |
|---|
| 43 | if (type != null) |
|---|
| 44 | { |
|---|
| 45 | if (size == 1) |
|---|
| 46 | { |
|---|
| 47 | return type.Name; |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | else |
|---|
| 55 | { |
|---|
| 56 | return "Unknown Unit"; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | private void SetInitialEquipment() |
|---|
| 61 | { |
|---|
| 62 | foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) |
|---|
| 63 | { |
|---|
| 64 | if (unitEquip.IsRequired) |
|---|
| 65 | { |
|---|
| 66 | if (CanEquipWithItem(unitEquip)) |
|---|
| 67 | { |
|---|
| 68 | equipment[unitEquip] = unitEquip.MinNumber; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | private void CalcCost() |
|---|
| 75 | { |
|---|
| 76 | String oldName = HasDefaultName() ? Name : null; |
|---|
| 77 | double oldpoints = points; |
|---|
| 78 | points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; |
|---|
| 79 | |
|---|
| 80 | foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) |
|---|
| 81 | { |
|---|
| 82 | double count = equipment[unitEquipItem]; |
|---|
| 83 | |
|---|
| 84 | if (unitEquipItem.IsRatioLimit) |
|---|
| 85 | { |
|---|
| 86 | if (unitEquipItem.RoundNumberUp) |
|---|
| 87 | { |
|---|
| 88 | points += Math.Ceiling(size * count) * unitEquipItem.Cost; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | points += Math.Floor(size * count) * unitEquipItem.Cost; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | else |
|---|
| 96 | { |
|---|
| 97 | if (count == WarFoundryCore.INFINITY) |
|---|
| 98 | { |
|---|
| 99 | points += size * unitEquipItem.Cost; |
|---|
| 100 | } |
|---|
| 101 | else |
|---|
| 102 | { |
|---|
| 103 | points += count * unitEquipItem.Cost; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if (oldpoints!=points) |
|---|
| 109 | { |
|---|
| 110 | OnPointsValueChanged(oldpoints, points); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (oldName!=null) |
|---|
| 114 | { |
|---|
| 115 | OnNameChanged(oldName, Name); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public int AdditionalTroopers |
|---|
| 120 | { |
|---|
| 121 | get { return Math.Max(Size - type.BaseSize, 0); } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public int Size |
|---|
| 125 | { |
|---|
| 126 | get { return size; } |
|---|
| 127 | set |
|---|
| 128 | { |
|---|
| 129 | if (value!=size) |
|---|
| 130 | { |
|---|
| 131 | int oldValue = size; |
|---|
| 132 | size = (value>0 ? value : 1); |
|---|
| 133 | CalcCost(); |
|---|
| 134 | OnUnitSizeChanged(oldValue, size); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | public UnitType UnitType |
|---|
| 140 | { |
|---|
| 141 | get { return type; } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public Army Army |
|---|
| 145 | { |
|---|
| 146 | get { return army; } |
|---|
| 147 | set |
|---|
| 148 | { |
|---|
| 149 | army = value; |
|---|
| 150 | |
|---|
| 151 | if (army == null) |
|---|
| 152 | { |
|---|
| 153 | Category = null; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public Race Race |
|---|
| 159 | { |
|---|
| 160 | get { return UnitType.Race; } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public ArmyCategory Category |
|---|
| 164 | { |
|---|
| 165 | get |
|---|
| 166 | { |
|---|
| 167 | if (cat==null) |
|---|
| 168 | { |
|---|
| 169 | if (Army!=null) |
|---|
| 170 | { |
|---|
| 171 | return Army.GetCategory(UnitType.MainCategory); |
|---|
| 172 | } |
|---|
| 173 | else |
|---|
| 174 | { |
|---|
| 175 | return null; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | else |
|---|
| 179 | { |
|---|
| 180 | return cat; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | set { cat = value; } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | public double PointsValue |
|---|
| 187 | { |
|---|
| 188 | get |
|---|
| 189 | { |
|---|
| 190 | if (points == 0) |
|---|
| 191 | { |
|---|
| 192 | CalcCost(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | return points; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public Unit[] ContainedUnits |
|---|
| 200 | { |
|---|
| 201 | get { return containedUnits.ToArray(); } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | public void AddContainedUnit(Unit unit) |
|---|
| 205 | { |
|---|
| 206 | if (UnitType.CanContainUnit(unit)) |
|---|
| 207 | { |
|---|
| 208 | if (!containedUnits.Contains(unit)) |
|---|
| 209 | { |
|---|
| 210 | containedUnits.Add(unit); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | unit.ParentUnit = this; |
|---|
| 214 | } |
|---|
| 215 | else |
|---|
| 216 | { |
|---|
| 217 | throw new InvalidContainershipException(this, unit); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | public void RemoveContainedUnit(Unit unit) |
|---|
| 222 | { |
|---|
| 223 | containedUnits.Remove(unit); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | public Unit ParentUnit |
|---|
| 227 | { |
|---|
| 228 | get { return parentUnit; } |
|---|
| 229 | set |
|---|
| 230 | { |
|---|
| 231 | if (!(parentUnit == value || (parentUnit != null && parentUnit.Equals(value)))) |
|---|
| 232 | { |
|---|
| 233 | parentUnit = value; |
|---|
| 234 | |
|---|
| 235 | if (value!=null) |
|---|
| 236 | { |
|---|
| 237 | value.AddContainedUnit(this); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | public UnitEquipmentItem[] GetAllowedOptionalEquipment() |
|---|
| 244 | { |
|---|
| 245 | List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); |
|---|
| 246 | |
|---|
| 247 | foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems()) |
|---|
| 248 | { |
|---|
| 249 | if (!item.IsRequired) |
|---|
| 250 | { |
|---|
| 251 | list.Add(item); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | return list.ToArray(); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | public UnitEquipmentItem[] GetEquipment() |
|---|
| 259 | { |
|---|
| 260 | return DictionaryUtils.ToKeyArray(equipment); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | public EquipmentItem[] GetRequiredEquipment() |
|---|
| 264 | { |
|---|
| 265 | List<EquipmentItem> list = new List<EquipmentItem>(); |
|---|
| 266 | |
|---|
| 267 | foreach(UnitEquipmentItem item in GetEquipment()) |
|---|
| 268 | { |
|---|
| 269 | if (item.IsRequired) |
|---|
| 270 | { |
|---|
| 271 | list.Add(item.EquipmentItem); |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | return list.ToArray(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | public double GetEquipmentAmount(UnitEquipmentItem item) |
|---|
| 279 | { |
|---|
| 280 | return GetEquipmentAmount(item.ID); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | public double GetEquipmentAmount(string equipID) |
|---|
| 284 | { |
|---|
| 285 | return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) |
|---|
| 289 | { |
|---|
| 290 | if (equip.IsRatioLimit) |
|---|
| 291 | { |
|---|
| 292 | throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit"); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | if (amount <1 && amount != WarFoundryCore.INFINITY) |
|---|
| 296 | { |
|---|
| 297 | amount = 0; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | SetEquipmentAmount(equip, amount); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) |
|---|
| 304 | { |
|---|
| 305 | if (!equip.IsRatioLimit) |
|---|
| 306 | { |
|---|
| 307 | throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | if (ratio > 1) |
|---|
| 311 | { |
|---|
| 312 | ratio = 1; |
|---|
| 313 | } |
|---|
| 314 | else if (ratio < 0) |
|---|
| 315 | { |
|---|
| 316 | ratio = 0; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | SetEquipmentAmount(equip, ratio); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) |
|---|
| 323 | { |
|---|
| 324 | double oldAmount = 0; |
|---|
| 325 | |
|---|
| 326 | if (equipment.ContainsKey(equip)) |
|---|
| 327 | { |
|---|
| 328 | oldAmount = equipment[equip]; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | if (amount!=oldAmount) |
|---|
| 332 | { |
|---|
| 333 | if (amount != 0) |
|---|
| 334 | { |
|---|
| 335 | equipment[equip] = amount; |
|---|
| 336 | } |
|---|
| 337 | else |
|---|
| 338 | { |
|---|
| 339 | equipment.Remove(equip); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | OnUnitEquipmentAmountChanged(equip, oldAmount, amount); |
|---|
| 343 | CalcCost(); |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | public bool CanEquipWithItem(UnitEquipmentItem item) |
|---|
| 348 | { |
|---|
| 349 | string mutex = item.MutexGroup; |
|---|
| 350 | |
|---|
| 351 | if (mutex == "") |
|---|
| 352 | { |
|---|
| 353 | return true; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | foreach (UnitEquipmentItem unitItem in GetEquipment()) |
|---|
| 357 | { |
|---|
| 358 | if (unitItem.MutexGroup == mutex) |
|---|
| 359 | { |
|---|
| 360 | return false; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | return true; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | public bool CanEquipWithItem(string equipID) |
|---|
| 368 | { |
|---|
| 369 | return CanEquipWithItem(UnitType.GetEquipmentItem(equipID)); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | private void OnPointsValueChanged(double oldValue, double newValue) |
|---|
| 373 | { |
|---|
| 374 | if (PointsValueChanged!=null) |
|---|
| 375 | { |
|---|
| 376 | PointsValueChanged(this, oldValue, newValue); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | private void OnUnitSizeChanged(int oldValue, int newValue) |
|---|
| 381 | { |
|---|
| 382 | if (UnitSizeChanged!=null) |
|---|
| 383 | { |
|---|
| 384 | UnitSizeChanged(this, oldValue, newValue); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) |
|---|
| 389 | { |
|---|
| 390 | if (UnitEquipmentAmountChanged!=null) |
|---|
| 391 | { |
|---|
| 392 | UnitEquipmentAmountChanged(equip, oldValue, newValue); |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | public Stat[] UnitStatsArray |
|---|
| 397 | { |
|---|
| 398 | get { return UnitType.UnitStatsArray; } |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | public Stat[] UnitStatsArrayWithName |
|---|
| 402 | { |
|---|
| 403 | get { return UnitType.UnitStatsArrayWithName; } |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | public string GetStatValue(string statName) |
|---|
| 407 | { |
|---|
| 408 | return UnitType.GetStatValue(statName); |
|---|
| 409 | } |
|---|
| 410 | } |
|---|
| 411 | } |
|---|