import { City, CityGenerationMode } from 'gen-city'; const seed = value => { let state = 2166136261; for (const char of String(value)) { state ^= char.charCodeAt(0); state = Math.imul(state, 16777619); } return Array.from({length:256}, () => { state = Math.imul(state ^ state >>> 15, 2246822519); return Math.abs(state % 1001); }); }; export async function generateFabric(key, width = 180, height = 100) { if (typeof key !== 'string' || !key || key.length > 256 || !Number.isInteger(width) || !Number.isInteger(height) || width < 64 || width > 320 || height < 64 || height > 240) { throw new RangeError('City generation bounds exceeded'); } const city = new City({width, height}); await city.generate({mode:CityGenerationMode.SEED,seed:seed(key),streetMinLength:9,probabilityIntersection:.12,probabilityTurn:.09,probabilityStreetEnd:.012,buildingMinSize:3,buildingMaxSize:7,buildingMinSpace:1,buildingMaxSpace:2,buildingOffset:1}); const fabric = { generator:'gen-city', version:'1.4.6', width, height, roads:city.getAllPaths().map(path => { const p=path.getPositions(); return [p.beg.x,p.beg.y,p.end.x,p.end.y]; }), buildings:city.getAllBuildings().map(building => building.vertices.map(point => [point.x,point.y])) }; if (fabric.roads.length > 4096 || fabric.buildings.length > 4096) throw new RangeError('City geometry budget exceeded'); return fabric; }