git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1159 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
beedee
2007-07-10 06:54:58 +00:00
parent 21f4b812e6
commit 8070b4a480
6 changed files with 173 additions and 25 deletions

View File

@@ -8,14 +8,14 @@ using System.IO;
namespace OpenRa.TechTreeTest namespace OpenRa.TechTreeTest
{ {
[Flags] [Flags]
public enum BuildingRace public enum Race
{ {
None = 0, None = 0,
Allies = 1, Allies = 1,
Soviet = 2 Soviet = 2
} }
class Building class Building : IRAUnit
{ {
readonly string friendlyName; readonly string friendlyName;
readonly string tag; readonly string tag;
@@ -46,9 +46,9 @@ namespace OpenRa.TechTreeTest
set { techLevel = value; } set { techLevel = value; }
} }
BuildingRace owner; Race owner;
public BuildingRace Owner public Race Owner
{ {
get { return owner; } get { return owner; }
set { owner = value; } set { owner = value; }
@@ -90,7 +90,7 @@ namespace OpenRa.TechTreeTest
bool buildable = false; bool buildable = false;
public bool Buildable { get { return buildable; } } public bool Buildable { get { return buildable; } }
public void CheckPrerequisites(IEnumerable<string> buildings, BuildingRace currentRace) public void CheckPrerequisites(IEnumerable<string> buildings, Race currentRace)
{ {
if ((buildable && ShouldMakeUnbuildable(buildings)) || !((owner & currentRace) == currentRace)) if ((buildable && ShouldMakeUnbuildable(buildings)) || !((owner & currentRace) == currentRace))
buildable = false; buildable = false;

View File

@@ -10,7 +10,7 @@ namespace OpenRa.TechTreeTest
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
TechTree techTree = new TechTree(BuildingRace.Soviet); TechTree techTree = new TechTree(Race.Allies);
public Form1() public Form1()
{ {
@@ -22,7 +22,7 @@ namespace OpenRa.TechTreeTest
{ {
buildableItems.Controls.Clear(); buildableItems.Controls.Clear();
foreach (Building b in techTree.BuildableItems) foreach (IRAUnit b in techTree.BuildableItems)
{ {
PictureBox box = new PictureBox(); PictureBox box = new PictureBox();
box.SizeMode = PictureBoxSizeMode.AutoSize; box.SizeMode = PictureBoxSizeMode.AutoSize;
@@ -32,13 +32,13 @@ namespace OpenRa.TechTreeTest
buildableItems.Controls.Add(box); buildableItems.Controls.Add(box);
Building k = b; IRAUnit k = b;
box.Click += delegate { Build(k); }; box.Click += delegate { Build(k); };
} }
} }
void Build(Building b) void Build(IRAUnit b)
{ {
techTree.Build(b.Tag); techTree.Build(b.Tag);
RefreshList(); RefreshList();

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Drawing;
namespace OpenRa.TechTreeTest
{
interface IRAUnit
{
bool Buildable { get; }
void CheckPrerequisites(IEnumerable<string> units, Race currentRace);
string FriendlyName { get; }
Bitmap Icon { get; }
Race Owner { get; set; }
string[] Prerequisites { get; set; }
bool ShouldMakeBuildable(IEnumerable<string> units);
bool ShouldMakeUnbuildable(IEnumerable<string> units);
string Tag { get; }
int TechLevel { get; set; }
}
}

View File

@@ -43,6 +43,7 @@
<Compile Include="Form1.Designer.cs"> <Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="IRAUnit.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx"> <EmbeddedResource Include="Form1.resx">
@@ -68,6 +69,7 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile> </Compile>
<Compile Include="TechTree.cs" /> <Compile Include="TechTree.cs" />
<Compile Include="Unit.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRa.FileFormats\OpenRa.FileFormats.csproj"> <ProjectReference Include="..\OpenRa.FileFormats\OpenRa.FileFormats.csproj">

View File

@@ -9,14 +9,15 @@ namespace OpenRa.TechTreeTest
{ {
class TechTree class TechTree
{ {
Dictionary<string, Building> buildings = new Dictionary<string, Building>(); Dictionary<string, IRAUnit> units = new Dictionary<string, IRAUnit>();
public ICollection<string> built = new List<string>(); public ICollection<string> built = new List<string>();
readonly BuildingRace currentRace; readonly Race currentRace;
public TechTree(BuildingRace race) public TechTree(Race race)
{ {
this.currentRace = race; this.currentRace = race;
LoadBuildings(); LoadBuildings();
LoadUnits();
LoadRules(); LoadRules();
built.Add("FACT"); built.Add("FACT");
@@ -27,10 +28,10 @@ namespace OpenRa.TechTreeTest
{ {
IniFile rulesFile; IniFile rulesFile;
rulesFile = new IniFile(File.OpenRead("../../../rules.ini")); rulesFile = new IniFile(File.OpenRead("../../../rules.ini"));
foreach (string key in buildings.Keys) foreach (string key in units.Keys)
{ {
IniSection section = rulesFile.GetSection(key); IniSection section = rulesFile.GetSection(key);
Building b = buildings[key]; IRAUnit b = units[key];
string s = section.GetValue("Prerequisite", "").ToUpper(); string s = section.GetValue("Prerequisite", "").ToUpper();
b.Prerequisites = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); b.Prerequisites = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
b.TechLevel = int.Parse(section.GetValue("TechLevel", "-1")); b.TechLevel = int.Parse(section.GetValue("TechLevel", "-1"));
@@ -38,21 +39,25 @@ namespace OpenRa.TechTreeTest
if (string.IsNullOrEmpty(s)) if (string.IsNullOrEmpty(s))
{ {
b.Owner = BuildingRace.None; s = section.GetValue("DoubleOwned", "No");
if (s.Equals("Yes", StringComparison.InvariantCultureIgnoreCase))
b.Owner = Race.Allies | Race.Soviet;
else
b.Owner = Race.None;
continue; continue;
} }
if (s.Equals("Both", StringComparison.InvariantCultureIgnoreCase)) if (s.Equals("Both", StringComparison.InvariantCultureIgnoreCase))
{ {
b.Owner = BuildingRace.Allies | BuildingRace.Soviet; b.Owner = Race.Allies | Race.Soviet;
continue; continue;
} }
string[] frags = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] frags = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (frags.Length > 1) if (frags.Length > 1)
b.Owner = BuildingRace.Allies | BuildingRace.Soviet; b.Owner = Race.Allies | Race.Soviet;
else else
b.Owner = (BuildingRace)Enum.Parse(typeof(BuildingRace), frags[0], true); b.Owner = (Race)Enum.Parse(typeof(Race), frags[0], true);
} }
} }
@@ -63,13 +68,24 @@ namespace OpenRa.TechTreeTest
Regex pattern = new Regex(@"^(\w+),([\w ]+)$"); Regex pattern = new Regex(@"^(\w+),([\w ]+)$");
Match m = pattern.Match(line); Match m = pattern.Match(line);
if (!m.Success) continue; if (!m.Success) continue;
buildings.Add(m.Groups[1].Value, new Building(m.Groups[1].Value, m.Groups[2].Value)); 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));
} }
} }
public bool Build(string key) public bool Build(string key)
{ {
Building b = buildings[key]; IRAUnit b = units[key];
if (!b.Buildable) return false; if (!b.Buildable) return false;
built.Add(key); built.Add(key);
CheckAll(); CheckAll();
@@ -78,7 +94,7 @@ namespace OpenRa.TechTreeTest
public bool Unbuild(string key) public bool Unbuild(string key)
{ {
Building b = buildings[key]; IRAUnit b = units[key];
if (!built.Contains(key)) return false; if (!built.Contains(key)) return false;
built.Remove(key); built.Remove(key);
CheckAll(); CheckAll();
@@ -87,15 +103,15 @@ namespace OpenRa.TechTreeTest
void CheckAll() void CheckAll()
{ {
foreach (Building building in buildings.Values) foreach (IRAUnit unit in units.Values)
building.CheckPrerequisites(built, currentRace); unit.CheckPrerequisites(built, currentRace);
} }
public IEnumerable<Building> BuildableItems public IEnumerable<IRAUnit> BuildableItems
{ {
get get
{ {
foreach (Building b in buildings.Values) foreach (IRAUnit b in units.Values)
if (b.Buildable) if (b.Buildable)
yield return b; yield return b;
} }

111
OpenRa.TechTreeTest/Unit.cs Normal file
View File

@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenRa.FileFormats;
using System.IO;
namespace OpenRa.TechTreeTest
{
class Unit : IRAUnit
{
bool buildable;
public bool Buildable { get { return buildable; } }
public void CheckPrerequisites(IEnumerable<string> units, Race currentRace)
{
if ((buildable && ShouldMakeUnbuildable(units)) || !((owner & currentRace) == currentRace))
buildable = false;
else if (!buildable && ShouldMakeBuildable(units))
buildable = true; ;
}
readonly string friendlyName;
public string FriendlyName { get { return friendlyName; } }
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.Width, reader.Height, palette);
return null;
}
catch (FileNotFoundException) { return LoadIcon("dog"); }
}
Race owner;
public Race Owner
{
get { return owner; }
set { owner = value; }
}
string[] prerequisites;
public string[] Prerequisites
{
get { return prerequisites; }
set { prerequisites = value; }
}
public bool ShouldMakeBuildable(IEnumerable<string> units)
{
if (techLevel > 10 || techLevel < 0)
return false;
if (prerequisites.Length == 0)
return true;
List<string> p = new List<string>(prerequisites);
foreach (string b in units)
p.Remove(b);
return p.Count == 0;
}
public bool ShouldMakeUnbuildable(IEnumerable<string> units)
{
if (prerequisites.Length == 0)
return false;
List<string> p = new List<string>(prerequisites);
foreach (string b in units)
p.Remove(b);
return p.Count == prerequisites.Length;
}
readonly string tag;
public string Tag
{
get { return tag; }
}
int techLevel;
public int TechLevel
{
get { return techLevel; }
set { techLevel = value; }
}
public Unit(string tag, string friendlyName)
{
this.friendlyName = friendlyName;
this.tag = tag;
}
}
}