const steps = [1, 2, 4, 8, 16, 32, 64]; const tileSide = 128; const worldLimit = 30000000; export function terrainTiles(view, width, height) { const {x, z, scale} = view; const left = Math.max(-worldLimit, x - width / (2 * scale)); const right = Math.min(worldLimit, x + width / (2 * scale)); const top = Math.max(-worldLimit, z - height / (2 * scale)); const bottom = Math.min(worldLimit, z + height / (2 * scale)); const step = steps.find(s => { const span = tileSide * s; return span * scale >= 180 && (Math.ceil((right - left) / span) + 1) * (Math.ceil((bottom - top) / span) + 1) <= 16; }) ?? steps.at(-1); const span = tileSide * step; const tiles = []; for (let tz = Math.floor(top / span) * span; tz < bottom; tz += span) { for (let tx = Math.floor(left / span) * span; tx < right; tx += span) { if (tx < -worldLimit || tz < -worldLimit || tx + span > worldLimit || tz + span > worldLimit) continue; tiles.push({x: tx, z: tz, step, span}); } } const distance = tile => (tile.x + span / 2 - x) ** 2 + (tile.z + span / 2 - z) ** 2; return tiles.sort((a, b) => distance(a) - distance(b)).slice(0, 16); } export function createTerrain(onChange) { const cache = new Map(); let pending = 0, generation = 0; async function load(scope, tile, entry) { const epoch = generation; const params = new URLSearchParams({scope, x: tile.x, z: tile.z, step: tile.step}); const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 3500); pending++; entry.busy = true; try { const response = await fetch(`/api/terrain?${params}`, { cache: "no-store", signal: controller.signal, headers: entry.etag ? {"If-None-Match": entry.etag} : {}, }); if (response.status === 200) { const bitmap = await createImageBitmap(await response.blob()); if (epoch !== generation) { bitmap.close(); return; } entry.image?.close(); entry.image = bitmap; entry.etag = response.headers.get("ETag"); } entry.after = performance.now() + (response.status === 202 ? 650 : 3500); } catch { entry.after = performance.now() + 3500; } finally { clearTimeout(timeout); pending--; entry.busy = false; if (epoch === generation) onChange(); } } return { paint(ctx, view, width, height, scope, online) { if (!scope) return 0; let drawn = 0; const now = performance.now(); const tiles = terrainTiles(view, width, height); const used = new Set(); for (const tile of tiles) { const key = `${scope}:${tile.x}:${tile.z}:${tile.step}`; used.add(key); const entry = cache.get(key) || {after: 0}; cache.delete(key); cache.set(key, entry); if (entry.image) { const x = (tile.x - view.x) * view.scale + width / 2; const y = (tile.z - view.z) * view.scale + height / 2; const side = tile.span * view.scale; ctx.drawImage(entry.image, x, y, side + .25, side + .25); drawn++; } if (online && pending < 4 && !entry.busy && entry.after <= now) load(scope, tile, entry); } for (const [key, entry] of cache) { if (cache.size <= 96) break; if (entry.busy || used.has(key)) continue; entry.image?.close(); cache.delete(key); } return drawn; }, clear() { generation++; for (const tile of cache.values()) tile.image?.close(); cache.clear(); }, }; }