File size: 24,920 Bytes
f22aa2f
 
 
 
 
1e5f49f
f22aa2f
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
1e5f49f
f22aa2f
1e5f49f
 
f22aa2f
1e5f49f
 
f22aa2f
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
1e5f49f
f22aa2f
1e5f49f
f22aa2f
 
 
1e5f49f
 
 
 
f22aa2f
 
1e5f49f
f22aa2f
 
 
1e5f49f
 
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
1e5f49f
 
f22aa2f
 
 
1e5f49f
 
 
f22aa2f
 
1e5f49f
f22aa2f
 
 
 
 
 
1e5f49f
 
 
 
f22aa2f
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
 
 
 
 
 
 
f22aa2f
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
 
 
 
 
 
 
f22aa2f
1e5f49f
 
 
 
 
 
 
f22aa2f
 
 
 
 
 
 
 
 
 
 
1e5f49f
 
f22aa2f
 
 
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
 
 
 
 
1e5f49f
f22aa2f
 
 
1e5f49f
f22aa2f
 
 
 
1e5f49f
 
 
 
 
 
f22aa2f
 
1e5f49f
f22aa2f
 
 
 
1e5f49f
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
 
 
1e5f49f
f22aa2f
1e5f49f
f22aa2f
 
 
 
 
1e5f49f
f22aa2f
 
1e5f49f
 
 
f22aa2f
 
 
1e5f49f
 
 
f22aa2f
 
1e5f49f
 
 
 
f22aa2f
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
1e5f49f
 
f22aa2f
 
 
 
 
1e5f49f
 
f22aa2f
 
 
 
 
 
1e5f49f
f22aa2f
1e5f49f
f22aa2f
 
1e5f49f
 
 
 
f22aa2f
 
 
 
 
1e5f49f
f22aa2f
 
 
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f49f
f22aa2f
 
1e5f49f
 
 
 
f22aa2f
1e5f49f
f22aa2f
1e5f49f
 
 
f22aa2f
 
1e5f49f
 
 
 
 
 
 
 
 
f22aa2f
1e5f49f
f22aa2f
1e5f49f
 
 
f22aa2f
 
 
 
1e5f49f
f22aa2f
 
 
1e5f49f
 
f22aa2f
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>World Grid Test</title>
    <style>
        body { font-family: 'Courier New', monospace; background-color: #111; color: #eee; margin: 0; padding: 0; overflow: hidden; display: flex; flex-direction: column; height: 100vh; }
        #game-container { display: flex; flex-grow: 1; overflow: hidden; }
        #scene-container { flex-grow: 3; position: relative; border-right: 2px solid #444; min-width: 250px; background-color: #000; height: 100%; box-sizing: border-box; overflow: hidden; cursor: default; }
        #ui-container { flex-grow: 2; padding: 25px; overflow-y: auto; background-color: #2b2b2b; min-width: 320px; height: 100%; box-sizing: border-box; display: flex; flex-direction: column; }
        #scene-container canvas { display: block; }
        #story-title { color: #f0c060; margin: 0 0 15px 0; padding-bottom: 10px; border-bottom: 1px solid #555; font-size: 1.6em; text-shadow: 1px 1px 1px #000; }
        #story-content { margin-bottom: 25px; line-height: 1.7; flex-grow: 1; font-size: 1.1em; }
        #stats-inventory-container { margin-bottom: 25px; padding: 15px; border: 1px solid #444; border-radius: 4px; background-color: #333; font-size: 0.95em; }
        #stats-display, #inventory-display { margin-bottom: 10px; line-height: 1.8; }
        #stats-display span, #inventory-display .item-tag { display: inline-block; background-color: #484848; padding: 3px 9px; border-radius: 15px; margin: 0 8px 5px 0; border: 1px solid #6a6a6a; white-space: nowrap; box-shadow: inset 0 1px 2px rgba(0,0,0,0.3); }
        #stats-display strong, #inventory-display strong { color: #ccc; margin-right: 6px; }
        #inventory-display em { color: #888; font-style: normal; }
        #choices-container { margin-top: auto; padding-top: 20px; border-top: 1px solid #555; }
        #choices-container h3 { margin-top: 0; margin-bottom: 12px; color: #ccc; font-size: 1.1em; }
        #choices { display: flex; flex-direction: column; gap: 12px; }
        .choice-button { display: block; width: 100%; padding: 12px 15px; margin-bottom: 0; background-color: #555; color: #eee; border: 1px solid #777; border-radius: 4px; cursor: pointer; text-align: left; font-family: 'Courier New', monospace; font-size: 1.05em; transition: background-color 0.2s, border-color 0.2s, box-shadow 0.1s; box-sizing: border-box; }
        .choice-button:hover:not(:disabled) { background-color: #e0b050; color: #111; border-color: #c89040; box-shadow: 0 0 5px rgba(255, 200, 100, 0.5); }
        .choice-button:disabled { background-color: #404040; color: #777; cursor: not-allowed; border-color: #555; opacity: 0.7; }
        .message { padding: 8px 12px; margin-bottom: 1em; border-left-width: 3px; border-left-style: solid; font-size: 0.95em; background-color: rgba(255, 255, 255, 0.05); border-color: #666; color: #aaa;}
        .message-failure { color: #f88; border-left-color: #a44; }
    </style>
</head>
<body>
    <div id="game-container">
        <div id="scene-container"></div>
        <div id="ui-container">
            <h2 id="story-title">Initializing...</h2>
            <div id="story-content"><p>Loading assets...</p></div>
            <div id="stats-inventory-container">
                <div id="stats-display">HP: ?/? | XP: ?</div>
                <div id="inventory-display">Inventory: Empty</div>
            </div>
            <div id="choices-container">
                <h3>What will you do?</h3>
                <div id="choices">Loading...</div>
            </div>
        </div>
    </div>

    <script type="importmap">
        { "imports": {
                "three": "https://unpkg.com/[email protected]/build/three.module.js",
                "three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
        }}
    </script>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        // Font loading removed for simplicity for now

        const sceneContainer = document.getElementById('scene-container');
        const storyTitleElement = document.getElementById('story-title');
        const storyContentElement = document.getElementById('story-content');
        const choicesElement = document.getElementById('choices');
        const statsElement = document.getElementById('stats-display');
        const inventoryElement = document.getElementById('inventory-display');

        let scene, camera, renderer, clock, controls;
        let worldGroup = null;
        let zoneGroups = {}; // Stores the THREE.Group for loaded zones { zoneId: group }
        let currentMessage = "";

        const MAT = {
            stone: new THREE.MeshStandardMaterial({ color: 0x777788, roughness: 0.85 }),
            wood: new THREE.MeshStandardMaterial({ color: 0x9F6633, roughness: 0.75 }),
            leaf: new THREE.MeshStandardMaterial({ color: 0x3E9B4E, roughness: 0.6, side: THREE.DoubleSide }),
            ground: new THREE.MeshStandardMaterial({ color: 0x556B2F, roughness: 0.95 }),
            dirt: new THREE.MeshStandardMaterial({ color: 0x8B5E3C, roughness: 0.9 }),
            grass: new THREE.MeshStandardMaterial({ color: 0x4CB781, roughness: 0.85 }),
            water: new THREE.MeshStandardMaterial({ color: 0x4682B4, roughness: 0.3, transparent: true, opacity: 0.85 }),
            simple: new THREE.MeshStandardMaterial({ color: 0xaaaaaa, roughness: 0.8 }),
        };

        // --- Game State ---
        let gameState = {}; // Initialized in startGame

        // --- Core Functions ---

        function initThreeJS() {
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0x1a1a1a);
            clock = new THREE.Clock();
            worldGroup = new THREE.Group();
            scene.add(worldGroup);

            const width = sceneContainer.clientWidth || 1;
            const height = sceneContainer.clientHeight || 1;
            camera = new THREE.PerspectiveCamera(60, width / height, 0.1, 1000);
            camera.position.set(0, 6, 12); // Slightly higher view

            renderer = new THREE.WebGLRenderer({ antialias: true });
            renderer.setSize(width, height);
            renderer.shadowMap.enabled = true;
            renderer.shadowMap.type = THREE.PCFSoftShadowMap;
            renderer.toneMapping = THREE.ACESFilmicToneMapping;
            renderer.outputColorSpace = THREE.SRGBColorSpace;
            sceneContainer.appendChild(renderer.domElement);

            controls = new OrbitControls(camera, renderer.domElement);
            controls.enableDamping = true;
            controls.dampingFactor = 0.1;
            controls.target.set(0, 1, 0);
            controls.maxPolarAngle = Math.PI / 2 - 0.05;

            window.addEventListener('resize', onWindowResize, false);
            // Removed mouse listeners for now
            setTimeout(onWindowResize, 50);
            animate();
        }

        function onWindowResize() {
             if (!renderer || !camera || !sceneContainer) return;
             const width = sceneContainer.clientWidth || 1;
             const height = sceneContainer.clientHeight || 1;
             camera.aspect = width / height;
             camera.updateProjectionMatrix();
             renderer.setSize(width, height);
        }

        function animate() {
            requestAnimationFrame(animate);
            controls.update(); // Need to update controls
            if (renderer && scene && camera) renderer.render(scene, camera);
        }

        function createMesh(geometry, material, pos = {x:0,y:0,z:0}, rot = {x:0,y:0,z:0}, scale = {x:1,y:1,z:1}) {
             const mesh = new THREE.Mesh(geometry, material);
             mesh.position.set(pos.x, pos.y, pos.z);
             mesh.rotation.set(rot.x, rot.y, rot.z);
             mesh.scale.set(scale.x, scale.y, scale.z);
             mesh.castShadow = true; mesh.receiveShadow = true;
             return mesh;
         }

        function createGround(material = MAT.ground, size = 20) {
             const geo = new THREE.PlaneGeometry(size, size);
             const ground = new THREE.Mesh(geo, material);
             ground.rotation.x = -Math.PI / 2; ground.position.y = 0;
             ground.receiveShadow = true; ground.castShadow = false;
             return ground;
        }

        function setupLighting(type = 'default') {
             worldGroup.children.filter(c => c.isLight).forEach(light => worldGroup.remove(light)); // Remove old lights from worldGroup

             let ambientIntensity = 0.5;
             let dirIntensity = 1.0;
             let dirColor = 0xffffff;
             let dirPosition = new THREE.Vector3(10, 15, 8);

             if (type === 'forest') { ambientIntensity = 0.4; dirIntensity = 0.8; dirColor = 0xccffcc; dirPosition = new THREE.Vector3(5, 10, 5); }
             if (type === 'cave') { ambientIntensity = 0.2; dirIntensity = 0; } // Rely on point light added in zone creator
             if (type === 'ruins') { ambientIntensity = 0.4; dirIntensity = 0.7; dirColor = 0xaaaaff; dirPosition = new THREE.Vector3(-8, 12, -5); }

             const ambientLight = new THREE.AmbientLight(0xffffff, ambientIntensity);
             worldGroup.add(ambientLight); // Add lights to worldGroup

             if (dirIntensity > 0) {
                 const directionalLight = new THREE.DirectionalLight(dirColor, dirIntensity);
                 directionalLight.position.copy(dirPosition);
                 directionalLight.castShadow = true;
                 directionalLight.shadow.mapSize.set(1024, 1024);
                 directionalLight.shadow.camera.near = 0.5; directionalLight.shadow.camera.far = 50;
                 const sb = 20; // shadow bounds
                 directionalLight.shadow.camera.left = -sb; directionalLight.shadow.camera.right = sb;
                 directionalLight.shadow.camera.top = sb; directionalLight.shadow.camera.bottom = -sb;
                 directionalLight.shadow.bias = -0.0005;
                 worldGroup.add(directionalLight);
             }
        }

        // --- Zone Creation Functions ---
        function createFieldZone(zoneId) {
            const group = new THREE.Group();
            group.add(createGround(MAT.grass, 30));
            const rockGeo = new THREE.IcosahedronGeometry(0.5 + Math.random()*0.5, 0);
            for(let i=0; i<5; i++) {
                 group.add(createMesh(rockGeo, MAT.stone, {x: (Math.random()-0.5)*25, y:0.3, z: (Math.random()-0.5)*25}));
            }
            group.visible = false;
            return {
                group,
                lighting: 'default',
                title: "Open Field",
                entryText: "You stand in an open, grassy field.",
                options: [] // Neighbors added dynamically
            };
        }

        function createForestZone(zoneId) {
             const group = new THREE.Group();
             group.add(createGround(MAT.ground, 30));
             const trunkGeo = new THREE.CylinderGeometry(0.2, 0.3, 4, 8);
             const leafGeo = new THREE.SphereGeometry(1.5, 8, 6);
             for(let i=0; i<25; i++) {
                 const x = (Math.random() - 0.5) * 28;
                 const z = (Math.random() - 0.5) * 28;
                 if(Math.sqrt(x*x+z*z) < 1) continue;
                 const tree = new THREE.Group();
                 const trunk = createMesh(trunkGeo, MAT.wood, {y: 2});
                 const leaves = createMesh(leafGeo, MAT.leaf, {y: 4.5});
                 tree.add(trunk); tree.add(leaves);
                 tree.position.set(x, 0, z);
                 tree.rotation.y = Math.random() * Math.PI * 2;
                 group.add(tree);
             }
             group.visible = false;
             return {
                group,
                lighting: 'forest',
                title: "Dark Forest",
                entryText: "Sunlight is sparse beneath the thick canopy.",
                options: [
                    { text: "Search for tracks (TBC)", action: "search"} // Placeholder for future skill check
                ]
             };
        }

        function createCaveZone(zoneId) {
            const group = new THREE.Group();
            group.add(createGround(MAT.stone.clone().set({color: 0x555560}), 18));
             const wallGeo = new THREE.SphereGeometry(12, 32, 16, 0, Math.PI*2, 0, Math.PI*0.7);
             const walls = createMesh(wallGeo, MAT.stone, {y: 4});
             walls.material.side = THREE.BackSide;
             group.add(walls);
             const coneGeo = new THREE.ConeGeometry(0.2, 1.0, 8);
             for(let i=0; i<15; i++){
                 const st = createMesh(coneGeo, MAT.stone, {x: (Math.random()-0.5)*16, y: 6 + Math.random()*2, z: (Math.random()-0.5)*16}, {x:Math.PI});
                 group.add(st);
             }
             // Add point light specific to cave
             const ptLight = new THREE.PointLight(0xffaa55, 1.5, 12, 1);
             ptLight.position.set(0, 3, 0);
             ptLight.castShadow = true;
             ptLight.shadow.mapSize.set(512, 512);
             group.add(ptLight);

             group.visible = false;
             return {
                group,
                lighting: 'cave',
                title: "Dim Cave",
                entryText: "It's dark and damp in here. Water drips.",
                options: []
             };
         }

         function createRuinsZone(zoneId) {
             const group = new THREE.Group();
             group.add(createGround(MAT.dirt, 25));
             const wallGeo = new THREE.BoxGeometry(0.5, 2, 3);
             for(let i=0; i<8; i++) {
                 const wall = createMesh(wallGeo, MAT.stone,
                     {x: (Math.random()-0.5)*20, y:1, z: (Math.random()-0.5)*20},
                     {y: Math.random() * Math.PI}
                 );
                 wall.scale.y = 0.5 + Math.random() * 0.8;
                 wall.rotation.x = (Math.random()-0.5)*0.1;
                 wall.rotation.z = (Math.random()-0.5)*0.1;
                 group.add(wall);
             }
             group.visible = false;
             return {
                 group,
                 lighting: 'ruins',
                 title: "Crumbling Ruins",
                 entryText: "The wind whistles through broken walls.",
                 options: [
                      { text: "Examine rubble (TBC)", action: "examine_rubble"}
                 ]
             };
         }

        // --- Map Grid & Zone Definitions ---
        const MAP_ROWS = 3;
        const MAP_COLS = 4;
        const zoneCreators = {}; // { "zone_0_0": createFunction, ... }

        function getZoneId(row, col) { return `zone_${row}_${col}`; }

        function populateZoneCreators() {
            for (let r = 0; r < MAP_ROWS; r++) {
                for (let c = 0; c < MAP_COLS; c++) {
                    const zoneId = getZoneId(r, c);
                    let creatorFunc;
                    // Simple pattern for variety
                    if (r === 0) creatorFunc = createForestZone;
                    else if (r === 1 && c % 2 === 0) creatorFunc = createFieldZone;
                    else if (r === 1 && c % 2 !== 0) creatorFunc = createRuinsZone;
                    else creatorFunc = createCaveZone;
                    zoneCreators[zoneId] = () => creatorFunc(zoneId); // Store function that calls creator with ID
                }
            }
             console.log("Zone creators populated.");
        }

        function getZoneNeighbors(zoneId) {
            const parts = zoneId.split('_');
            if (parts.length !== 3) return {};
            const r = parseInt(parts[1]);
            const c = parseInt(parts[2]);
            const neighbors = {};
            if (r > 0) neighbors.north = getZoneId(r - 1, c);
            if (r < MAP_ROWS - 1) neighbors.south = getZoneId(r + 1, c);
            if (c > 0) neighbors.west = getZoneId(r, c - 1);
            if (c < MAP_COLS - 1) neighbors.east = getZoneId(r, c + 1);
            return neighbors;
        }

        function startGame() {
            const defaultChar = {
                name: "Player",
                stats: { hp: 20, maxHp: 20, xp: 0 },
                inventory: []
            };
            gameState = {
                currentZoneId: null,
                character: JSON.parse(JSON.stringify(defaultChar))
            };
            populateZoneCreators();
            console.log("Starting new game:", gameState);
            transitionToZone(getZoneId(1, 1)); // Start in a central zone
        }

        function transitionToZone(newZoneId) {
             console.log(`Transitioning to ${newZoneId}`);
             currentMessage = "";

             // Hide current zone if exists
             if (gameState.currentZoneId && zoneGroups[gameState.currentZoneId]) {
                 zoneGroups[gameState.currentZoneId].visible = false;
             }

             let zoneInfo;
             // Load or unhide new zone
             if (zoneGroups[newZoneId]) {
                 zoneInfo = zoneGroups[newZoneId];
                 zoneInfo.group.visible = true;
             } else {
                 const creator = zoneCreators[newZoneId];
                 if (creator) {
                    zoneInfo = creator(); // Create the zone { group, lighting, title, entryText, options }
                    zoneGroups[newZoneId] = zoneInfo; // Store the created zone data
                    worldGroup.add(zoneInfo.group);
                    zoneInfo.group.visible = true;
                 } else {
                    console.error(`No creator found for zone ID: ${newZoneId}, going to default`);
                     const fallbackId = getZoneId(1, 1);
                     if (!zoneGroups[fallbackId]) { // Ensure fallback exists
                         zoneGroups[fallbackId] = zoneCreators[fallbackId]();
                         worldGroup.add(zoneGroups[fallbackId].group);
                     }
                     zoneInfo = zoneGroups[fallbackId];
                     zoneInfo.group.visible = true;
                     newZoneId = fallbackId;
                     currentMessage = `<p class="message message-failure">Error: Invalid zone transition.</p>`;
                 }
             }

             gameState.currentZoneId = newZoneId;
             setupLighting(zoneInfo.lighting || 'default');

             // Basic camera reset
             camera.position.set(0, 6, 12);
             controls.target.set(0, 1, 0);
             controls.update();

             renderCurrentPageUI();
        }


        function renderCurrentPageUI() {
            const zoneInfo = zoneGroups[gameState.currentZoneId];

            if (!zoneInfo) {
                console.error(`No zone info loaded for ${gameState.currentZoneId}`);
                storyTitleElement.textContent = "Error";
                storyContentElement.innerHTML = currentMessage + "<p>Cannot render current zone.</p>";
                choicesElement.innerHTML = `<button class="choice-button" onclick="transitionToZone('${getZoneId(1, 1)}')">Return to Start</button>`;
                updateStatsDisplay();
                updateInventoryDisplay();
                return;
            }

             storyTitleElement.textContent = zoneInfo.title || "Unknown Zone";
             storyContentElement.innerHTML = currentMessage + (zoneInfo.entryText ? `<p>${zoneInfo.entryText}</p>` : '');
             choicesElement.innerHTML = '';

             // Add Navigation Options
             const neighbors = getZoneNeighbors(gameState.currentZoneId);
             const directions = {'north': 'North', 'south': 'South', 'east': 'East', 'west': 'West'};
             for(const dir in neighbors) {
                  const neighborId = neighbors[dir];
                  const button = document.createElement('button');
                  button.classList.add('choice-button');
                  button.textContent = `Go ${directions[dir]}`;
                  button.onclick = () => transitionToZone(neighborId);
                  choicesElement.appendChild(button);
             }

             // Add Zone Specific Options
             if (zoneInfo.options && zoneInfo.options.length > 0) {
                 zoneInfo.options.forEach(option => {
                     const button = document.createElement('button');
                     button.classList.add('choice-button');
                     button.textContent = option.text;
                     // Add onclick based on option.action or other properties later
                     choicesElement.appendChild(button);
                 });
             }

             updateStatsDisplay();
             updateInventoryDisplay();
             updateActionInfo();
        }

        function updateStatsDisplay() {
            const { hp, maxHp, xp } = gameState.character.stats;
            const hpColor = hp / maxHp < 0.3 ? '#f88' : (hp / maxHp < 0.6 ? '#fd5' : '#8f8');
            statsElement.innerHTML = `<strong>Stats:</strong> <span style="color:${hpColor}">HP: ${hp}/${maxHp}</span> <span>XP: ${xp}</span>`;
        }

        function updateInventoryDisplay() {
             let invHtml = '<strong>Inventory:</strong> ';
             if (gameState.character.inventory.length === 0) {
                 invHtml += '<em>Empty</em>';
             } else {
                 gameState.character.inventory.forEach(item => {
                     const itemDef = itemsData[item] || { type: 'unknown', description: '???' };
                     const itemClass = `item-${itemDef.type || 'unknown'}`;
                     invHtml += `<span class="item-tag ${itemClass}" title="${itemDef.description}">${item}</span>`;
                 });
             }
             inventoryElement.innerHTML = invHtml;
        }

        function updateActionInfo() {
            actionInfoElement.textContent = `Zone: ${gameState.currentZoneId || 'None'}`;
        }

        // --- Pickup/Placement (Removed for now) ---
        function pickupItem() { console.log("Pickup disabled in this version."); }
        function placeItem() { console.log("Placement disabled in this version."); }
        function togglePlacementMode() { console.log("Placement disabled in this version.");}

        // --- Combat/Checks (Placeholders) ---
        function startCombat(enemyTypeId) {
            console.log("Combat Started (Placeholder):", enemyTypeId);
            currentMessage = `<p class="message message-combat">Placeholder: Encountered a ${enemyTypeId}!</p>`;
            renderCurrentPageUI(); // Re-render to show message
        }
        function handleCombatAction(action) {
             console.log("Combat Action (Placeholder):", action);
             currentMessage = `<p class="message message-info">Placeholder: Combat action '${action}' executed.</p>`;
             // Add basic HP loss for testing
             gameState.character.stats.hp -= 1;
             gameState.character.stats.hp = Math.max(0, gameState.character.stats.hp);
             if(gameState.character.stats.hp === 0) {
                 currentMessage += `<p class="message message-failure">You were defeated in placeholder combat!</p>`;
                 // Reset HP for now instead of game over
                 gameState.character.stats.hp = gameState.character.stats.maxHp;
             }
             renderCurrentPageUI();
        }
         // Make placeholders globally accessible if needed by inline HTML later
         window.startCombat = startCombat;
         window.handleCombatAction = handleCombatAction;


        // --- Initialization ---
        document.addEventListener('DOMContentLoaded', () => {
             console.log("DOM Ready - Initializing World Grid Test.");
             try {
                 initThreeJS();
                 if (!scene || !camera || !renderer) throw new Error("Three.js failed to initialize.");
                 startGame(); // Font not needed for this simplified version
                 console.log("Game world initialized and started.");
             } catch (error) {
                 console.error("Initialization failed:", error);
                 storyTitleElement.textContent = "Initialization Error";
                 storyContentElement.innerHTML = `<p style="color:red;">Failed to start game:</p><pre style="color:red; white-space: pre-wrap;">${error.stack || error}</pre>`;
                 if(sceneContainer) sceneContainer.innerHTML = '<p style="color:red; padding: 20px;">3D Scene Failed</p>';
                  document.getElementById('stats-inventory-container').style.display = 'none';
                  document.getElementById('choices-container').style.display = 'none';
             }
         });

    </script>
</body>
</html>