# HG changeset patch # User IBBoard # Date 1254841194 0 # Node ID 9ba56a8e5096322b6dbbc1d73d657791d09b39c4 # Parent 6902f49e16e08a134aaeef9961e4203db85a3e1e Re #198: Add slots with counts to units * Add initial API methods * Add value loading diff -r 6902f49e16e0 -r 9ba56a8e5096 api/Factories/Xml/WarFoundryXmlRaceFactory.cs --- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs Tue Oct 06 14:47:06 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs Tue Oct 06 14:59:54 2009 +0000 @@ -111,7 +111,8 @@ { string name = elem.GetAttribute("typeName"); UnitType type = new UnitType(id, name, parentRace); - LoadCoreValuesForUnitType(elem, type); + LoadCoreValuesForUnitType(elem, type); + LoadEquipmentSlotsForUnitType(elem, type); LoadEquipmentForUnitType(elem, type); LoadAbilitiesForUnitType(elem, type); LoadContainedUnitsForUnitType(elem, type); @@ -150,7 +151,17 @@ type.MainCategory = cat; XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats"); Stats unitStats = ParseUnitStats(statsElement, type.GameSystem); - type.SetUnitStats(unitStats); + type.SetUnitStats(unitStats); + } + + private void LoadEquipmentSlotsForUnitType(XmlElement elem, UnitType type) + { + foreach (XmlElement equip in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:equipmentSlots/race:equipmentSlot")) + { + string slotName = equip.GetAttribute("name"); + int slotLimit = XmlTools.GetIntValueFromAttribute(equip, "limit"); + type.AddEquipmentSlot(slotName, slotLimit); + } } private void LoadEquipmentForUnitType(XmlElement elem, UnitType type) @@ -187,7 +198,22 @@ mutexGroups = new string[0]; } - UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem, type, mutexGroups); + UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem, type, mutexGroups); + + string equipSlot = equip.GetAttribute("equipmentSlot"); + + if (equipSlot != "") + { + if (type.HasEquipmentSlot(equipSlot)) + { + unitEquipItem.SlotName = equipSlot; + } + else + { + throw new InvalidFileException("Attribute 'equipmentSlot' of unit equipment item " + id + " for " + type.Name + " was not a valid slot name"); + } + } + unitEquipItem.RoundNumberUp = equip.GetAttribute("roundDirection").Equals("up"); diff -r 6902f49e16e0 -r 9ba56a8e5096 api/Objects/UnitEquipmentItem.cs --- a/api/Objects/UnitEquipmentItem.cs Tue Oct 06 14:47:06 2009 +0000 +++ b/api/Objects/UnitEquipmentItem.cs Tue Oct 06 14:59:54 2009 +0000 @@ -10,230 +10,245 @@ { /// /// Summary description for UnitEquipmentItem. - /// - public class UnitEquipmentItem : WarFoundryObject - { - private EquipmentItem item; - private bool required; - private bool roundUp; - private int minNum; - private int maxNum; - private double minPercentage; - private double maxPercentage; - private double costMultiplier; - private RoundType roundType; - private string[] mutexGroups; - private UnitType unitType; - - public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor) : this(equipmentItem, equipmentFor, new string[0]) - { - //Do nothing extra - } - - public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor, params string[] mutexGroups) - { - item = equipmentItem; - unitType = equipmentFor; - this.mutexGroups = mutexGroups; - unitType.AddEquipmentItem(this); - } - - public override string Name - { - get - { - return item.Name; - } - set - { - base.Name = value; - } - } - - public override string ID { - get - { - return (EquipmentForUnit == null ? base.ID : EquipmentForUnit.ID) + EquipmentItemID; - } - set - { - base.ID = value; - } - } - - - public string EquipmentItemID - { - get { return item.ID; } - } - - public double Cost - { - get - { - return IBBMath.Round(EquipmentItem.Cost * CostMultiplier, CostRoundType); - } - } - - public double CostMultiplier - { - get { return costMultiplier; } - set - { - costMultiplier = value; - } - } - - public RoundType CostRoundType - { - get { return roundType; } - set - { - roundType = value; - } - } - - public bool IsRequired - { - get { return required; } - set { required = value; } - } - - public bool RoundNumberUp - { - get { return roundUp; } - set { roundUp = value; } - } - - [Obsolete("Use MutexGroups instead for greater flexibility")] - public string MutexGroup - { - get { return (mutexGroups.Length == 0 ? "" : mutexGroups[0]); } - } - - public String[] MutexGroups - { - get { return (string[]) mutexGroups.Clone(); } - } - - public UnitType EquipmentForUnit - { - get { return unitType; } - } - - public bool IsRatioLimit - { - get { return minPercentage!=100 || maxPercentage!=100; } - } - - public int MinNumber - { - get { return minNum; } - set - { - if (value >=0 || value == WarFoundryCore.INFINITY) - { - minNum = value; - } - //TODO: Check Min=0 || value == WarFoundryCore.INFINITY) - { - maxNum = value; - } - //TODO: Check Min=0 && value <= 100) - { - minPercentage = value; - } - //TODO: Check Min=0 && value <= 100) - { - maxPercentage = value; - } - //TODO: Check Min 1; - } - } - - public ArmourType ItemArmourType - { - get { return EquipmentItem.ItemArmourType; } - } - - public string Description - { - get { return EquipmentItem.Description; } - } - - public Race EquipmentForRace - { - get { return EquipmentItem.EquipmentForRace; } - } - - public bool CanBeUsedWithItem(EquipmentItem item) - { - return EquipmentItem.CanBeUsedWithItem(item); - } - - public bool CanBeUsedWithArmourType(ArmourType otherItemType) - { - return EquipmentItem.CanBeUsedWithArmourType(otherItemType); - } - - [Obsolete("Use UnitEquipmentUtil method instead")] - public bool IsMutuallyExclusive(UnitEquipmentItem item) - { - return UnitEquipmentUtil.ItemsAreMutuallyExclusive(this, item); - } + /// + public class UnitEquipmentItem : WarFoundryObject + { + private EquipmentItem item; + private bool required; + private bool roundUp; + private int minNum; + private int maxNum; + private double minPercentage; + private double maxPercentage; + private double costMultiplier; + private RoundType roundType; + private string[] mutexGroups; + private UnitType unitType; + private string slotName = ""; + + public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor) + : this(equipmentItem, equipmentFor, new string[0]) + { + //Do nothing extra + } + + public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor, params string[] mutexGroups) + { + item = equipmentItem; + unitType = equipmentFor; + this.mutexGroups = mutexGroups; + unitType.AddEquipmentItem(this); + } + + public override string Name + { + get + { + return item.Name; + } + set + { + base.Name = value; + } + } + + public override string ID + { + get + { + return (EquipmentForUnit == null ? base.ID : EquipmentForUnit.ID) + EquipmentItemID; + } + set + { + base.ID = value; + } + } + + + public string EquipmentItemID + { + get { return item.ID; } + } + + public double Cost + { + get + { + return IBBMath.Round(EquipmentItem.Cost * CostMultiplier, CostRoundType); + } + } + + public double CostMultiplier + { + get { return costMultiplier; } + set + { + costMultiplier = value; + } + } + + public RoundType CostRoundType + { + get { return roundType; } + set + { + roundType = value; + } + } + + public bool IsRequired + { + get { return required; } + set { required = value; } + } + + public bool RoundNumberUp + { + get { return roundUp; } + set { roundUp = value; } + } + + [Obsolete("Use MutexGroups instead for greater flexibility")] + public string MutexGroup + { + get { return (mutexGroups.Length == 0 ? "" : mutexGroups[0]); } + } + + public String[] MutexGroups + { + get { return (string[]) mutexGroups.Clone(); } + } + + public UnitType EquipmentForUnit + { + get { return unitType; } + } + + public bool IsRatioLimit + { + get { return minPercentage != 100 || maxPercentage != 100; } + } + + public int MinNumber + { + get { return minNum; } + set + { + if (value >= 0 || value == WarFoundryCore.INFINITY) + { + minNum = value; + } + //TODO: Check Min= 0 || value == WarFoundryCore.INFINITY) + { + maxNum = value; + } + //TODO: Check Min= 0 && value <= 100) + { + minPercentage = value; + } + //TODO: Check Min= 0 && value <= 100) + { + maxPercentage = value; + } + //TODO: Check Min 1; + } + } + + public ArmourType ItemArmourType + { + get { return EquipmentItem.ItemArmourType; } + } + + public string Description + { + get { return EquipmentItem.Description; } + } + + public Race EquipmentForRace + { + get { return EquipmentItem.EquipmentForRace; } + } + + public bool CanBeUsedWithItem(EquipmentItem item) + { + return EquipmentItem.CanBeUsedWithItem(item); + } + + public bool CanBeUsedWithArmourType(ArmourType otherItemType) + { + return EquipmentItem.CanBeUsedWithArmourType(otherItemType); + } + + [Obsolete("Use UnitEquipmentUtil method instead")] + public bool IsMutuallyExclusive(UnitEquipmentItem item) + { + return UnitEquipmentUtil.ItemsAreMutuallyExclusive(this, item); + } + + public string SlotName + { + get { return slotName; } + set + { + if (value != null && value != "") + { + slotName = value; + } + } + } } } diff -r 6902f49e16e0 -r 9ba56a8e5096 api/Objects/UnitType.cs --- a/api/Objects/UnitType.cs Tue Oct 06 14:47:06 2009 +0000 +++ b/api/Objects/UnitType.cs Tue Oct 06 14:59:54 2009 +0000 @@ -29,8 +29,9 @@ private Dictionary requiredAbilities = new Dictionary(); private Dictionary optionalAbilities = new Dictionary(); private String notes = ""; - private List containedTypes = new List(); - private Dictionary extraData = new Dictionary(); + private List containedTypes = new List(); + private Dictionary extraData = new Dictionary(); + private Dictionary slotLimits = new Dictionary(); public UnitType(string id, string typeName, Race parentRace) : base(id, typeName) @@ -446,7 +447,34 @@ get { return stats.StatsID; - } + } + } + + public void AddEquipmentSlot(string slotName, int slotLimit) + { + slotLimits.Add(slotName, slotLimit); + } + + public bool HasEquipmentSlot(string slotName) + { + return slotLimits.ContainsKey(slotName); + } + + /// + /// Gets the maximum number of items allowed in a single slot + /// + /// The name of the equipment slot to get the limit for the named slot + /// The maximum number of items allowed in a slot, or -1 if the slot is the default one or has not been specified + public int GetEquipmentSlotLimit(string slotName) + { + int slotLimit = -1; + + if (HasEquipmentSlot(slotName)) + { + slotLimit = DictionaryUtils.GetValue(slotLimits, slotName); + } + + return slotLimit; } } } \ No newline at end of file