Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs @ 359:2a9c046be55a
Re #345: Add failure message to requirements
* Implement message creation for "at least" requirement
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 01 May 2011 14:23:51 +0000 |
parents | dbe7ccb1e557 |
children | c19e61262400 |
rev | line source |
---|---|
356 | 1 // This file (RequiresNoMoreThanNOfUnitTypeRequirement.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 IBBoard.WarFoundry.API.Objects; | |
6 using System.Collections.Generic; | |
7 | |
8 namespace IBBoard.WarFoundry.API.Objects.Requirement | |
9 { | |
10 /// <summary> | |
11 /// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army. | |
12 /// </summary> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
13 public class RequiresNoMoreThanNOfUnitTypeRequirement : AbstractRequirement |
356 | 14 { |
15 private List<UnitCountRequirementData> limitedTypes; | |
16 | |
17 public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes) | |
18 { | |
19 limitedTypes = new List<UnitCountRequirementData>(); | |
20 | |
21 foreach (UnitType unitType in limitedUnitTypes) | |
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 /// 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> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
39 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) |
356 | 40 { |
41 Validation canAdd = Validation.Passed; | |
42 | |
43 foreach (UnitCountRequirementData limit in limitedTypes) | |
44 { | |
45 if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count) | |
46 { | |
47 canAdd = Validation.Failed; | |
48 break; | |
49 } | |
50 } | |
51 | |
52 return canAdd; | |
53 } | |
54 | |
55 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject) | |
56 { | |
57 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType); | |
58 } | |
59 | |
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 } | |
64 | |
65 /// <summary> | |
66 /// Adds a requirement for there to be no more than maxCount of a given UnitType | |
67 /// </summary> | |
68 /// <param name='unitType'> | |
69 /// The unit type to limit. | |
70 /// </param> | |
71 /// <param name='minCount'> | |
72 /// The maximum number of that type that must exist. | |
73 /// </param> | |
74 public void AddUnitTypeRequirement(UnitType unitType, int maxCount) | |
75 { | |
76 limitedTypes.Add(new UnitCountRequirementData(unitType, maxCount)); | |
77 } | |
78 | |
79 /// <summary> | |
80 /// Adds a requirement for there to be none of a given UnitType | |
81 /// </summary> | |
82 /// <param name='unitType'> | |
83 /// The unit type to limit. | |
84 /// </param> | |
85 public void AddUnitTypeRequirement(UnitType unitType) | |
86 { | |
87 AddUnitTypeRequirement(unitType, 0); | |
88 } | |
89 | |
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> | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
99 public override Validation ValidatesArmy(Army army) |
356 | 100 { |
101 Validation canAdd = Validation.Passed; | |
102 | |
103 foreach (UnitCountRequirementData limit in limitedTypes) | |
104 { | |
105 if (army.GetUnitTypeCount(limit.UnitType) > limit.Count) | |
106 { | |
107 canAdd = Validation.Failed; | |
108 break; | |
109 } | |
110 } | |
111 | |
112 return canAdd; | |
113 } | |
114 | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
115 protected override bool TypeEquals(object obj) |
356 | 116 { |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
117 RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
118 return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes); |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
119 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
120 |
356 | 121 public override int GetHashCode() |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
122 { |
356 | 123 return base.GetHashCode (); |
124 } | |
358
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
125 |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
126 protected override string GetValidationFailedMessage (Army army) |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
127 { |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
128 string message = ""; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
129 return message; |
dbe7ccb1e557
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
357
diff
changeset
|
130 } |
356 | 131 } |
132 } | |
133 |