Route planner: the solver's options and odds drawn on the valley map
- New "Route Planner" tab: pick a dragon colour, a goal (only this dragon / best odds this run) and a data sample. The solver runs in the browser from policy.json and walks the valley: every decision shows each option's chance of bonding the colour (with the best play afterwards) and of reaching an encounter alive; every roll shows its probabilities; deaths show where they happen; "stays best in N% of re-runs" and "no clear winner" flag noisy recommendations. Click any option on the map or in the list to explore another branch. - solver.js: values now carry P(reaching an encounter alive); advisor.step() gives the planner the next decision, roll or outcome at any history. - valley.js shares its painting with the planner. - Fix: route-map target chips no longer capture clicks on other chip rows. - Pages build gets a doctype and charset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d2ffb84b5e
commit
bdf66afabc
+1
-1
@@ -217,7 +217,7 @@
|
||||
};
|
||||
$("#targets-rare").innerHTML = RARE_T.map((t) => chip(t, t.startsWith("bond"))).join("");
|
||||
$("#targets-other").innerHTML = OTHER_T.map((t) => chip(t, false)).join("");
|
||||
$$(".tchip").forEach((b) => b.addEventListener("click", () => { state.target = b.dataset.t; $("#best-routes").innerHTML = ""; renderTargets(); renderMap(); }));
|
||||
$$(".tchip[data-t]").forEach((b) => b.addEventListener("click", () => { state.target = b.dataset.t; $("#best-routes").innerHTML = ""; renderTargets(); renderMap(); }));
|
||||
}
|
||||
const ORDER = {
|
||||
company: ["Alone", "1 Squadmate", "2 Squadmates", "Unknown"],
|
||||
|
||||
@@ -311,6 +311,42 @@ td { border-bottom-color: color-mix(in srgb, var(--line) 70%, transparent); }
|
||||
.stop .n { font-family: var(--font-display); font-weight: 400; color: var(--accent); }
|
||||
.journey { background: color-mix(in srgb, var(--surface) 85%, #000); }
|
||||
footer { border-top-color: var(--line); }
|
||||
|
||||
/* route planner */
|
||||
.planner-controls { align-items: end; }
|
||||
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
|
||||
.seg button { border: 0; border-radius: 0; background: transparent; padding: 7px 13px; color: var(--muted); font-size: 0.72rem; }
|
||||
.seg button[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 22%, var(--raised)); color: var(--ink); }
|
||||
.pl-head { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); gap: 10px; }
|
||||
.pl-close { color: var(--warn); margin: 0 0 4px; }
|
||||
.pl-kpi { padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); display: grid; gap: 2px; align-content: start; }
|
||||
.pl-kpi b { font-family: var(--font-display); font-weight: 400; font-size: 1.7rem; color: var(--c, var(--ink)); line-height: 1.1; }
|
||||
.pl-grid { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(300px, 1fr); gap: 16px; align-items: start; }
|
||||
@media (max-width: 1050px) { .pl-grid { grid-template-columns: minmax(0, 1fr); } }
|
||||
.planner-shell { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--map-ground); --frame: var(--vt, var(--accent)); border-color: color-mix(in srgb, var(--frame) 60%, transparent); box-shadow: 0 0 0 4px var(--bg), 0 0 0 5px color-mix(in srgb, var(--frame) 40%, transparent), 0 0 30px color-mix(in srgb, var(--frame) 15%, transparent); }
|
||||
#planner-map { display: block; width: 100%; min-width: 760px; height: auto; background: #d9b07a; }
|
||||
.pl-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 10px 14px; border-top: 1px solid var(--line); background: var(--surface); }
|
||||
.pl-legend b { display: inline-block; width: 22px; height: 6px; border-radius: 3px; vertical-align: 2px; margin-right: 4px; }
|
||||
.pl-legend .sw-route { background: var(--vt, var(--accent)); box-shadow: 0 0 0 3px #fff1c955; }
|
||||
.pl-legend .sw-opt { background: repeating-linear-gradient(90deg, var(--vt, var(--accent)) 0 5px, transparent 5px 10px); opacity: 0.7; }
|
||||
.pl-legend .sw-roll { background: repeating-linear-gradient(90deg, var(--muted) 0 2px, transparent 2px 8px); }
|
||||
.pl-legend .sw-risk { width: 12px; height: 12px; border-radius: 50%; background: var(--ember); vertical-align: -2px; }
|
||||
.pl-steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; align-content: start; max-height: 920px; overflow: auto; }
|
||||
.pl-step { display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
|
||||
.pl-step.chance { background: color-mix(in srgb, var(--surface) 82%, #000); border-style: dashed; }
|
||||
.pl-step.end { border-color: color-mix(in srgb, var(--vt, var(--accent)) 50%, var(--line)); }
|
||||
.pl-n { font-family: var(--font-display); color: var(--accent); font-size: 1.1rem; }
|
||||
.pl-step h4 { font-family: var(--font-display); font-weight: 400; font-size: 1.02rem; margin: 0 0 6px; }
|
||||
.pl-row { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(36px, 1fr) minmax(64px, auto); gap: 8px; align-items: center; width: 100%; text-align: left; background: transparent; border: 0; border-radius: 6px; padding: 4px 6px; color: var(--ink); font-family: var(--font-body); font-size: var(--step--1); text-transform: none; letter-spacing: normal; }
|
||||
button.pl-row { cursor: pointer; }
|
||||
button.pl-row:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
|
||||
.pl-row.taken { background: color-mix(in srgb, var(--accent) 14%, transparent); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 45%, transparent); }
|
||||
.pl-row i { display: block; height: 8px; border-radius: 3px; background: var(--c); width: var(--w); min-width: 2px; }
|
||||
.pl-row .m { text-align: right; display: grid; }
|
||||
.pl-row .m b { font-family: var(--font-data); font-weight: 600; }
|
||||
.pl-row .m small { color: var(--muted); font-family: var(--font-data); font-size: 10.5px; white-space: nowrap; }
|
||||
.pl-row .rec { font-style: normal; font-size: 9.5px; font-family: var(--font-label); letter-spacing: 0.1em; text-transform: uppercase; color: var(--bg); background: var(--accent); border-radius: 999px; padding: 1px 6px; margin-left: 4px; vertical-align: 1px; }
|
||||
.pl-risk { color: var(--ember); margin: 0 0 4px; }
|
||||
</style>
|
||||
|
||||
<header class="hero">
|
||||
@@ -326,6 +362,7 @@ footer { border-top-color: var(--line); }
|
||||
</div>
|
||||
<nav class="tabs" role="tablist" aria-label="Atlas pages">
|
||||
<button class="tab" role="tab" id="tab-overview" aria-controls="panel-overview" aria-selected="true">The Threshing</button>
|
||||
<button class="tab" role="tab" id="tab-planner" aria-controls="panel-planner" aria-selected="false">Route Planner</button>
|
||||
<button class="tab" role="tab" id="tab-valley" aria-controls="panel-valley" aria-selected="false">Valley Map</button>
|
||||
<button class="tab" role="tab" id="tab-flow" aria-controls="panel-flow" aria-selected="false">Route Flow</button>
|
||||
<button class="tab" role="tab" id="tab-rare" aria-controls="panel-rare" aria-selected="false">Blue & Black</button>
|
||||
@@ -362,6 +399,38 @@ footer { border-top-color: var(--line); }
|
||||
<div class="cta"><button type="button" class="primary" data-go="valley">Walk the valley map</button><button type="button" data-go="rare">Chasing a Blue or Black?</button><button type="button" data-go="dragons">Meet the bonded dragons</button></div>
|
||||
</section>
|
||||
</div>
|
||||
<div class="tabpanel" id="panel-planner" role="tabpanel" aria-labelledby="tab-planner" hidden>
|
||||
<section id="planner-sec" aria-labelledby="h-planner">
|
||||
<div class="section-head">
|
||||
<span class="eyebrow">Interactive · the best route for each dragon, worked out from every logged run</span>
|
||||
<h2 id="h-planner">Plan your run</h2>
|
||||
<p class="muted small">Pick the dragon you want. The planner walks the valley with you: at every choice it shows each option's chance of bonding that dragon if you take it and then play the best route afterwards. At every roll of the dice it shows what can happen. Click any option, on the map or in the list, to see where another choice leads.</p>
|
||||
</div>
|
||||
<div class="controls planner-controls">
|
||||
<div class="target-group"><span class="lbl small muted">I want</span><div class="targets" id="planner-colours"></div></div>
|
||||
<div class="target-group"><span class="lbl small muted">Goal</span>
|
||||
<div class="seg" id="planner-obj" role="group" aria-label="Goal">
|
||||
<button type="button" data-o="eventually" aria-pressed="true" title="You will die on purpose and try again whenever the run goes wrong, so never risk ending on another dragon">Only this dragon, ever</button>
|
||||
<button type="button" data-o="thisAttempt" aria-pressed="false" title="Maximise the chance on this one run, even if that risks a different dragon">Best odds this run</button>
|
||||
</div></div>
|
||||
<div class="target-group"><span class="lbl small muted">Data</span>
|
||||
<div class="seg" id="planner-sample" role="group" aria-label="Data">
|
||||
<button type="button" data-s="all" aria-pressed="true" title="Every logged run">All runs</button>
|
||||
<button type="button" data-s="lowBias" aria-pressed="false" title="Only riders who logged every run, and riders not chasing a rare dragon (less selective reporting)">Low-bias</button>
|
||||
</div></div>
|
||||
<button type="button" id="planner-reset">Start again</button>
|
||||
</div>
|
||||
<div class="pl-head" id="planner-head"></div>
|
||||
<div class="pl-grid">
|
||||
<div class="planner-shell deco">
|
||||
<div class="valley-wrap"><svg id="planner-map" viewBox="0 0 1000 790" role="img" aria-label="Valley map with the recommended route and the chance attached to every option"></svg></div>
|
||||
<div class="legend small pl-legend"><span><b class="sw-route"></b> recommended route</span><span><b class="sw-opt"></b> other options, with the chance of your dragon if you take them</span><span><b class="sw-roll"></b> a roll of the dice</span><span><b class="sw-risk"></b> chance of being killed there</span></div>
|
||||
</div>
|
||||
<ol class="pl-steps" id="planner-steps" aria-label="Your run, step by step"></ol>
|
||||
</div>
|
||||
<p class="small muted">How this is worked out: every choice the community form records is a decision; which dragon appears, whether the shadow shows and who dies are rolls. Each roll's odds are allowed to depend on earlier choices, as much as the data support and no more (see Methods and <code>docs/POLICY.md</code>). "Alive" is the chance of reaching a bonding encounter at all. The bond chance inside each encounter is the community average, because the form does not record the answers given there.</p>
|
||||
</section>
|
||||
</div>
|
||||
<div class="tabpanel" id="panel-valley" role="tabpanel" aria-labelledby="tab-valley" hidden>
|
||||
<section id="valley-sec" aria-labelledby="h-valley">
|
||||
<div class="section-head">
|
||||
@@ -586,7 +655,9 @@ adjusted OR = exp(βⱼ), 95% CI = exp(βⱼ ± 1.96·SEⱼ)</div>
|
||||
<div id="tip" hidden></div>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
|
||||
<script src="engine.js"></script>
|
||||
<script src="solver.js"></script>
|
||||
<script src="register.js"></script>
|
||||
<script src="app.js"></script>
|
||||
<script src="valley.js"></script>
|
||||
<script src="planner.js"></script>
|
||||
<script src="ui.js"></script>
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
/* planner.js — the route planner.
|
||||
* Pick a dragon colour and an objective; the solver (solver.js, re-run in the browser from data/policy.json)
|
||||
* walks the valley with you. At every decision it shows each option's chance of bonding that colour if you take it
|
||||
* and then play the best route afterwards, plus the chance of reaching a bonding encounter alive. At every chance
|
||||
* event it shows what can happen and how likely it is. Click any option or outcome to explore another branch. */
|
||||
(function () {
|
||||
"use strict";
|
||||
const S = window.ThreshSolver, V = window.ValleyMap;
|
||||
if (!S || !V) return;
|
||||
const $ = (q, el = document) => el.querySelector(q);
|
||||
const $$ = (q, el = document) => [...el.querySelectorAll(q)];
|
||||
const NS = "http://www.w3.org/2000/svg";
|
||||
const esc = (x) => String(x ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ }[c]));
|
||||
const pct = (p) => (p == null || isNaN(p) ? "–" : p === 0 ? "0%" : p < 0.001 ? "<0.1%" : (p * 100).toFixed(p < 0.1 ? 1 : 0) + "%");
|
||||
const DV = { Red: "--dr-red", Green: "--dr-green", Orange: "--dr-orange", Brown: "--dr-brown", Blue: "--dr-blue", Black: "--dr-black" };
|
||||
const COLOURS = ["Blue", "Black", "Red", "Green", "Orange", "Brown"];
|
||||
const L = V.places, INK = V.INK, PAPER = V.PAPER;
|
||||
const GATE = { x: 500, y: 786 };
|
||||
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;
|
||||
}
|
||||
|
||||
const st = { policy: null, colour: "Blue", objective: "eventually", sample: "all", h: {}, cache: new Map() };
|
||||
|
||||
function advisor() {
|
||||
const k = `${st.sample}|${st.colour}|${st.objective}`;
|
||||
if (!st.cache.has(k)) st.cache.set(k, S.advisor(st.policy.samples[st.sample], st.colour, st.objective));
|
||||
return st.cache.get(k);
|
||||
}
|
||||
|
||||
/* ---------- walking the plan ---------- */
|
||||
|
||||
// Where each history variable is drawn on the map.
|
||||
const placeOf = (v, val, h) => {
|
||||
switch (v) {
|
||||
case "company": return "comp:" + val;
|
||||
case "direction": return "dir:" + val;
|
||||
case "fs": return L[`see:${h.direction}:${val}`] ? `see:${h.direction}:${val}` : null;
|
||||
case "cadet": return val === "Left them to it" ? "away" : "res:" + val;
|
||||
case "post": return "fork:" + val;
|
||||
case "hour": return val === "Tree" ? "tree" : "valley";
|
||||
case "shadow": return val === "yes" ? "stream" : null;
|
||||
case "shadowColour": return val === "Black" ? "black" : "redwood";
|
||||
case "descend": return val.startsWith("Quickly") ? "treeRed" : "greens";
|
||||
case "valley": return val === "Face the Red" ? "valleyRed" : "meadow";
|
||||
case "meadow": return "meadow:" + val;
|
||||
default: return null;
|
||||
}
|
||||
};
|
||||
const HOLD = { cadet: "hour", post: "hour" }; // after these, the run walks on to the hour's walk
|
||||
|
||||
/** Default branch at a chance event: the one that can lead to the target colour, else the most likely. */
|
||||
function defaultOutcome(s) {
|
||||
const ent = Object.entries(s.probs).sort((a, b) => b[1] - a[1]);
|
||||
if (s.var === "meadow" || s.var === "shadowColour") { const hit = ent.find(([k]) => k === st.colour); if (hit) return hit[0]; }
|
||||
if (s.var === "shadow" && (st.colour === "Black" || st.colour === "Blue")) return "yes";
|
||||
return ent[0][0];
|
||||
}
|
||||
|
||||
/** Walk from the start: user's choices where given, otherwise the recommended choice / default outcome. */
|
||||
function walk() {
|
||||
const adv = advisor(), steps = [], h = {};
|
||||
const tree = st.policy.samples[st.sample].targets[st.colour][st.objective].tree;
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const s = adv.step(h);
|
||||
if (s.kind === "decision") {
|
||||
// on the recommended route the published tree knows how often this choice stayed best across posterior draws
|
||||
const node = S.locate(tree, h);
|
||||
if (node && node.type === "decision" && node.var === s.var && node.choice === s.choice) s.stillBest = node.stillBest;
|
||||
}
|
||||
steps.push({ ...s, before: { ...h } });
|
||||
if (s.kind === "end") break;
|
||||
const key = s.var === "encounter" ? "encounter" : s.var;
|
||||
const picked = st.h[key];
|
||||
const valid = s.kind === "decision" ? picked && s.options[picked] : picked && s.probs[picked] != null;
|
||||
const val = valid ? picked : s.kind === "decision" ? s.choice : defaultOutcome(s);
|
||||
steps[steps.length - 1].taken = val;
|
||||
steps[steps.length - 1].userSet = !!valid;
|
||||
h[key] = val;
|
||||
}
|
||||
return steps;
|
||||
}
|
||||
|
||||
/* ---------- drawing ---------- */
|
||||
|
||||
function anchor(steps, i) {
|
||||
// the place a step starts from: the place of the last earlier step that has one
|
||||
for (let j = i - 1; j >= 0; j--) {
|
||||
const s = steps[j];
|
||||
if (s.kind === "end") continue;
|
||||
if (HOLD[s.var] && i > j + 1 && steps.slice(j + 1, i).every((x) => !placeOf(x.var, x.taken, x.before))) return "hour";
|
||||
const p = placeOf(s.var, s.taken, s.before);
|
||||
if (p) return p;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function pill(g, x, y, text, { fill = PAPER.halo, ink = PAPER.ink, strong = false } = {}) {
|
||||
const w = 8 + text.length * (strong ? 7.4 : 6.6), hgt = strong ? 19 : 16;
|
||||
el("rect", { x: x - w / 2, y: y - hgt / 2, width: w, height: hgt, rx: hgt / 2, fill, "fill-opacity": 0.94, stroke: ink, "stroke-opacity": 0.35 }, g);
|
||||
const t = el("text", { x, y: y + (strong ? 4.5 : 4), "text-anchor": "middle", "font-size": strong ? 12.5 : 11, "font-weight": strong ? 700 : 500, fill: ink, "font-family": "var(--font-data)" }, g);
|
||||
t.textContent = text;
|
||||
}
|
||||
const at = (A, B, bend, t) => {
|
||||
const mx = (A.x + B.x) / 2 - (B.y - A.y) * bend, my = (A.y + B.y) / 2 + (B.x - A.x) * bend;
|
||||
return { x: (1 - t) ** 2 * A.x + 2 * (1 - t) * t * mx + t * t * B.x, y: (1 - t) ** 2 * A.y + 2 * (1 - t) * t * my + t * t * B.y };
|
||||
};
|
||||
|
||||
function drawMap(steps) {
|
||||
const svg = $("#planner-map");
|
||||
svg.innerHTML = "";
|
||||
V.paintBase(svg);
|
||||
svg.innerHTML = svg.innerHTML.replace(/vm-/g, "pl-"); // own filter ids: the other map's defs may be hidden
|
||||
// soften the painting so the plan reads on top
|
||||
el("rect", { x: 0, y: 0, width: 1000, height: 790, fill: "#f6e6c4", "fill-opacity": 0.35 }, svg);
|
||||
const tc = INK[st.colour];
|
||||
const gOpt = el("g", {}, svg), gRoute = el("g", { filter: "url(#pl-brush)" }, svg), gLab = el("g", {}, svg), gPlace = el("g", {}, svg);
|
||||
const used = new Set();
|
||||
|
||||
steps.forEach((s, i) => {
|
||||
if (s.kind === "end") return;
|
||||
const from = anchor(steps, i);
|
||||
const A = from ? L[from] : GATE;
|
||||
if (from) used.add(from);
|
||||
if (s.var === "encounter") {
|
||||
const P = L[from];
|
||||
if (!P) return;
|
||||
const v = s.options["Attempt the bond"];
|
||||
pill(gLab, P.x, P.y + 34, s.choice === "Attempt the bond" ? `attempt: ${pct(v.pX)} ${st.colour}` : "die on purpose", { strong: true, ink: s.choice === "Attempt the bond" ? tc : PAPER.death });
|
||||
return;
|
||||
}
|
||||
const entries = s.kind === "decision" ? Object.entries(s.options) : Object.entries(s.probs);
|
||||
const best = s.kind === "decision" ? Math.max(...entries.map(([, v]) => v.pX)) : null;
|
||||
entries.forEach(([val, v]) => {
|
||||
const pid = placeOf(s.var, val, s.before);
|
||||
if (!pid || !L[pid]) return;
|
||||
const B = L[pid];
|
||||
if (pid === from) return;
|
||||
const taken = val === s.taken;
|
||||
const d = V.curve(A, B, 0.08);
|
||||
if (taken) {
|
||||
el("path", { d, fill: "none", stroke: "#fff1c9", "stroke-width": 14, "stroke-opacity": 0.75, "stroke-linecap": "round" }, gRoute);
|
||||
el("path", { d, fill: "none", stroke: s.kind === "decision" ? tc : PAPER.ink, "stroke-width": 6, "stroke-opacity": 0.9, "stroke-linecap": "round", "stroke-dasharray": s.kind === "chance" ? "2 9" : null }, gRoute);
|
||||
} else {
|
||||
const rel = s.kind === "decision" ? (best > 0 ? v.pX / best : 0) : v;
|
||||
el("path", { d, fill: "none", stroke: s.kind === "decision" ? tc : PAPER.ink, "stroke-width": 1.5 + 3 * rel, "stroke-opacity": 0.25 + 0.4 * rel, "stroke-linecap": "round", "stroke-dasharray": "5 6" }, gOpt);
|
||||
}
|
||||
used.add(pid);
|
||||
const m = at(A, B, 0.08, s.kind === "decision" ? 0.7 : 0.55);
|
||||
const label = s.kind === "decision" ? pct(v.pX) : `roll ${pct(v)}`;
|
||||
pill(gLab, m.x, m.y, label, { strong: taken, ink: s.kind === "decision" ? (taken ? tc : PAPER.ink) : PAPER.ink });
|
||||
});
|
||||
if (s.risk && s.risk.p > 0.0005 && from) {
|
||||
const P = L[from];
|
||||
el("circle", { cx: P.x + 16, cy: P.y - 15, r: 13, fill: PAPER.death, stroke: PAPER.halo, "stroke-width": 1.5 }, gLab);
|
||||
const t = el("text", { x: P.x + 16, y: P.y - 11.5, "text-anchor": "middle", "font-size": 9.5, fill: "#fff3e0", "font-family": "var(--font-data)" }, gLab);
|
||||
t.textContent = pct(s.risk.p);
|
||||
el("title", {}, t).textContent = `${s.risk.label}: ${pct(s.risk.p)}`;
|
||||
}
|
||||
});
|
||||
// places: only those on the plan or offered as options
|
||||
Object.entries(L).forEach(([id, P]) => {
|
||||
if (!used.has(id)) return;
|
||||
const g = el("g", {}, gPlace);
|
||||
if (P.kind === "dragon") {
|
||||
el("circle", { cx: P.x, cy: P.y, r: 15, fill: PAPER.halo, stroke: INK[P.colour], "stroke-width": 2.4 }, g);
|
||||
el("path", { d: V.WING, fill: INK[P.colour], transform: `translate(${P.x - 12} ${P.y - 7.5}) scale(0.24)` }, g);
|
||||
} else {
|
||||
el("circle", { cx: P.x, cy: P.y, r: P.kind === "small" ? 6 : 8, fill: PAPER.ink, stroke: PAPER.halo, "stroke-width": 2.5 }, g);
|
||||
}
|
||||
if (!P.first) {
|
||||
const right = (P.lab || "right") !== "left";
|
||||
const t = el("text", { x: P.x + (right ? 20 : -20), y: P.y - 8, "text-anchor": right ? "start" : "end", "font-family": "var(--font-display)", "font-size": 13, fill: PAPER.ink, "paint-order": "stroke", stroke: PAPER.halo, "stroke-width": 3.5, "stroke-linejoin": "round" }, g);
|
||||
t.textContent = P.name;
|
||||
}
|
||||
});
|
||||
el("text", { x: GATE.x, y: 778, "text-anchor": "middle", "font-size": 11, "letter-spacing": "0.14em", fill: PAPER.ink, "font-family": "var(--font-label)", "paint-order": "stroke", stroke: PAPER.halo, "stroke-width": 3 }, svg).textContent = "ENTER THE VALLEY";
|
||||
V.finish(svg);
|
||||
svg.innerHTML = svg.innerHTML.replace(/url\(#vm-/g, "url(#pl-");
|
||||
bindHits(steps); // click targets go on last, after the id rewrite (which would drop listeners)
|
||||
}
|
||||
|
||||
// Invisible, wide click targets over every option trail on the map.
|
||||
function bindHits(steps) {
|
||||
const svg = $("#planner-map");
|
||||
const g = el("g", {}, svg);
|
||||
steps.forEach((s, i) => {
|
||||
if (s.kind === "end" || s.var === "encounter") return;
|
||||
const from = anchor(steps, i), A = from ? L[from] : GATE;
|
||||
const entries = s.kind === "decision" ? Object.entries(s.options) : Object.entries(s.probs);
|
||||
entries.forEach(([val, v]) => {
|
||||
const pid = placeOf(s.var, val, s.before);
|
||||
if (!pid || !L[pid] || pid === from) return;
|
||||
const hit = el("path", { d: V.curve(A, L[pid], 0.08), fill: "none", stroke: "#000", "stroke-opacity": 0, "stroke-width": 18, style: "cursor:pointer", tabindex: 0, role: "button",
|
||||
"aria-label": s.kind === "decision" ? `${s.label}: ${val}, ${st.colour} ${pct(v.pX)}` : `${s.label}: ${val}, ${pct(v)}` }, g);
|
||||
el("title", {}, hit).textContent = s.kind === "decision"
|
||||
? `${s.label}: ${val}\n${st.colour}: ${pct(v.pX)} · reach a bonding encounter alive: ${pct(v.pReach)}` : `${s.label}: ${val} (${pct(v)})`;
|
||||
const go = () => choose(s, val);
|
||||
hit.addEventListener("click", go);
|
||||
hit.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); go(); } });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- the step list ---------- */
|
||||
|
||||
const NAME = (s, val) => {
|
||||
if (s.var === "fs" || s.var === "meadow" || s.var === "shadowColour") return `${val} dragon`;
|
||||
if (s.var === "shadow") return val === "yes" ? "The shadow appears" : "No shadow";
|
||||
if (s.var === "company") return val === "Alone" ? "Alone" : `With ${val.toLowerCase()}`;
|
||||
if (s.var === "direction") return val === "Center" ? "Forest centre" : val;
|
||||
if (s.var === "post") return val === "Forest" ? "Deeper into the forest" : `${val} fork`;
|
||||
if (s.var === "hour") return val === "Tree" ? "Climb the tallest tree" : "Walk on to the valley centre";
|
||||
return val;
|
||||
};
|
||||
|
||||
function drawSteps(steps) {
|
||||
const box = $("#planner-steps");
|
||||
box.innerHTML = steps.map((s, i) => {
|
||||
if (s.kind === "end") {
|
||||
const rows = Object.entries(s.outcomes).sort((a, b) => b[1] - a[1]).map(([k, p]) =>
|
||||
`<div class="pl-row"><span>${k === "burn" ? "Incinerated" : "Bond " + esc(k)}</span><i style="--w:${100 * p}%;--c:${k === "burn" ? "var(--ember)" : `var(${DV[k]})`}"></i><span class="m"><b>${pct(p)}</b></span></div>`).join("");
|
||||
return `<li class="pl-step end"><span class="pl-n">${i + 1}</span><div><h4>${s.action === "Die on purpose" ? "Die on purpose and try again" : "The bonding encounter"}</h4>${rows}</div></li>`;
|
||||
}
|
||||
const isDec = s.kind === "decision";
|
||||
const entries = isDec ? Object.entries(s.options).sort((a, b) => b[1].pX - a[1].pX) : Object.entries(s.probs).sort((a, b) => b[1] - a[1]);
|
||||
const top = isDec ? Math.max(1e-9, entries[0][1].pX) : 1;
|
||||
const second = isDec && entries[1] ? entries[1][1].pX : null;
|
||||
const close = isDec && second != null && (entries[0][1].pX - second < 0.002 || (s.stillBest != null && s.stillBest < 0.75));
|
||||
const sure = isDec && s.stillBest != null ? `<p class="small muted">The recommended option stays best in ${pct(s.stillBest)} of 200 re-runs on the uncertain data.</p>` : "";
|
||||
const rows = entries.map(([val, v]) => {
|
||||
const taken = val === s.taken, rec = isDec && val === s.choice;
|
||||
const w = isDec ? (100 * v.pX) / top : 100 * v;
|
||||
const meta = `<span class="m">${isDec ? `<b>${pct(v.pX)}</b><small>alive ${pct(v.pReach)}${v.pOther > 0.0005 ? ` · other ${pct(v.pOther)}` : ""}</small>` : `<b>${pct(v)}</b>`}</span>`;
|
||||
return `<button type="button" class="pl-row${taken ? " taken" : ""}" data-i="${i}" data-v="${esc(val)}" aria-pressed="${taken}">
|
||||
<span>${esc(NAME(s, val))}${rec ? ' <em class="rec">best</em>' : ""}</span><i style="--w:${w}%;--c:${isDec ? `var(${DV[st.colour]})` : "var(--muted)"}"></i>${meta}</button>`;
|
||||
}).join("");
|
||||
const risk = s.risk && s.risk.p > 0.0005 ? `<p class="small pl-risk">${esc(s.risk.label)}: ${pct(s.risk.p)}</p>` : "";
|
||||
const head = isDec ? (s.var === "encounter" ? `A ${esc(s.encounter.split("-")[1] === "Greens" ? "pair of Greens" : s.encounter.split("-")[1] + " dragon")} waits` : esc(s.label)) : `${esc(s.label)} <span class="muted">· chance</span>`;
|
||||
return `<li class="pl-step ${isDec ? "dec" : "chance"}${s.userSet ? " user" : ""}"><span class="pl-n">${i + 1}</span><div>${risk}<h4>${head}</h4>
|
||||
${isDec && close ? '<p class="small pl-close">No clear winner: the top options are within the noise.</p>' : ""}${sure}${rows}</div></li>`;
|
||||
}).join("");
|
||||
$$(".pl-row[data-i]", box).forEach((b) => b.addEventListener("click", () => choose(steps[+b.dataset.i], b.dataset.v)));
|
||||
}
|
||||
|
||||
function choose(s, val) {
|
||||
const key = s.var === "encounter" ? "encounter" : s.var;
|
||||
// keep earlier choices, change this one, forget everything after it
|
||||
const order = ["company", "direction", "fs", "cadet", "post", "hour", "shadow", "shadowColour", "descend", "valley", "meadow", "encounter"];
|
||||
const keep = {};
|
||||
for (const k of order) { if (k === key) break; if (st.h[k] != null) keep[k] = st.h[k]; }
|
||||
Object.assign(keep, s.before);
|
||||
keep[key] = val;
|
||||
st.h = keep;
|
||||
render();
|
||||
}
|
||||
|
||||
function headline() {
|
||||
const T = st.policy.samples[st.sample].targets[st.colour], b = T[st.objective];
|
||||
const lb = st.policy.samples.lowBias.targets[st.colour].thisAttempt;
|
||||
const att = b.expectedAttempts.mean;
|
||||
$("#planner-head").innerHTML = `
|
||||
<div class="pl-kpi" style="--c: var(${DV[st.colour]})"><span class="lbl">Best route, ${st.colour}</span><b>${pct(b.pX.mean)}</b><span class="small muted">per attempt · 95% ${pct(b.pX.lo)}–${pct(b.pX.hi)}</span></div>
|
||||
<div class="pl-kpi"><span class="lbl">Attempts, about</span><b>${isFinite(att) ? Math.round(att) : "–"}</b><span class="small muted">if you die on purpose whenever it goes wrong</span></div>
|
||||
<div class="pl-kpi"><span class="lbl">Ends on another colour</span><b>${pct(b.pOther.mean)}</b><span class="small muted">per attempt, on this route</span></div>
|
||||
<div class="pl-kpi"><span class="lbl">Low-bias estimate</span><b>${pct(lb.pX.mean)}</b><span class="small muted">riders who logged every run</span></div>`;
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (!st.policy) return;
|
||||
$$("#planner-colours .tchip").forEach((b) => b.setAttribute("aria-pressed", b.dataset.c === st.colour));
|
||||
$$("#planner-obj button").forEach((b) => b.setAttribute("aria-pressed", b.dataset.o === st.objective));
|
||||
$$("#planner-sample button").forEach((b) => b.setAttribute("aria-pressed", b.dataset.s === st.sample));
|
||||
$(".planner-shell").style.setProperty("--vt", `var(${DV[st.colour]})`);
|
||||
headline();
|
||||
const steps = walk();
|
||||
drawMap(steps);
|
||||
drawSteps(steps);
|
||||
}
|
||||
|
||||
function init() {
|
||||
$("#planner-colours").innerHTML = COLOURS.map((c) => `<button type="button" class="tchip${c === "Blue" || c === "Black" ? " featured" : ""}" data-c="${c}" style="--c: var(${DV[c]})"><span class="sw"></span>${c}</button>`).join("");
|
||||
$$("#planner-colours .tchip").forEach((b) => b.addEventListener("click", () => { st.colour = b.dataset.c; st.h = {}; render(); }));
|
||||
$$("#planner-obj button").forEach((b) => b.addEventListener("click", () => { st.objective = b.dataset.o; render(); }));
|
||||
$$("#planner-sample button").forEach((b) => b.addEventListener("click", () => { st.sample = b.dataset.s; render(); }));
|
||||
$("#planner-reset").addEventListener("click", () => { st.h = {}; render(); });
|
||||
fetch("data/policy.json").then((r) => r.json()).then((p) => { st.policy = p; render(); })
|
||||
.catch(() => { $("#planner-head").innerHTML = '<p class="small muted">The route model (data/policy.json) could not be loaded.</p>'; });
|
||||
}
|
||||
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();
|
||||
window.RoutePlanner = { state: st, render };
|
||||
})();
|
||||
+1
-1
@@ -26,7 +26,7 @@
|
||||
function rng(seed) { let s = seed >>> 0; return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296); }
|
||||
|
||||
// ------------------------------------------------------------ tabs
|
||||
const TABS = ["overview", "valley", "flow", "rare", "dragons", "deep", "share"];
|
||||
const TABS = ["overview", "planner", "valley", "flow", "rare", "dragons", "deep", "share"];
|
||||
function showTab(name, focus) {
|
||||
if (!TABS.includes(name)) name = "overview";
|
||||
TABS.forEach((t) => {
|
||||
|
||||
@@ -383,5 +383,7 @@
|
||||
${out.length ? `<span class="eyebrow">Where they went next</span><div class="bars">${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("")}</div>` : ""}
|
||||
<span class="eyebrow">How those runs ended</span><div class="bars">${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("") || '<span class="small muted">No runs.</span>'}</div>`;
|
||||
}
|
||||
window.ValleyMap = { render, path, places: L };
|
||||
// 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 };
|
||||
})();
|
||||
|
||||
@@ -272,8 +272,10 @@
|
||||
|
||||
/* ---------- 3. The decision tree and backward induction ---------- */
|
||||
|
||||
const ZERO = { x: 0, o: 0 };
|
||||
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o });
|
||||
// x = P(target colour), o = P(another colour), r = P(reaching a bonding encounter alive)
|
||||
const ZERO = { x: 0, o: 0, r: 0 };
|
||||
const REACHED = { x: 0, o: 0, r: 1 };
|
||||
const add = (a, b, w) => ({ x: a.x + w * b.x, o: a.o + w * b.o, r: a.r + w * b.r });
|
||||
|
||||
/**
|
||||
* Solve for target colour X. score(v) = (1 - lam) * P(X) - lam * P(other) (+ tiny tie-break on P(X)).
|
||||
@@ -305,11 +307,11 @@
|
||||
const die = () => ZERO;
|
||||
|
||||
const encounter = (h, ctx) => best("encounter", { ...h, enc: ctx }, support.encounter, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
if (o === "Die on purpose") return REACHED;
|
||||
const d = params.encounter(ctx);
|
||||
let x = 0, oth = 0;
|
||||
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
|
||||
return { x, o: oth };
|
||||
return { x, o: oth, r: 1 };
|
||||
});
|
||||
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
|
||||
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
|
||||
@@ -355,11 +357,11 @@
|
||||
const pick = (name, h, f) => f(decide(name, h));
|
||||
const mix = (dist, f) => Object.entries(dist).reduce((acc, [k, p]) => (p ? add(acc, f(k), p) : acc), ZERO);
|
||||
const encounter = (h, ctx) => pick("encounter", { ...h, enc: ctx }, (o) => {
|
||||
if (o === "Die on purpose") return ZERO;
|
||||
if (o === "Die on purpose") return REACHED;
|
||||
const d = params.encounter(ctx);
|
||||
let x = 0, oth = 0;
|
||||
for (const [k, p] of Object.entries(d)) if (k !== "burn") { if (k === X) x += p; else oth += p; }
|
||||
return { x, o: oth };
|
||||
return { x, o: oth, r: 1 };
|
||||
});
|
||||
const tree = (h) => mix(params.chance("shadow", h), (s) => s === "yes"
|
||||
? mix(params.chance("shadowColour", h), (c) => encounter({ ...h, shadow: "yes" }, "shadow-" + c))
|
||||
@@ -392,7 +394,7 @@
|
||||
function exportTree(params, X, sol, draws, minShow = 1e-4) {
|
||||
const vals = (name, h) => sol.optionValues(name, h) || {};
|
||||
const conf = (name, h, chosen) => (draws.length ? draws.filter((d) => d.decide(name, h) === chosen).length / draws.length : null);
|
||||
const fmt = (v) => ({ pX: v.x, pOther: v.o });
|
||||
const fmt = (v) => ({ pX: v.x, pOther: v.o, pReach: v.r });
|
||||
const decision = (name, h, cont) => {
|
||||
const chosen = sol.decide(name, h);
|
||||
const ov = vals(name, h);
|
||||
@@ -605,15 +607,60 @@
|
||||
const sol = objective === "eventually"
|
||||
? (() => { const r = solveRatio(params, X, { support: m.support }); return solve(params, X, { support: m.support, lam: r.lam, record: true }); })()
|
||||
: solve(params, X, { support: m.support, record: true });
|
||||
const opts = (name, key) => Object.fromEntries(Object.entries(sol.optionValues(name, key) || {}).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }]));
|
||||
const dec = (name, key, extra = {}) => ({ kind: "decision", var: name, label: LABEL[name], choice: sol.decide(name, key), options: opts(name, key), ...extra });
|
||||
const cha = (name, key, extra = {}) => ({ kind: "chance", var: name, label: LABEL[name], probs: params.chance(name, key), ...extra });
|
||||
/**
|
||||
* Everything the planner needs at history h: the next decision (every option with P(target), P(other colour)
|
||||
* and P(reaching an encounter alive), each followed by the best play afterwards), or the next chance event with
|
||||
* its probabilities, or the end of the run with the encounter's outcome distribution. `risk` is the chance of
|
||||
* being killed at the step just survived. h may also hold `encounter` ("Attempt the bond" / "Die on purpose").
|
||||
*/
|
||||
function step(h) {
|
||||
if (!h.company) return dec("company", {});
|
||||
if (!h.direction) return dec("direction", { company: h.company });
|
||||
const a0 = { company: h.company, direction: h.direction };
|
||||
if (!h.fs) return cha("fs", a0);
|
||||
const a = { ...a0, fs: h.fs };
|
||||
const riskSight = params.chance("dieSight", a).die;
|
||||
if (!h.cadet) return dec("cadet", a, { risk: { label: "Killed at the first sighting", p: riskSight } });
|
||||
const b = { ...a, cadet: h.cadet };
|
||||
const riskCombat = HELP[h.cadet] ? params.chance("dieCombat", b).die : 0;
|
||||
if (HELP[h.cadet] && !h.post) return dec("post", b, { risk: { label: "Killed in the fight", p: riskCombat } });
|
||||
const c = { ...b, post: HELP[h.cadet] ? h.post : "(no fork)" };
|
||||
const riskHour = params.chance("dieHour", c).die;
|
||||
if (!h.hour) return dec("hour", c, { risk: { label: "Killed before the hour", p: riskHour } });
|
||||
const d = { ...c, hour: h.hour };
|
||||
let enc = null, key = null;
|
||||
if (h.hour === "Tree") {
|
||||
if (!h.shadow) return cha("shadow", d);
|
||||
if (h.shadow === "yes") {
|
||||
if (!h.shadowColour) return cha("shadowColour", d);
|
||||
enc = "shadow-" + h.shadowColour; key = { ...d, shadow: "yes", enc };
|
||||
} else {
|
||||
if (!h.descend) return dec("descend", { ...d, shadow: "no" });
|
||||
enc = h.descend.startsWith("Quickly") ? "tree-Red" : "tree-Greens"; key = { ...d, shadow: "no", descend: h.descend, enc };
|
||||
}
|
||||
} else {
|
||||
if (!h.valley) return dec("valley", d);
|
||||
if (h.valley === "Face the Red") { enc = "valley-Red"; key = { ...d, valley: h.valley, enc }; }
|
||||
else {
|
||||
if (!h.meadow) return cha("meadow", d);
|
||||
enc = "meadow-" + h.meadow; key = { ...d, valley: h.valley, meadow: h.meadow, enc };
|
||||
}
|
||||
}
|
||||
if (!h.encounter) return dec("encounter", key, { encounter: enc });
|
||||
return { kind: "end", encounter: enc, action: h.encounter, outcomes: h.encounter === "Die on purpose" ? { burn: 1 } : params.encounter(enc) };
|
||||
}
|
||||
return {
|
||||
colour: X, objective, value: sol.value,
|
||||
colour: X, objective, value: sol.value, step,
|
||||
next(h) {
|
||||
const nd = nextDecision(h);
|
||||
if (!nd) return null;
|
||||
const [name, key] = nd;
|
||||
const ov = sol.optionValues(name, key) || {};
|
||||
return { decision: name, label: LABEL[name], choice: sol.decide(name, key),
|
||||
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, { pX: v.x, pOther: v.o }])) };
|
||||
options: Object.fromEntries(Object.entries(ov).map(([o, v]) => [o, { pX: v.x, pOther: v.o, pReach: v.r }])) };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,6 +12,14 @@ const copy = (from, to) => {
|
||||
|
||||
fs.rmSync(out, { recursive: true, force: true });
|
||||
for (const f of fs.readdirSync(path.join(root, "apps/atlas"))) copy(`apps/atlas/${f}`, f);
|
||||
// The page is written as an Artifact body (the artifact host adds the document skeleton). For GitHub Pages and
|
||||
// local serving, give it its own doctype, charset and viewport.
|
||||
{
|
||||
const p = path.join(out, "index.html");
|
||||
const html = fs.readFileSync(p, "utf8");
|
||||
if (!/^\s*<!doctype/i.test(html))
|
||||
fs.writeFileSync(p, `<!doctype html>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1">\n${html}`);
|
||||
}
|
||||
copy("packages/core/src/engine.js", "engine.js");
|
||||
copy("packages/core/src/graph.js", "graph.js");
|
||||
copy("packages/core/src/solver.js", "solver.js");
|
||||
|
||||
Reference in New Issue
Block a user