Overhaul resource layer logic:
* ResourceType trait has been removed. * Simulation-related data is now defined on the ResourceLayer (which mods can subclass/replace). * Support non-money resources by moving the resource values to the PlayerResources trait. * Allow mods to disable the neighbour density override and instead always use the map-defined densities. * Allow mods to define their own resource placement logic (e.g. allow resources on slopes) by subclassing (Editor)ResourceLayer. * Improve ability to subclass/override ResourceRenderer by exposing more virtual methods.
This commit is contained in:
@@ -13,22 +13,47 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
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, IResourceLayerInfo, Requires<ResourceTypeInfo>
|
||||
public class EditorResourceLayerInfo : TraitInfo, IResourceLayerInfo, IMapPreviewSignatureInfo
|
||||
{
|
||||
public override object Create(ActorInitializer init) { return new EditorResourceLayer(init.Self); }
|
||||
[FieldLoader.LoadUsing(nameof(LoadResourceTypes))]
|
||||
public readonly Dictionary<string, ResourceLayerInfo.ResourceTypeInfo> ResourceTypes = null;
|
||||
|
||||
// Copied from ResourceLayerInfo
|
||||
protected static object LoadResourceTypes(MiniYaml yaml)
|
||||
{
|
||||
var ret = new Dictionary<string, ResourceLayerInfo.ResourceTypeInfo>();
|
||||
var resources = yaml.Nodes.FirstOrDefault(n => n.Key == "ResourceTypes");
|
||||
if (resources != null)
|
||||
foreach (var r in resources.Value.Nodes)
|
||||
ret[r.Key] = new ResourceLayerInfo.ResourceTypeInfo(r.Value);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
[Desc("Override the density saved in maps with values calculated based on the number of neighbouring resource cells.")]
|
||||
public readonly bool RecalculateResourceDensity = false;
|
||||
|
||||
void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<(MPos, Color)> destinationBuffer)
|
||||
{
|
||||
ResourceLayerInfo.PopulateMapPreviewSignatureCells(map, ResourceTypes, destinationBuffer);
|
||||
}
|
||||
|
||||
public override object Create(ActorInitializer init) { return new EditorResourceLayer(init.Self, this); }
|
||||
}
|
||||
|
||||
public class EditorResourceLayer : IResourceLayer, IWorldLoaded, INotifyActorDisposing
|
||||
{
|
||||
readonly EditorResourceLayerInfo info;
|
||||
protected readonly Map Map;
|
||||
protected readonly Dictionary<string, ResourceTypeInfo> ResourceInfo;
|
||||
protected readonly Dictionary<int, string> Resources;
|
||||
protected readonly Dictionary<byte, string> ResourceTypesByIndex;
|
||||
protected readonly CellLayer<ResourceLayerContents> Tiles;
|
||||
protected Dictionary<string, int> resourceValues;
|
||||
|
||||
public int NetWorth { get; protected set; }
|
||||
|
||||
@@ -39,10 +64,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles.Contains(cell) ? Tiles[cell] : default; }
|
||||
int IResourceLayer.GetMaxDensity(string resourceType)
|
||||
{
|
||||
if (!ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
return 0;
|
||||
|
||||
return resourceInfo.MaxDensity;
|
||||
return info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo) ? resourceInfo.MaxDensity : 0;
|
||||
}
|
||||
|
||||
bool IResourceLayer.CanAddResource(string resourceType, CPos cell, int amount) { return CanAddResource(resourceType, cell, amount); }
|
||||
@@ -52,17 +74,17 @@ namespace OpenRA.Mods.Common.Traits
|
||||
bool IResourceLayer.IsVisible(CPos cell) { return Map.Contains(cell); }
|
||||
bool IResourceLayer.IsEmpty => false;
|
||||
|
||||
public EditorResourceLayer(Actor self)
|
||||
public EditorResourceLayer(Actor self, EditorResourceLayerInfo info)
|
||||
{
|
||||
if (self.World.Type != WorldType.Editor)
|
||||
return;
|
||||
|
||||
this.info = info;
|
||||
Map = self.World.Map;
|
||||
Tiles = new CellLayer<ResourceLayerContents>(Map);
|
||||
ResourceInfo = self.TraitsImplementing<ResourceType>()
|
||||
.ToDictionary(r => r.Info.Type, r => r.Info);
|
||||
Resources = ResourceInfo.Values
|
||||
.ToDictionary(r => r.ResourceType, r => r.Type);
|
||||
ResourceTypesByIndex = info.ResourceTypes.ToDictionary(
|
||||
kv => kv.Value.ResourceIndex,
|
||||
kv => kv.Key);
|
||||
|
||||
Map.Resources.CellEntryChanged += UpdateCell;
|
||||
}
|
||||
@@ -72,6 +94,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (w.Type != WorldType.Editor)
|
||||
return;
|
||||
|
||||
var playerResourcesInfo = w.Map.Rules.Actors["player"].TraitInfoOrDefault<PlayerResourcesInfo>();
|
||||
resourceValues = playerResourcesInfo?.ResourceValues ?? new Dictionary<string, int>();
|
||||
|
||||
foreach (var cell in Map.AllCells)
|
||||
UpdateCell(cell);
|
||||
}
|
||||
@@ -87,9 +112,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
var newTile = ResourceLayerContents.Empty;
|
||||
var newTerrain = byte.MaxValue;
|
||||
if (Resources.TryGetValue(tile.Type, out var resourceType) && ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
if (ResourceTypesByIndex.TryGetValue(tile.Type, out var resourceType) && info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
|
||||
{
|
||||
newTile = new ResourceLayerContents(resourceType, CalculateCellDensity(resourceType, cell));
|
||||
newTile = new ResourceLayerContents(resourceType, CalculateCellDensity(new ResourceLayerContents(resourceType, tile.Index), cell));
|
||||
newTerrain = Map.Rules.TerrainInfo.GetTerrainIndex(resourceInfo.TerrainType);
|
||||
}
|
||||
|
||||
@@ -102,7 +127,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Map.CustomTerrain[uv] = newTerrain;
|
||||
CellChanged?.Invoke(cell, newTile.Type);
|
||||
|
||||
// Neighbouring cell density depends on this cell
|
||||
if (!info.RecalculateResourceDensity)
|
||||
return;
|
||||
|
||||
// Update neighbour density to account for this cell
|
||||
foreach (var d in CVec.Directions)
|
||||
{
|
||||
var neighbouringCell = cell + d;
|
||||
@@ -110,7 +138,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
continue;
|
||||
|
||||
var neighbouringTile = Tiles[neighbouringCell];
|
||||
var density = CalculateCellDensity(neighbouringTile.Type, neighbouringCell);
|
||||
if (neighbouringTile.Type == null)
|
||||
continue;
|
||||
|
||||
var density = CalculateCellDensity(neighbouringTile, neighbouringCell);
|
||||
if (neighbouringTile.Density == density)
|
||||
continue;
|
||||
|
||||
@@ -124,19 +155,22 @@ namespace OpenRA.Mods.Common.Traits
|
||||
void UpdateNetWorth(string oldResourceType, int oldDensity, string newResourceType, int newDensity)
|
||||
{
|
||||
// Density + 1 as workaround for fixing ResourceLayer.Harvest as it would be very disruptive to balancing
|
||||
if (oldResourceType != null && oldDensity > 0 && ResourceInfo.TryGetValue(oldResourceType, out var oldResourceInfo))
|
||||
NetWorth -= (oldDensity + 1) * oldResourceInfo.ValuePerUnit;
|
||||
if (oldResourceType != null && oldDensity > 0 && resourceValues.TryGetValue(oldResourceType, out var oldResourceValue))
|
||||
NetWorth -= (oldDensity + 1) * oldResourceValue;
|
||||
|
||||
if (newResourceType != null && newDensity > 0 && ResourceInfo.TryGetValue(newResourceType, out var newResourceInfo))
|
||||
NetWorth += (newDensity + 1) * newResourceInfo.ValuePerUnit;
|
||||
if (newResourceType != null && newDensity > 0 && resourceValues.TryGetValue(newResourceType, out var newResourceValue))
|
||||
NetWorth += (newDensity + 1) * newResourceValue;
|
||||
}
|
||||
|
||||
public int CalculateCellDensity(string resourceType, CPos c)
|
||||
protected virtual int CalculateCellDensity(ResourceLayerContents contents, CPos c)
|
||||
{
|
||||
var resources = Map.Resources;
|
||||
if (resourceType == null || !ResourceInfo.TryGetValue(resourceType, out var resourceInfo) || resources[c].Type != resourceInfo.ResourceType)
|
||||
if (contents.Type == null || !info.ResourceTypes.TryGetValue(contents.Type, out var resourceInfo) || resources[c].Type != resourceInfo.ResourceIndex)
|
||||
return 0;
|
||||
|
||||
if (!info.RecalculateResourceDensity)
|
||||
return contents.Density.Clamp(1, resourceInfo.MaxDensity);
|
||||
|
||||
// Set density based on the number of neighboring resources
|
||||
var adjacent = 0;
|
||||
for (var u = -1; u < 2; u++)
|
||||
@@ -144,7 +178,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
for (var v = -1; v < 2; v++)
|
||||
{
|
||||
var cell = c + new CVec(u, v);
|
||||
if (resources.Contains(cell) && resources[cell].Type == resourceInfo.ResourceType)
|
||||
if (resources.Contains(cell) && resources[cell].Type == resourceInfo.ResourceIndex)
|
||||
adjacent++;
|
||||
}
|
||||
}
|
||||
@@ -152,23 +186,20 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return Math.Max(int2.Lerp(0, resourceInfo.MaxDensity, adjacent, 9), 1);
|
||||
}
|
||||
|
||||
bool AllowResourceAt(string resourceType, CPos cell)
|
||||
protected virtual bool AllowResourceAt(string resourceType, CPos cell)
|
||||
{
|
||||
var mapResources = Map.Resources;
|
||||
if (!mapResources.Contains(cell))
|
||||
if (!Map.Ramp.Contains(cell) || Map.Ramp[cell] != 0)
|
||||
return false;
|
||||
|
||||
if (!ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
if (!info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
|
||||
return false;
|
||||
|
||||
// Ignore custom terrain types when spawning resources in the editor
|
||||
var terrainInfo = Map.Rules.TerrainInfo;
|
||||
var terrainType = terrainInfo.TerrainTypes[terrainInfo.GetTerrainInfo(Map.Tiles[cell]).TerrainType].Type;
|
||||
if (!resourceInfo.AllowedTerrainTypes.Contains(terrainType))
|
||||
return false;
|
||||
|
||||
// TODO: Check against actors in the EditorActorLayer
|
||||
return resourceInfo.AllowOnRamps || Map.Ramp[cell] == 0;
|
||||
return resourceInfo.AllowedTerrainTypes.Contains(terrainType);
|
||||
}
|
||||
|
||||
bool CanAddResource(string resourceType, CPos cell, int amount = 1)
|
||||
@@ -177,57 +208,57 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (!resources.Contains(cell))
|
||||
return false;
|
||||
|
||||
if (!ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
if (!info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
|
||||
return false;
|
||||
|
||||
// The editor allows the user to replace one resource type with another, so treat mismatching resource type as an empty cell
|
||||
var content = resources[cell];
|
||||
if (content.Type != resourceInfo.ResourceType)
|
||||
if (content.Type != resourceInfo.ResourceIndex)
|
||||
return amount <= resourceInfo.MaxDensity && AllowResourceAt(resourceType, cell);
|
||||
|
||||
var oldDensity = content.Type == resourceInfo.ResourceType ? content.Index : 0;
|
||||
var oldDensity = content.Type == resourceInfo.ResourceIndex ? content.Index : 0;
|
||||
return oldDensity + amount <= resourceInfo.MaxDensity;
|
||||
}
|
||||
|
||||
int AddResource(string resourceType, CPos cell, int amount = 1)
|
||||
protected virtual int AddResource(string resourceType, CPos cell, int amount = 1)
|
||||
{
|
||||
var resources = Map.Resources;
|
||||
if (!resources.Contains(cell))
|
||||
return 0;
|
||||
|
||||
if (!ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
if (!info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
|
||||
return 0;
|
||||
|
||||
// The editor allows the user to replace one resource type with another, so treat mismatching resource type as an empty cell
|
||||
var content = resources[cell];
|
||||
var oldDensity = content.Type == resourceInfo.ResourceType ? content.Index : 0;
|
||||
var oldDensity = content.Type == resourceInfo.ResourceIndex ? content.Index : 0;
|
||||
var density = (byte)Math.Min(resourceInfo.MaxDensity, oldDensity + amount);
|
||||
Map.Resources[cell] = new ResourceTile((byte)resourceInfo.ResourceType, density);
|
||||
Map.Resources[cell] = new ResourceTile((byte)resourceInfo.ResourceIndex, density);
|
||||
|
||||
return density - oldDensity;
|
||||
}
|
||||
|
||||
int RemoveResource(string resourceType, CPos cell, int amount = 1)
|
||||
protected virtual int RemoveResource(string resourceType, CPos cell, int amount = 1)
|
||||
{
|
||||
var resources = Map.Resources;
|
||||
if (!resources.Contains(cell))
|
||||
return 0;
|
||||
|
||||
if (!ResourceInfo.TryGetValue(resourceType, out var resourceInfo))
|
||||
if (!info.ResourceTypes.TryGetValue(resourceType, out var resourceInfo))
|
||||
return 0;
|
||||
|
||||
var content = resources[cell];
|
||||
if (content.Type == 0 || content.Type != resourceInfo.ResourceType)
|
||||
if (content.Type == 0 || content.Type != resourceInfo.ResourceIndex)
|
||||
return 0;
|
||||
|
||||
var oldDensity = content.Index;
|
||||
var density = (byte)Math.Max(0, oldDensity - amount);
|
||||
resources[cell] = density > 0 ? new ResourceTile((byte)resourceInfo.ResourceType, density) : default;
|
||||
resources[cell] = density > 0 ? new ResourceTile(resourceInfo.ResourceIndex, density) : default;
|
||||
|
||||
return oldDensity - density;
|
||||
}
|
||||
|
||||
void ClearResources(CPos cell)
|
||||
protected virtual void ClearResources(CPos cell)
|
||||
{
|
||||
Map.Resources[cell] = default;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user