From d9c582132992b8b65f083556a142d307adc4f09e Mon Sep 17 00:00:00 2001 From: Gustas <37534529+PunkPun@users.noreply.github.com> Date: Fri, 28 Mar 2025 12:18:42 +0200 Subject: [PATCH] Fix resource area calculation --- .../Traits/World/EditorResourceLayer.cs | 44 ++++++++++--------- .../Traits/World/ResourceLayer.cs | 4 +- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs b/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs index c327d1660d..e73d8e1c6d 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs @@ -174,12 +174,11 @@ 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 && resourceValues.TryGetValue(oldResourceType, out var oldResourceValue)) - NetWorth -= (oldDensity + 1) * oldResourceValue; + NetWorth -= oldDensity * oldResourceValue; if (newResourceType != null && newDensity > 0 && resourceValues.TryGetValue(newResourceType, out var newResourceValue)) - NetWorth += (newDensity + 1) * newResourceValue; + NetWorth += newDensity * newResourceValue; } public int CalculateRegionValue(CellRegion sourceRegion) @@ -188,39 +187,44 @@ namespace OpenRA.Mods.Common.Traits foreach (var cell in sourceRegion.CellCoords) { var mcell = cell.ToMPos(Map); - if (Map.Resources.Contains(mcell) && Map.Resources[mcell].Type != 0) - { - resourceValueInRegion++; - var rcell = Map.Resources[mcell]; - if (ResourceTypesByIndex.TryGetValue(rcell.Type, out var resourceType) && resourceValues.TryGetValue(resourceType, out var resourceValuePerUnit)) - resourceValueInRegion += Tiles[mcell].Density * resourceValuePerUnit; - } + if (!Map.Resources.Contains(mcell)) + continue; + + var resource = Map.Resources[mcell].Type; + if (resource != 0 + && ResourceTypesByIndex.TryGetValue(resource, out var resourceType) + && resourceValues.TryGetValue(resourceType, out var resourceValuePerUnit)) + resourceValueInRegion += Tiles[mcell].Density * resourceValuePerUnit; } return resourceValueInRegion; } - protected virtual int CalculateCellDensity(ResourceLayerContents contents, CPos c) + /// + /// Matches the logic in trait. + /// + protected virtual int CalculateCellDensity(ResourceLayerContents contents, CPos cell) { var resources = Map.Resources; - if (contents.Type == null || !info.ResourceTypes.TryGetValue(contents.Type, out var resourceInfo) || resources[c].Type != resourceInfo.ResourceIndex) + if (contents.Type == null || !info.ResourceTypes.TryGetValue(contents.Type, out var resourceInfo) || resources[cell].Type != resourceInfo.ResourceIndex) return 0; if (!info.RecalculateResourceDensity) return contents.Density.Clamp(1, resourceInfo.MaxDensity); - // Set density based on the number of neighboring resources + // Set density based on the number of neighboring resources. var adjacent = 0; - for (var u = -1; u < 2; u++) + var directions = CVec.Directions; + for (var i = 0; i < directions.Length; i++) { - for (var v = -1; v < 2; v++) - { - var cell = c + new CVec(u, v); - if (resources.Contains(cell) && resources[cell].Type == resourceInfo.ResourceIndex) - adjacent++; - } + var c = cell + directions[i]; + if (resources.Contains(c) && resources[c].Type == resourceInfo.ResourceIndex) + ++adjacent; } + // We need to have at least one resource in the cell. + // HACK: we should not be lerping to 9, as maximum adjacent resources is 8. + // HACK: it's too disruptive to fix. return Math.Max(int2.Lerp(0, resourceInfo.MaxDensity, adjacent, 9), 1); } diff --git a/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs b/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs index 5266eb40e3..9ebf3414a4 100644 --- a/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs @@ -160,7 +160,9 @@ namespace OpenRA.Mods.Common.Traits ++adjacent; } - // Adjacent includes the current cell, so is always >= 1 + // We need to have at least one resource in the cell. + // HACK: we should not be lerping to 9, as maximum adjacent resources is 8. + // HACK: it's too disruptive to fix. var density = Math.Max(int2.Lerp(0, resourceInfo.MaxDensity, adjacent, 9), 1); Content[cell] = new ResourceLayerContents(resource.Type, density); }