Add ITerrainInfoNotifyMapCreated interface.

This commit is contained in:
Paul Chote
2020-10-11 13:31:41 +01:00
committed by reaperrr
parent 87790069e9
commit be2ca77acf
4 changed files with 29 additions and 30 deletions

View File

@@ -1020,33 +1020,6 @@ namespace OpenRA
ProjectedCells = new ProjectedCellRegion(this, tl, br).ToArray(); ProjectedCells = new ProjectedCellRegion(this, tl, br).ToArray();
} }
public void FixOpenAreas()
{
var r = new Random();
var tileset = Rules.TileSet;
for (var j = Bounds.Top; j < Bounds.Bottom; j++)
{
for (var i = Bounds.Left; i < Bounds.Right; i++)
{
var type = Tiles[new MPos(i, j)].Type;
var index = Tiles[new MPos(i, j)].Index;
if (!tileset.Templates.ContainsKey(type))
{
Console.WriteLine("Unknown Tile ID {0}".F(type));
continue;
}
var template = tileset.Templates[type];
if (!template.PickAny)
continue;
index = (byte)r.Next(0, template.TilesCount);
Tiles[new MPos(i, j)] = new TerrainTile(type, index);
}
}
}
public byte GetTerrainIndex(CPos cell) public byte GetTerrainIndex(CPos cell)
{ {
const short InvalidCachedTerrainIndex = -1; const short InvalidCachedTerrainIndex = -1;

View File

@@ -14,6 +14,7 @@ using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileSystem; using OpenRA.FileSystem;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA namespace OpenRA
@@ -34,6 +35,11 @@ namespace OpenRA
float MaxHeightColorBrightness { get; } float MaxHeightColorBrightness { get; }
} }
public interface ITerrainInfoNotifyMapCreated : ITerrainInfo
{
void MapCreated(Map map);
}
public class TerrainTileInfo public class TerrainTileInfo
{ {
[FieldLoader.Ignore] [FieldLoader.Ignore]
@@ -140,7 +146,7 @@ namespace OpenRA
} }
} }
public class TileSet : ITerrainInfo public class TileSet : ITerrainInfo, ITerrainInfoNotifyMapCreated
{ {
public const string TerrainPaletteInternalName = "terrain"; public const string TerrainPaletteInternalName = "terrain";
@@ -270,5 +276,22 @@ namespace OpenRA
float ITerrainInfo.MinHeightColorBrightness { get { return MinHeightColorBrightness; } } float ITerrainInfo.MinHeightColorBrightness { get { return MinHeightColorBrightness; } }
float ITerrainInfo.MaxHeightColorBrightness { get { return MaxHeightColorBrightness; } } float ITerrainInfo.MaxHeightColorBrightness { get { return MaxHeightColorBrightness; } }
TerrainTile ITerrainInfo.DefaultTerrainTile { get { return new TerrainTile(Templates.First().Key, 0); } } TerrainTile ITerrainInfo.DefaultTerrainTile { get { return new TerrainTile(Templates.First().Key, 0); } }
void ITerrainInfoNotifyMapCreated.MapCreated(Map map)
{
// Randomize PickAny tile variants
var r = new MersenneTwister();
for (var j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
{
for (var i = map.Bounds.Left; i < map.Bounds.Right; i++)
{
var type = map.Tiles[new MPos(i, j)].Type;
if (!Templates.TryGetValue(type, out var template) || !template.PickAny)
continue;
map.Tiles[new MPos(i, j)] = new TerrainTile(type, (byte)r.Next(0, template.TilesCount));
}
}
}
} }
} }

View File

@@ -99,7 +99,8 @@ namespace OpenRA.Mods.Common.UtilityCommands
Map.PlayerDefinitions = MapPlayers.ToMiniYaml(); Map.PlayerDefinitions = MapPlayers.ToMiniYaml();
} }
Map.FixOpenAreas(); if (Map.Rules.TerrainInfo is ITerrainInfoNotifyMapCreated notifyMapCreated)
notifyMapCreated.MapCreated(Map);
var dest = Path.GetFileNameWithoutExtension(args[1]) + ".oramap"; var dest = Path.GetFileNameWithoutExtension(args[1]) + ".oramap";

View File

@@ -62,7 +62,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
map.SetBounds(tl, br); map.SetBounds(tl, br);
map.PlayerDefinitions = new MapPlayers(map.Rules, 0).ToMiniYaml(); map.PlayerDefinitions = new MapPlayers(map.Rules, 0).ToMiniYaml();
map.FixOpenAreas();
if (map.Rules.TerrainInfo is ITerrainInfoNotifyMapCreated notifyMapCreated)
notifyMapCreated.MapCreated(map);
Action<string> afterSave = uid => Action<string> afterSave = uid =>
{ {