Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs @ 487:248df19652f9
Re #410: Create "N units per M models in parent unit" requirement
* Add null adding context
* Add initial skeleton of "N units per M models in parent unit" requirement
* Update use of context
* Standardise some of "is applicable" testing
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 27 Jul 2012 20:31:12 +0100 |
parents | e0641e0eb86c |
children |
rev | line source |
---|---|
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
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. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 using System; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System.Collections.Generic; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using IBBoard.WarFoundry.API.Objects; |
480
e0641e0eb86c
Re #410: "N units per M models in parent" requirement
IBBoard <dev@ibboard.co.uk>
parents:
479
diff
changeset
|
7 using IBBoard.WarFoundry.API.Objects.Requirement.Context; |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 namespace IBBoard.WarFoundry.API.Objects.Requirement |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 /// A requirement where N of a UnitType can only be taken if there are M of a unit type in the army. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 public class UnitRequiresNUnitsForMUnitsRequirement : RequiresNUnitsForMObjectsRequirement<UnitType> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 //Note: We're sticking with the old requirement name to prevent breakage |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 public static readonly string REQUIREMENT_ID = "RequiresNUnitsForMUnits"; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 public UnitRequiresNUnitsForMUnitsRequirement(UnitType requirementOn, params UnitType[] requiredUnitTypes) : base(requirementOn, requiredUnitTypes) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 //Do nothing |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 |
479
f48c49b53738
Re #410: "N units per M models in parent" requirement
IBBoard <dev@ibboard.co.uk>
parents:
457
diff
changeset
|
24 protected override bool IsApplicable(IWarFoundryObject toObject, AddingContext context) |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 |
487
248df19652f9
Re #410: Create "N units per M models in parent unit" requirement
IBBoard <dev@ibboard.co.uk>
parents:
480
diff
changeset
|
29 protected override bool IsApplicableToBoth(IWarFoundryObject toObject, Army toArmy, AddingContext context) |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
30 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
31 bool isApplicable = false; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
32 UnitType addedType = toObject as UnitType; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
33 |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
34 if (addedType == null) |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
35 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
36 addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
37 } |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
38 |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
39 if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
40 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
41 foreach (UnitCountRequirementData limit in ConstraintTypes) |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
42 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
43 if (Arrays.Contains(limit.UnitTypes, addedType)) |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
44 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
45 isApplicable = true; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
46 break; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
47 } |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
48 } |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
49 } |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
50 |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
51 return isApplicable; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
52 } |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
53 |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 public override string RequirementID |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 get |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 return REQUIREMENT_ID; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 protected override int GetObjectCountFromArmy(Army toArmy) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 return GetObjectCountFromArmy(toArmy, AllowedObject); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 protected override int GetObjectCountFromArmy(Army toArmy, UnitType unitType) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 return toArmy.GetUnitTypeCount(unitType); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 protected override int GetAllowedObjectCount(Army army) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 return army.GetUnitTypeCount(AllowedObject); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 |