Files
OpenRA/OpenRa.TechTreeTest/Building.cs
beedee 572533978b Fixed special owner cases
git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1136 993157c7-ee19-0410-b2c4-bb4e9862e678
2007-07-07 08:06:59 +00:00

127 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
using System.Drawing;
using System.IO;
namespace OpenRa.TechTreeTest
{
[Flags]
public enum BuildingRace
{
None = 0,
Allies = 1,
Soviet = 2
}
class Building
{
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; }
}
BuildingRace owner;
public BuildingRace 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)
{
if (buildable && ShouldMakeUnbuildable(buildings))
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"); }
}
}
}