Use CellLayer for resources.

This commit is contained in:
Paul Chote
2014-05-17 02:00:40 +12:00
parent ce331a28e8
commit ad730a44c3
8 changed files with 61 additions and 35 deletions

View File

@@ -641,7 +641,8 @@ namespace OpenRA.Editor
for (var i = 0; i < surface1.Map.MapSize.X; i++) for (var i = 0; i < surface1.Map.MapSize.X; i++)
for (var j = 0; j < surface1.Map.MapSize.Y; j++) for (var j = 0; j < surface1.Map.MapSize.Y; j++)
{ {
if (surface1.Map.MapResources.Value[i, j].Type != 0) var cell = new CPos(i, j);
if (surface1.Map.MapResources.Value[cell].Type != 0)
totalResource += GetResourceValue(i, j); totalResource += GetResourceValue(i, j);
} }
@@ -654,9 +655,12 @@ namespace OpenRA.Editor
for (var u = -1; u < 2; u++) for (var u = -1; u < 2; u++)
for (var v = -1; v < 2; v++) for (var v = -1; v < 2; v++)
{ {
if (!surface1.Map.IsInMap(new CPos(x + u, y + v))) var cell = new CPos(x + u, y + v);
if (!surface1.Map.IsInMap(cell))
continue; continue;
if (surface1.Map.MapResources.Value[x + u, y + v].Type == resourceType)
if (surface1.Map.MapResources.Value[cell].Type == resourceType)
++sum; ++sum;
} }
@@ -666,7 +670,7 @@ namespace OpenRA.Editor
int GetResourceValue(int x, int y) int GetResourceValue(int x, int y)
{ {
var imageLength = 0; var imageLength = 0;
int type = surface1.Map.MapResources.Value[x, y].Type; var type = surface1.Map.MapResources.Value[new CPos(x, y)].Type;
var template = surface1.ResourceTemplates.FirstOrDefault(a => a.Value.Info.ResourceType == type).Value; var template = surface1.ResourceTemplates.FirstOrDefault(a => a.Value.Info.ResourceType == type).Value;
if (type == 1) if (type == 1)
imageLength = 12; imageLength = 12;

View File

@@ -21,12 +21,9 @@ namespace OpenRA.Editor
public void Apply(Surface surface) public void Apply(Surface surface)
{ {
surface.Map.MapResources.Value[surface.GetBrushLocation().X, surface.GetBrushLocation().Y] var type = (byte)resourceTemplate.Info.ResourceType;
= new TileReference<byte, byte> var index = (byte)random.Next(resourceTemplate.Info.MaxDensity);
{ surface.Map.MapResources.Value[surface.GetBrushLocation()] = new ResourceTile(type, index);
Type = (byte)resourceTemplate.Info.ResourceType,
Index = (byte)random.Next(resourceTemplate.Info.MaxDensity)
};
var ch = new int2(surface.GetBrushLocation().X / Surface.ChunkSize, var ch = new int2(surface.GetBrushLocation().X / Surface.ChunkSize,
surface.GetBrushLocation().Y / Surface.ChunkSize); surface.GetBrushLocation().Y / Surface.ChunkSize);

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Editor
public bool IsPaste { get { return TileSelection != null && ResourceSelection != null; } } public bool IsPaste { get { return TileSelection != null && ResourceSelection != null; } }
public TileReference<ushort, byte>[,] TileSelection; public TileReference<ushort, byte>[,] TileSelection;
public TileReference<byte, byte>[,] ResourceSelection; public ResourceTile[,] ResourceSelection;
public CPos SelectionStart; public CPos SelectionStart;
public CPos SelectionEnd; public CPos SelectionEnd;
@@ -206,9 +206,9 @@ namespace OpenRA.Editor
var key = Map.Actors.Value.FirstOrDefault(a => a.Value.Location() == brushLocation); var key = Map.Actors.Value.FirstOrDefault(a => a.Value.Location() == brushLocation);
if (key.Key != null) Map.Actors.Value.Remove(key.Key); if (key.Key != null) Map.Actors.Value.Remove(key.Key);
if (Map.MapResources.Value[brushLocation.X, brushLocation.Y].Type != 0) if (Map.MapResources.Value[brushLocation].Type != 0)
{ {
Map.MapResources.Value[brushLocation.X, brushLocation.Y] = new TileReference<byte, byte>(); Map.MapResources.Value[brushLocation] = new ResourceTile(0, 0);
var ch = new int2(brushLocation.X / ChunkSize, brushLocation.Y / ChunkSize); var ch = new int2(brushLocation.X / ChunkSize, brushLocation.Y / ChunkSize);
if (Chunks.ContainsKey(ch)) if (Chunks.ContainsKey(ch))
{ {
@@ -271,6 +271,7 @@ namespace OpenRA.Editor
for (var i = 0; i < ChunkSize; i++) for (var i = 0; i < ChunkSize; i++)
for (var j = 0; j < ChunkSize; j++) for (var j = 0; j < ChunkSize; j++)
{ {
var cell = new CPos(u * ChunkSize + i, v * ChunkSize + j);
var tr = Map.MapTiles.Value[u * ChunkSize + i, v * ChunkSize + j]; var tr = Map.MapTiles.Value[u * ChunkSize + i, v * ChunkSize + j];
var tile = TileSetRenderer.Data(tr.Type); var tile = TileSetRenderer.Data(tr.Type);
var index = (tr.Index < tile.Count) ? tr.Index : (byte)0; var index = (tr.Index < tile.Count) ? tr.Index : (byte)0;
@@ -279,9 +280,9 @@ namespace OpenRA.Editor
for (var y = 0; y < TileSetRenderer.TileSize; y++) for (var y = 0; y < TileSetRenderer.TileSize; y++)
p[(j * TileSetRenderer.TileSize + y) * stride + i * TileSetRenderer.TileSize + x] = Palette.GetColor(rawImage[x + TileSetRenderer.TileSize * y]).ToArgb(); p[(j * TileSetRenderer.TileSize + y) * stride + i * TileSetRenderer.TileSize + x] = Palette.GetColor(rawImage[x + TileSetRenderer.TileSize * y]).ToArgb();
if (Map.MapResources.Value[u * ChunkSize + i, v * ChunkSize + j].Type != 0) if (Map.MapResources.Value[cell].Type != 0)
{ {
var resourceImage = ResourceTemplates[Map.MapResources.Value[u * ChunkSize + i, v * ChunkSize + j].Type].Bitmap; var resourceImage = ResourceTemplates[Map.MapResources.Value[cell].Type].Bitmap;
var srcdata = resourceImage.LockBits(resourceImage.Bounds(), var srcdata = resourceImage.LockBits(resourceImage.Bounds(),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
@@ -509,15 +510,16 @@ namespace OpenRA.Editor
var height = Math.Abs((start - end).Y); var height = Math.Abs((start - end).Y);
TileSelection = new TileReference<ushort, byte>[width, height]; TileSelection = new TileReference<ushort, byte>[width, height];
ResourceSelection = new TileReference<byte, byte>[width, height]; ResourceSelection = new ResourceTile[width, height];
for (var x = 0; x < width; x++) for (var x = 0; x < width; x++)
{ {
for (var y = 0; y < height; y++) for (var y = 0; y < height; y++)
{ {
// TODO: crash prevention // TODO: crash prevention
var cell = new CPos(start.X + x, start.Y + y);
TileSelection[x, y] = Map.MapTiles.Value[start.X + x, start.Y + y]; TileSelection[x, y] = Map.MapTiles.Value[start.X + x, start.Y + y];
ResourceSelection[x, y] = Map.MapResources.Value[start.X + x, start.Y + y]; ResourceSelection[x, y] = Map.MapResources.Value[cell];
} }
} }
} }
@@ -534,10 +536,11 @@ namespace OpenRA.Editor
{ {
var mapX = loc.X + x; var mapX = loc.X + x;
var mapY = loc.Y + y; var mapY = loc.Y + y;
var cell = new CPos(mapX, mapY);
// TODO: crash prevention for outside of bounds // TODO: crash prevention for outside of bounds
Map.MapTiles.Value[mapX, mapY] = TileSelection[x, y]; Map.MapTiles.Value[mapX, mapY] = TileSelection[x, y];
Map.MapResources.Value[mapX, mapY] = ResourceSelection[x, y]; Map.MapResources.Value[cell] = ResourceSelection[x, y];
var ch = new int2(mapX / ChunkSize, mapY / ChunkSize); var ch = new int2(mapX / ChunkSize, mapY / ChunkSize);
if (Chunks.ContainsKey(ch)) if (Chunks.ContainsKey(ch))

View File

@@ -74,8 +74,9 @@ namespace OpenRA.Graphics
continue; continue;
var res = resourceRules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>() var res = resourceRules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>()
.Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].Type) .Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].Type)
.Select(t => t.TerrainType).FirstOrDefault(); .Select(t => t.TerrainType).FirstOrDefault();
if (res == null) if (res == null)
continue; continue;

View File

@@ -112,7 +112,7 @@ namespace OpenRA
public int2 MapSize; public int2 MapSize;
[FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles; [FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles;
[FieldLoader.Ignore] public Lazy<TileReference<byte, byte>[,]> MapResources; [FieldLoader.Ignore] public Lazy<CellLayer<ResourceTile>> MapResources;
[FieldLoader.Ignore] public CellLayer<int> CustomTerrain; [FieldLoader.Ignore] public CellLayer<int> CustomTerrain;
[FieldLoader.Ignore] Lazy<Ruleset> rules; [FieldLoader.Ignore] Lazy<Ruleset> rules;
@@ -126,15 +126,16 @@ namespace OpenRA
var tile = tileset.Templates.First(); var tile = tileset.Templates.First();
var tileRef = new TileReference<ushort, byte> { Type = tile.Key, Index = (byte)0 }; var tileRef = new TileReference<ushort, byte> { Type = tile.Key, Index = (byte)0 };
var size = new Size(1, 1);
var map = new Map() var map = new Map()
{ {
Title = "Name your map here", Title = "Name your map here",
Description = "Describe your map here", Description = "Describe your map here",
Author = "Your name here", Author = "Your name here",
MapSize = new int2(1, 1), MapSize = new int2(size),
Tileset = tileset.Id, Tileset = tileset.Id,
Options = new MapOptions(), Options = new MapOptions(),
MapResources = Exts.Lazy(() => new TileReference<byte, byte>[1, 1]), MapResources = Exts.Lazy(() => new CellLayer<ResourceTile>(size)),
MapTiles = Exts.Lazy(() => new TileReference<ushort, byte>[1, 1] { { tileRef } }), MapTiles = Exts.Lazy(() => new TileReference<ushort, byte>[1, 1] { { tileRef } }),
Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>()), Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>()),
Smudges = Exts.Lazy(() => new List<SmudgeReference>()) Smudges = Exts.Lazy(() => new List<SmudgeReference>())
@@ -385,9 +386,9 @@ namespace OpenRA
return tiles; return tiles;
} }
public TileReference<byte, byte>[,] LoadResourceTiles() public CellLayer<ResourceTile> LoadResourceTiles()
{ {
var resources = new TileReference<byte, byte>[MapSize.X, MapSize.Y]; var resources = new CellLayer<ResourceTile>(this);
using (var dataStream = Container.GetContent("map.bin")) using (var dataStream = Container.GetContent("map.bin"))
{ {
@@ -406,10 +407,11 @@ namespace OpenRA
var data = dataStream.ReadBytes(MapSize.X * MapSize.Y * 2); var data = dataStream.ReadBytes(MapSize.X * MapSize.Y * 2);
var d = 0; var d = 0;
// Load resource data // Load resource data
for (var i = 0; i < MapSize.X; i++) for (var i = 0; i < MapSize.X; i++)
for (var j = 0; j < MapSize.Y; j++) for (var j = 0; j < MapSize.Y; j++)
resources[i, j] = new TileReference<byte, byte>(data[d++], data[d++]); resources[i, j] = new ResourceTile(data[d++], data[d++]);
} }
return resources; return resources;
@@ -435,11 +437,14 @@ namespace OpenRA
// Resource data // Resource data
for (var i = 0; i < MapSize.X; i++) for (var i = 0; i < MapSize.X; i++)
{
for (var j = 0; j < MapSize.Y; j++) for (var j = 0; j < MapSize.Y; j++)
{ {
writer.Write(MapResources.Value[i, j].Type); var tile = MapResources.Value[new CPos(i, j)];
writer.Write(MapResources.Value[i, j].Index); writer.Write(tile.Type);
writer.Write(tile.Index);
} }
}
} }
return dataStream.ToArray(); return dataStream.ToArray();
@@ -452,10 +457,11 @@ namespace OpenRA
{ {
var oldMapTiles = MapTiles.Value; var oldMapTiles = MapTiles.Value;
var oldMapResources = MapResources.Value; var oldMapResources = MapResources.Value;
var newSize = new Size(width, height);
MapTiles = Exts.Lazy(() => Exts.ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height)); MapTiles = Exts.Lazy(() => Exts.ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height));
MapResources = Exts.Lazy(() => Exts.ResizeArray(oldMapResources, oldMapResources[0, 0], width, height)); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[0, 0]));
MapSize = new int2(width, height); MapSize = new int2(newSize);
} }
public void ResizeCordon(int left, int top, int right, int bottom) public void ResizeCordon(int left, int top, int right, int bottom)

View File

@@ -23,4 +23,18 @@ namespace OpenRA
public override int GetHashCode() { return Type.GetHashCode() ^ Index.GetHashCode(); } public override int GetHashCode() { return Type.GetHashCode() ^ Index.GetHashCode(); }
} }
public struct ResourceTile
{
public readonly byte Type;
public readonly byte Index;
public ResourceTile(byte type, byte index)
{
Type = type;
Index = index;
}
public override int GetHashCode() { return Type.GetHashCode() ^ Index.GetHashCode(); }
}
} }

View File

@@ -64,7 +64,7 @@ namespace OpenRA.Traits
foreach (var cell in w.Map.Cells) foreach (var cell in w.Map.Cells)
{ {
ResourceType t; ResourceType t;
if (!resources.TryGetValue(w.Map.MapResources.Value[cell.X, cell.Y].Type, out t)) if (!resources.TryGetValue(w.Map.MapResources.Value[cell].Type, out t))
continue; continue;
if (!AllowResourceAt(t, cell)) if (!AllowResourceAt(t, cell))

View File

@@ -148,7 +148,7 @@ namespace OpenRA.Utility
map.Smudges = Exts.Lazy(() => new List<SmudgeReference>()); map.Smudges = Exts.Lazy(() => new List<SmudgeReference>());
map.Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>()); map.Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>());
map.MapResources = Exts.Lazy(() => new TileReference<byte, byte>[mapSize, mapSize]); map.MapResources = Exts.Lazy(() => new CellLayer<ResourceTile>(new Size(mapSize, mapSize)));
map.MapTiles = Exts.Lazy(() => new TileReference<ushort, byte>[mapSize, mapSize]); map.MapTiles = Exts.Lazy(() => new TileReference<ushort, byte>[mapSize, mapSize]);
map.Options = new MapOptions(); map.Options = new MapOptions();
@@ -277,15 +277,16 @@ namespace OpenRA.Utility
if (o != 255 && overlayResourceMapping.ContainsKey(redAlertOverlayNames[o])) if (o != 255 && overlayResourceMapping.ContainsKey(redAlertOverlayNames[o]))
res = overlayResourceMapping[redAlertOverlayNames[o]]; res = overlayResourceMapping[redAlertOverlayNames[o]];
map.MapResources.Value[i, j] = new TileReference<byte, byte>(res.First, res.Second); var cell = new CPos(i, j);
map.MapResources.Value[cell] = new ResourceTile(res.First, res.Second);
if (o != 255 && overlayActorMapping.ContainsKey(redAlertOverlayNames[o])) if (o != 255 && overlayActorMapping.ContainsKey(redAlertOverlayNames[o]))
{ {
map.Actors.Value.Add("Actor" + actorCount++, map.Actors.Value.Add("Actor" + actorCount++,
new ActorReference(overlayActorMapping[redAlertOverlayNames[o]]) new ActorReference(overlayActorMapping[redAlertOverlayNames[o]])
{ {
new LocationInit(new CPos(i, j)), new LocationInit(cell),
new OwnerInit("Neutral") new OwnerInit("Neutral")
}); });
} }
@@ -342,7 +343,7 @@ namespace OpenRA.Utility
if (overlayResourceMapping.ContainsKey(kv.Value.ToLower())) if (overlayResourceMapping.ContainsKey(kv.Value.ToLower()))
res = overlayResourceMapping[kv.Value.ToLower()]; res = overlayResourceMapping[kv.Value.ToLower()];
map.MapResources.Value[cell.X, cell.Y] = new TileReference<byte, byte>(res.First, res.Second); map.MapResources.Value[cell] = new ResourceTile(res.First, res.Second);
if (overlayActorMapping.ContainsKey(kv.Value.ToLower())) if (overlayActorMapping.ContainsKey(kv.Value.ToLower()))
map.Actors.Value.Add("Actor" + actorCount++, map.Actors.Value.Add("Actor" + actorCount++,