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

@@ -1,126 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
using System.Drawing;
using System.IO;
namespace OpenRa.TechTreeTest
{
[Flags]
public enum Race
{
None = 0,
Allies = 1,
Soviet = 2
}
class Building : IRAUnit
{
readonly string friendlyName;
readonly string tag;
public string FriendlyName
{
get { return friendlyName; }
}
public string Tag
{
get { return tag; }
}
string[] prerequisites;
public string[] Prerequisites
{
get { return prerequisites; }
set { prerequisites = value; }
}
int techLevel;
public int TechLevel
{
get { return techLevel; }
set { techLevel = value; }
}
Race owner;
public Race Owner
{
get { return owner; }
set { owner = value; }
}
public Building(string tag, string friendlyName)
{
this.friendlyName = friendlyName;
this.tag = tag;
}
public bool ShouldMakeBuildable(IEnumerable<string> buildings)
{
if (techLevel > 10 || techLevel < 0)
return false;
if (prerequisites.Length == 0)
return true;
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == 0;
}
public bool ShouldMakeUnbuildable(IEnumerable<string> buildings)
{
if (prerequisites.Length == 0)
return false;
List<string> p = new List<string>(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<string> buildings, Race currentRace)
{
if ((buildable && ShouldMakeUnbuildable(buildings)) || !((owner & currentRace) == currentRace))
buildable = false;
else if (!buildable && ShouldMakeBuildable(buildings))
buildable = true;
}
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"); }
}
}
}

View File

@@ -22,25 +22,25 @@ namespace OpenRa.TechTreeTest
{
buildableItems.Controls.Clear();
foreach (IRAUnit b in techTree.BuildableItems)
foreach (Item b in techTree.BuildableItems)
{
PictureBox box = new PictureBox();
box.SizeMode = PictureBoxSizeMode.AutoSize;
box.Image = b.Icon;
toolTip1.SetToolTip(box, b.Tag + "\n" + b.Owner.ToString());
toolTip1.SetToolTip(box, b.Tooltip);
buildableItems.Controls.Add(box);
IRAUnit k = b;
Item k = b;
box.Click += delegate { Build(k); };
}
}
void Build(IRAUnit b)
void Build(Item b)
{
techTree.Build(b.Tag);
techTree.Build(b.tag);
RefreshList();
}
}

View File

@@ -1,19 +0,0 @@
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; }
}
}

123
OpenRa.TechTreeTest/Item.cs Normal file
View File

@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenRa.FileFormats;
using System.IO;
namespace OpenRa.TechTreeTest
{
class Item
{
public Item(string tag, string friendlyName, IniSection section, bool isStructure)
{
this.tag = tag;
this.friendlyName = friendlyName;
owner = ParseOwner(section);
techLevel = ParseTechLevel(section);
prerequisites = ParsePrerequisites(section);
}
static int ParseTechLevel(IniSection section)
{
return int.Parse(section.GetValue("TechLevel", "-1"));
}
static string[] ParsePrerequisites(IniSection section)
{
return section.GetValue("Prerequisite", "").ToUpper().Split(
new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
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;
}
public readonly string tag, friendlyName;
readonly int techLevel;
readonly Race owner;
readonly string[] prerequisites;
bool ShouldMakeBuildable(IEnumerable<string> buildings)
{
if (techLevel < 0)
return false;
if (prerequisites.Length == 0)
return true;
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == 0;
}
bool ShouldMakeUnbuildable(IEnumerable<string> buildings)
{
if (prerequisites.Length == 0)
return false;
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == prerequisites.Length;
}
public void CheckPrerequisites(IEnumerable<string> buildings, Race currentRace)
{
if ((canBuild && ShouldMakeUnbuildable(buildings)) || !((owner & currentRace) == currentRace))
canBuild = false;
else if (!canBuild && ShouldMakeBuildable(buildings))
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.Width, reader.Height, palette);
return null;
}
catch (FileNotFoundException) { return LoadIcon("dog"); }
}
public string Tooltip
{
get
{
return string.Format("{0} ({1})\n{2}", friendlyName, tag, owner);
}
}
}
}

View File

@@ -36,14 +36,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Building.cs" />
<Compile Include="Item.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="IRAUnit.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
@@ -68,8 +67,8 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Race.cs" />
<Compile Include="TechTree.cs" />
<Compile Include="Unit.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRa.FileFormats\OpenRa.FileFormats.csproj">

View File

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

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;
}
}

View File

@@ -1,111 +0,0 @@
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;
}
}
}