Mercurial > repos > IBBoard.WarFoundry.API
view API/Objects/Requirement/AbstractUnitRequirement.cs @ 464:59e1fb8a476a
Re #394: Make requirements (or factory) more closely match Rollcall methods
* Fix ratio check - we were doing an unnecessary division
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 24 Mar 2012 16:40:01 +0000 |
parents | 159dc9be36c2 |
children | 7b9ff7b1df24 |
line wrap: on
line source
// This file (AbstractRequirement.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 abstract class AbstractUnitRequirement<OBJECT_TYPE> : IRequirement where OBJECT_TYPE : IWarFoundryObject { private List<UnitCountRequirementData> requiredTypes; private OBJECT_TYPE allowedObject; public AbstractUnitRequirement(OBJECT_TYPE allowedObject, params UnitType[] requiredUnitTypes) { this.allowedObject = allowedObject; requiredTypes = new List<UnitCountRequirementData>(); foreach (UnitType unitType in requiredUnitTypes) { AddUnitTypeRequirement(unitType); } } public abstract string RequirementID { get; } protected List<UnitCountRequirementData> ConstraintTypes { get { return requiredTypes; } } public abstract void AddUnitTypeRequirement(UnitType unitType); public override bool Equals(object obj) { if (obj == null) { return false; } else if (obj.GetType().Equals(this.GetType())) { return TypeEquals((AbstractUnitRequirement<OBJECT_TYPE>)obj); } else { return false; } } protected virtual bool TypeEquals(AbstractUnitRequirement<OBJECT_TYPE> obj) { AbstractUnitRequirement<OBJECT_TYPE> otherReq = obj as AbstractUnitRequirement<OBJECT_TYPE>; if (!this.AllowedObject.Equals(otherReq.AllowedObject)) { return false; } else if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes)) { return false; } else { return true; } } public override int GetHashCode() { int hash = GetType().FullName.GetHashCode() + AllowedObject.GetHashCode(); foreach (UnitCountRequirementData limit in ConstraintTypes) { hash += limit.GetHashCode(); } return hash; } protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy) { return IsApplicable(toArmy) || IsApplicable(toObjectAdded); } protected virtual bool IsApplicable(Army toArmy) { return false; } protected virtual bool IsApplicable(IWarFoundryObject toObject) { return false; } public string GetValidationMessage(Army army) { string message = ""; Validation result = ValidatesArmy(army); if (!Validates.AsOkay(result)) { message = GetValidationFailedMessage(army); } return message; } protected virtual string ValidationFailedMessage { get { return "Army must contain: {0}."; } } private string GetValidationFailedMessage(Army army) { return String.Format(ValidationFailedMessage, GetFailedRequirementsString(army)); } protected abstract string GetFailedRequirementsString(Army army); public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy) { string message = ""; Validation result = AllowsAdding(toAdd, toArmy); if (!Validates.AsOkay(result)) { message = GetAllowsAddingFailedMessage(toAdd, toArmy); } return message; } protected virtual string AllowsAddingFailedMessage { get { return ValidationFailedMessage; } } protected string GetAllowsAddingFailedMessage(IWarFoundryObject toAdd, Army toArmy) { return String.Format(AllowsAddingFailedMessage, GetFailedAddingRequirementsString(toAdd, toArmy)); } protected abstract string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy); public abstract Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy); public abstract Validation ValidatesArmy(Army army); protected UnitType GetUnitTypeFromObject(IWarFoundryObject toObject) { UnitType unitType = null; if (toObject is UnitType) { unitType = (UnitType)toObject; } else if (toObject is Unit) { unitType = ((Unit)toObject).UnitType; } return unitType; } protected int GetUnitTypeCount(Army toArmy, UnitType unitType, IWarFoundryObject wfObject) { return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); } protected int GetCountFromObject(IWarFoundryObject wfObject, UnitType limitedType) { return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; } protected int GetObjectCount(Army toArmy, IWarFoundryObject wfObject) { return GetObjectCountFromArmy(toArmy) + GetObjectCountFromObject(wfObject); } 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; } public OBJECT_TYPE AllowedObject { get { return allowedObject; } } } }