annotate API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs @ 440:baa34d91031f

Re #350: Add requirement to allow N of unit for specific other units * Correct "adding" validation message - we haven't added when we check if adding is okay
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 16:14:26 +0000
parents 5252dfb9cdfb
children d2331ee59d74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
437
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
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
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
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.
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 using System;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System.Collections.Generic;
438
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
6 using System.Text;
437
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.WarFoundry.API.Objects.Requirement
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 public class RequiresNUnitsForMUnitsRequirement : AbstractRequirement
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 public static readonly string REQUIREMENT_ID = "RequiresNUnitsForMUnits";
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 private List<UnitCountRequirementData> requiredTypes;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 private UnitType allowedType;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public RequiresNUnitsForMUnitsRequirement(UnitType allowedType, params UnitType[] requiredUnitTypes)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 this.allowedType = allowedType;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 FailureStringPrefix = "Army must contain: ";
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 requiredTypes = new List<UnitCountRequirementData>();
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 foreach (UnitType unitType in requiredUnitTypes)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 AddUnitTypeRequirement(unitType);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 public override int GetHashCode()
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 int hash = 0;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 foreach (UnitCountRequirementData req in requiredTypes)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 hash += req.UnitType.GetHashCode();
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 return hash;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 protected override bool TypeEquals(object obj)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 RequiresNUnitsForMUnitsRequirement otherReq = (RequiresNUnitsForMUnitsRequirement)obj;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 return false;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 else
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 return true;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 protected string FailureStringPrefix { get; set; }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 {
438
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
57 StringBuilder sb = new StringBuilder();
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
58 sb.Append(FailureStringPrefix);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
59 sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
60 sb.Append(".");
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
61 return sb.ToString();
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
62 }
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
63
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
64 private List<string> GetFailedAddingRequirements(UnitType unitType, Army toArmy)
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
65 {
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
66 List<string> failures = new List<string>();
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
67 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, unitType);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
68
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
69 foreach (UnitCountRequirementData limit in requiredTypes)
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
70 {
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
71 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
72
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
73 if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount))
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
74 {
440
baa34d91031f Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 439
diff changeset
75 failures.Add(String.Format("{0} {1} for every {2} {3} (would have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount));
438
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
76 }
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
77 }
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
78
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
79 return failures;
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
80 }
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
81
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
82 private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount)
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
83 {
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
84 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
85 return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
86 }
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
87
439
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
88 private bool IsValidByRequirement(UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount)
438
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
89 {
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
90 double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
91 double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
92 return allowedTypeMultiplier <= limitedTypeMultiplier;
437
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 Validation canAdd = Validation.NotApplicable;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 bool typeFound = (addedUnitType == allowedType);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, wfObject);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 foreach (UnitCountRequirementData limit in requiredTypes)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 typeFound |= (addedUnitType == limit.UnitType);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105
438
410f3d85c9c5 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 437
diff changeset
106 if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount))
437
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
107 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 canAdd = Validation.Failed;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
109 break;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
110 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
112
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113 if (typeFound && canAdd == Validation.NotApplicable)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 canAdd = Validation.Passed;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
118 return canAdd;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
121 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
122 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
123 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
124 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
126 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
131 public override Validation ValidatesArmy(Army army)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 {
439
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
133 Validation canAdd = Validation.NotApplicable;
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
134 int allowedTypeCount = army.GetUnitTypeCount(allowedType);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
135 bool typeFound = (allowedTypeCount > 0);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
136
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
137 foreach (UnitCountRequirementData limit in requiredTypes)
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
138 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
139 int limitedTypeCount = army.GetUnitTypeCount(limit.UnitType);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
140 typeFound |= (limitedTypeCount > 0);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
141
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
142 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
143 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
144 canAdd = Validation.Failed;
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
145 break;
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
146 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
147 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
148
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
149 if (typeFound && canAdd == Validation.NotApplicable)
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
150 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
151 canAdd = Validation.Passed;
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
152 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
153
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
154 return canAdd;
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
155 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
156
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
157 protected override string GetValidationFailedMessage(Army army)
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
158 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
159 StringBuilder sb = new StringBuilder();
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
160 sb.Append(FailureStringPrefix);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
161 sb.Append(String.Join("; ", GetFailedValidationRequirements(army).ToArray()));
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
162 sb.Append(".");
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
163 return sb.ToString();
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
164 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
165
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
166 private List<string> GetFailedValidationRequirements(Army army)
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
167 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
168 List<string> failures = new List<string>();
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
169 int allowedTypeCount = army.GetUnitTypeCount(allowedType);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
170
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
171 foreach (UnitCountRequirementData limit in requiredTypes)
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
172 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
173 int limitedTypeCount = army.GetUnitTypeCount(limit.UnitType);
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
174
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
175 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
176 {
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
177 failures.Add(String.Format("{0} {1} for every {2} {3} (have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount));
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
178 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
179 }
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
180
5252dfb9cdfb Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents: 438
diff changeset
181 return failures;
437
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
182 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
183
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
184 public override string RequirementID
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
185 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
186 get
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
187 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
188 return REQUIREMENT_ID;
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
189 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
190 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
191
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
192 /// <summary>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
193 /// Adds a requirement for there to be at least minCount of a given UnitType, allowing allowedCount of this UnitType
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
194 /// </summary>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
195 /// <param name='unitType'>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
196 /// The unit type to require.
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
197 /// </param>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
198 /// <param name='minCount'>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
199 /// The minimum number of that type that must exist.
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
200 /// </param>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
201 public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
202 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
203 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
204 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
205
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
206 /// <summary>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
207 /// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
208 /// </summary>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
209 /// <param name='unitType'>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
210 /// The unit type to require.
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
211 /// </param>
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
212 public void AddUnitTypeRequirement(UnitType unitType)
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
213 {
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
214 AddUnitTypeRequirement(unitType, 1, 1);
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
215 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
216 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
217 }
0922851f6125 Re #350: Add requirement to allow N of unit for specific other units
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
218