All files / src/missions custom-imported-element.ts

81.89% Statements 647/790
74.32% Branches 165/222
85.29% Functions 29/34
81.89% Lines 647/790

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 9671x 1x       1x 1x 1x 1x                                     1x 22x 22x 22x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x     22x 22x 22x 22x 22x     22x 22x     22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x   22x 1x 1x 1x 1x 1x 1x 1x 1x   22x 1x 1x   22x                     22x 4x 4x         22x 152x 14x 14x 14x 14x 14x 152x 12x 12x 126x 140x     152x 152x 152x 17x 17x       17x 17x 17x 17x 17x 17x 17x 17x 13x 13x 13x 17x 13x 152x   22x 97x 14x 14x 83x 97x   22x 1x 1x         22x 11x 11x   11x 7x 2x 2x 11x 4x 4x 4x   11x 6x 6x 6x 6x 6x 6x 6x 6x 6x 7x 1x             1x                                               1x 1x 1x 1x     11x               11x                   11x         11x 24x 24x   24x 24x 24x 24x 24x 17x 17x 17x 17x 24x     11x 24x 24x 24x   11x 11x   22x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x   22x 42x 22x 22x 22x   42x 42x 35x 35x 35x 42x   22x       22x       22x       22x       22x 22x 22x 22x   22x 6x 6x 6x 6x 6x 6x 6x   22x 22x 22x 22x   22x 23x 23x 23x 23x     23x 23x 44x 10x 242x 242x 242x 44x 34x 33x 33x 33x 33x 33x 34x 44x 23x 23x     23x 23x 23x 23x 23x 23x 23x 23x 23x   23x   23x   23x 44x 44x 44x 44x 44x         44x 44x 21x 21x 21x 35x 23x 23x 23x 23x 23x 23x 23x   44x 44x     44x 44x 44x     44x 64x   64x     64x 1x 1x 1x 64x   63x 63x 63x 63x 63x             64x 64x 64x   64x 64x 64x 64x 64x 64x     44x 44x 10x 242x 242x 242x 242x 242x 242x     10x 9x 9x   10x 6x 33x 33x 6x     10x 10x 10x 242x 242x 242x 242x 242x 10x 10x 10x 10x 242x 242x 10x 9x 9x 10x 6x 6x 33x 33x 6x 10x 10x 44x   34x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x   33x 17x 17x 33x 34x 44x     23x 23x   23x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x   8x   8x             8x   8x 8x   8x 8x   8x 8x 8x 8x 8x 8x   8x 8x 8x   8x 8x   8x 8x 8x   8x 8x 8x     8x 8x 8x                           8x 8x     8x 8x 8x 8x 8x 8x 8x 23x   15x 9x 9x 9x 9x 9x         9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 15x   23x 23x 23x   22x 56x 56x   15x 15x 15x   15x 15x 15x 15x   15x 15x 15x 15x   15x 15x 15x 15x   15x 15x     15x 15x 15x 15x   15x 15x 15x 15x 15x                     15x 56x   22x 56x 56x 112x 112x 112x 112x 112x 112x 112x 56x   22x 1320x     1320x 2524x 2524x 2524x 2524x 2524x 2524x 2524x 651x 651x 651x 2524x 1320x   22x 33x 33x 68x 68x 68x 68x 68x 68x   68x 33x 33x 33x 68x 33x   22x 33x 33x 33x 33x 33x   33x   33x 68x 68x 68x 68x 68x   33x 33x 33x 33x 33x   22x 2x 2x 1x 1x 2x 2x 2x   22x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   22x 3x 3x   22x 7x 7x         22x 35x 35x   35x 35x 35x 35x   35x 35x 35x 35x   35x 35x 35x   35x   35x 35x 35x       35x 35x         22x 16x 16x       16x   16x 22x 22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x                                                   22x 9x 9x 9x 9x 9x 9x 22x   7x 7x 7x 7x 7x 16x   22x 4x 4x 4x 4x   4x   22x 6x 6x 6x 6x 6x 6x   22x 1x 1x 1x 3x 3x 3x 3x 2x 13x 11x 11x 2x 2x 3x 1x 1x   22x   1x 3x   2x 2x 2x 2x 2x 1x   22x 3x 3x 3x 3x   3x               3x                 3x 1x 1x 1x     1x 3x 3x 3x   3x 5x 5x 3x 3x 3x 22x  
import * as THREE from 'three';
import RAPIER from '@dimforge/rapier3d-compat';
import { MissionElement } from './types';
import { RobotAssemblySpec } from '../cad/types';
import type { FllPhysicsSpec, FllJointDef } from '../cad/physics-spec';
import { LEGO_COLORS, getLegoMaterial } from '../view/lego-visuals';
import { legoAssetManager } from '../cad/lego-asset-manager';
import { DualLockMarker } from './dual-lock-marker';
import { consolidateGroupMeshes } from '../cad/mesh-consolidator';
 
export interface CustomElementOptions {
  id: string;
  name: string;
  description?: string;
  sourceFile?: string;
  isBaseFixed?: boolean;
  isSolidRigidMode?: boolean;
  parentMissionId?: string;
}
 
/**
 * Custom Imported Mission Element
 * 
 * Instantiates any Studio 2.0 (.io) or LDraw (.ldr/.mpd/.dat) assembly as a
 * fully interactive physical mission model with accurate Rapier colliders,
 * joints, and authentic LEGO visual materials.
 */
export class CustomImportedMissionElement implements MissionElement {
  public readonly id: string;
  public readonly name: string;
  public readonly description: string;
  public readonly sourceFile?: string;
  public parentMissionId?: string;
  public isDualLocked: boolean = true;
  public isPlacedOnField: boolean = true;
  public readonly rootGroup: THREE.Group;
 
  private world!: RAPIER.World;
  private spec: RobotAssemblySpec;
  private basePos: { x: number; y: number; z: number } = { x: 0, y: 0.002, z: 0 };
  private yawDegrees: number = 0;
  private isBaseFixed: boolean;
  private isSolidRigidMode: boolean;
  private groundCorrectionY: number = 0;
  private dualLockMarker: DualLockMarker | null = null;
  private dualLockLocalOffset: { x: number; z: number } | null = null;
  private anchoredClusterId: string | null = null;
  private dualLockJoint: RAPIER.ImpulseJoint | null = null;
  private dualLockAnchorBody: RAPIER.RigidBody | null = null;
 
  // Physics Bodies
  private bodies: Map<string, RAPIER.RigidBody> = new Map();
  private initialBodyPoses: Map<string, { translation: { x: number; y: number; z: number }; rotation: { x: number; y: number; z: number; w: number } }> = new Map();
  private joints: RAPIER.ImpulseJoint[] = [];
  private physicsJointMap: Map<string, { joint: RAPIER.ImpulseJoint; def: FllJointDef }> = new Map();
  private jointInitialRelativeRot: Map<string, THREE.Quaternion> = new Map();
 
  // Visual Groups
  public readonly clusterMeshes: Map<string, THREE.Group> = new Map();
  private interactiveMeshes: THREE.Object3D[] = [];
 
  // Scored state
  private hasMovedFromInitial = false;
 
  constructor(spec: RobotAssemblySpec, options: CustomElementOptions) {
    this.spec = spec;
    this.id = options.id;
    this.name = options.name;
    this.description = options.description || `Imported mission model: ${options.name}`;
    this.sourceFile = options.sourceFile;
    this.parentMissionId = options.parentMissionId;
    this.isBaseFixed = options.isBaseFixed ?? true;
    this.isDualLocked = this.isBaseFixed;
    this.isSolidRigidMode = options.isSolidRigidMode ?? false;
    this.rootGroup = new THREE.Group();
  }
 
  public setSolidRigidMode(enabled: boolean): void {
    if (this.isSolidRigidMode === enabled) return;
    this.isSolidRigidMode = enabled;
    if (this.world) {
      this.destroy();
      this.createPhysicsAndVisuals();
      this.reset();
    }
  }
 
  public getSolidRigidMode(): boolean {
    return this.isSolidRigidMode;
  }
 
  public setIsBaseFixed(fixed: boolean): void {
    if (this.isBaseFixed === fixed) return;
    this.isBaseFixed = fixed;
    this.isDualLocked = fixed;
    if (this.world) {
      this.destroy();
      this.createPhysicsAndVisuals();
      this.reset();
    }
  }
 
  public getIsBaseFixed(): boolean {
    return this.isDualLocked;
  }
 
  /**
   * Resolves the primary base cluster ID that rests on the field mat and holds Dual Lock fasteners
   */
  public getBaseClusterId(): string {
    if (this.spec.physicsSpec?.dualLockAnchors && this.spec.physicsSpec.dualLockAnchors.length > 0) {
      const anchorClusterId = this.spec.physicsSpec.dualLockAnchors[0].clusterId;
      if (this.spec.clusters.some((c) => c.clusterId === anchorClusterId)) {
        return anchorClusterId;
      }
    }
    if (this.anchoredClusterId && this.spec.clusters.some((c) => c.clusterId === this.anchoredClusterId)) {
      return this.anchoredClusterId;
    }
    const root = this.spec.clusters.find((c) => c.isRootChassis || c.clusterId === 'chassis_root');
    if (root) return root.clusterId;
 
    // Pick the cluster with the lowest bottom-Y (resting closest to the mat)
    let lowestClusterId = this.spec.clusters[0]?.clusterId || 'base';
    let lowestY = Infinity;
    for (const c of this.spec.clusters) {
      let cMinY = Infinity;
      if (c.parts && c.parts.length > 0) {
        for (const p of c.parts) {
          if (p.position[1] / 1000 < cMinY) cMinY = p.position[1] / 1000;
        }
      } else {
        for (const col of c.colliders) {
          const hy = col.halfExtents ? col.halfExtents[1] : 0.015;
          const bottom = col.offset[1] - hy;
          if (bottom < cMinY) cMinY = bottom;
        }
      }
      if (cMinY < lowestY) {
        lowestY = cMinY;
        lowestClusterId = c.clusterId;
      }
    }
    return lowestClusterId;
  }
 
  public isClusterDualLockAnchored(clusterId: string): boolean {
    if (this.spec.physicsSpec?.dualLockAnchors && this.spec.physicsSpec.dualLockAnchors.length > 0) {
      return this.spec.physicsSpec.dualLockAnchors.some((a) => a.clusterId === clusterId);
    }
    return clusterId === this.getBaseClusterId();
  }
 
  public getAnchoredClusterId(): string | null {
    return this.isDualLocked ? (this.anchoredClusterId || this.getBaseClusterId()) : null;
  }
 
  /**
   * Sets Dual-Lock fastening status (base cluster fixed to field, mechanisms stay dynamic)
   */
  public setDualLocked(locked: boolean, anchorPoint?: { x: number; z: number }, targetClusterId?: string): void {
    this.isDualLocked = locked;
    this.isBaseFixed = locked;
 
    if (locked) {
      if (targetClusterId && this.spec.clusters.some((c) => c.clusterId === targetClusterId)) {
        this.anchoredClusterId = targetClusterId;
      }
    } else {
      this.anchoredClusterId = null;
      this.dualLockLocalOffset = null;
    }
 
    if (anchorPoint && locked) {
      const radYaw = (this.yawDegrees * Math.PI) / 180;
      const cosY = Math.cos(radYaw);
      const sinY = Math.sin(radYaw);
      const dx = anchorPoint.x - this.basePos.x;
      const dz = anchorPoint.z - this.basePos.z;
      this.dualLockLocalOffset = {
        x: dx * cosY - dz * sinY,
        z: dx * sinY + dz * cosY,
      };
    } else if (locked && !this.dualLockLocalOffset) {
      if (this.spec.physicsSpec?.dualLockAnchors?.[0]) {
        const anchor = this.spec.physicsSpec.dualLockAnchors[0];
        const rawX = anchor.localStudCoordinates[0];
        const rawZ = anchor.localStudCoordinates[2];
        const offX = Math.abs(rawX) > 2 ? rawX / 1000 : rawX;
        const offZ = Math.abs(rawZ) > 2 ? rawZ / 1000 : rawZ;
        this.dualLockLocalOffset = { x: offX, z: offZ };
      } else if (targetClusterId) {
        const targetCluster = this.spec.clusters.find((c) => c.clusterId === targetClusterId);
        if (targetCluster) {
          let avgX = 0;
          let avgZ = 0;
          let count = 0;
          if (targetCluster.parts && targetCluster.parts.length > 0) {
            for (const p of targetCluster.parts) {
              avgX += p.position[0] / 1000;
              avgZ += p.position[2] / 1000;
              count++;
            }
          } else if (targetCluster.colliders && targetCluster.colliders.length > 0) {
            for (const col of targetCluster.colliders) {
              avgX += col.offset[0];
              avgZ += col.offset[2];
              count++;
            }
          }
          if (count > 0) {
            this.dualLockLocalOffset = { x: avgX / count, z: avgZ / count };
          }
        }
      }
      if (!this.dualLockLocalOffset) {
        this.dualLockLocalOffset = { x: 0, z: 0 };
      }
    }
 
    // Clean up any existing physical Dual Lock joint
    if (this.dualLockJoint && this.world) {
      try {
        this.world.removeImpulseJoint(this.dualLockJoint, true);
      } catch {
        // ignore
      }
      this.dualLockJoint = null;
    }
    if (this.dualLockAnchorBody && this.world) {
      try {
        this.world.removeRigidBody(this.dualLockAnchorBody);
      } catch {
        // ignore
      }
      this.dualLockAnchorBody = null;
    }
 
    // Determine the base cluster ID (root frame where Dual Lock is attached to the field)
    const baseClusterId = this.getBaseClusterId();
 
    // Update Rapier bodies:
    // Only the base cluster where Dual Lock is applied is fixed to the field mat.
    // All mechanism clusters, levers, dials, and dynamic parts REMAIN DYNAMIC!
    for (const [cid, body] of this.bodies.entries()) {
      const isBase = this.isClusterDualLockAnchored(cid) || cid === baseClusterId;
      const shouldBeFixed = this.isSolidRigidMode || (locked && isBase);
 
      body.setBodyType(
        shouldBeFixed ? RAPIER.RigidBodyType.Fixed : RAPIER.RigidBodyType.Dynamic,
        true
      );
      if (!shouldBeFixed) {
        body.setLinearDamping(1.5);
        body.setAngularDamping(2.0);
        body.wakeUp();
      }
    }
 
    // Keep cluster specs synchronized
    for (const cluster of this.spec.clusters) {
      const isBase = this.isClusterDualLockAnchored(cluster.clusterId) || cluster.clusterId === baseClusterId;
      cluster.isFixed = this.isSolidRigidMode || (locked && isBase);
    }
 
    this.updateDualLockVisualMesh();
  }
 
  public getDualLockPosition(): { x: number; z: number } | null {
    if (!this.isDualLocked) return null;
    const offset = this.dualLockLocalOffset || { x: 0, z: 0 };
    const radYaw = (this.yawDegrees * Math.PI) / 180;
    const cosY = Math.cos(radYaw);
    const sinY = Math.sin(radYaw);
    return {
      x: this.basePos.x + (offset.x * cosY + offset.z * sinY),
      z: this.basePos.z + (-offset.x * sinY + offset.z * cosY),
    };
  }
 
  private updateDualLockVisualMesh(): void {
    if (!this.dualLockMarker) {
      this.dualLockMarker = new DualLockMarker(this.id);
      this.rootGroup.add(this.dualLockMarker.group);
    }
 
    this.dualLockMarker.setVisible(this.isDualLocked);
    if (this.isDualLocked) {
      const pos = this.getDualLockPosition() || this.basePos;
      this.dualLockMarker.setPosition(pos.x, 0.002, pos.z);
    }
  }
 
  public getDualLockMarker(): DualLockMarker | null {
    return this.dualLockMarker;
  }
 
  public setDualLockHoverHighlight(isHovered: boolean, isEraseMode: boolean = false): void {
    this.dualLockMarker?.setHoverHighlight(isHovered, isEraseMode);
  }
 
  public getSpec(): RobotAssemblySpec {
    return this.spec;
  }
 
  public getPhysicsSpec(): FllPhysicsSpec | undefined {
    return this.spec.physicsSpec;
  }
 
  public init(world: RAPIER.World, basePosition: { x: number; y: number; z: number }, yawDegrees = 0): void {
    this.world = world;
    this.basePos = { ...basePosition };
    this.yawDegrees = yawDegrees;
 
    if (!this.dualLockLocalOffset && this.spec.physicsSpec?.dualLockAnchors?.[0]) {
      const firstAnchor = this.spec.physicsSpec.dualLockAnchors[0];
      const rawX = firstAnchor.localStudCoordinates[0];
      const rawZ = firstAnchor.localStudCoordinates[2];
      const offX = Math.abs(rawX) > 2 ? rawX / 1000 : rawX;
      const offZ = Math.abs(rawZ) > 2 ? rawZ / 1000 : rawZ;
      this.dualLockLocalOffset = { x: offX, z: offZ };
    }
 
    this.createPhysicsAndVisuals();
    this.reset();
    this.updateDualLockVisualMesh();
  }
 
  private createPhysicsAndVisuals(): void {
    const yawRad = (this.yawDegrees * Math.PI) / 180;
    const halfYaw = yawRad / 2;
    const qy = Math.sin(halfYaw);
    const qw = Math.cos(halfYaw);
 
    // Calculate vertical normalization offset so lowest part rests exactly at ground level (this.basePos.y)
    let lowestPartY = Infinity;
    for (const cluster of this.spec.clusters) {
      if (cluster.parts && cluster.parts.length > 0) {
        for (const p of cluster.parts) {
          const py = p.position[1] / 1000;
          if (py < lowestPartY) lowestPartY = py;
        }
      } else {
        for (const col of cluster.colliders) {
          const cy = col.offset[1];
          const hy = col.halfExtents ? col.halfExtents[1] : 0.015;
          const bottomY = cy - hy;
          if (bottomY < lowestPartY) lowestPartY = bottomY;
        }
      }
    }
    this.groundCorrectionY = lowestPartY !== Infinity ? -lowestPartY : 0;
    const initialY = this.basePos.y + this.groundCorrectionY;
 
    // Color palette cycling for imported clusters
    const clusterPalette = [
      LEGO_COLORS.DARK_BLUE,
      LEGO_COLORS.RED,
      LEGO_COLORS.YELLOW,
      LEGO_COLORS.DARK_GRAY,
      LEGO_COLORS.LIGHT_GRAY,
      LEGO_COLORS.AZURE,
      LEGO_COLORS.ORANGE,
    ];
 
    let colorIdx = 0;
 
    const baseClusterId = this.getBaseClusterId();
 
    for (const cluster of this.spec.clusters) {
      const isBase = this.isClusterDualLockAnchored(cluster.clusterId) || cluster.clusterId === baseClusterId;
      const isFixedCluster = this.isSolidRigidMode || (this.isDualLocked && isBase);
      cluster.isFixed = isFixedCluster;
      const clusterColor = clusterPalette[colorIdx % clusterPalette.length];
      colorIdx++;
 
      // Create Rapier RigidBody:
      // Fixed if in solid rigid mode, or if tagged fixed (anchored to field mat).
      // Dynamic clusters react to robot pushes, tools, and collisions!
      let bodyDesc: RAPIER.RigidBodyDesc;
      if (this.isSolidRigidMode || isFixedCluster) {
        bodyDesc = RAPIER.RigidBodyDesc.fixed()
          .setTranslation(this.basePos.x, initialY, this.basePos.z)
          .setRotation({ x: 0, y: qy, z: 0, w: qw });
      } else {
        bodyDesc = RAPIER.RigidBodyDesc.dynamic()
          .setTranslation(this.basePos.x, initialY, this.basePos.z)
          .setRotation({ x: 0, y: qy, z: 0, w: qw })
          .setLinearDamping(2.0)
          .setAngularDamping(3.0)
          .setAdditionalMass(Math.max(0.05, cluster.totalMassKg));
      }
 
      const body = this.world.createRigidBody(bodyDesc);
      this.bodies.set(cluster.clusterId, body);
 
      // Create Visual Group for this cluster
      const clusterGroup = new THREE.Group();
      this.rootGroup.add(clusterGroup);
      this.clusterMeshes.set(cluster.clusterId, clusterGroup);
 
      // Create physical colliders in Rapier
      for (const col of cluster.colliders) {
        let colDesc: RAPIER.ColliderDesc;
 
        if (col.shape === 'sphere') {
          const r = col.radius || 0.015;
          colDesc = RAPIER.ColliderDesc.ball(r);
        } else if (col.shape === 'cylinder') {
          const r = col.radius || 0.015;
          const hh = col.halfHeight || 0.02;
          colDesc = RAPIER.ColliderDesc.cylinder(hh, r);
        } else {
          // Default: Box collider
          const hx = col.halfExtents ? col.halfExtents[0] : 0.03;
          const hy = col.halfExtents ? col.halfExtents[1] : 0.015;
          const hz = col.halfExtents ? col.halfExtents[2] : 0.03;
          colDesc = RAPIER.ColliderDesc.cuboid(hx, hy, hz);
        }
 
        // Collision filtering:
        // Group 3 (0x0008): Fixed base clusters
        // Group 4 (0x0010): Dynamic mechanism clusters
        // Both fixed and dynamic clusters collide with everything (filter 0xFFFF).
        // (Jointed pairs already have mutual contact disabled via rapierJoint.setContactsEnabled(false)).
        const membership = isFixedCluster ? 0x0008 : 0x0010;
        const filter = 0xFFFF;
        colDesc.setCollisionGroups((membership << 16) | filter);
 
        colDesc
          .setTranslation(col.offset[0], col.offset[1], col.offset[2])
          .setFriction(col.friction || 0.6)
          .setRestitution(col.restitution || 0.0);
        this.world.createCollider(colDesc, body);
      }
 
      // Render authentic LEGO bricks, plates, beams, pins, axles!
      const hasDetailedParts = cluster.parts && cluster.parts.length > 0;
      if (hasDetailedParts) {
        for (const part of cluster.parts!) {
          const partColor = part.colorHex ?? clusterColor;
          const partMesh = legoAssetManager.loadPartMeshSync(part.partNumber, partColor, part.role);
          partMesh.position.set(part.position[0] / 1000, part.position[1] / 1000, part.position[2] / 1000);
          partMesh.quaternion.set(part.rotation[0], part.rotation[1], part.rotation[2], part.rotation[3]);
          clusterGroup.add(partMesh);
        }
 
        // Consolidate cluster geometries to reduce draw calls from thousands down to 1-3
        if (cluster.parts!.length > 2) {
          consolidateGroupMeshes(clusterGroup, { clusterId: cluster.clusterId });
        }
 
        if (!isFixedCluster) {
          for (const child of clusterGroup.children) {
            this.interactiveMeshes.push(child);
          }
        }
 
        // Asynchronously upgrade to Draco GLB mesh when ready
        if (legoAssetManager.getRenderMode() === 'draco_glb') {
          Promise.all(
            cluster.parts!.map(async (part) => {
              const partColor = part.colorHex ?? clusterColor;
              const loadedMesh = await legoAssetManager.loadPartMesh(part.partNumber, partColor, part.role);
              loadedMesh.position.set(part.position[0] / 1000, part.position[1] / 1000, part.position[2] / 1000);
              loadedMesh.quaternion.set(part.rotation[0], part.rotation[1], part.rotation[2], part.rotation[3]);
              return loadedMesh;
            })
          ).then((upgradedMeshes) => {
            clusterGroup.clear();
            for (const m of upgradedMeshes) {
              clusterGroup.add(m);
            }
            if (cluster.parts!.length > 2) {
              consolidateGroupMeshes(clusterGroup, { clusterId: cluster.clusterId });
            }
            if (!isFixedCluster) {
              this.interactiveMeshes = this.interactiveMeshes.filter((m) => !clusterGroup.children.includes(m));
              for (const child of clusterGroup.children) {
                this.interactiveMeshes.push(child);
              }
            }
          });
        }
      } else {
        // Fallback: render collider bounding box if parts list is empty
        for (const col of cluster.colliders) {
          const hx = col.halfExtents ? col.halfExtents[0] : 0.03;
          const hy = col.halfExtents ? col.halfExtents[1] : 0.015;
          const hz = col.halfExtents ? col.halfExtents[2] : 0.03;
          const meshGeom = new THREE.BoxGeometry(hx * 2, hy * 2, hz * 2);
          const meshMat = getLegoMaterial(clusterColor, 0.35, 0.05);
          const colMesh = new THREE.Mesh(meshGeom, meshMat);
          colMesh.position.set(col.offset[0], col.offset[1], col.offset[2]);
          colMesh.castShadow = true;
          colMesh.receiveShadow = true;
          clusterGroup.add(colMesh);
 
          if (!isFixedCluster) {
            this.interactiveMeshes.push(colMesh);
          }
        }
      }
    }
 
    // Connect joints between clusters
    this.physicsJointMap.clear();
    this.jointInitialRelativeRot.clear();
 
    if (this.spec.physicsSpec?.joints && this.spec.physicsSpec.joints.length > 0) {
      for (const jointDef of this.spec.physicsSpec.joints) {
        const parentBody = this.bodies.get(jointDef.parentClusterId);
        const childBody = this.bodies.get(jointDef.childClusterId);
        if (!parentBody || !childBody) continue;
 
        const anchor = {
          x: jointDef.anchor[0],
          y: jointDef.anchor[1],
          z: jointDef.anchor[2],
        };
        const axis = {
          x: jointDef.axis[0],
          y: jointDef.axis[1],
          z: jointDef.axis[2],
        };
 
        let jointData: RAPIER.JointData;
        if (jointDef.type === 'prismatic') {
          jointData = RAPIER.JointData.prismatic(anchor, anchor, axis);
        } else if (jointDef.type === 'spherical') {
          jointData = RAPIER.JointData.spherical(anchor, anchor);
        } else if (jointDef.type === 'fixed') {
          jointData = RAPIER.JointData.fixed(
            anchor,
            { x: 0, y: 0, z: 0, w: 1 },
            anchor,
            { x: 0, y: 0, z: 0, w: 1 }
          );
        } else {
          // Default: revolute
          jointData = RAPIER.JointData.revolute(anchor, anchor, axis);
        }
 
        const rapierJoint = this.world.createImpulseJoint(jointData, parentBody, childBody, true);
        rapierJoint.setContactsEnabled(false);
 
        if (jointDef.type === 'revolute') {
          const revJoint = rapierJoint as RAPIER.RevoluteImpulseJoint;
          if (jointDef.limits) {
            const minRad =
              jointDef.limits.minDegrees !== undefined
                ? (jointDef.limits.minDegrees * Math.PI) / 180
                : -Math.PI;
            const maxRad =
              jointDef.limits.maxDegrees !== undefined
                ? (jointDef.limits.maxDegrees * Math.PI) / 180
                : Math.PI;
            revJoint.setLimits(minRad, maxRad);
          }
 
          const damping =
            jointDef.dynamics?.viscousDamping !== undefined
              ? jointDef.dynamics.viscousDamping
              : 0.6;
          const holdingTorque =
            jointDef.dynamics?.holdingTorqueNm !== undefined
              ? jointDef.dynamics.holdingTorqueNm
              : 0.065;
 
          revJoint.configureMotorVelocity(0.0, damping);
          revJoint.setMotorMaxForce(holdingTorque);
        } else if (jointDef.type === 'prismatic') {
          const prismJoint = rapierJoint as RAPIER.PrismaticImpulseJoint;
          if (
            jointDef.limits?.minMeters !== undefined &&
            jointDef.limits?.maxMeters !== undefined
          ) {
            prismJoint.setLimits(jointDef.limits.minMeters, jointDef.limits.maxMeters);
          }
          const damping = jointDef.dynamics?.viscousDamping ?? 0.6;
          const maxForce = jointDef.dynamics?.holdingTorqueNm ?? 5.0;
          prismJoint.configureMotorVelocity(0.0, damping);
          prismJoint.setMotorMaxForce(maxForce);
        }
 
        this.joints.push(rapierJoint);
        this.physicsJointMap.set(jointDef.id, { joint: rapierJoint, def: jointDef });
 
        // Record initial relative orientation at rest (0-degree reference)
        const pRot = parentBody.rotation();
        const cRot = childBody.rotation();
        const qP = new THREE.Quaternion(pRot.x, pRot.y, pRot.z, pRot.w);
        const qC = new THREE.Quaternion(cRot.x, cRot.y, cRot.z, cRot.w);
        const qRel0 = qP.clone().invert().multiply(qC);
        this.jointInitialRelativeRot.set(jointDef.id, qRel0);
      }
    } else {
      // Connect heuristic joints between clusters
      for (const jointSpec of this.spec.joints) {
        const parentBody = this.bodies.get(jointSpec.parentClusterId);
        const childBody = this.bodies.get(jointSpec.childClusterId);
        if (parentBody && childBody) {
          const jointData =
            jointSpec.type === 'SPHERICAL'
              ? RAPIER.JointData.spherical(
                  { x: jointSpec.anchorParent[0], y: jointSpec.anchorParent[1], z: jointSpec.anchorParent[2] },
                  { x: jointSpec.anchorChild[0], y: jointSpec.anchorChild[1], z: jointSpec.anchorChild[2] }
                )
              : RAPIER.JointData.revolute(
                  { x: jointSpec.anchorParent[0], y: jointSpec.anchorParent[1], z: jointSpec.anchorParent[2] },
                  { x: jointSpec.anchorChild[0], y: jointSpec.anchorChild[1], z: jointSpec.anchorChild[2] },
                  { x: jointSpec.axis[0], y: jointSpec.axis[1], z: jointSpec.axis[2] }
                );
          const rapierJoint = this.world.createImpulseJoint(jointData, parentBody, childBody, true);
          rapierJoint.setContactsEnabled(false);
          if (jointSpec.type === 'REVOLUTE') {
            (rapierJoint as RAPIER.RevoluteImpulseJoint).configureMotorVelocity(0.0, 0.5);
            (rapierJoint as RAPIER.RevoluteImpulseJoint).setMotorMaxForce(0.01);
          }
          this.joints.push(rapierJoint);
        }
      }
    }
 
    this.applyInitialPose();
    this.recordInitialPoses();
  }
 
  private applyInitialPose(): void {
    const initPose = this.spec.physicsSpec?.states?.initialPose;
    if (!initPose || !initPose.jointPositions) return;
 
    for (const [jointId, posValue] of Object.entries(initPose.jointPositions)) {
      const entry = this.physicsJointMap.get(jointId);
      if (!entry) continue;
 
      const { def } = entry;
      const parentBody = this.bodies.get(def.parentClusterId);
      const childBody = this.bodies.get(def.childClusterId);
      if (!parentBody || !childBody) continue;
 
      const pPos = parentBody.translation();
      const pRot = parentBody.rotation();
      const parentP = new THREE.Vector3(pPos.x, pPos.y, pPos.z);
      const parentQ = new THREE.Quaternion(pRot.x, pRot.y, pRot.z, pRot.w);
 
      if (def.type === 'revolute') {
        const anchorLocal = new THREE.Vector3(def.anchor[0], def.anchor[1], def.anchor[2]);
        const axisLocal = new THREE.Vector3(def.axis[0], def.axis[1], def.axis[2]).normalize();
        const angleRad = (posValue * Math.PI) / 180;
 
        const rLocal = new THREE.Quaternion().setFromAxisAngle(axisLocal, angleRad);
        const childQ = parentQ.clone().multiply(rLocal);
 
        // Child position in world: parentP + parentQ * (anchorLocal - rLocal * anchorLocal)
        const rotatedAnchor = anchorLocal.clone().applyQuaternion(rLocal);
        const offsetLocal = anchorLocal.clone().sub(rotatedAnchor);
        const offsetWorld = offsetLocal.clone().applyQuaternion(parentQ);
        const childP = parentP.clone().add(offsetWorld);
 
        childBody.setTranslation({ x: childP.x, y: childP.y, z: childP.z }, true);
        childBody.setRotation({ x: childQ.x, y: childQ.y, z: childQ.z, w: childQ.w }, true);
        childBody.setLinvel({ x: 0, y: 0, z: 0 }, true);
        childBody.setAngvel({ x: 0, y: 0, z: 0 }, true);
      } else if (def.type === 'prismatic') {
        const axisLocal = new THREE.Vector3(def.axis[0], def.axis[1], def.axis[2]).normalize();
        const offsetLocal = axisLocal.clone().multiplyScalar(posValue);
        const offsetWorld = offsetLocal.clone().applyQuaternion(parentQ);
        const childP = parentP.clone().add(offsetWorld);
 
        childBody.setTranslation({ x: childP.x, y: childP.y, z: childP.z }, true);
        childBody.setRotation({ x: parentQ.x, y: parentQ.y, z: parentQ.z, w: parentQ.w }, true);
        childBody.setLinvel({ x: 0, y: 0, z: 0 }, true);
        childBody.setAngvel({ x: 0, y: 0, z: 0 }, true);
      }
    }
  }
 
  private recordInitialPoses(): void {
    this.initialBodyPoses.clear();
    for (const [id, body] of this.bodies.entries()) {
      const trans = body.translation();
      const rot = body.rotation();
      this.initialBodyPoses.set(id, {
        translation: { x: trans.x, y: trans.y, z: trans.z },
        rotation: { x: rot.x, y: rot.y, z: rot.z, w: rot.w },
      });
    }
  }
 
  public update(_dt: number): void {
    if (!this.bodies.size) return;
 
    // Check if any non-fixed cluster has moved from its resting position
    for (const [id, body] of this.bodies.entries()) {
      const initPose = this.initialBodyPoses.get(id);
      if (!initPose) continue;
      const curTrans = body.translation();
      const dx = curTrans.x - initPose.translation.x;
      const dy = curTrans.y - initPose.translation.y;
      const dz = curTrans.z - initPose.translation.z;
      if (dx * dx + dy * dy + dz * dz > 0.001) {
        this.hasMovedFromInitial = true;
        break;
      }
    }
  }
 
  public syncVisuals(): void {
    const baseClusterId = this.getBaseClusterId();
    for (const [id, body] of this.bodies.entries()) {
      const group = this.clusterMeshes.get(id);
      if (!group) continue;
      const t = body.translation();
      const r = body.rotation();
      group.position.set(t.x, t.y, t.z);
      group.quaternion.set(r.x, r.y, r.z, r.w);
 
      if (id === baseClusterId) {
        this.basePos.x = t.x;
        this.basePos.z = t.z;
      }
    }
  }
 
  public reset(): void {
    const yawRad = (this.yawDegrees * Math.PI) / 180;
    const halfYaw = yawRad / 2;
    const qy = Math.sin(halfYaw);
    const qw = Math.cos(halfYaw);
    const zeroVel = { x: 0, y: 0, z: 0 };
 
    const initialY = this.basePos.y + this.groundCorrectionY;
 
    for (const body of this.bodies.values()) {
      body.setTranslation({ x: this.basePos.x, y: initialY, z: this.basePos.z }, true);
      body.setRotation({ x: 0, y: qy, z: 0, w: qw }, true);
      body.setLinvel(zeroVel, true);
      body.setAngvel(zeroVel, true);
    }
 
    this.applyInitialPose();
    this.hasMovedFromInitial = false;
    this.recordInitialPoses();
    this.syncVisuals();
  }
 
  public setPosition(pos: { x: number; y: number; z: number }, yawDegrees?: number): void {
    this.basePos = { ...pos };
    if (yawDegrees !== undefined) {
      this.yawDegrees = yawDegrees;
    }
    this.reset();
    this.updateDualLockVisualMesh();
  }
 
  public setRotation(yawDegrees: number): void {
    if (this.isDualLocked) {
      const fixedPoint = this.getDualLockPosition();
      if (fixedPoint) {
        const offset = this.dualLockLocalOffset || { x: 0, z: 0 };
        const radNew = (yawDegrees * Math.PI) / 180;
        const cosN = Math.cos(radNew);
        const sinN = Math.sin(radNew);
        this.basePos.x = fixedPoint.x - (offset.x * cosN + offset.z * sinN);
        this.basePos.z = fixedPoint.z - (-offset.x * sinN + offset.z * cosN);
      }
    }
    this.yawDegrees = yawDegrees;
    this.reset();
    this.updateDualLockVisualMesh();
  }
 
  public getYawDegrees(): number {
    return this.yawDegrees;
  }
 
  public getPosition(): { x: number; y: number; z: number } {
    return { ...this.basePos };
  }
 
  /**
   * Returns current joint angle in degrees for revolute joints, relative to initial rest orientation
   */
  public getJointAngleDegrees(jointId: string): number | null {
    const entry = this.physicsJointMap.get(jointId);
    if (!entry) return null;
 
    const { def } = entry;
    const parentBody = this.bodies.get(def.parentClusterId);
    const childBody = this.bodies.get(def.childClusterId);
    if (!parentBody || !childBody) return null;
 
    const pRot = parentBody.rotation();
    const cRot = childBody.rotation();
    const qP = new THREE.Quaternion(pRot.x, pRot.y, pRot.z, pRot.w);
    const qC = new THREE.Quaternion(cRot.x, cRot.y, cRot.z, cRot.w);
 
    const qRel = qP.clone().invert().multiply(qC);
    const qRel0 = this.jointInitialRelativeRot.get(jointId) || new THREE.Quaternion();
    const qDisp = qRel0.clone().invert().multiply(qRel);
 
    const axisVec = new THREE.Vector3(def.axis[0], def.axis[1], def.axis[2]).normalize();
 
    let w = qDisp.w;
    let dot = new THREE.Vector3(qDisp.x, qDisp.y, qDisp.z).dot(axisVec);
    if (w < 0) {
      w = -w;
      dot = -dot;
    }
    return (2 * Math.atan2(dot, w) * 180) / Math.PI;
  }
 
  /**
   * Evaluates scored pose definitions defined in physicsSpec.states.scoredPoses
   */
  public evaluateScoreState(): { isScored: boolean; points: number; label: string } | null {
    const scoredPoses = this.spec.physicsSpec?.states?.scoredPoses;
    if (!scoredPoses || scoredPoses.length === 0) {
      return null;
    }
 
    const sortedPoses = [...scoredPoses].sort((a, b) => b.points - a.points);
 
    for (const pose of sortedPoses) {
      const ev = pose.evaluator;
      let isMet = false;
 
      if (ev.type === 'joint_threshold') {
        if (ev.jointId) {
          const currentAngle = this.getJointAngleDegrees(ev.jointId);
          if (currentAngle !== null) {
            const minOk = ev.targetAngleMin === undefined || currentAngle >= ev.targetAngleMin;
            const maxOk = ev.targetAngleMax === undefined || currentAngle <= ev.targetAngleMax;
            isMet = minOk && maxOk;
          }
        }
      } else if (ev.type === 'cluster_zone') {
        if (ev.targetClusterId) {
          const body = this.bodies.get(ev.targetClusterId);
          const initPose = this.initialBodyPoses.get(ev.targetClusterId);
          if (body && initPose) {
            const cur = body.translation();
            const dx = cur.x - initPose.translation.x;
            const dy = cur.y - initPose.translation.y;
            const dz = cur.z - initPose.translation.z;
            isMet = Math.sqrt(dx * dx + dy * dy + dz * dz) > 0.02;
          }
        }
      } else if (ev.type === 'payload_transfer') {
        if (ev.targetClusterId) {
          const body = this.bodies.get(ev.targetClusterId);
          const baseClusterId = this.getBaseClusterId();
          const baseBody = this.bodies.get(baseClusterId);
          if (body && baseBody) {
            const cur = body.translation();
            const bPos = baseBody.translation();
            const dist = Math.hypot(cur.x - bPos.x, cur.z - bPos.z);
            isMet = dist > 0.05;
          }
        }
      }
 
      if (isMet) {
        return {
          isScored: true,
          points: pose.points,
          label: pose.label,
        };
      }
    }
 
    return {
      isScored: false,
      points: 0,
      label: 'Not Scored',
    };
  }
 
  public isSolved(): boolean {
    const scoreState = this.evaluateScoreState();
    if (scoreState !== null) {
      return scoreState.isScored;
    }
    return this.hasMovedFromInitial;
  }
 
  public getScore(): number {
    const scoreState = this.evaluateScoreState();
    if (scoreState !== null) {
      return scoreState.isScored ? scoreState.points : 0;
    }
    return this.hasMovedFromInitial ? 100 : 0;
  }
 
  public getInteractiveMeshes(): THREE.Object3D[] {
    const baseClusterId = this.getBaseClusterId();
    const meshes: THREE.Object3D[] = [];
    for (const [cid, group] of this.clusterMeshes.entries()) {
      const isFixed =
        this.isSolidRigidMode ||
        (this.isDualLocked && (this.isClusterDualLockAnchored(cid) || cid === baseClusterId));
      if (!isFixed) {
        group.traverse((obj) => {
          if ((obj as THREE.Mesh).isMesh) {
            meshes.push(obj);
          }
        });
      }
    }
    return meshes;
  }
 
  public applyUserDrag(groundTarget: THREE.Vector3): void {
    // Apply dynamic impulse only to non-fixed bodies
    for (const body of this.bodies.values()) {
      if (body.bodyType() === RAPIER.RigidBodyType.Fixed) continue;
 
      const curTrans = body.translation();
      const vx = Math.min(2.5, Math.max(-2.5, (groundTarget.x - curTrans.x) * 6.0));
      const vz = Math.min(2.5, Math.max(-2.5, (groundTarget.z - curTrans.z) * 6.0));
      body.setLinvel({ x: vx, y: 0, z: vz }, true);
    }
  }
 
  public destroy(): void {
    if (this.dualLockMarker) {
      this.rootGroup.remove(this.dualLockMarker.group);
      this.dualLockMarker = null;
    }
 
    if (this.dualLockJoint && this.world) {
      try {
        this.world.removeImpulseJoint(this.dualLockJoint, true);
      } catch {
        // ignore
      }
      this.dualLockJoint = null;
    }
    if (this.dualLockAnchorBody && this.world) {
      try {
        this.world.removeRigidBody(this.dualLockAnchorBody);
      } catch {
        // ignore
      }
      this.dualLockAnchorBody = null;
    }
 
    for (const joint of this.joints) {
      try {
        this.world.removeImpulseJoint(joint, true);
      } catch {
        // ignore
      }
    }
    this.joints = [];
    this.physicsJointMap.clear();
    this.jointInitialRelativeRot.clear();
 
    for (const body of this.bodies.values()) {
      this.world.removeRigidBody(body);
    }
    this.bodies.clear();
    this.rootGroup.clear();
  }
}