staging-tester / ocean-landing-serve / src/ lib/ ocean-scene.ts

export type Glyph = { x: number; y: number; ch: string };

const ramp = ' .,:-~=+*#%@';

const hash = (x: number, y: number) => {
	const n = Math.sin(x * 12.9898 + y * 78.233) * 43758.5453;
	return n - Math.floor(n);
};

const wrap = (v: number, span: number) => ((v % span) + span) % span;

export type Layout = { cols: number; rows: number; surface: number; coast: number; skyTop: number };

// skyTop is the first row below the page content; sky props stay between it and the surface so they never sit behind text.
export function layout(cols: number, rows: number, surface: number, skyTop: number): Layout {
	const coastWidth = cols < 70 ? 0 : Math.min(26, Math.round(cols * 0.13));
	return { cols, rows, surface, coast: cols - coastWidth, skyTop: Math.min(skyTop, surface - 4) };
}

const cliffHeight = (x: number, l: Layout) => {
	const d = x - l.coast;
	if (d < 0) return -1;
	return Math.min(7, Math.round(d * 0.6 + Math.sin(d * 0.9)));
};

export const shelfX = (y: number, l: Layout) => l.coast - (y - l.surface) * 0.9;

export function coastGrid(l: Layout): string[] {
	const g = Array.from({ length: l.rows }, () => Array<string>(l.cols).fill(' '));
	if (l.coast >= l.cols) return g.map((r) => r.join(''));
	for (let x = l.coast; x < l.cols; x++) {
		const h = cliffHeight(x, l);
		const top = l.surface - h;
		for (let y = Math.max(top, 0); y <= l.surface; y++) {
			g[y]![x] = y === top ? (cliffHeight(x - 1, l) < h ? '/' : '_') : ':';
		}
	}
	for (let y = l.surface + 1; y < l.rows; y++) {
		const edge = Math.round(shelfX(y, l));
		for (let x = Math.max(edge, 0); x < l.cols; x++) {
			g[y]![x] = x === edge ? '/' : (x + y * 2) % 5 === 0 ? '.' : ' ';
		}
	}
	const lx = l.cols - 6;
	const top = l.surface - cliffHeight(lx, l);
	[' /^\\', ' |#|', ' | |', ' |#|', '/___\\'].forEach((line, i) =>
		[...line].forEach((ch, j) => {
			const y = top - 5 + i;
			if (ch !== ' ' && y >= 0 && g[y]?.[lx - 2 + j] !== undefined) g[y]![lx - 2 + j] = ch;
		})
	);
	return g.map((r) => r.join(''));
}

export type Ripple = { x: number; y: number; at: number; strength: number };
export const rippleLife = 3;

// Height a set of expanding rings adds at a cell. Cells are 8x15px, so rows count for 15/8 of a column to keep rings round.
const rippleAt = (x: number, y: number, t: number, ripples: Ripple[]) => {
	let h = 0;
	for (const r of ripples) {
		const age = t - r.at;
		if (age < 0 || age > rippleLife) continue;
		const dist = Math.hypot(x - r.x, (y - r.y) * 1.875);
		const off = dist - age * 16;
		if (off > 6 || off < -10) continue;
		h += Math.cos(off * 1.1) * Math.exp(-(off * off) / 10) * r.strength * (1 - age / rippleLife);
	}
	return h;
};

// Depth field: sparse near the surface, dense toward the floor, with slow swells, surface caustics and the visitor's ripples.
export function seaGrid(t: number, l: Layout, ripples: Ripple[] = []): string[] {
	const out: string[] = [];
	const depth = l.rows - l.surface - 1;
	for (let y = l.surface + 1; y < l.rows; y++) {
		const d = (y - l.surface) / depth;
		const edge = Math.floor(shelfX(y, l));
		let line = '';
		for (let x = 0; x < l.cols; x++) {
			if (x >= edge) {
				line += ' ';
				continue;
			}
			const swell =
				Math.sin(x * 0.09 + t * 0.7 + y * 0.35) * 0.5 +
				Math.sin(x * 0.033 - t * 0.4 + y * 0.12) * 0.7 +
				Math.sin((x + y) * 0.15 + t * 1.1) * 0.25;
			const caustic =
				y - l.surface < 6
					? Math.max(0, Math.sin(x * 0.55 + t * 1.8) * Math.sin(x * 0.21 - t * 1.2 + y)) * 0.35
					: 0;
			const grain = (hash(x, y) - 0.5) * 0.08;
			const ring = ripples.length > 0 ? rippleAt(x, y, t, ripples) * 0.32 : 0;
			const v = Math.pow(d, 0.9) * 0.82 + swell * 0.16 + caustic + grain + ring - 0.04;
			line += ramp[Math.max(0, Math.min(ramp.length - 1, Math.floor(v * ramp.length)))]!;
		}
		out.push(line);
	}
	return out;
}

export function waveLine(t: number, l: Layout, ripples: Ripple[] = []) {
	const phase = t * 2.4;
	const shift = Math.floor(phase);
	let line = '';
	for (let x = 0; x <= l.coast + 1; x++) {
		const u = x - shift;
		const w = Math.sin(u * 0.31) + Math.sin(u * 0.11) * 0.6 + (ripples.length > 0 ? rippleAt(x + phase - shift - 1, l.surface + 1, t, ripples) * 1.4 : 0);
		line += w > 0.95 ? '^' : w > -0.15 ? '~' : '-';
	}
	return { line, offset: phase - shift - 1 };
}

export function clouds(t: number, l: Layout): Glyph[] {
	const art = [
		['      .--.', '   .-(    ).', '  (___.__)__)'],
		['     _  _', '  .-( `  ).-.', ' (___.__)___)'],
		['    .-.', ' .-(   )-.', ' (___.___)'],
		['        .--.', '   .--(    )--.', ' (____.______)__)']
	];
	const span = l.cols + 30;
	const band = Math.max(1, l.surface - l.skyTop - 5);
	return art.flatMap((lines, i) => {
		const x = wrap(i * (span / art.length) - t * (0.35 + i * 0.12), span) - 18;
		const y = l.skyTop + ((i * 3) % band);
		return lines.map((ch, j) => ({ x, y: y + j, ch }));
	});
}

const daylight = (hour: number) => hour >= 6 && hour < 19;

// With the visitor's local hour the sun rises at the left at 6:00 and sets at the right at 19:00; without it, it rests by the coast.
export function sun(l: Layout, hour: number | null = null): Glyph[] {
	if (l.surface - l.skyTop < 7) return [];
	if (hour !== null && !daylight(hour)) return [];
	const art = ['  \\ | /', '-- (   ) --', '  / | \\'];
	if (hour === null) return l.coast >= l.cols ? [] : art.map((ch, j) => ({ x: l.coast - 22, y: l.skyTop + j, ch }));
	const arc = (hour - 6) / 13;
	const band = l.surface - l.skyTop - 6;
	const x = 1 + arc * Math.max(0, l.coast - 14);
	const y = l.skyTop + (1 - Math.sin(Math.PI * arc)) * band;
	return art.map((ch, j) => ({ x, y: y + j, ch }));
}

export function moon(l: Layout, hour: number | null): Glyph[] {
	if (hour === null || daylight(hour) || l.surface - l.skyTop < 7) return [];
	const night = (hour - 19 + 24) % 24 / 11;
	const band = l.surface - l.skyTop - 7;
	const x = 1 + night * Math.max(0, l.coast - 10);
	const y = l.skyTop + (1 - Math.sin(Math.PI * Math.min(1, night))) * band;
	return ['  .-\'', ' /', ' \\', '  \'-.'].map((ch, j) => ({ x, y: y + j, ch }));
}

export function stars(t: number, l: Layout, hour: number | null): Glyph[] {
	if (hour === null || daylight(hour)) return [];
	const band = l.surface - l.skyTop - 3;
	if (band < 2) return [];
	return Array.from({ length: Math.round(l.cols / 5) }, (_, i) => {
		const twinkle = Math.sin(t * (0.8 + hash(i, 9)) + i * 7.3);
		return { x: Math.floor(hash(i, 5) * l.cols), y: l.skyTop + Math.floor(hash(i, 6) * band), ch: twinkle > 0.75 ? '+' : twinkle > -0.2 ? '.' : ' ' };
	}).filter((g) => g.ch !== ' ');
}

export function sailboat(t: number, l: Layout): Glyph[] {
	const x = wrap(l.coast * 0.7 - t * 0.3, l.coast + 12) - 8;
	const bob = Math.sin(t * 1.8) * 0.15;
	return ['   |\\', '   |_\\', '\\_____/'].map((ch, j) => ({ x, y: l.surface - 3 + j + bob, ch }));
}

export function birds(t: number, l: Layout): Glyph[] {
	const lead = wrap(t * 4.5, l.cols + 40) - 20;
	const line = [0, 1, 2, 3].map((i) => ({
		x: lead - i * 4,
		y: l.surface - 4.5 + i * 0.6 + Math.sin(t * 1.3 + i) * 0.2,
		ch: Math.sin(t * 3 - i * 0.8) > 0.3 ? '~v~' : '-v-'
	}));
	const gulls = [0, 1, 2].map((i) => ({
		x: wrap(t * (1.6 + i * 0.5) + i * 37, l.cols),
		y: l.skyTop + 0.5 + i * 1.2 + Math.sin(t * 0.8 + i * 2) * 0.5,
		ch: Math.sin(t * 5 + i * 2) > 0 ? 'v' : '-'
	}));
	return [...line, ...gulls];
}

export function bubbles(t: number, l: Layout): Glyph[] {
	const depth = l.rows - l.surface - 1;
	return Array.from({ length: Math.round(l.cols / 9) }, (_, i) => {
		const x0 = 2 + hash(i, 2) * (l.coast - 6);
		const y = l.rows - 1 - ((t * (1 + hash(i, 1) * 1.4) + hash(i, 3) * depth) % depth);
		return { x: x0 + Math.sin(t * 2 + i) * 0.6, y, ch: y < l.surface + 4 ? '.' : 'o' };
	}).filter((g) => g.x < shelfX(g.y, l) - 1);
}

export function beacon(t: number, l: Layout): Glyph[] {
	if (l.coast >= l.cols) return [];
	const lx = l.cols - 6;
	const top = l.surface - cliffHeight(lx, l) - 5;
	const sweep = wrap(t / 4, 1);
	if (sweep > 0.25) return [{ x: lx, y: top, ch: '*' }];
	const len = 4 + Math.round(sweep * 28);
	return [
		{ x: lx, y: top, ch: '*' },
		{ x: lx - len - 1, y: top + 0.1, ch: '- '.repeat(Math.ceil(len / 2)) }
	];
}