Kill ITerrainTypeModifier for explicit updating of a custom layer in the map. Functionally equivalent, but MUCH faster.
This commit is contained in:
@@ -53,7 +53,7 @@ namespace OpenRA.FileFormats
|
|||||||
|
|
||||||
public TileReference<ushort, byte>[,] MapTiles;
|
public TileReference<ushort, byte>[,] MapTiles;
|
||||||
public TileReference<byte, byte>[,] MapResources;
|
public TileReference<byte, byte>[,] MapResources;
|
||||||
|
public string [,] CustomTerrain;
|
||||||
|
|
||||||
// Temporary compat hacks
|
// Temporary compat hacks
|
||||||
public int XOffset { get { return TopLeft.X; } }
|
public int XOffset { get { return TopLeft.X; } }
|
||||||
@@ -148,6 +148,7 @@ namespace OpenRA.FileFormats
|
|||||||
// Rules
|
// Rules
|
||||||
Rules = yaml["Rules"].Nodes;
|
Rules = yaml["Rules"].Nodes;
|
||||||
|
|
||||||
|
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||||
LoadUid();
|
LoadUid();
|
||||||
LoadBinaryData();
|
LoadBinaryData();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,24 +88,19 @@ namespace OpenRA.Graphics
|
|||||||
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
||||||
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||||
|
|
||||||
var customTerrain = world.WorldActor.traits.WithInterface<ITerrainTypeModifier>();
|
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
int* c = (int*)bitmapData.Scan0;
|
int* c = (int*)bitmapData.Scan0;
|
||||||
|
|
||||||
for (var x = 0; x < map.Width; x++)
|
for (var x = 0; x < map.Width; x++)
|
||||||
for (var y = 0; y < map.Height; y++)
|
for (var y = 0; y < map.Height; y++)
|
||||||
{
|
{
|
||||||
var xy = new int2(x + map.TopLeft.X, y + map.TopLeft.Y);
|
var mapX = x + map.TopLeft.X;
|
||||||
foreach (var t in customTerrain)
|
var mapY = y + map.TopLeft.Y;
|
||||||
{
|
var custom = map.CustomTerrain[mapX,mapY];
|
||||||
var tt = t.GetTerrainType(xy);
|
if (custom == null)
|
||||||
if (tt != null)
|
continue;
|
||||||
{
|
*(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[custom].Color.ToArgb();
|
||||||
*(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[tt].Color.ToArgb();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bitmap.UnlockBits(bitmapData);
|
bitmap.UnlockBits(bitmapData);
|
||||||
|
|||||||
@@ -192,13 +192,8 @@ namespace OpenRA.Traits
|
|||||||
{
|
{
|
||||||
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
||||||
return float.PositiveInfinity;
|
return float.PositiveInfinity;
|
||||||
|
|
||||||
// Custom terrain types don't stack: pick one
|
var type = self.World.GetTerrainType(cell);
|
||||||
var customTerrain = self.World.WorldActor.traits.WithInterface<ITerrainTypeModifier>()
|
|
||||||
.Select( t => t.GetTerrainType(cell) )
|
|
||||||
.FirstOrDefault( t => t != null );
|
|
||||||
|
|
||||||
var type = (customTerrain != null) ? customTerrain : self.World.GetTerrainType(cell);
|
|
||||||
var additionalCost = self.World.WorldActor.traits.WithInterface<ITerrainCost>()
|
var additionalCost = self.World.WorldActor.traits.WithInterface<ITerrainCost>()
|
||||||
.Select( t => t.GetTerrainCost(cell, self) ).Sum();
|
.Select( t => t.GetTerrainCost(cell, self) ).Sum();
|
||||||
|
|
||||||
@@ -210,13 +205,8 @@ namespace OpenRA.Traits
|
|||||||
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
||||||
if( unitInfo == null )
|
if( unitInfo == null )
|
||||||
return 0f;
|
return 0f;
|
||||||
|
|
||||||
// Custom terrain types don't stack: pick one
|
var type = self.World.GetTerrainType(cell);
|
||||||
var customTerrain = self.World.WorldActor.traits.WithInterface<ITerrainTypeModifier>()
|
|
||||||
.Select( t => t.GetTerrainType(cell) )
|
|
||||||
.FirstOrDefault( t => t != null );
|
|
||||||
|
|
||||||
var type = (customTerrain != null) ? customTerrain : self.World.GetTerrainType(cell);
|
|
||||||
|
|
||||||
var modifier = self.traits
|
var modifier = self.traits
|
||||||
.WithInterface<ISpeedModifier>()
|
.WithInterface<ISpeedModifier>()
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ namespace OpenRA.Traits
|
|||||||
public interface IAvoidHazard { string Type { get; } }
|
public interface IAvoidHazard { string Type { get; } }
|
||||||
public interface IStoreOre { int Capacity { get; }}
|
public interface IStoreOre { int Capacity { get; }}
|
||||||
|
|
||||||
public interface ITerrainTypeModifier { string GetTerrainType(int2 cell); }
|
|
||||||
public interface ITerrainCost { float GetTerrainCost(int2 cell, Actor forActor); }
|
public interface ITerrainCost { float GetTerrainCost(int2 cell, Actor forActor); }
|
||||||
|
|
||||||
public interface IDisable { bool Disabled { get; } }
|
public interface IDisable { bool Disabled { get; } }
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace OpenRA.Traits
|
|||||||
{
|
{
|
||||||
public class ResourceLayerInfo : TraitInfo<ResourceLayer> { }
|
public class ResourceLayerInfo : TraitInfo<ResourceLayer> { }
|
||||||
|
|
||||||
public class ResourceLayer: IRenderOverlay, ILoadWorldHook, ITerrainTypeModifier
|
public class ResourceLayer: IRenderOverlay, ILoadWorldHook
|
||||||
{
|
{
|
||||||
World world;
|
World world;
|
||||||
|
|
||||||
@@ -75,15 +75,7 @@ namespace OpenRA.Traits
|
|||||||
if (content[x, y].type != null)
|
if (content[x, y].type != null)
|
||||||
content[x, y].density = GetIdealDensity(x, y);
|
content[x, y].density = GetIdealDensity(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetTerrainType(int2 cell)
|
|
||||||
{
|
|
||||||
if (content[cell.X,cell.Y].type == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return content[cell.X,cell.Y].type.info.TerrainType;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprite[] ChooseContent(ResourceType t)
|
Sprite[] ChooseContent(ResourceType t)
|
||||||
{
|
{
|
||||||
return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)];
|
return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)];
|
||||||
@@ -120,6 +112,8 @@ namespace OpenRA.Traits
|
|||||||
content[i, j].density = Math.Min(
|
content[i, j].density = Math.Min(
|
||||||
content[i, j].image.Length - 1,
|
content[i, j].image.Length - 1,
|
||||||
content[i, j].density + n);
|
content[i, j].density + n);
|
||||||
|
|
||||||
|
world.Map.CustomTerrain[i,j] = t.info.TerrainType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsFull(int i, int j) { return content[i, j].density == content[i, j].image.Length - 1; }
|
public bool IsFull(int i, int j) { return content[i, j].density == content[i, j].image.Length - 1; }
|
||||||
@@ -133,6 +127,7 @@ namespace OpenRA.Traits
|
|||||||
{
|
{
|
||||||
content[p.X, p.Y].type = null;
|
content[p.X, p.Y].type = null;
|
||||||
content[p.X, p.Y].image = null;
|
content[p.X, p.Y].image = null;
|
||||||
|
world.Map.CustomTerrain[p.X, p.Y] = null;
|
||||||
}
|
}
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,8 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static string GetTerrainType(this World world, int2 cell)
|
public static string GetTerrainType(this World world, int2 cell)
|
||||||
{
|
{
|
||||||
return world.TileSet.GetTerrainType(world.Map.MapTiles[cell.X, cell.Y]);
|
var custom = world.Map.CustomTerrain[cell.X, cell.Y];
|
||||||
|
return custom != null ? custom : world.TileSet.GetTerrainType(world.Map.MapTiles[cell.X, cell.Y]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TerrainTypeInfo GetTerrainInfo(this World world, int2 cell)
|
public static TerrainTypeInfo GetTerrainInfo(this World world, int2 cell)
|
||||||
|
|||||||
@@ -171,6 +171,10 @@ namespace OpenRA.Mods.RA
|
|||||||
currentTemplate = (ds == DamageState.Half && Info.DamagedTemplate > 0) ? Info.DamagedTemplate :
|
currentTemplate = (ds == DamageState.Half && Info.DamagedTemplate > 0) ? Info.DamagedTemplate :
|
||||||
(ds == DamageState.Dead && Info.DestroyedTemplate > 0) ? Info.DestroyedTemplate : Info.Template;
|
(ds == DamageState.Dead && Info.DestroyedTemplate > 0) ? Info.DestroyedTemplate : Info.Template;
|
||||||
|
|
||||||
|
// Update map
|
||||||
|
foreach (var c in TileSprites[currentTemplate].Keys)
|
||||||
|
self.World.Map.CustomTerrain[c.X, c.Y] = GetTerrainType(c);
|
||||||
|
|
||||||
if (Info.Long && ds == DamageState.Dead)
|
if (Info.Long && ds == DamageState.Dead)
|
||||||
{
|
{
|
||||||
// Long bridges have custom art for multiple segments being destroyed
|
// Long bridges have custom art for multiple segments being destroyed
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA
|
|||||||
public object Create(ActorInitializer init) { return new BridgeLayer(init.self, this); }
|
public object Create(ActorInitializer init) { return new BridgeLayer(init.self, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
class BridgeLayer : ILoadWorldHook, ITerrainTypeModifier
|
class BridgeLayer : ILoadWorldHook
|
||||||
{
|
{
|
||||||
readonly BridgeLayerInfo Info;
|
readonly BridgeLayerInfo Info;
|
||||||
readonly World world;
|
readonly World world;
|
||||||
@@ -106,13 +106,6 @@ namespace OpenRA.Mods.RA
|
|||||||
bridge.Create(tile, subTiles);
|
bridge.Create(tile, subTiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetTerrainType(int2 cell)
|
|
||||||
{
|
|
||||||
if (Bridges[ cell.X, cell.Y ] != null)
|
|
||||||
return Bridges[ cell.X, cell.Y ].GetTerrainType(cell);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used to check for neighbouring bridges
|
// Used to check for neighbouring bridges
|
||||||
public Bridge GetBridge(int2 cell)
|
public Bridge GetBridge(int2 cell)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ namespace OpenRA.Mods.RA
|
|||||||
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
|
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
|
||||||
|
|
||||||
// Is this valid terrain?
|
// Is this valid terrain?
|
||||||
var terrainType = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[p.X, p.Y]);
|
var terrainType = self.World.GetTerrainType(p);
|
||||||
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) continue;
|
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) continue;
|
||||||
|
|
||||||
// Don't drop on any actors
|
// Don't drop on any actors
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA
|
|||||||
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
|
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
|
||||||
|
|
||||||
// Is this valid terrain?
|
// Is this valid terrain?
|
||||||
var terrainType = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[p.X, p.Y]);
|
var terrainType = self.World.GetTerrainType(p);
|
||||||
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) continue;
|
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) continue;
|
||||||
|
|
||||||
// Don't spawn on any actors
|
// Don't spawn on any actors
|
||||||
|
|||||||
Reference in New Issue
Block a user