2 lines
24 KiB
JavaScript
2 lines
24 KiB
JavaScript
var Wt=Object.defineProperty;var dt=F=>{throw TypeError(F)};var Jt=(F,h,p)=>h in F?Wt(F,h,{enumerable:!0,configurable:!0,writable:!0,value:p}):F[h]=p;var C=(F,h,p)=>Jt(F,typeof h!="symbol"?h+"":h,p),ot=(F,h,p)=>h.has(F)||dt("Cannot "+p);var l=(F,h,p)=>(ot(F,h,"read from private field"),p?p.call(F):h.get(F)),I=(F,h,p)=>h.has(F)?dt("Cannot add the same private member more than once"):h instanceof WeakSet?h.add(F):h.set(F,p),it=(F,h,p,P)=>(ot(F,h,"write to private field"),P?P.call(F,p):h.set(F,p),p),ct=(F,h,p)=>(ot(F,h,"access private method"),p);(function(){"use strict";var Q,ut,_,ft,M,O,k,E,N;const F={R:["DRF","ULF","ULB","URF","DRB","DLF","DLB","URB"],L:["URF","ULB","DLB","URB","DRF","ULF","DLF","DRB"],U:["URB","URF","ULF","ULB","DRF","DLF","DLB","DRB"],D:["URF","ULF","ULB","URB","DLF","DLB","DRB","DRF"],F:["ULF","DLF","ULB","URB","URF","DRF","DLB","DRB"],B:["URF","ULF","URB","DRB","DRF","DLF","ULB","DLB"]},h={R:[2,0,0,1,1,0,0,2],L:[0,1,2,0,0,2,1,0],U:[0,0,0,0,0,0,0,0],D:[0,0,0,0,0,0,0,0],F:[1,2,0,0,2,1,0,0],B:[0,0,1,2,0,0,2,1]},p={URF:{URF:{0:[],1:["R","U"],2:["F'","U'"]},ULF:{0:["U"],1:["R","U2"],2:["F'"]},ULB:{0:["U2"],1:["R","U'"],2:["F'","U"]},URB:{0:["U'"],1:["R"],2:["F'","U2"]},DRF:{0:["R2","D'"],1:["R'"],2:["F"]},DLF:{0:["F2"],1:["R'","D'"],2:["F","D'"]},DLB:{0:["R2","D"],1:["R'","D2"],2:["F","D2"]},DRB:{0:["R2"],1:["R'","D"],2:["F","D"]}},ULF:{URF:{0:["U'"],1:["F"],2:["L'","U2"]},ULF:{0:[],1:["F","U"],2:["L'","U'"]},ULB:{0:["U"],1:["F","U2"],2:["L'"]},URB:{0:["U2"],1:["F","U'"],2:["L'","U"]},DRF:{0:["F2"],1:["F'","D"],2:["L","D"]},DLF:{0:["L2","D"],1:["F'"],2:["L"]},DLB:{0:["L2"],1:["F'","D'"],2:["L","D'"]},DRB:{0:["L2","D'"],1:["F'","D2"],2:["L","D2"]}},ULB:{URF:{0:["U2"],1:["L","U'"],2:["B'","U"]},ULF:{0:["U'"],1:["L"],2:["B'","U2"]},ULB:{0:[],1:["L","U"],2:["B'","U'"]},URB:{0:["U"],1:["L","U2"],2:["B'"]},DRF:{0:["L2","D"],1:["L'","D2"],2:["B","D2"]},DLF:{0:["L2"],1:["L'","D"],2:["B","D"]},DLB:{0:["L2","D"],1:["L'"],2:["B"]},DRB:{0:["B2"],1:["L'","D'"],2:["B","D'"]}},URB:{URF:{0:["U"],1:["B","U2"],2:["R'"]},ULF:{0:["U2"],1:["B","U'"],2:["R'","U"]},ULB:{0:["U'"],1:["B"],2:["R'","U2"]},URB:{0:[],1:["B","U"],2:["R'","U'"]},DRF:{0:["R2"],1:["B'","D'"],2:["R","D'"]},DLF:{0:["R2","D'"],1:["B'","D2"],2:["R","D2"]},DLB:{0:["B2"],1:["B'","D"],2:["R","D"]},DRB:{0:["R2","D"],1:["B'"],2:["R"]}},DRF:{URF:{0:["R2","U"],1:["F'"],2:["R"]},ULF:{0:["F2"],1:["F'","U"],2:["R","U"]},ULB:{0:["R2","U'"],1:["F'","U2"],2:["R","U2"]},URB:{0:["R2"],1:["F'","U'"],2:["R","U'"]},DRF:{0:[],1:["F","D"],2:["R'","D'"]},DLF:{0:["D'"],1:["F"],2:["R'","D2"]},DLB:{0:["D2"],1:["F","D'"],2:["R'","D"]},DRB:{0:["D"],1:["F","D2"],2:["R'"]}},DLF:{URF:{0:["F2"],1:["L'","U'"],2:["F","U'"]},ULF:{0:["L2","U'"],1:["L'"],2:["F"]},ULB:{0:["L2"],1:["L'","U"],2:["F","U"]},URB:{0:["L2","U"],1:["L'","U2"],2:["F","U2"]},DRF:{0:["D"],1:["L","D2"],2:["F'"]},DLF:{0:[],1:["L","D"],2:["F'","D'"]},DLB:{0:["D'"],1:["L"],2:["F'","D2"]},DRB:{0:["D2"],1:["L","D'"],2:["F'","D"]}},DLB:{URF:{0:["L2","U'"],1:["B'","U2"],2:["L","U2"]},ULF:{0:["L2"],1:["B'","U'"],2:["L","U'"]},ULB:{0:["L2","U"],1:["B'"],2:["L"]},URB:{0:["B2"],1:["B'","U"],2:["L","U"]},DRF:{0:["D2"],1:["B","D'"],2:["L'","D"]},DLF:{0:["D"],1:["B","D2"],2:["L'"]},DLB:{0:[],1:["B","D"],2:["L'","D'"]},DRB:{0:["D'"],1:["B"],2:["L'","D2"]}},DRB:{URF:{0:["R2"],1:["R'","U"],2:["B","U"]},ULF:{0:["R2","U"],1:["R'","U2"],2:["B","U2"]},ULB:{0:["B2"],1:["R'","U'"],2:["B","U'"]},URB:{0:["R2","U'"],1:["R'"],2:["B"]},DRF:{0:["D'"],1:["R"],2:["B'","D2"]},DLF:{0:["D2"],1:["R","D'"],2:["B'","D"]},DLB:{0:["D"],1:["R","D2"],2:["B'"]},DRB:{0:[],1:["R","D"],2:["B'","D'"]}}},P=[6,8,26,24,0,2,20,18],S=class S{constructor(){I(this,Q);C(this,"permutation",[...S.order]);C(this,"orientation",[0,0,0,0,0,0,0,0])}applyTurn(t){const e=F[t],r=h[t],s=[...this.permutation],o=[...this.orientation];for(let i=0;i<8;i++){const a=e[i],R=S.order.indexOf(a);this.permutation[i]=s[R];const L=(r[i]+o[R])%3;this.orientation[i]=L}}applyState(t,e){for(let r=0;r<8;r++){const s=ct(this,Q,ut).call(this,r),o=P[S.order.indexOf(this.permutation[r])];let i=Ft(o,t);i=ht(o,i,s);const a=P[r];Lt(a,e,i)}}reset(){this.permutation=[...S.order],this.orientation=[0,0,0,0,0,0,0,0]}encode(){let t=0;for(const r of this.permutation)t=t<<3,t+=S.order.indexOf(r);let e=0;for(const r of this.orientation)e=e<<2,e+=r;return[t>>0&255,t>>8&255,t>>16&255,e>>0&255,e>>8&255]}decode(t){let e=t[0]+(t[1]<<8)+(t[2]<<16),r=t[3]+(t[4]<<8);for(let s=7;s>=0;s--){const o=e&7;if(o>=8)return!1;this.permutation[s]=S.order[o],e=e>>3;const i=r&3;if(i>2)return!1;this.orientation[s]=i,r=r>>2}return!0}};Q=new WeakSet,ut=function(t){const e=S.order[t],r=this.permutation[t],s=this.orientation[t];return p[r][e][s]},C(S,"order",["URF","ULF","ULB","URB","DRF","DLF","DLB","DRB"]);let et=S;const pt={R:["UF","UL","UB","FR","DR","FL","BL","UR","DF","DL","DB","BR"],L:["UF","BL","UB","UR","FR","UL","DL","BR","DF","FL","DB","DR"],U:["UR","UF","UL","UB","FR","FL","BL","BR","DF","DL","DB","DR"],D:["UF","UL","UB","UR","FR","FL","BL","BR","DL","DB","DR","DF"],F:["FL","UL","UB","UR","UF","DF","BL","BR","FR","DL","DB","DR"],B:["UF","UL","BR","UR","FR","FL","UB","DB","DF","DL","BL","DR"]},wt={R:[0,0,0,0,0,0,0,0,0,0,0,0],L:[0,0,0,0,0,0,0,0,0,0,0,0],U:[0,0,0,0,0,0,0,0,0,0,0,0],D:[0,0,0,0,0,0,0,0,0,0,0,0],F:[1,0,0,0,1,1,0,0,1,0,0,0],B:[0,0,1,0,0,0,1,1,0,0,1,0]},yt={UF:{UF:{0:[],1:["F","R","U"]},UL:{0:["U"],1:["F'","L'"]},UB:{0:["U2"],1:["F","R","U'"]},UR:{0:["U'"],1:["F","R"]},FR:{0:["U'","R'"],1:["F"]},FL:{0:["U","L"],1:["F'"]},BL:{0:["U","L'"],1:["U2","B"]},BR:{0:["U'","R"],1:["U2","B'"]},DF:{0:["F2"],1:["F","R'","D'"]},DL:{0:["F2","D'"],1:["F'","L"]},DB:{0:["F2","D2"],1:["F","R'","D"]},DR:{0:["F2","D"],1:["F","R'"]}},UL:{UF:{0:["U'"],1:["L","F"]},UL:{0:[],1:["L","F","U"]},UB:{0:["U"],1:["L'","B'"]},UR:{0:["U2"],1:["L","F","U'"]},FR:{0:["U2","R'"],1:["U'","F"]},FL:{0:["L"],1:["U'","F'"]},BL:{0:["L'"],1:["U","B"]},BR:{0:["U2","R"],1:["U","B'"]},DF:{0:["L2","D"],1:["L","F'"]},DL:{0:["L2"],1:["L","F'","D'"]},DB:{0:["L2","D'"],1:["L'","B"]},DR:{0:["L2","D2"],1:["L","F'","D"]}},UB:{UF:{0:["U2"],1:["B'","R'","U"]},UL:{0:["U'"],1:["B","L"]},UB:{0:[],1:["B'","R'","U'"]},UR:{0:["U"],1:["B'","R'"]},FR:{0:["U","R'"],1:["B'","R2"]},FL:{0:["U'","L"],1:["B","L2"]},BL:{0:["U'","L'"],1:["B"]},BR:{0:["U","R"],1:["B'"]},DF:{0:["B2","D2"],1:["B'","R","D'"]},DL:{0:["B2","D"],1:["B","L'"]},DB:{0:["B2"],1:["B'","R","D"]},DR:{0:["B2","D'"],1:["B'","R"]}},UR:{UF:{0:["U"],1:["R'","F'"]},UL:{0:["U2"],1:["R'","F'","U"]},UB:{0:["U'"],1:["R","B"]},UR:{0:[],1:["R'","F'","U'"]},FR:{0:["R'"],1:["U","F"]},FL:{0:["U2","L"],1:["U","F'"]},BL:{0:["U2","L'"],1:["U'","B"]},BR:{0:["R"],1:["U'","B'"]},DF:{0:["R2","D'"],1:["R'","F"]},DL:{0:["R2","D2"],1:["R'","F","D'"]},DB:{0:["R2","D"],1:["R","B'"]},DR:{0:["R2"],1:["R'","F","D"]}},FR:{UF:{0:["R","U"],1:["F'"]},UL:{0:["R","U2"],1:["F'","U"]},UB:{0:["R","U'"],1:["F'","U2"]},UR:{0:["R"],1:["F'","U'"]},FR:{0:[],1:["F'","U'","R'"]},FL:{0:["F2"],1:["F'","U","L"]},BL:{0:["F2","L2"],1:["F'","U","L'"]},BR:{0:["R2"],1:["F'","U'","R"]},DF:{0:["R'","D'"],1:["F"]},DL:{0:["R'","D2"],1:["F","D'"]},DB:{0:["R'","D"],1:["F","D2"]},DR:{0:["R'"],1:["F","D"]}},FL:{UF:{0:["L'","U'"],1:["F"]},UL:{0:["L'"],1:["F","U"]},UB:{0:["L'","U"],1:["F","U2"]},UR:{0:["L'","U2"],1:["F","U'"]},FR:{0:["F2"],1:["F","U'","R'"]},FL:{0:[],1:["F","U","L"]},BL:{0:["L2"],1:["F","U","L'"]},BR:{0:["F2","R2"],1:["F","U'","R"]},DF:{0:["L","D"],1:["F'"]},DL:{0:["L"],1:["F'","D'"]},DB:{0:["L","D'"],1:["F'","D2"]},DR:{0:["L","D2"],1:["F'","D"]}},BL:{UF:{0:["L","U'"],1:["B'","U2"]},UL:{0:["L"],1:["B'","U'"]},UB:{0:["L","U"],1:["B'"]},UR:{0:["L","U2"],1:["B'","U"]},FR:{0:["B2","R2"],1:["B'","U","R'"]},FL:{0:["L2"],1:["B'","U'","L"]},BL:{0:[],1:["B'","U'","L'"]},BR:{0:["B2"],1:["B'","U","R"]},DF:{0:["L'","D"],1:["B","D2"]},DL:{0:["L'"],1:["B","D"]},DB:{0:["L'","D'"],1:["B"]},DR:{0:["L'","D2"],1:["B","D'"]}},BR:{UF:{0:["R'","U"],1:["B","U2"]},UL:{0:["R'","U2"],1:["B","U'"]},UB:{0:["R'","U'"],1:["B"]},UR:{0:["R'"],1:["B","U"]},FR:{0:["R2"],1:["B","U","R'"]},FL:{0:["B2","L2"],1:["B","U'","L"]},BL:{0:["B2"],1:["B","U'","L'"]},BR:{0:[],1:["B","U","R"]},DF:{0:["R","D'"],1:["B'","D2"]},DL:{0:["R","D2"],1:["B'","D"]},DB:{0:["R","D"],1:["B'"]},DR:{0:["R"],1:["B'","D'"]}},DF:{UF:{0:["F2"],1:["F'","R","U"]},UL:{0:["F2","U"],1:["F","L'"]},UB:{0:["F2","U2"],1:["F'","R","U'"]},UR:{0:["F2","U'"],1:["F'","R"]},FR:{0:["D","R"],1:["F'"]},FL:{0:["D'","L'"],1:["F"]},BL:{0:["D'","L"],1:["D2","B'"]},BR:{0:["D","R'"],1:["D2","B"]},DF:{0:[],1:["F'","R'","D'"]},DL:{0:["D'"],1:["F","L"]},DB:{0:["D2"],1:["F'","R'","D"]},DR:{0:["D"],1:["F'","R'"]}},DL:{UF:{0:["L2","U'"],1:["L'","F"]},UL:{0:["L2"],1:["L'","F","U"]},UB:{0:["L2","U"],1:["L","B'"]},UR:{0:["L2","U2"],1:["L'","F","U'"]},FR:{0:["D2","R"],1:["D","F'"]},FL:{0:["L'"],1:["D","F"]},BL:{0:["L"],1:["D'","B'"]},BR:{0:["D2","R'"],1:["D'","B"]},DF:{0:["D"],1:["L'","F'"]},DL:{0:[],1:["L'","F'","D'"]},DB:{0:["D'"],1:["L","B"]},DR:{0:["D2"],1:["L'","F'","D"]}},DB:{UF:{0:["B2","U2"],1:["B","R'","U"]},UL:{0:["B2","U'"],1:["B'","L"]},UB:{0:["B2"],1:["B","R'","U'"]},UR:{0:["B2","U"],1:["B","R'"]},FR:{0:["D'","R"],1:["D2","F'"]},FL:{0:["D","L'"],1:["D2","F"]},BL:{0:["D","L"],1:["B'"]},BR:{0:["D'","R'"],1:["B"]},DF:{0:["D2"],1:["B","R","D'"]},DL:{0:["D"],1:["B'","L'"]},DB:{0:[],1:["B","R","D"]},DR:{0:["D'"],1:["B","R"]}},DR:{UF:{0:["R2","U"],1:["R","F'"]},UL:{0:["R2","U2"],1:["R","F'","U"]},UB:{0:["R2","U'"],1:["R'","B"]},UR:{0:["R2"],1:["R","F'","U'"]},FR:{0:["R"],1:["D'","F'"]},FL:{0:["D2","L'"],1:["D'","F"]},BL:{0:["D2","L"],1:["D","B'"]},BR:{0:["R'"],1:["D","B"]},DF:{0:["D'"],1:["R","F"]},DL:{0:["D2"],1:["R","F","D'"]},DB:{0:["D"],1:["R'","B'"]},DR:{0:[],1:["R","F","D"]}}},at=[7,17,25,15,3,5,23,21,1,11,19,9],A=class A{constructor(){I(this,_);C(this,"permutation",[...A.order]);C(this,"orientation",[0,0,0,0,0,0,0,0,0,0,0,0])}applyTurn(t){const e=pt[t],r=wt[t],s=[...this.permutation],o=[...this.orientation];for(let i=0;i<12;i++){const a=e[i],R=A.order.indexOf(a);this.permutation[i]=s[R];const L=(r[i]+o[R])%2;this.orientation[i]=L}}applyState(t,e){for(let r=0;r<12;r++){const s=ct(this,_,ft).call(this,r),o=at[A.order.indexOf(this.permutation[r])];let i=Ft(o,t);i=ht(o,i,s);const a=at[r];Lt(a,e,i)}}reset(){this.permutation=[...A.order],this.orientation=[0,0,0,0,0,0,0,0,0,0,0,0]}encode(){let t=[];for(let r=0;r<6;r++){const s=A.order.indexOf(this.permutation[r*2]),o=A.order.indexOf(this.permutation[r*2+1]);t.push((o<<4)+s)}let e=0;for(const r of this.orientation)e=e<<1,e+=r;return[...t,e>>0&255,e>>8&255]}decode(t){for(let r=0;r<6;r++){const s=t[r]&15,o=t[r]>>4&15;if(s>=12||o>=12)return!1;this.permutation[r*2]=A.order[s],this.permutation[r*2+1]=A.order[o]}let e=t[6]+(t[7]<<8);for(let r=11;r>=0;r--)this.orientation[r]=e&1,e=e>>1;return!0}};_=new WeakSet,ft=function(t){const e=A.order[t],r=this.permutation[t],s=this.orientation[t];return yt[r][e][s]},C(A,"order",["UF","UL","UB","UR","FR","FL","BL","BR","DF","DL","DB","DR"]);let rt=A;const V=(n,t)=>(n%t+t)%t,gt=[12,14,10,16,4,22],tt=class tt{constructor(){C(this,"orientation",[0,0,0,0,0,0])}applyTurn(t){const e=tt.order.indexOf(t);this.orientation[e]=V(this.orientation[e]+1,4)}applyState(t,e){for(let r=0;r<6;r++){const s=t[r][4],o=this.orientation[r]*2,i=gt[r],a=e.cubies[i].getFaceletOfSide(r);a.uvs=[];for(let R=0;R<8;R++)a.uvs[R]=s[V(o+R,8)]}}reset(){this.orientation=[0,0,0,0,0,0]}encode(){let t=0;for(const e of this.orientation)t=t<<2,t+=e;return[t>>0&255,t>>8&255]}decode(t){let e=t[0]+(t[1]<<8);for(let r=5;r>=0;r--)this.orientation[r]=e&3,e=e>>2}};C(tt,"order",["R","L","D","U","F","B"]);let st=tt;const mt={R:[0,0,!1],L:[0,2,!1],D:[1,0,!0],U:[1,2,!0],F:[2,0,!1],B:[2,2,!1]},xt=n=>{const t=n[0],[e,r,s]=mt[t];if(n.endsWith("2"))return{axis:e,index:r,angle:2};let o=n.endsWith("'");s&&(o=!o);let i=3;return(r===0&&!o||r===2&&o)&&(i=1),{axis:e,index:r,angle:i}},bt=n=>{const t=n[0];return n.endsWith("2")?[t,t]:n.endsWith("'")?[t,t,t]:[t]},d={identity:n=>[...n],flipH:n=>[n[6],n[7],n[4],n[5],n[2],n[3],n[0],n[1]],flipV:n=>[n[2],n[3],n[0],n[1],n[6],n[7],n[4],n[5]],rotateCC:n=>[n[6],n[7],n[0],n[1],n[2],n[3],n[4],n[5]],rotateC:n=>[n[2],n[3],n[4],n[5],n[6],n[7],n[0],n[1]],rotate2:n=>[n[4],n[5],n[6],n[7],n[0],n[1],n[2],n[3]],rotateCFlipH:n=>[n[0],n[1],n[6],n[7],n[4],n[5],n[2],n[3]]},w={flipV12:{1:d.flipV,2:d.flipV,3:d.identity},flipV23:{1:d.identity,2:d.flipV,3:d.flipV},flipH12:{1:d.flipH,2:d.flipH,3:d.identity},flipH23:{1:d.identity,2:d.flipH,3:d.flipH},rcFvRcfh:{1:d.rotateC,2:d.flipV,3:d.rotateCFlipH},rcfhFhRcc:{1:d.rotateCFlipH,2:d.flipH,3:d.rotateCC},rcR2Rcc:{1:d.rotateC,2:d.rotate2,3:d.rotateCC}},Ut={0:{0:[0,3,6,15,24,21,18,9],2:[2,5,8,17,26,23,20,11]},1:{0:[0,1,2,11,20,19,18,9],2:[6,7,8,17,26,25,24,15]},2:{0:[0,1,2,5,8,7,6,3],2:[18,19,20,23,26,25,24,21]}},Rt=[[2,5,3,4],[0,5,1,4],[0,3,1,2]];class nt{scale(t){throw new Error("must be implemented in subclass")}add(t){throw new Error("must be implemented in subclass")}sub(t){throw new Error("must be implemented in subclass")}mult(t){throw new Error("must be implemented in subclass")}dot(t){throw new Error("must be implemented in subclass")}toArray(){throw new Error("must be implemented in subclass")}get squareMag(){const t=this;return this.dot(t)}get mag(){return Math.sqrt(this.squareMag)}get normalized(){return this.scale(1/this.mag)}get negate(){return this.scale(-1)}}class H extends nt{constructor(t,e){super(),this.x=t,this.y=e}scale(t){return new H(t*this.x,t*this.y)}add({x:t,y:e}){return new H(this.x+t,this.y+e)}sub({x:t,y:e}){return new H(this.x-t,this.y-e)}mult({x:t,y:e}){return new H(this.x*t,this.y*e)}dot({x:t,y:e}){return this.x*t+this.y*e}toArray(){return[this.x,this.y]}setUniform(t,e){t.uniform2f(e,this.x,this.y)}static get zero(){return new H(0,0)}}class D extends nt{constructor(t,e,r){super(),this.x=t,this.y=e,this.z=r}scale(t){return new D(t*this.x,t*this.y,t*this.z)}add({x:t,y:e,z:r}){return new D(this.x+t,this.y+e,this.z+r)}sub({x:t,y:e,z:r}){return new D(this.x-t,this.y-e,this.z-r)}mult({x:t,y:e,z:r}){return new D(this.x*t,this.y*e,this.z*r)}cross({x:t,y:e,z:r}){return new D(this.y*r-this.z*e,this.z*t-this.x*r,this.x*e-this.y*t)}dot({x:t,y:e,z:r}){return this.x*t+this.y*e+this.z*r}toArray(){return[this.x,this.y,this.z]}setUniform(t,e){t.uniform3f(e,this.x,this.y,this.z)}toV2(){return new H(this.x,this.y)}static get zero(){return new D(0,0,0)}static get one(){return new D(1,1,1)}static get up(){return new D(0,1,0)}static get down(){return new D(0,-1,0)}static get left(){return new D(1,0,0)}static get right(){return new D(-1,0,0)}static get forward(){return new D(0,0,1)}static get back(){return new D(0,0,-1)}static lerp(t,e,r){return t.add(e.sub(t).scale(r))}static angle(t,e){return Math.acos(t.dot(e)/Math.sqrt(t.squareMag*e.squareMag))}static getRotationAxis(t){return t===0?D.right:t===1?D.down:D.back}}class U extends nt{constructor(t,e,r,s){super(),this.x=t,this.y=e,this.z=r,this.w=s}scale(t){return new U(t*this.x,t*this.y,t*this.z,t*this.w)}add({x:t,y:e,z:r,w:s}){return new U(this.x+t,this.y+e,this.z+r,this.w+s)}sub({x:t,y:e,z:r,w:s}){return new U(this.x-t,this.y-e,this.z-r,this.w-s)}mult({x:t,y:e,z:r,w:s}){return new U(this.x*t,this.y*e,this.z*r,this.w*s)}dot({x:t,y:e,z:r,w:s}){return this.x*t+this.y*e+this.z*r+this.w*s}toV3(){return new D(this.x,this.y,this.z)}toArray(){return[this.x,this.y,this.z,this.w]}setUniform(t,e){t.uniform4f(e,this.x,this.y,this.z,this.w)}}class T{constructor(t,e,r,s){this.r1=t,this.r2=e,this.r3=r,this.r4=s}scale(t){return new T(this.r1.scale(t),this.r2.scale(t),this.r3.scale(t),this.r4.scale(t))}add({r1:t,r2:e,r3:r,r4:s}){return new T(this.r1.add(t),this.r2.add(e),this.r3.add(r),this.r4.add(s))}sub({r1:t,r2:e,r3:r,r4:s}){return new T(this.r1.sub(t),this.r2.sub(e),this.r3.sub(r),this.r4.sub(s))}mult(t){return"x"in t?new U(this.r1.dot(t),this.r2.dot(t),this.r3.dot(t),this.r4.dot(t)):new T(new U(this.r1.dot(t.c1),this.r1.dot(t.c2),this.r1.dot(t.c3),this.r1.dot(t.c4)),new U(this.r2.dot(t.c1),this.r2.dot(t.c2),this.r2.dot(t.c3),this.r2.dot(t.c4)),new U(this.r3.dot(t.c1),this.r3.dot(t.c2),this.r3.dot(t.c3),this.r3.dot(t.c4)),new U(this.r4.dot(t.c1),this.r4.dot(t.c2),this.r4.dot(t.c3),this.r4.dot(t.c4)))}toArray(){return[...this.r1.toArray(),...this.r2.toArray(),...this.r3.toArray(),...this.r4.toArray()]}static get identity(){return new T(new U(1,0,0,0),new U(0,1,0,0),new U(0,0,1,0),new U(0,0,0,1))}get c1(){return new U(this.r1.x,this.r2.x,this.r3.x,this.r4.x)}get c2(){return new U(this.r1.y,this.r2.y,this.r3.y,this.r4.y)}get c3(){return new U(this.r1.z,this.r2.z,this.r3.z,this.r4.z)}get c4(){return new U(this.r1.w,this.r2.w,this.r3.w,this.r4.w)}setUniform(t,e){const r=new Float32Array(this.toArray());t.uniformMatrix4fv(e,!0,r)}get transpose(){return new T(this.c1,this.c2,this.c3,this.c4)}get inverse(){const[t,e,r,s]=this.r1.toArray(),[o,i,a,R]=this.r2.toArray(),[L,u,y,c]=this.r3.toArray(),[B,g,f,b]=this.r4.toArray(),z=t*i-o*e,x=t*a-o*r,$=t*R-o*s,q=e*a-i*r,W=e*R-i*s,J=r*R-a*s,j=y*b-f*c,G=u*b-g*c,Y=u*f-g*y,K=L*b-B*c,X=L*f-B*y,Z=L*g-B*u,qt=1/(z*j-x*G+$*Y+q*K-W*X+J*Z);return new T(new U(i*j-a*G+R*Y,-e*j+r*G-s*Y,g*J-f*W+b*q,-u*J+y*W-c*q),new U(-o*j+a*K-R*X,t*j-r*K+s*X,-B*J+f*$-b*x,L*J-y*$+c*x),new U(o*G-i*K+R*Z,-t*G+e*K-s*Z,B*W-g*$+b*z,-L*W+u*$-c*z),new U(-o*Y+i*X-a*Z,t*Y-e*X+r*Z,-B*q+g*x-f*z,L*q-u*x+y*z)).scale(qt)}static perspective(t,e,r,s){const o=Math.tan(t/2),i=1/(e*o),a=1/o,R=s+r,L=s-r,u=.5/r,y=.5/s,c=-R/L,B=1/(y-u);return new T(new U(i,0,0,0),new U(0,a,0,0),new U(0,0,c,B),new U(0,0,-1,0))}static lookAt(t,e,r){const s=e.sub(t).normalized,o=s.cross(r).normalized,i=o.cross(s),a=-o.dot(t),R=-i.dot(t),L=s.dot(t);return new T(new U(o.x,o.y,o.z,a),new U(i.x,i.y,i.z,R),new U(-s.x,-s.y,-s.z,L),new U(0,0,0,1))}}class m{constructor(t,e){this.real=t,this.im=e}static fromAngle(t,e,r=!0){r&&(e*=Math.PI/180);const s=e/2,o=Math.cos(s),i=t.normalized.scale(Math.sin(s));return new m(o,i)}get matrix(){const{x:t,y:e,z:r}=this.im,s=this.real,o=t*t,i=e*e,a=r*r,R=t*e,L=t*r,u=t*s,y=e*r,c=e*s,B=r*s;return new T(new U(1-2*(i+a),2*(R-B),2*(L+c),0),new U(2*(R+B),1-2*(o+a),2*(y-u),0),new U(2*(L-c),2*(y+u),1-2*(o+i),0),new U(0,0,0,1))}mult({real:t,im:e}){return new m(this.real*t-this.im.dot(e),this.im.cross(e).add(e.scale(this.real)).add(this.im.scale(t)))}rotate(t){return new m(this.real,this.im.negate).mult(new m(0,t)).mult(this).im}get conjugate(){return new m(this.real,this.im.negate)}get mag(){return Math.sqrt(this.real*this.real+this.im.squareMag)}power(t){const{mag:e}=this,r=Math.acos(this.real/e),s=this.im.normalized,o=Math.pow(e,t);return new m(o*Math.cos(r*t),s.scale(o*Math.sin(r*t)))}static get identity(){return new m(1,D.zero)}static slerp(t,e,r){return t.mult(t.conjugate.mult(e).power(r))}}[D.right,D.left,D.down,D.up,D.back,D.forward].map(n=>n.scale(.5)),m.identity,m.identity,m.fromAngle(D.back,90),m.fromAngle(D.back,90),m.fromAngle(D.back,90).mult(m.fromAngle(D.down,90)),m.fromAngle(D.back,90).mult(m.fromAngle(D.down,90));const zt=(n,t)=>{const e=Math.floor(n/2),r=n%2,s=Math.floor(t/Math.pow(3,e))%3;return s===1||s===0&&r===1||s===2&&r===0},At=n=>{const t=Math.floor(n/1)%3,e=Math.floor(n/3)%3,r=Math.floor(n/9)%3;return[t,e,r]},Tt=(n,t,e)=>{const r=Math.floor(t/2),s=[];for(let i=0;i<3;i++)i!==r&&s.push(n[i]);const o=s[0]+s[1]*3;return e[t][o]};class Dt{constructor(t){I(this,M,new et);I(this,O,new rt);I(this,k,new st);I(this,E);C(this,"stateInfo",new St(this));it(this,E,t)}applyTurn(t){const e=bt(t);for(const r of e)l(this,M).applyTurn(r),l(this,O).applyTurn(r),l(this,E)&&l(this,k).applyTurn(r)}applyState(t,e){l(this,M).applyState(t,e),l(this,O).applyState(t,e),l(this,E)&&l(this,k).applyState(t,e)}encode(){const t=[];t.push(...l(this,M).encode()),t.push(...l(this,O).encode()),l(this,E)&&t.push(...l(this,k).encode());const e=new Uint8Array(t);return btoa(String.fromCharCode(...e))}decode(t){const e=atob(t);if(e.length!==(l(this,E)?15:13))return!1;const r=[];for(let u=0;u<e.length;u++)r.push(e.charCodeAt(u));const s=l(this,M),o=[...s.permutation],i=[...s.orientation];if(!s.decode(r.slice(0,5)))return s.permutation=o,s.orientation=i,!1;const a=l(this,O),R=[...a.permutation],L=[...a.orientation];return a.decode(r.slice(5,13))?(l(this,E)&&l(this,k).decode(r.slice(13)),!0):(s.permutation=o,s.orientation=i,a.permutation=R,a.orientation=L,!1)}reset(){l(this,M).reset(),l(this,O).reset(),l(this,k).reset()}}M=new WeakMap,O=new WeakMap,k=new WeakMap,E=new WeakMap;class St{constructor(t){I(this,N);it(this,N,t)}toString(){return l(this,N).encode()}}N=new WeakMap;const Ct={0:{0:w.rcR2Rcc,1:w.rcR2Rcc,2:w.flipV12,3:w.flipV12,4:w.flipV23,5:w.flipV23},1:{0:w.rcFvRcfh,1:w.rcFvRcfh,2:w.rcR2Rcc,3:w.rcR2Rcc,4:w.rcfhFhRcc,5:w.rcfhFhRcc},2:{0:w.flipH12,1:w.flipH12,2:w.flipH23,3:w.flipH23,4:w.rcR2Rcc,5:w.rcR2Rcc}},Ft=(n,t)=>{const e=At(n),r={};for(let s=0;s<6;s++)zt(s,n)||(r[s]=Tt(e,s,t));return r},ht=(n,t,e)=>{for(const r of e){let{axis:s,index:o,angle:i}=xt(r);const a=s*2+Math.sign(o),R={};for(const[u,y]of Object.entries(t)){const c=parseInt(u),B=Ct[s][c][i];if(c===a){R[a]=B(y);continue}const g=Rt[s].indexOf(c),f=Rt[s][V(g-i,4)];R[f]=B(y)}const L=Ut[s][o].indexOf(n);n=Ut[s][o][V(L+i*2,8)],t=R}return t},Lt=(n,t,e)=>{const r=t.cubies[n];for(const s of r.facelets)s.uvs=e[s.side]},Et=["URF","ULF","ULB","URB","DRF","DLF","DLB","DRB"],Bt=["UF","UL","UB","UR","FR","FL","BL","BR","DF","DL","DB","DR"],Mt=[{id:"URF",x:1,y:1,z:1},{id:"ULF",x:-1,y:1,z:1},{id:"ULB",x:-1,y:1,z:-1},{id:"URB",x:1,y:1,z:-1},{id:"DRF",x:1,y:-1,z:1},{id:"DLF",x:-1,y:-1,z:1},{id:"DLB",x:-1,y:-1,z:-1},{id:"DRB",x:1,y:-1,z:-1}],Ot=[{id:"UF",x:0,y:1,z:1},{id:"UL",x:-1,y:1,z:0},{id:"UB",x:0,y:1,z:-1},{id:"UR",x:1,y:1,z:0},{id:"FR",x:1,y:0,z:1},{id:"FL",x:-1,y:0,z:1},{id:"BL",x:-1,y:0,z:-1},{id:"BR",x:1,y:0,z:-1},{id:"DF",x:0,y:-1,z:1},{id:"DL",x:-1,y:-1,z:0},{id:"DB",x:0,y:-1,z:-1},{id:"DR",x:1,y:-1,z:0}],kt=[{id:"c0",x:0,y:1,z:0,faces:{up:"white"}},{id:"c1",x:0,y:-1,z:0,faces:{down:"yellow"}},{id:"c2",x:0,y:0,z:1,faces:{front:"green"}},{id:"c3",x:0,y:0,z:-1,faces:{back:"blue"}},{id:"c4",x:-1,y:0,z:0,faces:{left:"orange"}},{id:"c5",x:1,y:0,z:0,faces:{right:"red"}},{id:"core",x:0,y:0,z:0,faces:{}}],It={URF:["up","right","front"],ULF:["up","front","left"],ULB:["up","left","back"],URB:["up","back","right"],DRF:["down","right","front"],DLF:["down","left","front"],DLB:["down","back","left"],DRB:["down","right","back"]},Ht={UF:["up","front"],UL:["up","left"],UB:["up","back"],UR:["up","right"],FR:["front","right"],FL:["front","left"],BL:["back","left"],BR:["back","right"],DF:["down","front"],DL:["down","left"],DB:["down","back"],DR:["down","right"]},Pt=n=>({URF:["white","red","green"],ULF:["white","green","orange"],ULB:["white","orange","blue"],URB:["white","blue","red"],DRF:["yellow","red","green"],DLF:["yellow","orange","green"],DLB:["yellow","blue","orange"],DRB:["yellow","red","blue"]})[n],Nt=n=>({UF:["white","green"],UL:["white","orange"],UB:["white","blue"],UR:["white","red"],FR:["green","red"],FL:["green","orange"],BL:["blue","orange"],BR:["blue","red"],DF:["yellow","green"],DL:["yellow","orange"],DB:["yellow","blue"],DR:["yellow","red"]})[n];class $t{constructor(){this.state=new Dt(!1)}reset(){this.state=new Dt(!1)}rotateLayer(t,e,r){let s="";if(t==="y"?e===1?s=r===1?"U'":"U":e===-1&&(s=r===1?"D'":"D"):t==="x"?e===1?s=r===1?"R'":"R":e===-1&&(s=r===1?"L'":"L"):t==="z"&&(e===1?s=r===1?"F'":"F":e===-1&&(s=r===1?"B'":"B")),s){console.log("[RubiksJSModel] Applying move:",s);try{this.state.applyTurn(s),console.log("[RubiksJSModel] Move applied successfully")}catch(o){console.error("[RubiksJSModel] Failed to apply move:",s,o)}}}toCubies(){const t=this.state.encode(),e=atob(t),r=new Uint8Array(e.length);for(let c=0;c<e.length;c++)r[c]=e.charCodeAt(c);let s=r[0]+(r[1]<<8)+(r[2]<<16),o=r[3]+(r[4]<<8);const i=[],a=[];for(let c=7;c>=0;c--){const B=s&7;i[c]=Et[B],s=s>>3;const g=o&3;a[c]=g,o=o>>2}const R=[],L=[];for(let c=0;c<6;c++){const B=r[5+c],g=B&15,f=B>>4&15;R[c*2]=Bt[g],R[c*2+1]=Bt[f]}let u=r[11]+(r[12]<<8);for(let c=11;c>=0;c--)L[c]=u&1,u=u>>1;const y=[...kt];for(let c=0;c<8;c++){const B=i[c],g=a[c],f=Mt[c],b=Pt(B),z=It[f.id],x={};x[z[0]]=b[(0+g)%3],x[z[1]]=b[(1+g)%3],x[z[2]]=b[(2+g)%3],y.push({id:`corn${c}`,x:f.x,y:f.y,z:f.z,faces:x})}for(let c=0;c<12;c++){const B=R[c],g=L[c],f=Ot[c],b=Nt(B),z=Ht[f.id],x={};g===0?(x[z[0]]=b[0],x[z[1]]=b[1]):(x[z[0]]=b[1],x[z[1]]=b[0]),y.push({id:`edge${c}`,x:f.x,y:f.y,z:f.z,faces:x})}return y}validate(){return{valid:!0,errors:[]}}}const v=new $t,lt=()=>{try{const n=v.toCubies();postMessage({type:"STATE_UPDATE",payload:{cubies:n}})}catch(n){console.error("[Worker] Error generating cubies:",n),postMessage({type:"ERROR",payload:n.message})}};self.onmessage=n=>{const{type:t,payload:e}=n.data;switch(t){case"INIT":case"RESET":v.reset(),lt();break;case"ROTATE_LAYER":const{axis:r,index:s,direction:o}=e;v.rotateLayer(r,s,o),lt();break;case"VALIDATE":const i=v.validate();postMessage({type:"VALIDATION_RESULT",payload:{valid:i.valid,errors:i.errors}});break}}})();
|