view API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 345:008537acf244

Re #27: Unit requirements * Make basic "Requires at least" requirement take into account the unit type being added * Make unit version of "requires at least" requirement pass execution up to the matching parent method
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 11:00:09 +0000
parents 7bd2a7cdbfbd
children 44a6539fadf9
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 any number of that object 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);
24 }
25 }
27 /// <summary>
28 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
29 /// </summary>
30 /// <returns>
31 /// A <code>Validation</code> enum to show the result of the validation
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 Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
40 {
41 Validation isValid = Validation.Passed;
43 foreach (UnitCountRequirementData requirement in requiredTypes)
44 {
45 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
46 {
47 isValid = Validation.Failed;
48 break;
49 }
50 }
52 return isValid;
53 }
55 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
56 {
57 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
58 }
60 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
61 {
62 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
63 }
65 /// <summary>
66 /// Adds a requirement for there to be at least minCount of a given UnitType
67 /// </summary>
68 /// <param name='unitType'>
69 /// The unit type to require.
70 /// </param>
71 /// <param name='minCount'>
72 /// The minimum number of that type that must exist.
73 /// </param>
74 public void AddUnitTypeRequirement(UnitType unitType, int minCount)
75 {
76 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
77 }
79 /// <summary>
80 /// Adds a requirement for there to be one or more of a given UnitType
81 /// </summary>
82 /// <param name='unitType'>
83 /// The unit type to require.
84 /// </param>
85 public void AddUnitTypeRequirement (UnitType unitType)
86 {
87 AddUnitTypeRequirement(unitType, 1);
88 }
90 /// <summary>
91 /// Checks whether the supplied army is currently valid according to this requirement.
92 /// </summary>
93 /// <returns>
94 /// A <code>Validation</code> enum to show the result of the validation
95 /// </returns>
96 /// <param name='toValidate'>
97 /// The army to validate
98 /// </param>
99 public virtual Validation ValidatesArmy(Army toValidate)
100 {
101 Validation isValid = Validation.Passed;
103 foreach (UnitCountRequirementData requirement in requiredTypes)
104 {
105 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
106 {
107 isValid = Validation.Failed;
108 break;
109 }
110 }
112 return isValid;
113 }
114 }
115 }