annotate API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 347:44a6539fadf9

Re #27: Unit requirements * Add base interface for all requirements and extend it in existing requirements * Add stub method for getting requirements of race (UnitTypes need to be able to add them first before testing goes further)
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 15:51:39 +0000
parents 008537acf244
children 6c0404277cad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System.Collections.Generic;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.WarFoundry.API.Objects;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.WarFoundry.API.Objects.Requirement
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// </summary>
347
44a6539fadf9 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 345
diff changeset
13 public class RequiresAtLeastNUnitsRequirement : IRequirement
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 private List<UnitCountRequirementData> requiredTypes;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 public RequiresAtLeastNUnitsRequirement(params UnitType[] requiredUnitTypes)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 requiredTypes = new List<UnitCountRequirementData>();
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 foreach (UnitType unitType in requiredUnitTypes)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 AddUnitTypeRequirement(unitType);
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 /// <returns>
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
31 /// A <code>Validation</code> enum to show the result of the validation
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 /// </returns>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 /// <param name='wfObject'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 /// </param>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 /// <param name='toArmy'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 /// The army to add the object to.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 /// </param>
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
39 public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 {
345
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
41 Validation isValid = Validation.Passed;
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
42
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
43 foreach (UnitCountRequirementData requirement in requiredTypes)
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
44 {
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
45 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
46 {
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
47 isValid = Validation.Failed;
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
48 break;
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
49 }
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
50 }
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
51
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
52 return isValid;
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
53 }
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
54
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
55 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
56 {
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
57 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
58 }
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
59
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
60 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
61 {
008537acf244 Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 340
diff changeset
62 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 /// Adds a requirement for there to be at least minCount of a given UnitType
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 /// <param name='unitType'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 /// The unit type to require.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 /// </param>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 /// <param name='minCount'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 /// The minimum number of that type that must exist.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 /// </param>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 public void AddUnitTypeRequirement(UnitType unitType, int minCount)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 /// Adds a requirement for there to be one or more of a given UnitType
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 /// <param name='unitType'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 /// The unit type to require.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 /// </param>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 public void AddUnitTypeRequirement (UnitType unitType)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 AddUnitTypeRequirement(unitType, 1);
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 /// Checks whether the supplied army is currently valid according to this requirement.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 /// <returns>
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
94 /// A <code>Validation</code> enum to show the result of the validation
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 /// </returns>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 /// <param name='toValidate'>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 /// The army to validate
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 /// </param>
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
99 public virtual Validation ValidatesArmy(Army toValidate)
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 {
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
101 Validation isValid = Validation.Passed;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 foreach (UnitCountRequirementData requirement in requiredTypes)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106 {
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
107 isValid = Validation.Failed;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 break;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
109 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
110 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111
340
7bd2a7cdbfbd Re #27: Unit requirements
IBBoard <dev@ibboard.co.uk>
parents: 338
diff changeset
112 return isValid;
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116