diff --git a/OpenRa.TechTreeTest/Building.cs b/OpenRa.TechTreeTest/Building.cs new file mode 100644 index 0000000000..2fe40ed1d3 --- /dev/null +++ b/OpenRa.TechTreeTest/Building.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenRa.TechTreeTest +{ + class Building + { + readonly string friendlyName; + + public string FriendlyName + { + get { return friendlyName; } + } + + string[] prerequisites; + + public string[] Prerequisites + { + get { return prerequisites; } + set { prerequisites = value; } + } + + int techLevel; + + public int TechLevel + { + get { return techLevel; } + set { techLevel = value; } + } + + public Building(string friendlyName) + { + this.friendlyName = friendlyName; + } + + public bool ShouldMakeBuildable(IEnumerable buildings) + { + List p = new List(prerequisites); + foreach (string b in buildings) + p.Remove(b); + + return p.Count == 0; + } + + public bool ShouldMakeUnbuildable(IEnumerable buildings) + { + List p = new List(prerequisites); + foreach (string b in buildings) + p.Remove(b); + + return p.Count == prerequisites.Length; + } + + bool buildable = false; + public bool Buildable { get { return buildable; } } + + public void CheckPrerequisites(IEnumerable buildings) + { + if (buildable && ShouldMakeUnbuildable(buildings)) + buildable = false; + else if (!buildable && ShouldMakeBuildable(buildings)) + buildable = true; + } + } +} diff --git a/OpenRa.TechTreeTest/Form1.cs b/OpenRa.TechTreeTest/Form1.cs index 9602c31201..6a5d019eca 100644 --- a/OpenRa.TechTreeTest/Form1.cs +++ b/OpenRa.TechTreeTest/Form1.cs @@ -10,6 +10,7 @@ namespace OpenRa.TechTreeTest { public partial class Form1 : Form { + TechTree techTree; public Form1() { InitializeComponent(); diff --git a/OpenRa.TechTreeTest/TechTree.cs b/OpenRa.TechTreeTest/TechTree.cs index 85504056c1..8d9988efe5 100644 --- a/OpenRa.TechTreeTest/TechTree.cs +++ b/OpenRa.TechTreeTest/TechTree.cs @@ -10,6 +10,7 @@ namespace OpenRa.TechTreeTest class TechTree { Dictionary buildings = new Dictionary(); + List built; public TechTree() { LoadBuildings(); @@ -40,65 +41,31 @@ namespace OpenRa.TechTreeTest buildings.Add(m.Groups[0].Value, new Building(m.Groups[1].Value)); } } - } - class Building - { - readonly string friendlyName; - - public string FriendlyName + public bool Build(string key) { - get { return friendlyName; } - } - - string[] prerequisites; - - public string[] Prerequisites - { - get { return prerequisites; } - set { prerequisites = value; } + Building b = buildings[key]; + if (!b.Buildable) return false; + built.Add(key); + CheckAll(); + return true; } - int techLevel; - - public int TechLevel + public bool Unbuild(string key) { - get { return techLevel; } - set { techLevel = value; } + Building b = buildings[key]; + if (!built.Contains(key)) return false; + built.Remove(key); + CheckAll(); + return true; } - public Building(string friendlyName) + void CheckAll() { - this.friendlyName = friendlyName; - } - - public bool ShouldMakeBuildable(IEnumerable buildings) - { - List p = new List(prerequisites); - foreach (string b in buildings) - p.Remove(b); - - return p.Count == 0; - } - - public bool ShouldMakeUnbuildable(IEnumerable buildings) - { - List p = new List(prerequisites); - foreach (string b in buildings) - p.Remove(b); - - return p.Count == prerequisites.Length; - } - - bool buildable = false; - public bool Buildable { get { return buildable; } } - - public void CheckPrerequisites(IEnumerable buildings) - { - if (buildable && ShouldMakeUnbuildable(buildings)) - buildable = false; - else if (!buildable && ShouldMakeBuildable(buildings)) - buildable = true; + foreach (Building building in buildings.Values) + { + building.CheckPrerequisites(built); + } } } }