Refactor map-actor loading

This commit is contained in:
Paul Chote
2010-03-03 20:04:07 +13:00
parent ae2bbae16c
commit 97bdb9b63f
8 changed files with 57 additions and 65 deletions

View File

@@ -18,23 +18,18 @@
*/ */
#endregion #endregion
using System.Drawing;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public struct TreeReference public struct ActorReference
{ {
public readonly int X; public readonly int2 Location;
public readonly int Y; public readonly string Name;
public readonly string Image; public readonly string Owner;
public ActorReference( string name, int2 location, string owner )
public TreeReference(int xy, string image, int mapSize)
{ {
X = xy % mapSize; Name = name;
Y = xy / mapSize; Location = location;
Image = image; Owner = owner;
} }
public Point Location { get { return new Point(X, Y); } }
} }
} }

View File

@@ -42,7 +42,7 @@ namespace OpenRA.FileFormats
public int2 Size { get { return new int2(Width, Height); } } public int2 Size { get { return new int2(Width, Height); } }
public readonly TileReference[ , ] MapTiles; public readonly TileReference[ , ] MapTiles;
public readonly List<TreeReference> Trees = new List<TreeReference>(); public readonly List<ActorReference> Actors = new List<ActorReference>();
public readonly IEnumerable<int2> SpawnPoints; public readonly IEnumerable<int2> SpawnPoints;
@@ -55,8 +55,6 @@ namespace OpenRA.FileFormats
public Map(IniFile file) public Map(IniFile file)
{ {
IniSection basic = file.GetSection("Basic"); IniSection basic = file.GetSection("Basic");
Title = basic.GetValue("Name", "(null)"); Title = basic.GetValue("Name", "(null)");
BinaryPart = basic.GetValue("BinaryPart", "scm02ea.bin"); BinaryPart = basic.GetValue("BinaryPart", "scm02ea.bin");
@@ -88,8 +86,7 @@ namespace OpenRA.FileFormats
{ {
UnpackTileData(ReadPackedSection(file.GetSection("MapPack"))); UnpackTileData(ReadPackedSection(file.GetSection("MapPack")));
UnpackOverlayData(ReadPackedSection(file.GetSection("OverlayPack"))); UnpackOverlayData(ReadPackedSection(file.GetSection("OverlayPack")));
ReadTrees(file); ReadRATrees(file);
} }
else // CNC else // CNC
{ {
@@ -97,6 +94,10 @@ namespace OpenRA.FileFormats
ReadCncTrees(file); ReadCncTrees(file);
} }
LoadActors(file, "STRUCTURES");
LoadActors(file, "UNITS");
LoadActors(file, "INFANTRY");
SpawnPoints = file.GetSection("Waypoints") SpawnPoints = file.GetSection("Waypoints")
.Select(kv => Pair.New(int.Parse(kv.Key), new int2(int.Parse(kv.Value) % MapSize, int.Parse(kv.Value) / MapSize))) .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) .Where(a => a.First < 8)
@@ -195,14 +196,17 @@ namespace OpenRA.FileFormats
MapTiles[ j, i ].overlay = ReadByte( ms ); MapTiles[ j, i ].overlay = ReadByte( ms );
} }
void ReadTrees( IniFile file ) void ReadRATrees( IniFile file )
{ {
IniSection terrain = file.GetSection( "TERRAIN", true ); IniSection terrain = file.GetSection( "TERRAIN", true );
if( terrain == null ) if( terrain == null )
return; return;
foreach( KeyValuePair<string, string> kv in terrain ) foreach( KeyValuePair<string, string> kv in terrain )
Trees.Add( new TreeReference( int.Parse( kv.Key ), kv.Value, MapSize ) ); {
var loc = int.Parse( kv.Key );
Actors.Add( new ActorReference(kv.Value, new int2(loc % MapSize, loc / MapSize), null ) );
}
} }
void ReadCncTrees( IniFile file ) void ReadCncTrees( IniFile file )
@@ -212,8 +216,21 @@ namespace OpenRA.FileFormats
return; return;
foreach( KeyValuePair<string, string> kv in terrain ) foreach( KeyValuePair<string, string> kv in terrain )
// HACK: remove the ,none from the end {
Trees.Add( new TreeReference( int.Parse( kv.Key ), kv.Value.Substring(0,kv.Value.Length-5), MapSize ) ); var loc = int.Parse( kv.Key );
Actors.Add( new ActorReference( kv.Value.Substring(0,kv.Value.Length-5), 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(int x, int y) public bool IsInMap(int x, int y)

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -90,10 +90,10 @@
<Compile Include="Terrain.cs" /> <Compile Include="Terrain.cs" />
<Compile Include="TileReference.cs" /> <Compile Include="TileReference.cs" />
<Compile Include="TileSet.cs" /> <Compile Include="TileSet.cs" />
<Compile Include="TreeReference.cs" />
<Compile Include="Tuple.cs" /> <Compile Include="Tuple.cs" />
<Compile Include="TypeDictionary.cs" /> <Compile Include="TypeDictionary.cs" />
<Compile Include="Walkability.cs" /> <Compile Include="Walkability.cs" />
<Compile Include="ActorReference.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -29,12 +29,10 @@ namespace OpenRA.FileFormats
public Terrain( Stream stream ) public Terrain( Stream stream )
{ {
int Width, Height, IndexEnd, IndexStart;
uint ImgStart;
// Try loading as a cnc .tem // Try loading as a cnc .tem
BinaryReader reader = new BinaryReader( stream ); BinaryReader reader = new BinaryReader( stream );
Width = reader.ReadUInt16(); int Width = reader.ReadUInt16();
Height = reader.ReadUInt16(); int Height = reader.ReadUInt16();
if( Width != 24 || Height != 24 ) if( Width != 24 || Height != 24 )
throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) ); throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) );
@@ -42,9 +40,10 @@ namespace OpenRA.FileFormats
/*NumTiles = */reader.ReadUInt16(); /*NumTiles = */reader.ReadUInt16();
/*Zero1 = */reader.ReadUInt16(); /*Zero1 = */reader.ReadUInt16();
/*uint Size = */reader.ReadUInt32(); /*uint Size = */reader.ReadUInt32();
ImgStart = reader.ReadUInt32(); uint ImgStart = reader.ReadUInt32();
/*Zero2 = */reader.ReadUInt32(); /*Zero2 = */reader.ReadUInt32();
int IndexEnd, IndexStart;
if (reader.ReadUInt16() == 65535) // ID1 = FFFFh for cnc if (reader.ReadUInt16() == 65535) // ID1 = FFFFh for cnc
{ {
/*ID2 = */reader.ReadUInt16(); /*ID2 = */reader.ReadUInt16();
@@ -54,7 +53,6 @@ namespace OpenRA.FileFormats
else // Load as a ra .tem else // Load as a ra .tem
{ {
stream.Position = 0; stream.Position = 0;
// Try loading as an RA .tem
reader = new BinaryReader( stream ); reader = new BinaryReader( stream );
Width = reader.ReadUInt16(); Width = reader.ReadUInt16();
Height = reader.ReadUInt16(); Height = reader.ReadUInt16();
@@ -73,8 +71,6 @@ namespace OpenRA.FileFormats
reader.ReadUInt32(); reader.ReadUInt32();
IndexStart = reader.ReadInt32(); IndexStart = reader.ReadInt32();
} }
Log.Write("IndexStart: {0}",IndexStart);
stream.Position = IndexStart; stream.Position = IndexStart;
foreach( byte b in new BinaryReader(stream).ReadBytes(IndexEnd - IndexStart) ) foreach( byte b in new BinaryReader(stream).ReadBytes(IndexEnd - IndexStart) )

View File

@@ -76,7 +76,6 @@ namespace OpenRA.FileFormats
using( Stream s = FileSystem.Open( tilename + suffix ) ) using( Stream s = FileSystem.Open( tilename + suffix ) )
{ {
Log.Write(tilename+suffix);
if( !tiles.ContainsKey( (ushort)( start + i ) ) ) if( !tiles.ContainsKey( (ushort)( start + i ) ) )
tiles.Add( (ushort)( start + i ), new Terrain( s ) ); tiles.Add( (ushort)( start + i ), new Terrain( s ) );
} }

View File

@@ -101,11 +101,8 @@ namespace OpenRA
Timer.Time( "ChromeProv, SeqProv, viewport: {0}" ); Timer.Time( "ChromeProv, SeqProv, viewport: {0}" );
skipMakeAnims = true; skipMakeAnims = true;
foreach (var treeReference in Game.world.Map.Trees) foreach (var actorReference in Game.world.Map.Actors)
world.CreateActor(treeReference.Image, new int2(treeReference.Location), null); world.CreateActor(actorReference.Name, actorReference.Location, world.players.Values.FirstOrDefault(p => p.InternalName == actorReference.Owner) ?? world.players[0]);
Timer.Time( "trees: {0}" );
world.LoadMapActors(Rules.AllRules);
skipMakeAnims = false; skipMakeAnims = false;
Timer.Time( "map actors: {0}" ); Timer.Time( "map actors: {0}" );

View File

@@ -200,22 +200,6 @@ namespace OpenRA
return xy; return xy;
} }
public static void LoadMapActors(this World world, IniFile mapfile)
{
var toLoad =
mapfile.GetSection("STRUCTURES", true)
.Concat(mapfile.GetSection("UNITS", true));
foreach (var s in toLoad)
{
//num=owner,type,health,location,facing,...
var parts = s.Value.Split( ',' );
var loc = int.Parse(parts[3]);
world.CreateActor(parts[1].ToLowerInvariant(), new int2(loc % 128, loc / 128),
world.players.Values.FirstOrDefault(p => p.InternalName == parts[0]) ?? world.players[0]);
}
}
public static int2 ChooseRandomEdgeCell(this World w) public static int2 ChooseRandomEdgeCell(this World w)
{ {
var isX = w.SharedRandom.Next(2) == 0; var isX = w.SharedRandom.Next(2) == 0;

View File

@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<sequences> <sequences>
<unit name="rock1"> <!-- Blossom Tree -->
<unit name="split3">
<sequence name="idle" start="0" length="1" />
</unit>
<!-- Rocks -->
<unit name="rock1">
<sequence name="idle" start="0" length="1" /> <sequence name="idle" start="0" length="1" />
</unit> </unit>
<unit name="rock2"> <unit name="rock2">
@@ -21,6 +27,8 @@
<unit name="rock7"> <unit name="rock7">
<sequence name="idle" start="0" length="1" /> <sequence name="idle" start="0" length="1" />
</unit> </unit>
<!-- Trees -->
<unit name="tc04"> <unit name="tc04">
<sequence name="idle" start="0" length="1" /> <sequence name="idle" start="0" length="1" />
<sequence name="burn" start="1" length="9" /> <sequence name="burn" start="1" length="9" />
@@ -105,6 +113,8 @@
<sequence name="idle" start="0" length="1" /> <sequence name="idle" start="0" length="1" />
<sequence name="burn" start="1" length="9" /> <sequence name="burn" start="1" length="9" />
</unit> </unit>
<!-- Civilian Structures -->
<unit name="v01"> <unit name="v01">
<sequence name="idle" start="0" length="1" /> <sequence name="idle" start="0" length="1" />
<sequence name="damaged-idle" start="1" length="1" /> <sequence name="damaged-idle" start="1" length="1" />
@@ -183,10 +193,4 @@
<sequence name="idle" start="0" length="14" /> <sequence name="idle" start="0" length="14" />
<sequence name="damaged-idle" start="14" length="15" /> <sequence name="damaged-idle" start="14" length="15" />
</unit> </unit>
<unit name="barl">
<sequence name="idle" start="0" length="3" />
</unit>
<unit name="brl3">
<sequence name="idle" start="0" length="3" />
</unit>
</sequences> </sequences>