/* valley.js — the watercolour valley map.
* Every place on the map is a step in a run. Trails are drawn from the coded runs (engine.js):
* thickness = riders who walked it, colour = share of those riders who reached the chosen outcome.
* Dashed trails are routes the community has described but the form does not record. */
(function () {
"use strict";
const E = window.ThreshEngine;
const $ = (q, el = document) => el.querySelector(q);
const $$ = (q, el = document) => [...el.querySelectorAll(q)];
const NS = "http://www.w3.org/2000/svg";
const cssVar = (n) => getComputedStyle(document.documentElement).getPropertyValue(n).trim();
const esc = (x) => String(x ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));
const pct = (p, d = 0) => (p === null || p === undefined || isNaN(p) ? "–" : (p * 100).toFixed(d) + "%");
const DV = { Red: "--dr-red", Green: "--dr-green", Orange: "--dr-orange", Brown: "--dr-brown", Blue: "--dr-blue", Black: "--dr-black" };
// painted-map dragon inks (darker than the UI tones so they read on gold paper)
// chosen so every pair stays distinguishable under common colour-vision deficiencies (docs/COLOURS.md)
const INK = { Red: "#cc3346", Green: "#2f8f6f", Orange: "#e08a2a", Brown: "#4e3020", Blue: "#2a58b8", Black: "#22182a" };
const PAPER = { ink: "#3b2416", halo: "#f6e6c4", trailLow: "#8a6446", death: "#c4471c" };
function el(tag, attrs = {}, parent) {
const e = document.createElementNS(NS, tag);
for (const [k, v] of Object.entries(attrs)) if (v !== null && v !== undefined) e.setAttribute(k, v);
if (parent) parent.appendChild(e);
return e;
}
function rgb(c) {
c = (c || "").trim();
if (c.startsWith("#")) { const h = c.length === 4 ? [...c.slice(1)].map((x) => x + x).join("") : c.slice(1); return [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16)); }
const m = c.match(/\d+(\.\d+)?/g); return m ? m.slice(0, 3).map(Number) : [128, 128, 128];
}
const mix = (a, b, t) => { const A = rgb(a), B = rgb(b); return `rgb(${A.map((v, i) => Math.round(v + (B[i] - v) * t)).join(",")})`; };
function rng(seed) { let s = seed >>> 0; return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296); }
// ------------------------------------------------------------ places
// kind: start | way (a choice point) | dragon (a dragon is met here) | small (a choice made here)
const L = {
"comp:Alone": { x: 500, y: 736, name: "Alone", kind: "start", lab: "below", desc: "Enter the valley alone. The most common start, and the one that survives most often." },
"comp:1 Squadmate": { x: 380, y: 740, name: "One squadmate", kind: "start", lab: "below", desc: "Enter with one squadmate. You can later leave them and go on alone." },
"comp:2 Squadmates": { x: 620, y: 740, name: "Two squadmates", kind: "start", lab: "below", desc: "Enter with two squadmates. The first dragon is often deadlier for a group." },
"dir:West": { x: 190, y: 662, name: "West", sub: "toward the scent", kind: "way", lab: "left", desc: "Follow a scent west. A Brown, Green or Red finds you first." },
"dir:Center": { x: 500, y: 668, name: "Forest centre", kind: "way", lab: "right", desc: "Straight into the forest. Alone, a Red or a Brown finds you first." },
"dir:East": { x: 810, y: 662, name: "East", sub: "toward the roar", kind: "way", lab: "right", desc: "Toward the roar. Alone, a Red or an Orange finds you first. With one squadmate, a Green or an Orange." },
// first sightings (game roll)
"see:West:Red": { x: 110, y: 598, colour: "Red", kind: "dragon", first: true },
"see:West:Brown": { x: 185, y: 584, colour: "Brown", kind: "dragon", first: true },
"see:West:Green": { x: 262, y: 598, colour: "Green", kind: "dragon", first: true },
"see:Center:Red": { x: 448, y: 600, colour: "Red", kind: "dragon", first: true },
"see:Center:Brown": { x: 552, y: 600, colour: "Brown", kind: "dragon", first: true },
"see:East:Red": { x: 738, y: 598, colour: "Red", kind: "dragon", first: true },
"see:East:Orange": { x: 815, y: 584, colour: "Orange", kind: "dragon", first: true },
"see:East:Green": { x: 892, y: 598, colour: "Green", kind: "dragon", first: true },
scream: { x: 500, y: 512, name: "The scream", kind: "way", lab: "right", desc: "A scream in the trees. Help the squadmate fighting two cadets, walk away, or ignore it." },
bully: { x: 318, y: 462, name: "The bully fight", kind: "way", lab: "left", desc: "A squadmate is cornered by two cadets. Assess, even the numbers, or put yourself in the middle." },
"res:Killed the bullies yourself": { x: 222, y: 410, name: "Kill them", kind: "small", lab: "left", desc: "End the bullies yourself." },
"res:Squadmate's revenge": { x: 312, y: 398, name: "Squadmate's revenge", kind: "small", lab: "above", desc: "Let your squadmate take revenge." },
"res:Left bullies to the dragons": { x: 404, y: 410, name: "Dragons decide", kind: "small", lab: "right", desc: "Leave the bullies to the dragons." },
away: { x: 500, y: 440, name: "Walked away", kind: "small", lab: "right", desc: "Investigated the scream, then left them to it." },
wounded: { x: 684, y: 462, name: "The wounded cadet", kind: "way", lab: "right", desc: "Ignore the scream and you trip over a cadet missing a leg." },
"res:Tourniquet": { x: 600, y: 410, name: "Tourniquet", kind: "small", lab: "left", desc: "Use their belt as a tourniquet." },
"res:Left injured cadet": { x: 690, y: 398, name: "Leave them", kind: "small", lab: "above", desc: "Leave them to die." },
"res:Took their weapons": { x: 780, y: 410, name: "Take their weapons", kind: "small", lab: "right", desc: "Take their weapons and go." },
"fork:West": { x: 196, y: 336, name: "West fork", kind: "way", lab: "left", desc: "After the fight, the path splits: west." },
"fork:Forest": { x: 318, y: 318, name: "Deeper in", kind: "way", lab: "above", desc: "After the fight: deeper into the forest. Most recorded shadows came after this choice." },
"fork:East": { x: 432, y: 336, name: "East fork", kind: "way", lab: "right", desc: "After the fight, the path splits: east." },
hour: { x: 520, y: 272, name: "An hour's walk", sub: "the thorn", kind: "way", lab: "right", desc: "An hour of walking, and a thorn draws blood. No dragon bonds before this point." },
tree: { x: 316, y: 204, name: "The tallest tree", kind: "way", lab: "right", desc: "Climb for a view: a Red below, two Greens in a clearing and, very rarely, a shadow moving west." },
valley: { x: 694, y: 204, name: "Valley centre", kind: "way", lab: "left", desc: "Walk on to the valley centre." },
treeRed: { x: 168, y: 250, name: "The Red below", colour: "Red", kind: "dragon", lab: "left", desc: "Descend quickly onto a Red prowling under the tree. The hardest bond in the game." },
greens: { x: 124, y: 140, name: "Two Greens", colour: "Green", kind: "dragon", lab: "below", desc: "Two Greens in a clearing test you. A wrong answer sometimes brings an Orange or Brown to the rescue." },
stream: { x: 372, y: 142, name: "The shadow", sub: "a stream, a claw print", kind: "way", lab: "right", desc: "The rare third option at the tree. Follow the shadow west, past a stream and a claw print." },
redwood: { x: 452, y: 84, name: "Blue at the redwood", colour: "Blue", kind: "dragon", lab: "above", desc: "A Blue coiled around a redwood. Hold still and let it pass to reach the Black." },
black: { x: 262, y: 70, name: "Behind you", colour: "Black", kind: "dragon", lab: "left", desc: "A branch snaps behind you. Turn to face the threat." },
valleyRed: { x: 846, y: 250, name: "The valley Red", colour: "Red", kind: "dragon", lab: "below", desc: "A Red that has just incinerated another cadet. Most who choose to bond it succeed. Say it isn't your dragon to walk on to the meadow." },
meadow: { x: 742, y: 146, name: "The meadow", kind: "way", lab: "below", desc: "A meadow where an Orange and a Brown wait. Sometimes a Blue joins them." },
"meadow:Orange": { x: 652, y: 100, name: "Orange", colour: "Orange", kind: "dragon", lab: "left", desc: "The meadow Orange." },
"meadow:Brown": { x: 744, y: 80, name: "Brown", colour: "Brown", kind: "dragon", lab: "above", desc: "The meadow Brown." },
"meadow:Blue": { x: 836, y: 100, name: "Meadow Blue", colour: "Blue", kind: "dragon", lab: "right", desc: "The rare meadow Blue. Most riders who approach it are burned, even with the community's best answers." },
};
// routes described by the community that the form does not record
const KNOWN = [
["treeRed", "greens", "Wait and pursue the Green"],
["redwood", "greens", "Retreat, then find the Green"],
["redwood", "valley", "Retreat toward the valley"],
["comp:1 Squadmate", "comp:Alone", "Leave your squadmate"],
["comp:2 Squadmates", "comp:Alone", "Leave your squadmates"],
];
const SIGHT_NODES = new Set(Object.keys(L).filter((k) => k.startsWith("see:")));
function path(r) {
const s = [];
if (L["comp:" + r.company]) s.push("comp:" + r.company);
if (!["East", "West", "Center"].includes(r.direction)) return s;
s.push("dir:" + r.direction);
const sight = `see:${r.direction}:${r.first_sighting}`;
if (SIGHT_NODES.has(sight)) s.push(sight);
if (r.scream === "DIED at first sighting" || r.scream === "Unknown") return s;
s.push("scream");
if (r.scream === "Investigate & help") {
s.push("bully");
if (L["res:" + r.resolution]) s.push("res:" + r.resolution);
if (r.resolution.startsWith("DIED")) return s;
if (["Forest", "West", "East"].includes(r.post_path)) s.push("fork:" + r.post_path);
} else if (r.scream === "Ignore the scream") {
s.push("wounded");
if (L["res:" + r.resolution]) s.push("res:" + r.resolution);
} else if (r.scream === "Investigate & leave") s.push("away");
if (r.hour !== "Tree" && r.hour !== "Valley") return s;
s.push("hour");
if (r.hour === "Tree") {
s.push("tree");
if (r.branch === "Descend quickly: Red") s.push("treeRed");
else if (r.branch === "Descend slowly: two Greens") s.push("greens");
else if (r.branch === "Shadow (rare)") { s.push("stream"); if (r.enc_Blue || r.enc_Black) s.push("redwood"); if (r.enc_Black) s.push("black"); }
} else {
s.push("valley");
if (r.branch === "Valley Red") s.push("valleyRed");
else if (r.branch === "Passed the Red: meadow") {
s.push("meadow");
const tried = r.enc_Blue ? "Blue" : (r.outcome_color === "Orange" || r.outcome_color === "Brown") ? r.outcome_color : r.enc_Orange ? "Orange" : r.enc_Brown ? "Brown" : null;
if (tried) s.push("meadow:" + tried);
}
}
return s;
}
// ------------------------------------------------------------ painting
function defs(svg) {
const d = el("defs", {}, svg);
d.innerHTML = `
`;
}
function blob(g, x, y, r, fill, op, R) {
// irregular canopy blob from a few overlapping circles
for (let i = 0; i < 3; i++) el("circle", { cx: x + (R() - 0.5) * r * 0.9, cy: y + (R() - 0.5) * r * 0.6, r: r * (0.6 + R() * 0.45), fill, "fill-opacity": op }, g);
}
function paint(svg) {
const R = rng(21);
el("rect", { x: 0, y: 0, width: 1000, height: 780, fill: "url(#vm-sky)" }, svg);
el("rect", { x: 0, y: 0, width: 1000, height: 780, fill: "url(#vm-sun)" }, svg);
// far ridge and hazy hills
const far = el("g", { filter: "url(#vm-wash)" }, svg);
let d = "M-20 120";
for (let x = -20; x <= 1020; x += 60) d += ` Q${x + 30} ${40 + R() * 40} ${x + 60} ${90 + R() * 40}`;
el("path", { d: d + " L1020 -10 L-20 -10 Z", fill: "#c99a72", "fill-opacity": 0.55 }, far);
el("path", { d: "M-20 150 Q200 90 420 140 T820 120 T1020 150 L1020 200 L-20 200 Z", fill: "#d4a677", "fill-opacity": 0.45 }, far);
// forest: three depth tiers of canopy washes, kept out of the clearings
const clear = [[742, 100, 150, 85], [124, 140, 92, 58], [694, 205, 80, 44], [520, 272, 70, 32], [846, 250, 70, 40], [500, 740, 190, 46], [318, 455, 120, 60], [684, 455, 120, 60], [500, 660, 80, 30]];
const inClear = (x, y) => clear.some(([cx, cy, rx, ry]) => ((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 < 1);
const tiers = [
{ y0: 120, y1: 330, n: 150, r: [10, 20], cols: ["#b39a63", "#a99158"], op: 0.32 },
{ y0: 300, y1: 560, n: 170, r: [12, 24], cols: ["#8e8a54", "#7f7e4b"], op: 0.4 },
{ y0: 520, y1: 780, n: 170, r: [14, 30], cols: ["#6c6b40", "#5b5c37", "#6b5a35"], op: 0.45 },
];
tiers.forEach((t, ti) => {
const g = el("g", { filter: "url(#vm-wash)" }, svg);
// tall pale trunks, like a sunlit wood
for (let i = 0; i < t.n * 0.25; i++) {
const x = R() * 1000, y = t.y0 + R() * (t.y1 - t.y0);
if (inClear(x, y)) continue;
el("rect", { x, y: y - 50 - ti * 10, width: 2 + ti * 1.5, height: 60 + ti * 14, fill: "#7a5c3c", "fill-opacity": 0.18 + ti * 0.07 }, g);
}
for (let i = 0; i < t.n; i++) {
const x = R() * 1000, y = t.y0 + R() * (t.y1 - t.y0);
if (inClear(x, y)) continue;
blob(g, x, y, t.r[0] + R() * (t.r[1] - t.r[0]), t.cols[Math.floor(R() * t.cols.length)], t.op, R);
}
});
// clearings: warm ochre light
const cg = el("g", { filter: "url(#vm-wash)" }, svg);
clear.forEach(([cx, cy, rx, ry]) => el("ellipse", { cx, cy, rx: rx * 0.85, ry: ry * 0.8, fill: "#f0cf93", "fill-opacity": 0.55 }, cg));
// mist bands
const mg = el("g", { filter: "url(#vm-haze)" }, svg);
[[230, 0.35], [380, 0.2], [560, 0.18]].forEach(([y, o]) => el("rect", { x: -40, y, width: 1080, height: 26, fill: "#fbe9c4", "fill-opacity": o }, mg));
// the stream
const sg = el("g", { filter: "url(#vm-wash2)" }, svg);
const stream = "M560 -10 C 520 40, 470 70, 420 112 S 330 170, 270 150 S 170 120, 110 220 S 40 320, -10 350";
el("path", { d: stream, fill: "none", stroke: "#7fa3a6", "stroke-width": 13, "stroke-opacity": 0.65, "stroke-linecap": "round" }, sg);
el("path", { d: stream, fill: "none", stroke: "#cfe1dc", "stroke-width": 3, "stroke-opacity": 0.6, "stroke-linecap": "round" }, sg);
// rock outcrop for the valley Red, standing stones by the meadow
const rk = el("g", { filter: "url(#vm-wash2)" }, svg);
el("path", { d: "M800 272 L826 236 L862 230 L892 262 L880 284 L812 288 Z", fill: "#6e5c63", "fill-opacity": 0.75 }, rk);
el("path", { d: "M826 236 L862 230 L870 246 L834 252 Z", fill: "#9b8790", "fill-opacity": 0.6 }, rk);
// the tallest tree and the redwood
const big = el("g", { filter: "url(#vm-wash2)" }, svg);
el("rect", { x: 312, y: 150, width: 9, height: 70, fill: "#5a3f28", "fill-opacity": 0.85 }, big);
[[316, 150, 30], [302, 172, 22], [332, 168, 22], [316, 124, 22]].forEach(([x, y, r]) => el("circle", { cx: x, cy: y, r, fill: "#46502e", "fill-opacity": 0.75 }, big));
el("rect", { x: 446, y: 40, width: 11, height: 56, fill: "#8a4430", "fill-opacity": 0.85 }, big);
[[452, 40, 20], [440, 54, 14], [464, 54, 14]].forEach(([x, y, r]) => el("circle", { cx: x, cy: y, r, fill: "#3f4a2b", "fill-opacity": 0.7 }, big));
}
function overlay(svg) {
el("rect", { x: 0, y: 0, width: 1000, height: 780, filter: "url(#vm-paper)", opacity: 0.45, style: "mix-blend-mode:multiply", "pointer-events": "none" }, svg);
el("rect", { x: 0, y: 0, width: 1000, height: 780, fill: "url(#vm-vignette)", "pointer-events": "none" }, svg);
}
// tiny original winged glyph for dragon places
const WING = "M50 30 L18 6 L27 21 L3 16 L25 32 Z M50 30 L82 6 L73 21 L97 16 L75 32 Z M37 30 Q50 23 63 30 Q50 37 37 30 Z M63 30 L76 26 L80 28 L73 31 Z M37 30 Q24 37 10 34 Q22 33 36 31 Z";
// ------------------------------------------------------------ rendering
let target = "bond_Blue", selected = "meadow:Blue", lastState = null, tipEl = null;
const TGT = ["bond_Blue", "bond_Black", "see_Blue", "see_shadow", "bond_Red", "bond_Green", "bond_Orange", "bond_Brown", "bond_any"];
function tip(e, html) {
tipEl = tipEl || $("#tip");
tipEl.innerHTML = html; tipEl.hidden = false;
const r = tipEl.getBoundingClientRect();
tipEl.style.left = Math.min(window.innerWidth - r.width - 8, e.clientX + 14) + "px";
tipEl.style.top = Math.min(window.innerHeight - r.height - 8, e.clientY + 14) + "px";
}
const hideTip = () => { if (tipEl) tipEl.hidden = true; };
function renderTargets() {
$("#valley-targets").innerHTML = TGT.map((t) => {
const c = E.TARGETS[t].color ? DV[E.TARGETS[t].color] : "--dr-any";
return ` ${esc(E.TARGETS[t].label)} `;
}).join("");
$$("#valley-targets .tchip").forEach((b) => b.addEventListener("click", () => { target = b.dataset.vt; render(lastState); }));
}
function curve(A, B, bend) {
const mx = (A.x + B.x) / 2, my = (A.y + B.y) / 2, dx = B.x - A.x, dy = B.y - A.y;
return `M${A.x} ${A.y} Q${mx - dy * bend} ${my + dx * bend} ${B.x} ${B.y}`;
}
const isDeath = (r) => r.outcome === "Incinerated" || r.outcome === "Died before the hour";
function render(state) {
if (!state || !state.res || !$("#valley")) return;
lastState = state;
renderTargets();
const svg = $("#valley");
svg.setAttribute("viewBox", "0 0 1000 790");
svg.innerHTML = "";
defs(svg); paint(svg);
const T = E.TARGETS[target].fn;
const nodes = new Map(), edges = new Map();
state.runs.forEach((r) => {
const seq = path(r), hit = T(r) ? 1 : 0;
seq.forEach((id, i) => {
const n = nodes.get(id) || { id, n: 0, k: 0, runs: [], endDeaths: 0 };
n.n++; n.k += hit; n.runs.push(r);
if (i === seq.length - 1 && isDeath(r)) n.endDeaths++;
nodes.set(id, n);
if (i) { const key = seq[i - 1] + ">" + id; const e = edges.get(key) || { a: seq[i - 1], b: id, n: 0, k: 0 }; e.n++; e.k += hit; edges.set(key, e); }
});
});
const minN = state.minN || 10;
const ES = [...edges.values()];
const maxN = Math.max(1, ...ES.map((e) => e.n));
const pmax = Math.max(0.005, ...ES.filter((e) => e.n >= minN).map((e) => e.k / e.n));
const tcol = E.TARGETS[target].color ? INK[E.TARGETS[target].color] : "#8a3f6e";
$(".valley-shell").style.setProperty("--vt", cssVar(E.TARGETS[target].color ? DV[E.TARGETS[target].color] : "--dr-any"));
const colourOf = (e) => (e.n < minN ? "#a58a70" : mix(PAPER.trailLow, tcol, Math.pow(Math.min(1, e.k / e.n / pmax), 0.7)));
// known-but-untracked routes
const gK = el("g", { "pointer-events": "stroke" }, svg);
KNOWN.forEach(([a, b, label]) => {
const A = L[a], B = L[b];
const p = el("path", { d: curve(A, B, 0.18), fill: "none", stroke: PAPER.ink, "stroke-opacity": 0.45, "stroke-width": 2, "stroke-dasharray": "2 7", "stroke-linecap": "round" }, gK);
p.addEventListener("mousemove", (ev) => tip(ev, `
${esc(label)}
A route the community has described. The form doesn't record it, so it has no numbers.`));
p.addEventListener("mouseleave", hideTip);
});
// trails
const gT = el("g", { filter: "url(#vm-brush)" }, svg);
ES.sort((a, b) => b.n - a.n).forEach((e) => {
const A = L[e.a], B = L[e.b];
if (!A || !B) return;
const w = 2 + 24 * Math.sqrt(e.n / maxN);
const d = curve(A, B, 0.08);
el("path", { d, fill: "none", stroke: PAPER.halo, "stroke-width": w + 3, "stroke-opacity": 0.45, "stroke-linecap": "round" }, gT);
const p = el("path", { d, fill: "none", stroke: colourOf(e), "stroke-width": w, "stroke-opacity": e.n >= minN ? 0.85 : 0.45, "stroke-linecap": "round" }, gT);
p.addEventListener("mousemove", (ev) => tip(ev, `${esc(name(e.a))} → ${esc(name(e.b))}
${e.n.toLocaleString()} riders ${esc(E.TARGETS[target].label)}: ${pct(e.k / e.n, 1)} (${e.k}/${e.n})${e.n < minN ? "Too few riders to read " : ""}`));
p.addEventListener("mouseleave", hideTip);
});
// places
const gN = el("g", {}, svg);
const total = Math.max(1, ...[...nodes.values()].map((n) => n.n));
Object.entries(L).forEach(([id, P]) => {
const nd = nodes.get(id) || { n: 0, k: 0, runs: [], endDeaths: 0 };
const g = el("g", { class: "loc", tabindex: 0, role: "button", "aria-label": `${name(id)}: ${nd.n} riders` }, gN);
const sel = id === selected;
if (P.kind === "dragon") {
const r = 11 + 9 * Math.sqrt(nd.n / total);
el("circle", { cx: P.x, cy: P.y, r: r + 4, fill: PAPER.halo, "fill-opacity": 0.85, stroke: sel ? "#1d0a12" : INK[P.colour], "stroke-width": sel ? 3 : 1.6 }, g);
el("circle", { cx: P.x, cy: P.y, r, fill: "#fbf0d6", stroke: INK[P.colour], "stroke-width": 2.2 }, g);
el("path", { d: WING, fill: INK[P.colour], transform: `translate(${P.x - r * 0.82} ${P.y - r * 0.5}) scale(${(r * 1.64) / 100})` }, g);
} else if (P.kind === "start") {
el("path", { d: `M${P.x} ${P.y - 11} L${P.x + 9} ${P.y} L${P.x} ${P.y + 11} L${P.x - 9} ${P.y} Z`, fill: sel ? "#1d0a12" : PAPER.ink, stroke: PAPER.halo, "stroke-width": 2 }, g);
} else {
const r = P.kind === "small" ? 5 : 7 + 5 * Math.sqrt(nd.n / total);
el("circle", { cx: P.x, cy: P.y, r: r + 2.5, fill: PAPER.halo, stroke: sel ? "#1d0a12" : "none", "stroke-width": 2.5 }, g);
el("circle", { cx: P.x, cy: P.y, r, fill: P.kind === "small" ? "#7a5236" : PAPER.ink }, g);
}
// deaths that ended here
if (nd.endDeaths) {
const bx = P.x + (P.kind === "dragon" ? 18 : 10), by = P.y - (P.kind === "dragon" ? 16 : 10);
el("circle", { cx: bx, cy: by, r: 8.5, fill: PAPER.death, stroke: PAPER.halo, "stroke-width": 1.4 }, g);
const t = el("text", { x: bx, y: by + 3.2, "text-anchor": "middle", "font-size": 8.5, fill: "#fff3e0", "font-family": "var(--font-data)" }, g);
t.textContent = nd.endDeaths > 999 ? Math.round(nd.endDeaths / 100) / 10 + "k" : nd.endDeaths;
}
// labels (first sightings are labelled by their dragon glyph and tooltip only)
if (!P.first) {
const pos = P.lab || "right", off = P.kind === "dragon" ? 24 : 12;
let x = P.x, y = P.y + 4, anchor = "middle";
if (pos === "right") { x = P.x + off; anchor = "start"; }
else if (pos === "left") { x = P.x - off; anchor = "end"; }
else if (pos === "below") y = P.y + off + 12;
else if (pos === "above") y = P.y - off - 18;
const t1 = el("text", { x, y, "text-anchor": anchor, "font-family": "var(--font-display)", "font-size": P.kind === "small" ? 12.5 : 15, fill: PAPER.ink, "paint-order": "stroke", stroke: PAPER.halo, "stroke-width": 3.5, "stroke-linejoin": "round" }, g);
t1.textContent = P.name;
if (P.kind !== "small") {
const t2 = el("text", { x, y: y + 14, "text-anchor": anchor, "font-size": 11, fill: PAPER.ink, "fill-opacity": 0.85, "paint-order": "stroke", stroke: PAPER.halo, "stroke-width": 3, "font-family": "var(--font-body)" }, g);
t2.textContent = `${nd.n.toLocaleString()} riders${P.sub ? " · " + P.sub : ""}`;
}
}
const pick = () => { selected = id; render(state); };
g.addEventListener("click", pick);
g.addEventListener("keydown", (ev) => { if (ev.key === "Enter" || ev.key === " ") { ev.preventDefault(); pick(); } });
g.addEventListener("mousemove", (ev) => tip(ev, `${esc(name(id))}
${nd.n.toLocaleString()} riders${nd.endDeaths ? ` · ${nd.endDeaths} died here` : ""} ${esc(E.TARGETS[target].label)}: ${pct(nd.n ? nd.k / nd.n : null, 1)}`));
g.addEventListener("mouseleave", hideTip);
});
// sighting captions per direction
["West", "Center", "East"].forEach((dname) => {
const xs = Object.keys(L).filter((k) => k.startsWith(`see:${dname}:`)).map((k) => L[k]);
const cx = xs.reduce((a, p) => a + p.x, 0) / xs.length, cy = Math.min(...xs.map((p) => p.y)) - 26;
const t = el("text", { x: cx, y: cy, "text-anchor": "middle", "font-size": 10.5, "letter-spacing": "0.12em", fill: PAPER.ink, "fill-opacity": 0.8, "font-family": "var(--font-label)", "paint-order": "stroke", stroke: PAPER.halo, "stroke-width": 3 }, svg);
t.textContent = "FIRST DRAGON · ROLL";
});
// compass
const cr = el("g", { transform: "translate(52 726)", "aria-hidden": "true" }, svg);
el("circle", { r: 24, fill: PAPER.halo, "fill-opacity": 0.7, stroke: PAPER.ink, "stroke-opacity": 0.5 }, cr);
el("path", { d: "M0 -20 L5 0 L0 20 L-5 0 Z", fill: PAPER.ink }, cr);
const nT = el("text", { y: -28, "text-anchor": "middle", "font-size": 11, fill: PAPER.ink, "font-family": "var(--font-display)" }, cr); nT.textContent = "N";
overlay(svg);
renderCard(nodes, edges);
$("#valley-legend").innerHTML = `Trail width = riders who walked it
0% ${pct(pmax)}
riders who died at that spot
Dotted = described by the community, not tracked by the form
Faded = fewer than ${minN} riders · positions are illustrative `;
}
function name(id) {
const P = L[id];
if (!P) return id;
if (P.first) { const [, d, c] = id.split(":"); return `${c} dragon (first sighting, ${d === "Center" ? "forest centre" : d.toLowerCase()})`; }
return P.name;
}
function renderCard(nodes, edges) {
const P = L[selected], nd = nodes.get(selected) || { n: 0, k: 0, runs: [], endDeaths: 0 };
const card = $("#loc-card");
card.style.setProperty("--lc", P.colour ? cssVar(DV[P.colour]) : cssVar("--accent"));
const w = E.wilson(nd.k, nd.n);
const out = [...edges.values()].filter((e) => e.a === selected).sort((a, b) => b.n - a.n);
const outN = out.reduce((a, e) => a + e.n, 0);
const ends = {};
nd.runs.forEach((r) => {
const k = r.outcome === "Bonded" ? "Bonded " + r.outcome_color : r.outcome === "Incinerated" ? "Incinerated" : r.outcome === "Died before the hour" ? "Died before the hour" : "Not recorded";
ends[k] = (ends[k] || 0) + 1;
});
const order = ["Bonded Red", "Bonded Orange", "Bonded Brown", "Bonded Green", "Bonded Blue", "Bonded Black", "Incinerated", "Died before the hour", "Not recorded"];
const bar = (label, n, of, c) => `${esc(label)} ${n} · ${pct(n / Math.max(1, nd.n))}
`;
const maxEnd = Math.max(1, ...Object.values(ends));
const P2 = P.first ? { desc: "The game rolls which dragon finds you first. Lower your eyes and keep still." } : P;
card.innerHTML = `${P.kind === "dragon" ? "A dragon waits here" : "Selected place"} ${esc(name(selected))}
${esc(P2.desc || "")}
${nd.n.toLocaleString()} logged runs reached this spot${nd.endDeaths ? `, and ${nd.endDeaths} died here` : ""}. ${esc(E.TARGETS[target].label)}: ${pct(w.p, 1)} of them (95% CI ${pct(w.lo, 1)}–${pct(w.hi, 1)}).
${out.length ? `Where they went next ${out.map((e) => bar(name(e.b).replace(/ \(first sighting.*\)/, " first"), e.n, out[0].n, L[e.b].colour ? `var(${DV[L[e.b].colour]})` : "var(--accent)")).join("")}
` : ""}
How those runs ended ${order.filter((k) => ends[k]).map((k) => bar(k, ends[k], maxEnd, k.startsWith("Bonded") ? `var(${DV[k.split(" ")[1]]})` : k === "Incinerated" ? "var(--ember)" : "var(--faint)")).join("") || 'No runs. '}
`;
}
// shared with the route planner (planner.js), which paints the same valley and draws the solver's options on it
const paintBase = (svg) => { defs(svg); paint(svg); };
window.ValleyMap = { render, path, places: L, paintBase, finish: overlay, name, curve, INK, PAPER, WING };
})();