comparison API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children 4497ebce9a57
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
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;
7
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 any number of that object can be taken in an army.
12 /// </summary>
13 public class RequiresAtLeastNUnitsRequirement
14 {
15 private List<UnitCountRequirementData> requiredTypes;
16
17 public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes)
18 {
19 requiredTypes = new List<UnitCountRequirementData>();
20
21 foreach (UnitType unitType in requiredUnitTypes)
22 {
23 AddUnitTypeRequirement(unitType);
24 }
25 }
26
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 virtual bool AllowsAdding(WarFoundryObject wfObject, Army toArmy)
40 {
41 return this.ValidatesArmy(toArmy);
42 }
43
44 /// <summary>
45 /// Adds a requirement for there to be at least minCount of a given UnitType
46 /// </summary>
47 /// <param name='unitType'>
48 /// The unit type to require.
49 /// </param>
50 /// <param name='minCount'>
51 /// The minimum number of that type that must exist.
52 /// </param>
53 public void AddUnitTypeRequirement(UnitType unitType, int minCount)
54 {
55 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
56 }
57
58 /// <summary>
59 /// Adds a requirement for there to be one or more of a given UnitType
60 /// </summary>
61 /// <param name='unitType'>
62 /// The unit type to require.
63 /// </param>
64 public void AddUnitTypeRequirement (UnitType unitType)
65 {
66 AddUnitTypeRequirement(unitType, 1);
67 }
68
69 /// <summary>
70 /// Checks whether the supplied army is currently valid according to this requirement.
71 /// </summary>
72 /// <returns>
73 /// <c>true</c> if the army is valid, else <c>false</c>
74 /// </returns>
75 /// <param name='toValidate'>
76 /// The army to validate
77 /// </param>
78 public bool ValidatesArmy(Army toValidate)
79 {
80 bool canAdd = true;
81
82 foreach (UnitCountRequirementData requirement in requiredTypes)
83 {
84 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
85 {
86 canAdd = false;
87 break;
88 }
89 }
90
91 return canAdd;
92 }
93 }
94 }
95