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
using System.Drawing;
namespace OpenRA.FileFormats
{
public struct TreeReference
public struct ActorReference
{
public readonly int X;
public readonly int Y;
public readonly string Image;
public TreeReference(int xy, string image, int mapSize)
public readonly int2 Location;
public readonly string Name;
public readonly string Owner;
public ActorReference( string name, int2 location, string owner )
{
X = xy % mapSize;
Y = xy / mapSize;
Image = image;
Name = name;
Location = location;
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 readonly TileReference[ , ] MapTiles;
public readonly List<TreeReference> Trees = new List<TreeReference>();
public readonly List<ActorReference> Actors = new List<ActorReference>();
public readonly IEnumerable<int2> SpawnPoints;
@@ -55,8 +55,6 @@ namespace OpenRA.FileFormats
public Map(IniFile file)
{
IniSection basic = file.GetSection("Basic");
Title = basic.GetValue("Name", "(null)");
BinaryPart = basic.GetValue("BinaryPart", "scm02ea.bin");
@@ -88,8 +86,7 @@ namespace OpenRA.FileFormats
{
UnpackTileData(ReadPackedSection(file.GetSection("MapPack")));
UnpackOverlayData(ReadPackedSection(file.GetSection("OverlayPack")));
ReadTrees(file);
ReadRATrees(file);
}
else // CNC
{
@@ -97,6 +94,10 @@ namespace OpenRA.FileFormats
ReadCncTrees(file);
}
LoadActors(file, "STRUCTURES");
LoadActors(file, "UNITS");
LoadActors(file, "INFANTRY");
SpawnPoints = file.GetSection("Waypoints")
.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)
@@ -195,14 +196,17 @@ namespace OpenRA.FileFormats
MapTiles[ j, i ].overlay = ReadByte( ms );
}
void ReadTrees( IniFile file )
void ReadRATrees( IniFile file )
{
IniSection terrain = file.GetSection( "TERRAIN", true );
if( terrain == null )
return;
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 )
@@ -212,8 +216,21 @@ namespace OpenRA.FileFormats
return;
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)

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">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -90,10 +90,10 @@
<Compile Include="Terrain.cs" />
<Compile Include="TileReference.cs" />
<Compile Include="TileSet.cs" />
<Compile Include="TreeReference.cs" />
<Compile Include="Tuple.cs" />
<Compile Include="TypeDictionary.cs" />
<Compile Include="Walkability.cs" />
<Compile Include="ActorReference.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- 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 )
{
int Width, Height, IndexEnd, IndexStart;
uint ImgStart;
// Try loading as a cnc .tem
BinaryReader reader = new BinaryReader( stream );
Width = reader.ReadUInt16();
Height = reader.ReadUInt16();
int Width = reader.ReadUInt16();
int Height = reader.ReadUInt16();
if( Width != 24 || Height != 24 )
throw new InvalidDataException( string.Format( "{0}x{1}", Width, Height ) );
@@ -42,9 +40,10 @@ namespace OpenRA.FileFormats
/*NumTiles = */reader.ReadUInt16();
/*Zero1 = */reader.ReadUInt16();
/*uint Size = */reader.ReadUInt32();
ImgStart = reader.ReadUInt32();
uint ImgStart = reader.ReadUInt32();
/*Zero2 = */reader.ReadUInt32();
int IndexEnd, IndexStart;
if (reader.ReadUInt16() == 65535) // ID1 = FFFFh for cnc
{
/*ID2 = */reader.ReadUInt16();
@@ -54,7 +53,6 @@ namespace OpenRA.FileFormats
else // Load as a ra .tem
{
stream.Position = 0;
// Try loading as an RA .tem
reader = new BinaryReader( stream );
Width = reader.ReadUInt16();
Height = reader.ReadUInt16();
@@ -73,8 +71,6 @@ namespace OpenRA.FileFormats
reader.ReadUInt32();
IndexStart = reader.ReadInt32();
}
Log.Write("IndexStart: {0}",IndexStart);
stream.Position = 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 ) )
{
Log.Write(tilename+suffix);
if( !tiles.ContainsKey( (ushort)( start + i ) ) )
tiles.Add( (ushort)( start + i ), new Terrain( s ) );
}

View File

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

View File

@@ -200,22 +200,6 @@ namespace OpenRA
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)
{
var isX = w.SharedRandom.Next(2) == 0;

View File

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