git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@2049 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
145
OpenRa.Game/TechTree/Item.cs
Normal file
145
OpenRa.Game/TechTree/Item.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenRa.FileFormats;
|
||||
|
||||
namespace OpenRa.TechTree
|
||||
{
|
||||
public class Item
|
||||
{
|
||||
readonly bool isStructure;
|
||||
|
||||
public bool IsStructure { get { return isStructure; } }
|
||||
|
||||
public Item(string tag, string friendlyName, IniSection section, bool isStructure)
|
||||
{
|
||||
this.tag = tag;
|
||||
this.friendlyName = friendlyName;
|
||||
this.isStructure = isStructure;
|
||||
|
||||
owner = ParseOwner(section);
|
||||
techLevel = ParseTechLevel(section);
|
||||
Tuple<string[], string[]> pre = ParsePrerequisites(section, tag);
|
||||
alliedPrerequisites = pre.a;
|
||||
sovietPrerequisites = pre.b;
|
||||
}
|
||||
|
||||
static int ParseTechLevel(IniSection section)
|
||||
{
|
||||
return int.Parse(section.GetValue("TechLevel", "-1"));
|
||||
}
|
||||
|
||||
static Race ParseOwner(IniSection section)
|
||||
{
|
||||
if (section.GetValue("DoubleOwned", "No") == "Yes")
|
||||
return Race.Allies | Race.Soviet;
|
||||
|
||||
Race race = Race.None;
|
||||
string[] frags = section.GetValue("Owner", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (string s in frags)
|
||||
race |= (Race)Enum.Parse(typeof(Race), s, true);
|
||||
|
||||
return race;
|
||||
}
|
||||
|
||||
static Tuple<string[],string[]> ParsePrerequisites(IniSection section, string tag)
|
||||
{
|
||||
List<string> allied = new List<string>(section.GetValue("Prerequisite", "").ToUpper().Split(
|
||||
new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
|
||||
|
||||
List<string> soviet = new List<string>(allied);
|
||||
|
||||
if (allied.Remove("STEK"))
|
||||
allied.Add("ATEK");
|
||||
|
||||
if (soviet.Remove("ATEK"))
|
||||
soviet.Add("STEK");
|
||||
|
||||
if (soviet.Remove("TENT"))
|
||||
soviet.Add("BARR");
|
||||
|
||||
if (allied.Remove("BARR"))
|
||||
allied.Add("TENT");
|
||||
|
||||
if ((tag.Length == 2 && tag[0] == 'E') || tag == "MEDI" || tag == "THF" || tag == "SPY")
|
||||
{
|
||||
if (!allied.Contains("TENT"))
|
||||
allied.Add("TENT");
|
||||
if (!soviet.Contains("BARR"))
|
||||
soviet.Add("BARR");
|
||||
}
|
||||
|
||||
if (tag == "LST")
|
||||
{
|
||||
if (!soviet.Contains("SPEN"))
|
||||
soviet.Add("SPEN");
|
||||
|
||||
if (!allied.Contains("SYRD"))
|
||||
allied.Add("SYRD");
|
||||
}
|
||||
|
||||
return new Tuple<string[], string[]>(
|
||||
allied.ToArray(), soviet.ToArray());
|
||||
}
|
||||
|
||||
public readonly string tag, friendlyName;
|
||||
readonly int techLevel;
|
||||
readonly Race owner;
|
||||
readonly string[] alliedPrerequisites, sovietPrerequisites;
|
||||
|
||||
bool ShouldMakeBuildable(IEnumerable<string> buildings, string[] racePrerequisites)
|
||||
{
|
||||
if (techLevel < 0)
|
||||
return false;
|
||||
|
||||
if (racePrerequisites.Length == 0)
|
||||
return true;
|
||||
|
||||
List<string> p = new List<string>(racePrerequisites);
|
||||
foreach (string b in buildings)
|
||||
p.Remove(b);
|
||||
|
||||
return p.Count == 0;
|
||||
}
|
||||
|
||||
bool ShouldMakeUnbuildable(IEnumerable<string> buildings, string[] racePrerequisites)
|
||||
{
|
||||
if (racePrerequisites.Length == 0)
|
||||
return false;
|
||||
|
||||
List<string> p = new List<string>(racePrerequisites);
|
||||
foreach (string b in buildings)
|
||||
p.Remove(b);
|
||||
|
||||
return p.Count == racePrerequisites.Length;
|
||||
}
|
||||
|
||||
void CheckForBoth(IEnumerable<string> buildings)
|
||||
{
|
||||
if (canBuild && (ShouldMakeUnbuildable(buildings, alliedPrerequisites) && ShouldMakeUnbuildable(buildings, sovietPrerequisites)))
|
||||
canBuild = false;
|
||||
|
||||
else if (!canBuild && (ShouldMakeBuildable(buildings, alliedPrerequisites) || ShouldMakeBuildable(buildings, sovietPrerequisites)))
|
||||
canBuild = true;
|
||||
}
|
||||
|
||||
public void CheckPrerequisites(IEnumerable<string> buildings, Race currentRace)
|
||||
{
|
||||
if (currentRace == Race.None || currentRace == (Race.Allies | Race.Soviet))
|
||||
CheckForBoth(buildings);
|
||||
else
|
||||
{
|
||||
string[] racePrerequisites = (currentRace == Race.Allies) ? alliedPrerequisites : sovietPrerequisites;
|
||||
|
||||
if ((canBuild && ShouldMakeUnbuildable(buildings, racePrerequisites)) || !((owner & currentRace) == currentRace))
|
||||
canBuild = false;
|
||||
else if (!canBuild && ShouldMakeBuildable(buildings, racePrerequisites))
|
||||
canBuild = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool canBuild;
|
||||
public bool CanBuild { get { return canBuild; } }
|
||||
public string Tooltip { get { return string.Format("{0} ({1})\n{2}", friendlyName, tag, owner); } }
|
||||
}
|
||||
}
|
||||
110
OpenRa.Game/TechTree/TechTree.cs
Normal file
110
OpenRa.Game/TechTree/TechTree.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
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<string, Item> objects = new Dictionary<string, Item>();
|
||||
public ICollection<string> built = new List<string>();
|
||||
|
||||
Race currentRace = Race.None;
|
||||
|
||||
public Race CurrentRace
|
||||
{
|
||||
get { return currentRace; }
|
||||
set
|
||||
{
|
||||
currentRace = value;
|
||||
CheckAll();
|
||||
}
|
||||
}
|
||||
|
||||
public TechTree()
|
||||
{
|
||||
LoadRules();
|
||||
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()
|
||||
{
|
||||
IEnumerable<Tuple<string, string, bool>> definitions = Concat(
|
||||
Lines("buildings.txt", true),
|
||||
Lines("units.txt", false));
|
||||
|
||||
var rules = SharedResources.Rules;
|
||||
|
||||
foreach (Tuple<string, string, bool> 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<Item> BuildableItems
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (Item b in objects.Values)
|
||||
if (b.CanBuild)
|
||||
yield return b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user