Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs @ 467:750eed630c41
Fixes #395: Requirements can incorrectly allow based on common divisor
* Round the "limited/required" normalised count down to get correct values
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 31 Mar 2012 20:18:42 +0100 |
parents | bdfa314789cc |
children | f48c49b53738 |
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(RequiresNUnitsForMUnitsRequirement.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 System.Text; |
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 public abstract class RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 public RequiresNUnitsForMObjectsRequirement(OBJECT_TYPE allowedObject, params UnitType[] requiredUnitTypes) : base(allowedObject, requiredUnitTypes) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 //Do nothing |
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 public override int GetHashCode() |
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 int hash = 0; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
21 foreach (UnitCountRequirementData req in ConstraintTypes) |
455
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 hash += req.UnitType.GetHashCode(); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 } |
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 hash; |
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 protected override string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy) |
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 String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()); |
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 private List<string> GetFailedAddingRequirements(IWarFoundryObject obj, Army toArmy) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 List<string> failures = new List<string>(); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 int allowedObjectCount = GetAllowedObjectCount(toArmy, AllowedObject, obj); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
39 foreach (UnitCountRequirementData limit in ConstraintTypes) |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, obj); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 if (!IsValidByRequirement(obj, toArmy, limit, allowedObjectCount)) |
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 string unitTypeList = GetUnitTypeList(limit); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 failures.Add(String.Format("{0} × {1} for every {2} × {3} (would have {4} for {5})", limit.Count, unitTypeList, limit.AllowsCount, AllowedObject.Name, limitedTypeCount, allowedObjectCount)); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 } |
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 return failures; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 private bool IsValidByRequirement(IWarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, wfObject); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount); |
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 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 private bool IsValidByRequirement(UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 { |
467
750eed630c41
Fixes #395: Requirements can incorrectly allow based on common divisor
IBBoard <dev@ibboard.co.uk>
parents:
466
diff
changeset
|
61 //Round down limited units to get only whole amounts |
750eed630c41
Fixes #395: Requirements can incorrectly allow based on common divisor
IBBoard <dev@ibboard.co.uk>
parents:
466
diff
changeset
|
62 double normalisedLimited = Math.Floor(limitedTypeCount / (limit.Count * 1.0)); |
466
bdfa314789cc
Re #395: Requirements can incorrectly allow based on common divisor
IBBoard <dev@ibboard.co.uk>
parents:
465
diff
changeset
|
63 double normalisedAllowed = allowedTypeCount / (limit.AllowsCount * 1.0); |
bdfa314789cc
Re #395: Requirements can incorrectly allow based on common divisor
IBBoard <dev@ibboard.co.uk>
parents:
465
diff
changeset
|
64 return normalisedLimited >= 1 && normalisedAllowed <= normalisedLimited; |
455
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 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
|
68 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 Validation canAdd = Validation.NotApplicable; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType; |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
71 bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject); |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
74 foreach (UnitCountRequirementData limit in ConstraintTypes) |
455
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 typeFound |= (addedUnitType == limit.UnitType); |
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 if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount)) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 canAdd = Validation.Failed; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 break; |
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 } |
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 if (typeFound && canAdd == Validation.NotApplicable) |
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 canAdd = Validation.Passed; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 } |
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 canAdd; |
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 private int GetAllowedObjectCount(Army toArmy, OBJECT_TYPE obj, IWarFoundryObject wfObject) |
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 GetObjectCountFromArmy(toArmy, obj) + GetCountFromObject(wfObject, obj); |
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 private int GetCountFromObject(IWarFoundryObject wfObject, OBJECT_TYPE limitedType) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
99 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 public override Validation ValidatesArmy(Army army) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
104 { |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
105 Validation canAdd; |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
106 int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
107 |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
108 if (allowedTypeCount > 0) |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
109 { |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
110 canAdd = Validation.Passed; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
111 |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
112 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
|
113 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
114 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
115 |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
116 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
117 { |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
118 canAdd = Validation.Failed; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
119 break; |
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
120 } |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
121 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
122 } |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
123 else |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
124 { |
456
52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
455
diff
changeset
|
125 canAdd = Validation.NotApplicable; |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
126 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
127 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
128 return canAdd; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
129 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
130 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
131 protected override string GetFailedRequirementsString(Army army) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
132 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
133 return String.Join("; ", GetFailedValidationRequirements(army).ToArray()); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
134 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
135 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
136 private List<string> GetFailedValidationRequirements(Army army) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
137 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
138 List<string> failures = new List<string>(); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
139 int allowedTypeCount = GetAllowedObjectCount(army); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
140 |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
141 foreach (UnitCountRequirementData limit in ConstraintTypes) |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
142 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
143 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
144 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
145 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
146 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
147 string unitTypeList = GetUnitTypeList(limit); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
148 failures.Add(String.Format("{0} × {1} for every {2} × {3} (have {4} for {5})", limit.Count, unitTypeList, limit.AllowsCount, AllowedObject.Name, limitedTypeCount, allowedTypeCount)); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
149 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
150 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
151 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
152 return failures; |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
153 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
154 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
155 protected abstract int GetAllowedObjectCount(Army army); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
156 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
157 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
158 /// Adds a requirement for there to be at least minCount of a given UnitType, allowing allowedCount of this UnitType |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
159 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
160 /// <param name='unitType'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
161 /// The unit type to require. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
162 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
163 /// <param name='minCount'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
164 /// The minimum number of that type that must exist. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
165 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
166 /// <param name='allowedCount'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
167 /// The number of units allowed for every minCount units of the supplied unit type. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
168 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
169 public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
170 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
171 ConstraintTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount)); |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
172 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
173 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
174 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
175 /// Adds a requirement for there to be at least one of a given UnitType, allowing allowedCount of this UnitType |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
176 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
177 /// <param name='unitType'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
178 /// The unit type to require. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
179 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
180 /// <param name='allowedCount'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
181 /// The number of units allowed for each unit of the supplied unit type. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
182 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
183 public void AddUnitTypeRequirement(UnitType unitType, int allowedCount) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
184 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
185 AddUnitTypeRequirement(allowedCount, unitType); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
186 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
187 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
188 public override void AddUnitTypeRequirement(UnitType unitType) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
189 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
190 AddUnitTypeRequirement(1, unitType); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
191 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
192 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
193 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
194 /// Adds a requirement for there to be one or more of the given UnitTypes, allowing one of this UnitType. If multiple unit types |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
195 /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType). |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
196 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
197 /// <param name='unitType'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
198 /// The unit type or types to require. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
199 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
200 public void AddUnitTypeRequirement(params UnitType[] unitTypes) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
201 { |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
202 AddUnitTypeRequirement(1, unitTypes); |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
203 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
204 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
205 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
206 /// Adds a requirement for there to be one or more of the given UnitTypes, allowing allowsAmount of this UnitType. If multiple unit types |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
207 /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType). |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
208 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
209 /// <param name="allowsAmount"> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
210 /// The number of units to be allowed for each 1 of unitType |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
211 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
212 /// <param name='unitType'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
213 /// The unit type or types to require. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
214 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
215 public void AddUnitTypeRequirement(int allowsAmount, params UnitType[] unitTypes) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
216 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
217 ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount)); |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
218 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
219 |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
220 /// <summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
221 /// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing allowsAmount of this UnitType. If multiple unit types |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
222 /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType). |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
223 /// </summary> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
224 /// <param name='minCount'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
225 /// The minimum number of that type that must exist. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
226 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
227 /// <param name="allowsAmount"> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
228 /// The number of units to be allowed for each 1 of unitType |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
229 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
230 /// <param name='unitType'> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
231 /// The unit type or types to require. |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
232 /// </param> |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
233 public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes) |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
234 { |
457
8e01c3174cc3
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
456
diff
changeset
|
235 ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount)); |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
236 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
237 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
238 } |
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
239 |