Editor/Stair/ArchApproachAffector.cs
using System.Linq;

namespace Sunless.Architecture;

// A DRIVE THAT LANDED ON A PODIUM BELONGS TO THAT PODIUM IN THE STACK. The room is only where an approach has
// to live to have a grade to climb from, so reading its row under the house is reading it under the one thing
// it does not touch. A layer record moves the ROW alone, exactly as a porch's cover is nested, and it is
// re-derived every commit so a drive re-dragged onto the foundation comes back under its room.
public static class ArchApproachAffector {
	public static int Resolve( ArchPlan plan, ArchKit kit ) {
		var nested = 0;

		foreach ( var building in plan.Buildings ) {
			foreach ( var room in building.Rooms ) {
				nested += room.Approaches.Count( approach => Nest( plan, building, room, approach ) );
			}
		}

		return nested;
	}

	static bool Nest( ArchPlan plan, ArchBuilding building, ArchRoom room, ArchApproachPart approach ) {
		var parent = ArchApproachGen.Podium( building, approach )?.Id ?? room.Id;
		var record = plan.Layers.FirstOrDefault( entry => entry.ItemId == approach.Id );

		if ( record is null ) {
			// Never write one to say what ownership already says, or every drive off a wall grows a record.
			if ( parent == room.Id ) {
				return false;
			}

			record = new ArchLayerRecord { ItemId = approach.Id, Kind = ArchKind.Approach, Stage = ArchLayerStage.Structure };
			plan.Layers.Add( record );
		} else if ( record.ParentId == parent ) {
			return false;
		}

		record.ParentId = parent;

		return true;
	}
}