// Exploration only: never changes grains or the random stream. const FOG = 2; const FOG_SENSE = 24; // Reveal nearby ground through rock, in grains. const FOG_SENSE_FADE = 12; let fog, FW, FH; function fogAlloc() { FW = Math.ceil(W / FOG); FH = Math.ceil(H / FOG); fog = new Uint8Array(FW * FH); } function fogMark(x, y, v) { if (x < 0 || y < 0 || x >= W || y >= H) return; const i = Math.floor(y / FOG) * FW + Math.floor(x / FOG); if (v > fog[i]) fog[i] = v; } function fogSeen(x, y) { if (x < 0 || y < 0 || x >= W || y >= H) return 255; return fog[Math.floor(y / FOG) * FW + Math.floor(x / FOG)]; } function fogInit() { for (let x = 0; x < W; x++) for (let y = 0; y < Math.min(H, surfY[x] + 16); y++) fogMark(x, y, y <= surfY[x] ? 255 : (surfY[x] + 16 - y) * 255 / 16); } function fogLook(cx, cy) { for (let a = 0; a < 256; a++) { const dx = Math.cos(a * Math.PI / 128), dy = Math.sin(a * Math.PI / 128); let hit = -1; for (let d = 0; d < 48; d++) { const x = Math.floor(cx + dx * d), y = Math.floor(cy + dy * d); if (x < 0 || y < 0 || x >= W || y >= H) break; const ph = PH[mat[y * W + x]]; if (hit < 0 && (ph === PH_SOLID || ph === PH_POWDER)) hit = d; // into rock, sight fades out over 8 grains from however bright it arrived const v = Math.min(255, (48 - d) * 255 / 16) * (hit < 0 ? 1 : (8 - d + hit) / 8); if (v <= 0) break; fogMark(x, y, v); } } // one fog cell at a time, measured from its centre; full inside 12 grains, fading over the outer 12 grains const R2 = FOG_SENSE * FOG_SENSE, in2 = (FOG_SENSE - FOG_SENSE_FADE) ** 2; const fy0 = Math.max(0, Math.floor((cy - FOG_SENSE) / FOG)), fy1 = Math.min(FH - 1, Math.floor((cy + FOG_SENSE) / FOG)); const fx0 = Math.max(0, Math.floor((cx - FOG_SENSE) / FOG)), fx1 = Math.min(FW - 1, Math.floor((cx + FOG_SENSE) / FOG)); for (let fy = fy0; fy <= fy1; fy++) for (let fx = fx0; fx <= fx1; fx++) { const dx = (fx + 0.5) * FOG - cx, dy = (fy + 0.5) * FOG - cy, d2 = dx * dx + dy * dy, i = fy * FW + fx; if (d2 >= R2) continue; const v = d2 <= in2 ? 255 : (FOG_SENSE - Math.sqrt(d2)) * 255 / FOG_SENSE_FADE; if (v > fog[i]) fog[i] = v; } } function fogRevealAround(x, y, r) { for (let yy = Math.max(0, Math.floor(y - r)); yy <= Math.min(H - 1, y + r); yy++) for (let xx = Math.max(0, Math.floor(x - r)); xx <= Math.min(W - 1, x + r); xx++) { const d2 = (xx - x) ** 2 + (yy - y) ** 2; if (d2 >= r * r) continue; fogMark(xx, yy, r >= 6 && d2 <= (r - 6) ** 2 ? 255 : Math.min(255, (r - Math.sqrt(d2)) * 255 / 6)); } } function fogRevealAll() { fog.fill(255); }