Mercurial > repos > IBDev-IBBoard.WarFoundry.API
diff api/Objects/Race.cs @ 0:520818033bb6
Initial commit of WarFoundry code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 19 Dec 2008 15:57:51 +0000 |
parents | |
children | 613bc5eaac59 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Objects/Race.cs Fri Dec 19 15:57:51 2008 +0000 @@ -0,0 +1,339 @@ +// Race.cs +// +// Copyright (C) 2008 IBBoard +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// + +using System; +using System.Collections.Generic; +using System.IO; +using System.Xml; +using IBBoard.IO; +using IBBoard.WarFoundry.API.Factories; + +namespace IBBoard.WarFoundry.API.Objects +{ + public class Race : WarFoundryObject + { + public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; + + private string subID; + private string systemID; + private GameSystem system; + private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; + private Dictionary<string, UnitType> unitTypes; + private Dictionary<string, EquipmentItem> equipment; + private Dictionary<string, Ability> abilities; + private Category[] cats; + private FileInfo sourceFile; + + public Race(string raceID, string raceName, string gameSystemID) : this(raceID, "", raceName, gameSystemID) + { + } + + public Race(string raceID, string raceSubID, string raceName, string gameSystemID) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName) + { + subID = (raceSubID == null ? "" : raceSubID); + systemID = gameSystemID; + } + + public string SubID + { + get { return subID; } + set { subID = (value == null ? "" : value.Trim()); } + } + + public FileInfo SourceFile + { + get { return sourceFile; } + set { sourceFile = value; } + } + + public GameSystem GameSystem + { + get + { + if (system == null) + { + system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); + } + + return system; + } + set + { + if (value == null) + { + throw new ArgumentException("Game system for a race cannot be null"); + } + + system = value; + } + } + + /// <summary> + /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID. + /// </summary> + /// <param name="id"> + /// The ID of the category to get + /// </param> + /// <returns> + /// The <code>Category</code> with the specified ID, or null if one doesn't exist. + /// </returns> + public Category GetCategory(string id) + { + Category[] categories = RaceCategories; + Category cat = null; + + for (int i = 0; i<categories.Length; i++) + { + if (categories[i].ID == id) + { + cat = categories[i]; + break; + } + } + + return cat; + } + + /// <summary> + /// Gets a category based on its index within the list. Ordering is defined by the game system definition or by the race's overrides. + /// </summary> + /// <param name="index"> + /// A <see cref="System.Int32"/> + /// </param> + /// <returns> + /// A <see cref="Category"/> + /// </returns> + public virtual Category GetCategory(int index) + { + if (cats == null) + { + return GameSystem.GetCategory(index); + } + else + { + return Categories[index]; + } + } + + public Category[] Categories + { + get + { + return RaceCategories; + } + + set + { + RaceOverrideCategories = value; + } + } + + /*private virtual Category[] GetCategories() + { + if (cats==null) + { + return GameSystem.Categories; + } + else + { + return cats; + } + }*/ + + public void SetEquipmentItems(Dictionary<string, EquipmentItem> items) + { + equipment = items; + } + + public virtual EquipmentItem GetEquipmentItem(string id) + { + return (EquipmentItem)equipment[id]; + } + + public List<EquipmentItem> GetEquipmentList() + { + List<EquipmentItem> items = new List<EquipmentItem>(); + + foreach (EquipmentItem item in equipment.Values) + { + items.Add(item); + } + + return items; + } + + public virtual bool HasCategoryOverrides() + { + return cats!=null; + } + + public void SetUnitTypes(Dictionary<string, UnitType> types) + { + unitTypes = types; + } + + public UnitType[] GetUnitTypes(Category cat) + { + if (unitTypesByCat==null) + { + unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); + + foreach (Category category in RaceCategories) + { + unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); + } + + Dictionary<string,UnitType> catUnitTypes; + + foreach (UnitType unit in unitTypes.Values) + { + unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes); + + if (catUnitTypes == null) + { + throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name)); + } + + catUnitTypes.Add(unit.ID, unit); + } + } + + ICollection<UnitType> col = unitTypesByCat[cat].Values; + UnitType[] toRet = new UnitType[col.Count]; + int i = 0; + + foreach (UnitType type in col) + { + toRet[i++] = type; + } + + return toRet; + } + + public UnitType GetUnitType(string id) + { + return (UnitType)unitTypes[id]; + } + + public List<Ability> GetAbilityList() + { + List<Ability> items = new List<Ability>(); + + foreach (Ability ability in abilities.Values) + { + items.Add(ability); + } + + return items; + } + + public void SetAbilities(Dictionary<string, Ability> newAbilities) + { + abilities = newAbilities; + } + + public Ability GetAbility(string id) + { + Ability ability = null; + abilities.TryGetValue(id, out ability); + return ability; + } + + protected Category[] RaceCategories + { + get + { + Category[] cats = RaceOverrideCategories; + + if (cats == null) + { + //No overrides, so load system categories + cats = GameSystem.Categories; + } + + return cats; + } + } + + protected virtual Category[] RaceOverrideCategories + { + get + { + return RaceRawOverrideCategories; + } + set + { + RaceRawOverrideCategories = value; + } + } + + protected Category[] RaceRawOverrideCategories + { + get { return cats; } + set + { + if (value!=null && value.Length>0) + { + cats = value; + } + else + { + cats = null; + } + } + } + + protected virtual Dictionary<string, UnitType> RaceUnitTypes + { + get { return RaceRawUnitTypes; } + set { RaceRawUnitTypes = value; } + } + + protected virtual Dictionary<string, EquipmentItem> RaceEquipment + { + get { return RaceRawEquipment; } + set { RaceRawEquipment = value; } + } + + protected virtual Dictionary<string, Ability> RaceAbilities + { + get { return RaceRawAbilities; } + set { RaceRawAbilities = value; } + } + + protected Dictionary<string, UnitType> RaceRawUnitTypes + { + get { return unitTypes; } + set { unitTypes = value; } + } + + protected Dictionary<string, EquipmentItem> RaceRawEquipment + { + get { return equipment; } + set { equipment = value; } + } + + protected Dictionary<string, Ability> RaceRawAbilities + { + get { return abilities; } + set { abilities = value; } + } + } +}