Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs @ 458:680db2462e34
Re #379:
* Move GetObjectCountFromArmy(Army, OBJECT_TYPE) to top level and implement
* Fix army validation for appropriate "NA" returns
* Make basic "Requires..." requirements abstract so that we always need to make specific versions (e.g. UnitRequires... that knows how to check amount of units)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 26 Feb 2012 20:16:33 +0000 |
parents | afc6410e4efc |
children | f48c49b53738 |
rev | line source |
---|---|
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
1 // This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
2 // |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
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. |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
4 using System; |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
5 using System.Collections.Generic; |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
6 using IBBoard.WarFoundry.API.Objects; |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
7 |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
8 namespace IBBoard.WarFoundry.API.Objects.Requirement |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
9 { |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
10 /// <summary> |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
11 /// A requirement where a UnitType requires at least N units of one or more unit types before any number of that object can be taken in an army. |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
12 /// </summary> |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
13 public class UnitRequiresAtLeastNUnitsRequirement : RequiresAtLeastNUnitsRequirement<UnitType> |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
14 { |
455
afc6410e4efc
Re #379: Fix validation of requirements to check for unit
IBBoard <dev@ibboard.co.uk>
parents:
454
diff
changeset
|
15 public UnitRequiresAtLeastNUnitsRequirement(UnitType requirementOn) : base(requirementOn) |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
16 { |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
17 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
18 |
458 | 19 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) |
20 { | |
21 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; | |
22 } | |
23 | |
24 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) | |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
25 { |
458 | 26 return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); |
27 } | |
28 | |
29 protected override bool IsApplicable(IWarFoundryObject toObject) | |
30 { | |
31 return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); | |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
32 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
33 |
458 | 34 private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy) |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
35 { |
458 | 36 bool isApplicable = false; |
37 UnitType addedType = toObject as UnitType; | |
38 | |
39 if (addedType == null) | |
40 { | |
41 addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; | |
42 } | |
43 | |
44 if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) | |
45 { | |
46 foreach (UnitCountRequirementData limit in ConstraintTypes) | |
47 { | |
48 if (Arrays.Contains(limit.UnitTypes, addedType)) | |
49 { | |
50 isApplicable = true; | |
51 break; | |
52 } | |
53 } | |
54 } | |
55 | |
56 return isApplicable; | |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
57 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
58 |
458 | 59 protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj) |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
60 { |
458 | 61 return toArmy.GetUnitTypeCount(obj); |
360
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
62 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
63 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
64 } |
777725613edb
Re #345: Add failure message to requirements
IBBoard <dev@ibboard.co.uk>
parents:
350
diff
changeset
|
65 |