Reorganise FileFormats

This commit is contained in:
Paul Chote
2010-04-01 14:25:08 +13:00
committed by Bob
parent 5a95c8a071
commit 8a64209d89
27 changed files with 124 additions and 28 deletions

View File

@@ -0,0 +1,35 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.FileFormats
{
public struct ActorReference
{
public readonly int2 Location;
public readonly string Name;
public readonly string Owner;
public ActorReference( string name, int2 location, string owner )
{
Name = name;
Location = location;
Owner = owner;
}
}
}

View File

@@ -0,0 +1,268 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenRA.FileFormats
{
public class Map
{
public readonly string Title;
public readonly string Theater;
public readonly int INIFormat;
public readonly int MapSize;
public readonly int XOffset;
public readonly int YOffset;
public int2 Offset { get { return new int2( XOffset, YOffset ); } }
public readonly int Width;
public readonly int Height;
public int2 Size { get { return new int2(Width, Height); } }
public readonly TileReference[ , ] MapTiles;
public readonly List<ActorReference> Actors = new List<ActorReference>();
public readonly IEnumerable<int2> SpawnPoints;
static string Truncate( string s, int maxLength )
{
return s.Length <= maxLength ? s : s.Substring(0,maxLength );
}
public Map(string filename)
{
IniFile file = new IniFile(FileSystem.Open(filename));
IniSection basic = file.GetSection("Basic");
Title = basic.GetValue("Name", "(null)");
INIFormat = int.Parse(basic.GetValue("NewINIFormat", "0"));
IniSection map = file.GetSection("Map");
Theater = Truncate(map.GetValue("Theater", "TEMPERAT"), 8);
XOffset = int.Parse(map.GetValue("X", "0"));
YOffset = int.Parse(map.GetValue("Y", "0"));
Width = int.Parse(map.GetValue("Width", "0"));
Height = int.Parse(map.GetValue("Height", "0"));
MapSize = (INIFormat == 3) ? 128 : 64;
MapTiles = new TileReference[ MapSize, MapSize ];
for (int j = 0; j < MapSize; j++)
for (int i = 0; i < MapSize; i++)
MapTiles[i, j] = new TileReference();
if (INIFormat == 3) // RA map
{
UnpackRATileData(ReadPackedSection(file.GetSection("MapPack")));
UnpackRAOverlayData(ReadPackedSection(file.GetSection("OverlayPack")));
ReadRATrees(file);
}
else // CNC
{
UnpackCncTileData(FileSystem.Open(filename.Substring(0,filename.Length-4)+".bin"));
ReadCncOverlay(file);
ReadCncTrees(file);
}
LoadActors(file, "STRUCTURES");
LoadActors(file, "UNITS");
LoadActors(file, "INFANTRY");
SpawnPoints = file.GetSection("Waypoints")
.Where(kv => int.Parse(kv.Value) > 0)
.Select(kv => Pair.New(int.Parse(kv.Key), new int2(int.Parse(kv.Value) % MapSize, int.Parse(kv.Value) / MapSize)))
.Where(a => a.First < 8)
.Select(a => a.Second)
.ToArray();
}
static MemoryStream ReadPackedSection(IniSection mapPackSection)
{
StringBuilder sb = new StringBuilder();
for (int i = 1; ; i++)
{
string line = mapPackSection.GetValue(i.ToString(), null);
if (line == null)
break;
sb.Append(line.Trim());
}
byte[] data = Convert.FromBase64String(sb.ToString());
List<byte[]> chunks = new List<byte[]>();
BinaryReader reader = new BinaryReader(new MemoryStream(data));
try
{
while (true)
{
uint length = reader.ReadUInt32() & 0xdfffffff;
byte[] dest = new byte[8192];
byte[] src = reader.ReadBytes((int)length);
/*int actualLength =*/ Format80.DecodeInto(src, dest);
chunks.Add(dest);
}
}
catch (EndOfStreamException) { }
MemoryStream ms = new MemoryStream();
foreach (byte[] chunk in chunks)
ms.Write(chunk, 0, chunk.Length);
ms.Position = 0;
return ms;
}
static byte ReadByte( Stream s )
{
int ret = s.ReadByte();
if( ret == -1 )
throw new NotImplementedException();
return (byte)ret;
}
static ushort ReadWord(Stream s)
{
ushort ret = ReadByte(s);
ret |= (ushort)(ReadByte(s) << 8);
return ret;
}
void UnpackRATileData( MemoryStream ms )
{
for( int i = 0 ; i < MapSize ; i++ )
for( int j = 0 ; j < MapSize ; j++ )
MapTiles[j, i].tile = ReadWord(ms);
for( int i = 0 ; i < MapSize ; i++ )
for( int j = 0 ; j < MapSize ; j++ )
{
MapTiles[j, i].image = (byte)ms.ReadByte();
if( MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff )
MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 );
}
}
static string[] raOverlayNames =
{
"sbag", "cycl", "brik", "fenc", "wood",
"gold01", "gold02", "gold03", "gold04",
"gem01", "gem02", "gem03", "gem04",
"v12", "v13", "v14", "v15", "v16", "v17", "v18",
"fpls", "wcrate", "scrate", "barb", "sbag",
};
void UnpackRAOverlayData( MemoryStream ms )
{
for( int i = 0 ; i < MapSize ; i++ )
for( int j = 0 ; j < MapSize ; j++ )
{
byte o = ReadByte( ms );
MapTiles[ j, i ].overlay = (o == 255) ? null : raOverlayNames[o];
}
}
void ReadRATrees( IniFile file )
{
IniSection terrain = file.GetSection( "TERRAIN", true );
if( terrain == null )
return;
foreach( KeyValuePair<string, string> kv in terrain )
{
var loc = int.Parse( kv.Key );
Actors.Add( new ActorReference(kv.Value, new int2(loc % MapSize, loc / MapSize), null ) );
}
}
void UnpackCncTileData( Stream ms )
{
for( int i = 0 ; i < MapSize ; i++ )
for( int j = 0 ; j < MapSize ; j++ )
{
MapTiles[j, i].tile = (byte)ms.ReadByte();
MapTiles[j, i].image = (byte)ms.ReadByte();
if( MapTiles[ j, i ].tile == 0xff )
MapTiles[ j, i ].image = (byte)( i % 4 + ( j % 4 ) * 4 );
}
}
void ReadCncOverlay( IniFile file )
{
IniSection overlay = file.GetSection( "OVERLAY", true );
if( overlay == null )
return;
foreach( KeyValuePair<string, string> kv in overlay )
{
var loc = int.Parse( kv.Key );
int2 cell = new int2(loc % MapSize, loc / MapSize);
MapTiles[ cell.X, cell.Y ].overlay = kv.Value.ToLower();
}
}
void ReadCncTrees( IniFile file )
{
IniSection terrain = file.GetSection( "TERRAIN", true );
if( terrain == null )
return;
foreach( KeyValuePair<string, string> kv in terrain )
{
var loc = int.Parse( kv.Key );
Actors.Add( new ActorReference( kv.Value.Split(',')[0], new int2(loc % MapSize, loc / MapSize),null));
}
}
void LoadActors(IniFile file, string section)
{
foreach (var s in file.GetSection(section, true))
{
//num=owner,type,health,location,facing,...
var parts = s.Value.Split( ',' );
var loc = int.Parse(parts[3]);
Actors.Add( new ActorReference( parts[1].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), parts[0]));
}
}
public bool IsInMap(int2 xy)
{
return IsInMap(xy.X,xy.Y);
}
public bool IsInMap(int x, int y)
{
return (x >= XOffset && y >= YOffset && x < XOffset + Width && y < YOffset + Height);
}
}
}

View File

@@ -0,0 +1,151 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenRA.FileFormats
{
public class NewMap
{
// General info
public byte MapFormat = 1;
public string Title;
public string Description;
public string Author;
public int PlayerCount;
public string Preview;
// 'Simple' map data
public string Tiledata;
public byte TileFormat = 1;
public string Tileset;
public int2 Size;
public int[] Bounds;
// 'Complex' map data
public TileReference[ , ] MapTiles;
public Dictionary<string, ActorReference> Actors = new Dictionary<string, ActorReference>();
public Dictionary<string, int2> Waypoints = new Dictionary<string, int2>();
public Dictionary<string, MiniYaml> Rules = new Dictionary<string, MiniYaml>();
List<string> SimpleFields = new List<string>() {
"MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "Size", "Tiledata", "Preview", "Bounds"
};
public NewMap(string filename)
{
var yaml = MiniYaml.FromFile(filename);
// 'Simple' metadata
foreach (var field in SimpleFields)
{
if (!yaml.ContainsKey(field)) continue;
FieldLoader.LoadField(this,field,yaml[field].Value);
}
// Waypoints
foreach (var wp in yaml["Waypoints"].Nodes)
{
string[] loc = wp.Value.Value.Split(',');
Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]),int.Parse(loc[1])));
}
// TODO: Players
// Actors
foreach (var kv in yaml["Actors"].Nodes.ToPairs())
{
string[] vals = kv.Second.Split(' ');
string[] loc = vals[2].Split(',');
var a = new ActorReference(vals[0], new int2(int.Parse(loc[0]),int.Parse(loc[1])) ,vals[1]);
Actors.Add(kv.First,a);
}
// Rules
Rules = yaml["Rules"].Nodes;
}
public void SaveBinaryData(string filepath)
{
FileStream dataStream = new FileStream(filepath+".tmp", FileMode.Create, FileAccess.Write);
BinaryWriter writer = new BinaryWriter( dataStream );
writer.BaseStream.Seek(0, SeekOrigin.Begin);
// File header consists of a version byte, followed by 2 ushorts for width and height
writer.Write(TileFormat);
writer.Write((ushort)Size.X);
writer.Write((ushort)Size.Y);
// Tile data is stored as a base-64 encoded stream of
// {(2-byte) tile index, (1-byte) image index} pairs
for( int i = 0 ; i < Size.X ; i++ )
for( int j = 0 ; j < Size.Y ; j++ )
{
writer.Write( MapTiles[j,i].tile );
// Semi-hack: Convert clear and water tiles to "pick an image for me" magic number
byte image = (MapTiles[ j, i ].tile == 0xff || MapTiles[ j, i ].tile == 0xffff) ? byte.MaxValue : MapTiles[j,i].image;
writer.Write(image);
}
// TODO: Need a proper resources array to write
/*
// Resource data is stored as a base-64 encoded stream of
// {(1-byte) resource index, (1-byte) image index} pairs
for( int i = 0 ; i < Size.X ; i++ )
for( int j = 0 ; j < Size.Y ; j++ )
{
byte type = 0;
byte image = 0;
if (MapTiles[j,i].overlay != null)
{
var res = resourceMapping[MapTiles[j,i].overlay];
type = res.First;
image = res.Second;
}
writer.Write(type);
writer.Write(image);
}
*/
writer.Flush();
}
public void DebugContents()
{
foreach (var field in SimpleFields)
Console.WriteLine("Loaded {0}: {1}", field, this.GetType().GetField(field).GetValue(this));
Console.WriteLine("Loaded Waypoints:");
foreach (var wp in Waypoints)
Console.WriteLine("\t{0} => {1}",wp.Key,wp.Value);
Console.WriteLine("Loaded Actors:");
foreach (var wp in Actors)
Console.WriteLine("\t{0} => {1} {2} {3}",wp.Key,wp.Value.Name, wp.Value.Owner,wp.Value.Location);
}
}
}

View File

@@ -0,0 +1,88 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.IO;
namespace OpenRA.FileFormats
{
public class Terrain
{
public readonly List<byte[]> TileBitmapBytes = new List<byte[]>();
public Terrain( Stream stream )
{
// Try loading as a cnc .tem
BinaryReader reader = new BinaryReader( stream );
int Width = reader.ReadUInt16();
int Height = reader.ReadUInt16();
if( Width != 24 || Height != 24 )
throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) );
/*NumTiles = */reader.ReadUInt16();
/*Zero1 = */reader.ReadUInt16();
/*uint Size = */reader.ReadUInt32();
uint ImgStart = reader.ReadUInt32();
/*Zero2 = */reader.ReadUInt32();
int IndexEnd, IndexStart;
if (reader.ReadUInt16() == 65535) // ID1 = FFFFh for cnc
{
/*ID2 = */reader.ReadUInt16();
IndexEnd = reader.ReadInt32();
IndexStart = reader.ReadInt32();
}
else // Load as a ra .tem
{
stream.Position = 0;
reader = new BinaryReader( stream );
Width = reader.ReadUInt16();
Height = reader.ReadUInt16();
if( Width != 24 || Height != 24 )
throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) );
/*NumTiles = */reader.ReadUInt16();
reader.ReadUInt16();
/*XDim = */reader.ReadUInt16();
/*YDim = */reader.ReadUInt16();
/*uint FileSize = */reader.ReadUInt32();
ImgStart = reader.ReadUInt32();
reader.ReadUInt32();
reader.ReadUInt32();
IndexEnd = reader.ReadInt32();
reader.ReadUInt32();
IndexStart = reader.ReadInt32();
}
stream.Position = IndexStart;
foreach( byte b in new BinaryReader(stream).ReadBytes(IndexEnd - IndexStart) )
{
if (b != 255)
{
stream.Position = ImgStart + b * 24 * 24;
TileBitmapBytes.Add(new BinaryReader(stream).ReadBytes(24 * 24));
}
else
TileBitmapBytes.Add(null);
}
}
}
}

View File

@@ -0,0 +1,69 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System;
namespace OpenRA.FileFormats
{
public class TerrainColorSet
{
public readonly Dictionary<TerrainType, Color> colors = new Dictionary<TerrainType, Color>();
string NextLine( StreamReader reader )
{
string ret;
do
{
ret = reader.ReadLine();
if( ret == null )
return null;
ret = ret.Trim();
}
while( ret.Length == 0 || ret[ 0 ] == ';' );
return ret;
}
public TerrainColorSet( string colorFile )
{
StreamReader file = new StreamReader( FileSystem.Open(colorFile) );
while( true )
{
string line = NextLine( file );
if( line == null )
break;
string[] kv = line.Split('=');
TerrainType key = (TerrainType)Enum.Parse(typeof(TerrainType),kv[0]);
string[] entries = kv[1].Split(',');
Color val = Color.FromArgb(int.Parse(entries[0]),int.Parse(entries[1]),int.Parse(entries[2]));
colors.Add(key,val);
}
file.Close();
}
public Color ColorForTerrainType(TerrainType type)
{
return colors[type];
}
}
}

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.FileFormats
{
public struct TileReference
{
public ushort tile;
public byte image;
public string overlay;
public byte smudge;
public override int GetHashCode() { return tile.GetHashCode() ^ image.GetHashCode(); }
public override bool Equals( object obj )
{
if( obj == null )
return false;
TileReference r = (TileReference)obj;
return ( r.image == image && r.tile == tile );
}
public static bool operator ==( TileReference a, TileReference b ) { return a.Equals( b ); }
public static bool operator !=( TileReference a, TileReference b ) { return !a.Equals( b ); }
}
}

View File

@@ -0,0 +1,117 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace OpenRA.FileFormats
{
public class TileSet
{
public readonly Dictionary<ushort, Terrain> tiles = new Dictionary<ushort, Terrain>();
public readonly Walkability Walkability;
public readonly Dictionary<ushort, TileTemplate> walk
= new Dictionary<ushort, TileTemplate>();
string NextLine( StreamReader reader )
{
string ret;
do
{
ret = reader.ReadLine();
if( ret == null )
return null;
ret = ret.Trim();
}
while( ret.Length == 0 || ret[ 0 ] == ';' );
return ret;
}
public TileSet( string tilesetFile, string templatesFile, string suffix )
{
Walkability = new Walkability(templatesFile);
char tileSetChar = char.ToUpperInvariant( suffix[ 0 ] );
StreamReader tileIdFile = new StreamReader( FileSystem.Open(tilesetFile) );
while( true )
{
string tileSetStr = NextLine( tileIdFile );
string countStr = NextLine( tileIdFile );
string startStr = NextLine( tileIdFile );
string pattern = NextLine( tileIdFile );
if( tileSetStr == null || countStr == null || startStr == null || pattern == null )
break;
if( tileSetStr.IndexOf( tileSetChar.ToString() ) == -1 )
continue;
int count = int.Parse( countStr );
int start = int.Parse( startStr, NumberStyles.HexNumber );
for( int i = 0 ; i < count ; i++ )
{
string tilename = string.Format(pattern, i + 1);
if (!walk.ContainsKey((ushort)(start + i)))
walk.Add((ushort)(start + i), Walkability.GetTileTemplate(tilename));
using( Stream s = FileSystem.Open( tilename + "." + suffix ) )
{
if( !tiles.ContainsKey( (ushort)( start + i ) ) )
tiles.Add( (ushort)( start + i ), new Terrain( s ) );
}
}
}
tileIdFile.Close();
}
public byte[] GetBytes(TileReference r)
{
Terrain tile;
if( tiles.TryGetValue( r.tile, out tile ) )
return tile.TileBitmapBytes[ r.image ];
byte[] missingTile = new byte[ 24 * 24 ];
for( int i = 0 ; i < missingTile.Length ; i++ )
missingTile[ i ] = 0x36;
return missingTile;
}
public TerrainType GetTerrainType(TileReference r)
{
if (r.tile == 0xff || r.tile == 0xffff)
r.image = 0;
try {
return walk[r.tile].TerrainType[r.image];
}
catch (KeyNotFoundException)
{
return 0; // Default zero (walkable)
}
}
}
}

View File

@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using System;
namespace OpenRA.FileFormats
{
public enum TerrainType : byte
{
Clear = 0,
Water = 1,
Road = 2,
Rock = 3,
Tree = 4,
River = 5,
Rough = 6,
Wall = 7,
Beach = 8,
Ore = 9,
Special = 10,
}
public class TileTemplate
{
public int Index; // not valid for `interior` stuff. only used for bridges.
public string Name;
public int2 Size;
public string Bridge;
public float HP;
public Dictionary<int, TerrainType> TerrainType = new Dictionary<int, TerrainType>();
}
public class Walkability
{
Dictionary<string, TileTemplate> templates
= new Dictionary<string,TileTemplate>();
public Walkability(string templatesFile)
{
var file = new IniFile( FileSystem.Open( templatesFile ) );
foreach (var section in file.Sections)
{
var tile = new TileTemplate
{
Size = new int2(
int.Parse(section.GetValue("width", "0")),
int.Parse(section.GetValue("height", "0"))),
TerrainType = section
.Where(p => p.Key.StartsWith("tiletype"))
.ToDictionary(
p => int.Parse(p.Key.Substring(8)),
p => (TerrainType)Enum.Parse(typeof(TerrainType),p.Value)),
Name = section.GetValue("Name", null).ToLowerInvariant(),
Bridge = section.GetValue("bridge", null),
HP = float.Parse(section.GetValue("hp", "0"))
};
tile.Index = -1;
int.TryParse(section.Name.Substring(3), out tile.Index);
templates[tile.Name] = tile;
}
}
public TileTemplate GetTileTemplate(string terrainName)
{
return templates[terrainName];
}
}
}