Remove BuildingInfo.AllowPlacementOnResources and use TerrainTypes to check whether a building can be placed on particular cell

This commit is contained in:
michaeldgg2
2025-06-14 17:36:21 +02:00
committed by Gustas Kažukauskas
parent abc3a79330
commit d657a72107
4 changed files with 70 additions and 6 deletions

View File

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

View File

@@ -84,10 +84,7 @@ namespace OpenRA.Mods.Common.Traits
if (bi.AllowInvalidPlacement)
return true;
var resourceLayer = world.WorldActor.TraitOrDefault<IResourceLayer>();
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)

View File

@@ -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
{
/// <summary>
/// Replaces the BaseAttackNotifier with a new AttackNotifier that uses the
/// new attack system.
/// </summary>
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<string> terrainTypesWithResources = [];
IEnumerable<string> IBeforeUpdateActors.BeforeUpdateActors(ModData modData, List<MiniYamlNodeBuilder> resolvedActors)
{
var resourceLayerInfo = modData.DefaultRules.Actors[SystemActors.World].TraitInfoOrDefault<ResourceLayerInfo>();
if (resourceLayerInfo == null)
yield break;
foreach (var resourceType in resourceLayerInfo.ResourceTypes.Values)
{
terrainTypesWithResources.Add(resourceType.TerrainType);
}
}
public override IEnumerable<string> 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<List<string>>() ?? [];
foreach (var terrainType in terrainTypesWithResources)
{
if (!allowedTerrainTypes.Contains(terrainType))
allowedTerrainTypes.Add(terrainType);
}
terrainTypesNode.ReplaceValue(string.Join(", ", allowedTerrainTypes));
}
yield break;
}
}
}

View File

@@ -70,6 +70,7 @@ namespace OpenRA.Mods.Common.UpdateRules
// bleed only changes here.
new ReplaceBaseAttackNotifier(),
new RemoveBuildingInfoAllowPlacementOnResources(),
]),
];