-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrender_template.html
More file actions
1198 lines (1042 loc) · 44 KB
/
render_template.html
File metadata and controls
1198 lines (1042 loc) · 44 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Macondo 3D Board Renderer</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
z-index: 100;
}
#canvas-container {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="info">
<strong>Macondo 3D Board</strong><br />
Drag to rotate | Scroll to zoom | Right-click to pan
<br /><br />
<div
id="heatmapInfo"
style="
margin: 10px 0;
padding: 8px;
background: rgba(255, 255, 255, 0.1);
border-radius: 4px;
display: none;
"
>
<strong>Heatmap:</strong><br />
<span id="heatmapPlay"></span><br />
<span id="heatmapPly"></span>
</div>
<button id="saveBtn" style="padding: 5px 10px; cursor: pointer">
Save as PNG
</button>
</div>
<div id="canvas-container"></div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FontLoader } from 'three/addons/loaders/FontLoader.js';
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
// Board data - will be replaced by Go template
const FEN_DATA = "{{.FEN}}";
const RACK_DATA = "{{.Rack}}";
const REMAINING_TILES = {{.RemainingTiles}};
const TILE_COLOR = "{{.TileColor}}";
const BOARD_COLOR = "{{.BoardColor}}";
const PLAYER0_NAME = "{{.Player0Name}}";
const PLAYER1_NAME = "{{.Player1Name}}";
const PLAYER0_SCORE = {{.Player0Score}};
const PLAYER1_SCORE = {{.Player1Score}};
const HEATMAP_DATA = {{.Heatmap}};
const HEATMAP_ACTIVE = {{.HeatmapActive}};
const HEATMAP_PLAY = "{{.HeatmapPlay}}";
const HEATMAP_PLY = {{.HeatmapPly}};
const PLAYER_ON_TURN = {{.PlayerOnTurn}};
const LAST_PLAY = "{{.LastPlay}}";
console.log("FEN_DATA:", FEN_DATA);
console.log("RACK_DATA:", RACK_DATA);
console.log("REMAINING_TILES:", REMAINING_TILES);
console.log("TILE_COLOR:", TILE_COLOR);
console.log("BOARD_COLOR:", BOARD_COLOR);
// Color maps
const tileColors = {
"orange": { hex: 0xff6b35, textColor: 0x000000 }, // True orange
"yellow": { hex: 0xffa500, textColor: 0x000000 }, // Bright yellow-orange
"pink": { hex: 0xff69b4, textColor: 0x000000 },
"red": { hex: 0xe53935, textColor: 0xffffff },
"blue": { hex: 0x1976d2, textColor: 0xffffff }, // Deep blue
"black": { hex: 0x2c2c2c, textColor: 0xffffff },
"white": { hex: 0xf5f5f5, textColor: 0x000000 }
};
const boardColors = {
"jade": 0x00ffbd,
"teal": 0x00897b,
"blue": 0x2196f3,
"purple": 0x9c27b0,
"green": 0x4caf50,
"darkgreen": 0x1b5e20,
"brown": 0x795548
};
const tileColorConfig = tileColors[TILE_COLOR] || tileColors["orange"];
const boardColorHex = boardColors[BOARD_COLOR] || boardColors["jade"];
// Calculate label color based on board brightness
function getLabelColor(boardColor) {
// Extract RGB components
const r = (boardColor >> 16) & 0xff;
const g = (boardColor >> 8) & 0xff;
const b = boardColor & 0xff;
// Calculate perceived brightness (0-255)
const brightness = (r * 0.299 + g * 0.587 + b * 0.114);
// If board is bright, use dark labels, otherwise use lighter labels
return brightness > 128 ? 0x333333 : 0x666666;
}
const labelColor = getLabelColor(boardColorHex);
// Constants from jade
const gridSize = {{.BoardDimension}};
const squareSize = 5 * 15 / gridSize; // Scale so total board footprint is always 75 units
const boardThickness = 2;
const gridHeight = 1;
const tileDepth = 1.5 * 15 / gridSize;
const offset = (gridSize * squareSize) / 2 - squareSize / 2;
const boardTileZPos = boardThickness / 2 + gridHeight;
// Rack constants
const rackHeight = 3;
const rackWidth = 50;
const rackDepth = 7;
const rackYPos = -38;
// Bonus square layout (from game board)
const gridLayout = {{.BoardLayout}};
const bonusColors = {
'=': 0xcc5555, // Triple word (desaturated red)
'-': 0xff9999, // Double word
'"': 0x5566cc, // Triple letter (desaturated blue)
"'": 0x4eb7e1, // Double letter
'~': 0x22ff22, // Quadruple word
'^': 0x99ff99, // Quadruple letter
'*': 0x000000, // Starting square
' ': 0xffffff // No bonus
};
// Dim a color for heatmap mode - desaturate and lighten
function dimColor(hexColor) {
const color = new THREE.Color(hexColor);
const hsl = {};
color.getHSL(hsl);
// Reduce saturation significantly and increase lightness
hsl.s = hsl.s * 0.25; // Reduce saturation to 25%
hsl.l = hsl.l * 0.7 + 0.3; // Move lightness toward white
color.setHSL(hsl.h, hsl.s, hsl.l);
return color.getHex();
}
const bonusLabels = {
'=': '3W', // Triple word
'-': '2W', // Double word
'"': '3L', // Triple letter
"'": '2L', // Double letter
'~': '4W', // Quadruple word
'^': '4L', // Quadruple letter
'*': '', // Starting square
' ': '' // No bonus
};
// Letter scores from the current alphabet (passed from Go)
const letterScores = {{.AlphabetScores}};
function getLetterScore(letter) {
const upper = letter.toUpperCase();
// lowercase in FEN means blank
if (letter === letter.toLowerCase() && letter !== letter.toUpperCase()) {
return 0;
}
// Strip brackets for multi-character tiles like [CH], [LL], [RR]
const cleanLetter = upper.replace(/[\[\]]/g, '');
return letterScores[cleanLetter] || 0;
}
// Parse a string that may contain bracket-enclosed digraphs
// Returns an array of tiles (e.g., "AB[CH]D" -> ["A", "B", "CH", "D"])
function parseRackString(str) {
const tiles = [];
let i = 0;
while (i < str.length) {
const char = str[i];
if (char === '[') {
// Multi-character tile (digraph) like [CH], [LL], [RR]
// Collect everything until ']'
let multiTile = '';
i++; // skip the '['
while (i < str.length && str[i] !== ']') {
multiTile += str[i];
i++;
}
i++; // skip the ']'
tiles.push(multiTile);
} else {
// Single character tile
tiles.push(char);
i++;
}
}
return tiles;
}
function parseFEN(fen) {
const rows = fen.split("/");
const board = [];
for (const row of rows) {
const boardRow = [];
let i = 0;
while (i < row.length) {
const char = row[i];
if (char === '[') {
// Multi-character tile (digraph) like [CH], [LL], [RR]
// Collect everything until ']'
let multiTile = '';
i++; // skip the '['
while (i < row.length && row[i] !== ']') {
multiTile += row[i];
i++;
}
i++; // skip the ']'
boardRow.push(multiTile);
} else if (!isNaN(parseInt(char))) {
// Number indicating empty spaces
let num = "";
while (i < row.length && !isNaN(parseInt(row[i]))) {
num += row[i];
i++;
}
const emptySpaces = parseInt(num);
for (let j = 0; j < emptySpaces; j++) {
boardRow.push("");
}
} else {
// Single character tile
boardRow.push(char);
i++;
}
}
board.push(boardRow);
}
return board;
}
function createTile(letter, score, font) {
const group = new THREE.Group();
// Tile shape (rounded rectangle)
const width = squareSize - 0.75;
const height = squareSize - 0.25;
const radius = 0.5;
const shape = new THREE.Shape();
shape.moveTo(radius, 0);
shape.lineTo(width - radius, 0);
shape.quadraticCurveTo(width, 0, width, radius);
shape.lineTo(width, height - radius);
shape.quadraticCurveTo(width, height, width - radius, height);
shape.lineTo(radius, height);
shape.quadraticCurveTo(0, height, 0, height - radius);
shape.lineTo(0, radius);
shape.quadraticCurveTo(0, 0, radius, 0);
const extrudeSettings = {
steps: 1,
depth: tileDepth,
bevelEnabled: false
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
const material = new THREE.MeshStandardMaterial({
color: tileColorConfig.hex,
roughness: 0.7,
metalness: 0.1,
envMapIntensity: 1.5
});
const tile = new THREE.Mesh(geometry, material);
tile.castShadow = true;
tile.receiveShadow = true;
group.add(tile);
// Letter text - strip brackets if present (e.g., "[CH]" -> "CH")
const displayLetter = letter.toUpperCase().replace(/[\[\]]/g, '');
// Adjust font size for multi-character tiles (digraphs like CH, LL, RR)
let fontSize;
if (displayLetter.length === 1) {
fontSize = Math.min(2.6, width * 0.6);
} else if (displayLetter.length === 2) {
fontSize = Math.min(2.0, width * 0.45); // Smaller for digraphs
} else {
fontSize = Math.min(1.6, width * 0.35); // Even smaller for longer
}
const letterGeometry = new TextGeometry(displayLetter, {
font: font,
size: fontSize,
height: 0.1, // Some versions use 'height' instead of 'depth'
curveSegments: 12,
bevelEnabled: false
});
// Check if this is a blank tile (lowercase in original letter)
const isBlank = letter === letter.toLowerCase() && letter !== letter.toUpperCase();
// For blanks, use red text, but use blue if tile is red or pink for contrast
let textColor = tileColorConfig.textColor;
if (isBlank) {
if (TILE_COLOR === 'red' || TILE_COLOR === 'pink') {
textColor = 0x0000ff; // Blue for contrast on red/pink tiles
} else {
textColor = 0xff0000; // Red for blanks
}
}
const letterMaterial = new THREE.MeshBasicMaterial({ color: textColor });
const letterMesh = new THREE.Mesh(letterGeometry, letterMaterial);
letterMesh.castShadow = true;
// Adjust horizontal positioning based on letter width
let xOffset;
if (displayLetter.length >= 2) {
// Multi-character tiles need less offset (more centered)
xOffset = 0.05;
} else if (displayLetter === 'I') {
xOffset = 0.32;
} else if (displayLetter === 'J') {
xOffset = 0.25;
} else if (displayLetter === 'W' || displayLetter === 'M') {
xOffset = 0.1;
} else {
xOffset = 0.15;
}
letterMesh.position.set(xOffset * width, 0.2 * height, tileDepth);
group.add(letterMesh);
// Score text
if (score > 0) {
const scoreGeometry = new TextGeometry(score.toString(), {
font: font,
size: squareSize * 0.2,
height: 0.1 // Some versions use 'height' instead of 'depth'
});
const scoreMesh = new THREE.Mesh(scoreGeometry, letterMaterial);
scoreMesh.castShadow = true;
const scoreXOffset = score >= 10 ? 0.62 : 0.75;
scoreMesh.position.set(scoreXOffset * width, 0.1 * height, tileDepth);
group.add(scoreMesh);
}
return group;
}
// Rack geometry parameters
function rackGeomParams(rackHeight, rackDepth) {
const height1 = rackHeight * 0.4;
const height2 = rackHeight * 0.3;
const depth1 = 0.16 * rackDepth;
const depth2 = 0.4 * rackDepth;
const depth3 = 0.8 * rackDepth;
const radius1 = 0.015 * rackDepth;
const radius2 = 0.16 * rackDepth;
const slope = (rackHeight - radius1 - height2) / (depth2 + radius1 - depth3);
return { height1, height2, depth1, depth2, depth3, radius1, radius2, slope };
}
// Create the 3D rack
function createRack(scene, x, y, z) {
const shape = new THREE.Shape();
const { height1, height2, depth1, depth2, depth3, radius1, radius2 } =
rackGeomParams(rackHeight, rackDepth);
shape.moveTo(radius1, 0);
shape.lineTo(rackDepth - radius1, 0);
shape.quadraticCurveTo(rackDepth, 0, rackDepth, radius1);
shape.lineTo(rackDepth, height2);
const controlPointX = (rackDepth + depth3) / 2;
const controlPointY = height2 + radius2;
shape.bezierCurveTo(
controlPointX,
controlPointY,
controlPointX,
height2,
depth3,
height2
);
shape.lineTo(depth2 + radius1, rackHeight - radius1);
shape.quadraticCurveTo(depth2, rackHeight, depth2 - radius1, rackHeight);
shape.lineTo(depth1 + radius1, rackHeight);
shape.quadraticCurveTo(
depth1,
rackHeight,
depth1 - radius1,
rackHeight - radius1
);
shape.lineTo(0, height1);
shape.lineTo(0, radius1);
shape.quadraticCurveTo(0, 0, radius1, 0);
const extrudeSettings = {
steps: 1,
depth: rackWidth,
bevelEnabled: false,
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
const material = new THREE.MeshStandardMaterial({
color: 0xc8a850,
roughness: 0.4,
metalness: 0.2,
envMapIntensity: 1.0
});
const rack = new THREE.Mesh(geometry, material);
rack.position.set(x, y, z);
rack.rotation.set(Math.PI / 2, (3 * Math.PI) / 2, 0);
rack.receiveShadow = true;
rack.castShadow = true;
scene.add(rack);
}
// Create a tile on the rack
function createRackTile(scene, font, pos, letter, score, rackSlope) {
const xpos = -rackWidth / 2 + 2 * squareSize + pos * (squareSize - 0.6);
const ypos = rackYPos - squareSize - 0.9;
const zpos = 1.8;
const rotation = -Math.atan(rackSlope);
const tile = createTile(letter, score, font);
tile.position.set(xpos, ypos, zpos);
tile.rotation.x = rotation;
scene.add(tile);
}
// Create a chair
function createChair(scene, x, y, z) {
const chairMaterial = new THREE.MeshStandardMaterial({
color: 0x654321,
roughness: 0.6,
metalness: 0.1
});
// Seat
const seat = new THREE.Mesh(
new THREE.BoxGeometry(35, 35, 4),
chairMaterial
);
seat.position.set(x, y, z);
seat.castShadow = true;
seat.receiveShadow = true;
scene.add(seat);
// Backrest
const backrest = new THREE.Mesh(
new THREE.BoxGeometry(35, 4, 40),
chairMaterial
);
backrest.position.set(x, y + 20, z + 22);
backrest.castShadow = true;
scene.add(backrest);
// Chair legs
const legHeight = 36;
const chairLegPositions = [
[x - 14, y - 14, z - legHeight/2],
[x + 14, y - 14, z - legHeight/2],
[x - 14, y + 14, z - legHeight/2],
[x + 14, y + 14, z - legHeight/2]
];
chairLegPositions.forEach(([lx, ly, lz]) => {
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(2, 2, legHeight, 8),
chairMaterial
);
leg.rotation.x = Math.PI / 2;
leg.position.set(lx, ly, lz);
leg.castShadow = true;
scene.add(leg);
});
}
// Create a simple person
function createPerson(scene, x, y, z) {
const skinColor = 0xf0c8a0;
const shirtColor = 0x4a90e2;
const pantsColor = 0x2c3e50;
// Head
const head = new THREE.Mesh(
new THREE.SphereGeometry(7, 16, 16),
new THREE.MeshStandardMaterial({
color: skinColor,
roughness: 0.8,
metalness: 0.1
})
);
head.position.set(x, y, z + 36);
head.castShadow = true;
scene.add(head);
// Torso
const torso = new THREE.Mesh(
new THREE.BoxGeometry(14, 9, 18),
new THREE.MeshStandardMaterial({
color: shirtColor,
roughness: 0.7,
metalness: 0.1
})
);
torso.position.set(x, y, z + 20);
torso.castShadow = true;
scene.add(torso);
// Arms
const armGeometry = new THREE.CylinderGeometry(2.5, 2.5, 16, 8);
const armMaterial = new THREE.MeshStandardMaterial({
color: shirtColor,
roughness: 0.7,
metalness: 0.1
});
// Left arm
const leftArm = new THREE.Mesh(armGeometry, armMaterial);
leftArm.rotation.z = Math.PI / 6;
leftArm.position.set(x - 9, y + 2, z + 18);
leftArm.castShadow = true;
scene.add(leftArm);
// Right arm
const rightArm = new THREE.Mesh(armGeometry, armMaterial);
rightArm.rotation.z = -Math.PI / 6;
rightArm.position.set(x + 9, y + 2, z + 18);
rightArm.castShadow = true;
scene.add(rightArm);
// Hands on table
const handGeometry = new THREE.SphereGeometry(3, 8, 8);
const handMaterial = new THREE.MeshStandardMaterial({
color: skinColor,
roughness: 0.8,
metalness: 0.1
});
const leftHand = new THREE.Mesh(handGeometry, handMaterial);
leftHand.position.set(x - 18, y - 10, z + 4);
leftHand.castShadow = true;
scene.add(leftHand);
const rightHand = new THREE.Mesh(handGeometry, handMaterial);
rightHand.position.set(x + 18, y - 10, z + 4);
rightHand.castShadow = true;
scene.add(rightHand);
// Legs (sitting position)
const legGeometry = new THREE.CylinderGeometry(3.5, 3.5, 18, 8);
const legMaterial = new THREE.MeshStandardMaterial({
color: pantsColor,
roughness: 0.7,
metalness: 0.1
});
const leftLeg = new THREE.Mesh(legGeometry, legMaterial);
leftLeg.rotation.x = Math.PI / 2;
leftLeg.position.set(x - 5, y + 6, z - 4);
leftLeg.castShadow = true;
scene.add(leftLeg);
const rightLeg = new THREE.Mesh(legGeometry, legMaterial);
rightLeg.rotation.x = Math.PI / 2;
rightLeg.position.set(x + 5, y + 6, z - 4);
rightLeg.castShadow = true;
scene.add(rightLeg);
}
// Create unseen tiles in a grid to the right of the board
function createUnseenTiles(scene, font, remainingTiles) {
console.log("Inside createUnseenTiles, remainingTiles:", remainingTiles);
if (!remainingTiles || Object.keys(remainingTiles).length === 0) {
console.log("No remaining tiles to display");
return;
}
// Grid configuration
const tilesPerRow = 5;
const tileSpacing = squareSize + 0.5;
const startX = 55 + squareSize * 0.25; // Start just outside the circular board base (radius 55)
const startY = offset; // Start from top
const tableTopZ = -boardThickness / 2; // Table surface height
let currentIndex = 0;
// Sort tiles: blanks first, then alphabetically
const sortedEntries = Object.entries(remainingTiles).sort((a, b) => {
if (a[0] === '?') return -1;
if (b[0] === '?') return 1;
return a[0].localeCompare(b[0]);
});
console.log("Sorted entries:", sortedEntries);
for (const [letter, count] of sortedEntries) {
const score = getLetterScore(letter);
for (let i = 0; i < count; i++) {
const row = Math.floor(currentIndex / tilesPerRow);
const col = currentIndex % tilesPerRow;
const x = startX + col * tileSpacing;
const y = startY - row * tileSpacing;
const z = tableTopZ; // Bottom of tile sits on table surface
console.log(`Creating unseen tile ${currentIndex}: ${letter} at (${x}, ${y}, ${z})`);
const tile = createTile(letter, score, font);
tile.position.set(x, y, z);
scene.add(tile);
currentIndex++;
}
}
console.log(`Created ${currentIndex} unseen tiles total`);
// Add "Unseen Tiles" label above the tile pool
const labelGeometry = new TextGeometry('Unseen Tiles', {
font: font,
size: 2.5,
height: 0.1,
curveSegments: 8,
bevelEnabled: false
});
const labelMaterial = new THREE.MeshBasicMaterial({ color: 0x333333 });
const labelMesh = new THREE.Mesh(labelGeometry, labelMaterial);
// Center the label above the tile pool
const labelX = startX + (tilesPerRow * tileSpacing) / 2 - 7;
const labelY = startY + squareSize * 1.5;
const labelZ = tableTopZ + 0.1;
labelMesh.position.set(labelX, labelY, labelZ);
scene.add(labelMesh);
}
function createBoard(scene, font, boardArray) {
// Create circular base
const baseGeometry = new THREE.CylinderGeometry(55, 55, boardThickness, 64);
const baseMaterial = new THREE.MeshStandardMaterial({
color: boardColorHex,
roughness: 0.6,
metalness: 0.05,
envMapIntensity: 0.8
});
const base = new THREE.Mesh(baseGeometry, baseMaterial);
base.rotation.x = Math.PI / 2;
base.position.z = 0;
base.receiveShadow = true;
scene.add(base);
const gridBottomZPos = boardThickness / 2;
const wallThickness = squareSize * 0.05;
const wallHeight = squareSize * 0.11;
// Create grid squares
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const bonusType = gridLayout[i][j];
let color = bonusColors[bonusType] || 0xffffff;
// Dim colors in heatmap mode
if (HEATMAP_ACTIVE) {
color = dimColor(color);
}
const squareGeom = new THREE.BoxGeometry(
squareSize,
squareSize,
gridHeight
);
const squareMat = new THREE.MeshStandardMaterial({
color: color,
roughness: 0.8,
metalness: 0.0,
envMapIntensity: 0.5
});
const square = new THREE.Mesh(squareGeom, squareMat);
const x = i * squareSize - offset;
const y = j * squareSize - offset;
const z = gridBottomZPos + gridHeight / 2;
square.position.set(x, y, z);
square.receiveShadow = true;
scene.add(square);
// Add walls around square
const wallMat = new THREE.MeshBasicMaterial({ color: 0x444444 });
// Top wall
const topWall = new THREE.Mesh(
new THREE.BoxGeometry(squareSize, wallThickness, wallHeight),
wallMat
);
topWall.position.set(x, y + squareSize / 2, gridBottomZPos + gridHeight);
scene.add(topWall);
// Bottom wall
const bottomWall = new THREE.Mesh(
new THREE.BoxGeometry(squareSize, wallThickness, wallHeight),
wallMat
);
bottomWall.position.set(x, y - squareSize / 2, gridBottomZPos + gridHeight);
scene.add(bottomWall);
// Left wall
const leftWall = new THREE.Mesh(
new THREE.BoxGeometry(wallThickness, squareSize, wallHeight),
wallMat
);
leftWall.position.set(x - squareSize / 2, y, gridBottomZPos + gridHeight);
scene.add(leftWall);
// Right wall
const rightWall = new THREE.Mesh(
new THREE.BoxGeometry(wallThickness, squareSize, wallHeight),
wallMat
);
rightWall.position.set(x + squareSize / 2, y, gridBottomZPos + gridHeight);
scene.add(rightWall);
// Add bonus labels
if (bonusType !== ' ' && bonusLabels[bonusType]) {
const labelText = bonusLabels[bonusType];
const labelGeom = new TextGeometry(labelText, {
font: font,
size: squareSize * 0.28,
height: 0.02, // Some versions use 'height' instead of 'depth'
curveSegments: 4,
bevelEnabled: false
});
// Center the geometry
labelGeom.computeBoundingBox();
const textWidth = labelGeom.boundingBox.max.x - labelGeom.boundingBox.min.x;
const textHeight = labelGeom.boundingBox.max.y - labelGeom.boundingBox.min.y;
// L labels need a small rightward nudge since L is visually narrower than W
const xNudge = labelText.endsWith('L') ? 0.15 : 0;
const labelMat = new THREE.MeshBasicMaterial({ color: 0xffffff });
const labelMesh = new THREE.Mesh(labelGeom, labelMat);
labelMesh.position.set(
x - textWidth / 2 + xNudge,
y - textHeight / 2,
gridBottomZPos + gridHeight + 0.01
);
scene.add(labelMesh);
}
}
}
// Add column labels (A through the board dimension)
const columns = Array.from({ length: gridSize }, (_, i) => String.fromCharCode(65 + i));
columns.forEach((letter, index) => {
const geometry = new TextGeometry(letter, {
font: font,
size: squareSize * 0.375,
height: 0.05, // Some versions use 'height' instead of 'depth'
curveSegments: 8,
bevelEnabled: false
});
const material = new THREE.MeshBasicMaterial({ color: labelColor });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(
index * squareSize - offset - squareSize / 4,
offset + squareSize * 0.8,
boardThickness / 2 + 0.01
);
scene.add(mesh);
});
// Add row labels (1-15)
for (let i = 0; i < gridSize; i++) {
const number = (i + 1).toString();
const geometry = new TextGeometry(number, {
font: font,
size: squareSize * 0.375,
height: 0.05, // Some versions use 'height' instead of 'depth'
curveSegments: 8,
bevelEnabled: false
});
const material = new THREE.MeshBasicMaterial({ color: labelColor });
const mesh = new THREE.Mesh(geometry, material);
const textWidth = number.length * squareSize * 0.3;
mesh.position.set(
-offset - squareSize * 0.65 - textWidth,
(gridSize - 1 - i) * squareSize - offset - squareSize / 4,
boardThickness / 2 + 0.01
);
scene.add(mesh);
}
// Add tiles
boardArray.forEach((row, y) => {
row.forEach((tile, x) => {
if (tile !== "") {
const score = getLetterScore(tile);
const tileGroup = createTile(tile, score, font);
const posX = x * squareSize - offset - squareSize / 2 + 0.375;
const posY = (gridSize - 1 - y) * squareSize - offset - squareSize / 2 + 0.125;
tileGroup.position.set(posX, posY, boardTileZPos);
scene.add(tileGroup);
}
});
});
// Add table
const tableTop = new THREE.Mesh(
new THREE.BoxGeometry(180, 140, 4),
new THREE.MeshStandardMaterial({
color: 0x8B4513,
roughness: 0.6,
metalness: 0.1,
envMapIntensity: 0.7
})
);
tableTop.position.set(0, 0, -boardThickness / 2 - 2);
tableTop.receiveShadow = true;
tableTop.castShadow = true;
scene.add(tableTop);
// Table legs
const legMaterial = new THREE.MeshStandardMaterial({
color: 0x654321,
roughness: 0.5,
metalness: 0.1
});
const legPositions = [
[-80, -60], [80, -60], [-80, 60], [80, 60]
];
legPositions.forEach(([x, y]) => {
const leg = new THREE.Mesh(
new THREE.BoxGeometry(4, 4, 40),
legMaterial
);
leg.position.set(x, y, -boardThickness / 2 - 22);
leg.castShadow = true;
scene.add(leg);
});
}
// Create heatmap overlays on the board
function createHeatmapOverlays(scene, heatmapData) {
if (!heatmapData || heatmapData === null) {
console.log("No heatmap data available");
return;
}
console.log("Creating heatmap overlays", heatmapData);
const gridBottomZPos = boardThickness / 2;
const boardTileZPos = gridBottomZPos + gridHeight + 0.01;
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const heat = heatmapData[row][col];
if (heat <= 0) continue;
// Create color interpolation from blue (cold) to red (hot)
// Using blue → green → orange → red for better saturation
const color = new THREE.Color();
if (heat < 0.33) {
// Blue to green (0.0 - 0.33)
const t = heat * 3;
color.setRGB(0, t, 1 - t);
} else if (heat < 0.67) {
// Green to orange (0.33 - 0.67)
const t = (heat - 0.33) * 3;
color.setRGB(t, 1, 0);
} else {
// Orange to red (0.67 - 1.0)
const t = (heat - 0.67) * 3;
color.setRGB(1, 1 - t, 0);
}
// Create semi-transparent overlay plane
const overlayGeom = new THREE.PlaneGeometry(squareSize - 0.3, squareSize - 0.3);
const overlayMat = new THREE.MeshBasicMaterial({
color: color,
transparent: true,
opacity: 0.7,
side: THREE.DoubleSide
});
const overlay = new THREE.Mesh(overlayGeom, overlayMat);
// Position overlay on the board square
const x = col * squareSize - offset;
const y = (gridSize - 1 - row) * squareSize - offset;
const z = boardTileZPos + 0.02; // Slightly above tiles
overlay.position.set(x, y, z);
scene.add(overlay);
}
}
}
// Create scorepad with player names and scores
function createScorepad(scene, player0Name, player0Score, player1Name, player1Score, playerOnTurn, lastPlay) {
// Create canvas for text rendering
const canvas = document.createElement('canvas');
canvas.width = 768;
canvas.height = 480;
const ctx = canvas.getContext('2d');
// Draw notepad background
ctx.fillStyle = '#f9f3e8'; // Cream/paper color
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Add notepad lines
ctx.strokeStyle = '#a09080'; // Darker lines
ctx.lineWidth = 3;
for (let i = 0; i < 10; i++) {
const y = 75 + i * 45;
ctx.beginPath();
ctx.moveTo(60, y);
ctx.lineTo(canvas.width - 60, y);
ctx.stroke();
}
// Add red margin line (like a real notepad)
ctx.strokeStyle = '#d32f2f'; // Deeper red
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(60, 45);
ctx.lineTo(60, canvas.height - 45);
ctx.stroke();
// Draw text
ctx.fillStyle = '#000000'; // Pure black
ctx.font = 'bold 56px Arial';
ctx.fillText('Current Scores', 90, 60);
ctx.font = 'bold 46px Arial'; // Even larger
// Player 0
const p0Text = player0Name + ':';
ctx.fillText(p0Text, 90, 128);
if (playerOnTurn === 0) {
ctx.fillStyle = '#DC143C'; // Crimson red
ctx.font = 'bold 42px Arial';
ctx.fillText('★', 630, 128);
ctx.fillStyle = '#000000'; // Back to black
ctx.font = 'bold 46px Arial';
}
ctx.fillText(player0Score.toString(), 525, 128);
// Player 1
const p1Text = player1Name + ':';
ctx.fillText(p1Text, 90, 188);
if (playerOnTurn === 1) {
ctx.fillStyle = '#DC143C'; // Crimson red
ctx.font = 'bold 42px Arial';
ctx.fillText('★', 630, 188);
ctx.fillStyle = '#000000'; // Back to black
ctx.font = 'bold 46px Arial';
}