view api/Objects/Army.cs @ 325:e0580a009e75

Re #324: Add saving of Race and System data to files * Remove extra interfaces, as their replacements would be implementation specific and the break-down is now only a clean coding issue * Strip out extra code from Army and GameSystem saver
author IBBoard <dev@ibboard.co.uk>
date Sat, 12 Mar 2011 20:32:08 +0000
parents 6cb0fb78b9a6
children
line wrap: on
line source

// This file (Army.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
//
// 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.

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Factories;
using IBBoard.WarFoundry.API.Requirements;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for Army.
	/// </summary>
	public class Army : WarFoundryLoadedObject, ICostedWarFoundryObject
	{
		//private GameSystem system;
		private Race armyRace;
		private int maxPoints;
		private double pointsTotal;
		private Dictionary<Category, ArmyCategory> categories;

		public event ObjectAddDelegate UnitAdded;
		public event ObjectRemoveDelegate UnitRemoved;
		public event FailedUnitRequirementDelegate FailedRequirement;
		public event DoubleValChangedDelegate PointsValueChanged;
		private DoubleValChangedDelegate PointsValueChangedMethod;
		
		public Army(Race race, string armyName, int maxArmyPoints) : this(race, armyName, maxArmyPoints, null)
		{
		}

		public Army(Race race, string armyName, int maxArmyPoints, ZipFile file) : base(armyName)
		{
			armyRace = race;
			Name = armyName;
			maxPoints = maxArmyPoints;
			PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler);
		}
		
		public ArmyCategory GetCategory(Category cat)
		{
			ArmyCategory armyCat = null;
			ArmyCategories.TryGetValue(cat, out armyCat);
			return armyCat;
		}
		
		private Dictionary<Category, ArmyCategory> ArmyCategories
		{
			get
			{
				if (categories==null)
				{
					categories = new Dictionary<Category, ArmyCategory>();
					Category[] raceCats = Race.Categories;
					ArmyCategory cat;
					int raceCatCount = raceCats.Length;

					for (int i = 0; i < raceCatCount; i++)
					{
						Category raceCat = raceCats[i];
						cat = new ArmyCategory(this, raceCat);
						categories[raceCat] = cat;
						cat.PointsValueChanged+= PointsValueChangedMethod;
						cat.UnitAdded+=new ObjectAddDelegate(Army_UnitAdded);
						cat.UnitRemoved+=new ObjectRemoveDelegate(Army_UnitRemoved);
						cat.FailedRequirement+=new FailedUnitRequirementDelegate(Army_FailedRequirement);
					}
				}
				
				return categories;
			}
		}

		public ArmyCategory[] Categories
		{
			get 
			{
				return DictionaryUtils.ToArray<Category, ArmyCategory>(ArmyCategories);
			}
		}

		public Race Race
		{
			get { return armyRace; }
		}

		public GameSystem GameSystem
		{
			get { return (armyRace!=null ? armyRace.GameSystem : null); }
		}

		protected void OnUnitAdded(Unit unit)
		{
			OnUnitAdded(unit, null);
		}

		protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs)
		{
			if (UnitAdded != null)
			{
				UnitAdded(unit);
			}

			OnFailedRequirement(failedReqs);
		}

		protected void OnUnitRemoved(Unit unit)
		{
			OnUnitRemoved(unit, null);
		}

		protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs)
		{
			if (UnitRemoved!=null)
			{
				UnitRemoved(unit);
			}

			OnFailedRequirement(failedReqs);
		}

		protected void OnFailedRequirement(List<FailedUnitRequirement> failedReqs)
		{
			if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0)
			{
				FailedRequirement(failedReqs);
			}
		}

		private void OnPointsValueChanged(double oldValue, double newValue)
		{
			if (PointsValueChanged!=null)
			{
				PointsValueChanged(this, oldValue, newValue);
			}
		}
		
		private double TotalPoints
		{
			get { return pointsTotal; }
			set
			{
				double oldPoints = pointsTotal;
				pointsTotal = value;

				if (oldPoints!=pointsTotal)
				{
					OnPointsValueChanged(oldPoints, pointsTotal);
				}
			}
		}
		
		public double Points
		{
			get { return TotalPoints; }
		}
		
		public void AddUnit(Unit unit)
		{
			Category category = unit.UnitType.MainCategory;
			AddUnit(unit, category);
		}
		
		public void AddUnit(Unit unit, Category category)
		{			
			ArmyCategory armyCat = GetCategory(category);
			armyCat.AddUnit(unit);
		}
		
		public void RemoveUnit(Unit unit)
		{
			unit.Category.RemoveUnit(unit);
		}

		public Unit[] GetUnits(Category cat)
		{
			return GetUnits(this.GetCategory(cat));
		}

		public Unit[] GetUnits(ArmyCategory cat)
		{
			return cat.GetUnits();
		}

		public Unit[] GetUnits()
		{
			List<Unit> fullList = new List<Unit>();

			foreach(ArmyCategory cat in Categories)
			{
				fullList.AddRange(cat.GetUnits());
			}

			return fullList.ToArray();
		}

		public int MaxPoints
		{
			get { return maxPoints; }
			set 
			{
				if (value > 0)
				{
					maxPoints = value;
				}
			}
		}

		private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
		{
			if (obj is ArmyCategory)
			{
				double points = 0;

				foreach (ArmyCategory cat in Categories)
				{
					points+= cat.Points;
				}

				TotalPoints = points;
			}
		}

		public List<FailedUnitRequirement> CanAddUnit(Unit unit)
		{
			return CanAddUnitType(unit.UnitType);
		}

		public List<FailedUnitRequirement> CanAddUnitType(UnitType unitType)
		{
			return unitType.CanAddToArmy(this);
		}

		public List<FailedUnitRequirement> CanRemoveUnit(Unit unit)
		{
			return CanRemoveUnitType(unit.UnitType);
		}

		public List<FailedUnitRequirement> CanRemoveUnitType(UnitType unitType)
		{
			return unitType.CanRemoveFromArmy(this);
		}

		public int GetUnitTypeCount(UnitType unitType)
		{
			int count = 0;

			foreach (ArmyCategory cat in Categories)
			{
				count+= cat.GetUnitTypeCount(unitType);
			}

			return count;
		}

		private void Army_UnitAdded(WarFoundryObject val)
		{
			OnUnitAdded((Unit)val);
		}

		private void Army_UnitRemoved(WarFoundryObject val)
		{
			OnUnitRemoved((Unit)val);
		}

		private void Army_FailedRequirement(List<FailedUnitRequirement> val)
		{
			OnFailedRequirement(val);
		}
	}
}