Prompt specification
Build or customize the Three.js 3D scene "Renegotiate After a Resource Shortage" 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: Renegotiate After a Resource Shortage
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/renegotiate-after-a-resource-shortage-x3556
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing negotiated agreements between game players. Create a cooperative scenario where one promised resource becomes unavailable. Let players revise commitments and inspect feasible alternatives instead of silently breaking the game rules. Low-poly.
Working Three.js Starter (renegotiate-after-a-resource-shortage-x3556):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#dfe8df" />
<title>Common Ground — Cooperative Agreement Lab</title>
<style>
:root {
--ink: #17342f;
--muted: #667a72;
--paper: rgba(250, 248, 240, 0.94);
--paper-solid: #faf8f0;
--line: rgba(23, 52, 47, 0.13);
--green: #2f8063;
--green-soft: #dceee4;
--orange: #e46f45;
--orange-soft: #fae4da;
--red: #c8493c;
--red-soft: #f8dfda;
--blue: #398ca5;
--blue-soft: #dcecf1;
--yellow: #d89c32;
--yellow-soft: #f7ebc9;
--wood: #a9683c;
--shadow: 0 18px 45px rgba(39, 60, 52, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: #dfe8df;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
-webkit-font-smoothing: antialiased;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
button:focus-visible,
input:focus-visible {
outline: 3px solid rgba(57, 140, 165, 0.32);
outline-offset: 2px;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#world-labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
min-width: 112px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 10px;
background: rgba(250, 248, 240, 0.87);
box-shadow: 0 8px 24px rgba(27, 51, 44, 0.13);
transform: translate(-50%, -100%);
text-align: center;
backdrop-filter: blur(8px);
transition: opacity 160ms ease;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 9px;
height: 9px;
border-right: 1px solid rgba(255, 255, 255, 0.58);
border-bottom: 1px solid rgba(255, 255, 255, 0.58);
background: rgba(250, 248, 240, 0.87);
transform: translateX(-50%) rotate(45deg);
}
.world-label strong,
.world-label span {
display: block;
}
.world-label strong {
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.world-label span {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.world-label.player-label {
border-top: 3px solid var(--player-color, var(--green));
}
.world-label.outage-label {
color: #fff5ef;
border-color: rgba(255, 255, 255, 0.22);
background: rgba(164, 60, 43, 0.91);
}
.world-label.outage-label span {
color: #ffe5d9;
}
.world-label.outage-label::after {
background: rgba(164, 60, 43, 0.91);
border-color: rgba(255, 255, 255, 0.22);
}
.panel,
.topbar {
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
}
.topbar {
position: fixed;
z-index: 20;
top: 20px;
left: 24px;
right: 24px;
height: 62px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 14px 0 18px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 16px;
background: rgba(250, 248, 240, 0.9);
box-shadow: 0 12px 35px rgba(39, 60, 52, 0.12);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 33px;
height: 33px;
border: 2px solid var(--ink);
border-radius: 50%;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
inset: 5px;
border: 2px solid var(--ink);
border-radius: 50%;
}
.brand-mark::after {
inset: 11px;
background: var(--orange);
border: 0;
}
.brand-kicker,
.eyebrow {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.brand-name {
margin-top: 2px;
font-size: 15px;
font-weight: 850;
letter-spacing: 0.12em;
}
.top-actions {
display: flex;
align-items: center;
gap: 8px;
}
.incident-chip,
.scope-chip {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 30px;
padding: 7px 10px;
border-radius: 999px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.incident-chip {
color: #9f3f31;
background: var(--red-soft);
}
.incident-chip::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--red);
box-shadow: 0 0 0 4px rgba(200, 73, 60, 0.1);
}
.scope-chip {
color: #3e6e58;
background: var(--green-soft);
}
.small-button,
.icon-button,
.primary-button,
.secondary-button {
border: 0;
border-radius: 10px;
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
opacity 140ms ease;
}
.small-button:hover,
.icon-button:hover,
.primary-button:hover,
.secondary-button:hover {
transform: translateY(-1px);
}
.small-button {
padding: 8px 11px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.52);
font-size: 11px;
font-weight: 750;
}
.icon-button {
width: 34px;
height: 34px;
background: rgba(255, 255, 255, 0.62);
font-size: 19px;
}
.objective-panel {
top: 102px;
left: 24px;
width: 270px;
padding: 20px;
}
.objective-title {
margin: 8px 0 8px;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.story {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.incident-story {
margin-top: 13px;
padding: 12px;
border-left: 3px solid var(--orange);
border-radius: 0 10px 10px 0;
background: var(--orange-soft);
color: #754938;
font-size: 11px;
line-height: 1.5;
}
.objective-subhead {
display: flex;
align-items: center;
justify-content: space-between;
margin: 20px 0 10px;
}
.objective-subhead strong {
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-list {
display: grid;
gap: 8px;
}
.metric {
padding: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.42);
}
.metric-top,
.metric-bottom {
display: flex;
align-items: center;
justify-content: space-between;
}
.metric-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 750;
}
.metric-value {
font-size: 11px;
font-weight: 800;
}
.metric-bottom {
margin-top: 7px;
color: var(--muted);
font-size: 9px;
}
.metric-progress {
height: 4px;
margin-top: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 52, 47, 0.08);
}
.metric-progress span {
display: block;
height: 100%;
border-radius: inherit;
background: var(--resource-color);
transition: width 240ms ease;
}
.resource-icon {
display: inline-block;
width: 13px;
height: 13px;
flex: 0 0 auto;
background: var(--resource-color);
}
.resource-icon.timber {
border-radius: 3px;
transform: rotate(-7deg);
}
.resource-icon.water {
border-radius: 70% 30% 65% 35%;
transform: rotate(45deg) scale(0.8);
}
.resource-icon.power {
border-radius: 3px;
transform: rotate(45deg) scale(0.76);
}
.active-status {
margin-top: 14px;
padding: 11px;
border-radius: 11px;
font-size: 10px;
line-height: 1.45;
}
.active-status.bad {
color: #923f34;
background: var(--red-soft);
}
.active-status.good {
color: #27694f;
background: var(--green-soft);
}
.active-status strong {
display: block;
margin-bottom: 3px;
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.text-button {
margin-top: 12px;
padding: 0;
border: 0;
color: var(--green);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.negotiation-panel {
top: 102px;
right: 24px;
width: 370px;
max-height: calc(100vh - 126px);
padding: 18px;
overflow-y: auto;
scrollbar-width: thin;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.panel-heading h2 {
margin: 5px 0 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.draft-badge {
padding: 6px 8px;
border-radius: 999px;
color: #83532e;
background: var(--yellow-soft);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
}
.panel-intro {
margin: 9px 0 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.player-cards {
display: grid;
gap: 8px;
}
.player-card {
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.45);
}
.player-header {
display: flex;
align-items: center;
gap: 8px;
}
.player-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--player-color);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--player-color) 15%, transparent);
}
.player-name {
font-size: 12px;
font-weight: 850;
}
.player-role {
margin-left: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
}
.sealed-promise {
margin-left: auto;
color: var(--muted);
font-size: 9px;
text-align: right;
}
.stock-line {
margin: 7px 0 9px;
color: var(--muted);
font-size: 9px;
}
.allocation-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 7px;
}
.allocation-row {
display: flex;
align-items: center;
justify-content: space-between;
min-width: 0;
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.6);
}
.allocation-row.has-error {
border-color: rgba(200, 73, 60, 0.55);
background: var(--red-soft);
}
.allocation-label {
display: flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.allocation-row input {
width: 43px;
padding: 4px 5px;
border: 1px solid rgba(23, 52, 47, 0.16);
border-radius: 7px;
color: var(--ink);
background: #fffef9;
font-size: 11px;
font-weight: 850;
text-align: center;
appearance: textfield;
}
.allocation-row input::-webkit-inner-spin-button,
.allocation-row input::-webkit-outer-spin-button {
margin: 0;
appearance: none;
}
.card-note {
margin-top: 7px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.reserve-card {
margin-top: 8px;
padding: 11px;
border: 1px dashed rgba(23, 52, 47, 0.22);
border-radius: 12px;
background: rgba(255, 255, 255, 0.28);
}
.reserve-card.open {
border-style: solid;
border-color: rgba(57, 140, 165, 0.35);
background: var(--blue-soft);
}
.reserve-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.reserve-header strong {
font-size: 11px;
}
.consent-label {
display: flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 9px;
cursor: pointer;
}
.consent-label input {
accent-color: var(--green);
}
.reserve-actions {
display: flex;
align-items: center;
gap: 9px;
margin-top: 9px;
}
.reserve-actions .allocation-row {
flex: 1;
}
.validation-box {
margin-top: 13px;
padding: 12px;
border-radius: 12px;
background: rgba(23, 52, 47, 0.055);
}
.validation-heading {
display: flex;
align-items: center;
gap: 7px;
font-size: 11px;
font-weight: 850;
}
.validation-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--red);
}
.validation-box.valid .validation-dot {
background: var(--green);
box-shadow: 0 0 0 4px rgba(47, 128, 99, 0.12);
}
.balance-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
margin: 9px 0;
}
.balance-pill {
padding: 7px 5px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.55);
text-align: center;
font-size: 9px;
}
.balance-pill strong {
display: block;
margin-bottom: 2px;
font-size: 10px;
}
.balance-pill.bad {
color: #983f33;
border-color: rgba(200, 73, 60, 0.28);
background: var(--red-soft);
}
.error-list {
display: grid;
gap: 4px;
margin: 8px 0 0;
padding: 0;
list-style: none;
color: #943f34;
font-size: 9px;
line-height: 1.4;
}
.error-list li::before {
content: "•";
margin-right: 6px;
}
.validation-actions {
display: grid;
grid-template-columns: 1fr 1.45fr;
gap: 8px;
margin-top: 12px;
}
.primary-button,
.secondary-button {
min-height: 38px;
padding: 9px 12px;
font-size: 10px;
font-weight: 850;
}
.primary-button {
color: #fff;
background: var(--ink);
}
.primary-button:disabled {
cursor: not-allowed;
opacity: 0.34;
transform: none;
}
.secondary-button {
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.62);
}
.alternatives-panel {
right: 418px;
bottom: 24px;
left: 318px;
padding: 14px;
}
.alternatives-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 10px;
}
.alternatives-heading strong {
font-size: 12px;
}
.alternatives-heading span {
color: var(--muted);
font-size: 9px;
}
.alternative-list {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.alternative-card {
position: relative;
min-height: 122px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.52);
cursor: pointer;
text-align: left;
transition:
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
.alternative-card:hover {
transform: translateY(-2px);
border-color: rgba(47, 128, 99, 0.35);
background: rgba(255, 255, 255, 0.8);
}
.alternative-card.selected {
border-color: var(--green);
background: var(--green-soft);
box-shadow: inset 0 0 0 1px rgba(47, 128, 99, 0.16);
}
.alternative-tag {
display: inline-block;
margin-bottom: 7px;
padding: 4px 6px;
border-radius: 999px;
color: var(--green);
background: var(--green-soft);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.alternative-card:nth-child(2) .alternative-tag {
color: #346f82;
background: var(--blue-soft);
}
.alternative-card:nth-child(3) .alternative-tag {
color: #8a5e2c;
background: var(--yellow-soft);
}
.alternative-card strong {
display: block;
font-size: 11px;
line-height: 1.25;
}
.alternative-card p {
margin: 6px 0;
color: var(--muted);
font-size: 9px;
line-height: 1.42;
}
.alternative-impact {
color: var(--ink);
font-size: 8px;
font-weight: 750;
}
.alternative-load {
position: absolute;
right: 11px;
bottom: 10px;
color: var(--green);
font-size: 8px;
font-weight: 850;
}
#toast {
position: fixed;
z-index: 40;
left: 50%;
bottom: 28px;
max-width: min(460px, calc(100vw - 40px));
padding: 11px 15px;
border-radius: 11px;
color: #fff;
background: rgba(23, 52, 47, 0.94);
box-shadow: 0 12px 35px rgba(23, 52, 47, 0.24);
font-size: 11px;
font-weight: 700;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
background: rgba(166, 61, 47, 0.96);
}
dialog {
width: min(540px, calc(100vw - 32px));
padding: 24px;
border: 1px solid rgba(255, 255, 255, 0.68);
border-radius: 18px;
color: var(--ink);
background: var(--paper-solid);
box-shadow: 0 25px 80px rgba(23, 52, 47, 0.28);
}
dialog::backdrop {
background: rgba(24, 47, 42, 0.34);
backdrop-filter: blur(5px);
}
dialog h2 {
margin: 6px 0 8px;
font-size: 23px;
letter-spacing: -0.03em;
}
dialog p,
dialog li {
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
dialog ul {
padding-left: 19px;
}
.dialog-footer {
display: flex;
justify-content: flex-end;
margin-top: 18px;
}
@media (max-width: 1180px) {
.objective-panel {
width: 235px;
}
.negotiation-panel {
width: 345px;
}
.alternatives-panel {
right: 393px;
left: 282px;
}
.alternative-card p {
display: none;
}
}
@media (max-height: 760px) and (min-width: 801px) {
.objective-panel {
padding: 15px;
}
.objective-title {
font-size: 21px;
}
.story {
display: none;
}
.incident-story {
margin-top: 9px;
padding: 9px;
}
.objective-subhead {
margin-top: 12px;
}
.metric {
padding: 7px;
}
.alternatives-panel {
bottom: 12px;
padding: 10px;
}
.alternative-card {
min-height: 94px;
padding: 9px;
}
}
@media (max-width: 800px) {
body {
overflow: auto;
}
#app {
min-height: 100%;
height: auto;
padding: 12px;
}
.topbar {
position: relative;
top: auto;
left: auto;
right: auto;
width: 100%;
margin-bottom: 12px;
}
.incident-chip {
display: none;
}
.objective-panel,
.negotiation-panel,
.alternatives-panel {
position: relative;
top: auto;
right: auto;
bottom: auto;
left: auto;
width: 100%;
max-height: none;
margin-bottom: 12px;
}
.objective-panel,
.negotiation-panel {
padding: 17px;
}
.alternatives-panel {
padding: 12px;
}
.alternative-list {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.alternative-card {
min-width: 220px;
scroll-snap-align: start;
}
.alternative-card p {
display: block;
}
#toast {
bottom: 14px;
}
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" aria-label="Low-poly cooperative resource negotiation scene"></canvas>
<div id="world-labels" aria-hidden="true"></div>
<header class="topbar">
<div class="brand">
<span class="brand-mark" aria-hidden="true"></span>
<div>
<div class="brand-kicker">Cooperative allocation lab</div>
<div class="brand-name">COMMON GROUND</div>
</div>
</div>
<div class="top-actions">
<div class="incident-chip">Rill canal offline</div>
<button
id="reset-camera"
class="icon-button"
title="Reset camera"
aria-label="Reset camera"
>
↺
</button>
<button id="rules-button" class="small-button">Rules</button>
</div>
</header>
<aside class="panel objective-panel">
<div class="eyebrow">Shared objective · Round 03</div>
<h1 class="objective-title">Light the Night Market</h1>
<p class="story">
The neighborhood cooperative is building a public beacon together.
Every promise must still be physically possible when the covenant is sealed.
</p>
<div class="incident-story">
<strong>16:40 — Supply interruption.</strong><br />
Rill’s canal gate has closed. Of the six crates of water Rill sealed,
only three can still be delivered.
</div>
<div class="objective-subhead">
<strong>Target resources</strong>
<span id="scope-chip" class="scope-chip">Full market</span>
</div>
<div id="objective-metrics" class="metric-list"></div>
<div id="active-status" class="active-status bad">
<strong>Active covenant is at risk</strong>
<span>Loading agreement status…</span>
</div>
<button id="objective-rules-button" class="text-button">
How agreement rules work →
</button>
</aside>
<aside class="panel negotiation-panel">
<div class="panel-heading">
<div>
<div class="eyebrow">Negotiation workspace</div>
<h2>Revise commitments</h2>
</div>
<span id="draft-badge" class="draft-badge">Needs revision</span>
</div>
<p class="panel-intro">
Change the working draft below. It does not replace the active covenant
until every rule passes and you seal the revision.
</p>
<div id="player-cards" class="player-cards"></div>
<div id="reserve-card" class="reserve-card">
<div class="reserve-header">
<strong>Emergency cistern</strong>
<label class="consent-label">
<input id="reserve-consent" type="checkbox" />
Unanimous consent
</label>
</div>
<div class="reserve-actions">
<div class="allocation-row">
<span class="allocation-label">
<i class="resource-icon water" style="--resource-color: var(--blue)"></i>
Water
</span>
<input
id="reserve-water"
data-source="reserve"
data-resource="water"
type="number"
min="0"
max="2"
value="0"
aria-label="Reserve water commitment"
disabled
/>
</div>
<span class="card-note">2 sealed water · access requires consent</span>
</div>
</div>
<div id="validation-box" class="validation-box">
<div class="validation-heading">
<span class="validation-dot"></span>
<span id="validation-title">Checking the draft…</span>
</div>
<div id="balance-grid" class="balance-grid"></div>
<ul id="error-list" class="error-list"></ul>
</div>
<div class="validation-actions">
<button id="reset-draft" class="secondary-button">Reset draft</button>
<button id="seal-draft" class="primary-button" disabled>
Seal feasible revision
</button>
</div>
</aside>
<section class="panel alternatives-panel">
<div class="alternatives-heading">
<strong>Inspect feasible alternatives</strong>
<span>Loading a plan never changes the sealed agreement automatically.</span>
</div>
<div id="alternative-list" class="alternative-list"></div>
</section>
<div id="toast" role="status" aria-live="polite"></div>
<dialog id="rules-dialog">
<div class="eyebrow">Agreement protocol</div>
<h2>No promise is allowed to break physics.</h2>
<p>
The Night Market beacon can only be activated when a sealed revision
satisfies all four checks. The previous covenant remains active while
players negotiate.
</p>
<ul>
<li><strong>Exact demand:</strong> committed resources must equal the target, not fall short or overcommit.</li>
<li><strong>Real capacity:</strong> nobody may promise more than they can physically deliver.</li>
<li><strong>Approved conversion:</strong> Ember may convert one unit of power into one unit of water.</li>
<li><strong>Consent for reserves:</strong> the emergency cistern opens only with unanimous consent.</li>
<li><strong>Explicit sealing:</strong> a feasible draft must be deliberately sealed before it becomes binding.</li>
</ul>
<p>
The beacon also supports a documented <strong>low-water cycle</strong>:
six water instead of eight, with all timber and power targets unchanged.
</p>
<div class="dialog-footer">
<button id="close-rules" class="primary-button">Got it</button>
</div>
</dialog>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const clamp = THREE.MathUtils.clamp;
const clone = (value) => JSON.parse(JSON.stringify(value));
const RESOURCE_META = {
timber: {
label: "Timber",
color: "#a9683c",
cssColor: "#a9683c"
},
water: {
label: "Water",
color: "#398ca5",
cssColor: "#398ca5"
},
power: {
label: "Power",
color: "#d89c32",
cssColor: "#d89c32"
}
};
const TARGETS = {
full: { timber: 14, water: 8, power: 4 },
compact: { timber: 14, water: 6, power: 4 }
};
const PLAYER_DEFS = [
{
id: "pine",
name: "Pine",
role: "Builder",
color: "#3d8b68",
position: { x: -5.4, z: 3.1 },
resources: [
{ key: "timber", label: "Timber", stock: 10 }
]
},
{
id: "rill",
name: "Rill",
role: "Waterkeeper",
color: "#398ca5",
position: { x: 5.4, z: 2.4 },
resources: [
{ key: "timber", label: "Timber", stock: 4 },
{ key: "water", label: "Water", stock: 3 }
]
},
{
id: "ember",
name: "Ember",
role: "Engineer",
color: "#d87845",
position: { x: 1.0, z: -5.2 },
resources: [
{ key: "water", label: "Water", stock: 2 },
{ key: "power", label: "Power", stock: 8 }
]
}
];
const SOURCE_LIMITS = {
pine: { timber: 10, water: 0, power: 0 },
rill: { timber: 4, water: 3, power: 0 },
ember: { timber: 0, water: 8, power: 8 },
reserve: { timber: 0, water: 2, power: 0 }
};
const RESERVE_POSITION = { x: -1.8, z: -6.4 };
function emptyCommitment() {
return { timber: 0, water: 0, power: 0 };
}
function initialState() {
return {
targetMode: "full",
reserveOpen: false,
pine: { timber: 10, water: 0, power: 0 },
rill: { timber: 4, water: 6, power: 0 },
ember: { timber: 0, water: 2, power: 4 },
reserve: emptyCommitment()
};
}
let active = initialState();
let draft = clone(active);
let loadedAlternative = null;
const ALTERNATIVES = [
{
id: "convert",
tag: "Recommended",
title: "Trade Ember’s surplus",
summary:
"Rill sends 3 water. Ember converts 3 power into 3 water while still delivering 4 power.",
impact: "Full objective · no new consensus",
targetMode: "full",
reserveOpen: false,
commitments: {
pine: { timber: 10, water: 0, power: 0 },
rill: { timber: 4, water: 3, power: 0 },
ember: { timber: 0, water: 5, power: 4 },
reserve: { timber: 0, water: 0, power: 0 }
}
},
{
id: "cistern",
tag: "Unanimous",
title: "Open the emergency cistern",
summary:
"The shared reserve contributes 2 water. Rill and Ember each deliver 3 water.",
impact: "Full objective · reserve consent required",
targetMode: "full",
reserveOpen: true,
commitments: {
pine: { timber: 10, water: 0, power: 0 },
rill: { timber: 4, water: 3, power: 0 },
ember: { timber: 0, water: 3, power: 4 },
reserve: { timber: 0, water: 2, power: 0 }
}
},
{
id: "compact",
tag: "Trade-off",
title: "Run the low-water cycle",
summary:
"Use the documented 6-water beacon cycle. Timber and power targets remain unchanged.",
impact: "Weaker public outcome · no new supply",
targetMode: "compact",
reserveOpen: false,
commitments: {
pine: { timber: 10, water: 0, power: 0 },
rill: { timber: 4, water: 3, power: 0 },
ember: { timber: 0, water: 3, power: 4 },
reserve: { timber: 0, water: 0, power: 0 }
}
}
];
function validateAgreement(state) {
const target = TARGETS[state.targetMode];
const totals = { timber: 0, water: 0, power: 0 };
const errors = [];
const conversions = { ember: 0 };
const sources = [
...PLAYER_DEFS.map((player) => player.id),
"reserve"
];
for (const source of sources) {
const allocation = state[source];
for (const resource of ["timber", "water", "power"]) {
totals[resource] += allocation[resource] || 0;
}
if (source === "reserve" && !state.reserveOpen) {
const reservedTotal =
allocation.timber + allocation.water + allocation.power;
if (reservedTotal > 0) {
errors.push({
source,
resource: allocation.water > 0 ? "water" : "timber",
message: "The emergency cistern cannot be used without unanimous consent."
});
}
}
}
for (const player of PLAYER_DEFS) {
const allocation = state[player.id];
for (const resource of Object.keys(SOURCE_LIMITS[player.id])) {
const limit = SOURCE_LIMITS[player.id][resource];
if (allocation[resource] > limit) {
errors.push({
source: player.id,
resource,
message: `${player.name} can deliver at most ${limit} ${RESOURCE_META[resource].label.toLowerCase()}.`
});
}
}
}
const reserveAllocation = state.reserve;
if (state.reserveOpen && reserveAllocation.water > 2) {
errors.push({
source: "reserve",
resource: "water",
message: "The emergency cistern contains only 2 water."
});
}
const emberWater = state.ember.water;
const convertedPower = Math.max(0, emberWater - 2);
conversions.ember = convertedPower;
const emberPowerUsed = state.ember.power + convertedPower;
if (emberPowerUsed > 8) {
errors.push({
source: "ember",
resource: "power",
message: `Ember needs ${emberPowerUsed} power for this plan; only 8 are available.`
});
}
for (const resource of ["timber", "water", "power"]) {
if (totals[resource] < target[resource]) {
errors.push({
source: "objective",
resource,
message: `${RESOURCE_META[resource].label} is short by ${target[resource] - totals[resource]}.`
});
} else if (totals[resource] > target[resource]) {
errors.push({
source: "objective",
resource,
message: `${RESOURCE_META[resource].label} is overcommitted by ${totals[resource] - target[resource]}.`
});
}
}
return {
valid: errors.length === 0,
errors,
totals,
target,
conversions
};
}
function formatCommitment(commitment) {
const parts = [];
for (const resource of ["timber", "water", "power"]) {
if (commitment[resource]) {
parts.push(`${commitment[resource]} ${RESOURCE_META[resource].label.toLowerCase()}`);
}
}
return parts.length ? parts.join(" · ") : "no delivery";
}
function buildPlayerCards() {
$("#player-cards").innerHTML = PLAYER_DEFS.map((player) => {
const rows = player.resources
.map((resource) => {
const limit = SOURCE_LIMITS[player.id][resource.key];
return `
<label class="allocation-row" data-row-source="${player.id}" data-row-resource="${resource.key}">
<span class="allocation-label">
<i class="resource-icon ${resource.key}" style="--resource-color: ${RESOURCE_META[resource.key].cssColor}"></i>
${resource.label}
</span>
<input
type="number"
min="0"
max="${limit}"
step="1"
value="${draft[player.id][resource.key]}"
data-source="${player.id}"
data-resource="${resource.key}"
aria-label="${player.name} ${resource.label} commitment"
/>
</label>
`;
})
.join("");
const stockText = player.resources
.map((resource) => `${resource.stock} ${resource.label.toLowerCase()}`)
.join(" · ");
const note =
player.id === "rill"
? "Canal delivery is capped at 3 water after the gate failure."
: player.id === "ember"
? "Ember may convert 1 power into 1 water."
: "Pine’s timber stockpile is fully available.";
return `
<section class="player-card" style="--player-color: ${player.color}">
<div class="player-header">
<span class="player-dot"></span>
<span class="player-name">${player.name}</span>
<span class="player-role">${player.role}</span>
<span class="sealed-promise">Sealed: ${formatCommitment(active[player.id])}</span>
</div>
<div class="stock-line">Available now: ${stockText}</div>
<div class="allocation-grid">${rows}</div>
<div class="card-note" id="note-${player.id}">${note}</div>
</section>
`;
}).join("");
}
function buildAlternatives() {
$("#alternative-list").innerHTML = ALTERNATIVES.map((alternative) => `
<button class="alternative-card" data-alternative="${alternative.id}">
<span class="alternative-tag">${alternative.tag}</span>
<strong>${alternative.title}</strong>
<p>${alternative.summary}</p>
<span class="alternative-impact">${alternative.impact}</span>
<span class="alternative-load">Load into draft →</span>
</button>
`).join("");
}
function syncInputs() {
document.querySelectorAll("input[data-resource]").forEach((input) => {
if (document.activeElement !== input) {
input.value = draft[input.dataset.source][input.dataset.resource];
}
});
$("#reserve-consent").checked = draft.reserveOpen;
$("#reserve-water").disabled = !draft.reserveOpen;
$("#reserve-card").classList.toggle("open", draft.reserveOpen);
}
function renderObjective() {
const validation = validateAgreement(draft);
const target = validation.target;
$("#scope-chip").textContent =
draft.targetMode === "compact" ? "Low-water cycle" : "Full market";
$("#objective-metrics").innerHTML = ["timber", "water", "power"]
.map((resource) => {
const meta = RESOURCE_META[resource];
const total = validation.totals[resource];
const hasIssue = validation.errors.some(
(error) => error.resource === resource
);
const percent = clamp((total / target[resource]) * 100, 0, 100);
return `
<div class="metric">
<div class="metric-top">
<span class="metric-name">
<i class="resource-icon ${resource}" style="--resource-color: ${meta.cssColor}"></i>
${meta.label}
</span>
<span class="metric-value" style="${hasIssue ? "color: var(--red)" : ""}">
${total} / ${target[resource]}
</span>
</div>
<div class="metric-bottom">
<span>planned delivery</span>
<span>${hasIssue ? "rule issue" : "balanced"}</span>
</div>
<div class="metric-progress">
<span style="width: ${percent}%; --resource-color: ${meta.cssColor}"></span>
</div>
</div>
`;
})
.join("");
const activeValidation = validateAgreement(active);
if (activeValidation.valid) {
$("#active-status").className = "active-status good";
$("#active-status").innerHTML = `
<strong>Active covenant is feasible</strong>
<span>The sealed revision meets every target and delivery limit.</span>
`;
} else {
const waterGap = Math.max(0, active.rill.water - SOURCE_LIMITS.rill.water);
const powerGap = Math.max(0, active.ember.power - 8);
$("#active-status").className = "active-status bad";
$("#active-status").innerHTML = `
<strong>Active covenant is at risk</strong>
<span>
${
waterGap
? `Rill’s sealed promise exceeds current canal supply by ${waterGap} water.`
: powerGap
? `Ember is overcommitted by ${powerGap} power.`
: "The sealed agreement no longer satisfies the current rules."
}
</span>
`;
}
}
function renderValidation() {
const validation = validateAgreement(draft);
$("#validation-box").classList.toggle("valid", validation.valid);
$("#draft-badge").textContent = validation.valid ? "Feasible draft" : "Needs revision";
$("#draft-badge").style.color = validation.valid ? "#2f8063" : "#83532e";
$("#draft-badge").style.background = validation.valid
? "var(--green-soft)"
: "var(--yellow-soft)";
$("#validation-title").textContent = validation.valid
? "Every game rule passes"
: `${validation.errors.length} rule ${validation.errors.length === 1 ? "issue" : "issues"} block sealing`;
$("#balance-grid").innerHTML = ["timber", "water", "power"]
.map((resource) => {
const hasIssue = validation.errors.some(
(error) => error.resource === resource
);
return `
<div class="balance-pill ${hasIssue ? "bad" : ""}">
<strong>${validation.totals[resource]} / ${validation.target[resource]}</strong>
${RESOURCE_META[resource].label}
</div>
`;
})
.join("");
$("#error-list").innerHTML = validation.valid
? `<li style="color: var(--green)">The draft can be sealed without breaking any rules.</li>`
: validation.errors
.slice(0, 4)
.map((error) => `<li>${error.message}</li>`)
.join("") +
(validation.errors.length > 4
? `<li>${validation.errors.length - 4} additional issue(s) need attention.</li>`
: "");
$("#seal-draft").disabled = !validation.valid;
document.querySelectorAll(".allocation-row[data-row-source]").forEach((row) => {
const source = row.dataset.rowSource;
const resource = row.dataset.rowResource;
const hasError = validation.errors.some(
(error) => error.source === source && error.resource === resource
);
row.classList.toggle("has-error", hasError);
});
const converted = validation.conversions.ember;
$("#note-ember").textContent = converted
? `${converted} power converted to ${converted} water · ${draft.ember.power + converted} of 8 power used.`
: "Ember may convert 1 power into 1 water. No conversion in this draft.";
document.querySelectorAll(".alternative-card").forEach((card) => {
card.classList.toggle("selected", card.dataset.alternative === loadedAlternative);
});
}
function refreshUI() {
syncInputs();
renderObjective();
renderValidation();
}
let toastTimer;
function showToast(message, type = "info") {
const toast = $("#toast");
toast.textContent = message;
toast.className = type === "error" ? "show error" : "show";
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.className = "";
}, 3400);
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const canvas = $("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8df);
scene.fog = new THREE.Fog(0xdfe8df, 18, 35);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(0xf7f2df, 0x5f756c, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.25);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.0004;
scene.add(sun);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
const M = {
ground: material(0x829b87),
groundEdge: material(0x607c6c),
path: material(0xb5b79c),
platform: material(0xd8d1b8),
platformEdge: material(0xa89f83),
timber: material(0x9e6338),
timberLight: material(0xc18752),
darkTimber: material(0x6d4730),
water: material(0x398ca5, { roughness: 0.38 }),
power: material(0xe2a83d, {
emissive: 0x9f5c0d,
emissiveIntensity: 0.35,
roughness: 0.35
}),
stone: material(0x8e9d91),
cream: material(0xf0e8cf),
ink: material(0x263d37),
inactive: material(0x9ca99f)
};
function addMesh(parent, geometry, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const shadowPlane = addMesh(
scene,
new THREE.PlaneGeometry(200, 200),
material(0xdfe8df),
0,
-0.68,
0
);
shadowPlane.rotation.x = -Math.PI / 2;
shadowPlane.castShadow = false;
const board = addMesh(
scene,
new THREE.CylinderGeometry(10.3, 10.7, 0.62, 12),
M.ground,
0,
-0.35,
0
);
board.castShadow = false;
const boardTop = addMesh(
scene,
new THREE.CylinderGeometry(9.85, 9.85, 0.12, 12),
material(0x91a890),
0,
-0.01,
0
);
boardTop.castShadow = false;
const centralPlatform = addMesh(
scene,
new THREE.CylinderGeometry(3.25, 3.55, 0.28, 10),
M.platform,
0,
0.12,
0
);
centralPlatform.castShadow = false;
const centralRim = addMesh(
scene,
new THREE.TorusGeometry(3.25, 0.12, 5, 24),
M.platformEdge,
0,
0.28,
0
);
centralRim.rotation.x = Math.PI / 2;
function createPath(source, destination, color = 0xb5b79c, width = 0.42) {
const dx = destination.x - source.x;
const dz = destination.z - source.z;
const length = Math.hypot(dx, dz);
const path = addMesh(
scene,
new THREE.BoxGeometry(width, 0.045, length),
material(color),
(source.x + destination.x) / 2,
0.085,
(source.z + destination.z) / 2
);
path.rotation.y = Math.atan2(dx, dz);
path.castShadow = false;
return path;
}
PLAYER_DEFS.forEach((player) => {
createPath(player.position, { x: 0, z: 0 });
});
const beacon = new THREE.Group();
scene.add(beacon);
addMesh(
beacon,
new THREE.CylinderGeometry(1.42, 1.68, 0.42, 8),
M.stone,
0,
0.42,
0
);
const postPositions = [
[-0.76, -0.76],
[0.76, -0.76],
[-0.76, 0.76],
[0.76, 0.76]
];
postPositions.forEach(([x, z]) => {
addMesh(
beacon,
new THREE.BoxGeometry(0.24, 2.35, 0.24),
M.timber,
x,
1.76,
z
);
});
addMesh(
beacon,
new THREE.BoxGeometry(2.15, 0.2, 2.15),
M.timberLight,
0,
3.02,
0
);
const roof = addMesh(
beacon,
new THREE.ConeGeometry(1.78, 1.08, 4),
material(0xc87d48),
0,
3.65,
0
);
roof.rotation.y = Math.PI / 4;
const coreMaterial = material(0xf0a23c, {
emissive: 0xf07820,
emissiveIntensity: 1.3,
roughness: 0.24,
metalness: 0.08
});
const beaconCore = addMesh(
beacon,
new THREE.OctahedronGeometry(0.55, 0),
coreMaterial,
0,
1.66,
0
);
const beaconLight = new THREE.PointLight(0xffb45a, 4, 8, 2);
beaconLight.position.set(0, 1.8, 0);
scene.add(beaconLight);
const playerGroups = {};
function createPlayer(player, index) {
const group = new THREE.Group();
group.position.set(player.position.x, 0.08, player.position.z);
scene.add(group);
const playerMaterial = material(player.color);
const skin = material(0xd5aa7d);
const dark = material(0x33443d);
addMesh(
group,
new THREE.CylinderGeometry(0.34, 0.44, 0.76, 6),
playerMaterial,
0,
0.94,
0
);
addMesh(
group,
new THREE.IcosahedronGeometry(0.31, 0),
skin,
0,
1.55,
0
);
addMesh(
group,
new THREE.ConeGeometry(0.38, 0.26, 6),
playerMaterial,
0,
1.86,
0
);
addMesh(
group,
new THREE.BoxGeometry(0.25, 0.46, 0.32),
dark,
-0.19,
0.3,
0
);
addMesh(
group,
new THREE.BoxGeometry(0.25, 0.46, 0.32),
dark,
0.19,
0.3,
0
);
addMesh(
group,
new THREE.BoxGeometry(0.18, 0.56, 0.22),
playerMaterial,
-0.46,
0.9,
0
);
addMesh(
group,
new THREE.BoxGeometry(0.18, 0.56, 0.22),
playerMaterial,
0.46,
0.9,
0
);
const ring = addMesh(
group,
new THREE.TorusGeometry(0.63, 0.075, 5, 18),
material(player.color),
0,
0.08,
0
);
ring.rotation.x = Math.PI / 2;
ring.castShadow = false;
playerGroups[player.id] = {
group,
baseY: group.position.y,
index
};
}
PLAYER_DEFS.forEach(createPlayer);
// Player resource props.
const pineProps = new THREE.Group();
pineProps.position.set(-6.55, 0.12, 3.75);
scene.add(pineProps);
for (let i = 0; i < 5; i += 1) {
const log = addMesh(
pineProps,
new THREE.CylinderGeometry(0.17, 0.17, 1.9, 6),
i % 2 ? M.timber : M.timberLight,
(i % 3) * 0.13,
0.2 + Math.floor(i / 3) * 0.28,
Math.floor(i / 3) * 0.35
);
log.rotation.z = Math.PI / 2;
}
const rillProps = new THREE.Group();
rillProps.position.set(6.35, 0.08, 3.35);
scene.add(rillProps);
for (let i = 0; i < 3; i += 1) {
addMesh(
rillProps,
new THREE.CylinderGeometry(0.43, 0.5, 0.9, 8),
M.water,
i * 0.72 - 0.72,
0.46,
(i % 2) * 0.24
);
}
const emberProps = new THREE.Group();
emberProps.position.set(2.2, 0.08, -5.4);
scene.add(emberProps);
addMesh(
emberProps,
new THREE.BoxGeometry(1.45, 0.72, 1.0),
M.ink,
0,
0.38,
0
);
for (let i = 0; i < 3; i += 1) {
const crystal = addMesh(
emberProps,
new THREE.OctahedronGeometry(0.32, 0),
M.power,
-0.48 + i * 0.48,
1.02,
0
);
crystal.rotation.y = i * 0.6;
crystal.scale.y = 1.35;
}
const reserveProp = new THREE.Group();
reserveProp.position.set(RESERVE_POSITION.x, 0.08, RESERVE_POSITION.z);
scene.add(reserveProp);
const reserveTankMaterial = M.water.clone();
reserveTankMaterial.transparent = true;
reserveTankMaterial.opacity = 0.35;
addMesh(
reserveProp,
new THREE.CylinderGeometry(0.65, 0.72, 1.25, 8),
reserveTankMaterial,
0,
0.66,
0
);
const reserveLock = addMesh(
reserveProp,
new THREE.TorusGeometry(0.48, 0.07, 5, 12),
material(0x74867b),
0,
0.86,
0.65
);
reserveLock.rotation.x = Math.PI / 2;
// Rill's interruption beacon.
const outageGroup = new THREE.Group();
outageGroup.position.set(4.45, 0.05, 1.15);
scene.add(outageGroup);
addMesh(
outageGroup,
new THREE.CylinderGeometry(0.09, 0.12, 1.25, 6),
M.ink,
0,
0.62,
0
);
const outageMaterial = material(0xd84d39, {
emissive: 0xb7271c,
emissiveIntensity: 1.6
});
const outageLamp = addMesh(
outageGroup,
new THREE.OctahedronGeometry(0.28, 0),
outageMaterial,
0,
1.38,
0
);
const outageLight = new THREE.PointLight(0xe14c36, 2.2, 4, 2);
outageLight.position.set(0, 1.38, 0);
outageGroup.add(outageLight);
const dryCanal = addMesh(
scene,
new THREE.BoxGeometry(0.7, 0.035, 2.7),
material(0xb47d5b),
7.35,
0.09,
1.45
);
dryCanal.rotation.y = -0.28;
// Resource progress markers around the beacon.
const progressMarkers = [];
function createProgressMarker(resource, index, count, angleStart, angleEnd) {
let geometry;
if (resource === "timber") {
geometry = new THREE.BoxGeometry(0.22, 0.42, 0.14);
} else if (resource === "water") {
geometry = new THREE.OctahedronGeometry(0.18, 0);
} else {
geometry = new THREE.OctahedronGeometry(0.2, 0);
}
const markerMaterial = material(RESOURCE_META[resource].color, {
transparent: true,
opacity: 1
});
const t = count === 1 ? 0 : index / (count - 1);
const angle = THREE.MathUtils.lerp(angleStart, angleEnd, t);
const radius = 2.72;
const mesh = addMesh(
scene,
geometry,
markerMaterial,
Math.sin(angle) * radius,
0.36,
Math.cos(angle) * radius
);
mesh.rotation.y = angle;
mesh.castShadow = false;
progressMarkers.push({
mesh,
resource,
index
});
}
for (let i = 0; i < 14; i += 1) {
createProgressMarker("timber", i, 14, -2.65, -1.25);
}
for (let i = 0; i < 8; i += 1) {
createProgressMarker("water", i, 8, 3.45, 4.9);
}
for (let i = 0; i < 4; i += 1) {
createProgressMarker("power", i, 4, -0.85, 0.65);
}
// Animated delivery packets.
const packetGroup = new THREE.Group();
scene.add(packetGroup);
const packetGeometry = new THREE.BoxGeometry(0.19, 0.19, 0.19);
const packetMaterials = {
timber: material(RESOURCE_META.timber.color),
water: material(RESOURCE_META.water.color),
power: material(RESOURCE_META.power.color),
blocked: material(0xd84d39, {
emissive: 0xa3241b,
emissiveIntensity: 0.55
})
};
const packetMeta = [];
function sourceHasCapacityIssue(state, source, resource) {
const validation = validateAgreement(state);
return validation.errors.some(
(error) => error.source === source && error.resource === resource
);
}
function rebuildPackets(state) {
while (packetGroup.children.length) {
packetGroup.remove(packetGroup.children[0]);
}
packetMeta.length = 0;
const sources = [
...PLAYER_DEFS.map((player) => ({
id: player.id,
position: player.position
})),
{
id: "reserve",
position: RESERVE_POSITION
}
];
sources.forEach((source) => {
const allocation = state[source.id];
["timber", "water", "power"].forEach((resource) => {
const amount = allocation[resource] || 0;
if (!amount) return;
const count = Math.ceil(amount / 2);
const blocked = sourceHasCapacityIssue(state, source.id, resource);
for (let i = 0; i < count; i += 1) {
const mesh = new THREE.Mesh(
packetGeometry,
blocked ? packetMaterials.blocked : packetMaterials[resource]
);
mesh.castShadow = true;
packetGroup.add(mesh);
packetMeta.push({
mesh,
source: new THREE.Vector3(source.position.x, 0.95, source.position.z),
target: new THREE.Vector3(
Math.sin(i * 2.1 + source.position.x) * 0.48,
1.4 + (i % 2) * 0.22,
Math.cos(i * 1.7 + source.position.z) * 0.48
),
phase: i / count + source.position.x * 0.03,
blocked
});
}
});
});
}
function updateSceneState() {
const validation = validateAgreement(draft);
const completed = validation.valid;
coreMaterial.color.setHex(completed ? 0x70c792 : 0xf0a23c);
coreMaterial.emissive.setHex(completed ? 0x2f9864 : 0xf07820);
coreMaterial.emissiveIntensity = completed ? 1.65 : 1.0;
beaconLight.color.setHex(completed ? 0x83e3ac : 0xffb45a);
progressMarkers.forEach((marker) => {
const active = marker.index < validation.totals[marker.resource];
const color = active
? RESOURCE_META[marker.resource].color
: "#9ca99f";
marker.mesh.material.color.set(color);
marker.mesh.material.opacity = active ? 1 : 0.32;
marker.mesh.scale.setScalar(active ? 1 : 0.56);
marker.mesh.position.y = active ? 0.38 : 0.26;
});
reserveTankMaterial.opacity = draft.reserveOpen ? 1 : 0.3;
reserveTankMaterial.color.setHex(draft.reserveOpen ? 0x398ca5 : 0x849b99);
reserveLock.visible = !draft.reserveOpen;
rebuildPackets(draft);
updateWorldLabelText(validation);
}
// ------------------------------------------------------------
// World-space HTML labels
// ------------------------------------------------------------
const labelElements = {};
function createLabel(className) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
$("#world-labels").appendChild(element);
return element;
}
PLAYER_DEFS.forEach((player) => {
labelElements[player.id] = createLabel("player-label");
labelElements[player.id].style.setProperty("--player-color", player.color);
});
labelElements.outage = createLabel("outage-label");
labelElements.project = createLabel("project-label");
function updateWorldLabelText(validation) {
PLAYER_DEFS.forEach((player) => {
labelElements[player.id].innerHTML = `
<strong>${player.name}</strong>
<span>${formatCommitment(draft[player.id])}</span>
`;
});
labelElements.outage.innerHTML = `
<strong>Canal interruption</strong>
<span>3 promised water unavailable</span>
`;
labelElements.project.innerHTML = `
<strong>Night Market Beacon</strong>
<span>${
validation.valid
? "Working draft is feasible"
: `${validation.target.water - validation.totals.water} water short`
}</span>
`;
}
const labelAnchors = {
project: new THREE.Vector3(0, 4.45, 0),
outage: new THREE.Vector3(4.45, 2.05, 1.15)
};
PLAYER_DEFS.forEach((player) => {
labelAnchors[player.id] = new THREE.Vector3(
player.position.x,
2.35,
player.position.z
);
});
function positionLabel(element, point) {
const projected = point.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
if (!visible) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = "1";
}
function updateLabels() {
Object.entries(labelAnchors).forEach(([key, anchor]) => {
positionLabel(labelElements[key], anchor);
});
}
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
const cameraState = {
radius: 16.7,
theta: 0.72,
phi: 0.81,
target: new THREE.Vector3(0, 1.05, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 16.7;
cameraState.theta = 0.72;
cameraState.phi = 0.81;
updateCamera();
}
let dragging = false;
let previousPointer = { x: 0, y: 0 };
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointer = { x: event.clientX, y: event.clientY };
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
cameraState.theta -= dx * 0.007;
cameraState.phi = clamp(cameraState.phi + dy * 0.006, 0.42, 1.24);
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
});
const stopDragging = () => {
dragging = false;
};
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.012,
10.5,
23
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.theta += 0.12;
if (event.key === "ArrowRight") cameraState.theta -= 0.12;
if (event.key === "ArrowUp") cameraState.phi = clamp(cameraState.phi - 0.08, 0.42, 1.24);
if (event.key === "ArrowDown") cameraState.phi = clamp(cameraState.phi + 0.08, 0.42, 1.24);
if (event.key === "r" || event.key === "R") resetCamera();
updateCamera();
});
// ------------------------------------------------------------
// Interaction
// ------------------------------------------------------------
$("#player-cards").addEventListener("change", (event) => {
const input = event.target;
if (!input.matches("input[data-resource]")) return;
const source = input.dataset.source;
const resource = input.dataset.resource;
const value = Number.parseInt(input.value, 10);
draft[source][resource] = Number.isFinite(value) ? Math.max(0, value) : 0;
loadedAlternative = null;
refreshUI();
updateSceneState();
});
$("#reserve-consent").addEventListener("change", (event) => {
draft.reserveOpen = event.target.checked;
if (!draft.reserveOpen) {
draft.reserve = emptyCommitment();
}
loadedAlternative = null;
refreshUI();
updateSceneState();
});
$("#reserve-water").addEventListener("change", (event) => {
const value = Number.parseInt(event.target.value, 10);
draft.reserve.water = Number.isFinite(value) ? clamp(value, 0, 2) : 0;
loadedAlternative = null;
refreshUI();
updateSceneState();
});
$("#alternative-list").addEventListener("click", (event) => {
const card = event.target.closest("[data-alternative]");
if (!card) return;
const alternative = ALTERNATIVES.find(
(item) => item.id === card.dataset.alternative
);
draft.targetMode = alternative.targetMode;
draft.reserveOpen = alternative.reserveOpen;
for (const source of ["pine", "rill", "ember", "reserve"]) {
draft[source] = clone(alternative.commitments[source]);
}
loadedAlternative = alternative.id;
refreshUI();
updateSceneState();
showToast(
`${alternative.title} loaded into the draft. Review and seal it to update the covenant.`
);
});
$("#reset-draft").addEventListener("click", () => {
draft = clone(active);
loadedAlternative = null;
refreshUI();
updateSceneState();
showToast("Draft reset to the currently sealed agreement.");
});
$("#seal-draft").addEventListener("click", () => {
const validation = validateAgreement(draft);
if (!validation.valid) {
showToast("This revision cannot be sealed until every rule issue is fixed.", "error");
$("#validation-box").animate(
[
{ transform: "translateX(0)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(5px)" },
{ transform: "translateX(0)" }
],
{ duration: 260 }
);
return;
}
active = clone(draft);
loadedAlternative = null;
refreshUI();
updateSceneState();
showToast("Revision sealed. The covenant now satisfies every resource and consent rule.");
});
const rulesDialog = $("#rules-dialog");
function openRules() {
if (typeof rulesDialog.showModal === "function") {
rulesDialog.showModal();
} else {
rulesDialog.setAttribute("open", "");
}
}
$("#rules-button").addEventListener("click", openRules);
$("#objective-rules-button").addEventListener("click", openRules);
$("#close-rules").addEventListener("click", () => rulesDialog.close());
$("#reset-camera").addEventListener("click", resetCamera);
rulesDialog.addEventListener("click", (event) => {
if (event.target === rulesDialog) {
rulesDialog.close();
}
});
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
updateCamera();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
const packetPosition = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
Object.values(playerGroups).forEach(({ group, baseY, index }) => {
group.position.y = baseY + Math.sin(time * 1.45 + index * 1.7) * 0.045;
});
beaconCore.rotation.y = time * 0.72;
beaconCore.rotation.x = Math.sin(time * 0.5) * 0.12;
beaconCore.position.y = 1.66 + Math.sin(time * 1.7) * 0.08;
const outagePulse = 0.5 + 0.5 * Math.sin(time * 4.2);
outageLamp.scale.setScalar(0.86 + outagePulse * 0.24);
outageMaterial.emissiveIntensity = 0.8 + outagePulse * 1.8;
outageLight.intensity = 0.8 + outagePulse * 2.6;
packetMeta.forEach((packet) => {
let t = (time * 0.19 + packet.phase) % 1;
if (packet.blocked) {
t = Math.min(t, 0.43);
}
packetPosition.lerpVectors(packet.source, packet.target, t);
packetPosition.y += Math.sin(Math.PI * t) * 1.25;
packet.mesh.position.copy(packetPosition);
packet.mesh.rotation.set(time * 1.4, time * 1.9, time * 1.1);
packet.mesh.scale.setScalar(packet.blocked ? 0.78 : 1);
});
updateLabels();
renderer.render(scene, camera);
}
buildPlayerCards();
buildAlternatives();
refreshUI();
updateSceneState();
resize();
animate();
setTimeout(() => {
showToast(
"Supply interruption detected: Rill’s canal can deliver 3 fewer water than promised."
);
}, 700);
</script>
</body>
</html>
```