Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 76 additions & 15 deletions packages/core/src/agent-operations/author-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { DEFAULT_LEVEL_HEIGHT, resolveCeilingHeight } from '../services/level-he
import { planRoofLevel } from '../services/roof-level'
import { CEILING_DRAW_OFFSET } from '../services/storey'
import { resolveRoofElevation } from '../systems/roof/roof-elevation'
import { getSlabSlopePlane, sampleSlabSlopePlane } from '../systems/slab/slab-slope'
import { stairArrivalOpening } from '../systems/stair/stair-footprint'
import {
getMaxWallCurveOffset,
Expand Down Expand Up @@ -1317,25 +1318,84 @@ export function authorScene(
const id = `slab/${index}/${i}`
const label = s.name ? `"${s.name}"` : `slab ${i}`
const polygon = s.polygon as Vec2[]
// A slab over a wall's footprint, higher than half the wall: a cap, which would lift the
// wall onto it. Pascal caps its walls itself.
const lifted = [...wallNodes.values()].find(
(w) =>
s.top > w.height / 2 &&
polygonsOverlap(
polygon,
wallFootprint({
...w,
thickness: w.spec.thickness,
curveOffset: w.node.curveOffset,
}),
),
)
const slabLike = {
polygon,
elevation: s.top,
thickness: s.thickness,
recessed: false,
autoFromWalls: false,
slope: s.slope && s.slope.pitch > 0 ? s.slope : undefined,
}
const slabPlane = getSlabSlopePlane(slabLike)
const slabIsSloped = slabPlane.sloped
const maxOverlapTop = (wf: Vec2[]): number => {
if (!slabIsSloped) return s.top
const overlap = intersection(polygon, wf)
let maxTop = Number.NEGATIVE_INFINITY
for (const poly of overlap) {
for (const [vx, vz] of poly.outer) {
const top = sampleSlabSlopePlane(slabPlane, vx, vz)
if (top > maxTop) maxTop = top
}
}
if (Number.isFinite(maxTop)) return maxTop
for (const [vx, vz] of wf) {
if (pointInPolygon([vx, vz], polygon, true)) {
const top = sampleSlabSlopePlane(slabPlane, vx, vz)
if (top > maxTop) maxTop = top
}
}
for (const [vx, vz] of polygon) {
if (pointInPolygon([vx, vz], wf, true)) {
const top = sampleSlabSlopePlane(slabPlane, vx, vz)
if (top > maxTop) maxTop = top
}
}
for (let i = 0; i < polygon.length; i++) {
const [ax, az] = polygon[i]!
const [bx, bz] = polygon[(i + 1) % polygon.length]!
const edx = bx - ax
const edz = bz - az
for (let j = 0; j < wf.length; j++) {
const [cx, cz] = wf[j]!
const [dx, dz] = wf[(j + 1) % wf.length]!
const fdx = dx - cx
const fdz = dz - cz
const denom = edx * fdz - edz * fdx
if (Math.abs(denom) < 1e-12) continue
const t = ((cx - ax) * fdz - (cz - az) * fdx) / denom
const u = ((cx - ax) * edz - (cz - az) * edx) / denom
if (t >= -1e-9 && t <= 1 + 1e-9 && u >= -1e-9 && u <= 1 + 1e-9) {
const top = sampleSlabSlopePlane(slabPlane, ax + edx * t, az + edz * t)
if (top > maxTop) maxTop = top
}
}
}
return Number.isFinite(maxTop) ? maxTop : slabPlane.maxTop
}
// A slab over a wall's footprint, higher than half the wall at the overlap: a cap, which
// would lift the wall onto it. Pascal caps its walls itself.
let lifted:
| { wall: typeof wallNodes extends Map<unknown, infer V> ? V : never; overlapTop: number }
| undefined
for (const w of wallNodes.values()) {
const wf = wallFootprint({
...w,
thickness: w.spec.thickness,
curveOffset: w.node.curveOffset,
})
if (!polygonsOverlap(polygon, wf)) continue
const overlapTop = maxOverlapTop(wf)
if (overlapTop > w.height / 2) {
lifted = { wall: w, overlapTop }
break
}
}
if (lifted) {
defect(
id,
'slab_over_wall',
`${label} overlaps wall ${lifted.spec.id} with its top at ${metres(s.top)}, above half the wall's ${metres(lifted.height)}: it would lift the wall onto it. Pascal builds a wall's cap itself; fold it into the wall.`,
`${label} overlaps wall ${lifted.wall.spec.id} with its top at ${metres(lifted.overlapTop)}, above half the wall's ${metres(lifted.wall.height)}: it would lift the wall onto it. Pascal builds a wall's cap itself; fold it into the wall.`,
)
return
}
Expand All @@ -1357,6 +1417,7 @@ export function authorScene(
polygon: s.polygon,
elevation: s.top,
thickness: s.thickness,
...(s.slope && s.slope.pitch > 0 ? { slope: s.slope } : {}),
...(finish ? { slots: { surface: finish } } : {}),
})
place(node)
Expand Down
35 changes: 25 additions & 10 deletions packages/core/src/agent-operations/layout-clearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
*/

import { floorConstructionLift, liftedManualSlab } from '../lib/floor-construction-lift'
import { selectSlabSupportForItem } from '../lib/item-slab-support'
import {
getSlabSupportElevationForFootprint,
selectSlabSupportForItem,
} from '../lib/item-slab-support'
import { getRenderableSlabPolygon } from '../lib/slab-polygon'
import { levelBaseElevationAt } from '../lib/terrain-support-query'
import {
Expand Down Expand Up @@ -224,17 +227,29 @@ function itemFloorLift(node: ItemNode, nodes: Record<string, AnyNode>): number {
}
return parent?.type === 'level' && parent.id === levelId
})
const support = selectSlabSupportForItem(
slabs,
{ position: node.position, dimensions: getScaledDimensions(node), rotation: node.rotation },
(slab) =>
getRenderableSlabPolygon(slab, {
const footprint = {
position: node.position,
dimensions: getScaledDimensions(node),
rotation: node.rotation,
}
const renderedPolygons = new Map<string, [number, number][]>()
const renderedPolygonFor = (slab: Extract<AnyNode, { type: 'slab' }>): [number, number][] => {
let cached = renderedPolygons.get(slab.id)
if (!cached) {
cached = getRenderableSlabPolygon(slab, {
walls,
siblingSlabs: slabs.filter((other) => other.id !== slab.id),
}),
{ preferredSlabId: node.supportSlabId },
)
return support ? support.elevation : ground()
})
renderedPolygons.set(slab.id, cached)
}
return cached
}
const support = selectSlabSupportForItem(slabs, footprint, renderedPolygonFor, {
preferredSlabId: node.supportSlabId,
})
return support
? getSlabSupportElevationForFootprint(support, footprint, renderedPolygonFor(support))
: ground()
}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/agent-tools/author-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ export const authorSceneSlab = z.object({
default: 0,
},
),
slope: z
.object({
direction: measurement('angle', 'deg', {
default: 0,
description:
'Uphill azimuth in degrees in the XZ plane (0 = +X, 90 = +Z, 180 = -X, 270 = -Z).',
}),
pitch: measurement('angle', 'deg', {
min: 0,
max: 45,
description: 'Slope angle in degrees (0 = flat, max 45).',
}),
})
.optional()
.describe(
'Optional planar slope; top stays the low-edge walking surface and floor-placed items lift to the sloped surface.',
),
finish: z.string().optional().describe('library:<id> or #rrggbb (an exact colour).'),
surfaceCode: z
.string()
Expand Down
100 changes: 67 additions & 33 deletions packages/core/src/hooks/spatial-grid/floor-placed-elevation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
FloorPlacedFootprintContext,
FloorPlacedFootprintsResolver,
} from '../../registry/types'
import type { AnyNode, AnyNodeId, SlabNode } from '../../schema'
import type { AnyNode, AnyNodeId } from '../../schema'
import { hasSlabSlope } from '../../systems/slab/slab-slope'
import { getFloorPlacedFootprints } from './floor-placed-footprints'

export { getFloorPlacedFootprints } from './floor-placed-footprints'
Expand Down Expand Up @@ -110,46 +111,53 @@ export function getFloorPlacedElevation({
// A floor a room's script built (planks, tiles) is the walking surface on its plate: what stands
// there stands on the build's top, not inside it.
const onFloor = (slabId: string, elevation: number, at: [number, number, number]) =>
floorFinishTopAt(nodes, resolvedLevelId, slabId, at[0], at[2]) ?? finiteSlabElevation(elevation)
finiteSlabElevation(floorFinishTopAt(nodes, resolvedLevelId, slabId, at[0], at[2]) ?? elevation)

// A persisted support host pins the elevation while it still exists and
// overlaps a footprint — deterministic across stacked slabs. A stale
// host (deleted or reshaped away) silently falls through to the
// election below; this per-frame read path never writes the field.
// Skipped entirely under a pointer cap: the cursor, not the stored
// host, decides the target surface during a drag.
// overlaps a footprint — deterministic across stacked slabs. Across
// multi-footprint nodes (cabinet runs, multi-flight stairs) on a sloped or
// platform-covered host, the node rests on the highest pinned footprint so
// uphill parts never clip into the ramp. A stale host (deleted or reshaped
// away) silently falls through to the election below; this per-frame read
// path never writes the field. Skipped entirely under a pointer cap: the
// cursor, not the stored host, decides the target surface during a drag.
const supportSlabId = (effectiveNode as { supportSlabId?: string | null }).supportSlabId
if (maxElevation == null && supportSlabId) {
if (supportSlabId === GROUND_SUPPORT_ID) return groundLift()
const host = nodes[supportSlabId]
let pinned = Number.NEGATIVE_INFINITY
for (const footprint of footprints) {
if (host?.type === 'slab' && host.plateRole === 'base') {
const coverings = activeSpatialGrid()
.getSupportCandidatesForFootprint(
resolvedLevelId,
footprint.position ?? position,
footprint.dimensions,
footprint.rotation,
)
.map((candidate) => nodes[candidate.slabId])
.filter(
(candidate): candidate is SlabNode =>
candidate?.type === 'slab' && candidate.plateRole === 'platform',
)
.sort((a, b) => b.elevation - a.elevation)
const covering = coverings[0]
if (covering)
return onFloor(covering.id, covering.elevation, footprint.position ?? position)
const sampleAt = footprint.position ?? position
if (host?.type === 'slab' && host.parentId === resolvedLevelId && host.plateRole === 'base') {
let covered = false
for (const candidate of activeSpatialGrid().getSupportCandidatesForFootprint(
resolvedLevelId,
sampleAt,
footprint.dimensions,
footprint.rotation,
)) {
const candidateNode = nodes[candidate.slabId]
if (candidateNode?.type === 'slab' && candidateNode.plateRole === 'platform') {
covered = true
const elevation = onFloor(candidate.slabId, candidate.elevation, sampleAt)
if (elevation > pinned) pinned = elevation
}
}
if (covered) continue
}
const hosted = activeSpatialGrid().getHostSlabElevationForFootprint(
resolvedLevelId,
supportSlabId,
footprint.position ?? position,
sampleAt,
footprint.dimensions,
footprint.rotation,
)
if (hosted !== null) return onFloor(supportSlabId, hosted, footprint.position ?? position)
if (hosted !== null) {
const elevation = onFloor(supportSlabId, hosted, sampleAt)
if (elevation > pinned) pinned = elevation
}
}
if (pinned !== Number.NEGATIVE_INFINITY) return pinned
}

// Per footprint the support is its winning slab, or the level base when no slab
Expand All @@ -162,19 +170,45 @@ export function getFloorPlacedElevation({
// keeps this identical on flat ground, where `groundLift()` is 0: a composite node
// straddling a recessed slab and bare floor still rests on the floor rather than
// sinking into the recess.
let elected = Number.NEGATIVE_INFINITY
for (const footprint of footprints) {
const acceptedSlopedHosts =
maxElevation != null && footprints.length > 1 ? new Set<string>() : null
const unpinnedSupports = footprints.map((footprint) => {
const sampleAt = footprint.position ?? position
const support = activeSpatialGrid().getSlabSupportForItem(
resolvedLevelId,
footprint.position ?? position,
sampleAt,
footprint.dimensions,
footprint.rotation,
maxElevation,
)
const elevation =
support.slabId === null
? groundLift()
: onFloor(support.slabId, support.elevation, footprint.position ?? position)
if (acceptedSlopedHosts && support.slabId) {
const slabNode = nodes[support.slabId]
if (slabNode?.type === 'slab' && hasSlabSlope(slabNode)) {
acceptedSlopedHosts.add(support.slabId)
}
}
return { footprint, sampleAt, support }
})

let elected = Number.NEGATIVE_INFINITY
for (const { footprint, sampleAt, support } of unpinnedSupports) {
let elevation =
support.slabId === null ? groundLift() : onFloor(support.slabId, support.elevation, sampleAt)
if (acceptedSlopedHosts && acceptedSlopedHosts.size > 0) {
for (const slopedHostId of acceptedSlopedHosts) {
const hosted = activeSpatialGrid().getHostSlabElevationForFootprint(
resolvedLevelId,
slopedHostId,
sampleAt,
footprint.dimensions,
footprint.rotation,
)
if (hosted !== null) {
const hostedElevation = onFloor(slopedHostId, hosted, sampleAt)
if (hostedElevation > elevation) elevation = hostedElevation
}
}
}
if (elevation > elected) {
elected = elevation
}
Expand Down
Loading
Loading