pwned lots of code

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1160 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
chrisf
2007-07-10 07:37:19 +00:00
parent 8070b4a480
commit 7add484d5d
8 changed files with 181 additions and 328 deletions

View File

@@ -9,84 +9,57 @@ namespace OpenRa.TechTreeTest
{
class TechTree
{
Dictionary<string, IRAUnit> units = new Dictionary<string, IRAUnit>();
Dictionary<string, Item> objects = new Dictionary<string, Item>();
public ICollection<string> built = new List<string>();
readonly Race currentRace;
public TechTree(Race race)
{
this.currentRace = race;
LoadBuildings();
LoadUnits();
LoadRules();
built.Add("FACT");
CheckAll();
}
static IEnumerable<T> Concat<T>(IEnumerable<T> one, IEnumerable<T> two)
{
foreach (T t in one)
yield return t;
foreach (T t in two)
yield return t;
}
IEnumerable<Tuple<string, string, bool>> 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<string, string, bool>(
m.Groups[1].Value, m.Groups[2].Value, param);
}
}
void LoadRules()
{
IniFile rulesFile;
rulesFile = new IniFile(File.OpenRead("../../../rules.ini"));
foreach (string key in units.Keys)
{
IniSection section = rulesFile.GetSection(key);
IRAUnit b = units[key];
string s = section.GetValue("Prerequisite", "").ToUpper();
b.Prerequisites = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
b.TechLevel = int.Parse(section.GetValue("TechLevel", "-1"));
s = section.GetValue("Owner", "");
if (string.IsNullOrEmpty(s))
{
s = section.GetValue("DoubleOwned", "No");
if (s.Equals("Yes", StringComparison.InvariantCultureIgnoreCase))
b.Owner = Race.Allies | Race.Soviet;
else
b.Owner = Race.None;
continue;
}
if (s.Equals("Both", StringComparison.InvariantCultureIgnoreCase))
{
b.Owner = Race.Allies | Race.Soviet;
continue;
}
string[] frags = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (frags.Length > 1)
b.Owner = Race.Allies | Race.Soviet;
else
b.Owner = (Race)Enum.Parse(typeof(Race), frags[0], true);
}
}
IniFile rulesFile = new IniFile(File.OpenRead("../../../rules.ini"));
IEnumerable<Tuple<string, string, bool>> definitions = Concat(
Lines("../../../buildings.txt", true),
Lines("../../../units.txt", false));
void LoadBuildings()
{
foreach (string line in File.ReadAllLines("../../../buildings.txt"))
{
Regex pattern = new Regex(@"^(\w+),([\w ]+)$");
Match m = pattern.Match(line);
if (!m.Success) continue;
units.Add(m.Groups[1].Value, new Building(m.Groups[1].Value, m.Groups[2].Value));
}
}
void LoadUnits()
{
foreach (string line in File.ReadAllLines("../../../units.txt"))
{
Regex pattern = new Regex(@"^(\w+),([\w ]+)$");
Match m = pattern.Match(line);
if (!m.Success) continue;
units.Add(m.Groups[1].Value, new Unit(m.Groups[1].Value, m.Groups[2].Value));
}
foreach (Tuple<string, string, bool> p in definitions)
objects.Add(p.a, new Item(p.a, p.b, rulesFile.GetSection(p.a), p.c));
}
public bool Build(string key)
{
IRAUnit b = units[key];
if (!b.Buildable) return false;
Item b = objects[key];
if (!b.CanBuild) return false;
built.Add(key);
CheckAll();
return true;
@@ -94,7 +67,7 @@ namespace OpenRa.TechTreeTest
public bool Unbuild(string key)
{
IRAUnit b = units[key];
Item b = objects[key];
if (!built.Contains(key)) return false;
built.Remove(key);
CheckAll();
@@ -103,16 +76,16 @@ namespace OpenRa.TechTreeTest
void CheckAll()
{
foreach (IRAUnit unit in units.Values)
foreach (Item unit in objects.Values)
unit.CheckPrerequisites(built, currentRace);
}
public IEnumerable<IRAUnit> BuildableItems
public IEnumerable<Item> BuildableItems
{
get
{
foreach (IRAUnit b in units.Values)
if (b.Buildable)
foreach (Item b in objects.Values)
if (b.CanBuild)
yield return b;
}
}