From 4a4217c3b351ceda05d5e6925b43f1cf3f784150 Mon Sep 17 00:00:00 2001 From: Ashley Newson Date: Wed, 30 Apr 2025 01:06:27 +0100 Subject: [PATCH] Fix resource calculation in map generator The editor's resource value calculation logic was fixed in #21820 to match actual gameplay. The map generator logic originally copied the editor logic from EditorResourceLayer rather than ResourceLayer and associated game logic. As such, it inherited incorrect calculations. This change updates the map generator logic to match the now consistent logic in both ResourceLayer classes. Map generator resource settings are also adjusted -25% to continue roughly matching the previous resource output. --- .../Traits/World/ExperimentalMapGenerator.cs | 31 ++++++++++--------- mods/cnc/rules/map-generators.yaml | 8 ++--- mods/ra/rules/map-generators.yaml | 8 ++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/World/ExperimentalMapGenerator.cs b/OpenRA.Mods.Common/Traits/World/ExperimentalMapGenerator.cs index 3519795107..8ae428622a 100644 --- a/OpenRA.Mods.Common/Traits/World/ExperimentalMapGenerator.cs +++ b/OpenRA.Mods.Common/Traits/World/ExperimentalMapGenerator.cs @@ -1681,8 +1681,7 @@ namespace OpenRA.Mods.Common.Traits map.Resources.Clear(); // Return resource value of a given square. - // See https://github.com/OpenRA/OpenRA/blob/9302bac6199fbc925a85fd7a08fc2ba4b9317d16/OpenRA.Mods.Common/Traits/World/ResourceLayer.cs#L144-L166 - // https://github.com/OpenRA/OpenRA/blob/9302bac6199fbc925a85fd7a08fc2ba4b9317d16/OpenRA.Mods.Common/Traits/World/EditorResourceLayer.cs#L175-L183 + // Matches the logic in ResourceLayer trait. int CheckValue(CPos cpos) { if (!map.Resources.Contains(cpos)) @@ -1690,22 +1689,24 @@ namespace OpenRA.Mods.Common.Traits var resource = map.Resources[cpos].Type; if (resource == 0) return 0; - var adjacent = 0; - for (var y = -1; y <= 1; y++) - for (var x = -1; x <= 1; x++) - { - var offsetCpos = cpos + new CVec(x, y); - if (!map.Resources.Contains(offsetCpos)) - continue; - if (map.Resources[offsetCpos].Type == resource) - adjacent++; - } var resourceType = bestResource[cpos]; - var density = Math.Max(resourceType.MaxDensity * adjacent / /*maxAdjacent=*/9, 1); - // density + 1 to mirror a bug that got ossified due to balancing. - return param.ResourceValues[resourceType] * (density + 1); + var adjacent = 0; + var directions = CVec.Directions; + for (var i = 0; i < directions.Length; i++) + { + var c = cpos + directions[i]; + if (map.Resources.Contains(c) && map.Resources[c].Type == resource) + ++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. + var density = Math.Max(int2.Lerp(0, resourceType.MaxDensity, adjacent, 9), 1); + + return param.ResourceValues[resourceType] * density; } int CheckValue3By3(CPos cpos) diff --git a/mods/cnc/rules/map-generators.yaml b/mods/cnc/rules/map-generators.yaml index 733e7b7d6f..b77794f98a 100644 --- a/mods/cnc/rules/map-generators.yaml +++ b/mods/cnc/rules/map-generators.yaml @@ -268,7 +268,7 @@ Label: label-cnc-map-generator-choice-resources-low Settings: SpawnResourceSpawns: 1 - ResourcesPerPlayer: 25000 + ResourcesPerPlayer: 18750 ResourceSpawnWeights: split2: 1 split3: 1 @@ -278,7 +278,7 @@ Label: label-cnc-map-generator-choice-resources-medium Settings: SpawnResourceSpawns: 2 - ResourcesPerPlayer: 50000 + ResourcesPerPlayer: 37500 ResourceSpawnWeights: split2: 95 split3: 95 @@ -289,7 +289,7 @@ Label: label-cnc-map-generator-choice-resources-high Settings: SpawnResourceSpawns: 3 - ResourcesPerPlayer: 75000 + ResourcesPerPlayer: 56250 ResourceSpawnWeights: split2: 9 split3: 9 @@ -300,7 +300,7 @@ Label: label-cnc-map-generator-choice-resources-very-high Settings: SpawnResourceSpawns: 4 - ResourcesPerPlayer: 100000 + ResourcesPerPlayer: 75000 ResourceSpawnWeights: split2: 8 split3: 8 diff --git a/mods/ra/rules/map-generators.yaml b/mods/ra/rules/map-generators.yaml index 5912e2a51a..f72d42ed24 100644 --- a/mods/ra/rules/map-generators.yaml +++ b/mods/ra/rules/map-generators.yaml @@ -295,7 +295,7 @@ Label: label-ra-map-generator-choice-resources-low Settings: SpawnResourceSpawns: 2 - ResourcesPerPlayer: 25000 + ResourcesPerPlayer: 18750 ResourceSpawnWeights: mine: 1 MaximumExpansionResourceSpawns: 3 @@ -304,7 +304,7 @@ Label: label-ra-map-generator-choice-resources-medium Settings: SpawnResourceSpawns: 3 - ResourcesPerPlayer: 50000 + ResourcesPerPlayer: 37500 ResourceSpawnWeights: mine: 95 gmine: 5 @@ -314,7 +314,7 @@ Label: label-ra-map-generator-choice-resources-high Settings: SpawnResourceSpawns: 3 - ResourcesPerPlayer: 75000 + ResourcesPerPlayer: 56250 ResourceSpawnWeights: mine: 9 gmine: 1 @@ -324,7 +324,7 @@ Label: label-ra-map-generator-choice-resources-very-high Settings: SpawnResourceSpawns: 4 - ResourcesPerPlayer: 100000 + ResourcesPerPlayer: 75000 ResourceSpawnWeights: mine: 8 gmine: 2