Skip to content

Commit 5ddcc74

Browse files
David Engelhartclaude
andcommitted
lumopolis: the ring of planned parts sits a slab's room out, so a part built from it rises in place even beside an outer district
Round 6b re-review of e935c1a: - I1: the ring gap was 20, less than the 35 a one-building slab needs from any district, so in a dense world the move was refused and the part fell back to its layout slot. The ring now lies max(SLOT_CLEARANCE, the largest district's radius + a one-building slab + MIN_SIBLING_GAP) past the farthest district, and the moved district is centred on the wireframe, so no district that stood before can refuse it. PLAN_RING_GAP is now SLOT_CLEARANCE (the floor of that gap). - M1: the ring is set from the districts as laid out, before any district moves, so every wireframe of a compile shares one ring. - M2: a moved district also keeps WIRE_ROOM (a one-building slab + the sibling gap) from every wireframe still waiting, kept or new on the ring. - M3: several moves in one compile are settled together: each is checked against the others' new spots, and while one fails, the first failing one (in matched order) stays put and the rest are checked again. Deterministic. - Ring spots no longer hold -0, so they read the same after a JSON round trip. Tests: an outer nine-building district at the wireframe's angle, a waiting wireframe beside the target, a part added to the design as another is built, and two ring parts built in one compile (both rise; with targets 20 apart the first stays and the second rises). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e935c1a commit 5ddcc74

2 files changed

Lines changed: 140 additions & 41 deletions

File tree

‎src/main/world/planOverlay.ts‎

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ import { validateWorld } from '../../shared/world/validate'
1111
import { MIN_SIBLING_GAP, MOVED_TOLERANCE, SLOT_CLEARANCE, clear, districtRadius, placeBuilding } from './layout'
1212

1313
export const LINKED_DESIGN_TITLE = 'Linked design'
14-
/** How far outside the farthest district centre a planned part with no district sits. */
15-
export const PLAN_RING_GAP = 20
14+
/**
15+
* The least distance from the farthest district centre to the ring of planned parts with no district.
16+
* The ring is set at least one slab's room out (slabRoom), so the one-building district a ring part is
17+
* later built into can stand centred on its wireframe, clear of every district.
18+
*/
19+
export const PLAN_RING_GAP = SLOT_CLEARANCE
1620

1721
type Pos = [number, number, number]
18-
const round2 = (v: number): number => Math.round(v * 100) / 100
22+
/** How far a one-building district's centre stands from a district of `n` buildings: both slabs and a gap, and never nearer than a layout slot. */
23+
const slabRoom = (n: number): number => Math.max(SLOT_CLEARANCE, districtRadius(n) + districtRadius(1) + MIN_SIBLING_GAP)
24+
/** How far a one-building district's centre stands from a wireframe still waiting to be built: off its slab by a sibling gap. */
25+
const WIRE_ROOM = districtRadius(1) + MIN_SIBLING_GAP
26+
const sizeOf = (doc: WorldDoc, d: string): number => doc.buildings.filter((b) => b.districtId === d).length
27+
// "+ 0" turns -0 into 0, so a spot reads the same before and after the world is stored as JSON.
28+
const round2 = (v: number): number => Math.round(v * 100) / 100 + 0
1929
const has = (o: object, k: string): boolean => Object.prototype.hasOwnProperty.call(o, k)
2030

2131
/** Ids of the buildings a previous compile overlaid from a linked design: their positions are kept. */
@@ -39,12 +49,17 @@ function plannedBefore(previous: WorldDoc | null): Set<string> {
3949
* building off its slab, and a sibling the layout put on the spot never shares it. Only the previous
4050
* proposal of the same design (`key`) lends spots.
4151
* A part whose wireframe stood on the ring usually lands in a district new this compile (its folder had none),
42-
* which the layout put on a free slot elsewhere. When that district holds only the part, the district moves so
43-
* the part stands on the wireframe's spot, its slab under it, provided the slab stays clear of every other
44-
* district (SLOT_CLEARANCE, and the two radii plus MIN_SIBLING_GAP). Later compiles keep it there: the layout
45-
* keeps a district's place. A district that stood before never moves.
52+
* which the layout put on a free slot elsewhere. When that district holds only the part, the district moves to
53+
* the wireframe's spot, centred under the part, provided it stays clear of every other district (slabRoom) and
54+
* of every wireframe still waiting (`wires`, WIRE_ROOM). The ring lies at least slabRoom past every district, so
55+
* only another wireframe, or a district new this compile, can refuse it. Several such moves in one compile are
56+
* settled together: each is checked against the others' new spots, and while one fails, the first that fails (in
57+
* the order of `matched`) stays where the layout put it and the rest are checked again. Later compiles keep the
58+
* district there: the layout keeps a district's place. A district that stood before never moves.
4659
*/
47-
function holdPlannedSpots(doc: WorldDoc, key: string, matched: Record<string, string>, previous: WorldDoc | null, positions: Record<string, Pos>): void {
60+
function holdPlannedSpots(
61+
doc: WorldDoc, key: string, matched: Record<string, string>, previous: WorldDoc | null, positions: Record<string, Pos>, wires: Pos[],
62+
): void {
4863
const link = previous?.layers.plan.find((p) => p.reconcile !== undefined && p.id === key)
4964
const was = link?.reconcile
5065
const prevPos = previous ? previous.layout.positions : {}
@@ -63,17 +78,10 @@ function holdPlannedSpots(doc: WorldDoc, key: string, matched: Record<string, st
6378
const others = members.filter((b) => b.id !== bid).map((b): [number, number] => [positions[b.id][0], positions[b.id][2]])
6479
return Math.hypot(p[0] - c[0], p[2] - c[2]) <= districtRadius(members.length) + MOVED_TOLERANCE && clear([p[0], p[2]], others)
6580
}
66-
const size = (d: string): number => doc.buildings.filter((b) => b.districtId === d).length
67-
const moveDistrict = (bid: string, at: Pos): void => {
81+
const movers: { bid: string, d: string, at: Pos }[] = []
82+
const canMove = (bid: string): boolean => {
6883
const d = districtOf.get(bid) as string
69-
const c = positions[d]
70-
if (c === undefined || stoodBefore.has(d) || size(d) !== 1) return
71-
const b = positions[bid]
72-
const to: Pos = [round2(c[0] + at[0] - b[0]), 0, round2(c[2] + at[2] - b[2])]
73-
const room = (o: string): number => Math.max(SLOT_CLEARANCE, districtRadius(size(o)) + districtRadius(1) + MIN_SIBLING_GAP)
74-
if (!doc.districts.every((o) => o.id === d || Math.hypot(positions[o.id][0] - to[0], positions[o.id][2] - to[2]) >= room(o.id))) return
75-
positions[d] = to
76-
positions[bid] = at
84+
return positions[d] !== undefined && !stoodBefore.has(d) && sizeOf(doc, d) === 1
7785
}
7886
for (const [pid, bid] of Object.entries(matched)) {
7987
const at = wireAt.get(pid)
@@ -84,7 +92,22 @@ function holdPlannedSpots(doc: WorldDoc, key: string, matched: Record<string, st
8492
const fresh = prevDistrict.has(bid) ? undefined : at
8593
const spot = fresh ?? ((before === bid || !districtOf.has(before)) && prevDistrict.get(before) === districtOf.get(bid) ? held : undefined)
8694
if (spot && fits(bid, spot)) positions[bid] = spot
87-
else if (fresh) moveDistrict(bid, fresh)
95+
else if (fresh && canMove(bid)) movers.push({ bid, d: districtOf.get(bid) as string, at: fresh })
96+
}
97+
const apart = (a: Pos, b: Pos, room: number): boolean => Math.hypot(a[0] - b[0], a[2] - b[2]) >= room
98+
let going = movers
99+
for (;;) {
100+
const spotOf = (o: string): Pos => going.find((m) => m.d === o)?.at ?? positions[o]
101+
const roomy = (m: { d: string, at: Pos }): boolean =>
102+
doc.districts.every((o) => o.id === m.d || apart(spotOf(o.id), m.at, slabRoom(sizeOf(doc, o.id)))) &&
103+
wires.every((w) => apart(w, m.at, WIRE_ROOM))
104+
const stays = going.find((m) => !roomy(m))
105+
if (stays === undefined) break
106+
going = going.filter((m) => m !== stays)
107+
}
108+
for (const m of going) {
109+
positions[m.d] = m.at
110+
positions[m.bid] = m.at
88111
}
89112
}
90113

@@ -110,36 +133,42 @@ export function overlayPlan(doc: WorldDoc, plan: WorldDoc, previous: WorldDoc |
110133
}
111134

112135
const positions: Record<string, Pos> = { ...doc.layout.positions }
113-
holdPlannedSpots(doc, plan.projectKey, rec.matched, previous, positions)
114-
const glossary = { ...doc.glossary }
115-
const cargo = { ...doc.cargo }
116136
const kept = plannedBefore(previous)
117-
const buildings: Building[] = []
118-
const outside: string[] = []
119-
for (const pid of rec.missing) {
137+
const missing = rec.missing.map((pid) => {
120138
const pb = planById.get(pid) as Building
121139
const id = idFor.get(pid) as string
122-
const districtId = districtFor.get(pb.districtId) ?? ''
140+
const before = previous?.layout.positions[id]
141+
return { pb, id, districtId: districtFor.get(pb.districtId) ?? '', before: before && kept.has(id) ? before : undefined }
142+
})
143+
// The ring is set from the districts as laid out, before any district moves under a built part, so every
144+
// wireframe of this compile shares one ring, and it lies at least a slab's room past every district.
145+
const far = doc.districts.reduce((m, d) => Math.max(m, Math.hypot(positions[d.id][0], positions[d.id][2])), 0)
146+
const ring = far + Math.max(PLAN_RING_GAP, ...doc.districts.map((d) => slabRoom(sizeOf(doc, d.id))))
147+
const outside = missing.filter((m) => m.before === undefined && m.districtId === '')
148+
outside.forEach((m, i) => {
149+
const angle = (2 * Math.PI * (i + 0.5)) / outside.length
150+
positions[m.id] = [round2(ring * Math.cos(angle)), 0, round2(ring * Math.sin(angle))]
151+
})
152+
for (const m of missing) if (m.before) positions[m.id] = m.before
153+
// Wireframes placed so far (kept ones and the ring) stay unbuilt this compile: a moved district keeps off them.
154+
const wires = missing.filter((m) => positions[m.id] !== undefined).map((m) => positions[m.id])
155+
holdPlannedSpots(doc, plan.projectKey, rec.matched, previous, positions, wires)
156+
const glossary = { ...doc.glossary }
157+
const cargo = { ...doc.cargo }
158+
const buildings: Building[] = []
159+
for (const { pb, id, districtId, before } of missing) {
123160
buildings.push({ ...pb, id, districtId, process: '', status: 'proposed' })
124161
for (const t of [...pb.stack, ...pb.floors.flatMap((f) => f.tech)]) {
125162
if (!has(glossary, t)) glossary[t] = has(plan.glossary, t) ? plan.glossary[t] : ''
126163
}
127-
const before = previous?.layout.positions[id]
128-
if (before && kept.has(id)) positions[id] = before
129-
else if (districtId === '') outside.push(id)
130-
else {
164+
if (before === undefined && districtId !== '') {
131165
const siblings = [...doc.buildings, ...buildings].filter((b) => b.districtId === districtId && b.id !== id && positions[b.id])
132166
const [cx, , cz] = positions[districtId]
133167
const taken = siblings.map((b): [number, number] => [positions[b.id][0], positions[b.id][2]])
134168
const p = placeBuilding(id, doc.layout.seed, cx, cz, districtRadius(siblings.length + 1), taken)
135169
positions[id] = [p[0], 0, p[1]]
136170
}
137171
}
138-
const far = doc.districts.reduce((m, d) => Math.max(m, Math.hypot(positions[d.id][0], positions[d.id][2])), 0)
139-
outside.forEach((id, i) => {
140-
const angle = (2 * Math.PI * (i + 0.5)) / outside.length
141-
positions[id] = [round2((far + PLAN_RING_GAP) * Math.cos(angle)), 0, round2((far + PLAN_RING_GAP) * Math.sin(angle))]
142-
})
143172

144173
const builtLinks = new Set(doc.edges.map((e) => `${e.from}\n${e.to}`))
145174
const edges: WorldEdge[] = []

‎tests/electron/worldPlanOverlay.test.ts‎

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,89 @@ describe('overlayPlan', () => {
283283
const plan = design(ROUND4)
284284
const first = overlayPlan(built(), plan, null)
285285
const cronAt = first.layout.positions.cron
286-
expect(cronAt).toEqual([-70, 0, 0])
286+
// The ring lies a slab's room (SLOT_CLEARANCE, 35) past the farthest district (d2, 50 out).
287+
expect(cronAt).toEqual([-85, 0, 0])
287288
// The code's new "jobs" folder becomes district d3, which the layout put on a free slot at [70, 0, 0].
288289
const fresh = withBuilt(withDistrict(built(), 'd3', [70, 0, 0]), 'jobs-cron', 'Cron', 'd3', [72, 0, 1])
289290
const second = overlayPlan(fresh, plan, first)
290291
expect(second.layers.plan[0].reconcile?.matched).toEqual({ cron: 'jobs-cron' })
291-
// The building rises where the wireframe stood, and its slab moves with it, so it stands on its own slab.
292+
// The building rises where the wireframe stood, and its slab moves there, centred under it.
292293
expect(second.layout.positions['jobs-cron']).toEqual(cronAt)
293-
expect(second.layout.positions.d3).toEqual([-72, 0, -1])
294+
expect(second.layout.positions.d3).toEqual(cronAt)
294295
expect(second.layers.plan[0].buildings.map((b) => b.id)).toEqual(['mail'])
295296
expect(validateWorld(second).ok).toBe(true)
296297
// The next compile keeps the district where it now is (layoutWorld keeps a district's place): nothing moves.
297-
const kept = withBuilt(withDistrict(built(), 'd3', [-72, 0, -1]), 'jobs-cron', 'Cron', 'd3', [-70, 0, 0])
298+
const kept = withBuilt(withDistrict(built(), 'd3', [-85, 0, 0]), 'jobs-cron', 'Cron', 'd3', [-85, 0, 0])
298299
const third = overlayPlan(kept, plan, second)
299-
expect(third.layout.positions.d3).toEqual([-72, 0, -1])
300+
expect(third.layout.positions.d3).toEqual(cronAt)
301+
expect(third.layout.positions['jobs-cron']).toEqual(cronAt)
302+
})
303+
304+
it('a district at the outer edge right beside the wireframe does not stop the part rising in place (round 6b I1)', () => {
305+
const plan = design(ROUND4)
306+
// d5, nine buildings, stands at the outer edge (50 out, like d2) at the very angle the Cron wireframe takes.
307+
const edge = (): WorldDoc => {
308+
const doc = withDistrict(built(), 'd5', [-50, 0, 0])
309+
for (let k = 0; k < 9; k++) withBuilt(doc, `big-${k}`, `Big ${k}`, 'd5', [-50 + 6 * (k % 3) - 6, 0, 6 * Math.floor(k / 3) - 6])
310+
return doc
311+
}
312+
const first = overlayPlan(edge(), plan, null)
313+
const cronAt = first.layout.positions.cron
314+
// The ring sits d5's room out: its slab (5 * 3 + 6), a one-building slab (11) and the sibling gap (6), 38 in all.
315+
expect(cronAt).toEqual([-88, 0, 0])
316+
const second = overlayPlan(withBuilt(withDistrict(edge(), 'd3', [70, 0, 0]), 'jobs-cron', 'Cron', 'd3', [72, 0, 1]), plan, first)
317+
expect(second.layout.positions['jobs-cron']).toEqual(cronAt)
318+
expect(second.layout.positions.d3).toEqual(cronAt)
319+
noStacking(second)
320+
expect(validateWorld(second).ok).toBe(true)
321+
})
322+
323+
it('a moved district keeps off a wireframe still waiting, and the ring stays where it was before any move (round 6b M1, M2)', () => {
324+
const plan = design([...ROUND4, { op: 'addBuilding', id: 'alerts', districtId: 'ops', name: 'Alerts' }])
325+
const first = overlayPlan(built(), plan, null)
326+
const cronAt = first.layout.positions.cron
327+
const fresh = (): WorldDoc => withBuilt(withDistrict(built(), 'd3', [70, 0, 0]), 'jobs-cron', 'Cron', 'd3', [72, 0, 1])
328+
// The Alerts wireframe, still unbuilt, stands 10 from the Cron one (set by hand): a slab there would cover it.
329+
const crowded: WorldDoc = JSON.parse(JSON.stringify(first))
330+
crowded.layout.positions.alerts = [cronAt[0], 0, cronAt[2] + 10]
331+
const held = overlayPlan(fresh(), plan, crowded)
332+
expect(held.layers.plan[0].buildings.map((b) => b.id)).toEqual(['alerts', 'mail'])
333+
expect(held.layout.positions.alerts).toEqual([cronAt[0], 0, cronAt[2] + 10])
334+
expect(held.layout.positions.d3).toEqual([70, 0, 0])
335+
expect(held.layout.positions['jobs-cron']).toEqual([72, 0, 1])
336+
// The design gains a part with no district as Cron is built: it joins the ring as it stood before the move,
337+
// 35 past the farthest district (d3's layout slot, 70 out), not past d3's new spot.
338+
const grown = design([...ROUND4, { op: 'addBuilding', id: 'queue', districtId: 'ops', name: 'Queue' }])
339+
const second = overlayPlan(fresh(), grown, first)
340+
expect(second.layout.positions.d3).toEqual(cronAt)
341+
expect(second.layout.positions.queue).toEqual([-105, 0, 0])
342+
})
343+
344+
it('two ring wireframes built in one compile both rise in place, each checked against the other\'s new spot (round 6b M3)', () => {
345+
const TWO: DesignOp[] = [...ROUND4, { op: 'addBuilding', id: 'alerts', districtId: 'ops', name: 'Alerts' }]
346+
const plan = design(TWO)
347+
const first = overlayPlan(built(), plan, null)
348+
const [alertsAt, cronAt] = [first.layout.positions.alerts, first.layout.positions.cron]
349+
// The layout put each new district right beside the OTHER part's wireframe: checked against the other's old
350+
// slot, neither could move; checked against its new spot, both do.
351+
const code = (a: [number, number, number], c: [number, number, number]): WorldDoc => withBuilt(withBuilt(
352+
withDistrict(withDistrict(built(), 'd3', c), 'd4', a), 'jobs-cron', 'Cron', 'd3', c), 'jobs-alerts', 'Alerts', 'd4', a)
353+
const beside = code([cronAt[0], 0, cronAt[2] - 10], [alertsAt[0], 0, alertsAt[2] + 10])
354+
const second = overlayPlan(beside, plan, first)
355+
expect(second.layers.plan[0].reconcile?.matched).toEqual({ alerts: 'jobs-alerts', cron: 'jobs-cron' })
356+
expect(second.layout.positions.d3).toEqual(cronAt)
357+
expect(second.layout.positions['jobs-cron']).toEqual(cronAt)
358+
expect(second.layout.positions.d4).toEqual(alertsAt)
359+
expect(second.layout.positions['jobs-alerts']).toEqual(alertsAt)
360+
expect(overlayPlan(beside, plan, first)).toEqual(second)
361+
// Two wireframes 20 apart (set by hand) cannot both hold a slab: the first in the design's order (Alerts)
362+
// stays on its layout slot, and Cron, checked again against that slot, rises in place.
363+
const close: WorldDoc = JSON.parse(JSON.stringify(first))
364+
close.layout.positions.alerts = [cronAt[0] + 20, 0, cronAt[2]]
365+
const third = overlayPlan(code([0, 0, 70], [70, 0, 0]), plan, close)
366+
expect(third.layout.positions.d4).toEqual([0, 0, 70])
367+
expect(third.layout.positions['jobs-alerts']).toEqual([0, 0, 70])
368+
expect(third.layout.positions.d3).toEqual(cronAt)
300369
expect(third.layout.positions['jobs-cron']).toEqual(cronAt)
301370
})
302371

@@ -309,7 +378,8 @@ describe('overlayPlan', () => {
309378
expect(a.layout.positions.d3).toEqual([70, 0, 0])
310379
expect(a.layout.positions['jobs-cron']).toEqual([72, 0, 1])
311380
// Another district near the wireframe's spot: the slab would crowd it, so it stays put too.
312-
const near = withDistrict(withBuilt(withDistrict(built(), 'd3', [70, 0, 0]), 'jobs-cron', 'Cron', 'd3', [72, 0, 1]), 'd4', [-80, 0, 10])
381+
// (A district new this compile; one that stood before lies inside the ring, a slab's room in.)
382+
const near = withDistrict(withBuilt(withDistrict(built(), 'd3', [70, 0, 0]), 'jobs-cron', 'Cron', 'd3', [72, 0, 1]), 'd4', [-95, 0, 10])
313383
const b = overlayPlan(near, plan, first)
314384
expect(b.layout.positions.d3).toEqual([70, 0, 0])
315385
expect(b.layout.positions['jobs-cron']).toEqual([72, 0, 1])

0 commit comments

Comments
 (0)