cozyclay 3.1.1 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/bin/agent/agent-routes.mjs +4 -4
  3. package/bin/agent/main-agent.mjs +21 -7
  4. package/bin/agent/main-prompt.mjs +7 -6
  5. package/bin/agent/script-tool.mjs +2 -1
  6. package/bin/agent/studio-prompt.mjs +1 -1
  7. package/bin/agent/studio-rebase.mjs +5 -4
  8. package/bin/agent/studio-task-routes.mjs +1 -1
  9. package/bin/agent/studio-tasks.mjs +116 -25
  10. package/bin/agent/studio-tools.mjs +3 -2
  11. package/bin/cozyclay.mjs +4 -2
  12. package/bin/live-endpoint.mjs +2 -0
  13. package/dist/app/index.html +6 -6
  14. package/dist/assets/TaskBoard-GR8xZm5c.js +1 -0
  15. package/dist/assets/{app-BlwOpest.css → app-Bhe0TpGz.css} +1 -1
  16. package/dist/assets/app-C4QTcDcb.js +5144 -0
  17. package/dist/assets/compile-worker-oy_P9HHh.js +6 -0
  18. package/dist/assets/motion3-compile-dgGhrvPK.js +1 -0
  19. package/dist/assets/scene-asset-sync-BEjjhNEo.css +1 -0
  20. package/dist/assets/scene-asset-sync-qoxcdrhj.js +13 -0
  21. package/dist/assets/task-board-BqReHzOi.js +15 -0
  22. package/dist/assets/workflow-C87p6E1E.js +179 -0
  23. package/dist/cozyclay-package.json +1 -1
  24. package/dist/index.html +1 -1
  25. package/dist/sitemap.xml +1 -1
  26. package/dist/workflow/index.html +4 -4
  27. package/mcp/README.md +1 -1
  28. package/mcp/live-hub.mjs +5 -3
  29. package/mcp/tool-handlers.mjs +5 -5
  30. package/package.json +1 -1
  31. package/src/App.jsx +42 -64
  32. package/src/bus-task-scopes.js +59 -11
  33. package/src/bus-task-targets.js +35 -9
  34. package/src/bus-tasks.js +23 -10
  35. package/src/command-bus.js +7 -4
  36. package/src/commands/ai.js +1 -1
  37. package/src/commands/export.js +1 -1
  38. package/src/commands/motion3.js +27 -7
  39. package/src/export-workspace.js +244 -0
  40. package/src/keyframe-pack.js +5 -1
  41. package/src/live-control.js +14 -7
  42. package/src/motion3/compile-job.js +15 -3
  43. package/src/motion3/compiler/channels.js +74 -24
  44. package/src/motion3/compiler/compile.js +2 -1
  45. package/src/motion3/compiler/idle.js +2 -0
  46. package/src/motion3/compiler/overlap.js +180 -0
  47. package/src/motion3/compiler/pose.js +6 -4
  48. package/src/motion3/compiler/validate-common.js +1 -0
  49. package/src/motion3/compiler/validate-parts.js +5 -1
  50. package/src/motion3/compiler/validate.js +30 -10
  51. package/src/motion3/correct.js +236 -0
  52. package/src/motion3/docs.js +8 -6
  53. package/src/motion3/face-lean.js +31 -0
  54. package/src/motion3/fix/index.js +5 -1
  55. package/src/motion3/fix/reach-body.js +172 -0
  56. package/src/motion3/hint-check.js +44 -0
  57. package/src/motion3/json-patch.js +120 -12
  58. package/src/motion3/lenient.js +46 -4
  59. package/src/motion3/object-keys.js +17 -5
  60. package/src/motion3/pipeline.js +46 -9
  61. package/src/motion3/qa/arm-checks.js +96 -0
  62. package/src/motion3/qa/asked.js +113 -0
  63. package/src/motion3/qa/goals.js +37 -8
  64. package/src/motion3/qa/hints-goal.js +6 -5
  65. package/src/motion3/qa/index.js +5 -2
  66. package/src/motion3/qa/key-checks.js +4 -3
  67. package/src/motion3/qa/motion-life.js +243 -0
  68. package/src/motion3/qa/spec-checks.js +15 -9
  69. package/src/motion3/qa/verdict.js +50 -0
  70. package/src/motion3/refs.js +38 -14
  71. package/src/motion3/repair-watch.js +132 -0
  72. package/src/motion3/repair.js +52 -19
  73. package/src/motion3/report.js +50 -9
  74. package/src/motion3/schema/topics.js +6 -4
  75. package/src/motion3/schema/types.js +1 -1
  76. package/src/motion3/skills/basic.js +15 -8
  77. package/src/motion3/skills/index.js +52 -18
  78. package/src/motion3/skills/info.js +1 -1
  79. package/src/motion3/skills/stance.js +6 -0
  80. package/src/motion3/skills/walk-fit.js +170 -0
  81. package/src/panels/PoseImagePanel.jsx +3 -2
  82. package/src/panels/SculptPanel.jsx +2 -2
  83. package/src/result-modal.jsx +3 -3
  84. package/src/shell/ExportFolderPreference.jsx +103 -0
  85. package/src/shell/PreferencesDialog.jsx +3 -166
  86. package/src/shell/StatusBar.jsx +1 -1
  87. package/src/shell/preference-controls.jsx +114 -0
  88. package/src/shell/preference-pages.jsx +72 -0
  89. package/src/shell/preferences.css +46 -0
  90. package/src/shot-prompt.js +29 -9
  91. package/src/studio-actions.js +2 -2
  92. package/src/studio-agent-motion.js +19 -9
  93. package/src/studio-agent-protocol.js +1 -1
  94. package/src/studio-app-binding.js +3 -2
  95. package/src/workflow/AgentPanel.jsx +2 -2
  96. package/src/workflow/CozySceneNode.jsx +1 -1
  97. package/src/workflow/WorkflowBuilder.jsx +2 -2
  98. package/src/workflow/keyframe-pack-request.js +3 -10
  99. package/src/workflow/task-board.css +1 -1
  100. package/src/workflow/task-board.js +7 -5
  101. package/tools/loc-baseline.json +1 -2
  102. package/tools/run-tests.mjs +5 -5
  103. package/dist/assets/TaskBoard-ngdqiu7u.js +0 -1
  104. package/dist/assets/app-DmR7jBkz.js +0 -5144
  105. package/dist/assets/compile-worker-D0pKioa0.js +0 -5
  106. package/dist/assets/motion3-compile-BB3_1-EB.js +0 -1
  107. package/dist/assets/scene-asset-sync-CBx7XRti.css +0 -1
  108. package/dist/assets/scene-asset-sync-Cgdtr1Yw.js +0 -13
  109. package/dist/assets/task-board-ErQvfoaz.js +0 -14
  110. package/dist/assets/workflow-zQM-EKW1.js +0 -179
@@ -1,5 +0,0 @@
1
- (function(){let e=(e,t)=>[e[0]+t[0],e[1]+t[1],e[2]+t[2]],t=(e,t)=>[e[0]-t[0],e[1]-t[1],e[2]-t[2]],n=(e,t)=>[e[0]*t,e[1]*t,e[2]*t],r=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],i=(e,t)=>[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]],a=e=>Math.hypot(e[0],e[1],e[2]),o=(e,n)=>a(t(e,n));function s(e){let t=a(e);return t>1e-12?[e[0]/t,e[1]/t,e[2]/t]:[0,0,0]}let c=(e,t,n)=>Math.min(n,Math.max(t,e)),l=e=>e*Math.PI/180,u=e=>e*180/Math.PI,d=()=>[[1,0,0],[0,1,0],[0,0,1]];function f(e,t){let n=[[0,0,0],[0,0,0],[0,0,0]];for(let r=0;r<3;r++)for(let i=0;i<3;i++)n[r][i]=e[r][0]*t[0][i]+e[r][1]*t[1][i]+e[r][2]*t[2][i];return n}let p=(e,t)=>[e[0][0]*t[0]+e[0][1]*t[1]+e[0][2]*t[2],e[1][0]*t[0]+e[1][1]*t[1]+e[1][2]*t[2],e[2][0]*t[0]+e[2][1]*t[1]+e[2][2]*t[2]],m=e=>[[e[0][0],e[1][0],e[2][0]],[e[0][1],e[1][1],e[2][1]],[e[0][2],e[1][2],e[2][2]]],h=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]];function g(e){let t=l(e),n=Math.cos(t),r=Math.sin(t);return[[1,0,0],[0,n,-r],[0,r,n]]}function _(e){let t=l(e),n=Math.cos(t),r=Math.sin(t);return[[n,0,r],[0,1,0],[-r,0,n]]}function v(e){let t=l(e),n=Math.cos(t),r=Math.sin(t);return[[n,-r,0],[r,n,0],[0,0,1]]}function y(e,t){let[n,r,i]=s(e),a=l(t),o=Math.cos(a),c=Math.sin(a),u=1-o;return[[o+n*n*u,n*r*u-i*c,n*i*u+r*c],[r*n*u+i*c,o+r*r*u,r*i*u-n*c],[i*n*u-r*c,i*r*u+n*c,o+i*i*u]]}function b(e,o){let c=s(e),l=t(o,n(c,r(o,c)));return a(l)<1e-9&&(l=t([0,1,0],n(c,c[1])),a(l)<1e-9&&(l=t([1,0,0],n(c,c[0])))),l=s(l),h(c,l,i(c,l))}let x=(e,t,n,r)=>f(b(n,r),m(b(e,t)));function S(e){let t=e[0][0],n=e[1][1],r=e[2][2],i=t+n+r,a,o,s,c;if(i>0){let t=.5/Math.sqrt(i+1);c=.25/t,a=(e[2][1]-e[1][2])*t,o=(e[0][2]-e[2][0])*t,s=(e[1][0]-e[0][1])*t}else if(t>n&&t>r){let i=2*Math.sqrt(1+t-n-r);c=(e[2][1]-e[1][2])/i,a=.25*i,o=(e[0][1]+e[1][0])/i,s=(e[0][2]+e[2][0])/i}else if(n>r){let i=2*Math.sqrt(1+n-t-r);c=(e[0][2]-e[2][0])/i,a=(e[0][1]+e[1][0])/i,o=.25*i,s=(e[1][2]+e[2][1])/i}else{let i=2*Math.sqrt(1+r-t-n);c=(e[1][0]-e[0][1])/i,a=(e[0][2]+e[2][0])/i,o=(e[1][2]+e[2][1])/i,s=.25*i}let l=Math.hypot(a,o,s,c);return[a/l,o/l,s/l,c/l]}function C([e,t,n,r]){return[[1-2*(t*t+n*n),2*(e*t-n*r),2*(e*n+t*r)],[2*(e*t+n*r),1-2*(e*e+n*n),2*(t*n-e*r)],[2*(e*n-t*r),2*(t*n+e*r),1-2*(e*e+t*t)]]}let w=(e,t)=>u(Math.acos(c(r(s(e),s(t)),-1,1))),T=.01;function E(e){let n=e.bones,r=n.map(e=>e.name),i=Object.fromEntries(r.map((e,t)=>[e,t])),a=n.map(e=>e.parent),o=r.map(()=>[]);a.forEach((e,t)=>{e>=0&&o[e].push(t)});let s=r.map((e,t)=>{let n=[],r=[t];for(;r.length;){let e=r.shift();n.push(e),r.unshift(...o[e])}return n}),c=n.map(e=>e.localPos.map(e=>e*T)),l=n.map(e=>C(e.localQuat)),u=n.map(e=>e.worldPos.map(e=>e*T)),d=n.map(e=>C(e.worldQuat)),f=e.landmarks,h=i.Head,g=e=>p(m(d[h]),t(e.map(e=>e*T),u[h])),_={mouth:g(f.mouth),headCentre:g(f.headEllipsoid.centre),headRadii:f.headEllipsoid.radii.map(e=>e*T),sole:{ball:(f.sole?.ball??0)*T,tip:(f.sole?.tip??0)*T}};return{model:e.model,names:r,index:i,parent:a,children:o,subtree:s,restLocalPos:c,restLocalRot:l,restWorldPos:u,restWorldRot:d,landmarks:_}}function D(e){return{P:e.restWorldPos.map(e=>e.slice()),R:e.restWorldRot.map(e=>e.map(e=>e.slice()))}}function O(n,r,i,a,o){for(let s of n.subtree[i])r.P[s]=e(a,p(o,t(r.P[s],a))),r.R[s]=f(o,r.R[s])}function k(e,t){return{quats:e.names.map((n,r)=>{let i=e.parent[r];return S(i<0?t.R[r]:f(m(t.R[i]),t.R[r]))}),hips:t.P[e.index.Hips].slice()}}let A=(t,n,r)=>e(t.P[n],p(t.R[n],r)),j=(e,t,n,r)=>p(t.R[n],p(m(e.restWorldRot[n]),r)),M=(e,t,n)=>Math.acos(c((n*n-e*e-t*t)/(2*e*t),-1,1))*180/Math.PI,N=(e,t,n)=>Math.sqrt(e*e+t*t+2*e*t*Math.cos(n*Math.PI/180));function P(e,t,n,r=145,i=150){let a=N(e,t,r);if(n>=a)return n;let o=a-N(e,t,i);return n>a-2*o?n+(a-n)*(a-n)/(4*o):a-o}function ee(i,o,c,l,u,d=145,f=150){let p=N(c,l,d),m=a(t(o,i));if(m>=p)return o;let h=P(c,l,m,d,f),g=n(u,N(c,l,f)),_=e(i,g),v=t(o,_),y=a(v)>1e-9?s(v):n(u,-1),b=r(y,g);return e(_,n(y,-b+Math.sqrt(Math.max(0,b*b-r(g,g)+h*h))))}let F=(e,t)=>j(e,t,e.index.Spine2??e.index.Hips,[0,0,-1]),I=(e,t)=>j(e,t,e.index.Hips,[0,1,0]),L=(e,t,n)=>M(e,t,P(e,t,N(e,t,n)));function te(e,n,r,i,[o,s,c]=[`Arm`,`ForeArm`,`Hand`]){let l=n.P[e.index[`${r}${o}`]],u=n.P[e.index[`${r}${s}`]],d=n.P[e.index[`${r}${c}`]];return M(a(t(u,l)),a(t(d,u)),a(t(i,l)))}function R(e,n,o){let l=e.index[`${o}Arm`],u=e.index[`${o}ForeArm`],d=t(n.P[u],n.P[l]),f=t(n.P[e.index[`${o}Hand`]],n.P[u]),p=Math.acos(c(r(s(d),s(f)),-1,1))*180/Math.PI,m=i(d,f);p>145&&a(m)>1e-12&&O(e,n,u,n.P[u].slice(),y(m,L(a(d),a(f),p)-p))}let ne={elbow:[145,150,F],knee:[150,160,I]};function re(i,o,l,u,d,f,p,m,h=null){let g=o.P[l],_=o.P[u],v=o.P[d],y=a(t(_,g)),b=a(t(v,_)),S=ne[h],C=S?ee(g,f,y,b,S[2](i,o),S[0],S[1]):f,w=C===f?0:a(t(C,f)),T=t(C,g),E=a(T),D=n(T,1/Math.max(E,1e-9)),k=c(E,Math.abs(y-b)+1e-4,(y+b)*.9995),A=(y*y-b*b+k*k)/(2*k),M=Math.sqrt(Math.max(y*y-A*A,0)),N=s(t(p,n(D,r(p,D)))),P=e(g,e(n(D,A),n(N,M))),F=j(i,o,l,m);O(i,o,l,g.slice(),x(t(_,g),F,t(P,g),N));let I=j(i,o,u,m),L=e(g,n(D,k));return O(i,o,u,o.P[u].slice(),x(t(o.P[d],o.P[u]),I,t(L,o.P[u]),N)),w||E-k}function ie(e,n,r,i,o,s=null){let c=e.index[`${r}Foot`],l=e.index[`${r}ToeBase`],u=t(e.restWorldPos[l],e.restWorldPos[c]),d=s?p(s.R,p(_(i-s.yaw),p(g(o),u))):p(_(i),p(g(o),u)),f=s?p(s.R,[0,1,0]):[0,1,0];O(e,n,c,n.P[c].slice(),x(t(n.P[l],n.P[c]),j(e,n,c,[0,1,0]),d,f));let m=e.index[`${r}Toe_End`];if(!s&&o>0&&m!==void 0){let r=t(n.P[m],n.P[l]),i=[r[0],0,r[2]];a(i)>1e-6&&O(e,n,l,n.P[l].slice(),x(r,[0,1,0],i,[0,1,0]))}}function z(e,a,o){let c=e.index[`${o}Hand`],l=e.index[`${o}HandMiddle1`],u=s(t(a.P[l],a.P[c])),d=t(a.P[e.index[`${o}HandIndex1`]],a.P[e.index[`${o}HandPinky1`]]);return d=s(t(d,n(u,r(d,u)))),{d:u,l:d,n:i(u,d)}}function ae(e,n,r,i){let a=e.index[`${r}Arm`],o=e.index[`${r}Hand`],s=y(t(n.P[o],n.P[a]),i);O(e,n,a,n.P[a].slice(),s),O(e,n,o,n.P[o].slice(),m(s))}function oe(e,i,a,o,l){if(!o&&!l)return;let u=e.index[`${a}Hand`],d=e.index[`${a}ForeArm`],f=s(t(i.P[u],i.P[d])),{l:p}=z(e,i,a),m=s(t(p,n(f,r(p,f))));O(e,i,u,i.P[u].slice(),y(m,c(l,-70,70))),O(e,i,u,i.P[u].slice(),y(f,c(o,-85,85)))}let se={relaxed:{Index:[12,18,10],Middle:[15,20,12],Ring:[18,22,12],Pinky:[20,24,14],Thumb:[15,10,8]},fist:{Index:[80,95,60],Middle:[85,95,60],Ring:[85,95,60],Pinky:[85,95,60],Thumb:[15,10,8]},grip:{Index:[55,60,35],Middle:[60,62,35],Ring:[62,62,35],Pinky:[65,62,35],Thumb:[15,10,8]},cigarette:{Index:[8,10,6],Middle:[10,12,8],Ring:[60,75,45],Pinky:[65,80,45],Thumb:[35,25,20]},point:{Index:[0,0,0],Middle:[80,95,60],Ring:[85,95,60],Pinky:[85,95,60],Thumb:[40,30,30]},spherical:{Index:[35,40,25],Middle:[38,42,25],Ring:[40,42,25],Pinky:[42,44,25],Thumb:[15,10,8]}},ce={spherical:{Index:9,Middle:2,Ring:-5,Pinky:-12}};function le(e,t,n){let r=se.relaxed;return r[t][n]+Object.entries(e??{}).reduce((e,[i,a])=>e+(a||0)*((se[i]?.[t]?.[n]??r[t][n])-r[t][n]),0)}let ue=(e,t)=>se.fist[e][t];function de(e,t,n,r){let i=n===`Left`?-1:1,a=(e,t)=>le(r,e,t);for(let i of[`Index`,`Middle`,`Ring`,`Pinky`]){let a=Object.entries(r??{}).reduce((e,[t,n])=>e+(n||0)*(ce[t]?.[i]??0),0),o=e.index[`${n}Hand${i}1`];a&&o!==void 0&&O(e,t,o,t.P[o].slice(),y(z(e,t,n).n,a))}for(let r of[`Index`,`Middle`,`Ring`,`Pinky`,`Thumb`])for(let o=0;o<3;o++){let s=e.index[`${n}Hand${r}${o+1}`];if(s===void 0)continue;let{l:c,d:l}=z(e,t,n),u=r===`Thumb`&&o===0?l:c;O(e,t,s,t.P[s].slice(),y(u,i*a(r,o)))}let o=[0,1,2,3].map(e=>Object.entries(r??{}).reduce((t,[n,r])=>t+(r||0)*(fe[n]?.[e]??0),0));o.some(Boolean)&&pe(e,t,n,o)}let fe={fist:[-70,-5,5,80],grip:[-30,-30,30,10],spherical:[-40,-20,20,10]};function pe(e,n,r,[a,o,c,l]){let u=r===`Left`?-1:1,d=t=>e.index[`${r}HandThumb${t}`];if(d(1)===void 0)return;let{d:f,n:p}=z(e,n,r);O(e,n,d(1),n.P[d(1)].slice(),y(f,u*a)),O(e,n,d(1),n.P[d(1)].slice(),y(p,o));for(let[r,a]of[[2,c],[3,l]]){let o=s(i(s(t(n.P[d(r+1)],n.P[d(r)])),p));O(e,n,d(r),n.P[d(r)].slice(),y(o,u*a))}}let me=(e,t)=>[e[3]*t[0]+e[0]*t[3]+e[1]*t[2]-e[2]*t[1],e[3]*t[1]-e[0]*t[2]+e[1]*t[3]+e[2]*t[0],e[3]*t[2]+e[0]*t[1]-e[1]*t[0]+e[2]*t[3],e[3]*t[3]-e[0]*t[0]-e[1]*t[1]-e[2]*t[2]],he=e=>[-e[0],-e[1],-e[2],e[3]],ge=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2]+e[3]*t[3];function _e(e){let t=Math.hypot(e[0],e[1],e[2],e[3]);return t>1e-12?[e[0]/t,e[1]/t,e[2]/t,e[3]/t]:[0,0,0,1]}function ve(e){let t=e[3]<0?-1:1,n=e[0]*t,r=e[1]*t,i=e[2]*t,a=e[3]*t,o=Math.hypot(n,r,i);if(o<1e-12)return[2*n,2*r,2*i];let s=2*Math.atan2(o,a)/o;return[n*s,r*s,i*s]}function ye(e){let t=Math.hypot(e[0],e[1],e[2]);if(t<1e-12)return _e([e[0]/2,e[1]/2,e[2]/2,1]);let n=Math.sin(t/2)/t;return[e[0]*n,e[1]*n,e[2]*n,Math.cos(t/2)]}let be=(e,t)=>me(me(e,[t[0],t[1],t[2],0]),he(e)).slice(0,3);function xe(e,t){let n=1+e[0]*t[0]+e[1]*t[1]+e[2]*t[2];if(n>1e-9)return _e([e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0],n]);let r=Math.abs(e[0])<.9?[0,e[2],-e[1]]:[-e[2],0,e[0]],i=Math.hypot(...r);return[r[0]/i,r[1]/i,r[2]/i,0]}let Se=(e,t)=>{let n=t*Math.PI/360,r=Math.sin(n);return[e[0]*r,e[1]*r,e[2]*r,Math.cos(n)]},Ce=(e,t,n)=>me(e,ye(ve(me(he(e),t)).map(e=>e*n))),we={"y-bot-tpose":new URL(`/assets/y-bot-tpose-body-Dy1VKChZ.json`,``+self.location.href),"x-bot-tpose":new URL(`/assets/x-bot-tpose-body-BusyR3eg.json`,``+self.location.href)},Te=new Map;function Ee(e){let t=e.protocol===`file:`?globalThis.process?.getBuiltinModule?.(`node:fs`):null;if(t)return JSON.parse(t.readFileSync(e,`utf8`));let n=new XMLHttpRequest;if(n.open(`GET`,e.href,!1),n.send(),n.status!==200)throw Error(`motion body fit ${e.pathname}: HTTP ${n.status}`);return JSON.parse(n.responseText)}function De(e){return Te.has(e)||Te.set(e,Ee(we[e])),Te.get(e)}let Oe=.1,ke=new Map;function Ae(e){let t=typeof Buffer<`u`?Buffer.from(e,`base64`):Uint8Array.from(atob(e),e=>e.charCodeAt(0));return new Int8Array(t.buffer,t.byteOffset,t.byteLength)}function je(e,t){let n=new Set([t,e.parent[t],...e.children[t]]);return/^(Left|Right)Arm$/.test(e.names[t])&&n.add(e.parent[e.parent[t]]),/^(Left|Right)UpLeg$/.test(e.names[t])&&n.add(e.index.Spine),n}function Me(e){if(!we[e.model])return null;if(ke.has(e.model))return ke.get(e.model);let t=De(e.model),n=Object.entries(t.grids).map(([n,r])=>{let i=r.dims.map(e=>(e-1)*t.step/2),o=r.min.map((e,t)=>e+i[t]);return{b:e.index[n],min:r.min,dims:r.dims,data:Ae(r.data),centre:o,reach:a(i)}}),r=Object.fromEntries(Object.entries(t.radii).map(([t,n])=>[e.index[t],n])),i={step:t.step,grids:n,radii:r,skip:new Map(Object.keys(r).map(t=>[+t,je(e,+t)]))};return ke.set(e.model,i),i}function Ne(e,t,n){let[r,i,a]=t.dims,o=e.step,s=(n[0]-t.min[0])/o,c=(n[1]-t.min[1])/o,l=(n[2]-t.min[2])/o;if(s<0||c<0||l<0||s>r-1||c>i-1||l>a-1)return Oe;let u=Math.min(r-2,Math.floor(s)),d=Math.min(i-2,Math.floor(c)),f=Math.min(a-2,Math.floor(l)),p=s-u,m=c-d,h=l-f,g=(e,n,r)=>t.data[((u+e)*i+d+n)*a+f+r],_=g(0,0,0)*(1-p)+g(1,0,0)*p,v=g(0,0,1)*(1-p)+g(1,0,1)*p,y=g(0,1,0)*(1-p)+g(1,1,0)*p,b=g(0,1,1)*(1-p)+g(1,1,1)*p;return((_*(1-m)+y*m)*(1-h)+(v*(1-m)+b*m)*h)/1e3}let Pe=new WeakMap;function Fe(t,n){let r=Pe.get(n);return r||(r=t.grids.map(t=>e(n.P[t.b],p(n.R[t.b],t.centre))),Pe.set(n,r)),r}function Ie(e,n,r,i=null){let o=Oe,s=-1,c=Fe(e,n);return e.grids.forEach((l,u)=>{if(i?.has(l.b)||a(t(r,c[u]))-l.reach>o)return;let d=Ne(e,l,p(m(n.R[l.b]),t(r,n.P[l.b])));d<o&&(o=d,s=l.b)}),{d:o,bone:s}}function Le(r,i,o,s=null,c=.01){let l=[0,1,2].map(n=>{let a=[0,0,0];return a[n]=c,Ie(r,i,e(o,a),s).d-Ie(r,i,t(o,a),s).d}),u=a(l);return u>1e-9?n(l,1/u):null}let Re=e=>/^(Left|Right)Arm$/.test(e)?.5:/^(Left|Right)UpLeg$/.test(e)?.3:0,ze=(e,t)=>/Hand$/.test(e.names[t])?e.index[e.names[t].replace(/Hand$/,`HandMiddle1`)]:e.children[t][0];function Be(r,i,a,o){let s=r.radii[o],c=ze(i,o);if(!s||c===void 0)return[];let l=a.P[o],u=t(a.P[c],l),d=Re(i.names[o]);return s.filter(([e])=>e>=d).map(([t,r])=>({p:e(l,n(u,t)),r,t}))}let Ve=/^(Spine\d?|Neck|Head)$/,He=(e,t)=>e.children[t].find(t=>Ve.test(e.names[t]))??ze(e,t),Ue=.25;function We(i,o,s,l,u,d=null){let f=Ie(i,s,l,d);if(f.d>=u||f.bone<0)return l;let p=s.P[f.bone],m=He(o,f.bone),h=m===void 0?p:s.P[m],g=t(h,p),_=r(g,g)>1e-9?c(r(t(l,p),g)/r(g,g),0,1):0,v=t(l,e(p,n(g,_))),y=a(v)>1e-4?n(v,1/a(v)):Le(i,s,l,d);if(!y)return l;let b=new Set(i.grids.map(e=>e.b).filter(e=>e!==f.bone)),x=t=>Ie(i,s,e(l,n(y,t)),b).d>=u,S=0,C=.02;for(;!x(C)&&C<Ue;)S=C,C=Math.min(Ue,C*2);for(let e=0;e<10;e++){let e=(S+C)/2;x(e)?C=e:S=e}return e(l,n(y,C))}let Ge=.03;function Ke(e){let t=Me(e);return t&&{body:t,skip:new Set(e.names.map((e,t)=>/(Arm|ForeArm|Hand|Neck|Head.*)$/.test(e)?t:-1).filter(e=>e>=0))}}function qe(r,i,a,o,s,c){let l=0,u=Re(i.names[o]);for(let[i,d]of r.radii[o]??[]){if(i<u)continue;let{d:f,bone:p}=Ie(r,a,e(s,n(t(c,s),i)),r.skip.get(o));p>=0&&(l=Math.max(l,d-f))}return l}function Je(e,t,n,r){let i={depth:0,p:null,into:-1};for(let a of Be(e,t,n,r)){let{d:t,bone:o}=Ie(e,n,a.p,e.skip.get(r)),s=a.r-t;o>=0&&s>i.depth&&(i={depth:s,p:a.p,into:o,r:a.r})}return i}let Ye={twist:[-85,85],flex:[-70,70],dev:[-35,20]},Xe={twist:[-50,50],flex:[-40,40],dev:[-25,15]},Ze=[0,0,-1],Qe=100/(180/Math.PI),$e=180/Math.PI;function et(e,t=8,n=400){let r=0;for(let i of[`twist`,`flex`,`dev`]){let a=e[i],o=a>=0?Xe[i][1]:-Xe[i][0],s=a>=0?Ye[i][1]:-Ye[i][0],c=Math.abs(a);r+=c<=o?(c/o)**2:1+t*((Math.min(c,s)-o)/Math.max(1e-6,s-o))**2+(c>s?n*((c-s)/s)**2:0)}return r}function tt(e,n){let r=s(t(n,e)),i=Math.acos(c(-r[1],-1,1))*$e;return 2*(Math.max(0,i-70)/30)**2}function nt(e,a,o,c){let l=S(f(a,m(e)));l[3]<0&&(l=l.map(e=>-e));let u=2*Math.atan2(r([l[0],l[1],l[2]],o),l[3]),d=ye(n(o,u)),h=ve(me(l,he(d))),g=p(C(d),c),_=s(t(g,n(o,r(g,o)))),v=i(o,_);return{twist:u*$e,flex:r(h,_)*$e,dev:r(h,v)*$e,axF:_,axD:v}}function rt(e,t=0){let n=0;for(let[r,[i,a]]of Object.entries(Ye)){let o=(e[r]-(i+a)/2)/((a-i)/2),s=Math.max(0,Math.abs(o)-1+t);n+=o*o+400*s*s}return n}function it(o,l,u,d,h,g,_,v=0,y=!1){let b=o.index,S=b[`${u}Arm`],C=b[`${u}ForeArm`],w=b[`${u}Hand`],T=l.P[S],E=a(t(l.P[C],T)),D=a(t(l.P[w],l.P[C])),O=t(ee(T,h,E,D,F(o,l)),T),k=a(O),A=n(O,1/Math.max(k,1e-9)),j=c(k,Math.abs(E-D)+1e-4,(E+D)*.9995),M=(E*E-D*D+j*j)/(2*j),N=Math.sqrt(Math.max(E*E-M*M,0)),P=e=>t(e,n(A,r(e,A))),I=s(a(P(g))>1e-6?P(g):i(A,Math.abs(A[1])<.9?[0,1,0]:[1,0,0])),L=i(A,I),te=e(T,n(A,j)),R=o.restWorldRot,ne=p(m(R[C]),t(o.restWorldPos[w],o.restWorldPos[C])),re=p(m(R[C]),_),ie=f(m(R[C]),R[w]),ae=p(m(l.R[w]),z(o,l,u).l),oe=r=>{let i=e(n(I,Math.cos(r)),n(L,Math.sin(r))),a=t(te,e(T,e(n(A,M),n(i,N)))),o=f(x(ne,re,a,i),ie),c=nt(o,d,s(a),p(o,ae)),l=Math.abs(Math.atan2(Math.sin(r),Math.cos(r)))/Qe,u=e(T,e(n(A,M),n(i,N)));return{pv:i,need:c,elbow:u,cost:(y?et(c)+tt(T,u):rt(c,v))+.5*(1-Math.cos(r))+400*Math.max(0,l-1)**2}},se=e=>a(P(e))>1e-6?Math.atan2(r(e,L),r(e,I)):0;return{at:oe,angleOf:se,softmin:()=>{let t=Array.from({length:72},(e,t)=>oe(2*Math.PI*t/72)),r=Math.min(...t.map(e=>e.cost)),i=t.reduce((t,i)=>e(t,n(i.pv,Math.exp(-(i.cost-r)/.02))),[0,0,0]);return a(i)>.001?se(i):2*Math.PI*t.findIndex(e=>e.cost===r)/72},miss:Math.max(0,k-j)}}function at(e,t,n,r,i,a,o,{toward:s,w:c=1,margin:l=0,comfort:u=!1}={}){let d=it(e,t,n,r,i,a,o,l,u),f=s?d.angleOf(s):d.softmin(),p=d.at(c*Math.atan2(Math.sin(f),Math.cos(f)));return{pole:p.pv,need:p.need,cost:p.cost,miss:d.miss,elbow:p.elbow}}let ot=(e,[t,n])=>Math.min(n,Math.max(t,e));function st(r,i,o,s,c,l){let u=n(t(i.P[s],i.P[c]),1/a(t(i.P[s],i.P[c]))),d=f(i.R[c],f(m(r.restWorldRot[c]),r.restWorldRot[s])),{axF:h,axD:g,..._}=nt(d,l,u,p(f(d,m(i.R[s])),z(r,i,o).l)),v=Object.fromEntries(Object.entries(_).map(([e,t])=>[e,ot(t,Ye[e])])),b=f(f(y(e(n(h,v.flex),n(g,v.dev)),Math.hypot(v.flex,v.dev)),y(u,v.twist)),d);return O(r,i,c,i.P[c].slice(),y(u,.5*v.twist)),O(r,i,s,i.P[s].slice(),f(b,m(i.R[s]))),_}function ct(r,i,o){let s=r.index,c=s[`${o}Arm`],l=t(i.P[s[`${o}ForeArm`]],n(e(i.P[c],i.P[s[`${o}Hand`]]),.5));return a(l)>.005?l:j(r,i,c,Ze)}function lt(r,i,a,o,s,c,l){let u=r.index[`${a}Hand`],d=f(m(i.R[u]),s.R),h=p(m(i.R[u]),t(s.pos,i.P[u])),g=C(Ce(S(i.R[u]),S(f(o.R,m(d))),c)),_=i.P[u].slice(),v=e=>t(o.pos,p(e,h));return{goal:g,plane:ct(r,i,a),wristAt:l===`wrist`?()=>_:l===`palm`?v:r=>e(_,n(t(v(r),_),c))}}function ut(e,t,n,r,i,a,o){let s=e.index,c=s[`${n}Arm`],l=s[`${n}ForeArm`],u=s[`${n}Hand`],{w:d}=r,{goal:f,plane:p,wristAt:m}=lt(e,t,n,r.palm,r.measure(t),d,r.pin),h=at(e,t,n,f,m(f),p,i,{toward:r.pole,w:d,comfort:r.comfort}),g=0,_=0,v={twist:0,flex:0,dev:0};for(let r=0;r<3;r++){let a=m(r?t.R[u]:f);_=te(e,t,n,a),g=re(e,t,c,l,u,a,h.pole,i,`elbow`),v=st(e,t,n,u,l,f)}a[`hold_${o}`]={w:d,miss:Math.max(0,g),...v,swivel:mt(t,c,l,u,p),..._>145?{fold:_}:{},...r.second?{second:r.second}:{}},a[`arm_miss_${o}`]*=1-d}function dt(e,t,n,r){let{goal:i,plane:a,wristAt:o}=lt(e,t,n,r.palm,r.measure(t),r.w);return it(e,t,n,i,o(i),a,Ze,0,r.comfort)}function ft(e,n,r,i,o,{shoulder:s=!1}={}){let{goal:c,plane:l,wristAt:u}=lt(e,n,r,i,o,1),d=u(c),f=at(e,n,r,c,d,l,Ze,{margin:.15}),p=e.index,m=n.P[p[`${r}Arm`]],h=a(t(n.P[p[`${r}ForeArm`]],m)),g=a(t(n.P[p[`${r}Hand`]],n.P[p[`${r}ForeArm`]])),_=Math.max(0,N(h,g,145)-a(t(d,m))),v=s?at(e,n,r,c,d,l,Ze,{comfort:!0}):f;return(s?et(v.need)+tt(m,v.elbow)+pt(e,n,r,m,v.elbow,d):f.cost)+1*((f.miss*100)**2+(_*100)**2)}function pt(e,t,n,r,i,a){let o=Me(e),s=e.index;return o?4*(Math.max(qe(o,e,t,s[`${n}Arm`],r,i),qe(o,e,t,s[`${n}ForeArm`],i,a))*100)**2:0}function mt(e,i,a,o,c){let l=s(t(e.P[o],e.P[i])),u=e=>t(e,n(l,r(e,l)));return w(u(t(e.P[a],e.P[i])),u(c))}let ht=[[`torso_lo`,`Spine`,`Spine2`,.12],[`torso_hi`,`Spine2`,`Neck`,.105],[`hips`,`LeftUpLeg`,`RightUpLeg`,.095],[`neck`,`Neck`,`Head`,.055],...[`Left`,`Right`].flatMap(e=>{let t=e===`Left`?`l`:`r`;return[[`thigh_${t}`,`${e}UpLeg`,`${e}Leg`,.07],[`shin_${t}`,`${e}Leg`,`${e}Foot`,.05],[`uarm_${t}`,`${e}Arm`,`${e}ForeArm`,.042],[`farm_${t}`,`${e}ForeArm`,`${e}Hand`,.033],[`hand_${t}`,`${e}Hand`,`${e}HandMiddle3`,.022]]})],gt=(r,i,a=9)=>Array.from({length:a},(o,s)=>e(r,n(t(i,r),s/(a-1))));function B(e,n){let r=p(m(n.R),t(e,n.pos)),i=[0,n.half[1],0],o=[Math.abs(r[0]-i[0])-n.half[0],Math.abs(r[1]-i[1])-n.half[1],Math.abs(r[2]-i[2])-n.half[2]];return a([Math.max(o[0],0),Math.max(o[1],0),Math.max(o[2],0)])+Math.min(Math.max(o[0],o[1],o[2]),0)}function _t(i,o,s,l){let u=1/0,d=t(l,s),f=r(d,d);for(let l of gt(i,o)){let i=f<1e-12?0:c(r(t(l,s),d)/f,0,1);u=Math.min(u,a(t(l,e(s,n(d,i)))))}return u}function vt(e,n,r){let i=A(n,e.index.Head,e.landmarks.headCentre),o=n.R[e.index.Head],s=p(m(o),t(r,i)),c=e.landmarks.headRadii;return(1-a([s[0]/c[0],s[1]/c[1],s[2]/c[2]]))*Math.min(...c)}let yt=e=>Math.round(e.restWorldPos[e.index.LeftFoot][1]*1e3)/1e3,bt=(e,t,n,r)=>r?A(t,n,p(m(e.restWorldRot[n]),[0,-r,0])):t.P[n];function xt(e,n,r){let i=e.index,a=e.restWorldPos,o=e.landmarks.sole??{},s=i[`${r}Foot`],c=i[`${r}ToeBase`],l=i[`${r}Toe_End`]??c,u=[a[s][0],.005,a[s][2]-.045],d=p(m(e.restWorldRot[s]),t(u,a[s]));return{heel:A(n,s,d),ball:bt(e,n,c,o.ball),tip:bt(e,n,l,o.tip)}}function St(e,r){let i=p(m(r.R),t(e,r.pos));i[1]-=r.half[1];let o=i.map((e,t)=>Math.abs(e)-r.half[t]),s;if(Math.max(...o)<0){let e=o.indexOf(Math.max(...o));s=[0,0,0],s[e]=Math.sign(i[e])||1}else s=i.map((e,t)=>o[t]>0?Math.sign(e)*o[t]:0);let c=a(s);return p(r.R,c>1e-12?n(s,1/c):[0,1,0])}function Ct(e){let t=e?.half;if(!t)return{kind:`box`};if(e.renderer===`sphere`){let e=(t[0]+t[1]+t[2])/3;return Math.max(...t)-Math.min(...t)<=.05*e?{kind:`sphere`,r:e}:{kind:`box`}}return e.renderer===`cylinder`?{kind:`cylinder`,r:Math.min(t[0],t[2]),h:2*t[1]}:{kind:`box`}}let wt=(e,n)=>{let r=p(m(e.R),t(n,e.pos));return r[1]-=e.half[1],r},Tt=t=>e(t.pos,p(t.R,[0,t.half[1],0]));function Et(e,n){let r=Ct(e);if(r.kind===`sphere`)return a(t(n,Tt(e)))-r.r;if(r.kind===`cylinder`){let t=wt(e,n),i=Math.hypot(t[0],t[2])-r.r,a=Math.abs(t[1])-r.h/2;return Math.hypot(Math.max(i,0),Math.max(a,0))+Math.min(Math.max(i,a),0)}return B(n,e)}function Dt(r,i){let o=Ct(r);if(o.kind===`sphere`){let e=t(i,Tt(r)),o=a(e);return o>1e-12?n(e,1/o):p(r.R,[0,1,0])}if(o.kind===`cylinder`){let t=wt(r,i),s=Math.hypot(t[0],t[2]),c=s-o.r,l=Math.abs(t[1])-o.h/2,u=s>1e-12?[t[0]/s,0,t[2]/s]:[1,0,0],d=[0,Math.sign(t[1])||1,0],f;f=c<=0&&l<=0?c>l?u:d:e(n(u,Math.max(c,0)),n(d,Math.max(l,0)));let m=a(f);return p(r.R,m>1e-12?n(f,1/m):u)}return St(i,r)}function Ot(e,r){return t(r,n(Dt(e,r),Et(e,r)))}function kt(e,t,n,r){let i=-1/0;for(let a of gt(t,n))i=Math.max(i,r-Et(e,a));return i}let At=(e,t,n,r)=>e?.fix!==!1&&e?.fix?.grasp!==!1&&!!t?.prop&&(t.grasp!==void 0||t.at===void 0&&t.along===void 0&&jt(n,r));function jt(e,t){let n=Object.entries(e?.objects??{}).filter(([e,n])=>e===t||n.ancestors?.includes(t));return n.length===1&&Ct(n[0][1]).kind!==`box`}let Mt=new WeakMap;function Nt(r,i,a,o,s,c){Mt.has(a)||Mt.set(a,new Map);let l=Mt.get(a),u=`${o}|${s.prop}|${c.pos.map(e=>e.toFixed(6)).join(`,`)}`;if(l.has(u))return l.get(u);let d=i.props[s.prop],f=W[d.hand],h=D(r),g=ms(r,i,h,{prop:s.prop},!0),_=e=>p(m(g.R),t(e,g.pos)),v=e=>_(h.P[r.index[`${f}${e}`]]),y=[[v(`Hand`),v(`HandMiddle1`),.022],[v(`HandIndex1`),v(`HandPinky1`),.012]],b=Pt(a,o),x=Object.entries(a.objects).filter(([e,t])=>e===o||t.ancestors?.includes(o)),S=[0,1,0],C=t=>{let r={pos:e(c.pos,n(S,t)),R:c.R},i=-1/0;for(let[,e]of x){let t={...e,...q(r,hs(b,e))};for(let[e,n,r]of y)i=Math.max(i,kt(t,e,n,r))}return i},w=C(0),T=d.hand===`l`?1:-1,E=w>-.002,O=0,k=E?.15:.04,A=E?T:-T;if(E||C(A*k)>-.002)for(let e=0;e<12;e++){let e=(O+k)/2;C(A*e)>-.002===E?O=e:k=e}else O=0;let j={pos:e(c.pos,n(S,A*(E?k:O))),R:c.R};return l.set(u,j),j}function Pt(e,t){let n=e.objects[t];return n?{pos:n.pos,R:n.R}:Object.values(e.objects).find(e=>e.ancestors?.[0]===t)?.ancestorPoses?.[0]??{pos:[0,0,0],R:[[1,0,0],[0,1,0],[0,0,1]]}}function Ft(e,t){let n=Math.min(1,Math.max(0,(t-e)/4));return n*n*(3-2*n)}let It=(r,i,a)=>({pos:e(r.pos,n(t(i.pos,r.pos),a)),R:i.R}),Lt=[`schema`,`fps`,`frames`,`label`,`keys`,`anchors`,`props`,`fits`,`objects`,`generators`,`checks`,`fix`,`use`,`channels`,`detourRadius`,`search`,`goals`,`flow`,`idle`],Rt=[`f`,`set`,`step`,`src`],zt=[`skill`,`args`,`f0`],Bt={world:[`p`],body:[`p`],bone:[`bone`,`offset`],segment:[`from`,`to`,`t`,`tMin`,`offset`,`reach`],object:[`object`,`local`,`socket`,`grasp`],landmark:[`name`],prop:[`prop`,`point`,`offset`],person:[`person`,`bone`,`offset`]},Vt=[`hand`,`origin`,`points`],Ht=[`side`,`anchor`,`effector`,`offset`,`orient`],Ut={hinge:[`pivot`,`axis`,`channel`],carry:[`prop`,`at`,`along`,`visible`,`level`,`grab`,`release`,`keys`,`fall`,`grasp`,`mass`],switch:[`visible`,`level`],follow:[`follow`,`offset`,`aim`,`visible`,`level`],free:[`keys`,`fall`,`visible`,`level`]},Wt=[`at`,`onto`,`bounce`,`tumble`,`v`],Gt=[`f`,`pos`,`rot`,`aim`,`roll`,`lean`],Kt=[`type`,`f0`,`steps`,`step`,`period`,`swing`,`halfWidth`,`lift`,`heading`,`turnDistance`,`pelvisHigh`,`pelvisLow`,`firstStep`,`armSwing`,`vary`],qt=[`contacts`,`orient`,`below`,`clearance`,`appear`,`supports`,`allow`,`limits`,`mouthPoints`],Jt={contacts:[`name`,`point`,`target`,`tol`,`when`],orient:[`name`,`from`,`to`,`along`,`minCos`,`when`],below:[`name`,`joint`,`ref`,`min`,`when`],clearance:[`name`,`part`,`object`,`min`,`frames`],appear:[`name`,`channel`,`joint`,`anchor`,`tol`],supports:[`object`,`part`,`tol`],allow:[`part`,`object`,`depth`]},Yt=[`name`,`near`,`above`,`moved`,`still`,`speed`,`tol`,`by`,`at`,`mode`,`after`,`before`],Xt=[`name`,`distance`,`position`,`frames`,`every`],Zt=[`joint`,`bone`,`prop`,`anchor`,`landmark`,`object`,`person`],Qt=e=>e.flatMap(e=>[`${e}_l`,`${e}_r`]),$t=new Set([`root`,...Qt([`foot`,`knee`,`hand`,`elbow`])]),en=new Set(Qt([`knee`,`elbow`])),tn=new Set([`yaw`,`pitch`,`roll`,`spine_yaw`,`spine_pitch`,`spine_roll`,`head_yaw`,`head_pitch`,`head_roll`,`arm_rest`,`arm_hang`,...Qt([`fyaw`,`fpitch`,`armw`,`wtwist`,`wflex`,`aswing`,`abend`])]),nn=new Set(Qt([`fingers`])),rn=[`relaxed`,`fist`,`grip`,`cigarette`,`point`,`spherical`],an=[`power`,`pinch`,`hook`,`palm`,`spherical`],on={power:`grip`,pinch:`cigarette`,hook:`grip`,palm:`relaxed`,spherical:`spherical`},sn={power:[0,0,.02],hook:[0,0,.02],pinch:[.025,0,.01],palm:[-.045,0,.015],spherical:[-.015,0,.035]},cn=[[/^(armw)_[lr]$/,0,1,`0 FK, 1 IK to the hand target`],[/^arm_hang$/,0,1,`1 = a free FK forearm hangs plumb`],[/^w_.+_[lr]$/,0,1,`hand-target weight; a hand's sum <= 1`],[/^wtwist_[lr]$/,-85,85,`twist about the forearm, deg`],[/^wflex_[lr]$/,-70,70,`flex about the knuckle line, deg`],[/^fpitch_[lr]$/,-60,90,`toe-down pitch, deg: + lifts the heel (toes flat), - lifts the toes`],[/^(spine_pitch|head_pitch)$/,-120,120,`degrees, + bends forward`],[/^(spine_roll|head_roll)$/,-90,90,`degrees`]],ln=e=>cn.find(([t])=>t.test(e))??null,un={yaw:`yaw 0 faces +Z and + turns toward +X`,fyaw:`foot heading, as yaw`,pole:`body-frame direction (root yaw): +z forward, +x its left, +y up`,foot:`ANKLE target, world m (standing 0.087 X Bot, 0.105 Y Bot)`,hand:`WRIST target, world m`,root:`hips joint, world m (standing 1.03 X Bot, 0.99 Y Bot)`};un.root,`${un.yaw}`,un.foot,un.fyaw,`${un.pole}`,un.hand,`${un.pole}`;let dn=[[[`frames`,`when`,`range`,`f`,`from`,`within`,`during`,`at`],[`when`,`frames`,`at`,`f`]],[[`max`,`maxDist`,`distance`,`tolerance`,`limit`],[`tol`,`min`]]];function fn(e,t){for(let[n,r]of dn){let i=n.includes(e)&&r.find(n=>t.includes(n)&&n!==e);if(i)return`; did you mean "${i}"?`}return H(e,t,Math.max(2,Math.floor(e.length/3)))}let pn=e=>Array.isArray(e)&&e.length===3&&e.every(Number.isFinite),V=e=>JSON.stringify(e)?.slice(0,60);function mn(e,t){let n=Array.from({length:e.length+1},(e,n)=>[n,...Array(t.length).fill(0)]);for(let e=1;e<=t.length;e++)n[0][e]=e;for(let r=1;r<=e.length;r++)for(let i=1;i<=t.length;i++)n[r][i]=Math.min(n[r-1][i]+1,n[r][i-1]+1,n[r-1][i-1]+(e[r-1]===t[i-1]?0:1));return n[e.length][t.length]}function H(e,t,n=Math.max(3,Math.floor(e.length/3))){let r=null,i=1/0;for(let n of t){let t=mn(e,n);t<i&&(r=n,i=t)}return r&&i<=n?`; did you mean "${r}"?`:``}let hn={pelvis:`Hips`,hip:`Hips`,hips:`Hips`,root:`Hips`,chest:`Spine2`,upperchest:`Spine2`,torso:`Spine1`,belly:`Spine`,abdomen:`Spine`,neck:`Neck`,head:`Head`},gn={hand:`Hand`,wrist:`Hand`,foot:`Foot`,ankle:`Foot`,toe:`ToeBase`,toes:`ToeBase`,ball:`ToeBase`,arm:`Arm`,upperarm:`Arm`,forearm:`ForeArm`,lowerarm:`ForeArm`,elbow:`ForeArm`,shoulder:`Shoulder`,clavicle:`Shoulder`,thigh:`UpLeg`,upleg:`UpLeg`,upperleg:`UpLeg`,leg:`Leg`,lowerleg:`Leg`,shin:`Leg`,calf:`Leg`,knee:`Leg`},_n={l:`Left`,left:`Left`,r:`Right`,right:`Right`},vn=e=>e.toLowerCase().replace(/[\s_.-]+/g,``);function yn(e,t){let n=t.toLowerCase();return e.names.find(e=>e.toLowerCase()===n)??null}function bn(e,t){let n=vn(t);if(hn[n])return yn(e,hn[n]);let r=t.toLowerCase().trim(),i=/^(l|r|left|right)[\s_.-]+(.+)$/.exec(r)??/^(left|right)(.+)$/.exec(r),a=/^(.+?)[\s_.-]+(l|r|left|right)$/.exec(r)??/^(.+?)(left|right)$/.exec(r),[o,s]=i?[i[1],i[2]]:a?[a[2],a[1]]:[];return o?yn(e,`${_n[o]}${gn[vn(s)]??vn(s)}`):null}function xn(e,t){if(typeof t!=`string`||!t)return{bone:null,hint:``};if(e.index[t]!==void 0)return{bone:t,hint:``};let n=t.replace(/^mixamorig\d*:/i,``),r=yn(e,t)??yn(e,n)??bn(e,n);return r?{bone:r,hint:``}:{bone:null,hint:H(n,e.names)}}function Sn(e,t,n,r){return!t||t.index[r]!==void 0||(e(`${n} names unknown bone "${r}"${H(String(r),t.names)}`),!1)}function Cn(e,t,n,r,{groups:i=!1}={}){let a=Object.keys(t?.objects??{});if(!a.length||a.includes(r))return!0;let o=U(t,r);return o.length&&i?!0:(o.length?e(`${n} names "${r}", a group (Empty) with no body of its own; its parts are ${o.join(`, `)}. Name one part`):e(`${n} names object "${r}", which is not in the scene${H(String(r),[...new Set([...a,...a.flatMap(e=>t.objects[e].ancestors??[]),...Object.keys(t.others??{})])])}`),!1)}function wn(e,t,n,r,i){let[a,o]=String(i).split(/:(.*)/s);if(a===`joint`)return Sn(t,n,r,o);if(a===`anchor`)return e.anchors?.[o]?!0:t(`${r} "${i}" names no anchor`);if(a===`prop`){let[n,a]=(o??``).split(`.`);return e.props?.[n]?.points?.[a]||a===`origin`?!0:t(`${r} "${i}" names no prop point`)}t(`${r} "${i}" must be joint:<Bone>, prop:<prop>.<point> or anchor:<name>`)}let Tn=new WeakMap;function En(e){let t=e?.objects??{};if(Tn.has(t))return Tn.get(t);let n={};for(let[e,r]of Object.entries(t))(r.ancestors??[]).forEach((t,i)=>{let a=r.ancestorPoses?.[i];n[t]??={pos:a?.pos??[0,0,0],R:a?.R??[[1,0,0],[0,1,0],[0,0,1]],ancestors:r.ancestors.slice(i+1),children:[]};let o=i?r.ancestors[i-1]:e;n[t].children.includes(o)||n[t].children.push(o)});return Tn.set(t,n),n}let Dn={"x-bot-tpose":{model:`x-bot-tpose`,units:`cm`,mesh:`Beta_Surface`,bones:[{name:`Hips`,parent:-1,localPos:[-7727290721959434e-21,104.27487182617188,1.5543158054351807],localQuat:[.006458545995471647,0,0,.9999791433743129],worldPos:[-7727290721959434e-21,104.27487182617188,1.5543158054351807],worldQuat:[.006458545995471649,0,0,.9999791433743129]},{name:`Spine`,parent:0,localPos:[-42558306661763523e-26,10.182436647133528,-383026943495679e-29],localQuat:[-.0801557216468605,0,-2.4806808368112424e-7,.9967823535191669],worldPos:[-7727716305026052e-21,114.45645899708516,1.6858405330789255],worldQuat:[-.07371628519129939,1.6021591284630476e-9,-2.4806290981795794e-7,.9972792534177845]},{name:`Spine1`,parent:1,localPos:[0,10.00267477114727,3552713678800501e-30],localQuat:[6162975822039155e-48,-49630836753181624e-40,5211237859084076e-38,1],worldPos:[-2780995758607294e-21,124.35042288428505,.21513282040928483],worldQuat:[-.07371628519129939,1.6021591284630476e-9,-2.480629098179579e-7,.9972792534177845]},{name:`Spine2`,parent:2,localPos:[113754220886636e-28,9.322090642186126,2.823681128916178e-7],localQuat:[.01288561985057818,-4771173867766652e-25,-3.7024045453006494e-8,.9999169769541195],worldPos:[18291488873430703e-22,133.57119939581565,-1.1555073459065734],worldQuat:[-.06085960369636199,-4.799512607436978e-9,-2.849511007796114e-7,.9981463362843349]},{name:`Neck`,parent:3,localPos:[41474883340816403e-26,16.865321474300316,55067062021407764e-30],localQuat:[-693889390390723e-32,-8271806125530272e-39,5190558343770245e-38,1],worldPos:[11433183430736414e-21,150.31158623305856,-3.204535645093104],worldQuat:[-.060859603696361994,-4.799512607436981e-9,-2.8495110077961134e-7,.9981463362843349]},{name:`Head`,parent:4,localPos:[-1.0756747747233872e-7,9.341923497302815,2.841040809958507],localQuat:[-693889390390723e-32,-36734198463196485e-56,-10339757656912817e-40,1],worldPos:[1671650538423477e-20,159.92947501239226,-1.5195243864816557],worldQuat:[-.060859603696362,-4.7995126074369775e-9,-2.849511007796113e-7,.9981463362843349]},{name:`HeadTop_End`,parent:5,localPos:[9.856974393531148e-7,20.962781993760927,10.122935199433357],localQuat:[9020562075079394e-32,-4963083675318166e-39,-3143286327701505e-38,1],worldPos:[298931534294587e-19,181.96684091859,5.981578858112073],worldQuat:[-.060859603696361904,-4.799512607437009e-9,-2.8495110077961134e-7,.9981463362843349]},{name:`RightShoulder`,parent:3,localPos:[-4.569970073784302,11.195844421252815,-.8066318609761822],localQuat:[-.48443063216376725,.5709636753028516,-.52616363833282,-.4030871739493107],worldPos:[-4.569961889633833,144.58610943784936,-3.316387285708532],worldQuat:[.45900076979447035,-.5378833301202727,.5599368183567869,.4318223893631366]},{name:`RightArm`,parent:7,localPos:[39968028886505635e-31,10.838175719528936,-2842170943040401e-29],localQuat:[-.024616241704842487,.002562291621043601,-.10349841681885355,.9943216547083585],worldPos:[-15.162807207946486,144.06130443716168,-5.548485110836629],worldQuat:[.4999999123070338,-.5000002750229358,.49999979787788545,.5000000147920203]},{name:`RightForeArm`,parent:8,localPos:[-9.964063352185804e-9,27.841518023440628,0],localQuat:[-17109853453384572e-39,-8816207631167156e-54,2938735877055719e-54,1],worldPos:[-43.004325231382126,144.06132057494256,-5.548489180140211],worldQuat:[.4999999123070338,-.5000002750229358,.49999979787788545,.5000000147920203]},{name:`RightHand`,parent:9,localPos:[2285859917883215e-20,28.328838348366197,4472538876143517e-21],localQuat:[6229948061854065e-38,2350988701644575e-53,-19852334701272682e-39,1],worldPos:[-71.33316357974921,144.0613325226415,-5.548470451934339],worldQuat:[.4999999123070338,-.5000002750229358,.49999979787788545,.5000000147920203]},{name:`RightHandMiddle1`,parent:10,localPos:[-2368732377888705e-20,9.532501220730282,3985569264841615e-20],localQuat:[-9.781740202927786e-7,4908535656919963e-25,-.0005018059094885016,.9999998740949284],worldPos:[-80.86566480049744,144.061298192282,-5.5484955291275435],worldQuat:[.5002502631148101,-.4997497980010115,.4997483431213557,.5002513440251519]},{name:`RightHandMiddle2`,parent:11,localPos:[-8881784197001252e-30,3.700000000000017,0],localQuat:[3986107263256567e-37,1323489458050882e-38,.0008498714387871692,.9999996388592037],worldPos:[-84.56566293763908,144.0613075740096,-5.544782705328028],worldQuat:[-.4998253593741713,.5001747659318385,-.5001733119713432,-.4998264415205831]},{name:`RightHandMiddle3`,parent:12,localPos:[-25224267119483557e-29,2.9500000000000455,-1.5411302456413978e-8],localQuat:[-5680000760156732e-38,-3970467145889554e-38,-.00032184201183913403,.9999999482088584],worldPos:[-87.51566222254714,144.06131507131767,-5.54683672237727],worldQuat:[-.4999863107405839,.5000138752279871,-.5000124209192871,-.49998739241899415]},{name:`RightHandMiddle4`,parent:13,localPos:[1127986593019159e-28,2.9466287337241255,7.20586967872805e-9],localQuat:[1.5498268359238913e-7,.0009288141658670982,.00016639448508219264,.9999995548084492],worldPos:[-90.46229095218658,144.06132253675315,-5.546991694384452],worldQuat:[.49943854746932836,-.4996323747246579,.5005598653243383,.5003683130007397]},{name:`RightHandRing1`,parent:10,localPos:[-1.8651332219504173,9.103592315445724,.043069650068900955],localQuat:[6.963165823826897e-8,10850652509911485e-27,.0001558292973731088,.9999999878586124],worldPos:[-80.43675564821785,144.01826884807926,-7.413605017339948],worldQuat:[-.4999220263552031,.5000781487660197,-.5000777412794597,-.4999220592937387]},{name:`RightHandRing2`,parent:15,localPos:[-4440892098500626e-30,3.379273006312431,-2842170943040401e-29],localQuat:[75254050543037e-37,13234890125559426e-39,-.00022151509096650696,.9999999754655319],worldPos:[-83.81602849026015,144.01827033659325,-7.414658689507339],worldQuat:[.5000327889464959,-.4999673962237241,.4999669887298773,.5000328217947667]},{name:`RightHandRing3`,parent:16,localPos:[26290081223123707e-29,2.889684678650127,8202221124520293e-25],localQuat:[16000775075877487e-39,13234889885055366e-39,-.00011280510431914317,.9999999936375041],worldPos:[-86.70571314402952,144.01827160815185,-7.4142794883596075],worldQuat:[.5000891846393266,-.4999109867917636,.499910579294214,.5000892174416297]},{name:`RightHandRing4`,parent:17,localPos:[4716227408607665e-28,2.638815675202565,5682068149326369e-25],localQuat:[-1.339645129809642e-7,.00014607500499735936,-.0007992862081225936,.9999996699017618],worldPos:[-89.34452865122908,144.01827276927736,-7.41333786395688],worldQuat:[.5004155000832223,-.4994381238192544,.4995836834200939,.5005617154884825]},{name:`RightHandIndex1`,parent:10,localPos:[2.25983507131116,9.1082835183702,.5178621137028756],localQuat:[-6.059340578197898e-7,9953529057906979e-26,-.00016426752705445157,.9999999865079061],worldPos:[-80.4414477277744,143.54347484170384,-3.288636902648485],worldQuat:[.5000817363528871,-.4999184374449017,.49991735444845625,.5000824447930416]},{name:`RightHandIndex2`,parent:19,localPos:[-843769498715119e-29,3.7000000000000313,0],localQuat:[-47359322790488744e-39,-3970467069997446e-38,.00025564424489899254,.9999999673230096],worldPos:[-84.14144752826542,143.54348146979163,-3.2874218624291216],worldQuat:[-.4999539187402696,.5000462641269495,-.5000451813116483,-.49995462745726243]},{name:`RightHandIndex3`,parent:20,localPos:[-3423927807943983e-28,2.8500000000000227,-8893152880773414e-25],localQuat:[-13914829894769055e-38,-39704669556009313e-39,-8792197348428728e-20,.9999999961348633],worldPos:[-86.99144748059157,143.54348657664548,-3.287943125263367],worldQuat:[-.49999788186225497,.5000023052590155,-.5000012223814067,-.4999985904840416]},{name:`RightHandIndex4`,parent:21,localPos:[7900347043232614e-28,2.7721906128530236,5114202394906897e-25],localQuat:[8.659859370723384e-8,.0010026920012318767,8705832638285169e-20,.9999994935146673],worldPos:[-89.76363809337116,143.54349154251554,-3.2879626838817946],worldQuat:[.49945279532966436,-.4995441931072631,.5005458853548749,.5004561129854466]},{name:`RightHandThumb1`,parent:10,localPos:[2.6818519535315546,2.464805039121785,1.5739839631865493],localQuat:[.2532844925861686,.06161827244019283,-.22821025705768283,.9380672859078498],worldPos:[-73.79796992221794,142.48734898334772,-2.866619447484665],worldQuat:[.6789718782579114,-.19747746019729454,.5123797682139857,.48730566831705546]},{name:`RightHandThumb2`,parent:23,localPos:[-1887379141862766e-29,4.188985472990834,-2842170943040401e-29],localQuat:[-.002315749257559554,-.0011704489169351957,.009300110231568928,.9999533865657887],worldPos:[-77.01316043621617,140.61457222434876,-.9423386824772395],worldQuat:[.6765749035040036,-.20553967786615746,.5156358706838127,.4838589563451013]},{name:`RightHandThumb3`,parent:24,localPos:[-14210854715202004e-30,3.416271152352966,0],localQuat:[-.0016647383095556567,-.0008077810781858194,.006919747685379302,.9999743462849742],worldPos:[-79.66799955910238,139.08658379209294,.5702713693484767],worldQuat:[.6747462864925903,-.21146538352949726,.5180821304204447,.4812387625412099]},{name:`RightHandThumb4`,parent:25,localPos:[-13822276656583199e-30,2.5749773944199115,0],localQuat:[.005774853769703311,-.11750920454458612,-.048743708782553366,.9918579982867299],worldPos:[-81.68681626684736,137.9345819337196,1.6783234442112593],worldQuat:[.7432186106607572,-.23041213116196285,.41233882500284114,.47382807005935446]},{name:`RightHandPinky1`,parent:10,localPos:[-3.8062774279108913,8.076676225678526,.4867036950552688],localQuat:[4670719286902752e-21,2.4304698046602542e-9,.0005203629947084559,.9999998646002598],worldPos:[-79.40983953257197,143.5746349351307,-9.354749239786173],worldQuat:[.499741997111188,-.5002580522008165,.500262248259257,.49973743155597233]},{name:`RightHandPinky2`,parent:27,localPos:[-8881784197001252e-30,3.6000000000000654,0],localQuat:[4870027600139235e-37,529395781586262e-37,.0008462316753971022,.9999996419459117],worldPos:[-83.0098375822885,143.57460339401808,-9.358496377738998],worldQuat:[.49931848396687806,-.5006807705888573,.500684962782278,.49931391486245963]},{name:`RightHandPinky3`,parent:28,localPos:[-53290705182007514e-30,2.1000000000000085,-2.116385644512775e-8],localQuat:[1859605644060166e-37,-39704674301868344e-39,-.000496777829639297,.9999998766058863],worldPos:[-85.1098297375345,143.5745850175554,-9.364236374919699],worldQuat:[.49956714946047176,-.5004326584550336,.5004368529177667,.4995625824392057]},{name:`RightHandPinky4`,parent:29,localPos:[12789769243681803e-29,2.1157810760144855,1.4407362414203817e-8],localQuat:[-6.420659236909123e-8,.0015694812069815289,-4128528174330759e-20,.9999987675113715],worldPos:[-87.22560761141007,143.5745664664587,-9.367917366015948],worldQuat:[.4988017359417918,-.4996273949534528,.5011996406746996,.5003680791387806]},{name:`LeftShoulder`,parent:3,localPos:[4.570446071555214,11.195644630655181,-.80662723793772],localQuat:[.48442267749711376,.5709704717068556,-.5261616291252289,.40308972946685606],worldPos:[4.570454255590543,144.58590649477904,-3.316358019038552],worldQuat:[-.4589930047913493,-.5378899562605334,.5599354515710177,-.4318241616452356]},{name:`LeftArm`,parent:31,localPos:[-17763568394002505e-31,10.837705310376135,0],localQuat:[-.02460635908641656,-.002561394687342173,.10350452783474903,.9943212655210948],worldPos:[15.162823921302898,144.06131195632307,-5.548478497344785],worldQuat:[.5000007152647227,.4999994684527458,-.5000001196887698,.4999996965928613]},{name:`LeftForeArm`,parent:32,localPos:[8881784197001252e-30,27.841521654197066,-2842170943040401e-29],localQuat:[9612242362214153e-38,13234889800848425e-39,-2646977960169688e-38,1],worldPos:[43.00434557545168,144.06126546354233,-5.548455563844584],worldQuat:[.5000007152647227,.4999994684527458,-.5000001196887698,.4999996965928612]},{name:`LeftHand`,parent:33,localPos:[5438735050766752e-21,28.328834533709,-5.564248510836478e-7],localQuat:[-626962918533632e-37,-3970466940254537e-38,-26469779601696903e-39,1],worldPos:[71.333180109117,144.06121871342071,-5.548437667671921],worldQuat:[.5000007152647227,.4999994684527458,-.5000001196887698,.4999996965928613]},{name:`LeftHandThumb1`,parent:34,localPos:[-2.681731448380733,2.466144851985561,1.5761928025685847],localQuat:[.25309644676633125,-.06132323271522458,.2273527590163699,.9383455508133752],worldPos:[73.79932012001562,142.48502080725694,-2.866704767040701],worldQuat:[.6787362281261902,.19828588307841977,-.512706326258779,.48696207677299685]},{name:`LeftHandThumb2`,parent:35,localPos:[-6439293542825908e-30,4.18709445950184,2842170943040401e-29],localQuat:[-.002067032129087745,.0010427308040619535,-.008213407893731095,.9999635893478415],worldPos:[77.01712188965233,140.61296547844856,-.9502182343475976],worldQuat:[.6766109604816792,.2054209516609806,-.5155696739884216,.48392949077136155]},{name:`LeftHandThumb3`,parent:36,localPos:[5440092820663267e-30,3.4183883081631072,5684341886080802e-29],localQuat:[-.0015313589649970979,.0007452552330187441,-.006073119605284013,.9999801081784668],worldPos:[79.67313738956891,139.08416322654404,.5642862932655532],worldQuat:[.6749931166817439,.2106761779810745,-.5176795589715161,.48167179145825934]},{name:`LeftHandThumb4`,parent:37,localPos:[1482147737874584e-29,2.580581439610723,0],localQuat:[.005153407131366997,.12268277993699259,.04165153492193895,.9915581311958046],worldPos:[81.69402483309037,137.93008677758235,1.6794194947408763],worldQuat:[.7440625177876421,.2372081988172148,-.41152267473439397,.46984287609461295]},{name:`LeftHandIndex1`,parent:34,localPos:[-2.2598629283141953,9.109308911975873,.5180343214524328],localQuat:[6.665759936749852e-7,-29380348585960335e-27,-44076517660685066e-21,.9999999990284081],worldPos:[80.44248629452144,143.5431683499078,-3.28856742622205],worldQuat:[-.4999790098166289,-.5000211729545403,.5000224907507637,-.4999773245691765]},{name:`LeftHandIndex2`,parent:39,localPos:[-6217248937900877e-30,3.6999999999999886,-5684341886080802e-29],localQuat:[3.407213613716439e-7,3580919473408932e-26,.0001050981787351313,.9999999944771283],worldPos:[84.14248628039589,143.54315723870565,-3.2888905447059225],worldQuat:[.5000317287407754,.49996845295921,-.49997011163297345,.5000297028895362]},{name:`LeftHandIndex3`,parent:40,localPos:[-7172040739078511e-28,2.849999999999966,5684341886080802e-29],localQuat:[3.347505650683504e-7,-22176925334093855e-27,-6624910485486997e-20,.9999999978054721],worldPos:[86.99248625886426,143.54314673774508,-3.2885403736491297],worldQuat:[-.49999877255511554,-.5000014111400769,.5000034044318037,-.499996411845041]},{name:`LeftHandIndex4`,parent:41,localPos:[858424442640171e-27,2.7748656282368103,0],localQuat:[1.0594603907920247e-8,-.0003775632648206345,-13069537623522493e-21,.9999999286375816],worldPos:[89.76735188694607,143.54313465598005,-3.288567098859584],worldQuat:[.4998034244662043,.4998191246365437,-.5001986899385569,.5001786182186978]},{name:`LeftHandMiddle1`,parent:34,localPos:[-3660436011010404e-20,9.53340148931592,-3254047146583616e-20],localQuat:[4.084735851900896e-7,-12926721325528698e-27,-3164640698382836e-20,.9999999994991691],worldPos:[80.86658159844058,144.06123533397496,-5.548393210486787],worldQuat:[-.49998509605784175,-.5000150871851532,.5000161468752833,-.499983668904545]},{name:`LeftHandMiddle2`,parent:43,localPos:[-26645352591003757e-31,3.6999999999999744,-2842170943040401e-29],localQuat:[-1.6358384175735772e-7,-34673461654660564e-28,2119614093572879e-20,.9999999997753484],worldPos:[84.5665815912097,144.0612261326968,-5.548624346151146],worldQuat:[-.49999561254479435,-.500004571111091,.5000054672459868,-.49999434899615885]},{name:`LeftHandMiddle3`,parent:44,localPos:[-28697044740511046e-28,2.9500000006246694,-2842170943040401e-29],localQuat:[-4.275899279477681e-7,-884662274908745e-26,20689502181930057e-21,.9999999997858808],worldPos:[87.51658159123295,144.06121976164127,-5.548683572757025],worldQuat:[.5000057434864292,.4999944401365919,-.4999949087122563,.5000049075508173]},{name:`LeftHandMiddle4`,parent:45,localPos:[-8713030297258229e-27,2.952863459443819,-14210854715202004e-29],localQuat:[-2.6759425390821073e-8,-.0010241407152540845,279847572642705e-19,.9999994751761864],worldPos:[90.46944505000428,144.06121590958617,-5.548620670295296],worldQuat:[.499507394771273,.49946812318416767,-.5004927166472543,.5005307154158994]},{name:`LeftHandRing1`,parent:34,localPos:[1.865081040398466,9.104528720799948,.04302823471783768],localQuat:[6.248995762954407e-7,39451643110942306e-28,6313277301958232e-21,.9999999999798759],worldPos:[80.43771029434576,144.0181759602736,-7.413511224335965],worldQuat:[.5000041843415268,.49999599935162675,-.4999972754894557,.5000025407699983]},{name:`LeftHandRing2`,parent:47,localPos:[-4440892098500626e-30,3.1500000002318416,0],localQuat:[-19345585602378175e-38,0,4235164736267838e-37,1],worldPos:[83.58771029427925,144.01816676318134,-7.413468855985753],worldQuat:[.5000041843415268,.4999959993516267,-.4999972754894557,.5000025407699983]},{name:`LeftHandRing3`,parent:48,localPos:[2643289183090758e-21,2.9500000001810776,5.209859352817148e-7],localQuat:[-8757673760813274e-38,1323488980045867e-38,12839125457873287e-48,1],worldPos:[86.53771029421749,144.01815762904647,-7.413431820978903],worldQuat:[.5000041843415268,.4999959993516267,-.4999972754894557,.5000025407699983]},{name:`LeftHandRing4`,parent:49,localPos:[-23274330810174604e-21,2.644316435477009,-14607575451464072e-22],localQuat:[-2.7316933950438635e-7,.0004934373218353761,16047023509748673e-21,.9999998781310067],worldPos:[89.18202672913526,144.0181513691535,-7.413372979877784],worldQuat:[.5002587275852554,.5002347713372702,-.49974233369355947,.49976392320219065]},{name:`LeftHandPinky1`,parent:34,localPos:[3.806263215177876,8.077778818742132,.4868770149745387],localQuat:[-4975537953269548e-21,1.0178097892277194e-8,.0020448023965822897,.9999979093770162],worldPos:[79.41096125008677,143.57432960785403,-9.354694407950916],worldQuat:[.5010195873847003,.4989785133401168,-.498974180943756,.5010235354085267]},{name:`LeftHandPinky2`,parent:51,localPos:[-3552713678800501e-30,3.6000000000000796,2842170943040401e-29],localQuat:[6.285466836481913e-7,-13240964884135178e-26,-.00021066000242779323,.9999999778109838],worldPos:[83.01093113307373,143.5743594144743,-9.339968896109413],worldQuat:[.5009147763033496,.49908373336086237,-.499080029188825,.5009180955410079]},{name:`LeftHandPinky3`,parent:52,localPos:[23803181647963356e-29,2.1000000536000414,5684341886080802e-29],localQuat:[1.0560541723515637e-7,-70842344057535595e-28,-6708211165014274e-20,.9999999977499896],worldPos:[85.11091705114045,143.57437416212716,-9.332263779357556],worldQuat:[.500881348481691,.4991172819497667,-.4991136834189969,.5008845621759181]},{name:`LeftHandPinky4`,parent:53,localPos:[11048939541069558e-28,2.12553132909224,0],localQuat:[2.2034709579345264e-7,-.0007833987784531817,-.0002953855857226705,.9999996495167691],worldPos:[87.23643510003559,143.57438864025644,-9.324750154134009],worldQuat:[.5003428461990634,.49887255781562256,-.4996539623833923,.5011278531377935]},{name:`RightUpLeg`,parent:0,localPos:[-8.207789844904983,-6.771764137627812,-1.5122187689572422],localQuat:[2.7047671429134563e-9,.010356475588677583,.9999463702685825,2.6115275064568443e-7],worldPos:[-8.207797572195703,97.52320568879888,-.04524648166077205],worldQuat:[4.391377782486676e-9,.0038980599621582274,.9999924025353709,2.611298350175175e-7]},{name:`RightLeg`,parent:55,localPos:[6.19727558159866e-9,44.37146193723237,-16631140908884845e-29],localQuat:[-.03809137752293408,-816315213450579e-24,2.1414893185802513e-8,.9992742601300231],worldPos:[-8.207820750122902,53.15309218889458,.3006761286643062],worldQuat:[-4.658818794361865e-9,-.03419585714041286,.9994151506528081,2.396960478727042e-7]},{name:`RightFoot`,parent:56,localPos:[-5.538291603102152e-9,44.52781411684904,18207657603852567e-30],localQuat:[.45974006194132166,3725814189701004e-38,-1.2432846936543718e-7,.8880535318583977],worldPos:[-8.207842064194724,8.729415862750969,-2.742876350631811],worldQuat:[1.1031211398837241e-7,.4291034315577479,.903255359809859,3.3926052346823936e-7]},{name:`RightToeBase`,parent:57,localPos:[0,13.816866143314009,6217248937900877e-30],localQuat:[.335241516804437,7023902526244116e-39,-1.1130089220595614e-7,.9421322228915896],worldPos:[-8.207849224193675,.0007420744116171107,7.96768124078066],worldQuat:[1.6990321486208088e-7,.7070808667083169,.7071326947149592,3.8318019813577996e-7]},{name:`RightToe_End`,parent:58,localPos:[-17763568394002505e-31,9.278120542404253,4288670113483661e-30],localQuat:[-3388360082792521e-36,.01160806788231821,11912203416067455e-38,.9999326241102645],worldPos:[-8.207852022918948,6202649694084369e-20,17.245801758262132],worldQuat:[-.008208274430290369,.7070332309537893,.7070850529927397,-.0082074595446575]},{name:`LeftUpLeg`,parent:0,localPos:[8.207788043566438,-6.771802818529409,-1.5122137762205135],localQuat:[2.8191483378909245e-9,.01036795967915088,.9999462512615453,2.718950400523856e-7],worldPos:[8.207780316275715,97.52316694663398,-.04524198897490894],worldQuat:[4.575136162088223e-9,.00390954458172457,.9999923577013424,2.7187116164010067e-7]},{name:`LeftLeg`,parent:60,localPos:[6.468269475590205e-9,44.37138978410286,37414515929867775e-30],localQuat:[-.0381124510208252,-8172602612650612e-25,2.142781333519525e-8,.9992734566059407],worldPos:[8.207756184976574,53.15313355499844,.30169921260475335],worldQuat:[-4.888837197809569e-9,-.034205455626157316,.9994148221861352,2.504235506344471e-7]},{name:`LeftFoot`,parent:61,localPos:[-5.547779124981389e-9,44.52784595754781,-2220446049250313e-29],localQuat:[.4597486071334844,-3725832749974238e-39,-1.1668517803347452e-7,.8880491080108168],worldPos:[8.207733916824536,8.729483932050528,-2.742708742345943],worldQuat:[1.147816207643381e-7,.4291034480907216,.9032553519556492,3.412529433141002e-7]},{name:`LeftToeBase`,parent:62,localPos:[17763568394002505e-31,13.81686480841043,-17763568394002505e-31],localQuat:[.33524149798857344,10535853714492708e-39,-1.1452079069214378e-7,.94213222958689],worldPos:[8.207726760093172,.0008113791108694812,7.967848133811215],worldQuat:[1.7340034603185666e-7,.707080865528955,.7071326958942321,3.864673509538802e-7]},{name:`LeftToe_End`,parent:63,localPos:[0,9.278136754869934,-13125351500109517e-31],localQuat:[0,-.011869165745076847,18530151004086692e-38,.9999295589712888],worldPos:[8.207723964114914,.00013129905961646127,17.245984863756057],worldQuat:[.008393248559463194,.7070310534383599,.7070828826815805,.008392846428263363]}],landmarks:{mouth:[0,158.500185546875,11.717991638183594],headEllipsoid:{centre:[-95367431640625e-19,167.56204986572266,-.15480518341064453],radii:[8.804292678833008,13.326271057128906,12.424483299255371]},headJoint:[1671650538423477e-20,159.92947501239226,-1.5195243864816557],headTop:[298931534294587e-19,181.96684091859,5.981578858112073],sole:{ball:0,tip:-.7}}},"y-bot-tpose":{model:`y-bot-tpose`,units:`cm`,mesh:`Alpha_Surface`,bones:[{name:`Hips`,parent:-1,localPos:[-6757011760782916e-21,99.79193878173828,4844740033149719e-20],localQuat:[7615435492247396e-26,-17453292503320135e-22,4363323128594654e-20,.9999999990465473],worldPos:[-6757011760782916e-21,99.79193878173828,4844740033149719e-20],worldQuat:[7615435492247396e-26,-17453292503320135e-22,4363323128594654e-20,.9999999990465473]},{name:`Spine`,parent:0,localPos:[.0008597856133863019,9.923461876522225,-1.2273349078167588],localQuat:[-.06073022898235704,5303745488019905e-37,6119354919596912e-21,.9981542161661715],worldPos:[-8672608244745512e-21,109.71540069587908,-1.227286457407732],worldQuat:[-.060730228859120175,-4391963877518737e-21,4956605444176136e-20,.9981542149520963]},{name:`Spine1`,parent:1,localPos:[0,11.731977989043784,-3552713678800501e-30],localQuat:[.0001960924550491026,4662287733052689e-29,-9316072744031837e-21,.9999999807304797],worldPos:[-.0011632832276606084,121.36083978703566,-2.649627688070991],worldQuat:[-.06053449713743436,-494801144686156e-20,4026803743853935e-20,.9981661049195772]},{name:`Spine2`,parent:2,localPos:[1931491945550423e-26,13.458836780167971,6235012506294879e-28],localQuat:[.057711730916465316,21958232674411106e-22,3285013706200134e-21,.9983332890868721],worldPos:[-.0022371547694859653,134.72103871836885,-4.27608729015328],worldQuat:[-.002827710078055566,-2.2517339024978275e-7,4363254684471116e-20,.9999960010679366]},{name:`Neck`,parent:3,localPos:[2548122992670175e-20,15.027761372130044,.8779068542021755],localQuat:[.0028277058943183713,-8816242878226746e-54,-7234126583784003e-36,.9999960020316957],worldPos:[-.003523660156014883,149.7535246254643,-3.4831824403326026],worldQuat:[-4.186445695616128e-9,-1.017924801167934e-7,4363300912729469e-20,.999999999048075]},{name:`Head`,parent:4,localPos:[2562548663041439e-21,10.32183835340436,3.1424288991299827],localQuat:[12407709188295404e-40,13234889800848435e-39,-33812794736012347e-37,1],worldPos:[-.0044224830930743595,160.07536296607333,-.3407536277174854],worldQuat:[-4.1864456956161264e-9,-1.0179248011679337e-7,43633009127294685e-21,.999999999048075]},{name:`HeadTop_End`,parent:5,localPos:[.00015451594228192873,18.474670356296826,6.63639947863672],localQuat:[693889473108785e-32,-661744490042423e-38,-10158249377722142e-36,1],worldPos:[-.005881529143937624,178.55003332101543,6.295645696100038],worldQuat:[-4.186445688677232e-9,-1.0179248011679308e-7,4363300912729468e-20,.999999999048075]},{name:`LeftShoulder`,parent:3,localPos:[6.105824524896,9.10629188078309,.757062475638635],localQuat:[-.45383506682997793,-.5448493479758866,.5511388470821386,-.4397941465761588],worldPos:[6.10279217248512,143.83199925791556,-3.5705353792059826],worldQuat:[-.4525659925709384,-.5433084112379314,.5526580275773418,-.44109987213863294]},{name:`LeftArm`,parent:7,localPos:[4884981308350689e-30,12.92228632488431,2842170943040401e-29],localQuat:[-.010451820107802023,-.0010627185813176982,.10115046260752789,.9948156683526521],worldPos:[18.75785325853472,143.5671732022338,-6.171454434247386],worldQuat:[.49997802104022876,.5000219779933275,-.4999777974641308,.5000222015503447]},{name:`LeftForeArm`,parent:8,localPos:[3.059411390893274e-9,27.404682159441652,-2842170943040401e-29],localQuat:[-10173904404505293e-36,6815758221109549e-42,13234889800848457e-39,1],worldPos:[46.162535310990236,143.5695947084291,-6.171442183765884],worldQuat:[.49997802104022876,.5000219779933275,-.4999777974641308,.5000222015503447]},{name:`LeftHand`,parent:9,localPos:[-12820418092474029e-21,27.614463806173113,2.3361064904747764e-7],localQuat:[-3397640826470449e-36,-18680226235633534e-43,52939559203393736e-39,1],worldPos:[73.77699900937316,143.57203451753804,-6.171417016006557],worldQuat:[.49997802104022876,.5000219779933275,-.4999777974641308,.5000222015503447]},{name:`LeftHandRing1`,parent:10,localPos:[2.216630558205681,12.147010274148471,-.009996226470974534],localQuat:[5.56794949511021e-7,-3.067028835367668e-9,4777353976607745e-21,.9999999999884335],worldPos:[85.92400934395243,143.5831040665074,-8.388042142881478],worldQuat:[.49998068669282075,.5000193094968617,-.4999756886184537,.500024313263825]},{name:`LeftHandRing2`,parent:11,localPos:[0,3.601188659840844,0],localQuat:[-2306826384812874e-21,1861861504887997e-26,-8071095064110734e-21,.999999999964768],worldPos:[89.52519798990681,143.58341826103262,-8.388006124364832],worldQuat:[.4999754975118546,.5000244982373176,-.49997857087756026,.5000214312542258]},{name:`LeftHandRing3`,parent:12,localPos:[-7140954494389007e-28,3.3073120118204997,22737367544323206e-29],localQuat:[-32584970000852863e-37,-2067951531382572e-40,-6352747104407258e-37,1],worldPos:[92.83250998771065,143.5837220743585,-8.388026432414401],worldQuat:[.49997549751185466,.5000244982373176,-.49997857087756026,.5000214312542258]},{name:`LeftHandRing4`,parent:13,localPos:[-241726316367874e-19,3.660118102908669,16886514032421474e-21],localQuat:[8486035821629225e-21,.007396474405473809,.0007707649842267434,.9999723486255485],worldPos:[96.49262807680708,143.5840414103271,-8.388024734185503],worldQuat:[.5040493957792119,.5033194611486261,-.4958855340273986,.4966903097363882]},{name:`LeftHandMiddle1`,parent:10,localPos:[-18344155402694184e-21,12.77552795411546,-10320898695681535e-22],localQuat:[5.567949752588633e-7,-2.6633364343577455e-9,2.0800236288001427e-7,.9999999999998233],worldPos:[86.55252691351437,143.5731644085546,-6.171392959489538],worldQuat:[.49997840212413347,.5000215942797767,-.49997797319957754,.5000220284932966]},{name:`LeftHandMiddle2`,parent:15,localPos:[-3552713678800501e-30,3.61396789550858,-2842170943040401e-29],localQuat:[23697741078528623e-36,6203854594147698e-40,-26469779601696844e-39,1],worldPos:[90.16649479526663,143.5734797180353,-6.171389840137076],worldQuat:[.49997840212413347,.5000215942797767,-.49997797319957754,.5000220284932966]},{name:`LeftHandMiddle3`,parent:16,localPos:[28508498060020315e-21,3.4597625732240687,-8526512829121202e-29],localQuat:[1692147750009982e-35,20679515313825724e-41,264697796016969e-37,1],worldPos:[93.62625735534596,143.57378157349484,-6.171415362383127],worldQuat:[.49997840212413347,.5000215942797767,-.49997797319957754,.5000220284932966]},{name:`LeftHandMiddle4`,parent:17,localPos:[-30473889779614183e-21,3.680191040059995,-2842170943040401e-29],localQuat:[-133272714831852e-20,.004579424073341433,-.000654144096597114,.9999893004272004],worldPos:[97.30644838137125,143.57410266077613,-6.171381711981186],worldQuat:[.5019349111684311,.5026337814312043,-.4980094305837711,.4974004762403589]},{name:`LeftHandThumb1`,parent:10,localPos:[-3.002975029424964,3.7888090485209744,2.1671489778191813],localQuat:[.25189492500135036,-.05946403657250492,.2219235197808198,.940086659027326],worldPos:[77.56599819190912,141.40522033116738,-3.168440292478436],worldQuat:[.6772116616355364,.20343190021506413,-.5147394802435296,.484812432594653]},{name:`LeftHandThumb2`,parent:19,localPos:[-19984014443252818e-31,4.744970523440218,2842170943040401e-29],localQuat:[69460704858849485e-22,55796786860811154e-28,8.032883032279416e-7,.9999999999755533],worldPos:[81.24162844278966,139.28353174695044,-1.0464306132517085],worldQuat:[.6772151925776482,.20342778079987783,-.5147405038353289,.48480814210593404]},{name:`LeftHandThumb3`,parent:20,localPos:[-5984102102729594e-28,4.3821287009303775,5172751116333529e-27],localQuat:[7030691099565929e-33,7121429504040547e-33,-15407439555097887e-48,1],worldPos:[84.63615479208448,137.32403482337068,.9133179986734019],worldQuat:[.6772151925776482,.20342778079987775,-.5147405038353289,.48480814210593404]},{name:`LeftHandThumb4`,parent:21,localPos:[-51284654249972306e-22,3.459077859769903,4.03463474185628e-7],localQuat:[.009146816027813427,.1256374250101491,.07207247635857116,.9894125182879566],worldPos:[87.31565650284612,135.77728679427992,2.4602700847142343],worldQuat:[.7538118553684166,.20866722684171796,-.3911265183168664,.4850213622280347]},{name:`LeftHandIndex1`,parent:10,localPos:[-2.822044006965872,12.266617250897596,.23182516767607808],localQuat:[5.567949601161418e-7,-3.075281477739248e-9,487076476385117e-20,.9999999999879827],worldPos:[86.04363543486608,143.34129324170544,-3.34936752423002],worldQuat:[.49998073339592125,.5000192627891642,-.4999756419148924,.5000243599710406]},{name:`LeftHandIndex2`,parent:23,localPos:[-13322676295501878e-31,3.891967773631521,0],localQuat:[-30225470980600983e-37,0,-6214320207710297e-21,.999999999980691],worldPos:[89.93560319348254,143.34163280596658,-3.349327870283342],worldQuat:[.49997762610645813,.5000223698198841,-.4999787492167228,.5000212529526508]},{name:`LeftHandIndex3`,parent:24,localPos:[4071321058063404e-26,3.415161132822689,-11368683772161603e-29],localQuat:[46684518186119064e-39,-9183549615799121e-56,21175823681357537e-38,1],worldPos:[93.35076431329836,143.34193077006435,-3.3493355202090695],worldQuat:[.49997762610645813,.5000223698198841,-.4999787492167229,.5000212529526508]},{name:`LeftHandIndex4`,parent:25,localPos:[5.854722480158614e-7,3.0779876709090814,5684341886080802e-29],localQuat:[10393592226093013e-22,.004001602788212073,22779581052790166e-21,.9999919932955144],worldPos:[96.4287519724834,143.34219931663745,-3.3493430003052733],worldQuat:[.5019862492920715,.5020073437901309,-.4979631636001596,.4980272281710877]},{name:`LeftHandPinky1`,parent:10,localPos:[4.72583130478205,10.908194092064392,.22613216873591568],localQuat:[28521120612919424e-22,-3.0577730863769185e-9,.000141051374267014,.9999999900481878],worldPos:[84.68521515320175,143.3468662092345,-10.897243443373178],worldQuat:[.5000499694422342,.49995002290857044,-.49990869131728644,.5000912946652625]},{name:`LeftHandPinky2`,parent:27,localPos:[-5329070518200751e-30,4.136657059551979,2842170943040401e-29],localQuat:[-59544873416476465e-22,2.190560405006969e-9,-.0003678839387923365,.9999999323129735],worldPos:[88.82187203349876,143.3472081321215,-10.896074631422186],worldQuat:[.4998630353195086,.5001369272162459,-.5000896549937187,.49991032888321396]},{name:`LeftHandPinky3`,parent:28,localPos:[43876013933186186e-29,2.594834461051775,0],localQuat:[36591694282576963e-22,8409112616016821e-25,.00022980932036022433,.999999973587143],worldPos:[91.41670621645012,143.34745351466333,-10.897250657097295],worldQuat:[.49997978792115133,.5000202113293842,-.49997658739741363,.5000234114387793]},{name:`LeftHandPinky4`,parent:29,localPos:[-2948752353404416e-28,2.9238662720287607,-2842170943040401e-29],localQuat:[-10497831164166655e-22,.004099894588367042,-.0005750644579927158,.9999914300453439],worldPos:[94.34057247729058,143.3477086145383,-10.89723194250163],worldQuat:[.5017372856540723,.5023540149301887,-.4982094589732469,.49768210220741194]},{name:`RightShoulder`,parent:3,localPos:[-6.1056956701096174,9.10638358733786,.7570756312724551],localQuat:[.4538375688887134,-.5448472538987562,.5511399888422243,.43979272809263187],worldPos:[-6.1087279840345,143.8310253866605,-3.5705252284969617],worldQuat:[.45261579666489365,-.5432669079313696,.5526977464106563,.44105012013225253]},{name:`RightArm`,parent:31,localPos:[39968028886505635e-31,12.922286714484947,0],localQuat:[-.01045541384979725,.0010630628959740092,-.10114940861202837,.9948157373885607],worldPos:[-18.763745414580892,143.56389953844294,-6.171423447408117],worldQuat:[.5000215299672571,-.4999784691060287,.5000215360351153,.4999784630368383]},{name:`RightForeArm`,parent:32,localPos:[8.327427636345419e-9,27.404682159441318,-2842170943040401e-29],localQuat:[-3388329060809388e-36,-15146129380243438e-44,8271806125530284e-40,1],worldPos:[-46.16842747236397,143.56153906740224,-6.171423771692651],worldQuat:[.5000215299672571,-.49997846910602883,.5000215360351153,.4999784630368383]},{name:`RightHand`,parent:33,localPos:[12842842382454478e-21,27.614463806155165,15492399938921153e-21],localQuat:[-19727179218464944e-41,-7573064690121704e-44,0,1],worldPos:[-73.78289117474843,143.55914503465877,-6.17141126400836],worldQuat:[.5000215299672571,-.4999784691060287,.5000215360351153,.4999784630368383]},{name:`RightHandThumb1`,parent:34,localPos:[3.002974470681663,3.7888088905192205,2.1671490371761024],localQuat:[.25189499102226326,.05946427874712486,-.2219234615465524,.9400866397658112],worldPos:[-77.57151342274972,141.3916696607838,-3.168436839313407],worldQuat:[.6772291629398663,-.20337279117491877,.5147821060302558,.4847675236487542]},{name:`RightHandThumb2`,parent:35,localPos:[-68833827526759706e-31,4.744970470128291,2842170943040401e-29],localQuat:[8453832451066965e-21,-33376070678072775e-28,-3.9480215616198235e-7,.9999999999641884],worldPos:[-81.24677425676067,139.26933968812125,-1.0464288158806956],worldQuat:[.6772333413527709,-.20336817191604672,.5147836339017984,.48476200168592604]},{name:`RightHandThumb3`,parent:36,localPos:[-17763568394002505e-29,4.382135391768333,-4092726157978177e-27],localQuat:[-14347996972659013e-33,-5998687332454958e-33,-1387778780781447e-32,1],worldPos:[-84.64095942776746,137.30923516924733,.9133180789494266],worldQuat:[.6772333413527709,-.20336817191604672,.5147836339017984,.48476200168592604]},{name:`RightHandThumb4`,parent:37,localPos:[11177533225659175e-21,3.4590709023307085,-23241859054223823e-21],localQuat:[.00923206336161341,-.1271302153500302,-.0723276818427547,.989202397788438],worldPos:[-87.32019182850055,135.76203483248253,2.460268746809576],worldQuat:[.7545499013151971,-.2090649481886763,.389944180490974,.4846543407061524]},{name:`RightHandIndex1`,parent:34,localPos:[2.822042502024897,12.266617102415225,.23182522345669554],localQuat:[5.567922673046469e-7,-7.224337929733461e-9,-4743924457641052e-21,.9999999999885925],worldPos:[-86.04948829791044,143.32626324098368,-3.3493689108641775],worldQuat:[.5000241838181078,-.4999758222398485,.5000194389411731,.4999805530753941]},{name:`RightHandIndex2`,parent:39,localPos:[0,3.891967773619811,0],localQuat:[1695401251774016e-35,-4135903062840235e-40,602627167052741e-20,.9999999999818419],worldPos:[-89.94145605654246,143.32592367674323,-3.3493320316989923],worldQuat:[.5000211708188947,-.4999788355123435,.5000224519507367,.4999775398133359]},{name:`RightHandIndex3`,parent:40,localPos:[-51669779566054785e-28,3.4151611328221207,11368683772161603e-29],localQuat:[-13593959205239575e-36,0,0,1],worldPos:[-93.35661717635494,143.32562571266394,-3.349340831998884],worldQuat:[.5000211708188947,-.4999788355123435,.5000224519507367,.4999775398133359]},{name:`RightHandIndex4`,parent:41,localPos:[-12771572572845002e-22,3.0779876709101757,-2842170943040401e-29],localQuat:[-7290381402922513e-20,-.006709307422924062,.0013745331778991744,.9999765449936721],worldPos:[-96.4346048355366,143.3253571661075,-3.3493500406116747],worldQuat:[-.5026405594004304,.5040453607575476,-.49730671352060296,-.49596045712511533]},{name:`RightHandMiddle1`,parent:34,localPos:[16632154487439266e-21,12.775527954114011,-10320893863990932e-22],localQuat:[5.567949633221699e-7,-7.289108692409176e-9,-3.662527039828292e-7,.9999999999997777],worldPos:[-86.55841908156046,143.55804566121927,-6.171394786911078],worldQuat:[.5000219951158138,-.4999780112066047,.5000216276573223,.4999783641170999]},{name:`RightHandMiddle2`,parent:43,localPos:[8881784197001252e-31,3.6139678955093757,0],localQuat:[-33759614098155274e-37,-4135903062765133e-40,0,1],worldPos:[-90.17238696331393,143.55773035173834,-6.171392183522983],worldQuat:[.5000219951158138,-.4999780112066047,.5000216276573223,.4999783641170999]},{name:`RightHandMiddle3`,parent:44,localPos:[-29421479796454264e-21,3.4597625732165085,14210854715202004e-29],localQuat:[34003021678244415e-37,-8271806125530284e-40,-7940933880509075e-38,1],worldPos:[-93.63214952338268,143.55742849627876,-6.1714191126993185],worldQuat:[.5000219951158138,-.4999780112066047,.5000216276573223,.4999783641170999]},{name:`RightHandMiddle4`,parent:45,localPos:[28549065814509333e-21,3.680191040067001,-5684341886080802e-29],localQuat:[-5158123071926063e-20,-.006695195594000001,-.0017083442773867367,.9999761263426065],worldPos:[-97.31234054942114,143.557107408997,-6.171387912540353],worldQuat:[.504186145435319,-.5024851098604104,.49578202059538456,.49749897806327625]},{name:`RightHandRing1`,parent:34,localPos:[-2.216632303077052,12.147002761381543,-.009996270293299858],localQuat:[5.567954667309114e-7,-7.224304767843245e-9,-4803651127566965e-21,.9999999999883074],worldPos:[-85.92989472518438,143.5680950366908,-8.38804371451417],worldQuat:[.5000242136815972,-.49997579237346873,.5000194090805982,.49998058293828956]},{name:`RightHandRing2`,parent:47,localPos:[0,3.6011962892353893,-8526512829121202e-29],localQuat:[-5732183384702703e-36,0,8071083662125689e-21,.9999999999674287],worldPos:[-89.53109100054763,143.5677808414962,-8.388009160440863],worldQuat:[.5000201783188615,-.4999798280944457,.5000234444494263,.4999765472235214]},{name:`RightHandRing3`,parent:48,localPos:[-6055600465515454e-27,3.307312011785328,-11368683772161603e-29],localQuat:[32821615918857455e-37,-36734198463196485e-56,21175823681357546e-38,1],worldPos:[-92.83840299967426,143.56749228695628,-8.38803081342569],worldQuat:[.5000201783188615,-.4999798280944457,.5000234444494263,.4999765472235214]},{name:`RightHandRing4`,parent:49,localPos:[2417262415121968e-20,3.6601181029476635,-2842170943040401e-29],localQuat:[-6164489635359998e-20,-.007613221748708619,-.000300224892743606,.9999709720384515],worldPos:[-96.49852108877111,143.56717295098406,-8.388030603602541],worldQuat:[.5039317385023292,-.503652452411389,.49602123868179077,.49633651979941534]},{name:`RightHandPinky1`,parent:34,localPos:[-4.725833119502555,10.90819434071372,.2261320753251539],localQuat:[5.567949828639406e-7,-7.530380981813863e-9,-3132050030138696e-21,.99999999999494],worldPos:[-84.69106594001192,143.3320733953033,-10.897244515904388],worldQuat:[.5000233780731588,-.4999766283665967,.5000202446951756,.49997974695226793]},{name:`RightHandPinky2`,parent:51,localPos:[0,4.13665008553545,-2842170943040401e-29],localQuat:[1470867824118891e-37,8271806125977124e-40,10394374390791114e-21,.9999999999459783],worldPos:[-88.8277160097222,143.33171248313417,-10.897218653721097],worldQuat:[.5000181811018849,-.4999818257697831,.5000254416448413,.49997454952763204]},{name:`RightHandPinky3`,parent:52,localPos:[-3950617610826157e-27,2.5948410036845218,-8526512829121202e-29],localQuat:[6674018442413745e-36,1836709923259698e-55,-10428465638715964e-21,.9999999999456235],worldPos:[-91.42255700325643,143.33148608987582,-10.897256374372848],worldQuat:[.5000233951179858,-.49997661132017523,.5000202276502416,.4999797639985816]},{name:`RightHandPinky4`,parent:53,localPos:[4362732397567015e-27,2.923866272034857,5684341886080802e-29],localQuat:[-8226663950025322e-20,-.00849884034311731,.0022061069470672434,.9999614472754239],worldPos:[-94.34642326410449,143.33123099000105,-10.897237895108226],worldQuat:[-.5031095764062739,.5053508241180749,-.4968132089402998,-.4946493041680384]},{name:`LeftUpLeg`,parent:0,localPos:[9.123873709849553,-6.657188518716367,-.05540310527662988],localQuat:[20251728747483253e-21,-.006350177975401161,.9999754277247583,-.002969642442257367],worldPos:[9.12444806072654,93.13554649657034,-.05532280954860631],worldQuat:[18783520922709306e-21,-.006350171978846871,.999975297231096,-.00301328568169704]},{name:`LeftLeg`,parent:55,localPos:[-4618527782440651e-29,40.59943638074365,1170175067954915e-28],localQuat:[-.018114309191591866,.00018249364298742092,-.005920146666904975,.9998183786878614],worldPos:[9.369107731941927,52.540121711606496,-.5709414745281038],worldQuat:[-715314874584385e-19,-.02446331907456045,.9996964939479204,.00290876113461602]},{name:`LeftFoot`,parent:56,localPos:[-10658141036401503e-30,42.09902591219656,20872192862952943e-30],localQuat:[.5405498192560871,.01436647216366454,.024951033886733327,.8408192096329419],worldPos:[9.12441738143423,10.49219684921195,-2.6300976499043784],worldQuat:[-.01346031169880001,.5198601039256607,.853859207560786,-.022107600933591907]},{name:`LeftToeBase`,parent:57,localPos:[17763568394002505e-31,15.721559146548017,-1509903313490213e-29],localQuat:[.2276920621809942,-.032521469150786096,-.015365592016704316,.9730686396375217],worldPos:[9.497939603229476,3.2836504838127345,11.336461068911172],worldQuat:[.0016492651748272183,.7007886739142866,.7132730439227728,.01157925951782756]},{name:`LeftToe_End`,parent:58,localPos:[17763568394002505e-31,9.999999999999991,4.890746918562172e-9],localQuat:[3842630640765734e-25,-2181956872127079e-33,8673617379884037e-34,1],worldPos:[9.355871856763304,3.108427383452366,21.333916426176543],worldQuat:[.0016492651792767014,.700788674188371,.7132730436534856,.011579259517193808]},{name:`RightUpLeg`,parent:0,localPos:[-9.125032356625555,-6.655601347887028,-.05535274484958799],localQuat:[-17972283601838397e-21,-.006333801286546503,.9999752669985558,.0030574547301418053],worldPos:[-9.12445807483328,93.1355411499169,-.055336149820714026],worldQuat:[-19441205218510228e-21,-.006333807477113975,.9999753994199062,.00301381152055421]},{name:`RightLeg`,parent:60,localPos:[7105427357601002e-30,40.59943859177964,26645352591003757e-30],localQuat:[-.018147073530042527,-.00018249394989624574,.005920149654138874,.9998177845219228],worldPos:[-9.369160167563622,52.54009755727,-.5696263108436963],worldQuat:[7086285029500266e-20,-.024479715368387448,.9996960941175567,-.0029082503401009767]},{name:`RightFoot`,parent:61,localPos:[-24868995751603507e-30,42.09902387472938,-6217248937900877e-29],localQuat:[.5405636018310247,-.014366037304083883,-.024951225824318232,.8408103506008421],worldPos:[-9.12451164224619,10.492242047309674,-2.6301615125796047],worldQuat:[.013459958226241265,.5198600714589466,.8538592128932476,.02210837363015364]},{name:`RightToeBase`,parent:62,localPos:[3552713678800501e-30,15.721558809599616,26645352591003757e-31],localQuat:[.22770869920802236,.03211960072148292,.015257901862228916,.9730797891155923],worldPos:[-9.498060396707743,3.2836958492734345,11.33639620371245],worldQuat:[-.0013617607248561205,.700801240791512,.7132661380535977,-.011277536030161229]},{name:`RightToe_End`,parent:63,localPos:[3.1880595940947387e-9,9.999999999999998,5.687534443410414e-9],localQuat:[-3109963189751316e-25,-16439215440311446e-32,-17347234759768052e-34,1],worldPos:[-9.356269180688868,3.1086870931784847,21.33385924122101],worldQuat:[-.0013617607213487338,.7008012405696887,.7132661382715444,-.011277536030584614]}],landmarks:{mouth:[0,158.399150390625,12.493875312805176],headEllipsoid:{centre:[-16689300537109375e-21,167.32984161376953,.6807799339294434],radii:[8.005595684051514,13.133369445800781,12.374487400054932]},headJoint:[-.0044224830930743595,160.07536296607333,-.3407536277174854],headTop:[-.005881529143937624,178.55003332101543,6.295645696100038],sole:{ball:3.3,tip:2.6}}}},On=new Map;function kn(e){return Dn[e]?(On.has(e)||On.set(e,E(Dn[e])),On.get(e)):null}function An(e,t,n,r){let i=e?.people?.[t];if(!i)return null;let a=i.names.indexOf(xn(kn(i.rig),n).bone??``);if(a<0)return null;let o=(Math.max(0,Math.min(i.frames-1,Math.round(r)))*i.names.length+a)*3;return[i.P[o],i.P[o+1],i.P[o+2]]}let jn=e=>Object.entries(e?.people??{}).map(([e,t])=>`${e} (${t.name})`).join(`, `)||`none: no other character has an installed take`;function Mn(e,t){let n=new Map,r=(t,r)=>{let i=e?.people?.[t],a=i&&xn(kn(i.rig),r).bone;a&&n.set(t,new Set([...n.get(t)??[],a]))},i=(e,t)=>{if(typeof e==`string`){let n=/^person:([^.]+)(?:\.(.+))?$/.exec(e);n&&(n[2]||t===`from`)&&r(n[1],n[2]??`Hips`)}else if(Array.isArray(e))e.forEach(e=>i(e));else if(e&&typeof e==`object`){e.kind===`person`&&r(e.person,e.bone);for(let[t,n]of Object.entries(e))i(n,t)}};return i(t),n.size?Object.fromEntries([...n].map(([t,n])=>{let r=e.people[t],i=[...n],a=r.names.length,o=[];for(let e=0;e<r.frames;e++)for(let t of i){let n=(e*a+r.names.indexOf(t))*3;o.push(r.P[n],r.P[n+1],r.P[n+2])}return[t,{name:r.name,rig:r.rig,frames:r.frames,names:i,P:o}]})):null}let Nn=.045,Pn=[[1,0,0],[0,1,0],[0,0,1]],Fn=(e,i)=>t(e,n(i,r(e,i)));function In(e){if(!e)return-1;let t=[0,1,2].reduce((t,n)=>e[n]>e[t]?n:t,0);return[0,1,2].filter(e=>e!==t).every(t=>e[t]<Nn)?t:-1}let Ln=(e,t)=>e===`cylinder`&&!!t&&t[1]<Nn&&Math.min(t[0],t[2])>=Nn&&Math.abs(t[0]-t[2])<.1*t[0],Rn=(e,t)=>e?.objects?.[t]?.grasp??e?.others?.[t]?.grasp??null;function zn(e,t){let n=[],r=(t,r)=>Object.entries(Rn(e,t)??{}).forEach(([e,i])=>n.push({...i,name:r+e,owner:t}));r(t,``);for(let i of U(e,t)){i!==t&&r(i,`${i}.`);let{half:a,renderer:o}=e.objects[i],s=In(a);s>=0?n.push({name:i,owner:i,at:[0,a[1],0],axis:Pn[s],radius:Math.min(...a.filter((e,t)=>t!==s))}):Ln(o,a)&&n.push({name:i,owner:i,at:[0,a[1],0],rim:a[0],normal:Pn[1]})}return n}let Bn=(e,t,n)=>zn(e,t).find(e=>e.name===n);function Vn(t,n){let r=e=>e?s(p(n.R,e)):void 0;return{p:e(n.pos,p(n.R,t.at??[0,0,0])),axis:r(t.axis),from:r(t.from),thumb:r(t.thumb),radius:t.radius,...t.rim?{rim:t.rim,normal:r(t.normal)}:{}}}function Hn(r,o){let c=Fn(t(o,r.p),r.normal),l=a(c)>1e-6?s(c):s(Fn(Math.abs(r.normal[1])<.9?[0,1,0]:[1,0,0],r.normal));return{p:e(r.p,n(l,r.rim)),axis:s(i(r.normal,l))}}function Un(r,i){let a;for(let o of r){let r=p(m(o.R),t(i,e(o.pos,p(o.R,[0,o.half[1],0])))),s=[0,1,2].map(e=>Math.abs(r[e])-o.half[e]),c=[0,1,2].reduce((e,t)=>s[t]>s[e]?t:e,0),l=Math.max(...s);(!a||l<a.gap)&&(a={gap:l,n:n([o.R[0][c],o.R[1][c],o.R[2][c]],r[c]<0?-1:1)})}return a?.n}function Wn(e,n){let i=t(n,e.p);return e.rim?Math.hypot(r(i,e.normal),a(Fn(i,e.normal))-e.rim):a(e.axis?Fn(i,e.axis):i)}function Gn(t,o,c,l,u=0){let d=t===`Right`,f=e=>d?e:n(e,-1);if(o.axis&&c!==`palm`&&!(o.from&&a(Fn(o.axis,o.from))<.001)){if(o.from){let e=f(o.from),t=s(Fn(o.axis,e)),a=t=>o.thumb?r(t,o.thumb):r(i(t,e),l),c=a(t)>=a(n(t,-1))?t:n(t,-1);return qn(i(c,e),c,e)}let t=Kn(l,o.axis),a=p(_(u),[d?1:-1,0,0]),c=Math.abs(t[1]),m=o.thumb??Fn(e(n([0,1,0],1-c),n(a,c)),t),h=s(Fn(o.axis,t)),g=r(h,m)>=0?h:n(h,-1);return qn(t,g,i(t,g))}let m=f(o.from??n(l,-1)),h=Kn(l,m);return qn(h,i(m,h),m)}function Kn(e,t){let n=Fn(e,t);return s(a(n)>.001?n:Fn(Math.abs(t[1])<.9?[0,1,0]:[1,0,0],t))}let qn=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]];function Jn(e,n){let r=W[n],i=e.restWorldPos,o=e.index;return a(t(i[o[`${r}ForeArm`]],i[o[`${r}Arm`]]))+a(t(i[o[`${r}Hand`]],i[o[`${r}ForeArm`]]))}let Yn=new Set([`body`]);function Xn(e,t,n,r,i,a){let o=e.spec.anchors?.[t];if(!o)throw Error(`MOTION_SPEC_INVALID: unknown anchor "${t}"`);let s=tr(e,o,t,n,r,i,a),c=Yn.has(o.kind)&&e.keepOut;return c?We(c.body,e.sk,n,We(c.body,e.sk,n,s,Ge,c.skip),Ge,c.skip):s}let Zn=[`r`,`l`],Qn={l:`left`,r:`right`};function $n(e,t,n,{s:r,diag:i,c:a},o){let s=i.frame,c=()=>e.restingAt?.(t.object,s)??e.objectsAt(s)[t.object]??En(e.scene)[t.object],l=e.objectPose&&s!==void 0?e.objectHand(t.object,s):null;if(o&&l&&Zn.indexOf(l)>Zn.indexOf(o))throw Error(`MOTION_SPEC_INVALID: anchor "${n}" is on "${t.object}", which the ${Qn[l]} hand holds at f${s}; the right arm is solved before the left, so the right hand cannot follow it. Carry "${t.object}" in the right hand (a right-hand prop) and put the left hand on it, or give the right hand another anchor`);let u=!e.objectPose||s===void 0||l===o?c():er(e,n,t.object,s,()=>e.objectPose(t.object,s,{s:r,diag:i,c:a}));if(!u)throw Error(`MOTION_SPEC_INVALID: anchor "${n}" names unknown object "${t.object}"`);return u}function er(e,t,n,r,i){if((e.nest??0)>=16)throw Error(`MOTION_SPEC_INVALID: anchor "${t}" on "${n}" at f${r} reads a pose that needs this frame solved again (an anchor and carry loop); anchor the hand on a point that does not ride on the hand it moves`);e.nest=(e.nest??0)+1;try{return i()}finally{e.nest--}}function tr(r,i,o,s,c,l,u){let{sk:d}=r,f=d.index;switch(i.kind){case`world`:return i.p.slice();case`body`:return e(l.root??s.P[f.Hips],p(_(l.yaw??0),i.p));case`bone`:return e(s.P[f[i.bone]],i.offset??[0,0,0]);case`segment`:{let r=s.P[f[i.from]],o=s.P[f[i.to]],c=i.offset??[0,0,0],l=i=>e(e(r,n(t(o,r),i)),c);if(!i.reach)return l(i.t??.5);let u=s.P[f[`${W[i.reach]}Arm`]],p=.95*Jn(d,i.reach);for(let e=i.t??.45;e>=(i.tMin??.15);e-=.01)if(a(t(l(e),u))<=p)return l(e);return l(i.tMin??.15)}case`object`:{if(i.grasp&&i.local===void 0)return rr(r,o,s,c,l,u).p;let t=$n(r,i,o,{s,diag:c,c:l},u);return e(t.pos,p(t.R,i.local??[0,0,0]))}case`landmark`:return A(s,f.Head,d.landmarks[i.name]??(()=>{throw Error(`MOTION_SPEC_INVALID: unknown landmark "${i.name}"`)})());case`prop`:{let t=nr(r,i,o,c?.frame)??c.props?.[i.prop]?.[i.point];if(!t)throw Error(`MOTION_SPEC_INVALID: anchor "${o}" needs prop ${i.prop}.${i.point}, which is held by an arm solved later`);return e(t,i.offset??[0,0,0])}case`person`:{let t=An(r.scene,i.person,i.bone,c?.frame??0);if(!t)throw Error(`MOTION_SPEC_INVALID: anchor "${o}" names person "${i.person}" bone "${i.bone}", which the scene does not have (people: ${jn(r.scene)})`);return e(t,i.offset??[0,0,0])}default:throw Error(`MOTION_SPEC_INVALID: anchor "${o}" has unknown kind "${i.kind}"`)}}function nr(n,r,i,a){let o=n.spec.props?.[r.prop]?.hand;if(!o||a===void 0||!n.solvedAt||!n.carrySpan)return null;let s=null;for(let[e,t]of Object.entries(n.spec.objects??{})){if(!K(t)||n.spec.props?.[t.prop]?.hand!==o)continue;let[r,i]=n.carrySpan(t);if(a>=r&&a<=i)return null;let c=a<r?r:i;(!s||Math.abs(a-c)<Math.abs(a-s.at))&&(s={id:e,at:c})}if(!s)return null;let c=er(n,i,s.id,s.at,()=>n.solvedAt(s.at).diag.props?.[r.prop]?.[r.point]),l=n.objectPose(s.id,s.at),u=n.objectPose(s.id,a);return c&&l&&u?e(u.pos,p(u.R,p(m(l.R),t(c,l.pos)))):null}function rr(i,o,c,l,u,d){let f=i.spec.anchors[o],m={s:c,diag:l,c:u},h=e=>$n(i,{...f,object:e},o,m,d),g=f.local&&(t=>e(t.pos,p(t.R,f.local)))(h(f.object));if(i.sockets??=new Map,!i.sockets.has(o)){let e=zn(i.scene,f.object),t=i.sk.index,n=g??c.P[t[d?`${W[d]}Arm`:`Hips`]],r=f.socket?Bn(i.scene,f.object,f.socket):e.map(e=>({k:e,d:Wn(Vn(e,h(e.owner)),n)})).sort((e,t)=>e.d-t.d)[0]?.k;i.sockets.set(o,r??null)}let _=i.sockets.get(o),v=_?Vn(_,h(_.owner)):{p:g??h(f.object).pos};if(_?.rim)v=ir(i,o,d,_,h(_.owner),v,g??c.P[i.sk.index[d?`${W[d]}Arm`:`Hips`]]);else if(g)v.p=g;else if(v.axis&&v.radius>0){let o=Math.abs(sn[f.grasp]?.[2]??0),l=v.radius-o,u=c.P[i.sk.index[`${W[d]??`Right`}Arm`]],p=t(u,v.p),m=t(p,n(v.axis,r(p,v.axis)));l>1e-4&&a(m)>1e-6&&(v.p=e(v.p,n(s(m),l)))}return!v.axis&&!v.from&&(v.from=Un(U(i.scene,f.object).map(e=>({...h(e),half:i.scene.objects[e].half})),v.p)),v}function ir(e,n,r,i,a,o,s){e.rims??=new Map;let c=(r?null:[...e.rims.keys()].find(e=>e.startsWith(`${n}|`)&&!e.endsWith(`|`)))??`${n}|${r??``}`;if(!e.rims.has(c)){let n=Hn(o,s),r=e=>p(m(a.R),e);e.rims.set(c,{at:r(t(n.p,a.pos)),axis:r(n.axis)})}return Vn(e.rims.get(c),a)}function ar(t,r,i,a){let{sk:o}=t;for(let[s,c]of Object.entries(t.spec.props??{})){if(c.hand!==r)continue;let t=z(o,i,W[r]),l=n(c.origin.map(e=>i.P[o.index[e]]).reduce((t,n)=>e(t,n),[0,0,0]),1/c.origin.length),u={};for(let[r,[i,a,o]]of Object.entries(c.points??{}))u[r]=e(l,e(n(t.d,i),e(n(t.l,a),n(t.n,o))));u.origin=l,u.axes=t,(a.props??={})[s]=u}}let or={joint:`joint:<Bone>`,bone:`bone:<Bone>`,prop:`prop:<prop>.<point>`,anchor:`anchor:<name>`,landmark:`landmark:mouth`,object:`object:<id>[.center|.top|.base]`,person:`person:<id>.<Bone>`};function sr(e){let t=/^([a-z]+):(.+)$/s.exec(String(e));if(!t)return null;let[,n,r]=t;if(n===`prop`){let[e,t]=r.split(`.`);return{kind:n,name:e,point:t??``}}if(n===`object`){let e=/^(.+)\.(center|top|base)$/s.exec(r);return{kind:n,name:e?e[1]:r,point:e?e[2]:null}}if(n===`person`){let e=r.lastIndexOf(`.`);return{kind:n,name:e>0?r.slice(0,e):r,point:e>0?r.slice(e+1):``}}return{kind:n,name:r,point:null}}function cr(t,n){let r=[1/0,1/0,1/0],i=[-1/0,-1/0,-1/0];for(let n of t)for(let t of[-1,1])for(let a of[0,2])for(let o of[-1,1]){let s=e(n.pos,p(n.R,[t*n.half[0],a*n.half[1],o*n.half[2]]));for(let e=0;e<3;e++)r[e]=Math.min(r[e],s[e]),i[e]=Math.max(i[e],s[e])}let a=[(r[0]+i[0])/2,(r[1]+i[1])/2,(r[2]+i[2])/2];return n===`top`?[a[0],i[1],a[2]]:n===`base`?[a[0],r[1],a[2]]:a}function lr(e,t,n){let r=e.frames?.[t-(e.f0??0)],i=sr(n);if(!r||!i)return null;let{s:a,diag:o,c:s}=r,c=e.ctx.sk;switch(i.kind){case`joint`:case`bone`:{let{bone:e}=xn(c,i.name);return e?a.P[c.index[e]].slice():null}case`prop`:{let e=o.props?.[i.name]?.[i.point];return Array.isArray(e)?e.slice():null}case`anchor`:return e.ctx.spec.anchors?.[i.name]?Xn(e.ctx,i.name,a,{...o,frame:t},s):null;case`landmark`:return c.landmarks?.[i.name]?A(a,c.index.Head,c.landmarks[i.name]):null;case`object`:{let n=Lo(e,i.name,t);return n?cr(n,i.point):null}case`person`:return An(e.ctx.scene,i.name,i.point,t);default:return null}}function ur(e,n,r,i){let o=e=>{let t=sr(e);return t?.kind===`object`&&t.point===null?t:null},s=o(i)?[r,o(i)]:o(r)?[i,o(r)]:null;if(s){let t=lr(e,n,s[0]),r=Lo(e,s[1].name,n);return t&&r?Math.max(0,Math.min(...r.map(e=>B(t,e)))):null}let c=lr(e,n,r),l=lr(e,n,i);return c&&l?a(t(c,l)):null}function dr(e,{sk:t,scene:n},r,i){if(typeof r!=`string`||!r)return``;let a=r.replace(/\.(center|top|base)$/,``),[o,s]=r.split(`.`),c=t&&(i.includes(`joint`)||i.includes(`bone`))?xn(t,r).bone:null,l=[i.includes(`object`)&&(n?.objects?.[a]||U(n,a).length)&&`object:${r}`,c&&`${i.includes(`joint`)?`joint`:`bone`}:${c}`,i.includes(`anchor`)&&e.anchors?.[r]&&`anchor:${r}`,i.includes(`prop`)&&e.props?.[o]&&(s===`origin`||e.props[o].points?.[s])&&`prop:${r}`,i.includes(`landmark`)&&r===`mouth`&&`landmark:mouth`].filter(Boolean);return l.length?`; did you mean ${l.map(e=>`"${e}"`).join(` or `)}?`:``}function fr(e,t,{sk:n,scene:r},i,a,o=Zt){let s=sr(a),c=`${i} "${a}"`;if(!s||!o.includes(s.kind))return t(`${c} must be ${o.map(e=>or[e]).join(`, `)}${s?``:dr(e,{sk:n,scene:r},a,o)}`),!1;if(s.kind===`joint`||s.kind===`bone`){let{bone:e,hint:r}=n?xn(n,s.name):{bone:s.name};return e||t(`${c}: unknown bone${r}`),!!e}if(s.kind===`prop`){let n=!!(e.props?.[s.name]&&(s.point===`origin`||e.props[s.name].points?.[s.point]));return n||t(`${c}: spec.props has no point ${s.name}.${s.point}`),n}if(s.kind===`anchor`)return e.anchors?.[s.name]||t(`${c}: no anchor "${s.name}" in spec.anchors${H(s.name,Object.keys(e.anchors??{}))}`),!!e.anchors?.[s.name];if(s.kind===`landmark`)return s.name!==`mouth`&&t(`${c}: the only landmark is "mouth"`),s.name===`mouth`;if(s.kind===`person`){let e=!!An(r,s.name,s.point,0);return e||t(`${c}: no person "${s.name}" with joint "${s.point}" (people with an installed take: ${jn(r)})`),e}return Cn(t,r,c,s.name,{groups:!0})}let pr=[0,1,0];function mr(e,n,r=`up`,i){if(Array.isArray(r))return s(r);if(r===`face`){let{sk:t}=e.ctx;return s(j(t,e.frames[n-(e.f0??0)].s,t.index.Head,[0,0,1]))}let o=r===`up`?null:lr(e,n,r),c=o?t(o,i):pr;return a(c)>1e-9?s(c):pr}function hr(t,n,r){let i=t.f0??0,a=Math.max(i,Math.min(i+t.frames.length-1,r)),o=lr(t,a,n.follow);if(!o)return null;let s=C(xe(pr,mr(t,a,n.aim,o)));return{pos:e(o,p(s,n.offset??[0,0,0])),R:s}}function gr(e,t){let n=t.length,r=Array(n).fill(0);for(let i=1;i<n-1;i++){let n=e[i]-e[i-1],a=e[i+1]-e[i],o=(t[i]-t[i-1])/n,s=(t[i+1]-t[i])/a;if(o*s>0){let e=2*a+n,t=a+2*n;r[i]=(e+t)/(e/o+t/s)}}return r}function _r(e,t,n,r){if(e.length===1||r<=e[0])return t[0];if(r>=e[e.length-1])return t[t.length-1];let i=0;for(;r>e[i+1];)i++;let a=e[i+1]-e[i],o=(r-e[i])/a,s=o*o,c=s*o;return(2*c-3*s+1)*t[i]+(c-2*s+o)*a*n[i]+(-2*c+3*s)*t[i+1]+(c-s)*a*n[i+1]}let vr=/^(spine|head)_(yaw|pitch|roll)$|^(aswing|abend)_[lr]$/,yr={spine:0,head:2,arms:3},br=e=>/^(spine|head)_/.test(e)?[`l`,`r`]:/^(aswing|abend)_([lr])$/.test(e)?[e.slice(-1)]:[],xr=e=>/^spine_/.test(e)?`spine`:/^head_/.test(e)?`head`:/^(aswing|abend)_[lr]$/.test(e)?`arms`:null;function Sr(e){if(e===!1)return{smooth:!1,lag:{spine:0,head:0,arms:0}};let t=e??{},n=t.lag===!1?0:t.lag,r=typeof n==`number`?{spine:n,head:n,arms:n}:{...yr,...n??{}};return{smooth:t.smooth!==!1,lag:r}}function Cr(e,t){let n=t.length,r=Array(n).fill(0);for(let i=1;i<n-1;i++)t[i]!==t[i-1]&&t[i]!==t[i+1]&&(r[i]=(t[i+1]-t[i-1])/(e[i+1]-e[i-1]));for(let i=0;i<n-1;i++){let n=e[i+1]-e[i],a=Math.min(t[i],t[i+1]),o=Math.max(t[i],t[i+1]),s=Math.min(6,.15*(o-a));for(let c=0;c<12;c++){let c=0;for(let s=1;s<16;s++){let l=_r([e[i],e[i+1]],[t[i],t[i+1]],[r[i],r[i+1]],e[i]+n*s/16);c=Math.max(c,l-o,a-l)}if(c<=s+1e-9)break;r[i]*=.7,r[i+1]*=.7}}return r}let wr=e=>Array.isArray(e)?`vec`:e&&typeof e==`object`?`map`:`num`;function Tr(e,t,n=Sr()){let r=new Map;for(let t of[...e].sort((e,t)=>e.f-t.f))for(let[e,n]of Object.entries(t.set??{}))r.has(e)||r.set(e,[]),r.get(e).push({f:t.f,value:n,step:t.step?.includes(e)});let i={},a={};for(let[e,o]of r){let r=[...new Map(o.map(e=>[e.f,e])).values()],s=wr(r[0].value),c=s===`vec`?r[0].value.length:0,l=s===`num`?[r.map(e=>e.value)]:s===`vec`?Array.from({length:c},(e,t)=>r.map(e=>e.value[t])):[...new Set(r.flatMap(e=>Object.keys(e.value)))].map(e=>({name:e,y:r.map(t=>t.value[e]??0)})),u=r.map(e=>e.f),d=n.smooth&&vr.test(e),f=n.lag[xr(e)]??0,p=e=>f>0?e-f*Math.min(1,(t-1-e)/(2*f)):e,m=(e,t=!0)=>{let n=d&&t?Cr(u,e):gr(u,e);return i=>{let a=t?p(i):i;for(let t=1;t<r.length;t++)if(r[t].step&&a>=u[t-1]&&a<u[t])return e[t-1];return _r(u,e,n,a)}},h=Array(t);if(s===`num`){let e=m(l[0]);for(let n=0;n<t;n++)h[n]=e(n)}else if(s===`vec`){let e=l.map(m);for(let n=0;n<t;n++)h[n]=e.map(e=>e(n))}else{let e=l.map(({name:e,y:t})=>[e,m(t)]);for(let n=0;n<t;n++)h[n]=Object.fromEntries(e.map(([e,t])=>[e,t(n)]))}if(i[e]=h,s===`num`&&(d||f>0)&&br(e).length){let n=m(l[0],!1);a[e]=Array.from({length:t},(e,t)=>n(t))}}for(let[e,t]of Object.entries(a)){let n=br(e).map(e=>i[`armw_${e}`]).filter(Boolean);n.length&&(i[e]=i[e].map((e,r)=>{let i=Math.min(1,4*Math.max(0,...n.map(e=>e[r])));return e+(t[r]-e)*i*(2-i)}))}return i}let Er=(e,t)=>Object.fromEntries(Object.entries(e).map(([e,n])=>[e,n[t]])),Dr=.2,Or=e=>Math.round(e*1e5)/1e5,kr=(e,t)=>2*Math.acos(Math.min(1,Math.abs(ge(e,t)))),Ar=(r,i,a)=>r.lean>0&&r.aim&&a>0?e(r.pos,n(t(s(r.aim),be(i,[0,1,0])),a)):r.pos;function jr(t,r){let a=s(i(t,Math.abs(t[1])<.9?[0,1,0]:[1,0,0])),o=i(t,a),c=[t];for(let i of[r/2,r])for(let r=0;r<8;r++){let l=2*Math.PI*r/8,u=Math.min(i,45)*Math.PI/180,d=e(n(a,Math.cos(l)),n(o,Math.sin(l)));c.push(s(e(n(t,Math.cos(u)),n(d,Math.sin(u)))))}return c}function Mr(e){let t=e[0].map(e=>({total:e.cost,from:-1})),n=[t];for(let r=1;r<e.length;r++)t=e[r].map(t=>{let i={total:1/0,from:-1};return e[r-1].forEach((e,a)=>{let o=n[r-1][a].total+Dr*kr(e.q,t.q)**2;o<i.total&&(i={total:o,from:a})}),{total:i.total+t.cost,from:i.from}}),n.push(t);let r=[];for(let i=e.length-1,a=t.reduce((e,n,r)=>n.total<t[e].total?r:e,0);i>=0;a=n[i][a].from,i--)r[i]=a;return r}function Nr(e,t,n){let r=(Math.sqrt(5)-1)/2,i=t,a=n,o=a-r*(a-i),s=i+r*(a-i),c=e(o),l=e(s);for(let t=0;t<12;t++)c<l?(a=s,s=o,l=c,o=a-r*(a-i),c=e(o)):(i=o,o=s,c=l,s=i+r*(a-i),l=e(s));return(i+a)/2}function Pr(e,t,n){let r=e.map((e,r)=>{if(e.aim===void 0)return{fixed:_e(e.rot)};let i=_e(t(r)),a=e=>{let t=me(xe(s(be(i,[0,1,0])),e),i);return n=>me(Se(e,n),t)};if(e.lean>0&&n)return{leans:jr(s(e.aim),e.lean).map(a),roll:e.roll};let o=a(s(e.aim));return e.roll!==void 0||!n?{fixed:e.roll?o(e.roll):o(0)}:{rolled:o}}),i=e=>e.roll===void 0?Array.from({length:24},(e,t)=>t*15-180):[e.roll],a=r.map((e,t)=>e.fixed?[{q:e.fixed,cost:0,r:0}]:e.leans?e.leans.flatMap(r=>i(e).map(i=>{let a=r(i);return{q:a,cost:n(t,a),r:i,rolled:r,fixedRoll:e.roll!==void 0}})):Array.from({length:24},(r,i)=>{let a=i*15-180,o=e.rolled(a);return{q:o,cost:n(t,o),r:a}})),o=Mr(a),c=a.map((e,t)=>e[o[t]]);for(let[e,t]of r.entries())t.leans&&(r[e]=c[e].fixedRoll?{fixed:c[e].q}:{rolled:c[e].rolled});let l=[],u=null;for(let[t,i]of r.entries()){if(e[t].aim===void 0){l.push(e[t].rot),u=i.fixed;continue}let r=(i.fixed??i.rolled(Nr(e=>{let r=i.rolled(e),a=[c[t-1],c[t+1]].filter(Boolean);return n(t,r)+a.reduce((e,t)=>e+Dr*kr(t.q,r)**2,0)},c[t].r-15/2,c[t].r+15/2))).map(Or);u&&ge(r,u)<0&&(r=r.map(e=>-e)),l.push(r),u=_e(r)}return l}let Fr=1e3,Ir=.03,Lr=Array.from({length:72},(e,t)=>2*Math.PI*t/72-Math.PI);function Rr(e,t){let n=t.length,r=Array.from(t);for(let t=0;t<n;t++)for(let i=t+1;i<=Math.min(t+2,n-1);i++){let a=e[i][t-i+2]/e[t][2];for(let r=t;r<=Math.min(t+2,n-1);r++)e[i][r-i+2]-=a*e[t][r-t+2];r[i]-=a*r[t]}for(let t=n-1;t>=0;t--){for(let i=t+1;i<=Math.min(t+2,n-1);i++)r[t]-=e[t][i-t+2]*r[i];r[t]/=e[t][2]}return r}function zr(i,a,o,s=null){let c=o.length,l=o.slice();for(let e=1;e<c;e++)l[e]=l[e-1]+Math.atan2(Math.sin(l[e]-l[e-1]),Math.cos(l[e]-l[e-1]));for(let o=0;o<4&&c>2;o++){let o=l.map(()=>new Float64Array(5)),u=new Float64Array(c);l.forEach((e,t)=>{let n=i(t,e),r=i(t,e+Ir),a=i(t,e-Ir);o[t][2]=Math.max(.05,(r+a-2*n)/(Ir*Ir)),u[t]=-(r-a)/(2*Ir)});let d=l.map((e,t)=>a(t,e)),f=l.map((e,r)=>n(t(a(r,e+Ir),a(r,e-Ir)),1/(2*Ir)));for(let i=0;i+2<c;i++){let a=[1,-2,1],c=e(t(d[i],n(d[i+1],2)),d[i+2]),l=s?2*Fr*s[i]:2*Fr;for(let e=0;e<3;e++){u[i+e]-=l*a[e]*r(c,f[i+e]);for(let t=0;t<3;t++)o[i+e][t-e+2]+=l*a[e]*a[t]*r(f[i+e],f[i+t])}}let p=Rr(o,u);l.forEach((e,t)=>{l[t]=e+Math.max(-.6,Math.min(.6,p[t]))})}return l}function Br(e,n,r){let i=e.map((e,t)=>r(t,e)),o=e=>e>=0&&e+1<i.length?a(t(i[e+1],i[e])):0;return e.slice(1).map((e,t)=>t).filter(t=>o(t)>.05&&o(t)>2*Math.max(o(t-1),o(t+1))&&w(n(t,e[t]),n(t+1,e[t+1]))>45)}function Vr(e,t,n,i){let a=i.length,o=45*Math.PI/180,s=[],c=[];for(let r=0;r<n;r++)s.push(i.map(t=>e(r,t))),c.push(i.map(e=>t(r,e)));let l=s[0].slice(),u=[];for(let e=1;e<n;e++){let t=Array(a),n=new Int32Array(a);for(let i=0;i<a;i++){let u=1/0;for(let t=0;t<a;t++){let a=Math.acos(Math.max(-1,Math.min(1,r(c[e-1][t],c[e][i])))),s=l[t]+10*a*a+(a>o?400*(a-o)**2:0);s<u&&(u=s,n[i]=t)}t[i]=u+s[e][i]}u.push(n),l=t}let d=Array(n);d[n-1]=l.indexOf(Math.min(...l));for(let e=n-1;e>0;e--)d[e-1]=u[e-1][d[e]];return d.map(e=>i[e])}function Hr(e,t,n,r,i=Lr){let a=zr(e,t,n);if(!Br(a,r,t).length)return a;let o=Vr(e,r,a.length,i),s=zr(e,t,o),c=Br(s,r,t);if(!c.length)return s;let l=zr(e,t,o,new Float64Array(Math.max(0,s.length-2)).map((e,t)=>c.some(e=>e>=t-4&&e<=t+1+4)?16:1));return Br(l,r,t).length?a:l}function Ur(e){return e.map((t,n)=>{let r=0,i=0;for(let t=Math.max(0,n-6);t<=Math.min(e.length-1,n+6);t++){let a=Math.exp(-((t-n)**2)/8);r+=a*e[t],i+=a}return r/i})}function Wr(e,t,n,r){let i=n.map(n=>{let{s:i,held:a}=r(n);return dt(e,i,t,a)}),a=Hr((e,t)=>i[e].at(t).cost,(e,t)=>i[e].at(t).elbow,i.map(e=>e.softmin()),(e,t)=>i[e].at(t).pv);return new Map(n.map((e,t)=>[e,i[t].at(a[t]).pv]))}let Gr=ht.filter(e=>e[0].startsWith(`torso`)),Kr=ht.find(e=>e[0]===`hand_l`)[3];function qr(i,o,s){let l=t(s,o),u=c(r(t(i,o),l)/Math.max(r(l,l),1e-12),0,1);return a(t(i,e(o,n(l,u))))}function Jr(t){let n=[];for(let r of[-1,1])for(let i of[0,2])for(let a of[-1,1])n.push(e(t.pos,p(t.R,[r*t.half[0],i*t.half[1],a*t.half[2]])));return n}function Yr(e,t,n,r,i=()=>null){let a=e.index,o=Object.entries(t.ctx.spec.objects??{}).filter(([,e])=>ds(e)).map(([e])=>e);for(let e=0;e<t.frames.length;e++){let{s,diag:c}=t.frames[e],l=s.P,u=[];for(let[e,t]of Object.entries(c.props??{}))for(let[n,r]of Object.entries(t))Array.isArray(r)&&n!==`origin`&&u.push([`prop_${e}`,r,.004]);for(let[t,i,o,s]of Gr){let c=new Map;for(let[e,t,n]of u)c.set(e,Math.max(c.get(e)??-1/0,s+n-qr(t,l[a[i]],l[a[o]])));for(let[i,a]of c)a>n.body&&r(`BODY`,`${i} into ${t}`,e,a,n.body)}let d=2*Kr-_t(l[a.LeftHand],l[a.LeftHandMiddle3],l[a.RightHand],l[a.RightHandMiddle3]),f=Math.max(i(`hand_l`,`hand_r`)??0,n.hands);if(d>f&&r(`SELF`,`hands overlap`,e,d,f),!o.length)continue;let p=t.objectsAt(e);for(let t of o){if(!p[t]?.half)continue;let i=Jr(p[t]);for(let[a,s]of Object.entries(p)){if(o.includes(a)||!s.half||s.ignore)continue;let c=-Math.min(...i.map(e=>B(e,s)));c>n.object&&r(`OBJECT`,`${t} into ${a}`,e,c,n.object)}}}}let Xr=[[/^Hips$/,`hips`],[/^(Spine|Spine1)$/,`torso_lo`],[/^Spine2$/,`torso_hi`],[/^Neck$/,`neck`],[/^Head/,`head`],[/Shoulder$/,`shoulder`],[/UpLeg$/,`thigh`],[/Leg$/,`shin`],[/(Foot|Toe)/,`foot`],[/ForeArm$/,`farm`],[/Arm$/,`uarm`],[/Hand$/,`hand`]];function Zr(e,t){let n=e.names[t]??``,r=/^Left/.test(n)?`_l`:/^Right/.test(n)?`_r`:``,i=Xr.find(([e])=>e.test(n));return i?`${i[1]}${r}`:n}let Qr=[`Arm`,`ForeArm`,`Hand`].flatMap(e=>[`Left${e}`,`Right${e}`]);function $r(e,t,n,r,i){let a=Me(e);if(!a)return;let o=Qr.map(t=>e.index[t]);t.frames.forEach(({s:t},s)=>{for(let c of o){let o=Je(a,e,t,c);if(o.into<0)continue;let l=Zr(e,c),u=Zr(e,o.into),d=Math.max(i(l,u)??0,n.skin);o.depth>d&&r(`BODY`,`${l} into ${u}`,s,o.depth,d)}})}let ei=new WeakMap;function ti(e){let t=ei.get(e);return t||(t=Object.values(e).filter(e=>e.half&&!e.ignore).map(e=>({RT:m(e.R),c:[e.pos[0]+e.R[0][1]*e.half[1],e.pos[1]+e.R[1][1]*e.half[1],e.pos[2]+e.R[2][1]*e.half[1]],half:e.half,up:[e.R[1][0],e.R[1][1],e.R[1][2]],r:a(e.half)})),ei.set(e,t)),t}function ni(e,n){if(Math.hypot(n[0]-e.c[0],n[2]-e.c[2])>e.r)return null;let r=p(e.RT,t(n,e.c)),i=-1/0,a=1/0;for(let t=0;t<3;t++){let n=e.up[t],o=e.half[t];if(Math.abs(n)<1e-9){if(Math.abs(r[t])>o)return null;continue}let s=(-o-r[t])/n,c=(o-r[t])/n;i=Math.max(i,Math.min(s,c)),a=Math.min(a,Math.max(s,c))}return i<=a?n[1]+a:null}function ri(e,t,n){let r=0;for(let i of ti(e.objectsAt(t))){let e=ni(i,n);e!==null&&e<=n[1]+.05&&e>r&&(r=e)}return r}function ii(e,t,n){let r=xt(e.ctx.sk,e.frames[t].s,n);for(let n of[`heel`,`ball`,`tip`])r[n]=[r[n][0],r[n][1]-ri(e,t,r[n]),r[n][2]];return r}let ai=[`Hips`,`Head`,`LeftHand`,`RightHand`,`LeftFoot`,`RightFoot`,`LeftLeg`,`RightLeg`,`LeftForeArm`,`RightForeArm`],oi=new Set([`Hips`,`Head`]),si=[[`knee_l`,`LeftUpLeg`,`LeftLeg`,`LeftFoot`],[`knee_r`,`RightUpLeg`,`RightLeg`,`RightFoot`],[`elbow_l`,`LeftArm`,`LeftForeArm`,`LeftHand`],[`elbow_r`,`RightArm`,`RightForeArm`,`RightHand`]],ci=.75,li=e=>Math.round(e*100)/100;function ui(e,n){let r=n.frames,i=r.length;return ai.map(n=>{let o=r.map(t=>t.s.P[e.index[n]]),s=new Float64Array(i),c=new Float64Array(i),l=o.map(()=>[0,0,0]);for(let e=1;e<i;e++)s[e]=a(t(o[e],o[e-1]));for(let e=2;e<i;e++)l[e]=t(t(o[e],o[e-1]),t(o[e-1],o[e-2])),c[e]=a(l[e]);return{j:n,core:oi.has(n),v:s,a:c,A:l}})}function di({a:e,A:t},n){let i=0;for(let t of[n-3,n-2,n+2,n+3])t>=2&&t<e.length&&(i=Math.max(i,e[t]));let a=e[n]>0?[n-1,n+1].filter(t=>t>=2&&t<e.length).map(i=>r(t[i],t[n])/e[n]):[];return a.length?Math.max(i,Math.max(...a)+Math.min(0,...a)):i}let fi=(e,t,n)=>e.a[t]>n&&e.a[t]>2*di(e,t);function pi(e,t,n,r){for(let i of ui(e,t)){let e=i.core?n.accCore:n.accLimb;for(let t=2;t<i.a.length;t++)fi(i,t,e)&&r(`POP`,`${i.j} accel ${(i.a[t]*100).toFixed(2)} cm/f^2 in one frame`,t,i.a[t],e)}}let mi=[`hips`,`torso_lo`,`torso_hi`];function hi(e,t,n){let{P:r}=e.frames[t].s,i=e.ctx.sk.index;return ht.some(([e,t,n,a])=>mi.includes(e)&&Math.min(r[i[t]][1],r[i[n]][1])-a<.04)?!0:n.some(n=>{let a=e.objectsAt(t)[n.object],o=ht.find(e=>e[0]===(n.part??`hips`));return a&&o&&Math.min(...gt(r[i[o[1]]],r[i[o[2]]]).map(e=>B(e,a)))-o[3]<(n.tol??.04)})}function gi(t,r=[]){let i=t.ctx.sk.index,a=t.frames;return a.map(({s:o},s)=>{if(hi(t,s,r))return null;let{P:c}=o,l=s>0&&s<a.length-1&&Math.hypot(a[s+1].s.P[i.Hips][0]-a[s-1].s.P[i.Hips][0],a[s+1].s.P[i.Hips][2]-a[s-1].s.P[i.Hips][2])/2>.006,u=[];for(let e of[`Left`,`Right`]){let n=ii(t,s,e);(l||Math.min(n.heel[1],n.ball[1])<.02)&&u.push(n.heel,n.ball,n.tip)}if(!u.length)return`air`;let d=n([`Hips`,`Spine1`,`Spine2`,`Head`].reduce((t,n)=>e(t,c[i[n]]),[0,0,0]),.25),f=u.map(e=>e[0]),p=u.map(e=>e[2]);return Math.min(d[0]-Math.min(...f),Math.max(...f)-d[0],d[2]-Math.min(...p),Math.max(...p)-d[2])})}function _i(e,t){let{P:n}=e.frames[t].s,r=e.ctx.sk.index;return[`Left`,`Right`].some(i=>[`${i}Hand`,`${i}HandMiddle1`].some(i=>r[i]!==void 0&&n[r[i]][1]-ri(e,t,n[r[i]])<.06))}function vi(e,t=[]){let n=e.frames.length,r=[],i=e.frames.map((n,r)=>!hi(e,r,t)&&[`Left`,`Right`].every(t=>{let n=ii(e,r,t);return Math.min(n.heel[1],n.ball[1])>=.02})&&!_i(e,r));for(let e=0;e<n;e++){if(!i[e])continue;let t=e;for(;t+1<n&&i[t+1];)t++;r.push([e,t]),e=t}return r}function yi(e,t={}){let n=e.frames,r=n.length,i=e.ctx.sk.index,a=9.81/(e.fps??24)**2,o=e.f0??0,s=[],c=e=>n[Math.max(0,Math.min(r-1,e))].s.P[i.Hips][1];for(let[n,i]of vi(e,t.supports)){let e=i-n+1;if(e<4)continue;let t=1-((n>0?c(n)-c(n-1):c(n+1)-c(n))-(i<r-1?c(i+1)-c(i):c(i)-c(i-1)))/e/a;if(!(t<=ci))for(let e=n;e<=i;e++)s.push({kind:`FLOAT`,msg:`no foot on the ground f${n+o}-f${i+o}, hips fall at ${Math.round(Math.max(0,1-t)*100)}% of g`,frame:e,value:+t.toFixed(4),limit:ci})}return s}let bi=(e,n,[,r,i,a])=>w(t(n.P[e.index[i]],n.P[e.index[r]]),t(n.P[e.index[a]],n.P[e.index[i]]));function xi(t,r,i,a){let o=i?.limits??{},s=e=>Object.hasOwn(o,e),c=[],l=(e,t,n,r,i)=>c.push({kind:e,msg:t,frame:n,value:+r.toFixed(4),limit:i});for(let e of ui(t,r)){let{j:t,core:n,v:r,a:i}=e,o=n?`speedCore`:`speedLimb`,c=n?`accCore`:`accLimb`;for(let n=1;n<r.length;n++)s(o)&&r[n]>a[o]&&l(`SPEED`,`${t} ${(r[n]*100).toFixed(1)} cm/frame`,n,r[n],a[o]),s(c)&&n>1&&i[n]>a[c]&&!fi(e,n,a[c])&&l(`ACC`,`${t} accel ${(i[n]*100).toFixed(2)} cm/f^2`,n,i[n],a[c])}return s(`knee`)&&r.frames.forEach(({s:e},n)=>{for(let r of si.slice(0,2)){let i=bi(t,e,r);i>a.knee&&l(`KNEE`,`${r[0]===`knee_l`?`Left`:`Right`} knee bent ${i.toFixed(0)}deg`,n,i,a.knee)}}),s(`balance`)&&gi(r,i.supports).forEach((i,o)=>{if(i===`air`)l(`BALANCE`,`no foot on the ground`,o,0,0);else if(i!==null&&-i>a.balance){let s=n([`Hips`,`Spine1`,`Spine2`,`Head`].reduce((n,i)=>e(n,r.frames[o].s.P[t.index[i]]),[0,0,0]),.25);l(`BALANCE`,`centre of mass ${s[0].toFixed(2)},${s[2].toFixed(2)} outside the feet`,o,-i,a.balance)}}),c}function Si(e,t,n={}){let r=t.f0??0,i=t.fps??24,a=(e,t)=>{let n=0;for(let t=1;t<e.length;t++)e[t]>e[n]&&(n=t);return[Math.round(e[n]*t*100)/100,n+r]},o=Object.fromEntries(ui(e,t).map(({j:e,v:t,a:n})=>[e,{speed:a(t,i),accel:a(n,i*i)}]).filter(([,e])=>e.speed[0]>=.05)),s=gi(t,n.supports),c=e=>{let t=s.map((t,n)=>e(t)?n+r:-1).filter(e=>e>=0);return t.length?[t[0],t[t.length-1]]:null},l=vi(t,n.supports).slice(0,4).map(([e,t])=>[e+r,t+r]),u=null;s.forEach((e,t)=>{typeof e==`number`&&(!u||e<u[0])&&(u=[li(e),t+r])});let d=c(e=>typeof e==`number`&&e<0),f=Object.fromEntries(si.map(n=>{let i=[0,r];return t.frames.forEach(({s:t},a)=>{let o=bi(e,t,n);o>i[0]&&(i=[o,a+r])}),[n[0],[Math.round(i[0]),i[1]]]}));return{units:`[value, frame]; speed m/s, accel m/s^2, balance m inside the feet (< 0 outside), bend deg (0 straight)`,peaks:o,balance:{min:u,...d?{outside:d}:{},...l.length?{air:l}:{}},bend:f}}function Ci(e,t,n,r){let i=e=>Object.hasOwn(n??{},e),a=new Float64Array(t.frames.length);for(let n of ui(e,t)){let{core:e,v:t,a:o}=n,s=e?`speedCore`:`speedLimb`,c=e?`accCore`:`accLimb`;for(let e=1;e<t.length;e++)i(s)&&(a[e]=Math.max(a[e],t[e]/r[s])),!(e<2)&&(fi(n,e,r[c])&&(a[e]=Math.max(a[e],Math.sqrt(o[e]/Math.max(r[c],2*di(n,e))))),i(c)&&(a[e]=Math.max(a[e],Math.sqrt(o[e]/r[c]))))}return a}let wi=[0,1,0],Ti=new Map;function Ei(e,t){let n=`${e.model}:${t}`;if(Ti.has(n))return Ti.get(n);let r=Me(e),i=r?[]:null;for(let n of r?.grids??[]){if(n.b!==e.index[`${t}Foot`]&&n.b!==e.index[`${t}ToeBase`])continue;let[a,o,s]=n.dims,c=new Set;for(let e=0;e<a;e++)for(let t=0;t<o;t++)for(let a=0;a<s;a++){if(Math.abs(n.data[(e*o+t)*s+a]/1e3)>r.step/2)continue;let l=[n.min[0]+e*r.step,n.min[1]+t*r.step,n.min[2]+a*r.step],u=l.map(e=>Math.floor(e/.025)).join();c.has(u)||(c.add(u),i.push([n.b,l]))}}return Ti.set(n,i),i}function Di(t,r,i){let a=Ei(t,i);if(!a?.length){let a=xt(t,r,i),o=(t,r)=>n(e(t,r),.5);return[a.heel,o(a.heel,a.ball),a.ball,o(a.ball,a.tip),a.tip]}return a.map(([t,n])=>e(r.P[t],p(r.R[t],n)))}function Oi(e,t){let n={top:-1/0,side:-1/0,topAt:null,sideAt:null};for(let i of e){let e=-B(i,t);if(e<=0)continue;let a=r(St(i,t),wi)>.7?`top`:`side`;e>n[a]&&(n[a]=e,n[`${a}At`]=i)}return n}function ki(e,t,n,r,i,a,s){for(let[c,l]of[[`Left`,`l`],[`Right`,`r`]]){let u=Di(e,t,c),d=`foot_${l}`;for(let[e,t,c,l]of r){if(u.every(e=>o(e,c)>l))continue;let r=Oi(u,t),f=a(d,e),p=f??i.allow,m=f??i.groundAllow;r.side>p?s(`COLLIDE`,`${d} x ${e}`,n,r.side,p):r.top>m&&s(`COLLIDE`,`${d} x ${e}`,n,r.top,m)}}}let Ai={allow:.005,groundAllow:.01,reach:.01,slide:.0015,plantH:.012,speedCore:.04,speedLimb:.16,accCore:.015,accLimb:.05,flip:60,knee:150,headAllow:.003,self:0,body:.005,skin:.015,hands:.01,object:.005,grab:.03,balance:.08},ji=(e,t,n)=>{for(let r of e??[])if(t.startsWith(r.part)&&(r.object===n||r.object.endsWith(`*`)&&n.startsWith(r.object.slice(0,-1))))return r.depth;return null};function Mi(i,c,l={}){let u={...Ai,...l.limits},d=[],f=i.index,m=c.frames,h=m.length,g=(e,t,n,r,i)=>d.push({kind:e,msg:t,frame:n,value:+r.toFixed(4),limit:i});for(let d=0;d<h;d++){let{s:h,diag:_}=m[d],v=c.objectsAt(d),y=Object.entries(v).filter(([,e])=>e.half&&!e.ignore).map(([t,n])=>[t,n,e(n.pos,p(n.R,[0,n.half[1],0])),a(n.half)]);for(let[t,r,i,a]of ht){let s=h.P[f[r]],c=h.P[f[i]],p=n(e(s,c),.5),m=a+o(s,c)/2,_=null;for(let[e,n,r,i]of y){let f=ji(l.allow,t,e)??u.allow;if(m-(o(p,r)-i)<=f)continue;_??=gt(s,c);let h=a-Math.min(..._.map(e=>B(e,n)));h>f&&g(`COLLIDE`,`${t} x ${e}`,d,h,f)}}ki(i,h,d,y,u,(e,t)=>ji(l.allow,e,t),g);for(let e of[`Left`,`Right`]){let t=xt(i,h,e),n=Math.min(t.heel[1],t.ball[1],t.tip[1]);n<-u.groundAllow&&g(`GROUND`,`${e} sole below floor`,d,-n,u.groundAllow)}for(let[e,t]of Object.entries(_))/miss_[lr]$/.test(e)&&t>u.reach&&g(`REACH`,e,d,t,u.reach);let b=[];for(let e of[`Left`,`Right`])b.push(...gt(h.P[f[`${e}Hand`]],h.P[f[`${e}HandMiddle3`]],5).map(t=>[`hand_${e[0].toLowerCase()}`,t,.022]));for(let[e,t]of Object.entries(_.props??{}))for(let[n,r]of Object.entries(t))Array.isArray(r)&&n!==`origin`&&!(l.mouthPoints??[]).includes(`${e}.${n}`)&&b.push([`prop_${e}`,r,.004]);for(let[e,t,n]of b){let r=vt(i,h,t)+n;r>u.headAllow&&g(`FACE`,`${e} into head`,d,r,u.headAllow)}_t(h.P[f.LeftLeg],h.P[f.LeftFoot],h.P[f.RightLeg],h.P[f.RightFoot])<.1+u.self&&g(`SELF`,`shins touch`,d,.1,.1),_t(h.P[f.LeftUpLeg],h.P[f.LeftLeg],h.P[f.RightUpLeg],h.P[f.RightLeg])<.135+u.self&&g(`SELF`,`thighs overlap`,d,.135,.135);let x=s(t(h.P[f.Spine],h.P[f.Hips]))[1]<.5;for(let e of[`Left`,`Right`]){let i=h.P[f[`${e}UpLeg`]],a=h.P[f[`${e}Leg`]],o=h.P[f[`${e}Foot`]],c=w(t(a,i),t(o,a)),l=t(h.P[f[`${e}ToeBase`]],o);if(x){let e=s(t(o,a));l=t(l,n(e,r(l,e)))}else l[1]=0;let u=t(a,[(i[0]+o[0])/2,(i[1]+o[1])/2,(i[2]+o[2])/2]);c>3&&r(u,l)<-.002&&g(`KNEE`,`${e} knee bends backward`,d,c,0),c>160.01&&g(`KNEE`,`${e} knee folds ${c.toFixed(0)}deg`,d,c,160);let p=w(t(h.P[f[`${e}ForeArm`]],h.P[f[`${e}Arm`]]),t(h.P[f[`${e}Hand`]],h.P[f[`${e}ForeArm`]]));p>150.01&&g(`ELBOW`,`${e} elbow folds ${p.toFixed(0)}deg`,d,p,150)}}let _=(e,t)=>ji(l.allow,e,t);Yr(i,c,u,g,_),$r(i,c,u,g,_);for(let e of[`Left`,`Right`]){let t=m.map((t,n)=>ii(c,n,e));for(let n=1;n<h;n++){let r=t[n-1],i=t[n];for(let t of[`heel`,`ball`]){let a=i[t][1]<u.plantH&&r[t][1]<u.plantH&&Math.abs(i[t][1]-r[t][1])<5e-4,o=Math.hypot(i[t][0]-r[t][0],i[t][2]-r[t][2]);a&&o>u.slide&&g(`SLIDE`,`${e} ${t} slides`,n,o,u.slide)}}}for(let e=0;e<i.names.length;e++){let t=i.parent[e];if(t<0)continue;let n=o(i.restWorldPos[e],i.restWorldPos[t]),r=Math.max(...m.map(r=>Math.abs(o(r.s.P[e],r.s.P[t])-n)));r>.001&&g(`LENGTH`,`${i.names[t]}-${i.names[e]}`,0,r,.001)}return pi(i,c,u,g),Ni(i,m,u,g),d}function Ni(e,n,r,o){let c=e.index,l=Math.sin(20*Math.PI/180);for(let e of[`Left`,`Right`])for(let[u,d,f]of[[`${e}UpLeg`,`${e}Leg`,`${e}Foot`],[`${e}Arm`,`${e}ForeArm`,`${e}Hand`]]){let e=null,p=0;for(let m=0;m<n.length;m++){let h=n[m].s.P,g=t(h[c[d]],h[c[u]]),_=t(h[c[f]],h[c[d]]),v=i(g,_);if(a(v)<l*a(g)*a(_))continue;let y=s(v),b=e?w(y,e):0,x=m-p;b/x>r.flip&&o(`FLIP`,`${d} bend plane jumps ${b.toFixed(0)}deg${x>1?` over ${x} frames`:``}`,m,b/x,r.flip),e=y,p=m}}}let Pi=ht.filter(e=>e[0].startsWith(`torso`)),Fi=Object.fromEntries(ht.map(e=>[e[0],e[3]])),Ii=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]],Li=(e,t)=>[e[0][t],e[1][t],e[2][t]];function Ri(i,o,s,l){let u=t(o,i),d=t(l,s),f=t(i,s),p=r(u,u),m=r(d,d),h=r(d,f);if(p<1e-12||m<1e-12)return a(f);let g=r(u,f),_=r(u,d),v=p*m-_*_,y=v>1e-12?c((_*h-g*m)/v,0,1):0,b=(_*y+h)/m;return b<0?(b=0,y=c(-g/p,0,1)):b>1&&(b=1,y=c((_-g)/p,0,1)),a(t(e(i,n(u,y)),e(s,n(d,b))))}function zi(t,r,i){let a=t.index,o=z(t,r,i);return{pos:n(e(r.P[a[`${i}HandIndex2`]],r.P[a[`${i}HandMiddle2`]]),.5),R:Ii(o.d,o.l,o.n)}}function Bi(a,o,c,l,u,d){let h=o.objects[c];if(o.props[h.prop].hand!==`r`)return null;let g=Object.entries(o.anchors??{}).reduce((e,[t,n])=>e+(n.kind===`prop`&&n.prop===h.prop?Math.max(0,u[`w_${t}_l`]??0):0),0);if(!(g>1e-4))return null;let _=zi(a,l,W.r).R,v=Li(_,0),b=s(i(d,t(v,n(d,r(v,d))))),x=e=>t(e,n(b,2*r(e,b))),S=Ii(x(v),x(Li(_,1)),n(x(Li(_,2)),-1)),C=zi(a,l,W.l),w=a.index,T=w.LeftHand,E=l.P[T],D=f(m(l.R[T]),C.R),O=p(m(l.R[T]),t(C.pos,E)),k=(o.fits??[]).some(e=>e.side===`l`&&o.anchors?.[e.anchor]?.kind===`prop`&&o.anchors[e.anchor].prop===h.prop&&u[`w_${e.anchor}_l`]>1e-4)?`palm`:`wrist`,A=e=>f(y(d,e*180/Math.PI),S),j=e=>f(A(e),m(D)),M=n=>k===`wrist`?e(E,p(j(n),O)):t(C.pos,p(j(n),O)),N=e=>k===`wrist`?E:M(e),P=p(m(l.R[T]),t(l.P[w.LeftHandMiddle3],E)),ee=l.P[w.LeftArm],F=ct(a,l,W.l),I=o.fix===!1||o.fix?.self===!1?null:Me(a),L={uarm_l:w.LeftArm,farm_l:w.LeftForeArm,hand_l:w.LeftHand},te=(e,t,n)=>I?qe(I,a,l,L[n],e,t):Math.max(0,...Pi.map(([,r,i,a])=>Fi[n]+a-Ri(e,t,l.P[w[r]],l.P[w[i]]))),R=new Map,ne=t=>{if(R.has(t))return R.get(t);let n=N(t),r=it(a,l,W.l,j(t),n,F,Ze),i=e=>{let t=r.at(e);return{pv:t.pv,elbow:t.elbow,cost:t.cost+1*((100*te(ee,t.elbow,`uarm_l`))**2+(100*te(t.elbow,n,`farm_l`))**2)}},o={th:0,cost:1/0};for(let e=0;e<36;e++){let t=2*Math.PI*e/36-Math.PI,n=i(t).cost;n<o.cost&&(o={th:t,cost:n})}let s=e(n,p(j(t),P)),c=te(n,s,`hand_l`),u=Math.max(0,c-Ai.body),d=2*Fi.hand_l-Ri(n,s,l.P[w.RightHand],l.P[w.RightHandMiddle3]),f=Math.max(0,d-Ai.hands),m=1*((100*c)**2+(100*r.miss)**2)+(u>0?100*(100*u)**2:0)+(f>0?100*(100*f)**2:0),h={at:i,best:{th:o.th,pole:i(o.th).pv,cost:o.cost+m}};return R.set(t,h),h},re=e=>ne(e).best.cost+.3*(1-Math.cos(e));return{id:c,weight:g,pin:k,at:C.pos,rolled:A,arm:ne,cost:re,point:M,softmin:()=>{let e=Array.from({length:36},(e,t)=>{let n=2*Math.PI*t/36-Math.PI;return[n,re(n)]}),t=Math.min(...e.map(e=>e[1])),n=e.reduce((e,[n,r])=>{let i=Math.exp(-(r-t)/.05);return[e[0]+i*Math.cos(n),e[1]+i*Math.sin(n)]},[0,0]);return Math.atan2(n[1],n[0])},measure:e=>zi(a,e,W.l)}}let Vi=(e,t)=>Math.exp(-Math.max(0,e.cost(t)-3)/10),Hi=(e,t,n,r,i)=>({w:t*Math.min(1,e.weight)*r,second:e.id,pin:e.pin,pole:i,palm:{pos:e.at,R:e.rolled(n)},measure:e.measure}),Ui=[30,10],Wi=[`the fingers' line`,`the palm normal`,`the knuckle line`],Gi=(e,t)=>[e[0][t],e[1][t],e[2][t]],Ki=e=>{let t=m(e.R);return{pos:n(p(t,e.pos),-1),R:t}},qi=e=>Math.round(e*100)/100;function Ji(e,t,n){let r=S(e),i=S(t);return ge(r,i)<0&&(i=i.map(e=>-e)),C(Ce(r,i,n))}function Yi(e,[t,n],[r,i]){let a=e>=0?n:-t,o=e>=0?i:-r,s=Math.abs(e);if(s<=a)return(s/a)**2;let c=(Math.min(s,o)-a)/Math.max(1e-6,o-a);return 1+8*c*c+(s>o?400*((s-o)/o)**2:0)}function Xi(e,n,r,i,o){let{goal:s,plane:c,wristAt:l}=lt(e,n,r,i,o,1),u=l(s),d=it(e,n,r,s,u,c,Ze),f=d.at(d.softmin()),p=e.index,m=n.P[p[`${r}Arm`]],h=a(t(n.P[p[`${r}ForeArm`]],m)),g=a(t(n.P[p[`${r}Hand`]],n.P[p[`${r}ForeArm`]])),_=Math.max(0,N(h,g,145)-a(t(u,m))),v=Me(e),y=v?Math.max(qe(v,e,n,p[`${r}Arm`],m,f.elbow),qe(v,e,n,p[`${r}ForeArm`],f.elbow,u),qe(v,e,n,p[`${r}Hand`],u,i.pos)):0;return[`twist`,`flex`,`dev`].reduce((e,t)=>e+Yi(f.need[t],Xe[t],Ye[t]),0)+20*((d.miss*100)**2+(_*100)**2)+4*(y*100)**2}function Zi(e,t,n,r,i,a,o){if(r.aim===void 0)return o({pos:r.pos,R:C(r.rot)});let c=S(q(i,a).R),l=s(r.aim),u=me(xe(s(be(c,[0,1,0])),l),c),d=e=>({pos:r.pos,R:C(me(Se(l,e),u))});if(r.roll!==void 0)return o(d(r.roll));let f=null;for(let r=0;r<4;r++){let o=d(360/4*r-180),s=ft(e,t,n,q(o,Ki(a)),i);(!f||s<f.strain)&&(f={p:o,strain:s})}return o(f.p)}function Qi(e,t){let n=Object.entries(e?.objects??{}).filter(([e,n])=>(e===t||n.ancestors?.includes(t))&&n.half);if(!n.length)return!1;let r=n.map(([,e])=>e.half).reduce((e,t)=>t[0]*t[1]*t[2]>e[0]*e[1]*e[2]?t:e),i=[0,1,2].reduce((e,t)=>r[t]>r[e]?t:e,0);return[0,1,2].filter(e=>e!==i).every(e=>r[e]<.045)}let $i=(e,t)=>e.length<=t?e:Array.from({length:t},(n,r)=>e[Math.round(r*(e.length-1)/(t-1))]);function ea(e,t){let n=ve(S(f(m(e),t))),r=Math.round(2*a(n)*180/Math.PI),i=[0,1,2].reduce((e,t)=>Math.abs(n[t])>Math.abs(n[e])?t:e,0);return`today's grip turned ${r} deg, mostly about ${Wi[i]}`}function ta({sk:e,o:t,side:n,grab:r,rest:i,plain:a,palmOf:o,map:s=e=>e}){let c=new Map,l=e=>{let t=Math.round(e);if(!c.has(t)){let e=a(t);c.set(t,{s:e,now:o(e)})}return c.get(t)},{s:u,now:d}=l(r),p=[...t.keys].sort((e,t)=>e.f-t.f),m=$i(p,8),h=e=>hs({pos:d.pos,R:e},i),g=(t,r,i)=>{let{s:a,now:o}=l(r);return Xi(e,a,n,q(i,Ki(t)),o)},_=t=>Xi(e,u,n,q(i,Ki(t)),d),v=t=>{let r=h(t);return _(r)+m.reduce((t,i)=>{let{s:a,now:o}=l(s(i.f));return t+Zi(e,a,n,i,o,r,e=>g(r,s(i.f),e))},0)},b=r=>{let a=h(r),o=Math.round(s(p[0].f)),c=Math.round(s(p[p.length-1].f)),u=va(t,s,e=>S(q(l(e).now,a).R),(t,r,i,o)=>{let{s,now:c}=l(t);return ft(e,s,n,q({pos:r,R:C(i)},Ki(a)),c,o)},2*(i?.half?.[1]??0)),d=_(a),f=d;for(let e=o;e<=c;e+=4){let t=g(a,e,xa(u,e));d+=t,f=Math.max(f,t)}return{A:a,cost:d,worst:f}},x={name:`today`,R:d.R,...b(d.R)};if(x.worst<=1.5)return{offset:x.A,name:`today`,cost:qi(x.cost),today:qi(x.cost)};let w={R:d.R,rough:v(d.R)};for(let e of Ui)for(let t=0,n=!0;n&&t<2;t++){n=!1;for(let t of[0,1,2])for(let r of[1,-1]){let i=f(y(Gi(w.R,t),r*e),w.R),a=v(i);a<w.rough-1e-6&&(w={R:i,rough:a},n=!0)}}let T=w.R===d.R?x:{name:ea(d.R,w.R),...b(w.R)},E=T.cost<x.cost-Math.max(2,.1*x.cost)?T:x;return{offset:E.A,name:E.name,cost:qi(E.cost),today:qi(x.cost)}}let na=180/Math.PI,ra=[-40,-40/2,0,40/2,40];function ia(e){let t=Math.min(...e.map(e=>e[1])),n=e.map(([e,n])=>[e,Math.exp(-(n-t)/.3)]);return n.reduce((e,[t,n])=>e+t*n,0)/n.reduce((e,[,t])=>e+t,0)}function aa(e){let t=Array.from({length:12},(t,n)=>{let r=2*Math.PI*n/12-Math.PI;return[r,e(r)]}),n=Math.min(...t.map(e=>e[1])),r=t.reduce((e,[t,r])=>{let i=Math.exp(-(r-n)/.05);return[e[0]+i*Math.cos(t),e[1]+i*Math.sin(t)]},[0,0]);return Math.atan2(r[1],r[0])}let oa=({d:e,l:t,n})=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]],sa=(e,t)=>e.anchors?.[t]?.kind===`object`&&!!e.anchors[t].grasp,ca=(e,t)=>(e.keys??[]).some(e=>(!e.src||e.src.startsWith(`keys[`))&&(e.set?.[`wtwist_${t}`]!==void 0||e.set?.[`wflex_${t}`]!==void 0));function la(e,t,n,r){let i=new Set(U(t,n));return(e.fits??[]).some(a=>{if(a.side!==r||!sa(e,a.anchor))return!1;let o=e.anchors[a.anchor].object;return o===n||i.has(o)||U(t,o).some(e=>i.has(e))})}function ua(e,t,n){let{spec:r,scene:i}=e;if(!e.objectHand||!(r.fits??[]).some(e=>e.side===t&&sa(r,e.anchor)))return null;for(let[a,o]of Object.entries(r.objects??{}))if(o?.prop&&e.objectHand(a,n)===t&&la(r,i,a,t))return{id:a,grab:e.carrySpan(o)[0],keyed:!!o.keys?.length};return null}function da(e,n,r,i,a,o,c,l,u,d=null,p=void 0){let h=n.index,g=h[`${i}Arm`],_=h[`${i}ForeArm`],v=h[`${i}Hand`],b=r.R[_],x=d?.w??rr(e,l.fit.anchor,r,c,o,a),S=u(r),C=s(t(r.P[v],r.P[_])),w=d?.type??e.spec.anchors[l.fit.anchor].grasp,T=Gn(i,x,w,C,o.yaw),E=(e=>({pos:u(e),R:oa(z(n,e,i))}))(r),D=e=>{let t=lt(n,r,i,{pos:S,R:e},E,1,`palm`);return at(n,r,i,t.goal,t.wristAt(t.goal),t.plane,Ze).cost},O=(e,t)=>[e[0][t],e[1][t],e[2][t]],k=e=>f(y(O(T,1),e*na),T),{R:A,roll:j,tilt:M}=fa(e,l.fit.anchor,a,p,()=>{let e=x.axis&&!x.from&&w!==`palm`?aa(e=>D(k(e))+.3*(1-Math.cos(e))):0,t=k(e),n=e=>f(y(O(t,2),e),t),r=x.axis&&w!==`palm`?ia(ra.map(e=>[e,D(n(e))+1*(e/40)**2])):0;return{R:n(r),roll:e,tilt:r}}),{goal:N,plane:P,wristAt:ee}=lt(n,r,i,{pos:S,R:A},E,l.w,`palm`),F=at(n,r,i,N,ee(N),P,Ze,{w:l.w}),I={twist:0,flex:0,dev:0};for(let e=0;e<3;e++)re(n,r,g,_,v,ee(e?r.R[v]:N),F.pole,Ze,`elbow`),I=st(n,r,i,v,_,N);c[`grip_${a}`]={w:l.w,socket:!0,...I,roll:j*na,tilt:M},c[`socket_${a}`]={rel:f(m(b),r.R[v])}}function fa(e,t,n,r,i){let a=`w_${t}_${n}`,o=t=>e.weightAt?.(a,t)??0;if(!e.weightAt||!Number.isInteger(r))return i();let s=r,c;for(;s>0&&o(s-1)>.001;)s--;for(c=s;c<e.frameCount-1&&o(c)<.999&&o(c+1)>.001;c++);let l=`${a}:${s}`;if(e.graspPlans??=new Map,r>=c){let t=i();return r===c&&e.graspPlans.set(l,t),t}return e.graspPlans.has(l)||(e.graspPlans.set(l,null),(e.hold?.plainFrame??e.solvedAt)(c)),e.graspPlans.get(l)??i()}function pa(e,t,n,r,i,a){let o=`${i}:${a.grab}`;e.socketTurns??=new Map,e.socketTurns.has(o)||(e.socketTurns.set(o,null),e.socketTurns.set(o,(e.hold?.plainFrame??e.solvedAt)(a.grab)?.diag?.[`socket_${i}`]?.rel??null));let s=e.socketTurns.get(o);if(!s)return;let c=t.index,l=c[`${r}ForeArm`],u=c[`${r}Hand`];st(t,n,r,u,l,f(n.R[l],s))}let ma={m:.01,deg:5},ha=e=>e*e*(3-2*e),ga=(e,t)=>{let n=t=>e.timeMap?Math.round(e.timeMap(t)):t;return[n(t.grab??0),t.release===void 0?e.frameCount-1:n(t.release)]},_a=e=>{let t=m(e.R);return{pos:n(p(t,e.pos),-1),R:t}};function va(r,i,a,o,s=0){let c=[...r.keys].sort((e,t)=>e.f-t.f),l=c.map(e=>i(e.f)),u=[],d=Pr(c,e=>a(l[e]),o&&((e,t)=>o(l[e],Ar(c[e],t,s),t,c[e].lean?{shoulder:!0}:void 0))),f=c.map((e,t)=>Ar(e,_e(d[t]),s).slice());for(let e of d){let t=_e(e);u.push(u.length&&ge(u[u.length-1],t)<0?t.map(e=>-e):t)}let p=c.length-1,m=e=>n(ve(me(he(u[e]),u[e+1])),1/(l[e+1]-l[e])),h=(t,r)=>t===0||t===p?[0,0,0]:n(e(n(r(t),l[t]-l[t-1]),n(r(t-1),l[t+1]-l[t])),1/(l[t+1]-l[t-1]));return{t:l,p:f,q:u,m:f.map((e,r)=>h(r,e=>n(t(f[e+1],f[e]),1/(l[e+1]-l[e])))),w:u.map((e,t)=>h(t,m)),rots:d}}let ya=new WeakMap;function ba(e,t){ya.has(e)||ya.set(e,new Map);let n=ya.get(e),r=e.timeMap,i=e.baked,a=n.get(t);if(!a||a.map!==r||a.baked!==i){let a=e.ctx.hold,o=new Map,s=e=>{let t=Math.round(e);return o.has(t)||o.set(t,a.plainFrame(t).s),o.get(t)};n.set(t,{map:r,baked:i,curve:va(e.ctx.spec.objects[t],e=>r?r(e):e,e=>S(a.carryOn(t,s(e)).R),(e,n,r,i)=>a.strainOn(t,s(e),{pos:n,R:C(r)},i),2*(e.ctx.scene?.objects?.[t]?.half?.[1]??0))})}return n.get(t).curve}function xa({t,p:r,q:i,m:a,w:o},s){let c=t.length;if(c===1||s<=t[0])return{pos:r[0].slice(),R:C(i[0])};if(s>=t[c-1])return{pos:r[c-1].slice(),R:C(i[c-1])};let l=0;for(;s>t[l+1];)l++;let u=t[l+1]-t[l],d=(s-t[l])/u,f=d*d,p=f*d,m=2*p-3*f+1,h=p-2*f+d,g=-2*p+3*f,_=p-f,v=e(e(n(r[l],m),n(a[l],u*h)),e(n(r[l+1],g),n(a[l+1],u*_))),y=me(he(i[l]),i[l+1]),b=ve(y),x=e(e(n(b,g),n(o[l],u*h)),n(be(y,o[l+1]),u*_));return{pos:v,R:C(me(i[l],ye(x)))}}function Sa(e,[t,n],r){let i=e.t[0],a=e.t[e.t.length-1];if(r<t||r>n)return 0;if(r>=i&&r<=a)return 1;let o=r<i?Math.min(8,i-t):8,s=r<i?i-r:r-a;return o>0&&s<o?ha(1-s/o):0}let Ca=e=>Object.entries(e.objects??{}).filter(([,e])=>K(e)&&e.keys?.length);function wa(r,i,a,o){let{sk:s,spec:c,scene:l}=r,u=Ca(c);if(!u.length)return null;let d=new Set(u.map(([e])=>e).filter(e=>At(c,c.objects[e],l,e)).map(e=>c.objects[e])),f=(e,t)=>ms(s,c,t,{prop:e.prop},d.has(e)),p=new WeakMap,m=new Set,h=(e,t)=>r.restingAt?.(e,t)??l?.objects?.[e]??En(l)[e],g=new WeakMap,_=e=>i.timeMap?Math.round(i.timeMap(e)):e,v=e=>{g.has(i)||g.set(i,new Map);let t=g.get(i),n=c.objects[e],r=c.props?.[n.prop]?.hand,o=ga(i,n)[0],u=i.baked??{};if(t.has(e))return t.get(e);if(t.set(e,null),!(r&&n.at===void 0&&n.along===void 0&&(n.grab!==void 0||!l?.objects?.[e])&&Qi(l,e)&&!(c.fits??[]).some(e=>e.side===r&&e.orient===!1)&&!la(c,l,e,r)&&![`wtwist`,`wflex`].some(e=>Math.abs(u[`${e}_${r}`]?.[o]??0)>.001)&&_(Math.min(...n.keys.map(e=>e.f)))<=o))return null;m.add(e);try{t.set(e,ta({sk:s,o:n,side:W[r],grab:o,rest:h(e,o),plain:e=>a(e).s,palmOf:e=>f(n,e),map:_}))}finally{m.delete(e)}return t.get(e)},y=e=>{p.has(i.baked)||p.set(i.baked,new Map);let t=p.get(i.baked),n=c.objects[e];if(t.has(e))return t.get(e);let o=v(e);if(o&&o.name!==`today`){let r=d.has(n)?Nt(s,c,l,e,n,o.offset):o.offset;return t.set(e,r),t.set(`${e}|taken`,o.offset),r}let u=l?.objects?.[e]??En(l)[e],h=!l?.objects?.[e]&&!!u,g=n.at===void 0&&n.along===void 0&&(h||n.grab!==void 0);m.add(e);let _;try{_=g?a(ga(i,n)[0]).s:D(s)}finally{m.delete(e)}let y=hs(f(n,_),g?r.restingAt?.(e,ga(i,n)[0])??{pos:u.pos,R:u.R}:ms(s,c,_,n)),b=g&&d.has(n)?Nt(s,c,l,e,n,y):y;return t.set(e,b),t.set(`${e}|taken`,y),b},b=(e,t)=>{let n=y(e),r=p.get(i.baked)?.get(`${e}|taken`);return r&&r!==n&&t!==void 0?It(r,n,Ft(ga(i,c.objects[e])[0],t)):n},x=(e,t)=>q(f(c.objects[e],t),y(e)),S=(e,t,n,r)=>{let i=c.objects[e];return ft(s,t,W[c.props[i.prop].hand],q(n,_a(y(e))),f(i,t),r)},C=(e,t,n)=>{let r=ba(i,e),a=Sa(r,ga(i,t),n);return a>0?{w:a,palm:q(xa(r,n),_a(b(e,n))),measure:e=>f(t,e),comfort:t.keys.some(e=>e.lean>0)}:null},w=new WeakMap,T=(e,t)=>{w.has(i.baked)||w.set(i.baked,new Map);let n=w.get(i.baked),r=n.get(e);if(r&&r.map===i.timeMap)return r.plan;let[o,l]=ga(i,t),u=[];for(let t=Math.max(0,o);t<=Math.min(i.frameCount-1,l);t++)Sa(ba(i,e),[o,l],t)>0&&u.push(t);let d=Wr(s,W[c.props[t.prop].hand],u,n=>({s:a(n).s,held:C(e,t,n)}));return n.set(e,{map:i.timeMap,plan:d}),d},E=!1,O=(e,t,n)=>{let r=x(e,t).R;return Bi(s,c,e,t,n,[r[0][1],r[1][1],r[2][1]])},k=new WeakMap,A=(e,t)=>{k.has(i.baked)||k.set(i.baked,new Map);let n=k.get(i.baked),r=n.get(e);if(r&&r.map===i.timeMap)return r.plan;let[a,s]=ga(i,t),c=new Map;E=!0;let l=[],u=()=>{let e=zr((e,t)=>l[e].grip.cost(t),(e,t)=>l[e].grip.point(t),l.map(e=>e.grip.softmin())),t=Ur(l.map((t,n)=>Vi(t.grip,e[n]))),n=t=>l[t].grip.arm(e[t]),r=zr((e,t)=>n(e).at(t).cost,(e,t)=>n(e).at(t).elbow,l.map((e,t)=>n(t).best.th));l.forEach((i,a)=>c.set(i.f,{roll:e[a],gate:t[a],pole:n(a).at(r[a]).pv})),l=[]};for(let t=Math.max(0,a);t<=Math.min(i.frameCount-1,s);t++){let n=Sa(ba(i,e),[a,s],t)>0?o(t):null,r=n&&O(e,n.s,n.c);r?l.push({f:t,grip:r}):l.length&&u()}return l.length&&u(),E=!1,n.set(e,{map:i.timeMap,plan:c}),c},j=(t,r,a)=>{let o=v(t);if(!o||o.name===`today`)return null;let s=ba(i,t),c=s.t[0],l=s.t[s.t.length-1],[u,d]=ga(i,r);if(a>=c&&a<=l)return null;let p=xa(s,a<c?c:l),m=q(p,_a(o.offset)),h=e=>f(r,e),g=T(t,r).get(Math.round(a<c?c:l));if(a<u||a>d){let e=ha(Math.max(0,1-(a<u?u-a:a-d)/12));return e>.001?{w:e,palm:m,measure:h,pole:g}:null}let _=C(t,r,a);return{w:1,palm:_?{pos:e(n(m.pos,1-_.w),n(_.palm.pos,_.w)),R:Ji(m.R,_.palm.R,_.w)}:m,measure:h,pole:g}},M=new WeakMap,N=(e,n,r)=>{M.has(i.baked)||M.set(i.baked,new Map);let o=M.get(i.baked),s=o.get(e);if(s&&s.map===i.timeMap)return s.off;m.add(e);let c;try{c=f(n,a(Math.max(ga(i,n)[0],Math.round(ba(i,e).t[0]))).s)}finally{m.delete(e)}let l=hs(c,r).R,u=Math.acos(Math.min(1,Math.max(-1,(l[0][0]+l[1][1]+l[2][2]-1)/2)))*180/Math.PI,d=Math.hypot(...t(r.pos,c.pos))>ma.m||u>ma.deg;return o.set(e,{map:i.timeMap,off:d}),d},P=(e,t,n)=>{let r=ba(i,e),[a,o]=ga(i,t),s=Math.max(a,Math.round(r.t[0]));if(!(n<s&&n>=s-12)||s-a>=8||s>o)return null;let c=q(xa(r,s),_a(y(e)));if(!N(e,t,c))return null;let l=ha(1-(s-n)/12);return l>.001?{w:l,palm:c,measure:e=>f(t,e),pole:T(e,t).get(s)}:null},ee=(e,t,n)=>{let r=ba(i,e),a=r.t[r.t.length-1],o=ga(i,t)[1];return n>o&&n-a<8?{curve:r,release:o,w:ha(1-(n-a)/8)}:null},F=(e,t,n)=>{let r=ee(e,t,n);return r&&{w:r.w,palm:q(xa(r.curve,r.release),_a(y(e))),measure:e=>f(t,e),pole:T(e,t).get(r.release)}};return{lead:(e,t,n,r)=>{for(let[a,o]of u){let s=c.props?.[o.prop]?.hand===e?j(a,o,t)??P(a,o,t)??F(a,o,t):null;if(s)return s;let l=c.props?.[o.prop]?.hand===e?C(a,o,t):null;if(l)return{...l,pole:T(a,o).get(t)};let u=e===`l`&&n&&!E?ee(a,o,t):null,d=e===`l`&&n&&!E?Sa(ba(i,a),ga(i,o),t)||(u?.w??0):0,f=d>0?O(a,n,r):null,p=f&&A(a,o).get(u?u.release:t),m=p?.roll??f?.softmin();if(f)return Hi(f,d,m,p?.gate??Vi(f,m),p?.pole??f.arm(m).best.pole)}return null},attach:y,attachAt:b,attaching:e=>m.has(e),plainFrame:a,carryOn:x,strainOn:S,span:e=>ga(i,c.objects[e]),firstKey:e=>{let t=Math.min(...c.objects[e].keys.map(e=>e.f));return i.timeMap?Math.round(i.timeMap(t)):t},grips:()=>u.map(([e,t])=>{let n=v(e);return n&&{id:e,hand:c.props[t.prop].hand,name:n.name,cost:n.cost,today:n.today}}).filter(Boolean)}}let Ta=[0,0,0];function Ea(e,r,i,a){return Array.isArray(e?.v)?e.v:e?.v===`hand`&&i?n(t(r.pos,i.pos),a):Ta}let Da=e=>e.some(e=>e!==0),Oa=(e,t,n)=>(t+Math.sqrt(t*t+2*n*Math.max(0,e)))/n,ka=(e,t,n)=>{let r=Math.min(t,n);return[e[0]*r,0,e[2]*r]};function Aa(t,n,r,i,a=[]){let o=t(n),s=o.top+o.h,c=t=>({pos:e(n.pos,ka(r,Oa(s-t,r[1],i),1/0)),R:n.R});for(let e of[...new Set(a)].filter(e=>e<=s).sort((e,t)=>t-e)){let n=t(c(e));if(Math.abs(n.top-e)<1e-9)return n}return t(c(0))}let ja=.5,Ma=6*Math.PI,Na=[0,-1,0],Pa=[[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]],Fa=e=>e*180/Math.PI;function Ia(t){let r=t.filter(e=>e.half[0]*e.half[1]*e.half[2]>1e-12);if(!r.length)return null;let i=0,a=[0,0,0],o=[];for(let t of r){let r=t.half[0]*t.half[1]*t.half[2];i+=r,a=e(a,n(e(t.pos,p(t.R,[0,t.half[1],0])),r));for(let n of[-1,1])for(let r of[0,2])for(let i of[-1,1])o.push(e(t.pos,p(t.R,[n*t.half[0],r*t.half[1],i*t.half[2]])))}return{com:n(a,1/i),lo:[0,1,2].map(e=>Math.min(...o.map(t=>t[e]))),hi:[0,1,2].map(e=>Math.max(...o.map(t=>t[e])))}}let La=e=>[0,1,2,3,4,5,6,7].map(t=>[t&1?e.hi[0]:e.lo[0],t&2?e.hi[1]:e.lo[1],t&4?e.hi[2]:e.lo[2]]),Ra=(t,n)=>e(t.pos,p(t.R,n)),za=(e,t)=>Math.min(...La(e).map(e=>Ra(t,e)[1])),Ba=(e,t,n)=>({pos:[t.pos[0],t.pos[1]+n-za(e,t),t.pos[2]],R:t.R}),Va=(n,r,i)=>({pos:e(r,p(i,t(n.pos,r))),R:f(i,n.R)}),Ha=e=>Pa.reduce((t,n,i)=>r(p(e,n),Na)>r(p(e,Pa[t]),Na)?i:t,0),Ua=(e,t)=>{let n=t>>1;return Pa[t][n]>0?e.hi[n]:e.lo[n]},Wa=(e,t)=>Math.abs(Ua(e,t)-e.com[t>>1]);function Ga(e,t,r){let i=f(t,[[e[0][0],e[1][0],e[2][0]],[e[0][1],e[1][1],e[2][1]],[e[0][2],e[1][2],e[2][2]]]),o=Math.acos(c((i[0][0]+i[1][1]+i[2][2]-1)/2,-1,1));if(o*r<1)return[0,0,0];let l=s([i[2][1]-i[1][2],i[0][2]-i[2][0],i[1][0]-i[0][1]]),u=Math.min(Ma,o*r);return a(l)>0?n(l,u):[0,0,0]}function Ka(n,r,i,o,s,c=[0,0,0]){let l=a(i),u=Ra(r,n.com),d=!c.some(e=>e!==0),m=a=>{let o=l>0?f(y(i,Fa(l*a)),r.R):r.R,s=d?[0,-.5*Za*a*a,0]:[c[0]*a,c[1]*a-.5*Za*a*a,c[2]*a];return{pos:t(e(u,s),p(o,n.com)),R:o}},h=Math.max(0,za(n,r)-o);if(l===0)return{tl:d?Math.sqrt(2*h/Za):Oa(h,c[1],Za),at:m};let g=1/(s*8),_=(d?Math.sqrt(2*(h+a(t(n.hi,n.lo)))/Za):Oa(h+a(t(n.hi,n.lo)),c[1],Za))+g,v=0,b=g;for(;b<_&&za(n,m(b))>o;)v=b,b+=g;for(let e=0;e<24&&b-v>1e-7;e++){let e=(v+b)/2;za(n,m(e))>o?v=e:b=e}return{tl:b,at:m}}let qa=(e,t)=>.12+.9*Math.sqrt(Math.max(e,1e-4)/Za)*Math.sqrt(t/90);function Ja(o,l,u,d,f=[0,0,0]){let m=[],h=Ba(o,l,u),g=0,_=(e,t,n)=>{let r=Ra(h,o.com)[1]-u,i=qa(r,n);m.push({from:h,pivot:e,axis:t,angle:n,time:i}),h=Ba(o,Va(h,e,y(t,n)),u),g+=i},v=Ha(h.R),b=e=>Fa(Math.acos(c(r(p(h.R,Pa[e]),Na),-1,1))),x=b(v);if(x>ja){let t=[0,1,2,3,4,5].filter(e=>e>>1!=v>>1).reduce((e,t)=>b(t)<b(e)?t:e,v===0||v===1?2:0);x>Fa(Math.atan2(Wa(o,t),Wa(o,v)))&&(v=t);let r=La(o).map(e=>Ra(h,e)).filter(e=>e[1]<=u+1e-4),a=p(h.R,Pa[v]);_(n(r.reduce(e,[0,0,0]),1/r.length),s(i(a,Na)),b(v))}let S=x>ja||a(f)>0?d:0;for(let a=0;a<3;a++){let a=Ra(h,o.com),c=a[1]-u,l=null;for(let e=0;e<6;e++){if(e>>1==v>>1)continue;let n=p(h.R,Pa[e]),i=Ra(h,Pa[e].map((t,n)=>t?Ua(o,e):n===v>>1?Ua(o,v):o.com[n])),s=-r(t(a,i),n),u=Wa(o,e);if(!(s<0||u<c*.8))continue;let d=s<0||Fa(Math.atan2(s,c))<15?0:Math.hypot(c,s)-c,m=r(n,f);(!l||d<l.cost-1e-9||Math.abs(d-l.cost)<=1e-9&&m>l.side)&&(l={j:e,out:n,edge:i,cost:d,side:m})}if(!l||l.cost>S)break;S-=l.cost;let d=La(o).map(e=>Ra(h,e)).filter(e=>e[1]<=u+1e-4&&r(t(e,l.edge),l.out)>=-1e-4);_(d.length?n(d.reduce(e,[0,0,0]),1/d.length):[l.edge[0],u,l.edge[2]],s(i(l.out,Na)),90),v=l.j}return{steps:m,rest:h,time:g}}function Ya(e,t){let n=t;for(let t of e.steps){if(n<t.time){let e=n/t.time;return Va(t.from,t.pivot,y(t.axis,t.angle*e*e))}n-=t.time}return e.rest}let Xa=(e,t)=>a(t)**2*((e.hi[0]-e.lo[0])**2+(e.hi[1]-e.lo[1])**2+(e.hi[2]-e.lo[2])**2)/(24*Za),Za=9.8,Qa=[`prop`,`follow`,`pivot`,`axis`,`channel`],$a=e=>!!e&&typeof e==`object`&&!Array.isArray(e)&&Qa.every(t=>e[t]===void 0)&&(e.keys!==void 0||e.fall!==void 0),eo=e=>e?.fall===void 0?[]:Array.isArray(e.fall)?e.fall:[e.fall],to=(e,t)=>e?.objects?.[t]??En(e)[t]??null,no=(e,t)=>Object.keys(e?.objects??{}).filter(n=>n===t||e.objects[n].ancestors?.includes(t));function ro(t){let n=[];for(let r of[-1,1])for(let i of[0,2])for(let a of[-1,1])n.push(e(t.pos,p(t.R,[r*t.half[0],i*t.half[1],a*t.half[2]])));return n}let io=e=>Math.max(...ro(e).map(e=>e[1])),ao=(e,n)=>{let r=p(m(e.R),t([n[0],e.pos[1]+e.half[1],n[2]],e.pos));return Math.abs(r[0])<=e.half[0]&&Math.abs(r[2])<=e.half[2]};function oo(e,t){let n=new Set;for(let[r,i]of Object.entries(t?.objects??{}))[r,...i.ancestors??[]].some(t=>ds(e.objects?.[t]))&&n.add(r);return n}function so(e,t,n){let r=to(e,t);return no(e,t).map(t=>({id:t,...q(n,hs(r,e.objects[t])),half:e.objects[t].half}))}function co(t,n,r,i,a=null){let o=so(n,r,i),s=new Set(o.map(e=>e.id)),c=oo(t,n);if(!o.length)return{support:null,top:0,h:0};let l=Math.min(...o.flatMap(e=>ro(e).map(e=>e[1]))),u=o.flatMap(ro).reduce((t,n)=>e(t,n),[0,0,0]).map(e=>e/(o.length*8)),d=(a?no(n,a):Object.keys(n.objects)).filter(e=>!s.has(e)&&!c.has(e)),f=null;for(let e of d){let t=n.objects[e],r=io(t);ao(t,u)&&r<=l+.005&&(!f||r>f.top)&&(f={support:e,top:r})}if(!f&&a)for(let e of d){let t=io(n.objects[e]);t<=l+.005&&(!f||t>f.top)&&(f={support:e,top:t})}return f??={support:null,top:0},{...f,h:Math.max(0,l-f.top)}}let lo=new WeakMap;function uo(e,t){lo.has(e)||lo.set(e,new Map);let n=lo.get(e);return n.has(t)||n.set(t,Ia(so(e,t,{pos:[0,0,0],R:d()}))),n.get(t)}function fo(e,t,n,r,i,a,o,s=null){let c=Ea(a,r,s,o),l=Da(c),u=r=>co(e,t,n,r,a.onto??null),d=l?Aa(u,r,c,Za,Object.values(t.objects).map(io)):u(r),f=a.tumble===!1?null:uo(t,n);if(f){let e=s?Ga(s.R,r.R,o):[0,0,0],t=Ka(f,r,e,d.top,o,c),n=Ja(f,t.at(t.tl),d.top,.3*d.h+Xa(f,e),[-e[2],0,e[0]]);if(n.steps.length||e.some(e=>e)||l)return{t0:i,land:i+t.tl*o,end:i+(t.tl+n.time)*o,h:d.h,support:d.support,top:d.top,at:e=>{let a=(e-i)/o;return a<=0?r:a<=t.tl?t.at(a):Ya(n,a-t.tl)},rest:n.rest}}let p=d.h,m=l?Oa(p,c[1],Za):Math.sqrt(2*p/Za),h=ka(c,1/0,m),g=Math.min(.6,Math.max(0,a.bounce??.25)),_=Math.min(g*Za*m,Math.sqrt(2*Za*.03)),v=2*_/Za,y={pos:[r.pos[0]+h[0],r.pos[1]-p,r.pos[2]+h[2]],R:r.R};return{t0:i,land:i+m*o,end:i+(m+v)*o,h:p,support:d.support,top:d.top,at:e=>{let t=(e-i)/o;if(t<=0)return r;let n=t<=m?(l?c[1]*t:0)-.5*Za*t*t:t<=m+v?-p+_*(t-m)-.5*Za*(t-m)**2:-p,a=ka(c,t,m);return{pos:[r.pos[0]+a[0],r.pos[1]+(l?n:Math.min(0,n)),r.pos[2]+a[2]],R:r.R}},rest:y}}function po(e,t,n=-1/0,r=1/0){let i=K(e)?[]:[...e.keys??[]].sort((e,t)=>e.f-t.f),a=eo(e).map(e=>({...e,t:t(e.at)})).filter(e=>e.t>=n&&e.t<r).sort((e,t)=>e.t-t.t),o=[],s=[],c=0;for(let e of i){for(;c<a.length&&a[c].t<t(e.f);)s.length&&o.push({run:s}),s=[],o.push({fall:a[c++]});s.push(e)}for(s.length&&o.push({run:s});c<a.length;)o.push({fall:a[c++]});return o}function mo(e,t,n,r,i=!0){let a=n;for(let n of e)if(n.run){let{t:e}=n.curve;if(t<e[0])return a;if(t<=e[e.length-1])return xa(n.curve,t);a=xa(n.curve,e[e.length-1])}else{if(t<n.fall.t)return a;let e=i?n.plan??=r(a,n.fall):r(a,n.fall);if(t<e.end)return e.at(t);a=e.rest}return a}let ho=new WeakMap;function go(e,t,n,r,i){ho.has(e.scene)||ho.set(e.scene,new Map);let a=ho.get(e.scene),o=`${t}|${i}|${e.grab}|${e.release}`,s=a.get(o);if(s&&s.map===r&&s.o===n&&s.spec===e.spec)return s.events;let c=to(e.scene,t),l=r??(e=>e),[u,d]=i===`before`?[-1/0,e.grab]:i===`after`?[e.release,1/0]:[-1/0,1/0],f=po(n,l,u,d);for(let e of f)e.run&&(e.curve=va({keys:e.run},l,()=>S(c.R),null));return a.set(o,{map:r,o:n,spec:e.spec,events:f}),f}let _o=(e,t)=>(n,r)=>fo(e.spec,e.scene,t,n,r.t,r,e.spec.fps??30);function vo(e,t,n){let r=to(e.scene,t),i=e.spec.objects?.[t];return!r||!i?null:mo(go(e,t,i,e.timeMap,`all`),n,{pos:r.pos,R:r.R},_o(e,t))}function yo(e,t,n,r,i){let a=to(e.scene,t);return!a||!eo(n).length?a&&{pos:a.pos,R:a.R}:mo(go({...e,grab:i},t,n,e.timeMap,`before`),r,{pos:a.pos,R:a.R},_o(e,t))}function bo(e,t,n,r,i,a,o=null){let s=eo(n).length?go({...e,release:i},t,n,e.timeMap,`after`):[];return s.length?mo(s,r,a,(n,r)=>fo(e.spec,e.scene,t,n,r.t,r,e.spec.fps??30,r.t<=i+1?o:null),!1):null}function xo(e,t,n){let r=e.timeMap??(e=>e);return eo(t).some(e=>r(e.at)>=n)}function U(e,t){let n=e?.objects??{};return Object.keys(n).filter(e=>e===t||n[e].ancestors?.includes(t))}function So(e,t){return K(e)&&t>=(e.grab??0)&&(e.release===void 0||t<=e.release)}function Co(e,t){return e?.objects?.[t]??En(e)[t]??null}function wo(e,t,n){return[n,...t?.objects?.[n]?.ancestors??En(t)[n]?.ancestors??[]].find(t=>ds(e.objects?.[t]))??null}function To(e,t){let n=t=>e.timeMap?Math.round(e.timeMap(t)):t;return[n(t.grab??0),t.release===void 0?e.frameCount-1:n(t.release)]}let Eo=(e,t,n)=>n.at===void 0&&n.along===void 0&&(!e?.objects?.[t]&&!!En(e)[t]||n.grab!==void 0),Do=new WeakMap,Oo=new WeakMap,ko=new WeakMap,Ao=e=>({spec:e.ctx.spec,scene:e.ctx.scene,timeMap:e.timeMap});function jo(e,t,n){let{spec:r,scene:i}=e.ctx,a=wo(r,i,t),o=r.objects?.[a];if(!K(o))return null;let s=yo(Ao(e),a,o,n,To(e,o)[0]);return s&&(a===t?s:q(s,hs(Co(i,a),Co(i,t))))}function Mo(t,n){ko.has(t)||ko.set(t,new Map);let r=ko.get(t);if(r.has(n))return r.get(n);let{spec:i,scene:a}=t.ctx,o=i.objects?.[n],s=Co(a,n),c=U(a,n),l=null;if(K(o)&&o.release!==void 0&&s&&c.length){let r=To(t,o)[1];if(r<t.frameCount-1&&!xo(Ao(t),o,r)){let u=Fo(t,n,o,r),d=c.flatMap(e=>ro({...q(u,hs(s,a.objects[e])),half:a.objects[e].half})),f=Math.min(...d.map(e=>e[1])),p=d.reduce((t,n)=>e(t,n),[0,0,0]).map(e=>e/d.length);for(let[e,o]of Object.entries(a.objects)){let s=c.includes(e)?null:wo(i,a,e),u=i.objects?.[s];if(!s||s===n||K(u)||ls(u))continue;let d={...zo(t,e,r),half:o.half},m=io(d);ao(d,p)&&m<=f+.005&&(!l||m>l.top)&&(l={mover:s,top:m})}}}return r.set(n,l?.mover??null),l?.mover??null}function No(e,t){let n=e.frames[t-(e.f0??0)];if(n)return n;Do.has(e)||Do.set(e,new Map);let r=Do.get(e);return r.has(t)||r.set(t,Ml(e.ctx,Er(e.baked,t),t)),r.get(t)}function Po(e,t,n,r,i){let{sk:a,spec:o,scene:s}=e.ctx;if(!Eo(s,t,n))return ms(a,o,i,n);let c=At(o,n,s,t);if(n.keys?.length&&e.ctx.hold){if(e.ctx.hold.attaching(t))return Co(s,t);let l=q(ms(a,o,i,{prop:n.prop},c),e.ctx.hold.attachAt(t,r)),u=To(e,n)[0];return c?It(jo(e,t,u)??Co(s,t),l,Ft(u,r)):l}let l=e=>ms(a,o,e,{prop:n.prop},c),u=To(e,n)[0],d=hs(l(u===r?i:No(e,u).s),jo(e,t,u)??Co(s,t));return c?It(q(l(i),d),q(l(i),Nt(a,o,s,t,n,d)),Ft(u,r)):q(l(i),d)}function Fo(e,t,n,r){let i=No(e,r);Oo.has(i)||Oo.set(i,new Map);let a=Oo.get(i);return a.has(t)||a.set(t,Po(e,t,n,r,i.s)),a.get(t)}function Io(e,t,n,r=null){let{spec:i,scene:a}=e.ctx,o=ds(i.objects?.[t])?i.objects[t]:void 0,s=Co(a,t);if(ls(o))return hr(r?{...e,f0:n,frames:[r]}:e,o,n);if(K(o)&&(s||!Eo(a,t,o))){let[i,a]=To(e,o),c=Math.max(i,Math.min(n,a));if(n<i&&s)return jo(e,t,n);let l=n>a?bo(Ao(e),t,o,n,a,Fo(e,t,o,a),a>0?Fo(e,t,o,a-1):null):null;if(l)return l;let u=n>a?Mo(e,t):null;if(u){let r=Fo(e,t,o,a);return q(Io(e,u,n),hs(Io(e,u,a),r))}return r&&c===n?Po(e,t,o,n,r.s):Fo(e,t,o,c)}if(!s)return null;if($a(o))return vo(Ao(e),t,n);if(o&&a.objects[t])return e.ctx.objectsAt(n)[t];if(o){let r=U(a,t)[0],i=e.ctx.objectsAt(n)[r];return q(i,hs(a.objects[r],s))}return{pos:s.pos,R:s.R}}function Lo(e,t,n){let r=e.frames?.[n-(e.f0??0)],i=e.ctx.scene,a=U(i,t);return!r||!a.length?null:a.map(t=>{let r=zo(e,t,n);return{id:t,pos:r.pos,R:r.R,half:i.objects[t].half}})}function Ro(e,t,n){let{spec:r,scene:i,sk:a}=e.ctx,o=r.objects?.[wo(r,i,t)];if(K(o)){let[t,i]=To(e,o);return n>=t&&n<=i?r.props?.[o.prop]?.hand??null:null}if(!ls(o))return null;let[s,c=``]=String(o.follow).split(/:(.*)/s);if(s===`prop`)return r.props?.[c.split(`.`)[0]]?.hand??null;let l=(s===`joint`||s===`bone`)&&/^(Left|Right)(Arm|ForeArm|Hand)/.exec(xn(a,c).bone??``);return l?l[1]===`Left`?`l`:`r`:null}function zo(e,t,n,r=null){let{spec:i,scene:a}=e.ctx,o=Co(a,t),s=wo(i,a,t),c=i.objects?.[s];if(!o)return null;if(K(c)||ls(c)||$a(c)){let t=Io(e,s,n,r);return t&&q(t,hs(Co(a,s),o))}return s?a.objects[t]?e.ctx.objectsAt(n)[t]??o:s===t?Io(e,t,n):q(Io(e,s,n),hs(Co(a,s),o)):{pos:o.pos,R:o.R}}let Bo=[`Index`,`Middle`,`Ring`,`Pinky`],Vo=e=>{let t=Math.min(1,Math.max(0,(e-.6)/.30000000000000004));return t*t*(3-2*t)};function Ho(e,t){let n=-1/0;for(let r=0;r+1<e.length;r++)for(let i of t)n=Math.max(n,kt(i,e[r],e[r+1],.009));return n}function Uo(n,r,i,a,o){if(a<=0||Ho(n,o)>=-.002)return 0;let s=a=>{let o=y(i,a);return n.map(n=>e(r,p(o,t(n,r))))};if(Ho(s(a),o)<-.002)return null;let c=0,l=a;for(let e=0;e<8;e++){let e=(c+l)/2;Ho(s(e),o)>=-.002?l=e:c=e}return l}function Wo(n,r,i,a,o){if(Ho(n,o)<-.002)return 0;let s=a=>{let o=y(i,-a);return n.map(n=>e(r,p(o,t(n,r))))};if(Ho(s(a),o)>=-.002)return Ho(s(a),o)<Ho(n,o)?a:0;let c=0,l=a;for(let e=0;e<8;e++){let e=(c+l)/2;Ho(s(e),o)<-.002?l=e:c=e}return l}function Go(e,t,n,r,i,a,o,s,c=!0){let l=Uo(r.map(e=>t.P[e]),t.P[n],i,a,o),u=l??(c?a:0);return u>0&&O(e,t,n,t.P[n].slice(),y(i,u*s)),l!==null}function Ko(r,a,o,{entries:c,stops:l=[],weight:u,preset:d}){if(!c?.length||!(u>0))return{touched:[]};let f=[...c,...l],m=o===`Left`?-1:1,h=e=>r.index[`${o}Hand${e}`],g=[];for(let i of Bo){let s=[1,2,3,4].map(e=>h(`${i}${e}`));if(s.some(e=>e===void 0))continue;let l=!1,_=s.map(e=>a.P[e]),v=n(z(r,a,o).l,m),b=[0,1,2].reduce((n,r)=>{let a=y(v,-le(d,i,r));return n.map((i,o)=>o>r?e(n[r],p(a,t(i,n[r]))):i)},_),x=Ho(_,c)>=-.002&&Ho(b,c)<Ho(_,c);for(let e=0;e<3;e++){let t=n(z(r,a,o).l,m),c=le(d,i,e),p=x?c:0;p>0&&O(r,a,s[e],a.P[s[e]].slice(),y(t,-p*u)),l=Go(r,a,s[e],s.slice(e),t,Math.max(0,ue(i,e)-c+p),f,u,!1)}l&&g.push(i)}let _=[1,2,3,4].map(e=>h(`Thumb${e}`));if(_.every(e=>e!==void 0)){let{n:e}=z(r,a,o),c=!1;for(let o of[1,2]){let l=n(s(i(s(t(a.P[_[o+1]],a.P[_[o]])),e)),m),d=Wo(_.slice(o).map(e=>a.P[e]),a.P[_[o]],l,70,f);d>0&&O(r,a,_[o],a.P[_[o]].slice(),y(l,-d*u)),c=Go(r,a,_[o],_.slice(o),l,70,f,u,!1)}c&&g.push(`Thumb`)}return{touched:g}}function qo(e,t,n,r,i){let a=null;for(let i of e.spec.fits??[]){let o=e.spec.anchors?.[i.anchor],s=t[`w_${i.anchor}_${r}`]??0;if(i.side!==r||o?.kind!==`object`||!o.grasp||!(s>1e-4)||a&&a.weight>=s||e.objectHand(o.object,n)===r||e.sockets?.get(i.anchor)?.rim)continue;let c=e.objectsAt(n),l=U(e.scene,o.object).map(e=>c[e]).filter(Boolean),u=Math.min(1,Object.entries(t[`fingers_${r}`]??{}).reduce((e,[t,n])=>e+(t===`relaxed`?0:n||0),0));l.length&&(a={entries:l,weight:Vo(s)*u,preset:t[`fingers_${r}`]})}return a??Jo(e,t,n,r,i)}function Jo(e,t,n,r,i){let{spec:a,scene:o}=e;for(let[s,c]of Object.entries(a.objects??{})){if(!At(a,c,o,s)||a.props?.[c.prop]?.hand!==r||!i)continue;let[l,u]=e.carrySpan(c);if(n<l||n>u||e.hold?.attaching(s))continue;let d=U(o,s).map(t=>({...o.objects[t],...e.objectPose(t,n,{s:i})})),f=Object.entries(e.objectsAt(n)).filter(([e,t])=>!U(o,s).includes(e)&&t.half&&!t.carried&&d.some(e=>Math.hypot(...e.pos.map((e,n)=>e-t.pos[n]))<.25+Math.hypot(...t.half))).map(([,e])=>e);if(d.length)return{entries:d,stops:f,weight:1,preset:t[`fingers_${r}`]}}return null}let Yo=(e,[t,n])=>Math.min(n,Math.max(t,e));function Xo(r,i,a){let o=r.index,c=o[`${a}Hand`],l=o[`${a}ForeArm`],u=r.restWorldRot,d=s(t(i.P[c],i.P[l])),h=f(i.R[l],f(m(u[l]),u[c])),{axF:g,axD:_,...v}=nt(h,i.R[c],d,p(f(h,m(i.R[c])),z(r,i,a).l)),b=Object.fromEntries(Object.entries(v).map(([e,t])=>[e,Yo(t,Ye[e])]));if(Object.keys(v).every(e=>Math.abs(v[e]-b[e])<1e-6))return null;let x=f(f(y(e(n(g,b.flex),n(_,b.dev)),Math.hypot(b.flex,b.dev)),y(d,b.twist)),h);return O(r,i,c,i.P[c].slice(),f(x,m(i.R[c]))),{need:v,got:b}}let W={l:`Left`,r:`Right`},Zo=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)},Qo=[0,0,1],$o=[`UpLeg`,`Leg`,`Foot`],es=(e,t=[0,0,0])=>Array.isArray(e)||ArrayBuffer.isView(e)?[e[0],e[1],e[2]]:t,G=(e,t=0)=>typeof e==`number`&&Number.isFinite(e)?e:t;function ts(e,t,n,r,i,a){let o=n.length;for(let s of n){let n=e.index[s];O(e,t,n,t.P[n].slice(),f(_(r/o),f(g(i/o),v(a/o))))}}let ns=.9995;function rs(r,i,o,s,c,l,u,d){let f=a(t(i.P[s],i.P[o]))+a(t(i.P[c],i.P[s])),p=t(l,i.P[o]),m=a(p),h=.99*f,g=.009500000000000064*f;if(m<=h||m-ns*f>.05)return l;let _=h+g*(1-Math.exp(-(m-h)/g));return u[`reach_over_${d}`]=Math.max(0,m-ns*f),e(i.P[o],n(p,_/m))}let is=new Map;function as(r,i){let a=`${r.model}|${i}`;if(is.has(a))return is.get(a);let o=(90-i)*Math.min(1,Math.max(0,(i-45)/27)),s=Ke(r),c=a=>{let o=D(r);return[[`Left`,-1],[`Right`,1]].every(([c,l])=>{let u=r.index[`${c}Arm`],d=r.index[`${c}ForeArm`],f=r.index[`${c}Hand`],p=r.index[`${c}HandMiddle1`];return O(r,o,u,o.P[u].slice(),v(l*i)),O(r,o,d,o.P[d].slice(),v(l*a)),[.5,.75,1].map(r=>e(o.P[d],n(t(o.P[f],o.P[d]),r))).concat([o.P[p]]).every(e=>Ie(s.body,o,e,s.skip).d>=.095)})},l=o;if(s&&o>0){for(let e=0;e<=o;e+=.5)if(!c(e)){l=Math.max(0,e-2);break}}return is.set(a,l),l}let os=e=>e-360*Math.round(e/360);function ss(e,t,n){let r=os(t),i=os(n),a=(Math.max(Math.abs(r),Math.abs(i))-60)/60;if(a<=0)return null;let o=a>=1?1:a*a*(3-2*a);return{yaw:e,R:f(_(e),f(g(r*o),v(i*o)))}}function cs(r,i,a={}){let o=D(r),c={},l=G(i.arm_rest,72),u=G(i.arm_hang)&&G(i.arm_hang)*as(r,l);for(let[e,t,n]of[[`Left`,-1,`l`],[`Right`,1,`r`]]){let a=r.index[`${e}Arm`],s=r.index[`${e}ForeArm`],c=u*(1-Math.min(1,Math.max(0,G(i[`armw_${n}`])+G(i[`hangoff_${n}`]))));O(r,o,a,o.P[a].slice(),v(t*l)),c&&O(r,o,s,o.P[s].slice(),v(t*c))}for(let[e,t]of Object.entries(W)){let n=G(i[`abend_${e}`]),a=G(i[`aswing_${e}`]),s=r.index[`${t}ForeArm`],c=r.index[`${t}Arm`];n&&(O(r,o,s,o.P[s].slice(),g(-n)),R(r,o,t)),a&&O(r,o,c,o.P[c].slice(),g(-a))}ts(r,o,[`Neck`,`Head`],G(i.head_yaw),G(i.head_pitch),G(i.head_roll)),ts(r,o,[`Spine`,`Spine1`,`Spine2`],G(i.spine_yaw),G(i.spine_pitch),G(i.spine_roll));let d=r.index.Hips,m=G(i.yaw);O(r,o,d,o.P[d].slice(),f(_(m),f(g(G(i.pitch)),v(G(i.roll)))));let h=t(es(i.root,o.P[d]),o.P[d]);for(let t=0;t<o.P.length;t++)o.P[t]=e(o.P[t],h);let y=ss(m,G(i.pitch),G(i.roll)),b=y?.R??_(m);for(let[e,t]of Object.entries(W)){if(!i[`foot_${e}`])continue;let n=es(i[`foot_${e}`]);c[`leg_miss_${e}`]=re(r,o,r.index[`${t}UpLeg`],r.index[`${t}Leg`],r.index[`${t}Foot`],n,p(b,es(i[`knee_${e}`],Qo)),Qo,`knee`);let a=te(r,o,t,n,$o);a>150&&(c[`leg_fold_${e}`]=a),ie(r,o,t,G(i[`fyaw_${e}`],m),G(i[`fpitch_${e}`]),y)}for(let l of a.armOrder??[`r`,`l`]){let u=W[l],d=G(i[`armw_${l}`]),f=r.index[`${u}Arm`],m=r.index[`${u}ForeArm`],h=r.index[`${u}Hand`],g=s(p(b,es(i[`elbow_${l}`],[0,-1,-.3])));if(c[`arm_miss_${l}`]=0,d>1e-4&&a.handTarget){let i=o.P[h].slice(),p=a.handTarget(l,o,c)??i,_=e(i,n(t(p,i),d));a.keepOut&&(_=a.keepOut(l,o,_));let v=a.push?.(l);v&&(_=e(_,v));let y=a.sag?.(l,o,_);y&&(_=e(_,y));let b=a.skinPush?.(l);b&&(_=e(_,n(b,Zo(d)))),a.softReach&&(_=rs(r,o,f,m,h,_,c,l));let x=s(t(o.P[m],n(e(o.P[f],i),.5))),S=e(n(x,1-d),n(g,d));c[`arm_miss_${l}`]=re(r,o,f,m,h,_,S,Ze,a.foldFree?null:`elbow`);let C=te(r,o,u,_);C>145&&!a.foldFree&&(c[`arm_fold_${l}`]=C)}oe(r,o,u,G(i[`wtwist_${l}`]),G(i[`wflex_${l}`])),de(r,o,u,i[`fingers_${l}`]);let _=a.hold?.(l,o);(!_||_.w<1)&&a.grip?.(l,o,c),_&&ut(r,o,u,_,Ze,c,l);let v=a.swivel?.(l);v&&(ae(r,o,u,v*Zo(d)),_||Xo(r,o,u));let y=a.heldShapes?.(l,o);y&&(c[`closed_${l}`]=Ko(r,o,u,y).touched),a.afterArm?.(l,o,c)}return{s:o,diag:c}}let K=e=>typeof e?.prop==`string`,ls=e=>!K(e)&&e?.follow!==void 0,us=e=>!!e&&typeof e==`object`&&!Array.isArray(e)&&[`prop`,`follow`,`pivot`,`axis`,`channel`,`keys`,`fall`].every(t=>e[t]===void 0),ds=e=>!!e&&typeof e==`object`&&!us(e);function fs(t,r,i,a){let o=r.props[a.prop];return n(o.origin.map(e=>i.P[t.index[e]]).reduce((t,n)=>e(t,n),[0,0,0]),1/o.origin.length)}function ps(r,i,a){let o=r.index[`${W[i.hand]}Hand`],s=r.restWorldPos,c=n(i.origin.map(e=>s[r.index[e]]).reduce((t,n)=>e(t,n),[0,0,0]),1/i.origin.length);return A(a,o,p(m(r.restWorldRot[o]),t(c,s[o])))}function ms(a,o,c,l,u=!1){let d=z(a,c,W[o.props[l.prop].hand]),f=u?ps(a,o.props[l.prop],c):fs(a,o,c,l),p=t=>e(n(d.d,t[0]),e(n(d.l,t[1]),n(d.n,t[2]))),m=s(p(l.along??[0,0,1])),h=t(d.d,n(m,r(d.d,m)));Math.hypot(...h)<1e-6&&(h=t(d.l,n(m,r(d.l,m)))),h=s(h);let g=i(h,m);return{pos:e(f,p(l.at??[0,0,0])),R:[[h[0],m[0],g[0]],[h[1],m[1],g[1]],[h[2],m[2],g[2]]]}}let q=(t,n)=>({pos:e(t.pos,p(t.R,n.pos)),R:f(t.R,n.R)}),hs=(e,n)=>({pos:p(m(e.R),t(n.pos,e.pos)),R:f(m(e.R),n.R)});function gs(n,r,i,a){let o=i[n.channel]?.[a]??0,s=y(n.axis??[0,1,0],o);return{pos:e(n.pivot,p(s,t(r.pos,n.pivot))),R:f(s,r.R??d()),angle:o}}let _s=180/Math.PI,vs=[0,1,0],ys=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]],bs=(e,i)=>t(e,n(i,r(e,i)));function xs(e,t,n){let r=null;for(let i of e.fits??[]){let e=t[`w_${i.anchor}_${i.side}`]??0;i.side===n&&i.orient!==!1&&e>1e-4&&(!r||e>r.w)&&(r={fit:i,w:e})}return r&&{...r,w:r.w*Math.min(1,Math.max(0,t[`armw_${n}`]??0))}}function Ss(t,r,i,a,o){if(o.prop){let s=r.props?.[o.prop];if(!s)return null;let c=z(t,i,a),l=n(s.origin.map(e=>i.P[t.index[e]]).reduce(e,[0,0,0]),1/s.origin.length),[u,d,f]=o.point===`origin`?[0,0,0]:s.points?.[o.point]??[0,0,0];return e(l,e(n(c.d,u),e(n(c.l,d),n(c.n,f))))}let s=o.joints??[`${a}HandIndex2`,`${a}HandMiddle2`];return n(s.map(e=>i.P[t.index[e]]).reduce(e,[0,0,0]),1/s.length)}function Cs(e,t){let{spec:n}=e,r=e.scene?.objects?.[t];for(let e of[t,...r?.ancestors??[]]){let t=n.objects?.[e];if(t?.prop)return{key:e,entry:t,side:n.props?.[t.prop]?.hand??null}}return null}function ws(e,t,n,r){let i=e.spec.objects?.[t],a=e.scene?.objects?.[t]?.half;if(!e.hold?.span||!i?.prop||e.spec.props?.[i.prop]?.hand!==n||!i.keys?.length||i.grab===void 0||i.at!==void 0||i.along!==void 0||a&&Ds(a)>=0)return null;let[o]=e.hold.span(t);return o>=1&&r<=Math.max(o,e.hold.firstKey(t)-1)?Math.min(r,o-1):null}function Ts(e,t,n,r,i,a){let o=e.scene?.objects??{},s=i?.kind===`object`?U(e.scene,i.object):Object.keys(o),c=null;for(let i of s){let s=o[i]?.half;if(!s)continue;let l=ws(e,Cs(e,i)?.key,n,t),u=e.objectPose?.(i,l??t,l===null?a:null)??o[i];if(!u?.R)continue;let d={pos:u.pos,R:u.R,half:s},f=B(r,d);(!c||f<c.d)&&(c={box:d,d:f,id:i})}let l=c&&Cs(e,c.id);return!c||l?.side===n&&ws(e,l.key,n,t)===null?null:i?.kind===`object`||c.d<=.06?{...c.box,id:c.id}:null}let Es=[0,1,2];function Ds(e){let t=Es.reduce((t,n)=>e[n]>e[t]?n:t,0);return Es.filter(e=>e!==t).every(t=>e[t]<.045)?t:-1}function Os(t,o,c,l,u){let d=t===`Right`,f=p(_(l??0),[d?1:-1,0,0]),m=Math.abs(o[1]),h=bs(e(n(vs,1-m),n(f,m)),o),g=s(a(h)>.001?h:bs(f,o)),v=u?bs(u,o):null;if(!v||a(v)<=.3)return ys(o,g,i(o,g));let y=e=>Math.acos(Math.max(-1,Math.min(1,r(e,c))))*_s-(r(e,g)>0?30:0),b=s(v),x=y(b)<=y(n(b,-1))?b:n(b,-1);return ys(o,x,i(o,x))}function ks(e,t,n){let a=[e[0][0],e[1][0],e[2][0]],o=[e[0][1],e[1][1],e[2][1]],s=Math.atan2(r(i(t,o),a),r(t,o))*_s,[c,l]=Xe.twist,u=Math.min(l,Math.max(c,n+s))-n;if(Math.abs(u-s)<1e-6)return e;let d=p(y(a,u),t);return ys(a,d,i(a,d))}function As(n,r,i,a,o,c,l,u){let d=xs(n.spec,c,o),h=ua(n,o,l),g=d&&d.w>=.001&&sa(n.spec,d.fit.anchor);if(h||g?ca(n.spec,o):Math.abs(c[`wtwist_${o}`]??0)>.001||Math.abs(c[`wflex_${o}`]??0)>.001)return;if(h&&l>h.grab)return h.keyed?void 0:pa(n,r,i,a,o,h);if(!d||d.w<.001)return;if(g)return da(n,r,i,a,o,c,u,d,e=>Ss(r,n.spec,e,a,d.fit.effector??{}),null,l);if(Object.keys(n.spec.objects??{}).some(e=>n.spec.objects[e]?.prop&&n.objectHand?.(e,l)===o&&ws(n,e,o,l)===null))return;let _={s:i,diag:u,c},v=n.spec.anchors?.[d.fit.anchor];if(v?.kind===`prop`)return;let y=e(Xn(n,d.fit.anchor,i,u,c,o),d.fit.offset??[0,0,0]),b=Ts(n,l,o,y,v,_),x=b&&Ss(r,n.spec,i,a,d.fit.effector??{});if(!x)return;let S=e=>({pos:Ss(r,n.spec,e,a,d.fit.effector??{}),R:(({d:e,l:t,n})=>ys(e,t,n))(z(r,e,a))}),C=r.index,w=C[`${a}Arm`],T=C[`${a}ForeArm`],E=C[`${a}Hand`],D=Ds(b.half),O=D>=0?p(b.R,Es.map(e=>+(e===D))):null;if(O&&Math.abs(O[1])>.8&&!Cs(n,b.id))return da(n,r,i,a,o,c,u,d,e=>Ss(r,n.spec,e,a,d.fit.effector??{}),{w:{p:y,axis:O},type:`power`},l);let k=z(r,i,a),A=s(bs(k.l,k.d)),j=s(t(i.P[E],i.P[T])),M=f(i.R[T],f(m(r.restWorldRot[T]),r.restWorldRot[E])),N=nt(M,i.R[E],j,p(f(M,m(i.R[E])),k.l)).twist,{goal:P,plane:ee,wristAt:F}=lt(r,i,a,{pos:x,R:ks(Os(a,k.d,A,c.yaw,O),A,N)},S(i),d.w,`palm`),I={twist:0,flex:0,dev:0};for(let e=0;e<2;e++)re(r,i,w,T,E,F(e?i.R[E]:P),ee,Ze,`elbow`),I=st(r,i,a,E,T,P);u[`grip_${o}`]={w:d.w,...I}}let js={period:3.8,deg:.8},Ms={yaw:.9,pitch:.6},Ns={period:5.6,m:.012,list:1.4},Ps=.5,Fs=2*Math.PI,Is=e=>e*e*(3-2*e),Ls=(e,t)=>Array.isArray(e)?e.some((e,n)=>Math.abs(e-t[n])>1e-4):e&&typeof e==`object`?Object.keys({...e,...t}).some(n=>Math.abs((e[n]??0)-(t?.[n]??0))>.001):Math.abs((e??0)-(t??0))>.01;function Rs(e,t){let n=2166136261;for(let t=0;t<e.length;t++)n=Math.imul(n^e.charCodeAt(t),16777619);let r=n>>>0;return Array.from({length:t},()=>{r=r+1831565813>>>0;let e=Math.imul(r^r>>>15,r|1);return e^=e+Math.imul(e^e>>>7,e|61),((e^e>>>14)>>>0)/4294967296*Fs})}function zs(e,t,n,r){let i=Array(n).fill(!1),a=Object.keys(e);i[0]=!0;for(let t=1;t<n;t++)a.some(n=>Ls(e[n][t],e[n][t-1]))&&(i[t]=i[t-1]=!0);let o=a.filter(e=>/^(armw_[lr]|w_.+_[lr])$/.test(e));for(let t=0;t<n;t++)o.some(n=>e[n][t]>.001)&&(i[t]=!0);for(let e of Object.values(t.objects??{}))if(Number.isFinite(e?.grab))for(let t=Math.max(0,e.grab);t<Math.min(n,Number.isFinite(e.release)?e.release:n);t++)i[t]=!0;for(let e of t.goals??[]){let t=Array.isArray(e?.at)?e.at:null;if(!(e?.still===void 0||!t?.length))for(let e=Math.max(0,Math.round(t[0]));e<=Math.min(n-1,Math.round(t[t.length-1]));e++)i[e]=!0}let s=Math.max(1,Math.round(Ps*r)),c=Array(n).fill(1/0);for(let e=0,t=-1/0;e<n;e++)i[e]&&(t=e),c[e]=e-t;for(let e=n-1,t=1/0;e>=0;e--)i[e]&&(t=e),c[e]=Math.min(c[e],t-e);return c.map(e=>Is(Math.min(1,e/s)))}function Bs(e,t,n,r){let i={},a=Math.max(1,Math.round(Ps*r));for(let r of[`l`,`r`]){let o=Object.keys(e).filter(e=>e===`armw_${r}`||RegExp(`^w_.+_${r}$`).test(e)),s=Array.from({length:n},(t,n)=>o.some(t=>e[t][n]>.001));for(let e of Object.values(t.objects??{}))if(!(!Number.isFinite(e?.grab)||t.props?.[e.prop]?.hand!==r))for(let t=Math.max(0,e.grab);t<Math.min(n,Number.isFinite(e.release)?e.release:n);t++)s[t]=!0;if(!s.some(Boolean)){i[r]=null;continue}let c=Array(n).fill(1/0);for(let e=0,t=-1/0;e<n;e++)s[e]&&(t=e),c[e]=e-t;for(let e=n-1,t=1/0;e>=0;e--)s[e]&&(t=e),c[e]=Math.min(c[e],t-e);i[r]=c.map(e=>1-Is(Math.min(1,e/a)))}return i}function Vs(e,{spec:t,sk:n,frameCount:r}){if(t.idle===!1||!e.root||!e.foot_l||!e.foot_r)return null;let i=t.fps??30,a=zs(e,t,r,i);if(!a.some(e=>e>0))return null;let o=n.restWorldPos,s=o[n.index.Hips][1],c=o[n.index.LeftFoot][1],[l,u,d,f,p]=Rs(JSON.stringify([e.root[0].map(e=>e.toFixed(3)),(e.yaw?.[0]??0).toFixed(1)]),5),m=Array(r).fill(null);for(let t=0;t<r;t++){if(!(a[t]>0))continue;let n=t/i,r=a[t],o=m[t]={spine_pitch:-r*js.deg*Math.sin(Fs*n/js.period+l),head_yaw:r*Ms.yaw*(.6*Math.sin(Fs*n/7.3+u)+.4*Math.sin(Fs*n/11.9+p)),head_pitch:r*Ms.pitch*Math.sin(Fs*n/9.1+d)};if(![`l`,`r`].every(n=>e[`foot_${n}`][t][1]<c+.03&&Math.abs(e[`fpitch_${n}`]?.[t]??0)<2)||e.root[t][1]<s-.15)continue;let h=r*Math.sin(Fs*n/Ns.period+f),g=(e.yaw?.[t]??0)*Math.PI/180;o.root=[Math.cos(g)*Ns.m*h,0,-Math.sin(g)*Ns.m*h],o.roll=Ns.list*h,o.spine_roll=-Ns.list*h}return m}function Hs(e,t,n){let r=t?.[n];if(!r)return e;let i={...e};for(let[t,n]of Object.entries(r))i[t]=Array.isArray(n)?Array.isArray(e[t])?e[t].map((e,t)=>e+n[t]):e[t]:(Number(e[t])||0)+n;return i}let Us=(e,t,n)=>Math.min(n,Math.max(t,e));function Ws(e){let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);let n=t>>>0;return()=>{n=n+1831565813>>>0;let e=Math.imul(n^n>>>15,n|1);return e^=e+Math.imul(e^e>>>7,e|61),((e^e>>>14)>>>0)/4294967296*2-1}}let Gs={noise:.02,side:.03,first:.22,second:.08,last:.22,beforeLast:.08,turn:0},Ks={noise:.025,side:.012,stride:.035,second:.05,beforeLast:.06,turn:.06},qs={noise:.1,first:.1,last:.25};function Js(e,{s0:t,turn:n,seed:r}){let i=e.steps,a=Us(e.vary??1,0,2),o=Ws(r),s=Array.from({length:i},()=>({time:o(),length:o(),lift:o(),arm:o()})),c=o()<0?1:-1,l=e=>e%2==0?c:-c,u=Array.from({length:Math.ceil(i/2)},()=>o()),d=e=>u[Math.floor((e-1)/2)],f=Array.from({length:i},(t,n)=>n===i-1?0:n===0?e.firstStep:e.step),p=t,m=f.map(e=>{let t=Math.abs(n(p,p+e));return p+=e,Math.min(1,t/60)}),h=i>=4,g=s.map((e,t)=>{if(i<2)return 1;let n=(t===0||t===i-1?.5:1)*(Gs.noise*e.time+Gs.side*l(t))+Gs.turn*m[t];return t===0&&(n+=Gs.first*(1-m[0])),t===1&&h&&(n+=Gs.second),t===i-1&&(n+=Gs.last),t===i-2&&h&&(n+=Gs.beforeLast),1+a*n}),_=g.reduce((e,t)=>e+t,0),v=i*e.period,y=[];for(let e=0,t=0;e<i;e++)y.push(Math.round(t)),t+=v*g[e]/_;let b=e.period-e.swing,x=y.map((t,n)=>{if(a===0)return e.swing;let r=(n<i-1?y[n+1]:v)-t;return Math.min(r-2,Math.max(e.swing,r-b),n===i-1?e.swing+3:1/0)}),S=f.map((e,t)=>t>0&&t<i-1),C=s.map((e,t)=>{if(!S[t])return 1;let n=Ks.noise*(.7*e.length+.3*e.time)+Ks.side*l(t)+Ks.stride*d(t)-Ks.turn*m[t];return t===1&&h&&(n-=Ks.second),t===i-2&&h&&(n-=Ks.beforeLast),1+a*n}),w=S.filter(Boolean).length,T=C.reduce((e,t,n)=>e+(S[n]?t:0),0);return{lift:y,air:x,inc:f.map((e,t)=>S[t]&&a>0?e*C[t]*w/T:e),height:s.map((t,n)=>e.lift*(1+a*(qs.noise*t.lift-(n===0?qs.first:0)-(n===i-1&&i>1?qs.last:0)))),arm:s.map(e=>1+a*.1*e.arm)}}let Ys={strike:4,load:4,mid:3,toeOff:5},Xs={strike:2,load:14,mid:7,toeOff:48,pow:1.8},Zs=[1,2.2],Qs=e=>{let t=Math.min(1,Math.max(0,((e??Zs[1])-Zs[0])/(Zs[1]-Zs[0])));return t*t*(3-2*t)},$s=.004,ec=[.25,.5],tc=e=>e*e*e*(e*(6*e-15)+10),nc=new WeakSet,rc=(n,r,i,a)=>e(n,p(_(r),p(g(i),p(_(-r),t(a,n)))));function ic(e,n,r){let i=e.index,o=e.restWorldPos,s=a(t(o[i.LeftLeg],o[i.LeftUpLeg])),c=a(t(o[i.LeftFoot],o[i.LeftLeg])),l=Math.min(1,Math.max(0,(13-n)/4)),u=Qs(r),d=(e,t)=>e+(t-e)*u,f=d(Xs.strike,18)+l*Ys.strike,p=d(Xs.load,22)+l*Ys.load,m=d(Xs.mid,7)+l*Ys.mid,h=d(Xs.toeOff,45)+l*Ys.toeOff;return{reach:e=>Math.sqrt(s*s+c*c+2*s*c*Math.cos(e*Math.PI/180)),hipOff:{l:t(o[i.LeftUpLeg],o[i.Hips]),r:t(o[i.RightUpLeg],o[i.Hips])},strike:f,load:p,mid:m,trail:e=>m+(h-m)*Math.min(1,Math.max(0,e))**d(Xs.pow,1)}}let ac=(t,n,r)=>e(t.root[n],p(_(t.yaw?.[n]??0),p(v(t.roll?.[n]??0),r)));function oc(e,t,{f0:n,end:r,period:i,pace:a,ground:o,start:s}){let c=e.index,l=e.restWorldPos,{reach:u,hipOff:d,strike:f,load:m,mid:h,trail:g}=ic(e,i,a),y=Math.max(2,Math.round(.35*i)),b=(e,t)=>{if(t<e.a)return f;if(e.heel!==void 0&&t>=e.heel)return g((t-e.heel)/Math.max(1,e.b-e.heel));let n=(t-e.a)/i/.4;return e.strike&&n<1?(n<.5?f:h)+(m-(n<.5?f:h))*Math.sin(Math.PI*n):h},x=Tr(t,r+1),S=l[c.Hips][1]-.008,C=x.root.map(e=>[e[0],e[2]]);for(let e=0;e<2;e++)C=C.map((e,t)=>{if(t<=n||t>=r)return e;let i=0,a=0,o=0;for(let e=-6;e<=6;e++){let s=Math.min(r,Math.max(n,t+e)),c=Math.exp(-(e*e)/8);i+=c*C[s][0],a+=c*C[s][1],o+=c}let s=Math.min(1,(t-n)/8,(r-t)/8),c=s*s*(3-2*s);return[e[0]+(i/o-e[0])*c,e[1]+(a/o-e[1])*c]});let w=e=>{let t=[C[e][0],x.root[e][1],C[e][1]],i=x.yaw?.[e]??0,a=x.roll?.[e]??0,s=[];for(let n of[`l`,`r`]){let r=o[n].find(t=>e>=t.a-(t.strike?y:0)&&e<=t.b);if(!r||r.free!==void 0&&e>=r.free)continue;let c=x[`foot_${n}`][e],l=p(_(i),p(v(a),d[n])),f=u(b(r,e)),m=Math.hypot(t[0]+l[0]-c[0],t[2]+l[2]-c[2]);m<f&&s.push(c[1]+Math.sqrt(f*f-m*m)-l[1])}if(!s.length)return S;let c=Math.min(...s),l=Math.min(S,c-$s*Math.log(s.reduce((e,t)=>e+Math.exp(-(t-c)/$s),0)))-.001,f=Math.min(1,(e-n)/8,(r-e)/8),m=f*f*(3-2*f);return t[1]+(l-t[1])*m};for(let e of[`l`,`r`])for(let t of o[e]){if(t.heel===void 0||!t.flat)continue;let e=p(_(t.yw),[0,0,1]),r=Math.max(n+1,t.a);for(;r<t.heel&&(C[r][0]-t.flat[0])*e[0]+(C[r][1]-t.flat[2])*e[2]<0;)r++;t.free=r}let T=[];for(let e=n;e<=r;e++)T[e]=w(e);let E=Math.max(2,Math.round(.25*i)),D=(e,t)=>{let n=e;for(let r=e;r<=t;r++)T[r]<T[n]&&(n=r);return n},O=[...new Set([`l`,`r`].flatMap(e=>o[e].filter(e=>e.strike&&e.a>n+2&&e.a<r-2).map(e=>e.a)))].sort((e,t)=>e-t),k=[{f:n,y:T[n]}];O.forEach((e,t)=>{let n=D(Math.max(k.at(-1).f+2,e-2),Math.min(r-2,e+E));if(t>0){let e=k.at(-1),t={f:n,y:T[n]},r=t.f-e.f,i=null;for(let n=Math.ceil(e.f+ec[0]*r);n<=Math.floor(e.f+ec[1]*r);n++){let r=T[n];for(let t=e.f+1;t<n;t++){let i=tc((t-e.f)/(n-e.f));i>1e-6&&(r=Math.min(r,e.y+(T[t]-e.y)/i))}for(let e=n+1;e<t.f;e++){let i=tc((e-n)/(t.f-n));i<1-1e-6&&(r=Math.min(r,(T[e]-t.y*i)/(1-i)))}(!i||r>i.y)&&(i={f:n,y:r})}i&&i.y>Math.max(e.y,t.y)&&k.push(i)}n>k.at(-1).f&&k.push({f:n,y:T[n]})}),k.push({f:r,y:T[r]});for(let e=1;e+1<k.length;e++){let t=k[e],n=Math.min(...[k[e-1],k[e+1]].map(e=>e.y+.008*(t.f-e.f)**2/2));t.y>Math.max(k[e-1].y,k[e+1].y)&&(t.y=Math.min(t.y,n))}let A=[];for(let e=0;e<30;e++){let e=0,t=cc(k.map(e=>e.f),k.map(e=>e.y),0,0);for(let i=0;i+1<k.length;i++){let a=k[i],o=k[i+1],s=0;for(let e=a.f;e<=o.f;e++)A[e]=t(e),s=Math.max(s,A[e]-T[e]);if(s>3e-4){let t=a.y>o.y&&i>0||o.f===r?a:o;t.f!==n&&t.f!==r&&(t.y-=s,e=Math.max(e,s))}}if(!e)break}for(let e=n;e<=r;e++)A[e]=Math.min(A[e],T[e]);let j=t.filter(e=>/ step \d+$/.test(e.src??``)).sort((e,t)=>e.f-t.f),M=0,N=s;for(let e=n+1;e<r;e++){for(;M<j.length&&j[M].f<=e;)N=j[M++].src;t.push({f:e,set:{root:[C[e][0],A[e],C[e][1]]},src:N})}}function sc(r,i,{end:o,period:s,pace:c,ground:l}){let{reach:u,hipOff:d,trail:f}=ic(r,s,c),p=Tr(i,o+1),m=new Map(i.map(e=>[e.f,e.src]));for(let r of[`l`,`r`])for(let s of l[r]){if(s.free===void 0||!Number.isFinite(s.b)||s.b>o)continue;let c=[],l=[],h=0;for(let e=s.free;e<=s.b;e++){let n=ac(p,e,d[r]),i=u(f((e-s.heel)/Math.max(1,s.b-s.heel))),o=e=>a(t(n,rc(s.ball,s.yw,e,s.flat)))-i,m=h,g=50;if(o(m)<=0)g=m;else if(o(g)<=0)for(let e=0;e<24;e++){let e=(m+g)/2;o(e)>0?m=e:g=e}h=g,c.push(g),l.push({f:e,hip:n,want:i})}let g=c.map((e,t)=>Math.max(...c.slice(t).map((e,t)=>e-t*15)));for(let e=0;e<2;e++)g=g.map((e,t)=>Math.max(c[t],((g[t-1]??0)+e+(g[t+1]??e))/3));l.forEach(({f:o,hip:c,want:l},u)=>{let d=rc(s.ball,s.yw,g[u],s.flat),f=a(t(c,d))-l;f>0&&(d=e(d,n(t(c,d),f/a(t(c,d))))),i.push({f:o,set:{[`foot_${r}`]:d,[`fpitch_${r}`]:g[u],[`fyaw_${r}`]:s.yw},src:m.get(o)})})}}function cc(e,t,n,r){let i=e.length-1,a=e.slice(1).map((t,n)=>t-e[n]),o=[],s=[],c=[],l=[];for(let e=0;e<=i;e++)e===0?(o[e]=0,s[e]=2*a[0],c[e]=a[0],l[e]=6*((t[1]-t[0])/a[0]-n)):e===i?(o[e]=a[i-1],s[e]=2*a[i-1],c[e]=0,l[e]=6*(r-(t[i]-t[i-1])/a[i-1])):(o[e]=a[e-1],s[e]=2*(a[e-1]+a[e]),c[e]=a[e],l[e]=6*((t[e+1]-t[e])/a[e]-(t[e]-t[e-1])/a[e-1]));for(let e=1;e<=i;e++){let t=o[e]/s[e-1];s[e]-=t*c[e-1],l[e]-=t*l[e-1]}let u=[];u[i]=l[i]/s[i];for(let e=i-1;e>=0;e--)u[e]=(l[e]-c[e]*u[e+1])/s[e];return n=>{let r=0;for(;r<i-1&&n>e[r+1];)r++;let o=n-e[r],s=a[r],c=(e[r+1]-n)/s,l=o/s;return c*t[r]+l*t[r+1]+((c**3-c)*u[r]+(l**3-l)*u[r+1])*s*s/6}}let lc=e=>e*180/Math.PI,uc=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)};function dc(i,o,s,c,l){let u=t(o,i),d=Math.min(c+l-1e-6,Math.max(Math.abs(c-l)+1e-6,a(u))),f=n(u,1/a(u)),p=t(s,n(f,r(s,f)));p=n(p,1/(a(p)||1));let m=(c*c+d*d-l*l)/(2*c*d),h=e(i,e(n(f,c*m),n(p,c*Math.sqrt(Math.max(0,1-m*m))))),g=t(o,h);return lc(Math.atan2(-r(g,s),-g[1]))}function fc(e,{end:n,ground:r,sk:i}){let o=Tr(e,n+2),s=new Map(e.map(e=>[e.f,e.src])),c=i?.index,l=i?.restWorldPos,u=i&&a(t(l[c.LeftLeg],l[c.LeftUpLeg])),d=i&&a(t(l[c.LeftFoot],l[c.LeftLeg]));for(let a of[`l`,`r`]){let f=r[a],m=a===`l`?`Left`:`Right`,h=i&&t(l[c[`${m}UpLeg`]],l[c.Hips]),v=i&&t(l[c[`${m}ToeBase`]],l[c[`${m}Foot`]]),y=i&&l[c[`${m}ToeBase`]][1],b=i&&[0,.005-l[c[`${m}Foot`]][1],-.045];for(let r=0;r+1<f.length;r++){let c=f[r],l=f[r+1];if(!Number.isFinite(c.b)||!l.strike||l.a>n||l.a-c.b<3)continue;let m=[...new Set(e.filter(e=>!nc.has(e)&&e.f>c.b&&e.f<l.a&&e.set[`foot_${a}`]).map(e=>e.f))].sort((e,t)=>e-t),x=[c.b,...m,l.a],S=o[`foot_${a}`],C=e=>S[Math.max(0,Math.min(S.length-1,e))],w=t(C(c.b),C(c.b-1)),T=t(C(l.a+1),C(l.a)),E=[0,1,2].map(e=>cc(x,x.map(t=>C(t)[e]),w[e],T[e])),D=e=>o[`fyaw_${a}`]?.[e]??0,O=e=>o[`fpitch_${a}`]?.[e]??0,k=(e,t)=>dc(ac(o,e,h),t,p(_(D(e)),[0,0,1]),u,d)-3,A=l.a-c.b,j=i&&O(c.b)-k(c.b,C(c.b)),M=i&&O(l.a)-k(l.a,C(l.a));for(let t=c.b+1;t<l.a;t++){let n=E.map(e=>e(t)),r={[`foot_${a}`]:n};if(i){let e=Math.min(50,k(t,n)+j*(1-uc((t-c.b)/(.35*A)))+M*(1-uc((l.a-t)/(.2*A)))),i=y+.03*uc(Math.min(t-c.b,l.a-t)/3),o=e=>n[1]+p(g(e),v)[1];if(o(e)<i){let t=-30,n=e;for(let e=0;e<20;e++){let e=(t+n)/2;o(e)<i?n=e:t=e}e=t}let s=O(t),u=e=>n[1]+p(g(e),b)[1],d=Math.min(.04,Math.max(u(s),.025));if(e<s&&u(e)<d){let t=e,n=s;for(let e=0;e<20;e++){let e=(t+n)/2;u(e)<d?t=e:n=e}e=n}r[`fpitch_${a}`]=e}let o={f:t,set:r,src:s.get(t)};nc.add(o),e.push(o)}}}}let pc=[6,4],mc=[18,3],hc=[6,3],gc=[.52,.64],_c=e=>c(.66-.18*e,.2,.48),vc=e=>c(.32-.11*e,0,.45),yc=e=>c(2.8-.45*e,1.2,3),bc=e=>c(4.7-1.1*e,1.8,4),xc=[.2,.6],Sc=e=>p(_(e),[0,0,1]),Cc=e=>p(_(e),[1,0,0]),wc=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)};function Tc(e,t){let n={};for(let r of[...e].sort((e,t)=>e.f-t.f))r.f<=t&&Object.assign(n,r.set);return n}function Ec(r,i,o,s=`walk`){let l=`${s} start`,u=[],d=(e,t)=>{let n={f:e,set:t,src:l};return u.push(n),n},f=Tc(i,r.f0);if(!f.foot_l||!f.foot_r||!f.root)throw Error(`MOTION_SPEC_INVALID: walk needs foot_l, foot_r and root keyed before f0`);if(r.vary!==void 0&&!(Number.isFinite(r.vary)&&r.vary>=0&&r.vary<=2))throw Error(`MOTION_SPEC_INVALID: ${s} vary must be a number from 0 (every step the same) to 2, got ${JSON.stringify(r.vary)}`);let m=o.index,h=o.restWorldPos,g=h[m.LeftFoot][1],v=h[m.Hips][1],y={l:t(h[m.LeftToeBase],h[m.LeftFoot]),r:t(h[m.RightToeBase],h[m.RightFoot])},b=[0,.005-g,-.045],x={l:-1/0,r:-1/0},S=a(t(h[m.LeftLeg],h[m.LeftUpLeg]))+a(t(h[m.LeftFoot],h[m.LeftLeg])),C=h[m.Hips][1]-h[m.LeftUpLeg][1],w=r.step??.5,T=r.halfWidth??.08,E=e=>Math.sqrt(Math.max(0,(.98*S)**2-e**2))+g+C-v,D={steps:8,step:w,period:20,swing:14,halfWidth:T,lift:.08,turnDistance:.4,firstStep:.4,pelvisHigh:Math.min(-.005,E(Math.hypot(.03,T-.035))),pelvisLow:E(Math.hypot(w/2,T)),vary:1,...r},O=D.step/D.period/(.5/20);D.armSwing=r.armSwing??c(6.4*O,4,22);let k=_c(O),A=vc(O),j=yc(O),M=bc(O),N=c(.9*O,0,3),P=mc[0]+mc[1]*Math.min(O,2.5),ee=hc[0]+hc[1]*Math.min(O,2.5),[F,I]=D.heading??[f.yaw??0,f.yaw??0],L=e=>F+(I-F)*wc(e/D.turnDistance),te=n(e(f.foot_l,f.foot_r),.5);te[1]=0;let R=[te];for(let t=0;t<4e3;t++)R.push(e(R[t],n(Sc(L(t*.01)),.01)));let ne=e=>R[Math.max(0,Math.min(R.length-1,Math.round(e/.01)))],re=(t,r)=>{let i=e(ne(t),n(Cc(L(t)),(r===`l`?1:-1)*D.halfWidth));return i[1]=g,i},ie={l:-.07,r:.07},z={foot_l:f.foot_l,foot_r:f.foot_r,fyaw_l:f.fyaw_l??F,fyaw_r:f.fyaw_r??F},ae=[`root`,`yaw`,`roll`,`foot_l`,`foot_r`,`fyaw_l`,`fyaw_r`,`spine_yaw`,`spine_roll`,`spine_pitch`,`aswing_l`,`aswing_r`,`abend_l`,`abend_r`,`wflex_l`,`wflex_r`,`head_yaw`],oe=f.roll??0,se=f.spine_roll??0,ce=f.spine_pitch??0,le={l:[{a:r.f0,b:1/0,strike:!1}],r:[{a:r.f0,b:1/0,strike:!1}]};d(r.f0,Object.fromEntries(ae.map(e=>[e,f[e]??z[e]??(e===`yaw`?F:0)])));let ue=ie.r,de=null,fe=Js(D,{s0:ue,turn:(e,t)=>L(t)-L(e),seed:[r.f0,D.steps,D.step,D.period,D.swing,D.firstStep,F,I,te[0].toFixed(3),te[2].toFixed(3)].join(`|`)});for(let i=0;i<D.steps;i++){l=`${s} step ${i+1}`;let a=i%2==0?`l`:`r`,o=a===`l`?`r`:`l`,u=i===D.steps-1,m=u?ue:ue+fe.inc[i],h=r.f0+16+fe.lift[i],g=h+fe.air[i],S=h+Math.floor(fe.air[i]/2);if(i===0){let t=e(ne(ie[o]),n(Cc(F),(o===`l`?1:-1)*D.halfWidth*.75));t[1]=f.root[1],d(h-4,{root:t,yaw:F}),d(h,{arm_hang:1})}let C=c(Math.abs(m-ie[a])/1.1,.3,1.15),I=50*(.55+.45*C)*(u?.8:1),te=z[`foot_${a}`],R=z[`fyaw_${a}`],ae=e(te,p(_(R),y[a])),pe=Math.max(h-Math.max(4,Math.round((i===0?.35:.3)*D.period)),x[a]+1,r.f0+1);Object.assign(le[a].at(-1),{b:h,heel:pe,ball:ae,yw:R,flat:te});for(let e=pe;e<=h;e++){let t=I*((e-pe)/Math.max(1,h-pe))**1.8;d(e,{[`foot_${a}`]:rc(ae,R,t,te),[`fpitch_${a}`]:t,[`fyaw_${a}`]:R})}let me=Math.max(1,Math.round(.22*fe.air[i])),he=re((ie[a]+m)/2,a);he[1]+=fe.height[i];let ge=rc(ae,R,Math.min(I,50),te),_e=e(ge,n(t(he,ge),.35));_e[1]=ge[1]+.18*C*Math.min(1,(fe.air[i]/10)**2)*(.55+.44999999999999996*Qs(O))*(fe.height[i]/D.lift);let ve=n(Cc(L(ie[o])),a===`l`?1:-1),ye=z[`foot_${o}`],be=e=>(e[0]-ye[0])*ve[0]+(e[2]-ye[2])*ve[2],xe=be(he)-be(_e);xe>0&&(_e[0]+=ve[0]*xe,_e[2]+=ve[2]*xe);let Se=h+Math.max(2,Math.round(.18*fe.air[i]));!u&&Se<S-1&&d(Se,{[`foot_${a}`]:_e,[`fpitch_${a}`]:.7*Math.min(I,50)}),d(S,{[`foot_${a}`]:he,[`fyaw_${a}`]:L((ie[a]+m)/2),[`fpitch_${a}`]:0});let Ce=ie[o]+(gc[0]+(gc[1]-gc[0])*Qs(O))*(m-ie[o]),we=de?de.s+(Ce-de.s)*((S-de.f)/(g-de.f)):null,Te=we===null?ie[o]+.03:we-.015*Math.abs(m-ie[a])/2,Ee=i===0?.015:.032,De=e(ne(Te),n(Cc(L(Te)),(o===`l`?1:-1)*Ee));De[1]=v+D.pelvisHigh;let Oe=r.pelvisLow??(fe.inc[i]>w?Math.min(D.pelvisLow,E(Math.hypot(fe.inc[i]/2,T))):D.pelvisLow),ke=ne(Ce).slice();ke[1]=v+(u?D.pelvisHigh:Oe),de={f:g,s:Ce};let Ae=re(m,a),je=L(m),Me=e(Ae,p(_(je),b)),Ne=-(pc[0]+(pc[1]-pc[0])*Qs(O))*(u?.5:1),Pe=Math.max(2,Math.round(.2*D.period)),Fe=rc(Me,je,Ne,Ae),Ie=e(Fe,n(Sc(je),-.12*Math.abs(m-ie[a])));Ie[1]+=.03,!u&&me>1&&g-me>S+1&&d(g-me,{[`foot_${a}`]:Ie,[`fpitch_${a}`]:.6*Ne});for(let e=0;e<=Pe;e++){let t=Ne*(1-wc(e/Pe));d(g+e,{[`foot_${a}`]:rc(Me,je,t,Ae),[`fpitch_${a}`]:t,[`fyaw_${a}`]:je})}x[a]=g+Pe,le[a].push({a:g,b:1/0,strike:!0}),z[`foot_${a}`]=Ae,z[`fyaw_${a}`]=je,ie[a]=m;let Le=(u?0:i<3?[0,.3,.7][i]:1)*fe.arm[i],Re=a===`l`?1:-1,ze=c(C,.4,1.1),Be=u?0:i===0?.5:1,Ve=4.5*ze*Be*(o===`l`?1:-1),He=(g-S)/D.period,Ue=e=>{let t=[Math.cos(Math.PI*(e-He)),Math.cos(Math.PI*e)];return t.map(e=>e/Math.max(...t.map(Math.abs)))},We=Ue(-A).map(e=>-4.5*ze*Re*Le*e),Ge=Ue(k).map(e=>5.5*ze*Re*Le*e),Ke=Ue(.92).map(e=>-Math.sign(Ve)*j*ze*Be*e),qe=Ue(He).map((e,t)=>t?.8*e:e),Je=e=>({aswing_l:-.85*D.armSwing*Re*Le*e,aswing_r:D.armSwing*Re*Le*e,abend_l:Le*(P-Re*ee*e),abend_r:Le*(P+Re*ee*e)});d(S,{root:De,yaw:L(Te)+We[0],roll:oe+Ve,spine_roll:se-Ve*1.26+Ke[0],spine_yaw:Ge[0]-We[0],...Je(qe[0]),wflex_l:8*Le,wflex_r:8*Le}),d(g,{root:ke,yaw:L(Ce)+We[1],roll:oe,spine_roll:se+Ke[1],spine_yaw:Ge[1]-We[1],...Je(qe[1]),head_yaw:0});let Ye=ce+N*Be,Xe=M*ze*Be;d(Math.max(r.f0+1,g-Math.round(xc[1]*D.period)),{spine_pitch:Ye-.6*Xe}),d(Math.max(r.f0+1,g-Math.round(xc[0]*D.period)),{spine_pitch:Ye+Xe}),ue=m}let pe=r.f0+16+D.steps*D.period+18,me=Math.max(...u.map(e=>e.f));return r.pelvisHigh===void 0&&r.pelvisLow===void 0&&(fc(u,{end:me,ground:le}),oc(o,u,{f0:r.f0,end:me,period:D.period,pace:O,ground:le,start:`${s} start`}),sc(o,u,{end:me,period:D.period,pace:O,ground:le}),fc(u,{end:me,ground:le,sk:o})),l=`${s} end`,d(pe,Tc(u,pe)),u}function Dc(e,t){let n=(e.keys??[]).map((e,t)=>e.src?e:{...e,src:`keys[${t}]`});return(e.generators??[]).forEach((e,r)=>{if(e.type!==`walk`)throw Error(`MOTION_SPEC_INVALID: generators[${r}] has unknown type "${e.type}" (only "walk")`);n=n.concat(Ec(e,n,t,`generators[${r}] walk`))}),n}let Oc={yaw:.5,pitch:.2,roll:.7},kc={yaw:4,pitch:6,roll:10},Ac=[`yaw`,`pitch`,`roll`];function jc(e){let t=new Map;for(let n of e){let e=/ (start|step \d+)$/.exec(n.src??``),r=e&&n.src.slice(0,-e[0].length);if(!e||!/\bwalk(To)?$/.test(r))continue;let i=t.get(r)??[1/0,-1/0];t.set(r,[Math.min(i[0],n.f),Math.max(i[1],n.f)])}return[...t.values()].filter(([e,t])=>t>e)}let Mc=(e,t)=>{let n=Math.floor(t/2),r=t=>e[Math.max(0,Math.min(e.length-1,t))];return e.map((e,t)=>{let i=0;for(let e=t-n;e<=t+n;e++)i+=r(e);return i/(2*n+1)})};function Nc(e,t,n){let r=jc(t);if(!r.length)return;let i=t=>e[t]??Array(n).fill(0);for(let a of Ac){let o=i(`spine_${a}`),s=i(a).map((e,t)=>e+o[t]),c=Mc(s,33),l=null;for(let[e,o]of r)if(!t.some(t=>t.f>=e&&t.f<=o&&t.set?.[`head_${a}`]!==void 0&&!/\bwalk(To)? (start|step \d+|end)$/.test(t.src??``))){l??=i(`head_${a}`).slice();for(let t=Math.max(0,e);t<=Math.min(n-1,o);t++){let n=Math.min(1,(t-e)/8,(o-t)/8),r=Math.max(-kc[a],Math.min(kc[a],s[t]-c[t]));l[t]-=Oc[a]*r*n*n*(3-2*n)}}l&&(e[`head_${a}`]=l)}}function Pc(e,t){let n=e.length,r=new Float64Array(n);for(let i=0;i<n;i++)if(e[i]>0)for(let a=Math.max(0,i-t-1);a<=Math.min(n-1,i+t+1);a++){let n=1-Math.abs(i-a)/(t+1),o=n*n*(3-2*n);r[a]=Math.max(r[a],e[i]*o)}return r}let Fc=e=>Math.round(e*1e4)/1e4;function Ic(e,n){let{sk:i}=e.ctx,o=i.index,s=i.restWorldPos,c=e.baked.root;if(!c)return null;let l=e.frames.length,u=new Float64Array(l);for(let n=0;n<l;n++){let{s:i,diag:c,c:l}=e.frames[n];for(let[e,d]of Object.entries(W)){if(!(c[`leg_miss_${e}`]>5e-4)||!Array.isArray(l[`foot_${e}`]))continue;let f=.999*(a(t(s[o[`${d}Leg`]],s[o[`${d}UpLeg`]]))+a(t(s[o[`${d}Foot`]],s[o[`${d}Leg`]]))),p=t(l[`foot_${e}`],i.P[o[`${d}UpLeg`]]),m=p[1]*p[1]-r(p,p)+f*f,h=m>=0?-p[1]-Math.sqrt(m):-p[1];h>0&&h<=.05&&(u[n]=Math.max(u[n],h+5e-4))}}let d=u.filter(e=>e>0).length;if(!d)return null;let f=Pc(u,6),p=[];for(let t=0;t<l;t++){if(!(f[t]>0))continue;let n=e.f0+t,r=c[n];c[n]=[r[0],r[1]-f[t],r[2]],p.push(n)}return n.solve(p),{frames:d,eased:p.length,max:Fc(Math.max(...u)),how:`hips lowered`}}function Lc(e){let t=0,n=0;for(let r of e.frames)for(let e of[`l`,`r`]){let i=r.diag[`reach_over_${e}`]??0;i>.001&&(t++,n=Math.max(n,i))}return t?{frames:t,max:Fc(n),how:`wrist target clamped to reach`}:null}let Rc=.01;function zc(n,r){let i=n.frames.length,a=Ai.plantH,o=0,s=0,c=0,l=new Set;for(let[r,u]of[[`l`,`Left`],[`r`,`Right`]]){let d=n.baked[`foot_${r}`];if(!d)continue;let f=n.frames.map((e,t)=>ii(n,t,u)),p=(e,t)=>f[e][t][1]<a&&f[e-1][t][1]<a&&Math.abs(f[e][t][1]-f[e-1][t][1])<5e-4,m=Array.from({length:i},()=>null);for(let n=1;n<i;){if(!p(n,`ball`)&&!p(n,`heel`)){n++;continue}let r=null,l=null,u=n,d=0,h=[0,0,0],g=!1,_=[],v=e=>{let n=t(l,f[e][r]);n[1]=0,h=n,_.push([e,n]),d=Math.max(d,Math.hypot(n[0],n[2]))};for(;u<i&&(p(u,`ball`)||p(u,`heel`));u++){let t=p(u,`ball`)?`ball`:`heel`;g||=f[u][t===`ball`?`heel`:`ball`][1]>=a,t!==r&&(r=t,l=e(f[u-1][r],h)),v(u)}if(g&&d>=Rc)for(;u<i&&f[u][r][1]<.03;u++)v(u);if(d>2e-4&&(d<Rc||g)){o++,c=Math.max(c,d),d>=Rc&&s++;for(let[e,t]of _)m[e]=t;let e=Math.max(4,Math.ceil(Math.hypot(h[0],h[2])/.02));for(let t=1;t<=e&&u-1+t<i&&!m[u-1+t];t++){let n=t/(e+1),r=1-n*n*(3-2*n);m[u-1+t]=[h[0]*r,0,h[2]*r]}}n=u}for(let t=0;t<i;t++){if(!m[t])continue;let r=n.f0+t;d[r]=e(d[r],m[t]),l.add(r)}}return o?(r.solve([...l]),{plants:o,...s?{pivots:s}:{},frames:l.size,max:Fc(c),how:s?`planted heel/ball held in place; turning feet pivot on it`:`planted heel/ball held in place`}):null}let Bc=e=>e*e*(3-2*e);function Vc(e,t){let n=e.frames.length,r=Ai.plantH,i=new Set,a=0;for(let[t,o]of[[`l`,`Left`],[`r`,`Right`]]){let s=e.baked[`fyaw_${t}`]??e.baked.yaw;if(!e.baked[`foot_${t}`]||!s)continue;let c=e.frames.map((t,n)=>ii(e,n,o)),l=(e,t)=>c[e][t][1]<r&&c[e-1][t][1]<r&&Math.abs(c[e][t][1]-c[e-1][t][1])<5e-4,u=e=>e>0&&e<n&&l(e,`heel`)&&l(e,`ball`),d=Array.from({length:n},(t,n)=>s[e.f0+n]),f=d.slice();for(let e=1;e<n;e++){if(!u(e))continue;let t=f[e-1],r=e,i=0;for(;u(r);r++)i=Math.max(i,Math.abs(f[r]-t));if(i<=1){e=r;continue}a=Math.max(a,i);for(let n=e;n<r;n++)f[n]=t;let o=r<n?d[r]-t:0,s=Math.max(4,Math.ceil(Math.abs(o)/6));for(let e=0;o&&e<s&&r+e<n&&!u(r+e);e++)f[r+e]=d[r+e]-o*(1-Bc((e+1)/(s+1)));e=r}let p=f.map((e,t)=>e!==d[t]);if(!p.some(Boolean))continue;let m=e.baked[`fyaw_${t}`]=s.slice();p.forEach((t,n)=>{t&&(m[e.f0+n]=f[n],i.add(e.f0+n))})}return i.size?(t.solve([...i]),{frames:i.size,max:Fc(a)}):null}function Hc(e,t){let{sk:n}=e.ctx,r=e.frames.length,i=new Set,a=0;for(let[t,o]of Object.entries(W)){let s=e.baked[`foot_${t}`],c=n.index[`${o}Foot`];if(!s)continue;let l=e.frames.map(({s:t},r)=>{let i=xt(n,t,o),a=Math.min(i.heel[1],i.ball[1],i.tip[1])-t.P[c][1]+s[e.f0+r][1];return Math.min(.15,-a-.001)});if(!l.some(e=>e>0))continue;let u=Pc(l,2);for(let t=0;t<r;t++){if(!(u[t]>0))continue;let n=e.f0+t;s[n]=[s[n][0],s[n][1]+u[t],s[n][2]],a=Math.max(a,l[t]),i.add(n)}}return i.size?(t.solve([...i]),{frames:i.size,max:Fc(a),how:`soles lifted onto the floor`}):null}let Uc={l:ht.filter(e=>e[0]===`hand_l`||e[0]===`farm_l`),r:ht.filter(e=>e[0]===`hand_r`||e[0]===`farm_r`)};function Wc(t,r,i,a,o){let{sk:s}=t.ctx,c=s.index,l=t.frames.length,u=Array.from({length:l},()=>null);for(let s=0;s<l;s++){let{s:l,c:d}=t.frames[s],f=t.objectsAt(s),p=null;for(let[t,s]of Object.entries(f)){if(!s.half||s.ignore)continue;let u=null;for(let[e,n,d,f]of Uc[r]){let r=ji(a,e,t)??Ai.allow,p=gt(l.P[c[n]],l.P[c[d]]),m=p.map(e=>B(e,s)),h=Math.min(...m),g=f-h-r;if(!(g<=0)){if(g>i){o.deep++;continue}(!u||g>u[0])&&(u=[g,p[m.indexOf(h)]])}}u&&(p=e(p??[0,0,0],n(St(u[1],s),u[0]+.002)))}if(p){if(!(d[`armw_${r}`]>1e-4)){o.skipped++;continue}u[s]=p}}return u}function Gc(t,r,i){let o=t.ctx.spec.checks?.allow,s=t.frames.length,c={deep:0,skipped:0},l=new Set,u=0;for(let d=0;d<3;d++){c.deep=0,c.skipped=0;let d=new Set;for(let r of[`l`,`r`]){let f=Wc(t,r,i,o,c),p=t.ctx.push[r],m=Array.from({length:s},()=>null);for(let e=0;e<s;e++)if(f[e]){l.add(`${r}${e}`);for(let t=Math.max(0,e-4-1);t<=Math.min(s-1,e+4+1);t++){let r=1-Math.abs(e-t)/5,i=n(f[e],r*r*(3-2*r));(!m[t]||a(i)>a(m[t]))&&(m[t]=i)}}for(let n=0;n<s;n++){if(!m[n])continue;let r=t.f0+n,i=e(p.get(r)??[0,0,0],m[n]);p.set(r,i),u=Math.max(u,a(i)),d.add(r)}}if(!d.size)break;r.solve([...d])}return!l.size&&!c.skipped?null:{frames:l.size,max:Fc(u),skipped:c.skipped,tooDeep:c.deep,how:`hand pushed out along the box normal`}}let Kc=e=>{let t=e.ctx.spec?.checks?.limits;return Ci(e.ctx.sk,e,t,{...Ai,...t})};function qc(e,t,n){let r=[];for(let i=1;i<n;i++){if(e[i]<=1)continue;let a=i;for(;a+1<n&&e[a+1]>1;)a++;let o=Math.max(6,2*(a-i+1)),s=Math.max(0,...t.filter(e=>e<=i-1-o)),c=Math.min(n-1,...t.filter(e=>e>=a+o));r.length&&s<=r[r.length-1][1]?r[r.length-1][1]=Math.max(r[r.length-1][1],c):r.push([s,c]),i=a}return r}function Jc(e,t){let n=t[t.length-1]-t[0],r=t.slice(1).map((n,r)=>{let i=0;for(let a=Math.floor(t[r])+1;a<=Math.ceil(n);a++)i=Math.max(i,e[a]??0);return{len:n-t[r],need:1.1*i}}),i=e=>r.reduce((t,n)=>t+n.len*Math.max(n.need,e),0),a=.35,o=1;for(let e=0;e<40;e++){let e=(a+o)/2;i(e)>n?o=e:a=e}let s=r.map(e=>e.len*Math.max(e.need,a)),c=n/s.reduce((e,t)=>e+t,0),l=[t[0]];return s.forEach((e,t)=>l.push(l[t]+e*c)),l[l.length-1]=t[t.length-1],{T:l,stretch:Math.max(...s.map((e,t)=>e*c/r[t].len))}}function Yc(e,t){let n=e.frames.length,r=[],i=0,a=1;for(let o=0;o<3;o++){let o=Kc(e),s=[...new Set(e.keys.map(e=>e.f))].sort((e,t)=>e-t),c=qc(o,s,n);if(!c.length)break;let l=c.map(([e,t])=>{let n=[...new Set([e,...s.filter(n=>n>e&&n<t),t])].sort((e,t)=>e-t),r=Jc(o,n);return a=Math.max(a,r.stretch),{cuts:n,T:r.T}});i+=c.length;let u=e=>{for(let{cuts:t,T:n}of l){if(e<t[0]||e>t[t.length-1])continue;let r=0;for(;r<t.length-2&&e>t[r+1];)r++;return n[r]+(n[r+1]-n[r])*(e-t[r])/(t[r+1]-t[r])}return e};r.push(u),t.rebake(e.keys.map(e=>({...e,f:u(e.f)}))),t.solve(t.all())}if(!r.length)return null;e.timeMap=e=>r.reduce((e,t)=>t(e),e);let o=Kc(e).filter(e=>e>1).length;return{windows:i,maxStretch:Math.round(a*100)/100,stillFast:o,how:`key intervals around speed spikes stretched`}}let Xc=Array.from({length:19},(e,t)=>-90+t*10),Zc=.003,Qc=.08;function $c(e,t,n){let r=null;for(let i of e.ctx.spec.checks?.below??[]){let a=String(i.joint).replace(/^joint:/,``),o=String(i.ref).replace(/^joint:/,``),s=e.f0+t;(Array.isArray(i.when)?s>=i.when[0]&&s<=i.when[1]:!i.when||(e.frames[t].c[i.when]??0)>.5)&&a===`${n}ForeArm`&&o===`${n}Arm`&&Number.isFinite(i.min)&&(r=Math.max(r??0,i.min))}return r}let el={l:`Left`,r:`Right`};function tl(e,t,n){let r=0;for(let i of e.fits??[]){let a=e.anchors?.[i.anchor],o=t[`w_${i.anchor}_${i.side}`]??0;(i.side===n||a?.kind===`prop`&&e.props?.[a.prop]?.hand===n)&&(r=Math.max(r,o))}return Math.min(1,r)}let nl=Object.fromEntries(ht.map(e=>[e[0],e[3]]));function rl(e,t,n,r){let i=0;for(let a of e)i=Math.max(i,r-Math.min(...gt(t,n,5).map(e=>B(e,a))));return i}function il(n,r,i,a,o,s=[]){let c=r.index,l=c[`${a}Arm`],u=c[`${a}ForeArm`],d=i.P[l],f=i.P[c[`${a}Hand`]],m=o?e(d,p(y(t(f,d),o),t(i.P[u],d))):i.P[u],h=Math.max(qe(n,r,i,l,d,m),qe(n,r,i,u,m,f)),g=a===`Left`?`l`:`r`;return{depth:h,scene:s.length?Math.max(rl(s,d,m,nl[`uarm_${g}`]),rl(s,m,f,nl[`farm_${g}`])):0,rise:Math.max(0,m[1]-(d[1]-.09)),under:d[1]-m[1]}}let al=(e,n)=>{let r=e.index,i=e.restWorldPos;return a(t(i[r[`${n}ForeArm`]],i[r[`${n}Arm`]]))+a(t(i[r[`${n}Hand`]],i[r[`${n}ForeArm`]]))};function ol(n,r,i){let{sk:o}=n.ctx,s=el[i],c=n.frames.length,l=n.ctx.swivel[i],u=n.frames.map(({s:c,c:u},d)=>{let f=Zo(u[`armw_${i}`]??0),m=(l.get(n.f0+d)??0)*f;if(!m&&il(r,o,c,s,0).depth<=Zc)return null;let h=c.P[o.index[`${s}Arm`]],g=al(o,s)+.15,_=Object.values(n.objectsAt(d)).filter(n=>n.half&&!n.ignore&&a(t(e(n.pos,p(n.R,[0,n.half[1],0])),h))-a(n.half)<g),v=il(r,o,c,s,-m,_).scene,y=$c(n,d,s);return Xc.map(e=>{let{depth:t,scene:n,rise:i,under:a}=il(r,o,c,s,e*f-m,_),l=Math.max(0,t-Zc)*100,u=y===null?0:Math.max(0,y-a),d=Math.max(0,n-Math.max(v,Zc))*100;return 40*(l*l+d*d)+.05*(e/10)**2+4*(i*100)**2+400*(u*100)**2})});if(u.every(e=>!e))return null;let d=Xc.indexOf(0),f=Xc.map((e,t)=>t===d?0:1/0),m=Xc.map(()=>0),h=[];for(let e=0;e<c;e++){let t=u[e]??f,n=m,r=[];m=t.map((e,t)=>{let i=1/0,a=d;for(let e=0;e<Xc.length;e++){if(Math.abs(Xc[t]-Xc[e])>10)continue;let r=n[e]+2*((Xc[t]-Xc[e])/10)**2;r<i&&(i=r,a=e)}return r.push(a),i+e}),h.push(r)}let g=Array(c),_=m.indexOf(Math.min(...m));for(let e=c-1;e>=0;e--)g[e]=Xc[_],_=h[e][_];return g.map((e,t)=>(g[Math.max(0,t-1)]+2*g[t]+g[Math.min(c-1,t+1)])/4)}function sl(e,t,r,i){let{sk:o}=e.ctx,s=el[r],c=o.index,l=e.frames.length,u=Array(l).fill(null),d=[c.Head,c.Neck,c.HeadTop_End].filter(e=>e!==void 0);for(let f=0;f<l;f++){let{s:l,c:p}=e.frames[f],m=e.f0+f,h=1-tl(e.ctx.spec,p,r);if(h<.05)continue;let g=null;for(let n of[c[`${s}Hand`],c[`${s}ForeArm`]]){let r=Je(t,o,l,n);if(r.into<0||d.includes(r.into))continue;let i=Math.min(.03,ji(e.ctx.spec.checks?.allow,Zr(o,n),Zr(o,r.into))??0),a=r.depth-Math.max(i,Zc);a>0&&(!g||a>g.past)&&(g={...r,b:n,past:a})}if(!g)continue;if(!(p[`armw_${r}`]>1e-4)){i.skipped++;continue}let _=a(e.ctx.skinPush[r].get(m)??[0,0,0]);if(_>=Qc){i.deep++;continue}let v=Le(t,l,g.p,new Set([...t.skip.get(g.b),...d]));v&&(u[f]=n(v,h*Math.min(.03,Qc-_,g.past+.003)))}let f=Array(l).fill(null);return u.forEach((e,t)=>{if(e)for(let r=Math.max(0,t-5-1);r<=Math.min(l-1,t+5+1);r++){let i=1-Math.abs(t-r)/6,o=n(e,i*i*(3-2*i));(!f[r]||a(o)>a(f[r]))&&(f[r]=o)}}),f}function cl(t,r){let i=Me(t.ctx.sk);if(!i)return null;let o=t.frames.length,s={skipped:0,deep:0},c=new Set,l=new Set,u=0,d=0;for(let n=0;n<3;n++){let n=new Set;for(let e of[`r`,`l`]){let r=ol(t,i,e);r&&r.forEach((r,i)=>{let a=t.f0+i,o=t.ctx.swivel[e].get(a)??0,s=Math.abs(r)<.5?0:r;Math.abs(s-o)<.5||(s?t.ctx.swivel[e].set(a,s):t.ctx.swivel[e].delete(a),u=Math.max(u,Math.abs(s)),s&&c.add(`${e}${i}`),n.add(a))})}n.size&&r.solve([...n]),n=new Set,s.skipped=0,s.deep=0;for(let r of[`r`,`l`])sl(t,i,r,s).forEach((i,o)=>{if(!i)return;let s=t.f0+o,c=e(t.ctx.skinPush[r].get(s)??[0,0,0],i);t.ctx.skinPush[r].set(s,c),d=Math.max(d,a(c)),l.add(`${r}${o}`),n.add(s)});if(!n.size)break;r.solve([...n])}if(l.size){let i=new Set;for(let r of[`r`,`l`]){let s=e=>t.ctx.skinPush[r].get(t.f0+e)??[0,0,0];for(let c=0;c<2;c++){let c=Array.from({length:o},(e,t)=>s(t));c.forEach((s,l)=>{let u=[-2,-1,0,1,2].reduce((t,r,i)=>e(t,n(c[Math.min(o-1,Math.max(0,l+r))],[1,4,6,4,1][i]/16)),[0,0,0]);a(u)<1e-5&&!t.ctx.skinPush[r].has(t.f0+l)||(t.ctx.skinPush[r].set(t.f0+l,u),i.add(t.f0+l))})}}i.size&&r.solve([...i])}return!c.size&&!l.size&&!s.skipped?null:{elbowFrames:c.size,maxTurn:Math.round(u),handFrames:l.size,maxPush:Fc(d),skipped:s.skipped,tooDeep:s.deep,how:`elbow turned about the shoulder-wrist line; a hand a little inside pushed out along the skin`}}let ll=[`reach`,`slide`,`penetration`,`self`,`retime`,`stance`,`grasp`];function ul(e){if(e===!1)return{reach:!1,slide:!1,penetration:0,self:!1,retime:!1,stance:!1,grasp:!1};let t=e??{},n=t.penetration===!0?.02:Math.min(.05,Math.max(0,Number(t.penetration)||0));return{reach:t.reach!==!1,slide:t.slide!==!1,penetration:n,self:t.self!==!1,retime:t.retime===!0,stance:t.stance!==!1,grasp:t.grasp!==!1}}let dl=(e,t)=>e?{...t??{frames:0,how:`planted heel/ball held in place`},turns:e,how:`${t?.how??`planted heel/ball held in place`}; a flat planted foot keeps its heading until it lifts`}:t,fl=(e,t)=>e&&t?{frames:Math.max(e.frames,t.frames),eased:Math.max(e.eased,t.eased),max:Math.max(e.max,t.max),how:e.how}:e??t;function pl(e,t,n,{whole:r=!0}={}){let i={},a=(e,t)=>{t&&(i[e]=t)};return t.retime&&r&&a(`retime`,Yc(e,n)),t.slide&&a(`ground`,Hc(e,n)),t.reach&&a(`reachLegs`,Ic(e,n)),t.slide&&a(`slide`,dl(Vc(e,n),zc(e,n))),t.reach&&i.slide?.pivots&&a(`reachLegs`,fl(i.reachLegs,Ic(e,n))),t.penetration>0&&a(`penetration`,Gc(e,n,t.penetration)),t.self&&a(`self`,cl(e,n)),t.reach&&a(`reachArms`,Lc(e)),i}function ml(e){let t=Ct(e),n=e.half;return t.kind===`sphere`?4/3*Math.PI*t.r**3:t.kind===`cylinder`?Math.PI*t.r*t.r*t.h:8*n[0]*n[1]*n[2]}function hl(e,t,n){let r=e?.objects?.[n]?.mass;if(Number.isFinite(r))return r;let i=U(t,n).map(e=>t.objects[e]);if(i.length===1&&Ct(i[0]).kind===`sphere`&&2*Ct(i[0]).r<.12)return .15;let a=i.reduce((e,t)=>e+ml(t)*400,0);return Math.min(60,Math.max(.02,a))}let gl=9.81,_l=e=>e?.fix!==!1&&e?.fix?.grasp!==!1,vl=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)},yl=e=>Object.entries(e.objects??{}).filter(([,t])=>K(t)&&e.props?.[t.prop]).map(([t,n])=>[t,n,e.props[n.prop].hand]);function bl(r,i,o,c,l,u){let{spec:d,scene:f,sk:p}=r;if(!_l(d)||!(u>.001))return null;for(let[m,h,g]of yl(d)){if(g!==i||h.keys?.length)continue;let _=hl(d,f,m),[v,y]=r.carrySpan(h);if(o<v||o>y||_*gl*Jn(p,i)<2)continue;let b=vl((o-v)/4)*vl((y-o)/4)*u,x=Math.min(.06,.006*_)*b;if(!(x>0))return null;let S=t(c.P[p.index[`${W[i]}Arm`]],l),C=a([S[0],0,S[2]])>1e-6?s([S[0],0,S[2]]):[0,0,0];return e([0,-x,0],n(C,x))}return null}let xl=(e,n,r,i)=>{let a=t(n.P[e.index[`${W[r]}Hand`]],n.P[e.index[`${W[r]}Arm`]]);return i*gl*Math.hypot(a[0],a[2])};function Sl(e,t,n){let{spec:r,scene:i}=t.ctx,a=t.f0??0;if(!_l(r))return;let o=yl(r).map(([e,t,n])=>({id:e,o:t,k:n,mass:hl(r,i,e)}));o.length&&t.frames.forEach(({s:r},i)=>{let s=i+a;for(let i of o)Ro(t,i.id,s)===i.k&&n({...i,f:s,Nm:xl(e,r,i.k,i.mass)})})}function Cl(e,t){let n={};Sl(e,t,({id:e,k:t,mass:r,f:i,Nm:a})=>{let o=n[`hand_${t}`]??={object:e,mass:Math.round(r*1e3)/1e3,maxNm:0,at:i,frames:[i,i]};a>o.maxNm&&(o.maxNm=a,o.at=i),o.frames[1]=i});for(let e of Object.values(n))e.maxNm=Math.round(e.maxNm*10)/10;return Object.keys(n).length?n:null}function wl(e,t,n,r,i){let a=r===`r`?`l`:`r`,{spec:o,scene:s}=e.ctx,c=new Set([t,...U(s,t)]);return Object.entries(o.anchors??{}).some(([t,r])=>(e.baked?.[`w_${t}_${a}`]?.[i]??0)>.5&&(r.kind===`object`&&c.has(r.object)||r.kind===`prop`&&r.prop===n.prop))}function Tl(e,t){let n=[];return Sl(e,t,({id:e,o:r,k:i,f:a,Nm:o})=>{o>25&&!wl(t,e,r,i,a)&&n.push({kind:`LOAD`,msg:`${W[i].toLowerCase()} holds ${e} at ${o.toFixed(0)} N m`,frame:a,value:Math.round(o*10)/10,limit:25})}),n}function El(r,i,o){let{sk:s,scene:c}=r.ctx,l=r.frames[o-(r.f0??0)]?.s,u=zo(r,i,o),d=Pt(c,i),f=c.objects[U(c,i)[0]]??c.objects[i];if(!l||!u||!f?.half)return null;let h=e=>p(m(u.R),t(e,u.pos)),g=p(m(d.R),t(e(f.pos,p(f.R,[0,f.half[1],0])),d.pos)),_=h(n(e(l.P[s.index.RightHand],l.P[s.index.RightHandMiddle1]),.5)),v=h(l.P[s.index.LeftArm]),y=[0,1,2].flatMap(e=>[-1,1].map(t=>{let n=g.slice();return n[e]+=t*f.half[e],{p:n,i:e,sg:t}})).filter(e=>!(e.i===1&&e.sg<0)),b=y.reduce((e,t)=>Math.abs(_[t.i]-t.p[t.i])<Math.abs(_[e.i]-e.p[e.i])?t:e);return y.filter(e=>e!==b).reduce((e,n)=>a(t(n.p,v))<a(t(e.p,v))?n:e).p.map(e=>Math.round(e*1e3)/1e3)}let Dl={LOAD(e,t){let[,n,r]=/^(left|right) holds (\S+) at/.exec(t.subject)??[],i=e.spec?.objects?.[r];if(!i||!e.out)return null;let a=n===`left`?`l`:`r`,o=a===`r`?`left`:`right`,[s]=To(e.out,i),c=hl(e.spec,e.out.ctx.scene,r),l={cause:`objects.${r} (${c.toFixed(1)} kg)`,hint:`${r} weighs ${c.toFixed(1)} kg: ${t.worst.toFixed(0)} N m on the ${n} arm at f${t.worstFrame} (one hand holds 25); put the ${o} hand on it too (a reachGrip on an object anchor on ${r} from f${s}), or carry it closer to the body`};if(a!==`r`)return l;let u=El(e.out,r,Math.max(s,Math.min(t.worstFrame,To(e.out,i)[1])));if(!u)return l;let d=`${r}Other`,f={kind:`object`,object:r,local:u,grasp:`palm`};return{...l,fix:{ops:t=>[t.anchors?{op:`add`,path:`/anchors/${d}`,value:f}:{op:`add`,path:`/anchors`,value:{[d]:f}},{op:`add`,path:`/use/-`,value:{skill:`reachGrip`,f0:Math.max(0,s-10),args:{anchor:d,side:`l`,reach:Math.min(10,s)||1,hold:Math.max(1,To(e.out,i)[1]-s),release:1}}}]}}}},Ol=e=>e*e*(3-2*e);function kl(e,t,n,r=()=>void 0){let i=new Map,a=new Map,o=r(),s=s=>{if(r()!==o&&(o=r(),i.clear(),a.clear()),i.has(s))return i.get(s);let c={};for(let[r,i]of Object.entries(t?.objects??{})){let a=[r,...i.ancestors??[]].find(t=>ds(e.objects?.[t])),l=e.objects?.[a];c[r]=l?K(l)||ls(l)?{...i,carried:a}:$a(l)?{...i,...q(vo({spec:e,scene:t,timeMap:o},a,s),hs(t.objects[a]??En(t)[a],i))}:{...i,...gs(l,i,n,s)}:i}return i.set(s,c),c};return{all:s,obstacles:e=>(r()!==o&&s(e),a.has(e)||a.set(e,Object.fromEntries(Object.entries(s(e)).filter(([,e])=>!e.carried))),a.get(e))}}function Al(t,r,i,a){if(a.prop)return i.props?.[a.prop]?.[a.point];let o=a.joints??[`RightHandIndex2`,`RightHandMiddle2`];return n(o.map(e=>r.P[t.index[e]]).reduce((t,n)=>e(t,n),[0,0,0]),1/o.length)}function jl(i,o,c,l=!1,u=!0,d=null){let{sk:f}=i;return{softReach:i.softReach,foldFree:l,hold:u&&i.hold?(e,t)=>e===d?null:i.hold.lead(e,c,t,o):void 0,push:l?void 0:e=>i.push?.[e]?.get(c),swivel:e=>i.swivel?.[e]?.get(c),skinPush:l?void 0:e=>i.skinPush?.[e]?.get(c),handTarget(l,u,d){d.frame=c;let m=u.P[f.index[l===`l`?`LeftHand`:`RightHand`]],h=[];for(let[t,n]of Object.entries(o)){let r=/^w_(.+)_([lr])$/.exec(t);!r||r[2]!==l||!(n>1e-6)||h.push([n,e(Xn(i,r[1],u,d,o,l),o[`fit_${r[1]}_${l}`]??[0,0,0])])}let g=h.reduce((e,[t])=>e+t,0);if(g>1){for(let e of h)e[0]/=g;g=1}let _=n(o[`hand_${l}`]??m,1-g);for(let[t,r]of h)_=e(_,n(r,t));let v=Math.max(0,...h.map(([e])=>Ol(Math.min(1,Math.min(e,1-e)*3))));if(v>0){let c=u.P[f.index.Spine],l=u.P[f.index.Neck],d=t(l,c),m=Math.min(1,Math.max(0,r(t(_,c),d)/r(d,d))),h=e(c,n(d,m)),g=i.spec.detourRadius??.25,b=t(_,h),x=a(b);if(x<g){let r=p(y([0,1,0],o.yaw??0),[0,0,1]),i=s(e(x>1e-6?n(b,1/x):r,n(r,.6)));_=e(_,n(t(e(h,n(i,g)),_),v))}}return _},keepOut:i.keepOut&&!l?(r,a,s)=>{let c=(1-tl(i.spec,o,r))*Math.min(1,Math.max(0,o[`armw_${r}`]??0));for(let r=0;r<2&&c>.001;r++){let r=We(i.keepOut.body,f,a,s,Ge,i.keepOut.skip);if(r===s)break;s=e(s,n(t(r,s),c))}return s}:void 0,afterArm:(e,t,n)=>ar(i,e,t,n),grip:l?void 0:(e,t,n)=>As(i,f,t,W[e],e,o,c,n),sag:l?void 0:(e,t,n)=>bl(i,e,c,t,n,(1-tl(i.spec,o,e))*Math.min(1,Math.max(0,o[`armw_${e}`]??0))),heldShapes:l||!i.grasp?void 0:(e,t)=>qo(i,o,c,e,t)}}function Ml(n,r,i,o=!0){let s={...Hs(r,n.idle,i)};for(let e of[`l`,`r`])n.busy?.[e]&&(s[`hangoff_${e}`]=n.busy[e][i]);for(let r of n.spec.fits??[]){let c=`w_${r.anchor}_${r.side}`;if(!(s[c]>1e-4))continue;let l={...s,[c]:1,[`armw_${r.side}`]:1};for(let e of Object.keys(l))e!==c&&RegExp(`^w_.+_${r.side}$`).test(e)&&(l[e]=0);let u=[0,0,0],d=null,f=null,p=sa(n.spec,r.anchor),m=n.sk.index[`${W[r.side]}Arm`],h=n.sk.index[`${W[r.side]}Hand`];for(let s=0;s<6;s++){l[`fit_${r.anchor}_${r.side}`]=u;let{s,diag:c}=cs(n.sk,l,jl(n,l,i,!0,o,r.side)),g=Al(n.sk,s,c,r.effector),_=e(Xn(n,r.anchor,s,c,l,r.side),r.offset??[0,0,0]);if(!g)break;let v=t(_,g),y=a(v);p&&(!d||y<d.err)&&(d={err:y,hand:t(s.P[h],g)}),f={goal:_,shoulder:s.P[m]},u=e(u,v)}p&&d&&f&&a(t(e(f.goal,u),f.shoulder))>Jn(n.sk,r.side)+.02&&(u=d.hand),s[`fit_${r.anchor}_${r.side}`]=u}let{s:c,diag:l}=cs(n.sk,s,jl(n,s,i,!1,o));return l.frame=i,{s:c,diag:l,c:s}}function Nl(e,{sk:t,scene:n},r={}){if(e?.schema!==`cozyclay.motion-spec.v1`)throw Error(`MOTION_SPEC_INVALID: schema must be cozyclay.motion-spec.v1`);let i=Math.round(e.frames);if(!(i>=1&&i<=2e4))throw Error(`MOTION_SPEC_INVALID: frames must be 1..20000`);let a=ul(e.fix),[o,s]=r.window?[Math.max(0,r.window[0]),Math.min(i-1,r.window[1])]:[0,i-1],c={sk:t,spec:e,scene:n,grasp:a.grasp,softReach:a.reach,push:{l:new Map,r:new Map},swivel:{l:new Map,r:new Map},skinPush:{l:new Map,r:new Map},keepOut:a.self?Ke(t):null},l={ctx:c,fps:e.fps??30,frameCount:i,f0:o,frames:[]};c.objectPose=(e,t,n)=>zo(l,e,t,n),c.objectHand=(e,t)=>Ro(l,e,t),c.restingAt=(e,t)=>jo(l,e,t),c.carrySpan=e=>To(l,e),c.solvedAt=e=>No(l,e),c.weightAt=(e,t)=>l.baked?.[e]?.[t],c.frameCount=i;let u={rebake(r){l.keys=r,l.baked=Tr(r,i,Sr(e.flow)),c.socketTurns=new Map,c.graspPlans=new Map,Nc(l.baked,r,i),c.idle=Vs(l.baked,{spec:e,sk:t,frameCount:i}),c.busy=Bs(l.baked,e,i,l.fps);let a=kl(e,n,l.baked,()=>l.timeMap);c.objectsAt=a.all,l.objectsAt=o?e=>a.obstacles(e+o):a.obstacles},solve(e){for(let t of e)t>=o&&t<=s&&(l.frames[t-o]=Ml(c,Er(l.baked,t),t))},all:()=>Array.from({length:s-o+1},(e,t)=>o+t)};return c.hold=wa(c,l,e=>Ml(c,Er(l.baked,e),e,!1),e=>Ml(c,Er(l.baked,e),e)),u.rebake(r.keys??Dc(e,t)),u.solve(u.all()),l.fixed=r.fixes===!1?{}:pl(l,a,u,{whole:!r.window}),c.hold&&l.timeMap&&u.solve(u.all()),l}let Pl=`{ f, pos: [x, y, z], rot: [x, y, z, w] } or { f, pos, aim: [x, y, z], roll? }`,Fl=`{ at, onto?, bounce?, tumble?, v? }`,Il=e=>Array.isArray(e)&&e.length===4&&e.every(Number.isFinite)&&Math.abs(Math.hypot(...e)-1)<=.01,Ll=e=>pn(e)&&Math.hypot(...e)>1e-9;function Rl(e,t,n){if(n.rot!==void 0&&n.aim!==void 0)return e(`${t} gives both rot and aim; give one: rot [x, y, z, w], or aim [x, y, z] (the world direction its +Y points) with roll`);if(n.aim===void 0){n.roll!==void 0&&e(`${t}.roll turns the object about aim; give aim [x, y, z] with it, or leave roll out (rot fixes the roll)`),n.lean!==void 0&&e(`${t}.lean lets the compiler lean aim; give aim [x, y, z] with it (rot is used exactly)`),Il(n.rot)||e(`${t}.rot must be a unit quaternion [x, y, z, w], got ${V(n.rot)}${n.rot===void 0?`; or give aim [x, y, z], the world direction its +Y points`:``}`);return}Ll(n.aim)||e(`${t}.aim must be a non-zero world direction [x, y, z] for the object's +Y (a bat: knob to barrel), got ${V(n.aim)}`),n.roll!==void 0&&!Number.isFinite(n.roll)&&e(`${t}.roll must be degrees about aim (a number), got ${V(n.roll)}`),n.lean!==void 0&&!(Number.isFinite(n.lean)&&n.lean>=0&&n.lean<=45)&&e(`${t}.lean must be the most degrees the compiler may lean aim (0..45), got ${V(n.lean)}`)}function zl(e,t,n,r,i,a=!1){if(!Array.isArray(r.keys))return e(`${n}.keys must be an array of ${Pl}`);let o=Number.isInteger(i)?i-1:1/0,s=a?0:r.grab??0,c=a?o:r.release??o,l=new Map;r.keys.forEach((r,i)=>{let o=`${n}.keys[${i}]`;if(!r||typeof r!=`object`||Array.isArray(r))return e(`${o} must be ${Pl}, got ${V(r)}`);for(let e of Object.keys(r))Gt.includes(e)||t(`${o} has unknown field "${e}"${H(e,Gt)}; a key is ${Pl}`);Number.isInteger(r.f)?r.f<s||r.f>c?e(a?`${o}.f = ${r.f} is outside the take (0..${c})`:`${o}.f = ${r.f} is outside grab..release (${s}..${c}); keys pose a carried object only while it is in hand (drop its prop to move it on its own, C17)`):l.has(r.f)?e(`${o}.f = ${r.f}: keys[${l.get(r.f)}] is at that frame already; one key per frame`):l.set(r.f,i):e(`${o}.f must be a whole frame, got ${V(r.f)}`),pn(r.pos)||e(`${o}.pos must be [x, y, z], the world position of the object's base (a group: its root), got ${V(r.pos)}`),Rl(e,o,r)})}function Bl(e,t,n,r,i){let a=/^object:(.+?)(?:\.(?:center|top|base))?$/s.exec(i)?.[1];if(a===void 0)return;let o=U(n,a).flatMap(t=>[t,...n.objects[t].ancestors??[]].find(t=>ds(e.objects?.[t]))??[]).find(t=>ls(e.objects[t]));o&&t(`${r} "${i}": "${a}" moves with the follow entry objects.${o}; a follow entry rides a joint, landmark, prop or anchor point, or an object no follow entry moves`)}function Vl(e,t,n,r,i){fr(e,t,n,`${r}.follow`,i.follow)&&Bl(e,t,n.scene,`${r}.follow`,i.follow),i.offset!==void 0&&!pn(i.offset)&&t(`${r}.offset must be [x, y, z], metres along the record's own axes after aim (+Y along aim), got ${V(i.offset)}`);let a=i.aim;if(a!==void 0&&a!==`face`&&a!==`up`){if(Array.isArray(a)){Ll(a)||t(`${r}.aim must be a non-zero world direction [x, y, z], got ${V(a)}`);return}if(typeof a!=`string`||!/^[a-z]+:/.test(a))return t(`${r}.aim must be "face", "up", a world direction [x, y, z] or a point ref (toward it), where the record's +Y points; got ${V(a)}`);fr(e,t,n,`${r}.aim`,a)&&Bl(e,t,n.scene,`${r}.aim`,a)}}function Hl(e,t,n,r){return n===void 0||Number.isInteger(n)&&n>=0&&(!Number.isInteger(r)||n<r)?!0:(e(`${t} must be a whole frame 0..${Number.isInteger(r)?r-1:`frames-1`}, got ${V(n)}`),!1)}function Ul(e,t,n,r,{frames:i,scene:a,carry:o}){let s=Array.isArray(r.fall)?r.fall:[r.fall],c=new Set;s.length||e(`${n}.fall is an empty list; give ${Fl} or a list of them`),s.forEach((s,l)=>{let u=Array.isArray(r.fall)?`${n}.fall[${l}]`:`${n}.fall`;if(!s||typeof s!=`object`||Array.isArray(s))return e(`${u} must be ${Fl} (at: the frame it starts to fall), got ${V(s)}`);for(let e of Object.keys(s))Wt.includes(e)||t(`${u} has unknown field "${e}"${H(e,Wt)}; a fall is ${Fl}`);if(s.at===void 0)e(`${u}.at must be the whole frame it starts to fall`);else if(Hl(e,`${u}.at`,s.at,i)){c.has(s.at)&&e(`${u}.at = ${s.at}: another fall starts on that frame`),c.add(s.at);let t=r.grab??0,n=r.release;o&&s.at>=t&&(n===void 0||s.at<n)&&e(`${u}.at = ${s.at} is while it is in hand (grab ${t}..release ${n??`end`}); a carried object falls before its grab or from its release on`)}s.onto!==void 0&&Cn(e,a,`${u}.onto`,s.onto,{groups:!0}),s.bounce!==void 0&&!(Number.isFinite(s.bounce)&&s.bounce>=0&&s.bounce<=.6)&&e(`${u}.bounce must be 0..0.6 (the share of its landing speed it bounces back with; 0 lands dead), got ${V(s.bounce)}`),s.v!==void 0&&!(o&&s.v===`hand`||pn(s.v))&&e(`${u}.v must be [vx, vy, vz] m/s (the velocity it leaves with)${o?` or "hand" (the velocity the hand gives it at the release)`:``}, got ${V(s.v)}`),s.tumble!==void 0&&typeof s.tumble!=`boolean`&&e(`${u}.tumble must be true (it rolls to rest on a stable face, the default) or false (it drops straight down as it was), got ${V(s.tumble)}`)})}let Wl=(e,t)=>{let n=0;for(;n<e.length&&e[n]===t[n];)n+=1;return n};function Gl(e){let t=String(e),n=an.reduce((e,n)=>Wl(n,t)>Wl(e,t)?n:e);return Wl(n,t)>=4?`; did you mean "${n}"?`:H(t,an)}function Kl(e,t,n){n.mass!==void 0&&!(Number.isFinite(n.mass)&&n.mass>=.01&&n.mass<=60)&&e(`${t}.mass must be kg in 0.01..60, got ${V(n.mass)}`)}function ql(e,t,n){n.visible!==void 0&&(typeof n.visible!=`string`||!n.visible)&&e(`${t}.visible must name a channel (shown above 0.5)`),n.level!==void 0&&(typeof n.level!=`string`||!n.level)&&e(`${t}.level must name a channel (the record's level 0..4: 0 dark, 1 as authored; it scales its light, glow and effect rate)`)}function Jl(e,t,{sk:n,scene:r,frames:i=e.frames,unknown:a=t}){for(let[o,s]of Object.entries(e.objects??{})){let c=`objects.${o}`,l=s?.prop===void 0?ls(s)?`follow`:$a(s)?`free`:us(s)?`switch`:`hinge`:`carry`,u=Ut[l];(l!==`switch`&&l!==`follow`||!r?.others?.[o])&&Cn(t,r,c,o,{groups:!0});for(let e of Object.keys(s??{}))!u.includes(e)&&(l!==`hinge`||e!==`keys`&&e!==`fall`)&&a(`${c} (${l}) has unknown field "${e}"${H(e,u)}; fields: ${u.join(`, `)}`);if(Kl(t,c,s),l===`follow`){Vl(e,t,{sk:n,scene:r},c,s),ql(t,c,s);continue}if(l===`switch`){s.visible===void 0&&s.level===void 0&&t(`${c} needs visible or level (a channel): a switch shows, hides or dims "${o}" without moving it; a carry needs prop, a hinge pivot and channel`),ql(t,c,s);continue}if(l===`free`){s.keys!==void 0&&zl(t,a,c,s,i,!0),s.fall!==void 0&&Ul(t,a,c,s,{frames:i,scene:r,carry:!1}),ql(t,c,s);continue}if(l===`carry`){e.props?.[s.prop]||t(`${c}.prop "${s.prop}" is not in spec.props`);for(let e of[`at`,`along`])s[e]!==void 0&&!pn(s[e])&&t(`${c}.${e} must be [d, l, n] along the palm axes, got ${V(s[e])}`);s.grasp!==void 0&&!an.includes(s.grasp)&&t(`${c}.grasp must be one of ${an.join(`, `)}${Gl(s.grasp)}, got ${V(s.grasp)}`),ql(t,c,s),Hl(t,`${c}.grab`,s.grab,i)&&Hl(t,`${c}.release`,s.release,i)&&s.grab!==void 0&&s.release!==void 0&&!(s.grab<s.release)&&t(`${c}: grab (${s.grab}) must come before release (${s.release})`),s.keys!==void 0&&zl(t,a,c,s,i),s.fall!==void 0&&Ul(t,a,c,s,{frames:i,scene:r,carry:!0});continue}(s?.keys!==void 0||s?.fall!==void 0)&&t(`${c}: a hinge turns by its channel "${s.channel}", keyed in spec.keys; keys and fall move an object with no pivot, axis or channel (C17), or a carried one (prop)`),pn(s?.pivot)||t(`${c}.pivot must be [x, y, z] (world hinge point), got ${V(s?.pivot)}`),s?.axis!==void 0&&(!pn(s.axis)||Math.hypot(...s.axis)<1e-9)&&t(`${c}.axis must be a non-zero [x, y, z], got ${V(s.axis)}`),(typeof s?.channel!=`string`||!s.channel)&&t(`${c}.channel must name the angle channel (degrees)`)}}let J=e=>{let t=Math.round(e*1e3)/1e3;return Object.is(t,-0)?0:t},Yl=e=>`[${e.map(e=>{let t=J(e);return t>0?`+${t}`:`${t}`}).join(`, `)}]`,Xl=[`near`,`above`,`moved`,`still`,`speed`],Zl={near:.05,above:0,moved:.05,still:.01},Ql=new Set([`near`,`still`]),$l=new Set([`moved`,`still`,`speed`]),eu=e=>Xl.filter(t=>e?.[t]!==void 0),tu=(e,t)=>e.name??`goals[${t}]`,nu=(e,t)=>(Ql.has(t)?e.tol:e.by)??Zl[t],ru=(e,t)=>e.mode??(t===`speed`?`any`:`all`),iu=e=>!e.startsWith(`object:`)||/\.(center|top|base)$/.test(e)?e:`${e}.base`;function au(t,n){let[,r,i]=/^object:(.+)\.(center|top|base)$/s.exec(iu(n))??[],a=U(t,r).map(e=>t.objects[e]);if(!a.length)return null;let o=[1/0,1/0,1/0],s=[-1/0,-1/0,-1/0];for(let t of a)for(let n of[-1,1])for(let r of[0,2])for(let i of[-1,1]){let a=e(t.pos,p(t.R,[n*t.half[0],r*t.half[1],i*t.half[2]]));for(let e=0;e<3;e++)o[e]=Math.min(o[e],a[e]),s[e]=Math.max(s[e],a[e])}let c=[(o[0]+s[0])/2,(o[1]+s[1])/2,(o[2]+s[2])/2];return i===`top`?[c[0],s[1],c[2]]:i===`base`?[c[0],o[1],c[2]]:c}let ou=e=>e.moved??e.still,su=(e,t,n=Array.isArray(t.at)?t.at[0]:t.at)=>ou(t).startsWith(`object:`)?au(e.ctx.scene,ou(t)):lr(e,n,ou(t));function cu(e,n,r){let i=lr(e,r,n),o=e.fps??24;if(!i)return null;let s=lr(e,r-1,n),c=lr(e,r+1,n);if(s&&c)return a(t(c,s))*o/2;let l=s??c;return l?a(t(i,l))*o:null}function lu(e,n,r,i,o){if(r===`near`)return ur(e,i,n.near[0],n.near[1]);if(r===`above`){let t=lr(e,i,n.above[0]),r=lr(e,i,n.above[1]);return t&&r?t[1]-r[1]:null}if(r===`speed`)return cu(e,n.speed,i);let s=o&&lr(e,i,iu(ou(n)));return s?a(t(s,o)):null}let uu=(e,t,n)=>Ql.has(e)?t-n:n-t,du=e=>Math.max(1e-4,Math.round(e*1e4)/1e4);function fu(e,t,n,r,i){let a=nu(t,n),o=(n===`moved`||n===`still`)&&!ou(t).startsWith(`object:`),s=n===`moved`||n===`still`?su(e.out,t,r):null,c=[];for(let o=Math.max(r,e.f0);o<=Math.min(i,e.last);o++){let r=lu(e.out,t,n,o,s);r!==null&&c.push({f:o,v:r,short:uu(n,r,a)})}return{rows:c,from:o?r:void 0}}function pu(e,t){if(e.events.has(t))return e.events.get(t);if(e.visiting.has(t))return null;e.visiting.add(t);let n=e.goals[t],r=eu(n)[0],i=r&&mu(e,t),a;if(i&&!i.wait){let{rows:t}=fu(e,n,r,i.a,i.b);a=t.length?t.find(e=>e.short<=0)?.f??null:void 0}else a=i?.wait?null:void 0;return e.visiting.delete(t),e.events.set(t,a),a}function mu(e,t){let n=e.goals[t],[r,i]=Array.isArray(n.at)?n.at:[n.at,n.at];for(let a of[`after`,`before`]){if(n[a]===void 0)continue;let o=e.names.get(n[a]);if(o===void 0||o===t)return{wait:`${a} "${n[a]}" names no other goal`};let s=pu(e,o);if(s===void 0)return{skip:!0};if(s===null)return{wait:`waits for "${n[a]}" (${a}), which never holds`};if(a===`after`?r=Math.max(r,s+1):i=Math.min(i,s-1),r>i)return{wait:`no frames left ${a} "${n[a]}" (it holds first at f${s}; at is f${Array.isArray(n.at)?`${n.at[0]}-f${n.at[1]}`:n.at})`}}return{a:r,b:i}}function hu(e,t,n,r,i,a){let o=i?`f${n.f}, the best of f${i[0]}-f${i[1]}`:`f${n.f}`;return t===`near`?`${e}: ${J(n.v)} m at ${o} (needs <= ${r})`:t===`above`?`${e}: ${J(n.v)} m above at ${o} (needs >= ${r})`:t===`speed`?`${e}: ${J(n.v)} m/s at ${o} (needs >= ${r})`:t===`still`?`${e}: ${J(n.v)} m from ${a===void 0?`its scene pose`:`f${a}`} at ${o} (needs <= ${r})`:`${e}: moved ${J(n.v)} m${a===void 0?``:` from f${a}`} at ${o} (needs >= ${r})`}function gu(e,t,n){let r=e.f0??0;return mu({out:e,goals:t,names:new Map(t.map((e,t)=>[tu(e,t),t])),f0:r,last:r+(e.frames?.length??0)-1,events:new Map,visiting:new Set},n)}function _u(e,t){let n=[],r=[],i=e.f0??0,a={out:e,goals:t,names:new Map(t.map((e,t)=>[tu(e,t),t])),f0:i,last:i+(e.frames?.length??0)-1,events:new Map,visiting:new Set};return t.forEach((e,t)=>{let o=eu(e)[0];if(!o)return;let s=tu(e,t),c=nu(e,o),l=mu(a,t),u=Array.isArray(e.at)?e.at[0]:e.at;if(l.skip)return;if(l.wait){r.push({name:s,kind:o,ok:!1,value:null,at:null,need:c}),u>=i&&u<=a.last&&n.push({kind:`GOAL`,msg:`${s}: ${l.wait}`,frame:u-i,value:.01,limit:0});return}let{rows:d,from:f}=fu(a,e,o,l.a,l.b);if(!d.length)return;o===`moved`&&f!==void 0&&(d=[d.reduce((e,t)=>t.v>e.v?t:e)]);let p=ru(e,o)===`any`,m=Array.isArray(e.at)?l.a!==l.b:!1,h=d.reduce((e,t)=>(p?t.short<e.short:t.short>e.short)?t:e),g=h.short<=0;if(r.push({name:s,kind:o,ok:g,value:J(h.v),at:h.f,need:c}),!g)for(let e of p?[h]:d.filter(e=>e.short>0))n.push({kind:`GOAL`,msg:hu(s,o,e,c,p&&m?[l.a,l.b]:null,f),frame:e.f-i,value:du(e.short),limit:0})}),{issues:n,results:r}}let vu=`{ name?, near | above | moved | still | speed, tol?, by?, at, mode?, after?, before? }`,yu=new Set([`near`,`still`]),bu=new Set([`above`,`moved`,`speed`]);function xu(e,t){let n=Number.isFinite(t)&&t!==0?Math.abs(t):.05;return e===`speed`?`speed takes by (the point reaches by m/s): use by: ${n} (m/s), not tol`:e===`above`?`above takes by (a.y - b.y >= by, default 0): use by: ${n} for a at least ${n} m above b, or by: -${n} to let a sit up to ${n} m below b, not tol`:`moved takes by (the object moves >= by, default 0.05): use by: ${n} (metres), not tol`}let Su=e=>typeof e==`string`&&e!==``&&!/^[a-z]+:/.test(e),Cu=e=>[`moved`,`still`,`speed`].find(t=>Su(e?.[t]));function wu(e){return Array.isArray(e)&&e.some(Cu)?e.map(e=>{let t=Cu(e);return t?{...e,[t]:`object:${e[t]}`}:e}):e}function Tu(e,t,n,r,i=``){let a=e=>Number.isInteger(e);if(n===void 0)return e(`${t} needs at: a frame or [f0, f1] (absolute frames within 0..${r-1})`);let o=Array.isArray(n)?n:[n,n];if(!(o.length===2&&o.every(a)&&o[0]<=o[1]))return e(`${t}.at must be an integer frame or [f0, f1] with f0 <= f1, got ${V(n)}`);(o[0]<0||o[1]>r-1)&&e(`${t}.at = ${V(n)} is outside 0..${r-1} (spec.frames ${r})${o[1]>r-1?i:``}`)}function Eu(e,t,n,r,i,a){let o=i[a];if(typeof o!=`string`)return t(`${r}.${a} must be an object id or a point ref (object:<id>[.center|.top|.base], prop:<prop>.<point>, joint:<Bone>, landmark:mouth, anchor:<name>), got ${V(o)}`);fr(e,t,n,`${r}.${a}`,o)&&a!==`speed`&&!o.startsWith(`object:`)&&(Number.isInteger(i.at)||Array.isArray(i.at)&&i.at[0]===i.at[1])&&t(`${r}.${a} "${o}" measures ${a===`moved`?`how far the point gets`:`how far the point strays`} from where it is at at[0], so at must be [f0, f1] with f0 < f1, got ${V(i.at)}`)}function Du(e,t,n,r,i,a){let o=`goals[${i}]`,s={sk:n.sk,scene:n.scene},c=n.unknown??t;if(!r||typeof r!=`object`||Array.isArray(r))return t(`${o} must be ${vu}, got ${V(r)}`);for(let e of Object.keys(r))Yt.includes(e)||c(`${o} has unknown field "${e}"${H(e,Yt)} (fields: ${Yt.join(`, `)})`);r.name!==void 0&&(typeof r.name!=`string`||!r.name.trim()?t(`${o}.name must be a non-empty string, got ${V(r.name)}`):a.has(r.name)?t(`${o}.name "${r.name}" is already used by goals[${a.get(r.name)}]; goal names must differ`):a.set(r.name,i));let l=eu(r),u=l[0];if(l.length||t(`${o} needs one predicate: ${Xl.join(`, `)}`),l.length>1&&t(`${o} names ${l.join(` and `)}; a goal takes exactly one of ${Xl.join(`, `)} (split it into ${l.length} goals)`),l.length===1&&$l.has(u))Eu(e,t,s,o,r,u);else if(l.length===1){let n=r[u];Array.isArray(n)&&n.length===2&&n.every(e=>typeof e==`string`)?n.forEach((n,r)=>fr(e,t,s,`${o}.${u}[${r}]`,n)):t(`${o}.${u} must be [ref, ref] (two point refs), got ${V(n)}`)}r.tol!==void 0&&(l.length===1&&!yu.has(u)?t(`${o}.tol applies to near and still; ${xu(u,r.tol)}`):Number.isFinite(r.tol)&&r.tol>=0||t(`${o}.tol must be a number >= 0 (metres), got ${V(r.tol)}`)),r.by===void 0?u===`speed`&&l.length===1&&t(`${o}.speed needs by: the speed the point must reach, in m/s (e.g. by: 2)`):l.length===1&&!bu.has(u)?t(`${o}.by applies to above, moved and speed; ${u} takes tol`):(!Number.isFinite(r.by)||u===`moved`&&r.by<0||u===`speed`&&r.by<=0)&&t(`${o}.by must be a ${u===`moved`?`number >= 0 (metres)`:u===`speed`?`number > 0 (m/s)`:`finite number (metres)`}, got ${V(r.by)}`),r.mode!==void 0&&r.mode!==`all`&&r.mode!==`any`&&t(`${o}.mode must be "all" or "any", got ${V(r.mode)}`),Tu(t,o,r.at,n.frames,n.past)}function Ou(e,t){let n=new Map(e.map((e,t)=>[e&&typeof e==`object`?tu(e,t):`goals[${t}]`,t])),r=e.map((e,r)=>[`after`,`before`].flatMap(i=>{if(!e||typeof e!=`object`||e[i]===void 0)return[];let a=n.get(e[i]);return typeof e[i]!=`string`||a===void 0?(t(`goals[${r}].${i} must name another goal (its name, or goals[<i>] when it has none), got ${V(e[i])}${typeof e[i]==`string`?H(e[i],[...n.keys()]):``}`),[]):a===r?(t(`goals[${r}].${i} names the goal itself; name the goal whose first met frame this one waits for`),[]):[a]})),i=e.map(()=>0),a=[],o=n=>{if(i[n]!==2){if(i[n]===1){let r=a.slice(a.indexOf(n)).map(t=>`"${tu(e[t],t)}"`);return t(`goals wait on each other through after/before: ${[...r,r[0]].join(` -> `)}; one of them must not wait`)}i[n]=1,a.push(n);for(let e of r[n])o(e);a.pop(),i[n]=2}};e.forEach((e,t)=>o(t))}function ku(e,t,n){if(e.goals===void 0)return;if(!Array.isArray(e.goals))return t(`spec.goals must be an array of goals ${vu}, got ${V(e.goals)}`);let r=new Map;e.goals.forEach((i,a)=>Du(e,t,n,i,a,r)),Ou(e.goals,t)}let Au=new Set([`l`,`r`]),ju=e=>!!e&&typeof e==`object`&&!Array.isArray(e);function Mu(e,t,n,r){if(ju(n))for(let i of Object.keys(n))r.includes(i)||e(`${t} has unknown field "${i}"${fn(i,r)} (fields: ${r.join(`, `)})`)}function Nu(e,t,{sk:n,scene:r,unknown:i=t}){for(let[a,o]of Object.entries(e.anchors??{})){let s=`anchors.${a}`;if(/_[lr]$/.test(a)&&t(`${s}: an anchor name may not end in _l or _r (the weight channel w_${a}_l would read as anchor "${a.slice(0,-2)}"); the side goes on the weight channel, so name it "${a.slice(0,-2)}"`),!o||!Bt[o.kind]){t(`${s}.kind must be one of ${Object.keys(Bt).join(`, `)}, got ${V(o?.kind)}`);continue}Mu(i,`${s} (${o.kind})`,o,[`kind`,...Bt[o.kind]]);for(let e of[`p`,`offset`,`local`])o[e]!==void 0&&!pn(o[e])&&t(`${s}.${e} must be [x, y, z], got ${V(o[e])}`);(o.kind===`world`||o.kind===`body`)&&o.p===void 0&&t(`${s} (${o.kind}) needs p: [x, y, z]${o.kind===`body`?`, ${un.pole}`:``}`),o.kind===`bone`&&Sn(t,n,s,o.bone),o.kind===`segment`&&(Sn(t,n,`${s}.from`,o.from),Sn(t,n,`${s}.to`,o.to),o.reach!==void 0&&!Au.has(o.reach)&&t(`${s}.reach must be "l" or "r"`)),o.kind===`object`&&Cn(t,r,s,o.object,{groups:!0})&&(o.grasp!==void 0&&!an.includes(o.grasp)&&t(`${s}.grasp must be one of ${an.join(`, `)}, got ${V(o.grasp)}`),o.socket!==void 0&&r?.objects&&!Bn(r,o.object,o.socket)&&t(`${s}.socket: "${o.object}" has no socket ${V(o.socket)} (sockets: ${zn(r,o.object).map(e=>e.name).join(`, `)||`none`})`),o.socket!==void 0&&o.grasp===void 0&&t(`${s}.socket needs grasp (${an.join(`, `)}): the socket says how the hand takes it`)),o.kind===`person`&&!An(r,o.person,o.bone??``,0)&&t(`${s} (person) needs person: the id of another character with an installed take (people: ${jn(r)}) and bone: one of its joints, got ${V(o.person)} ${V(o.bone)}`),o.kind===`landmark`&&o.name!==`mouth`&&t(`${s}: the only landmark is "mouth", got ${V(o.name)}`),o.kind===`prop`&&!e.props?.[o.prop]?.points?.[o.point]&&o.point!==`origin`&&t(`${s} needs prop point ${o.prop}.${o.point}, which spec.props does not define`)}}function Pu(e,t,{sk:n,unknown:r=t}){for(let[i,a]of Object.entries(e.props??{})){let e=`props.${i}`;Mu(r,e,a,Vt),Au.has(a?.hand)||t(`${e}.hand must be "l" or "r", got ${V(a?.hand)}`),!Array.isArray(a?.origin)||!a.origin.length?t(`${e}.origin must list one or more bones`):a.origin.forEach(r=>Sn(t,n,`${e}.origin`,r));for(let[n,r]of Object.entries(a?.points??{}))pn(r)||t(`${e}.points.${n} must be [d, l, n] along the palm axes, got ${V(r)}`)}(e.fits??[]).forEach((i,a)=>{let o=`fits[${a}]`;Mu(r,o,i,Ht),Au.has(i?.side)||t(`${o}.side must be "l" or "r", got ${V(i?.side)}`),e.anchors?.[i?.anchor]||t(`${o}.anchor "${i?.anchor}" is not in spec.anchors${H(String(i?.anchor),Object.keys(e.anchors??{}))}`);let s=i?.effector;s?.prop===void 0?Array.isArray(s?.joints)?s.joints.forEach(e=>Sn(t,n,`${o}.effector`,e)):t(`${o}.effector must be { prop, point } or { joints: [bone, ...] }`):e.props?.[s.prop]?.points?.[s.point]||t(`${o}.effector ${s.prop}.${s.point} is not a point of spec.props`),i?.offset!==void 0&&!pn(i.offset)&&t(`${o}.offset must be [x, y, z]`),i?.orient!==void 0&&typeof i.orient!=`boolean`&&t(`${o}.orient must be true or false (false: the hand takes no grip turn), got ${V(i.orient)}`)})}function Fu(e,t,{frames:n,unknown:r=t,past:i=``}){(e.generators??[]).forEach((e,a)=>{let o=`generators[${a}]`;if(e?.type!==`walk`)return t(`${o}.type must be "walk", got ${V(e?.type)}`);Mu(r,o,e,Kt),(!Number.isInteger(e.f0)||e.f0<0||e.f0>n-1)&&t(`${o}.f0 must be an integer frame within 0..${n-1}, got ${V(e.f0)}${e.f0>n-1?i:``}`),e.heading!==void 0&&!(Array.isArray(e.heading)&&e.heading.length===2&&e.heading.every(Number.isFinite))&&t(`${o}.heading must be [h0, h1] in degrees (${un.yaw}), got ${V(e.heading)}`);for(let n of Kt.slice(2))n!==`heading`&&e[n]!==void 0&&!Number.isFinite(e[n])&&t(`${o}.${n} must be a finite number, got ${V(e[n])}`);e.steps!==void 0&&!(Number.isInteger(e.steps)&&e.steps>=1)&&t(`${o}.steps must be an integer >= 1, got ${V(e.steps)}`),e.turnDistance!==void 0&&!(e.turnDistance>0)&&t(`${o}.turnDistance must be > 0 m, got ${V(e.turnDistance)}`)})}function Iu(e,t,{sk:n,scene:r,frames:i,unknown:a=t,past:o=``}){let s=e.checks??{};for(let e of Object.keys(s))qt.includes(e)||a(`checks.${e} is not a check${H(e,qt)} (checks: ${qt.join(`, `)})`);let c=(e,n=()=>{})=>{let r=s[e],i=`{ ${Jt[e].join(`, `)} }`;if(r!==void 0){if(!Array.isArray(r))return t(`checks.${e} must be an array of ${i}, got ${V(r)}`);r.forEach((r,o)=>{if(!ju(r))return t(`checks.${e}[${o}] must be ${i}, got ${V(r)}`);Mu(a,`checks.${e}[${o}]`,r,Jt[e]),n(r,o)})}},l=(e,n)=>{n!==void 0&&typeof n!=`string`&&(Array.isArray(n)&&n.length===2&&n.every(Number.isInteger)&&n[0]<=n[1]&&n[0]>=0&&n[1]<=i-1||t(`${e}.when must be a weight channel or [f0, f1] within 0..${i-1}, got ${V(n)}${Array.isArray(n)&&n[1]>i-1?o:``}`))};c(`contacts`,(r,i)=>{wn(e,t,n,`checks.contacts[${i}].point`,r.point),wn(e,t,n,`checks.contacts[${i}].target`,r.target),l(`checks.contacts[${i}]`,r.when)}),c(`orient`,(r,i)=>{wn(e,t,n,`checks.orient[${i}].from`,r.from),wn(e,t,n,`checks.orient[${i}].to`,r.to),l(`checks.orient[${i}]`,r.when),r.along===`face`||r.along===`up`||pn(r.along)||t(`checks.orient[${i}].along must be "face", "up" or [x, y, z]`)}),c(`below`,(e,r)=>{Sn(t,n,`checks.below[${r}].joint`,e.joint),Sn(t,n,`checks.below[${r}].ref`,e.ref),l(`checks.below[${r}]`,e.when)}),c(`clearance`,(e,n)=>{Cn(t,r,`checks.clearance[${n}]`,e.object),l(`checks.clearance[${n}]`,e.frames)}),c(`appear`,(r,i)=>{Sn(t,n,`checks.appear[${i}].joint`,r.joint),e.anchors?.[r.anchor]||t(`checks.appear[${i}].anchor "${r.anchor}" is not in spec.anchors`)}),c(`supports`,(e,n)=>Cn(t,r,`checks.supports[${n}]`,e.object)),c(`allow`);for(let e of Object.keys(s.limits??{}))e in Ai||a(`checks.limits.${e} is not a QA limit${H(e,Object.keys(Ai))}`);let u=e.fix;if(u!==void 0&&u!==!1&&(typeof u!=`object`||!u))t(`spec.fix must be false or { ${ll.join(`, `)} }`);else for(let e of Object.keys(u||{}))ll.includes(e)||a(`spec.fix.${e} is not a fix${H(e,ll)} (fixes: ${ll.join(`, `)})`);u&&u.grasp!==void 0&&typeof u.grasp!=`boolean`&&t(`spec.fix.grasp must be true (fingers close on what a hand holds, the default) or false (the preset grip, the object at its grab-frame offset), got ${V(u.grasp)}`)}function Lu(e,t,n){Nu(e,t,n),Pu(e,t,n),Jl(e,t,n),Fu(e,t,n),Iu(e,t,n),ku(e,t,n)}function Ru(e){let t=new Set(e.channels??[]);for(let n of Object.values(e.objects??{}))for(let e of[n?.channel,n?.visible,n?.level])typeof e==`string`&&e&&t.add(e);let n=e.checks??{},r=e=>Array.isArray(e)?e.filter(e=>e&&typeof e==`object`):[];for(let e of[n.contacts,n.orient,n.below,n.clearance].flatMap(r))typeof e.when==`string`&&t.add(e.when);for(let e of r(n.appear))typeof e.channel==`string`&&t.add(e.channel);return t}function zu(e,t,n,r){if($t.has(n)){if(!pn(r))return e(`${t} ${n} must be [x, y, z] (3 finite numbers), got ${V(r)}`);if(en.has(n)){let i=Math.hypot(...r);if(i<1e-6)return e(`${t} ${n} = ${V(r)} has no direction; a pole is a ${un.pole}`);n.startsWith(`knee`)&&r[2]<-.7*i&&e(`${t} ${n} = ${V(r)} points backward; knees bend toward +z. A pole is a ${un.pole}`)}n.startsWith(`foot`)&&r[1]<.04&&e(`${t} ${n} y = ${r[1]} puts the ankle into the floor; foot_* is the ${un.foot}`);return}if(nn.has(n)){if(!r||typeof r!=`object`||Array.isArray(r))return e(`${t} ${n} must be { preset: weight }, presets ${rn.join(`, `)}; got ${V(r)}`);for(let[i,a]of Object.entries(r))rn.includes(i)?Number.isFinite(a)&&a>=0&&a<=1||e(`${t} ${n}.${i} must be a weight within 0..1, got ${V(a)}`):e(`${t} ${n} preset "${i}" is unknown${H(i,rn)} (presets: ${rn.join(`, `)})`);return}if(!Number.isFinite(r))return e(`${t} ${n} must be a finite number${n===`yaw`?` (degrees; ${un.yaw})`:n.startsWith(`fyaw`)?` (${un.fyaw})`:``}, got ${V(r)}`);let i=ln(n);i&&(r<i[1]||r>i[2])&&e(`${t} ${n} = ${r} is outside ${i[1]}..${i[2]} (${i[3]})`)}let Bu=e=>Array.isArray(e)?e:[],Vu=e=>Array.isArray(e)?e[1]:e;function Hu(e,t){let n=[...Bu(e.keys).map(e=>e?.f),...Bu(e.generators).map(e=>e?.f0)].filter(Number.isInteger),r=e.checks??{},i=[...[`contacts`,`orient`,`below`].flatMap(e=>Bu(r[e]).map(e=>Vu(e?.when))),...Bu(r.clearance).map(e=>Vu(e?.frames)),...Bu(e.goals).map(e=>Vu(e?.at))].filter(Number.isInteger),a=Math.max(t-1,...n,...i)+1;return`; set frames >= ${a}${Math.max(0,...n)+1>=a?` or frames: "auto"`:``}`}function Uu(e,t,n,r,i=``){if(e.keys!==void 0&&!Array.isArray(e.keys))return t(`spec.keys must be an array of { f, set, step? }`);let a=Object.keys(e.anchors??{}),o=Ru(e),s=[...$t,...tn,...nn,...o,...a.flatMap(e=>[`w_${e}_l`,`w_${e}_r`])];(e.keys??[]).forEach((e,c)=>{let l=e?.src?e.src.startsWith(`keys[`)?e.src:`${e.src} key`:`keys[${c}]`;if(!e||typeof e!=`object`)return t(`${l} must be { f, set, step? }`);for(let t of Object.keys(e))Rt.includes(t)||r(`${l} has unknown field "${t}"${fn(t,Rt)}; a key is { f, set, step? }`);if(!Number.isInteger(e.f))return t(`${l}.f must be an integer frame, got ${V(e.f)}`);(e.f<0||e.f>n-1)&&t(`${l}.f = ${e.f} is outside 0..${n-1} (spec.frames ${n})${e.f>0?i:``}`);let u=`${l} (f${e.f})`;if(!e.set||typeof e.set!=`object`||Array.isArray(e.set))return t(`${u} needs set: { channel: value }`);for(let[n,r]of Object.entries(e.set)){let e=/^w_(.+)_([lr])$/.exec(n);e&&!a.includes(e[1])?t(`${u} sets ${n}, but there is no anchor "${e[1]}"${H(n,s)}`):e||$t.has(n)||tn.has(n)||nn.has(n)?zu(t,u,n,r):o.has(n)||n.endsWith(`_vis`)?Number.isFinite(r)||t(`${u} ${n} must be a finite number, got ${V(r)}`):t(`${u} sets unknown channel "${n}"${H(n,s)}`)}for(let n of e.step??[])n in e.set||t(`${u} step names "${n}", which this key does not set`);e.step!==void 0&&!Array.isArray(e.step)&&t(`${u} step must be an array of channel names`)})}function Wu(e,t,n){if(e===void 0||e===!1)return;if(!e||typeof e!=`object`||Array.isArray(e))return t(`spec.flow must be false or { smooth?, lag? }, got ${V(e)}`);for(let t of Object.keys(e))t!==`smooth`&&t!==`lag`&&n(`spec.flow has unknown field "${t}" (fields: smooth, lag)`);e.smooth!==void 0&&typeof e.smooth!=`boolean`&&t(`spec.flow.smooth must be true or false, got ${V(e.smooth)}`);let r=e=>Number.isFinite(e)&&e>=0&&e<=6,i=e.lag;if(!(i===void 0||i===!1||r(i))){if(!i||typeof i!=`object`||Array.isArray(i))return t(`spec.flow.lag must be false, 0..6 frames or { spine?, head?, arms? }, got ${V(i)}`);for(let[e,a]of Object.entries(i))[`spine`,`head`,`arms`].includes(e)?r(a)||t(`spec.flow.lag.${e} must be 0..6 frames, got ${V(a)}`):n(`spec.flow.lag has unknown field "${e}" (fields: spine, head, arms)`)}}function Gu(e,{sk:t,scene:n,lenient:r=!1}={}){let i=[],a=e=>{i.length<12&&i.push(e)},o=r?()=>{}:a;if(!e||typeof e!=`object`)throw Error(`MOTION_SPEC_INVALID: spec must be an object`);e.schema!==`cozyclay.motion-spec.v1`&&a(`spec.schema must be "cozyclay.motion-spec.v1", got ${V(e.schema)}`);for(let t of Object.keys(e))Lt.includes(t)||o(`spec.${t} is not a spec field${H(t,Lt)} (fields: ${Lt.join(`, `)})`);e.fps!==void 0&&e.fps!==24&&a(`spec.fps must be 24, the timeline rate, got ${V(e.fps)}`),Wu(e.flow,a,o);let s=e.frames;(!Number.isInteger(s)||s<1||s>2e4)&&a(`spec.frames must be an integer 1..20000 or "auto", got ${V(s)}`);let c=Number.isInteger(s)?Hu(e,s):``;if(Uu(e,a,Number.isInteger(s)?s:2e4,o,c),Lu(e,a,{sk:t,scene:n,frames:Number.isInteger(s)?s:2e4,lenient:r,unknown:o,past:c}),i.length)throw Error(`MOTION_SPEC_INVALID: ${i.join(`
2
- `)}${i.length>=12?`
3
- (more problems not listed)`:``}`)}function Ku(e,t,n){let{s:r,diag:i,c:a}=e.frames[t],[o,s]=n.split(/:(.*)/s);if(o===`joint`)return r.P[e.ctx.sk.index[s]];if(o===`prop`){let[e,t]=s.split(`.`);return i.props?.[e]?.[t]??null}if(o===`anchor`)return Xn(e.ctx,s,r,i,a);throw Error(`MOTION_SPEC_INVALID: check point "${n}"`)}function qu(e,t,n){let r=/^prop:([^.]+)\./.exec(t)?.[1],i=e.ctx.spec.props?.[r]?.hand,{spec:a,scene:o}=e.ctx;return i?Object.keys(a.objects??{}).find(t=>At(a,a.objects[t],o,t)&&Ro(e,t,n)===i)??null:null}function Ju(e,t){let[n,r]=t.split(/:(.*)/s),i=n===`anchor`?e.ctx.spec.anchors?.[r]:null;return i?.kind!==`object`||!i.grasp?null:e.ctx.sockets?.get(r)?.owner??i.object}let Yu=(e,t,n)=>Array.isArray(n)?t+(e.f0??0)>=n[0]&&t+(e.f0??0)<=n[1]:!n||(e.frames[t].c[n]??0)>.99;function Xu(e,t,n){return n===`up`?[0,1,0]:n===`face`?j(e.ctx.sk,e.frames[t].s,e.ctx.sk.index.Head,[0,0,1]):s(n)}function Zu(e,n={}){let i=[],o=e.frames.length,c=e.ctx.sk.index,l=(e,t,n,r,a)=>i.push({kind:e,msg:t,frame:n,value:+r.toFixed(4),limit:a});for(let i=0;i<o;i++){for(let r of n.contacts??[])if(Yu(e,i,r.when)){let n=Ku(e,i,r.point),o=Ku(e,i,r.target);if(!n||!o)continue;let s=Ju(e,r.target)??qu(e,r.point,i+(e.f0??0)),c=s?ur(e,i,r.point,`object:${s}`)??a(t(n,o)):a(t(n,o));c>(r.tol??.01)&&l(`CONTACT`,`${r.name}: ${(c*100).toFixed(1)} cm apart`,i,c,r.tol??.01)}for(let a of n.orient??[])if(Yu(e,i,a.when)){let n=Ku(e,i,a.from),o=Ku(e,i,a.to);if(!n||!o)continue;let c=r(s(t(o,n)),Xu(e,i,a.along));c<(a.minCos??.64)&&l(`ORIENT`,`${a.name}: cos ${c.toFixed(2)}`,i,c,a.minCos??.64)}for(let t of n.below??[])if(Yu(e,i,t.when)){let{P:n}=e.frames[i].s,r=n[c[t.ref]][1]-n[c[t.joint]][1];r<(t.min??.08)&&l(`BELOW`,`${t.name}: ${t.joint} only ${(r*100).toFixed(1)} cm below ${t.ref}`,i,r,t.min??.08)}for(let t of n.clearance??[])if(Yu(e,i,t.frames)){let n=e.objectsAt(i)[t.object],r=ht.find(e=>e[0]===t.part);if(!n||!r)continue;let{P:a}=e.frames[i].s,o=Math.min(...gt(a[c[r[1]]],a[c[r[2]]]).map(e=>B(e,n)))-r[3];o<t.min&&l(`CLEARANCE`,`${t.name}: ${(o*100).toFixed(1)} cm`,i,o,t.min)}}for(let r of n.appear??[])for(let n=1;n<o;n++){if((e.frames[n-1].c[r.channel]??0)>.5==(e.frames[n].c[r.channel]??0)>.5)continue;let i=a(t(Ku(e,n,`joint:${r.joint}`),Ku(e,n,`anchor:${r.anchor}`)));i>(r.tol??.035)&&l(`APPEAR`,`${r.name}: switches with the hand ${(i*100).toFixed(1)} cm from ${r.anchor}`,n,i,r.tol??.035)}return i}let Qu=/^(.+): [\d.]+ cm off its keys$/,$u={twist:`twist about the forearm`,flex:`flex`,dev:`deviation (+ toward the thumb)`};function ed(t,n){let r=t.objects[n]??En(t)[n];return U(t,n).flatMap(n=>{let i=t.objects[n],a=hs(r,i),[o,s,c]=i.half;return[-o,o].flatMap(t=>[0,2*s].flatMap(n=>[-c,c].map(r=>e(a.pos,p(a.R,[t,n,r])))))})}function td(t,n,r){let{scene:i}=t.ctx,a=t.f0??0;for(let[s]of Ca(t.ctx.spec)){let c=ba(t,s),l=ed(i,s);if(l.length)for(let i=Math.max(a,Math.ceil(c.t[0]));i<=Math.min(a+t.frames.length-1,Math.floor(c.t[c.t.length-1]));i++){let u=xa(c,i),d=Io(t,s,i),f=Math.max(...l.map(t=>o(e(u.pos,p(u.R,t)),e(d.pos,p(d.R,t)))));f>n.reach&&r(`REACH`,`${s}: ${(f*100).toFixed(1)} cm off its keys`,i-a,f,n.reach)}}}function nd(e,t){let n=Qu.exec(t.subject)?.[1],r=e.spec?.objects?.[n];if(!r?.keys?.length)return null;let i=t.worstFrame,a=e.spec.props?.[r.prop]?.hand,o=e.fr(i).diag[`hold_${a}`]??{},s=r.keys.reduce((e,t,n)=>Math.abs(t.f-i)<Math.abs(r.keys[e].f-i)?n:e,0),c=Object.entries(Ye).filter(([e,[t,n]])=>o[e]<t||o[e]>n).map(([e,[t,n]])=>`the wrist ${$u[e]} needs ${Math.round(o[e])}deg (limit ${t}..${n})`);c.length&&(c[c.length-1]+=` after the elbow swung ${Math.round(o.swivel??0)}deg and the forearm turned`);let l=o.miss>.005&&o.fold,u=l?`the grip is ${(o.miss*100).toFixed(1)} cm inside the forearm's reach (the elbow would fold ${Math.round(o.fold)}deg; it bends to 145deg and stops at 150deg)`:`the arm is ${(o.miss*100).toFixed(1)} cm short`,d=[...o.miss>.005?[u]:[],...c],f=r.keys[s].aim&&r.keys[s].roll===void 0?``:`; an aim key without roll takes the roll the arm holds most easily`;return{cause:`objects.${n}.keys[${s}] f${r.keys[s].f}`,hint:`${n} at f${i}: ${d.length?d.join(`, `):`the hand cannot hold it there`}; ${l?`key a pose farther from the shoulder, or turn or move the body away from it`:`key a pose nearer the hand, or bring the body to it`} (root, yaw, spine_yaw)${f}`}}let rd=e=>Math.round(e*10)/10,id=e=>Math.round(e*100)/100;function ad(e){let t=Ca(e.ctx.spec);return t.length?Object.fromEntries(t.map(([t,n])=>{let r=ba(e,t);return[t,[...n.keys].sort((e,t)=>e.f-t.f).map((n,i)=>{let a=Io(e,t,Math.round(r.t[i])),c=[a.R[0][1],a.R[1][1],a.R[2][1]],l=n.aim?s(n.aim):p(C(r.q[i]),[0,1,0]),u=rd(w(l,c));return{f:n.f,deg:u,cm:rd(o(n.pos,a.pos)*100),...u>=1?{got:c.map(id)}:{},...n.aim?{rot:r.rots[i]}:{}}})]})):null}let od=.03,sd=t=>e(t.pos,p(t.R,[0,t.half[1],0])),cd=(e,t)=>[e[0][t],e[1][t],e[2][t]];function ld(e,o,c=null){let l=sd(e),u=sd(o),d=t(l,u),f=[0,1,2].map(t=>cd(e.R,t)),p=[0,1,2].map(e=>cd(o.R,e)),m=[...f,...p,...f.flatMap(e=>p.map(t=>i(e,t)))],h={depth:1/0,axis:[0,1,0]};for(let t of m){if(a(t)<1e-6)continue;let i=s(t),l=(e,t)=>t.reduce((t,n,a)=>t+Math.abs(r(n,i))*e.half[a],0),u=l(e,f)+l(o,p);if(u-Math.abs(r(d,i))<=0)return{depth:u-Math.abs(r(d,i)),axis:i};let m=r(c??d,i)<0?n(i,-1):i,g=u-r(d,m);g<h.depth&&(h={depth:g,axis:m})}return h}let ud=(t,r,i,a)=>{let o=i===`l`?`Left`:`Right`,s=n(e(r.P[t.index[`${o}HandIndex2`]],r.P[t.index[`${o}HandMiddle2`]]),.5);return a.some(e=>B(s,e)<=0)},dd=(e,t,n)=>!t.visible||(e.baked?.[t.visible]?.[n]??0)>.5,fd=(e,t,n)=>U(e.ctx.scene,t).map(t=>({...e.ctx.scene.objects[t],...jo(e,t,n)}));function pd(e,t,n){let{sk:r,spec:i}=e.ctx,a=e.frames[n-(e.f0??0)];if(!a)return null;let o=fs(r,i,a.s,i.objects[t]);return Math.max(0,Math.min(...fd(e,t,n).map(e=>B(o,e))))}function md(t,r,i){let a=fd(t,r,i);if(!a.length)return null;let o=Math.min(...a.map(e=>e.pos[1]-Math.abs(e.R[1][0])*e.half[0]-Math.abs(e.R[1][2])*e.half[2])),s=n(a.map(sd).reduce((t,n)=>e(t,n),[0,0,0]),1/a.length),c=[s[0],o-.01,s[2]],l=null;for(let[e,n]of Object.entries(t.ctx.objectsAt(i))){if(n.carried||!n.half)continue;let t=B(c,n);t<=.02&&(!l||t<l[1])&&(l=[e,t])}return l?.[0]??null}function hd(t,r,i){let s=Object.entries(r.ctx.spec.objects??{}).filter(([,e])=>K(e));if(!s.length)return[];let c=i.checks??{},l={...Ai,...c.limits},u=c.allow,d=[],f=r.f0??0,p=t.index,m=(e,t,n,r,i)=>d.push({kind:e,msg:t,frame:n,value:+r.toFixed(4),limit:i}),h=(e,t)=>e.reduce((e,n)=>t.reduce((e,t)=>e??ji(u,n,t),e),null)??l.allow;for(let[c,u]of s){let[s,d]=To(r,u),g=i.props?.[u.prop]?.hand,_=new Set([`hand_${g}`,`farm_${g}`]);if(u.grab!==void 0&&s>0&&r.frames[s-f]&&dd(r,u,s-1)){let e=pd(r,c,s),t=[u.fall??[]].flat().some(e=>Array.isArray(e.v)&&e.at<s);t&&e>od?m(`CATCH`,`${c} missed by ${(e*100).toFixed(1)} cm`,s-f,e,od):!t&&e>l.grab&&m(`GRAB`,`${c}: hand ${(e*100).toFixed(1)} cm from it at grab`,s-f,e,l.grab)}let v=u.grab===void 0?null:md(r,c,s),y=e=>Math.abs(e-s)<=3||u.release!==void 0&&Math.abs(e-d)<=3;for(let i=0;i<r.frames.length;i++){let l=i+f,d=Lo(r,c,l);if(!d||!dd(r,u,l))continue;let g=d.map(e=>e.id),b=d.map(e=>[e,sd(e),a(e.half)]),{s:x}=r.frames[i],S=Object.entries(r.frames[i].diag).filter(([e,n])=>/^hold_[lr]$/.test(e)&&n?.second===c&&(n.w>=.5||ud(t,x,e.slice(-1),d))).map(([e])=>`hand_${e.slice(-1)}`);for(let[t,r,a,s]of ht){if(_.has(t)||S.includes(t))continue;let l=x.P[p[r]],u=x.P[p[a]],d=n(e(l,u),.5),f=s+o(l,u)/2,v=h([t],[c,...g]),y=null,C=-1/0;for(let[e,t,n]of b)f-(o(d,t)-n)<=v||(y??=gt(l,u),C=Math.max(C,s-Math.min(...y.map(t=>B(t,e)))));C>v&&m(`COLLIDE`,`${t} x ${c}`,i,C,v)}if(!(l<s))for(let[e,t]of Object.entries(r.objectsAt(i))){if(!t.half||t.ignore||e===v&&y(l))continue;let n=h([c,...g],[e]),r=sd(t),s=a(t.half),u=-1/0;for(let[e,i,a]of b)o(i,r)<a+s+n&&(u=Math.max(u,ld(e,t).depth));u>n&&m(`COLLIDE`,`${c} x ${e}`,i,u,n)}}}return td(r,l,m),d}let gd=.004,_d=.995,vd=.01,yd=[`Index`,`Middle`,`Ring`,`Pinky`],bd={l:`Left`,r:`Right`};function xd(e,t){let n=U(e,t),r=n.length===1?Ct(e.objects[n[0]]).kind:`box`;return r===`sphere`?`spherical`:r===`cylinder`?`power`:null}function Sd(e){let{spec:t,scene:n}=e.ctx,r=[],i=t.fix?.grasp===!1?{...t,fix:{...t.fix,grasp:!0}}:t;for(let[t,a]of Object.entries(i.objects??{})){if(!K(a)||!At(i,a,n,t))continue;let o=i.props?.[a.prop]?.hand,[s,c]=To(e,a);o&&r.push({id:t,k:o,grasp:a.grasp??xd(n,t),from:s+4+1,to:c})}for(let t of i.fits??[]){let n=i.anchors?.[t.anchor];if(n?.kind!==`object`||!n.grasp||e.ctx.sockets?.get(t.anchor)?.rim)continue;let a=`w_${t.anchor}_${t.side}`,o=e.f0??0,s=e.baked?.[`fingers_${t.side}`],c=e=>Object.entries(s?.[e]??{}).reduce((e,[t,n])=>e+(t===`relaxed`?0:n||0),0);r.push({id:n.object,k:t.side,grasp:n.grasp,from:o,to:o+e.frames.length-1,held:t=>(e.baked?.[a]?.[t]??0)>=_d&&c(t)>=_d,rests:!0})}return r}let Cd=(e,t,n)=>U(e.ctx.scene,t).map(t=>({...e.ctx.scene.objects[t],...zo(e,t,n)}));function wd(e,t,n){let r=bd[n],i=n=>t.P[e.index[`${r}Hand${n}`]],a=[{bone:`${r}Hand`,finger:`palm`,a:t.P[e.index[`${r}Hand`]],b:i(`Middle1`),r:.022}];for(let e of[...yd,`Thumb`])for(let t=1;t<=3;t++)a.push({bone:`${r}Hand${e}${t}`,finger:e,tip:t===3,a:i(`${e}${t}`),b:i(`${e}${t+1}`),r:.009});return a}function Td(t,r){return t.map(t=>{let i={depth:-1/0};for(let e of r){let n=kt(e,t.a,t.b,t.r);n>i.depth&&(i={depth:n,entry:e})}let a=n(e(t.a,t.b),.5),o=i.entry&&Ot(i.entry,a);return{...t,depth:i.depth,at:o,normal:i.entry&&Dt(i.entry,a)}})}function Ed(e,t){if(!t.length)return 1/0;let n=[e[0],e[2]],r=t.map(e=>[e[0],e[2]]),i=(e,t)=>{let r=[t[0]-e[0],t[1]-e[1]],i=Math.max(0,Math.min(1,((n[0]-e[0])*r[0]+(n[1]-e[1])*r[1])/(r[0]**2+r[1]**2||1)));return Math.hypot(n[0]-e[0]-i*r[0],n[1]-e[1]-i*r[1])},a=Dd(r);return a.length>=3&&a.every((e,t)=>{let r=a[(t+1)%a.length];return(r[0]-e[0])*(n[1]-e[1])-(r[1]-e[1])*(n[0]-e[0])>=0})?0:Math.min(...a.map((e,t)=>i(e,a[(t+1)%a.length])))}function Dd(e){let t=[...e].sort((e,t)=>e[0]-t[0]||e[1]-t[1]),n=(e,t,n)=>(t[0]-e[0])*(n[1]-e[1])-(t[1]-e[1])*(n[0]-e[0]);if(t.length<3)return t;let r=[],i=[];for(let e of t){for(;r.length>=2&&n(r.at(-2),r.at(-1),e)<=0;)r.pop();r.push(e)}for(let e of t.reverse()){for(;i.length>=2&&n(i.at(-2),i.at(-1),e)<=0;)i.pop();i.push(e)}return[...r.slice(0,-1),...i.slice(0,-1)]}function Od(t,i,a,o,c,l){let u=Td(wd(t,i,a.k),o),d=bd[a.k].toLowerCase();if(a.rests&&u[0].depth<-.03)return;for(let e of u)e.finger!==`palm`&&e.depth>gd&&c(`FINGER_PEN`,`${e.bone} into ${a.id}`,l,e.depth,gd);let f=e=>u.some(t=>t.finger===e&&t.tip&&t.depth>=-.008),m=yd.filter(f).length,h=f(`Thumb`);(a.grasp===`pinch`?h&&f(`Index`):m>=3||h&&m>=2)||c(`FINGER_GAP`,`${d} fingers off ${a.id}`,l,m+ +!!h,a.grasp===`pinch`?2:3);let g=u.filter(e=>e.depth>=-.008&&e.normal),_=n(o.reduce((t,n)=>e(t,e(n.pos,p(n.R,[0,n.half[1],0]))),[0,0,0]),1/o.length),v=g.filter(e=>e.normal[1]<=-.3).map(e=>e.at),y=g.filter(e=>e.finger===`Thumb`).map(e=>e.normal),b=g.filter(e=>yd.includes(e.finger)).map(e=>e.normal),x=t=>t.length?s(t.reduce(e,[0,0,0])):null,S=x(y),C=x(b),w=S&&C&&Math.acos(Math.max(-1,Math.min(1,r(S,C))))*180/Math.PI>=120,T=(e,t)=>Math.acos(Math.max(-1,Math.min(1,r(e,t))))*180/Math.PI,E=g.some((e,t)=>g.slice(t+1).some(t=>T(e.normal,t.normal)>=150));!a.rests&&!w&&!E&&Ed(_,v)>vd&&c(`SUPPORT`,`${a.id} not supported`,l,Math.min(1,Ed(_,v)),vd)}function kd(e,t){if(t.ctx.spec.fix===!1)return[];let n=[],r=t.f0??0,i=(e,t,r,i,a)=>n.push({kind:e,msg:t,frame:r,value:+i.toFixed(4),limit:a});for(let n of Sd(t))for(let a=Math.max(n.from,r);a<=Math.min(n.to,r+t.frames.length-1);a++){if(n.held&&!n.held(a))continue;let o=Cd(t,n.id,a);o.length&&Od(e,t.frames[a-r].s,n,o,i,a-r)}return n}let Ad=.5,jd={twist:.5,flex:1,dev:1},Md=(e,t)=>t[e].map(t=>t*jd[e]);function Nd(e,t,n,r){let{diag:i}=e.frames[n],a=e.baked??{};if((i[`hold_${t}`]?.w??0)>Ad||(i[`grip_${t}`]?.w??0)>Ad)return!0;for(let n of e.ctx.spec.fits??[])if(n.side===t&&(a[`w_${n.anchor}_${t}`]?.[r]??0)>Ad)return!0;return Object.keys(e.ctx.spec.objects??{}).some(n=>e.ctx.spec.objects[n]?.prop&&Ro(e,n,r)===t)}function Pd(e,r){let i=e.index,o=e.restWorldRot,s=r.f0??0,c={};for(let[l,u]of[[`Left`,`l`],[`Right`,`r`]]){let d=i[`${l}Hand`],h=i[`${l}ForeArm`],g=i[`${l}Arm`],_={frames:0,past:0,limit:0,raised:0};r.frames.forEach(({s:i},c)=>{let v=c+s;if(!Nd(r,u,c,v))return;let y=n(t(i.P[d],i.P[h]),1/Math.max(1e-9,a(t(i.P[d],i.P[h])))),b=f(i.R[h],f(m(o[h]),o[d])),x=nt(b,i.R[d],y,p(f(b,m(i.R[d])),z(e,i,l).l));_.frames++;for(let e of[`flex`,`dev`,`twist`]){let t=Math.round(x[e]),n=_[e];_[e]=n?[Math.min(n[0],t),t<n[0]?v:n[1],Math.max(n[2],t),t>n[2]?v:n[3]]:[t,v,t,v]}let S=(e,t)=>[`flex`,`dev`,`twist`].some(n=>{let[r,i]=Md(n,e);return x[n]<r+t||x[n]>i-t});S(Xe,-1)&&_.past++,S(Ye,1)&&_.limit++;let C=t(i.P[h],i.P[g]),w=Math.round(Math.acos(Math.max(-1,Math.min(1,-C[1]/Math.max(1e-9,a(C)))))*180/Math.PI);(!_.elevation||w>_.elevation[0])&&(_.elevation=[w,v]),w>90&&_.raised++}),_.frames&&(c[`hand_${u}`]=_)}for(let e of r.ctx.hold?.grips?.()??[])c[`hand_${e.hand}`]&&(c[`hand_${e.hand}`].grip={object:e.id,chose:e.name,cost:[e.cost,e.today]});return Object.keys(c).length?c:null}let Fd=.5,Id=[`flex`,`dev`,`twist`],Ld={l:`Left`,r:`Right`},Rd={flex:e=>e>=0?`flex`:`extension`,dev:e=>e>=0?`radial deviation`:`ulnar deviation`,twist:()=>`twist`};function zd(e){let t=null;for(let n of Id){let[r,i]=Ye[n],a=e[n],o=a>=0?i:r,s=a>=0?a-i:r-a;(!t||s>t.past)&&(t={axis:n,value:a,edge:o,past:s})}return t}function Bd(e,r,i,o){let{s,diag:c}=r.frames[o],l=c[`hold_${i}`];if((l?.w??0)>Fd)return l;let u=Ld[i],d=e.index,h=d[`${u}Hand`],g=d[`${u}ForeArm`],_=e.restWorldRot,v=n(t(s.P[h],s.P[g]),1/Math.max(1e-9,a(t(s.P[h],s.P[g])))),y=f(s.R[g],f(m(_[g]),_[h]));return nt(y,s.R[h],v,p(f(y,m(s.R[h])),z(e,s,u).l))}function Vd(e,t){let n=[],r=t.f0??0;for(let i of[`l`,`r`])t.frames.forEach((a,o)=>{if(!Nd(t,i,o,o+r))return;let s=zd(Bd(e,t,i,o));if(s.past<=.5)return;let c=Math.abs(s.edge);n.push({kind:`WRIST`,msg:`hand_${i} ${Rd[s.axis](s.value)} ${Math.round(Math.abs(s.value))}deg (limit ${Math.round(c)})`,frame:o,value:+Math.abs(s.value).toFixed(2),limit:c})});return n}function Hd(e,t){let n=/^hand_([lr]) /.exec(t.subject)?.[1];if(!n)return null;let r=t.worstFrame,i=`at f${r}${t.count>1?` (f${t.first}-f${t.last}, ${t.count} frames)`:``}`,a=Math.round(e.out.ctx?.swivel?.[n]?.get(r)??0);if(Math.abs(a)>=1)return{cause:`fix.self elbow turn ${a}deg at f${r}`,hint:`${t.subject} ${i}: fix.self turned the elbow ${a} deg to keep the arm out of the body and the hand kept its turn, so the wrist took it; key elbow_${n} (the elbow pole) or the hand's target so the arm clears the body without the turn`};let o=(e.out.frames[e.rel(r)]?.diag[`hold_${n}`]?.w??0)>Fd&&Object.keys(e.spec.objects??{}).find(t=>e.spec.props?.[e.spec.objects[t]?.prop]?.hand===n),s=e.fr(r).c.spine_pitch??0;return{cause:o?`objects.${o}.keys near f${r}`:e.cause(`armw_${n}`,r),hint:`${t.subject} ${i}: the arm cannot turn the hand that far from where the body stands; lean the upper body over the hand (spine_pitch ${Math.round(s)} at f${r}: try ${Math.round(s+20)}) or bring the body to it (root), ${o?`or key ${o} so the hand need not bend that far (its aim or pos)`:`or move what the hand reaches`}; repair: { rounds } tries leans of 10-30 deg here and keeps one that helps`}}let Ud={WRIST:Hd};function Wd(e,t){let n=Math.max(0,Math.min(e.frames-1,t))*e.names.length,r=t=>{let r=e.names.indexOf(t),i=(n+r)*3;return[e.P[i],e.P[i+1],e.P[i+2]]};return ht.map(([e,t,n,i])=>[e,r(t),r(n),i])}function Gd(e,t,n={}){let r=Object.entries(t.ctx?.scene?.people??{}),i=[];if(!r.length)return i;let a=e.index,s=t.f0??0,c={...Ai,...n.limits}.allow;return t.frames.forEach(({s:e},t)=>{let l=t+s,u=ht.map(([t,n,r,i])=>[t,e.P[a[n]],e.P[a[r]],i]);for(let[s,d]of r){if(!ht.every(([,e,t])=>d.names.includes(e)&&d.names.includes(t)))continue;let r=Wd(d,l),f=r.find(e=>e[0]===`hips`)[1];if(!(o(e.P[a.Hips],f)>2.2))for(let[e,a,o,l]of u){let u=Math.max(...r.map(([,e,t,n])=>l+n-_t(a,o,e,t))),d=ji(n.allow,e,`person:${s}`)??c;u>d&&i.push({kind:`PERSON`,msg:`${e} x person:${s}`,frame:t,value:+u.toFixed(4),limit:d})}}}),i}let Kd=(e,t)=>` (${e}the knee would fold ${Math.round(t)}deg; it bends to 150deg and stops at 160deg)`;function qd(e,i,c,l,u){let d=i===`l`?`Left`:`Right`,f=e.fr(c).s.P,p=f[e.I[`${d}UpLeg`]],m=f[e.I[`${d}Leg`]],h=f[e.I[`${d}Foot`]],g=t(u,p),_=N(o(p,m),o(m,h),150)+.002,v=n(s(g),Math.max(0,_-a(g))),y=g[1]+Math.sqrt(Math.max(0,g[1]*g[1]-r(g,g)+_*_));return g[1]<0?{cause:e.cause(`root`,c),hint:`root: raise ${J(y)} m at f${c}${l}, or key foot_${i} farther from the hip (wider or more forward)`,fix:{channel:`root`,f:c,delta:[0,y,0]}}:{cause:e.cause(`foot_${i}`,c),hint:`foot_${i}: move ${Yl(v)} m out from the hip at f${c}${l}, or raise root ${J(y)} m`,fix:{channel:`foot_${i}`,f:c,delta:v}}}let Jd=.003,Yd=e=>/(^|_)l$|^Left/.test(e)?`l`:/(^|_)r$|^Right/.test(e)?`r`:null;function Xd(e,t,n,r,i){let a=e.spec.anchors?.[t],o=e=>`/anchors/${t}/${e}`;if(a?.kind===`world`)return{path:o(`p`),delta:r};if(a?.kind===`body`)return{path:o(`p`),delta:p(_(-(e.fr(n).c.yaw??0)),r)};if([`bone`,`segment`,`prop`,`person`].includes(a?.kind))return{path:o(`offset`),delta:r};if(a?.kind!==`object`)return;let s=e.fr(n),c=$n(e.out.ctx,a,t,{...s,diag:{...s.diag,frame:n}},i);return{path:o(`local`),delta:p(m(c.R),r)}}function Zd(e,t,n,r,i=``){let a=/^prop_(.+)$/.exec(t),o=a?e.spec.props?.[a[1]]?.hand:Yd(t),s,c,l,u=r;if(/^(hand|farm|uarm)_/.test(t)||a){let t=e.hand(o,n);s=t.label,l=t.anchor?Xd(e,t.anchor,n,r,o):t.ch===`hand_${o}`?{channel:t.ch,f:n,delta:r}:void 0;let i=t.anchor&&e.spec.anchors[t.anchor]?.kind===`object`?e.spec.anchors[t.anchor].object:null;i&&l&&(s=`anchors.${t.anchor}.local (a point on ${i}, in ${i}'s axes; ${i} stays)`,u=l.delta),c=t.anchor?`anchor ${t.anchor}${t.fit===null?``:` (fits[${t.fit}])`} via ${e.cause(t.ch,n)}`:e.cause(t.ch,n)}else/^(shin|foot|sole)_/.test(t)?(s=`foot_${o}`,c=e.cause(s,n),l={channel:s,f:n,delta:r}):(s=`root`,c=e.cause(`root`,n),l={channel:`root`,f:n,delta:r,.../^root held since /.test(c)?{hold:!0}:{}});return/ (walk|walkTo) /.test(` ${c}`)&&/step|start|end/.test(c)&&!/^anchor/.test(s)?{cause:c,hint:`walk path: ${s} needs ${Yl(r)} m at f${n}${i}; change that walk's heading/turnDistance/step (or give one a { range: [lo, hi] })`}:{cause:c,hint:`${s}: move ${Yl(u)} m at f${n}${i}`,fix:l}}let Qd=e=>ht.find(t=>t[0]===e);function $d(e,t,n,r){let[,i,a]=Qd(n),{s:o}=e.fr(t),s=gt(o.P[e.I[i]],o.P[e.I[a]]),c=s.map(e=>B(e,r)),l=Math.min(...c);return{p:s[c.indexOf(l)],n:St(s[c.indexOf(l)],r),d:l}}function ef(e,t,r,i,a){let o=t.worstFrame,s=e.out.objectsAt(e.rel(o))[i];if(!s||!Qd(r))return null;let{n:c}=$d(e,o,r,s);return Zd(e,r,o,n(c,a),` (out of ${i})`)}function tf(e,r,i,a){let o=r.worstFrame,s=e.out.objectsAt(e.rel(o))[a];if(!s)return null;let c=Di(e.sk,e.fr(o).s,i.endsWith(`_l`)?`Left`:`Right`),l=Oi(c,s),u=r.worst-r.limit+Jd;if(!(l.side>0))return l.topAt?Zd(e,i,o,n(St(l.topAt,s),u),` (out of ${a}'s top)`):null;let d=Math.max(...c.filter(e=>B(e,s)<0).map(e=>2*s.half[1]-p(m(s.R),t(e,s.pos))[1]))+Jd,f=n(St(l.sideAt,s),u);if(d>.25)return Zd(e,i,o,f,` (out of ${a}'s side)`);let h=Zd(e,i,o,p(s.R,[0,d,0]),` (over ${a}'s top; its ${r.worst.toFixed(3)} m is through a side)`);return h.hint+=/^walk path/.test(h.hint)?`; a higher lift raises the swing`:`; or move ${Yl(f)} m out of that side`,h}function nf(e,t,r,i,a,o){return Zd(e,r,t.worstFrame,n(s(i),a),` (away from the ${o})`)}function rf(e,t,n){let{s:r,diag:i}=e.fr(t),a=e.I;if(n.startsWith(`hand_`)){let e=n.endsWith(`l`)?`Left`:`Right`;return gt(r.P[a[`${e}Hand`]],r.P[a[`${e}HandMiddle3`]],5)}return Object.entries(i.props?.[n.slice(5)]??{}).filter(([e,t])=>Array.isArray(t)&&e!==`origin`).map(([,e])=>e)}let af={COLLIDE(e,t){let[,n,r]=/^(\S+) x (.+)$/.exec(t.subject)??[];if(/^foot_[lr]$/.test(n??``))return tf(e,t,n,r);let i=ef(e,t,n,r,t.worst-t.limit+Jd);return i&&/^(hand|farm)_/.test(n)&&!e.spec.fix?.penetration&&t.worst-t.limit<=.05&&(i.hint+=`; or fix.penetration: 0.02 pushes shallow hand hits out`,e.spec.fix!==!1&&(i.fix={path:`/fix/penetration`,value:.02})),i},PERSON(i,o){let[,l,u]=/^(\S+) x person:(.+)$/.exec(o.subject)??[],d=Qd(l),f=i.out?.ctx?.scene?.people?.[u];if(!d||!f)return null;let p=o.worstFrame,{s:m}=i.fr(p),h=gt(m.P[i.I[d[1]]],m.P[i.I[d[2]]]),g=Wd(f,p).map(([i,o,s,l])=>{let u=t(s,o),f=Math.max(r(u,u),1e-12);return h.map(s=>{let p=e(o,n(u,c(r(t(s,o),u)/f,0,1)));return{other:i,p:s,q:p,depth:d[3]+l-a(t(s,p))}}).reduce((e,t)=>t.depth>e.depth?t:e)}).reduce((e,t)=>t.depth>e.depth?t:e),_=a(t(g.p,g.q))>1e-6?s(t(g.p,g.q)):s([g.p[0]-g.q[0]||1,0,g.p[2]-g.q[2]]),v=Zd(i,l,p,n(_,o.worst-o.limit+Jd),` (out of ${f.name}'s ${g.other})`);return{...v,hint:`${v.hint}; or, for a touch you mean, checks.allow [{ part: "${l}", object: "person:${u}", depth: ${J(o.worst+Jd)} }]`}},CLEARANCE(e,t){let n=(e.spec.checks?.clearance??[]).find(e=>t.subject.startsWith(`${e.name}:`));return n&&ef(e,t,n.part,n.object,n.min-t.worst+Jd)},GROUND(e,t){let n=t.subject.startsWith(`Left`)?`l`:`r`,r=t.worstFrame,i=e.fr(r).c,a=e.fr(r).diag[`leg_fold_${n}`];if(a)return qd(e,n,r,Kd(`the sole is in the floor because the knee is on its stop: `,a),i[`foot_${n}`]);let o=i[`fpitch_${n}`]?`, or less fpitch_${n} (now ${J(i[`fpitch_${n}`])} deg; + lifts the heel)`:``;return{cause:e.cause(`foot_${n}`,r),hint:`foot_${n}: raise ${J(t.worst+.001)} m at f${r} (foot_* is the ankle; it stands ${yt(e.sk)} m above the floor)${o}`,fix:{channel:`foot_${n}`,f:r,delta:[0,t.worst+.001,0]}}},FACE(e,n){let r=n.subject.replace(/ into head$/,``),i=n.worstFrame,{s:a}=e.fr(i),o=A(a,e.I.Head,e.sk.landmarks.headCentre),s=rf(e,i,r).reduce((t,n)=>vt(e.sk,a,n)>vt(e.sk,a,t)?n:t);return nf(e,n,r,t(s,o),n.worst-n.limit+Jd,`head`)},BODY(i,o){let[,s,l]=/^(\S+) into (\S+)$/.exec(o.subject)??[],u=Qd(l),d=o.worstFrame,{s:f}=i.fr(d),p=/^(hand|farm|uarm)_([lr])$/.exec(s??``),m=p&&Me(i.sk);if(m){let e=i.I[`${p[2]===`l`?`Left`:`Right`}${{hand:`Hand`,farm:`ForeArm`,uarm:`Arm`}[p[1]]}`],t=Je(m,i.sk,f,e),r=t.p&&Le(m,f,t.p,m.skip.get(e));if(r){let e=Zd(i,s,d,n(r,o.worst-o.limit+Jd),` (out of the ${l})`);return p[1]!==`hand`&&(e.hint+=`; or turn elbow_${p[2]} away from the ${l}`),e}}if(!u)return null;let h=f.P[i.I[u[1]]],g=f.P[i.I[u[2]]],_=t(g,h),v=i=>e(h,n(_,c(r(t(i,h),_)/r(_,_),0,1))),y=rf(i,d,s).reduce((e,n)=>a(t(n,v(n)))<a(t(e,v(e)))?n:e);return nf(i,o,s,t(y,v(y)),o.worst-o.limit+Jd,`torso`)},SELF(e,r){let i=r.worstFrame,{s:a}=e.fr(i),o=a.P,c=e.I;if(r.subject===`hands overlap`){let a=s(t(o[c.LeftHand],o[c.RightHand])),l=(r.worst-r.limit+Jd)/2,u=Zd(e,`hand_l`,i,n(a,l));return{cause:u.cause,hint:`${u.hint}; and ${Zd(e,`hand_r`,i,n(a,-l)).hint}`,fix:Zd(e,`hand_l`,i,n(a,2*l)).fix}}let l=r.subject===`shins touch`,u=l?.1:.135,[d,f]=l?[`Leg`,`Foot`]:[`UpLeg`,`Leg`],p=_t(o[c[`Left${d}`]],o[c[`Left${f}`]],o[c[`Right${d}`]],o[c[`Right${f}`]]),m=t(o[c[`Left${f}`]],o[c[`Right${f}`]]);m[1]=0;let h=(u-p+Jd)/2,g=s(m);return{cause:e.cause(`foot_l`,i),hint:`foot_l: move ${Yl(n(g,h))} m and foot_r ${Yl(n(g,-h))} m at f${i} (${l?`shins`:`thighs`} ${J(p)} m apart, need ${u}); or spread knee_l/knee_r`,fix:{channel:`foot_l`,f:i,delta:n(g,2*h)}}},OBJECT(e,t){let[,n,r]=/^(\S+) into (.+)$/.exec(t.subject)??[],i=e.spec.objects?.[n]?.channel;if(!i)return null;let a=t=>e.out.baked[i]?.[t]??0,o=t.first>0?a(t.first-1):a(0);return{cause:e.cause(i,t.worstFrame),hint:`${i}: keep it within ${J(o)} deg (it reaches ${J(a(t.worstFrame))} at f${t.worstFrame}, ${J(t.worst)} m into ${r}), or move ${r}`,fix:{channel:i,f:t.worstFrame,value:o}}},BALANCE(r,i){let a=i.worstFrame,{s:o}=r.fr(a),s=o.P,l=r.I;if(i.subject===`no foot on the ground`)return{cause:r.cause(`root`,a),hint:`at f${a} lower foot_l or foot_r onto the ground, the floor or the top of an object under it (the ankle stands ${yt(r.sk)} m above it), or declare checks.supports for the seat the hips rest on`};let u=n([`Hips`,`Spine1`,`Spine2`,`Head`].reduce((t,n)=>e(t,s[l[n]]),[0,0,0]),.25),d=[`Left`,`Right`].flatMap(e=>Object.values(xt(r.sk,o,e))),f=d.map(e=>e[0]),p=d.map(e=>e[2]),m=.07,h=[c(u[0],Math.min(...f)-m,Math.max(...f)+m),u[1],c(u[2],Math.min(...p)-m,Math.max(...p)+m)];return{cause:r.cause(`root`,a),hint:`root: move ${Yl(t(h,u))} m at f${a} (centre of mass over the feet), or step a foot under it`,fix:{channel:`root`,f:a,delta:t(h,u)}}}},of=.02;function sf(e,r,i,o,c=``){let l=r===`l`?`Left`:`Right`,{s:u}=e.fr(i),d=t(u.P[e.I[`${l}Hand`]],u.P[e.I[`${l}Arm`]]),f=Math.hypot(d[0],d[2])/Math.max(a(d),1e-9);if(f<.5)return null;let p=n(s([d[0],0,d[2]]),Math.min(.2,o/f));return{delta:p,hint:`body: move ${Yl(p)} m toward the target at f${i}, ${e.cause(`root`,i)} (arm ${r} is at full reach)${c}`,fix:{channel:`root`,f:i,delta:p,hold:!0}}}let cf=(e,t,n)=>(e.fr(n).diag[`reach_over_${t}`]??0)>.001||(e.fr(n).diag[`arm_miss_${t}`]??0)>.002&&!e.fr(n).diag[`arm_fold_${t}`];function lf(e,t,n,r){let i=null;for(let[a,o]of Object.entries(e.fr(n).c))a===r||!RegExp(`^w_.+_${t}$`).test(a)||!(o>of)||i&&o<=i.w||(i={ch:a,w:o});return i}function uf(e,t,n,r,i){let a=n.slice(-1),o=e.hand(a,r),s=o.anchor?lf(e,a,r,o.ch):null;if(!s)return null;let c=Array.isArray(t.at)?t.at[1]:t.at,l=e.bracket(s.ch,r).prev,u=l&&Math.abs(e.out.baked[s.ch]?.[Math.round(l.f)]??1)<=of&&c>=r&&!(e.bracket(s.ch,r).next?.f<=c)?c:r,d=s.w/Math.max(1,(e.fr(r).c[o.ch]??1)+s.w),f=s.ch.replace(/^w_/,``).replace(/_[lr]$/,``);return{cause:e.cause(s.ch,r),hint:`${s.ch} is ${J(s.w)} at f${r}, so anchor ${f} takes ${Math.round(d*100)}% of the hand target from ${o.label}: key ${s.ch} 0 at f${u}${i}`,fix:{channel:s.ch,f:u,value:0}}}function df(e,t){let n=null;return(e.spec?.goals??[]).forEach((e,r)=>{let i=tu(e,r);t.subject.startsWith(`${i}:`)&&(!n||i.length>n.name.length)&&(n={goal:e,name:i,i:r})}),n}function ff(e,t,n){let r=[t,...e.out.ctx.scene?.objects?.[t]?.ancestors??[]].find(t=>So(e.spec.objects?.[t],n));return r?e.spec.objects[r]:null}function pf(e,t,n){let[,r,i]=/^([a-z]+):(.+)$/s.exec(t)??[];if(r===`prop`)return e.spec.props?.[i.split(`.`)[0]]?.hand?`hand_${e.spec.props[i.split(`.`)[0]].hand}`:null;if(r===`object`){let t=ff(e,i.replace(/\.(center|top|base)$/,``),n),r=t&&e.spec.props?.[t.prop]?.hand;return r?`hand_${r}`:null}if(r===`landmark`)return`root`;if(r!==`joint`&&r!==`bone`)return null;let a=xn(e.sk,i).bone??``,o=/^Left/.test(a)?`l`:/^Right/.test(a)?`r`:null;return o&&/^(Left|Right)(Hand|ForeArm)/.test(a)?`hand_${o}`:o&&/^(Left|Right)(Foot|Toe|Leg)/.test(a)?`foot_${o}`:`root`}function mf(e,t,n,r){let i=t.slice(-1);if(!t.startsWith(`hand_`))return{channel:t,f:n,delta:r};let a=e.hand(i,n);return a.ch===`hand_${i}`?{channel:a.ch,f:n,delta:r}:a.anchor&&e.spec.anchors?.[a.anchor]?.kind===`world`?{path:`/anchors/${a.anchor}/p`,delta:r}:null}function hf(e,r,i,o,c,l){if(i===`moved`||i===`still`){let u=r[i],d=pf(e,u,o),f=su(e.out,r,l),p=lr(e.out,o,iu(u)),m=p&&f&&a(t(p,f))>.001?s(t(p,f)):[0,1,0];return d?[d,n(m,i===`still`?-c:c)]:null}let[u,d]=r[i],f=pf(e,u,o),p=pf(e,d,o),m=e=>e?.startsWith(`hand_`)?2:+!!e,h=m(f)>=m(p),g=h?f:p,_=h?1:-1;if(!g)return null;if(i===`above`)return[g,[0,_*c,0]];let v=lr(e.out,o,u),y=lr(e.out,o,d);return v&&y?[g,n(s(t(y,v)),_*c)]:null}let gf={GOAL(e,t){let n=e.spec&&df(e,t),r=t.worstFrame;if(!n)return{cause:`goal ${t.subject.split(`:`)[0]}`,hint:`change the keys around f${r} until the goal holds`};let{goal:i,name:a,i:o}=n,s=eu(i)[0],c=t.worst+.003,l=gu(e.out,e.spec.goals,o);if(l.wait){let e=i.after??i.before;return{cause:`goal ${a}`,hint:`${a} ${l.wait}: make goal "${e}" hold${i.after!==void 0&&i.before!==void 0?` and "${i.before}"`:``} where this one needs it, or move at`}}let u=s===`speed`?`m/s`:`m`,d=s===`near`||s===`still`?`<=`:`>=`,f=` (goal "${a}" is ${J(t.worst)} ${u} ${s===`still`?`off`:`short`}; needs ${d} ${nu(i,s)})`;if(s===`speed`){let t=pf(e,i.speed,r);return{cause:t?e.cause(t,r):`goal ${a}`,hint:`${i.speed} is too slow at f${r}: put its keys around f${r} closer in time, or farther apart in space${f}`}}let p=hf(e,i,s,r,c,l.a);if(!p){if(s===`still`&&i.still.startsWith(`object:`)){let e=i.still.slice(7).replace(/\.(center|top|base)$/,``);return{cause:`objects.${e}`,hint:`${e} moves at f${r}, where it should stay: hold its hinge channel or keys until then, or start them later${f}`}}if(s===`still`)return{cause:`goal ${a}`,hint:`${i.still} moves at f${r} and nothing that follows the body moves it; name a joint, prop point or held object${f}`};if(s===`moved`&&!i.moved.startsWith(`object:`))return{cause:`goal ${a}`,hint:`${i.moved} does not move with the body at f${r}; name a joint, prop point or held object`};if(s===`moved`){let e=i.moved.slice(7).replace(/\.(center|top|base)$/,``);return{cause:`objects.${e}`,hint:`${e} is not in a hand at f${r}, so nothing moves it: carry it (objects.${e} = { prop, grab }) or hinge it`}}return{cause:`goal ${a}`,hint:`neither ${i[s][0]} nor ${i[s][1]} moves with the body at f${r}; name a joint, prop point or held object`}}let[m,h]=p;if(m.startsWith(`hand_`)){let t=uf(e,i,m,r,f);if(t)return t;let n=cf(e,m.slice(-1),r)&&sf(e,m.slice(-1),r,c,f);if(n)return{cause:e.cause(`root`,r),hint:n.hint,fix:n.fix}}let g=Zd(e,m,r,h,f),_=mf(e,m,r,h);return{cause:g.cause,hint:g.hint,..._?{fix:_}:{}}}},_f={Leg:[`UpLeg`,`Leg`,`Foot`,`knee`],ForeArm:[`Arm`,`ForeArm`,`Hand`,`elbow`]},vf=e=>e.startsWith(`Left`)?`l`:`r`,yf=e=>/^(w_|armw)/.test(e)?.5:/^(root|foot|hand)/.test(e)?1:/^(knee|elbow)/.test(e)?.2:.01;function bf(e,t,n){let r=vf(t),i=e.out.baked,a=[`root`,`yaw`,`pitch`];t===`Head`?a=[`head_yaw`,`head_pitch`,`spine_pitch`,`spine_yaw`,`root`,`yaw`]:/Hand|ForeArm/.test(t)?a=[`hand_${r}`,`armw_${r}`,`elbow_${r}`,`aswing_${r}`,`abend_${r}`,...Object.keys(i).filter(e=>RegExp(`^w_.+_${r}$`).test(e)),`spine_yaw`,`spine_pitch`,`root`,`yaw`]:/Foot|Leg/.test(t)&&(a=[`foot_${r}`,`knee_${r}`,`fpitch_${r}`,`root`]);let s=e=>{let t=i[e]?.[n-1],r=i[e]?.[n];return t===void 0||r===void 0||typeof t==`object`&&!Array.isArray(t)?0:(Array.isArray(t)?o(t,r):Math.abs(r-t))*yf(e)};return a.filter(e=>i[e]).sort((e,t)=>s(t)-s(e))}function xf(e,t,n){let r=t.subject.split(` `)[0],i=t.worstFrame,a=bf(e,r,i)[0]??`root`,{prev:o,next:s}=e.bracket(a,i),c=e.keys(a).find(e=>e.step&&Math.abs(e.f-i)<=1);if(c)return{cause:e.cause(a,i),hint:`${a}: a step key jumps at f${Math.round(c.f)}; ease it over 4+ frames (key the old value 4 frames earlier, drop the step)`};let l=Math.ceil((t.worst/t.limit)**+n*1.1*10)/10,u=Math.round(o?.f??i-4),d=Math.round(s?.f??i+4),f=Math.ceil((d-u)*(l-1)),p=/^w_(.+)_[lr]$/.exec(a);return{cause:e.cause(a,i),hint:`retime: stretch f${u}-f${d} by ${l}x (+${f} frames, move the keys from f${d} on later)${p?` - the hand switches to anchor ${p[1]} too fast`:``}; or fix.retime: true`,fix:e.spec.fix===!1?void 0:{path:`/fix/retime`,value:!0}}}function Sf(e,t,n){let[r,i]=n.split(/:(.*)/s);if(r===`joint`)return t.s.P[e.I[i]];if(r===`prop`){let[e,n]=i.split(`.`);return t.diag.props?.[e]?.[n]}return Xn(e.out.ctx,i,t.s,t.diag,t.c)}function Cf(e,t,n,r){let i=Er(e.out.baked,t),a=r(e.fr(t)),o=null;for(let[s,c,l]of n){let n=r(Ml(e.out.ctx,{...i,[s]:c},t))-a;n>1e-6&&(!o||n/l>o.gain/o.unit)&&(o={ch:s,value:c,gain:n,unit:l})}return{best:o,base:a,c0:i}}let wf=(e,t,n,r)=>e.fr(n).s.P[e.I[`${t===`l`?`Left`:`Right`}${_f[r][0]}`]],Tf={REACH(i,c){let[,l,u]=/^(leg|arm)_miss_([lr])$/.exec(c.subject)??[],d=c.worstFrame,f=c.worst+.002;if(l===`leg`){let e=i.fr(d).diag[`leg_fold_${u}`];if(e)return qd(i,u,d,Kd(`the ankle target is ${J(c.worst)} m inside the shin's reach: `,e),i.fr(d).c[`foot_${u}`]);let l=i.fr(d).c[`foot_${u}`]??[0,0,0],p=wf(i,u,d,`Leg`),m=t(l,p),h=o(p,l)-c.worst,g=m[1]*m[1]-r(m,m)+h*h,_=[m[0],0,m[2]],v=g>=0?`or root down ${J(-m[1]-Math.sqrt(g)+.002)} m${c.worst<=.05?` (fix.reach does this when on)`:``}`:`or root ${Yl(n(s(_),a(_)-h*.95))} m toward the foot (the foot is ${J(a(_))} m sideways of the hip: the root and foot keys disagree)`;return{cause:i.cause(`foot_${u}`,d),hint:`foot_${u}: move ${Yl(n(s(t(p,l)),f))} m toward the hip at f${d}, ${v}`,fix:{channel:`foot_${u}`,f:d,delta:n(s(t(p,l)),f)}}}let p=u===`l`?`Left`:`Right`,{s:m,diag:h}=i.fr(d),g=s(t(m.P[i.I[`${p}Hand`]],m.P[i.I[`${p}Arm`]])),_=h[`arm_fold_${u}`];if(_){let e=Zd(i,`hand_${u}`,d,n(g,f),` (the hand target is ${J(c.worst)} m inside the forearm's reach: the elbow would fold ${Math.round(_)}deg; it bends to 145deg and stops at 150deg)`);return{cause:e.cause,hint:`${e.hint}, or bring root ${Yl(n([-g[0],0,-g[2]],f))} m back from it`,fix:e.fix}}let v=Zd(i,`hand_${u}`,d,n(g,-f),``),y=i.hand(u,d).anchor,b=JSON.stringify([i.spec.goals??[],i.spec.checks?.contacts??[]]),x=y&&(i.spec.anchors?.[y]?.kind===`object`||b.includes(`"anchor:${y}"`)),S=i.spec.fits?.[i.hand(u,d).fit??-1],C=S?.effector?.prop&&i.fr(d).diag.props?.[S.effector.prop]?.[S.effector.point],w=C?o(C,e(Sf(i,i.fr(d),`anchor:${y}`),S.offset??[0,0,0]))+.01:f,T=x&&sf(i,u,d,Math.min(f,w));return T?{cause:v.cause,hint:`${T.hint} (out of reach by ${J(c.worst)} m), or ${v.hint}`,fix:T.fix}:{cause:v.cause,hint:`${v.hint} (out of reach by ${J(c.worst)} m), or bring root ${Yl(n([g[0],0,g[2]],f))} m closer`,fix:v.fix}},KNEE(r,i){let a=vf(i.subject),c=a===`l`?`Left`:`Right`,l=i.worstFrame,{s:u,c:d}=r.fr(l),f=u.P,m=r.I;if(/backward/.test(i.subject)){let n=t(f[m[`${c}ToeBase`]],f[m[`${c}Foot`]]);n[1]=0;let i=e(s(p(_(-(d.yaw??0)),n)),[0,.3,0]);return{cause:r.cause(`knee_${a}`,l),hint:`knee_${a}: key ${Yl(s(i))} (body frame, along the foot) at f${l}; a knee pole is +z forward, +x the character's left`,fix:{channel:`knee_${a}`,f:l,value:s(i)}}}if(/ folds /.test(i.subject))return qd(r,a,l,` (the knee folds ${J(i.worst)}deg, past its ${i.limit}deg stop; it bends freely to 150deg)`,f[m[`${c}Foot`]]);let h=f[m[`${c}UpLeg`]],g=f[m[`${c}Leg`]],v=f[m[`${c}Foot`]],y=o(h,g),b=o(g,v),x=(180-(i.limit-5))*Math.PI/180,S=Math.sqrt(y*y+b*b-2*y*b*Math.cos(x));return{cause:r.cause(`foot_${a}`,l),hint:`foot_${a}: move ${Yl(n(s(t(v,h)),S-o(h,v)))} m away from the hip at f${l} (the knee folds ${J(i.worst)} deg, limit ${i.limit})`,fix:{channel:`foot_${a}`,f:l,delta:n(s(t(v,h)),S-o(h,v))}}},ELBOW(e,r){let i=vf(r.subject),a=i===`l`?`Left`:`Right`,c=r.worstFrame,l=e.fr(c).s.P,u=e.I,d=l[u[`${a}Arm`]],f=l[u[`${a}ForeArm`]],p=l[u[`${a}Hand`]],m=n(s(t(p,d)),N(o(d,f),o(f,p),145)-o(d,p));return Zd(e,`hand_${i}`,c,m,` (the elbow folds ${J(r.worst)}deg, past its ${r.limit}deg stop; it bends freely to 145deg)`)},FLIP(e,n){let[,r,i]=/^(Left|Right)(Leg|ForeArm) /.exec(n.subject)??[],o=n.worstFrame;if(!i)return null;let s=vf(r),[c,l,u,d]=_f[i],f=e.I,p=+(/ over (\d+) frames$/.exec(n.subject)?.[1]??1),m=e.fr(o).diag,h=i===`ForeArm`&&(m[`arm_fold_${s}`]??m[`hold_${s}`]?.fold);if(h){let t=m[`hold_${s}`]?.fold&&Object.entries(e.spec.objects??{}).find(([,t])=>t.keys?.length&&e.spec.props?.[t.prop]?.hand===s)?.[0];return{cause:t?`objects.${t}.keys`:e.cause(e.hand(s,o).ch,o),hint:`the ${t?`keys of ${t} ask`:`hand target asks`} the elbow to fold ${Math.round(h)}deg at f${o}, past its 150deg stop, so the hand swings round the shoulder on the stop: move ${t?`the keyed pose`:`the target`} out from the shoulder (REACH says how far)`}}let g=n=>{let i=e.fr(n).s.P;return w(t(i[f[`${r}${l}`]],i[f[`${r}${c}`]]),t(i[f[`${r}${u}`]],i[f[`${r}${l}`]]))},_=Array.from({length:Math.max(1,p-1)},(e,t)=>p>1?o-1-t:o).reduce((e,t)=>g(t)<g(e)?t:e),v=g(_),y=`${d}_${s}`,b=e.out.baked[y]?.[Math.max(0,o-1)];if(b&&a(b)<.5)return{cause:e.cause(y,o),hint:`${y}: the pole passes near zero (length ${J(a(b))}) at f${o} while it turns between opposite keys; add an in-between pole key (e.g. the average direction, normalised) so it turns, not shrinks`};let x=v<12?`the limb is almost straight at f${_} (bend ${J(v)} deg), so the pole cannot hold it: bring the ${i===`Leg`?`foot`:`hand`} target ~0.02 m toward the ${i===`Leg`?`hip`:`shoulder`}, or `:``;return{cause:e.cause(y,o),hint:`${y}: ${x}keep the pole on one side through f${o}${b?` (key ${Yl(b)} there)`:``}`,fix:b&&{channel:y,f:o,value:b}}},SLIDE(e,t){let n=vf(t.subject),r=n===`l`?`Left`:`Right`,i=t.subject.includes(`heel`)?`heel`:`ball`,a=Math.max(0,t.first-1),o=t=>xt(e.sk,e.fr(t).s,r)[i],s=Math.hypot(o(t.last)[0]-o(a)[0],o(t.last)[2]-o(a)[2]),c=e.out.baked[`foot_${n}`]?.[a],l=e.out.baked[`fyaw_${n}`],u=e.out.baked[`foot_${n}`]?.[t.last];return l&&c&&u&&Math.hypot(u[0]-c[0],u[2]-c[2])<s/2&&Math.abs(l[t.last]-l[a])>1?{cause:e.cause(`fyaw_${n}`,t.worstFrame),hint:`fyaw_${n}: the flat foot turns ${J(l[a])} -> ${J(l[t.last])} deg over f${a}-f${t.last} and the ${i} slides ${J(s)} m; key fyaw_${n} ${J(l[a])} at f${t.last} so it turns once the foot is up, or lift the heel first (fpitch_${n} +) so it pivots on the ball`,fix:{channel:`fyaw_${n}`,f:t.last,value:l[a]}}:{cause:e.cause(`foot_${n}`,t.worstFrame),hint:`foot_${n}: hold it still over f${a}-f${t.last}${c?` (key ${Yl(c)} at both ends)`:``}; the ${i} slides ${J(s)} m${s<.01?`; fix.slide locks this when on`:``}`,fix:c&&{channel:`foot_${n}`,f:t.last,value:c}}},SPEED:(e,t)=>xf(e,t,1),POP:(e,t)=>xf(e,t,.5),ACC:(e,t)=>xf(e,t,.5),FLOAT:(e,t)=>({cause:e.cause(`root`,t.worstFrame),hint:`over f${t.first}-f${t.last} lower the root or key foot_l/foot_r back onto the ground, the floor or the top of an object under the foot (the ankle stands ${yt(e.sk)} m above it); a jump needs the hips to rise and fall; a seat or ledge the hips rest on goes in checks.supports (a foot on an object's top needs none)`}),LENGTH:()=>({cause:`compiler`,hint:`a bone changed length: report the spec, this is a compiler fault`}),CONTACT(e,r){let i=(e.spec.checks?.contacts??[]).find(e=>r.subject.startsWith(`${e.name}:`)),a=r.worstFrame,o=e.rel(a);if(!i)return null;let c=Ku(e.out,o,i.point),l=Ku(e.out,o,i.target),u=t(l,c),d=/^prop:([^.]+)/.exec(i.point),f=d?e.spec.props?.[d[1]]?.hand:vf(i.point.slice(6)),{diag:p}=e.fr(a),m=Math.max(p[`reach_over_${f}`]??0,p[`arm_miss_${f}`]??0),h=/^anchor:(.+)$/.exec(i.target)?.[1],g=(e.spec.fits??[]).findIndex(e=>e.side===f&&e.anchor===h),_=g>=0?`fits[${g}] (${f} -> anchor ${h})`:Zd(e,`hand_${f}`,a,u).cause,v=n(s([u[0],0,u[2]]),m+.01),y=p[`arm_fold_${f}`];if(m>.002&&y){let t=e.fr(a).s.P[e.I[`${f===`l`?`Left`:`Right`}Arm`]],r=n(s([t[0]-l[0],0,t[2]-l[2]]),m+.01);return{cause:_,hint:`inside the forearm's reach by ${J(m)} m at f${a} (the elbow would fold ${Math.round(y)}deg; it stops at 150deg): bring root ${Yl(r)} m back from ${h??`the target`}, or move the target out from the shoulder`,fix:{channel:`root`,f:a,delta:r}}}if(m>.002)return{cause:_,hint:`out of reach by ${J(m)} m at f${a}: bring root ${Yl(v)} m toward ${h??`the target`}, or move the target`,fix:{channel:`root`,f:a,delta:v}};let b=Zd(e,`hand_${f}`,a,u);return{cause:_,hint:`${b.hint}${g<0&&h?`; or add fits: [{ side: "${f}", anchor: "${h}", effector: ${d?`{ prop: "${d[1]}", point: "${i.point.split(`.`)[1]}" }`:`{ joints: [...] }`} }]`:``}`,fix:b.fix}},ORIENT(e,n){let i=(e.spec.checks?.orient??[]).find(e=>n.subject.startsWith(`${e.name}:`)),a=n.worstFrame;if(!i)return null;let o=/^prop:([^.]+)/.exec(i.from),l=o?e.spec.props?.[o[1]]?.hand:vf(i.from.slice(6)),u=i.minCos??.64,d=t=>i.along===`up`?[0,1,0]:i.along===`face`?j(e.sk,t.s,e.I.Head,[0,0,1]):s(i.along),f=n=>r(s(t(Sf(e,n,i.to),Sf(e,n,i.from))),d(n)),p=Er(e.out.baked,a),m=p[`wtwist_${l}`]??0,h=p[`wflex_${l}`]??0,{best:g,base:_}=Cf(e,a,[[`wtwist_${l}`,c(m+15,-85,85),15],[`wtwist_${l}`,c(m-15,-85,85),15],[`wflex_${l}`,c(h+15,-70,70),15],[`wflex_${l}`,c(h-15,-70,70),15]],f);if(!g)return{cause:e.cause(`wtwist_${l}`,a),hint:`turn the prop another way: wrist twist/flex do not help at f${a} (cos ${J(_)}, needs ${u}); change elbow_${l} or the prop points`};let v=g.ch.startsWith(`wtwist`)?m:h,y=g.ch.startsWith(`wtwist`)?85:70,b=c(v+(g.value-v)*Math.min(4,(u-_+.03)/g.gain),-y,y);return{cause:e.cause(g.ch,a),hint:`${g.ch}: key ${J(b)} deg at f${a} (now ${J(v)}; cos ${J(_)}, needs ${u})`,fix:{channel:g.ch,f:a,value:b}}},BELOW(e,t){let n=(e.spec.checks?.below??[]).find(e=>t.subject.startsWith(`${e.name}:`)),r=t.worstFrame;if(!n)return null;let i=vf(n.joint),a=`${/Leg/.test(n.joint)?`knee`:`elbow`}_${i}`,o=Er(e.out.baked,r)[a]??[0,-1,-.3],s=t=>t.s.P[e.I[n.ref]][1]-t.s.P[e.I[n.joint]][1],c=(n.min??.08)+.01,l=Er(e.out.baked,r),u=s(e.fr(r)),d=[[0,-1,0],[.5,-1,0],[-.5,-1,0],[0,-1,-.4],[0,-1,.4],[.5,-1,.5],[-.5,-1,.5],[.5,-1,-.4],[-.5,-1,-.4]].map(t=>({p:t,d:s(Ml(e.out.ctx,{...l,[a]:t},r)),turn:w(t,o)})),f=d.filter(e=>e.d>=c).sort((e,t)=>e.turn-t.turn)[0]??d.sort((e,t)=>t.d-e.d)[0];return f.d>u?{cause:e.cause(a,r),hint:`${a}: key ${Yl(f.p)} (body frame: +y up, +z forward) at f${r} (${n.joint} ${J(u)} m below ${n.ref} -> ${J(f.d)}, needs ${n.min??.08})`,fix:{channel:a,f:r,value:f.p}}:{cause:e.cause(a,r),hint:`${a}: no pole change lowers ${n.joint} at f${r}; lower the hand target instead`}},APPEAR(e,n){let r=(e.spec.checks?.appear??[]).find(e=>n.subject.startsWith(`${e.name}:`)),i=n.worstFrame;if(!r)return null;let o=null;for(let n=Math.max(e.out.f0??0,i-24);n<=Math.min((e.out.f0??0)+e.out.frames.length-1,i+24);n++){let i=a(t(Ku(e.out,e.rel(n),`joint:${r.joint}`),Ku(e.out,e.rel(n),`anchor:${r.anchor}`)));(!o||i<o.d)&&(o={q:n,d:i})}let s=r.tol??.035;return{cause:e.cause(r.channel,i),hint:o.d<=s?`${r.channel}: move the step from f${i} to f${o.q} (${r.joint} ${J(o.d)} m from ${r.anchor} there)`:`${r.joint} never gets within ${s} m of ${r.anchor} near f${i} (closest ${J(o.d)} m at f${o.q}): hold its weight w_${r.anchor}_${vf(r.joint)} at 1 around the switch`}}},Ef=.003;function Df(e,t,n,r){let i=e.hand(t,n);if(i.ch===`hand_${t}`)return{channel:i.ch,f:n,delta:r};if(i.fit!==null&&i.fit!==void 0)return e.spec.fits[i.fit].offset?{path:`/fits/${i.fit}/offset`,delta:r}:{path:`/fits/${i.fit}/offset`,value:r};if(i.anchor&&e.spec.anchors?.[i.anchor]?.kind===`world`)return{path:`/anchors/${i.anchor}/p`,delta:r}}function Of(e,t,n,r,i){let a=Zd(e,`hand_${t}`,n,r,i),o=Df(e,t,n,r);return o?{...a,fix:o}:a}function kf(e,t,n,r){let i=e.spec.anchors?.[e.hand(t,n).anchor],a=e.out.ctx.scene;return i?.kind===`object`&&[a.objects?.[i.object]?.ancestors,En(a)[i.object]?.ancestors,[i.object]].some(e=>e?.includes(r))}function Af(n,r){let i=p(m(n.R),t(r,n.pos)),a=[0,n.half[1],0];return e(n.pos,p(n.R,i.map((e,t)=>c(e,a[t]-n.half[t],a[t]+n.half[t]))))}let jf={REACH:(e,t)=>nd(e,t)??Tf.REACH(e,t),GRAB(e,r){let i=r.subject.split(`:`)[0],o=r.worstFrame,s=e.spec?.objects?.[i],c={cause:`objects.${i}.grab`,hint:`${i}: move the hand to it by f${o}, or set objects.${i}.grab to the frame the hand arrives`};if(!e.out||!K(s))return c;let l=null;for(let t=o-24;t<=o+24;t++){let n=pd(e.out,i,t);n!==null&&n<=r.limit&&(!l||Math.abs(t-o)<Math.abs(l[0]-o))&&(l=[t,n])}if(l)return{cause:c.cause,hint:`${i}: set objects.${i}.grab to ${l[0]} (the hand is ${(l[1]*100).toFixed(1)} cm from it there, ${(r.worst*100).toFixed(1)} cm at f${o})`,fix:{path:`/objects/${i}/grab`,value:l[0]}};let{sk:u,spec:d,scene:f}=e.out.ctx,p=fs(u,d,e.fr(o).s,s),m=U(f,i).map(e=>Af(f.objects[e],p)).reduce((e,n)=>a(t(n,p))<a(t(e,p))?n:e),h=a(t(m,p)),g=n(t(m,p),h>1e-9?(h-r.limit/2)/h:0),_=Of(e,d.props[s.prop].hand,o,g,` (to ${i} at grab)`);return{..._,hint:`${_.hint}; or set objects.${i}.grab to the frame the hand arrives`}},COLLIDE(e,r){let[,i,a]=/^(\S+) x (.+)$/.exec(r.subject)??[],o=e.spec?.objects??{},s=K(o[a])?a:K(o[i])?i:null;if(!s||!e.out)return af.COLLIDE(e,r);let c=o[s],l=e.spec.props[c.prop].hand,[u,d]=To(e.out,c),f=r.worst-r.limit+Ef,p=r.worstFrame,m=p>=u&&p<=d,h=Math.min(p,d),g=Lo(e.out,s,p)??[];if(s===a){let t=ht.find(e=>e[0]===i),{s:r}=e.fr(p);if(!t||!g.length)return null;let a=gt(r.P[e.I[t[1]]],r.P[e.I[t[2]]]),[o,c]=g.flatMap(e=>a.map(t=>[e,t])).reduce((e,t)=>B(t[1],t[0])<B(e[1],e[0])?t:e),u=St(c,o),d=/^(hand|farm|uarm)_([lr])$/.exec(i)?.[2];return m&&d&&d!==l&&kf(e,d,p,s)?Zd(e,`hand_${d}`,p,n(u,f),` (out of ${s})`):m?Of(e,l,p,n(u,-f),` (${s} off ${i})`):Zd(e,i,p,n(u,f),` (out of ${s})`)}let _=e.out.objectsAt(e.rel(p))[a];if(!_||!g.length)return null;let v=Lo(e.out,s,Math.max(u,r.first-1))??g,y=g.map((e,n)=>ld(e,_,t(v[n].pos,_.pos))).reduce((e,t)=>t.depth>e.depth?t:e);return Of(e,l,h,n(y.axis,y.depth-r.limit+Ef),m?` (${s} out of ${a})`:` (${s} is put down in ${a}; it stays where the hand lets go at f${d})`)}},Mf=e=>e.spec?.fix?.grasp===!1,Nf={cause:`spec.fix.grasp false: the fingers stopped at the preset curl`,hint:`turn grasp physics on (fix.grasp true): the fingers close until they touch and the thing rests in the hand`,fix:{path:`/fix/grasp`,value:!0}};function Pf(e,t,n){let{scene:r}=e.out.ctx,i=e.out.objectsAt?.(e.rel(n))??{};return U(r,t).map(t=>({...r.objects[t],...e.out.ctx.objectPose?.(t,n)??i[t]??{}}))}let Ff={FINGER_GAP(t,r){if(Mf(t))return Nf;let[,i,a]=/^(left|right) fingers off (\S+)$/.exec(r.subject)??[];if(!a||!t.out)return null;let o=i===`left`?`l`:`r`,s=i===`left`?`Left`:`Right`,c=r.worstFrame,{s:l}=t.fr(c),u=n(e(l.P[t.I[`${s}Hand`]],l.P[t.I[`${s}HandMiddle1`]]),.5),d=Pf(t,a,c);if(!d.length)return null;let f=d.reduce((e,t)=>Et(t,u)<Et(e,u)?t:e),p=Et(f,u);return Zd(t,`hand_${o}`,c,n(Dt(f,u),-Math.max(0,p-.03)),` (onto ${a}: the fingers reach it from nearer)`)},FINGER_PEN(e,t){if(Mf(e))return Nf;let[,r,i]=/^(\S+) into (\S+)$/.exec(t.subject)??[];if(!i||!e.out||e.I[r]===void 0)return null;let a=r.startsWith(`Left`)?`l`:`r`,o=t.worstFrame,s=e.fr(o).s.P[e.I[r]],c=Pf(e,i,o);if(!c.length)return null;let l=c.reduce((e,t)=>Et(t,s)<Et(e,s)?t:e);return Zd(e,`hand_${a}`,o,n(Dt(l,s),t.worst-t.limit+.005),` (out of ${i})`)},SUPPORT(e,t){if(Mf(e))return Nf;let n=/^(\S+) not supported$/.exec(t.subject)?.[1],r=e.spec?.objects?.[n];if(!r||!e.out)return null;let i=e.out.ctx.scene.objects[U(e.out.ctx.scene,n)[0]],a=i?Ct(i).kind:`box`,o=a===`sphere`?`spherical`:a===`cylinder`?`power`:`palm`;return r.grasp===o?{cause:`objects.${n}.grasp ${o}`,hint:`${n} rests on the fingers only: hold it nearer the palm (the hand lower under it) or with both hands`}:{cause:`objects.${n} (no grasp for a ${a})`,hint:`${n}: set objects.${n}.grasp to "${o}" (how a ${a} is held)`,fix:{path:`/objects/${n}/grasp`,value:o}}},CATCH(e,n){let[,r]=/^(\S+) missed by/.exec(n.subject)??[];if(!e.spec?.objects?.[r]||!e.out)return null;let i=n.worstFrame,o=e.spec.anchors?.[`${r}Catch0`]?.p;if(!o)return{cause:`objects.${r}.grab`,hint:`${r}: the hand is ${(n.worst*100).toFixed(1)} cm from it at f${i}; reach it earlier`};let s=null;for(let n=Math.max(0,i-30);n<i;n++){let i=jo(e.out,r,n)?.pos;i&&(!s||a(t(i,o))<s.d)&&(s={f:n,d:a(t(i,o))})}let c=s?i-s.f:0,l=`${r}: the hand gets there ${c} frame(s) after it passed (${(n.worst*100).toFixed(1)} cm off at f${i}); start catchObject ${c} frame(s) earlier`;return c>0?{cause:`use catchObject ${r} f0`,hint:l,fix:{ops:e=>{let t=(e.use??[]).findIndex(e=>e.skill===`catchObject`&&e.args?.object===r);return t<0?null:[{op:`replace`,path:`/use/${t}/f0`,value:Math.max(0,e.use[t].f0-c)}]}}}:{cause:`use catchObject ${r}`,hint:l}}},If=[`add`,`remove`,`replace`,`test`],Lf=e=>{throw Error(`MOTION_SPEC_INVALID: ${e}`)},Rf=(e,t)=>JSON.stringify(e)===JSON.stringify(t),zf=e=>e.split(`/`).slice(1).map(e=>e.replaceAll(`~1`,`/`).replaceAll(`~0`,`~`)),Bf=e=>e.map(e=>`/${String(e).replaceAll(`~`,`~0`).replaceAll(`/`,`~1`)}`).join(``);function Vf(e,t){if(Array.isArray(e))return`${t} has ${e.length} entries`;let n=Object.keys(e);return n.length?`${t} has ${n.slice(0,8).join(`, `)}${n.length>8?`, ...`:``}`:`${t} is empty`}function Hf(e,t,n){let r=e;for(let e=0;e<t.length-1;e++){let i=t[e],a=e?t[e-1]:`the spec`;(typeof r!=`object`||!r||!Object.hasOwn(r,Array.isArray(r)?Number(i):i)||Array.isArray(r)&&!/^\d+$/.test(i))&&Lf(`${n}: ${Bf(t.slice(0,e+1))} does not exist${r&&typeof r==`object`?` (${Vf(r,a)})`:``}`),r=r[Array.isArray(r)?Number(i):i]}return(typeof r!=`object`||!r)&&Lf(`${n}: ${Bf(t.slice(0,-1))} is not an object or array`),r}function Uf(e,t,n,r){(!t||typeof t!=`object`||!If.includes(t.op))&&Lf(`patch[${n}]: op must be ${If.join(`, `)}, got ${JSON.stringify(t?.op)}`);let i=`patch[${n}] (${t.op} ${t.path})`;if((typeof t.path!=`string`||t.path!==``&&!t.path.startsWith(`/`))&&Lf(`${i}: path must be a JSON Pointer such as "/keys/0/f"`),t.op!==`remove`&&!Object.hasOwn(t,`value`)&&Lf(`${i}: needs a value`),t.path===``){if(t.op===`replace`)return structuredClone(t.value);if(t.op===`test`)return Rf(e,t.value)||Lf(`${i}: the spec differs from the test value`),e;Lf(`${i}: ${t.op} needs a path below the spec`)}let a=zf(t.path),o=a[a.length-1],s=Hf(e,a,i),c=a.length>1?a[a.length-2]:`the spec`;if(Array.isArray(s)){if(o===`-`)return t.op!==`add`&&Lf(`${i}: "-" (append) works with add only`),s.push(structuredClone(t.value)),e;let n=/^\d+$/.test(o)?Number(o):NaN,r=t.op===`add`?s.length:s.length-1;return n>=0&&n<=r||Lf(`${i}: ${t.path} does not exist (${Vf(s,c)})`),t.op===`add`?s.splice(n,0,structuredClone(t.value)):t.op===`remove`?s.splice(n,1):t.op===`replace`?s[n]=structuredClone(t.value):Rf(s[n],t.value)||Lf(`${i}: value is ${JSON.stringify(s[n])?.slice(0,60)}, not ${JSON.stringify(t.value)?.slice(0,60)}`),e}return t.op===`replace`&&r&&!Object.hasOwn(s,o)?r.push(`${i}: ${t.path} did not exist (${Vf(s,c)}); added`):t.op!==`add`&&!Object.hasOwn(s,o)&&Lf(`${i}: ${t.path} does not exist (${Vf(s,c)})`),t.op===`remove`?delete s[o]:t.op===`test`?Rf(s[o],t.value)||Lf(`${i}: value is ${JSON.stringify(s[o])?.slice(0,60)}, not ${JSON.stringify(t.value)?.slice(0,60)}`):s[o]=structuredClone(t.value),e}function Wf(e,t,n){return Array.isArray(t)||Lf(`patch must be an array of { op, path, value }`),t.reduce((e,t,r)=>Uf(e,t,r,n),structuredClone(e))}let Gf=e=>Array.isArray(e)?e.map(Gf):typeof e==`number`?Math.round(e*1e4)/1e4:e,Kf=e=>e.split(`/`).slice(1).map(e=>e.replaceAll(`~1`,`/`).replaceAll(`~0`,`~`)),qf=e=>e.map(e=>`/${String(e).replaceAll(`~`,`~0`).replaceAll(`/`,`~1`)}`).join(``),Jf=e=>typeof e==`number`&&Number.isFinite(e);function Yf(e,t){let n=e;for(let e of t){if(typeof n!=`object`||!n||!Object.hasOwn(n,e))return;n=n[e]}return n}function Xf(e,t){if(Jf(e)&&Jf(t))return e+t;if(Array.isArray(e)&&Array.isArray(t)&&e.length===t.length&&e.every(Jf)&&t.every(Jf))return e.map((e,n)=>e+t[n])}function Zf(e,t,n){let r=-1;return(e.keys??[]).forEach((i,a)=>{!i?.set||!Object.hasOwn(i.set,t)||!Jf(i.f)||Math.abs(i.f-n)>2||(r<0||Math.abs(i.f-n)<Math.abs(e.keys[r].f-n))&&(r=a)}),r}function Qf(e,t,n,r){let i=/^use\[(\d+)\] standIdle$/.exec(r?.src??``),a=n.delta;if(!i||n.channel!==`root`||!Array.isArray(a)||Math.hypot(a[0],a[2])<Math.abs(a[1]))return null;let o=Number(i[1]),s=t.use?.[o],c=e.fr(r.f).c.root;if(s?.skill!==`standIdle`)return null;let l=Array.isArray(s.args?.at)?s.args.at:c&&[c[0],c[2]];if(!l)return null;let u=Gf(l.map((e,t)=>e+(t===0?a[0]:t===l.length-1?a[2]:0)));return s.args?[{op:Object.hasOwn(s.args,`at`)?`replace`:`add`,path:`/use/${o}/args/at`,value:u}]:[{op:`add`,path:`/use/${o}/args`,value:{at:u}}]}function $f(e,t,n,r){let i=t.use?.[n],a=i?.args??{},o=r.delta,s=(e.fr(r.f).c.yaw??0)*Math.PI/180,c=o[0]*Math.sin(s)+o[2]*Math.cos(s);if(i?.skill!==`walkTo`||Math.abs(o[0]*Math.cos(s)-o[2]*Math.sin(s))>Math.abs(c))return null;let l=(e,t)=>[i.args?{op:Object.hasOwn(a,e)?`replace`:`add`,path:`/use/${n}/args/${e}`,value:Gf(t)}:{op:`add`,path:`/use/${n}/args`,value:{[e]:Gf(t)}}];if(a.steps===void 0&&Array.isArray(a.to))return l(`to`,a.to.map((e,t)=>e+(t===0?o[0]:t===a.to.length-1?o[2]:0)));let u=a.steps??8,d=u<=2?`firstStep`:`step`,f=(a[d]??(d===`step`?.5:.4))+c/Math.max(1,u-2);return u>=2&&f>.05?l(d,f):null}function ep(e,t,n){let r=e.keys(`root`),i=e=>/^keys\[\d+\]$/.test(e.src),a=r.findLastIndex(e=>e.f<=n.f);if(a<0||!i(r[a]))return null;let o=a,s=a;for(;o>0&&i(r[o-1]);)--o;for(;s<r.length-1&&i(r[s+1]);)s+=1;return r.slice(o,s+1).map(e=>{let r=Number(/\d+/.exec(e.src)[0]),i=t.keys?.[r]?.set?.root;return Array.isArray(i)&&i.length===3?{op:`replace`,path:`/keys/${r}/set/root`,value:Gf([i[0]+n.delta[0],i[1],i[2]+n.delta[2]])}:null}).filter(Boolean)}function tp(e,t,n){if(!Array.isArray(n.delta)||!e?.fr)return null;let r=e.bracket(`root`,n.f).prev,i=/^use\[(\d+)\] (standIdle|walkTo)\b/.exec(r?.src??``);if(i?.[2]===`standIdle`)return Qf(e,t,n,r);if(i?.[2]===`walkTo`)return $f(e,t,Number(i[1]),n);let a=r&&ep(e,t,n);return a?.length?a:null}function np(e,t,n){let{channel:r,f:i}=n;if(typeof r!=`string`||!Jf(i))return null;if(n.hold)return tp(e,t,n);let a=e=>n.value===void 0?Xf(e,n.delta):n.value,o=Zf(t,r,i);if(o>=0){let e=a(t.keys[o].set[r]);return e===void 0?null:[{op:`replace`,path:qf([`keys`,o,`set`,r]),value:Gf(e)}]}if(!e?.fr)return null;let{prev:s,next:c}=e.bracket(r,i),l=s&&i-s.f<=2?s:c&&c.f-i<=2?c:null,u=n.delta!==void 0&&Qf(e,t,n,l);if(u)return u;let d=a(e.fr(i).c[r]);return d===void 0?null:[{op:`add`,path:`/keys/-`,value:{f:Math.round(i),set:{[r]:Gf(d)}}}]}function rp(e,t){if(typeof t.path!=`string`||!t.path.startsWith(`/`))return null;let n=Kf(t.path),r=Yf(e,n),i=t.delta===void 0?t.value:r===void 0&&t.path.endsWith(`/offset`)?t.delta:Xf(r,t.delta);return i===void 0?null:r===void 0?Yf(e,n.slice(0,-1))===void 0?n.length===2&&n[0]===`fix`?[{op:`add`,path:qf([n[0]]),value:{[n[1]]:Gf(i)}}]:null:[{op:`add`,path:t.path,value:Gf(i)}]:[{op:`replace`,path:t.path,value:Gf(i)}]}function ip(e,t,n){let r=e.ops?e.ops(n):e.channel===void 0?e.path===void 0?null:rp(n,e):np(t,n,e);if(!r)return null;try{return JSON.stringify(Wf(n,r))===JSON.stringify(n)?null:r}catch{return null}}function ap(e,{X:t,userSpec:n}={}){if(!n||typeof n!=`object`)return e;for(let r of e)if(r.fix)try{let e=ip(r.fix,t,n);e&&(r.patch=e)}catch{}return e}let op={...af,...Tf,...jf,...gf,...Ud,...Dl,...Ff};function sp(e){let t=new Map;for(let n of e.keys??[])for(let e of Object.keys(n.set??{}))t.has(e)||t.set(e,[]),t.get(e).push({f:n.f,src:n.src??`keys`,step:n.step?.includes(e)});for(let e of t.values())e.sort((e,t)=>e.f-t.f);return t}function cp(e,t,n){let r=sp(t),i={sk:e,out:t,spec:n,I:e.index,fr:e=>t.frames[Math.max(0,Math.min(t.frames.length-1,e-(t.f0??0)))],rel:e=>Math.max(0,Math.min(t.frames.length-1,e-(t.f0??0))),keys:e=>r.get(e)??[],bracket(e,t){let n=i.keys(e),r=null,a=null;for(let e of n)if(e.f<=t)r=e;else{a=e;break}return{prev:r,next:a}},cause(e,t){let{prev:n,next:r}=i.bracket(e,t);if(!n&&!r)return`${e} (not keyed)`;let a=n&&t-n.f<=2?n:r&&r.f-t<=2?r:null;return a?`${a.src} f${Math.round(a.f)} ${e}`:r?n?`${e} between ${n.src} f${Math.round(n.f)} and ${r.src===n.src?``:`${r.src} `}f${Math.round(r.f)}`:`${e} before ${r.src} f${Math.round(r.f)}`:`${e} held since ${n.src} f${Math.round(n.f)}`},hand(e,t){let r=i.fr(t).c;if(!(r[`armw_${e}`]>.5))return{label:`arm ${e} (FK: aswing_${e}/abend_${e}, or armw_${e} 1 and hand_${e})`,ch:`aswing_${e}`};let a=null;for(let[t,n]of Object.entries(r)){let r=/^w_(.+)_([lr])$/.exec(t);r&&r[2]===e&&n>.5&&(!a||n>a.w)&&(a={w:n,anchor:r[1],ch:t})}if(!a)return{label:`hand_${e}`,ch:`hand_${e}`};let o=(n.fits??[]).findIndex(t=>t.anchor===a.anchor&&t.side===e);return{label:`anchor ${a.anchor}`,ch:a.ch,anchor:a.anchor,fit:o>=0?o:null}}};return i}function lp(e,t,n,r,{limit:i=16,userSpec:a}={}){if(!r.length)return r;let o=cp(e,t,n);for(let e of r.slice(0,i))try{let t=op[e.kind]?.(o,e);t&&(e.cause=t.cause,e.hint=t.hint),t?.fix&&Object.defineProperty(e,"fix",{value:t.fix,enumerable:!1,configurable:!0})}catch(t){e.cause=`unknown`,e.hint=`no hint (${t.message})`}return ap(r,{X:o,userSpec:a})}let up=new Set([`ORIENT`,`BELOW`,`CLEARANCE`]),dp=e=>Math.max(0,up.has(e.kind)?e.limit-e.worst:e.worst-e.limit),fp=e=>e.count*(1+Math.min(3,dp(e)/Math.max(Math.abs(e.limit),.01))),pp=e=>Math.round(fp(e)*100)/100,mp=/(?<![\w\-.[])[-+]?\d+(\.\d+)?|\bf\d+\b/g,hp=e=>{let t=e.subject.indexOf(`: `),n=t>=0?e.subject.slice(0,t+2):``;return`${e.kind} ${n}${e.subject.slice(n.length).replace(mp,e=>e[0]===`f`?`f#`:`#`)}`};function gp(e){for(let t of e)t.excess=Math.round(dp(t)*1e4)/1e4,t.severity=pp(t);return e.sort((e,t)=>(t.kind===`GOAL`)-(e.kind===`GOAL`)||t.severity-e.severity||t.count-e.count)}function _p(e){let t=new Map;for(let n of e){let e=hp({kind:n.kind,subject:n.msg}),r=t.get(e)??{kind:n.kind,subject:n.msg,count:0,worst:n.value,worstFrame:n.frame,first:n.frame,last:n.frame,limit:n.limit};r.count++,r.first=Math.min(r.first,n.frame),r.last=Math.max(r.last,n.frame),(up.has(n.kind)?n.value<r.worst:n.value>r.worst)&&(r.worst=n.value,r.worstFrame=n.frame,r.subject=n.msg),t.set(e,r)}return gp([...t.values()])}function vp(e,t,n,r={}){let i=n.checks??{},a=_u(t,n.goals??[]),o=[...Mi(e,t,{allow:i.allow,limits:i.limits,mouthPoints:i.mouthPoints}),...Zu(t,i),...hd(e,t,n),...kd(e,t),...Tl(e,t),...yi(t,i),...xi(e,t,i,{...Ai,...i.limits}),...Vd(e,t),...Gd(e,t,i),...a.issues];if(t.f0)for(let e of o)e.frame+=t.f0;let s=_p(o);return r.explain!==!1&&lp(e,t,n,s,{userSpec:r.userSpec}),{ok:o.length===0,total:o.length,problems:s.length,...n.goals?{goals:a.results}:{},groups:s}}let yp=[`contacts`,`orient`,`below`,`clearance`,`appear`];function bp(e){let t=e?.checks;if(!t||typeof t!=`object`||!yp.some(e=>Array.isArray(t[e])&&t[e].some(e=>e&&typeof e==`object`&&e.name===void 0)))return e;let n={...t};for(let e of yp)Array.isArray(t[e])&&(n[e]=t[e].map((t,n)=>t&&typeof t==`object`&&t.name===void 0?{...t,name:`${e}[${n}]`}:t));return{...e,checks:n}}let xp=(e,t)=>({...t,...e?{description:e}:{}}),Y=(e,t)=>xp(e,{type:`number`,...t}),Sp=(e,t)=>xp(e,{type:`integer`,...t}),X=(e,t)=>xp(e,{type:`string`,...t}),Cp=(e,t)=>xp(e,{type:`boolean`,...t}),Z=(e,t)=>xp(t,{$ref:`#/$defs/${e}`}),wp=e=>xp(e,{enum:[`l`,`r`]}),Tp=(e,t)=>xp(t,{type:`array`,prefixItems:[e,e],minItems:2,maxItems:2});`${Zt.join(`|`)}`,`${ht.map(e=>e[0]).join(`, `)}`,Sp(`frame`,{minimum:0}),X(`written by the skills expander; leave it out`),X(`a skill name (topic skills)`),Sp(`first frame`,{minimum:0});let Ep={type:{const:`walk`},f0:Sp(`first frame; needs a standing pose keyed before it`,{minimum:0}),steps:Sp(`footsteps`,{minimum:1,default:8}),step:Y(`stride, m`,{default:.5}),period:Sp(`frames per step`,{default:20}),swing:Sp(`frames a foot is in the air`,{default:14}),halfWidth:Y(`feet half-width, m`,{default:.08}),lift:Y(`foot lift, m`,{default:.08}),heading:Tp({type:`number`},`[h0, h1] degrees; ${un.yaw}`),turnDistance:Y(`m walked while the heading turns h0 -> h1`,{default:.4}),pelvisHigh:Y(`hips offset over the stance foot, m`),pelvisLow:Y(`hips offset at double support, m`),firstStep:Y(`first stride, m`,{default:.4}),armSwing:Y(`degrees`,{default:14}),vary:Y(`0..2, uneven steps as people walk (build-up, slow stop, left/right, turns) around the averages above; 0 = every step period frames, step m`,{default:1,minimum:0,maximum:2})};Sp(null,{minimum:1,maximum:2e4}),X(`a short name for the take`),Y(`m; a hand moving between targets arcs around the torso this far out`,{default:.25}),Sp(`compiles at most`,{minimum:1,maximum:64,default:32}),Z(`vec3`,`world: a world point (m); body: root-relative, ${un.pole}`),X(`bone name`),Z(`vec3`,`world offset, m`),X(`bone`),X(`bone`),Y(`0 = from .. 1 = to`,{default:.5}),Y(`with reach: the lowest t to slide back to`,{default:.15}),wp("slide toward `from` until this arm reaches"),X(`scene object id`),Z(`vec3`,`point in the object's frame, m`),X(`a prop held by the other hand`),X(`a point of that prop, or origin`),X(`a character id`),X(`a socket of the object`),wp(),(e=>Z(`vec3`,`${e} [d, l, n] on the palm axes (d to the middle finger, l to the thumb side, n = d x l), m`))(`point:`),wp(),X(`an anchor name`),X(),X(),Z(`vec3`,`world offset, m`);let Dp={f:Sp(),pos:Z(`vec3`),rot:{type:`array`,items:{type:`number`},minItems:4,maxItems:4,description:`quat x, y, z, w`},aim:Z(`vec3`,`or rot: +Y dir`),roll:Y(`deg about aim`),lean:Y(`aim tilt`,{minimum:0,maximum:45})},Op={type:`object`,properties:{at:Sp(),onto:X(),bounce:Y(null,{default:.25,minimum:0,maximum:.6}),tumble:{type:`boolean`},v:{oneOf:[{$ref:`#/$defs/vec3`},{const:`hand`}],description:`m/s`}},required:[`at`],additionalProperties:!1};Z(`vec3`,`world hinge point`),Z(`vec3`,`default +Y`),X(`angle channel, deg`),X(`spec.props name: it rides that hand`),Z(`vec3`,`palm offset [d, l, n]`),Z(`vec3`,`+Y on the palm, fixed`),X(`channel: shown > 0.5`),X(`channel: strength 0..4`),Sp(`hand takes it`,{default:0}),Sp(`let go (else end)`),Object.fromEntries(Gt.map(e=>[e,Dp[e]])),{...Op},Y(`kg`,{minimum:.01,maximum:60}),Z(`ref`),Z(`vec3`),X(`the QA group's name`),Z(`checkRef`),Z(`checkRef`),Y(`m; default contacts 0.01, appear 0.035, supports 0.04`),Z(`when`),Z(`checkRef`),Z(`checkRef`),Y(`default 0.64`),X(`bone`),X(`bone`),Y(`m; below default 0.08`),Z(`part`),X(`scene object id`),Z(`frameSpan`),X(`a visibility channel`),X(`an anchor name`),Y(`allowed depth, m`),Object.fromEntries(Object.entries(Ai).map(([e,t])=>[e,Y(null,{default:t})])),X(`prop.point`),X(),Tp({$ref:`#/$defs/ref`},`distance <= tol`),Tp({$ref:`#/$defs/ref`},`a.y - b.y >= by`),X("a ref or an object id: an object's base moves at least `by` from the scene pose; any other point (prop:, joint:, landmark:, anchor:) at least `by` from where it is at at[0] (at: [f0, f1])"),X("a ref or an object id: stays within `tol` of where moved measures from (an object: its scene pose; a point: where it is at at[0], at: [f0, f1])"),X("a point ref: its speed reaches `by` m/s (mode any by default)"),Y(`m; near default 0.05, still 0.01`),Y(`above m, default 0; moved m, default 0.05; speed m/s, needed`),Sp(null,{minimum:0}),X(`a goal's name: at starts after the first frame that goal holds`),X(`a goal's name: at ends before the first frame that goal holds`),X(`the key of the answer`),Tp({$ref:`#/$defs/ref`},`min/max/mean/first/last m`),Z(`ref`,`world points`),Z(`frameSpan`,`absolute, default the whole take`),Sp(`position: every n frames (<= 16 rows)`,{minimum:1}),Cp(`wrist targets past reach go onto it softly; hips lower for near-miss ankles`,{default:!0}),Cp(`lifts sunk soles; a planted heel or ball drifting under 1 cm stays put; a heel-up foot pivots on its ball`,{default:!0}),Cp(`arms kept out of the body's skin (wrists, elbows)`,{default:!0}),Cp(`speed spikes borrow frames from the neighbouring keys`,{default:!1}),Cp(`a standIdle stance in a scene body slides out of it (observations.placed)`,{default:!0}),Cp(`fingers close on what the hand holds; it rests in the hand`,{default:!0});let kp=e=>e.f0+16+(e.steps??8)*(e.period??20)+18,Ap=e=>({type:`array`,items:{type:`number`},minItems:2,maxItems:2,description:e}),jp=e=>Object.fromEntries(e.map(e=>[e,X(e===`car`?`car prefix: fills <car>_Seat_Driver, _Seat_Driver_Back, _Door_L/R, _Door_Handle, _Steering_Wheel`:`scene object id (overrides the car default)`)])),Mp=Object.fromEntries(Kt.filter(e=>e!==`type`&&e!==`f0`).map(e=>[e,Ep[e]])),Np={standIdle:{args:{at:Ap(`[x, z] world m; default the root keyed before f0, else within reach of the next reachGrip's anchor (output.observations.placed); a stance in a body slides out of it`),heading:Y(`degrees (yaw); default the keyed yaw`),until:Sp(`hold the pose to this frame`)},needs:`args.at, or a root keyed before f0`,frames:`1 (to args.until when given)`,adds:[`keys: a standing pose, FK arms down`],example:{f0:0,args:{at:[0,0],heading:0}},place:{at:.03}},walkTo:{args:{to:Ap(`[x, z] world m: the heading turns toward it and the steps cover the distance`),...Mp,heading:{oneOf:[Y(),Ep.heading],description:`degrees: h1, or [h0, h1]`},arrive:Sp(`with to and no steps/period/step/swing: the frame to end by (feet together); the engine picks the cadence (observations.planned)`)},needs:`a standing pose keyed before f0`,frames:`16 + steps * period + 18`,adds:[`keys: a walk ending feet together`],example:{f0:10,args:{to:[.5,1.5]}},tune:{lift:.01,halfWidth:.01,firstStep:.01,step:.01},place:{firstStep:.03,step:.03}},reachGrip:{args:{anchor:X(`not ending in _l/_r`),side:wp(`the hand`),reach:Sp(`frames to reach`,{default:20}),hold:Sp(`frames held`,{default:22}),release:Sp(`frames to let go`,{default:12}),elbow:Z(`vec3`,`pole`),fingers:{enum:rn,default:`grip`},define:{type:`object`,description:`defines anchor args.anchor`},orient:{type:`boolean`,description:`false: no grip turn`}},required:[`anchor`,`side`],needs:`args.anchor in anchors or define`,frames:`reach + hold + release (54)`,adds:[`props.<anchor>Grip (fingers point)`,`fits: fingers on the anchor`,`checks.contacts "fingers on <anchor>"`,`checks.allow hand on its object`,`keys: w_<anchor>_<side>, fingers_<side>, armw_<side>`],example:{f0:10,args:{anchor:`bar`,side:`r`,define:{kind:`world`,p:[-.2,1.2,.33]}}},tune:{reach:2,release:2}},sitInSeat:{args:{...jp([`car`,`seat`,`seatBack`,`wheel`]),offset:{...Z(`vec3`,`hips from the seat top, seat frame`),default:[0,.17,.02]},floor:Y(`floor y under the feet, m`)},needs:`args.car or args.seat (a scene box)`,frames:`1`,adds:[`keys: seated, hands at the wheel`,`checks.supports seat`,`checks.allow seat, back, wheel`],example:{f0:0,args:{car:`Car`}}},exitCar:{args:{...jp([`car`,`seat`,`door`,`handle`]),side:wp(`driver door side, default l`),channel:X(`the door hinge channel`,{default:`door`})},needs:`seated in the car before f0 (sitInSeat); a car on the ground (seat base y 0.4)`,frames:`246`,adds:[`objects: door and handle hinges`,`anchors handle, lthigh, rthigh`,`props.grip`,`fits, checks.contacts, checks.allow`],example:{f0:0,args:{car:`Car`}}},flip:{args:{kind:{enum:[`back`,`front`,`cartwheel`],default:`back`,description:`a standing back or front tuck, or a cartwheel`},travel:Y(`back/front: m along the heading at landing (default back -0.25, front 0.4)`),side:wp(`cartwheel: toward the character's l (default) or r`),heading:Y(`degrees (yaw); default the keyed yaw`)},needs:`a standing pose keyed before f0`,frames:`back/front 58 (one turn, the hips 0.5 m up); cartwheel 68`,adds:[`keys every frame: back/front crouch, push-off, a ballistic tucked flight turning the whole body (pitch to -360 back, +360 front) and a landing that stands up`,`cartwheel: a side step, then the whole body rolls 360 like a wheel on the straddled legs and the arms overhead, the hands planted in turn`,`whole-body pitch and roll take full turns in any key (no range); past 60 deg the knee and elbow poles and the feet turn with the body`],example:{f0:10,args:{kind:`back`}}},smokeCigarette:{args:{until:Sp(`hold to this frame; default the last frame; under f0+264 the beats are paced to fit`),kit:{type:`object`,properties:{cig:X(`the cigarette kit's rootId (object.createKit)`),lighter:X(`the lighter kit's rootId`),exhale:X(`the exhale kit's rootId (or an effect): it follows the mouth`)},required:[`cig`,`lighter`],additionalProperties:!1,description:`kits the skill carries and lights: it adds their spec.objects entries itself`}},needs:`a stand or squat keyed before f0`,frames:`265 nominal; fits 199..264 by pacing`,adds:[`anchors lpocket, rpocket, hold, mouth, flame`,`props cig, lighter`,`channels cig_vis, lit_vis, flame_vis, ember_vis, exhale, ember`,`checks contacts, orient, below, appear, mouthPoints`],example:{f0:0,args:{}}},hitReaction:{args:{dir:Ap(`[x, z] the way the body is thrown`),from:X(`or what hits it: object:<id> | person:<id> (thrown away from it)`),speed:Y(`launch speed, m/s (a shove 2, a car at 30 km/h about 8)`,{default:4,exclusiveMinimum:0}),land:{enum:[`back`,`front`],description:`default: back when hit from the front`}},needs:`a pose; dir or from`,frames:`~40`,adds:[`keys: thrown to rest`],example:{f0:10,args:{dir:[0,-1],speed:3}}},throwObject:{args:{object:X(`the carried object's id (objects.<id>.prop on the hand's prop)`),side:wp(`the throwing hand (default r)`),to:{type:`array`,items:{type:`number`},minItems:3,maxItems:3,description:`[x, y, z] world m: where it lands`},arc:{enum:[`low`,`high`],description:`launch angle: low 20 deg, high 50 deg (default low)`}},required:[`object`,`to`],needs:`the object in that hand (its objects entry on the hand's prop)`,frames:`24 (wind-up, swing, release at f0+18, follow-through)`,adds:[`keys: wind-up, swing, follow-through`,`objects.<id>: release and fall { v: "hand" } when the entry leaves them out`,`goals: <id> lands at the target`],example:{f0:40,args:{object:`ball`,to:[0,0,5]}}},catchObject:{args:{object:X(`the flying object's id (objects.<id>.fall { at, v })`),side:{enum:[`r`,`both`],description:`the right hand, or both (a thing over 1 kg takes both)`}},required:[`object`],needs:`the object flying: objects.<id>.fall { at, v }`,frames:`to the catch (the first frame in reach, at least 10 from f0) + 14`,adds:[`keys: reach, close, give with it`,`objects.<id>: prop, grab, grasp when the entry leaves them out`,`props.<id>Catch`],example:{f0:0,args:{object:`ball`}}}},Pp={_l:`_r`,_r:`_l`},Fp=/^(yaw|roll|spine_yaw|spine_roll|head_yaw|head_roll|fyaw_[lr]|wtwist_[lr]|wflex_[lr])$/,Ip=/^(root|foot_[lr]|hand_[lr])$/,Lp=/^(knee|elbow)_[lr]$/,Rp=/^(yaw|fyaw_[lr])$/,zp=e=>e&&typeof e==`object`?structuredClone(e):e,Bp=e=>e.replace(/_(l|r)$/,e=>Pp[e]).replace(/^(Left|Right)/,e=>e===`Left`?`Right`:`Left`),Vp=()=>({origin:[0,0,0],yaw:0,mirror:!1,negate:new Set,names:{}}),Hp=(t,n)=>e(t.origin,p(_(t.yaw),t.mirror?[-n[0],n[1],n[2]]:n)),Up=(e,t)=>e.mirror?[-t[0],t[1],t[2]]:t.slice();function Wp(e,t){if(!e.mirror)return t;let n=/^w_(.+)_([lr])$/.exec(t);return n?`w_${e.names[n[1]]??n[1]}_${n[2]===`l`?`r`:`l`}`:/_[lr]$/.test(t)?Bp(t):t}function Gp(e,t){let n={};for(let[r,i]of Object.entries(t)){let t=zp(i);Ip.test(r)?t=Hp(e,t):Lp.test(r)?t=Up(e,t):typeof t==`number`&&(e.mirror&&(Fp.test(r)||e.negate.has(r))&&(t=-t),Rp.test(r)&&(t+=e.yaw)),n[Wp(e,r)]=t}return n}function Kp(e,t=0,n=Vp()){let r=[],i=e=>Wp(n,e);return{state:e,keys:r,frame:n,at(i,a){let o=Gp(n,a);Object.assign(e,zp(o)),r.push({f:t+i,set:o})},hold(n,...a){r.push({f:t+n,set:Object.fromEntries(a.filter(t=>e[i(t)]!==void 0).map(t=>[i(t),zp(e[i(t)])]))})},step(n,a,o){let s=i(a);r.push({f:t+n-1,set:{[s]:e[s]}}),e[s]=o,r.push({f:t+n,set:{[s]:o},step:[s]})}}}let Q=(e,t)=>{throw Error(`MOTION_SPEC_INVALID: ${e}: ${t}`)};function qp(e,t,n){let r=e.scene?.objects?.[n];return r||Q(e.tag,`args.${t} "${n}" is not a scene object`),r}let Jp=e=>Math.atan2(e[0][2],e[2][2])*180/Math.PI,Yp=.01,Xp=[`Left`,`Right`].flatMap(e=>[[`${e}UpLeg`,`${e}Leg`,.07],[`${e}Leg`,`${e}Foot`,.05],[`${e}Foot`,`${e}ToeBase`,.045],[`${e}ToeBase`,`${e}Toe_End`,.04]]).concat([[`LeftUpLeg`,`RightUpLeg`,.095],[`Hips`,`Spine2`,.12]]),Zp=[[0,-1],[1,0],[-1,0],[Math.SQRT1_2,-Math.SQRT1_2],[-Math.SQRT1_2,-Math.SQRT1_2]];function Qp(n,r,i){let a=n.index,o=n.restWorldPos,s=o[a.Hips],c=_(i),l=t=>e([r[0],0,r[1]],p(c,[t[0]-s[0],t[1],t[2]-s[2]])),u=(n,r,i)=>[0,.25,.5,.75,1].map(a=>[e(n,t(r,n).map(e=>e*a)),i]),d=[`Left`,`Right`].flatMap(e=>{let t=o[a[`${e}Arm`]],n=Math.sign(t[0]-s[0])*.04;return u(l(t),l([t[0]+n,t[1]-.62,t[2]]),.045)});return Xp.filter(([e,t])=>a[e]!==void 0&&a[t]!==void 0).flatMap(([e,t,n])=>u(l(o[a[e]]),l(o[a[t]]),n)).concat(d)}let $p=e=>Object.values(e?.objects??{}).filter(e=>e.half&&e.pos&&e.R&&e.pos[1]+2*e.half[1]*e.R[1][1]>.12);function em(e,t,n,r){let i=1/0;for(let[a,o]of Qp(e,n,r))for(let e of t)i=Math.min(i,B(a,e)-o-.01);return i}function tm(e,t,n,r){let i=$p(t);if(!i.length||em(e,i,n,r)>=0)return null;let a=_(r);for(let t=Yp;t<=.600000001;t+=Yp)for(let[o,s]of Zp){let c=p(a,[o*t,0,s*t]),l=[n[0]+c[0],n[1]+c[2]];if(em(e,i,l,r)>=0)return{at:l.map(e=>+e.toFixed(4)),moved:+t.toFixed(2)}}return null}function nm(t,n){if(n?.kind===`world`&&Array.isArray(n.p))return n.p;if(n?.kind!==`object`||!t)return null;let r=t.objects?.[n.object]??t.groups?.[n.object];if(!r)return null;if(!n.grasp||n.local)return e(r.pos,p(r.R,n.local??[0,0,0]));let i=U(t,n.object).map(e=>t.objects[e]).filter(e=>e?.half).map(t=>e(t.pos,p(t.R,[0,t.half[1],0])));return i.reduce((t,n)=>e(t,n.map(e=>e/i.length)),[0,0,0])}function rm(e){let{sk:n,scene:r,use:i=[],index:o=-1}=e,s=i.slice(o+1).find(e=>e?.skill===`reachGrip`),c=nm(r,s&&(s.args?.define??e.anchors?.[s.args?.anchor]));if(!c)return null;let l=n.index,u=n.restWorldPos,d=s.args.side===`l`?`Left`:`Right`,f=u[l.Hips],m=u[l[`${d}Arm`]],h=a(t(u[l[`${d}ForeArm`]],m))+a(t(u[l[`${d}Hand`]],u[l[`${d}ForeArm`]]))+a(t(u[l[`${d}HandMiddle2`]],u[l[`${d}Hand`]])),g=m[1]-c[1],v=Math.max(.15,Math.sqrt(Math.max(0,(.8*h)**2-g*g))),y=e.args.heading??e.state?.yaw??0,b=_(y),x=$p(r),S=p(b,[m[0]-f[0],0,m[2]-f[2]+v]),C=[c[0]-S[0],c[2]-S[2]],w=0,T=C;for(;w<=.600000001&&em(n,x,T,y)<0;w+=Yp){let e=p(b,[0,0,-(w+Yp)]);T=[C[0]+e[0],C[1]+e[2]]}return{at:T.map(e=>+e.toFixed(4)),heading:y,anchor:s.args.anchor,moved:+w.toFixed(2)}}let im=.75,am=.08,om=[.15,.3,.45,.6,.7,.8,.9],sm=e=>e*e*e*(10-15*e+6*e*e),cm=.9,lm=.7,um=(e,t)=>{let n=Math.min(1,Math.max(0,e));return n<lm?cm*sm((n/lm)**t):cm+.09999999999999998*sm((n-lm)/.30000000000000004)},dm=e=>[...new Map(om.map(t=>Math.round(t*e)).filter(t=>t>0&&t<e).map(t=>[t,[um(t/e,.65),um(t/e,.75)]])).entries()].map(([e,[t,n]])=>[e,t,n]),fm=180/Math.PI,pm=(e,t,n)=>Math.max(t,Math.min(n,e));function mm(e,t){let n=hm(e,t),r=e.sk.index;return!!n&&n[1]>=e.sk.restWorldPos[r.LeftLeg][1]+.1}function hm(t,n){if(n?.kind===`world`&&Array.isArray(n.p))return n.p.map(Number);let r=n?.kind===`object`?t.scene?.objects?.[n.object]:null;return!r||[n.object,...r.ancestors??[]].some(e=>gm(t,e))?null:e(r.pos,p(r.R,n.local??[0,r.half?.[1]??0,0]))}function gm(e,t){let n=e.objects?.[t];return!n||typeof n!=`object`?!1:n.follow!==void 0||n.hinge!==void 0||Math.min(...(n.keys??[]).map(e=>e.f).filter(Number.isFinite),n.fall?.at??1/0)<(e.until??1/0)}let _m=new WeakMap;function vm(e){if(!_m.has(e)){let n=D(e),r=e.index;de(e,n,`Right`,{}),_m.set(e,a(t(n.P[r.RightHandThumb4],n.P[r.RightHandIndex4])))}return _m.get(e)}function ym(e,t){let n=e.scene?.objects??{};if(t?.kind===`object`)return n[t.object]?.half?n[t.object]:null;if(t?.kind!==`world`||!Array.isArray(t.p))return null;let r=t.p.map(Number);return Object.values(n).filter(e=>e.half&&B(r,e)<=.01).sort((e,t)=>B(r,e)-B(r,t))[0]??null}function bm(e,n,r,i){let a=r[0]-n[0],o=r[2]-n[2],s=Math.hypot(a,o);if(s<1e-6)return!1;let c=[n[0]+a/s*am,n[1],n[2]+o/s*am];return Object.entries(e.scene?.objects??{}).some(([e,n])=>{if(!n.half||i.has(e)||i.has(n.parent))return!1;let r=t(c,[0,.12,0]);return B(c,n)>0&&B(r,n)<=0})}function xm(t,n,r){let i=ym(t,n),a=hm(t,n)??(i&&e(i.pos,p(i.R,[0,i.half[1],0])));if(!i||!a||2*Math.min(...i.half)+.025>vm(t.sk))return!1;let o=new Set(Object.entries(t.scene.objects).filter(([,e])=>e===i).map(([e])=>e));return!(Array.isArray(r.root)&&bm(t,a,r.root,o))}function Sm(e,t,n){let r=hm(e,t),i=n.root;if(!r||!Array.isArray(i))return null;let a=e.sk.restWorldPos,o=e.sk.index,s=[i[0],i[1]+a[o.Head][1]-a[o.Hips][1],i[2]],c=[r[0]-s[0],r[1]-s[1],r[2]-s[2]],l=Math.hypot(c[0],c[2]);if(l<.05)return null;let u=Math.atan2(c[0],c[2])*fm-(n.yaw??0)-(n.spine_yaw??0);for(;u>180;)u-=360;for(;u<-180;)u+=360;let d=Math.atan2(-c[1],l)*fm-(n.pitch??0)-(n.spine_pitch??0);return{head_yaw:pm(im*u,-60,60),head_pitch:pm(im*d,-30,50)}}let Cm=(e,t,n)=>e.some(e=>e.f>=t&&e.f<=n&&(e.set?.head_yaw!==void 0||e.set?.head_pitch!==void 0)),wm=(e,t)=>sn[e]?sn[e].map((e,n)=>n===2&&t===`r`?-e:e):[0,0,0];function Tm(t){let{sk:n,args:r,state:i}=t,a=n.index,o=n.restWorldPos,s=o[a.LeftFoot][1],c=r.heading??i.yaw??0,l=r.at??(i.root?[i.root[0],i.root[2]]:null),u=null;if(!l){let e=rm(t);e||Q(t.tag,`needs args.at: [x, z] (no root is keyed before f0), or a reachGrip after it to stand within reach of`),{at:l,heading:c}=e,u={at:l,heading:c,why:`within reach of anchor ${e.anchor}`}}else if(r.at&&t.place!==!1){let e=tm(n,t.scene,[l[0],l[l.length-1]],c);e&&(l=e.at,u={at:l,heading:c,why:`moved ${e.moved} m out of a scene body`})}let d=[l[0],0,l[l.length-1]],f=_(c),m=t=>{let n=e(d,p(f,t));return n[1]=s,n},h={root:[d[0],o[a.Hips][1]-.01,d[2]],yaw:c,pitch:0,roll:0,spine_yaw:0,spine_pitch:0,spine_roll:0,head_yaw:0,head_pitch:0,foot_l:m([.09,0,-.03]),foot_r:m([-.09,0,.03]),fyaw_l:c,fyaw_r:c,fpitch_l:0,fpitch_r:0,knee_l:[.05,0,1],knee_r:[-.05,0,1],armw_l:0,armw_r:0,aswing_l:0,aswing_r:0,abend_l:0,abend_r:0,arm_hang:1},g=[{f:t.f0,set:h}];return r.until!==void 0&&g.push({f:r.until,set:structuredClone(h)}),{keys:g,end:r.until??t.f0,...u?{placed:u}:{}}}function Em(e){let{args:t,state:n}=e;(!n.foot_l||!n.foot_r||!n.root)&&Q(e.tag,`needs a standing pose keyed before f0 (use standIdle or another skill first)`);let r={type:`walk`,f0:e.f0,...t};delete r.to,delete r.arrive;let i=n.yaw??0,a=t.step??.5,o=t.firstStep??.4;if(t.to){let e=[(n.foot_l[0]+n.foot_r[0])/2,(n.foot_l[2]+n.foot_r[2])/2],s=t.to[0]-e[0],c=t.to[t.to.length-1]-e[1],l=Math.hypot(s,c),u=Math.atan2(s,c)*180/Math.PI;for(;u-i>180;)u-=360;for(;u-i<-180;)u+=360;r.heading=t.heading??[i,u],r.steps=t.steps??Math.max(2,2+Math.round((l-o)/a))}typeof r.heading==`number`&&(r.heading=[i,r.heading]);let s=Ec(r,e.keys,e.sk,e.tag),c=kp(r),l=e.frames-1;return c>l&&Math.max(...s.filter(e=>e.f<c).map(e=>e.f))<=l?{keys:s.filter(e=>e.f<=l),end:l,cut:`settle cut at f${l} (hold to the end)`}:{keys:s,end:c}}function Dm(e={},t,n,r){let i=t=>e[t]&&JSON.stringify(e[t])!==JSON.stringify(r);if(!i(t))return t;let a=`${t}_${n}`;for(let e=2;i(a);e++)a=`${t}_${n}${e}`;return a}let Om=(e,t)=>{let n=e?.objects??{};if(!t||n[t]||t.startsWith(`person:`))return t?[t]:[];let r=Object.keys(n).filter(e=>n[e].ancestors?.includes(t));return r.length?r:[t]};function km(e){let{args:t,state:n}=e,r=t.side,i=t.anchor;r!==`l`&&r!==`r`&&Q(e.tag,`args.side must be "l" or "r", got ${JSON.stringify(r)}`),(typeof i!=`string`||/_[lr]$/.test(i))&&Q(e.tag,`args.anchor must name an anchor (not ending in _l/_r)`);let a=t.reach??20,o=t.hold??22,s=t.release??12,c=(t.define??e.anchors?.[i])?.grasp,l=t.fingers??on[c]??`grip`,u=`w_${i}_${r}`,d=`fingers_${r}`,f=`armw_${r}`,p=`elbow_${r}`,m=!(n[f]>.5),h=e.frames-1,g=e.f0+a<=h&&e.f0+a+o+s>h,_=Math.min(a+o,h-e.f0);for(let[e,t]of[[u,0],[d,{[l]:0}],[f,0]])n[e]===void 0&&(n[e]=t);let v=a+o+s,y={...e,until:e.f0+a},b=!Cm(e.keys??[],e.f0,e.f0+v)&&Sm(y,t.define??e.anchors?.[i],n),x=b?{head_yaw:n.head_yaw??0,head_pitch:n.head_pitch??0}:null,S=Kp(n,e.f0);S.hold(0,u,d,f,p,...b?[`head_yaw`,`head_pitch`]:[]);let C=t.define??e.anchors?.[i],w=n[u],T=n[f],E=(e,t,n)=>e+(t-e)*n;if(mm(y,C))for(let[e,t,n]of dm(a))S.at(e,{[u]:E(w,1,t),[f]:E(T,1,n)});let D=Math.round(.4*a);D>0&&D<a&&xm(y,C,n)&&S.hold(D,d),b&&S.at(Math.max(1,Math.round(.3*a)),b),S.at(a,{[u]:1,[d]:{...n[d],[l]:1},[f]:1,...t.elbow?{[p]:t.elbow}:{}}),S.hold(_,u,d,f,p,...b?[`head_yaw`,`head_pitch`]:[]),g||S.at(v,{[u]:0,[d]:{...n[d],[l]:0},...m?{[f]:0}:{},...x??{}});let O=r===`l`?`Left`:`Right`,k={hand:r,origin:[`${O}HandIndex2`,`${O}HandMiddle2`],points:{fingers:wm(c,r)}},A=Dm(e.props,`${i}Grip`,r,k),j=t.define?{[i]:t.define}:{},M=t.define??e.anchors?.[i],N=M?.kind===`object`?M.object:M?.kind===`person`?`person:${M.person}`:null;return{keys:S.keys,end:g?h:e.f0+a+o+s,...g?{cut:`${_<a+o?`hold`:`release`} cut at f${h} (hold to the end)`}:{},anchors:j,props:{[A]:k},fits:[{side:r,anchor:i,effector:{prop:A,point:`fingers`},...t.orient===!1?{orient:!1}:{}}],checks:{contacts:[{name:`fingers on ${i}`,point:`prop:${A}.fingers`,target:`anchor:${i}`,tol:.04,when:u}],allow:Om(e.scene,N).map(e=>({part:`hand`,object:e,depth:.04}))}}}let Am=[.54,.4,-.075],jm=.4,Mm=new Set([`l`,`r`]);function Nm(e,t=`l`){let n=e.car;return{seat:e.seat??(n&&`${n}_Seat_Driver`),seatBack:e.seatBack??(n&&`${n}_Seat_Driver_Back`),door:e.door??(n&&`${n}_Door_${t.toUpperCase()}`),handle:e.handle??(n&&`${n}_Door_Handle`),wheel:e.wheel??(n&&`${n}_Steering_Wheel`)}}function Pm(e,n,r){let i=qp(e,`seat`,n);Math.abs(i.pos[1]-jm)>.05&&Q(e.tag,`seat "${n}" stands at y ${i.pos[1]}; the car skills are tuned for a seat base at y ${jm} (a car on the ground)`);let a=Vp();a.yaw=Jp(i.R),a.mirror=r===`r`;let o=t(i.pos,p(_(a.yaw),a.mirror?[-Am[0],Am[1],Am[2]]:Am));return a.origin=[o[0],0,o[2]],a.names={lthigh:`rthigh`,rthigh:`lthigh`},a}let Fm=[[`hand`,`thigh*`,.07],[`farm`,`thigh*`,.07],[`hand`,`hips`,.07],[`farm`,`hips`,.07],[`hand_l`,`hand_r`,.035]].map(([e,t,n])=>({part:e,object:t,depth:n})),Im=e=>({A:e.restWorldPos[e.index.LeftFoot][1],H:t=>t+(e.restWorldPos[e.index.Hips][1]-1.0427487182617188)});function Lm(t){let{args:n,sk:r}=t,i=Nm(n),a=qp(t,`seat`,i.seat),{A:o}=Im(r),s=Jp(a.R),c=e=>p(_(s),e),l=n.offset??[0,.17,.02],u=n.floor??a.pos[1],d=e(e(a.pos,p(a.R,[0,2*a.half[1],0])),c(l)),f=t=>{let n=e(d,c([t,0,.452]));return n[1]=u+o,n},m=t=>e(d,c([t,.348,.324])),h={root:d,yaw:s,pitch:0,spine_pitch:-12,spine_yaw:0,spine_roll:0,head_yaw:0,head_pitch:10,foot_l:f(.17),foot_r:f(-.17),fyaw_l:s,fyaw_r:s,fpitch_l:0,fpitch_r:0,knee_l:[.15,.2,1],knee_r:[-.15,.2,1],armw_l:1,armw_r:1,hand_l:m(.22),hand_r:m(-.22),elbow_l:[.6,-1,-.3],elbow_r:[-.6,-1,-.3],aswing_l:0,aswing_r:0,abend_l:0,abend_r:0,wtwist_l:0,wtwist_r:0,wflex_l:0,wflex_r:0},g=[{part:`torso_lo`,object:i.seat,depth:.04},{part:`hips`,object:i.seat,depth:.04},{part:`thigh`,object:i.seat,depth:.045}];return i.seatBack&&t.scene.objects[i.seatBack]&&g.push({part:`torso`,object:i.seatBack,depth:.03}),i.wheel&&t.scene.objects[i.wheel]&&g.push({part:`hand`,object:i.wheel,depth:.05}),{keys:[{f:t.f0,set:h}],checks:{supports:[{object:i.seat,part:`hips`,tol:.04}],allow:g},end:t.f0}}function Rm(t){let n=t.args.side??`l`;Mm.has(n)||Q(t.tag,`args.side must be "l" or "r", got ${JSON.stringify(n)}`);let r=Nm(t.args,n),i=Pm(t,r.seat,n),a=t.args.channel??`door`;for(let[e,n]of[[`door`,r.door],[`handle`,r.handle]])qp(t,e,n);i.negate.add(a);let o={[a]:0,w_handle_l:0,w_lthigh_l:0,w_rthigh_r:0,fingers_l:{grip:0,fist:0}},s=Kp(t.state,t.f0,i),c={};for(let[e,n]of Object.entries(o))t.state[Wp(i,e)]===void 0&&(c[e]=n);Object.keys(c).length&&s.at(0,c),zm(s,a,Im(t.sk));let l=t.scene.objects[r.door],u=(i.mirror?-1:1)*l.half[0],d={pivot:e(l.pos,p(l.R,[u,0,l.half[2]])),axis:[0,1,0],channel:a},f=e=>i.mirror?Bp(e):e,m=e=>i.mirror?{lthigh:`rthigh`,rthigh:`lthigh`}[e]??e:e,h=(e,t)=>({kind:`segment`,from:f(`${e}UpLeg`),to:f(`${e}Leg`),t:.45,tMin:.15,offset:[0,.07,0],reach:i.mirror?t===`l`?`r`:`l`:t}),g=i.mirror?`r`:`l`;return{keys:s.keys,objects:{[r.door]:d,[r.handle]:d},anchors:{handle:{kind:`object`,object:r.handle,local:Up(i,[-.005,.015,0])},[m(`lthigh`)]:h(`Left`,`l`),[m(`rthigh`)]:h(`Right`,`r`)},props:{grip:{hand:g,origin:[f(`LeftHandIndex2`),f(`LeftHandMiddle2`)],points:{fingers:[0,0,0]}}},fits:[{side:g,anchor:`handle`,effector:{prop:`grip`,point:`fingers`}}],checks:{contacts:[{name:`fingers on the door handle`,point:`prop:grip.fingers`,target:`anchor:handle`,tol:.04,when:`w_handle_${g}`}],allow:[{part:`hand`,object:r.door,depth:.03},{part:`hand`,object:r.handle,depth:.04},...Fm]},end:t.f0+246}}function zm(e,t,{A:n,H:r}){e.hold(12,`w_handle_l`,`fingers_l`,`head_yaw`,`spine_yaw`,`spine_roll`,`elbow_l`,`hand_l`),e.at(32,{w_handle_l:1,fingers_l:{grip:1,fist:0},head_yaw:35,spine_yaw:10,spine_roll:-8,elbow_l:[.3,-1,-.6],hand_l:[1,.96,.06]}),e.hold(32,t),e.at(38,{[t]:-3}),e.at(50,{[t]:-10,spine_roll:-16,spine_yaw:14}),e.hold(54,`w_handle_l`,`fingers_l`,`w_lthigh_l`,`elbow_l`,t),e.at(66,{w_handle_l:0,fingers_l:{grip:0,fist:0},[t]:-10}),e.at(70,{w_lthigh_l:1,elbow_l:[.5,-.3,-1],spine_roll:-3,spine_yaw:6}),e.at(84,{[t]:-68,head_yaw:40}),e.hold(64,`w_rthigh_r`,`elbow_r`,`spine_pitch`),e.at(80,{w_rthigh_r:1,elbow_r:[-.5,-.3,-1],spine_roll:0,spine_yaw:0,spine_pitch:0}),e.hold(84,`yaw`,`root`,`foot_l`,`foot_r`,`fyaw_l`,`fyaw_r`,`knee_l`,`knee_r`),e.at(92,{foot_r:[.4,jm+n+.03,.36],fyaw_r:15}),e.at(97,{foot_l:[.96,.62,.32],fyaw_l:40,knee_l:[.5,.4,1],yaw:30,root:[.64,.915,-.06]}),e.at(101,{foot_r:[.56,jm+n,.34],fyaw_r:35}),e.at(105,{foot_l:[1.18,.5,.1],fyaw_l:60}),e.at(116,{foot_l:[1.28,n,-.12],fyaw_l:75,knee_l:[.4,.5,1],yaw:62,root:[.82,.9,-.06],head_yaw:20,knee_r:[.1,.6,1],spine_pitch:10}),e.hold(120,`foot_r`,`fyaw_r`),e.at(129,{foot_r:[.94,.64,.36],fyaw_r:65,knee_r:[0,.5,1],yaw:80,root:[.86,.9,-.03]}),e.at(135,{foot_r:[1.18,.5,.26],fyaw_r:85}),e.hold(142,`foot_l`,`fyaw_l`),e.at(144,{foot_r:[1.29,n,.22],fyaw_r:90,knee_r:[-.1,.4,1],yaw:90,root:[.88,.9,.02],head_yaw:0,knee_l:[.1,.4,1],spine_pitch:16}),e.hold(144,`head_pitch`,`pitch`,`elbow_l`,`elbow_r`),e.at(152,{spine_pitch:28,head_pitch:-8,root:[.91,.9,.02],elbow_l:[.6,-.2,-1],elbow_r:[-.6,-.2,-1]}),e.at(162,{spine_pitch:50,pitch:22,head_pitch:-36,root:[.99,.91,.02]}),e.at(174,{root:[1.09,.96,.02],spine_pitch:36,pitch:12,head_pitch:-24}),e.hold(166,`armw_l`,`armw_r`),e.at(186,{root:[1.19,r(1),.02],spine_pitch:12,pitch:3,head_pitch:-8,armw_l:0,armw_r:0}),e.hold(187,`w_lthigh_l`,`w_rthigh_r`),e.at(190,{w_lthigh_l:0,w_rthigh_r:0}),e.at(196,{root:[1.21,r(1.005),.02],spine_pitch:0,pitch:0,head_pitch:0}),e.at(205,{root:[1.26,r(.985),.165]}),e.hold(208,`foot_l`,`fyaw_l`),e.at(212,{root:[1.3,r(.985),.16]}),e.at(214,{foot_l:[1.4,n+.06,-.12]}),e.at(220,{foot_l:[1.535,n,-.132],fyaw_l:110,root:[1.38,r(.98),.05]}),e.at(230,{root:[1.5,r(.975),-.08]}),e.hold(232,`foot_r`,`fyaw_r`),e.at(236,{root:[1.52,r(.975),-.085]}),e.at(239,{foot_r:[1.53,n+.07,.09]}),e.at(246,{foot_r:[1.765,n,-.048],fyaw_r:125,yaw:110,root:[1.62,r(.98),-.08]})}let Bm=[`cig`,`lighter`,`exhale`],Vm={follow:`landmark:mouth`,aim:`face`,visible:`exhale`},Hm={cig:`cigarette`,lighter:`lighter`},Um=[-.05,.07,.1],Wm=.88,Gm=[.245,.08,.02],Km={l:[.13,-.035,.08],r:[-.13,-.035,.08]};function qm(e,t){return Object.entries(e?.objects??{}).map(([e,t])=>[e,t,!0]).concat(Object.entries(e?.others??{}).map(([e,t])=>[e,t,!1])).filter(([,e])=>e.ancestors?.includes(t))}function Jm(e,t,n){let r=t[n];return(typeof r!=`string`||!En(e.scene)[r])&&Q(e.tag,`args.kit.${n} ${JSON.stringify(r)} is not a group in the scene; object.createKit { kit: "${Hm[n]}" } answers its rootId`),r}let Ym={cig:.03,lighter:.045},Xm=.07,Zm=.04;function Qm(e,t){let n=e.args.kit;if(n===void 0)return null;(!n||typeof n!=`object`||Array.isArray(n))&&Q(e.tag,`args.kit must be { cig, lighter, exhale? } (kit root ids), got ${JSON.stringify(n)}`);for(let t of Object.keys(n))Bm.includes(t)||Q(e.tag,`args.kit has unknown field "${t}"; fields: ${Bm.join(`, `)}`);let r=Jm(e,n,`cig`),i=Jm(e,n,`lighter`),a={[r]:{prop:`cig`,grab:t.cig,at:[0,0,0],along:[0,0,1],visible:`cig_vis`},[i]:{prop:`lighter`,grab:t.lit,release:t.unlit,at:[0,0,0],along:[0,1,0],visible:`lit_vis`}};for(let[t,n,i]of qm(e.scene,r))i&&n.glow||!i&&n.renderer===`light`?a[t]={visible:`ember_vis`,level:`ember`}:!i&&n.renderer===`effect`&&(a[t]={visible:`ember_vis`});for(let[t,n,r]of qm(e.scene,i))(r&&n.glow||!r&&n.renderer===`light`)&&(a[t]={visible:`flame_vis`});n.exhale!==void 0&&((typeof n.exhale!=`string`||!(e.scene?.others?.[n.exhale]||e.scene?.objects?.[n.exhale]||En(e.scene)[n.exhale]))&&Q(e.tag,`args.kit.exhale ${JSON.stringify(n.exhale)} is not a scene object; object.createKit { kit: "exhale" } answers its rootId (it follows the mouth)`),a[n.exhale]={...Vm});let o=e=>t.squat?Xm:e;return{objects:a,allow:[[`thigh_r`,r,o(Ym.cig)],[`thigh_l`,i,o(Ym.lighter)],[`hips`,r,o(Ym.cig)],[`hips`,i,o(Ym.lighter)],...t.squat?[[`shin`,r,Xm],[`shin`,i,Xm],[`hand_l`,`thigh_l`,Zm],[`hand_r`,`thigh_r`,Zm],[`hand`,`hips`,Zm]]:[]].map(([e,t,n])=>({part:e,object:t,depth:n}))}}function $m(r){let{sk:i,state:o}=r,s=i.index,c=i.restWorldPos;if(!(o.root[1]<.7*c[s.Hips][1]))return null;let{s:l}=cs(i,o),u=o.yaw??0,d=l.P[s.RightArm],f=e=>p(_(-u),t(e,o.root)),m=a(t(c[s.RightForeArm],c[s.RightArm]))+a(t(c[s.RightHand],c[s.RightForeArm])),h=e(l.P[s.RightLeg],p(_(u),Um)),g=a(t(h,d)),v=f(g>Wm*m?e(d,n(t(h,d),Wm*m/g)):h),y=e(f(A(l,s.Head,i.landmarks.mouth)),[0,0,.2]),b=Ke(i),x=(t,n)=>{let r=e(o.root,p(_(u),[n*Gm[0],Gm[1],Gm[2]]));return f(b?We(b.body,i,l,r,Ge,b.skip):r)},S=e=>e.map(e=>Math.round(e*1e3)/1e3);return{hold:S(v),mouthOut:S(y),lpocket:S(x(`Left`,1)),rpocket:S(x(`Right`,-1))}}let eh={TW_M:40,FX_M:0,TW_H:0,FX_H:10,TW_L:-80,FX_L:-50},th=[[`hand_l`,`hand_r`,.045],[`hand_r`,`hand_l`,.045],[`farm_r`,`hand_l`,.02],[`hand_l`,`farm_r`,.02]].map(([e,t,n])=>({part:e,object:t,depth:n})),nh=[.4,-1,.6],rh=[-.4,-1,-.3],ih=[.9,-.6,.3],ah=[`armw_r`,`armw_l`,`w_rpocket_r`,`w_lpocket_l`,`w_hold_r`,`w_mouth_r`,`w_flame_l`,`elbow_r`,`elbow_l`,`wtwist_r`,`wflex_r`,`wtwist_l`,`wflex_l`,`head_pitch`,`head_yaw`,`spine_pitch`,`root`,`fingers_l`,`fingers_r`,`cig_vis`,`lit_vis`,`flame_vis`,`ember_vis`,`exhale`,`ember`],oh=e=>(e/24).toFixed(1),sh={armw_r:0,armw_l:0,w_rpocket_r:0,w_lpocket_l:0,w_hold_r:0,w_mouth_r:0,w_flame_l:0,elbow_r:[-.5,-.4,-1],elbow_l:[.5,-.4,-1],wtwist_r:0,wflex_r:0,wtwist_l:0,wflex_l:0,head_pitch:0,head_yaw:0,spine_pitch:0,fingers_l:{grip:0,fist:0},fingers_r:{cigarette:0},cig_vis:0,lit_vis:0,flame_vis:0,ember_vis:0,exhale:0,ember:0};function ch(e){let t=e.args.until??(e.auto?e.f0+264:e.frames-1),n=t-e.f0;n<198&&Q(e.tag,`needs frames f${e.f0}..f${e.f0+198} at least: 199 frames, ${oh(199)} s at 24 fps, paced 0.75 (265 frames, ${oh(265)} s, at its own pace); it has f${e.f0}..f${t}, `+(e.args.until===void 0?`so set frames >= ${e.f0+198+1} or frames: "auto"`:`so set args.until >= ${e.f0+198}`)),Array.isArray(e.state.root)||Q(e.tag,`needs a pose keyed before f0 (root): standing or squatting`);let r=Object.fromEntries(Object.entries(sh).filter(([t])=>e.state[t]===void 0));Object.keys(r).length&&Object.assign(e.state,structuredClone(r)),e.state.head_yaw=0;let i=e.state.head_pitch,a=(e.state.pitch??0)+e.state.spine_pitch,o=e=>e+i,s=a?p(g(a),nh):nh,c=a?p(g(a),rh):rh,l=$m(e),u=e.self?l?20:15:eh.FX_M;l&&(e.state.w_mouthOut_r=0);let d=n<264?n/264:1,f=e=>Math.round(e*d),m=Kp(e.state,e.f0),h=d===1?m:{...m,at:(e,t)=>m.at(f(e),t),hold:(e,...t)=>m.hold(f(e),...t),step:(e,t,n)=>m.step(f(e),t,n)},_=e.state.root.slice(),v=e.state.yaw??0,y=(e,t)=>{h.hold(e,`w_mouth_r`,`w_hold_r`,`wtwist_r`,`wflex_r`,`elbow_r`,`head_pitch`,`root`,...l?[`w_mouthOut_r`]:[]),l&&h.at(Math.round((e+t)/2),{w_hold_r:0,w_mouthOut_r:1,w_mouth_r:0}),h.at(t,{w_mouth_r:1,w_hold_r:0,...l?{w_mouthOut_r:0}:{},wtwist_r:eh.TW_M,wflex_r:u,elbow_r:s,head_pitch:o(6),root:_})},b=(e,t)=>{h.hold(e,`w_mouth_r`,`w_hold_r`,`wtwist_r`,`wflex_r`,`elbow_r`,`head_pitch`,...l?[`w_mouthOut_r`]:[]),l&&h.at(Math.round((e+t)/2),{w_mouth_r:0,w_mouthOut_r:1,w_hold_r:0}),h.at(t,{w_mouth_r:0,w_hold_r:1,...l?{w_mouthOut_r:0}:{},wtwist_r:eh.TW_H,wflex_r:eh.FX_H,elbow_r:c,head_pitch:o(0)})};h.hold(0,...ah),h.at(2,{w_rpocket_r:1,elbow_r:[-.5,-.4,-1]}),h.hold(6,`armw_r`,`w_rpocket_r`,`elbow_r`,`fingers_r`),h.at(20,{armw_r:1}),h.at(28,{fingers_r:{cigarette:1}}),h.step(28,`cig_vis`,1),h.hold(36,`w_rpocket_r`,`w_mouth_r`,`wtwist_r`,`wflex_r`,`elbow_r`,`head_pitch`),h.at(58,{w_rpocket_r:0,w_mouth_r:1,wtwist_r:eh.TW_M,wflex_r:u,elbow_r:s,head_pitch:o(6)}),h.at(2,{w_lpocket_l:1,elbow_l:[.5,-.4,-1]}),h.hold(12,`armw_l`,`w_lpocket_l`,`elbow_l`,`fingers_l`),h.at(26,{armw_l:1}),h.at(34,{fingers_l:{grip:0,fist:1}}),h.step(35,`lit_vis`,1),h.hold(44,`w_lpocket_l`,`w_flame_l`,`wtwist_l`,`wflex_l`,`elbow_l`),h.at(60,{w_lpocket_l:0,w_flame_l:1,wtwist_l:eh.TW_L,wflex_l:eh.FX_L,elbow_l:ih}),h.step(63,`flame_vis`,1),h.step(73,`ember_vis`,1),h.step(79,`flame_vis`,0),h.step(73,`ember`,1),h.at(80,{ember:2}),h.hold(90,`ember`),h.at(104,{ember:.6}),h.hold(194,`ember`),h.at(200,{ember:2}),h.hold(212,`ember`),h.at(224,{ember:.6}),h.step(116,`exhale`,1),h.step(138,`exhale`,0),h.step(238,`exhale`,1),h.step(260,`exhale`,0),h.hold(80,`w_flame_l`,`w_lpocket_l`,`wtwist_l`,`wflex_l`,`elbow_l`);let x=l&&e.self?8:0;h.at(x?96+x:l?100:96,{w_flame_l:0,w_lpocket_l:1,wtwist_l:0,wflex_l:0,elbow_l:[.5,-.4,-1]}),h.hold(101+x,`fingers_l`),h.step(102+x,`lit_vis`,0),h.hold(104+x,`armw_l`,`w_lpocket_l`),h.at(112,{fingers_l:{grip:0,fist:0}}),h.at(120,{armw_l:0,w_lpocket_l:1}),h.at(123,{w_lpocket_l:0}),b(92,114),h.at(124,{head_pitch:o(-14)}),h.at(140,{head_pitch:o(0)});let S=[Math.cos(v*Math.PI/180),0,-Math.sin(v*Math.PI/180)];h.hold(120,`root`,`head_yaw`),h.at(146,{root:_.map((e,t)=>e+.03*S[t]-(t===1?.005:0)),head_yaw:-22}),h.at(168,{head_yaw:0}),y(170,194),b(212,236),h.at(246,{head_pitch:o(-14)}),h.at(264,{head_pitch:o(0)}),m.hold(n,...ah,...l?[`w_mouthOut_r`]:[]);let C=!!l&&e.self,w=Qm(e,{cig:e.f0+f(28),lit:e.f0+f(35),unlit:e.f0+f(C?110:102),squat:C});return{keys:m.keys,end:t,...d<1?{paced:Math.floor(d*100)/100}:{},channels:[`exhale`,`ember`],...w?{objects:w.objects}:{},anchors:{lpocket:{kind:`body`,p:e.self&&l?.lpocket||Km.l},rpocket:{kind:`body`,p:e.self&&l?.rpocket||Km.r},hold:{kind:`body`,p:l?.hold??[-.24,.03,.06]},...l?{mouthOut:{kind:`body`,p:l.mouthOut}}:{},mouth:{kind:`landmark`,name:`mouth`},flame:{kind:`prop`,prop:`cig`,point:`tip`,offset:[0,-.02,0]}},props:{cig:{hand:`r`,origin:[`RightHandIndex2`,`RightHandMiddle2`],points:{filter:[0,0,-.026],tip:[0,0,.058]}},lighter:{hand:`l`,origin:[`LeftHand`,`LeftHandMiddle1`],points:{body:[.01,0,0],flame:[.01,.036,0]}}},fits:[{side:`r`,anchor:`mouth`,effector:{prop:`cig`,point:`filter`}},{side:`l`,anchor:`flame`,effector:{prop:`lighter`,point:`flame`}}],checks:{contacts:[{name:`filter on the lips`,point:`prop:cig.filter`,target:`anchor:mouth`,tol:.01,when:`w_mouth_r`},{name:`flame under the cigarette tip`,point:`prop:lighter.flame`,target:`anchor:flame`,tol:.01,when:`flame_vis`}],orient:[{name:`lit end points away from the face`,from:`prop:cig.filter`,to:`prop:cig.tip`,along:`face`,minCos:.64,when:`w_mouth_r`},{name:`lighter upright`,from:`prop:lighter.body`,to:`prop:lighter.flame`,along:`up`,minCos:.85,when:`flame_vis`}],below:[{name:`no chicken-wing elbow`,joint:`RightForeArm`,ref:`RightArm`,min:.08,when:`w_mouth_r`}],appear:[{name:`cigarette from the right pocket`,channel:`cig_vis`,joint:`RightHand`,anchor:`rpocket`,tol:.035},{name:`lighter from the left pocket`,channel:`lit_vis`,joint:`LeftHand`,anchor:`lpocket`,tol:.035}],mouthPoints:[`cig.filter`],allow:[...w?.allow??[],...th]}}}let lh=9.81/576,uh=e=>Math.min(1,Math.max(0,e)),$=e=>{let t=uh(e);return t*t*(3-2*t)},dh=(e,t,n)=>e+(t-e)*n,fh=(e,t,n)=>e.map((e,r)=>e+(t[r]-e)*n),ph=(e,t,n,r,i)=>$((e-t)/(n-t))-$((e-r)/(i-r)),mh={l:1,r:-1},hh=(e,t,n)=>f(_(e),f(g(t),v(n))),gh=(e,t,n,r,i)=>p(m(ss(e,t,n)?.R??_(e)),p(r,i));function _h(e,t,n,r,i,a){let o=ss(e.h,n,r);if(!o)return{fyaw:e.h,fpitch:a};let s=e.toe[t],c=p(i,p(g(a),s)),l=p(m(o.R),c),u=Math.atan2(-s[1],s[2]),d=180/Math.PI,f=Math.asin(Math.max(-1,Math.min(1,-l[1]/Math.hypot(...l))))-u;return{fyaw:e.h+Math.atan2(l[0],l[2])*d,fpitch:Math.max(-60,Math.min(90,f*d))}}function vh(e){let{sk:n,args:r,state:i}=e,a=n.index,s=n.restWorldPos;(!i.foot_l||!i.foot_r||!i.root)&&Q(e.tag,`needs a standing pose keyed before f0 (use standIdle or another skill first)`);let c=r.heading??i.yaw??0,l=_(c),u=s[a.Hips],d=o(s[a.LeftUpLeg],s[a.LeftLeg])+o(s[a.LeftLeg],s[a.LeftFoot]),f=o(s[a.LeftArm],s[a.LeftForeArm])+o(s[a.LeftForeArm],s[a.LeftHand]);return{h:c,R:l,F:p(l,[0,0,1]),L:p(l,[1,0,0]),ank:s[a.LeftFoot][1],y0:u[1]-.01,leg:d,arm:f,feet:{l:i.foot_l.slice(),r:i.foot_r.slice()},root:i.root.slice(),hipOff:{l:t(s[a.LeftUpLeg],u),r:t(s[a.RightUpLeg],u)},shoulder:{l:t(s[a.LeftArm],u),r:t(s[a.RightArm],u)},toe:{l:t(s[a.LeftToeBase],s[a.LeftFoot]),r:t(s[a.RightToeBase],s[a.RightFoot])}}}function yh(r,i,a){let{args:o}=r,s=(a===`back`?-1:1)*360,c=o.travel??(a===`back`?-.25:.4),l=i.y0+.05,u=i.y0-.02,d=Math.max(10,Math.round(Math.sqrt(.9/lh)+Math.sqrt(2*(l+.5-.05-u)/lh))),f=(u-l+lh*d*d/2)/d,h=f-lh*d,g=l-f*5/2,_=u+h*6/2,v=c/(5/2+d+6/2),y=r.f0,b=y+10,x=b+5,S=x+d,C=S+6,w=C+14,T=t=>e(i.feet[t],n(i.F,c)),E=t=>e(i.feet[t],p(i.R,i.toe[t])),D=t=>{let r,a;if(t<=b)r=dh(i.root[1],g,$((t-y)/10)),a=0;else if(t<=x){let e=t-b;r=g+f/5*e*e/2,a=v/5*e*e/2}else if(t<=S){let e=t-x;r=l+f*e-lh*e*e/2,a=v*5/2+v*e}else if(t<=C){let e=t-S;r=u+h*e-h/6*e*e/2,a=v*5/2+v*d+v*e-v/6*e*e/2}else r=dh(_,i.y0,$((t-C)/14)),a=c;return e([i.root[0],r,i.root[2]],n(i.F,a))},O=e=>s*$((e-x+2)/(d+5)),k=e=>hh(i.h,O(e),0),A=(e,n)=>p(m(k(e)),t(n,D(e))),j={l:A(x,rc(E(`l`),i.h,50,i.feet.l)),r:A(x,rc(E(`r`),i.h,50,i.feet.r))},M={l:A(S,T(`l`)),r:A(S,T(`r`))},N=[];for(let t=y;t<=w+1;t++){let r=t>w?0:O(t),a=hh(i.h,r,0),o=D(t),s=(t-x)/d,c=t>x&&t<S?ph(s,.12,.34,.6,.82):0,l={root:o,yaw:i.h,pitch:r,roll:0,armw_l:0,armw_r:0,fyaw_l:i.h,fyaw_r:i.h};for(let u of[`l`,`r`]){let d,f;if(t<=b)d=i.feet[u],f=0;else if(t<=x)f=50*$((t-b)/5),d=rc(E(u),i.h,f,i.feet[u]);else if(t<S){let t=1-$(s/.2),r=$((s-.72)/.28),l=e(i.hipOff[u],fh([0,-i.leg*.97,0],[mh[u]*.02,-.24,.16],c));d=e(o,p(a,e(e(n(j[u],t),n(M[u],r)),n(l,1-t-r)))),f=dh(dh(50,30,$(s/.2)),0,$((s-.7)/.3))}else d=T(u),f=0;let m=t>x&&t<S?_h(i,u,r,0,a,f):{fyaw:i.h,fpitch:f};Object.assign(l,{[`foot_${u}`]:d,[`fpitch_${u}`]:m.fpitch,[`fyaw_${u}`]:m.fyaw,[`knee_${u}`]:gh(i.h,r,0,a,[mh[u]*.05,.5*c,1])})}N.push({f:t,set:l})}let P=a===`back`?[-12,18]:[10,25];for(let[e,t,n,r,i]of[[y,0,0,0,0],[b,-45,15,20,0],[x,160,5,-8,P[0]],[Math.round(x+.3*d),55,85,32,P[1]],[Math.round(x+.62*d),55,85,32,P[1]],[S,75,25,22,0],[C,70,25,28,0],[w,0,0,0,0]])N.push({f:e,set:{aswing_l:t,aswing_r:t,abend_l:n,abend_r:n,spine_pitch:r,head_pitch:i}});return{keys:N,end:w+1}}function bh(r,i){let{args:a}=r,o=a.side===`r`?-1:1,s=-o,c=r.f0,l=c+12,u=l+34,d=u+14,f=o>0?`l`:`r`,h=f===`l`?`r`:`l`,g={foot:t=>e(i.hipOff[t],[mh[t]*.3,-i.leg*.93,0]),hand:t=>e(i.shoulder[t],[mh[t]*.32,i.arm*.88,0])},_={foot:i.ank,hand:.1},v=[[`foot`,f],[`hand`,f],[`hand`,h],[`foot`,h],[`foot`,f]],y=e=>e<=l?0:e<=u?s*360*$((e-l)/34):s*360,b=(t,n,r,a)=>e(n,p(hh(i.h,0,y(t)),g[r](a))),x=hh(i.h,0,0),S=t(i.feet[h],p(x,g.foot(h))),C=b(l,S,`foot`,f);C[1]=i.ank;let w=[],T={foot:{l:[],r:[]},hand:{l:[],r:[]}},E=0,D=i.feet[f].slice(),O=l;T.foot[h].push({a:c,b:l,p:i.feet[h]});for(let e=c;e<=d+1;e++){if(e<=l){w.push(fh(i.root,S,$((e-c)/12)));continue}e===l+1&&(D=C.slice());let n=t(D,p(hh(i.h,0,y(e)),g[v[E][0]](v[E][1]))),r=v[E+1];if(r&&e<=u){let a=b(e,n,...r);if(a[1]<=_[r[0]]){let o=b(e-1,w.at(-1),...r),s=o[1]-_[r[0]],c=a[1]-_[r[0]],l=fh(o,a,s>0?s/(s-c):1);T[v[E][0]][v[E][1]].push({a:O,b:e,p:D}),E++,O=e,D=[l[0],_[r[0]],l[2]],n=t(D,p(hh(i.h,0,y(e)),g[r[0]](r[1])))}}w.push(n)}T[v[E][0]][v[E][1]].push({a:O,b:d+1,p:D});let k=v[v.length-2];E>=v.length-1&&(T[k[0]][k[1]].at(-1).b=d+1);let A=w.map((t,r)=>{let i=[0,0,0],a=0;for(let t=-3;t<=3;t++){let o=Math.min(w.length-1,Math.max(0,r+t)),s=Math.exp(-(t*t)/4);i=e(i,n(w[o],s)),a+=s}return n(i,1/a)}),j=t=>t<=u?A[t-c]:e(A[u-c],[0,(i.y0-A[u-c][1])*$((t-u)/14),0]),M=(r,i,a)=>{let o=t(r,i),s=Math.hypot(...o);return s>a?e(i,n(o,a/s)):r},N=(n,r,a,o)=>{for(let s of T[n][r]){if(a>=s.a-6&&a<=s.b)return fh(o,s.p,$((a-s.a+6)/6));if(a>s.b&&a<=s.b+10){let o=hh(i.h,0,y(s.b)),c=p(m(o),t(s.p,j(s.b))),l=e(j(a),p(hh(i.h,0,y(a)),fh(c,g[n](r),$((a-s.b-1)/9))));return fh(s.p,l,$((a-s.b)/8))}}return o},P=(e,t,n)=>T[e][t].some(e=>n>=e.a&&n<=e.b),ee=Object.fromEntries([`l`,`r`].map(e=>{let t=P(`foot`,e,u)?T.foot[e].at(-1).p.slice():b(u,j(u),`foot`,e);return t[1]=i.ank,[e,t]})),F=[];for(let t=c;t<=d+1;t++){let n=t===d+1?0:y(t),r=hh(i.h,0,y(t)),a=j(t),o={root:a,yaw:i.h,pitch:0,roll:n};for(let s of[`l`,`r`]){let d;if(t<=l&&s===f){let n=$((t-c-1)/8);d=e(fh(i.feet[s],C,n),[0,.07*Math.sin(Math.PI*n),0])}else if(t>u)d=ee[s];else{let n=$((t-u+4)/4);d=M(fh(N(`foot`,s,t,b(t,a,`foot`,s)),ee[s],n),e(a,p(r,i.hipOff[s])),i.leg*.995)}let m=t>l&&t<=u?_h(i,s,0,y(t),r,0):{fyaw:i.h,fpitch:0};Object.assign(o,{[`foot_${s}`]:d,[`fyaw_${s}`]:m.fyaw,[`fpitch_${s}`]:m.fpitch,[`knee_${s}`]:gh(i.h,0,n,r,[mh[s]*.05,0,1]),[`hand_${s}`]:M(t>l&&t<=u?N(`hand`,s,t,b(t,a,`hand`,s)):b(t,a,`hand`,s),e(a,p(r,i.shoulder[s])),i.arm*.995),[`elbow_${s}`]:gh(i.h,0,n,r,[mh[s]*.4,0,-1]),[`armw_${s}`]:t<=l?0:t<=u?$((t-l)/6):1-$((t-u)/8)})}o.arm_rest=t<=l?dh(72,-56,$((t-c)/12)):t<=u?-56:dh(-56,72,$((t-u)/14)),F.push({f:t,set:o})}return{keys:F,end:d+1}}function xh(e){let t=e.args.kind??`back`;[`back`,`front`,`cartwheel`].includes(t)||Q(e.tag,`kind must be "back", "front" or "cartwheel", got ${JSON.stringify(t)}`);let n=vh(e);return t===`cartwheel`?bh(e,n):yh(e,n,t)}let Sh=9.81,Ch=.75,wh={back:.16,front:.11},Th=.12,Eh=e=>e-Th*(1-Math.exp(-e/Th)),Dh=(e,t,n)=>t+n*Math.log1p(Math.exp((e-t)/n)),Oh=3/24,kh=.03,Ah=e=>{let t=Math.min(1,Math.max(0,e));return t*t*t*(t*(6*t-15)+10)},jh=e=>(e+540)%360-180,Mh=e=>e.map(e=>Math.round(e*1e3)/1e3),Nh=e=>Math.round(e*10)/10;function Ph(e,t,n){let r=null,i=null;for(let a of e){let e=a.set?.[t];Array.isArray(e)&&(a.f<=n&&(!r||a.f>=r.f)&&(r={f:a.f,v:e}),a.f>n&&(!i||a.f<i.f)&&(i={f:a.f,v:e}))}if(!r||!i)return r?.v;let a=(n-r.f)/(i.f-r.f);return r.v.map((e,t)=>e+(i.v[t]-e)*a)}function Fh(e,t){let{args:n,scene:r}=e,i=n.dir;if(!i&&typeof n.from==`string`){let[a,o]=n.from.split(/:(.*)/s),s=a===`object`?r?.objects?.[o]?.pos:a===`person`?An(r,o,`Hips`,e.f0):null;s||Q(e.tag,`args.from "${n.from}" must be object:<id> of a scene body or person:<id> of a character with an installed take`),i=[t[0]-s[0],t[2]-s[2]]}(!Array.isArray(i)||i.length!==2||!(Math.hypot(i[0],i[1])>1e-6))&&Q(e.tag,`needs args.dir: [x, z] (the way the body is thrown) or args.from: object:<id> | person:<id> (what hits it)`);let a=Math.hypot(i[0],i[1]);return[i[0]/a,i[1]/a]}function Ih(t){let{sk:n,args:r,state:i}=t,a=n.index,o=n.restWorldPos;i.root||Q(t.tag,`needs a pose keyed before f0 (standIdle, a walk, ...)`);let s=Ph(t.keys,`root`,t.f0)??i.root.slice(),c={};for(let e of[`l`,`r`]){let n=Ph(t.keys,`foot_${e}`,t.f0),r=Ph(t.keys,`foot_${e}`,t.f0-1);n&&(c[e]={at:n,v:r?n.map((e,t)=>e-r[t]):[0,0,0]})}let l=i.yaw??0,[u,d]=Fh(t,s),f=r.speed??4;Number.isFinite(f)&&f>0||Q(t.tag,`args.speed must be a launch speed above 0 m/s, got ${JSON.stringify(r.speed)}`);let m=Math.min(2.5,Math.max(.8,.3*f)),h=p(_(l),[0,0,1]),v=(r.land??(u*h[0]+d*h[2]<=0?`back`:`front`))===`back`,y=Math.atan2(u,d)*180/Math.PI,b=v?y+180:y,x=jh(b-l),S=v?-90:90,C=v?wh.back:wh.front,w=s[1],T=e=>w+m*Eh(e)-.5*Sh*e*e,E=e=>m*(1-Math.exp(-e/Th))-Sh*e,D=0,O=(m+Math.sqrt(m*m+2*Sh*Math.max(0,w-C)))/Sh+1;for(let e=0;e<60;e++){let e=(D+O)/2;T(e)+E(e)*Oh/2>C-kh||E(e)>0?D=e:O=e}let k=D,A=Math.min(0,E(k)),j=T(k),M=k+Oh,N=f/(Ch*Sh),P=M+N,ee=e=>e<k?T(e):e<M?j+A*(e-k)-A/(2*Oh)*(e-k)**2:C-kh*(1-Ah((e-M)/.25)),F=o[a.LeftFoot][1],I=o[a.Hips][1],L=v?1:-1,te=.9*Math.hypot(...[0,1,2].map(e=>o[a.LeftFoot][e]-o[a.Hips][e])),R=t.f0+Math.ceil((P+.6)*24),ne=[];for(let n=t.f0;n<=R;n++){let r=(n-t.f0)/24,i=r<M,a=Math.min(Math.max(0,r-M),N),o=i?f*Eh(r):f*Eh(M)+f*a-.5*Ch*Sh*a*a,m=ee(r),h=[s[0]+u*o,m,s[2]+d*o],y=Math.max(.35,.9*M),b=l+x*Ah(r/Math.max(.25,y)),C=S*Ah(r/y),w=(e=>p(_(b),p(g(C),e))),T=Ah(r/Th)*Math.exp(-r/.18)*Math.sin(2*Math.PI*r/.55),E=Ah(r/.3)*Math.exp(-r/.45)*Math.sin(2*Math.PI*r/.6),D=Ah((r-M)/.3),O=-(I-F)+.06*(1-D)+(v?.03:.01)*D,k=Ah(r/.25),A=F+.01+(!v&&!i&&a<N?.04:0),j=(r,i,a)=>{let o=e(h,w([i,O,a])),s=h[1]-.05;v&&(o[1]=s-.05*Math.log1p(Math.exp((s-o[1])/.05))),o[1]=Dh(o[1],A,.03);let l=c[r];if(!l)return o;let u=l.at.map((e,r)=>e+l.v[r]*(n-t.f0)*(1-k));return u[1]=Math.max(l.at[1],u[1]),P(o.map((e,t)=>u[t]+(e-u[t])*k))},P=e=>{let t=e.map((e,t)=>e-h[t]),n=Math.hypot(...t),r=te-.02*Math.log1p(Math.exp((te-n)/.02));return n>1e-9?h.map((e,i)=>e+t[i]*(1+(r/n-1)*k)):e},R=ss(0,C,0),re=R?p(R.R,[0,1,0]):[0,1,0],ie=Math.atan2(re[2],re[1])*180/Math.PI,z=p(g(-ie),[0,-Math.sin(C*Math.PI/180),Math.max(.05,Math.cos(C*Math.PI/180))]),ae={root:Mh(h),yaw:Nh(b),pitch:Nh(C),roll:0,foot_l:Mh(j(`l`,.11,.04+.1*E*k-(v?.1:0)*D)),foot_r:Mh(j(`r`,-.11,.04-.1*E*k-(v?.1:0)*D)),fyaw_l:Nh(b),fyaw_r:Nh(b),fpitch_l:Nh(v?-50*D:-.5*ie),fpitch_r:Nh(v?-50*D:-.5*ie),knee_l:Mh([.05,z[1],z[2]]),knee_r:Mh([-.05,z[1],z[2]]),spine_pitch:Nh(L*22*T*Math.min(1,f/4)),spine_yaw:0,spine_roll:Nh(10*E*(1-D)),head_pitch:Nh(L*30*T*Math.min(1,f/4)+(v?-8:8)*D),head_roll:Nh(12*D),arm_rest:Nh(72-22*D),aswing_l:Nh(L*45*E+8*D),aswing_r:Nh(-L*35*E-8*D),abend_l:Nh(25+30*E*E),abend_r:Nh(30+25*E*E)};n===t.f0&&Object.assign(ae,{armw_l:0,armw_r:0,fingers_l:{relaxed:1},fingers_r:{relaxed:1}}),ne.push({f:n,set:ae})}return{keys:ne,end:R}}let Lh=e=>e.map(e=>Math.round(e*1e3)/1e3);function Rh(e,t){let n=Object.entries(e?.objects??{}).filter(([e,n])=>e===t||n.ancestors?.includes(t)).map(([,e])=>e),r=e?.objects?.[t]?.pos??n[0]?.ancestorPoses?.[0]?.pos;return!r||!n.length?0:r[1]-Math.min(...n.map(e=>e.pos[1]))}function zh(r){let{args:i,state:o,sk:s,scene:c,tag:l}=r,u=i.object,d=i.side??`r`,f=r.objects?.[u];d!==`l`&&d!==`r`&&Q(l,`args.side must be "l" or "r", got ${JSON.stringify(d)}`),(!f?.prop||r.props?.[f.prop]?.hand!==d)&&Q(l,`args.object "${u}" must be carried by the ${d===`r`?`right`:`left`} hand (objects.${u}.prop on a prop with hand "${d}")`),(!Array.isArray(i.to)||i.to.length!==3)&&Q(l,`args.to must be [x, y, z] world m (where it lands)`);let m=(i.arc===`high`?50:20)*Math.PI/180,h=r.fps??24,g=d===`l`?`Left`:`Right`,v=s.restWorldPos,y=o.root??v[s.index.Hips],b=_(o.yaw??0),x=e(y,p(b,t(v[s.index[`${g}Arm`]],v[s.index.Hips]))),S=[i.to[0]-x[0],0,i.to[2]-x[2]],C=a(S);C<.5&&Q(l,`args.to is ${C.toFixed(2)} m from the shoulder; throw it at least 0.5 m`);let w=n(S,1/C),T=e(x,e(n(w,.42),[0,.22,0])),E=e(T,n(w,.09)),D=i.to[1]+Rh(c,u),O=a([i.to[0]-E[0],0,i.to[2]-E[2]]),k=D-E[1],A=2*Math.cos(m)**2*(O*Math.tan(m)-k);A>0||Q(l,`args.to is too high for a ${i.arc??`low`} arc from the hand; use arc "high" or a lower point`);let j=Math.sqrt(Za*O*O/A),M=e(n(w,j*Math.cos(m)),[0,j*Math.sin(m),0]),N=(M[1]+Math.sqrt(M[1]**2+2*Za*Math.max(0,E[1]-D)))/Za,P=r.f0+10+8,ee=Math.min(r.frames-1,P+Math.ceil(N*h)+8),F=n([w[2],0,-w[0]],d===`r`?-1:1),I=`hand_${d}`,L=`armw_${d}`,te=e(x,e(e(n(w,-.12),n(F,.25)),[0,-.05,0])),R=e(x,e(e(n(F,.32),n(w,.12)),[0,.08,0])),ne=e(T,n(M,.45/h)),re=e(x,e(n(w,.46),[0,-.1,0])),ie=e(x,e(e(n(w,.3),n(F,-.15)),[0,-.4,0])),z=`fingers_${d}`,ae=on[f.grasp]??`grip`,oe={...o[z],[ae]:1},se={...o[z],[ae]:0},ce=[{f:r.f0,set:{[L]:o[L]??0,[z]:oe,...o[I]?{[I]:o[I]}:{}}},{f:P,set:{[z]:oe}},{f:P+3,set:{[z]:se}},{f:r.f0+10,set:{[L]:1,[I]:Lh(te)}},{f:r.f0+10+4,set:{[L]:1,[I]:Lh(R)}},{f:P-1,set:{[L]:1,[I]:Lh(t(T,n(M,1/h)))}},{f:P,set:{[L]:1,[I]:Lh(T)}},{f:P+1,set:{[L]:1,[I]:Lh(ne)}},{f:P+3,set:{[L]:1,[I]:Lh(re)}},{f:P+10,set:{[L]:1,[I]:Lh(ie)}}],le=`${u}Target`;return{keys:ce,end:P+10,anchors:{[le]:{kind:`world`,p:Lh([i.to[0],D,i.to[2]])}},fills:{[u]:{release:P,fall:{at:P,v:Lh(M)}}},goals:[{name:`${u} lands at the target`,near:[`object:${u}`,`anchor:${le}`],at:ee,tol:.25}],throw:{release:P,speed:Math.round(j*100)/100,v:Lh(M),flight:Math.round(N*100)/100}}}let Bh=[.35,.65],Vh=e=>e.map(e=>Math.round(e*1e3)/1e3),Hh=(e,t,n)=>Math.min(n,Math.max(t,e));function Uh(n,r,i){let a=n.restWorldPos,o=r.root??a[n.index.Hips],s=_(r.yaw??0);return{hips:o,shoulder:e(o,p(s,t(a[n.index[`${i}Arm`]],a[n.index.Hips]))),head:e(o,p(s,t(a[n.index.Head],a[n.index.Hips]))),fwd:p(s,[0,0,1])}}function Wh(i){let{args:o,state:c,sk:l,scene:u,tag:d}=i,f=o.object,m=o.side??`r`,h=i.objects?.[f],g=i.fps??24;m!==`r`&&m!==`both`&&Q(d,`args.side must be "r" or "both", got ${JSON.stringify(m)}`);let v=Array.isArray(h?.fall)?h.fall[0]:h?.fall;Array.isArray(v?.v)||Q(d,`args.object "${f}" must fly: objects.${f}.fall { at, v: [vx, vy, vz] }`);let y=Uh(l,c,`Right`),b={spec:{objects:{[f]:{fall:h.fall}},fps:g},scene:u,timeMap:null},x=e=>vo(b,f,e)?.pos,S=null,C=null;for(let e=v.at+1;e<i.frames;e++){let n=x(e),i=a(t(n,y.shoulder)),o=r(t(n,y.hips),y.fwd),s=Math.max(0,Bh[0]-i,i-Bh[1],.15-o,y.hips[1]-.1-n[1],n[1]-y.head[1]);if((!C||s<C.off)&&(C={f:e,p:n,off:s}),s===0){S={f:e,p:n};break}}S||Q(d,`${f} never comes within reach of the right hand (in front of the chest, ${Bh[0]}-${Bh[1]} m from the shoulder): nearest at f${C.f}, [${Vh(C.p).join(`, `)}], ${C.off.toFixed(2)} m out; move the body (standIdle at) or the throw`);let w=Math.max(S.f,i.f0+10+2),T=S.p,E=n(t(x(S.f),x(S.f-1)),g),D=a(E),O=hl({objects:i.objects},u,f),k=Hh(.08+.02*O*D,.08,.15),A=s(E),j=e=>1-(1-e)**3,M=[T,...Array.from({length:6},(t,r)=>e(T,n(A,k*j((r+1)/6)))),e(T,n(A,k*.4))],N=Math.max(i.f0,w-2-20),P=m===`both`||O>1,ee=Math.max(0,...Object.values(u.objects).filter(e=>e===u.objects[f]||e.ancestors?.includes(f)).map(e=>Math.max(...e.half))),F=p(_(c.yaw??0),[1,0,0]),I={keys:[],end:w+6+8,props:{},anchors:{},fits:[],channels:[],fills:{[f]:{prop:`${f}Catch`,grab:w,grasp:`spherical`}},catch:{contact:w,point:Vh(T),speed:Math.round(D*100)/100,give:Math.round(k*1e3)/1e3,hands:P?2:1}};for(let t of P?[`r`,`l`]:[`r`]){let r=t===`l`?`Left`:`Right`,i=t===`r`?`${f}Catch`:`${f}CatchL`,a=t===`r`?[0,0,0]:n(F,2*ee+.04),o=`armw_${t}`,s=`fingers_${t}`,l={...c[s],spherical:0},u={...c[s],spherical:1},d=M.map((e,t)=>`${i}${t}`),p=e=>`w_${d[e]}_${t}`;I.keys.push({f:N,set:{[o]:c[o]??0,[p(0)]:0,[s]:l,[`elbow_${t}`]:t===`r`?[-1,-.5,-.5]:[1,-.5,-.5]}},...dm(w-2-N).map(([e,t,n])=>({f:N+e,set:{[p(0)]:t,[o]:(c[o]??0)+(1-(c[o]??0))*n}})),{f:w-2,set:{[o]:1,[p(0)]:1,[s]:l}},{f:w,set:{[p(0)]:1,[s]:u}},...M.slice(1).flatMap((e,t)=>{let n=t+1,r=n<=6?w+n:w+6+8;return[{f:n<=6?r-1:w+6,set:{[p(n-1)]:1,[p(n)]:0}},{f:r,set:{[p(n-1)]:0,[p(n)]:1}}]})),I.props[i]={hand:t,origin:[`${r}HandIndex2`,`${r}HandMiddle2`],points:{fingers:[0,0,0]}},d.forEach((n,r)=>{I.anchors[n]={kind:`world`,p:Vh(e(M[r],a))},I.fits.push({side:t,anchor:n,effector:{prop:i,point:`fingers`}}),I.channels.push(p(r))})}return I}let Gh={standIdle:Tm,sitInSeat:Lm,exitCar:Rm,walkTo:Em,reachGrip:km,smokeCigarette:ch,flip:xh,hitReaction:Ih,throwObject:zh,catchObject:Wh},Kh=Object.keys(Gh),qh=e=>{throw Error(`MOTION_SPEC_INVALID: ${e}`)},Jh=(e,t)=>JSON.stringify(e)===JSON.stringify(t);function Yh(e,t,n,r){for(let[i,a]of Object.entries(t??{}))e[i]&&!Jh(e[i],a)&&qh(`${r} defines ${n}.${i} differently from an earlier skill; rename one or define it yourself in spec.${n}`),e[i]=a}function Xh(e,t){for(let[n,r]of Object.entries(t??{})){if(n===`limits`&&r&&typeof r==`object`&&!Array.isArray(r)){e.limits={...e.limits,...r};continue}if(!Array.isArray(r)||e[n]!==void 0&&!Array.isArray(e[n])){e[n]=r;continue}let t=e[n]??(e[n]=[]);for(let e of r)t.some(t=>Jh(t,e))||t.push(e)}}function Zh(e,t,n){e.args!==void 0&&(!e.args||typeof e.args!=`object`||Array.isArray(e.args))&&qh(`use[${t}].args must be an object, got ${JSON.stringify(e.args)}`);let r=Object.keys(Np[e.skill].args);if(!n)for(let n of Object.keys(e.args??{}))r.includes(n)||qh(`use[${t}].args.${n} is not an arg of ${e.skill}${fn(n,r)} (args: ${r.join(`, `)})`)}function Qh(e,{sk:t,scene:n,lenient:r=!1,auto:i=!1}){if(!e.use?.length)return{spec:e,used:[]};Array.isArray(e.use)||qh(`spec.use must be an array of { skill, args, f0 }`);let a=(e.keys??[]).map((e,t)=>({...e,src:`keys[${t}]`})),o={anchors:{},props:{},objects:{},fits:[],checks:{},channels:[],fills:{},goals:[]},s=[],c=[];e.use.forEach((l,u)=>{let d=`use[${u}] ${l?.skill}`;Gh[l?.skill]||qh(`use[${u}].skill "${l?.skill}" is not a skill${H(String(l?.skill),Kh)} (skills: ${Kh.join(`, `)})`);for(let e of Object.keys(l))!zt.includes(e)&&!r&&qh(`use[${u}] has unknown field "${e}"${fn(e,zt)}; an entry is { skill, args, f0 }`);Zh(l,u,r),(!Number.isInteger(l.f0)||l.f0<0||l.f0>e.frames-1)&&qh(`use[${u}].f0 must be an integer frame within 0..${e.frames-1}, got ${JSON.stringify(l.f0)}`);let f=[...s,...a],p={sk:t,scene:n,frames:e.frames,fps:e.fps??24,auto:i,props:{...o.props,...e.props},self:e.fix!==!1&&e.fix?.self!==!1,f0:l.f0,args:structuredClone(l.args??{}),tag:d,keys:f,use:e.use,index:u,place:ul(e.fix).stance,state:structuredClone(Tc(f,l.f0)),anchors:{...o.anchors,...e.anchors},objects:{...o.objects,...e.objects}},m=Gh[l.skill](p),h=Math.max(m.end??l.f0,...m.keys.map(e=>e.f));h>e.frames-1&&qh(`${d} needs ${h-l.f0} frames from f${l.f0} (to f${h}), past the last frame f${e.frames-1}; set frames >= ${h+1} or frames: "auto"`),s.push(...m.keys.map(e=>({...e,src:e.src??d}))),Yh(o.anchors,m.anchors,`anchors`,d),Yh(o.props,m.props,`props`,d),Yh(o.objects,m.objects,`objects`,d);for(let e of m.fits??[])o.fits.some(t=>Jh(t,e))||o.fits.push(e);for(let e of m.channels??[])o.channels.includes(e)||o.channels.push(e);Xh(o.checks,m.checks);for(let[e,t]of Object.entries(m.fills??{}))o.fills[e]={...o.fills[e],...t};o.goals.push(...m.goals??[]),c.push({skill:l.skill,f0:l.f0,end:m.end,...m.paced?{paced:m.paced}:{},...m.cut?{cut:m.cut}:{},...m.placed?{placed:m.placed}:{}})});let l={...e,keys:[...s,...a],anchors:{...o.anchors,...e.anchors},props:{...o.props,...e.props},objects:{...o.objects,...e.objects}},u=[...new Set([...o.channels,...Array.isArray(e.channels)?e.channels:[]])];u.length&&(l.channels=u);let d=e.fits??[];l.fits=[...o.fits.filter(e=>!d.some(t=>t.side===e.side&&t.anchor===e.anchor)),...d],Xh(o.checks,e.checks),l.checks=o.checks;for(let[e,t]of Object.entries(o.fills))l.objects[e]&&(l.objects[e]={...t,...l.objects[e]});return o.goals.length&&(l.goals=[...o.goals,...e.goals??[]]),delete l.use,{spec:l,used:c}}let $h=new Set([`f`,`f0`,`steps`,`period`,`swing`,`until`,`reach`,`hold`,`release`]),eg=e=>e&&typeof e==`object`&&!Array.isArray(e)&&(e.range??e.search)!==void 0&&Object.keys(e).every(e=>[`range`,`search`,`int`].includes(e)),tg=e=>e.reduce((e,t)=>typeof t==`number`?`${e}[${t}]`:e?`${e}.${t}`:t,``);function ng(e){let t=[],n=(e,r)=>{if(eg(e)){let n=e.range??e.search;if(!(Array.isArray(n)&&n.length===2&&n.every(Number.isFinite)&&n[0]<n[1]))throw Error(`MOTION_SPEC_INVALID: ${tg(r)} range must be [lo, hi] with lo < hi, got ${JSON.stringify(n)}`);t.push({path:r,lo:n[0],hi:n[1],int:e.int??$h.has(r[r.length-1])});return}if(Array.isArray(e))e.forEach((e,t)=>n(e,[...r,t]));else if(e&&typeof e==`object`)for(let[t,i]of Object.entries(e))n(i,[...r,t])};for(let[t,r]of Object.entries(e??{}))[`schema`,`fps`,`frames`,`search`].includes(t)||n(r,[t]);return t}let rg=(e,t)=>{let n=Math.min(e.hi,Math.max(e.lo,t));return e.int?Math.round(n):Math.round(n*1e4)/1e4};function ig(e,t,n){let r=structuredClone(e);return t.forEach((e,t)=>{let i=r;for(let t of e.path.slice(0,-1))i=i[t];i[e.path[e.path.length-1]]=n[t]}),r}let ag=e=>e.groups.reduce((e,t)=>e+fp(t),0);function og(e,t,n,r,i){let a=n=>r(ig(e,t,n)),o=a(n),s=e=>JSON.stringify({...e,keys:void 0,generators:void 0}),c=e=>Tr(Dc(e,i),e.frames,Sr(e.flow)),l=c(o),u=1/0,d=-1/0;for(let r=0;r<t.length;r++)for(let i of[t[r].lo,t[r].hi]){let f=a(n.map((e,n)=>n===r?rg(t[r],i):e));if(s(f)!==s(o))return null;let p=c(f);for(let t of new Set([...Object.keys(l),...Object.keys(p)])){let n=l[t],r=p[t];for(let t=0;t<e.frames;t++)JSON.stringify(n?.[t])!==JSON.stringify(r?.[t])&&(u=Math.min(u,t),d=Math.max(d,t))}}return u>d?null:[Math.max(0,u-8),Math.min(e.frames-1,d+8)]}let sg=[2,3,5,7,11,13,17,19,23,29],cg=(e,t)=>Array.from({length:t},(t,n)=>{let r=1,i=0,a=e,o=sg[n%sg.length];for(;a>0;)r/=o,i+=a%o*r,a=Math.floor(a/o);return i});function lg(e,t,{sk:n,scene:r},i){let a=performance.now(),o=Math.max(1,Math.min(64,Math.round(e.search?.budget??32))),s=t.map(e=>rg(e,(e.lo+e.hi)/2)),c=og(e,t,s,i,n),l=new Map,u=0,d=null,f=a=>{let s=a.join(`,`);if(l.has(s))return l.get(s);if(u>=o)return 1/0;u++;let f;try{let o=i(ig(e,t,a));f=ag(vp(n,Nl({fps:24,...o},{sk:n,scene:r},{window:c}),o,{explain:!1}))}catch(e){if(!/MOTION_SPEC_INVALID/.test(e.message))throw e;f=1e9}return l.set(s,f),(!d||f<d.cost)&&(d={values:a,cost:f}),f},p=()=>d?.cost===0||u>=o,m=e=>t.map((t,n)=>rg(t,t.lo+(t.hi-t.lo)*e[n]));f(s);let h=t.length===1?[4,3,5,2,6,1,7,0,8].map(e=>[e/8]):Array.from({length:Math.min(o>>1,8*t.length)},(e,n)=>cg(n+1,t.length));for(let e of h){if(p())break;f(m(e))}let g=t.map(e=>(e.hi-e.lo)/8);for(;!p()&&g.some((e,n)=>e>(t[n].int?.5:(t[n].hi-t[n].lo)*.001));){let e=!1;for(let n=0;n<t.length&&!p();n++)for(let r of[-1,1]){let i=d.values.slice();i[n]=rg(t[n],i[n]+r*g[n]);let a=d.cost;f(i),d.cost<a&&(e=!0)}e||(g=g.map(e=>e/2))}return{spec:ig(e,t,d.values),search:{values:Object.fromEntries(t.map((e,t)=>[tg(e.path),d.values[t]])),evals:u,cost:Math.round(d.cost*100)/100,window:c,ms:Math.round(performance.now()-a)}}}function ug(e){let t=structuredClone(e);for(let n of ng(e)){let e=t;for(let t of n.path.slice(0,-1))e=e[t];e[n.path[n.path.length-1]]=n.hi}return t}function dg(e,{sk:t,scene:n,lenient:r=!1}={}){if(e?.frames!==`auto`)return e;let{spec:i,used:a}=Qh({...ug(e),frames:2e4},{sk:t,scene:n,lenient:r,auto:!0}),o=[...Array.isArray(i.keys)?i.keys.map(e=>e?.f):[],...a.map(e=>e.end),...Array.isArray(i.generators)?i.generators.map(e=>e&&Number.isFinite(e.f0)?kp(e):null):[]];return{...e,frames:Math.max(0,...o.filter(Number.isFinite))+1}}let fg=new WeakSet;function pg(e){return fg.add(e),e}let mg=e=>Math.round(e*1e3)/1e3,hg=e=>Math.round(e*10)/10,gg=e=>e.map(mg);function _g(e,t,n){let r=e.frameCount-1,i=e.timeMap??(e=>e),a=t.map((e,t)=>({beat:e.skill,src:`use[${t}]`,f:[e.f0,e.end??e.f0]}));(n.generators??[]).forEach((e,t)=>a.push({beat:e.type,src:`generators[${t}]`,f:[e.f0,e.f0+16+(e.steps??8)*(e.period??20)+18]}));let o=e=>a.some(t=>e>=t.f[0]&&e<=t.f[1]),s=[...new Set((n.keys??[]).map(e=>e.f))].filter(e=>Number.isFinite(e)&&!o(e)).sort((e,t)=>e-t),c=null;for(let e of s)c&&!a.some(t=>t.src!==`keys`&&t.f[1]>c.f[1]&&t.f[0]<e)?c.f[1]=e:a.push(c={beat:`keys`,src:`keys`,f:[e,e]});let l=e=>Math.max(0,Math.min(r,Math.round(i(e))));for(let e of a)e.f=[l(e.f[0]),l(e.f[1])];return a.sort((e,t)=>e.f[0]-t.f[0]).slice(0,12)}function vg(e){let t=e.frameCount,n=e.f0??0,r={l:new Uint8Array(t),r:new Uint8Array(t)};for(let[t,i]of[[`l`,`Left`],[`r`,`Right`]]){let a=e.frames.map((t,n)=>t?ii(e,n,i):null);a.forEach((e,i)=>{let o=a[i>0?i-1:i+1];if(!e||!o)return;let s=[`heel`,`ball`].some(t=>e[t][1]<Ai.plantH&&Math.abs(e[t][1]-o[t][1])<5e-4&&Math.hypot(e[t][0]-o[t][0],e[t][2]-o[t][2])<=Ai.slide);r[t][n+i]=+!!s})}return r}function yg(e,[t,n]){let r=[],i=null;for(let a=t;a<=n+1;a++){let t=a<=n&&e[a]===1;t&&i===null&&(i=a),!t&&i!==null&&(a-i>=3&&r.push([i,a-1]),i=null)}if(r.length<=4)return r;let a=r.slice(1,-1).sort((e,t)=>t[1]-t[0]-(e[1]-e[0])||e[0]-t[0]).slice(0,2);return[r[0],...a,r[r.length-1]].sort((e,t)=>e[0]-t[0])}function bg(e,t){let n=e.frameCount,r=[],i=Object.entries(t.objects??{});for(let[e,t]of i)for(let i=1;i<n;i++){let n=So(t,i-1),a=So(t,i);a&&!n&&r.push({f:i,object:e,event:`grab`}),n&&!a&&r.push({f:i-1,object:e,event:`release`})}let a={};for(let[e,t]of i)typeof t?.visible==`string`&&(a[t.visible]??=[]).push(e);for(let t of new Set([...Object.keys(a),...Object.keys(e.baked??{}).filter(e=>e.endsWith(`_vis`))])){let i=e.baked?.[t];if(i)for(let e=1;e<n;e++){let n=i[e-1]>.5,o=i[e]>.5;if(n!==o)for(let n of a[t]??[t])r.push({f:e,object:n,event:o?`show`:`hide`})}}return r.sort((e,t)=>e.f-t.f)}function xg(e,t,n,r){for(let[t,i]of Object.entries(e.objects??{}))if(e.props?.[i?.prop]?.hand===n&&So(i,r))return`object:${t}`;let i=null,a=.5;for(let[e,r]of Object.entries(t)){let t=/^w_(.+)_([lr])$/.exec(e);t&&t[2]===n&&r>a&&(i=t[1],a=r)}return i&&`anchor:${i}`}function Sg(e,t,n){let r=e.frames[n-(e.f0??0)];if(!r)return null;let{s:i,c:a}=r,o=e.ctx.sk.index;return{root:gg(i.P[o.Hips]),yaw:hg(a.yaw??0),hand_l:gg(i.P[o.LeftHand]),hand_r:gg(i.P[o.RightHand]),on_l:xg(t,a,`l`,n),on_r:xg(t,a,`r`,n)}}function Cg(e,{used:t,spec:n}){let r=_g(e,t??[],n);if(!r.length)return[];let i=vg(e),a=bg(e,n);return r.map(t=>({...t,end:Sg(e,n,t.f[1]),planted:{l:yg(i.l,t.f),r:yg(i.r,t.f)},events:a.filter(e=>e.f>=t.f[0]&&e.f<=t.f[1])}))}let wg=e=>`[${e[0]},${e[1]}]`,Tg=(e,t)=>{e&&!e.includes(t)&&e.push(t)};function Eg(e,t){if(!(Array.isArray(e)&&e.length===2&&e.every(Number.isInteger)&&e[0]<=e[1]))return null;let n=Math.max(0,e[0]),r=Math.min(t,e[1]);return n<=r?[n,r]:null}function Dg(e,t){if(!Array.isArray(e.goals)||!Number.isInteger(e.frames))return;let n=e.frames-1;e.goals.forEach((e,r)=>{if(!e||typeof e!=`object`||!Array.isArray(e.at))return;let i=Eg(e.at,n);!i||i[0]===e.at[0]&&i[1]===e.at[1]||(Tg(t,`goals[${r}]${e.name?` "${e.name}"`:``} at ${wg(e.at)} -> ${wg(i)}: the clip is 0..${n}`),e.at=i)})}let Og=e=>e.rot===void 0?{aim:e.aim,...e.roll===void 0?{}:{roll:e.roll},...e.lean===void 0?{}:{lean:e.lean}}:{rot:e.rot},kg=e=>e&&(e.rot!==void 0||e.aim!==void 0),Ag=e=>e&&typeof e==`object`&&!Array.isArray(e)&&!kg(e)&&e.roll===void 0&&e.lean===void 0;function jg(e,t,n,r){let i=t.keys.map((e,t)=>({k:e,i:t})).filter(({k:e})=>e&&Number.isInteger(e.f)).sort((e,t)=>e.k.f-t.k.f),a=i.filter(({k:e})=>Ag(e));if(!a.length)return;let o=n?.objects?.[e]??En(n??{})[e],s=o?K(t)?{aim:[o.R[0][1],o.R[1][1],o.R[2][1]].map(e=>Math.round(e*1e4)/1e4)}:{rot:S(o.R)}:null,c=i.filter(e=>kg(e.k)),l=[],u=[];for(let{k:e,i:t}of a){let n=c.filter(t=>t.k.f<e.f).at(-1)??c.find(t=>t.k.f>e.f);n?(Object.assign(e,Og(n.k)),l.push(t)):s&&(Object.assign(e,s),u.push(t))}l.length&&Tg(r,`objects.${e}.keys[${l.join(`,`)}] had no rot or aim: each holds the turn of the nearest key before it (else after)`),u.length&&Tg(r,`objects.${e}.keys[${u.join(`,`)}] had no rot or aim: ${s.aim?`+Y as in the scene, aim [${s.aim}]`:`turned as in the scene`}`)}function Mg(e,t,n){t.keys.forEach((t,r)=>{if(!Array.isArray(t?.rot)||t.rot.length!==4||!t.rot.every(Number.isFinite))return;let i=Math.hypot(...t.rot);Math.abs(i-1)<=.01||i<=.5||i>=2||(t.rot=t.rot.map(e=>e/i),Tg(n,`objects.${e}.keys[${r}].rot had length ${i.toFixed(3)}: normalized`))})}function Ng(e,t,n){if(!e||typeof e!=`object`)return e;Dg(e,n);for(let[r,i]of Object.entries(e.objects??{}))!i||typeof i!=`object`||!Array.isArray(i.keys)||!(K(i)||$a(i))||(jg(r,i,t,n),Mg(r,i,n));return e}function Pg(e,t,n,r){let i=Number.isInteger(e)?[e,e]:e,a=Eg(i,t);return a&&(a[0]!==i[0]||a[1]!==i[1])&&Tg(r,`${n}.frames ${wg(i)} -> ${wg(a)}: the take is 0..${t}`),a}function Fg(e,t,n,r,i){if(Math.ceil(t/e)<=n)return e;let a=Math.ceil(t/n);return Tg(i,`${r}.every ${e} -> ${a}: a position probe answers at most ${n} rows`),a}let Ig=`{ name, distance: [refA, refB] | position: ref, frames: [f0, f1], every }`,Lg=e=>Math.round(e*1e6)/1e6,Rg=e=>Math.round(e*1e4)/1e4;function zg(e,t){let n=/^(joint|bone):(.+)$/s.exec(t);return n?`${n[1]}:${xn(e,n[2]).bone??n[2]}`:t}function Bg(e,t,n,r,i){let a=e.frames===void 0?[0,n]:Pg(e.frames,n,t,i);return a||r(`${t}.frames must be [f0, f1], integer frames within 0..${n} with f0 <= f1, got ${V(e.frames)}`),a}function Vg(e,t,{spec:n,sk:r,scene:i,notes:a}){let o=[],s=e=>{o.length<12&&o.push(e)},c=e.frameCount-1,l=[],u=new Map;if(Array.isArray(t)?t.length>16?s(`probes has ${t.length} entries; at most 16 per compile`):t.forEach((e,t)=>{let o=`probes[${t}]`;if(!e||typeof e!=`object`||Array.isArray(e))return s(`${o} must be ${Ig}`);for(let t of Object.keys(e))Xt.includes(t)||s(`${o} has unknown field "${t}"${H(t,Xt)}; a probe is ${Ig}`);let d=e.name??o;if(typeof d!=`string`||!d?s(`${o}.name must be a non-empty string, got ${V(e.name)}`):u.has(d)?s(`${o}.name "${d}" is already used by ${u.get(d)}; probe names must differ`):u.set(d,o),e.distance===void 0==(e.position===void 0))return s(`${o} needs exactly one of distance: [refA, refB] or position: ref`);let f=e.distance===void 0?`position`:`distance`,p=f===`distance`?e.distance:[e.position];if(f===`distance`&&!(Array.isArray(p)&&p.length===2&&p.every(e=>typeof e==`string`)))return s(`${o}.distance must be [refA, refB], two point refs, got ${V(e.distance)}`);if(f===`position`&&typeof e.position!=`string`)return s(`${o}.position must be a point ref, got ${V(e.position)}`);let m=p.map((e,t)=>fr(n,s,{sk:r,scene:i},f===`distance`?`${o}.distance[${t}]`:`${o}.position`,e)).every(Boolean),h=Bg(e,o,c,s,a);if(e.every!==void 0&&!(Number.isInteger(e.every)&&e.every>=1))return s(`${o}.every must be a positive integer, got ${V(e.every)}`);if(!m||!h)return;let g=h[1]-h[0]+1,_=e.every??(f===`position`?Math.ceil(g/16):1),v=f===`position`?Fg(_,g,16,o,a):_;l.push({name:d,kind:f,refs:p.map(e=>zg(r,e)),range:h,every:v})}):s(`probes must be an array of ${Ig}`),o.length)throw Error(`MOTION_SPEC_INVALID: ${o.join(`
4
- `)}${o.length>=12?`
5
- (more problems not listed)`:``}`);return l}function Hg([e,t],n){let r=[];for(let i=e;i<=t;i+=n)r.push(i);return r}function Ug(e,t){let n=Hg(t.range,t.every).map(n=>[n,ur(e,n,t.refs[0],t.refs[1])]).filter(([,e])=>Number.isFinite(e));if(!n.length)return{min:null,minAt:null,max:null,maxAt:null,mean:null,first:null,last:null};let r=n[0],i=n[0],a=0;for(let e of n)e[1]<r[1]&&(r=e),e[1]>i[1]&&(i=e),a+=e[1];return{min:Lg(r[1]),minAt:r[0],max:Lg(i[1]),maxAt:i[0],mean:Lg(a/n.length),first:Lg(n[0][1]),last:Lg(n[n.length-1][1])}}function Wg(e,t){return{at:Hg(t.range,t.every).map(n=>{let r=lr(e,n,t.refs[0]);return{f:n,p:r?r.map(Rg):null}})}}function Gg(e,t,n){return Object.fromEntries(Vg(e,t,n).map(t=>[t.name,t.kind===`distance`?Ug(e,t):Wg(e,t)]))}let Kg=.3,qg=[`Thumb4`,`Index4`,`Middle4`,`Ring4`,`Pinky4`],Jg=e=>Math.round(e*1e3)/1e3;function Yg(t,r){let i=t.index,a={};for(let[o,s]of[[`Left`,`l`],[`Right`,`r`]]){let c=n(e(r.P[i[`${o}Hand`]],r.P[i[`${o}HandMiddle1`]]),.5);a[`hand_${s}`]=[c,...qg.map(e=>r.P[i[`${o}Hand${e}`]]).filter(Boolean)],a[`foot_${s}`]=Object.values(xt(t,r,o))}return a}let Xg=(e,t,n)=>[t,...e.ctx.scene.objects[t].ancestors??[]].some(t=>{let r=e.ctx.spec.objects?.[t];return r?.visible&&!((e.baked?.[r.visible]?.[n]??0)>.5)});function Zg(t,n){let{scene:r,spec:i}=t.ctx,o=[];for(let[s,c]of Object.entries(r?.objects??{})){let r=c.ancestors??[];if([s,...r].some(e=>ls(i.objects?.[e]))||Xg(t,s,n))continue;let l=zo(t,s,n);if(!l)continue;let u={pos:l.pos,R:l.R,half:c.half};o.push({id:s,root:r[r.length-1]??s,hand:Ro(t,s,n),box:u,c:e(u.pos,p(u.R,[0,c.half[1],0])),r:a(c.half)})}return o}function Qg(t,r,i){let a=n(t.reduce((t,n)=>e(t,n),[0,0,0]),1/t.length),s=Math.max(...t.map(e=>o(e,a))),c=null;for(let e of r){if(i&&e.hand===i||o(a,e.c)-e.r-s>(c?c[2]:Kg))continue;let n=Math.min(...t.map(t=>B(t,e.box)));n<=Kg&&(!c||n<c[2])&&(c=[e.root,e.id,n])}return c}function $g(e,t){let n=[];return e.forEach((e,r)=>{let i=n[n.length-1];if(!e)return;let[a,o,s]=e,c=r+t;i&&i.object===a&&i.frames[1]===c-1?(i.frames[1]=c,s<i.gap[0]&&(i.gap[0]=s,i.gap[1]=c,i.part=o),s>i.gap[2]&&(i.gap[2]=s,i.gap[3]=c)):n.push({object:a,part:o,frames:[c,c],gap:[s,c,s,c]})}),n.sort((e,t)=>e.gap[0]-t.gap[0]).slice(0,2).sort((e,t)=>e.frames[0]-t.frames[0]).map(({object:e,part:t,frames:n,gap:r})=>({object:e,...t===e?{}:{part:t},frames:n,gap:[Jg(r[0]),r[1],Jg(r[2]),r[3]]}))}function e_(e,t){if(!Object.keys(t.ctx.scene?.objects??{}).length)return null;let n=t.f0??0,r={hand_l:[],hand_r:[],foot_l:[],foot_r:[]};t.frames.forEach(({s:i},a)=>{let o=Zg(t,a+n),s=Yg(e,i);for(let e of Object.keys(r))r[e].push(Qg(s[e],o,e.startsWith(`hand_`)?e.slice(-1):null))});let i=Object.fromEntries(Object.entries(r).map(([e,t])=>[e,$g(t,n)]).filter(([,e])=>e.length));return Object.keys(i).length?i:null}let t_=.25,n_=.75,r_=Array.from({length:16},(e,t)=>9+t),i_=[`steps`,`period`,`step`,`swing`],a_=new Set([`POP`,`REACH`,`SLIDE`,`FLOAT`,`FLIP`,`KNEE`]),o_=new Map,s_=e=>Math.round(e*100)/100,c_=e=>(Array.isArray(e?.use)?e.use:[]).map((e,t)=>({i:t,u:e})).filter(({u:e})=>e?.skill===`walkTo`&&Number.isInteger(e.args?.arrive)&&Array.isArray(e.args?.to)&&!i_.some(t=>e.args[t]!==void 0)&&Object.values(e.args).every(e=>!(e&&typeof e==`object`&&!Array.isArray(e))));function l_(e,t){if(t===2)return e>=t_&&e<=n_?{first:e,step:e}:null;let n=Math.min(.62,Math.max(.3,.8*e/(t-2+.8))),r=(e-n)/(t-2);return r>=t_&&r<=n_?{first:n,step:r}:null}function u_(e,t,n){let r=[];for(let i of r_)for(let a=2;a<=40;a++){let o=l_(e,a);if(!o)continue;let s=t+16+a*i+18,c=o.step*24/i,l=Math.abs(Math.log(o.step*i/9.7)),u=Math.max(0,n-s)/Math.max(1,n-t);r.push({args:{steps:a,period:i,swing:Math.round(.88*i),step:s_(o.step),firstStep:s_(o.first)},end:s,v:c,late:s>n,score:l+1.5*u+Math.max(0,.5-c)*4})}return{onTime:r.filter(e=>!e.late).sort((e,t)=>e.score-t.score),late:r.filter(e=>e.late).sort((e,t)=>e.end-t.end||e.score-t.score)}}function d_(e,t){let{steps:n,period:r}=e.args,i=Math.round(.8*r),a=n>2&&e.args.firstStep<.62?{firstStep:.62,step:s_((t-.62)/(n-2))}:null;return[{},a,{lift:.06},a&&{...a,lift:.06}].filter(Boolean).map(t=>({...e,args:{...e.args,swing:i,...t}})).filter(e=>e.args.step>=t_&&e.args.step<=n_)}let f_=(e,t,n)=>e.groups.filter(e=>a_.has(e.kind)&&e.last>=t&&e.first<=n).reduce((e,t)=>e+t.count,0);function p_(e,{sk:t,scene:n,trial:r}){let i=[];for(let{i:a,u:o}of c_(e)){let s=JSON.stringify([e.keys,e.use.slice(0,a+1),e.anchors,e.props,e.fits,e.fix,n?.objects??null]);if(o_.has(s)){i.push(o_.get(s));continue}let c=e.use.slice(0,a).filter(e=>e.f0<o.f0),l,u=[];try{let{spec:r,used:i}=Qh({...e,frames:2e4,use:c},{sk:t,scene:n,lenient:!0});l=Tc(r.keys??[],o.f0),u=i.map(e=>e.end??0)}catch{l=null}if(!l?.foot_l||!l?.foot_r)continue;let d=[(l.foot_l[0]+l.foot_r[0])/2,(l.foot_l[2]+l.foot_r[2])/2],f=o.args.to,p=Math.hypot(f[0]-d[0],f[f.length-1]-d[1]),{onTime:m,late:h}=u_(p,o.f0,o.args.arrive),g=t=>{let n=Math.max(t.end,...u)+1;return{schema:e.schema,fps:e.fps??24,frames:n,keys:(e.keys??[]).filter(e=>e.f<n),use:[...c,{...o,args:{...o.args,...t.args}}],...e.anchors?{anchors:e.anchors}:{},...e.props?{props:e.props}:{},...e.fits?{fits:e.fits}:{},...e.fix===void 0?{}:{fix:e.fix}}},_=null,v=0,y=null,b=0,x=[...m.slice(0,6),...m.slice(0,3).flatMap(e=>d_(e,p))].slice(0,18);for(let e of[...x,...h.slice(0,22-x.length)]){let t;try{t=f_(r(g(e)),o.f0,e.end)}catch{t=1/0}if(v++,b+=+!e.late,(!y||t<y.issues)&&(y={c:e,issues:t}),t===0){_=e;break}}if(_??=y?.issues<1/0?y.c:m[0]??h[0],!_)continue;let S=_.args,C=o.args.arrive,w=`use[${a}] walkTo arrive f${C}: steps ${S.steps}, period ${S.period}, swing ${S.swing}, step ${S.step} m, firstStep ${S.firstStep} m${S.lift?`, lift ${S.lift} m`:``} (${s_(_.v)} m/s), ends f${_.end}`,T=m.length?`the ${b} walks tried that end by f${C} pop or miss a foot`:`even the quickest walk over ${s_(p)} m (a step every ${r_[0]} frames, strides up to ${n_} m, 34 frames to start and stop) ends f${h[0]?.end}`,E=_.late?`; f${C} is too soon: ${T}; give arrive >= f${_.end} or a shorter distance`:``,D=y&&y.c===_&&y.issues>0?`; no candidate walked clean (${y.issues} issues in its frames)`:``,O={i:a,args:S,end:_.end,note:`${w}${E}${D} (${v} checked)`};o_.size>=64&&o_.delete(o_.keys().next().value),o_.set(s,O),i.push(O)}return i}function m_(e,t){if(!t.length)return e;let n=e.use.map((e,n)=>{let r=t.find(e=>e.i===n);return r&&e?.skill===`walkTo`?{...e,args:{...e.args,...r.args}}:e});return{...e,use:n}}let h_=e=>Math.round(performance.now()-e);function g_(e,t){let n=structuredClone(e.checks??{}),r=e=>Array.isArray(e)?[Math.round(t(e[0])),Math.round(t(e[1]))]:e;for(let e of[`contacts`,`orient`,`below`])for(let t of n[e]??[])t.when=r(t.when);for(let e of n.clearance??[])e.frames=r(e.frames);return{...e,checks:n}}function __(e,t){if(!e||typeof e!=`object`||Array.isArray(e))return{spec:e,probes:t};let n=wu(e.goals);if(e.probes===void 0&&n===e.goals)return{spec:e,probes:t};let{probes:r,...i}=e,a=r===void 0?t:t?[...t,...Array.isArray(r)?r:[r]]:r;return{spec:n===e.goals?i:{...i,goals:n},probes:a}}function v_(e,{sk:t,scene:n},r={}){let{spec:i,probes:a}=__(e,r.probes),o=performance.now(),s={sk:t,scene:n,lenient:r.lenient===!0},c=[],l=r.checks&&bp({checks:r.checks}).checks,u=[],d=e=>{let{spec:t,used:r}=Qh(bp(m_(e,u)),s),i=Ng(JSON.parse(JSON.stringify(l?{...t,checks:l}:t)),n,c);return Gu(i,s),{full:i,used:r}};u=p_(i,{sk:t,scene:n,trial:e=>{let n=d(e).full;return vp(t,Nl({fps:24,...n},s),n,{explain:!1})}});let f=i?.frames===`auto`&&u.length?{...i,frames:dg(m_(i,u),s).frames}:dg(i,s),p=ng(f),m=f,h=null;p.length&&({spec:m,search:h}=lg(f,p,s,e=>d(e).full)),i?.frames===`auto`&&pg(m);let g=performance.now(),{full:_,used:v}=d(m),y=Nl({fps:24,..._},s),b=h_(g),x=performance.now(),S=y.timeMap?g_(_,y.timeMap):_,C=vp(t,y,S,{userSpec:m}),w=Si(t,y,S.checks),T=ad(y);T&&Object.assign(w,{units:`${w.units}; held: per key, deg = the drawn +Y off the asked one (aim, or rot's +Y), cm = the drawn base off pos, got = the drawn +Y (when 1 deg off or more), rot = what an aim key used`,held:T});let E=e_(t,y),D=Pd(t,y);E&&Object.assign(w,{units:`${w.units}; near: hand (palm, fingertips), foot (sole) to the nearest object within 0.3 m (top group; part: the body at the closest frame), 2 closest runs; gap [min, f, max, f] m, < 0 inside`,near:E}),D&&Object.assign(w,{units:`${w.units}; wrist: deg on the frames a hand holds (a fit, a carry or a grip on); flex + toward the palm, dev + radial / - ulnar, twist at the wrist joint; [min, f, max, f], frames past the comfortable range and at the limit; grip: the grip a keyed carry of a thin body keeps (grip-choice.js), cost [it, today's] (comfort-priced wrist, reach and body over the keys); elevation [max deg, f] of the upper arm (90 level), raised: frames the elbow is over the shoulder`,wrist:D});let O=Cl(t,y);O&&Object.assign(w,{units:`${w.units}; load: what each hand carries, kg, and its most N m (mass x 9.81 x the shoulder-to-wrist distance across) at f; frames [first, last] held`,load:O});let k=v.filter(e=>e.paced).map(e=>`${e.skill} paced ${e.paced.toFixed(2)} to fit f${e.f0}..f${e.end}`);k.length&&(w.paced=k);let A=v.filter(e=>e.cut).map(e=>`${e.skill}: ${e.cut}`);A.length&&(w.cut=A);let j=v.filter(e=>e.placed).map(e=>`${e.skill} f${e.f0}: ${e.placed.why}: at [${e.placed.at.join(`, `)}], heading ${e.placed.heading}`);j.length&&(w.placed=j),u.length&&(w.planned=u.map(e=>e.note));let M=Cg(y,{used:v,spec:_}),N=a&&Gg(y,a,{spec:_,sk:t,scene:n,notes:c});return{out:y,qa:C,observations:w,spec:m,expanded:_,used:v,fixed:y.fixed,search:h,timeline:M,...N?{probes:N}:{},...c.length?{notes:c}:{},ms:{compile:b,qa:h_(x),total:h_(o)}}}let y_=[-1,1,-2,2],b_=e=>typeof e==`number`&&Number.isFinite(e),x_=e=>dp(e)<=.5*Math.max(Math.abs(e.limit),.01);function S_(e,t){let n=/\buse\[(\d+)\] (\w+)/.exec(e),r=Number(n?.[1]),i=n&&t.use?.[r],a=n&&i?.skill===n[2]?Np[i.skill]:null;if(!a?.tune)return[];let o=[];for(let e of[1,2])for(let[t,n]of Object.entries(a.tune)){let s=a.args[t]??{},c=i.args?.[t]??s.default;if(b_(c))for(let a of y_.filter(t=>Math.abs(t)===e)){let e=c+a*n,l=s.type===`integer`?Math.round(e):Math.round(e*1e4)/1e4;!(l>0)||l===c||o.push(i.args?[{op:i.args[t]===void 0?`add`:`replace`,path:`/use/${r}/args/${t}`,value:l}]:[{op:`add`,path:`/use/${r}/args`,value:{[t]:l}}])}}return o}function C_(e,t,n){if(![`SPEED`,`ACC`,`POP`].includes(e.kind))return[];let r=/between keys\[(\d+)\] f(\d+) and keys\[(\d+)\] f(\d+)/.exec(t);if(!r)return[];let[i,a,o,s]=r.slice(1).map(Number),c=n.keys??[];if(c[i]?.f!==a||c[o]?.f!==s)return[];let l=Number.isInteger(n.frames)?n.frames-1:1/0,u=[];for(let e of[1,2])a-e>=0&&u.push([{op:`replace`,path:`/keys/${i}/f`,value:a-e}]),s+e<=l&&u.push([{op:`replace`,path:`/keys/${o}/f`,value:s+e}]);return u}let w_=e=>Math.round(e*1e4)/1e4;function T_(e,t,n){let r=e.keys??[],i=[],a=(e.use??[]).map((e,t)=>({u:e,i:t})).filter(({u:e})=>(e?.skill===`standIdle`||e?.skill===`walkTo`)&&e.f0<=t).sort((e,t)=>e.u.f0-t.u.f0).at(-1),o=r.map((e,t)=>({k:e,j:t})).filter(({k:e})=>Array.isArray(e?.set?.root)&&e.set.root.length===3&&b_(e.f)&&e.f>(a?.u.f0??-1));if(o.some(({k:e})=>e.f<=t))for(let e of[0,2])for(let t of[-n,n])i.push(o.map(({k:n,j:r})=>({op:`replace`,path:`/keys/${r}/set/root`,value:n.set.root.map((n,r)=>r===e?w_(n+t*.03):n)})));else if(a){let{u:e,i:t}=a,r=Np[e.skill].place??{},o=e.args?.steps??8;for(let[a,s]of Object.entries(r)){if(a===`step`&&o<=2)continue;let r=e.args?.[a]??Np[e.skill].args[a]?.default,c=n=>[e.args?{op:e.args[a]===void 0?`add`:`replace`,path:`/use/${t}/args/${a}`,value:n}:{op:`add`,path:`/use/${t}/args`,value:{[a]:n}}];if(Array.isArray(r))for(let e of[0,r.length-1])for(let t of[-n,n])i.push(c(r.map((n,r)=>r===e?w_(n+t*s):n)));else if(b_(r))for(let e of[-n,n])r+e*s>.05&&i.push(c(w_(r+e*s)))}}let s=r.map((e,t)=>({f:e?.f,j:t})).filter(({j:e})=>b_(r[e]?.set?.spine_pitch)&&b_(r[e].f)).sort((e,t)=>e.f-t.f),c=[s.filter(e=>e.f<=t).at(-1),s.find(e=>e.f>t)].filter(Boolean).map(e=>e.j);for(let e of c.length?[n,-n]:[])i.push(c.map(t=>({op:`replace`,path:`/keys/${t}/set/spine_pitch`,value:r[t].set.spine_pitch+e*5})));return i}let E_=[10,20,30,-10];function D_(e,t){let n=e.keys??[],r=Math.round(t),i=Number.isInteger(e.frames)?e.frames-1:1/0,a=n.filter(e=>b_(e?.f)&&b_(e?.set?.spine_pitch)).sort((e,t)=>e.f-t.f),o=a.filter(e=>e.f<r).at(-1),s=a.find(e=>e.f>r),c=o?.set.spine_pitch??0,l=n.findIndex(e=>e?.f===r&&e.set&&typeof e.set==`object`);return E_.map(e=>{let t=c+e,a=[l>=0?{op:n[l].set.spine_pitch===void 0?`add`:`replace`,path:`/keys/${l}/set/spine_pitch`,value:t}:{op:`add`,path:`/keys/-`,value:{f:r,set:{spine_pitch:t}}}];return(!o||r-o.f>12)&&r-12>=0&&a.push({op:`add`,path:`/keys/-`,value:{f:r-12,set:{spine_pitch:c}}}),(!s||s.f-r>12)&&r+12<=i&&a.push({op:`add`,path:`/keys/-`,value:{f:r+12,set:{spine_pitch:s?.set.spine_pitch??c}}}),a})}function O_(e,t){if(typeof e.cause!=`string`||e.kind===`WRIST`&&e.cause.startsWith(`fix.self`))return[];if(e.kind===`WRIST`&&b_(e.worstFrame))return[...D_(t,e.worstFrame),...[1,2].flatMap(n=>T_(t,e.worstFrame,n)).filter(e=>e.every(e=>e.path.endsWith(`/spine_pitch`)))];let n=!e.patch&&x_(e)?[...S_(e.cause,t),...C_(e,e.cause,t)]:[];return e.kind!==`GOAL`||!b_(e.worstFrame)||dp(e)>.1?n:[...n,...T_(t,e.worstFrame,1),...T_(t,e.worstFrame,2)]}let k_=e=>e?.qa?.groups??[],A_=e=>k_(e).reduce((e,t)=>e+(t.severity??0),0),j_=e=>(e?.qa?.goals??[]).filter(e=>!e.ok).length,M_=(e,t)=>k_(e).find(e=>hp(e)===t),N_=e=>(e?.qa?.goals??[]).filter(e=>e.ok).map(e=>e.name),P_=(e,t)=>A_(e)<A_(t)-1e-9&&j_(e)<=j_(t)&&N_(t).every(t=>N_(e).includes(t));function F_(e,t,n){let r=Math.min(8,Math.max(1,Math.round(Number(t?.rounds??4))||4)),i=performance.now(),a=()=>performance.now()-i>=2e4,o=e,s=n(e),c=1,l=0,u=0,d=[],f=e=>{try{let t=Wf(o,e);return c+=1,{next:t,r:n(t)}}catch{return l+=1,null}},p=(e,t,n,{next:r,r:i})=>{d.push({kind:e.kind,subject:e.subject,patch:n,severity:[e.severity,M_(i,t)?.severity??0]}),o=r,s=i},m=e=>{let t=null;for(let n of O_(e,o)){if(a())break;let e=f(n);if(e&&(P_(e.r,t?.r??s)?(t&&(l+=1),t={...e,patch:n}):l+=1,t&&A_(t.r)===0&&!j_(t.r)))break}return t&&p(e,hp(e),t.patch,t),!!t};for(;u<r&&!a();){let e=k_(s).filter(e=>e.patch).slice(0,3).map(hp),t=k_(s).filter(e=>O_(e,o).length).slice(0,2).map(hp);if(!e.length&&!t.length)break;u+=1;let n=0;for(let t of e){let e=M_(s,t);if(!e?.patch||a())continue;let r=f(e.patch);r&&P_(r.r,s)?(p(e,t,e.patch,r),n+=1):r&&(l+=1)}for(let e of n?[]:t){let t=M_(s,e);if(t&&!a()&&m(t)){n+=1;break}}if(!n)break}return{spec:o,result:s,repair:{rounds:u,compiles:c,applied:d,rejected:l}}}function I_(e,n,r={position:[0,0,0],yaw:0},i={}){let a=n.frames.length,o=e.names.length,s=new Float32Array(a*o*4),c=new Float32Array(a*3),l=_(-(r.yaw??0));for(let i=0;i<a;i++){let a=n.frames[i].s,{quats:u,hips:d}=k(e,{P:a.P.map(e=>p(l,t(e,r.position))),R:a.R.map(e=>f(l,e))});u.forEach((e,t)=>s.set(e,(i*o+t)*4)),c.set(d,i*3)}let u={},d=n.f0??0;for(let[e,t]of Object.entries(n.ctx.spec.objects??{})){let r=K(t),i=ls(t),o=r||i,s=!us(t),c=s?new Float32Array(a*3):null,l=s?new Float32Array(a*4):null,f=o||!s||$a(t),p=f&&t.visible?new Uint8Array(a):null,m=o?new Uint8Array(a):null,h=f&&t.level?new Float32Array(a):null,[g,_]=r?To(n,t):[0,0],v=r&&Mo(n,e)?new Uint8Array(a):null;for(let r=0;r<a;r++){if(p&&(p[r]=+((n.baked?.[t.visible]?.[r+d]??0)>.5)),h&&(h[r]=Math.max(0,Math.min(4,n.baked?.[t.level]?.[r+d]??1))),!s)continue;let a=Io(n,e,r+d);a&&(c.set(a.pos,r*3),l.set(S(a.R),r*4),m&&(m[r]=i||r+d>=g&&r+d<=_?1:0),v&&(v[r]=+(r+d>_)))}u[e]={...s?{pos:c,quat:l}:{},...p?{vis:p}:{},...m?{hold:m}:{},...v?{world:v}:{},...h?{lvl:h}:{}}}return{schema:`cozyclay.take.v3`,fps:n.fps,frameCount:a,rig:e.model,bones:e.names.slice(),rot:s,hips:c,objects:u,placement:r,...i}}let L_=(e,t)=>{throw Object.assign(Error(t),{code:e})};function R_(e){try{return e()}catch(e){throw/^MOTION_SPEC_INVALID/.test(e.message)&&L_(`INVALID_ARGUMENT`,e.message.replace(/^MOTION_SPEC_INVALID:\s*/,``)),e}}let z_=e=>Array.isArray(e)?e.map(z_):e&&typeof e==`object`&&!ArrayBuffer.isView(e)?Object.fromEntries(Object.keys(e).map(t=>[t,z_(e[t])])):e;function B_({model:e,scene:t,spec:n,probes:r,checks:i,lenient:a,repair:o,bake:s}){let c=kn(e),l={...i?{checks:i}:{},...a?{lenient:!0}:{}},u=e=>{let{spec:n,probes:i}=__(e,r);n?.fps!==void 0&&n.fps!==24&&L_(`INVALID_ARGUMENT`,`spec.fps must be 24, the timeline rate.`);let a={fps:24,...n},o=R_(()=>v_(a,{sk:c,scene:t},{...l,probes:i}));return{...o,stored:o.spec===a?n:o.spec}},d=performance.now(),f=o?F_(n,o,u):{spec:n,result:u(n)},p=f.result,m=f.spec?.label??null,h=f.repair?{...p.ms,repair:Math.round(performance.now()-d)}:p.ms,g=s?I_(c,p.out,{position:s.position,yaw:s.yaw},{spec:p.stored,prompt:(`prompt`in s?s.prompt:m)??``}):void 0,_=g&&Mn(t,[p.expanded,p.stored]);return _&&(g.people=_),{stored:p.stored,qa:p.qa,observations:p.observations,used:p.used,fixed:p.fixed,search:p.search,timeline:p.timeline,...p.probes?{probes:p.probes}:{},...p.notes?{notes:p.notes}:{},ms:h,frameCount:p.out.frameCount,label:m,...f.repair?{repair:f.repair}:{},...g?{take:g}:{}}}function V_(e,t=new Set){if(ArrayBuffer.isView(e))t.add(e.buffer);else if(e&&typeof e==`object`)for(let n of Object.values(e))V_(n,t);return[...t]}function H_({id:e,job:t}){try{let n=B_(z_(t));return{message:{id:e,value:n},transfer:V_(n)}}catch(t){return{message:{id:e,error:{message:String(t?.message??t),code:t?.code??null,name:t?.name??`Error`}},transfer:[]}}}self.onmessage=({data:e})=>{let{message:t,transfer:n}=H_(e);self.postMessage(t,n)}})();