Prompt specification
Build or customize the Three.js 3D scene "River Settlement Cooperation Game" 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: River Settlement Cooperation Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/river-settlement-cooperation-game-x3555
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring negotiated agreements between game players. Build an original board where neighboring settlements share a ferry and reservoir. Use explicit agreements and visible resource accounting without assuming all preferences are identical. Voxel style.
Working Three.js Starter (river-settlement-cooperation-game-x3555):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tide Commons — Voxel Agreement Board</title>
<style>
:root {
--ink: #193936;
--muted: #637975;
--paper: #f7f3e8;
--paper-2: #eee8da;
--line: #d1ccbb;
--green: #3f8767;
--green-dark: #26624c;
--blue: #397b94;
--orange: #d27a3d;
--red: #b75045;
--shadow: 0 12px 35px rgba(24, 52, 48, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #c8ded8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button {
color: inherit;
}
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 3px solid rgba(57, 123, 148, 0.35);
outline-offset: 2px;
}
#app {
width: 100%;
height: 100%;
display: grid;
grid-template-rows: 66px 1fr;
}
.topbar {
position: relative;
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
background: var(--paper);
border-bottom: 2px solid var(--line);
box-shadow: 0 4px 15px rgba(24, 52, 48, 0.08);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 240px;
}
.brand-mark {
width: 34px;
height: 34px;
position: relative;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 2px;
transform: rotate(-2deg);
}
.brand-mark i {
display: block;
background: var(--green);
}
.brand-mark i:nth-child(1) { grid-column: 2; background: var(--blue); }
.brand-mark i:nth-child(2) { grid-column: 1; grid-row: 2; background: var(--blue); }
.brand-mark i:nth-child(3) { grid-column: 2; grid-row: 2; }
.brand-mark i:nth-child(4) { grid-column: 3; grid-row: 2; background: var(--blue); }
.brand-mark i:nth-child(5) { grid-column: 2; grid-row: 3; background: var(--green-dark); }
.brand-title {
margin: 0;
font-size: 18px;
line-height: 1;
letter-spacing: 0.13em;
font-weight: 850;
}
.brand-subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
font-weight: 750;
}
.day-status {
display: flex;
align-items: center;
gap: 10px;
padding: 9px 13px;
border: 1px solid var(--line);
background: #fffdf6;
border-radius: 4px;
font-size: 12px;
letter-spacing: 0.04em;
}
.day-dot {
width: 8px;
height: 8px;
background: var(--green);
box-shadow: 0 0 0 4px rgba(63, 135, 103, 0.13);
}
.top-actions {
display: flex;
gap: 8px;
}
.button {
border: 1px solid var(--line);
background: #fffdf6;
padding: 9px 12px;
border-radius: 3px;
cursor: pointer;
font-size: 12px;
font-weight: 760;
transition: transform 120ms ease, background 120ms ease;
}
.button:hover {
background: white;
transform: translateY(-1px);
}
.button.primary {
background: var(--green-dark);
border-color: var(--green-dark);
color: white;
}
.button.primary:hover {
background: #1d5742;
}
.button.orange {
background: var(--orange);
border-color: var(--orange);
color: white;
}
.button.subtle {
background: transparent;
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 280px minmax(320px, 1fr) 360px;
position: relative;
}
.panel {
min-height: 0;
overflow: auto;
padding: 17px;
background: rgba(247, 243, 232, 0.96);
z-index: 10;
scrollbar-width: thin;
scrollbar-color: #b7b8aa transparent;
}
.left-panel {
border-right: 2px solid var(--line);
}
.right-panel {
border-left: 2px solid var(--line);
}
.panel-kicker {
margin: 0 0 4px;
color: var(--orange);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel-title {
margin: 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.panel-intro {
margin: 8px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.settlement-list {
display: grid;
gap: 9px;
}
.settlement-card {
width: 100%;
text-align: left;
border: 1px solid var(--line);
background: rgba(255, 253, 246, 0.92);
border-radius: 4px;
padding: 11px;
cursor: pointer;
transition: border-color 130ms ease, transform 130ms ease, box-shadow 130ms ease;
}
.settlement-card:hover {
transform: translateY(-1px);
border-color: #9aa99f;
box-shadow: 0 6px 16px rgba(31, 59, 52, 0.07);
}
.settlement-card.active {
border-color: var(--settlement-color);
box-shadow: inset 4px 0 0 var(--settlement-color);
}
.settlement-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.settlement-name {
display: flex;
align-items: center;
gap: 7px;
font-size: 13px;
font-weight: 850;
}
.settlement-swatch {
width: 9px;
height: 9px;
background: var(--settlement-color);
box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.12);
}
.player-name {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.inventory {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
margin-top: 10px;
}
.inventory-item {
padding: 5px 6px;
background: var(--paper-2);
border-radius: 2px;
}
.inventory-item small {
display: block;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.inventory-item strong {
display: block;
margin-top: 2px;
font-size: 14px;
}
.priority {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 9px;
color: var(--muted);
font-size: 9px;
}
.priority b {
color: var(--ink);
}
.preference-bars {
display: flex;
gap: 3px;
height: 4px;
margin-top: 7px;
}
.preference-bars i {
display: block;
height: 100%;
border-radius: 1px;
}
.viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #c8ded8;
}
#canvasHost,
#labelLayer {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labelLayer {
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
pointer-events: auto;
transform: translate(-50%, -100%);
padding: 5px 8px;
border: 1px solid rgba(25, 57, 54, 0.22);
background: rgba(255, 253, 246, 0.91);
box-shadow: 0 4px 12px rgba(20, 50, 45, 0.12);
border-radius: 3px;
white-space: nowrap;
cursor: pointer;
}
.world-label strong {
display: block;
font-size: 10px;
letter-spacing: 0.02em;
}
.world-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
}
.board-title {
position: absolute;
top: 16px;
left: 18px;
pointer-events: none;
padding: 9px 12px;
background: rgba(247, 243, 232, 0.78);
border-left: 3px solid var(--orange);
}
.board-title strong {
display: block;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.board-title span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.legend {
position: absolute;
left: 18px;
bottom: 17px;
display: flex;
gap: 12px;
align-items: center;
padding: 8px 10px;
background: rgba(247, 243, 232, 0.82);
border: 1px solid rgba(25, 57, 54, 0.12);
font-size: 9px;
pointer-events: none;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 8px;
height: 8px;
display: inline-block;
}
.legend .active { background: var(--green); }
.legend .pending { background: var(--orange); }
.legend .shared { background: var(--blue); }
.camera-help {
position: absolute;
right: 16px;
bottom: 17px;
color: rgba(25, 57, 54, 0.7);
font-size: 9px;
text-align: right;
pointer-events: none;
}
#toast {
position: absolute;
left: 50%;
bottom: 62px;
transform: translate(-50%, 12px);
max-width: min(420px, 80%);
padding: 10px 14px;
color: white;
background: rgba(25, 57, 54, 0.94);
border-left: 4px solid var(--orange);
box-shadow: var(--shadow);
font-size: 12px;
opacity: 0;
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
z-index: 30;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.field-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 7px;
font-size: 11px;
font-weight: 780;
}
.field-label output {
color: var(--green-dark);
font-size: 10px;
font-weight: 850;
}
select {
width: 100%;
border: 1px solid var(--line);
background: #fffdf6;
color: var(--ink);
padding: 9px;
border-radius: 3px;
cursor: pointer;
font-size: 11px;
}
.pair-route {
margin: 12px 0;
padding: 12px;
background: #e5eee9;
border: 1px solid #c9d9d0;
border-radius: 4px;
}
.pair-route strong {
display: block;
font-size: 13px;
}
.pair-route span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.status-pill {
display: inline-flex;
align-items: center;
gap: 5px;
margin-bottom: 8px;
padding: 4px 7px;
border-radius: 2px;
background: #dce8df;
color: var(--green-dark);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-pill.pending {
background: #f3dfcc;
color: #99531f;
}
.status-pill.none {
background: #e5e1d5;
color: #6e746e;
}
.terms-card,
.proposal-card,
.evaluation-card {
margin: 10px 0;
border: 1px solid var(--line);
background: #fffdf6;
border-radius: 4px;
padding: 12px;
}
.proposal-card {
border-color: #e0ae7f;
box-shadow: inset 4px 0 0 var(--orange);
}
.terms-title {
margin: 0 0 9px;
font-size: 12px;
font-weight: 850;
}
.term-row {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 5px 0;
border-bottom: 1px dashed #ddd8ca;
font-size: 10px;
}
.term-row:last-child {
border-bottom: 0;
}
.term-row span {
color: var(--muted);
}
.term-row b {
text-align: right;
}
.evaluation-card {
background: #edf2ec;
border-color: #cedbd0;
}
.evaluation-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
font-size: 11px;
font-weight: 800;
}
.score {
display: inline-block;
min-width: 43px;
padding: 4px 6px;
color: white;
background: var(--green);
text-align: center;
border-radius: 2px;
font-size: 10px;
}
.evaluation-card p {
margin: 8px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.form-section {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.form-section h3 {
margin: 0 0 12px;
font-size: 11px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.form-field {
margin-bottom: 14px;
}
input[type="range"] {
width: 100%;
accent-color: var(--green);
cursor: pointer;
}
.exchange-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.exchange-grid label {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
}
.exchange-grid select {
margin-top: 5px;
}
.fine-print {
margin: 8px 0 12px;
padding: 9px;
color: var(--muted);
background: #ece8dc;
border-radius: 3px;
font-size: 9px;
line-height: 1.5;
}
.action-row {
display: flex;
gap: 7px;
margin-top: 10px;
}
.action-row .button {
flex: 1;
}
.ledger-log {
margin-top: 15px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.ledger-log h3 {
margin: 0 0 9px;
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.log-entry {
position: relative;
padding: 7px 0 7px 13px;
border-left: 2px solid #b5c6b8;
font-size: 9px;
line-height: 1.45;
color: var(--muted);
}
.log-entry strong {
color: var(--ink);
}
.empty-state {
padding: 20px 12px;
text-align: center;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.modal {
position: fixed;
inset: 0;
z-index: 100;
display: none;
align-items: center;
justify-content: center;
padding: 20px;
background: rgba(18, 42, 39, 0.5);
backdrop-filter: blur(4px);
}
.modal.open {
display: flex;
}
.modal-card {
width: min(520px, 100%);
padding: 22px;
background: var(--paper);
border: 2px solid var(--ink);
box-shadow: 10px 10px 0 rgba(25, 57, 54, 0.24);
}
.modal-card h2 {
margin: 0 0 8px;
font-size: 22px;
}
.modal-card p,
.modal-card li {
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.modal-card ol {
padding-left: 20px;
}
@media (max-width: 1080px) {
.workspace {
grid-template-columns: 235px minmax(280px, 1fr) 325px;
}
.panel {
padding: 13px;
}
.day-status {
display: none;
}
}
@media (max-width: 850px) {
#app {
grid-template-rows: 60px 1fr;
}
.workspace {
grid-template-columns: 1fr;
}
.left-panel {
display: none;
}
.right-panel {
position: absolute;
right: 10px;
top: 70px;
bottom: 10px;
width: min(340px, calc(100% - 20px));
border: 2px solid var(--line);
box-shadow: var(--shadow);
border-radius: 4px;
}
.brand-subtitle {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</div>
<div>
<h1 class="brand-title">TIDE COMMONS</h1>
<div class="brand-subtitle">Basin 04 · Negotiation table</div>
</div>
</div>
<div class="day-status">
<span class="day-dot"></span>
<span>Negotiation window</span>
<b id="dayBadge">DAY 07</b>
</div>
<div class="top-actions">
<button class="button subtle" id="helpButton">How to play</button>
<button class="button" id="resetView">Reset view</button>
<button class="button primary" id="advanceDay">Advance day</button>
</div>
</header>
<div class="workspace">
<aside class="panel left-panel">
<p class="panel-kicker">Player ledgers</p>
<h2 class="panel-title">Settlement accounts</h2>
<p class="panel-intro">
Every player values the same three resources differently.
Select a settlement to open a neighboring agreement.
</p>
<div id="settlementList" class="settlement-list"></div>
</aside>
<main class="viewport" id="viewport">
<div id="canvasHost"></div>
<div id="labelLayer"></div>
<div class="board-title">
<strong>The Bracken Basin</strong>
<span>Shared-water territory · voxel survey 04</span>
</div>
<div class="legend">
<span><i class="active"></i> Active agreement</span>
<span><i class="pending"></i> Pending proposal</span>
<span><i class="shared"></i> Shared infrastructure</span>
</div>
<div class="camera-help">
Drag to orbit · Scroll to zoom · Click a district or route
</div>
<div id="toast" role="status" aria-live="polite"></div>
</main>
<aside class="panel right-panel">
<p class="panel-kicker">Agreement desk</p>
<h2 class="panel-title">Negotiate explicitly</h2>
<p class="panel-intro">
Neighboring settlements share one ferry and one reservoir.
Every term and transfer is written into the ledger.
</p>
<div id="rightContent"></div>
</aside>
</div>
</div>
<div class="modal" id="helpModal" role="dialog" aria-modal="true" aria-labelledby="helpTitle">
<div class="modal-card">
<h2 id="helpTitle">How the commons work</h2>
<ol>
<li>Choose a neighboring pair from the agreement desk.</li>
<li>Set ferry departures, reservoir shares, and the daily barter.</li>
<li>Send the proposal, then explicitly accept it as the receiving player.</li>
<li>Advance a day to apply active agreements to both resource ledgers.</li>
</ol>
<p>
The orange proposal score is the receiving player's own evaluation.
It changes with that player's preferences, so one offer does not suit every player.
</p>
<button class="button primary" id="closeHelp">Got it</button>
</div>
</div>
<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 viewport = $("#viewport");
const host = $("#canvasHost");
const RESOURCE = {
grain: { name: "Grain", short: "Grain", color: 0xe0b34d, css: "#d8a83b" },
timber: { name: "Timber", short: "Timber", color: 0x8c5b3e, css: "#8c5b3e" },
water: { name: "Water", short: "Water", color: 0x438fa7, css: "#438fa7" }
};
const settlements = [
{
id: "bracken",
name: "Bracken Quay",
player: "Mara Voss",
x: -6,
z: 3,
color: 0x6f9d5d,
css: "#6f9d5d",
inventory: { grain: 14, timber: 8, water: 8 },
preference: { grain: 0.60, timber: 0.20, water: 0.20 },
note: "Keeps the northern granary open through winter."
},
{
id: "alder",
name: "Alder Landing",
player: "Niko Reed",
x: -3,
z: 3,
color: 0xb86e4e,
css: "#b86e4e",
inventory: { grain: 6, timber: 15, water: 7 },
preference: { grain: 0.25, timber: 0.55, water: 0.20 },
note: "Maintains the timber yards and dock machinery."
},
{
id: "reedbank",
name: "Reedbank",
player: "Esen Vale",
x: -3,
z: 0,
color: 0x4d8f80,
css: "#4d8f80",
inventory: { grain: 7, timber: 6, water: 12 },
preference: { grain: 0.15, timber: 0.20, water: 0.65 },
note: "Stores the basin's cleanest drinking water."
},
{
id: "emberfen",
name: "Emberfen",
player: "Tavi Orin",
x: 3,
z: 0,
color: 0xb78943,
css: "#b78943",
inventory: { grain: 11, timber: 9, water: 6 },
preference: { grain: 0.45, timber: 0.20, water: 0.35 },
note: "Grows surplus grain along the warm eastern channels."
},
{
id: "saltglass",
name: "Saltglass",
player: "Ivo March",
x: 6,
z: 0,
color: 0x7389a4,
css: "#7389a4",
inventory: { grain: 6, timber: 14, water: 10 },
preference: { grain: 0.20, timber: 0.50, water: 0.30 },
note: "Builds boats from the sheltered eastern timber stands."
},
{
id: "pine",
name: "Pine Reach",
player: "Sena Holt",
x: 3,
z: -3,
color: 0x4f8261,
css: "#4f8261",
inventory: { grain: 8, timber: 7, water: 13 },
preference: { grain: 0.10, timber: 0.20, water: 0.70 },
note: "Protects the upper reservoir and its wetland habitats."
}
];
const settlementById = new Map(settlements.map(s => [s.id, s]));
const edges = [
{ id: "bracken--alder", a: "bracken", b: "alder" },
{ id: "alder--reedbank", a: "alder", b: "reedbank" },
{ id: "reedbank--emberfen", a: "reedbank", b: "emberfen" },
{ id: "emberfen--saltglass", a: "emberfen", b: "saltglass" },
{ id: "emberfen--pine", a: "emberfen", b: "pine" }
];
const edgeById = new Map(edges.map(e => [e.id, e]));
const state = {
day: 7,
focusedSettlement: "bracken",
selectedEdge: "bracken--alder",
agreements: new Map(),
proposals: new Map(),
drafts: new Map(),
history: [
{
day: 6,
title: "Bracken Quay ↔ Alder Landing",
text: "Ferry departures set to 3/day. Alder accepted the shared reservoir split and 2-grain / 1-timber barter."
},
{
day: 6,
title: "Reedbank ↔ Emberfen",
text: "Reedbank retained 70% of reservoir release. Emberfen accepted 1-water / 1-grain daily exchange."
}
]
};
state.agreements.set("bracken--alder", {
departures: 3,
splitA: 35,
aResource: "grain",
bResource: "timber",
amount: 2
});
state.agreements.set("reedbank--emberfen", {
departures: 2,
splitA: 70,
aResource: "water",
bResource: "grain",
amount: 1
});
function defaultDraft(edge) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const topA = Object.keys(a.preference).sort(
(x, y) => b.preference[y] - b.preference[x]
)[0];
return {
departures: 2,
splitA: 50,
aResource: topA,
bResource: b.preference.grain >= b.preference.timber ? "grain" : "timber",
amount: 1
};
}
function getDraft(edge) {
if (!state.drafts.has(edge.id)) {
state.drafts.set(edge.id, defaultDraft(edge));
}
return state.drafts.get(edge.id);
}
function priorityOf(s) {
return Object.entries(s.preference).sort((a, b) => b[1] - a[1])[0][0];
}
function edgeName(edge) {
return `${settlementById.get(edge.a).name} ↔ ${settlementById.get(edge.b).name}`;
}
function resourceOptions(selected) {
return Object.entries(RESOURCE).map(([id, r]) =>
`<option value="${id}" ${id === selected ? "selected" : ""}>${r.name}</option>`
).join("");
}
// ---------------------------------------------------------------------
// THREE.JS SCENE
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc8ded8);
scene.fog = new THREE.Fog(0xc8ded8, 34, 67);
const camera = new THREE.PerspectiveCamera(39, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
renderer.domElement.setAttribute("aria-label", "Interactive voxel map of neighboring settlements");
host.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff7df, 0x49665b, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffefd0, 3.0);
sun.position.set(-11, 19, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.0005;
scene.add(sun);
const world = new THREE.Group();
scene.add(world);
function makeBox(
width, height, depth, color, x, y, z,
parent = world,
materialOptions = {}
) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02,
...materialOptions
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Board and water.
makeBox(20.8, 0.5, 16.8, 0x397f91, 0, -0.62, 0, world, {
roughness: 0.42,
metalness: 0.08
});
const frameColor = 0x315f58;
makeBox(21.4, 0.45, 0.55, frameColor, 0, -0.35, 8.35);
makeBox(21.4, 0.45, 0.55, frameColor, 0, -0.35, -8.35);
makeBox(0.55, 0.45, 16.7, frameColor, -10.35, -0.35, 0);
makeBox(0.55, 0.45, 16.7, frameColor, 10.35, -0.35, 0);
// Pixel-like water glints.
const glintMaterial = new THREE.MeshBasicMaterial({
color: 0xc5e2dc,
transparent: true,
opacity: 0.28
});
const glintLocations = [
[-8.8, -6.3], [-7.3, -6.3], [8.5, 5.8], [7.2, 5.8],
[8.5, -6.1], [-8.5, 6.2], [0.6, 6.6], [8.8, 2.8],
[-8.7, -2.2], [1.0, -6.8], [8.8, -4.8]
];
for (const [x, z] of glintLocations) {
const glint = new THREE.Mesh(
new THREE.BoxGeometry(1.0, 0.025, 0.11),
glintMaterial
);
glint.position.set(x, -0.35, z);
world.add(glint);
}
// Land tiles around every settlement.
const landTiles = new Map();
function addLandTile(x, z) {
const key = `${x.toFixed(2)},${z.toFixed(2)}`;
if (landTiles.has(key)) return;
const colors = [0x7fa36a, 0x789d65, 0x87aa70, 0x739662];
const index = Math.abs(Math.round(x * 7 + z * 13)) % colors.length;
const tile = makeBox(
1.02, 0.28, 1.02,
colors[index],
x, -0.07, z
);
landTiles.set(key, tile);
}
for (const s of settlements) {
for (let dx = -1; dx <= 1; dx++) {
for (let dz = -1; dz <= 1; dz++) {
addLandTile(s.x + dx, s.z + dz);
}
}
}
for (const edge of edges) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
addLandTile((a.x + b.x) / 2, (a.z + b.z) / 2);
}
function addTree(parent, x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0.16, z);
tree.scale.setScalar(scale);
makeBox(0.22, 0.8, 0.22, 0x76503a, 0, 0.4, 0, tree);
makeBox(0.8, 0.55, 0.8, 0x3f7954, 0, 1.0, 0, tree);
makeBox(0.58, 0.5, 0.58, 0x4f8c5d, 0, 1.48, 0, tree);
makeBox(0.32, 0.35, 0.32, 0x66a46b, 0, 1.86, 0, tree);
parent.add(tree);
}
const settlementGroups = new Map();
const settlementLabels = new Map();
const resourceBeacons = new Map();
function addWindows(group, x, y, z, width = 0.18) {
const windowMaterial = {
color: 0xffd978,
emissive: 0xdca643,
emissiveIntensity: 0.35,
roughness: 0.5
};
makeBox(width, 0.22, 0.04, windowMaterial.color, x, y, z, group, windowMaterial);
}
function buildSettlement(s, index) {
const group = new THREE.Group();
group.position.set(s.x, 0, s.z);
group.userData.settlementId = s.id;
world.add(group);
makeBox(2.75, 0.42, 2.75, 0x9b9874, 0, 0.22, 0, group);
makeBox(2.42, 0.13, 2.42, 0xb5ae87, 0, 0.48, 0, group);
const wallColor = [0xe6d8b8, 0xd7c4a5, 0xe3d4bc][index % 3];
const roofColor = s.color;
if (index % 3 === 0) {
makeBox(1.1, 1.05, 1.0, wallColor, -0.45, 1.02, -0.05, group);
makeBox(1.3, 0.22, 1.18, roofColor, -0.45, 1.65, -0.05, group);
makeBox(1.05, 0.22, 0.93, roofColor, -0.45, 1.87, -0.05, group);
makeBox(0.76, 0.2, 0.65, roofColor, -0.45, 2.08, -0.05, group);
makeBox(0.65, 0.72, 0.68, 0xd8c8aa, 0.7, 0.91, 0.43, group);
makeBox(0.79, 0.18, 0.8, roofColor, 0.7, 1.35, 0.43, group);
} else if (index % 3 === 1) {
makeBox(0.9, 1.75, 0.9, wallColor, -0.42, 1.33, -0.12, group);
makeBox(1.1, 0.22, 1.1, roofColor, -0.42, 2.3, -0.12, group);
makeBox(0.82, 0.22, 0.82, roofColor, -0.42, 2.52, -0.12, group);
makeBox(0.48, 0.3, 0.48, 0xf0d784, -0.42, 2.75, -0.12, group);
makeBox(0.72, 0.78, 0.72, 0xd8c8aa, 0.72, 0.85, 0.35, group);
makeBox(0.86, 0.18, 0.86, roofColor, 0.72, 1.32, 0.35, group);
} else {
makeBox(0.85, 0.86, 0.9, wallColor, -0.58, 0.91, 0.1, group);
makeBox(1.02, 0.2, 1.05, roofColor, -0.58, 1.43, 0.1, group);
makeBox(0.68, 1.15, 0.72, wallColor, 0.55, 1.03, -0.28, group);
makeBox(0.86, 0.2, 0.88, roofColor, 0.55, 1.69, -0.28, group);
makeBox(0.58, 0.18, 0.6, roofColor, 0.55, 1.87, -0.28, group);
}
addWindows(group, -0.95, 1.02, 0.47, 0.2);
addWindows(group, 0.3, 1.02, 0.47, 0.2);
// Dock and posts.
makeBox(0.62, 0.16, 1.05, 0x8e6948, 0.88, 0.5, 1.42, group);
makeBox(0.15, 0.55, 0.15, 0x6c513c, 0.68, 0.45, 1.78, group);
makeBox(0.15, 0.55, 0.15, 0x6c513c, 1.08, 0.45, 1.78, group);
// Resource beacon cubes.
const beacons = {};
Object.keys(RESOURCE).forEach((resourceId, i) => {
const beacon = makeBox(
0.28, 0.32, 0.28,
RESOURCE[resourceId].color,
-0.78 + i * 0.45,
0.73,
1.0,
group,
{ emissive: RESOURCE[resourceId].color, emissiveIntensity: 0.12 }
);
beacons[resourceId] = beacon;
});
resourceBeacons.set(s.id, beacons);
addTree(group, -1.55, -1.28, 0.72);
if (index % 2 === 0) addTree(group, 1.55, -1.2, 0.62);
// Small crop rows.
for (let i = 0; i < 3; i++) {
makeBox(
0.22, 0.18, 0.32,
0xc7a64a,
-0.93 + i * 0.32,
0.61,
1.13,
group
);
}
settlementGroups.set(s.id, group);
const label = document.createElement("button");
label.className = "world-label";
label.innerHTML = `<strong>${s.name}</strong><small>${s.player}</small>`;
label.addEventListener("click", () => chooseSettlement(s.id));
$("#labelLayer").appendChild(label);
settlementLabels.set(s.id, label);
}
settlements.forEach(buildSettlement);
const edgeGroups = new Map();
const edgePins = new Map();
function buildSharedInfrastructure(edge) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const group = new THREE.Group();
group.userData.edgeId = edge.id;
world.add(group);
const midX = (a.x + b.x) / 2;
const midZ = (a.z + b.z) / 2;
const horizontal = a.z === b.z;
const normalX = horizontal ? 0 : 1;
const normalZ = horizontal ? 1 : 0;
// Shared route.
if (horizontal) {
makeBox(3.0, 0.12, 0.58, 0xa4815c, midX, 0.38, midZ, group);
} else {
makeBox(0.58, 0.12, 3.0, 0xa4815c, midX, 0.38, midZ, group);
}
// Voxel ferry.
const ferry = new THREE.Group();
ferry.position.set(
midX + normalX * 0.48,
0.48,
midZ + normalZ * 0.48
);
if (!horizontal) ferry.rotation.y = Math.PI / 2;
makeBox(1.05, 0.23, 0.48, 0xc86838, 0, 0, 0, ferry);
makeBox(0.78, 0.24, 0.39, 0xe59a57, -0.03, 0.2, 0, ferry);
makeBox(0.38, 0.32, 0.32, 0xf1e5c9, 0.12, 0.45, 0, ferry);
makeBox(0.43, 0.12, 0.38, 0x355e66, 0.12, 0.66, 0, ferry);
makeBox(0.08, 0.7, 0.08, 0x654c39, -0.28, 0.48, 0, ferry);
makeBox(0.36, 0.18, 0.08, 0xf1e5c9, -0.13, 0.73, 0, ferry);
group.add(ferry);
// Shared reservoir.
const reservoir = new THREE.Group();
reservoir.position.set(
midX + normalX * 1.72,
0.22,
midZ + normalZ * 1.72
);
makeBox(1.1, 0.26, 1.1, 0x777b68, 0, 0, 0, reservoir);
makeBox(
0.82, 0.13, 0.82,
0x4d9caf,
0, 0.17, 0,
reservoir,
{ roughness: 0.38, metalness: 0.08 }
);
const wall = 0xbbb79d;
makeBox(1.08, 0.28, 0.12, wall, 0, 0.14, -0.49, reservoir);
makeBox(1.08, 0.28, 0.12, wall, 0, 0.14, 0.49, reservoir);
makeBox(0.12, 0.28, 0.86, wall, -0.49, 0.14, 0, reservoir);
makeBox(0.12, 0.28, 0.86, wall, 0.49, 0.14, 0, reservoir);
group.add(reservoir);
// Agreement status pin.
const pin = new THREE.Group();
pin.position.set(
midX - normalX * 1.72,
0.26,
midZ - normalZ * 1.72
);
makeBox(0.08, 1.25, 0.08, 0x66594a, 0, 0.62, 0, pin);
const flag = makeBox(0.48, 0.34, 0.09, 0x8a958b, 0.24, 1.1, 0, pin);
group.add(pin);
edgeGroups.set(edge.id, group);
edgePins.set(edge.id, flag);
}
edges.forEach(buildSharedInfrastructure);
// Selection bracket around the focused settlement.
const selectionRing = new THREE.Group();
const ringMaterial = {
color: 0xe7f6ec,
emissive: 0x8dd4bc,
emissiveIntensity: 0.32
};
makeBox(3.05, 0.06, 0.08, ringMaterial.color, 0, 0.57, -1.48, selectionRing, ringMaterial);
makeBox(3.05, 0.06, 0.08, ringMaterial.color, 0, 0.57, 1.48, selectionRing, ringMaterial);
makeBox(0.08, 0.06, 2.9, ringMaterial.color, -1.48, 0.57, 0, selectionRing, ringMaterial);
makeBox(0.08, 0.06, 2.9, ringMaterial.color, 1.48, 0.57, 0, selectionRing, ringMaterial);
world.add(selectionRing);
// ---------------------------------------------------------------------
// CAMERA CONTROLS — IMPLEMENTED WITHOUT ADDONS
// ---------------------------------------------------------------------
const orbit = {
yaw: 0.72,
pitch: 0.74,
distance: 31,
target: new THREE.Vector3(0, 0.5, 0)
};
function updateCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.distance;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
function resetView() {
orbit.yaw = 0.72;
orbit.pitch = 0.74;
orbit.distance = 31;
orbit.target.set(0, 0.5, 0);
updateCamera();
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
let dragging = false;
let moved = false;
function pickInteractive(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const objects = [...settlementGroups.values(), ...edgeGroups.values()];
const hits = raycaster.intersectObjects(objects, true);
for (const hit of hits) {
let object = hit.object;
while (object) {
if (object.userData.settlementId) {
return { type: "settlement", id: object.userData.settlementId };
}
if (object.userData.edgeId) {
return { type: "edge", id: object.userData.edgeId };
}
object = object.parent;
}
}
return null;
}
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = { x: event.clientX, y: event.clientY };
dragging = true;
moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging && pointerDown) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 3) moved = true;
if (moved) {
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(orbit.pitch + dy * 0.004, 0.35, 1.43);
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
}
} else {
renderer.domElement.style.cursor = pickInteractive(event.clientX, event.clientY)
? "pointer"
: "grab";
}
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
pointerDown = null;
if (!moved) {
const hit = pickInteractive(event.clientX, event.clientY);
if (hit?.type === "settlement") {
chooseSettlement(hit.id);
} else if (hit?.type === "edge") {
state.selectedEdge = hit.id;
const edge = edgeById.get(hit.id);
state.focusedSettlement = edge.a;
renderAll();
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
pointerDown = null;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance + event.deltaY * 0.018,
18,
43
);
updateCamera();
}, { passive: false });
// ---------------------------------------------------------------------
// AGREEMENT LOGIC AND ACCOUNTING
// ---------------------------------------------------------------------
function clampScore(value) {
return Math.max(0, Math.min(100, Math.round(value)));
}
function evaluateTerms(edge, terms) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const aWater = Math.round(3 * terms.splitA / 100);
const bWater = 3 - aWater;
const bScore = clampScore(
28 +
terms.departures * 5 +
bWater * 9 * b.preference.water +
terms.amount * b.preference[terms.bResource] * 12 -
terms.amount * b.preference[terms.aResource] * 8
);
const aScore = clampScore(
28 +
terms.departures * 5 +
aWater * 9 * a.preference.water +
terms.amount * a.preference[terms.aResource] * 12 -
terms.amount * a.preference[terms.bResource] * 8
);
return {
aScore,
bScore,
aWater,
bWater,
note:
`${b.name} values ${RESOURCE[terms.bResource].name.toLowerCase()} most ` +
`(${Math.round(b.preference[terms.bResource] * 100)}% priority), receives ` +
`${bWater}/3 reservoir units/day, and receives ${terms.amount} ` +
`${RESOURCE[terms.bResource].name.toLowerCase()} per day.`
};
}
function transfer(settlementId, resourceId, delta) {
const s = settlementById.get(settlementId);
const before = s.inventory[resourceId];
const after = Math.max(0, before + delta);
s.inventory[resourceId] = after;
return after - before;
}
function applyAgreement(edge, terms, phase) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const parts = [];
// Explicit ferry fuel liability: A pays one timber per agreement day.
const fuel = transfer(edge.a, "timber", -1);
parts.push(`${a.name} paid ${Math.abs(fuel)} timber ferry fuel`);
// Explicit reservoir release.
const aWater = Math.round(3 * terms.splitA / 100);
const bWater = 3 - aWater;
transfer(edge.a, "water", aWater);
transfer(edge.b, "water", bWater);
parts.push(`reservoir released ${aWater} water to ${a.name} / ${bWater} to ${b.name}`);
// Explicit bilateral barter.
const aToB = transfer(edge.a, terms.aResource, -terms.amount);
const bToA = transfer(edge.b, terms.bResource, -terms.amount);
if (terms.amount > 0) {
parts.push(
`${a.name} sent ${Math.abs(aToB)} ${RESOURCE[terms.aResource].name.toLowerCase()}` +
`; ${b.name} sent ${Math.abs(bToA)} ${RESOURCE[terms.bResource].name.toLowerCase()}`
);
}
state.history.unshift({
day: state.day,
title: `${phase}: ${edgeName(edge)}`,
text: parts.join(" · ")
});
}
function advanceDay() {
state.day += 1;
for (const edge of edges) {
const terms = state.agreements.get(edge.id);
if (terms) applyAgreement(edge, terms, "Daily settlement");
}
state.history = state.history.slice(0, 8);
renderAll();
showToast(`Day ${state.day}: recurring ferry, reservoir, and barter entries posted.`);
}
// ---------------------------------------------------------------------
// UI RENDERING
// ---------------------------------------------------------------------
function renderSettlementList() {
const list = $("#settlementList");
list.innerHTML = settlements.map(s => {
const priority = priorityOf(s);
const active = state.focusedSettlement === s.id ? "active" : "";
return `
<button
class="settlement-card ${active}"
style="--settlement-color:${s.css}"
data-settlement="${s.id}"
aria-pressed="${state.focusedSettlement === s.id}"
>
<div class="settlement-head">
<div class="settlement-name">
<i class="settlement-swatch"></i>
${s.name}
</div>
</div>
<div class="player-name">Player: ${s.player}</div>
<div class="inventory">
<div class="inventory-item">
<small>Grain</small>
<strong>${s.inventory.grain}</strong>
</div>
<div class="inventory-item">
<small>Timber</small>
<strong>${s.inventory.timber}</strong>
</div>
<div class="inventory-item">
<small>Water</small>
<strong>${s.inventory.water}</strong>
</div>
</div>
<div class="priority">
<span>Top priority</span>
<b>${RESOURCE[priority].name} ${Math.round(s.preference[priority] * 100)}%</b>
</div>
<div class="preference-bars" aria-label="Resource preferences">
${Object.entries(s.preference).map(([id, value]) =>
`<i style="width:${value * 100}%;background:${RESOURCE[id].css}"></i>`
).join("")}
</div>
</button>
`;
}).join("");
list.querySelectorAll("[data-settlement]").forEach(button => {
button.addEventListener("click", () => {
chooseSettlement(button.dataset.settlement);
});
});
}
function termsMarkup(edge, terms) {
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const bWater = 3 - Math.round(3 * terms.splitA / 100);
return `
<div class="term-row">
<span>Shared ferry</span>
<b>${terms.departures} departures/day</b>
</div>
<div class="term-row">
<span>Ferry fuel</span>
<b>${a.name}: 1 timber/day</b>
</div>
<div class="term-row">
<span>Reservoir split</span>
<b>${a.name} ${terms.splitA}% / ${b.name} ${100 - terms.splitA}%</b>
</div>
<div class="term-row">
<span>Water released</span>
<b>${a.name} ${3 - bWater} / ${b.name} ${bWater} per day</b>
</div>
<div class="term-row">
<span>Daily barter</span>
<b>${a.name} → ${b.name}: ${terms.amount} ${RESOURCE[terms.aResource].name}</b>
</div>
<div class="term-row">
<span>Counter-barter</span>
<b>${b.name} → ${a.name}: ${terms.amount} ${RESOURCE[terms.bResource].name}</b>
</div>
`;
}
function renderRight() {
const edge = edgeById.get(state.selectedEdge);
const agreement = state.agreements.get(edge.id);
const proposal = state.proposals.get(edge.id);
const draft = getDraft(edge);
const evaluation = evaluateTerms(edge, draft);
const a = settlementById.get(edge.a);
const b = settlementById.get(edge.b);
const pairOptions = edges.map(e => {
const active = state.agreements.has(e.id);
const pending = state.proposals.has(e.id);
const suffix = pending ? " · proposal" : active ? " · active" : "";
return `<option value="${e.id}" ${e.id === state.selectedEdge ? "selected" : ""}>
${edgeName(e)}${suffix}
</option>`;
}).join("");
let content = `
<label class="field-label" for="pairSelect">Neighboring pair</label>
<select id="pairSelect">
${pairOptions}
</select>
<div class="pair-route">
<strong>${a.name} ↔ ${b.name}</strong>
<span>
Shared assets: 1 voxel ferry · 1 reservoir · 3 water units released daily
</span>
</div>
`;
if (agreement) {
content += `
<div class="terms-card">
<span class="status-pill">Active agreement</span>
<h3 class="terms-title">Binding terms</h3>
${termsMarkup(edge, agreement)}
</div>
`;
}
if (proposal) {
const proposalEvaluation = proposal.evaluation;
content += `
<div class="proposal-card">
<span class="status-pill pending">Awaiting acceptance</span>
<h3 class="terms-title">Proposal from ${a.name}</h3>
${termsMarkup(edge, proposal.terms)}
<div class="evaluation-card">
<div class="evaluation-head">
<span>${b.name}'s read</span>
<span class="score">${proposalEvaluation.bScore}/100</span>
</div>
<p>${proposalEvaluation.note}</p>
</div>
<div class="action-row">
<button class="button" id="reviseProposal">Revise</button>
<button class="button orange" id="acceptProposal">Accept as ${b.name}</button>
</div>
</div>
`;
} else {
content += `
<div class="form-section">
<h3>${agreement ? "Draft a revision" : "Draft agreement"}</h3>
<div class="form-field">
<label class="field-label" for="departures">
Shared ferry departures
<output id="departuresOutput">${draft.departures}/day</output>
</label>
<input id="departures" type="range" min="1" max="4" step="1" value="${draft.departures}">
</div>
<div class="form-field">
<label class="field-label" for="splitA">
Reservoir share for ${a.name}
<output id="splitOutput">${draft.splitA}% / ${100 - draft.splitA}%</output>
</label>
<input id="splitA" type="range" min="0" max="100" step="5" value="${draft.splitA}">
</div>
<div class="form-field">
<label class="field-label" for="exchangeAmount">
Daily barter quantity
<output id="amountOutput">${draft.amount} unit${draft.amount === 1 ? "" : "s"}</output>
</label>
<input id="exchangeAmount" type="range" min="1" max="4" step="1" value="${draft.amount}">
</div>
<div class="exchange-grid">
<label>
${a.name} gives
<select id="aResource">${resourceOptions(draft.aResource)}</select>
</label>
<label>
${b.name} gives
<select id="bResource">${resourceOptions(draft.bResource)}</select>
</label>
</div>
<div class="fine-print">
Explicit standing obligations:
${a.name} pays 1 timber/day for ferry fuel.
The reservoir releases 3 water units/day according to the split.
Both barter transfers occur when the agreement is accepted and once per later day.
</div>
<div class="evaluation-card">
<div class="evaluation-head">
<span>${b.name}'s live read</span>
<span class="score" id="liveScore">${evaluation.bScore}/100</span>
</div>
<p id="liveEvaluation">${evaluation.note}</p>
</div>
<button class="button primary" id="sendProposal" style="width:100%;margin-top:10px">
Send proposal to ${b.name}
</button>
</div>
`;
}
content += `
<div class="ledger-log">
<h3>Account activity</h3>
${
state.history.length
? state.history.slice(0, 5).map(item => `
<div class="log-entry">
<strong>Day ${item.day} · ${item.title}</strong><br>
${item.text}
</div>
`).join("")
: `<div class="empty-state">No transfers recorded yet.</div>`
}
</div>
`;
$("#rightContent").innerHTML = content;
$("#pairSelect").addEventListener("change", event => {
state.selectedEdge = event.target.value;
const nextEdge = edgeById.get(state.selectedEdge);
state.focusedSettlement = nextEdge.a;
renderAll();
});
const departuresInput = $("#departures");
const splitInput = $("#splitA");
const amountInput = $("#exchangeAmount");
const aResourceInput = $("#aResource");
const bResourceInput = $("#bResource");
function readAndEvaluate() {
draft.departures = Number(departuresInput.value);
draft.splitA = Number(splitInput.value);
draft.amount = Number(amountInput.value);
draft.aResource = aResourceInput.value;
draft.bResource = bResourceInput.value;
$("#departuresOutput").textContent = `${draft.departures}/day`;
$("#splitOutput").textContent = `${draft.splitA}% / ${100 - draft.splitA}%`;
$("#amountOutput").textContent =
`${draft.amount} unit${draft.amount === 1 ? "" : "s"}`;
const currentEvaluation = evaluateTerms(edge, draft);
$("#liveScore").textContent = `${currentEvaluation.bScore}/100`;
$("#liveEvaluation").textContent = currentEvaluation.note;
}
[departuresInput, splitInput, amountInput, aResourceInput, bResourceInput]
.forEach(input => input?.addEventListener("input", readAndEvaluate));
$("#sendProposal")?.addEventListener("click", () => {
readAndEvaluate();
state.proposals.set(edge.id, {
terms: { ...draft },
evaluation: evaluateTerms(edge, draft)
});
renderAll();
showToast(`Proposal sent to ${b.name}. Explicit acceptance is still required.`);
});
$("#reviseProposal")?.addEventListener("click", () => {
state.proposals.delete(edge.id);
renderAll();
});
$("#acceptProposal")?.addEventListener("click", () => {
const pending = state.proposals.get(edge.id);
if (!pending) return;
state.agreements.set(edge.id, { ...pending.terms });
state.proposals.delete(edge.id);
applyAgreement(edge, pending.terms, "Opening settlement");
renderAll();
showToast(`${b.name} accepted the agreement. The opening transfer is posted.`);
});
}
function renderSceneState() {
const focused = settlementById.get(state.focusedSettlement);
selectionRing.position.set(focused.x, 0, focused.z);
for (const edge of edges) {
const pin = edgePins.get(edge.id);
const hasProposal = state.proposals.has(edge.id);
const hasAgreement = state.agreements.has(edge.id);
const selected = edge.id === state.selectedEdge;
let color = 0x90978d;
if (hasAgreement) color = 0x3f8767;
if (hasProposal) color = 0xd27a3d;
if (selected) color = hasProposal ? 0xf09a57 : hasAgreement ? 0x64b38e : 0x5792a8;
pin.material.color.setHex(color);
pin.material.emissive.setHex(color);
pin.material.emissiveIntensity = selected ? 0.3 : 0.08;
}
for (const s of settlements) {
const beacons = resourceBeacons.get(s.id);
for (const [resourceId, mesh] of Object.entries(beacons)) {
const count = s.inventory[resourceId];
const scale = 0.72 + Math.min(count / 15, 1.1) * 0.4;
mesh.scale.y = scale;
mesh.material.emissiveIntensity = 0.08 + Math.min(count / 30, 0.24);
}
}
}
function renderAll() {
$("#dayBadge").textContent = `DAY ${String(state.day).padStart(2, "0")}`;
renderSettlementList();
renderRight();
renderSceneState();
}
function chooseSettlement(id) {
const focused = state.focusedSettlement;
if (focused !== id) {
const directEdge = edges.find(e =>
(e.a === focused && e.b === id) ||
(e.b === focused && e.a === id)
);
if (directEdge) {
state.selectedEdge = directEdge.id;
state.focusedSettlement = id;
renderAll();
return;
}
}
state.focusedSettlement = id;
const neighborEdge = edges.find(e => e.a === id || e.b === id);
if (neighborEdge) state.selectedEdge = neighborEdge.id;
renderAll();
}
let toastTimer;
function showToast(message) {
const toast = $("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 3400);
}
$("#advanceDay").addEventListener("click", advanceDay);
$("#resetView").addEventListener("click", resetView);
$("#helpButton").addEventListener("click", () => {
$("#helpModal").classList.add("open");
});
$("#closeHelp").addEventListener("click", () => {
$("#helpModal").classList.remove("open");
});
$("#helpModal").addEventListener("click", event => {
if (event.target === $("#helpModal")) {
$("#helpModal").classList.remove("open");
}
});
// ---------------------------------------------------------------------
// RESIZE, LABELS, AND ANIMATION
// ---------------------------------------------------------------------
function resizeRenderer() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resizeRenderer).observe(viewport);
const projected = new THREE.Vector3();
function updateLabels() {
for (const s of settlements) {
projected.set(s.x, 3.55, s.z).project(camera);
const label = settlementLabels.get(s.id);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.style.display = visible ? "block" : "none";
const x = (projected.x * 0.5 + 0.5) * viewport.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * viewport.clientHeight;
label.style.left = `${x}px`;
label.style.top = `${y}px`;
}
}
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
resizeRenderer();
resetView();
renderAll();
animate();
</script>
</body>
</html>
```