# HG changeset patch # User IBBoard # Date 1302120346 0 # Node ID acd390dba551eeec6972fb4e6452ce2e1d049b74 # Parent 407757e597f9c48d4f260d101aedf991e965f673 Re #27: Unit requirements * First draft of "Unit requires no more than X" requirement diff -r 407757e597f9 -r acd390dba551 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Mon Apr 04 19:45:38 2011 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Wed Apr 06 20:05:46 2011 +0000 @@ -36,7 +36,7 @@ /// /// The army to add the object to. /// - public Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) { Validation canAdd = Validation.Passed; diff -r 407757e597f9 -r acd390dba551 API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs Wed Apr 06 20:05:46 2011 +0000 @@ -0,0 +1,58 @@ +// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. +using System; +using System.Collections.Generic; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Objects.Requirement +{ + /// + /// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army. + /// + public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement + { + private UnitType requirementOnType; + + public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base() + { + 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) + { + return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; + } + + + private bool IsApplicable(WarFoundryObject toObject, Army toArmy) + { + return IsApplicable(toArmy) || IsApplicable(toObject); + } + + + private 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)); + } + } +} + diff -r 407757e597f9 -r acd390dba551 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Mon Apr 04 19:45:38 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Wed Apr 06 20:05:46 2011 +0000 @@ -16,7 +16,6 @@ false - v2.0 publish\ @@ -190,6 +189,7 @@ +