comparison API/Objects/Requirement/RaceRequiresAtLeastNUnitsRequirement.cs @ 459:7a00aeba3d2f

Re #379: Fix validation of requirements to check for unit * Re-add requirement for handling numbers of units in an army
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Mar 2012 20:27:33 +0000
parents
children f48c49b53738
comparison
equal deleted inserted replaced
458:680db2462e34 459:7a00aeba3d2f
1 // This file (RaceRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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 IBBoard.Collections;
6
7 namespace IBBoard.WarFoundry.API.Objects.Requirement
8 {
9 public class RaceRequiresAtLeastNUnitsRequirement : RequiresAtLeastNUnitsRequirement<Race>
10 {
11 private static readonly string REQUIREMENT_ID_RACE = "RaceRequiresAtLeastNUnits";
12
13 public RaceRequiresAtLeastNUnitsRequirement(Race requirementOn, params UnitType[] requiredUnitTypes) : base(requirementOn, requiredUnitTypes)
14 {
15 //Do nothing special
16 }
17
18 protected override int GetObjectCountFromArmy(Army toArmy, Race obj)
19 {
20 return toArmy.Race == AllowedObject ? 1 : 0;
21 }
22
23 protected override bool IsApplicable(Army toArmy)
24 {
25 bool isApplicable = false;
26 SimpleSet<UnitType> checkedTypes = new SimpleSet<UnitType>();
27
28 foreach (UnitCountRequirementData limit in ConstraintTypes)
29 {
30 foreach (UnitType unitType in limit.UnitTypes)
31 {
32 if (!checkedTypes.Contains(unitType))
33 {
34 if (toArmy.GetUnitTypeCount(unitType) > 0)
35 {
36 isApplicable = true;
37 break;
38 }
39
40 checkedTypes.Add(unitType);
41 }
42 }
43 }
44
45 return isApplicable;
46 }
47
48 public override string RequirementID
49 {
50 get
51 {
52 return REQUIREMENT_ID_RACE;
53 }
54 }
55 }
56 }
57