annotate API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs @ 480:e0641e0eb86c

Re #410: "N units per M models in parent" requirement * Move context to a sub-folder/namespace to reduce clutter * Add simple "parent unit" context
author IBBoard <dev@ibboard.co.uk>
date Wed, 23 May 2012 21:00:33 +0100
parents f48c49b53738
children 248df19652f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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, Army toArmy, AddingContext context)
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
479
f48c49b53738 Re #410: "N units per M models in parent" requirement
IBBoard <dev@ibboard.co.uk>
parents: 457
diff changeset
26 return IsApplicable(toObject, context) || IsApplicableForRequiredType(toObject, toArmy);
455
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
479
f48c49b53738 Re #410: "N units per M models in parent" requirement
IBBoard <dev@ibboard.co.uk>
parents: 457
diff changeset
29 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
30 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 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
32 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
456
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
34 private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy)
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 bool isApplicable = false;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
37 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
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)
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
40 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
41 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
42 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
43
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
44 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
45 {
457
8e01c3174cc3 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 456
diff changeset
46 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
47 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
48 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
49 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
50 isApplicable = true;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
51 break;
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 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
54 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
55
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
56 return isApplicable;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
57 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
58
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 public override string RequirementID
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 get
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 return REQUIREMENT_ID;
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 }
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)
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 GetObjectCountFromArmy(toArmy, AllowedObject);
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 GetObjectCountFromArmy(Army toArmy, UnitType unitType)
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 toArmy.GetUnitTypeCount(unitType);
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 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
78 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 return army.GetUnitTypeCount(AllowedObject);
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83