comparison API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs @ 437:0922851f6125

Re #350: Add requirement to allow N of unit for specific other units * Extend unit count requirement data to handle fixed numbers of units allowed per unit required * Add initial stub of requirement using some copy-and-paste, but starting to avoid ticket:379
author IBBoard <dev@ibboard.co.uk>
date Tue, 29 Nov 2011 20:55:21 +0000
parents
children 410f3d85c9c5
comparison
equal deleted inserted replaced
436:dc842b41adfd 437:0922851f6125
1 // This file(RequiresNUnitsForMUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
2 //
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.
4 using System;
5 using System.Collections.Generic;
6
7 namespace IBBoard.WarFoundry.API.Objects.Requirement
8 {
9 public class RequiresNUnitsForMUnitsRequirement : AbstractRequirement
10 {
11 public static readonly string REQUIREMENT_ID = "RequiresNUnitsForMUnits";
12 private List<UnitCountRequirementData> requiredTypes;
13 private UnitType allowedType;
14
15 public RequiresNUnitsForMUnitsRequirement(UnitType allowedType, params UnitType[] requiredUnitTypes)
16 {
17 this.allowedType = allowedType;
18 FailureStringPrefix = "Army must contain: ";
19 requiredTypes = new List<UnitCountRequirementData>();
20
21 foreach (UnitType unitType in requiredUnitTypes)
22 {
23 AddUnitTypeRequirement(unitType);
24 }
25 }
26
27 public override int GetHashCode()
28 {
29 int hash = 0;
30
31 foreach (UnitCountRequirementData req in requiredTypes)
32 {
33 hash += req.UnitType.GetHashCode();
34 }
35
36 return hash;
37 }
38
39 protected override bool TypeEquals(object obj)
40 {
41 RequiresNUnitsForMUnitsRequirement otherReq = (RequiresNUnitsForMUnitsRequirement)obj;
42 if (!Collections.Collections.AreEqual(requiredTypes, otherReq.requiredTypes))
43 {
44 return false;
45 }
46 else
47 {
48 return true;
49 }
50 }
51
52 protected string FailureStringPrefix { get; set; }
53
54 protected override string GetValidationFailedMessage(Army army)
55 {
56 return "";
57 }
58
59 protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
60 {
61 return "";
62 }
63
64 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
65 {
66 Validation canAdd = Validation.NotApplicable;
67 UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType;
68 bool typeFound = (addedUnitType == allowedType);
69 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, wfObject);
70
71 foreach (UnitCountRequirementData limit in requiredTypes)
72 {
73 typeFound |= (addedUnitType == limit.UnitType);
74 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
75 double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
76 double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
77
78 if (allowedTypeMultiplier > limitedTypeMultiplier)
79 {
80 canAdd = Validation.Failed;
81 break;
82 }
83 }
84
85 if (typeFound && canAdd == Validation.NotApplicable)
86 {
87 canAdd = Validation.Passed;
88 }
89
90 return canAdd;
91 }
92
93 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
94 {
95 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
96 }
97
98 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
99 {
100 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
101 }
102
103 public override Validation ValidatesArmy(Army army)
104 {
105 return Validation.NotApplicable;
106 }
107
108 public override string RequirementID
109 {
110 get
111 {
112 return REQUIREMENT_ID;
113 }
114 }
115
116 /// <summary>
117 /// Adds a requirement for there to be at least minCount of a given UnitType, allowing allowedCount of this UnitType
118 /// </summary>
119 /// <param name='unitType'>
120 /// The unit type to require.
121 /// </param>
122 /// <param name='minCount'>
123 /// The minimum number of that type that must exist.
124 /// </param>
125 public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount)
126 {
127 requiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
128 }
129
130 /// <summary>
131 /// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType
132 /// </summary>
133 /// <param name='unitType'>
134 /// The unit type to require.
135 /// </param>
136 public void AddUnitTypeRequirement(UnitType unitType)
137 {
138 AddUnitTypeRequirement(unitType, 1, 1);
139 }
140 }
141 }
142