Prompt specification
Build or customize the Three.js 3D scene "Blocked Container Retrieval Puzzle" 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: Blocked Container Retrieval Puzzle
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/blocked-container-retrieval-puzzle-x2124
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a stacking puzzle where a target container lies beneath others. Plan legal moves with limited spare slots and verify crane reach and stack occupancy. Low-poly.
Working Three.js Starter (blocked-container-retrieval-puzzle-x2124):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stack Shift — Crane Puzzle</title>
<style>
:root {
--ink: #edf7f3;
--muted: #91a49f;
--panel: rgba(13, 23, 27, 0.92);
--line: rgba(180, 220, 208, 0.15);
--mint: #79efc0;
--mint-dark: #173f35;
--amber: #ffbd59;
--red: #ff7285;
--blue: #66bfff;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #101a1e;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
color: inherit;
cursor: pointer;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.topbar {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
right: 20px;
display: flex;
justify-content: space-between;
align-items: flex-start;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
padding: 11px 15px 11px 11px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(12, 21, 25, 0.82);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 9px;
background: var(--mint);
color: #10251f;
font-weight: 950;
letter-spacing: -1px;
box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.12);
}
.brand h1 {
margin: 0;
font-size: 14px;
line-height: 1;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.05em;
}
.telemetry {
display: flex;
gap: 8px;
pointer-events: auto;
}
.telemetry span {
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(12, 21, 25, 0.82);
color: var(--muted);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.telemetry b {
margin-left: 5px;
color: var(--mint);
font-weight: 800;
}
.panel {
position: fixed;
z-index: 6;
top: 86px;
right: 20px;
width: 342px;
max-height: calc(100vh - 108px);
overflow: auto;
padding: 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 22px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(16px);
scrollbar-width: thin;
scrollbar-color: #35534c transparent;
}
.eyebrow {
color: var(--mint);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel h2 {
margin: 8px 0 3px;
font-size: 21px;
letter-spacing: -0.025em;
}
.subhead {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 4px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.16);
margin-bottom: 12px;
}
.mode-switch button {
border: 0;
border-radius: 7px;
padding: 8px;
background: transparent;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.mode-switch button.active {
background: var(--mint-dark);
color: var(--mint);
box-shadow: inset 0 0 0 1px rgba(121, 239, 192, 0.17);
}
.selection {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 12px;
}
.selection-card {
min-width: 0;
padding: 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.selection-card label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
}
.selection-card strong {
display: block;
overflow: hidden;
color: var(--ink);
font-size: 12px;
white-space: nowrap;
text-overflow: ellipsis;
}
.checks {
border: 1px solid var(--line);
border-radius: 11px;
overflow: hidden;
}
.check {
display: grid;
grid-template-columns: 20px 1fr auto;
gap: 8px;
align-items: center;
min-height: 45px;
padding: 8px 10px;
border-bottom: 1px solid var(--line);
}
.check:last-child { border-bottom: 0; }
.check-icon {
width: 17px;
height: 17px;
display: grid;
place-items: center;
border-radius: 50%;
background: rgba(145, 164, 159, 0.1);
color: var(--muted);
font-size: 10px;
font-weight: 900;
}
.check-main strong {
display: block;
font-size: 11px;
letter-spacing: 0.02em;
}
.check-main small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.check-value {
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
}
.check.pass .check-icon {
background: rgba(121, 239, 192, 0.15);
color: var(--mint);
}
.check.pass .check-value { color: var(--mint); }
.check.fail .check-icon {
background: rgba(255, 114, 133, 0.15);
color: var(--red);
}
.check.fail .check-value { color: var(--red); }
.verdict {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 10px;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.13);
}
.verdict span {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#verdictBadge {
padding: 5px 8px;
border-radius: 6px;
background: rgba(145, 164, 159, 0.12);
color: var(--muted);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
#verdictBadge.ready {
background: rgba(121, 239, 192, 0.15);
color: var(--mint);
}
#verdictBadge.invalid {
background: rgba(255, 114, 133, 0.15);
color: var(--red);
}
.primary {
width: 100%;
margin-top: 9px;
padding: 11px;
border: 0;
border-radius: 9px;
background: var(--mint);
color: #10251f;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.13);
}
.primary:disabled {
opacity: 0.38;
cursor: not-allowed;
box-shadow: none;
}
.secondary-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.secondary {
padding: 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: var(--ink);
font-size: 10px;
font-weight: 750;
}
.secondary:hover { background: rgba(255, 255, 255, 0.07); }
.divider {
height: 1px;
margin: 16px 0;
background: var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 10px;
}
.section-title h3 {
margin: 0;
font-size: 12px;
letter-spacing: 0.04em;
}
.tiny-btn {
border: 1px solid var(--line);
border-radius: 7px;
padding: 6px 8px;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
font: 750 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.tiny-btn:hover { color: var(--ink); }
.spare-meter {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
.meter-card {
padding: 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(0, 0, 0, 0.12);
}
.meter-head {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
font: 750 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.meter-head span { color: var(--muted); }
.meter-track {
height: 4px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.meter-fill {
height: 100%;
width: 0;
border-radius: inherit;
background: var(--blue);
transition: width 0.25s ease;
}
#planList {
display: grid;
gap: 6px;
}
.plan-step {
display: grid;
grid-template-columns: 25px 1fr auto;
align-items: center;
gap: 8px;
width: 100%;
padding: 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
text-align: left;
}
.plan-step:hover { background: rgba(121, 239, 192, 0.07); }
.step-number {
width: 23px;
height: 23px;
display: grid;
place-items: center;
border-radius: 6px;
background: var(--mint-dark);
color: var(--mint);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.step-copy strong {
display: block;
font-size: 10px;
}
.step-copy small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.step-check { color: var(--mint); }
.hint {
position: fixed;
z-index: 5;
left: 20px;
bottom: 18px;
max-width: min(560px, calc(100vw - 390px));
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(12, 21, 25, 0.82);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
backdrop-filter: blur(12px);
pointer-events: none;
}
.hint b { color: var(--ink); }
#toast {
position: fixed;
z-index: 20;
top: 92px;
left: 50%;
max-width: 420px;
padding: 11px 15px;
border: 1px solid rgba(255, 114, 133, 0.35);
border-radius: 10px;
background: rgba(42, 18, 23, 0.94);
color: #ffd7dd;
font-size: 12px;
font-weight: 700;
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
transform: translate(-50%, -15px);
opacity: 0;
pointer-events: none;
transition: 0.22s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.success {
border-color: rgba(121, 239, 192, 0.35);
background: rgba(15, 45, 36, 0.95);
color: #c9ffe9;
}
#winBanner {
position: fixed;
z-index: 8;
left: 50%;
top: 47%;
min-width: 330px;
padding: 18px 22px;
border: 1px solid rgba(121, 239, 192, 0.4);
border-radius: 14px;
background: rgba(10, 31, 26, 0.94);
text-align: center;
box-shadow: 0 25px 80px rgba(0, 0, 0, 0.42);
transform: translate(-50%, -50%) scale(0.94);
opacity: 0;
pointer-events: none;
transition: 0.3s ease;
}
#winBanner.show {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
}
#winBanner h2 {
margin: 7px 0 6px;
font-size: 24px;
letter-spacing: -0.03em;
}
#winBanner p {
margin: 0;
color: var(--muted);
font-size: 11px;
}
@media (max-width: 780px) {
.topbar { left: 10px; right: 10px; top: 10px; }
.brand p, .telemetry span:nth-child(2) { display: none; }
.telemetry { gap: 4px; }
.panel {
top: auto;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 43vh;
padding: 13px;
}
.hint { display: none; }
.selection-card strong { font-size: 10px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="topbar">
<div class="brand">
<div class="brand-mark">S</div>
<div>
<h1>Stack Shift</h1>
<p>Yard 07 · Container recovery puzzle</p>
</div>
</div>
<div class="telemetry">
<span>CRANE REACH <b>6.2 M</b></span>
<span>SPARE BAYS <b>2</b></span>
<span>STATUS <b id="topStatus">ACTIVE</b></span>
</div>
</div>
<aside class="panel">
<div class="eyebrow">Move validator</div>
<h2>Recover the target</h2>
<p class="subhead">The amber TARGET container is trapped beneath three cargo units. Move only exposed containers and respect crane reach and bay capacity.</p>
<div class="mode-switch">
<button id="sourceMode" class="active">1 · Pick source</button>
<button id="destMode">2 · Drop bay</button>
</div>
<div class="selection">
<div class="selection-card">
<label>PICK</label>
<strong id="sourceValue">Not selected</strong>
</div>
<div class="selection-card">
<label>DROP</label>
<strong id="destValue">Not selected</strong>
</div>
</div>
<div class="checks">
<div class="check" id="sourceCheck">
<div class="check-icon">?</div>
<div class="check-main">
<strong>Stack occupancy</strong>
<small>Source must be the exposed container</small>
</div>
<div class="check-value">WAIT</div>
</div>
<div class="check" id="reachCheck">
<div class="check-icon">?</div>
<div class="check-main">
<strong>Crane reach</strong>
<small>Pick and drop radius must be within 6.2 m</small>
</div>
<div class="check-value">WAIT</div>
</div>
<div class="check" id="capacityCheck">
<div class="check-icon">?</div>
<div class="check-main">
<strong>Destination capacity</strong>
<small>Reserved yards cannot receive cargo</small>
</div>
<div class="check-value">WAIT</div>
</div>
<div class="check" id="ruleCheck">
<div class="check-icon">?</div>
<div class="check-main">
<strong>Routing rule</strong>
<small>Only TARGET may enter the outbound bay</small>
</div>
<div class="check-value">WAIT</div>
</div>
</div>
<div class="verdict">
<span>Move verdict</span>
<div id="verdictBadge">Waiting</div>
</div>
<button class="primary" id="moveBtn" disabled>Execute move</button>
<div class="secondary-row">
<button class="secondary" id="clearBtn">Clear selection</button>
<button class="secondary" id="resetBtn">Reset yard</button>
</div>
<div class="divider"></div>
<div class="section-title">
<h3>Spare capacity</h3>
<span class="eyebrow">Limited staging</span>
</div>
<div class="spare-meter">
<div class="meter-card">
<div class="meter-head"><span>S1</span><b id="s1Count">0 / 2</b></div>
<div class="meter-track"><div class="meter-fill" id="s1Fill"></div></div>
</div>
<div class="meter-card">
<div class="meter-head"><span>S2</span><b id="s2Count">0 / 1</b></div>
<div class="meter-track"><div class="meter-fill" id="s2Fill"></div></div>
</div>
</div>
<div class="section-title">
<h3>Legal move plan</h3>
<button class="tiny-btn" id="planBtn">Recalculate</button>
</div>
<div id="planList"></div>
<button class="secondary" id="autoBtn" style="width:100%; margin-top:8px;">Run legal sequence</button>
</aside>
<div class="hint">
<b>Controls:</b> drag to orbit · wheel to zoom · choose PICK, then DROP · click the yard to select.
Click a plan step to prefill its move.
</div>
<div id="toast"></div>
<div id="winBanner">
<div class="eyebrow">Recovery complete</div>
<h2>TARGET CLEARED</h2>
<p>The outbound bay is clear and the target is safely staged.</p>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const ITEM_H = 0.70;
const ITEM_GAP = 0.08;
const PAD_TOP = 0.10;
const MAX_REACH = 6.25;
const CRANE_BASE = new THREE.Vector3(-1.2, 0, 0.2);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x172328);
scene.fog = new THREE.Fog(0x172328, 18, 34);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
$("scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 80);
// Camera controls are implemented directly; no Three.js addons are used.
const cameraState = {
azimuth: 0.63,
polar: 0.91,
distance: 17.5,
target: new THREE.Vector3(0.15, 1.15, -0.15)
};
function updateCamera() {
const { azimuth, polar, distance, target } = cameraState;
camera.position.set(
target.x + distance * Math.sin(polar) * Math.sin(azimuth),
target.y + distance * Math.cos(polar),
target.z + distance * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xbde7e1, 0x3f3730, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffefd2, 3.2);
sun.position.set(-7, 12, 7);
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 = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x7cc7ff, 0.75);
rim.position.set(8, 6, -8);
scene.add(rim);
const flat = (color, options = {}) => new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.05,
flatShading: true,
...options
});
function makeTextTexture(text, color = "#dcebe7", background = "rgba(0,0,0,0)") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (background !== "rgba(0,0,0,0)") {
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
ctx.fillStyle = color;
ctx.font = "800 48px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
return texture;
}
function textPlane(text, width, height, color, background = "rgba(0,0,0,0)") {
const material = new THREE.MeshBasicMaterial({
map: makeTextTexture(text, color, background),
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
return new THREE.Mesh(new THREE.PlaneGeometry(width, height), material);
}
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(29, 23),
flat(0x5a625d, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(28, 28, 0x718079, 0x606963);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
const road = new THREE.Mesh(
new THREE.PlaneGeometry(28, 3.2),
flat(0x313b3e, { roughness: 1 })
);
road.rotation.x = -Math.PI / 2;
road.position.set(0, 0.012, -5.25);
road.receiveShadow = true;
scene.add(road);
for (let x = -13; x <= 13; x += 2.2) {
const stripe = new THREE.Mesh(
new THREE.PlaneGeometry(1.1, 0.08),
new THREE.MeshBasicMaterial({ color: 0xc7b989, transparent: true, opacity: 0.65 })
);
stripe.rotation.x = -Math.PI / 2;
stripe.position.set(x, 0.02, -5.25);
scene.add(stripe);
}
function addWarehouse(x, z, width, height, depth, color) {
const group = new THREE.Group();
const body = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), flat(color));
body.position.y = height / 2;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const roof = new THREE.Mesh(
new THREE.CylinderGeometry(depth * 0.54, depth * 0.54, width, 3, 1, false, 0, Math.PI),
flat(0x394448)
);
roof.rotation.z = Math.PI / 2;
roof.position.y = height;
roof.castShadow = true;
group.add(roof);
const door = new THREE.Mesh(new THREE.PlaneGeometry(width * 0.34, height * 0.55), flat(0x172328));
door.position.set(0, height * 0.275, depth / 2 + 0.012);
group.add(door);
for (let i = -1; i <= 1; i++) {
const window = new THREE.Mesh(new THREE.PlaneGeometry(width * 0.12, height * 0.11), flat(0x7ca6a4, {
emissive: 0x31595b,
emissiveIntensity: 0.35
}));
window.position.set(i * width * 0.26, height * 0.72, depth / 2 + 0.014);
group.add(window);
}
group.position.set(x, 0, z);
scene.add(group);
}
addWarehouse(-7.8, -7.8, 7.2, 3.0, 3.4, 0x475356);
addWarehouse(4.8, -8.3, 8.5, 3.6, 3.7, 0x3d4a4f);
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.17, 1.1, 6), flat(0x5b4436));
trunk.position.y = 0.55;
const crown = new THREE.Mesh(new THREE.ConeGeometry(0.72, 1.8, 7), flat(0x3f6d58));
crown.position.y = 1.7;
crown.castShadow = true;
group.add(trunk, crown);
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
}
[
[-10.5, -3.4, 1.15], [-10.8, 4.8, 0.9], [9.7, 5.0, 1.1],
[10.6, -3.7, 0.8], [-6.8, 5.7, 0.72]
].forEach(args => addTree(...args));
const locations = {
A: {
id: "A", name: "WORK STACK", x: -3.25, z: -2.45, capacity: 4,
kind: "work", items: [], color: 0xffbd59
},
B: {
id: "B", name: "INBOUND", x: 0.75, z: -2.85, capacity: 2,
kind: "locked", items: [], color: 0x75818a
},
C: {
id: "C", name: "INBOUND", x: 3.45, z: 1.25, capacity: 2,
kind: "locked", items: [], color: 0x75818a
},
S1: {
id: "S1", name: "SPARE 01", x: -3.4, z: 1.75, capacity: 2,
kind: "spare", items: [], color: 0x66bfff
},
S2: {
id: "S2", name: "SPARE 02", x: -0.2, z: 2.9, capacity: 1,
kind: "spare", items: [], color: 0x66bfff
},
OUT: {
id: "OUT", name: "OUTBOUND", x: 4.6, z: -1.2, capacity: 1,
kind: "goal", items: [], color: 0x79efc0
},
FAR: {
id: "FAR", name: "OVERFLOW", x: 5.6, z: 3.0, capacity: 2,
kind: "locked", items: [], color: 0xff7285
}
};
const items = {
target: { id: "target", label: "TARGET", color: 0xffa638, height: ITEM_H, location: "A" },
blockA: { id: "blockA", label: "A-03", color: 0xe85d75, height: ITEM_H, location: "A" },
blockB: { id: "blockB", label: "B-08", color: 0x8d6cc7, height: ITEM_H, location: "A" },
blockC: { id: "blockC", label: "C-17", color: 0x3d9ed0, height: ITEM_H, location: "A" },
inbound1: { id: "inbound1", label: "R-44", color: 0x4e8f82, height: ITEM_H, location: "B" },
inbound2: { id: "inbound2", label: "M-61", color: 0xa78350, height: ITEM_H, location: "B" },
inbound3: { id: "inbound3", label: "N-12", color: 0x607889, height: ITEM_H, location: "C" },
overflow: { id: "overflow", label: "X-90", color: 0xa9515f, height: ITEM_H, location: "FAR" }
};
locations.A.items = ["target", "blockA", "blockB", "blockC"];
locations.B.items = ["inbound1", "inbound2"];
locations.C.items = ["inbound3"];
locations.FAR.items = ["overflow"];
const clickables = [];
function slotPosition(location, index) {
return new THREE.Vector3(
location.x,
PAD_TOP + ITEM_H / 2 + index * (ITEM_H + ITEM_GAP),
location.z
);
}
function createLocation(location) {
const group = new THREE.Group();
group.position.set(location.x, 0, location.z);
const padMaterial = flat(location.color, {
emissive: location.color,
emissiveIntensity: 0.035,
roughness: 0.92
});
const pad = new THREE.Mesh(
new THREE.BoxGeometry(2.5, 0.12, 1.56),
padMaterial
);
pad.position.y = 0.04;
pad.receiveShadow = true;
pad.userData.locationId = location.id;
group.add(pad);
clickables.push(pad);
const borderMaterial = new THREE.MeshBasicMaterial({
color: location.color,
transparent: true,
opacity: location.kind === "locked" ? 0.22 : 0.75
});
for (const x of [-1.17, 1.17]) {
const rail = new THREE.Mesh(new THREE.BoxGeometry(0.06, 0.055, 1.48), borderMaterial);
rail.position.set(x, 0.125, 0);
group.add(rail);
}
const label = textPlane(
`${location.id} ${location.name}`,
2.15, 0.47,
location.kind === "spare" ? "#b9e8ff" : location.kind === "goal" ? "#a9ffdc" : "#d2dcda",
"rgba(0,0,0,0)"
);
label.rotation.x = -Math.PI / 2;
label.position.set(0, 0.135, 1.12);
group.add(label);
if (location.kind === "goal") {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.32, 0.035, 6, 48),
new THREE.MeshBasicMaterial({ color: 0x79efc0, transparent: true, opacity: 0.85 })
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.15;
group.add(ring);
}
scene.add(group);
location.visual = group;
location.pad = pad;
location.padMaterial = padMaterial;
}
Object.values(locations).forEach(createLocation);
function createContainer(item) {
const group = new THREE.Group();
const width = 2.12;
const depth = 1.12;
const material = flat(item.color, {
emissive: item.color,
emissiveIntensity: 0.015
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(width, ITEM_H, depth),
material
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const railMaterial = flat(new THREE.Color(item.color).multiplyScalar(0.72));
for (const y of [-ITEM_H / 2 + 0.045, ITEM_H / 2 - 0.045]) {
const rail = new THREE.Mesh(new THREE.BoxGeometry(width + 0.035, 0.07, depth + 0.035), railMaterial);
rail.position.y = y;
rail.castShadow = true;
group.add(rail);
}
for (let i = 0; i < 10; i++) {
const x = -0.93 + i * (1.86 / 9);
for (const z of [-depth / 2 - 0.012, depth / 2 + 0.012]) {
const rib = new THREE.Mesh(new THREE.BoxGeometry(0.045, 0.49, 0.035), railMaterial);
rib.position.set(x, 0, z);
group.add(rib);
}
}
for (const x of [-width / 2 + 0.04, width / 2 - 0.04]) {
for (const z of [-depth / 2 + 0.04, depth / 2 - 0.04]) {
const post = new THREE.Mesh(new THREE.BoxGeometry(0.09, 0.60, 0.09), railMaterial);
post.position.set(x, 0, z);
group.add(post);
}
}
const label = textPlane(
item.label,
item.id === "target" ? 1.22 : 0.92,
0.28,
item.id === "target" ? "#24170a" : "#f1f5f4",
item.id === "target" ? "#ffd487" : "rgba(16,27,31,0.86)"
);
label.position.set(0, 0.01, depth / 2 + 0.037);
group.add(label);
group.traverse(object => {
if (object.isMesh) object.userData.itemId = item.id;
});
item.mesh = group;
item.material = material;
item.locationId = item.location;
group.position.copy(slotPosition(locations[item.location], locations[item.location].items.indexOf(item.id)));
scene.add(group);
clickables.push(group);
}
Object.values(items).forEach(createContainer);
// Crane reach visualization.
const reachPoints = [];
for (let i = 0; i <= 128; i++) {
const angle = i / 128 * Math.PI * 2;
reachPoints.push(new THREE.Vector3(
CRANE_BASE.x + Math.cos(angle) * MAX_REACH,
0.045,
CRANE_BASE.z + Math.sin(angle) * MAX_REACH
));
}
const reachGeometry = new THREE.BufferGeometry().setFromPoints(reachPoints);
const reachLine = new THREE.Line(
reachGeometry,
new THREE.LineDashedMaterial({
color: 0xffc56d,
dashSize: 0.28,
gapSize: 0.18,
transparent: true,
opacity: 0.58
})
);
reachLine.computeLineDistances();
scene.add(reachLine);
const reachDisk = new THREE.Mesh(
new THREE.CircleGeometry(MAX_REACH, 96),
new THREE.MeshBasicMaterial({
color: 0xffc56d,
transparent: true,
opacity: 0.025,
depthWrite: false,
side: THREE.DoubleSide
})
);
reachDisk.rotation.x = -Math.PI / 2;
reachDisk.position.set(CRANE_BASE.x, 0.025, CRANE_BASE.z);
scene.add(reachDisk);
// Low-poly rotating crane.
const crane = new THREE.Group();
crane.position.copy(CRANE_BASE);
scene.add(crane);
const craneYellow = flat(0xf0ad3f, { metalness: 0.12 });
const craneDark = flat(0x354247, { metalness: 0.35 });
const craneBase = new THREE.Mesh(new THREE.CylinderGeometry(0.9, 1.05, 0.45, 8), craneDark);
craneBase.position.y = 0.23;
craneBase.castShadow = true;
crane.add(craneBase);
const mast = new THREE.Mesh(new THREE.CylinderGeometry(0.18, 0.26, 4.55, 8), craneYellow);
mast.position.y = 2.65;
mast.castShadow = true;
crane.add(mast);
for (let y = 0.8; y < 4.1; y += 0.8) {
const collar = new THREE.Mesh(new THREE.BoxGeometry(0.55, 0.09, 0.55), craneDark);
collar.position.y = y;
collar.castShadow = true;
crane.add(collar);
}
const jib = new THREE.Group();
jib.position.y = 4.85;
crane.add(jib);
const boom = new THREE.Mesh(
new THREE.BoxGeometry(MAX_REACH + 0.25, 0.20, 0.22),
craneYellow
);
boom.position.x = (MAX_REACH + 0.25) / 2 - 0.1;
boom.castShadow = true;
jib.add(boom);
const lowerBoom = new THREE.Mesh(
new THREE.BoxGeometry(MAX_REACH + 0.1, 0.13, 0.14),
craneDark
);
lowerBoom.position.set((MAX_REACH + 0.1) / 2 - 0.1, -0.27, 0);
lowerBoom.castShadow = true;
jib.add(lowerBoom);
for (let x = 0.5; x < MAX_REACH; x += 0.85) {
const brace = new THREE.Mesh(new THREE.BoxGeometry(0.055, 0.62, 0.08), craneDark);
brace.position.set(x, -0.12, 0);
brace.rotation.z = x % 1.7 < 0.85 ? 0.56 : -0.56;
jib.add(brace);
}
const counterweight = new THREE.Mesh(new THREE.BoxGeometry(1.2, 0.7, 0.85), craneDark);
counterweight.position.set(-0.7, -0.2, 0);
counterweight.castShadow = true;
jib.add(counterweight);
const trolley = new THREE.Group();
trolley.position.set(2, -0.18, 0);
jib.add(trolley);
const trolleyBody = new THREE.Mesh(new THREE.BoxGeometry(0.48, 0.32, 0.48), craneDark);
trolleyBody.castShadow = true;
trolley.add(trolleyBody);
const hook = new THREE.Group();
hook.position.y = -1.12;
trolley.add(hook);
const hookRing = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.035, 5, 12, Math.PI * 1.55),
craneDark
);
hookRing.rotation.z = Math.PI * 0.75;
hookRing.castShadow = true;
hook.add(hookRing);
const cable = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 1, 5),
flat(0x253136)
);
trolley.add(cable);
const HOOK_ORIGIN_Y = crane.position.y + jib.position.y + trolley.position.y;
const sourceRing = new THREE.Mesh(
new THREE.TorusGeometry(1.34, 0.045, 6, 48),
new THREE.MeshBasicMaterial({ color: 0x79efc0, transparent: true, opacity: 0.95 })
);
sourceRing.rotation.x = Math.PI / 2;
sourceRing.position.y = 0.16;
sourceRing.visible = false;
scene.add(sourceRing);
const destRing = new THREE.Mesh(
new THREE.TorusGeometry(1.34, 0.045, 6, 48),
new THREE.MeshBasicMaterial({ color: 0x66bfff, transparent: true, opacity: 0.95 })
);
destRing.rotation.x = Math.PI / 2;
destRing.position.y = 0.17;
destRing.visible = false;
scene.add(destRing);
let selectedSource = null;
let selectedDestination = null;
let pickMode = "source";
let busy = false;
let carrying = null;
let currentPlan = [];
let toastTimer;
function distanceFromCrane(location) {
return Math.hypot(location.x - CRANE_BASE.x, location.z - CRANE_BASE.z);
}
function setCheck(id, state, text) {
const element = $(id);
element.classList.remove("pass", "fail");
if (state) element.classList.add(state);
element.querySelector(".check-icon").textContent = state === "pass" ? "✓" : state === "fail" ? "×" : "?";
element.querySelector(".check-value").textContent = text;
}
function showToast(message, success = false) {
clearTimeout(toastTimer);
const toast = $("toast");
toast.textContent = message;
toast.classList.toggle("success", success);
toast.classList.add("show");
toastTimer = setTimeout(() => toast.classList.remove("show"), 2700);
}
function validateSelection() {
const result = {
ok: false,
reason: "Select a source and destination.",
source: null,
destination: null,
sourceTop: false,
reach: false,
capacity: false,
rule: false
};
if (!selectedSource || !selectedDestination) return result;
const item = items[selectedSource];
const source = locations[item.locationId];
const destination = locations[selectedDestination];
result.source = source;
result.destination = destination;
const exposed = source.items.length > 0 && source.items[source.items.length - 1] === item.id;
result.sourceTop = exposed;
const sourceReach = distanceFromCrane(source);
const destinationReach = distanceFromCrane(destination);
result.reach = sourceReach <= MAX_REACH && destinationReach <= MAX_REACH;
result.sourceReach = sourceReach;
result.destinationReach = destinationReach;
const movableSource = source.kind === "work" || source.kind === "spare";
const sameBay = source.id === destination.id;
if (destination.kind === "spare") {
result.rule = item.id !== "target";
result.capacity = destination.items.length < destination.capacity;
} else if (destination.kind === "goal") {
result.rule = item.id === "target";
result.capacity = destination.items.length === 0;
} else {
result.rule = false;
result.capacity = false;
}
result.ok = exposed && movableSource && !sameBay && result.reach && result.rule && result.capacity;
if (!exposed) result.reason = `${item.label} is buried. Move the exposed cargo above it first.`;
else if (!movableSource) result.reason = `${source.name} is a reserved inbound stack.`;
else if (sameBay) result.reason = "Pick and drop bays must be different.";
else if (!result.reach) result.reason = `Outside crane reach. Maximum radius is ${MAX_REACH.toFixed(1)} m.`;
else if (destination.kind === "locked") result.reason = `${destination.name} is reserved and cannot receive cargo.`;
else if (destination.kind === "goal" && item.id !== "target") result.reason = "Only the TARGET container may enter OUTBOUND.";
else if (destination.kind === "spare" && item.id === "target") result.reason = "TARGET must be delivered directly to OUTBOUND.";
else if (!result.capacity) result.reason = `${destination.id} is full (${destination.items.length}/${destination.capacity}).`;
return result;
}
function refreshHighlights(validation = null) {
Object.values(locations).forEach(location => {
const active = validation?.ok && validation.destination.id === location.id;
location.padMaterial.emissiveIntensity = active ? 0.32 : location.kind === "locked" ? 0.02 : 0.06;
});
Object.values(items).forEach(item => {
const selected = item.id === selectedSource;
item.material.emissiveIntensity = selected ? 0.24 : 0.015;
});
if (selectedSource && items[selectedSource].locationId) {
const location = locations[items[selectedSource].locationId];
sourceRing.position.set(location.x, 0.16, location.z);
sourceRing.visible = true;
} else {
sourceRing.visible = false;
}
if (selectedDestination) {
const location = locations[selectedDestination];
destRing.position.set(location.x, 0.17, location.z);
destRing.visible = true;
destRing.material.color.setHex(validation?.ok ? 0x79efc0 : 0x66bfff);
} else {
destRing.visible = false;
}
}
function refreshUI() {
const sourceItem = selectedSource ? items[selectedSource] : null;
const destination = selectedDestination ? locations[selectedDestination] : null;
$("sourceValue").textContent = sourceItem
? `${sourceItem.label} · ${sourceItem.locationId}`
: "Not selected";
$("destValue").textContent = destination
? `${destination.id} · ${destination.name}`
: "Not selected";
$("sourceMode").classList.toggle("active", pickMode === "source");
$("destMode").classList.toggle("active", pickMode === "dest");
const validation = validateSelection();
if (!sourceItem || !destination) {
["sourceCheck", "reachCheck", "capacityCheck", "ruleCheck"].forEach(id => setCheck(id, null, "WAIT"));
} else {
setCheck("sourceCheck", validation.sourceTop ? "pass" : "fail",
validation.sourceTop ? "EXPOSED" : "BLOCKED");
setCheck("reachCheck", validation.reach ? "pass" : "fail",
`${validation.sourceReach.toFixed(1)} / ${validation.destinationReach.toFixed(1)} M`);
setCheck("capacityCheck", validation.capacity ? "pass" : "fail",
`${destination.items.length}/${destination.capacity}`);
setCheck("ruleCheck", validation.rule ? "pass" : "fail",
validation.rule ? "CLEAR" : "BLOCKED");
}
const badge = $("verdictBadge");
badge.className = "";
if (busy) {
badge.textContent = "Crane moving";
} else if (sourceItem && destination) {
badge.textContent = validation.ok ? "Legal" : "Invalid";
badge.classList.add(validation.ok ? "ready" : "invalid");
} else {
badge.textContent = "Waiting";
}
$("moveBtn").disabled = !validation.ok || busy;
$("autoBtn").disabled = busy || currentPlan.length === 0;
$("planBtn").disabled = busy;
["S1", "S2"].forEach(id => {
const location = locations[id];
$(`${id.toLowerCase()}Count`).textContent = `${location.items.length} / ${location.capacity}`;
$(`${id.toLowerCase()}Fill`).style.width = `${location.items.length / location.capacity * 100}%`;
});
const solved = locations.OUT.items.length === 1 && locations.OUT.items[0] === "target";
$("winBanner").classList.toggle("show", solved);
$("topStatus").textContent = solved ? "CLEAR" : busy ? "MOVING" : "ACTIVE";
refreshHighlights(validation);
}
function setMode(mode) {
pickMode = mode;
refreshUI();
}
function selectSource(itemId) {
if (busy) return;
selectedSource = itemId;
selectedDestination = null;
pickMode = "dest";
refreshUI();
}
function selectDestination(locationId) {
if (busy) return;
selectedDestination = locationId;
refreshUI();
}
function clearSelection() {
selectedSource = null;
selectedDestination = null;
pickMode = "source";
refreshUI();
}
// Breadth-first planner over legal stack states.
function clonePlanState() {
const state = {};
Object.values(locations).forEach(location => state[location.id] = [...location.items]);
return state;
}
function planStateKey(state) {
return Object.keys(state).sort().map(id => `${id}:${state[id].join(",")}`).join("|");
}
function computePlan() {
const start = clonePlanState();
const queue = [{ state: start, path: [] }];
const visited = new Set([planStateKey(start)]);
const movable = ["A", "S1", "S2"];
const spares = ["S1", "S2"];
while (queue.length) {
const current = queue.shift();
const state = current.state;
if (state.OUT.length === 1 && state.OUT[0] === "target") {
return current.path;
}
const moves = [];
for (const sourceId of movable) {
const sourceItems = state[sourceId];
if (!sourceItems.length) continue;
const itemId = sourceItems[sourceItems.length - 1];
if (itemId === "target") {
if (state.OUT.length === 0 && distanceFromCrane(locations.OUT) <= MAX_REACH) {
moves.push({ itemId, from: sourceId, to: "OUT" });
}
} else {
for (const destinationId of spares) {
if (destinationId === sourceId) continue;
if (state[destinationId].length >= locations[destinationId].capacity) continue;
if (distanceFromCrane(locations[destinationId]) > MAX_REACH) continue;
moves.push({ itemId, from: sourceId, to: destinationId });
}
}
}
// Prefer filling the larger spare first, then delivery.
moves.sort((a, b) => {
if (a.to === "OUT") return -1;
if (b.to === "OUT") return 1;
return locations[b.to].capacity - locations[a.to].capacity;
});
for (const move of moves) {
const next = {};
Object.keys(state).forEach(id => next[id] = [...state[id]]);
const sourceIndex = next[move.from].indexOf(move.itemId);
next[move.from].splice(sourceIndex, 1);
next[move.to].push(move.itemId);
const key = planStateKey(next);
if (visited.has(key)) continue;
visited.add(key);
queue.push({
state: next,
path: [...current.path, move]
});
}
}
return [];
}
function renderPlan() {
currentPlan = computePlan();
const list = $("planList");
if (!currentPlan.length) {
list.innerHTML = `<div style="padding:12px;color:#91a49f;font-size:11px;line-height:1.5">No legal sequence remains. Reset the yard to recover the target.</div>`;
$("autoBtn").disabled = true;
return;
}
list.innerHTML = "";
currentPlan.forEach((move, index) => {
const button = document.createElement("button");
button.className = "plan-step";
button.innerHTML = `
<div class="step-number">${index + 1}</div>
<div class="step-copy">
<strong>${items[move.itemId].label} → ${move.to}</strong>
<small>${move.from} to ${move.to} · reach verified</small>
</div>
<div class="step-check">✓</div>
`;
button.addEventListener("click", () => {
selectedSource = move.itemId;
selectedDestination = move.to;
pickMode = "source";
refreshUI();
showToast(`Step ${index + 1} loaded into the validator.`);
});
list.appendChild(button);
});
$("autoBtn").disabled = false;
}
const easeInOut = t => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
function angleFor(location) {
const dx = location.x - CRANE_BASE.x;
const dz = location.z - CRANE_BASE.z;
return Math.atan2(-dz, dx);
}
function animateArm(location, duration = 750) {
const startAngle = jib.rotation.y;
const targetAngle = angleFor(location);
let delta = ((targetAngle - startAngle + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;
const startDistance = trolley.position.x;
const targetDistance = distanceFromCrane(location);
const startTime = performance.now();
return new Promise(resolve => {
function frame(now) {
const t = Math.min(1, (now - startTime) / duration);
const eased = easeInOut(t);
jib.rotation.y = startAngle + delta * eased;
trolley.position.x = startDistance + (targetDistance - startDistance) * eased;
if (t < 1) requestAnimationFrame(frame);
else resolve();
}
requestAnimationFrame(frame);
});
}
function animateHook(worldY, duration = 500) {
const start = hook.position.y;
const target = worldY - HOOK_ORIGIN_Y;
const startTime = performance.now();
return new Promise(resolve => {
function frame(now) {
const t = Math.min(1, (now - startTime) / duration);
hook.position.y = start + (target - start) * easeInOut(t);
if (t < 1) requestAnimationFrame(frame);
else resolve();
}
requestAnimationFrame(frame);
});
}
async function animateMove(item, destinationId) {
busy = true;
clearSelection();
refreshUI();
const source = locations[item.locationId];
const sourcePosition = item.mesh.position.clone();
const sourceIndex = source.items.indexOf(item.id);
await animateArm(source, 700);
await animateHook(sourcePosition.y + ITEM_H / 2 + 0.04, 450);
source.items.splice(sourceIndex, 1);
item.locationId = null;
carrying = item;
await animateHook(3.55, 420);
await animateArm(locations[destinationId], 850);
const destination = locations[destinationId];
const destinationIndex = destination.items.length;
const destinationPosition = slotPosition(destination, destinationIndex);
await animateHook(destinationPosition.y + ITEM_H / 2 + 0.04, 620);
destination.items.push(item.id);
item.locationId = destinationId;
item.mesh.position.copy(destinationPosition);
carrying = null;
await animateHook(3.55, 420);
busy = false;
renderPlan();
refreshUI();
}
async function executeMove() {
if (busy) return;
const validation = validateSelection();
if (!validation.ok) {
showToast(validation.reason);
return;
}
const item = items[selectedSource];
const destination = selectedDestination;
clearSelection();
await animateMove(item, destination);
showToast(`${item.label} moved to ${destination}.`, true);
}
async function runPlan() {
if (busy) return;
renderPlan();
if (!currentPlan.length) return;
$("autoBtn").disabled = true;
for (const step of currentPlan) {
if (busy) break;
await animateMove(items[step.itemId], step.to);
await new Promise(resolve => setTimeout(resolve, 160));
}
showToast("Legal recovery sequence completed.", true);
}
function resetYard() {
if (busy) return;
carrying = null;
Object.values(locations).forEach(location => location.items.length = 0);
locations.A.items = ["target", "blockA", "blockB", "blockC"];
locations.B.items = ["inbound1", "inbound2"];
locations.C.items = ["inbound3"];
locations.S1.items = [];
locations.S2.items = [];
locations.OUT.items = [];
locations.FAR.items = ["overflow"];
Object.values(items).forEach(item => {
item.locationId = item.location;
item.mesh.position.copy(slotPosition(locations[item.location], locations[item.location].items.indexOf(item.id)));
});
jib.rotation.y = angleFor(locations.A);
trolley.position.x = distanceFromCrane(locations.A);
hook.position.y = 3.55 - HOOK_ORIGIN_Y;
clearSelection();
renderPlan();
refreshUI();
showToast("Yard reset.", true);
}
// Pointer picking.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = { x: event.clientX, y: event.clientY, moved: false };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 5) pointerDown.moved = true;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(cameraState.polar - dy * 0.005, 0.42, 1.25);
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
if (!pointerDown) return;
const wasClick = !pointerDown.moved;
pointerDown = null;
if (!wasClick || busy) return;
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickables, true);
if (!hits.length) return;
let object = hits[0].object;
while (object && !object.userData.itemId && !object.userData.locationId) {
object = object.parent;
}
if (!object) return;
if (object.userData.itemId) {
const item = items[object.userData.itemId];
if (pickMode === "source") {
selectSource(item.id);
} else {
selectDestination(item.locationId);
}
} else if (object.userData.locationId) {
const location = locations[object.userData.locationId];
if (pickMode === "source") {
if (location.items.length) {
const top = location.items[location.items.length - 1];
selectSource(top);
} else {
showToast(`${location.id} is empty. Choose a container as the source.`);
}
} else {
selectDestination(location.id);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => pointerDown = null);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
10.5,
24
);
updateCamera();
}, { passive: false });
$("sourceMode").addEventListener("click", () => setMode("source"));
$("destMode").addEventListener("click", () => setMode("dest"));
$("moveBtn").addEventListener("click", executeMove);
$("clearBtn").addEventListener("click", clearSelection);
$("resetBtn").addEventListener("click", resetYard);
$("planBtn").addEventListener("click", () => {
renderPlan();
showToast("Legal move plan recalculated.", true);
});
$("autoBtn").addEventListener("click", runPlan);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const hookWorld = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
if (carrying) {
hook.getWorldPosition(hookWorld);
carrying.mesh.position.set(
hookWorld.x,
hookWorld.y - carrying.height / 2 - 0.04,
hookWorld.z
);
}
const cableLength = Math.max(0.05, -hook.position.y);
cable.position.y = hook.position.y / 2;
cable.scale.y = cableLength;
renderer.render(scene, camera);
}
renderPlan();
refreshUI();
animate();
</script>
</body>
</html>
```