annotate api/Requirements/UnitRequiresAtLeastRequirement.cs @ 138:c11c0da01bbc

Fixes #147: "Replace Weapon" button doesn't always enable (regression) r167 introduced the bug by triggering an "add item to unit type" call before the mutex group was set * Added optional mutex group parameter to constructor * Remove setter on mutex group
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Sep 2009 18:00:52 +0000
parents 2f3cafb69799
children 6083010a005c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
2f3cafb69799 Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents: 82
diff changeset
1 // This file (UnitRequiresAtLeastRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
15
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
2 //
104
2f3cafb69799 Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents: 82
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.
15
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
5 using System;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Text;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.WarFoundry.API;
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
9 using IBBoard.WarFoundry.API.Objects;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
10
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
11 namespace IBBoard.WarFoundry.API.Requirements
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
12 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
13 /// <summary>
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
14 /// Summary description for UnitRequiresRequirement.
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
15 /// </summary>
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
16 public class UnitRequiresAtLeastRequirement : UnitRequirement
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
17 {
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 private UnitType[] requiredTypes;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 private int[] requiredCounts;
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
20 private String unitList;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
21
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
22 public UnitRequiresAtLeastRequirement(UnitType type, UnitType requiredUnitType) : this(type, new UnitType[]{requiredUnitType}, new int[]{1})
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
23 {
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
26 public UnitRequiresAtLeastRequirement(UnitType type, UnitType[] requiredUnitTypes, int[] minNumsRequired) : base(type)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 if (requiredUnitTypes.Length != minNumsRequired.Length)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 throw new ArgumentException("List of required unit types and list of number of units required must be equal");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 else if (requiredUnitTypes.Length == 1)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 throw new ArgumentException("List of required unit types must not be empty");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 }
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
36
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 requiredTypes = requiredUnitTypes;
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
38 requiredCounts = minNumsRequired;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
39
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
40 if (requiredTypes.Length > 1)
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
41 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
42 StringBuilder sb = new StringBuilder(requiredCounts[0]+" x "+requiredTypes[0].Name);
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
43
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
44 for (int i = 1; i<requiredTypes.Length; i++)
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
45 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
46 sb.Append(", "+requiredCounts[i]+" x "+requiredTypes[i].Name);
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
47 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
48
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
49 unitList = sb.ToString();
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
50 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
51 else
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
52 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
53 unitList = requiredTypes[0].Name;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
54 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
55 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
56
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
57 public override string Description
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
58 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
59 get { return "The army must include at least the following units to include a unit of type "+unitType.Name+": "+unitList; }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
60 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
61
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
62 protected override AbstractFailedRequirement CanRemoveFromArmy(Army army, UnitType type)
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
63 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
64 return null;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
65 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
66
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
67 protected override AbstractFailedRequirement CanAddToArmy(Army army, UnitType type)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 FailedRequirement failure = null;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 int count = requiredTypes.Length;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 for (int i = 0; i < count; i++)
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
73 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
74 if (army.GetUnitTypeCount(requiredTypes[i]) < requiredCounts[i])
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
75 {
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 failure = new FailedRequirement(this);
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
77 break;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
81 return failure;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
82 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
83 }
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
84 }