# HG changeset patch # User IBBoard # Date 1324415013 0 # Node ID 4fbb7f205f7e4877f50c2a954cd0793b7a1e93ac # Parent ca1e8f7c8b7308e617a94185434d143fb1aff4db Re #350: Add requirement to allow N of unit for specific other units * Extend factory to support additive unit types * Add three parameter method to requirement to set min, allowed and additive unit types Note: Naughty code (untested) added to requirement to make factory compile! Needs testing. diff -r ca1e8f7c8b73 -r 4fbb7f205f7e API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs --- a/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs Tue Dec 20 20:26:54 2011 +0000 +++ b/API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactory.cs Tue Dec 20 21:03:33 2011 +0000 @@ -4,6 +4,7 @@ using System; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Objects.Requirement; +using System.Collections.Generic; namespace IBBoard.WarFoundry.API.Factories.Requirement { @@ -35,13 +36,8 @@ foreach (string requirement in data.Split('|')) { string[] requirementParts = requirement.Split(':'); - string unitID = requirementParts[0]; - UnitType unitType = raceFactory.GetUnitType(unitID, race); - - if (unitType == null) - { - throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID)); - } + string unitIDs = requirementParts[0]; + UnitType[] unitTypes = GetUnitTypes(unitIDs, race, raceFactory); if (requirementParts.Length == 2) { @@ -53,23 +49,42 @@ if (amounts.Length == 1) { minCount = 1; - allowedAmounts = ParseAllowedAmount(amounts[0], unitID); + allowedAmounts = ParseAllowedAmount(amounts[0], unitIDs); } else { - minCount = ParseMinCount(amounts[0], unitID); - allowedAmounts = ParseAllowedAmount(amounts[1], unitID); + minCount = ParseMinCount(amounts[0], unitIDs); + allowedAmounts = ParseAllowedAmount(amounts[1], unitIDs); } - req.AddUnitTypeRequirement(unitType, minCount, allowedAmounts); + req.AddUnitTypeRequirement(minCount, allowedAmounts, unitTypes); } else { - req.AddUnitTypeRequirement(unitType); + req.AddUnitTypeRequirement(unitTypes); } } } + private UnitType[] GetUnitTypes(string data, Race race, IRaceFactory raceFactory) + { + List unitTypes = new List(); + + foreach (string unitID in data.Split(';')) + { + UnitType unitType = raceFactory.GetUnitType(unitID, race); + + if (unitType == null) + { + throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires N units for M units' requirement", unitID)); + } + + unitTypes.Add(unitType); + } + + return unitTypes.ToArray(); + } + private int ParseMinCount(string minCount, string unitID) { try @@ -78,7 +93,7 @@ } catch (FormatException) { - throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", minCount, unitID)); + throw new InvalidRequirementException(String.Format("Invalid minimum amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", minCount, unitID)); } } @@ -90,7 +105,7 @@ } catch (FormatException) { - throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit type '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID)); + throw new InvalidRequirementException(String.Format("Invalid allowed amount '{0}' for unit types '{1}' for 'Requires N units for M units' requirement", allowedAmount, unitID)); } } } diff -r ca1e8f7c8b73 -r 4fbb7f205f7e API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Tue Dec 20 20:26:54 2011 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Tue Dec 20 21:03:33 2011 +0000 @@ -278,6 +278,24 @@ { requiredTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount)); } + + /// + /// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing allowsAmount 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 minimum number of that type that must exist. + /// + /// + /// The number of units to be allowed for each 1 of unitType + /// + /// + /// The unit type or types to require. + /// + public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes) + { + requiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount)); + } } }