Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Objects/Unit.cs @ 61:3c77722a02b5
Re #61 - Complete structure of WarFoundry API objects
* Add containership support to Unit class
* Add method to UnitType to check if a UnitType is of an allowed type
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 09 Apr 2009 14:17:53 +0000 |
parents | 3a90f70dac73 |
children | 2094bd0ba652 |
line wrap: on
line source
// This file (Unit.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. // // 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. using System; using System.Collections.Generic; using System.Text; using System.Xml; using IBBoard.Lang; namespace IBBoard.WarFoundry.API.Objects { /// <summary> /// Summary description for UnitInstance. /// </summary> public class Unit : WarFoundryObject { private UnitType type; private int size; private Army army; private Race race; private double points; private ArmyCategory cat; private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); private List<Unit> containedUnits = new List<Unit>(); public event DoubleValChangedDelegate PointsValueChanged; public event IntValChangedDelegate UnitSizeChanged; public event DoubleValChangedDelegate UnitEquipmentAmountChanged; public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){} public Unit(UnitType unitType, int startSize, Army parentArmy) { Army = parentArmy; type = unitType; Size = startSize; setInitialEquipment(); CalcCost(); } protected override string DefaultName() { if (type != null) { if (size == 1) { return type.Name; } else { return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name); } } else { return "Unknown Unit"; } } private void setInitialEquipment() { foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) { if (unitEquip.IsRequired) { if (CanEquipWithItem(unitEquip.ID)) { equipment[unitEquip] = unitEquip.MinNumber; } } } } private void CalcCost() { String oldName = HasDefaultName() ? Name : null; double oldpoints = points; points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) { EquipmentItem equipItem = unitEquipItem.EquipmentItem; double count = (double)equipment[unitEquipItem]; if (unitEquipItem.IsRatioLimit) { if (unitEquipItem.RoundNumberUp) { points+= Math.Ceiling(size * count) * equipItem.Cost; } else { points+= Math.Floor(size * count) * equipItem.Cost; } } else { if (count == WarFoundryCore.INFINITY) { points+= size * equipItem.Cost; } else { points+= count * equipItem.Cost; } } } if (oldpoints!=points) { OnPointsValueChanged(oldpoints, points); } if (oldName!=null) { OnNameChanged(oldName, Name); } } public int AdditionalTroopers { get { return Math.Max(Size - type.BaseSize, 0); } } public int Size { get { return size; } set { if (value!=size) { int oldValue = size; size = (value>0 ? value : 1); CalcCost(); OnUnitSizeChanged(oldValue, size); } } } public UnitType UnitType { get { return type; } } public Army Army { get { return army; } set { army = value; if (army == null) { Category = null; } } } public Race Race { get { return race; } set { race = value; } } public ArmyCategory Category { get { if (cat==null) { if (Army!=null) { return Army.GetCategory(UnitType.MainCategory); } else { return null; } } else { return cat; } } set { cat = value; } } public double PointsValue { get { if (points == 0) { CalcCost(); } return points; } } public Unit[] ContainedUnits { get { return containedUnits.ToArray(); } } public void AddContainedUnit(Unit unit) { if (UnitType.CanContainUnit(unit)) { if (!containedUnits.Contains(unit)) { containedUnits.Add(unit); } } else { throw new InvalidContainershipException(this, unit); } } public void RemoveContainedUnit(Unit unit) { containedUnits.Remove(unit); } public UnitEquipmentItem[] GetAllowedOptionalEquipment() { List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems()) { if (!item.IsRequired) { list.Add(item); } } return (UnitEquipmentItem[])list.ToArray(); } public UnitEquipmentItem[] GetEquipment() { UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count]; int i = 0; foreach (UnitEquipmentItem item in equipment.Keys) { items[i++] = item; } return items; } public UnitEquipmentItem[] GetRequiredEquipment() { List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); foreach(UnitEquipmentItem item in equipment.Keys) { if (item.IsRequired) { list.Add(item); } } return (UnitEquipmentItem[])list.ToArray(); } public double GetEquipmentAmount(UnitEquipmentItem item) { if (equipment.ContainsKey(item)) { return (double)equipment[item]; } else { return 0; } } public double GetEquipmentAmount(string equipID) { return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); } public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) { if (equip.IsRatioLimit) { throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has a ratio limit, not an absolute limit"); } if (amount <1 && amount != WarFoundryCore.INFINITY) { amount = 0; } SetEquipmentAmount(equip, amount); } public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) { if (!equip.IsRatioLimit) { throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has an absolute limit, not a ratio limit"); } if (ratio > 1) { ratio = 1; } else if (ratio < 0) { ratio = 0; } SetEquipmentAmount(equip, ratio); } private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) { double oldAmount = 0; if (equipment.ContainsKey(equip)) { oldAmount = equipment[equip]; } if (amount!=oldAmount) { if (amount != 0) { equipment[equip] = amount; } else { equipment.Remove(equip); } OnUnitEquipmentAmountChanged(equip, oldAmount, amount); CalcCost(); } } public bool CanEquipWithItem(string equipID) { string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup; if (mutex == "") { return true; } foreach (UnitEquipmentItem item in equipment.Keys) { if (item.MutexGroup == mutex) { return false; } } return true; } private void OnPointsValueChanged(double oldValue, double newValue) { if (PointsValueChanged!=null) { PointsValueChanged(this, oldValue, newValue); } } private void OnUnitSizeChanged(int oldValue, int newValue) { if (UnitSizeChanged!=null) { UnitSizeChanged(this, oldValue, newValue); } } private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) { if (UnitEquipmentAmountChanged!=null) { UnitEquipmentAmountChanged(equip, oldValue, newValue); } } public Stats UnitStats { get { return UnitType.UnitStats; } } } }