Remove editor-specific resource rendering.

Mods must manually move their *ResourceRenderer definitions from
World onto BaseWorld to restore resource rendering in the editor.
This commit is contained in:
Paul Chote
2020-07-26 18:14:49 +01:00
committed by teinarss
parent e7af295b5e
commit 46cf56d6ff
9 changed files with 103 additions and 269 deletions

View File

@@ -11,7 +11,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -19,25 +18,27 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Required for the map editor to work. Attach this to the world actor.")]
public class EditorResourceLayerInfo : TraitInfo, Requires<ResourceTypeInfo>
public class EditorResourceLayerInfo : TraitInfo, IResourceLayerInfo, Requires<ResourceTypeInfo>
{
public override object Create(ActorInitializer init) { return new EditorResourceLayer(init.Self); }
}
public class EditorResourceLayer : IWorldLoaded, IRenderOverlay, INotifyActorDisposing
public class EditorResourceLayer : IResourceLayer, IWorldLoaded, INotifyActorDisposing
{
protected readonly Map Map;
protected readonly TileSet Tileset;
protected readonly Dictionary<int, ResourceType> Resources;
protected readonly CellLayer<EditorCellContents> Tiles;
protected readonly HashSet<CPos> Dirty = new HashSet<CPos>();
readonly Dictionary<PaletteReference, TerrainSpriteLayer> spriteLayers = new Dictionary<PaletteReference, TerrainSpriteLayer>();
protected readonly CellLayer<ResourceLayerContents> Tiles;
public int NetWorth { get; protected set; }
bool disposed;
public event Action<CPos, ResourceType> CellChanged;
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles[cell]; }
bool IResourceLayer.IsVisible(CPos cell) { return Map.Contains(cell); }
public EditorResourceLayer(Actor self)
{
if (self.World.Type != WorldType.Editor)
@@ -46,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits
Map = self.World.Map;
Tileset = self.World.Map.Rules.TileSet;
Tiles = new CellLayer<EditorCellContents>(Map);
Tiles = new CellLayer<ResourceLayerContents>(Map);
Resources = self.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r);
@@ -60,75 +61,80 @@ namespace OpenRA.Mods.Common.Traits
foreach (var cell in Map.AllCells)
UpdateCell(cell);
// Build the sprite layer dictionary for rendering resources
// All resources that have the same palette must also share a sheet and blend mode
foreach (var r in Resources)
{
var res = r;
var layer = spriteLayers.GetOrAdd(r.Value.Palette, pal =>
{
var first = res.Value.Variants.First().Value.GetSprite(0);
return new TerrainSpriteLayer(w, wr, first.Sheet, first.BlendMode, pal, false);
});
// Validate that sprites are compatible with this layer
var sheet = layer.Sheet;
var sprites = res.Value.Variants.Values.SelectMany(v => Exts.MakeArray(v.Length, x => v.GetSprite(x)));
if (sprites.Any(s => s.Sheet != sheet))
throw new InvalidDataException("Resource sprites span multiple sheets. Try loading their sequences earlier.");
var blendMode = layer.BlendMode;
if (sprites.Any(s => s.BlendMode != blendMode))
throw new InvalidDataException("Resource sprites specify different blend modes. "
+ "Try using different palettes for resource types that use different blend modes.");
}
}
public void UpdateCell(CPos cell)
{
var uv = cell.ToMPos(Map);
if (!Map.Resources.Contains(uv))
return;
var tile = Map.Resources[uv];
var t = Tiles[cell];
if (t.Density > 0)
NetWorth -= (t.Density + 1) * t.Type.Info.ValuePerUnit;
var t = Tiles[uv];
ResourceType type;
var newTile = ResourceLayerContents.Empty;
var newTerrain = byte.MaxValue;
if (Resources.TryGetValue(tile.Type, out type))
{
Tiles[uv] = new EditorCellContents
newTile = new ResourceLayerContents
{
Type = type,
Variant = ChooseRandomVariant(type),
Density = CalculateCellDensity(type, cell)
};
Map.CustomTerrain[uv] = Tileset.GetTerrainIndex(type.Info.TerrainType);
}
else
{
Tiles[uv] = EditorCellContents.Empty;
Map.CustomTerrain[uv] = byte.MaxValue;
newTerrain = Tileset.GetTerrainIndex(type.Info.TerrainType);
}
// Ingame resource rendering is a giant hack (#6395),
// so we must also touch all the neighbouring tiles
Dirty.Add(cell);
// Nothing has changed
if (newTile.Type == t.Type && newTile.Density == t.Density)
return;
UpdateNetWorth(t.Type, t.Density, newTile.Type, newTile.Density);
Tiles[uv] = newTile;
Map.CustomTerrain[uv] = newTerrain;
if (CellChanged != null)
CellChanged(cell, type);
// Neighbouring cell density depends on this cell
foreach (var d in CVec.Directions)
Dirty.Add(cell + d);
{
var neighbouringCell = cell + d;
if (!Tiles.Contains(neighbouringCell))
continue;
var neighbouringTile = Tiles[neighbouringCell];
var density = CalculateCellDensity(neighbouringTile.Type, neighbouringCell);
if (neighbouringTile.Density == density)
continue;
UpdateNetWorth(neighbouringTile.Type, neighbouringTile.Density, neighbouringTile.Type, density);
neighbouringTile.Density = density;
Tiles[neighbouringCell] = neighbouringTile;
if (CellChanged != null)
CellChanged(neighbouringCell, type);
}
}
protected virtual string ChooseRandomVariant(ResourceType t)
void UpdateNetWorth(ResourceType oldType, int oldDensity, ResourceType newType, int newDensity)
{
return t.Variants.Keys.Random(Game.CosmeticRandom);
// Density + 1 as workaround for fixing ResourceLayer.Harvest as it would be very disruptive to balancing
if (oldType != null && oldDensity > 0)
NetWorth -= (oldDensity + 1) * oldType.Info.ValuePerUnit;
if (newType != null && newDensity > 0)
NetWorth += (newDensity + 1) * newType.Info.ValuePerUnit;
}
public int ResourceDensityAt(CPos c)
public int CalculateCellDensity(ResourceType type, CPos c)
{
var resources = Map.Resources;
if (type == null || resources[c].Type != type.Info.ResourceType)
return 0;
// Set density based on the number of neighboring resources
var adjacent = 0;
var type = Tiles[c].Type;
var resources = Map.Resources;
for (var u = -1; u < 2; u++)
{
for (var v = -1; v < 2; v++)
@@ -142,83 +148,14 @@ namespace OpenRA.Mods.Common.Traits
return Math.Max(int2.Lerp(0, type.Info.MaxDensity, adjacent, 9), 1);
}
public virtual EditorCellContents UpdateDirtyTile(CPos c)
{
var t = Tiles[c];
var type = t.Type;
// Empty tile
if (type == null)
{
t.Sequence = null;
return t;
}
// Density + 1 as workaround for fixing ResourceLayer.Harvest as it would be very disruptive to balancing
if (t.Density > 0)
NetWorth -= (t.Density + 1) * type.Info.ValuePerUnit;
// Set density based on the number of neighboring resources
t.Density = ResourceDensityAt(c);
NetWorth += (t.Density + 1) * type.Info.ValuePerUnit;
t.Sequence = type.Variants[t.Variant];
t.Frame = int2.Lerp(0, t.Sequence.Length - 1, t.Density, type.Info.MaxDensity);
return t;
}
void IRenderOverlay.Render(WorldRenderer wr)
{
if (wr.World.Type != WorldType.Editor)
return;
foreach (var c in Dirty)
{
if (Tiles.Contains(c))
{
var resource = UpdateDirtyTile(c);
Tiles[c] = resource;
foreach (var kv in spriteLayers)
{
// resource.Type is meaningless (and may be null) if resource.Sequence is null
if (resource.Sequence != null && resource.Type.Palette == kv.Key)
kv.Value.Update(c, resource.Sequence, resource.Frame);
else
kv.Value.Clear(c);
}
}
}
Dirty.Clear();
foreach (var l in spriteLayers.Values)
l.Draw(wr.Viewport);
}
void INotifyActorDisposing.Disposing(Actor self)
{
if (disposed)
return;
foreach (var kv in spriteLayers.Values)
kv.Dispose();
Map.Resources.CellEntryChanged -= UpdateCell;
disposed = true;
}
}
public struct EditorCellContents
{
public static readonly EditorCellContents Empty = default(EditorCellContents);
public ResourceType Type;
public int Density;
public string Variant;
public ISpriteSequence Sequence;
public int Frame;
}
}

View File

@@ -16,20 +16,36 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public struct ResourceLayerContents
{
public static readonly ResourceLayerContents Empty = default(ResourceLayerContents);
public ResourceType Type;
public int Density;
}
public interface IResourceLayerInfo : ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface IResourceLayer
{
event Action<CPos, ResourceType> CellChanged;
ResourceLayerContents GetResource(CPos cell);
bool IsVisible(CPos cell);
}
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
public class ResourceLayerInfo : TraitInfo, Requires<ResourceTypeInfo>, Requires<BuildingInfluenceInfo>
public class ResourceLayerInfo : TraitInfo, IResourceLayerInfo, Requires<ResourceTypeInfo>, Requires<BuildingInfluenceInfo>
{
public override object Create(ActorInitializer init) { return new ResourceLayer(init.Self); }
}
public class ResourceLayer : IWorldLoaded
public class ResourceLayer : IResourceLayer, IWorldLoaded
{
static readonly CellContents EmptyCell = default(CellContents);
readonly World world;
readonly BuildingInfluence buildingInfluence;
protected readonly CellLayer<CellContents> Content;
protected readonly CellLayer<ResourceLayerContents> Content;
public bool IsResourceLayerEmpty { get { return resCells < 1; } }
@@ -42,7 +58,7 @@ namespace OpenRA.Mods.Common.Traits
world = self.World;
buildingInfluence = self.Trait<BuildingInfluence>();
Content = new CellLayer<CellContents>(world.Map);
Content = new CellLayer<ResourceLayerContents>(world.Map);
}
int GetAdjacentCellsWith(ResourceType t, CPos cell)
@@ -85,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits
// Adjacent includes the current cell, so is always >= 1
var adjacent = GetAdjacentCellsWith(type, cell);
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
var temp = GetResource(cell);
var temp = Content[cell];
temp.Density = Math.Max(density, 1);
Content[cell] = temp;
@@ -120,12 +136,12 @@ namespace OpenRA.Mods.Common.Traits
|| (currentResourceType == null && AllowResourceAt(newResourceType, cell));
}
CellContents CreateResourceCell(ResourceType t, CPos cell)
ResourceLayerContents CreateResourceCell(ResourceType t, CPos cell)
{
world.Map.CustomTerrain[cell] = world.Map.Rules.TileSet.GetTerrainIndex(t.Info.TerrainType);
++resCells;
return new CellContents
return new ResourceLayerContents
{
Type = t
};
@@ -161,7 +177,7 @@ namespace OpenRA.Mods.Common.Traits
if (--c.Density < 0)
{
Content[cell] = EmptyCell;
Content[cell] = ResourceLayerContents.Empty;
world.Map.CustomTerrain[cell] = byte.MaxValue;
--resCells;
}
@@ -184,30 +200,18 @@ namespace OpenRA.Mods.Common.Traits
--resCells;
// Clear cell
Content[cell] = EmptyCell;
Content[cell] = ResourceLayerContents.Empty;
world.Map.CustomTerrain[cell] = byte.MaxValue;
if (CellChanged != null)
CellChanged(cell, c.Type);
}
public CellContents GetResource(CPos cell) { return Content[cell]; }
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }
public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
public int GetMaxResourceDensity(CPos cell)
{
if (Content[cell].Type == null)
return 0;
return Content[cell].Type.Info.MaxDensity;
}
public struct CellContents
{
public static readonly CellContents Empty = default(CellContents);
public ResourceType Type;
public int Density;
}
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Content[cell]; }
bool IResourceLayer.IsVisible(CPos cell) { return !world.FogObscures(cell); }
}
}

View File

@@ -18,7 +18,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Visualizes the state of the `ResourceLayer`.", " Attach this to the world actor.")]
public class ResourceRendererInfo : TraitInfo, Requires<ResourceLayerInfo>
public class ResourceRendererInfo : TraitInfo, Requires<IResourceLayerInfo>
{
[FieldLoader.Require]
[Desc("Only render these ResourceType names.")]
@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Traits
public class ResourceRenderer : IWorldLoaded, IRenderOverlay, ITickRender, INotifyActorDisposing
{
protected readonly ResourceLayer ResourceLayer;
protected readonly IResourceLayer ResourceLayer;
protected readonly CellLayer<RendererCellContents> RenderContent;
protected readonly ResourceRendererInfo Info;
@@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Traits
{
Info = info;
ResourceLayer = self.Trait<ResourceLayer>();
ResourceLayer = self.Trait<IResourceLayer>();
ResourceLayer.CellChanged += AddDirtyCell;
RenderContent = new CellLayer<RendererCellContents>(self.World.Map);
@@ -84,7 +84,7 @@ namespace OpenRA.Mods.Common.Traits
// because the shroud may not be enabled.
foreach (var cell in w.Map.AllCells)
{
var type = ResourceLayer.GetResourceType(cell);
var type = ResourceLayer.GetResource(cell).Type;
if (type != null && Info.RenderTypes.Contains(type.Info.Type))
{
var resourceContent = ResourceLayer.GetResource(cell);
@@ -117,7 +117,7 @@ namespace OpenRA.Mods.Common.Traits
{
foreach (var cell in dirty)
{
if (self.World.FogObscures(cell))
if (!ResourceLayer.IsVisible(cell))
continue;
var resourceContent = ResourceLayer.GetResource(cell);