using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; using OpenRa.FileFormats; using OpenRa.Game; namespace OpenRa.TechTree { public class TechTree { Dictionary objects = new Dictionary(); public ICollection built = new List(); Race currentRace = Race.None; public Race CurrentRace { get { return currentRace; } set { currentRace = value; CheckAll(); } } public TechTree() { LoadRules(); CheckAll(); } static IEnumerable Concat(IEnumerable one, IEnumerable two) { foreach (T t in one) yield return t; foreach (T t in two) yield return t; } IEnumerable> Lines(string filename, bool param) { Regex pattern = new Regex(@"^(\w+),([\w ]+)$"); foreach (string s in File.ReadAllLines("../../../" + filename)) { Match m = pattern.Match(s); if (m == null || !m.Success) continue; yield return new Tuple( m.Groups[1].Value, m.Groups[2].Value, param); } } void LoadRules() { IEnumerable> definitions = Concat( Lines("buildings.txt", true), Lines("units.txt", false)); var rules = SharedResources.Rules; foreach (Tuple p in definitions) objects.Add(p.a, new Item(p.a, p.b, rules.GetSection(p.a), p.c)); } public bool Build(string key, bool force) { if( string.IsNullOrEmpty( key ) ) return false; key = key.ToUpperInvariant(); Item b = objects[ key ]; if (!force && !b.CanBuild) return false; built.Add(key); CheckAll(); return true; } public bool Build(string key) { return Build(key, false); } public bool Unbuild(string key) { key = key.ToUpperInvariant(); Item b = objects[key]; if (!built.Contains(key)) return false; built.Remove(key); CheckAll(); return true; } void CheckAll() { foreach (Item unit in objects.Values) unit.CheckPrerequisites(built, currentRace); } public IEnumerable BuildableItems { get { foreach (Item b in objects.Values) if (b.CanBuild) yield return b; } } } }