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);

View File

@@ -1,107 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.D2k.Traits
{
using ClearSides = D2kResourceRenderer.ClearSides;
[Desc("Used to render spice with round borders.")]
public class D2kEditorResourceLayerInfo : EditorResourceLayerInfo
{
public override object Create(ActorInitializer init) { return new D2kEditorResourceLayer(init.Self); }
}
public class D2kEditorResourceLayer : EditorResourceLayer
{
public D2kEditorResourceLayer(Actor self)
: base(self) { }
public override EditorCellContents UpdateDirtyTile(CPos c)
{
var t = Tiles[c];
// Empty tile
if (t.Type == null)
{
t.Sequence = null;
return t;
}
NetWorth -= t.Density * t.Type.Info.ValuePerUnit;
t.Density = ResourceDensityAt(c);
NetWorth += t.Density * t.Type.Info.ValuePerUnit;
int index;
var clear = FindClearSides(t.Type, c);
if (clear == ClearSides.None)
{
var sprites = D2kResourceRenderer.Variants[t.Variant];
var frame = t.Density > t.Type.Info.MaxDensity / 2 ? 1 : 0;
t.Sequence = t.Type.Variants.First().Value;
t.Frame = sprites[frame];
}
else if (D2kResourceRenderer.SpriteMap.TryGetValue(clear, out index))
{
t.Sequence = t.Type.Variants.First().Value;
t.Frame = index;
}
else
t.Sequence = null;
return t;
}
protected override string ChooseRandomVariant(ResourceType t)
{
return D2kResourceRenderer.Variants.Keys.Random(Game.CosmeticRandom);
}
bool CellContains(CPos c, ResourceType t)
{
return Tiles.Contains(c) && Tiles[c].Type == t;
}
ClearSides FindClearSides(ResourceType t, CPos p)
{
var ret = ClearSides.None;
if (!CellContains(p + new CVec(0, -1), t))
ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight;
if (!CellContains(p + new CVec(-1, 0), t))
ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft;
if (!CellContains(p + new CVec(1, 0), t))
ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight;
if (!CellContains(p + new CVec(0, 1), t))
ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight;
if (!CellContains(p + new CVec(-1, -1), t))
ret |= ClearSides.TopLeft;
if (!CellContains(p + new CVec(1, -1), t))
ret |= ClearSides.TopRight;
if (!CellContains(p + new CVec(-1, 1), t))
ret |= ClearSides.BottomLeft;
if (!CellContains(p + new CVec(1, 1), t))
ret |= ClearSides.BottomRight;
return ret;
}
}
}

View File

@@ -171,7 +171,7 @@ namespace OpenRA.Mods.D2k.Traits
if (clear == ClearSides.None)
{
var sprites = Variants[content.Variant];
var frame = density > ResourceLayer.GetMaxResourceDensity(cell) / 2 ? 1 : 0;
var frame = density > renderType.Info.MaxDensity / 2 ? 1 : 0;
UpdateSpriteLayers(cell, renderType.Variants.First().Value, sprites[frame], renderType.Palette);
}

View File

@@ -159,6 +159,8 @@
ValuePerUnit: 60
AllowedTerrainTypes: Clear,Road
AllowUnderActors: true
ResourceRenderer:
RenderTypes: Tiberium, BlueTiberium
World:
Inherits: ^BaseWorld
@@ -182,8 +184,6 @@ World:
Type: Crater
Sequence: craters
ResourceLayer:
ResourceRenderer:
RenderTypes: Tiberium, BlueTiberium
ResourceClaimLayer:
WarheadDebugOverlay:
CustomTerrainDebugOverlay:

View File

@@ -118,6 +118,8 @@
ValuePerUnit: 25
AllowedTerrainTypes: SpiceSand
AllowUnderActors: true
D2kResourceRenderer:
RenderTypes: Spice
World:
Inherits: ^BaseWorld
@@ -144,8 +146,6 @@ World:
WarheadDebugOverlay:
BuildableTerrainLayer:
ResourceLayer:
D2kResourceRenderer:
RenderTypes: Spice
ResourceClaimLayer:
CustomTerrainDebugOverlay:
SmudgeLayer@Rock:
@@ -242,7 +242,7 @@ EditorWorld:
Inherits: ^BaseWorld
EditorActorLayer:
EditorCursorLayer:
D2kEditorResourceLayer:
EditorResourceLayer:
EditorSelectionLayer:
LoadWidgetAtGameStart:
EditorActionManager:

View File

@@ -170,6 +170,8 @@
ValuePerUnit: 50
AllowedTerrainTypes: Clear,Road
AllowUnderActors: true
ResourceRenderer:
RenderTypes: Ore, Gems
World:
Inherits: ^BaseWorld
@@ -203,8 +205,6 @@ World:
Type: Crater
Sequence: craters
ResourceLayer:
ResourceRenderer:
RenderTypes: Ore, Gems
ResourceClaimLayer:
WarheadDebugOverlay:
SpawnMapActors:

View File

@@ -237,6 +237,8 @@
CliffBackImpassabilityLayer:
SubterraneanActorLayer:
JumpjetActorLayer:
ResourceRenderer:
RenderTypes: Tiberium, BlueTiberium, Veins
World:
Inherits: ^BaseWorld
@@ -277,8 +279,6 @@ World:
SmokeType: largesmoke
Sequence: largecraters
ResourceLayer:
ResourceRenderer:
RenderTypes: Tiberium, BlueTiberium, Veins
BridgeLayer:
CustomTerrainDebugOverlay:
ResourceClaimLayer: