LINE1 10 20 30 40 50 60 70 80 90 100 1101import * as THREE from 'three';2import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';3import {4 decorateBuilding,5 decorateStreets,6 decorateInterior,7 decorateRoof,8 decorateCar,9 decorateRooms,10 decorateRubble,11 damageFor,12} from './detail.js';13import { NEST, PILL, WORKBENCH } from './config.js';1415export const STOREY = 3.5;16export const LOT = 36;17export const LOT_RANGE = 6;18export const WORLD_HALF = (LOT_RANGE + 0.5) * LOT;19const WALL_T = 0.3;20const CELL = 12;21const PALETTE = [0x8a5a44, 0x9c8a74, 0x6e6a66, 0xa8a095, 0x5d4a3f, 0x7b6b5a, 0x7d7f85, 0x9a6d55];22const ROOF = 0x5a5a60;23const FLOOR = 0x8c7f68;24const GRASS = 0x3d5a2c;25const ASPHALT = 0x2e2e31;26const CAR_COLORS = [0x8c1d18, 0x1d3a6b, 0xcfcfcf, 0x2b2b2b, 0x6d7a3a, 0xb08b2e];27const PLAYER_B = { x0: -9, x1: 9, z0: -7, z1: 7, storeys: 3 };28const STEP = 0.6;29const STEP_HEX = 0x8f8a82;30const RAIL_HEX = 0x4a4a4a;31const PARAPET_HEX = 0x6b6358;32export const walkPads = [];3334export const solids = [];35export const buildings = [];36let playerB = null;37const grid = new Map();38const geoms = [];394041export function resetWorld() {42 solids.length = 0;43 buildings.length = 0;44 walkPads.length = 0;45 grid.clear();46 geoms.length = 0;47 playerB = null;48}495051export function commitMesh() {52 const geo = mergeGeometries(geoms, false);53 for (const g of geoms) g.dispose();54 geoms.length = 0;55 return new THREE.Mesh(geo, new THREE.MeshLambertMaterial({ vertexColors: true }));56}5758function mulberry32(a) {59 return function () {60 a |= 0;61 a = (a + 0x6d2b79f5) | 0;62 let t = Math.imul(a ^ (a >>> 15), 1 | a);63 t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;64 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;65 };66}6768function paint(geo, hex) {69 const c = new THREE.Color(hex);70 const n = geo.attributes.position.count;71 const arr = new Float32Array(n * 3);72 for (let i = 0; i < n; i++) {73 arr[i * 3] = c.r;74 arr[i * 3 + 1] = c.g;75 arr[i * 3 + 2] = c.b;76 }77 geo.setAttribute('color', new THREE.BufferAttribute(arr, 3));78 geo.deleteAttribute('uv');79 return geo;80}8182export function box(x0, y0, z0, x1, y1, z1, hex) {83 const g = new THREE.BoxGeometry(x1 - x0, y1 - y0, z1 - z0);84 g.translate((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2);85 geoms.push(paint(g, hex));86}8788function cellKeys(x0, z0, x1, z1) {89 const keys = [];90 for (let i = Math.floor(x0 / CELL); i <= Math.floor(x1 / CELL); i++) {91 for (let k = Math.floor(z0 / CELL); k <= Math.floor(z1 / CELL); k++) {92 keys.push(i * 100003 + k);93 }94 }95 return keys;96}9798export function solidBox(x0, y0, z0, x1, y1, z1, hex) {99 const s = { x0, y0, z0, x1, y1, z1 };100 solids.push(s);101 for (const key of cellKeys(x0, z0, x1, z1)) {102 if (!grid.has(key)) grid.set(key, []);103 grid.get(key).push(s);104 }105 box(x0, y0, z0, x1, y1, z1, hex);106}107108export function querySolids(x0, z0, x1, z1) {109 const out = new Set();110 for (const key of cellKeys(x0, z0, x1, z1)) {111 const list = grid.get(key);112 if (list) for (const s of list) out.add(s);113 }114 return out;115}116117export function blockedAt(x, z, r, yMin = 0, yMax = 1.8) {118 for (const s of querySolids(x - r, z - r, x + r, z + r)) {119 if (s.y1 <= yMin || s.y0 >= yMax) continue;120 const dx = Math.max(s.x0 - x, 0, x - s.x1);121 const dz = Math.max(s.z0 - z, 0, z - s.z1);122 if (dx * dx + dz * dz < r * r) return true;123 }124 return false;125}126127export function inBuilding(x, z, margin = 0) {128 for (const b of buildings) {129 if (x > b.x0 - margin && x < b.x1 + margin && z > b.z0 - margin && z < b.z1 + margin) return true;130 }131 return false;132}133134export function segmentSolidHit(p0, p1) {135 const x0 = Math.min(p0.x, p1.x), x1 = Math.max(p0.x, p1.x);136 const z0 = Math.min(p0.z, p1.z), z1 = Math.max(p0.z, p1.z);137 let best = Infinity;138 for (const s of querySolids(x0, z0, x1, z1)) {139 const t = segmentAABB(p0, p1, s);140 if (t >= 0 && t < best) best = t;141 }142 return best;143}144145export function segmentAABB(p0, p1, b) {146 let tmin = 0;147 let tmax = 1;148 const o = [p0.x, p0.y, p0.z];149 const d = [p1.x - p0.x, p1.y - p0.y, p1.z - p0.z];150 const mn = [b.x0, b.y0, b.z0];151 const mx = [b.x1, b.y1, b.z1];152 for (let i = 0; i < 3; i++) {153 if (Math.abs(d[i]) < 1e-9) {154 if (o[i] < mn[i] || o[i] > mx[i]) return -1;155 } else {156 let t1 = (mn[i] - o[i]) / d[i];157 let t2 = (mx[i] - o[i]) / d[i];158 if (t1 > t2) [t1, t2] = [t2, t1];159 tmin = Math.max(tmin, t1);160 tmax = Math.min(tmax, t2);161 if (tmin > tmax) return -1;162 }163 }164 return tmin;165}166167function facadeRects(L, H, openings) {168 const us = [...new Set([0, L, ...openings.flatMap((o) => [o[0], o[2]])])].sort((a, b) => a - b);169 const vs = [...new Set([0, H, ...openings.flatMap((o) => [o[1], o[3]])])].sort((a, b) => a - b);170 const rects = [];171 for (let j = 0; j < vs.length - 1; j++) {172 let runStart = null;173 const vc = (vs[j] + vs[j + 1]) / 2;174 for (let i = 0; i < us.length - 1; i++) {175 const uc = (us[i] + us[i + 1]) / 2;176 const open = openings.some((o) => uc > o[0] && uc < o[2] && vc > o[1] && vc < o[3]);177 if (!open) {178 if (runStart === null) runStart = us[i];179 } else if (runStart !== null) {180 rects.push([runStart, vs[j], us[i], vs[j + 1]]);181 runStart = null;182 }183 }184 if (runStart !== null) rects.push([runStart, vs[j], us[us.length - 1], vs[j + 1]]);185 }186 return rects;187}188189function windowOpenings(L, storeys, enabled) {190 const out = [];191 if (!enabled) return out;192 const n = Math.max(1, Math.floor(L / 5));193 for (let s = 0; s < storeys; s++) {194 for (let k = 0; k < n; k++) {195 const c = (L * (k + 1)) / (n + 1);196 out.push([c - 0.8, s * STOREY + 1.0, c + 0.8, s * STOREY + 2.6]);197 }198 }199 return out;200}201202function addFacade(b, side, hex, enabled, solid) {203 const alongX = side === 'n' || side === 's';204 const L = alongX ? b.x1 - b.x0 : b.z1 - b.z0;205 const H = b.storeys * STOREY;206 const rects = facadeRects(L, H, [...windowOpenings(L, b.storeys, enabled), ...(b.doors?.[side] ?? [])]);207 for (const [u0, v0, u1, v1] of rects) {208 if (alongX) {209 const z0 = side === 'n' ? b.z1 - WALL_T : b.z0;210 const z1 = side === 'n' ? b.z1 : b.z0 + WALL_T;211 solid(b.x0 + u0, v0, z0, b.x0 + u1, v1, z1, hex);212 } else {213 const x0 = side === 'e' ? b.x1 - WALL_T : b.x0;214 const x1 = side === 'e' ? b.x1 : b.x0 + WALL_T;215 solid(x0, v0, b.z0 + u0, x1, v1, b.z0 + u1, hex);216 }217 }218}219220221222function clipApi(b) {223 const d = b.damage;224 if (!d) return detailApi;225 const trimX1 = (x0, y0, x1) => {226 if (y0 < d.cutH - 0.01) return x1;227 if (x0 >= d.cutX) return null;228 return Math.min(x1, d.cutX);229 };230 return {231 box(x0, y0, z0, x1, y1, z1, hex) {232 const nx1 = trimX1(x0, y0, x1);233 if (nx1 !== null) box(x0, y0, z0, nx1, y1, z1, hex);234 },235 solidBox(x0, y0, z0, x1, y1, z1, hex) {236 const nx1 = trimX1(x0, y0, x1);237 if (nx1 !== null) solidBox(x0, y0, z0, nx1, y1, z1, hex);238 },239 add(g, hex) {240 g.computeBoundingBox();241 const { min } = g.boundingBox;242 if (min.y < d.cutH - 0.01 || min.x < d.cutX) detailApi.add(g, hex);243 },244 };245}246247function buildShell(b, rnd) {248 const hex = PALETTE[Math.floor(rnd() * PALETTE.length)];249 const api = clipApi(b);250 b.open = {};251 for (const side of ['n', 's', 'e', 'w']) {252 const open = b.player ? true : rnd() < 0.8;253 b.open[side] = open;254 addFacade(b, side, hex, open, api.solidBox);255 }256 api.box(b.x0, 0.05, b.z0, b.x1, 0.1, b.z1, FLOOR);257 if (!b.player) {258 for (let s = 1; s < b.storeys; s++) {259 api.box(b.x0 + 0.3, s * STOREY - 0.15, b.z0 + 0.3, b.x1 - 0.3, s * STOREY, b.z1 - 0.3, FLOOR);260 }261 }262 const H = b.storeys * STOREY;263 api.box(b.x0 - 0.2, H, b.z0 - 0.2, b.x1 + 0.2, H + 0.3, b.z1 + 0.2, ROOF);264 decorateBuilding(api, b);265 if (!b.player) decorateRooms(api, b);266 if (b.damage) decorateRubble(detailApi, b);267}268269270export function addGeom(g, hex) {271 geoms.push(paint(g, hex));272}273274const detailApi = { box, solidBox, add: addGeom };275276277export function buildCity(root) {278 resetWorld();279 const rnd = mulberry32(1337);280 const lotCenters = [];281282 box(-WORLD_HALF - 20, -0.1, -WORLD_HALF - 20, WORLD_HALF + 20, 0, WORLD_HALF + 20, ASPHALT);283284 for (let ix = -LOT_RANGE; ix <= LOT_RANGE; ix++) {285 for (let iz = -LOT_RANGE; iz <= LOT_RANGE; iz++) {286 const cx = ix * LOT;287 const cz = iz * LOT;288 box(cx - 12, 0, cz - 12, cx + 12, 0.05, cz + 12, GRASS);289 lotCenters.push({ cx, cz });290 if (ix === 0 && iz === 0) continue;291 if (rnd() < 0.15) continue;292 const fw = 12 + rnd() * 8;293 const fd = 12 + rnd() * 8;294 const ox = (rnd() * 2 - 1) * (11 - fw / 2);295 const oz = (rnd() * 2 - 1) * (11 - fd / 2);296 const b = {297 x0: cx + ox - fw / 2,298 x1: cx + ox + fw / 2,299 z0: cz + oz - fd / 2,300 z1: cz + oz + fd / 2,301 storeys: 1 + Math.floor(rnd() * 5),302 };303 b.damage = damageFor(b);304 buildShell(b, rnd);305 buildings.push(b);306 }307 }308309 decorateStreets(detailApi, lotCenters);310 buildPlayerBuilding();311312 let cars = 0;313 for (let tries = 0; tries < 400 && cars < 45; tries++) {314 const x = (rnd() * 2 - 1) * WORLD_HALF;315 const z = (rnd() * 2 - 1) * WORLD_HALF;316 if (Math.hypot(x, z) < 25 || inBuilding(x, z, 2) || blockedAt(x, z, 3)) continue;317 const along = rnd() < 0.5;318 const L = 4.2;319 const W = 1.9;320 const hex = CAR_COLORS[Math.floor(rnd() * CAR_COLORS.length)];321 const hx = along ? L / 2 : W / 2;322 const hz = along ? W / 2 : L / 2;323 solidBox(x - hx, 0, z - hz, x + hx, 0.8, z + hz, hex);324 decorateCar(detailApi, x, z, along, hex);325 cars++;326 }327328 const mesh = commitMesh();329 root.add(mesh);330 const bounds = { x0: -WORLD_HALF, x1: WORLD_HALF, z0: -WORLD_HALF, z1: WORLD_HALF };331 return { mesh, bounds, layout: { spawn: NEST, workbench: WORKBENCH, pill: PILL } };332}333334export function groundAt(x, z, feet) {335 let best = 0;336 for (const p of walkPads) {337 if (x < p.x0 || x > p.x1 || z < p.z0 || z > p.z1) continue;338 const t = p.z1 === p.z0 ? 0 : (z - p.z0) / (p.z1 - p.z0);339 const h = p.h0 + (p.h1 - p.h0) * t;340 if (h <= feet + STEP && h > best) best = h;341 }342 return best;343}344345346347348349350351const LANDING_Z = [-0.9, 1.2];352const FLIGHT_STEPS = 9;353const FLIGHT_RUN = 0.35;354355function buildOutsideStairs(b) {356 const deck = 2 * STOREY;357 const roof = b.storeys * STOREY + 0.3;358 const xIn = b.x1 - WALL_T;359 const xOut = b.x1 + 1.4;360 const [lz0, lz1] = LANDING_Z;361362 walkPads.push({ x0: xIn, x1: xOut, z0: lz0, z1: lz1, h0: deck, h1: deck });363 box(b.x1, deck - 0.2, lz0, xOut, deck, lz1, STEP_HEX);364 solidBox(xIn, deck, lz0 - 0.1, xOut + 0.1, deck + 1, lz0, RAIL_HEX);365 solidBox(xOut, deck, lz0, xOut + 0.1, deck + 1, lz1, RAIL_HEX);366 for (const z of [lz0 + 0.1, lz1 - 0.1]) box(b.x1 + 0.6, 0, z - 0.1, b.x1 + 0.8, deck - 0.2, z + 0.1, STEP_HEX);367368 const rise = (roof - deck) / FLIGHT_STEPS;369 for (let k = 1; k <= FLIGHT_STEPS; k++) {370 const z0 = lz1 + (k - 1) * FLIGHT_RUN;371 const z1 = z0 + FLIGHT_RUN;372 const h = deck + k * rise;373 walkPads.push({ x0: xIn, x1: xOut, z0, z1, h0: h, h1: h });374 box(b.x1, h - 0.2, z0, xOut, h, z1, STEP_HEX);375 solidBox(xOut, h, z0, xOut + 0.1, h + 1, z1, RAIL_HEX);376 }377378 379 const flightTop = lz1 + FLIGHT_STEPS * FLIGHT_RUN;380 walkPads.push({ x0: b.x0 - 0.2, x1: b.x1 + 0.2, z0: b.z0 - 0.2, z1: b.z1 + 0.2, h0: roof, h1: roof });381 382 walkPads.push({ x0: b.x1 - 0.2, x1: xOut, z0: flightTop, z1: b.z1 + 0.2, h0: roof, h1: roof });383 box(b.x1 - 0.2, roof - 0.2, flightTop, xOut, roof, b.z1 + 0.2, ROOF);384 solidBox(xOut, roof, flightTop, xOut + 0.1, roof + 1, b.z1 + 0.2, PARAPET_HEX);385 solidBox(b.x0 - 0.3, roof, b.z0 - 0.3, b.x1 + 0.3, roof + 1, b.z0 - 0.2, PARAPET_HEX);386 solidBox(b.x0 - 0.3, roof, b.z0 - 0.2, b.x0 - 0.2, roof + 1, b.z1 + 0.2, PARAPET_HEX);387 388 solidBox(b.x0 - 0.3, roof, b.z1 + 0.2, xOut + 0.1, roof + 1, b.z1 + 0.3, PARAPET_HEX);389 solidBox(b.x1 + 0.2, roof, b.z0 - 0.2, b.x1 + 0.3, roof + 1, lz1, PARAPET_HEX);390 decorateRoof(detailApi, b, solidBox, roof);391}392393function buildPlayerBuilding() {394 const b = { ...PLAYER_B, player: true };395 playerB = b;396 const partH = STOREY * b.storeys;397 const top = 2 * STOREY;398 399 b.doors = { e: [[6.4, top, 7.6, top + 2.0]] };400 buildShell(b, () => 0.5);401 solidBox(-1 - WALL_T / 2, 0, -7, -1 + WALL_T / 2, partH, -2, 0x8a7f72);402 solidBox(-1 - WALL_T / 2, 0, 2, -1 + WALL_T / 2, partH, 7, 0x8a7f72);403404 for (const h of [STOREY, top]) {405 box(b.x0 + 0.3, h - 0.15, b.z0 + 0.3, b.x1 - 0.3, h, b.z1 - 0.3, FLOOR);406 }407 walkPads.push({ x0: b.x0 + 0.3, x1: b.x1 - 0.3, z0: b.z0 + 0.3, z1: b.z1 - 0.3, h0: top, h1: top });408409 solidBox(-7.5, top, -5, -5.5, top + 0.9, -3, 0x6b5238);410 solidBox(-8.6, top, 3.5, -7.4, top + 2.0, 4.7, 0x35404a);411 solidBox(3.6, top, 5.0, 5.4, top + 0.9, 6.0, 0x4a3b2c);412 decorateInterior(detailApi, b, solidBox);413 buildOutsideStairs(b);414 buildings.push(b);415}416417export function nearPlayerBuilding(x, z, margin) {418 const b = playerB;419 if (!b) return false;420 return x > b.x0 - margin && x < b.x1 + margin && z > b.z0 - margin && z < b.z1 + margin;421}