Deleted TechTreeTest and moved the useful stuff into a dll

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1193 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
beedee
2007-07-13 10:09:33 +00:00
parent 4975d808fa
commit 07eb099b3b
15 changed files with 29 additions and 537 deletions

161
OpenRa.TechTree/Item.cs Normal file
View File

@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenRa.FileFormats;
using System.IO;
namespace OpenRa.TechTree
{
public class Item
{
public Item(string tag, string friendlyName, IniSection section, bool isStructure)
{
this.tag = tag;
this.friendlyName = friendlyName;
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;
}
public void CheckPrerequisites(IEnumerable<string> buildings, Race currentRace)
{
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; } }
Bitmap icon;
public Bitmap Icon
{
get { return icon ?? (icon = LoadIcon(tag)); }
}
static Package package = new Package("../../../hires.mix");
static Palette palette = new Palette(File.OpenRead("../../../temperat.pal"));
static Bitmap LoadIcon(string tag)
{
string filename = tag + "icon.shp";
try
{
Stream s = package.GetContent(filename);
ShpReader reader = new ShpReader(s);
foreach (ImageHeader h in reader)
return BitmapBuilder.FromBytes(h.Image, reader.Size, palette);
return null;
}
catch (FileNotFoundException) { return LoadIcon("dog"); }
}
public string Tooltip
{
get
{
return string.Format("{0} ({1})\n{2}", friendlyName, tag, owner);
}
}
}
}

View File

@@ -0,0 +1,56 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2BFC3861-E90E-4F77-B254-8FB8285E43AC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OpenRa.TechTree</RootNamespace>
<AssemblyName>OpenRa.TechTree</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Item.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Race.cs" />
<Compile Include="TechTree.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRa.FileFormats\OpenRa.FileFormats.csproj">
<Project>{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}</Project>
<Name>OpenRa.FileFormats</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("OpenRa.TechTree")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("TOSHIBA")]
[assembly: AssemblyProduct("OpenRa.TechTree")]
[assembly: AssemblyCopyright("Copyright © TOSHIBA 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("af5f7d2f-f905-4a72-9542-f86acbe508ec")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

14
OpenRa.TechTree/Race.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenRa.TechTree
{
[Flags]
public enum Race
{
None = 0,
Allies = 1,
Soviet = 2
}
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
using System.IO;
using System.Text.RegularExpressions;
namespace OpenRa.TechTree
{
public class TechTree
{
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;
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 = new IniFile(File.OpenRead("../../../rules.ini"));
IEnumerable<Tuple<string, string, bool>> definitions = Concat(
Lines("../../../buildings.txt", true),
Lines("../../../units.txt", false));
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)
{
Item b = objects[key];
if (!b.CanBuild) return false;
built.Add(key);
CheckAll();
return true;
}
public bool Unbuild(string key)
{
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;
}
}
}
}