# HG changeset patch # User IBBoard # Date 1302379060 0 # Node ID 6c0404277cad11ef22396df8342d940adddb2db6 # Parent 12a56786120c325e64ed733dcc4eb243180314ed Re #27: Unit requirements * Rebuild base "at least" requirement to say "N/A" when added unit isn't constrained * Remove duplicate code from "unit requires at least" requirement and override where necessary diff -r 12a56786120c -r 6c0404277cad API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:14:11 2011 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:57:40 2011 +0000 @@ -38,8 +38,66 @@ /// public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) { + return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy) + { + return IsApplicable(toArmy) || IsApplicable(toObjectAdded); + } + + protected virtual bool IsApplicable(Army toArmy) + { + return false; + } + + protected virtual bool IsApplicable(WarFoundryObject toObject) + { + bool isApplicable = false; + UnitType unitType = GetUnitTypeFromObject(toObject); + + if (unitType != null) + { + isApplicable = IsApplicable(unitType); + } + + return isApplicable; + } + + protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject) + { + UnitType unitType = null; + + if (toObject is UnitType) + { + unitType = (UnitType)toObject; + } + else if (toObject is Unit) + { + unitType = ((Unit)toObject).UnitType; + } + + return unitType; + } + + private bool IsApplicable (UnitType unitType) + { + bool isApplicable = false; + foreach (UnitCountRequirementData requirement in requiredTypes) + { + if (requirement.UnitType.Equals(unitType)) + { + isApplicable = true; + break; + } + } + return isApplicable; + } + + private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy) + { Validation isValid = Validation.Passed; - + foreach (UnitCountRequirementData requirement in requiredTypes) { if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) @@ -48,7 +106,7 @@ break; } } - + return isValid; } diff -r 12a56786120c -r 6c0404277cad API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:14:11 2011 +0000 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sat Apr 09 19:57:40 2011 +0000 @@ -19,42 +19,21 @@ requirementOnType = requirementOn; } - /// - /// Checks whether the supplied WarFoundryObject can be added to the supplied army. - /// - /// - /// A Validation enum to show the result of the validation - /// - /// - /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement - /// - /// - /// The army to add the object to. - /// - public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + protected override bool IsApplicable(WarFoundryObject toObjectAdded) { - return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded); } - - private bool IsApplicable(WarFoundryObject toObject, Army toArmy) + private bool IsRequirementOnType(WarFoundryObject toObjectAdded) { - return IsApplicable(toArmy) || IsApplicable(toObject); + return requirementOnType.Equals(toObjectAdded) || (toObjectAdded is Unit && requirementOnType.Equals(((Unit)toObjectAdded).UnitType)); } - - private bool IsApplicable(Army toArmy) + protected override bool IsApplicable(Army toArmy) { return toArmy.GetUnitTypeCount(requirementOnType) > 0; } - - private bool IsApplicable(WarFoundryObject toObject) - { - return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType)); - } - - public override Validation ValidatesArmy(Army toArmy) { return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;