# HG changeset patch # User IBBoard # Date 1323291693 0 # Node ID 206a45fdfa9ea8d16e23180d1af6ebde124fee20 # Parent 86725e88052e137ca3d583836ddc35d7cb764240 Re #350: Add requirement to allow N of unit for specific other units * Extend N for M requirement to start handling multiple unit types in an additive method * Extend requirement data object to handle multiple unit types diff -r 86725e88052e -r 206a45fdfa9e API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Sun Dec 04 20:40:31 2011 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Wed Dec 07 21:01:33 2011 +0000 @@ -68,20 +68,34 @@ foreach (UnitCountRequirementData limit in requiredTypes) { - int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType); + int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, unitType); if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount)) { - failures.Add(String.Format("{0} {1} for every {2} {3} (would have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount)); + string unitTypeList = GetUnitTypeList(limit); + failures.Add(String.Format("{0} {1} for every {2} {3} (would have {4} for {5})", limit.Count, unitTypeList, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount)); } } return failures; } + private string GetUnitTypeList(UnitCountRequirementData limit) + { + List namesList = new List(); + + foreach (UnitType unitType in limit.UnitTypes) + { + namesList.Add(unitType.Name); + } + + string[] names = namesList.ToArray(); + return String.Join(" or ", names); + } + private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount) { - int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject); + int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, wfObject); return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount); } @@ -118,6 +132,18 @@ return canAdd; } + private int GetUnitTypesCount(Army toArmy, UnitType[] unitTypes, WarFoundryObject wfObject) + { + int count = 0; + + foreach (UnitType unitType in unitTypes) + { + count += GetUnitTypeCount(toArmy, unitType, wfObject); + } + + return count; + } + private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) { return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); @@ -221,14 +247,20 @@ } /// - /// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType + /// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType. If multiple unit types + /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType). /// /// - /// The unit type to require. + /// The unit type or types to require. /// - public void AddUnitTypeRequirement(UnitType unitType) + public void AddUnitTypeRequirement(params UnitType[] unitTypes) { - AddUnitTypeRequirement(unitType, 1); + AddUnitTypeRequirement(1, unitTypes); + } + + private void AddUnitTypeRequirement(int minCount, params UnitType[] unitTypes) + { + requiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, 1)); } } } diff -r 86725e88052e -r 206a45fdfa9e API/Objects/Requirement/UnitCountRequirementData.cs --- a/API/Objects/Requirement/UnitCountRequirementData.cs Sun Dec 04 20:40:31 2011 +0000 +++ b/API/Objects/Requirement/UnitCountRequirementData.cs Wed Dec 07 21:01:33 2011 +0000 @@ -8,7 +8,7 @@ { public class UnitCountRequirementData { - private UnitType unitType; + private UnitType[] unitTypes; private int count; private int allows; @@ -17,16 +17,31 @@ //Do nothing special } - public UnitCountRequirementData(UnitType unitType, int count, int allows) + public UnitCountRequirementData(UnitType unitType, int count, int allows) : this(new UnitType[]{unitType}, count, allows) + { + //Do nothing special + } + + public UnitCountRequirementData(UnitType[] unitTypes, int count) : this(unitTypes, count, WarFoundryCore.INFINITY) { - this.unitType = unitType; + //Do nothing special + } + + public UnitCountRequirementData(UnitType[] unitTypes, int count, int allows) + { + this.unitTypes = unitTypes ?? new UnitType[0]; this.count = count; this.allows = allows; } public UnitType UnitType { - get { return unitType; } + get { return unitTypes.Length > 0 ? unitTypes[0] : null; } + } + + public UnitType[] UnitTypes + { + get { return unitTypes; } } public int Count