From ad8e14504b57a7aa34dc38f64eb5cb235d5b0c8f Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Mon, 26 Oct 2026 20:45:51 -0400 Subject: [PATCH 1/4] Classify: a city twenty years old whose founding files still stand is an old walled city Co-Authored-By: Claude Opus 5.5 --- src/classify.mjs | 4 ++++ test/snapshots/SNKRX.json | 1 + test/snapshots/You-Dont-Know-JS.json | 1 + test/snapshots/axum.json | 1 + test/snapshots/flask.json | 1 + test/snapshots/gallery.json | 1 + test/snapshots/gson.json | 1 + test/snapshots/jq.json | 1 + test/snapshots/ky.json | 1 + test/snapshots/nanoGPT.json | 1 + test/snapshots/terraform-aws-eks.json | 1 + test/snapshots/uuid.json | 1 + 12 files changed, 15 insertions(+) diff --git a/src/classify.mjs b/src/classify.mjs index 8b65455..0989028 100644 --- a/src/classify.mjs +++ b/src/classify.mjs @@ -199,6 +199,8 @@ export function cityMetrics(city, now = Date.now() / 1000) { const recentWindow = 90 * 86400; const recentCommits = commits.filter(c => c[0] > end - recentWindow).length; const avgPerWindow = commitCount / Math.max(1, seen / recentWindow); + // the old walled city: how many of the files still standing were built in the repo's first five years + const founded = city.truncated && city.firstCommit ? city.firstCommit : city.start; return { files: n, @@ -232,6 +234,7 @@ export function cityMetrics(city, now = Date.now() / 1000) { commitsPerWeek: commitCount / Math.max(1, seen / (7 * 86400)), idleYears: history ? Math.max(0, (now - end) / YEAR) : 0, staleShare: F.filter(f => end - f.last > 2 * YEAR).length / Math.max(1, n), + foundingShare: F.filter(f => f.born < founded + 5 * YEAR).length / Math.max(1, n), recentBoost: avgPerWindow && commitCount >= 200 && recentCommits >= 30 ? recentCommits / avgPerWindow : 0, hardware: (city.extras?.hardwareFiles || 0) > 0, infraFiles: infraIdx.length, @@ -353,6 +356,7 @@ const ARCHETYPES = { const STATES = { ruins: m => m.history && m.staleShare > 0.35 && m.ageYears > 4 && `ancient ruins: ${pct(m.staleShare)} of files untouched for 2+ years`, ghost: m => m.history && m.idleYears > 1.5 && `ghost town: no commits for ${m.idleYears.toFixed(1)} years`, + walled: m => m.history && m.ageYears >= 20 && m.foundingShare >= 0.2 && `old walled city: ${pct(m.foundingShare)} of files date from its first five years, ${Math.floor(m.ageYears)} years ago`, boom: m => m.history && m.recentBoost > 2.5 && `boomtown: ${m.recentBoost.toFixed(1)}× the usual commit rate lately`, garden: m => m.testShare > 0.6 && `garden city: ${pct(m.testShare)} of the code is tests`, maze: m => m.cycleShare > 0.5 && `old medina: ${pct(m.cycleShare)} of files sit in import cycles`, diff --git a/test/snapshots/SNKRX.json b/test/snapshots/SNKRX.json index 69c0f14..cb9abbd 100644 --- a/test/snapshots/SNKRX.json +++ b/test/snapshots/SNKRX.json @@ -48,6 +48,7 @@ "ageYears": 1.310410614241894, "commitsPerWeek": 1.5210158948424426, "staleShare": 0, + "foundingShare": 1, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/You-Dont-Know-JS.json b/test/snapshots/You-Dont-Know-JS.json index 41138c2..66f677b 100644 --- a/test/snapshots/You-Dont-Know-JS.json +++ b/test/snapshots/You-Dont-Know-JS.json @@ -44,6 +44,7 @@ "ageYears": 12.173959965269855, "commitsPerWeek": 1.5474957466593235, "staleShare": 0.8409090909090909, + "foundingShare": 0.8181818181818182, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/axum.json b/test/snapshots/axum.json index 1cfc499..9293190 100644 --- a/test/snapshots/axum.json +++ b/test/snapshots/axum.json @@ -44,6 +44,7 @@ "ageYears": 4.853932998707126, "commitsPerWeek": 4.540585715188863, "staleShare": 0.07339449541284404, + "foundingShare": 1, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/flask.json b/test/snapshots/flask.json index 244f819..cd70600 100644 --- a/test/snapshots/flask.json +++ b/test/snapshots/flask.json @@ -44,6 +44,7 @@ "ageYears": 15.872805283038, "commitsPerWeek": 1.9523790852940475, "staleShare": 0.28, + "foundingShare": 0.72, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/gallery.json b/test/snapshots/gallery.json index c3e4a83..6bedb1f 100644 --- a/test/snapshots/gallery.json +++ b/test/snapshots/gallery.json @@ -51,6 +51,7 @@ "ageYears": 4.083967665475194, "commitsPerWeek": 1.7738517481402771, "staleShare": 0.07407407407407407, + "foundingShare": 1, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/gson.json b/test/snapshots/gson.json index 93fd827..9cc0370 100644 --- a/test/snapshots/gson.json +++ b/test/snapshots/gson.json @@ -44,6 +44,7 @@ "ageYears": 17.625486126955156, "commitsPerWeek": 1.285239865134112, "staleShare": 0.2727272727272727, + "foundingShare": 0.5950413223140496, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/jq.json b/test/snapshots/jq.json index 0af6af7..84ded65 100644 --- a/test/snapshots/jq.json +++ b/test/snapshots/jq.json @@ -44,6 +44,7 @@ "ageYears": 13.832254480695617, "commitsPerWeek": 1.3702857351105027, "staleShare": 0.37209302325581395, + "foundingShare": 0.9069767441860465, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/ky.json b/test/snapshots/ky.json index 2fba61e..d11d85f 100644 --- a/test/snapshots/ky.json +++ b/test/snapshots/ky.json @@ -44,6 +44,7 @@ "ageYears": 8.094338574543057, "commitsPerWeek": 0.840533064359412, "staleShare": 0.019230769230769232, + "foundingShare": 0.34615384615384615, "recentBoost": 9.25338705587434, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/nanoGPT.json b/test/snapshots/nanoGPT.json index fd200e5..e1c34bc 100644 --- a/test/snapshots/nanoGPT.json +++ b/test/snapshots/nanoGPT.json @@ -44,6 +44,7 @@ "ageYears": 1.9501042221208202, "commitsPerWeek": 1.09086993273011, "staleShare": 0, + "foundingShare": 1, "recentBoost": 0, "hardware": false, "infraFiles": 0, diff --git a/test/snapshots/terraform-aws-eks.json b/test/snapshots/terraform-aws-eks.json index 1d6649b..543b078 100644 --- a/test/snapshots/terraform-aws-eks.json +++ b/test/snapshots/terraform-aws-eks.json @@ -44,6 +44,7 @@ "ageYears": 8.336243884199051, "commitsPerWeek": 1.209269634892521, "staleShare": 0.20454545454545456, + "foundingShare": 0.6590909090909091, "recentBoost": 0, "hardware": false, "infraFiles": 88, diff --git a/test/snapshots/uuid.json b/test/snapshots/uuid.json index e5f7fbf..93ec354 100644 --- a/test/snapshots/uuid.json +++ b/test/snapshots/uuid.json @@ -44,6 +44,7 @@ "ageYears": 12.440389256470707, "commitsPerWeek": 0.14327050590446247, "staleShare": 0.6190476190476191, + "foundingShare": 0.7142857142857143, "recentBoost": 0, "hardware": false, "infraFiles": 0, From c1e0fde6566eb2d530d07e74c34dc49ce1e8e701 Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Mon, 26 Oct 2026 20:56:45 -0400 Subject: [PATCH 2/4] Page: an old walled city keeps its founding quarter behind a wall The walled state gets drawn: the round patch of the city where files from the first five years are thickest, snapped to whole blocks where the plan has small ones, is walled off down the middle of the streets (a nearest-lot distance field, so the wall never cuts a building). Towers along it, four gates at the widest streets, up when the founding years end. Works on every plan; walls.js is pure, the studio builds the meshes. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- web/index.html | 1 + web/main.js | 8 ++- web/studio.js | 25 +++++-- web/walls.js | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+), 9 deletions(-) create mode 100644 web/walls.js diff --git a/README.md b/README.md index d3d1829..784630d 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ The terminal tells you why your repo got its plan: this repo is Tokyo: mostly small buildings (median 99 lines); 3.3 imports per file, so everything connects ``` -Some repos also get a condition on top of their plan. A repo with no recent commits is a ghost town. One where most files haven't been touched in years is ancient ruins. A sudden rush of commits makes a boomtown, and a repo that's mostly tests is a garden city. +Some repos also get a condition on top of their plan. A repo with no recent commits is a ghost town. One where most files haven't been touched in years is ancient ruins. A sudden rush of commits makes a boomtown, and a repo that's mostly tests is a garden city. A repo twenty years old whose first five years still stand is an old walled city: a wall with towers and gates goes round the quarter where those founding files cluster. ## How to read the city diff --git a/web/index.html b/web/index.html index aaa00f9..357b54e 100644 --- a/web/index.html +++ b/web/index.html @@ -242,6 +242,7 @@ + diff --git a/web/main.js b/web/main.js index ead1f08..109e791 100644 --- a/web/main.js +++ b/web/main.js @@ -8,6 +8,7 @@ import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; import { hierarchy, treemap, treemapSquarify } from 'd3-hierarchy'; import { buildStudio } from './studio.js'; import { buildLayout, CITY_KINDS } from './layouts.js'; +import { planWall, foundingEnd } from './walls.js'; import { createPixelPipeline } from './pixel.js'; import { rubbleMesh, RUBBLE_VERTEX } from './rubble.js'; import { PERF, HEAP, timed } from './perf.js'; @@ -167,6 +168,11 @@ if (layout) { Object.assign(B[i], { x: l.x, z: l.z, w: l.w * 0.88, d: l.d * 0.88, rot: l.rot, y: base + (l.y || 0), h: Math.max(layout.heights[i], unit * 0.1) }); }); } +// an old walled city (twenty years on, its founding files still standing) keeps its old town behind a +// wall, whatever the plan; it goes up when the founding years end +const wall = layout && data.identity?.states?.some(s => s.id === 'walled') + ? timed('wall', () => { const until = foundingEnd(data), w = planWall({ layout, files, until }); return w && { ...w, born: Math.max(0, t01(until)) }; }) + : null; // ------------------------------------------------------------ renderer // ?record=1&w=1920&h=1080&fps=30&dur=20 → deterministic frame capture driven by the CLI @@ -426,7 +432,7 @@ const heat = new Float32Array(N); const focusAttr = new Float32Array(N).fill(1); const glowAttr = new Float32Array(N); // 1 for the buildings of the district under the pointer const studio = NEON ? null : timed('studio (all)', () => buildStudio({ name: STYLE, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, - shared, heat, focusAttr, glowAttr, inDeg, hub, t01, layout })); + shared, heat, focusAttr, glowAttr, inDeg, hub, t01, layout, wall })); if (studio) { document.body.classList.add('studio', `style-${STYLE}`); if (studio.style.uiFont) document.body.style.setProperty('--ui-font', studio.style.uiFont); diff --git a/web/studio.js b/web/studio.js index a4736bc..0236598 100644 --- a/web/studio.js +++ b/web/studio.js @@ -19,7 +19,7 @@ export const STYLES = { hemi: { sky: '#f6f3ee', ground: '#a69d90', intensity: 1.25 }, heightScale: 0.42, maxHeight: 4.5, building: '#f1ede6', buildingJitter: 0.035, ground: '#d8d1c5', plate: '#e4ded4', roughness: 0.92, - rail: '#8f877c', water: '#b7c2c3', land: '#ddd6ca', yard: '#c9c4b8', dirt: '#c8b48f', sand: '#e6d8b5', dune: '#d6c49c', cliff: '#cbbfae', plaza: '#ece7de', park: '#c6cab0', lawn: '#d4d4bf', hill: '#d9d1c4', road: '#c9c1b4', + rail: '#8f877c', water: '#b7c2c3', land: '#ddd6ca', yard: '#c9c4b8', dirt: '#c8b48f', sand: '#e6d8b5', dune: '#d6c49c', cliff: '#cbbfae', plaza: '#ece7de', park: '#c6cab0', lawn: '#d4d4bf', hill: '#d9d1c4', road: '#c9c1b4', wall: '#d3cbbd', windows: 0, accent: '#d8452b', edges: { color: '#2a2724', opacity: 0.07 }, toneMapping: THREE.NeutralToneMapping, exposure: 1.15, bloom: null, ink: '#2a2724', inkSoft: 'rgba(42,39,36,.55)', font: "'Instrument Serif', Georgia, serif", labelCase: 'none', @@ -27,7 +27,7 @@ export const STYLES = { plan: { background: '#f1ede4', fog: null, flat: true, building: '#1c1b1a', buildingJitter: 0, ground: '#f1ede4', plate: '#e7e2d7', - rail: '#2f5d7c', water: '#dce3e3', land: '#f1ede4', yard: '#e4e0d6', dirt: '#e0d2b6', sand: '#f1e7d0', dune: '#e6d9bc', cliff: '#ddd5c6', plaza: '#ece8df', park: '#e1e3d0', lawn: '#ebebdf', hill: '#e4dfd4', road: '#ddd7cb', + rail: '#2f5d7c', water: '#dce3e3', land: '#f1ede4', yard: '#e4e0d6', dirt: '#e0d2b6', sand: '#f1e7d0', dune: '#e6d9bc', cliff: '#ddd5c6', plaza: '#ece8df', park: '#e1e3d0', lawn: '#ebebdf', hill: '#e4dfd4', road: '#ddd7cb', wall: '#6b655c', windows: 0, accent: '#d23c2a', edges: { color: '#d23c2a', opacity: 0.22 }, toneMapping: THREE.NoToneMapping, exposure: 1.0, bloom: null, ink: '#1c1b1a', inkSoft: 'rgba(28,27,26,.55)', font: "'Instrument Serif', Georgia, serif", labelCase: 'upper', @@ -37,7 +37,7 @@ export const STYLES = { sun: { color: '#fff4e0', intensity: 2.6, elevation: 50, azimuth: 75 }, hemi: { sky: '#c4e4ee', ground: '#61944a', intensity: 1.0 }, building: '#bdb293', buildingJitter: 0.06, ground: '#61944a', plate: '#cdc8d2', roughness: 1, - rail: '#4a4458', water: '#2f6188', land: '#61944a', yard: '#9c97a8', dirt: '#968a6e', sand: '#dccfa4', dune: '#bdb293', cliff: '#968a6e', plaza: '#e0d7b8', park: '#3d6b36', lawn: '#93bf62', hill: '#3d6b36', road: '#6e6880', + rail: '#4a4458', water: '#2f6188', land: '#61944a', yard: '#9c97a8', dirt: '#968a6e', sand: '#dccfa4', dune: '#bdb293', cliff: '#968a6e', plaza: '#e0d7b8', park: '#3d6b36', lawn: '#93bf62', hill: '#3d6b36', road: '#6e6880', wall: '#b0a58a', windows: 0, accent: '#c0413b', edges: null, toneMapping: THREE.NoToneMapping, exposure: 1.0, bloom: null, ink: '#14121c', inkSoft: 'rgba(20,18,28,.7)', font: "'Pixelify Sans', monospace", uiFont: "'Pixelify Sans', monospace", labelCase: 'none', @@ -48,7 +48,7 @@ export const STYLES = { hemi: { sky: '#56607e', ground: '#17151a', intensity: 1.15 }, heightScale: 0.55, maxHeight: 6, building: '#3d4049', buildingJitter: 0.04, ground: '#14161c', plate: '#24262e', roughness: 0.8, - rail: '#6d6050', water: '#0b1626', land: '#1c1e25', yard: '#25272e', dirt: '#2a241c', sand: '#2a2620', dune: '#221f1a', cliff: '#1d1f25', plaza: '#2d2f37', park: '#18211a', lawn: '#1b211d', hill: '#15161c', road: '#0e0f13', + rail: '#6d6050', water: '#0b1626', land: '#1c1e25', yard: '#25272e', dirt: '#2a241c', sand: '#2a2620', dune: '#221f1a', cliff: '#1d1f25', plaza: '#2d2f37', park: '#18211a', lawn: '#1b211d', hill: '#15161c', road: '#0e0f13', wall: '#3a3833', windows: 1, windowColor: '#ffc27a', accent: '#ffc27a', edges: { color: '#ffc27a', opacity: 0.018 }, hubEdgeBoost: 3, toneMapping: THREE.ACESFilmicToneMapping, exposure: 1.1, bloom: { strength: 0.35, radius: 0.4, threshold: 0.85 }, ink: '#e8e2d6', inkSoft: 'rgba(232,226,214,.55)', font: "'Instrument Serif', Georgia, serif", labelCase: 'none', @@ -80,7 +80,7 @@ function slabEach(list) { return geos.length ? mergeGeometries(geos) : null; } -function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn) { +function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn, wall) { const mat = color => S.flat ? new THREE.MeshBasicMaterial({ color }) : new THREE.MeshStandardMaterial({ color, roughness: 0.95, flatShading: true }); const add = (geo, color, surface) => { @@ -200,6 +200,17 @@ function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn) { mesh.castShadow = mesh.receiveShadow = !S.flat; scene.add(mesh); } + // the old town's wall (walls.js): stone runs along the streets, square towers with red roofs, all + // going up together when the founding years end + if (wall) { + const wh = u * 0.5, ts = u * 0.46, th = wh * 1.8; + const grown = g => { const n = g.attributes.position.count, a = new Float32Array(n * 2); for (let k = 0; k < n; k++) { a[k * 2] = wall.born; a[k * 2 + 1] = landTop; } return g.setAttribute('aGrow', new THREE.BufferAttribute(a, 2)); }; + add(grown(mergeGeometries(wall.runs.map(([x0, x1, z]) => new THREE.BoxGeometry(x1 - x0, wh, wall.cell * 1.02).translate((x0 + x1) / 2, landTop + wh / 2, z)))), S.wall); + if (wall.towers.length) { + add(grown(mergeGeometries(wall.towers.map(([x, z]) => new THREE.BoxGeometry(ts, th, ts).translate(x, landTop + th / 2, z)))), S.wall); + add(grown(mergeGeometries(wall.towers.map(([x, z]) => new THREE.ConeGeometry(ts * 0.78, ts * 0.8, 4).rotateY(Math.PI / 4).translate(x, landTop + th + ts * 0.4, z)))), S.accent); + } + } for (const h of S.flat ? [] : layout.hills) { // seen from straight above, hills are just blobs const mesh = new THREE.Mesh(new THREE.IcosahedronGeometry(1, 2), mat(S.hill)); mesh.scale.set(h.r, h.h, h.r * 0.8); @@ -210,7 +221,7 @@ function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn) { } export function buildStudio(ctx) { - const { name, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, shared, heat, focusAttr, inDeg, hub, t01, layout } = ctx; + const { name, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, shared, heat, focusAttr, inDeg, hub, t01, layout, wall } = ctx; // a desert city stands on sand: the open ground and the lawns between blocks become dunes const S = layout?.sand ? { ...STYLES[name], land: STYLES[name].sand, lawn: STYLES[name].dune } : STYLES[name]; const N = files.length; @@ -277,7 +288,7 @@ export function buildStudio(ctx) { scene.add(ground); // ---- city terrain from a planner: land, raised blocks, parks, plazas, bridges, hills - if (layout) timed('terrain', () => buildTerrain(scene, layout, S, plateStep, shared, street, streets?.blockBorn)); + if (layout) timed('terrain', () => buildTerrain(scene, layout, S, plateStep, shared, street, streets?.blockBorn, wall)); // district names appear with their land // (a district's name goes up with its land, ahead of its first building) // (and not before the ground under the name itself has come up: a district's first file can be far off) diff --git a/web/walls.js b/web/walls.js new file mode 100644 index 0000000..2349584 --- /dev/null +++ b/web/walls.js @@ -0,0 +1,189 @@ +// The old walled city (the classifier's 'walled' state): a repo twenty years old whose first five +// years still stand keeps them behind a city wall. The wall goes round the blocks where those +// founding files cluster, down the middle of the streets between them and the newer town, so it +// never cuts through a building whatever the plan. Towers stand along it, gates open where it +// meets the widest streets, and it goes up when the founding years end. +// +// planWall works on the finished layout and returns plain numbers; the studio turns them into walls. + +const YEAR = 365.25 * 86400; + +// the end of the founding years, by the same founding day the classifier counts from +export function foundingEnd(data) { + const founded = data.truncated && data.firstCommit ? data.firstCommit : data.start; + return founded + 5 * YEAR; +} + +function inside([x, z], poly) { + let c = false; + for (let k = 0, m = poly.length - 1; k < poly.length; m = k++) { + const [xa, za] = poly[k], [xb, zb] = poly[m]; + if ((za > z) !== (zb > z) && x < xa + (z - za) / (zb - za) * (xb - xa)) c = !c; + } + return c; +} + +// → { cell, runs: [[x0, x1, z]] wall pieces (one row of cells each), towers: [[x, z]], gates: [[x, z]] }, or null +export function planWall({ layout, files, until }) { + const u = layout.unit; + const live = [...files.keys()].filter(i => !files[i].gone && layout.lots[i] && layout.lots[i].w > 0.01); + const old = live.map(i => files[i].born < until ? 1 : 0); + const oldAll = old.reduce((s, x) => s + x, 0); + if (oldAll < 6 || live.length < 20) return null; + const pos = live.map(i => [layout.lots[i].x, layout.lots[i].z]); + + // the old town: the round patch of the city where founding buildings are thickest, the one with the + // most of them over what the city's average would put there (a quarter of the city at most) + const floor = oldAll / live.length, least = Math.max(8, Math.ceil(live.length * 0.04)), most = Math.ceil(live.length * 0.25); + const olds = live.map((_, k) => k).filter(k => old[k]); + const step = Math.max(1, Math.floor(olds.length / 200)); + let best = null; + for (let c = 0; c < olds.length; c += step) { + const [cx, cz] = pos[olds[c]]; + const order = live.map((_, k) => [Math.hypot(pos[k][0] - cx, pos[k][1] - cz), k]).sort((a, b) => a[0] - b[0]); + let gain = 0; + for (let m = 0; m < most; m++) { + gain += old[order[m][1]] - floor; + if (m + 1 >= least && (!best || gain > best.gain)) best = { gain, order, m: m + 1 }; + } + } + if (!best || best.gain <= 0) return null; + const inTown = new Uint8Array(live.length); + for (let m = 0; m < best.m; m++) inTown[best.order[m][1]] = 1; + // a block goes in or out whole, so the wall keeps to the streets; only a block too big for that + // (a whole compound of the factory town) is split between its buildings + const blocks = layout.blocks.map(b => { + const xs = b.poly.map(p => p[0]), zs = b.poly.map(p => p[1]); + return { poly: b.poly, box: [Math.min(...xs), Math.min(...zs), Math.max(...xs), Math.max(...zs)], lots: [] }; + }); + live.forEach((_, k) => { + const [x, z] = pos[k]; + blocks.find(b => x >= b.box[0] && x <= b.box[2] && z >= b.box[1] && z <= b.box[3] && inside([x, z], b.poly))?.lots.push(k); + }); + for (const b of blocks) { + if (!b.lots.length || b.lots.length > best.m * 0.3) continue; + const keep = b.lots.filter(k => inTown[k]).length >= b.lots.length / 2 ? 1 : 0; + for (const k of b.lots) inTown[k] = keep; + } + if (live.reduce((s, _, k) => s + (inTown[k] && old[k]), 0) < 6) return null; + + // every patch of ground, labelled with its nearest building's lot (a chamfer distance transform): + // the wall runs where ground that belongs to the old town meets ground that belongs to the rest + let X0 = Infinity, Z0 = Infinity, X1 = -Infinity, Z1 = -Infinity; + const foot = live.map(i => { const l = layout.lots[i]; return { ...l, r: Math.hypot(l.w, l.d) / 2, c: Math.cos(l.rot || 0), s: Math.sin(l.rot || 0) }; }); + for (const l of foot) { X0 = Math.min(X0, l.x - l.r); Z0 = Math.min(Z0, l.z - l.r); X1 = Math.max(X1, l.x + l.r); Z1 = Math.max(Z1, l.z + l.r); } + const reach = u * 0.7; // how far out from its buildings a wall stands when nothing is across the street + X0 -= reach * 2; Z0 -= reach * 2; X1 += reach * 2; Z1 += reach * 2; + const cell = Math.max(u * 0.12, Math.max(X1 - X0, Z1 - Z0) / 1600); + const W = Math.ceil((X1 - X0) / cell), H = Math.ceil((Z1 - Z0) / cell); + const lab = new Int32Array(W * H).fill(-1), dist = new Float32Array(W * H).fill(1e9); + foot.forEach((l, k) => { + const i0 = Math.max(0, Math.floor((l.x - l.r - X0) / cell)), i1 = Math.min(W - 1, Math.ceil((l.x + l.r - X0) / cell)); + const j0 = Math.max(0, Math.floor((l.z - l.r - Z0) / cell)), j1 = Math.min(H - 1, Math.ceil((l.z + l.r - Z0) / cell)); + for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) { + // three.js turns a lot's +x to (cos r, -sin r) and +z to (sin r, cos r) + const dx = X0 + (i + 0.5) * cell - l.x, dz = Z0 + (j + 0.5) * cell - l.z; + if (Math.abs(dx * l.c - dz * l.s) <= l.w / 2 && Math.abs(dx * l.s + dz * l.c) <= l.d / 2) { lab[j * W + i] = k; dist[j * W + i] = 0; } + } + }); + const R2 = Math.SQRT2; + const relax = (p, q, w) => { if (dist[q] + w < dist[p]) { dist[p] = dist[q] + w; lab[p] = lab[q]; } }; + for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) { + const p = j * W + i; + if (i > 0) relax(p, p - 1, 1); + if (j > 0) { relax(p, p - W, 1); if (i > 0) relax(p, p - W - 1, R2); if (i < W - 1) relax(p, p - W + 1, R2); } + } + for (let j = H - 1; j >= 0; j--) for (let i = W - 1; i >= 0; i--) { + const p = j * W + i; + if (i < W - 1) relax(p, p + 1, 1); + if (j < H - 1) { relax(p, p + W, 1); if (i < W - 1) relax(p, p + W + 1, R2); if (i > 0) relax(p, p + W - 1, R2); } + } + const near = reach / cell; + + // the wall: the old town's ground along its edge, two cells thick, out in the street (not on a block) + const own = new Uint8Array(W * H); + for (let p = 0; p < W * H; p++) own[p] = lab[p] >= 0 && inTown[lab[p]] && dist[p] <= near ? 1 : 0; + // one town in one piece: the biggest stretch of its ground (not a few lots across the canal), and + // with no holes in it (a yard in the middle is inside the walls, not walled off) + const pieces = (want) => { + const id = new Int32Array(W * H).fill(-1), size = [], queue = new Int32Array(W * H); + for (let s = 0; s < W * H; s++) { + if (own[s] !== want || id[s] >= 0) continue; + const k = size.length; + let head = 0, tail = 0, n = 0, edge = false; + queue[tail++] = s; id[s] = k; + while (head < tail) { + const p = queue[head++], i = p % W; + n++; + if (i === 0 || i === W - 1 || p < W || p >= W * (H - 1)) edge = true; + for (const q of [i > 0 ? p - 1 : -1, i < W - 1 ? p + 1 : -1, p - W, p + W]) { + if (q >= 0 && q < W * H && own[q] === want && id[q] < 0) { id[q] = k; queue[tail++] = q; } + } + } + size.push({ n, edge }); + } + return { id, size }; + }; + const land = pieces(1); + const main = land.size.reduce((b, s, k) => s.n > land.size[b].n ? k : b, 0); + for (let p = 0; p < W * H; p++) if (own[p] && land.id[p] !== main) own[p] = 0; + const holes = pieces(0); + for (let p = 0; p < W * H; p++) if (!own[p] && !holes.size[holes.id[p]].edge) own[p] = 1; + const edge = (src) => { + const out = new Uint8Array(W * H); + for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) { + const p = j * W + i; + if (src[p] && (i === 0 || j === 0 || i === W - 1 || j === H - 1 || !src[p - 1] || !src[p + 1] || !src[p - W] || !src[p + W])) out[p] = 1; + } + return out; + }; + const e1 = edge(own), inner = own.map((v, p) => v && !e1[p] ? 1 : 0), e2 = edge(inner); + const wall = own.map((v, p) => (e1[p] || e2[p]) && dist[p] >= 1 ? 1 : 0); + const at = p => [X0 + (p % W + 0.5) * cell, Z0 + (Math.floor(p / W) + 0.5) * cell]; + const wallCells = []; + for (let p = 0; p < W * H; p++) if (wall[p]) wallCells.push(p); + if (wallCells.length < 20) return null; + + // gates: one to each side of the old town, where the wall crosses the widest street there + let cxs = 0, czs = 0; + for (const p of wallCells) { const [x, z] = at(p); cxs += x; czs += z; } + const c = [cxs / wallCells.length, czs / wallCells.length]; + const gates = []; + for (let side = 0; side < 4; side++) { + let best = -1; + for (const p of wallCells) { + const [x, z] = at(p), a = Math.atan2(z - c[1], x - c[0]); + const s = ((Math.round(a / (Math.PI / 2)) % 4) + 4) % 4; + if (s !== side || !e1[p] || dist[p] >= near * 0.95) continue; // a street between blocks, not open ground + if (best < 0 || dist[p] > dist[best]) best = p; + } + if (best >= 0) gates.push(at(best)); + } + const gateHalf = u * 0.45; + const gap = p => { const [x, z] = at(p); return gates.some(g => Math.hypot(x - g[0], z - g[1]) < gateHalf); }; + for (const p of wallCells) if (gap(p)) wall[p] = 0; + + // towers: a pair flanking each gate, then one every few lots along the wall + const towers = []; + const free = ([x, z], d) => towers.every(t => Math.hypot(x - t[0], z - t[1]) >= d); + for (const g of gates) { + const ring = wallCells.filter(p => wall[p] && Math.abs(Math.hypot(at(p)[0] - g[0], at(p)[1] - g[1]) - gateHalf - cell) < cell * 1.5).map(at); + if (!ring.length) continue; + const a = ring[0], b = ring.reduce((m, q) => Math.hypot(q[0] - a[0], q[1] - a[1]) > Math.hypot(m[0] - a[0], m[1] - a[1]) ? q : m, a); + towers.push(a); if (b !== a) towers.push(b); + } + for (const p of wallCells) if (wall[p] && e1[p] && dist[p] >= 2 && free(at(p), u * 3)) towers.push(at(p)); + + // the wall as runs of cells along each row, so it's a handful of boxes rather than thousands + const runs = []; + for (let j = 0; j < H; j++) { + for (let i = 0; i < W; i++) { + if (!wall[j * W + i]) continue; + let k = i; + while (k + 1 < W && wall[j * W + k + 1]) k++; + runs.push([X0 + i * cell, X0 + (k + 1) * cell, Z0 + (j + 0.5) * cell]); + i = k; + } + } + return { cell, runs, towers, gates }; +} From e84bb2087eaa11cb5230f3a5c22467c881d81deb Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Mon, 2 Nov 2026 23:29:14 -0500 Subject: [PATCH 3/4] Walled city: a wall for every walled city, clear of every building The wall now stands wherever the classifier says the city is walled, and never on a building: - Deleted buildings stand in its way. The files deleted lately stand for most of the timeline, so they're in the distance field like any other lot: they take no part in choosing the old town, they go on the side of the street their ground was on, and the wall goes down the street past them. Each lot also takes every cell it touches, not only the ones whose middle it covers, and a tower stands only where none of the ground under it touches a lot (towers are wider than the wall). - No old quarter, the whole city. When the founding files are spread evenly (every file founding, say) or the city is too small for a quarter of its own, the old town is the whole city: the wall follows the hull round all its buildings, deleted ones too, so a city on islands is one town, with a gate on each side. The wall stands from the ground up, so where it crosses water it stands in it, not over it. planWall returns null only for a city with no lots. - Long cities keep fine cells. The grid covers only the ground round the old town, and its cells are capped by area (three million), not by the longest side over 1,600, which made them wider than a lot on a long Manhattan or Mountain City of 10,000 files and lost the wall. - Fast on big cities: lots are counted in rings round each candidate centre instead of sorted, blocks and towers are filed on a coarse grid, and wall cells stack into boxes along the columns too. On a 30,000-file city, 120-220 ms for an old quarter and 50-75 ms for the whole city (was 1-1.7 s). - The studio skips an empty wall, as it does other merged meshes, and the pixel wall is #968a6e, palette stone that reads against grass and road. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- web/studio.js | 12 +- web/walls.js | 331 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 250 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index e5494eb..a662286 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ The terminal tells you why your repo got its plan: this repo is Tokyo: mostly small buildings (median 99 lines); 3.3 imports per file, so everything connects ``` -Some repos also get a condition on top of their plan. A repo with no recent commits is a ghost town. One where most files haven't been touched in years is ancient ruins. A sudden rush of commits makes a boomtown, and a repo that's mostly tests is a garden city. A repo twenty years old whose first five years still stand is an old walled city: a wall with towers and gates goes round the quarter where those founding files cluster. +Some repos also get a condition on top of their plan. A repo with no recent commits is a ghost town. One where most files haven't been touched in years is ancient ruins. A sudden rush of commits makes a boomtown, and a repo that's mostly tests is a garden city. A repo twenty years old whose first five years still stand is an old walled city: a wall with towers and gates goes round the quarter where those founding files cluster, or round the whole city when they're spread all through it. ## How to read the city diff --git a/web/studio.js b/web/studio.js index 0236598..e899775 100644 --- a/web/studio.js +++ b/web/studio.js @@ -37,7 +37,7 @@ export const STYLES = { sun: { color: '#fff4e0', intensity: 2.6, elevation: 50, azimuth: 75 }, hemi: { sky: '#c4e4ee', ground: '#61944a', intensity: 1.0 }, building: '#bdb293', buildingJitter: 0.06, ground: '#61944a', plate: '#cdc8d2', roughness: 1, - rail: '#4a4458', water: '#2f6188', land: '#61944a', yard: '#9c97a8', dirt: '#968a6e', sand: '#dccfa4', dune: '#bdb293', cliff: '#968a6e', plaza: '#e0d7b8', park: '#3d6b36', lawn: '#93bf62', hill: '#3d6b36', road: '#6e6880', wall: '#b0a58a', + rail: '#4a4458', water: '#2f6188', land: '#61944a', yard: '#9c97a8', dirt: '#968a6e', sand: '#dccfa4', dune: '#bdb293', cliff: '#968a6e', plaza: '#e0d7b8', park: '#3d6b36', lawn: '#93bf62', hill: '#3d6b36', road: '#6e6880', wall: '#968a6e', windows: 0, accent: '#c0413b', edges: null, toneMapping: THREE.NoToneMapping, exposure: 1.0, bloom: null, ink: '#14121c', inkSoft: 'rgba(20,18,28,.7)', font: "'Pixelify Sans', monospace", uiFont: "'Pixelify Sans', monospace", labelCase: 'none', @@ -201,13 +201,15 @@ function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn, wa scene.add(mesh); } // the old town's wall (walls.js): stone runs along the streets, square towers with red roofs, all - // going up together when the founding years end + // going up together when the founding years end. They stand from the ground up: round a whole city + // on the sea they cross the water between its islands, and stand in it rather than over it if (wall) { const wh = u * 0.5, ts = u * 0.46, th = wh * 1.8; - const grown = g => { const n = g.attributes.position.count, a = new Float32Array(n * 2); for (let k = 0; k < n; k++) { a[k * 2] = wall.born; a[k * 2 + 1] = landTop; } return g.setAttribute('aGrow', new THREE.BufferAttribute(a, 2)); }; - add(grown(mergeGeometries(wall.runs.map(([x0, x1, z]) => new THREE.BoxGeometry(x1 - x0, wh, wall.cell * 1.02).translate((x0 + x1) / 2, landTop + wh / 2, z)))), S.wall); + const grown = g => { const n = g.attributes.position.count, a = new Float32Array(n * 2); for (let k = 0; k < n; k++) { a[k * 2] = wall.born; a[k * 2 + 1] = 0; } return g.setAttribute('aGrow', new THREE.BufferAttribute(a, 2)); }; + if (wall.walls.length) add(grown(mergeGeometries(wall.walls.map(([xa, za, xb, zb, t]) => new THREE.BoxGeometry(Math.hypot(xb - xa, zb - za), landTop + wh, t) + .rotateY(-Math.atan2(zb - za, xb - xa)).translate((xa + xb) / 2, (landTop + wh) / 2, (za + zb) / 2)))), S.wall); if (wall.towers.length) { - add(grown(mergeGeometries(wall.towers.map(([x, z]) => new THREE.BoxGeometry(ts, th, ts).translate(x, landTop + th / 2, z)))), S.wall); + add(grown(mergeGeometries(wall.towers.map(([x, z]) => new THREE.BoxGeometry(ts, landTop + th, ts).translate(x, (landTop + th) / 2, z)))), S.wall); add(grown(mergeGeometries(wall.towers.map(([x, z]) => new THREE.ConeGeometry(ts * 0.78, ts * 0.8, 4).rotateY(Math.PI / 4).translate(x, landTop + th + ts * 0.4, z)))), S.accent); } } diff --git a/web/walls.js b/web/walls.js index 2349584..ee9ac9f 100644 --- a/web/walls.js +++ b/web/walls.js @@ -2,7 +2,9 @@ // years still stand keeps them behind a city wall. The wall goes round the blocks where those // founding files cluster, down the middle of the streets between them and the newer town, so it // never cuts through a building whatever the plan. Towers stand along it, gates open where it -// meets the widest streets, and it goes up when the founding years end. +// meets the widest streets, and it goes up when the founding years end. When the founding files are +// spread evenly through the city there is no old quarter to pick out: the whole city is the old town, +// and the wall goes round its edge. // // planWall works on the finished layout and returns plain numbers; the studio turns them into walls. @@ -23,114 +25,257 @@ function inside([x, z], poly) { return c; } -// → { cell, runs: [[x0, x1, z]] wall pieces (one row of cells each), towers: [[x, z]], gates: [[x, z]] }, or null -export function planWall({ layout, files, until }) { - const u = layout.unit; - const live = [...files.keys()].filter(i => !files[i].gone && layout.lots[i] && layout.lots[i].w > 0.01); - const old = live.map(i => files[i].born < until ? 1 : 0); - const oldAll = old.reduce((s, x) => s + x, 0); - if (oldAll < 6 || live.length < 20) return null; - const pos = live.map(i => [layout.lots[i].x, layout.lots[i].z]); +// convex hull of points [x, z] (monotone chain), in order round it +function hull(pts) { + pts.sort((a, b) => a[0] - b[0] || a[1] - b[1]); + const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); + const lo = [], hi = []; + for (const p of pts) { while (lo.length >= 2 && cross(lo[lo.length - 2], lo[lo.length - 1], p) <= 0) lo.pop(); lo.push(p); } + for (let k = pts.length - 1; k >= 0; k--) { const p = pts[k]; while (hi.length >= 2 && cross(hi[hi.length - 2], hi[hi.length - 1], p) <= 0) hi.pop(); hi.push(p); } + return lo.slice(0, -1).concat(hi.slice(0, -1)); +} - // the old town: the round patch of the city where founding buildings are thickest, the one with the - // most of them over what the city's average would put there (a quarter of the city at most) - const floor = oldAll / live.length, least = Math.max(8, Math.ceil(live.length * 0.04)), most = Math.ceil(live.length * 0.25); - const olds = live.map((_, k) => k).filter(k => old[k]); - const step = Math.max(1, Math.floor(olds.length / 200)); +// the old town: the round patch of the city where founding buildings are thickest, the one with the +// most of them over what the city's average would put there (a quarter of the city at most); null +// when there is none (founding files spread evenly, or too few files for a quarter of their own) +function oldTown(layout, xs, zs, old) { + const u = layout.unit, n = xs.length; + let oldAll = 0; + for (const o of old) oldAll += o; + const floor = oldAll / n, least = Math.max(8, Math.ceil(n * 0.04)), most = Math.ceil(n * 0.25); + if (oldAll < 6 || least > most) return null; + const olds = []; + for (let k = 0; k < n; k++) if (old[k]) olds.push(k); + let X0 = Infinity, Z0 = Infinity, X1 = -Infinity, Z1 = -Infinity; + for (let k = 0; k < n; k++) { X0 = Math.min(X0, xs[k]); Z0 = Math.min(Z0, zs[k]); X1 = Math.max(X1, xs[k]); Z1 = Math.max(Z1, zs[k]); } + // the lots round each centre are counted in thin rings out from it rather than sorted by distance + // (a sort per centre took a second on a city of 30,000 files) + const ring = u * 0.1, R = Math.ceil(Math.hypot(X1 - X0, Z1 - Z0) / ring) + 1; + const ringOf = (k, cx, cz) => Math.floor(Math.sqrt((xs[k] - cx) ** 2 + (zs[k] - cz) ** 2) / ring); + const count = new Int32Array(R), founding = new Int32Array(R); let best = null; - for (let c = 0; c < olds.length; c += step) { - const [cx, cz] = pos[olds[c]]; - const order = live.map((_, k) => [Math.hypot(pos[k][0] - cx, pos[k][1] - cz), k]).sort((a, b) => a[0] - b[0]); - let gain = 0; - for (let m = 0; m < most; m++) { - gain += old[order[m][1]] - floor; - if (m + 1 >= least && (!best || gain > best.gain)) best = { gain, order, m: m + 1 }; + for (let c = 0; c < olds.length; c += Math.ceil(olds.length / 200)) { + const cx = xs[olds[c]], cz = zs[olds[c]]; + count.fill(0); founding.fill(0); + for (let k = 0; k < n; k++) { const r = ringOf(k, cx, cz); count[r]++; founding[r] += old[k]; } + for (let r = 0, m = 0, gain = 0; r < R; r++) { + m += count[r]; gain += founding[r] - floor * count[r]; + if (m > most) break; + if (m >= least && (!best || gain > best.gain)) best = { gain, cx, cz, r, m }; } } if (!best || best.gain <= 0) return null; - const inTown = new Uint8Array(live.length); - for (let m = 0; m < best.m; m++) inTown[best.order[m][1]] = 1; + const town = new Uint8Array(n); + for (let k = 0; k < n; k++) if (ringOf(k, best.cx, best.cz) <= best.r) town[k] = 1; // a block goes in or out whole, so the wall keeps to the streets; only a block too big for that - // (a whole compound of the factory town) is split between its buildings - const blocks = layout.blocks.map(b => { - const xs = b.poly.map(p => p[0]), zs = b.poly.map(p => p[1]); - return { poly: b.poly, box: [Math.min(...xs), Math.min(...zs), Math.max(...xs), Math.max(...zs)], lots: [] }; - }); - live.forEach((_, k) => { - const [x, z] = pos[k]; - blocks.find(b => x >= b.box[0] && x <= b.box[2] && z >= b.box[1] && z <= b.box[3] && inside([x, z], b.poly))?.lots.push(k); + // (a whole compound of the factory town) is split between its buildings. Blocks are filed under the + // squares of a coarse grid they cover, so a lot only tries the few around it + const g = u * 4, GW = Math.floor((X1 - X0) / g) + 1, GH = Math.floor((Z1 - Z0) / g) + 1; + const filed = Array.from({ length: GW * GH }, () => []); + const blocks = layout.blocks.map((b, id) => { + const bx = b.poly.map(p => p[0]), bz = b.poly.map(p => p[1]); + const box = [Math.min(...bx), Math.min(...bz), Math.max(...bx), Math.max(...bz)]; + for (let j = Math.max(0, Math.floor((box[1] - Z0) / g)); j <= Math.min(GH - 1, Math.floor((box[3] - Z0) / g)); j++) + for (let i = Math.max(0, Math.floor((box[0] - X0) / g)); i <= Math.min(GW - 1, Math.floor((box[2] - X0) / g)); i++) filed[j * GW + i].push(id); + return { poly: b.poly, box, lots: [] }; }); + for (let k = 0; k < n; k++) { + const x = xs[k], z = zs[k]; + const id = filed[Math.floor((z - Z0) / g) * GW + Math.floor((x - X0) / g)].find(id => { + const b = blocks[id]; + return x >= b.box[0] && x <= b.box[2] && z >= b.box[1] && z <= b.box[3] && inside([x, z], b.poly); + }); + if (id !== undefined) blocks[id].lots.push(k); + } for (const b of blocks) { if (!b.lots.length || b.lots.length > best.m * 0.3) continue; - const keep = b.lots.filter(k => inTown[k]).length >= b.lots.length / 2 ? 1 : 0; - for (const k of b.lots) inTown[k] = keep; + const keep = b.lots.filter(k => town[k]).length >= b.lots.length / 2 ? 1 : 0; + for (const k of b.lots) town[k] = keep; } - if (live.reduce((s, _, k) => s + (inTown[k] && old[k]), 0) < 6) return null; + let kept = 0; + for (let k = 0; k < n; k++) kept += town[k] & old[k]; + return kept >= 6 ? town : null; +} - // every patch of ground, labelled with its nearest building's lot (a chamfer distance transform): - // the wall runs where ground that belongs to the old town meets ground that belongs to the rest - let X0 = Infinity, Z0 = Infinity, X1 = -Infinity, Z1 = -Infinity; - const foot = live.map(i => { const l = layout.lots[i]; return { ...l, r: Math.hypot(l.w, l.d) / 2, c: Math.cos(l.rot || 0), s: Math.sin(l.rot || 0) }; }); - for (const l of foot) { X0 = Math.min(X0, l.x - l.r); Z0 = Math.min(Z0, l.z - l.r); X1 = Math.max(X1, l.x + l.r); Z1 = Math.max(Z1, l.z + l.r); } +// → { walls: [[xa, za, xb, zb, thickness]] straight pieces of wall, towers: [[x, z]], gates: [[x, z]] }, +// or null for a city with no lots at all +export function planWall({ layout, files, until }) { + const lot = i => layout.lots[i] && layout.lots[i].w > 0.01; + const live = [...files.keys()].filter(i => !files[i].gone && lot(i)); + // the files deleted lately stand for most of the timeline: they have no say in where the old town + // is, but the wall goes round them like any other building + const gone = [...files.keys()].filter(i => files[i].gone && lot(i)); + if (!live.length) return null; + const foot = [...live, ...gone].map(i => { const l = layout.lots[i]; return { ...l, r: Math.hypot(l.w, l.d) / 2, c: Math.cos(l.rot || 0), s: Math.sin(l.rot || 0) }; }); + const here = foot.slice(0, live.length); + const town = oldTown(layout, here.map(l => l.x), here.map(l => l.z), live.map(i => files[i].born < until ? 1 : 0)); + // with no old quarter to pick out, or none big enough to wall, the old town is the whole city + return town && quarterWall(layout, foot, live.length, town) || cityWall(layout, foot); +} + +// half a gate's width, and half a tower's (studio.js: 0.46 wide, the eaves of its roof out to 0.26), in lots +const GATE = 0.45, TOWER = 0.26; + +// towers: a pair flanking each gate, then one every few lots along the wall, where `clear` lets them +// (filed by squares three lots wide, so a long wall doesn't check each spot against every tower) +function placeTowers(u, along, gates, clear) { + const towers = [], D = u * 3, filed = new Map(); + const key = (x, z) => Math.floor(x / D) * 65536 + Math.floor(z / D); + const put = t => { towers.push(t); const k = key(t[0], t[1]); if (!filed.has(k)) filed.set(k, []); filed.get(k).push(t); }; + const free = ([x, z]) => { + for (let a = -1; a <= 1; a++) for (let b = -1; b <= 1; b++) + for (const t of filed.get(key(x + a * D, z + b * D)) || []) if (Math.hypot(x - t[0], z - t[1]) < D) return false; + return true; + }; + for (const g of gates) { + const ring = along.filter(p => Math.abs(Math.hypot(p[0] - g[0], p[1] - g[1]) - u * (GATE + 0.25)) < u * 0.2 && clear(p)); + if (!ring.length) continue; + const a = ring[0], b = ring.reduce((m, q) => Math.hypot(q[0] - a[0], q[1] - a[1]) > Math.hypot(m[0] - a[0], m[1] - a[1]) ? q : m, a); + put(a); if (b !== a) put(b); + } + for (const p of along) if (free(p) && clear(p)) put(p); + return towers; +} + +// the whole city as the old town: the wall follows its outline, the hull round all its buildings +// (deleted ones too) out as far as a wall stands from them, so it's one town even where it's scattered +// over islands and open ground, and it never comes near a building +function cityWall(layout, foot) { + const u = layout.unit, reach = u * 0.7, thick = u * 0.24; + const corners = foot.flatMap(l => [[-1, -1], [1, -1], [1, 1], [-1, 1]].map(([a, b]) => + // three.js turns a lot's +x to (cos r, -sin r) and +z to (sin r, cos r) + [l.x + (l.c * a * l.w + l.s * b * l.d) / 2, l.z + (-l.s * a * l.w + l.c * b * l.d) / 2])); + const out = hull(hull(corners).flatMap(([x, z]) => Array.from({ length: 8 }, (_, k) => [x + Math.cos(k * Math.PI / 4) * reach, z + Math.sin(k * Math.PI / 4) * reach]))); + const edges = out.map((a, k) => [a, out[(k + 1) % out.length]]); + // gates: one to each side, straight out from the middle of the city + let cx = 0, cz = 0; + for (const [x, z] of out) { cx += x / out.length; cz += z / out.length; } + const gates = []; + for (let side = 0; side < 4; side++) { + const dx = Math.round(Math.cos(side * Math.PI / 2)), dz = Math.round(Math.sin(side * Math.PI / 2)); + for (const [[xa, za], [xb, zb]] of edges) { + const ex = xb - xa, ez = zb - za, den = dx * ez - dz * ex; + if (!den) continue; + const t = ((xa - cx) * ez - (za - cz) * ex) / den, s = ((xa - cx) * dz - (za - cz) * dx) / den; + if (t > 0 && s >= 0 && s <= 1) { gates.push([cx + dx * t, cz + dz * t]); break; } + } + } + // each edge, less the gaps for the gates; a piece runs on by half its thickness past a corner, so + // the pieces meet there + const walls = [], along = []; + for (const [[xa, za], [xb, zb]] of edges) { + const L = Math.hypot(xb - xa, zb - za); + if (L < 1e-9) continue; + const ux = (xb - xa) / L, uz = (zb - za) / L; + const cuts = []; + for (const [gx, gz] of gates) { // where this edge passes within half a gate's width of a gate + const m = (gx - xa) * ux + (gz - za) * uz, h2 = (u * GATE) ** 2 - ((gx - xa) * uz - (gz - za) * ux) ** 2; + if (h2 > 0) cuts.push([m - Math.sqrt(h2), m + Math.sqrt(h2)]); + } + cuts.sort((a, b) => a[0] - b[0]); + let from = -thick / 2; + for (const [c0, c1] of [...cuts, [Infinity, Infinity]]) { + const to = Math.min(c0, L + thick / 2); + if (to > from) walls.push([xa + ux * from, za + uz * from, xa + ux * to, za + uz * to, thick]); + from = Math.max(from, c1); + if (from >= L + thick / 2) break; + } + for (let d = 0; d < L; d += u * 0.1) along.push([xa + ux * d, za + uz * d]); + } + const towers = placeTowers(u, along, gates, ([x, z]) => gates.every(g => Math.hypot(x - g[0], z - g[1]) >= u * GATE)); + return { walls, towers, gates }; +} + +// a quarter of the city as the old town: every patch of ground labelled with its nearest building's +// lot (a chamfer distance transform), and the wall run where ground that belongs to the old town meets +// ground that belongs to the rest; null when there's too little of it to wall +function quarterWall(layout, foot, live, town) { + const u = layout.unit; const reach = u * 0.7; // how far out from its buildings a wall stands when nothing is across the street + // only the ground round the old town is needed, with room for the lots across its streets + let X0 = Infinity, Z0 = Infinity, X1 = -Infinity, Z1 = -Infinity; + for (let k = 0; k < live; k++) if (town[k]) { const l = foot[k]; X0 = Math.min(X0, l.x - l.r); Z0 = Math.min(Z0, l.z - l.r); X1 = Math.max(X1, l.x + l.r); Z1 = Math.max(Z1, l.z + l.r); } X0 -= reach * 2; Z0 -= reach * 2; X1 += reach * 2; Z1 += reach * 2; - const cell = Math.max(u * 0.12, Math.max(X1 - X0, Z1 - Z0) / 1600); - const W = Math.ceil((X1 - X0) / cell), H = Math.ceil((Z1 - Z0) / cell); - const lab = new Int32Array(W * H).fill(-1), dist = new Float32Array(W * H).fill(1e9); - foot.forEach((l, k) => { - const i0 = Math.max(0, Math.floor((l.x - l.r - X0) / cell)), i1 = Math.min(W - 1, Math.ceil((l.x + l.r - X0) / cell)); - const j0 = Math.max(0, Math.floor((l.z - l.r - Z0) / cell)), j1 = Math.min(H - 1, Math.ceil((l.z + l.r - Z0) / cell)); + // cells an eighth of a lot, or bigger on a big town to keep to three million of them: counted by + // area, so a long thin one gets cells as fine as a square one of the same size + const cell = Math.max(u * 0.12, Math.sqrt((X1 - X0) * (Z1 - Z0) / 3e6)); + const W = Math.ceil((X1 - X0) / cell), H = Math.ceil((Z1 - Z0) / cell), N = W * H; + const lab = new Int32Array(N).fill(-1), dist = new Float32Array(N).fill(1e9); + // a lot takes every cell it touches, not only the ones whose middle it covers (half a cell more + // along each of its sides, a little over for the wall's overlap), so no wall cell is on a building + const seed = k => { + const l = foot[k], pad = cell * 0.52 * (Math.abs(l.c) + Math.abs(l.s)), r = l.r + cell * 1.5; + const i0 = Math.max(0, Math.floor((l.x - r - X0) / cell)), i1 = Math.min(W - 1, Math.ceil((l.x + r - X0) / cell)); + const j0 = Math.max(0, Math.floor((l.z - r - Z0) / cell)), j1 = Math.min(H - 1, Math.ceil((l.z + r - Z0) / cell)); for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) { // three.js turns a lot's +x to (cos r, -sin r) and +z to (sin r, cos r) const dx = X0 + (i + 0.5) * cell - l.x, dz = Z0 + (j + 0.5) * cell - l.z; - if (Math.abs(dx * l.c - dz * l.s) <= l.w / 2 && Math.abs(dx * l.s + dz * l.c) <= l.d / 2) { lab[j * W + i] = k; dist[j * W + i] = 0; } + if (Math.abs(dx * l.c - dz * l.s) <= l.w / 2 + pad && Math.abs(dx * l.s + dz * l.c) <= l.d / 2 + pad) { lab[j * W + i] = k; dist[j * W + i] = 0; } } - }); + }; const R2 = Math.SQRT2; const relax = (p, q, w) => { if (dist[q] + w < dist[p]) { dist[p] = dist[q] + w; lab[p] = lab[q]; } }; - for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) { - const p = j * W + i; - if (i > 0) relax(p, p - 1, 1); - if (j > 0) { relax(p, p - W, 1); if (i > 0) relax(p, p - W - 1, R2); if (i < W - 1) relax(p, p - W + 1, R2); } - } - for (let j = H - 1; j >= 0; j--) for (let i = W - 1; i >= 0; i--) { - const p = j * W + i; - if (i < W - 1) relax(p, p + 1, 1); - if (j < H - 1) { relax(p, p + W, 1); if (i < W - 1) relax(p, p + W + 1, R2); if (i > 0) relax(p, p + W - 1, R2); } + const sweep = () => { + for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) { + const p = j * W + i; + if (i > 0) relax(p, p - 1, 1); + if (j > 0) { relax(p, p - W, 1); if (i > 0) relax(p, p - W - 1, R2); if (i < W - 1) relax(p, p - W + 1, R2); } + } + for (let j = H - 1; j >= 0; j--) for (let i = W - 1; i >= 0; i--) { + const p = j * W + i; + if (i < W - 1) relax(p, p + 1, 1); + if (j < H - 1) { relax(p, p + W, 1); if (i < W - 1) relax(p, p + W + 1, R2); if (i > 0) relax(p, p + W - 1, R2); } + } + }; + const inTown = new Uint8Array(foot.length); + inTown.set(town); + for (let k = 0; k < live; k++) seed(k); + sweep(); + if (foot.length > live) { + // a deleted building belongs to the side of the street its ground was on without it, then stands + // in the field like the rest, so the wall goes down the street past it, not through it + for (let k = live; k < foot.length; k++) { + const i = Math.floor((foot[k].x - X0) / cell), j = Math.floor((foot[k].z - Z0) / cell); + inTown[k] = i >= 0 && j >= 0 && i < W && j < H && lab[j * W + i] >= 0 ? inTown[lab[j * W + i]] : 0; + } + for (let k = live; k < foot.length; k++) seed(k); + sweep(); } const near = reach / cell; // the wall: the old town's ground along its edge, two cells thick, out in the street (not on a block) - const own = new Uint8Array(W * H); - for (let p = 0; p < W * H; p++) own[p] = lab[p] >= 0 && inTown[lab[p]] && dist[p] <= near ? 1 : 0; + const own = new Uint8Array(N); + for (let p = 0; p < N; p++) own[p] = lab[p] >= 0 && inTown[lab[p]] && dist[p] <= near ? 1 : 0; // one town in one piece: the biggest stretch of its ground (not a few lots across the canal), and // with no holes in it (a yard in the middle is inside the walls, not walled off) + const id = new Int32Array(N), queue = new Int32Array(N); const pieces = (want) => { - const id = new Int32Array(W * H).fill(-1), size = [], queue = new Int32Array(W * H); - for (let s = 0; s < W * H; s++) { + const size = []; + id.fill(-1); + for (let s = 0; s < N; s++) { if (own[s] !== want || id[s] >= 0) continue; const k = size.length; - let head = 0, tail = 0, n = 0, edge = false; + let head = 0, tail = 0, edge = false; queue[tail++] = s; id[s] = k; while (head < tail) { const p = queue[head++], i = p % W; - n++; - if (i === 0 || i === W - 1 || p < W || p >= W * (H - 1)) edge = true; - for (const q of [i > 0 ? p - 1 : -1, i < W - 1 ? p + 1 : -1, p - W, p + W]) { - if (q >= 0 && q < W * H && own[q] === want && id[q] < 0) { id[q] = k; queue[tail++] = q; } - } + if (i === 0 || i === W - 1 || p < W || p >= N - W) edge = true; + if (i > 0 && own[p - 1] === want && id[p - 1] < 0) { id[p - 1] = k; queue[tail++] = p - 1; } + if (i < W - 1 && own[p + 1] === want && id[p + 1] < 0) { id[p + 1] = k; queue[tail++] = p + 1; } + if (p >= W && own[p - W] === want && id[p - W] < 0) { id[p - W] = k; queue[tail++] = p - W; } + if (p < N - W && own[p + W] === want && id[p + W] < 0) { id[p + W] = k; queue[tail++] = p + W; } } - size.push({ n, edge }); + size.push({ n: tail, edge }); } - return { id, size }; + return size; }; const land = pieces(1); - const main = land.size.reduce((b, s, k) => s.n > land.size[b].n ? k : b, 0); - for (let p = 0; p < W * H; p++) if (own[p] && land.id[p] !== main) own[p] = 0; + const main = land.reduce((b, s, k) => s.n > land[b].n ? k : b, 0); + for (let p = 0; p < N; p++) if (own[p] && id[p] !== main) own[p] = 0; const holes = pieces(0); - for (let p = 0; p < W * H; p++) if (!own[p] && !holes.size[holes.id[p]].edge) own[p] = 1; + for (let p = 0; p < N; p++) if (!own[p] && !holes[id[p]].edge) own[p] = 1; const edge = (src) => { - const out = new Uint8Array(W * H); + const out = new Uint8Array(N); for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) { const p = j * W + i; if (src[p] && (i === 0 || j === 0 || i === W - 1 || j === H - 1 || !src[p - 1] || !src[p + 1] || !src[p - W] || !src[p + W])) out[p] = 1; @@ -141,7 +286,7 @@ export function planWall({ layout, files, until }) { const wall = own.map((v, p) => (e1[p] || e2[p]) && dist[p] >= 1 ? 1 : 0); const at = p => [X0 + (p % W + 0.5) * cell, Z0 + (Math.floor(p / W) + 0.5) * cell]; const wallCells = []; - for (let p = 0; p < W * H; p++) if (wall[p]) wallCells.push(p); + for (let p = 0; p < N; p++) if (wall[p]) wallCells.push(p); if (wallCells.length < 20) return null; // gates: one to each side of the old town, where the wall crosses the widest street there @@ -159,31 +304,39 @@ export function planWall({ layout, files, until }) { } if (best >= 0) gates.push(at(best)); } - const gateHalf = u * 0.45; - const gap = p => { const [x, z] = at(p); return gates.some(g => Math.hypot(x - g[0], z - g[1]) < gateHalf); }; + const gap = p => { const [x, z] = at(p); return gates.some(g => Math.hypot(x - g[0], z - g[1]) < u * GATE); }; for (const p of wallCells) if (gap(p)) wall[p] = 0; - // towers: a pair flanking each gate, then one every few lots along the wall - const towers = []; - const free = ([x, z], d) => towers.every(t => Math.hypot(x - t[0], z - t[1]) >= d); - for (const g of gates) { - const ring = wallCells.filter(p => wall[p] && Math.abs(Math.hypot(at(p)[0] - g[0], at(p)[1] - g[1]) - gateHalf - cell) < cell * 1.5).map(at); - if (!ring.length) continue; - const a = ring[0], b = ring.reduce((m, q) => Math.hypot(q[0] - a[0], q[1] - a[1]) > Math.hypot(m[0] - a[0], m[1] - a[1]) ? q : m, a); - towers.push(a); if (b !== a) towers.push(b); - } - for (const p of wallCells) if (wall[p] && e1[p] && dist[p] >= 2 && free(at(p), u * 3)) towers.push(at(p)); + // a tower is wider than the wall: it stands only where none of the ground under it touches a building + const clear = ([x, z]) => { + const i0 = Math.floor((x - u * TOWER - X0) / cell), i1 = Math.floor((x + u * TOWER - X0) / cell); + const j0 = Math.floor((z - u * TOWER - Z0) / cell), j1 = Math.floor((z + u * TOWER - Z0) / cell); + if (i0 < 0 || j0 < 0 || i1 >= W || j1 >= H) return false; + for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) if (dist[j * W + i] === 0) return false; + return true; + }; + const towers = placeTowers(u, wallCells.filter(p => wall[p] && e1[p]).map(at), gates, clear); - // the wall as runs of cells along each row, so it's a handful of boxes rather than thousands - const runs = []; + // the wall as boxes: a run of cells along a row, stacked onto the same run in the rows before it, so + // it's a handful of boxes rather than thousands (each a hair over its cells, so they meet) + const boxes = []; + let open = new Map(); for (let j = 0; j < H; j++) { + const next = new Map(); for (let i = 0; i < W; i++) { if (!wall[j * W + i]) continue; let k = i; while (k + 1 < W && wall[j * W + k + 1]) k++; - runs.push([X0 + i * cell, X0 + (k + 1) * cell, Z0 + (j + 0.5) * cell]); + const b = open.get(i * W + k) || boxes[boxes.push([i, k, j, j]) - 1]; + b[3] = j; next.set(i * W + k, b); i = k; } + open = next; } - return { cell, runs, towers, gates }; + const hair = cell * 0.01; + const walls = boxes.map(([i0, i1, j0, j1]) => { + const x0 = X0 + i0 * cell - hair, x1 = X0 + (i1 + 1) * cell + hair, z0 = Z0 + j0 * cell - hair, z1 = Z0 + (j1 + 1) * cell + hair; + return x1 - x0 >= z1 - z0 ? [x0, (z0 + z1) / 2, x1, (z0 + z1) / 2, z1 - z0] : [(x0 + x1) / 2, z0, (x0 + x1) / 2, z1, x1 - x0]; + }); + return { walls, towers, gates }; } From dc32e052f9f545f75f0d4fc974606aae16467c1d Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Mon, 2 Nov 2026 23:29:14 -0500 Subject: [PATCH 4/4] Hosted site: cities are scanned again for the walled state The classifier now measures foundingShare and can set `walled`, so a city kept in R2 from before has neither. SCANNER goes to 9: each repo is scanned afresh the next time it's opened. Co-Authored-By: Claude Opus 5.5 --- deploy/cloudflare/container/version.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/cloudflare/container/version.mjs b/deploy/cloudflare/container/version.mjs index 5d8d697..44e26ed 100644 --- a/deploy/cloudflare/container/version.mjs +++ b/deploy/cloudflare/container/version.mjs @@ -2,4 +2,4 @@ // scanner changes what it finds: cities are cached per version, and the Worker only keeps a city from a // container that says it runs this version (after a deploy, containers already running keep the old // image for a while, and their cities mustn't be cached as new ones). -export const SCANNER = 8; +export const SCANNER = 9;