const NightCityMap = (() => { const Z = 4, SCALE = 256 * 16; const B = { x0: 0, y0: 0, x1: 14 * 256, y1: 13 * 256 }; const fracToLatLng = (fx, fy) => { const px = B.x0 + fx * (B.x1 - B.x0); const py = B.y0 + fy * (B.y1 - B.y0); return L.latLng(pyToLat(py), pxToLng(px)); }; const pxToLng = (px) => (px / SCALE) * 360 - 180; const pyToLat = (py) => { const n = Math.PI * (1 - (2 * py) / SCALE); return (Math.atan(Math.sinh(n)) * 180) / Math.PI; }; const latLngToUnits = (latlng) => { const p = L.CRS.EPSG3857.latLngToPoint(L.latLng(latlng), Z); const u = ((p.x - B.x0) / (B.x1 - B.x0)) * 1000; const v = ((p.y - B.y0) / (B.y1 - B.y0)) * 700; return { x: Math.max(0, Math.min(1000, u)), y: Math.max(0, Math.min(700, v)), }; }; const DISTRICTS = [ { name: 'Watson', box: [0.03, 0.03, 0.30, 0.24] }, { name: 'City Center', box: [0.33, 0.02, 0.54, 0.18] }, { name: 'Westbrook', box: [0.55, 0.04, 0.90, 0.28] }, { name: 'Heywood', box: [0.22, 0.26, 0.66, 0.60] }, { name: 'Santo Domingo', box: [0.66, 0.28, 0.95, 0.62] }, { name: 'Pacifica', box: [0.06, 0.62, 0.42, 0.95] }, { name: 'Badlands', box: [0.42, 0.62, 1.00, 0.97] }, { name: 'Badlands', box: [0.90, 0.10, 1.00, 0.60] }, ]; const POIS = [ { id: 'kabuki', name: 'Kabuki Market', fx: 0.13, fy: 0.10 }, { id: 'arasaka', name: 'Arasaka Waterfront', fx: 0.50, fy: 0.045 }, { id: 'corpo', name: 'Corpo Plaza', fx: 0.44, fy: 0.10 }, { id: 'japantown', name: 'Japantown', fx: 0.63, fy: 0.15 }, { id: 'afterlife', name: 'Afterlife', fx: 0.42, fy: 0.30 }, { id: 'mbh10', name: 'Megabuilding H10', fx: 0.33, fy: 0.42 }, { id: 'dogtown', name: 'Dogtown Gate', fx: 0.55, fy: 0.70 }, { id: 'mall', name: 'Grand Illusion Mall', fx: 0.23, fy: 0.78 }, ].map((p) => ({ ...p, latlng: fracToLatLng(p.fx, p.fy), units: { x: p.fx * 1000, y: p.fy * 700 } })); let map; const points = { A: null, B: null }; let onChange = () => {}; let routeLine = null; const markers = { A: null, B: null }; function districtAt(units) { const fx = units.x / 1000, fy = units.y / 700; for (const d of DISTRICTS) { const [x0, y0, x1, y1] = d.box; if (fx >= x0 && fx <= x1 && fy >= y0 && fy <= y1) return d.name; } return 'Night City outskirts'; } function nearestPOI(units) { let best = null, bestD = 55; for (const p of POIS) { const d = Math.hypot(p.units.x - units.x, (p.units.y - units.y) * (10 / 7)); if (d < bestD) { bestD = d; best = p; } } return best; } function placeName(units) { const poi = nearestPOI(units); return poi ? poi.name : districtAt(units); } function pinIcon(color, label) { return L.divIcon({ className: 'nc-pin-wrap', html: `
${label}
`, iconSize: [0, 0], iconAnchor: [0, 0], }); } function render() { for (const key of ['A', 'B']) { const p = points[key]; if (markers[key]) { markers[key].remove(); markers[key] = null; } if (p) { markers[key] = L.marker(p.latlng, { icon: pinIcon(key === 'A' ? '#00d4ff' : '#ffe600', key), zIndexOffset: 1000, keyboard: false, }).addTo(map); } } if (routeLine) { routeLine.remove(); routeLine = null; } if (points.A && points.B) { const { A, B } = points; const mid = fracToLatLng( (A.fx + B.fx) / 2 + (B.fy - A.fy) * 0.08, (A.fy + B.fy) / 2 - (B.fx - A.fx) * 0.08 ); routeLine = L.polyline([A.latlng, mid, B.latlng], { color: '#00f0ff', weight: 4, opacity: 0.9, dashArray: '12 10', className: 'nc-route', }).addTo(map); map.fitBounds(L.latLngBounds(A.latlng, B.latlng).pad(0.25), { animate: true }); } } function setPoint(which, latlng) { if (which === 'A' && points.A && points.B) { points.A = null; points.B = null; which = 'A'; } const units = latLngToUnits(latlng); points[which] = { latlng: L.latLng(latlng), lat: latlng.lat, lng: latlng.lng, x: units.x, y: units.y, fx: units.x / 1000, fy: units.y / 700, name: placeName(units), }; render(); onChange({ A: points.A, B: points.B }); } function init(container, callback) { map = L.map(container, { minZoom: 3, maxZoom: 5, zoomControl: true, attributionControl: false, maxBounds: L.latLngBounds(fracToLatLng(-0.03, -0.03), fracToLatLng(1.03, 1.03)), maxBoundsViscosity: 0.7, }); map.setView(fracToLatLng(0.5, 0.45), 4); L.tileLayer('img/tiles/{z}/{x}/{y}.webp', { minZoom: 3, maxZoom: 5, noWrap: true, bounds: L.latLngBounds(fracToLatLng(0, 0), fracToLatLng(1, 1)), }).addTo(map); for (const p of POIS) { L.marker(p.latlng, { icon: L.divIcon({ className: 'nc-poi-wrap', html: `
${p.name}
`, iconSize: [0, 0], }), keyboard: false, }) .on('click', (e) => { L.DomEvent.stopPropagation(e); setPoint(points.A === null ? 'A' : 'B', p.latlng); }) .addTo(map); } map.on('click', (e) => setPoint(points.A === null ? 'A' : 'B', e.latlng)); if (callback) onChange = callback; } return { init, setPoint: (which, latlng) => setPoint(which, latlng), get points() { return points; }, reset() { points.A = null; points.B = null; render(); map.setView(fracToLatLng(0.5, 0.45), 4); onChange({ A: null, B: null }); }, refresh() { if (map) map.invalidateSize({ animate: false }); }, }; })();