diff --git a/OpenRA.Mods.Common/Traits/Buildings/Building.cs b/OpenRA.Mods.Common/Traits/Buildings/Building.cs index 21b3813130..917acc67d4 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/Building.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/Building.cs @@ -45,8 +45,6 @@ namespace OpenRA.Mods.Common.Traits public readonly bool AllowInvalidPlacement = false; - public readonly bool AllowPlacementOnResources = false; - [Desc("Clear smudges from underneath the building footprint.")] public readonly bool RemoveSmudgesOnBuild = true; diff --git a/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs b/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs index 4c003f5a5b..bf6ca7da4a 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/BuildingUtils.cs @@ -84,10 +84,7 @@ namespace OpenRA.Mods.Common.Traits if (bi.AllowInvalidPlacement) return true; - var resourceLayer = world.WorldActor.TraitOrDefault(); - return bi.Tiles(cell).All(t => world.Map.Contains(t) && - (bi.AllowPlacementOnResources || resourceLayer == null || resourceLayer.GetResource(t).Type == null) && - world.IsCellBuildable(t, ai, bi, toIgnore)); + return bi.Tiles(cell).All(t => world.Map.Contains(t) && world.IsCellBuildable(t, ai, bi, toIgnore)); } public static IEnumerable<(CPos Cell, Actor Actor)> GetLineBuildCells(World world, CPos cell, ActorInfo ai, BuildingInfo bi, Player owner) diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/20250330/RemoveBuildingInfoAllowPlacementOnResources.cs b/OpenRA.Mods.Common/UpdateRules/Rules/20250330/RemoveBuildingInfoAllowPlacementOnResources.cs new file mode 100644 index 0000000000..226778181d --- /dev/null +++ b/OpenRA.Mods.Common/UpdateRules/Rules/20250330/RemoveBuildingInfoAllowPlacementOnResources.cs @@ -0,0 +1,68 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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.Collections.Generic; +using OpenRA.Mods.Common.Traits; + +namespace OpenRA.Mods.Common.UpdateRules.Rules +{ + /// + /// Replaces the BaseAttackNotifier with a new AttackNotifier that uses the + /// new attack system. + /// + public class RemoveBuildingInfoAllowPlacementOnResources : UpdateRule, IBeforeUpdateActors + { + public override string Name => "Remove AllowPlacementOnResources from BuildingInfo"; + public override string Description => "Removes AllowPlacementOnResources from BuildingInfo and adds terrains with resources" + + "to TerrainTypes (if a Building trait uses AllowPlacementOnResources: true)."; + + readonly HashSet terrainTypesWithResources = []; + + IEnumerable IBeforeUpdateActors.BeforeUpdateActors(ModData modData, List resolvedActors) + { + var resourceLayerInfo = modData.DefaultRules.Actors[SystemActors.World].TraitInfoOrDefault(); + if (resourceLayerInfo == null) + yield break; + + foreach (var resourceType in resourceLayerInfo.ResourceTypes.Values) + { + terrainTypesWithResources.Add(resourceType.TerrainType); + } + } + + public override IEnumerable UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode) + { + foreach (var buildingInfo in actorNode.ChildrenMatching("Building")) + { + if (buildingInfo.RemoveNodes("AllowPlacementOnResources") == 0) + continue; + + var terrainTypesNode = buildingInfo.Value.NodeWithKeyOrDefault("TerrainTypes"); + if (terrainTypesNode == null) + { + terrainTypesNode = new MiniYamlNodeBuilder("TerrainTypes", ""); + buildingInfo.AddNode(terrainTypesNode); + } + + var allowedTerrainTypes = terrainTypesNode?.NodeValue>() ?? []; + foreach (var terrainType in terrainTypesWithResources) + { + if (!allowedTerrainTypes.Contains(terrainType)) + allowedTerrainTypes.Add(terrainType); + } + + terrainTypesNode.ReplaceValue(string.Join(", ", allowedTerrainTypes)); + } + + yield break; + } + } +} diff --git a/OpenRA.Mods.Common/UpdateRules/UpdatePath.cs b/OpenRA.Mods.Common/UpdateRules/UpdatePath.cs index 1a59fed2d8..6ca5356007 100644 --- a/OpenRA.Mods.Common/UpdateRules/UpdatePath.cs +++ b/OpenRA.Mods.Common/UpdateRules/UpdatePath.cs @@ -70,6 +70,7 @@ namespace OpenRA.Mods.Common.UpdateRules // bleed only changes here. new ReplaceBaseAttackNotifier(), + new RemoveBuildingInfoAllowPlacementOnResources(), ]), ];