/* Tarkov Maps -- every map on one shared canvas. * * Coordinates: LatLng is (y, x) in *world pixels* at native resolution. * A map placed in grid cell (gx, gy) occupies the world rectangle * [gx*cellSize, gy*cellSize] .. [+width, +height]. Because cellSize is * tileSize * 2**maxZoom, the tile-index shift for a cell is exactly * cell * 2**z at every zoom -- so each map keeps its own locally numbered * pyramid and we translate on the way out in getTileUrl(). */ const ZOOM_HEADROOM = 2; // how far past 1:1 the user may zoom const BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; const OffsetTileLayer = L.TileLayer.extend({ getTileUrl(coords) { const n = 2 ** coords.z; return L.Util.template(this._url, { z: coords.z, x: coords.x - this.options.cell[0] * n, y: coords.y - this.options.cell[1] * n, }); }, }); const rectOf = (m) => L.latLngBounds( [m.origin[1], m.origin[0]], [m.origin[1] + m.size[1], m.origin[0] + m.size[0]], ); function readHash() { const p = location.hash.replace(/^#/, '').split('/'); if (p.length !== 3) return null; const [z, y, x] = p.map(Number); return [z, y, x].every(Number.isFinite) ? { z, y, x } : null; } async function main() { const world = await fetch('data/world.json', { cache: 'no-cache' }).then((r) => { if (!r.ok) throw new Error(`world.json: ${r.status}`); return r.json(); }); if (!world.maps.length) throw new Error('world.json contains no maps'); const ZMAX = world.maxZoom; // CRS where one LatLng unit == one source pixel at zoom ZMAX, y pointing // down so world coordinates read the same way as image coordinates. const CRS = L.extend({}, L.CRS.Simple, { transformation: new L.Transformation(1, 0, 1, 0), scale: (z) => 2 ** (z - ZMAX), zoom: (s) => Math.log(s) / Math.LN2 + ZMAX, }); const extent = world.maps.reduce( (acc, m) => (acc ? acc.extend(rectOf(m)) : rectOf(m)), null, ); const map = L.map('map', { crs: CRS, minZoom: 0, maxZoom: ZMAX + ZOOM_HEADROOM, zoomSnap: 0.25, zoomDelta: 0.5, wheelPxPerZoomLevel: 90, maxBounds: extent.pad(0.08), maxBoundsViscosity: 0.75, attributionControl: false, }); L.control.attribution({ prefix: false }) .addAttribution(world.attribution) .addTo(map); for (const m of world.maps) { new OffsetTileLayer(`tiles/${m.id}/{z}/{x}/{y}.webp`, { cell: m.cell, bounds: rectOf(m), tileSize: world.tileSize, minZoom: 0, maxZoom: ZMAX + ZOOM_HEADROOM, maxNativeZoom: ZMAX, noWrap: true, keepBuffer: 2, updateWhenIdle: false, errorTileUrl: BLANK, }).addTo(map); } // --- picker ----------------------------------------------------------- const list = document.getElementById('maplist'); const buttons = world.maps.map((m) => { const b = document.createElement('button'); b.textContent = m.name; b.dataset.id = m.id; b.addEventListener('click', () => { map.flyToBounds(rectOf(m), { padding: [20, 20], duration: 0.6 }); if (window.matchMedia('(max-width: 640px)').matches) { document.getElementById('panel').open = false; } }); list.appendChild(b); return b; }); const syncUI = () => { const c = map.getCenter(); const active = world.maps.find((m) => rectOf(m).contains(c)); buttons.forEach((b) => b.setAttribute('aria-current', String(active?.id === b.dataset.id))); const z = Math.round(map.getZoom() * 4) / 4; history.replaceState(null, '', `#${z}/${Math.round(c.lat)}/${Math.round(c.lng)}`); }; map.on('moveend zoomend', syncUI); // --- initial view ----------------------------------------------------- const h = readHash(); if (h) map.setView([h.y, h.x], h.z); else map.fitBounds(rectOf(world.maps[0]), { padding: [20, 20] }); syncUI(); document.getElementById('boot').remove(); window.tarkovMap = map; // handy from the console when checking a new map window.tarkovWorld = world; } main().catch((err) => { document.getElementById('boot').textContent = `Failed to load: ${err.message}`; console.error(err); });