Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs @ 458:680db2462e34
Re #379:
* Move GetObjectCountFromArmy(Army, OBJECT_TYPE) to top level and implement
* Fix army validation for appropriate "NA" returns
* Make basic "Requires..." requirements abstract so that we always need to make specific versions (e.g. UnitRequires... that knows how to check amount of units)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 26 Feb 2012 20:16:33 +0000 |
parents | 8e01c3174cc3 |
children | f0594621e4a0 |
rev | line source |
---|---|
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
2 // |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
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. |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
4 using System; |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
5 using System.Collections.Generic; |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
6 using IBBoard.WarFoundry.API.Objects; |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
7 |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
8 namespace IBBoard.WarFoundry.API.Objects.Requirement |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
9 { |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
10 /// <summary> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
11 /// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army. |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
12 /// </summary> |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
13 public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement<UnitType> |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
14 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
15 public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType allowedObject, params UnitType[] unitTypes) : base(allowedObject, unitTypes) |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
16 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
17 //Do nothing special |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
18 } |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
19 |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
20 /// <summary> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
21 /// Checks whether the supplied WarFoundryObject can be added to the supplied army. |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
22 /// </summary> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
23 /// <returns> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
24 /// A <code>Validation</code> enum to show the result of the validation |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
25 /// </returns> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
26 /// <param name='wfObject'> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
27 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
28 /// </param> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
29 /// <param name='toArmy'> |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
30 /// The army to add the object to. |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
31 /// </param> |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
32 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
33 { |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
34 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
35 } |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
36 |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
37 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
38 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
39 return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
40 } |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
41 |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
42 protected override bool IsApplicable(IWarFoundryObject toObject) |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
43 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
44 return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
45 } |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
46 |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
47 private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy) |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
48 { |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
49 bool isApplicable = false; |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
50 UnitType addedType = toObject as UnitType; |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
51 |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
52 if (addedType == null) |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
53 { |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
54 addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
55 } |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
56 |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
57 if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
58 { |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
59 foreach (UnitCountRequirementData limit in ConstraintTypes) |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
60 { |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
61 if (Arrays.Contains(limit.UnitTypes, addedType)) |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
62 { |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
63 isApplicable = true; |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
64 break; |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
65 } |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
66 } |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
67 } |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
68 |
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
69 return isApplicable; |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
70 } |
458 | 71 |
72 protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj) | |
73 { | |
74 return toArmy.GetUnitTypeCount(obj); | |
75 } | |
362
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
76 } |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
77 } |
c19e61262400
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
343
diff
changeset
|
78 |