Prompt specification
Build or customize the Three.js 3D scene "Random Roof Repairs on a Tabletop" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Random Roof Repairs on a Tabletop
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/random-roof-repairs-on-a-tabletop-x3190
Scene Description Prompt:
Use Three.js from cdnjs to use a seeded toy sequence of isolated roof damage events and fixed repair costs. Compare individual savings with a shared reserve using the same total starting tokens. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (random-roof-repairs-on-a-tabletop-x3190):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Roof Reserve — Pooling Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111c;
--panel: rgba(13, 27, 41, 0.9);
--panel-solid: #0d1b29;
--panel-raised: #112335;
--line: rgba(160, 194, 218, 0.15);
--text: #edf6fb;
--muted: #91a8b8;
--individual: #f5b95f;
--individual-soft: rgba(245, 185, 95, 0.14);
--shared: #5de0c0;
--shared-soft: rgba(93, 224, 192, 0.13);
--danger: #ff6978;
--radius: 18px;
--shadow: 0 22px 65px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html {
min-width: 320px;
background: var(--bg);
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
background:
radial-gradient(circle at 12% 8%, rgba(40, 104, 132, 0.18), transparent 27rem),
radial-gradient(circle at 88% 12%, rgba(32, 120, 104, 0.14), transparent 28rem),
linear-gradient(180deg, #081521 0%, #06101a 100%);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
line-height: 1.45;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
button:focus-visible,
input:focus-visible,
canvas:focus-visible {
outline: 2px solid var(--shared);
outline-offset: 3px;
}
.app {
width: min(1580px, calc(100% - 32px));
margin: 0 auto;
padding: 26px 0 38px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
margin-bottom: 20px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
position: relative;
width: 47px;
height: 47px;
flex: 0 0 auto;
border: 1px solid rgba(93, 224, 192, 0.34);
border-radius: 14px;
background: linear-gradient(145deg, rgba(93, 224, 192, 0.16), rgba(93, 224, 192, 0.03));
box-shadow: inset 0 0 24px rgba(93, 224, 192, 0.07);
}
.brand-mark::before {
content: "";
position: absolute;
left: 11px;
top: 18px;
width: 24px;
height: 13px;
background: var(--shared);
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.brand-mark::after {
content: "";
position: absolute;
left: 15px;
top: 29px;
width: 16px;
height: 9px;
border-radius: 2px 2px 4px 4px;
background: #dce9ee;
}
.eyebrow {
margin: 0 0 2px;
color: var(--shared);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.35rem, 2.4vw, 2rem);
line-height: 1.1;
letter-spacing: -0.035em;
}
.top-meta {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 0.78rem;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--shared);
box-shadow: 0 0 0 5px rgba(93, 224, 192, 0.09);
}
.summary-grid {
display: grid;
grid-template-columns: minmax(0, 1fr) 190px minmax(0, 1fr);
gap: 14px;
margin-bottom: 14px;
}
.strategy-card,
.equal-card,
.panel {
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.strategy-card {
position: relative;
overflow: hidden;
padding: 18px 20px 16px;
}
.strategy-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--accent);
}
.strategy-card.individual {
--accent: var(--individual);
--soft: var(--individual-soft);
}
.strategy-card.shared {
--accent: var(--shared);
--soft: var(--shared-soft);
}
.strategy-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.strategy-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 0.78rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.strategy-name i {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--accent);
box-shadow: 0 0 15px color-mix(in srgb, var(--accent) 55%, transparent);
}
.strategy-badge {
padding: 4px 8px;
border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
border-radius: 999px;
color: var(--accent);
background: var(--soft);
font-size: 0.66rem;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric-main {
display: flex;
align-items: baseline;
gap: 9px;
}
.metric-main strong {
font-size: clamp(1.8rem, 3vw, 2.45rem);
line-height: 1;
letter-spacing: -0.05em;
}
.metric-main span {
color: var(--muted);
font-size: 0.76rem;
}
.meter {
height: 5px;
margin: 13px 0 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.meter > span {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--accent);
transition: width 350ms ease;
}
.stat-row {
display: flex;
flex-wrap: wrap;
gap: 9px 20px;
}
.stat-row div {
color: var(--muted);
font-size: 0.73rem;
}
.stat-row strong {
margin-right: 4px;
color: var(--text);
font-size: 0.88rem;
}
.equal-card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 15px;
text-align: center;
}
.equal-icon {
display: grid;
place-items: center;
width: 30px;
height: 30px;
margin-bottom: 8px;
border: 1px solid var(--line);
border-radius: 50%;
color: var(--muted);
font-weight: 800;
}
.equal-card strong {
font-size: 1.05rem;
letter-spacing: -0.025em;
}
.equal-card span {
margin-top: 3px;
color: var(--muted);
font-size: 0.67rem;
line-height: 1.3;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 310px;
gap: 14px;
margin-bottom: 14px;
}
.viewport-card {
position: relative;
min-width: 0;
height: clamp(540px, 63vh, 720px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background: #081521;
box-shadow: var(--shadow);
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.viewport-top {
position: absolute;
z-index: 3;
top: 14px;
left: 14px;
right: 14px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.view-tag,
.view-help {
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(6, 16, 26, 0.76);
backdrop-filter: blur(10px);
}
.view-tag {
padding: 9px 11px;
}
.view-tag strong {
display: block;
font-size: 0.76rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.view-tag span {
color: var(--muted);
font-size: 0.68rem;
}
.view-help {
padding: 8px 10px;
color: var(--muted);
font-size: 0.68rem;
text-align: right;
}
.world-labels {
position: absolute;
inset: 0;
z-index: 2;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
display: none;
min-width: 110px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(6, 16, 26, 0.84);
box-shadow: 0 9px 25px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(8px);
color: var(--text);
font-size: 0.66rem;
font-weight: 800;
letter-spacing: 0.055em;
text-align: center;
text-transform: uppercase;
white-space: nowrap;
}
.world-label.individual {
border-color: rgba(245, 185, 95, 0.34);
color: var(--individual);
}
.world-label.shared {
border-color: rgba(93, 224, 192, 0.34);
color: var(--shared);
}
.world-label.event-label {
min-width: 150px;
padding: 6px 9px;
font-size: 0.61rem;
}
.view-reset {
position: absolute;
z-index: 4;
right: 14px;
bottom: 14px;
}
.small-button {
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(6, 16, 26, 0.8);
color: var(--muted);
cursor: pointer;
font-size: 0.7rem;
font-weight: 750;
}
.small-button:hover {
color: var(--text);
border-color: rgba(160, 194, 218, 0.35);
}
.control-panel {
padding: 18px;
}
.panel-title {
margin: 0;
font-size: 0.76rem;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-subtitle {
margin: 5px 0 16px;
color: var(--muted);
font-size: 0.73rem;
}
.control-group {
padding: 15px 0;
border-top: 1px solid var(--line);
}
.control-group:first-of-type {
border-top: 0;
padding-top: 0;
}
.control-label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
color: var(--muted);
font-size: 0.7rem;
font-weight: 750;
}
.control-label output {
color: var(--text);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.button-row.three {
grid-template-columns: repeat(3, 1fr);
}
.control-button {
min-height: 40px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: #102131;
color: var(--text);
cursor: pointer;
font-size: 0.75rem;
font-weight: 800;
transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
}
.control-button:hover {
transform: translateY(-1px);
border-color: rgba(93, 224, 192, 0.4);
background: #142a3b;
}
.control-button.primary {
border-color: rgba(93, 224, 192, 0.38);
background: var(--shared);
color: #062019;
}
.control-button.primary:hover {
background: #78e8ce;
}
input[type="number"] {
width: 100%;
min-height: 39px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: #091723;
color: var(--text);
}
input[type="range"] {
width: 100%;
accent-color: var(--shared);
cursor: pointer;
}
.seed-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.seed-row .control-button {
min-height: 39px;
}
.assumption {
display: grid;
gap: 9px;
margin-top: 2px;
}
.assumption-row {
display: flex;
justify-content: space-between;
gap: 12px;
color: var(--muted);
font-size: 0.7rem;
}
.assumption-row strong {
color: var(--text);
font-weight: 750;
}
.lower-grid {
display: grid;
grid-template-columns: minmax(0, 1.4fr) minmax(300px, 0.8fr);
gap: 14px;
margin-bottom: 14px;
}
.timeline-panel,
.log-panel,
.method-panel {
padding: 18px;
}
.section-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 14px;
}
.section-head p {
margin: 0;
color: var(--muted);
font-size: 0.72rem;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
color: var(--muted);
font-size: 0.67rem;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 50%;
}
.legend .gold {
background: var(--individual);
}
.legend .mint {
background: var(--shared);
}
.timeline {
display: flex;
gap: 8px;
overflow-x: auto;
padding: 2px 2px 9px;
scrollbar-width: thin;
scrollbar-color: rgba(160, 194, 218, 0.25) transparent;
}
.event-step {
position: relative;
display: grid;
grid-template-columns: auto 1fr;
gap: 8px;
min-width: 118px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(5, 15, 24, 0.56);
color: var(--text);
cursor: pointer;
text-align: left;
transition: border-color 150ms ease, transform 150ms ease, background 150ms ease;
}
.event-step:hover {
transform: translateY(-2px);
border-color: rgba(160, 194, 218, 0.34);
}
.event-step.active {
border-color: rgba(93, 224, 192, 0.5);
background: rgba(93, 224, 192, 0.08);
}
.step-number {
display: grid;
place-items: center;
width: 27px;
height: 27px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
color: var(--muted);
font-size: 0.65rem;
font-weight: 850;
}
.event-step.active .step-number {
background: var(--shared);
color: #062019;
}
.step-house {
display: block;
font-size: 0.73rem;
font-weight: 800;
}
.step-cost {
display: block;
margin-top: 1px;
color: var(--muted);
font-size: 0.63rem;
}
.result-dots {
grid-column: 1 / -1;
display: flex;
gap: 5px;
margin-top: 1px;
}
.result-dots i {
width: 7px;
height: 7px;
border-radius: 50%;
background: #2b3c4a;
}
.result-dots i.ok {
background: var(--shared);
}
.result-dots i.miss {
background: var(--danger);
}
.result-dots i.gold.ok {
background: var(--individual);
}
.log-list {
display: grid;
gap: 8px;
min-height: 204px;
max-height: 245px;
overflow: auto;
scrollbar-width: thin;
}
.log-row {
display: grid;
grid-template-columns: 34px minmax(0, 1fr) auto;
align-items: center;
gap: 9px;
padding: 9px 0;
border-bottom: 1px solid rgba(160, 194, 218, 0.08);
}
.log-row:last-child {
border-bottom: 0;
}
.log-id {
color: var(--muted);
font-size: 0.65rem;
font-weight: 850;
}
.log-row strong {
display: block;
font-size: 0.73rem;
}
.log-row small {
color: var(--muted);
font-size: 0.63rem;
}
.log-status {
display: flex;
gap: 4px;
}
.log-status span {
min-width: 34px;
padding: 3px 5px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.05);
color: var(--muted);
font-size: 0.57rem;
font-weight: 850;
text-align: center;
}
.log-status span.ok {
background: rgba(93, 224, 192, 0.11);
color: var(--shared);
}
.log-status span.gold.ok {
background: var(--individual-soft);
color: var(--individual);
}
.log-status span.miss {
background: rgba(255, 105, 120, 0.12);
color: var(--danger);
}
.empty-log {
display: grid;
min-height: 190px;
place-items: center;
color: var(--muted);
font-size: 0.75rem;
text-align: center;
}
.method-panel {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(240px, 0.7fr);
gap: 24px;
align-items: start;
}
.conclusion {
margin: 7px 0 0;
color: #c8d7e1;
font-size: 0.82rem;
line-height: 1.6;
}
.method-steps {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 9px;
}
.method-step {
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(5, 15, 24, 0.45);
}
.method-step b {
display: block;
margin-bottom: 4px;
color: var(--shared);
font-size: 0.64rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.method-step span {
color: var(--muted);
font-size: 0.68rem;
line-height: 1.45;
}
.footer-note {
margin: 14px 2px 0;
color: #637b8c;
font-size: 0.65rem;
text-align: center;
}
@media (max-width: 1080px) {
.summary-grid {
grid-template-columns: 1fr 1fr;
}
.equal-card {
grid-column: 1 / -1;
grid-row: 2;
min-height: 76px;
flex-direction: row;
gap: 12px;
}
.equal-icon {
margin: 0;
}
.workspace {
grid-template-columns: 1fr;
}
.control-panel {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 18px;
}
.control-group,
.control-group:first-of-type {
padding: 0;
border: 0;
}
}
@media (max-width: 760px) {
.app {
width: min(100% - 20px, 1580px);
padding-top: 16px;
}
.topbar {
align-items: flex-start;
}
.top-meta {
display: none;
}
.summary-grid,
.lower-grid,
.method-panel {
grid-template-columns: 1fr;
}
.equal-card {
grid-column: auto;
grid-row: auto;
}
.viewport-card {
height: 530px;
}
.control-panel {
grid-template-columns: 1fr 1fr;
}
.method-steps {
grid-template-columns: 1fr 1fr;
}
}
@media (max-width: 480px) {
.strategy-card {
padding: 16px;
}
.control-panel,
.method-steps {
grid-template-columns: 1fr;
}
.view-help {
display: none;
}
.viewport-card {
height: 490px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Resilience / Toy Model 08</p>
<h1>Roof Reserve</h1>
</div>
</div>
<div class="top-meta">
<span class="live-dot"></span>
<span>Seeded damage sequence · fixed repair costs</span>
</div>
</header>
<section class="summary-grid" aria-label="Strategy comparison">
<article class="strategy-card individual">
<div class="strategy-head">
<div class="strategy-name"><i></i>Individual savings</div>
<div class="strategy-badge">8 sealed envelopes</div>
</div>
<div class="metric-main">
<strong id="individualRemaining">3,200</strong>
<span>tokens unspent</span>
</div>
<div class="meter"><span id="individualMeter"></span></div>
<div class="stat-row">
<div><strong id="individualCovered">0 / 10</strong> repairs funded</div>
<div><strong id="individualUnmet">0</strong> unmet</div>
<div><strong id="individualProtected">8 / 8</strong> homes protected</div>
</div>
</article>
<div class="equal-card">
<div class="equal-icon">=</div>
<strong id="equalTokens">3,200 TOK</strong>
<span>identical starting capital</span>
</div>
<article class="strategy-card shared">
<div class="strategy-head">
<div class="strategy-name"><i></i>Shared reserve</div>
<div class="strategy-badge">1 common pool</div>
</div>
<div class="metric-main">
<strong id="sharedRemaining">3,200</strong>
<span>tokens unspent</span>
</div>
<div class="meter"><span id="sharedMeter"></span></div>
<div class="stat-row">
<div><strong id="sharedCovered">0 / 10</strong> repairs funded</div>
<div><strong id="sharedUnmet">0</strong> unmet</div>
<div><strong id="sharedProtected">8 / 8</strong> homes protected</div>
</div>
</article>
</section>
<main class="workspace">
<section class="viewport-card" id="viewport" aria-label="Three-dimensional neighborhood comparison">
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D comparison of individual household savings and a shared roof repair reserve. Drag to orbit and scroll to zoom."
></canvas>
<div class="world-labels" id="worldLabels">
<div class="world-label individual" id="individualBoardLabel">Individual · 8 envelopes</div>
<div class="world-label shared" id="sharedBoardLabel">Shared · 1 reserve</div>
<div class="world-label event-label individual" id="individualEventLabel"></div>
<div class="world-label event-label shared" id="sharedEventLabel"></div>
</div>
<div class="viewport-top">
<div class="view-tag">
<strong>Isolated roof shocks</strong>
<span id="viewStatus">Ready · 10 deterministic events</span>
</div>
<div class="view-help">
Drag to orbit · Wheel to zoom<br />
Click an event below to replay to it
</div>
</div>
<button class="small-button view-reset" id="cameraReset" type="button">
Reset camera
</button>
</section>
<aside class="control-panel">
<section class="control-group">
<h2 class="panel-title">Simulation</h2>
<p class="panel-subtitle">Apply the same shocks to both funding systems.</p>
<div class="button-row">
<button class="control-button primary" id="playButton" type="button">▶ Play</button>
<button class="control-button" id="nextButton" type="button">Next event</button>
</div>
<div class="button-row three" style="margin-top: 8px">
<button class="control-button" id="resetButton" type="button">Reset</button>
<button class="control-button" id="replayButton" type="button">Replay</button>
<button class="control-button" id="skipButton" type="button">Finish</button>
</div>
</section>
<section class="control-group">
<div class="control-label">
<label for="speed">Playback speed</label>
<output id="speedOutput">1.0×</output>
</div>
<input id="speed" type="range" min="0.5" max="2.5" step="0.1" value="1">
<div class="control-label" style="margin-top: 17px">
<label for="totalInput">Starting tokens — both strategies</label>
</div>
<input id="totalInput" type="number" min="1600" max="4800" step="100" value="3200">
</section>
<section class="control-group">
<div class="control-label">
<label for="seedInput">Deterministic seed</label>
<output id="seedOutput">#7319</output>
</div>
<form class="seed-row" id="seedForm">
<input id="seedInput" type="number" min="0" max="999999" step="1" value="7319">
<button class="control-button" type="submit">Rebuild</button>
</form>
<div class="assumption">
<div class="assumption-row">
<span>Sequence</span>
<strong>10 isolated events</strong>
</div>
<div class="assumption-row">
<span>Households</span>
<strong>8</strong>
</div>
<div class="assumption-row">
<span>Repair costs</span>
<strong>Fixed after seeding</strong>
</div>
</div>
</section>
</aside>
</main>
<section class="lower-grid">
<article class="panel timeline-panel">
<div class="section-head">
<div>
<h2 class="panel-title">Event tape</h2>
<p id="eventProgress">0 of 10 events applied</p>
</div>
<div class="legend">
<span><i class="gold"></i>Individual</span>
<span><i class="mint"></i>Shared</span>
</div>
</div>
<div class="timeline" id="timeline"></div>
</article>
<article class="panel log-panel">
<div class="section-head">
<div>
<h2 class="panel-title">Repair ledger</h2>
<p>Most recent events first</p>
</div>
</div>
<div class="log-list" id="eventLog" aria-live="polite">
<div class="empty-log">Play the sequence to inspect each repair decision.</div>
</div>
</article>
</section>
<section class="panel method-panel">
<div>
<h2 class="panel-title">What this toy model compares</h2>
<p class="conclusion" id="conclusion">
Both strategies begin with exactly the same number of tokens. Individual savings divide that capital
into eight closed household envelopes; the shared strategy keeps all tokens in one common reserve.
Play the sequence to see when access to pooled capital matters.
</p>
</div>
<div class="method-steps">
<div class="method-step">
<b>01 · Seed</b>
<span>A repeatable generator chooses houses and fixed repair costs.</span>
</div>
<div class="method-step">
<b>02 · Isolate</b>
<span>Each event affects one roof. No cascades or correlated repairs.</span>
</div>
<div class="method-step">
<b>03 · Pay</b>
<span>An individual event can use only that home's envelope.</span>
</div>
<div class="method-step">
<b>04 · Pool</b>
<span>A shared event can draw from every household's common reserve.</span>
</div>
</div>
</section>
<p class="footer-note">
Educational simulation only. No debt, interest, insurance, external aid, or reallocation rules are modeled.
A lower shared balance means capital was deployed, not lost.
</p>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const HOUSE_COUNT = 8;
const EVENT_COUNT = 10;
const DEFAULT_TOKENS = 3200;
const $ = (id) => document.getElementById(id);
const format = new Intl.NumberFormat("en-US");
const ui = {
viewport: $("viewport"),
canvas: $("scene"),
individualRemaining: $("individualRemaining"),
sharedRemaining: $("sharedRemaining"),
individualMeter: $("individualMeter"),
sharedMeter: $("sharedMeter"),
individualCovered: $("individualCovered"),
sharedCovered: $("sharedCovered"),
individualUnmet: $("individualUnmet"),
sharedUnmet: $("sharedUnmet"),
individualProtected: $("individualProtected"),
sharedProtected: $("sharedProtected"),
equalTokens: $("equalTokens"),
timeline: $("timeline"),
eventLog: $("eventLog"),
eventProgress: $("eventProgress"),
conclusion: $("conclusion"),
viewStatus: $("viewStatus"),
playButton: $("playButton"),
nextButton: $("nextButton"),
resetButton: $("resetButton"),
replayButton: $("replayButton"),
skipButton: $("skipButton"),
speed: $("speed"),
speedOutput: $("speedOutput"),
totalInput: $("totalInput"),
seedForm: $("seedForm"),
seedInput: $("seedInput"),
seedOutput: $("seedOutput"),
cameraReset: $("cameraReset"),
individualBoardLabel: $("individualBoardLabel"),
sharedBoardLabel: $("sharedBoardLabel"),
individualEventLabel: $("individualEventLabel"),
sharedEventLabel: $("sharedEventLabel")
};
function mulberry32(seed) {
let a = seed >>> 0;
return function random() {
a += 0x6D2B79F5;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function shuffled(values, random) {
const result = values.slice();
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
function generateEvents(seed) {
const random = mulberry32(seed);
const order = shuffled(Array.from({ length: HOUSE_COUNT }, (_, i) => i), random);
const damageTypes = ["hail", "wind", "branch impact", "rain", "snow load"];
return Array.from({ length: EVENT_COUNT }, (_, index) => {
const house = index < HOUSE_COUNT
? order[index]
: order[Math.floor(random() * HOUSE_COUNT)];
const isLargeShock = index === 0 || index === 4 || index === 8;
// The large shocks exceed the default per-household envelope.
// The maximum possible sequence total remains below 3,200 tokens.
const cost = isLargeShock
? 430 + Math.floor(random() * 41)
: 170 + Math.floor(random() * 81);
return {
id: index + 1,
house,
cost,
kind: damageTypes[Math.floor(random() * damageTypes.length)]
};
});
}
const state = {
seed: 7319,
totalTokens: DEFAULT_TOKENS,
speed: 1,
events: generateEvents(7319),
individualBalances: Array(HOUSE_COUNT).fill(DEFAULT_TOKENS / HOUSE_COUNT),
sharedBalance: DEFAULT_TOKENS,
results: [],
playing: false,
nextAt: 0
};
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081521);
scene.fog = new THREE.Fog(0x081521, 25, 52);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas: ui.canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
ui.viewStatus.textContent = "WebGL is unavailable in this browser";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const orbit = {
theta: 0,
targetTheta: 0,
phi: 0.83,
targetPhi: 0.83,
radius: 22,
targetRadius: 22,
target: new THREE.Vector3(0, 0.25, 0)
};
const hemisphere = new THREE.HemisphereLight(0xc4edff, 0x182330, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d8, 3.1);
keyLight.position.set(-7, 13, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
scene.add(keyLight);
const mintLight = new THREE.PointLight(0x5de0c0, 30, 18, 2);
mintLight.position.set(6, 4, -3);
scene.add(mintLight);
const goldLight = new THREE.PointLight(0xf5b95f, 24, 18, 2);
goldLight.position.set(-6, 4, -3);
scene.add(goldLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: 0x08141f,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.34;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(42, 28, 0x274050, 0x142633);
grid.position.y = -0.325;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const roofGeometry = createRoofGeometry(0.96, 0.9, 0.48);
const bodyGeometry = new THREE.BoxGeometry(0.84, 0.72, 0.78);
const windowGeometry = new THREE.BoxGeometry(0.19, 0.19, 0.025);
const doorGeometry = new THREE.BoxGeometry(0.19, 0.36, 0.025);
const chimneyGeometry = new THREE.BoxGeometry(0.16, 0.34, 0.16);
const houseBodyColors = [
0xb9d4d9, 0xc8c2ae, 0xa7c8c2, 0xd7b89c,
0x9fb6ca, 0xc8d3b5, 0xd3c1a1, 0xa9bbc8
];
const roofBaseColors = [
0xc77655, 0x567c8e, 0xd19b5f, 0x6b7887,
0xb96c5f, 0x4f7b72, 0xc28b5d, 0x65758a
];
function createRoofGeometry(width, depth, height) {
const vertices = new Float32Array([
-width / 2, 0, -depth / 2,
width / 2, 0, -depth / 2,
0, height, -depth / 2,
-width / 2, 0, depth / 2,
width / 2, 0, depth / 2,
0, height, depth / 2
]);
const indices = [
0, 2, 1,
3, 4, 5,
0, 3, 5, 0, 5, 2,
1, 2, 5, 1, 5, 4,
0, 1, 4, 0, 4, 3
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createHouse(index) {
const root = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color: houseBodyColors[index],
roughness: 0.78,
metalness: 0.02,
flatShading: true
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: roofBaseColors[index],
roughness: 0.7,
metalness: 0.04,
flatShading: true
});
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 0.36;
body.castShadow = true;
body.receiveShadow = true;
root.add(body);
const roof = new THREE.Mesh(roofGeometry, roofMaterial);
roof.position.y = 0.72;
roof.castShadow = true;
roof.receiveShadow = true;
root.add(roof);
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0xffd58a,
emissive: 0xff9d3c,
emissiveIntensity: 0.55,
roughness: 0.45
});
const windowMesh = new THREE.Mesh(windowGeometry, windowMaterial);
windowMesh.position.set(-0.21, 0.42, 0.402);
root.add(windowMesh);
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x38505d,
roughness: 0.9
});
const door = new THREE.Mesh(doorGeometry, doorMaterial);
door.position.set(0.19, 0.18, 0.403);
root.add(door);
const chimney = new THREE.Mesh(
chimneyGeometry,
new THREE.MeshStandardMaterial({
color: 0x697983,
roughness: 0.9,
flatShading: true
})
);
chimney.position.set(0.22, 1.06, -0.16);
chimney.castShadow = true;
root.add(chimney);
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.105, 0),
new THREE.MeshStandardMaterial({
color: 0x5de0c0,
emissive: 0x5de0c0,
emissiveIntensity: 0.7,
roughness: 0.45,
flatShading: true
})
);
marker.position.y = 1.58;
marker.visible = false;
root.add(marker);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.68, 0.035, 6, 24),
new THREE.MeshBasicMaterial({
color: 0xff6978,
transparent: true,
opacity: 0.9
})
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.04;
halo.visible = false;
root.add(halo);
return {
root,
body,
roof,
marker,
halo,
roofBaseColor: new THREE.Color(roofBaseColors[index]),
state: {
hitStart: -1000,
outcome: null,
failedEver: false
}
};
}
function createTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.08, 0.11, 0.52, 5),
new THREE.MeshStandardMaterial({ color: 0x6e5845, roughness: 1 })
);
trunk.position.y = 0.26;
trunk.castShadow = true;
group.add(trunk);
const crown = new THREE.Mesh(
new THREE.ConeGeometry(0.38, 0.88, 6),
new THREE.MeshStandardMaterial({
color: 0x3f806c,
roughness: 0.92,
flatShading: true
})
);
crown.position.y = 0.84;
crown.castShadow = true;
group.add(crown);
return group;
}
function createTokenColumn(radius, maxHeight, x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 1.08, radius * 1.16, 0.12, 8),
new THREE.MeshStandardMaterial({
color: 0x263b49,
roughness: 0.72,
metalness: 0.2,
flatShading: true
})
);
base.position.y = 0.06;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const fill = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 8),
new THREE.MeshStandardMaterial({
color: 0xf5b95f,
emissive: 0x6b4210,
emissiveIntensity: 0.22,
roughness: 0.5,
metalness: 0.25,
flatShading: true
})
);
fill.castShadow = true;
group.add(fill);
return { group, fill, maxHeight };
}
function setTokenColumn(column, ratio) {
const safeRatio = Math.max(0.001, Math.min(1, ratio));
const height = column.maxHeight * safeRatio;
column.fill.scale.y = height;
column.fill.position.y = 0.12 + height / 2;
}
function createBoard(kind, x) {
const group = new THREE.Group();
group.position.x = x;
const platform = new THREE.Mesh(
new THREE.BoxGeometry(7.45, 0.28, 6.15),
new THREE.MeshStandardMaterial({
color: kind === "individual" ? 0x172737 : 0x14302f,
roughness: 0.92,
metalness: 0.03
})
);
platform.position.y = -0.14;
platform.receiveShadow = true;
platform.castShadow = true;
group.add(platform);
const edgeMaterial = new THREE.MeshStandardMaterial({
color: kind === "individual" ? 0xf5b95f : 0x5de0c0,
emissive: kind === "individual" ? 0x5c3510 : 0x174b42,
emissiveIntensity: 0.6,
roughness: 0.6
});
const frontEdge = new THREE.Mesh(
new THREE.BoxGeometry(7.1, 0.035, 0.045),
edgeMaterial
);
frontEdge.position.set(0, 0.02, 3.02);
group.add(frontEdge);
const houses = [];
const wallets = [];
const xPositions = [-2.55, -0.85, 0.85, 2.55];
const zPositions = [-0.72, 1.28];
let houseIndex = 0;
for (const z of zPositions) {
for (const houseX of xPositions) {
const house = createHouse(houseIndex);
house.root.position.set(houseX, 0, z);
group.add(house.root);
houses.push(house);
if (kind === "individual") {
const wallet = createTokenColumn(0.17, 0.62, houseX, z + 0.66);
group.add(wallet.group);
wallets.push(wallet);
}
houseIndex++;
}
}
let reserve = null;
if (kind === "shared") {
reserve = createTokenColumn(0.48, 1.62, 0, -2.3);
group.add(reserve.group);
const lineMaterial = new THREE.LineBasicMaterial({
color: 0x5de0c0,
transparent: true,
opacity: 0.32
});
for (const house of houses) {
const points = [
new THREE.Vector3(house.root.position.x, 0.035, house.root.position.z),
new THREE.Vector3(house.root.position.x * 0.54, 0.035, -1.2),
new THREE.Vector3(0, 0.035, -2.3)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
group.add(new THREE.Line(geometry, lineMaterial));
}
}
group.add(createTree(-3.3, -2.45, 0.85));
group.add(createTree(3.3, 2.45, 0.78));
group.add(createTree(3.3, -2.42, 0.62));
scene.add(group);
return {
kind,
group,
houses,
wallets,
reserve,
failedEver: Array(HOUSE_COUNT).fill(false)
};
}
const individualBoard = createBoard("individual", -4.55);
const sharedBoard = createBoard("shared", 4.55);
const boards = [individualBoard, sharedBoard];
// ---------------------------------------------------------------------
// Direct camera controls: pointer orbit, wheel/pinch-like zoom, keyboard.
// ---------------------------------------------------------------------
let dragging = false;
let dragX = 0;
let dragY = 0;
ui.canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
ui.canvas.setPointerCapture(event.pointerId);
});
ui.canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
orbit.targetTheta -= dx * 0.006;
orbit.targetPhi = THREE.MathUtils.clamp(orbit.targetPhi - dy * 0.005, 0.42, 1.34);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && ui.canvas.hasPointerCapture?.(event.pointerId)) {
ui.canvas.releasePointerCapture(event.pointerId);
}
}
ui.canvas.addEventListener("pointerup", endDrag);
ui.canvas.addEventListener("pointercancel", endDrag);
ui.canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius + event.deltaY * 0.012,
13,
34
);
}, { passive: false });
ui.canvas.addEventListener("keydown", (event) => {
const step = 0.08;
if (event.key === "ArrowLeft") orbit.targetTheta += step;
else if (event.key === "ArrowRight") orbit.targetTheta -= step;
else if (event.key === "ArrowUp") orbit.targetPhi = Math.max(0.42, orbit.targetPhi - step);
else if (event.key === "ArrowDown") orbit.targetPhi = Math.min(1.34, orbit.targetPhi + step);
else if (event.key === "+" || event.key === "=") orbit.targetRadius = Math.max(13, orbit.targetRadius - 1);
else if (event.key === "-" || event.key === "_") orbit.targetRadius = Math.min(34, orbit.targetRadius + 1);
else return;
event.preventDefault();
});
function resetCamera() {
orbit.targetTheta = 0;
orbit.targetPhi = 0.83;
orbit.targetRadius = 22;
}
ui.cameraReset.addEventListener("click", resetCamera);
function resizeRenderer() {
const width = Math.max(1, ui.viewport.clientWidth);
const height = Math.max(1, ui.viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resizeRenderer).observe(ui.viewport);
resizeRenderer();
function updateCamera() {
orbit.theta += (orbit.targetTheta - orbit.theta) * 0.085;
orbit.phi += (orbit.targetPhi - orbit.phi) * 0.085;
orbit.radius += (orbit.targetRadius - orbit.radius) * 0.085;
const aspectFit = camera.aspect < 1.2 ? 1.2 / camera.aspect : 1;
const radius = orbit.radius * aspectFit;
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + radius * cosPhi,
orbit.target.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
function projectLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * ui.viewport.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * ui.viewport.clientHeight;
element.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateWorldLabels() {
const individualBoardPosition = individualBoard.group.localToWorld(
new THREE.Vector3(0, 2.25, -2.78)
);
const sharedBoardPosition = sharedBoard.group.localToWorld(
new THREE.Vector3(0, 2.25, -2.78)
);
projectLabel(ui.individualBoardLabel, individualBoardPosition);
projectLabel(ui.sharedBoardLabel, sharedBoardPosition);
if (state.results.length === 0) {
ui.individualEventLabel.style.display = "none";
ui.sharedEventLabel.style.display = "none";
return;
}
const latest = state.results[state.results.length - 1];
const individualPosition = individualBoard.houses[latest.event.house].root.getWorldPosition(
new THREE.Vector3()
);
individualPosition.y += 1.93;
const sharedPosition = sharedBoard.houses[latest.event.house].root.getWorldPosition(
new THREE.Vector3()
);
sharedPosition.y += 1.93;
projectLabel(ui.individualEventLabel, individualPosition);
projectLabel(ui.sharedEventLabel, sharedPosition);
}
// ---------------------------------------------------------------------
// Simulation
// ---------------------------------------------------------------------
function resetSimulation() {
state.playing = false;
state.results = [];
state.individualBalances = Array(HOUSE_COUNT).fill(state.totalTokens / HOUSE_COUNT);
state.sharedBalance = state.totalTokens;
for (const board of boards) {
board.failedEver.fill(false);
for (const house of board.houses) {
house.state.hitStart = -1000;
house.state.outcome = null;
house.state.failedEver = false;
house.marker.visible = false;
house.halo.visible = false;
house.roof.material.color.copy(house.roofBaseColor);
}
}
buildTimeline();
updateUI();
}
function simulateEvent(event, index, animate = true) {
const individualBalance = state.individualBalances[event.house];
const individualCovered = individualBalance >= event.cost;
if (individualCovered) {
state.individualBalances[event.house] -= event.cost;
} else {
individualBoard.failedEver[event.house] = true;
}
const sharedCovered = state.sharedBalance >= event.cost;
if (sharedCovered) {
state.sharedBalance -= event.cost;
} else {
sharedBoard.failedEver[event.house] = true;
}
const result = {
event,
index,
individualCovered,
sharedCovered
};
state.results.push(result);
const now = performance.now() / 1000;
for (const [board, covered] of [
[individualBoard, individualCovered],
[sharedBoard, sharedCovered]
]) {
const house = board.houses[event.house];
house.state.outcome = covered;
house.state.failedEver = board.failedEver[event.house];
house.state.hitStart = animate ? now : -1000;
}
updateUI();
return result;
}
function nextEvent(animate = true) {
if (state.results.length >= state.events.length) return;
const index = state.results.length;
simulateEvent(state.events[index], index, animate);
}
function jumpToEvent(index) {
state.playing = false;
state.results = [];
state.individualBalances = Array(HOUSE_COUNT).fill(state.totalTokens / HOUSE_COUNT);
state.sharedBalance = state.totalTokens;
for (const board of boards) {
board.failedEver.fill(false);
for (const house of board.houses) {
house.state.hitStart = -1000;
house.state.outcome = null;
house.state.failedEver = false;
}
}
for (let i = 0; i <= index; i++) {
simulateEvent(state.events[i], i, false);
}
updateUI();
}
function finishSequence() {
state.playing = false;
while (state.results.length < state.events.length) {
nextEvent(false);
}
updateUI();
}
function togglePlay() {
if (state.playing) {
state.playing = false;
updateUI();
return;
}
if (state.results.length >= state.events.length) {
resetSimulation();
}
state.playing = true;
state.nextAt = performance.now() + 320;
updateUI();
}
function buildTimeline() {
ui.timeline.innerHTML = state.events.map((event, index) => `
<button class="event-step" type="button" data-index="${index}" aria-label="Replay through event ${event.id}">
<span class="step-number">${String(event.id).padStart(2, "0")}</span>
<span>
<span class="step-house">H${String(event.house + 1).padStart(2, "0")}</span>
<span class="step-cost">${format.format(event.cost)} tokens</span>
</span>
<span class="result-dots" aria-hidden="true">
<i class="gold"></i>
<i></i>
</span>
</button>
`).join("");
ui.timeline.querySelectorAll(".event-step").forEach((button) => {
button.addEventListener("click", () => {
jumpToEvent(Number(button.dataset.index));
});
});
}
function updateTimeline() {
const steps = ui.timeline.querySelectorAll(".event-step");
steps.forEach((step, index) => {
const result = state.results[index];
step.classList.toggle("active", Boolean(result) && index === state.results.length - 1);
const individualDot = step.querySelector(".result-dots i.gold");
const sharedDot = step.querySelector(".result-dots i:nth-child(2)");
individualDot.className = result
? `gold ${result.individualCovered ? "ok" : "miss"}`
: "gold";
sharedDot.className = result
? (result.sharedCovered ? "ok" : "miss")
: "";
});
}
function updateEventLog() {
if (state.results.length === 0) {
ui.eventLog.innerHTML = `
<div class="empty-log">
Play the sequence to inspect each repair decision.
</div>
`;
return;
}
const recent = state.results.slice(-5).reverse();
ui.eventLog.innerHTML = recent.map((result) => `
<div class="log-row">
<span class="log-id">E${String(result.event.id).padStart(2, "0")}</span>
<div>
<strong>H${String(result.event.house + 1).padStart(2, "0")} · ${result.event.kind}</strong>
<small>${format.format(result.event.cost)} fixed repair tokens</small>
</div>
<div class="log-status">
<span class="gold ${result.individualCovered ? "ok" : "miss"}">
${result.individualCovered ? "IND ✓" : "IND !"}
</span>
<span class="${result.sharedCovered ? "ok" : "miss"}">
${result.sharedCovered ? "POOL ✓" : "POOL !"}
</span>
</div>
</div>
`).join("");
}
function updateUI() {
const individualCovered = state.results.filter((result) => result.individualCovered).length;
const sharedCovered = state.results.filter((result) => result.sharedCovered).length;
const individualUnmet = state.results.length - individualCovered;
const sharedUnmet = state.results.length - sharedCovered;
const individualRemaining = state.individualBalances.reduce((sum, balance) => sum + balance, 0);
const individualProtected = individualBoard.failedEver.filter(Boolean).length;
const sharedProtected = sharedBoard.failedEver.filter(Boolean).length;
ui.individualRemaining.textContent = format.format(Math.round(individualRemaining));
ui.sharedRemaining.textContent = format.format(Math.round(state.sharedBalance));
ui.individualMeter.style.width = `${(individualRemaining / state.totalTokens) * 100}%`;
ui.sharedMeter.style.width = `${(state.sharedBalance / state.totalTokens) * 100}%`;
ui.individualCovered.textContent = `${individualCovered} / ${state.events.length}`;
ui.sharedCovered.textContent = `${sharedCovered} / ${state.events.length}`;
ui.individualUnmet.textContent = individualUnmet;
ui.sharedUnmet.textContent = sharedUnmet;
ui.individualProtected.textContent = `${HOUSE_COUNT - individualProtected} / ${HOUSE_COUNT}`;
ui.sharedProtected.textContent = `${HOUSE_COUNT - sharedProtected} / ${HOUSE_COUNT}`;
ui.equalTokens.textContent = `${format.format(state.totalTokens)} TOK`;
ui.eventProgress.textContent = `${state.results.length} of ${state.events.length} events applied`;
ui.playButton.textContent = state.playing ? "❚❚ Pause" : "▶ Play";
ui.viewStatus.textContent = state.results.length
? `Event ${state.results.length} of ${state.events.length} · ${state.events[state.results.length - 1].kind}`
: `Ready · ${state.events.length} deterministic events`;
individualBoard.wallets.forEach((wallet, index) => {
const initial = state.totalTokens / HOUSE_COUNT;
setTokenColumn(wallet, state.individualBalances[index] / initial);
});
if (sharedBoard.reserve) {
setTokenColumn(sharedBoard.reserve, state.sharedBalance / state.totalTokens);
}
const latest = state.results[state.results.length - 1];
if (latest) {
ui.individualEventLabel.textContent =
`E${latest.event.id} · H${latest.event.house + 1} · ${latest.event.cost} · ${latest.individualCovered ? "FUNDED" : "UNMET"}`;
ui.sharedEventLabel.textContent =
`E${latest.event.id} · H${latest.event.house + 1} · ${latest.event.cost} · ${latest.sharedCovered ? "FUNDED" : "UNMET"}`;
}
if (state.results.length === 0) {
ui.conclusion.textContent =
`Both strategies begin with exactly ${format.format(state.totalTokens)} tokens. ` +
`Individual savings divide that capital into eight closed household envelopes; the shared strategy keeps ` +
`all tokens in one common reserve. Play the sequence to see when access to pooled capital matters.`;
} else {
const repairDifference = sharedCovered - individualCovered;
const protectedDifference = sharedProtected - individualProtected;
ui.conclusion.textContent =
`So far, the shared reserve funds ${sharedCovered} of ${state.events.length} repairs, while individual ` +
`savings fund ${individualCovered}. The shared reserve protects ${protectedDifference > 0 ? `${protectedDifference} more household${protectedDifference === 1 ? "" : "s"}` : protectedDifference < 0 ? `${Math.abs(protectedDifference)} fewer household${protectedDifference === -1 ? "" : "s"}` : "the same number of households"} ` +
`and covers ${repairDifference > 0 ? `${repairDifference} more event${repairDifference === 1 ? "" : "s"}` : repairDifference < 0 ? `${Math.abs(repairDifference)} fewer event${repairDifference === -1 ? "" : "s"}` : "the same number of events"}. ` +
`Pooling changes access to the same starting capital; it does not create additional capital.`;
}
updateTimeline();
updateEventLog();
}
// ---------------------------------------------------------------------
// UI events
// ---------------------------------------------------------------------
ui.playButton.addEventListener("click", togglePlay);
ui.nextButton.addEventListener("click", () => {
state.playing = false;
if (state.results.length >= state.events.length) resetSimulation();
nextEvent(true);
});
ui.resetButton.addEventListener("click", resetSimulation);
ui.replayButton.addEventListener("click", () => {
resetSimulation();
state.playing = true;
state.nextAt = performance.now() + 320;
updateUI();
});
ui.skipButton.addEventListener("click", finishSequence);
ui.speed.addEventListener("input", () => {
state.speed = Number(ui.speed.value);
ui.speedOutput.textContent = `${state.speed.toFixed(1)}×`;
});
ui.totalInput.addEventListener("change", () => {
const value = Number(ui.totalInput.value);
state.totalTokens = THREE.MathUtils.clamp(
Number.isFinite(value) ? value : DEFAULT_TOKENS,
1600,
4800
);
ui.totalInput.value = state.totalTokens;
resetSimulation();
});
ui.seedForm.addEventListener("submit", (event) => {
event.preventDefault();
const value = Number(ui.seedInput.value);
state.seed = Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 7319;
ui.seedInput.value = state.seed;
ui.seedOutput.textContent = `#${state.seed}`;
state.events = generateEvents(state.seed);
resetSimulation();
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const green = new THREE.Color(0x5de0c0);
const red = new THREE.Color(0xff6978);
function updateHouseVisuals(now) {
for (const board of boards) {
for (const house of board.houses) {
const status = house.state;
const elapsed = now - status.hitStart;
if (status.hitStart > -100 && elapsed < 0.72) {
house.halo.visible = true;
house.marker.visible = false;
house.roof.material.color.copy(red);
const pulse = 1 + Math.sin(elapsed * 22) * 0.08;
house.halo.scale.setScalar(pulse);
house.halo.material.opacity = 1 - elapsed / 0.72;
} else {
house.halo.visible = false;
if (status.outcome !== null) {
house.marker.visible = true;
house.marker.material.color.copy(status.outcome ? green : red);
house.marker.material.emissive.copy(status.outcome ? green : red);
house.marker.rotation.y += 0.025;
if (status.failedEver) {
house.roof.material.color.copy(red);
} else {
house.roof.material.color.copy(house.roofBaseColor).lerp(green, 0.42);
}
}
}
}
}
}
function animate(timestamp) {
requestAnimationFrame(animate);
const now = timestamp / 1000;
if (state.playing && timestamp >= state.nextAt) {
if (state.results.length < state.events.length) {
nextEvent(true);
state.nextAt = timestamp + 1280 / state.speed;
if (state.results.length >= state.events.length) {
state.playing = false;
updateUI();
}
} else {
state.playing = false;
updateUI();
}
}
updateCamera();
updateHouseVisuals(now);
updateWorldLabels();
renderer.render(scene, camera);
}
ui.seedOutput.textContent = `#${state.seed}`;
buildTimeline();
resetSimulation();
requestAnimationFrame(animate);
</script>
</body>
</html>
```