| 1 | // This file (Army.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 | |
|---|
| 5 | using System;
|
|---|
| 6 | using System.IO; |
|---|
| 7 | using System.Collections.Generic;
|
|---|
| 8 | using System.Text;
|
|---|
| 9 | using System.Xml;
|
|---|
| 10 | using IBBoard.WarFoundry.API; |
|---|
| 11 | using IBBoard.WarFoundry.API.Factories; |
|---|
| 12 | using IBBoard.WarFoundry.API.Requirements; |
|---|
| 13 | using ICSharpCode.SharpZipLib.Zip;
|
|---|
| 14 |
|
|---|
| 15 | namespace IBBoard.WarFoundry.API.Objects
|
|---|
| 16 | {
|
|---|
| 17 | /// <summary>
|
|---|
| 18 | /// Summary description for Army.
|
|---|
| 19 | /// </summary>
|
|---|
| 20 | public class Army : WarFoundryObject
|
|---|
| 21 | {
|
|---|
| 22 | //private GameSystem system;
|
|---|
| 23 | private Race armyRace;
|
|---|
| 24 | private int maxPoints;
|
|---|
| 25 | private double pointsTotal;
|
|---|
| 26 | private Dictionary<Category, ArmyCategory> categories;
|
|---|
| 27 |
|
|---|
| 28 | public event ObjectAddDelegate UnitAdded;
|
|---|
| 29 | public event ObjectRemoveDelegate UnitRemoved;
|
|---|
| 30 | public event FailedUnitRequirementDelegate FailedRequirement;
|
|---|
| 31 | public event DoubleValChangedDelegate PointsValueChanged;
|
|---|
| 32 | private DoubleValChangedDelegate PointsValueChangedMethod; |
|---|
| 33 | |
|---|
| 34 | public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null) |
|---|
| 35 | { |
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName)
|
|---|
| 39 | {
|
|---|
| 40 | armyRace = race;
|
|---|
| 41 | Name = armyName;
|
|---|
| 42 | maxPoints = maxArmyPoints;
|
|---|
| 43 | PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
|
|---|
| 44 | } |
|---|
| 45 |
|
|---|
| 46 | public ArmyCategory GetCategory(Category cat)
|
|---|
| 47 | {
|
|---|
| 48 | ArmyCategory armyCat = null;
|
|---|
| 49 | ArmyCategories.TryGetValue(cat, out armyCat);
|
|---|
| 50 | return armyCat;
|
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | private Dictionary<Category, ArmyCategory> ArmyCategories |
|---|
| 54 | { |
|---|
| 55 | get |
|---|
| 56 | { |
|---|
| 57 | if (categories==null) |
|---|
| 58 | { |
|---|
| 59 | categories = new Dictionary<Category, ArmyCategory>(); |
|---|
| 60 | Category[] raceCats = Race.Categories; |
|---|
| 61 | ArmyCategory cat; |
|---|
| 62 | int raceCatCount = raceCats.Length; |
|---|
| 63 | |
|---|
| 64 | for (int i = 0; i < raceCatCount; i++) |
|---|
| 65 | { |
|---|
| 66 | Category raceCat = raceCats[i]; |
|---|
| 67 | cat = new ArmyCategory(this, raceCat); |
|---|
| 68 | categories[raceCat] = cat; |
|---|
| 69 | cat.PointsValueChanged+= PointsValueChangedMethod; |
|---|
| 70 | cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded); |
|---|
| 71 | cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | return categories; |
|---|
| 76 | } |
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public ArmyCategory[] Categories
|
|---|
| 80 | {
|
|---|
| 81 | get
|
|---|
| 82 | {
|
|---|
| 83 | return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public Race Race
|
|---|
| 88 | {
|
|---|
| 89 | get { return armyRace; }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | public GameSystem GameSystem
|
|---|
| 93 | {
|
|---|
| 94 | get { return (armyRace!=null ? armyRace.GameSystem : null); }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | protected void OnUnitAdded(Unit unit)
|
|---|
| 98 | {
|
|---|
| 99 | OnUnitAdded(unit, null);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
|
|---|
| 103 | {
|
|---|
| 104 | if (UnitAdded!=null)
|
|---|
| 105 | {
|
|---|
| 106 | UnitAdded(unit);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0)
|
|---|
| 110 | {
|
|---|
| 111 | FailedRequirement(failedReqs);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | protected void OnUnitRemoved(Unit unit)
|
|---|
| 116 | {
|
|---|
| 117 | OnUnitRemoved(unit, null);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
|
|---|
| 121 | {
|
|---|
| 122 | if (UnitRemoved!=null)
|
|---|
| 123 | {
|
|---|
| 124 | UnitRemoved(unit);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | if (FailedRequirement!=null && failedReqs!=null && failedReqs.Count > 0)
|
|---|
| 128 | {
|
|---|
| 129 | FailedRequirement(failedReqs);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | private void OnPointsValueChanged(double oldValue, double newValue)
|
|---|
| 134 | {
|
|---|
| 135 | if (PointsValueChanged!=null)
|
|---|
| 136 | {
|
|---|
| 137 | PointsValueChanged(this, oldValue, newValue);
|
|---|
| 138 | }
|
|---|
| 139 | } |
|---|
| 140 |
|
|---|
| 141 | private double TotalPoints
|
|---|
| 142 | {
|
|---|
| 143 | get { return pointsTotal; }
|
|---|
| 144 | set
|
|---|
| 145 | {
|
|---|
| 146 | double oldPoints = pointsTotal;
|
|---|
| 147 | pointsTotal = value;
|
|---|
| 148 |
|
|---|
| 149 | if (oldPoints!=pointsTotal)
|
|---|
| 150 | {
|
|---|
| 151 | OnPointsValueChanged(oldPoints, pointsTotal);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | public double PointsTotal
|
|---|
| 157 | {
|
|---|
| 158 | get { return TotalPoints; }
|
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | public void AddUnit(Unit unit) |
|---|
| 162 | { |
|---|
| 163 | List<FailedUnitRequirement> failedReqs = CanAddUnit(unit); |
|---|
| 164 | unit.Army = this; |
|---|
| 165 | ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
|---|
| 166 | armyCat.AddUnit(unit); |
|---|
| 167 | OnUnitAdded(unit, failedReqs); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | public void RemoveUnit(Unit unit) |
|---|
| 171 | { |
|---|
| 172 | List<FailedUnitRequirement> failedReqs = CanRemoveUnit(unit); |
|---|
| 173 | unit.Army = null; |
|---|
| 174 | ArmyCategory armyCat = GetCategory(unit.UnitType.MainCategory); |
|---|
| 175 | armyCat.RemoveUnit(unit); |
|---|
| 176 | OnUnitRemoved(unit, failedReqs); |
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | public Unit[] GetUnits(Category cat)
|
|---|
| 180 | {
|
|---|
| 181 | return GetUnits(this.GetCategory(cat));
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public Unit[] GetUnits(ArmyCategory cat)
|
|---|
| 185 | {
|
|---|
| 186 | return cat.GetUnits();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | public Unit[] GetUnits()
|
|---|
| 190 | {
|
|---|
| 191 | List<Unit> fullList = new List<Unit>();
|
|---|
| 192 |
|
|---|
| 193 | foreach(ArmyCategory cat in Categories)
|
|---|
| 194 | { |
|---|
| 195 | fullList.AddRange(cat.GetUnits());
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | return fullList.ToArray();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | public int MaxPoints
|
|---|
| 202 | {
|
|---|
| 203 | get { return maxPoints; }
|
|---|
| 204 | set
|
|---|
| 205 | {
|
|---|
| 206 | if (value > 0)
|
|---|
| 207 | {
|
|---|
| 208 | maxPoints = value;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
|
|---|
| 214 | {
|
|---|
| 215 | if (obj is ArmyCategory)
|
|---|
| 216 | {
|
|---|
| 217 | double points = 0;
|
|---|
| 218 |
|
|---|
| 219 | foreach (ArmyCategory cat in Categories)
|
|---|
| 220 | {
|
|---|
| 221 | points+= cat.PointsTotal;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | TotalPoints = points;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | public List<FailedUnitRequirement> CanAddUnit(Unit unit)
|
|---|
| 229 | {
|
|---|
| 230 | return CanAddUnitType(unit.UnitType);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType)
|
|---|
| 234 | {
|
|---|
| 235 | return unitType.CanAddToArmy(this);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | public List<FailedUnitRequirement> CanRemoveUnit(Unit unit)
|
|---|
| 239 | {
|
|---|
| 240 | return CanRemoveUnitType(unit.UnitType);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType)
|
|---|
| 244 | {
|
|---|
| 245 | return unitType.CanRemoveFromArmy(this);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | public int GetUnitTypeCount(UnitType unitType)
|
|---|
| 249 | {
|
|---|
| 250 | int count = 0;
|
|---|
| 251 |
|
|---|
| 252 | foreach (ArmyCategory cat in Categories)
|
|---|
| 253 | {
|
|---|
| 254 | count+= cat.GetUnitTypeCount(unitType);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | return count;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | private void Army_UnitAdded(object val)
|
|---|
| 261 | {
|
|---|
| 262 | OnUnitAdded((Unit)val);
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | private void Army_UnitRemoved(object val)
|
|---|
| 266 | {
|
|---|
| 267 | OnUnitRemoved((Unit)val);
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|