view api/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 331:e1d1b81b192a

Re #27: Define unit requirements * Implement code for min limits > 1
author IBBoard <dev@ibboard.co.uk>
date Sun, 27 Mar 2011 19:50:33 +0000
parents 8ca5dd75d5b8
children 2cb3bd9b11ea
line source
1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
2 //
3 // 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.
4 using System;
5 using System.Collections.Generic;
6 using IBBoard.WarFoundry.API.Objects;
8 namespace IBBoard.WarFoundry.API.Objects.Requirement
9 {
10 /// <summary>
11 /// A requirement where a WarFoundryObject requires at least N units of one or more unit types before it can be taken in an army.
12 /// </summary>
13 public class RequiresAtLeastNUnitsRequirement
14 {
15 private List<UnitCountRequirementData> requiredTypes;
17 public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes)
18 {
19 requiredTypes = new List<UnitCountRequirementData>();
21 foreach (UnitType unitType in requiredUnitTypes)
22 {
23 AddUnitTypeRequirement(unitType, 1);
24 }
25 }
27 /// <summary>
28 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
29 /// </summary>
30 /// <returns>
31 /// <c>true</c> if the object can be added, else <c>false</c>
32 /// </returns>
33 /// <param name='wfObject'>
34 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
35 /// </param>
36 /// <param name='toArmy'>
37 /// The army to add the object to.
38 /// </param>
39 public bool AllowsAdding(WarFoundryObject wfObject, Army toArmy)
40 {
41 bool canAdd = true;
43 foreach (UnitCountRequirementData requirement in requiredTypes)
44 {
45 if (toArmy.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
46 {
47 canAdd = false;
48 break;
49 }
50 }
52 return canAdd;
53 }
55 public void AddUnitTypeRequirement(UnitType unitType, int minCount)
56 {
57 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
58 }
59 }
60 }