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

This commit is contained in:
chrisf
2008-03-25 00:45:43 +00:00
parent eb7094d49e
commit 33e856345f
16 changed files with 126 additions and 179 deletions

148
OpenRa.Game/Item.cs Normal file
View File

@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
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); } }
}
}

View File

@@ -71,10 +71,13 @@
<Compile Include="Building.cs" />
<Compile Include="Game.cs" />
<Compile Include="IOrderGenerator.cs" />
<Compile Include="Item.cs" />
<Compile Include="Network\Packet.cs" />
<Compile Include="Player.cs" />
<Compile Include="PlayerOwned.cs" />
<Compile Include="Race.cs" />
<Compile Include="Rules.cs" />
<Compile Include="SharedResources.cs" />
<Compile Include="Sheet.cs" />
<Compile Include="Log.cs" />
<Compile Include="Network\Network.cs" />
@@ -99,6 +102,7 @@
<Compile Include="Sprite.cs" />
<Compile Include="SpriteRenderer.cs" />
<Compile Include="SpriteSheetBuilder.cs" />
<Compile Include="TechTree.cs" />
<Compile Include="TerrainCosts.cs" />
<Compile Include="TerrainRenderer.cs" />
<Compile Include="Tree.cs" />
@@ -121,10 +125,6 @@
<Project>{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}</Project>
<Name>OpenRa.FileFormats</Name>
</ProjectReference>
<ProjectReference Include="..\OpenRa.TechTree\OpenRa.TechTree.csproj">
<Project>{2BFC3861-E90E-4F77-B254-8FB8285E43AC}</Project>
<Name>OpenRa.TechTree</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">

14
OpenRa.Game/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

@@ -12,7 +12,7 @@ namespace OpenRa.Game
static Rules()
{
IniFile rulesIni = new IniFile(FileSystem.Open("rules.ini"));
var rulesIni = SharedResources.Rules;
foreach (string line in Util.ReadAllLines(FileSystem.Open("units.txt")))
{

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
using IjwFramework.Types;
namespace OpenRa.Game
{
class SharedResources
{
static Lazy<IniFile> rules = new Lazy<IniFile>( () => new IniFile( FileSystem.Open( "rules.ini" )));
public static IniFile Rules { get { return rules.Value; } }
}
}

View File

@@ -13,6 +13,8 @@ namespace OpenRa.Game
public readonly RectangleF uv;
public readonly float2 size;
readonly float2[] uvhax;
internal Sprite(Sheet sheet, Rectangle bounds, TextureChannel channel)
{
this.bounds = bounds;
@@ -25,6 +27,14 @@ namespace OpenRa.Game
(float)(bounds.Width) / sheet.Size.Width,
(float)(bounds.Height) / sheet.Size.Height);
uvhax = new float2[]
{
MapTextureCoords( new float2(0,0) ),
MapTextureCoords( new float2(1,0) ),
MapTextureCoords( new float2(0,1) ),
MapTextureCoords( new float2(1,1) ),
};
this.size = new float2(bounds.Size);
}
@@ -35,8 +45,11 @@ namespace OpenRa.Game
p.Y > 0 ? uv.Bottom : uv.Top);
}
public float2 Size { get { return size; } }
}
public float2 FastMapTextureCoords(int k)
{
return uvhax[k];
}
}
public enum TextureChannel
{

110
OpenRa.Game/TechTree.cs Normal file
View 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;
}
}
}
}

View File

@@ -36,7 +36,7 @@ namespace OpenRa.Game
static TerrainCosts()
{
IniFile file = new IniFile(FileSystem.Open("rules.ini"));
IniFile file = SharedResources.Rules;
for( int i = 0 ; i < 10 ; i++ )
{

View File

@@ -17,17 +17,24 @@ namespace OpenRa.Game
return new float2(paletteLine / 16.0f, channelSelect[(int)channel]);
}
public static Vertex MakeVertex(float2 o, float2 uv, Sprite r, int palette)
static float2 KLerp(float2 o, float2 d, int k)
{
switch (k)
{
case 0: return o;
case 1: return new float2(o.X + d.X, o.Y);
case 2: return new float2(o.X, o.Y + d.Y);
case 3: return new float2(o.X + d.X, o.Y + d.Y);
default: throw new InvalidOperationException();
}
}
static Vertex MakeVertex(float2 o, int k, Sprite r, float2 attrib)
{
return new Vertex(
float2.Lerp( o, o + r.Size, uv ),
r.MapTextureCoords(uv),
EncodeVertexAttributes(r.channel, palette));
}
static float Lerp(float a, float b, float t)
{
return (1 - t) * a + t * b;
KLerp( o, r.size, k ),
r.FastMapTextureCoords(k),
attrib);
}
public static string[] ReadAllLines(Stream s)
@@ -49,28 +56,27 @@ namespace OpenRa.Game
return result;
}
static float2[] uv =
{
new float2( 0,0 ),
new float2( 1,0 ),
new float2( 0,1 ),
new float2( 1,1 ),
};
public static void CreateQuad(List<Vertex> vertices, List<ushort> indices, float2 o, Sprite r, int palette)
{
ushort offset = (ushort)vertices.Count;
float2 attrib = EncodeVertexAttributes(r.channel, palette);
foreach( float2 p in uv )
vertices.Add(Util.MakeVertex(o, p, r, palette));
Vertex[] v = new Vertex[]
{
Util.MakeVertex(o, 0, r, attrib),
Util.MakeVertex(o, 1, r, attrib),
Util.MakeVertex(o, 2, r, attrib),
Util.MakeVertex(o, 3, r, attrib),
};
indices.Add(offset);
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 2));
vertices.AddRange(v);
indices.Add((ushort)(offset + 1));
indices.Add((ushort)(offset + 3));
indices.Add((ushort)(offset + 2));
ushort[] i = new ushort[]
{
offset, (ushort)(offset + 1), (ushort)(offset + 2), (ushort)(offset + 1), (ushort)(offset + 3), (ushort)(offset + 2)
};
indices.AddRange(i);
}
public static void FastCopyIntoChannel(Sprite dest, byte[] src)