view api/Requirements/UnitRequiresAtLeastRequirement.cs @ 104:2f3cafb69799

Re #121: Migrate to AGPL license * Update all API files to AGPL license * Include AGPL license and remove GPL/LGPL documents * Fix copyright dates where they're known
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Aug 2009 10:36:50 +0000
parents 3ea0ab04352b
children 6083010a005c
line source
1 // This file (UnitRequiresAtLeastRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 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.
5 using System;
6 using System.Collections.Generic;
7 using System.Text;
8 using IBBoard.WarFoundry.API;
9 using IBBoard.WarFoundry.API.Objects;
11 namespace IBBoard.WarFoundry.API.Requirements
12 {
13 /// <summary>
14 /// Summary description for UnitRequiresRequirement.
15 /// </summary>
16 public class UnitRequiresAtLeastRequirement : UnitRequirement
17 {
18 private UnitType[] requiredTypes;
19 private int[] requiredCounts;
20 private String unitList;
22 public UnitRequiresAtLeastRequirement(UnitType type, UnitType requiredUnitType) : this(type, new UnitType[]{requiredUnitType}, new int[]{1})
23 {
24 }
26 public UnitRequiresAtLeastRequirement(UnitType type, UnitType[] requiredUnitTypes, int[] minNumsRequired) : base(type)
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 }
37 requiredTypes = requiredUnitTypes;
38 requiredCounts = minNumsRequired;
40 if (requiredTypes.Length > 1)
41 {
42 StringBuilder sb = new StringBuilder(requiredCounts[0]+" x "+requiredTypes[0].Name);
44 for (int i = 1; i<requiredTypes.Length; i++)
45 {
46 sb.Append(", "+requiredCounts[i]+" x "+requiredTypes[i].Name);
47 }
49 unitList = sb.ToString();
50 }
51 else
52 {
53 unitList = requiredTypes[0].Name;
54 }
55 }
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 }
62 protected override AbstractFailedRequirement CanRemoveFromArmy(Army army, UnitType type)
63 {
64 return null;
65 }
67 protected override AbstractFailedRequirement CanAddToArmy(Army army, UnitType type)
68 {
69 FailedRequirement failure = null;
70 int count = requiredTypes.Length;
72 for (int i = 0; i < count; i++)
73 {
74 if (army.GetUnitTypeCount(requiredTypes[i]) < requiredCounts[i])
75 {
76 failure = new FailedRequirement(this);
77 break;
78 }
79 }
81 return failure;
82 }
83 }
84 }