# HG changeset patch # User IBBoard # Date 1330287393 0 # Node ID 680db2462e34827b3b21e87fb364b4e25a2f687e # Parent 8e01c3174cc34758b343cb92d01da34ba905b48c Re #379: * Move GetObjectCountFromArmy(Army, OBJECT_TYPE) to top level and implement * Fix army validation for appropriate "NA" returns * Make basic "Requires..." requirements abstract so that we always need to make specific versions (e.g. UnitRequires... that knows how to check amount of units) diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/AbstractUnitRequirement.cs --- a/API/Objects/Requirement/AbstractUnitRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -153,6 +153,8 @@ protected abstract int GetObjectCountFromArmy(Army toArmy); + protected abstract int GetObjectCountFromArmy(Army toArmy, OBJECT_TYPE obj); + protected virtual int GetObjectCountFromObject(IWarFoundryObject wfObject) { return allowedObject.Equals(wfObject) ? 1 : 0; diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -13,7 +13,7 @@ /// /// The definition for how this requirement is built from a data file is defined in the class. /// - public class RequiresAtLeastNUnitsRequirement : AbstractUnitRequirement where OBJECT_TYPE : IWarFoundryObject + public abstract class RequiresAtLeastNUnitsRequirement : AbstractUnitRequirement where OBJECT_TYPE : IWarFoundryObject { public static readonly string REQUIREMENT_ID = "RequiresAtLeastNUnits"; @@ -156,16 +156,26 @@ /// public override Validation ValidatesArmy(Army toValidate) { - Validation isValid = Validation.Passed; + Validation isValid = Validation.NotApplicable; + int allowedTypeCount = GetObjectCountFromArmy(toValidate, AllowedObject); - foreach (UnitCountRequirementData requirement in ConstraintTypes) + if (allowedTypeCount > 0) { - if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) + isValid = Validation.Passed; + + foreach (UnitCountRequirementData requirement in ConstraintTypes) { - isValid = Validation.Failed; - break; + if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) + { + isValid = Validation.Failed; + break; + } } } + else + { + isValid = Validation.NotApplicable; + } return isValid; } diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -132,8 +132,6 @@ return GetObjectCountFromArmy(toArmy, obj) + GetCountFromObject(wfObject, obj); } - protected abstract int GetObjectCountFromArmy(Army toArmy, OBJECT_TYPE obj); - private int GetCountFromObject(IWarFoundryObject wfObject, OBJECT_TYPE limitedType) { return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -11,7 +11,7 @@ /// /// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army. /// - public class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractUnitRequirement where OBJECT_TYPE : IWarFoundryObject + public abstract class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractUnitRequirement where OBJECT_TYPE : IWarFoundryObject { public static readonly string REQUIREMENT_ID = "RequiresNoMoreThanNUnits"; @@ -102,16 +102,26 @@ /// public override Validation ValidatesArmy(Army army) { - Validation canAdd = Validation.Passed; - - foreach (UnitCountRequirementData limit in ConstraintTypes) + Validation canAdd = Validation.NotApplicable; + int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject); + + if (allowedTypeCount > 0) { - if (army.GetUnitTypeCount(limit.UnitType) > limit.Count) + canAdd = Validation.Passed; + + foreach (UnitCountRequirementData limit in ConstraintTypes) { - canAdd = Validation.Failed; - break; + if (army.GetUnitTypeCount(limit.UnitType) > limit.Count) + { + canAdd = Validation.Failed; + break; + } } } + else + { + canAdd = Validation.NotApplicable; + } return canAdd; } diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -16,24 +16,49 @@ { } - protected override bool IsApplicable(IWarFoundryObject toObjectAdded) + public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) + { + return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) { - return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded); + return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); + } + + protected override bool IsApplicable(IWarFoundryObject toObject) + { + return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); } - private bool IsRequirementOnType(IWarFoundryObject toObjectAdded) + private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy) { - return AllowedObject.Equals(toObjectAdded) || (toObjectAdded is Unit && AllowedObject.Equals(((Unit)toObjectAdded).UnitType)); + bool isApplicable = false; + UnitType addedType = toObject as UnitType; + + if (addedType == null) + { + addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; + } + + if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) + { + foreach (UnitCountRequirementData limit in ConstraintTypes) + { + if (Arrays.Contains(limit.UnitTypes, addedType)) + { + isApplicable = true; + break; + } + } + } + + return isApplicable; } - protected override bool IsApplicable(Army toArmy) + protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj) { - return toArmy.GetUnitTypeCount(AllowedObject) > 0; - } - - public override Validation ValidatesArmy(Army toArmy) - { - return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable; + return toArmy.GetUnitTypeCount(obj); } } } diff -r 8e01c3174cc3 -r 680db2462e34 API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Feb 26 15:14:01 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Sun Feb 26 20:16:33 2012 +0000 @@ -68,6 +68,11 @@ return isApplicable; } + + protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj) + { + return toArmy.GetUnitTypeCount(obj); + } } }