# HG changeset patch # User IBBoard # Date 1322600121 0 # Node ID 0922851f6125558595522cd3be4552942a36c713 # Parent dc842b41adfdb894b982ff89b144c0d79d5f9ee4 Re #350: Add requirement to allow N of unit for specific other units * Extend unit count requirement data to handle fixed numbers of units allowed per unit required * Add initial stub of requirement using some copy-and-paste, but starting to avoid ticket:379 diff -r dc842b41adfd -r 0922851f6125 API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs Tue Nov 29 20:55:21 2011 +0000 @@ -0,0 +1,142 @@ +// This file(RequiresNUnitsForMUnitsRequirement.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; + +namespace IBBoard.WarFoundry.API.Objects.Requirement +{ + public class RequiresNUnitsForMUnitsRequirement : AbstractRequirement + { + public static readonly string REQUIREMENT_ID = "RequiresNUnitsForMUnits"; + private List requiredTypes; + private UnitType allowedType; + + public RequiresNUnitsForMUnitsRequirement(UnitType allowedType, params UnitType[] requiredUnitTypes) + { + this.allowedType = allowedType; + FailureStringPrefix = "Army must contain: "; + requiredTypes = new List(); + + foreach (UnitType unitType in requiredUnitTypes) + { + AddUnitTypeRequirement(unitType); + } + } + + public override int GetHashCode() + { + int hash = 0; + + foreach (UnitCountRequirementData req in requiredTypes) + { + hash += req.UnitType.GetHashCode(); + } + + return hash; + } + + protected override bool TypeEquals(object obj) + { + RequiresNUnitsForMUnitsRequirement otherReq = (RequiresNUnitsForMUnitsRequirement)obj; + if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes)) + { + return false; + } + else + { + return true; + } + } + + protected string FailureStringPrefix { get; set; } + + protected override string GetValidationFailedMessage(Army army) + { + return ""; + } + + protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy) + { + return ""; + } + + public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) + { + Validation canAdd = Validation.NotApplicable; + UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType; + bool typeFound = (addedUnitType == allowedType); + int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, wfObject); + + foreach (UnitCountRequirementData limit in requiredTypes) + { + typeFound |= (addedUnitType == limit.UnitType); + int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject); + double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0); + double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0); + + if (allowedTypeMultiplier > limitedTypeMultiplier) + { + canAdd = Validation.Failed; + break; + } + } + + if (typeFound && canAdd == Validation.NotApplicable) + { + canAdd = Validation.Passed; + } + + return canAdd; + } + + private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) + { + return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); + } + + private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType) + { + return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; + } + + public override Validation ValidatesArmy(Army army) + { + return Validation.NotApplicable; + } + + public override string RequirementID + { + get + { + return REQUIREMENT_ID; + } + } + + /// + /// Adds a requirement for there to be at least minCount of a given UnitType, allowing allowedCount of this UnitType + /// + /// + /// The unit type to require. + /// + /// + /// The minimum number of that type that must exist. + /// + public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount) + { + requiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount)); + } + + /// + /// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType + /// + /// + /// The unit type to require. + /// + public void AddUnitTypeRequirement(UnitType unitType) + { + AddUnitTypeRequirement(unitType, 1, 1); + } + } +} + diff -r dc842b41adfd -r 0922851f6125 API/Objects/Requirement/UnitCountRequirementData.cs --- a/API/Objects/Requirement/UnitCountRequirementData.cs Mon Nov 28 20:11:33 2011 +0000 +++ b/API/Objects/Requirement/UnitCountRequirementData.cs Tue Nov 29 20:55:21 2011 +0000 @@ -10,11 +10,18 @@ { private UnitType unitType; private int count; + private int allows; - public UnitCountRequirementData(UnitType unitType, int count) + public UnitCountRequirementData(UnitType unitType, int count) : this(unitType, count, WarFoundryCore.INFINITY) + { + //Do nothing special + } + + public UnitCountRequirementData(UnitType unitType, int count, int allows) { this.unitType = unitType; this.count = count; + this.allows = allows; } public UnitType UnitType @@ -27,6 +34,11 @@ get { return count; } } + public int AllowsCount + { + get { return allows; } + } + public override bool Equals (object obj) { if (obj == null) @@ -40,13 +52,13 @@ else { UnitCountRequirementData other = (UnitCountRequirementData)obj; - return UnitType.Equals(other.UnitType) && Count == other.Count; + return UnitType.Equals(other.UnitType) && Count == other.Count && AllowsCount == other.AllowsCount; } } public override int GetHashCode() { - return UnitType.GetHashCode() + Count; + return UnitType.GetHashCode() + Count + AllowsCount; } } } diff -r dc842b41adfd -r 0922851f6125 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Mon Nov 28 20:11:33 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Tue Nov 29 20:55:21 2011 +0000 @@ -191,6 +191,7 @@ PreserveNewest +