15
|
1 // This file (UnitRequiresAtLeastRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
|
|
2 //
|
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
|
|
4
|
82
|
5 using System;
|
0
|
6 using System.Collections.Generic;
|
|
7 using System.Text;
|
|
8 using IBBoard.WarFoundry.API;
|
82
|
9 using IBBoard.WarFoundry.API.Objects;
|
|
10
|
|
11 namespace IBBoard.WarFoundry.API.Requirements
|
|
12 {
|
|
13 /// <summary>
|
|
14 /// Summary description for UnitRequiresRequirement.
|
|
15 /// </summary>
|
|
16 public class UnitRequiresAtLeastRequirement : UnitRequirement
|
|
17 {
|
0
|
18 private UnitType[] requiredTypes;
|
|
19 private int[] requiredCounts;
|
82
|
20 private String unitList;
|
|
21
|
|
22 public UnitRequiresAtLeastRequirement(UnitType type, UnitType requiredUnitType) : this(type, new UnitType[]{requiredUnitType}, new int[]{1})
|
|
23 {
|
0
|
24 }
|
|
25
|
82
|
26 public UnitRequiresAtLeastRequirement(UnitType type, UnitType[] requiredUnitTypes, int[] minNumsRequired) : base(type)
|
0
|
27 {
|
|
28 if (requiredUnitTypes.Length != minNumsRequired.Length)
|
|
29 {
|
|
30 throw new ArgumentException("List of required unit types and list of number of units required must be equal");
|
|
31 }
|
|
32 else if (requiredUnitTypes.Length == 1)
|
|
33 {
|
|
34 throw new ArgumentException("List of required unit types must not be empty");
|
|
35 }
|
82
|
36
|
0
|
37 requiredTypes = requiredUnitTypes;
|
82
|
38 requiredCounts = minNumsRequired;
|
|
39
|
|
40 if (requiredTypes.Length > 1)
|
|
41 {
|
|
42 StringBuilder sb = new StringBuilder(requiredCounts[0]+" x "+requiredTypes[0].Name);
|
|
43
|
|
44 for (int i = 1; i<requiredTypes.Length; i++)
|
|
45 {
|
|
46 sb.Append(", "+requiredCounts[i]+" x "+requiredTypes[i].Name);
|
|
47 }
|
|
48
|
|
49 unitList = sb.ToString();
|
|
50 }
|
|
51 else
|
|
52 {
|
|
53 unitList = requiredTypes[0].Name;
|
|
54 }
|
|
55 }
|
|
56
|
|
57 public override string Description
|
|
58 {
|
|
59 get { return "The army must include at least the following units to include a unit of type "+unitType.Name+": "+unitList; }
|
|
60 }
|
|
61
|
|
62 protected override AbstractFailedRequirement CanRemoveFromArmy(Army army, UnitType type)
|
|
63 {
|
|
64 return null;
|
|
65 }
|
|
66
|
|
67 protected override AbstractFailedRequirement CanAddToArmy(Army army, UnitType type)
|
0
|
68 {
|
|
69 FailedRequirement failure = null;
|
|
70 int count = requiredTypes.Length;
|
|
71
|
|
72 for (int i = 0; i < count; i++)
|
82
|
73 {
|
|
74 if (army.GetUnitTypeCount(requiredTypes[i]) < requiredCounts[i])
|
|
75 {
|
0
|
76 failure = new FailedRequirement(this);
|
82
|
77 break;
|
0
|
78 }
|
|
79 }
|
|
80
|
82
|
81 return failure;
|
|
82 }
|
|
83 }
|
|
84 }
|