Actors as dict

This commit is contained in:
Paul Chote
2010-05-24 21:12:15 +12:00
parent 2dfa4d9db7
commit 7ef2ed1434
9 changed files with 1981 additions and 437 deletions

View File

@@ -112,7 +112,6 @@ namespace MapConverter
foreach (var pkg in manifest.Packages) FileSystem.Mount(pkg); foreach (var pkg in manifest.Packages) FileSystem.Mount(pkg);
ConvertIniMap(args[1]); ConvertIniMap(args[1]);
Map.DebugContents();
Save(args[2]); Save(args[2]);
} }
@@ -275,7 +274,7 @@ namespace MapConverter
Map.MapResources[i,j] = new TileReference<byte,byte>(res.First, res.Second); Map.MapResources[i,j] = new TileReference<byte,byte>(res.First, res.Second);
if (o != 255 && overlayActorMapping.ContainsKey(raOverlayNames[o])) if (o != 255 && overlayActorMapping.ContainsKey(raOverlayNames[o]))
Map.Actors.Add("Actor"+ActorCount++, new ActorReference( overlayActorMapping[raOverlayNames[o]], new int2(i,j), "Neutral")); Map.Actors.Add("Actor"+ActorCount, new ActorReference("Actor"+ActorCount++, overlayActorMapping[raOverlayNames[o]], new int2(i,j), "Neutral"));
} }
} }
@@ -288,7 +287,7 @@ namespace MapConverter
foreach( KeyValuePair<string, string> kv in terrain ) foreach( KeyValuePair<string, string> kv in terrain )
{ {
var loc = int.Parse( kv.Key ); var loc = int.Parse( kv.Key );
Map.Actors.Add("Actor"+ActorCount++, new ActorReference(kv.Value.ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), "Neutral" ) ); Map.Actors.Add("Actor"+ActorCount, new ActorReference("Actor"+ActorCount++,kv.Value.ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), "Neutral" ) );
} }
} }
@@ -329,7 +328,7 @@ namespace MapConverter
Map.MapResources[ cell.X, cell.Y ] = new TileReference<byte,byte>(res.First, res.Second); Map.MapResources[ cell.X, cell.Y ] = new TileReference<byte,byte>(res.First, res.Second);
if (overlayActorMapping.ContainsKey(kv.Value.ToLower())) if (overlayActorMapping.ContainsKey(kv.Value.ToLower()))
Map.Actors.Add("Actor"+ActorCount++, new ActorReference( overlayActorMapping[kv.Value.ToLower()], new int2(cell.X,cell.Y), "Neutral")); Map.Actors.Add("Actor"+ActorCount, new ActorReference("Actor"+ActorCount++, overlayActorMapping[kv.Value.ToLower()], new int2(cell.X,cell.Y), "Neutral"));
} }
} }
@@ -343,7 +342,7 @@ namespace MapConverter
foreach( KeyValuePair<string, string> kv in terrain ) foreach( KeyValuePair<string, string> kv in terrain )
{ {
var loc = int.Parse( kv.Key ); var loc = int.Parse( kv.Key );
Map.Actors.Add("Actor"+ActorCount++, new ActorReference( kv.Value.Split(',')[0].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize),"Neutral")); Map.Actors.Add("Actor"+ActorCount, new ActorReference("Actor"+ActorCount++, kv.Value.Split(',')[0].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize),"Neutral"));
} }
} }
@@ -356,7 +355,7 @@ namespace MapConverter
var loc = int.Parse(parts[3]); var loc = int.Parse(parts[3]);
if (parts[0] == "") if (parts[0] == "")
parts[0] = "Neutral"; parts[0] = "Neutral";
Map.Actors.Add("Actor"+ActorCount++, new ActorReference( parts[1].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), parts[0])); Map.Actors.Add("Actor"+ActorCount, new ActorReference("Actor"+ActorCount++, parts[1].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), parts[0]));
} }
} }

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Linq; using System.Linq;
@@ -186,7 +186,8 @@ namespace OpenRA.Editor
return; return;
var owner = "Neutral"; var owner = "Neutral";
Map.Actors[NextActorName()] = new ActorReference(Actor.Info.Name.ToLowerInvariant(), GetBrushLocation(), owner); var id = NextActorName();
Map.Actors[id] = new ActorReference(id,Actor.Info.Name.ToLowerInvariant(), GetBrushLocation(), owner);
} }
Random r = new Random(); Random r = new Random();
@@ -316,7 +317,7 @@ namespace OpenRA.Editor
new Rectangle(Map.XOffset * 24 + Offset.X, Map.YOffset * 24 + Offset.Y, Map.Width * 24, Map.Height * 24)); new Rectangle(Map.XOffset * 24 + Offset.X, Map.YOffset * 24 + Offset.Y, Map.Width * 24, Map.Height * 24));
foreach (var ar in Map.Actors) foreach (var ar in Map.Actors)
DrawActor(e.Graphics, ar.Value.Location, ActorTemplates[ar.Value.Name]); DrawActor(e.Graphics, ar.Value.Location, ActorTemplates[ar.Value.Type]);
foreach (var wp in Map.Waypoints) foreach (var wp in Map.Waypoints)
e.Graphics.DrawRectangle(Pens.LimeGreen, new Rectangle( e.Graphics.DrawRectangle(Pens.LimeGreen, new Rectangle(
@@ -345,7 +346,7 @@ namespace OpenRA.Editor
{ {
var x = Map.Actors.FirstOrDefault(a => a.Value.Location == GetBrushLocation()); var x = Map.Actors.FirstOrDefault(a => a.Value.Location == GetBrushLocation());
if (x.Key != null) if (x.Key != null)
DrawActorBorder(e.Graphics, x.Value.Location, ActorTemplates[x.Value.Name]); DrawActorBorder(e.Graphics, x.Value.Location, ActorTemplates[x.Value.Type]);
} }
} }
} }

View File

@@ -20,22 +20,25 @@
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public struct ActorReference public class ActorReference
{ {
public readonly string Id;
public readonly string Type;
public readonly int2 Location; public readonly int2 Location;
public readonly string Name;
public readonly string Owner; public readonly string Owner;
public ActorReference( string name, int2 location, string owner )
public ActorReference(MiniYaml my)
{ {
Name = name; FieldLoader.Load(this, my);
}
// Legacy construtor for old format maps
public ActorReference(string id, string type, int2 location, string owner )
{
Id = id;
Type = type;
Location = location; Location = location;
Owner = owner; Owner = owner;
} }
public override string ToString ()
{
return string.Format("{0} {1} {2},{3}", Name, Owner, Location.X,Location.Y);
}
} }
} }

View File

@@ -133,11 +133,23 @@ namespace OpenRA.FileFormats
// Actors // Actors
if (MapFormat == 1 ) if (MapFormat == 1 )
{ {
int actors = 0;
foreach (var kv in yaml["Actors"].Nodes) foreach (var kv in yaml["Actors"].Nodes)
{ {
string[] vals = kv.Value.Value.Split(' '); string[] vals = kv.Value.Value.Split(' ');
string[] loc = vals[2].Split(','); string[] loc = vals[2].Split(',');
var a = new ActorReference(vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral"); var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral");
Actors.Add(a.Id, a);
}
}
else if (MapFormat == 2)
{
int actors = 0;
foreach (var kv in yaml["Actors"].Nodes)
{
string[] vals = kv.Value.Value.Split(' ');
string[] loc = vals[2].Split(',');
var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), vals[1]);
Actors.Add(kv.Key, a); Actors.Add(kv.Key, a);
} }
} }
@@ -145,10 +157,8 @@ namespace OpenRA.FileFormats
{ {
foreach (var kv in yaml["Actors"].Nodes) foreach (var kv in yaml["Actors"].Nodes)
{ {
string[] vals = kv.Value.Value.Split(' '); var player = new ActorReference(kv.Value);
string[] loc = vals[2].Split(','); Actors.Add(player.Id, player);
var a = new ActorReference(vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), vals[1]);
Actors.Add(kv.Key, a);
} }
} }
@@ -169,7 +179,7 @@ namespace OpenRA.FileFormats
public void Save(string filepath) public void Save(string filepath)
{ {
MapFormat = 2; MapFormat = 3;
Dictionary<string, MiniYaml> root = new Dictionary<string, MiniYaml>(); Dictionary<string, MiniYaml> root = new Dictionary<string, MiniYaml>();
foreach (var field in SimpleFields) foreach (var field in SimpleFields)
@@ -180,12 +190,15 @@ namespace OpenRA.FileFormats
} }
Dictionary<string, MiniYaml> playerYaml = new Dictionary<string, MiniYaml>(); Dictionary<string, MiniYaml> playerYaml = new Dictionary<string, MiniYaml>();
foreach(var p in Players) foreach(var p in Players)
playerYaml.Add("PlayerReference@{0}".F(p.Key), FieldSaver.Save(p.Value)); playerYaml.Add("PlayerReference@{0}".F(p.Key), FieldSaver.Save(p.Value));
root.Add("Players",new MiniYaml(null, playerYaml)); root.Add("Players",new MiniYaml(null, playerYaml));
root.Add("Actors", MiniYaml.FromDictionary<string, ActorReference>(Actors)); Dictionary<string, MiniYaml> actorYaml = new Dictionary<string, MiniYaml>();
foreach(var p in Actors)
actorYaml.Add("ActorReference@{0}".F(p.Key), FieldSaver.Save(p.Value));
root.Add("Actors",new MiniYaml(null, actorYaml));
root.Add("Waypoints", MiniYaml.FromDictionary<string, int2>(Waypoints)); root.Add("Waypoints", MiniYaml.FromDictionary<string, int2>(Waypoints));
root.Add("Smudges", MiniYaml.FromList<SmudgeReference>(Smudges)); root.Add("Smudges", MiniYaml.FromList<SmudgeReference>(Smudges));
root.Add("Rules", new MiniYaml(null, Rules)); root.Add("Rules", new MiniYaml(null, Rules));
@@ -303,24 +316,6 @@ namespace OpenRA.FileFormats
return (x >= TopLeft.X && y >= TopLeft.Y && x < BottomRight.X && y < BottomRight.Y); return (x >= TopLeft.X && y >= TopLeft.Y && x < BottomRight.X && y < BottomRight.Y);
} }
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);
Console.WriteLine("Loaded Smudges:");
foreach (var s in Smudges)
Console.WriteLine("\t{0} {1} {2}", s.Type, s.Location, s.Depth);
}
static T[,] ResizeArray<T>(T[,] ts, T t, int width, int height) static T[,] ResizeArray<T>(T[,] ts, T t, int width, int height)
{ {
var result = new T[width, height]; var result = new T[width, height];

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA
Game.skipMakeAnims = true; // rude hack Game.skipMakeAnims = true; // rude hack
foreach (var actorReference in world.Map.Actors) foreach (var actorReference in world.Map.Actors)
MapActors[actorReference.Key] = world.CreateActor(actorReference.Value.Name, actorReference.Value.Location, MapActors[actorReference.Key] = world.CreateActor(actorReference.Value.Type, actorReference.Value.Location,
world.players.Values.FirstOrDefault(p => p.InternalName == actorReference.Value.Owner)); world.players.Values.FirstOrDefault(p => p.InternalName == actorReference.Value.Owner));
Game.skipMakeAnims = false; Game.skipMakeAnims = false;

View File

@@ -1 +1 @@
b22b0197b126d6236a7c1e18c3001c83af10156c a28a1988fcaca4de0c90a8cb9cd9a2b3aacb4582

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -53,4 +53,4 @@ Terrain:
Music: Music:
mods/ra/music.yaml mods/ra/music.yaml
ShellmapUid:b22b0197b126d6236a7c1e18c3001c83af10156c ShellmapUid:a28a1988fcaca4de0c90a8cb9cd9a2b3aacb4582