annotate API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs @ 473:0d032c04210e

Re #359: Add "only contained" attribute to unit types * Make sure remove handles sub-units as well Note: warning the user that the unit has sub-units rather than just removing them is left to the UI
author IBBoard <dev@ibboard.co.uk>
date Mon, 16 Apr 2012 20:47:14 +0100
parents 8e01c3174cc3
children f48c49b53738
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;
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 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
9 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 /// <summary>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 /// 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
12 /// </summary>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 public class UnitRequiresNUnitsForMUnitsRequirement : RequiresNUnitsForMObjectsRequirement<UnitType>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 //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
16 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
17
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 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
19 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 //Do nothing
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 }
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 /// <summary>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 /// </summary>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 /// <returns>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 /// A <code>Validation</code> enum to show the result of the validation
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 /// </returns>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 /// <param name='wfObject'>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 /// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 /// </param>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 /// <param name='toArmy'>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 /// The army to add the object to.
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 /// </param>
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy)
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
456
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
42 return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 protected override bool IsApplicable(IWarFoundryObject toObject)
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 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
48 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
456
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
50 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
51 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
52 bool isApplicable = false;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
53 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
54
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
55 if (addedType == null)
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
56 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
57 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
58 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
59
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
60 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
61 {
457
8e01c3174cc3 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 456
diff changeset
62 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
63 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
64 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
65 {
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
66 isApplicable = true;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
67 break;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
68 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
69 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
70 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
71
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
72 return isApplicable;
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
73 }
52baffdd2ab9 Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents: 455
diff changeset
74
455
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 public override string RequirementID
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 get
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 REQUIREMENT_ID;
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 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
84 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 return GetObjectCountFromArmy(toArmy, AllowedObject);
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 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
89 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 return toArmy.GetUnitTypeCount(unitType);
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 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
94 {
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 return army.GetUnitTypeCount(AllowedObject);
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 }
afc6410e4efc Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99