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
@@ -0,0 +1,6 @@
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 R(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 z(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 te={elbow:[145,150,F],knee:[150,160,I]};function ne(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=te[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 B(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 V(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 re(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 ie(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}=V(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 ae={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]}},oe={spherical:{Index:9,Middle:2,Ring:-5,Pinky:-12}};function se(e,t,n){let r=ae.relaxed;return r[t][n]+Object.entries(e??{}).reduce((e,[i,a])=>e+(a||0)*((ae[i]?.[t]?.[n]??r[t][n])-r[t][n]),0)}let ce=(e,t)=>ae.fist[e][t];function le(e,t,n,r){let i=n===`Left`?-1:1,a=(e,t)=>se(r,e,t);for(let i of[`Index`,`Middle`,`Ring`,`Pinky`]){let a=Object.entries(r??{}).reduce((e,[t,n])=>e+(n||0)*(oe[t]?.[i]??0),0),o=e.index[`${n}Hand${i}1`];a&&o!==void 0&&O(e,t,o,t.P[o].slice(),y(V(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}=V(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)*(H[n]?.[e]??0),0));o.some(Boolean)&&ue(e,t,n,o)}let H={fist:[-70,-5,5,80],grip:[-30,-30,30,10],spherical:[-40,-20,20,10]};function ue(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}=V(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 de=(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]],fe=e=>[-e[0],-e[1],-e[2],e[3]],pe=(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2]+e[3]*t[3];function me(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 he(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 ge(e){let t=Math.hypot(e[0],e[1],e[2]);if(t<1e-12)return me([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 _e=(e,t)=>de(de(e,[t[0],t[1],t[2],0]),fe(e)).slice(0,3);function ve(e,t){let n=1+e[0]*t[0]+e[1]*t[1]+e[2]*t[2];if(n>1e-9)return me([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 ye=(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)]},be=(e,t,n)=>de(e,ge(he(de(fe(e),t)).map(e=>e*n))),xe={"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)},Se=new Map;function Ce(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 we(e){return Se.has(e)||Se.set(e,Ce(xe[e])),Se.get(e)}let Te=.1,Ee=new Map;function De(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 Oe(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 ke(e){if(!xe[e.model])return null;if(Ee.has(e.model))return Ee.get(e.model);let t=we(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:De(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,Oe(e,+t)]))};return Ee.set(e.model,i),i}function Ae(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 Te;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 je=new WeakMap;function Me(t,n){let r=je.get(n);return r||(r=t.grids.map(t=>e(n.P[t.b],p(n.R[t.b],t.centre))),je.set(n,r)),r}function Ne(e,n,r,i=null){let o=Te,s=-1,c=Me(e,n);return e.grids.forEach((l,u)=>{if(i?.has(l.b)||a(t(r,c[u]))-l.reach>o)return;let d=Ae(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 Pe(r,i,o,s=null,c=.01){let l=[0,1,2].map(n=>{let a=[0,0,0];return a[n]=c,Ne(r,i,e(o,a),s).d-Ne(r,i,t(o,a),s).d}),u=a(l);return u>1e-9?n(l,1/u):null}let Fe=e=>/^(Left|Right)Arm$/.test(e)?.5:/^(Left|Right)UpLeg$/.test(e)?.3:0,Ie=(e,t)=>/Hand$/.test(e.names[t])?e.index[e.names[t].replace(/Hand$/,`HandMiddle1`)]:e.children[t][0];function Le(r,i,a,o){let s=r.radii[o],c=Ie(i,o);if(!s||c===void 0)return[];let l=a.P[o],u=t(a.P[c],l),d=Fe(i.names[o]);return s.filter(([e])=>e>=d).map(([t,r])=>({p:e(l,n(u,t)),r,t}))}let Re=/^(Spine\d?|Neck|Head)$/,ze=(e,t)=>e.children[t].find(t=>Re.test(e.names[t]))??Ie(e,t),Be=.25;function Ve(i,o,s,l,u,d=null){let f=Ne(i,s,l,d);if(f.d>=u||f.bone<0)return l;let p=s.P[f.bone],m=ze(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)):Pe(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=>Ne(i,s,e(l,n(y,t)),b).d>=u,S=0,C=.02;for(;!x(C)&&C<Be;)S=C,C=Math.min(Be,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 He=.03;function Ue(e){let t=ke(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 We(r,i,a,o,s,c){let l=0,u=Fe(i.names[o]);for(let[i,d]of r.radii[o]??[]){if(i<u)continue;let{d:f,bone:p}=Ne(r,a,e(s,n(t(c,s),i)),r.skip.get(o));p>=0&&(l=Math.max(l,d-f))}return l}function Ge(e,t,n,r){let i={depth:0,p:null,into:-1};for(let a of Le(e,t,n,r)){let{d:t,bone:o}=Ne(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 Ke={twist:[-85,85],flex:[-70,70],dev:[-35,20]},qe={twist:[-50,50],flex:[-40,40],dev:[-25,15]},Je=[0,0,-1],Ye=100/(180/Math.PI),Xe=180/Math.PI;function Ze(e,t=8,n=400){let r=0;for(let i of[`twist`,`flex`,`dev`]){let a=e[i],o=a>=0?qe[i][1]:-qe[i][0],s=a>=0?Ke[i][1]:-Ke[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 Qe(e,n){let r=s(t(n,e)),i=Math.acos(c(-r[1],-1,1))*Xe;return 2*(Math.max(0,i-70)/30)**2}function $e(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=ge(n(o,u)),h=he(de(l,fe(d))),g=p(C(d),c),_=s(t(g,n(o,r(g,o)))),v=i(o,_);return{twist:u*Xe,flex:r(h,_)*Xe,dev:r(h,v)*Xe,axF:_,axD:v}}function et(e,t=0){let n=0;for(let[r,[i,a]]of Object.entries(Ke)){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 tt(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),R=e(T,n(A,j)),z=o.restWorldRot,te=p(m(z[C]),t(o.restWorldPos[w],o.restWorldPos[C])),ne=p(m(z[C]),_),B=f(m(z[C]),z[w]),re=p(m(l.R[w]),V(o,l,u).l),ie=r=>{let i=e(n(I,Math.cos(r)),n(L,Math.sin(r))),a=t(R,e(T,e(n(A,M),n(i,N)))),o=f(x(te,ne,a,i),B),c=$e(o,d,s(a),p(o,re)),l=Math.abs(Math.atan2(Math.sin(r),Math.cos(r)))/Ye,u=e(T,e(n(A,M),n(i,N)));return{pv:i,need:c,elbow:u,cost:(y?Ze(c)+Qe(T,u):et(c,v))+.5*(1-Math.cos(r))+400*Math.max(0,l-1)**2}},ae=e=>a(P(e))>1e-6?Math.atan2(r(e,L),r(e,I)):0;return{at:ie,angleOf:ae,softmin:()=>{let t=Array.from({length:72},(e,t)=>ie(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?ae(i):2*Math.PI*t.findIndex(e=>e.cost===r)/72},miss:Math.max(0,k-j)}}function nt(e,t,n,r,i,a,o,{toward:s,w:c=1,margin:l=0,comfort:u=!1}={}){let d=tt(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 rt=(e,[t,n])=>Math.min(n,Math.max(t,e));function it(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,..._}=$e(d,l,u,p(f(d,m(i.R[s])),V(r,i,o).l)),v=Object.fromEntries(Object.entries(_).map(([e,t])=>[e,rt(t,Ke[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 at(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,Je)}function ot(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(be(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:at(r,i,a),wristAt:l===`wrist`?()=>_:l===`palm`?v:r=>e(_,n(t(v(r),_),c))}}function st(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}=ot(e,t,n,r.palm,r.measure(t),d,r.pin),h=nt(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);_=R(e,t,n,a),g=ne(e,t,c,l,u,a,h.pole,i,`elbow`),v=it(e,t,n,u,l,f)}a[`hold_${o}`]={w:d,miss:Math.max(0,g),...v,swivel:dt(t,c,l,u,p),..._>145?{fold:_}:{},...r.second?{second:r.second}:{}},a[`arm_miss_${o}`]*=1-d}function ct(e,t,n,r){let{goal:i,plane:a,wristAt:o}=ot(e,t,n,r.palm,r.measure(t),r.w);return tt(e,t,n,i,o(i),a,Je,0,r.comfort)}function lt(e,n,r,i,o,{shoulder:s=!1}={}){let{goal:c,plane:l,wristAt:u}=ot(e,n,r,i,o,1),d=u(c),f=nt(e,n,r,c,d,l,Je,{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?nt(e,n,r,c,d,l,Je,{comfort:!0}):f;return(s?Ze(v.need)+Qe(m,v.elbow)+ut(e,n,r,m,v.elbow,d):f.cost)+1*((f.miss*100)**2+(_*100)**2)}function ut(e,t,n,r,i,a){let o=ke(e),s=e.index;return o?4*(Math.max(We(o,e,t,s[`${n}Arm`],r,i),We(o,e,t,s[`${n}ForeArm`],i,a))*100)**2:0}function dt(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 ft=[[`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]]})],pt=(r,i,a=9)=>Array.from({length:a},(o,s)=>e(r,n(t(i,r),s/(a-1))));function U(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 mt(i,o,s,l){let u=1/0,d=t(l,s),f=r(d,d);for(let l of pt(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 ht(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 gt=e=>Math.round(e.restWorldPos[e.index.LeftFoot][1]*1e3)/1e3,_t=(e,t,n,r)=>r?A(t,n,p(m(e.restWorldRot[n]),[0,-r,0])):t.P[n];function vt(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:_t(e,n,c,o.ball),tip:_t(e,n,l,o.tip)}}function yt(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 bt(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 xt=(e,n)=>{let r=p(m(e.R),t(n,e.pos));return r[1]-=e.half[1],r},St=t=>e(t.pos,p(t.R,[0,t.half[1],0]));function Ct(e,n){let r=bt(e);if(r.kind===`sphere`)return a(t(n,St(e)))-r.r;if(r.kind===`cylinder`){let t=xt(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 U(n,e)}function wt(r,i){let o=bt(r);if(o.kind===`sphere`){let e=t(i,St(r)),o=a(e);return o>1e-12?n(e,1/o):p(r.R,[0,1,0])}if(o.kind===`cylinder`){let t=xt(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 yt(i,r)}function Tt(e,r){return t(r,n(wt(e,r),Ct(e,r)))}function Et(e,t,n,r){let i=-1/0;for(let a of pt(t,n))i=Math.max(i,r-Ct(e,a));return i}let Dt=(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&&Ot(n,r));function Ot(e,t){let n=Object.entries(e?.objects??{}).filter(([e,n])=>e===t||n.ancestors?.includes(t));return n.length===1&&bt(n[0][1]).kind!==`box`}let kt=new WeakMap;function At(r,i,a,o,s,c){kt.has(a)||kt.set(a,new Map);let l=kt.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=q[d.hand],h=D(r),g=Ps(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=jt(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,...Y(r,Fs(b,e))};for(let[e,n,r]of y)i=Math.max(i,Et(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 jt(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 Mt(e,t){let n=Math.min(1,Math.max(0,(t-e)/4));return n*n*(3-2*n)}let Nt=(r,i,a)=>({pos:e(r.pos,n(t(i.pos,r.pos),a)),R:i.R}),Pt=[`schema`,`fps`,`frames`,`label`,`keys`,`anchors`,`props`,`fits`,`objects`,`generators`,`checks`,`fix`,`use`,`channels`,`detourRadius`,`search`,`goals`,`flow`,`idle`],Ft=[`f`,`set`,`step`,`src`],It=[`skill`,`args`,`f0`],Lt={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`]},Rt=[`hand`,`origin`,`points`],zt=[`side`,`anchor`,`effector`,`offset`,`orient`],Bt={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`]},Vt=[`at`,`onto`,`bounce`,`tumble`,`v`],Ht=[`f`,`pos`,`rot`,`aim`,`roll`,`lean`],Ut=[`type`,`f0`,`steps`,`step`,`period`,`swing`,`halfWidth`,`lift`,`heading`,`turnDistance`,`pelvisHigh`,`pelvisLow`,`firstStep`,`armSwing`,`vary`],Wt=[`contacts`,`orient`,`below`,`clearance`,`appear`,`supports`,`allow`,`limits`,`mouthPoints`],Gt={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`]},Kt=[`name`,`near`,`above`,`moved`,`still`,`speed`,`tol`,`by`,`at`,`mode`,`after`,`before`],qt=[`name`,`distance`,`position`,`frames`,`every`],Jt=[`joint`,`bone`,`prop`,`anchor`,`landmark`,`object`,`person`],Yt=e=>e.flatMap(e=>[`${e}_l`,`${e}_r`]),Xt=new Set([`root`,...Yt([`foot`,`knee`,`hand`,`elbow`])]),Zt=new Set(Yt([`knee`,`elbow`])),Qt=new Set([`yaw`,`pitch`,`roll`,`spine_yaw`,`spine_pitch`,`spine_roll`,`head_yaw`,`head_pitch`,`head_roll`,`arm_rest`,`arm_hang`,...Yt([`fyaw`,`fpitch`,`armw`,`wtwist`,`wflex`,`aswing`,`abend`])]),$t=new Set(Yt([`fingers`])),en=[`relaxed`,`fist`,`grip`,`cigarette`,`point`,`spherical`],tn=[`power`,`pinch`,`hook`,`palm`,`spherical`],nn={power:`grip`,pinch:`cigarette`,hook:`grip`,palm:`relaxed`,spherical:`spherical`},rn={power:[0,0,.02],hook:[0,0,.02],pinch:[.025,0,.01],palm:[-.045,0,.015],spherical:[-.015,0,.035]},an=[[/^(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`]],on=e=>an.find(([t])=>t.test(e))??null,sn={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)`};sn.root,`${sn.yaw}`,sn.foot,sn.fyaw,`${sn.pole}`,sn.hand,`${sn.pole}`;let cn=[[[`frames`,`when`,`range`,`f`,`from`,`within`,`during`,`at`],[`when`,`frames`,`at`,`f`]],[[`max`,`maxDist`,`distance`,`tolerance`,`limit`],[`tol`,`min`]]];function ln(e,t){for(let[n,r]of cn){let i=n.includes(e)&&r.find(n=>t.includes(n)&&n!==e);if(i)return`; did you mean "${i}"?`}return G(e,t,Math.max(2,Math.floor(e.length/3)))}let un=e=>Array.isArray(e)&&e.length===3&&e.every(Number.isFinite),W=e=>JSON.stringify(e)?.slice(0,60);function dn(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 G(e,t,n=Math.max(3,Math.floor(e.length/3))){let r=null,i=1/0;for(let n of t){let t=dn(e,n);t<i&&(r=n,i=t)}return r&&i<=n?`; did you mean "${r}"?`:``}let fn={pelvis:`Hips`,hip:`Hips`,hips:`Hips`,root:`Hips`,chest:`Spine2`,upperchest:`Spine2`,torso:`Spine1`,belly:`Spine`,abdomen:`Spine`,neck:`Neck`,head:`Head`},pn={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`},mn={l:`Left`,left:`Left`,r:`Right`,right:`Right`},hn=e=>e.toLowerCase().replace(/[\s_.-]+/g,``);function gn(e,t){let n=t.toLowerCase();return e.names.find(e=>e.toLowerCase()===n)??null}function _n(e,t){let n=hn(t);if(fn[n])return gn(e,fn[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?gn(e,`${mn[o]}${pn[hn(s)]??hn(s)}`):null}function vn(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=gn(e,t)??gn(e,n)??_n(e,n);return r?{bone:r,hint:``}:{bone:null,hint:G(n,e.names)}}function yn(e,t,n,r){return!t||t.index[r]!==void 0||(e(`${n} names unknown bone "${r}"${G(String(r),t.names)}`),!1)}function bn(e,t,n,r,{groups:i=!1}={}){let a=Object.keys(t?.objects??{});if(!a.length||a.includes(r))return!0;let o=K(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`):t?.others?.[r]?e(`${n} names "${r}", a ${t.others[r].renderer??`record`} with no body (no size to stand on, hold or hit); name a scene body${G(String(r),a)}`):e(`${n} names object "${r}", which is not in the scene${G(String(r),[...new Set([...a,...a.flatMap(e=>t.objects[e].ancestors??[]),...Object.keys(t.others??{})])])}`),!1)}function xn(e,t,n,r,i){let[a,o]=String(i).split(/:(.*)/s);if(a===`joint`)return yn(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 Sn=new WeakMap;function Cn(e){let t=e?.objects??{};if(Sn.has(t))return Sn.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 Sn.set(t,n),n}let wn={"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}}}},Tn=new Map;function En(e){return wn[e]?(Tn.has(e)||Tn.set(e,E(wn[e])),Tn.get(e)):null}function Dn(e,t,n,r){let i=e?.people?.[t];if(!i)return null;let a=i.names.indexOf(vn(En(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 On=e=>Object.entries(e?.people??{}).map(([e,t])=>`${e} (${t.name})`).join(`, `)||`none: no other character has an installed take`;function kn(e,t){let n=new Map,r=(t,r)=>{let i=e?.people?.[t],a=i&&vn(En(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 An=.045,jn=[[1,0,0],[0,1,0],[0,0,1]],Mn=(e,i)=>t(e,n(i,r(e,i)));function Nn(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]<An)?t:-1}let Pn=(e,t)=>e===`cylinder`&&!!t&&t[1]<An&&Math.min(t[0],t[2])>=An&&Math.abs(t[0]-t[2])<.1*t[0],Fn=(e,t)=>e?.objects?.[t]?.grasp??e?.others?.[t]?.grasp??null;function In(e,t){let n=[],r=(t,r)=>Object.entries(Fn(e,t)??{}).forEach(([e,i])=>n.push({...i,name:r+e,owner:t}));r(t,``);for(let i of K(e,t)){i!==t&&r(i,`${i}.`);let{half:a,renderer:o}=e.objects[i],s=Nn(a);s>=0?n.push({name:i,owner:i,at:[0,a[1],0],axis:jn[s],radius:Math.min(...a.filter((e,t)=>t!==s))}):Pn(o,a)&&n.push({name:i,owner:i,at:[0,a[1],0],rim:a[0],normal:jn[1]})}return n}let Ln=(e,t,n)=>In(e,t).find(e=>e.name===n);function Rn(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 zn(r,o){let c=Mn(t(o,r.p),r.normal),l=a(c)>1e-6?s(c):s(Mn(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 Bn(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 Vn(e,n){let i=t(n,e.p);return e.rim?Math.hypot(r(i,e.normal),a(Mn(i,e.normal))-e.rim):a(e.axis?Mn(i,e.axis):i)}function Hn(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(Mn(o.axis,o.from))<.001)){if(o.from){let e=f(o.from),t=s(Mn(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 Wn(i(c,e),c,e)}let t=Un(l,o.axis),a=p(_(u),[d?1:-1,0,0]),c=Math.abs(t[1]),m=o.thumb??Mn(e(n([0,1,0],1-c),n(a,c)),t),h=s(Mn(o.axis,t)),g=r(h,m)>=0?h:n(h,-1);return Wn(t,g,i(t,g))}let m=f(o.from??n(l,-1)),h=Un(l,m);return Wn(h,i(m,h),m)}function Un(e,t){let n=Mn(e,t);return s(a(n)>.001?n:Mn(Math.abs(t[1])<.9?[0,1,0]:[1,0,0],t))}let Wn=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]];function Gn(e,n){let r=q[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 Kn=new Set([`body`]);function qn(e,t,n,r,i,a){let o=e.spec.anchors?.[t];if(!o)throw Error(`MOTION_SPEC_INVALID: unknown anchor "${t}"`);let s=Qn(e,o,t,n,r,i,a),c=Kn.has(o.kind)&&e.keepOut;return c?Ve(c.body,e.sk,n,Ve(c.body,e.sk,n,s,He,c.skip),He,c.skip):s}let Jn=[`r`,`l`],Yn={l:`left`,r:`right`};function Xn(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]??Cn(e.scene)[t.object],l=e.objectPose&&s!==void 0?e.objectHand(t.object,s):null;if(o&&l&&Jn.indexOf(l)>Jn.indexOf(o))throw Error(`MOTION_SPEC_INVALID: anchor "${n}" is on "${t.object}", which the ${Yn[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():Zn(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 Zn(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 Qn(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[`${q[i.reach]}Arm`]],p=.95*Gn(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 er(r,o,s,c,l,u).p;let t=Xn(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=$n(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=Dn(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: ${On(r.scene)})`);return e(t,i.offset??[0,0,0])}default:throw Error(`MOTION_SPEC_INVALID: anchor "${o}" has unknown kind "${i.kind}"`)}}function $n(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(!Os(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=Zn(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 er(i,o,c,l,u,d){let f=i.spec.anchors[o],m={s:c,diag:l,c:u},h=e=>Xn(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=In(i.scene,f.object),t=i.sk.index,n=g??c.P[t[d?`${q[d]}Arm`:`Hips`]],r=f.socket?Ln(i.scene,f.object,f.socket):e.map(e=>({k:e,d:Vn(Rn(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=_?Rn(_,h(_.owner)):{p:g??h(f.object).pos};if(_?.rim)v=tr(i,o,d,_,h(_.owner),v,g??c.P[i.sk.index[d?`${q[d]}Arm`:`Hips`]]);else if(g)v.p=g;else if(v.axis&&v.radius>0){let o=Math.abs(rn[f.grasp]?.[2]??0),l=v.radius-o,u=c.P[i.sk.index[`${q[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=Bn(K(i.scene,f.object).map(e=>({...h(e),half:i.scene.objects[e].half})),v.p)),v}function tr(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=zn(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 Rn(e.rims.get(c),a)}function nr(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=V(o,i,q[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 rr={joint:`joint:<Bone>`,bone:`bone:<Bone>`,prop:`prop:<prop>.<point>`,anchor:`anchor:<name>`,landmark:`landmark:mouth`,object:`object:<id>[.center|.top|.base|.rim]`,person:`person:<id>.<Bone>`};function ir(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|rim)$/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 ar(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 or(t){let n=(t??[]).map(t=>({o:e(t.pos,p(t.R,[0,2*t.half[1],0])),n:[t.R[0][1],t.R[1][1],t.R[2][1]],r:Math.min(t.half[0],t.half[2])}));return n.length?n.reduce((e,t)=>t.o[1]>e.o[1]?t:e):null}function sr(n,r){let i=t(r,n.o),o=i[0]*n.n[0]+i[1]*n.n[1]+i[2]*n.n[2],s=t(i,n.n.map(e=>e*o)),c=a(s);return c>1e-6?e(n.o,s.map(e=>e*n.r/c)):n.o}function cr(e,t,n){let r=e.frames?.[t-(e.f0??0)],i=ir(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}=vn(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]?qn(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=ns(e,i.name,t);return n?i.point===`rim`?or(n).o.slice():ar(n,i.point):null}case`person`:return Dn(e.ctx.scene,i.name,i.point,t);default:return null}}function lr(e,n,r,i){let o=e=>{let t=ir(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=cr(e,n,s[0]),r=ns(e,s[1].name,n);return t&&r?Math.max(0,Math.min(...r.map(e=>U(t,e)))):null}let c=[r,i].map(e=>{let t=ir(e);return t?.kind===`object`&&t.point===`rim`?t:null});if(c[0]||c[1]){let[o,s]=c[0]?[c[0],i]:[c[1],r],l=cr(e,n,s),u=or(ns(e,o.name,n));return l&&u?a(t(l,sr(u,l))):null}let l=cr(e,n,r),u=cr(e,n,i);return l&&u?a(t(l,u)):null}function ur(e,{sk:t,scene:n},r,i=Jt){if(typeof r!=`string`||!r)return[];let a=r.replace(/\.(center|top|base|rim)$/,``),[o,s]=r.split(`.`),c=t&&(i.includes(`joint`)||i.includes(`bone`))?vn(t,r).bone:null;return[i.includes(`object`)&&(n?.objects?.[a]||K(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)}function dr(e,t,n,r){let i=ur(e,t,n,r);return i.length?`; did you mean ${i.map(e=>`"${e}"`).join(` or `)}?`:``}function fr(e,t,{sk:n,scene:r},i,a,o=Jt){let s=ir(a),c=`${i} "${a}"`;if(!s||!o.includes(s.kind))return t(`${c} must be ${o.map(e=>rr[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?vn(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${G(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=!!Dn(r,s.name,s.point,0);return e||t(`${c}: no person "${s.name}" with joint "${s.point}" (people with an installed take: ${On(r)})`),e}return bn(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:cr(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=cr(t,a,n.follow);if(!o)return null;let s=C(ve(pr,mr(t,a,n.aim,o)));return{pos:e(o,p(s,n.offset??[0,0,0])),R:s}}let gr={spine:1,chest:2.5,head:3.5,aswing:3,abend:5,wrist:9.5,hand:0},_r=[1.5,3],vr=.5,yr={frames:8,min:6,frac:.08,max:3},br=/^(aswing|abend|wflex)_[lr]$/,xr=/^use\[\d+\] (?!standIdle$)|\bwalk(To)? (start|step \d+|end)$/,Sr=e=>xr.test(e?.src??``);function Cr(e){let t=/^(spine|chest|head)_(yaw|pitch|roll)$|^(aswing|abend|wflex|wtwist)_[lr]$|^(hand|armw|elbow)_[lr]$/.exec(e);return t?t[1]??(t[4]?`hand`:t[3]===`wflex`||t[3]===`wtwist`?`wrist`:t[3]):null}function wr(e){let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);let n=Math.imul(t>>>0^t>>>0>>>15,t>>>0|1);return n^=n+Math.imul(n^n>>>7,n|61),((n^n>>>14)>>>0)/4294967296*2-1}function Tr(e,t,n){let r=e.length;for(let e=1;e<r;e++)t[e]=Math.min(t[e],t[e-1]+n(e-1));for(let e=r-2;e>=0;e--)t[e]=Math.min(t[e],t[e+1]+n(e));return t.some(e=>e>0)?{max:Math.max(...t),at(n){if(n<=e[0])return t[0];if(n>=e[r-1])return t[r-1];let i=0;for(;n>e[i+1];)i++;return t[i]+(t[i+1]-t[i])*(n-e[i])/(e[i+1]-e[i])}}:null}function Er(e,t,n,r,i=null){let a=Cr(e);if(!a||!gr[a]||!(n>0)||t.length<2)return null;let o=t.map(e=>e.f),s=o.length;return Tr(o,o.map((e,r)=>Sr(t[r])||Sr(t[r+1])||i&&[o[r-1],e,o[r+1]].some(e=>e!==void 0&&i[e]<2)?0:gr[a]*Math.min(1,(r<s-1?o[r+1]-e:24)/24)*n),e=>(t[e].value===t[e+1].value?vr:.2)*(o[e+1]-o[e]))}function Dr(e,t,n,r,i=null){let a=new Map,o=[],s=(e,t)=>JSON.stringify(e)===JSON.stringify(t);for(let t of e){for(let e of t)(!a.has(e.f)||Sr(e))&&a.set(e.f,e);for(let e=1;e<t.length;e++)s(t[e-1].value,t[e].value)||o.push([t[e-1].f,t[e].f,t.weight])}let c=[...a.values()].sort((e,t)=>e.f-t.f);if(!(n>0)||c.length<2)return null;let l=c.map(e=>e.f),u=l.length,d=l.map((e,a)=>{if(e===0||Sr(c[a])||Sr(c[a+1])||i&&[l[a-1],e,l[a+1]].some(e=>e!==void 0&&i[e]<2))return 0;let o=wr(`${r}|${e}`),s=Math.min(a<u-1?l[a+1]-e:24,a>0?e-l[a-1]:24);return(o<0?`l`:`r`)!==t||s<6?0:Math.min(_r[0]+(_r[1]-_r[0])*Math.abs(o),s/3)*Math.min(1,n)}),f=e=>o.filter(([t,n])=>t<l[e+1]&&n>l[e]),p=e=>l[e+1]-l[e];return Tr(l,d,e=>(f(e).length?f(e).some(e=>e[2])?0:.15:vr)*p(e))}let Or=e=>e<=0||e>=1?0:64*(e*(1-e))**3;function kr(e,t,n,r,i,a=()=>!0){if(!br.test(e)||!(r>0))return null;let o=[],s=t.map(e=>e.f);for(let c=1;c<s.length;c++){let l=n[c]-n[c-1],u=s[c+1];if(Math.abs(l)<1||Sr(t[c])||Sr(t[c-1])||u!==void 0&&n[c+1]!==n[c])continue;let d=Math.min(yr.frames,.5*(s[c]-s[c-1]),u===void 0?1/0:.5*(u-s[c])),f=Math.sign(l)*Math.min(yr.max*(d/yr.frames)**2,yr.frac*Math.abs(l))*Math.min(1,r);/^abend/.test(e)&&n[c]+f<0&&(f=Math.min(0,-n[c])),d>=yr.min&&Math.abs(f)>.001&&s[c]+.75*d<=i&&a(s[c]-d/4,s[c]+.75*d)&&o.push([s[c]-d/4,d,f])}return o.length?e=>{let t=0;for(let[n,r,i]of o)t+=i*Or((e-n)/r);return t}:null}function Ar(e,t){let n=Array(t).fill(1/0);for(let r=0,i=-1/0;r<t;r++)e.some(e=>e[r]>.001)&&(i=r),n[r]=r-i;for(let r=t-1,i=1/0;r>=0;r--)e.some(e=>e[r]>.001)&&(i=r),n[r]=Math.min(n[r],i-r);return n}function jr(e){let t={l:[],r:[]},n=Math.round(e?.frames??0);for(let r of Object.values(e?.objects??{})){let i=e.props?.[r?.prop]?.hand;Number.isFinite(r?.grab)&&t[i]&&t[i].push([r.grab,Number.isFinite(r.release)?r.release:n])}for(let n of e?.goals??[]){let e=Array.isArray(n?.at)?n.at:null;if(e?.length&&[`near`,`above`,`still`].some(e=>n[e]!==void 0)&&/hand/i.test(JSON.stringify(n)))for(let n of[`l`,`r`])t[n].push([e[0],e[e.length-1]])}return t}function Mr(e,t,n,r=6){let i=new Uint8Array(n);for(let[t,r]of e)for(let e=Math.max(0,Math.floor(t));e<=Math.min(n-1,Math.ceil(r));e++)i[e]=1;for(let e of t)for(let t=0;t<n;t++)e[t]>.001&&(i[t]=1);return Ar([i],n).map(e=>Math.max(0,e-r))}function Nr(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 Pr(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 Fr=/^(spine|chest|head)_(yaw|pitch|roll)$|^(aswing|abend)_[lr]$/,Ir={spine:0,head:2,arms:3},Lr=/^(hand|armw|elbow|aswing|abend|wflex|wtwist)_[lr]$/,Rr=e=>/^(spine|chest|head)_/.test(e)?[`l`,`r`]:/^(aswing|abend|wflex|wtwist)_([lr])$/.test(e)?[e.slice(-1)]:[],zr=e=>/^(spine|chest)_/.test(e)?`spine`:/^head_/.test(e)?`head`:/^(aswing|abend)_[lr]$/.test(e)?`arms`:null;function Br(e,t=``,n={l:[],r:[]}){if(e===!1)return{smooth:!1,lag:{spine:0,head:0,arms:0},overlap:0,seed:t,hands:n};let r=e??{},i=r.lag===!1?0:r.lag,a=typeof i==`number`?{spine:i,head:i,arms:i}:{...Ir,...i??{}},o=r.overlap===!1?0:typeof r.overlap==`number`?r.overlap:1;return{smooth:r.smooth!==!1,lag:a,overlap:o,seed:t,hands:n}}function Vr(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=Pr([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 Hr=e=>Array.isArray(e)?`vec`:e&&typeof e==`object`?`map`:`num`;function Ur(e,t,n=Br()){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),src:t.src});let i=n.overlap??0,a=[...r];if(i>0)for(let[e,t]of r)/^spine_/.test(e)&&a.push([e.replace(`spine`,`chest`),t]);let o=new Map,s=new Map,c={},l={};if(i>0){for(let[e,t]of a)/^w_.+_[lr]$/.test(e)&&o.set(e,p(e,t));for(let e of[`l`,`r`])c[e]=Mr(n.hands?.[e]??[],[...o].filter(([t])=>t.endsWith(`_${e}`)).map(([,e])=>e.frames),t);for(let e of[`l`,`r`])l[e]=Dr([...r].filter(([t])=>Lr.test(t)&&t.endsWith(`_${e}`)).map(([e,t])=>Object.assign([...t],{weight:/^armw_/.test(e)})),e,i,n.seed??``,c[e]);for(let[e,t]of a)/^armw_[lr]$/.test(e)&&o.set(e,p(e,t))}let u=e=>{let n=Rr(e).filter(e=>o.has(`armw_${e}`)),r=n.join(``);return n.length?(s.has(r)||s.set(r,Ar(n.map(e=>o.get(`armw_${e}`).frames),t)),s.get(r)):null},d={},f={};for(let[e,t]of a){let n=o.get(e)??p(e,t,u(e));d[e]=n.frames,n.plain&&(f[e]=n.plain)}for(let[e,t]of Object.entries(f)){let n=Rr(e).map(e=>d[`armw_${e}`]).filter(Boolean);n.length&&(d[e]=d[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 d;function p(e,r,a=null){let o=[...new Map(r.map(e=>[e.f,e])).values()],s=Hr(o[0].value),c=s===`vec`?o[0].value.length:0,u=s===`num`?[o.map(e=>e.value)]:s===`vec`?Array.from({length:c},(e,t)=>o.map(e=>e.value[t])):[...new Set(o.flatMap(e=>Object.keys(e.value)))].map(e=>({name:e,y:o.map(t=>t.value[e]??0)})),d=o.map(e=>e.f),f=n.smooth&&Fr.test(e),p=n.lag[zr(e)]??0,m=s===`num`?Er(e,o,i,n.seed??``,a):null,h=Lr.test(e)?l[e.slice(-1)]:null,g=p+(m?.max??0),_=e=>m?Math.min(m.at(e),a?vr*a[Math.round(e)]:1/0):0,v=(e,n)=>Math.min(1,(t-1-e)/(2*n)),y=e=>h?e-h.at(e)*v(e,h.max):e,b=e=>y(e)-(g>0?(p+_(e))*v(e,g):0),x=(e,n)=>{if(!a)return!0;for(let r=Math.max(0,Math.floor(e));r<=Math.min(t-1,Math.ceil(n+g));r++)if(a[r]<2)return!1;return!0},S=(n,r=!0)=>{let a=f&&r?Vr(d,n):Nr(d,n),c=r&&s===`num`?kr(e,o,n,i,t-1,x):null;return e=>{let t=r?b(e):y(e);for(let e=1;e<o.length;e++)if(o[e].step&&t>=d[e-1]&&t<d[e])return n[e-1];return Pr(d,n,a,t)+(c?c(t):0)}},C=Array(t);if(s===`num`){let e=S(u[0]);for(let n=0;n<t;n++)C[n]=e(n)}else if(s===`vec`){let e=u.map(S);for(let n=0;n<t;n++)C[n]=e.map(e=>e(n))}else{let e=u.map(({name:e,y:t})=>[e,S(t)]);for(let n=0;n<t;n++)C[n]=Object.fromEntries(e.map(([e,t])=>[e,t(n)]))}if(!(s===`num`&&(f||g>0)&&Rr(e).length))return{frames:C};let w=S(u[0],!1);return{frames:C,plain:Array.from({length:t},(e,t)=>w(t))}}}let Wr=(e,t)=>Object.fromEntries(Object.entries(e).map(([e,n])=>[e,n[t]])),Gr=.2,Kr=e=>Math.round(e*1e5)/1e5,qr=(e,t)=>2*Math.acos(Math.min(1,Math.abs(pe(e,t)))),Jr=(r,i,a)=>r.lean>0&&r.aim&&a>0?e(r.pos,n(t(s(r.aim),_e(i,[0,1,0])),a)):r.pos;function Yr(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 Xr(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+Gr*qr(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 Zr(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 Qr(e,t,n){let r=e.map((e,r)=>{if(e.aim===void 0)return{fixed:me(e.rot)};let i=me(t(r)),a=e=>{let t=de(ve(s(_e(i,[0,1,0])),e),i);return n=>de(ye(e,n),t)};if(e.lean>0&&n)return{leans:Yr(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=Xr(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(Zr(e=>{let r=i.rolled(e),a=[c[t-1],c[t+1]].filter(Boolean);return n(t,r)+a.reduce((e,t)=>e+Gr*qr(t.q,r)**2,0)},c[t].r-15/2,c[t].r+15/2))).map(Kr);u&&pe(r,u)<0&&(r=r.map(e=>-e)),l.push(r),u=me(r)}return l}function $r(t,r,i){if(!(r>0&&r<=.3))return t;let a=!1,c=t.keys.map(t=>!t.aim||t.lean!==void 0||t.roll!==void 0||!(o(e(t.pos,n(s(t.aim),r)),i(t.f))<.25)?t:(a=!0,{...t,lean:45,leaned:!0}));return a?{...t,keys:c}:t}let ei=1e3,ti=.03,ni=Array.from({length:72},(e,t)=>2*Math.PI*t/72-Math.PI);function ri(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 ii(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+ti),a=i(t,e-ti);o[t][2]=Math.max(.05,(r+a-2*n)/(ti*ti)),u[t]=-(r-a)/(2*ti)});let d=l.map((e,t)=>a(t,e)),f=l.map((e,r)=>n(t(a(r,e+ti),a(r,e-ti)),1/(2*ti)));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*ei*s[i]:2*ei;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=ri(o,u);l.forEach((e,t)=>{l[t]=e+Math.max(-.6,Math.min(.6,p[t]))})}return l}function ai(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 oi(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 si(e,t,n,r,i=ni){let a=ii(e,t,n);if(!ai(a,r,t).length)return a;let o=oi(e,r,a.length,i),s=ii(e,t,o),c=ai(s,r,t);if(!c.length)return s;let l=ii(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 ai(l,r,t).length?a:l}function ci(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 li(e,t,n,r){let i=n.map(n=>{let{s:i,held:a}=r(n);return ct(e,i,t,a)}),a=si((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 ui=ft.filter(e=>e[0].startsWith(`torso`)),di=ft.find(e=>e[0]===`hand_l`)[3];function fi(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 pi(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 mi(e,t,n,r,i=()=>null){let a=e.index,o=Object.entries(t.ctx.spec.objects??{}).filter(([,e])=>js(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 ui){let c=new Map;for(let[e,t,n]of u)c.set(e,Math.max(c.get(e)??-1/0,s+n-fi(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*di-mt(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=pi(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=>U(e,s)));c>n.object&&r(`OBJECT`,`${t} into ${a}`,e,c,n.object)}}}}let hi=[[/^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 gi(e,t){let n=e.names[t]??``,r=/^Left/.test(n)?`_l`:/^Right/.test(n)?`_r`:``,i=hi.find(([e])=>e.test(n));return i?`${i[1]}${r}`:n}let _i=[`Arm`,`ForeArm`,`Hand`].flatMap(e=>[`Left${e}`,`Right${e}`]);function vi(e,t,n,r,i){let a=ke(e);if(!a)return;let o=_i.map(t=>e.index[t]);t.frames.forEach(({s:t},s)=>{for(let c of o){let o=Ge(a,e,t,c);if(o.into<0)continue;let l=gi(e,c),u=gi(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 yi=new WeakMap;function bi(e){let t=yi.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)})),yi.set(e,t)),t}function xi(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 Si(e,t,n){let r=0;for(let i of bi(e.objectsAt(t))){let e=xi(i,n);e!==null&&e<=n[1]+.05&&e>r&&(r=e)}return r}function Ci(e,t,n){let r=vt(e.ctx.sk,e.frames[t].s,n);for(let n of[`heel`,`ball`,`tip`])r[n]=[r[n][0],r[n][1]-Si(e,t,r[n]),r[n][2]];return r}let wi=[`Hips`,`Head`,`LeftHand`,`RightHand`,`LeftFoot`,`RightFoot`,`LeftLeg`,`RightLeg`,`LeftForeArm`,`RightForeArm`],Ti=new Set([`Hips`,`Head`]),Ei=[[`knee_l`,`LeftUpLeg`,`LeftLeg`,`LeftFoot`],[`knee_r`,`RightUpLeg`,`RightLeg`,`RightFoot`],[`elbow_l`,`LeftArm`,`LeftForeArm`,`LeftHand`],[`elbow_r`,`RightArm`,`RightForeArm`,`RightHand`]],Di=.75,Oi=e=>Math.round(e*100)/100;function ki(e,n){let r=n.frames,i=r.length;return wi.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:Ti.has(n),v:s,a:c,A:l}})}function Ai({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 ji=(e,t,n)=>e.a[t]>n&&e.a[t]>2*Ai(e,t);function Mi(e,t,n,r){for(let i of ki(e,t)){let e=i.core?n.accCore:n.accLimb;for(let t=2;t<i.a.length;t++)ji(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 Ni=[`hips`,`torso_lo`,`torso_hi`];function Pi(e,t,n){let{P:r}=e.frames[t].s,i=e.ctx.sk.index;return ft.some(([e,t,n,a])=>Ni.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=ft.find(e=>e[0]===(n.part??`hips`));return a&&o&&Math.min(...pt(r[i[o[1]]],r[i[o[2]]]).map(e=>U(e,a)))-o[3]<(n.tol??.04)})}function Fi(t,r=[]){let i=t.ctx.sk.index,a=t.frames;return a.map(({s:o},s)=>{if(Pi(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=Ci(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 Ii(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]-Si(e,t,n[r[i]])<.06))}function Li(e,t=[]){let n=e.frames.length,r=[],i=e.frames.map((n,r)=>!Pi(e,r,t)&&[`Left`,`Right`].every(t=>{let n=Ci(e,r,t);return Math.min(n.heel[1],n.ball[1])>=.02})&&!Ii(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 Ri(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 Li(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<=Di))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:Di})}return s}let zi=(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 Bi(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 ki(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]&&!ji(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 Ei.slice(0,2)){let i=zi(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`)&&Fi(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 Vi(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(ki(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=Fi(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=Li(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=[Oi(e),t+r])});let d=c(e=>typeof e==`number`&&e<0),f=Object.fromEntries(Ei.map(n=>{let i=[0,r];return t.frames.forEach(({s:t},a)=>{let o=zi(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 Hi(e,t,n,r){let i=e=>Object.hasOwn(n??{},e),a=new Float64Array(t.frames.length);for(let n of ki(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)&&(ji(n,e,r[c])&&(a[e]=Math.max(a[e],Math.sqrt(o[e]/Math.max(r[c],2*Ai(n,e))))),i(c)&&(a[e]=Math.max(a[e],Math.sqrt(o[e]/r[c]))))}return a}let Ui=[0,1,0],Wi=new Map;function Gi(e,t){let n=`${e.model}:${t}`;if(Wi.has(n))return Wi.get(n);let r=ke(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 Wi.set(n,i),i}function Ki(t,r,i){let a=Gi(t,i);if(!a?.length){let a=vt(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 qi(e,t){let n={top:-1/0,side:-1/0,topAt:null,sideAt:null};for(let i of e){let e=-U(i,t);if(e<=0)continue;let a=r(yt(i,t),Ui)>.7?`top`:`side`;e>n[a]&&(n[a]=e,n[`${a}At`]=i)}return n}function Ji(e,t,n,r,i,a,s){for(let[c,l]of[[`Left`,`l`],[`Right`,`r`]]){let u=Ki(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=qi(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 Yi={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},Xi=(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 Zi(i,c,l={}){let u={...Yi,...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 ft){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=Xi(l.allow,t,e)??u.allow;if(m-(o(p,r)-i)<=f)continue;_??=pt(s,c);let h=a-Math.min(..._.map(e=>U(e,n)));h>f&&g(`COLLIDE`,`${t} x ${e}`,d,h,f)}}Ji(i,h,d,y,u,(e,t)=>Xi(l.allow,e,t),g);for(let e of[`Left`,`Right`]){let t=vt(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(...pt(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=ht(i,h,t)+n;r>u.headAllow&&g(`FACE`,`${e} into head`,d,r,u.headAllow)}mt(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),mt(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)=>Xi(l.allow,e,t);mi(i,c,u,g,_),vi(i,c,u,g,_);for(let e of[`Left`,`Right`]){let t=m.map((t,n)=>Ci(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 Mi(i,c,u,g),Qi(i,m,u,g),d}function Qi(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 $i=ft.filter(e=>e[0].startsWith(`torso`)),ea=Object.fromEntries(ft.map(e=>[e[0],e[3]])),ta=(e,t,n)=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]],na=(e,t)=>[e[0][t],e[1][t],e[2][t]];function ra(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 ia(t,r,i){let a=t.index,o=V(t,r,i);return{pos:n(e(r.P[a[`${i}HandIndex2`]],r.P[a[`${i}HandMiddle2`]]),.5),R:ta(o.d,o.l,o.n)}}function aa(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 _=ia(a,l,q.r).R,v=na(_,0),b=s(i(d,t(v,n(d,r(v,d))))),x=e=>t(e,n(b,2*r(e,b))),S=ta(x(v),x(na(_,1)),n(x(na(_,2)),-1)),C=ia(a,l,q.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=at(a,l,q.l),I=o.fix===!1||o.fix?.self===!1?null:ke(a),L={uarm_l:w.LeftArm,farm_l:w.LeftForeArm,hand_l:w.LeftHand},R=(e,t,n)=>I?We(I,a,l,L[n],e,t):Math.max(0,...$i.map(([,r,i,a])=>ea[n]+a-ra(e,t,l.P[w[r]],l.P[w[i]]))),z=new Map,te=t=>{if(z.has(t))return z.get(t);let n=N(t),r=tt(a,l,q.l,j(t),n,F,Je),i=e=>{let t=r.at(e);return{pv:t.pv,elbow:t.elbow,cost:t.cost+1*((100*R(ee,t.elbow,`uarm_l`))**2+(100*R(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=R(n,s,`hand_l`),u=Math.max(0,c-Yi.body),d=2*ea.hand_l-ra(n,s,l.P[w.RightHand],l.P[w.RightHandMiddle3]),f=Math.max(0,d-Yi.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 z.set(t,h),h},ne=e=>te(e).best.cost+.3*(1-Math.cos(e));return{id:c,weight:g,pin:k,at:C.pos,rolled:A,arm:te,cost:ne,point:M,softmin:()=>{let e=Array.from({length:36},(e,t)=>{let n=2*Math.PI*t/36-Math.PI;return[n,ne(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=>ia(a,e,q.l)}}let oa=(e,t)=>Math.exp(-Math.max(0,e.cost(t)-3)/10),sa=(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}),ca=[30,10],la=[`the fingers' line`,`the palm normal`,`the knuckle line`],ua=(e,t)=>[e[0][t],e[1][t],e[2][t]],da=e=>{let t=m(e.R);return{pos:n(p(t,e.pos),-1),R:t}},fa=e=>Math.round(e*100)/100;function pa(e,t,n){let r=S(e),i=S(t);return pe(r,i)<0&&(i=i.map(e=>-e)),C(be(r,i,n))}function ma(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 ha(e,n,r,i,o){let{goal:s,plane:c,wristAt:l}=ot(e,n,r,i,o,1),u=l(s),d=tt(e,n,r,s,u,c,Je),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=ke(e),y=v?Math.max(We(v,e,n,p[`${r}Arm`],m,f.elbow),We(v,e,n,p[`${r}ForeArm`],f.elbow,u),We(v,e,n,p[`${r}Hand`],u,i.pos)):0;return[`twist`,`flex`,`dev`].reduce((e,t)=>e+ma(f.need[t],qe[t],Ke[t]),0)+20*((d.miss*100)**2+(_*100)**2)+4*(y*100)**2}function ga(e,t,n,r,i,a,o){if(r.aim===void 0)return o({pos:r.pos,R:C(r.rot)});let c=S(Y(i,a).R),l=s(r.aim),u=de(ve(s(_e(c,[0,1,0])),l),c),d=e=>({pos:r.pos,R:C(de(ye(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=lt(e,t,n,Y(o,da(a)),i);(!f||s<f.strain)&&(f={p:o,strain:s})}return o(f.p)}function _a(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 va=(e,t)=>e.length<=t?e:Array.from({length:t},(n,r)=>e[Math.round(r*(e.length-1)/(t-1))]);function ya(e,t){let n=he(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 ${la[i]}`}function ba({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=va(p,8),h=e=>Fs({pos:d.pos,R:e},i),g=(t,r,i)=>{let{s:a,now:o}=l(r);return ha(e,a,n,Y(i,da(t)),o)},_=t=>ha(e,u,n,Y(i,da(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+ga(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=La(t,s,e=>S(Y(l(e).now,a).R),(t,r,i,o)=>{let{s,now:c}=l(t);return lt(e,s,n,Y({pos:r,R:C(i)},da(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,Ba(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:fa(x.cost),today:fa(x.cost)};let w={R:d.R,rough:v(d.R)};for(let e of ca)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(ua(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:ya(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:fa(E.cost),today:fa(x.cost)}}let xa=180/Math.PI,Sa=[-40,-40/2,0,40/2,40];function Ca(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 wa(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 Ta=({d:e,l:t,n})=>[[e[0],t[0],n[0]],[e[1],t[1],n[1]],[e[2],t[2],n[2]]],Ea=(e,t)=>e.anchors?.[t]?.kind===`object`&&!!e.anchors[t].grasp,Da=(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 Oa(e,t,n,r){let i=new Set(K(t,n));return(e.fits??[]).some(a=>{if(a.side!==r||!Ea(e,a.anchor))return!1;let o=e.anchors[a.anchor].object;return o===n||i.has(o)||K(t,o).some(e=>i.has(e))})}function ka(e,t,n){let{spec:r,scene:i}=e;if(!e.objectHand||!(r.fits??[]).some(e=>e.side===t&&Ea(r,e.anchor)))return null;for(let[a,o]of Object.entries(r.objects??{}))if(o?.prop&&e.objectHand(a,n)===t&&Oa(r,i,a,t))return{id:a,grab:e.carrySpan(o)[0],keyed:!!o.keys?.length};return null}function Aa(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??er(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=Hn(i,x,w,C,o.yaw),E=(e=>({pos:u(e),R:Ta(V(n,e,i))}))(r),D=e=>{let t=ot(n,r,i,{pos:S,R:e},E,1,`palm`);return nt(n,r,i,t.goal,t.wristAt(t.goal),t.plane,Je).cost},O=(e,t)=>[e[0][t],e[1][t],e[2][t]],k=e=>f(y(O(T,1),e*xa),T),{R:A,roll:j,tilt:M}=ja(e,l.fit.anchor,a,p,()=>{let e=x.axis&&!x.from&&w!==`palm`?wa(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`?Ca(Sa.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}=ot(n,r,i,{pos:S,R:A},E,l.w,`palm`),F=nt(n,r,i,N,ee(N),P,Je,{w:l.w}),I={twist:0,flex:0,dev:0};for(let e=0;e<3;e++)ne(n,r,g,_,v,ee(e?r.R[v]:N),F.pole,Je,`elbow`),I=it(n,r,i,v,_,N);c[`grip_${a}`]={w:l.w,socket:!0,...I,roll:j*xa,tilt:M},c[`socket_${a}`]={rel:f(m(b),r.R[v])}}function ja(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 Ma(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`];it(t,n,r,u,l,f(n.R[l],s))}let Na={m:.01,deg:5},Pa=e=>e*e*(3-2*e),Fa=(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)]},Ia=e=>{let t=m(e.R);return{pos:n(p(t,e.pos),-1),R:t}};function La(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=Qr(c,e=>a(l[e]),o&&((e,t)=>o(l[e],Jr(c[e],t,s),t,c[e].lean?{shoulder:!0}:void 0))),f=c.map((e,t)=>Jr(e,me(d[t]),s).slice());for(let e of d){let t=me(e);u.push(u.length&&pe(u[u.length-1],t)<0?t.map(e=>-e):t)}let p=c.length-1,m=e=>n(he(de(fe(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 Ra=new WeakMap;function za(e,t){Ra.has(e)||Ra.set(e,new Map);let n=Ra.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:La(a.entry?.(t)??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 Ba({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=de(fe(i[l]),i[l+1]),b=he(y),x=e(e(n(b,g),n(o[l],u*h)),n(_e(y,o[l+1]),u*_));return{pos:v,R:C(de(i[l],ge(x)))}}function Va(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?Pa(1-s/o):0}let Ha=e=>Object.entries(e.objects??{}).filter(([,e])=>Os(e)&&e.keys?.length);function Ua(r,i,a,o){let{sk:s,spec:c,scene:l}=r,u=Ha(c);if(!u.length)return null;let d=new Set(u.map(([e])=>e).filter(e=>Dt(c,c.objects[e],l,e)).map(e=>c.objects[e])),f=new WeakMap,p=e=>i.timeMap?Math.round(i.timeMap(e)):e,m=e=>{f.has(i)||f.set(i,new Map);let t=f.get(i);if(!t.has(e)){let n=2*(l?.objects?.[e]?.half?.[1]??0);t.set(e,$r(c.objects[e],n,e=>A(a(p(e)).s,s.index.Head,s.landmarks.mouth)))}return t.get(e)},h=(e,t)=>Ps(s,c,t,{prop:e.prop},d.has(e)),g=new WeakMap,_=new Set,v=(e,t)=>r.restingAt?.(e,t)??l?.objects?.[e]??Cn(l)[e],y=new WeakMap,b=e=>i.timeMap?Math.round(i.timeMap(e)):e,x=e=>{y.has(i)||y.set(i,new Map);let t=y.get(i),n=c.objects[e],r=c.props?.[n.prop]?.hand,o=Fa(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])&&_a(l,e)&&!(c.fits??[]).some(e=>e.side===r&&e.orient===!1)&&!Oa(c,l,e,r)&&![`wtwist`,`wflex`].some(e=>Math.abs(u[`${e}_${r}`]?.[o]??0)>.001)&&b(Math.min(...n.keys.map(e=>e.f)))<=o))return null;_.add(e);try{t.set(e,ba({sk:s,o:m(e),side:q[r],grab:o,rest:v(e,o),plain:e=>a(e).s,palmOf:e=>h(n,e),map:b}))}finally{_.delete(e)}return t.get(e)},S=e=>{g.has(i.baked)||g.set(i.baked,new Map);let t=g.get(i.baked),n=c.objects[e];if(t.has(e))return t.get(e);let o=x(e);if(o&&o.name!==`today`){let r=d.has(n)?At(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]??Cn(l)[e],f=!l?.objects?.[e]&&!!u,p=n.at===void 0&&n.along===void 0&&(f||n.grab!==void 0);_.add(e);let m;try{m=p?a(Fa(i,n)[0]).s:D(s)}finally{_.delete(e)}let v=Fs(h(n,m),p?r.restingAt?.(e,Fa(i,n)[0])??{pos:u.pos,R:u.R}:Ps(s,c,m,n)),y=p&&d.has(n)?At(s,c,l,e,n,v):v;return t.set(e,y),t.set(`${e}|taken`,v),y},C=(e,t)=>{let n=S(e),r=g.get(i.baked)?.get(`${e}|taken`);return r&&r!==n&&t!==void 0?Nt(r,n,Mt(Fa(i,c.objects[e])[0],t)):n},w=(e,t)=>Y(h(c.objects[e],t),S(e)),T=(e,t,n,r)=>{let i=c.objects[e];return lt(s,t,q[c.props[i.prop].hand],Y(n,Ia(S(e))),h(i,t),r)},E=(e,t,n)=>{let r=za(i,e),a=Va(r,Fa(i,t),n);return a>0?{w:a,palm:Y(Ba(r,n),Ia(C(e,n))),measure:e=>h(t,e),comfort:m(e).keys.some(e=>e.lean>0)}:null},O=new WeakMap,k=(e,t)=>{O.has(i.baked)||O.set(i.baked,new Map);let n=O.get(i.baked),r=n.get(e);if(r&&r.map===i.timeMap)return r.plan;let[o,l]=Fa(i,t),u=[];for(let t=Math.max(0,o);t<=Math.min(i.frameCount-1,l);t++)Va(za(i,e),[o,l],t)>0&&u.push(t);let d=li(s,q[c.props[t.prop].hand],u,n=>({s:a(n).s,held:E(e,t,n)}));return n.set(e,{map:i.timeMap,plan:d}),d},j=!1,M=(e,t,n)=>{let r=w(e,t).R;return aa(s,c,e,t,n,[r[0][1],r[1][1],r[2][1]])},N=new WeakMap,P=(e,t)=>{N.has(i.baked)||N.set(i.baked,new Map);let n=N.get(i.baked),r=n.get(e);if(r&&r.map===i.timeMap)return r.plan;let[a,s]=Fa(i,t),c=new Map;j=!0;let l=[],u=()=>{let e=ii((e,t)=>l[e].grip.cost(t),(e,t)=>l[e].grip.point(t),l.map(e=>e.grip.softmin())),t=ci(l.map((t,n)=>oa(t.grip,e[n]))),n=t=>l[t].grip.arm(e[t]),r=ii((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=Va(za(i,e),[a,s],t)>0?o(t):null,r=n&&M(e,n.s,n.c);r?l.push({f:t,grip:r}):l.length&&u()}return l.length&&u(),j=!1,n.set(e,{map:i.timeMap,plan:c}),c},ee=(t,r,a)=>{let o=x(t);if(!o||o.name===`today`)return null;let s=za(i,t),c=s.t[0],l=s.t[s.t.length-1],[u,d]=Fa(i,r);if(a>=c&&a<=l)return null;let f=Ba(s,a<c?c:l),p=Y(f,Ia(o.offset)),m=e=>h(r,e),g=k(t,r).get(Math.round(a<c?c:l));if(a<u||a>d){let e=Pa(Math.max(0,1-(a<u?u-a:a-d)/12));return e>.001?{w:e,palm:p,measure:m,pole:g}:null}let _=E(t,r,a);return{w:1,palm:_?{pos:e(n(p.pos,1-_.w),n(_.palm.pos,_.w)),R:pa(p.R,_.palm.R,_.w)}:p,measure:m,pole:g}},F=new WeakMap,I=(e,n,r)=>{F.has(i.baked)||F.set(i.baked,new Map);let o=F.get(i.baked),s=o.get(e);if(s&&s.map===i.timeMap)return s.off;_.add(e);let c;try{c=h(n,a(Math.max(Fa(i,n)[0],Math.round(za(i,e).t[0]))).s)}finally{_.delete(e)}let l=Fs(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))>Na.m||u>Na.deg;return o.set(e,{map:i.timeMap,off:d}),d},L=(e,t,n)=>{let r=za(i,e),[a,o]=Fa(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=Y(Ba(r,s),Ia(S(e)));if(!I(e,t,c))return null;let l=Pa(1-(s-n)/12);return l>.001?{w:l,palm:c,measure:e=>h(t,e),pole:k(e,t).get(s)}:null},R=(e,t,n)=>{let r=za(i,e),a=r.t[r.t.length-1],o=Fa(i,t)[1];return n>o&&n-a<8?{curve:r,release:o,w:Pa(1-(n-a)/8)}:null},z=(e,t,n)=>{let r=R(e,t,n);return r&&{w:r.w,palm:Y(Ba(r.curve,r.release),Ia(S(e))),measure:e=>h(t,e),pole:k(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?ee(a,o,t)??L(a,o,t)??z(a,o,t):null;if(s)return s;let l=c.props?.[o.prop]?.hand===e?E(a,o,t):null;if(l)return{...l,pole:k(a,o).get(t)};let u=e===`l`&&n&&!j?R(a,o,t):null,d=e===`l`&&n&&!j?Va(za(i,a),Fa(i,o),t)||(u?.w??0):0,f=d>0?M(a,n,r):null,p=f&&P(a,o).get(u?u.release:t),m=p?.roll??f?.softmin();if(f)return sa(f,d,m,p?.gate??oa(f,m),p?.pole??f.arm(m).best.pole)}return null},attach:S,attachAt:C,attaching:e=>_.has(e),plainFrame:a,entry:m,carryOn:w,strainOn:T,span:e=>Fa(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=x(e);return n&&{id:e,hand:c.props[t.prop].hand,name:n.name,cost:n.cost,today:n.today}}).filter(Boolean)}}let Wa=[0,0,0];function Ga(e,r,i,a){return Array.isArray(e?.v)?e.v:e?.v===`hand`&&i?n(t(r.pos,i.pos),a):Wa}let Ka=e=>e.some(e=>e!==0),qa=(e,t,n)=>(t+Math.sqrt(t*t+2*n*Math.max(0,e)))/n,Ja=(e,t,n)=>{let r=Math.min(t,n);return[e[0]*r,0,e[2]*r]};function Ya(t,n,r,i,a=[]){let o=t(n),s=o.top+o.h,c=t=>({pos:e(n.pos,Ja(r,qa(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 Xa=.5,Za=6*Math.PI,Qa=[0,-1,0],$a=[[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]],eo=e=>e*180/Math.PI;function to(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 no=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]]),ro=(t,n)=>e(t.pos,p(t.R,n)),io=(e,t)=>Math.min(...no(e).map(e=>ro(t,e)[1])),ao=(e,t,n)=>({pos:[t.pos[0],t.pos[1]+n-io(e,t),t.pos[2]],R:t.R}),oo=(n,r,i)=>({pos:e(r,p(i,t(n.pos,r))),R:f(i,n.R)}),so=e=>$a.reduce((t,n,i)=>r(p(e,n),Qa)>r(p(e,$a[t]),Qa)?i:t,0),co=(e,t)=>{let n=t>>1;return $a[t][n]>0?e.hi[n]:e.lo[n]},lo=(e,t)=>Math.abs(co(e,t)-e.com[t>>1]);function uo(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(Za,o*r);return a(l)>0?n(l,u):[0,0,0]}function fo(n,r,i,o,s,c=[0,0,0]){let l=a(i),u=ro(r,n.com),d=!c.some(e=>e!==0),m=a=>{let o=l>0?f(y(i,eo(l*a)),r.R):r.R,s=d?[0,-.5*_o*a*a,0]:[c[0]*a,c[1]*a-.5*_o*a*a,c[2]*a];return{pos:t(e(u,s),p(o,n.com)),R:o}},h=Math.max(0,io(n,r)-o);if(l===0)return{tl:d?Math.sqrt(2*h/_o):qa(h,c[1],_o),at:m};let g=1/(s*8),_=(d?Math.sqrt(2*(h+a(t(n.hi,n.lo)))/_o):qa(h+a(t(n.hi,n.lo)),c[1],_o))+g,v=0,b=g;for(;b<_&&io(n,m(b))>o;)v=b,b+=g;for(let e=0;e<24&&b-v>1e-7;e++){let e=(v+b)/2;io(n,m(e))>o?v=e:b=e}return{tl:b,at:m}}let po=(e,t)=>.12+.9*Math.sqrt(Math.max(e,1e-4)/_o)*Math.sqrt(t/90);function mo(o,l,u,d,f=[0,0,0]){let m=[],h=ao(o,l,u),g=0,_=(e,t,n)=>{let r=ro(h,o.com)[1]-u,i=po(r,n);m.push({from:h,pivot:e,axis:t,angle:n,time:i}),h=ao(o,oo(h,e,y(t,n)),u),g+=i},v=so(h.R),b=e=>eo(Math.acos(c(r(p(h.R,$a[e]),Qa),-1,1))),x=b(v);if(x>Xa){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>eo(Math.atan2(lo(o,t),lo(o,v)))&&(v=t);let r=no(o).map(e=>ro(h,e)).filter(e=>e[1]<=u+1e-4),a=p(h.R,$a[v]);_(n(r.reduce(e,[0,0,0]),1/r.length),s(i(a,Qa)),b(v))}let S=x>Xa||a(f)>0?d:0;for(let a=0;a<3;a++){let a=ro(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,$a[e]),i=ro(h,$a[e].map((t,n)=>t?co(o,e):n===v>>1?co(o,v):o.com[n])),s=-r(t(a,i),n),u=lo(o,e);if(!(s<0||u<c*.8))continue;let d=s<0||eo(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=no(o).map(e=>ro(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,Qa)),90),v=l.j}return{steps:m,rest:h,time:g}}function ho(e,t){let n=t;for(let t of e.steps){if(n<t.time){let e=n/t.time;return oo(t.from,t.pivot,y(t.axis,t.angle*e*e))}n-=t.time}return e.rest}let go=(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*_o),_o=9.8,vo=[`prop`,`follow`,`pivot`,`axis`,`channel`],yo=e=>!!e&&typeof e==`object`&&!Array.isArray(e)&&vo.every(t=>e[t]===void 0)&&(e.keys!==void 0||e.fall!==void 0),bo=e=>e?.fall===void 0?[]:Array.isArray(e.fall)?e.fall:[e.fall],xo=(e,t)=>e?.objects?.[t]??Cn(e)[t]??null,So=(e,t)=>Object.keys(e?.objects??{}).filter(n=>n===t||e.objects[n].ancestors?.includes(t));function Co(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 wo=e=>Math.max(...Co(e).map(e=>e[1])),To=(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 Eo(e,t){let n=new Set;for(let[r,i]of Object.entries(t?.objects??{}))[r,...i.ancestors??[]].some(t=>js(e.objects?.[t]))&&n.add(r);return n}function Do(e,t,n){let r=xo(e,t);return So(e,t).map(t=>({id:t,...Y(n,Fs(r,e.objects[t])),half:e.objects[t].half}))}function Oo(t,n,r,i,a=null){let o=Do(n,r,i),s=new Set(o.map(e=>e.id)),c=Eo(t,n);if(!o.length)return{support:null,top:0,h:0};let l=Math.min(...o.flatMap(e=>Co(e).map(e=>e[1]))),u=o.flatMap(Co).reduce((t,n)=>e(t,n),[0,0,0]).map(e=>e/(o.length*8)),d=(a?So(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=wo(t);To(t,u)&&r<=l+.005&&(!f||r>f.top)&&(f={support:e,top:r})}if(!f&&a)for(let e of d){let t=wo(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 ko=new WeakMap;function Ao(e,t){ko.has(e)||ko.set(e,new Map);let n=ko.get(e);return n.has(t)||n.set(t,to(Do(e,t,{pos:[0,0,0],R:d()}))),n.get(t)}function jo(e,t,n,r,i,a,o,s=null){let c=Ga(a,r,s,o),l=Ka(c),u=r=>Oo(e,t,n,r,a.onto??null),d=l?Ya(u,r,c,_o,Object.values(t.objects).map(wo)):u(r),f=a.tumble===!1?null:Ao(t,n);if(f){let e=s?uo(s.R,r.R,o):[0,0,0],t=fo(f,r,e,d.top,o,c),n=mo(f,t.at(t.tl),d.top,.3*d.h+go(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):ho(n,a-t.tl)},rest:n.rest}}let p=d.h,m=l?qa(p,c[1],_o):Math.sqrt(2*p/_o),h=Ja(c,1/0,m),g=Math.min(.6,Math.max(0,a.bounce??.25)),_=Math.min(g*_o*m,Math.sqrt(2*_o*.03)),v=2*_/_o,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*_o*t*t:t<=m+v?-p+_*(t-m)-.5*_o*(t-m)**2:-p,a=Ja(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 Mo(e,t,n=-1/0,r=1/0){let i=Os(e)?[]:[...e.keys??[]].sort((e,t)=>e.f-t.f),a=bo(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 No(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 Ba(n.curve,t);a=Ba(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 Po=new WeakMap;function Fo(e,t,n,r,i){Po.has(e.scene)||Po.set(e.scene,new Map);let a=Po.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=xo(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=Mo(n,l,u,d);for(let e of f)e.run&&(e.curve=La({keys:e.run},l,()=>S(c.R),null));return a.set(o,{map:r,o:n,spec:e.spec,events:f}),f}let Io=(e,t)=>(n,r)=>jo(e.spec,e.scene,t,n,r.t,r,e.spec.fps??30);function Lo(e,t,n){let r=xo(e.scene,t),i=e.spec.objects?.[t];return!r||!i?null:No(Fo(e,t,i,e.timeMap,`all`),n,{pos:r.pos,R:r.R},Io(e,t))}function Ro(e,t,n,r,i){let a=xo(e.scene,t);return!a||!bo(n).length?a&&{pos:a.pos,R:a.R}:No(Fo({...e,grab:i},t,n,e.timeMap,`before`),r,{pos:a.pos,R:a.R},Io(e,t))}function zo(e,t,n,r,i,a,o=null){let s=bo(n).length?Fo({...e,release:i},t,n,e.timeMap,`after`):[];return s.length?No(s,r,a,(n,r)=>jo(e.spec,e.scene,t,n,r.t,r,e.spec.fps??30,r.t<=i+1?o:null),!1):null}function Bo(e,t,n){let r=e.timeMap??(e=>e);return bo(t).some(e=>r(e.at)>=n)}function K(e,t){let n=e?.objects??{};return Object.keys(n).filter(e=>e===t||n[e].ancestors?.includes(t))}function Vo(e,t){return Os(e)&&t>=(e.grab??0)&&(e.release===void 0||t<=e.release)}function Ho(e,t){return e?.objects?.[t]??Cn(e)[t]??null}function Uo(e,t,n){return[n,...t?.objects?.[n]?.ancestors??Cn(t)[n]?.ancestors??[]].find(t=>js(e.objects?.[t]))??null}function Wo(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 Go=(e,t,n)=>n.at===void 0&&n.along===void 0&&(!e?.objects?.[t]&&!!Cn(e)[t]||n.grab!==void 0),Ko=new WeakMap,qo=new WeakMap,Jo=new WeakMap,Yo=e=>({spec:e.ctx.spec,scene:e.ctx.scene,timeMap:e.timeMap});function Xo(e,t,n){let{spec:r,scene:i}=e.ctx,a=Uo(r,i,t),o=r.objects?.[a];if(!Os(o))return null;let s=Ro(Yo(e),a,o,n,Wo(e,o)[0]);return s&&(a===t?s:Y(s,Fs(Ho(i,a),Ho(i,t))))}function Zo(t,n){Jo.has(t)||Jo.set(t,new Map);let r=Jo.get(t);if(r.has(n))return r.get(n);let{spec:i,scene:a}=t.ctx,o=i.objects?.[n],s=Ho(a,n),c=K(a,n),l=null;if(Os(o)&&o.release!==void 0&&s&&c.length){let r=Wo(t,o)[1];if(r<t.frameCount-1&&!Bo(Yo(t),o,r)){let u=es(t,n,o,r),d=c.flatMap(e=>Co({...Y(u,Fs(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:Uo(i,a,e),u=i.objects?.[s];if(!s||s===n||Os(u)||ks(u))continue;let d={...is(t,e,r),half:o.half},m=wo(d);To(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 Qo(e,t){let n=e.frames[t-(e.f0??0)];if(n)return n;Ko.has(e)||Ko.set(e,new Map);let r=Ko.get(e);return r.has(t)||r.set(t,lu(e.ctx,Wr(e.baked,t),t)),r.get(t)}function $o(e,t,n,r,i){let{sk:a,spec:o,scene:s}=e.ctx;if(!Go(s,t,n))return Ps(a,o,i,n);let c=Dt(o,n,s,t);if(n.keys?.length&&e.ctx.hold){if(e.ctx.hold.attaching(t))return Ho(s,t);let l=Y(Ps(a,o,i,{prop:n.prop},c),e.ctx.hold.attachAt(t,r)),u=Wo(e,n)[0];return c?Nt(Xo(e,t,u)??Ho(s,t),l,Mt(u,r)):l}let l=e=>Ps(a,o,e,{prop:n.prop},c),u=Wo(e,n)[0],d=Fs(l(u===r?i:Qo(e,u).s),Xo(e,t,u)??Ho(s,t));return c?Nt(Y(l(i),d),Y(l(i),At(a,o,s,t,n,d)),Mt(u,r)):Y(l(i),d)}function es(e,t,n,r){let i=Qo(e,r);qo.has(i)||qo.set(i,new Map);let a=qo.get(i);return a.has(t)||a.set(t,$o(e,t,n,r,i.s)),a.get(t)}function ts(e,t,n,r=null){let{spec:i,scene:a}=e.ctx,o=js(i.objects?.[t])?i.objects[t]:void 0,s=Ho(a,t);if(ks(o))return hr(r?{...e,f0:n,frames:[r]}:e,o,n);if(Os(o)&&(s||!Go(a,t,o))){let[i,a]=Wo(e,o),c=Math.max(i,Math.min(n,a));if(n<i&&s)return Xo(e,t,n);let l=n>a?zo(Yo(e),t,o,n,a,es(e,t,o,a),a>0?es(e,t,o,a-1):null):null;if(l)return l;let u=n>a?Zo(e,t):null;if(u){let r=es(e,t,o,a);return Y(ts(e,u,n),Fs(ts(e,u,a),r))}return r&&c===n?$o(e,t,o,n,r.s):es(e,t,o,c)}if(!s)return null;if(yo(o))return Lo(Yo(e),t,n);if(o&&a.objects[t])return e.ctx.objectsAt(n)[t];if(o){let r=K(a,t)[0],i=e.ctx.objectsAt(n)[r];return Y(i,Fs(a.objects[r],s))}return{pos:s.pos,R:s.R}}function ns(e,t,n){let r=e.frames?.[n-(e.f0??0)],i=e.ctx.scene,a=K(i,t);return!r||!a.length?null:a.map(t=>{let r=is(e,t,n);return{id:t,pos:r.pos,R:r.R,half:i.objects[t].half}})}function rs(e,t,n){let{spec:r,scene:i,sk:a}=e.ctx,o=r.objects?.[Uo(r,i,t)];if(Os(o)){let[t,i]=Wo(e,o);return n>=t&&n<=i?r.props?.[o.prop]?.hand??null:null}if(!ks(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(vn(a,c).bone??``);return l?l[1]===`Left`?`l`:`r`:null}function is(e,t,n,r=null){let{spec:i,scene:a}=e.ctx,o=Ho(a,t),s=Uo(i,a,t),c=i.objects?.[s];if(!o)return null;if(Os(c)||ks(c)||yo(c)){let t=ts(e,s,n,r);return t&&Y(t,Fs(Ho(a,s),o))}return s?a.objects[t]?e.ctx.objectsAt(n)[t]??o:s===t?ts(e,t,n):Y(ts(e,s,n),Fs(Ho(a,s),o)):{pos:o.pos,R:o.R}}let as=[`Index`,`Middle`,`Ring`,`Pinky`],os=e=>{let t=Math.min(1,Math.max(0,(e-.6)/.30000000000000004));return t*t*(3-2*t)};function ss(e,t){let n=-1/0;for(let r=0;r+1<e.length;r++)for(let i of t)n=Math.max(n,Et(i,e[r],e[r+1],.009));return n}function cs(n,r,i,a,o){if(a<=0||ss(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(ss(s(a),o)<-.002)return null;let c=0,l=a;for(let e=0;e<8;e++){let e=(c+l)/2;ss(s(e),o)>=-.002?l=e:c=e}return l}function ls(n,r,i,a,o){if(ss(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(ss(s(a),o)>=-.002)return ss(s(a),o)<ss(n,o)?a:0;let c=0,l=a;for(let e=0;e<8;e++){let e=(c+l)/2;ss(s(e),o)<-.002?l=e:c=e}return l}function us(e,t,n,r,i,a,o,s,c=!0){let l=cs(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 ds(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 as){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(V(r,a,o).l,m),b=[0,1,2].reduce((n,r)=>{let a=y(v,-se(d,i,r));return n.map((i,o)=>o>r?e(n[r],p(a,t(i,n[r]))):i)},_),x=ss(_,c)>=-.002&&ss(b,c)<ss(_,c);for(let e=0;e<3;e++){let t=n(V(r,a,o).l,m),c=se(d,i,e),p=x?c:0;p>0&&O(r,a,s[e],a.P[s[e]].slice(),y(t,-p*u)),l=us(r,a,s[e],s.slice(e),t,Math.max(0,ce(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}=V(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=ls(_.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=us(r,a,_[o],_.slice(o),l,70,f,u,!1)}c&&g.push(`Thumb`)}return{touched:g}}function fs(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=K(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:os(s)*u,preset:t[`fingers_${r}`]})}return a??ps(e,t,n,r,i)}function ps(e,t,n,r,i){let{spec:a,scene:o}=e;for(let[s,c]of Object.entries(a.objects??{})){if(!Dt(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=K(o,s).map(t=>({...o.objects[t],...e.objectPose(t,n,{s:i})})),f=Object.entries(e.objectsAt(n)).filter(([e,t])=>!K(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 ms=(e,[t,n])=>Math.min(n,Math.max(t,e));function hs(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}=$e(h,i.R[c],d,p(f(h,m(i.R[c])),V(r,i,a).l)),b=Object.fromEntries(Object.entries(v).map(([e,t])=>[e,ms(t,Ke[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 q={l:`Left`,r:`Right`},gs=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)},_s=[0,0,1],vs=[`UpLeg`,`Leg`,`Foot`],ys=(e,t=[0,0,0])=>Array.isArray(e)||ArrayBuffer.isView(e)?[e[0],e[1],e[2]]:t,J=(e,t=0)=>typeof e==`number`&&Number.isFinite(e)?e:t;function bs(e,t,n,r,i,a,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 xs=.9995;function Ss(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-xs*f>.05)return l;let _=h+g*(1-Math.exp(-(m-h)/g));return u[`reach_over_${d}`]=Math.max(0,m-xs*f),e(i.P[o],n(p,_/m))}let Cs=new Map;function ws(r,i){let a=`${r.model}|${i}`;if(Cs.has(a))return Cs.get(a);let o=(90-i)*Math.min(1,Math.max(0,(i-45)/27)),s=Ue(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=>Ne(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 Cs.set(a,l),l}let Ts=e=>e-360*Math.round(e/360);function Es(e,t,n){let r=Ts(t),i=Ts(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 Ds(r,i,a={}){let o=D(r),c={},l=J(i.arm_rest,72),u=J(i.arm_hang)&&J(i.arm_hang)*ws(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,J(i[`armw_${n}`])+J(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(q)){let n=J(i[`abend_${e}`]),a=J(i[`aswing_${e}`]),s=r.index[`${t}ForeArm`],c=r.index[`${t}Arm`];n&&(O(r,o,s,o.P[s].slice(),g(-n)),z(r,o,t)),a&&O(r,o,c,o.P[c].slice(),g(-a))}bs(r,o,[`Neck`,`Head`],J(i.head_yaw),J(i.head_pitch),J(i.head_roll));let d=J(i.spine_yaw),m=J(i.spine_pitch),h=J(i.spine_roll);bs(r,o,[`Spine`,`Spine1`],d,m,h,3),bs(r,o,[`Spine2`],J(i.chest_yaw,d),J(i.chest_pitch,m),J(i.chest_roll,h),3);let y=r.index.Hips,b=J(i.yaw);O(r,o,y,o.P[y].slice(),f(_(b),f(g(J(i.pitch)),v(J(i.roll)))));let x=t(ys(i.root,o.P[y]),o.P[y]);for(let t=0;t<o.P.length;t++)o.P[t]=e(o.P[t],x);let S=Es(b,J(i.pitch),J(i.roll)),C=S?.R??_(b);for(let[e,t]of Object.entries(q)){if(!i[`foot_${e}`])continue;let n=ys(i[`foot_${e}`]);c[`leg_miss_${e}`]=ne(r,o,r.index[`${t}UpLeg`],r.index[`${t}Leg`],r.index[`${t}Foot`],n,p(C,ys(i[`knee_${e}`],_s)),_s,`knee`);let a=R(r,o,t,n,vs);a>150&&(c[`leg_fold_${e}`]=a),B(r,o,t,J(i[`fyaw_${e}`],b),J(i[`fpitch_${e}`]),S)}for(let l of a.armOrder??[`r`,`l`]){let u=q[l],d=J(i[`armw_${l}`]),f=r.index[`${u}Arm`],m=r.index[`${u}ForeArm`],h=r.index[`${u}Hand`],g=s(p(C,ys(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,gs(d)))),a.softReach&&(_=Ss(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}`]=ne(r,o,f,m,h,_,S,Je,a.foldFree?null:`elbow`);let C=R(r,o,u,_);C>145&&!a.foldFree&&(c[`arm_fold_${l}`]=C)}ie(r,o,u,J(i[`wtwist_${l}`]),J(i[`wflex_${l}`])),le(r,o,u,i[`fingers_${l}`]);let _=a.hold?.(l,o);(!_||_.w<1)&&a.grip?.(l,o,c),_&&st(r,o,u,_,Je,c,l);let v=a.swivel?.(l);v&&(re(r,o,u,v*gs(d)),_||hs(r,o,u));let y=a.heldShapes?.(l,o);y&&(c[`closed_${l}`]=ds(r,o,u,y).touched),a.afterArm?.(l,o,c)}return{s:o,diag:c}}let Os=e=>typeof e?.prop==`string`,ks=e=>!Os(e)&&e?.follow!==void 0,As=e=>!!e&&typeof e==`object`&&!Array.isArray(e)&&[`prop`,`follow`,`pivot`,`axis`,`channel`,`keys`,`fall`].every(t=>e[t]===void 0),js=e=>!!e&&typeof e==`object`&&!As(e);function Ms(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 Ns(r,i,a){let o=r.index[`${q[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 Ps(a,o,c,l,u=!1){let d=V(a,c,q[o.props[l.prop].hand]),f=u?Ns(a,o.props[l.prop],c):Ms(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 Y=(t,n)=>({pos:e(t.pos,p(t.R,n.pos)),R:f(t.R,n.R)}),Fs=(e,n)=>({pos:p(m(e.R),t(n.pos,e.pos)),R:f(m(e.R),n.R)});function Is(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 Ls=180/Math.PI,Rs=[0,1,0],zs=(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 Vs(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 Hs(t,r,i,a,o){if(o.prop){let s=r.props?.[o.prop];if(!s)return null;let c=V(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 Us(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&&qs(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 Gs(e,t,n,r,i,a){let o=e.scene?.objects??{},s=i?.kind===`object`?K(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,Us(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=U(r,d);(!c||f<c.d)&&(c={box:d,d:f,id:i})}let l=c&&Us(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 Ks=[0,1,2];function qs(e){let t=Ks.reduce((t,n)=>e[n]>e[t]?n:t,0);return Ks.filter(e=>e!==t).every(t=>e[t]<.045)?t:-1}function Js(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(Rs,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 zs(o,g,i(o,g));let y=e=>Math.acos(Math.max(-1,Math.min(1,r(e,c))))*Ls-(r(e,g)>0?30:0),b=s(v),x=y(b)<=y(n(b,-1))?b:n(b,-1);return zs(o,x,i(o,x))}function Ys(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))*Ls,[c,l]=qe.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 zs(a,d,i(a,d))}function Xs(n,r,i,a,o,c,l,u){let d=Vs(n.spec,c,o),h=ka(n,o,l),g=d&&d.w>=.001&&Ea(n.spec,d.fit.anchor);if(h||g?Da(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:Ma(n,r,i,a,o,h);if(!d||d.w<.001)return;if(g)return Aa(n,r,i,a,o,c,u,d,e=>Hs(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(qn(n,d.fit.anchor,i,u,c,o),d.fit.offset??[0,0,0]),b=Gs(n,l,o,y,v,_),x=b&&Hs(r,n.spec,i,a,d.fit.effector??{});if(!x)return;let S=e=>({pos:Hs(r,n.spec,e,a,d.fit.effector??{}),R:(({d:e,l:t,n})=>zs(e,t,n))(V(r,e,a))}),C=r.index,w=C[`${a}Arm`],T=C[`${a}ForeArm`],E=C[`${a}Hand`],D=qs(b.half),O=D>=0?p(b.R,Ks.map(e=>+(e===D))):null;if(O&&Math.abs(O[1])>.8&&!Us(n,b.id))return Aa(n,r,i,a,o,c,u,d,e=>Hs(r,n.spec,e,a,d.fit.effector??{}),{w:{p:y,axis:O},type:`power`},l);let k=V(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=$e(M,i.R[E],j,p(f(M,m(i.R[E])),k.l)).twist,{goal:P,plane:ee,wristAt:F}=ot(r,i,a,{pos:x,R:Ys(Js(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++)ne(r,i,w,T,E,F(e?i.R[E]:P),ee,Je,`elbow`),I=it(r,i,a,E,T,P);u[`grip_${o}`]={w:d.w,...I}}let Zs={period:3.8,deg:.8},Qs={yaw:.9,pitch:.6},$s={period:5.6,m:.012,list:1.4},ec=.5,tc=2*Math.PI,nc=e=>e*e*(3-2*e),rc=(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 ic(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*tc})}function ac(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=>rc(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(ec*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=>nc(Math.min(1,e/s)))}function oc(e,t,n,r){let i={},a=Math.max(1,Math.round(ec*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-nc(Math.min(1,e/a)))}return i}function sc(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=ac(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]=ic(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*Zs.deg*Math.sin(tc*n/Zs.period+l),head_yaw:r*Qs.yaw*(.6*Math.sin(tc*n/7.3+u)+.4*Math.sin(tc*n/11.9+p)),head_pitch:r*Qs.pitch*Math.sin(tc*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(tc*n/$s.period+f),g=(e.yaw?.[t]??0)*Math.PI/180;o.root=[Math.cos(g)*$s.m*h,0,-Math.sin(g)*$s.m*h],o.roll=$s.list*h,o.spine_roll=-$s.list*h}return m}function cc(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;for(let t of[`chest_pitch`,`chest_roll`])e[t]!==void 0&&r[t.replace(`chest`,`spine`)]!==void 0&&(i[t]=e[t]+r[t.replace(`chest`,`spine`)]);return i}let lc=(e,t,n)=>Math.min(n,Math.max(t,e));function uc(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 dc={noise:.02,side:.03,first:.22,second:.08,last:.22,beforeLast:.08,turn:0},fc={noise:.025,side:.012,stride:.035,second:.05,beforeLast:.06,turn:.06},pc={noise:.1,first:.1,last:.25};function mc(e,{s0:t,turn:n,seed:r}){let i=e.steps,a=lc(e.vary??1,0,2),o=uc(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)*(dc.noise*e.time+dc.side*l(t))+dc.turn*m[t];return t===0&&(n+=dc.first*(1-m[0])),t===1&&h&&(n+=dc.second),t===i-1&&(n+=dc.last),t===i-2&&h&&(n+=dc.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=fc.noise*(.7*e.length+.3*e.time)+fc.side*l(t)+fc.stride*d(t)-fc.turn*m[t];return t===1&&h&&(n-=fc.second),t===i-2&&h&&(n-=fc.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*(pc.noise*t.lift-(n===0?pc.first:0)-(n===i-1&&i>1?pc.last:0)))),arm:s.map(e=>1+a*.1*e.arm)}}let hc={strike:4,load:4,mid:3,toeOff:5},gc={strike:2,load:14,mid:7,toeOff:48,pow:1.8},_c=[1,2.2],vc=e=>{let t=Math.min(1,Math.max(0,((e??_c[1])-_c[0])/(_c[1]-_c[0])));return t*t*(3-2*t)},yc=.004,bc=[.25,.5],xc=e=>e*e*e*(e*(6*e-15)+10),Sc=new WeakSet,Cc=(n,r,i,a)=>e(n,p(_(r),p(g(i),p(_(-r),t(a,n)))));function wc(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=vc(r),d=(e,t)=>e+(t-e)*u,f=d(gc.strike,18)+l*hc.strike,p=d(gc.load,22)+l*hc.load,m=d(gc.mid,7)+l*hc.mid,h=d(gc.toeOff,45)+l*hc.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(gc.pow,1)}}let Tc=(t,n,r)=>e(t.root[n],p(_(t.yaw?.[n]??0),p(v(t.roll?.[n]??0),r)));function Ec(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}=wc(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=Ur(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-yc*Math.log(s.reduce((e,t)=>e+Math.exp(-(t-c)/yc),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+bc[0]*r);n<=Math.floor(e.f+bc[1]*r);n++){let r=T[n];for(let t=e.f+1;t<n;t++){let i=xc((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=xc((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=Oc(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 Dc(r,i,{end:o,period:s,pace:c,ground:l}){let{reach:u,hipOff:d,trail:f}=wc(r,s,c),p=Ur(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=Tc(p,e,d[r]),i=u(f((e-s.heel)/Math.max(1,s.b-s.heel))),o=e=>a(t(n,Cc(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=Cc(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 Oc(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 kc=e=>e*180/Math.PI,Ac=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)};function jc(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 kc(Math.atan2(-r(g,s),-g[1]))}function Mc(e,{end:n,ground:r,sk:i}){let o=Ur(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=>!Sc.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=>Oc(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)=>jc(Tc(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-Ac((t-c.b)/(.35*A)))+M*(1-Ac((l.a-t)/(.2*A)))),i=y+.03*Ac(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)};Sc.add(o),e.push(o)}}}}let Nc=[6,4],Pc=[18,3],Fc=[6,3],Ic=[.52,.64],Lc=e=>c(.66-.18*e,.2,.48),Rc=e=>c(.32-.11*e,0,.45),zc=e=>c(2.8-.45*e,1.2,3),Bc=e=>c(4.7-1.1*e,1.8,4),Vc=[.2,.6],Hc=e=>p(_(e),[0,0,1]),Uc=e=>p(_(e),[1,0,0]),Wc=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)};function Gc(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 Kc(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=Gc(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=Lc(O),A=Rc(O),j=zc(O),M=Bc(O),N=c(.9*O,0,3),P=Pc[0]+Pc[1]*Math.min(O,2.5),ee=Fc[0]+Fc[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),R=n(e(f.foot_l,f.foot_r),.5);R[1]=0;let z=[R];for(let t=0;t<4e3;t++)z.push(e(z[t],n(Hc(L(t*.01)),.01)));let te=e=>z[Math.max(0,Math.min(z.length-1,Math.round(e/.01)))],ne=(t,r)=>{let i=e(te(t),n(Uc(L(t)),(r===`l`?1:-1)*D.halfWidth));return i[1]=g,i},B={l:-.07,r:.07},V={foot_l:f.foot_l,foot_r:f.foot_r,fyaw_l:f.fyaw_l??F,fyaw_r:f.fyaw_r??F},re=[`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`],ie=f.roll??0,ae=f.spine_roll??0,oe=f.spine_pitch??0,se={l:[{a:r.f0,b:1/0,strike:!1}],r:[{a:r.f0,b:1/0,strike:!1}]};d(r.f0,Object.fromEntries(re.map(e=>[e,f[e]??V[e]??(e===`yaw`?F:0)])));let ce=B.r,le=null,H=mc(D,{s0:ce,turn:(e,t)=>L(t)-L(e),seed:[r.f0,D.steps,D.step,D.period,D.swing,D.firstStep,F,I,R[0].toFixed(3),R[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?ce:ce+H.inc[i],h=r.f0+16+H.lift[i],g=h+H.air[i],S=h+Math.floor(H.air[i]/2);if(i===0){let t=e(te(B[o]),n(Uc(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-B[a])/1.1,.3,1.15),I=50*(.55+.45*C)*(u?.8:1),R=V[`foot_${a}`],z=V[`fyaw_${a}`],re=e(R,p(_(z),y[a])),ue=Math.max(h-Math.max(4,Math.round((i===0?.35:.3)*D.period)),x[a]+1,r.f0+1);Object.assign(se[a].at(-1),{b:h,heel:ue,ball:re,yw:z,flat:R});for(let e=ue;e<=h;e++){let t=I*((e-ue)/Math.max(1,h-ue))**1.8;d(e,{[`foot_${a}`]:Cc(re,z,t,R),[`fpitch_${a}`]:t,[`fyaw_${a}`]:z})}let de=Math.max(1,Math.round(.22*H.air[i])),fe=ne((B[a]+m)/2,a);fe[1]+=H.height[i];let pe=Cc(re,z,Math.min(I,50),R),me=e(pe,n(t(fe,pe),.35));me[1]=pe[1]+.18*C*Math.min(1,(H.air[i]/10)**2)*(.55+.44999999999999996*vc(O))*(H.height[i]/D.lift);let he=n(Uc(L(B[o])),a===`l`?1:-1),ge=V[`foot_${o}`],_e=e=>(e[0]-ge[0])*he[0]+(e[2]-ge[2])*he[2],ve=_e(fe)-_e(me);ve>0&&(me[0]+=he[0]*ve,me[2]+=he[2]*ve);let ye=h+Math.max(2,Math.round(.18*H.air[i]));!u&&ye<S-1&&d(ye,{[`foot_${a}`]:me,[`fpitch_${a}`]:.7*Math.min(I,50)}),d(S,{[`foot_${a}`]:fe,[`fyaw_${a}`]:L((B[a]+m)/2),[`fpitch_${a}`]:0});let be=B[o]+(Ic[0]+(Ic[1]-Ic[0])*vc(O))*(m-B[o]),xe=le?le.s+(be-le.s)*((S-le.f)/(g-le.f)):null,Se=xe===null?B[o]+.03:xe-.015*Math.abs(m-B[a])/2,Ce=i===0?.015:.032,we=e(te(Se),n(Uc(L(Se)),(o===`l`?1:-1)*Ce));we[1]=v+D.pelvisHigh;let Te=r.pelvisLow??(H.inc[i]>w?Math.min(D.pelvisLow,E(Math.hypot(H.inc[i]/2,T))):D.pelvisLow),Ee=te(be).slice();Ee[1]=v+(u?D.pelvisHigh:Te),le={f:g,s:be};let De=ne(m,a),Oe=L(m),ke=e(De,p(_(Oe),b)),Ae=-(Nc[0]+(Nc[1]-Nc[0])*vc(O))*(u?.5:1),je=Math.max(2,Math.round(.2*D.period)),Me=Cc(ke,Oe,Ae,De),Ne=e(Me,n(Hc(Oe),-.12*Math.abs(m-B[a])));Ne[1]+=.03,!u&&de>1&&g-de>S+1&&d(g-de,{[`foot_${a}`]:Ne,[`fpitch_${a}`]:.6*Ae});for(let e=0;e<=je;e++){let t=Ae*(1-Wc(e/je));d(g+e,{[`foot_${a}`]:Cc(ke,Oe,t,De),[`fpitch_${a}`]:t,[`fyaw_${a}`]:Oe})}x[a]=g+je,se[a].push({a:g,b:1/0,strike:!0}),V[`foot_${a}`]=De,V[`fyaw_${a}`]=Oe,B[a]=m;let Pe=(u?0:i<3?[0,.3,.7][i]:1)*H.arm[i],Fe=a===`l`?1:-1,Ie=c(C,.4,1.1),Le=u?0:i===0?.5:1,Re=4.5*Ie*Le*(o===`l`?1:-1),ze=(g-S)/D.period,Be=e=>{let t=[Math.cos(Math.PI*(e-ze)),Math.cos(Math.PI*e)];return t.map(e=>e/Math.max(...t.map(Math.abs)))},Ve=Be(-A).map(e=>-4.5*Ie*Fe*Pe*e),He=Be(k).map(e=>5.5*Ie*Fe*Pe*e),Ue=Be(.92).map(e=>-Math.sign(Re)*j*Ie*Le*e),We=Be(ze).map((e,t)=>t?.8*e:e),Ge=e=>({aswing_l:-.85*D.armSwing*Fe*Pe*e,aswing_r:D.armSwing*Fe*Pe*e,abend_l:Pe*(P-Fe*ee*e),abend_r:Pe*(P+Fe*ee*e)});d(S,{root:we,yaw:L(Se)+Ve[0],roll:ie+Re,spine_roll:ae-Re*1.26+Ue[0],spine_yaw:He[0]-Ve[0],...Ge(We[0]),wflex_l:8*Pe,wflex_r:8*Pe}),d(g,{root:Ee,yaw:L(be)+Ve[1],roll:ie,spine_roll:ae+Ue[1],spine_yaw:He[1]-Ve[1],...Ge(We[1]),head_yaw:0});let Ke=oe+N*Le,qe=M*Ie*Le;d(Math.max(r.f0+1,g-Math.round(Vc[1]*D.period)),{spine_pitch:Ke-.6*qe}),d(Math.max(r.f0+1,g-Math.round(Vc[0]*D.period)),{spine_pitch:Ke+qe}),ce=m}let ue=r.f0+16+D.steps*D.period+18,de=Math.max(...u.map(e=>e.f));return r.pelvisHigh===void 0&&r.pelvisLow===void 0&&(Mc(u,{end:de,ground:se}),Ec(o,u,{f0:r.f0,end:de,period:D.period,pace:O,ground:se,start:`${s} start`}),Dc(o,u,{end:de,period:D.period,pace:O,ground:se}),Mc(u,{end:de,ground:se,sk:o})),l=`${s} end`,d(ue,Gc(u,ue)),u}function qc(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(Kc(e,n,t,`generators[${r}] walk`))}),n}let Jc={yaw:.5,pitch:.2,roll:.7},Yc={yaw:4,pitch:6,roll:10},Xc=[`yaw`,`pitch`,`roll`];function Zc(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 Qc=(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 $c(e,t,n){let r=Zc(t);if(!r.length)return;let i=t=>e[t]??Array(n).fill(0);for(let a of Xc){let o=i(`spine_${a}`),s=i(a).map((e,t)=>e+o[t]),c=Qc(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(-Yc[a],Math.min(Yc[a],s[t]-c[t]));l[t]-=Jc[a]*r*n*n*(3-2*n)}}l&&(e[`head_${a}`]=l)}}function el(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 tl=e=>Math.round(e*1e4)/1e4;function nl(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(q)){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=el(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:tl(Math.max(...u)),how:`hips lowered`}}function rl(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:tl(n),how:`wrist target clamped to reach`}:null}let il=.01,al=.08,ol=.002,sl=[.25,.5,.75,1],cl=e=>e*e*(3-2*e);function ll(e,t,n,r){if(!(t[`armw_${n}`]>=.5))return null;let i=null;for(let[e,r]of Object.entries(t)){let t=/^w_(.+)_([lr])$/.exec(e);t&&t[2]===n&&r>=.5&&(!i||r>i.w)&&(i={w:r,name:t[1]})}let a=i&&e.spec.anchors?.[i.name];return a&&(a.kind===`world`||a.kind===`object`&&!e.objectHand?.(a.object,r))?i.name:null}function ul(e,t,n,r){let{s:i}=e.frames[t-e.f0],a=e.ctx.sk.index,o=e.ctx.scene?.objects??{},s=Object.entries(e.objectsAt(t-e.f0)??{}).filter(([,e])=>e.half&&!e.ignore),c=e=>r!==void 0&&(e===r||o[e]?.ancestors?.includes(r)),l=0;for(let[e,t,r,o]of ft){if(a[t]===void 0||a[r]===void 0)continue;let u=pt(i.P[a[t]],i.P[a[r]],5);for(let[t,r]of s)e===`hand_${n}`&&c(t)||(l+=Math.max(0,o-Math.min(...u.map(e=>U(e,r)))))}return l}function dl(e,t,n){let r=0;return Mi(e,{frames:t},n,(e,t,n,i,a)=>{r+=i-a}),r}function fl(e,t,n,r){if(r?.prop)return n.props?.[r.prop]?.[r.point]??null;let i=(r?.joints??[`RightHandIndex2`,`RightHandMiddle2`]).map(n=>t.P[e.index[n]]).filter(Boolean);return i.length?i.reduce((e,t)=>e.map((e,n)=>e+t[n]/i.length),[0,0,0]):null}function pl(e,t,n){let r=[];e.frames.forEach((i,a)=>{let o=e.f0+a;if(!(i.diag[`arm_miss_${t}`]>n)||i.diag[`arm_fold_${t}`]||!ll(e.ctx,i.c,t,o))return;let s=r.at(-1);s&&o-s[1]<=3?s[1]=o:r.push([o,o])});let i=r=>r>=e.f0&&r<e.f0+e.frames.length&&e.frames[r-e.f0].diag[`arm_miss_${t}`]>n&&!e.frames[r-e.f0].diag[`arm_fold_${t}`];for(let e of r)for(let t=0;t<10&&i(e[1]+1);t++)e[1]+=1;return r}function ml(e,n){let{ctx:i}=e,{sk:o}=i,c=o.index,l=e.frameCount,u=e.baked,d=i.spec.checks?.limits?.reach??.01,f=t=>e.frames[t-e.f0],p=e.f0,m=e.f0+e.frames.length-1,h=[],g={pitch:new Float64Array(l),move:new Float64Array(l)},_=[];for(let v of[`r`,`l`])for(let[y,b]of pl(e,v,d)){let d=e=>f(e).s.P[c[`${q[v]}Arm`]],x=e=>{let{s:t,diag:n,c:r}=f(e),a=ll(i,r,v,e);return a?qn(i,a,t,{...n,frame:e},r):null},S=e=>f(e).c[`armw_${v}`]>=.99&&f(e).c[`w_${ll(i,f(e).c,v,e)}_${v}`]>=.99,C=Array.from({length:b-y+1},(e,t)=>y+t),w=C.some(S)?C.filter(S):C,T=-1,E=null;for(let e of w){let n=x(e);n&&(!E||a(t(n,d(e)))>a(t(E,d(T))))&&(T=e,E=n)}if(!E)continue;let{s:D,diag:O,c:k}=f(T),A=v===`l`?`r`:`l`,j=(i.spec.fits??[]).find(e=>e.side===v&&e.anchor===ll(i,k,v,T)),M=j&&fl(o,D,O,j.effector),N=Math.max(M?a(t(M,E)):0,O[`arm_miss_${v}`]??0),P=Gn(o,v)+(M?a(t(M,D.P[c[`${q[v]}Hand`]])):0),ee=d(T),F=(k.yaw??0)*Math.PI/180,I=[Math.sin(F),0,Math.cos(F)],L=[E[0]-ee[0],0,E[2]-ee[2]],R=a(L)>.001?s(L):I,z=r(R,I)>.3&&E[1]<ee[1]+.1?25:0,te=.15*Math.min(1,Math.max(0,(ee[1]-E[1]-.2)/.4)),ne=Math.max(0,1-Math.max(g.pitch[T]/25,g.move[T]/al));if(!ne)continue;u.root||(u.root=Array.from({length:l},()=>o.restWorldPos[c.Hips].slice()),_.push(`root`)),u.spine_pitch||(u.spine_pitch=Array(l).fill(0),_.push(`spine_pitch`));let B=u.root.map(e=>e.slice()),V=u.spine_pitch.slice(),re=(e,t)=>{u.root[e]=[B[e][0]+R[0]*al*t,B[e][1]-te*t,B[e][2]+R[2]*al*t],u.spine_pitch[e]=V[e]+z*t},ie=(e,t)=>[`l`,`r`].every(n=>!((e[`leg_miss_${n}`]??0)>Math.max(t[`leg_miss_${n}`]??0,5e-4)+ol)),ae=(e,t)=>!(f(T).c[`armw_${A}`]>.5)||(e[`arm_miss_${A}`]??0)<=(t[`arm_miss_${A}`]??0)+ol,oe=i.spec.anchors[ll(i,k,v,T)]?.object,se=f(T),ce=ul(e,T,v,oe),le=r=>{re(T,r),n.solve([T]);let{s:i,diag:s}=f(T),c=j&&fl(o,i,s,j.effector),l={t:r,miss:Math.max(c?a(t(c,E)):0,s[`arm_miss_${v}`]??0),ok:ie(s,O)&&ae(s,O)&&ul(e,T,v,oe)<=ce+ol,bent:a(t(E,d(T)))<=.97*P};return u.root[T]=B[T],u.spine_pitch[T]=V[T],e.frames[T-e.f0]=se,l},H=null,ue=0;for(let e of sl.map(e=>e*ne)){let t=le(e);if(!t.ok)break;if((!H||t.miss<H.miss)&&(H=t),t.miss<=il&&t.bent){let t=ue,n=e;for(let e=0;e<4;e++){let e=le((t+n)/2);e.ok&&e.miss<=il&&e.bent?(n=e.t,H=e):t=e.t}break}ue=e}if(!H||!(H.miss<N-il))continue;let de=(e,t)=>Math.min(1,Math.max(0,u[e]?.[t]??1)),fe=ll(i,k,v,T),pe=[],me=e=>de(`armw_${v}`,e)*de(`w_${fe}_${v}`,e)*(e>=y&&e<=b?1:cl(1-(e<y?y-e:e-b)/11));for(let e=Math.max(p,y-10);e<=Math.min(m,b+10);e++)re(e,H.t*me(e)),pe.push(e);let he=pe.map(e=>f(e)),ge=pe.map(t=>ul(e,t,v,oe));n.solve(pe);let _e=e=>he[e-pe[0]],ve=(e,n)=>{let r=x(n),i=j&&r&&fl(o,e.s,e.diag,j.effector);return Math.max(i?a(t(i,r)):0,e.diag[`arm_miss_${v}`]??0)},ye=e=>C.reduce((t,n)=>t+(e(n).diag[`arm_miss_${v}`]??0),0),be=e=>Math.max(...w.map(t=>ve(e(t),t))),xe=pe.map(t=>ul(e,t,v,oe)),Se={...Yi,...i.spec.checks?.limits},Ce=Array.from({length:Math.min(m,pe.at(-1)+3)-Math.max(p,pe[0]-3)+1},(e,t)=>Math.max(p,pe[0]-3)+t),we=dl(o,Ce.map(e=>f(e)),Se)>dl(o,Ce.map(e=>e>=pe[0]&&e<=pe.at(-1)?he[e-pe[0]]:f(e)),Se)+1e-4,Te=pe.every((e,t)=>ie(f(e).diag,he[t].diag)&&ae(f(e).diag,he[t].diag)&&xe[t]<=ge[t]+ol),Ee=be(_e),De=be(f);if(!Te||we||!(ye(f)<ye(_e)-1e-6)||!(De<Ee-il/2)){for(let e of pe)u.root[e]=B[e],u.spine_pitch[e]=V[e];pe.forEach((t,n)=>{e.frames[t-e.f0]=he[n]});continue}for(let e of pe)g.pitch[e]+=z*H.t*me(e),g.move[e]+=al*H.t*me(e);h.push({side:v,frames:[y,b],lean:tl(z*H.t),shift:[tl(R[0]*al*H.t),tl(R[2]*al*H.t)],drop:tl(te*H.t),miss:[tl(Ee),tl(De)]})}if(!h.length)for(let e of _)delete u[e];return h.length?{runs:h,how:`an out-of-reach hand on a pinned target: the body leaned (spine_pitch deg), shifted its hips (x, z m) and lowered them (m) toward it, eased in and out; the feet kept their targets`}:null}let hl=.01;function gl(n,r){let i=n.frames.length,a=Yi.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)=>Ci(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>=hl)for(;u<i&&f[u][r][1]<.03;u++)v(u);if(d>2e-4&&(d<hl||g)){o++,c=Math.max(c,d),d>=hl&&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:tl(c),how:s?`planted heel/ball held in place; turning feet pivot on it`:`planted heel/ball held in place`}):null}let _l=e=>e*e*(3-2*e);function vl(e,t){let n=e.frames.length,r=Yi.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)=>Ci(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-_l((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:tl(a)}):null}function yl(e,t){let{sk:n}=e.ctx,r=e.frames.length,i=new Set,a=0;for(let[t,o]of Object.entries(q)){let s=e.baked[`foot_${t}`],c=n.index[`${o}Foot`];if(!s)continue;let l=e.frames.map(({s:t},r)=>{let i=vt(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=el(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:tl(a),how:`soles lifted onto the floor`}):null}let bl={l:ft.filter(e=>e[0]===`hand_l`||e[0]===`farm_l`),r:ft.filter(e=>e[0]===`hand_r`||e[0]===`farm_r`)};function xl(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 bl[r]){let r=Xi(a,e,t)??Yi.allow,p=pt(l.P[c[n]],l.P[c[d]]),m=p.map(e=>U(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(yt(u[1],s),u[0]+.002)))}if(p){if(!(d[`armw_${r}`]>1e-4)){o.skipped++;continue}u[s]=p}}return u}function Sl(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=xl(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:tl(u),skipped:c.skipped,tooDeep:c.deep,how:`hand pushed out along the box normal`}}let Cl=e=>{let t=e.ctx.spec?.checks?.limits;return Hi(e.ctx.sk,e,t,{...Yi,...t})};function wl(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 Tl(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 El(e,t){let n=e.frames.length,r=[],i=0,a=1;for(let o=0;o<3;o++){let o=Cl(e),s=[...new Set(e.keys.map(e=>e.f))].sort((e,t)=>e-t),c=wl(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=Tl(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=Cl(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 Dl=Array.from({length:19},(e,t)=>-90+t*10),Ol=.003,kl=.08;function Al(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 jl={l:`Left`,r:`Right`};function Ml(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(ft.map(e=>[e[0],e[3]]));function Pl(e,t,n,r){let i=0;for(let a of e)i=Math.max(i,r-Math.min(...pt(t,n,5).map(e=>U(e,a))));return i}function Fl(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(We(n,r,i,l,d,m),We(n,r,i,u,m,f)),g=a===`Left`?`l`:`r`;return{depth:h,scene:s.length?Math.max(Pl(s,d,m,Nl[`uarm_${g}`]),Pl(s,m,f,Nl[`farm_${g}`])):0,rise:Math.max(0,m[1]-(d[1]-.09)),under:d[1]-m[1]}}let Il=(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 Ll(n,r,i){let{sk:o}=n.ctx,s=jl[i],c=n.frames.length,l=n.ctx.swivel[i],u=n.frames.map(({s:c,c:u},d)=>{let f=gs(u[`armw_${i}`]??0),m=(l.get(n.f0+d)??0)*f;if(!m&&Fl(r,o,c,s,0).depth<=Ol)return null;let h=c.P[o.index[`${s}Arm`]],g=Il(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=Fl(r,o,c,s,-m,_).scene,y=Al(n,d,s);return Dl.map(e=>{let{depth:t,scene:n,rise:i,under:a}=Fl(r,o,c,s,e*f-m,_),l=Math.max(0,t-Ol)*100,u=y===null?0:Math.max(0,y-a),d=Math.max(0,n-Math.max(v,Ol))*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=Dl.indexOf(0),f=Dl.map((e,t)=>t===d?0:1/0),m=Dl.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<Dl.length;e++){if(Math.abs(Dl[t]-Dl[e])>10)continue;let r=n[e]+2*((Dl[t]-Dl[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]=Dl[_],_=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 Rl(e,t,r,i){let{sk:o}=e.ctx,s=jl[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-Ml(e.ctx.spec,p,r);if(h<.05)continue;let g=null;for(let n of[c[`${s}Hand`],c[`${s}ForeArm`]]){let r=Ge(t,o,l,n);if(r.into<0||d.includes(r.into))continue;let i=Math.min(.03,Xi(e.ctx.spec.checks?.allow,gi(o,n),gi(o,r.into))??0),a=r.depth-Math.max(i,Ol);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(_>=kl){i.deep++;continue}let v=Pe(t,l,g.p,new Set([...t.skip.get(g.b),...d]));v&&(u[f]=n(v,h*Math.min(.03,kl-_,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 zl(t,r){let i=ke(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=Ll(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`])Rl(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:tl(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 Bl=[`reach`,`slide`,`penetration`,`self`,`retime`,`stance`,`grasp`];function Vl(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 Hl=(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,Ul=(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 Wl(e,t,n,{whole:r=!0}={}){let i={},a=(e,t)=>{t&&(i[e]=t)};return t.retime&&r&&a(`retime`,El(e,n)),t.slide&&a(`ground`,yl(e,n)),t.reach&&a(`reachLegs`,nl(e,n)),t.reach&&a(`reachBody`,ml(e,n)),t.slide&&a(`slide`,Hl(vl(e,n),gl(e,n))),t.reach&&i.slide?.pivots&&a(`reachLegs`,Ul(i.reachLegs,nl(e,n))),t.penetration>0&&a(`penetration`,Sl(e,n,t.penetration)),t.self&&a(`self`,zl(e,n)),t.reach&&a(`reachArms`,rl(e)),i}function Gl(e){let t=bt(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 Kl(e,t,n){let r=e?.objects?.[n]?.mass;if(Number.isFinite(r))return r;let i=K(t,n).map(e=>t.objects[e]);if(i.length===1&&bt(i[0]).kind===`sphere`&&2*bt(i[0]).r<.12)return .15;let a=i.reduce((e,t)=>e+Gl(t)*400,0);return Math.min(60,Math.max(.02,a))}let ql=9.81,Jl=e=>e?.fix!==!1&&e?.fix?.grasp!==!1,Yl=e=>{let t=Math.min(1,Math.max(0,e));return t*t*(3-2*t)},Xl=e=>Object.entries(e.objects??{}).filter(([,t])=>Os(t)&&e.props?.[t.prop]).map(([t,n])=>[t,n,e.props[n.prop].hand]);function Zl(r,i,o,c,l,u){let{spec:d,scene:f,sk:p}=r;if(!Jl(d)||!(u>.001))return null;for(let[m,h,g]of Xl(d)){if(g!==i||h.keys?.length)continue;let _=Kl(d,f,m),[v,y]=r.carrySpan(h);if(o<v||o>y||_*ql*Gn(p,i)<2)continue;let b=Yl((o-v)/4)*Yl((y-o)/4)*u,x=Math.min(.06,.006*_)*b;if(!(x>0))return null;let S=t(c.P[p.index[`${q[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 Ql=(e,n,r,i)=>{let a=t(n.P[e.index[`${q[r]}Hand`]],n.P[e.index[`${q[r]}Arm`]]);return i*ql*Math.hypot(a[0],a[2])};function $l(e,t,n){let{spec:r,scene:i}=t.ctx,a=t.f0??0;if(!Jl(r))return;let o=Xl(r).map(([e,t,n])=>({id:e,o:t,k:n,mass:Kl(r,i,e)}));o.length&&t.frames.forEach(({s:r},i)=>{let s=i+a;for(let i of o)rs(t,i.id,s)===i.k&&n({...i,f:s,Nm:Ql(e,r,i.k,i.mass)})})}function eu(e,t){let n={};$l(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 tu(e,t,n,r,i){let a=r===`r`?`l`:`r`,{spec:o,scene:s}=e.ctx,c=new Set([t,...K(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 nu(e,t){let n=[];return $l(e,t,({id:e,o:r,k:i,f:a,Nm:o})=>{o>25&&!tu(t,e,r,i,a)&&n.push({kind:`LOAD`,msg:`${q[i].toLowerCase()} holds ${e} at ${o.toFixed(0)} N m`,frame:a,value:Math.round(o*10)/10,limit:25})}),n}function ru(r,i,o){let{sk:s,scene:c}=r.ctx,l=r.frames[o-(r.f0??0)]?.s,u=is(r,i,o),d=jt(c,i),f=c.objects[K(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 iu={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]=Wo(e.out,i),c=Kl(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=ru(e.out,r,Math.max(s,Math.min(t.worstFrame,Wo(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,Wo(e.out,i)[1]-s),release:1}}}]}}}},au=e=>e*e*(3-2*e);function ou(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=>js(e.objects?.[t])),l=e.objects?.[a];c[r]=l?Os(l)||ks(l)?{...i,carried:a}:yo(l)?{...i,...Y(Lo({spec:e,scene:t,timeMap:o},a,s),Fs(t.objects[a]??Cn(t)[a],i))}:{...i,...Is(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 su(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 cu(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(qn(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])=>au(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-Ml(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=Ve(i.keepOut.body,f,a,s,He,i.keepOut.skip);if(r===s)break;s=e(s,n(t(r,s),c))}return s}:void 0,afterArm:(e,t,n)=>nr(i,e,t,n),grip:l?void 0:(e,t,n)=>Xs(i,f,t,q[e],e,o,c,n),sag:l?void 0:(e,t,n)=>Zl(i,e,c,t,n,(1-Ml(i.spec,o,e))*Math.min(1,Math.max(0,o[`armw_${e}`]??0))),heldShapes:l||!i.grasp?void 0:(e,t)=>fs(i,o,c,e,t)}}function lu(n,r,i,o=!0){let s={...cc(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=Ea(n.spec,r.anchor),m=n.sk.index[`${q[r.side]}Arm`],h=n.sk.index[`${q[r.side]}Hand`];for(let s=0;s<6;s++){l[`fit_${r.anchor}_${r.side}`]=u;let{s,diag:c}=Ds(n.sk,l,cu(n,l,i,!0,o,r.side)),g=su(n.sk,s,c,r.effector),_=e(qn(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))>Gn(n.sk,r.side)+.02&&(u=d.hand),s[`fit_${r.anchor}_${r.side}`]=u}let{s:c,diag:l}=Ds(n.sk,s,cu(n,s,i,!1,o));return l.frame=i,{s:c,diag:l,c:s}}function uu(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=Vl(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?Ue(t):null},l={ctx:c,fps:e.fps??30,frameCount:i,f0:o,frames:[]};c.objectPose=(e,t,n)=>is(l,e,t,n),c.objectHand=(e,t)=>rs(l,e,t),c.restingAt=(e,t)=>Xo(l,e,t),c.carrySpan=e=>Wo(l,e),c.solvedAt=e=>Qo(l,e),c.weightAt=(e,t)=>l.baked?.[e]?.[t],c.frameCount=i;let u={rebake(r){l.keys=r,l.baked=Ur(r,i,Br(e.flow,e.label??``,jr(e))),c.socketTurns=new Map,c.graspPlans=new Map,$c(l.baked,r,i),c.idle=sc(l.baked,{spec:e,sk:t,frameCount:i}),c.busy=oc(l.baked,e,i,l.fps);let a=ou(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]=lu(c,Wr(l.baked,t),t))},all:()=>Array.from({length:s-o+1},(e,t)=>o+t)};return c.hold=Ua(c,l,e=>lu(c,Wr(l.baked,e),e,!1),e=>lu(c,Wr(l.baked,e),e)),u.rebake(r.keys??qc(e,t)),u.solve(u.all()),l.fixed=r.fixes===!1?{}:Wl(l,a,u,{whole:!r.window}),c.hold&&l.timeMap&&u.solve(u.all()),l}let du=`{ f, pos: [x, y, z], rot: [x, y, z, w] } or { f, pos, aim: [x, y, z], roll? }`,fu=`{ at, onto?, bounce?, tumble?, v? }`,pu=e=>Array.isArray(e)&&e.length===4&&e.every(Number.isFinite)&&Math.abs(Math.hypot(...e)-1)<=.01,mu=e=>un(e)&&Math.hypot(...e)>1e-9;function hu(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)`),pu(n.rot)||e(`${t}.rot must be a unit quaternion [x, y, z, w], got ${W(n.rot)}${n.rot===void 0?`; or give aim [x, y, z], the world direction its +Y points`:``}`);return}mu(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 ${W(n.aim)}`),n.roll!==void 0&&!Number.isFinite(n.roll)&&e(`${t}.roll must be degrees about aim (a number), got ${W(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 ${W(n.lean)}`)}function gu(e,t,n,r,i,a=!1){if(!Array.isArray(r.keys))return e(`${n}.keys must be an array of ${du}`);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 ${du}, got ${W(r)}`);for(let e of Object.keys(r))Ht.includes(e)||t(`${o} has unknown field "${e}"${G(e,Ht)}; a key is ${du}`);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 ${W(r.f)}`),un(r.pos)||e(`${o}.pos must be [x, y, z], the world position of the object's base (a group: its root), got ${W(r.pos)}`),hu(e,o,r)})}function _u(e,t,n,r,i){let a=/^object:(.+?)(?:\.(?:center|top|base))?$/s.exec(i)?.[1];if(a===void 0)return;let o=K(n,a).flatMap(t=>[t,...n.objects[t].ancestors??[]].find(t=>js(e.objects?.[t]))??[]).find(t=>ks(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 vu(e,t,n,r,i){fr(e,t,n,`${r}.follow`,i.follow)&&_u(e,t,n.scene,`${r}.follow`,i.follow),i.offset!==void 0&&!un(i.offset)&&t(`${r}.offset must be [x, y, z], metres along the record's own axes after aim (+Y along aim), got ${W(i.offset)}`);let a=i.aim;if(a!==void 0&&a!==`face`&&a!==`up`){if(Array.isArray(a)){mu(a)||t(`${r}.aim must be a non-zero world direction [x, y, z], got ${W(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 ${W(a)}`);fr(e,t,n,`${r}.aim`,a)&&_u(e,t,n.scene,`${r}.aim`,a)}}function yu(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 ${W(n)}`),!1)}function bu(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 ${fu} 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 ${fu} (at: the frame it starts to fall), got ${W(s)}`);for(let e of Object.keys(s))Vt.includes(e)||t(`${u} has unknown field "${e}"${G(e,Vt)}; a fall is ${fu}`);if(s.at===void 0)e(`${u}.at must be the whole frame it starts to fall`);else if(yu(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&&bn(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 ${W(s.bounce)}`),s.v!==void 0&&!(o&&s.v===`hand`||un(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 ${W(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 ${W(s.tumble)}`)})}let xu=(e,t)=>{let n=0;for(;n<e.length&&e[n]===t[n];)n+=1;return n};function Su(e){let t=String(e),n=tn.reduce((e,n)=>xu(n,t)>xu(e,t)?n:e);return xu(n,t)>=4?`; did you mean "${n}"?`:G(t,tn)}function Cu(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 ${W(n.mass)}`)}function wu(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 Tu(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?ks(s)?`follow`:yo(s)?`free`:As(s)?`switch`:`hinge`:`carry`,u=Bt[l];(l!==`switch`&&l!==`follow`||!r?.others?.[o])&&bn(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}"${G(e,u)}; fields: ${u.join(`, `)}`);if(Cu(t,c,s),l===`follow`){vu(e,t,{sk:n,scene:r},c,s),wu(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`),wu(t,c,s);continue}if(l===`free`){s.keys!==void 0&&gu(t,a,c,s,i,!0),s.fall!==void 0&&bu(t,a,c,s,{frames:i,scene:r,carry:!1}),wu(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&&!un(s[e])&&t(`${c}.${e} must be [d, l, n] along the palm axes, got ${W(s[e])}`);s.grasp!==void 0&&!tn.includes(s.grasp)&&t(`${c}.grasp must be one of ${tn.join(`, `)}${Su(s.grasp)}, got ${W(s.grasp)}`),wu(t,c,s),yu(t,`${c}.grab`,s.grab,i)&&yu(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&&gu(t,a,c,s,i),s.fall!==void 0&&bu(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)`),un(s?.pivot)||t(`${c}.pivot must be [x, y, z] (world hinge point), got ${W(s?.pivot)}`),s?.axis!==void 0&&(!un(s.axis)||Math.hypot(...s.axis)<1e-9)&&t(`${c}.axis must be a non-zero [x, y, z], got ${W(s.axis)}`),(typeof s?.channel!=`string`||!s.channel)&&t(`${c}.channel must name the angle channel (degrees)`)}}let X=e=>{let t=Math.round(e*1e3)/1e3;return Object.is(t,-0)?0:t},Eu=e=>`[${e.map(e=>{let t=X(e);return t>0?`+${t}`:`${t}`}).join(`, `)}]`,Du=[`near`,`above`,`moved`,`still`,`speed`],Ou={near:.05,above:0,moved:.05,still:.01},ku=new Set([`near`,`still`]),Au=new Set([`moved`,`still`,`speed`]),ju=e=>Du.filter(t=>e?.[t]!==void 0),Mu=(e,t)=>e.name??`goals[${t}]`,Nu=(e,t)=>(ku.has(t)?e.tol:e.by)??Ou[t],Pu=(e,t)=>e.mode??(t===`speed`?`any`:`all`),Fu=e=>!e.startsWith(`object:`)||/\.(center|top|base|rim)$/.test(e)?e:`${e}.base`;function Iu(t,n){let[,r,i]=/^object:(.+)\.(center|top|base|rim)$/s.exec(Fu(n))??[],a=K(t,r).map(e=>t.objects[e]);if(!a.length)return null;if(i===`rim`)return or(a).o;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 Lu=e=>e.moved??e.still,Ru=(e,t,n=Array.isArray(t.at)?t.at[0]:t.at)=>Lu(t).startsWith(`object:`)?Iu(e.ctx.scene,Lu(t)):cr(e,n,Lu(t));function zu(e,n,r){let i=cr(e,r,n),o=e.fps??24;if(!i)return null;let s=cr(e,r-1,n),c=cr(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}let Bu=`landmark:mouth`,Vu=/^object:(.+)\.top$/s;function Hu(e,t){let[n,r]=t.near,i=[n,r].map(e=>Vu.exec(e)?.[1]),a=i[0]&&r===Bu?0:i[1]&&n===Bu?1:-1;if(a<0)return t.near;let o=K(e,i[a]).map(t=>e.objects[t]).filter(e=>e?.half&&e.pos&&e.R);return(o.length?o.reduce((e,t)=>t.pos[1]+2*t.half[1]>e.pos[1]+2*e.half[1]?t:e):null)?.renderer===`cylinder`?t.near.map((e,t)=>t===a?`object:${i[a]}.rim`:e):t.near}function Uu(e,t){let n=t.near.find(e=>Vu.test(e));if(!n)return``;let r=Vu.exec(n)[1];return Hu(e,t)===t.near?` (${n} is the top-face centre of its world bounding box; object:${r}.rim is its opening, which tilts with it)`:` (measured at ${r}'s rim nearest the mouth, object:${r}.rim; its .top is its bounding box's top)`}function Wu(e,n,r,i,o){if(r===`near`){let[t,r]=Hu(e.ctx.scene,n);return lr(e,i,t,r)}if(r===`above`){let t=cr(e,i,n.above[0]),r=cr(e,i,n.above[1]);return t&&r?t[1]-r[1]:null}if(r===`speed`)return zu(e,n.speed,i);let s=o&&cr(e,i,Fu(Lu(n)));return s?a(t(s,o)):null}let Gu=(e,t,n)=>ku.has(e)?t-n:n-t,Ku=e=>Math.max(1e-4,Math.round(e*1e4)/1e4);function qu(e,t,n,r,i){let a=Nu(t,n),o=(n===`moved`||n===`still`)&&!Lu(t).startsWith(`object:`),s=n===`moved`||n===`still`?Ru(e.out,t,r):null,c=[];for(let o=Math.max(r,e.f0);o<=Math.min(i,e.last);o++){let r=Wu(e.out,t,n,o,s);r!==null&&c.push({f:o,v:r,short:Gu(n,r,a)})}return{rows:c,from:o?r:void 0}}function Ju(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=ju(n)[0],i=r&&Yu(e,t),a;if(i&&!i.wait){let{rows:t}=qu(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 Yu(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=Ju(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 Xu(e,t,n,r,i,a,o=``){let s=i?`f${n.f}, the best of f${i[0]}-f${i[1]}`:`f${n.f}`;return t===`near`?`${e}: ${X(n.v)} m at ${s} (needs <= ${r})${o}`:t===`above`?`${e}: ${X(n.v)} m above at ${s} (needs >= ${r})`:t===`speed`?`${e}: ${X(n.v)} m/s at ${s} (needs >= ${r})`:t===`still`?`${e}: ${X(n.v)} m from ${a===void 0?`its scene pose`:`f${a}`} at ${s} (needs <= ${r})`:`${e}: moved ${X(n.v)} m${a===void 0?``:` from f${a}`} at ${s} (needs >= ${r})`}function Zu(e,t,n){let r=e.f0??0;return Yu({out:e,goals:t,names:new Map(t.map((e,t)=>[Mu(e,t),t])),f0:r,last:r+(e.frames?.length??0)-1,events:new Map,visiting:new Set},n)}function Qu(e,t){let n=[],r=[],i=e.f0??0,a={out:e,goals:t,names:new Map(t.map((e,t)=>[Mu(e,t),t])),f0:i,last:i+(e.frames?.length??0)-1,events:new Map,visiting:new Set};return t.forEach((t,o)=>{let s=ju(t)[0];if(!s)return;let c=Mu(t,o),l=Nu(t,s),u=Yu(a,o),d=Array.isArray(t.at)?t.at[0]:t.at;if(u.skip)return;if(u.wait){r.push({name:c,kind:s,ok:!1,value:null,at:null,need:l}),d>=i&&d<=a.last&&n.push({kind:`GOAL`,msg:`${c}: ${u.wait}`,frame:d-i,value:.01,limit:0});return}let{rows:f,from:p}=qu(a,t,s,u.a,u.b);if(!f.length)return;s===`moved`&&p!==void 0&&(f=[f.reduce((e,t)=>t.v>e.v?t:e)]);let m=Pu(t,s)===`any`,h=Array.isArray(t.at)?u.a!==u.b:!1,g=f.reduce((e,t)=>(m?t.short<e.short:t.short>e.short)?t:e),_=g.short<=0;if(r.push({name:c,kind:s,ok:_,value:X(g.v),at:g.f,need:l}),_)return;let v=s===`near`?Uu(e.ctx.scene,t):``;for(let e of m?[g]:f.filter(e=>e.short>0))n.push({kind:`GOAL`,msg:Xu(c,s,e,l,m&&h?[u.a,u.b]:null,p,s===`near`?v:``),frame:e.f-i,value:Ku(e.short),limit:0})}),{issues:n,results:r}}let $u=`{ name?, near | above | moved | still | speed, tol?, by?, at, mode?, after?, before? }`,ed=new Set([`near`,`still`]),td=new Set([`above`,`moved`,`speed`]);function nd(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 rd=e=>typeof e==`string`&&e!==``&&!/^[a-z]+:/.test(e),id=e=>[`moved`,`still`,`speed`].find(t=>rd(e?.[t]));function ad(e){return Array.isArray(e)&&e.some(id)?e.map(e=>{let t=id(e);return t?{...e,[t]:`object:${e[t]}`}:e}):e}function od(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 ${W(n)}`);(o[0]<0||o[1]>r-1)&&e(`${t}.at = ${W(n)} is outside 0..${r-1} (spec.frames ${r})${o[1]>r-1?i:``}`)}function sd(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 ${W(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 ${W(i.at)}`)}function cd(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 ${$u}, got ${W(r)}`);for(let e of Object.keys(r))Kt.includes(e)||c(`${o} has unknown field "${e}"${G(e,Kt)} (fields: ${Kt.join(`, `)})`);r.name!==void 0&&(typeof r.name!=`string`||!r.name.trim()?t(`${o}.name must be a non-empty string, got ${W(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=ju(r),u=l[0];if(l.length||t(`${o} needs one predicate: ${Du.join(`, `)}`),l.length>1&&t(`${o} names ${l.join(` and `)}; a goal takes exactly one of ${Du.join(`, `)} (split it into ${l.length} goals)`),l.length===1&&Au.has(u))sd(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 ${W(n)}`)}r.tol!==void 0&&(l.length===1&&!ed.has(u)?t(`${o}.tol applies to near and still; ${nd(u,r.tol)}`):Number.isFinite(r.tol)&&r.tol>=0||t(`${o}.tol must be a number >= 0 (metres), got ${W(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&&!td.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 ${W(r.by)}`),r.mode!==void 0&&r.mode!==`all`&&r.mode!==`any`&&t(`${o}.mode must be "all" or "any", got ${W(r.mode)}`),od(t,o,r.at,n.frames,n.past)}function ld(e,t){let n=new Map(e.map((e,t)=>[e&&typeof e==`object`?Mu(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 ${W(e[i])}${typeof e[i]==`string`?G(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=>`"${Mu(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 ud(e,t,n){if(e.goals===void 0)return;if(!Array.isArray(e.goals))return t(`spec.goals must be an array of goals ${$u}, got ${W(e.goals)}`);let r=new Map;e.goals.forEach((i,a)=>cd(e,t,n,i,a,r)),ld(e.goals,t)}let dd=new Set([`l`,`r`]),fd=e=>!!e&&typeof e==`object`&&!Array.isArray(e);function pd(e,t,n,r){if(fd(n))for(let i of Object.keys(n))r.includes(i)||e(`${t} has unknown field "${i}"${ln(i,r)} (fields: ${r.join(`, `)})`)}function md(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||!Lt[o.kind]){t(`${s}.kind must be one of ${Object.keys(Lt).join(`, `)}, got ${W(o?.kind)}`);continue}pd(i,`${s} (${o.kind})`,o,[`kind`,...Lt[o.kind]]);for(let e of[`p`,`offset`,`local`])o[e]!==void 0&&!un(o[e])&&t(`${s}.${e} must be [x, y, z], got ${W(o[e])}`);(o.kind===`world`||o.kind===`body`)&&o.p===void 0&&t(`${s} (${o.kind}) needs p: [x, y, z]${o.kind===`body`?`, ${sn.pole}`:``}`),o.kind===`bone`&&yn(t,n,s,o.bone),o.kind===`segment`&&(yn(t,n,`${s}.from`,o.from),yn(t,n,`${s}.to`,o.to),o.reach!==void 0&&!dd.has(o.reach)&&t(`${s}.reach must be "l" or "r"`)),o.kind===`object`&&bn(t,r,s,o.object,{groups:!0})&&(o.grasp!==void 0&&!tn.includes(o.grasp)&&t(`${s}.grasp must be one of ${tn.join(`, `)}, got ${W(o.grasp)}`),o.socket!==void 0&&r?.objects&&!Ln(r,o.object,o.socket)&&t(`${s}.socket: "${o.object}" has no socket ${W(o.socket)} (sockets: ${In(r,o.object).map(e=>e.name).join(`, `)||`none`})`),o.socket!==void 0&&o.grasp===void 0&&t(`${s}.socket needs grasp (${tn.join(`, `)}): the socket says how the hand takes it`)),o.kind===`person`&&!Dn(r,o.person,o.bone??``,0)&&t(`${s} (person) needs person: the id of another character with an installed take (people: ${On(r)}) and bone: one of its joints, got ${W(o.person)} ${W(o.bone)}`),o.kind===`landmark`&&o.name!==`mouth`&&t(`${s}: the only landmark is "mouth", got ${W(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 hd(e,t,{sk:n,unknown:r=t}){for(let[i,a]of Object.entries(e.props??{})){let e=`props.${i}`;pd(r,e,a,Rt),dd.has(a?.hand)||t(`${e}.hand must be "l" or "r", got ${W(a?.hand)}`),!Array.isArray(a?.origin)||!a.origin.length?t(`${e}.origin must list one or more bones`):a.origin.forEach(r=>yn(t,n,`${e}.origin`,r));for(let[n,r]of Object.entries(a?.points??{}))un(r)||t(`${e}.points.${n} must be [d, l, n] along the palm axes, got ${W(r)}`)}(e.fits??[]).forEach((i,a)=>{let o=`fits[${a}]`;pd(r,o,i,zt),dd.has(i?.side)||t(`${o}.side must be "l" or "r", got ${W(i?.side)}`),e.anchors?.[i?.anchor]||t(`${o}.anchor "${i?.anchor}" is not in spec.anchors${G(String(i?.anchor),Object.keys(e.anchors??{}))}`);let s=i?.effector;if(s?.prop!==void 0){let n=e.props?.[s.prop];n?s.point!==`origin`&&!n.points?.[s.point]&&t(`${o}.effector ${s.prop}.${s.point} is not a point of spec.props.${s.prop} (points: ${[...Object.keys(n.points??{}),`origin`].join(`, `)})`):t(`${o}.effector.prop "${s.prop}" is not in spec.props${G(String(s.prop),Object.keys(e.props??{}))}`)}else Array.isArray(s?.joints)?s.joints.forEach(e=>yn(t,n,`${o}.effector`,e)):t(`${o}.effector must be { prop, point } or { joints: [bone, ...] }`);i?.offset!==void 0&&!un(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 ${W(i.orient)}`)})}function gd(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 ${W(e?.type)}`);pd(r,o,e,Ut),(!Number.isInteger(e.f0)||e.f0<0||e.f0>n-1)&&t(`${o}.f0 must be an integer frame within 0..${n-1}, got ${W(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 (${sn.yaw}), got ${W(e.heading)}`);for(let n of Ut.slice(2))n!==`heading`&&e[n]!==void 0&&!Number.isFinite(e[n])&&t(`${o}.${n} must be a finite number, got ${W(e[n])}`);e.steps!==void 0&&!(Number.isInteger(e.steps)&&e.steps>=1)&&t(`${o}.steps must be an integer >= 1, got ${W(e.steps)}`),e.turnDistance!==void 0&&!(e.turnDistance>0)&&t(`${o}.turnDistance must be > 0 m, got ${W(e.turnDistance)}`)})}function _d(e,t,{sk:n,scene:r,frames:i,unknown:a=t,past:o=``}){let s=e.checks??{};for(let e of Object.keys(s))Wt.includes(e)||a(`checks.${e} is not a check${G(e,Wt)} (checks: ${Wt.join(`, `)})`);let c=(e,n=()=>{})=>{let r=s[e],i=`{ ${Gt[e].join(`, `)} }`;if(r!==void 0){if(!Array.isArray(r))return t(`checks.${e} must be an array of ${i}, got ${W(r)}`);r.forEach((r,o)=>{if(!fd(r))return t(`checks.${e}[${o}] must be ${i}, got ${W(r)}`);pd(a,`checks.${e}[${o}]`,r,Gt[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 ${W(n)}${Array.isArray(n)&&n[1]>i-1?o:``}`))};c(`contacts`,(r,i)=>{xn(e,t,n,`checks.contacts[${i}].point`,r.point),xn(e,t,n,`checks.contacts[${i}].target`,r.target),l(`checks.contacts[${i}]`,r.when)}),c(`orient`,(r,i)=>{xn(e,t,n,`checks.orient[${i}].from`,r.from),xn(e,t,n,`checks.orient[${i}].to`,r.to),l(`checks.orient[${i}]`,r.when),r.along===`face`||r.along===`up`||un(r.along)||t(`checks.orient[${i}].along must be "face", "up" or [x, y, z]`)}),c(`below`,(e,r)=>{yn(t,n,`checks.below[${r}].joint`,e.joint),yn(t,n,`checks.below[${r}].ref`,e.ref),l(`checks.below[${r}]`,e.when)}),c(`clearance`,(e,n)=>{bn(t,r,`checks.clearance[${n}]`,e.object),l(`checks.clearance[${n}]`,e.frames)}),c(`appear`,(r,i)=>{yn(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)=>bn(t,r,`checks.supports[${n}]`,e.object)),c(`allow`);for(let e of Object.keys(s.limits??{}))e in Yi||a(`checks.limits.${e} is not a QA limit${G(e,Object.keys(Yi))}`);let u=e.fix;if(u!==void 0&&u!==!1&&(typeof u!=`object`||!u))t(`spec.fix must be false or { ${Bl.join(`, `)} }`);else for(let e of Object.keys(u||{}))Bl.includes(e)||a(`spec.fix.${e} is not a fix${G(e,Bl)} (fixes: ${Bl.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 ${W(u.grasp)}`)}function vd(e,t,n){md(e,t,n),hd(e,t,n),Tu(e,t,n),gd(e,t,n),_d(e,t,n),ud(e,t,n)}function yd(e){let t=new Map;for(let n of e){let e=/\[(\d+)\]( \(f(\d+)\))?/.exec(n),r=e?`${n.slice(0,e.index)}[#]${e[2]?` (f#)`:``}${n.slice(e.index+e[0].length)}`:n;t.has(r)||t.set(r,{line:n,idx:[],fs:[]}),e&&(t.get(r).idx.push(e[1]),e[3]&&t.get(r).fs.push(`f${e[3]}`))}return[...t.entries()].map(([e,t])=>t.idx.length>1?e.replace(`[#]`,`[${t.idx.join(`,`)}]`).replace(` (f#)`,` (${t.fs.join(`, `)})`):t.line)}function bd(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 xd(e,t,n,r){if(Xt.has(n)){if(!un(r))return e(`${t} ${n} must be [x, y, z] (3 finite numbers), got ${W(r)}`);if(Zt.has(n)){let i=Math.hypot(...r);if(i<1e-6)return e(`${t} ${n} = ${W(r)} has no direction; a pole is a ${sn.pole}`);n.startsWith(`knee`)&&r[2]<-.7*i&&e(`${t} ${n} = ${W(r)} points backward; knees bend toward +z. A pole is a ${sn.pole}`)}n.startsWith(`foot`)&&r[1]<.04&&e(`${t} ${n} y = ${r[1]} puts the ankle into the floor; foot_* is the ${sn.foot}`);return}if($t.has(n)){if(!r||typeof r!=`object`||Array.isArray(r))return e(`${t} ${n} must be { preset: weight }, presets ${en.join(`, `)}; got ${W(r)}`);for(let[i,a]of Object.entries(r))en.includes(i)?Number.isFinite(a)&&a>=0&&a<=1||e(`${t} ${n}.${i} must be a weight within 0..1, got ${W(a)}`):e(`${t} ${n} preset "${i}" is unknown${G(i,en)} (presets: ${en.join(`, `)})`);return}if(!Number.isFinite(r))return e(`${t} ${n} must be a finite number${n===`yaw`?` (degrees; ${sn.yaw})`:n.startsWith(`fyaw`)?` (${sn.fyaw})`:``}, got ${W(r)}`);let i=on(n);i&&(r<i[1]||r>i[2])&&e(`${t} ${n} = ${r} is outside ${i[1]}..${i[2]} (${i[3]})`)}let Sd=e=>Array.isArray(e)?e:[],Cd=e=>Array.isArray(e)?e[1]:e;function wd(e,t){let n=[...Sd(e.keys).map(e=>e?.f),...Sd(e.generators).map(e=>e?.f0)].filter(Number.isInteger),r=e.checks??{},i=[...[`contacts`,`orient`,`below`].flatMap(e=>Sd(r[e]).map(e=>Cd(e?.when))),...Sd(r.clearance).map(e=>Cd(e?.frames)),...Sd(e.goals).map(e=>Cd(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 Td(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=bd(e),s=[...Xt,...Qt,...$t,...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))Ft.includes(t)||r(`${l} has unknown field "${t}"${ln(t,Ft)}; a key is { f, set, step? }`);if(!Number.isInteger(e.f))return t(`${l}.f must be an integer frame, got ${W(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]}"${G(n,s)}`):e||Xt.has(n)||Qt.has(n)||$t.has(n)?xd(t,u,n,r):o.has(n)||n.endsWith(`_vis`)?Number.isFinite(r)||t(`${u} ${n} must be a finite number, got ${W(r)}`):t(`${u} sets unknown channel "${n}"${G(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 Ed(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?, overlap? }, got ${W(e)}`);for(let t of Object.keys(e))[`smooth`,`lag`,`overlap`].includes(t)||n(`spec.flow has unknown field "${t}" (fields: smooth, lag, overlap)`);e.smooth!==void 0&&typeof e.smooth!=`boolean`&&t(`spec.flow.smooth must be true or false, got ${W(e.smooth)}`);let r=e.overlap;r!==void 0&&typeof r!=`boolean`&&!(Number.isFinite(r)&&r>=0&&r<=2)&&t(`spec.flow.overlap must be true, false or a scale 0..2, got ${W(r)}`);let i=e=>Number.isFinite(e)&&e>=0&&e<=6,a=e.lag;if(!(a===void 0||a===!1||i(a))){if(!a||typeof a!=`object`||Array.isArray(a))return t(`spec.flow.lag must be false, 0..6 frames or { spine?, head?, arms? }, got ${W(a)}`);for(let[e,r]of Object.entries(a))[`spine`,`head`,`arms`].includes(e)?i(r)||t(`spec.flow.lag.${e} must be 0..6 frames, got ${W(r)}`):n(`spec.flow.lag has unknown field "${e}" (fields: spine, head, arms)`)}}function Dd(e,{sk:t,scene:n,lenient:r=!1,problems:i=[]}={}){let a=[...i],o=e=>{a.length<60&&a.push(e)},s=r?()=>{}:o;if(!e||typeof e!=`object`)throw Error(`MOTION_SPEC_INVALID: spec must be an object`);e.schema!==`cozyclay.motion-spec.v1`&&o(`spec.schema must be "cozyclay.motion-spec.v1", got ${W(e.schema)}`);for(let t of Object.keys(e))Pt.includes(t)||s(`spec.${t} is not a spec field${G(t,Pt)} (fields: ${Pt.join(`, `)})`);e.fps!==void 0&&e.fps!==24&&o(`spec.fps must be 24, the timeline rate, got ${W(e.fps)}`),Ed(e.flow,o,s);let c=e.frames;(!Number.isInteger(c)||c<1||c>2e4)&&o(`spec.frames must be an integer 1..20000 or "auto", got ${W(c)}`);let l=Number.isInteger(c)?wd(e,c):``;if(Td(e,o,Number.isInteger(c)?c:2e4,s,l),vd(e,o,{sk:t,scene:n,frames:Number.isInteger(c)?c:2e4,lenient:r,unknown:s,past:l}),!a.length)return;let u=yd(a),d=u.length>12||a.length>=60;throw Error(`MOTION_SPEC_INVALID: ${u.slice(0,12).join(`
2
+ `)}${d?`\n(${u.length>12?`${u.length-12}`:`more`}${a.length>=60?`+`:``} more problems not listed)`:``}`)}function Od(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 qn(e.ctx,s,r,i,a);throw Error(`MOTION_SPEC_INVALID: check point "${n}"`)}function kd(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=>Dt(a,a.objects[t],o,t)&&rs(e,t,n)===i)??null:null}function Ad(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 jd=(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 Md(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 Nd(e,n,r){let i=Od(e,n,r.point),o=Od(e,n,r.target);if(!i||!o)return null;let s=Ad(e,r.target)??kd(e,r.point,n+(e.f0??0));return s?lr(e,n,r.point,`object:${s}`)??a(t(i,o)):a(t(i,o))}function Pd(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 t of n.contacts??[])if(jd(e,i,t.when)){let n=Nd(e,i,t);n!==null&&n>(t.tol??.01)&&l(`CONTACT`,`${t.name}: ${(n*100).toFixed(1)} cm apart`,i,n,t.tol??.01)}for(let a of n.orient??[])if(jd(e,i,a.when)){let n=Od(e,i,a.from),o=Od(e,i,a.to);if(!n||!o)continue;let c=r(s(t(o,n)),Md(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(jd(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(jd(e,i,t.frames)){let n=e.objectsAt(i)[t.object],r=ft.find(e=>e[0]===t.part);if(!n||!r)continue;let{P:a}=e.frames[i].s,o=Math.min(...pt(a[c[r[1]]],a[c[r[2]]]).map(e=>U(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(Od(e,n,`joint:${r.joint}`),Od(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 Fd=/^(.+): [\d.]+ cm off its keys$/,Id={twist:`twist about the forearm`,flex:`flex`,dev:`deviation (+ toward the thumb)`};function Ld(t,n){let r=t.objects[n]??Cn(t)[n];return K(t,n).flatMap(n=>{let i=t.objects[n],a=Fs(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 Rd(t,n,r){let{scene:i}=t.ctx,a=t.f0??0;for(let[s]of Ha(t.ctx.spec)){let c=za(t,s),l=Ld(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=Ba(c,i),d=ts(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 zd(e,t){let n=Fd.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(Ke).filter(([e,[t,n]])=>o[e]<t||o[e]>n).map(([e,[t,n]])=>`the wrist ${Id[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 Bd=e=>Math.round(e*10)/10,Vd=e=>Math.round(e*100)/100;function Hd(e){let t=Ha(e.ctx.spec);return t.length?Object.fromEntries(t.map(([t,n])=>{let r=za(e,t);return[t,[...(e.ctx.hold?.entry?.(t)??n).keys].sort((e,t)=>e.f-t.f).map((n,i)=>{let a=ts(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=Bd(w(l,c));return{f:n.f,deg:u,cm:Bd(o(n.pos,a.pos)*100),...u>=1?{got:c.map(Vd)}:{},...n.aim?{rot:r.rots[i]}:{},...n.leaned?{lean:n.lean}:{}}})]})):null}let Ud=.03,Wd=t=>e(t.pos,p(t.R,[0,t.half[1],0])),Gd=(e,t)=>[e[0][t],e[1][t],e[2][t]];function Kd(e,o,c=null){let l=Wd(e),u=Wd(o),d=t(l,u),f=[0,1,2].map(t=>Gd(e.R,t)),p=[0,1,2].map(e=>Gd(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 qd=(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=>U(s,e)<=0)},Jd=(e,t,n)=>!t.visible||(e.baked?.[t.visible]?.[n]??0)>.5,Yd=(e,t,n)=>K(e.ctx.scene,t).map(t=>({...e.ctx.scene.objects[t],...Xo(e,t,n)}));function Xd(e,t,n){let{sk:r,spec:i}=e.ctx,a=e.frames[n-(e.f0??0)];if(!a)return null;let o=Ms(r,i,a.s,i.objects[t]);return Math.max(0,Math.min(...Yd(e,t,n).map(e=>U(o,e))))}function Zd(t,r,i){let a=Yd(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(Wd).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=U(c,n);t<=.02&&(!l||t<l[1])&&(l=[e,t])}return l?.[0]??null}function Qd(t,r,i){let s=Object.entries(r.ctx.spec.objects??{}).filter(([,e])=>Os(e));if(!s.length)return[];let c=i.checks??{},l={...Yi,...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??Xi(u,n,t),e),null)??l.allow;for(let[c,u]of s){let[s,d]=Wo(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]&&Jd(r,u,s-1)){let e=Xd(r,c,s),t=[u.fall??[]].flat().some(e=>Array.isArray(e.v)&&e.at<s);t&&e>Ud?m(`CATCH`,`${c} missed by ${(e*100).toFixed(1)} cm`,s-f,e,Ud):!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:Zd(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=ns(r,c,l);if(!d||!Jd(r,u,l))continue;let g=d.map(e=>e.id),b=d.map(e=>[e,Wd(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||qd(t,x,e.slice(-1),d))).map(([e])=>`hand_${e.slice(-1)}`);for(let[t,r,a,s]of ft){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??=pt(l,u),C=Math.max(C,s-Math.min(...y.map(t=>U(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=Wd(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,Kd(e,t).depth));u>n&&m(`COLLIDE`,`${c} x ${e}`,i,u,n)}}}return Rd(r,l,m),d}let $d=.004,ef=.995,tf=.01,nf=[`Index`,`Middle`,`Ring`,`Pinky`],rf={l:`Left`,r:`Right`};function af(e,t){let n=K(e,t),r=n.length===1?bt(e.objects[n[0]]).kind:`box`;return r===`sphere`?`spherical`:r===`cylinder`?`power`:null}function of(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(!Os(a)||!Dt(i,a,n,t))continue;let o=i.props?.[a.prop]?.hand,[s,c]=Wo(e,a);o&&r.push({id:t,k:o,grasp:a.grasp??af(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)>=ef&&c(t)>=ef,rests:!0})}return r}let sf=(e,t,n)=>K(e.ctx.scene,t).map(t=>({...e.ctx.scene.objects[t],...is(e,t,n)}));function cf(e,t,n){let r=rf[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[...nf,`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 lf(t,r){return t.map(t=>{let i={depth:-1/0};for(let e of r){let n=Et(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&&Tt(i.entry,a);return{...t,depth:i.depth,at:o,normal:i.entry&&wt(i.entry,a)}})}function uf(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=df(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 df(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 ff(t,i,a,o,c,l){let u=lf(cf(t,i,a.k),o),d=rf[a.k].toLowerCase();if(a.rests&&u[0].depth<-.03)return;for(let e of u)e.finger!==`palm`&&e.depth>$d&&c(`FINGER_PEN`,`${e.bone} into ${a.id}`,l,e.depth,$d);let f=e=>u.some(t=>t.finger===e&&t.tip&&t.depth>=-.008),m=nf.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=>nf.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&&uf(_,v)>tf&&c(`SUPPORT`,`${a.id} not supported`,l,Math.min(1,uf(_,v)),tf)}function pf(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 of(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=sf(t,n.id,a);o.length&&ff(e,t.frames[a-r].s,n,o,i,a-r)}return n}let mf=.5,hf={twist:.5,flex:1,dev:1},gf=(e,t)=>t[e].map(t=>t*hf[e]);function _f(e,t,n,r){let{diag:i}=e.frames[n],a=e.baked??{};if((i[`hold_${t}`]?.w??0)>mf||(i[`grip_${t}`]?.w??0)>mf)return!0;for(let n of e.ctx.spec.fits??[])if(n.side===t&&(a[`w_${n.anchor}_${t}`]?.[r]??0)>mf)return!0;return Object.keys(e.ctx.spec.objects??{}).some(n=>e.ctx.spec.objects[n]?.prop&&rs(e,n,r)===t)}function vf(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(!_f(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=$e(b,i.R[d],y,p(f(b,m(i.R[d])),V(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]=gf(n,e);return x[n]<r+t||x[n]>i-t});S(qe,-1)&&_.past++,S(Ke,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 yf=.5,bf=[`flex`,`dev`,`twist`],xf={l:`Left`,r:`Right`},Sf={flex:e=>e>=0?`flex`:`extension`,dev:e=>e>=0?`radial deviation`:`ulnar deviation`,twist:()=>`twist`};function Cf(e){let t=null;for(let n of bf){let[r,i]=Ke[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 wf(e,r,i,o){let{s,diag:c}=r.frames[o],l=c[`hold_${i}`];if((l?.w??0)>yf)return l;let u=xf[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 $e(y,s.R[h],v,p(f(y,m(s.R[h])),V(e,s,u).l))}function Tf(e,t){let n=[],r=t.f0??0;for(let i of[`l`,`r`])t.frames.forEach((a,o)=>{if(!_f(t,i,o,o+r))return;let s=Cf(wf(e,t,i,o));if(s.past<=.5)return;let c=Math.abs(s.edge);n.push({kind:`WRIST`,msg:`hand_${i} ${Sf[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 Ef(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)>yf&&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 Df={WRIST:Ef};function Of(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 ft.map(([e,t,n,i])=>[e,r(t),r(n),i])}function kf(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={...Yi,...n.limits}.allow;return t.frames.forEach(({s:e},t)=>{let l=t+s,u=ft.map(([t,n,r,i])=>[t,e.P[a[n]],e.P[a[r]],i]);for(let[s,d]of r){if(!ft.every(([,e,t])=>d.names.includes(e)&&d.names.includes(t)))continue;let r=Of(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-mt(a,o,e,t))),d=Xi(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 Af=.5,jf=180/Math.PI,Mf={l:`Left`,r:`Right`},Nf={"over the head":0,high:1,wing:2};function Pf(e,{s:i,diag:o,c:s},l){let u=(o[`hold_${l}`]?.w??0)>Af&&!o[`hold_${l}`].second;if(!u&&!((s[`armw_${l}`]??0)>Af))return null;let d=e.index,f=i.P[d[`${Mf[l]}Arm`]],p=i.P[d[`${Mf[l]}ForeArm`]],m=i.P[d[`${Mf[l]}Hand`]],h=d.Spine2??d.Hips,g=j(e,i,h,[0,1,0]),_=n(j(e,i,h,[1,0,0]),l===`l`?1:-1),v=A(i,d.Head,e.landmarks.headCentre);if(r(t(m,v),g)>e.landmarks.headRadii[1]+.1)return null;let y=t(p,f),b=Math.acos(c(-r(y,g)/a(y),-1,1))*jf,x=Math.atan2(r(y,_),-r(y,g))*jf;if(r(t(p,v),g)>0)return{kind:`over the head`,deg:b,held:u};if(b>105)return{kind:`high`,deg:b,held:u};let S=t(m,i.P[d.Spine]),C=a(t(S,n(g,r(S,g))));return x>75&&C<.3?{kind:`wing`,deg:x,held:u}:null}function Ff(e,t,n,r,i){let a=e.ctx.spec,o=t=>e.timeMap?e.timeMap(t):t;for(let[s,c]of Ha(a)){if(a.props?.[c.prop]?.hand!==t)continue;let l=(e.ctx.hold?.entry?.(s)??c).keys,u=l.map((e,t)=>[t,e]).filter(([,e])=>o(e.f)>=n&&o(e.f)<=r),d=l.reduce((e,t,n)=>Math.abs(o(t.f)-i)<Math.abs(o(l[e].f)-i)?n:e,0);return{id:s,picks:u.length?u:[[d,l[d]]]}}return null}let If=e=>Math.round(e*100)/100,Lf=(e,t)=>t.map(([t,n])=>`objects.${e}.keys[${t}] (f${n.f})`).join(`, `);function Rf(e,n,r){let i=r.k,a=i===`l`?1:-1,o=r.held&&Ff(n,i,r.first,r.last,r.worst);if(o){let i=o.picks.filter(([,e])=>e.aim&&!(e.lean>=45));if(i.length)return`give ${Lf(o.id,i)} lean 45 (tips the aim to the hand's side, the elbow stays down)`;let a=n.frames[Math.max(0,Math.min(n.frames.length-1,Math.round(r.worst-(n.f0??0))))],s=o.picks[0][1].pos,c=t(a.s.P[e.index.Spine2??e.index.Hips],s),l=Math.hypot(c[0],c[2])||1;return`move ${Lf(o.id,o.picks)} pos by [${If(.05*c[0]/l)}, -0.08, ${If(.05*c[2]/l)}] (lower, nearer the chest)`}return`key elbow_${i} [${(r.kind===`wing`?[If(.1*a),-1,.2]:[If(.3*a),-1,r.mouth?.35:-.2]).join(`, `)}] from f${r.first} to f${r.last} (down${r.kind===`wing`?` and in`:` and a little out`}${r.mouth?`, in front of the chest`:``})`}let zf={"over the head":`upper arm wraps over the head`,high:`elbow over the shoulder`,wing:`elbow flared out to the side`};function Bf(e,n){let r=[],i=n.f0??0,o=t=>A(t,e.index.Head,e.landmarks.mouth);for(let s of[`l`,`r`]){let c=null,l=()=>{c&&c.n>=4&&r.push(c),c=null};n.frames.forEach((n,r)=>{let u=Pf(e,n,s),d=r+i;if(c&&(d-c.last>6||u&&u.held!==c.held)&&l(),!u)return;let f=a(t(n.s.P[e.index[`${Mf[s]}Hand`]],o(n.s)))<.25;c??={k:s,kind:u.kind,held:u.held,first:d,worst:d,deg:u.deg,n:0,mouth:!1},c.last=d,c.n++,c.mouth||=f,(Nf[u.kind]<Nf[c.kind]||u.kind===c.kind&&u.deg>c.deg)&&Object.assign(c,{kind:u.kind,deg:u.deg,worst:d})}),l()}return r.sort((e,t)=>Nf[e.kind]-Nf[t.kind]||t.n-e.n),r.slice(0,2).map(t=>`ARM arm_${t.k} ${zf[t.kind]} f${t.first}-f${t.last} (worst f${t.worst}, upper arm ${Math.round(t.deg)}deg ${t.kind===`wing`?`out`:`up`}): ${Rf(e,n,t)}`)}let Vf=(e,t)=>` (${e}the knee would fold ${Math.round(t)}deg; it bends to 150deg and stops at 160deg)`;function Hf(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 ${X(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 ${Eu(v)} m out from the hip at f${c}${l}, or raise root ${X(y)} m`,fix:{channel:`foot_${i}`,f:c,delta:v}}}let Uf=.003,Wf=e=>/(^|_)l$|^Left/.test(e)?`l`:/(^|_)r$|^Right/.test(e)?`r`:null;function Gf(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=Xn(e.out.ctx,a,t,{...s,diag:{...s.diag,frame:n}},i);return{path:o(`local`),delta:p(m(c.R),r)}}function Kf(e,t,n,r,i=``){let a=/^prop_(.+)$/.exec(t),o=a?e.spec.props?.[a[1]]?.hand:Wf(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?Gf(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 ${Eu(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 ${Eu(u)} m at f${n}${i}`,fix:l}}let qf=e=>ft.find(t=>t[0]===e);function Jf(e,t,n,r){let[,i,a]=qf(n),{s:o}=e.fr(t),s=pt(o.P[e.I[i]],o.P[e.I[a]]),c=s.map(e=>U(e,r)),l=Math.min(...c);return{p:s[c.indexOf(l)],n:yt(s[c.indexOf(l)],r),d:l}}function Yf(e,t,r,i,a){let o=t.worstFrame,s=e.out.objectsAt(e.rel(o))[i];if(!s||!qf(r))return null;let{n:c}=Jf(e,o,r,s);return Kf(e,r,o,n(c,a),` (out of ${i})`)}function Xf(e,r,i,a){let o=r.worstFrame,s=e.out.objectsAt(e.rel(o))[a];if(!s)return null;let c=Ki(e.sk,e.fr(o).s,i.endsWith(`_l`)?`Left`:`Right`),l=qi(c,s),u=r.worst-r.limit+Uf;if(!(l.side>0))return l.topAt?Kf(e,i,o,n(yt(l.topAt,s),u),` (out of ${a}'s top)`):null;let d=Math.max(...c.filter(e=>U(e,s)<0).map(e=>2*s.half[1]-p(m(s.R),t(e,s.pos))[1]))+Uf,f=n(yt(l.sideAt,s),u);if(d>.25)return Kf(e,i,o,f,` (out of ${a}'s side)`);let h=Kf(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 ${Eu(f)} m out of that side`,h}function Zf(e,t,r,i,a,o){return Kf(e,r,t.worstFrame,n(s(i),a),` (away from the ${o})`)}function Qf(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 pt(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 $f={COLLIDE(e,t){let[,n,r]=/^(\S+) x (.+)$/.exec(t.subject)??[];if(/^foot_[lr]$/.test(n??``))return Xf(e,t,n,r);let i=Yf(e,t,n,r,t.worst-t.limit+Uf);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=qf(l),f=i.out?.ctx?.scene?.people?.[u];if(!d||!f)return null;let p=o.worstFrame,{s:m}=i.fr(p),h=pt(m.P[i.I[d[1]]],m.P[i.I[d[2]]]),g=Of(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=Kf(i,l,p,n(_,o.worst-o.limit+Uf),` (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: ${X(o.worst+Uf)} }]`}},CLEARANCE(e,t){let n=(e.spec.checks?.clearance??[]).find(e=>t.subject.startsWith(`${e.name}:`));return n&&Yf(e,t,n.part,n.object,n.min-t.worst+Uf)},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 Hf(e,n,r,Vf(`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 ${X(i[`fpitch_${n}`])} deg; + lifts the heel)`:``;return{cause:e.cause(`foot_${n}`,r),hint:`foot_${n}: raise ${X(t.worst+.001)} m at f${r} (foot_* is the ankle; it stands ${gt(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=Qf(e,i,r).reduce((t,n)=>ht(e.sk,a,n)>ht(e.sk,a,t)?n:t);return Zf(e,n,r,t(s,o),n.worst-n.limit+Uf,`head`)},BODY(i,o){let[,s,l]=/^(\S+) into (\S+)$/.exec(o.subject)??[],u=qf(l),d=o.worstFrame,{s:f}=i.fr(d),p=/^(hand|farm|uarm)_([lr])$/.exec(s??``),m=p&&ke(i.sk);if(m){let e=i.I[`${p[2]===`l`?`Left`:`Right`}${{hand:`Hand`,farm:`ForeArm`,uarm:`Arm`}[p[1]]}`],t=Ge(m,i.sk,f,e),r=t.p&&Pe(m,f,t.p,m.skip.get(e));if(r){let e=Kf(i,s,d,n(r,o.worst-o.limit+Uf),` (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=Qf(i,d,s).reduce((e,n)=>a(t(n,v(n)))<a(t(e,v(e)))?n:e);return Zf(i,o,s,t(y,v(y)),o.worst-o.limit+Uf,`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+Uf)/2,u=Kf(e,`hand_l`,i,n(a,l));return{cause:u.cause,hint:`${u.hint}; and ${Kf(e,`hand_r`,i,n(a,-l)).hint}`,fix:Kf(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=mt(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+Uf)/2,g=s(m);return{cause:e.cause(`foot_l`,i),hint:`foot_l: move ${Eu(n(g,h))} m and foot_r ${Eu(n(g,-h))} m at f${i} (${l?`shins`:`thighs`} ${X(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 ${X(o)} deg (it reaches ${X(a(t.worstFrame))} at f${t.worstFrame}, ${X(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 ${gt(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(vt(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 ${Eu(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)}}}},ep=.02;function tp(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 ${Eu(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 np=(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 rp(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>ep)||i&&o<=i.w||(i={ch:a,w:o});return i}function ip(e,t,n,r,i){let a=n.slice(-1),o=e.hand(a,r),s=o.anchor?rp(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)<=ep&&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 ${X(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 ap(e,t){let n=null;return(e.spec?.goals??[]).forEach((e,r)=>{let i=Mu(e,r);t.subject.startsWith(`${i}:`)&&(!n||i.length>n.name.length)&&(n={goal:e,name:i,i:r})}),n}function op(e,t,n){let r=[t,...e.out.ctx.scene?.objects?.[t]?.ancestors??[]].find(t=>Vo(e.spec.objects?.[t],n));return r?e.spec.objects[r]:null}function sp(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=op(e,i.replace(/\.(center|top|base|rim)$/,``),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=vn(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 cp(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 lp(e,r,i,o,c,l){if(i===`moved`||i===`still`){let u=r[i],d=sp(e,u,o),f=Ru(e.out,r,l),p=cr(e.out,o,Fu(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]=i===`near`?Hu(e.out.ctx.scene,r):r[i],f=sp(e,u,o),p=sp(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=cr(e.out,o,u),y=cr(e.out,o,d);return v&&y?[g,n(s(t(y,v)),_*c)]:null}let up={GOAL(e,t){let n=e.spec&&ap(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=ju(i)[0],c=t.worst+.003,l=Zu(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 ${X(t.worst)} ${u} ${s===`still`?`off`:`short`}; needs ${d} ${Nu(i,s)})`;if(s===`speed`){let t=sp(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=lp(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|rim)$/,``);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|rim)$/,``);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=ip(e,i,m,r,f);if(t)return t;let n=np(e,m.slice(-1),r)&&tp(e,m.slice(-1),r,c,f);if(n)return{cause:e.cause(`root`,r),hint:n.hint,fix:n.fix}}let g=Kf(e,m,r,h,f),_=cp(e,m,r,h);return{cause:g.cause,hint:g.hint,..._?{fix:_}:{}}}},dp={Leg:[`UpLeg`,`Leg`,`Foot`,`knee`],ForeArm:[`Arm`,`ForeArm`,`Hand`,`elbow`]},fp=e=>e.startsWith(`Left`)?`l`:`r`,pp=e=>/^(w_|armw)/.test(e)?.5:/^(root|foot|hand)/.test(e)?1:/^(knee|elbow)/.test(e)?.2:.01;function mp(e,t,n){let r=fp(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))*pp(e)};return a.filter(e=>i[e]).sort((e,t)=>s(t)-s(e))}function hp(e,t,n){let r=t.subject.split(` `)[0],i=t.worstFrame,a=mp(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 gp(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 qn(e.out.ctx,i,t.s,t.diag,t.c)}function _p(e,t,n,r){let i=Wr(e.out.baked,t),a=r(e.fr(t)),o=null;for(let[s,c,l]of n){let n=r(lu(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 vp=(e,t,n,r)=>e.fr(n).s.P[e.I[`${t===`l`?`Left`:`Right`}${dp[r][0]}`]],yp={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 Hf(i,u,d,Vf(`the ankle target is ${X(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=vp(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 ${X(-m[1]-Math.sqrt(g)+.002)} m${c.worst<=.05?` (fix.reach does this when on)`:``}`:`or root ${Eu(n(s(_),a(_)-h*.95))} m toward the foot (the foot is ${X(a(_))} m sideways of the hip: the root and foot keys disagree)`;return{cause:i.cause(`foot_${u}`,d),hint:`foot_${u}: move ${Eu(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=Kf(i,`hand_${u}`,d,n(g,f),` (the hand target is ${X(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 ${Eu(n([-g[0],0,-g[2]],f))} m back from it`,fix:e.fix}}let v=Kf(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(gp(i,i.fr(d),`anchor:${y}`),S.offset??[0,0,0]))+.01:f,T=x&&tp(i,u,d,Math.min(f,w));return T?{cause:v.cause,hint:`${T.hint} (out of reach by ${X(c.worst)} m), or ${v.hint}`,fix:T.fix}:{cause:v.cause,hint:`${v.hint} (out of reach by ${X(c.worst)} m), or bring root ${Eu(n([g[0],0,g[2]],f))} m closer`,fix:v.fix}},KNEE(r,i){let a=fp(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 ${Eu(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 Hf(r,a,l,` (the knee folds ${X(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 ${Eu(n(s(t(v,h)),S-o(h,v)))} m away from the hip at f${l} (the knee folds ${X(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=fp(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 Kf(e,`hand_${i}`,c,m,` (the elbow folds ${X(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=fp(r),[c,l,u,d]=dp[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 ${X(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 ${X(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 ${Eu(b)} there)`:``}`,fix:b&&{channel:y,f:o,value:b}}},SLIDE(e,t){let n=fp(t.subject),r=n===`l`?`Left`:`Right`,i=t.subject.includes(`heel`)?`heel`:`ball`,a=Math.max(0,t.first-1),o=t=>vt(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 ${X(l[a])} -> ${X(l[t.last])} deg over f${a}-f${t.last} and the ${i} slides ${X(s)} m; key fyaw_${n} ${X(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 ${Eu(c)} at both ends)`:``}; the ${i} slides ${X(s)} m${s<.01?`; fix.slide locks this when on`:``}`,fix:c&&{channel:`foot_${n}`,f:t.last,value:c}}},SPEED:(e,t)=>hp(e,t,1),POP:(e,t)=>hp(e,t,.5),ACC:(e,t)=>hp(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 ${gt(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=Od(e.out,o,i.point),l=Od(e.out,o,i.target),u=t(l,c),d=/^prop:([^.]+)/.exec(i.point),f=d?e.spec.props?.[d[1]]?.hand:fp(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})`:Kf(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 ${X(m)} m at f${a} (the elbow would fold ${Math.round(y)}deg; it stops at 150deg): bring root ${Eu(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 ${X(m)} m at f${a}: bring root ${Eu(v)} m toward ${h??`the target`}, or move the target`,fix:{channel:`root`,f:a,delta:v}};let b=Kf(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:fp(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(gp(e,n,i.to),gp(e,n,i.from))),d(n)),p=Wr(e.out.baked,a),m=p[`wtwist_${l}`]??0,h=p[`wflex_${l}`]??0,{best:g,base:_}=_p(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 ${X(_)}, 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 ${X(b)} deg at f${a} (now ${X(v)}; cos ${X(_)}, 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=fp(n.joint),a=`${/Leg/.test(n.joint)?`knee`:`elbow`}_${i}`,o=Wr(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=Wr(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(lu(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 ${Eu(f.p)} (body frame: +y up, +z forward) at f${r} (${n.joint} ${X(u)} m below ${n.ref} -> ${X(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(Od(e.out,e.rel(n),`joint:${r.joint}`),Od(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} ${X(o.d)} m from ${r.anchor} there)`:`${r.joint} never gets within ${s} m of ${r.anchor} near f${i} (closest ${X(o.d)} m at f${o.q}): hold its weight w_${r.anchor}_${fp(r.joint)} at 1 around the switch`}}},bp=.003;function xp(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 Sp(e,t,n,r,i){let a=Kf(e,`hand_${t}`,n,r,i),o=xp(e,t,n,r);return o?{...a,fix:o}:a}function Cp(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,Cn(a)[i.object]?.ancestors,[i.object]].some(e=>e?.includes(r))}function wp(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 Tp={REACH:(e,t)=>zd(e,t)??yp.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||!Os(s))return c;let l=null;for(let t=o-24;t<=o+24;t++){let n=Xd(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=Ms(u,d,e.fr(o).s,s),m=K(f,i).map(e=>wp(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),_=Sp(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=Os(o[a])?a:Os(o[i])?i:null;if(!s||!e.out)return $f.COLLIDE(e,r);let c=o[s],l=e.spec.props[c.prop].hand,[u,d]=Wo(e.out,c),f=r.worst-r.limit+bp,p=r.worstFrame,m=p>=u&&p<=d,h=Math.min(p,d),g=ns(e.out,s,p)??[];if(s===a){let t=ft.find(e=>e[0]===i),{s:r}=e.fr(p);if(!t||!g.length)return null;let a=pt(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)=>U(t[1],t[0])<U(e[1],e[0])?t:e),u=yt(c,o),d=/^(hand|farm|uarm)_([lr])$/.exec(i)?.[2];return m&&d&&d!==l&&Cp(e,d,p,s)?Kf(e,`hand_${d}`,p,n(u,f),` (out of ${s})`):m?Sp(e,l,p,n(u,-f),` (${s} off ${i})`):Kf(e,i,p,n(u,f),` (out of ${s})`)}let _=e.out.objectsAt(e.rel(p))[a];if(!_||!g.length)return null;let v=ns(e.out,s,Math.max(u,r.first-1))??g,y=g.map((e,n)=>Kd(e,_,t(v[n].pos,_.pos))).reduce((e,t)=>t.depth>e.depth?t:e);return Sp(e,l,h,n(y.axis,y.depth-r.limit+bp),m?` (${s} out of ${a})`:` (${s} is put down in ${a}; it stays where the hand lets go at f${d})`)}},Ep=e=>e.spec?.fix?.grasp===!1,Dp={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 Op(e,t,n){let{scene:r}=e.out.ctx,i=e.out.objectsAt?.(e.rel(n))??{};return K(r,t).map(t=>({...r.objects[t],...e.out.ctx.objectPose?.(t,n)??i[t]??{}}))}let kp={FINGER_GAP(t,r){if(Ep(t))return Dp;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=Op(t,a,c);if(!d.length)return null;let f=d.reduce((e,t)=>Ct(t,u)<Ct(e,u)?t:e),p=Ct(f,u);return Kf(t,`hand_${o}`,c,n(wt(f,u),-Math.max(0,p-.03)),` (onto ${a}: the fingers reach it from nearer)`)},FINGER_PEN(e,t){if(Ep(e))return Dp;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=Op(e,i,o);if(!c.length)return null;let l=c.reduce((e,t)=>Ct(t,s)<Ct(e,s)?t:e);return Kf(e,`hand_${a}`,o,n(wt(l,s),t.worst-t.limit+.005),` (out of ${i})`)},SUPPORT(e,t){if(Ep(e))return Dp;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[K(e.out.ctx.scene,n)[0]],a=i?bt(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=Xo(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}}},Ap=[`add`,`remove`,`replace`,`move`,`test`],jp=e=>{throw Error(`MOTION_SPEC_INVALID: ${e}`)},Mp=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Np=e=>e.split(`/`).slice(1).map(e=>e.replaceAll(`~1`,`/`).replaceAll(`~0`,`~`)),Pp=e=>e.map(e=>`/${String(e).replaceAll(`~`,`~0`).replaceAll(`/`,`~1`)}`).join(``),Fp=/^@[A-Za-z_]\w*=[^&]*(&[A-Za-z_]\w*=[^&]*)*$/,Ip=e=>Fp.test(e)?e.slice(1).split(`&`).map(e=>[e.slice(0,e.indexOf(`=`)),e.slice(e.indexOf(`=`)+1)]):null,Lp=(e,t)=>typeof e==`object`&&!!e&&t.every(([t,n])=>Object.hasOwn(e,t)&&String(e[t])===n),Rp=e=>typeof e==`object`&&!!e&&!Array.isArray(e),zp=e=>/^-?\d+(\.\d+)?$/.test(e)?Number(e):e,Bp=[`name`,`skill`,`anchor`,`f`,`f0`,`side`,`type`];function Vp(e,t,n){if(!Array.isArray(e)){let n=Object.keys(e);return n.length?`${t} has ${n.slice(0,8).join(`, `)}${n.length>8?`, ...`:``}`:`${t} is empty`}let r=n?.[0]??Bp.find(t=>e.some(e=>e&&typeof e==`object`&&Object.hasOwn(e,t)));if(!r)return`${t} has ${e.length} entries`;let i=[...new Set(e.filter(e=>e&&Object.hasOwn(e,r)).map(e=>e[r]))],a=Number(n?.[1]),o=i.every(e=>typeof e==`number`)?(Number.isFinite(a)?[...i].sort((e,t)=>Math.abs(e-a)-Math.abs(t-a)):i).slice(0,8).sort((e,t)=>e-t):i.slice(0,8);return`${t} has ${e.length} entries; by ${r}: /${t}/@${r}=<${r}>, ${r} ${o.join(`, `)}${i.length>o.length?`, ...`:``}`}function Hp(e,t,n,r,i,a){let o=Ip(t),s=[];e.forEach((e,t)=>Lp(e,o)&&s.push(t));let c=o.length===1&&o[0][0]===`f`;if(s.length>1&&!c){let n=Bp.find(t=>!o.some(e=>e[0]===t)&&new Set(s.map(n=>e[n]?.[t])).size===s.length);jp(`${r}: ${s.length} ${i} entries have ${t.slice(1)}${n?` (${n} ${s.map(t=>e[t][n]).join(`, `)}; add &${n}=<${n}>)`:` (indices ${s.join(`, `)})`}`)}if(s.length){let t=(t,n)=>{let r=e[t];for(let e of n){if(typeof r!=`object`||!r||!Object.hasOwn(r,e))return!1;r=r[e]}return!0};return String(s.findLast(e=>t(e,n))??s.findLast(e=>t(e,n.slice(0,-1)))??s.at(-1))}if(!a)return-1;let l=Object.fromEntries(o.map(([e,t])=>[e,zp(t)]));return n.length>1&&(l[n[0]]={}),e.push(l),String(e.length-1)}function Up(e,t,n,r,i){let a=e,o=[];for(let e=0;e<t.length;e++){let s=t[e];if(Array.isArray(a)&&Ip(s)&&e<t.length-1){let c=e?o[e-1]:`the spec`,l=Hp(a,s,t.slice(e+1),n,c,!1);l===-1&&!r&&jp(`${n}: ${Pp([...o,s])} matches nothing (${Vp(a,c,Ip(s)[0])})`),s=l===-1?Hp(a,s,t.slice(e+1),n,c,!0):l,l===-1&&(o.created=!0,i?.push(`${n}: created ${Pp(o)}/${s} (${t[e].slice(1)})`))}o.push(s),e<t.length-1&&(a=typeof a==`object`&&a?a[Array.isArray(a)?Number(s):s]:void 0)}return o}function Wp(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))&&jp(`${n}: ${Pp(t.slice(0,e+1))} does not exist${r&&typeof r==`object`?` (${Vp(r,a)})`:``}`),r=r[Array.isArray(r)?Number(i):i]}return(typeof r!=`object`||!r)&&jp(`${n}: ${Pp(t.slice(0,-1))} is not an object or array`),r}function Gp(e,t,n,r,i,a){let o=Ip(t),s=e.flatMap((e,t)=>Lp(e,o)?[t]:[]);if(n===`add`)return s.length&&jp(`${i}: ${a} already has an entry with ${t.slice(1)}; replace its fields or remove it first`),(typeof r!=`object`||!r||Array.isArray(r))&&jp(`${i}: add through a selector takes an object value`),e.push({...structuredClone(r),...Object.fromEntries(o.map(([e,t])=>[e,zp(t)]))}),null;if(s.length||jp(`${i}: ${a} has no entry with ${t.slice(1)} (${Vp(e,a,o[0])})`),n===`remove`){for(let t of s.reverse())e.splice(t,1);return null}return Hp(e,t,[],i,a,!1)}function Kp(e,t,n,r){(!t||typeof t!=`object`||!Ap.includes(t.op))&&jp(`patch[${n}]: op must be ${Ap.join(`, `)}, got ${JSON.stringify(t?.op)}`);let i=`patch[${n}] (${t.op} ${t.path})`;if((typeof t.path!=`string`||t.path!==``&&!t.path.startsWith(`/`))&&jp(`${i}: path must be a JSON Pointer such as "/keys/0/f" or "/keys/@f=24/set/hand_l"`),t.op===`move`){(typeof t.from!=`string`||!t.from.startsWith(`/`))&&jp(`${i}: move needs from, a JSON Pointer such as "/keys/@f=120"`);let a=Up(e,Np(t.from),i,!1),o=a.at(-1),s=Wp(e,a,i),c=a.at(-2)??`the spec`,l;if(Array.isArray(s)&&Ip(o)){let e=s.flatMap((e,t)=>Lp(e,Ip(o))?[t]:[]);e.length||jp(`${i}: ${c} has no entry with ${o.slice(1)} (${Vp(s,c,Ip(o)[0])})`),l=e.map(e=>s[e]).reduce((e,t)=>Object.fromEntries([...new Set([...Object.keys(e),...Object.keys(t)])].map(n=>[n,Rp(e[n])&&Rp(t[n])?{...e[n],...t[n]}:Array.isArray(e[n])&&Array.isArray(t[n])&&n===`step`?[...new Set([...e[n],...t[n]])]:Object.hasOwn(t,n)?t[n]:e[n]])));for(let t of e.reverse())s.splice(t,1)}else Object.hasOwn(s,Array.isArray(s)?Number(o):o)||jp(`${i}: ${t.from} does not exist (${Vp(s,c)})`),l=s[Array.isArray(s)?Number(o):o],Array.isArray(s)?s.splice(Number(o),1):delete s[o];return Kp(e,{op:`add`,path:t.path,value:l},n,r)}if(t.op!==`remove`&&!Object.hasOwn(t,`value`)&&jp(`${i}: needs a value`),t.path===``){if(t.op===`replace`)return structuredClone(t.value);if(t.op===`test`)return Mp(e,t.value)||jp(`${i}: the spec differs from the test value`),e;jp(`${i}: ${t.op} needs a path below the spec`)}let a=t.op===`add`||t.op===`replace`&&!!r,o=Up(e,Np(t.path),i,a,r),s=Wp(e,o,i),c=o[o.length-1],l=o.length>1?o[o.length-2]:`the spec`;if(Array.isArray(s)&&Ip(c)){if(t.op===`replace`&&r&&!s.some(e=>Lp(e,Ip(c))))return r.push(`${i}: nothing had ${c.slice(1)}; added`),Kp(e,{...t,op:`add`},n);if(c=Gp(s,c,t.op,t.value,i,l),c===null)return e}if(Array.isArray(s)){if(c===`-`)return t.op!==`add`&&jp(`${i}: "-" (append) works with add only`),s.push(structuredClone(t.value)),e;let n=/^\d+$/.test(c)?Number(c):NaN,r=t.op===`add`?s.length:s.length-1;return n>=0&&n<=r||jp(`${i}: ${t.path} does not exist (${Vp(s,l)})`),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):Mp(s[n],t.value)||jp(`${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,c)?o.created||r.push(`${i}: ${t.path} did not exist (${Vp(s,l)}); added`):t.op!==`add`&&!Object.hasOwn(s,c)&&jp(`${i}: ${t.path} does not exist (${Vp(s,l)})`),t.op===`remove`?delete s[c]:t.op===`test`?Mp(s[c],t.value)||jp(`${i}: value is ${JSON.stringify(s[c])?.slice(0,60)}, not ${JSON.stringify(t.value)?.slice(0,60)}`):s[c]=structuredClone(t.value),e}function qp(e,t,n){return Array.isArray(t)||jp(`patch must be an array of { op, path, value }`),t.reduce((e,t,r)=>Kp(e,t,r,n),structuredClone(e))}let Jp=e=>Array.isArray(e)?e.map(Jp):typeof e==`number`?Math.round(e*1e4)/1e4:e,Yp=e=>e.split(`/`).slice(1).map(e=>e.replaceAll(`~1`,`/`).replaceAll(`~0`,`~`)),Xp=e=>e.map(e=>`/${String(e).replaceAll(`~`,`~0`).replaceAll(`/`,`~1`)}`).join(``),Zp=e=>typeof e==`number`&&Number.isFinite(e);function Qp(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 $p(e,t){if(Zp(e)&&Zp(t))return e+t;if(Array.isArray(e)&&Array.isArray(t)&&e.length===t.length&&e.every(Zp)&&t.every(Zp))return e.map((e,n)=>e+t[n])}function em(e,t,n){let r=-1;return(e.keys??[]).forEach((i,a)=>{!i?.set||!Object.hasOwn(i.set,t)||!Zp(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 tm(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=Jp(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 nm(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:Jp(t)}:{op:`add`,path:`/use/${n}/args`,value:{[e]:Jp(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 rm(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:Jp([i[0]+n.delta[0],i[1],i[2]+n.delta[2]])}:null}).filter(Boolean)}function im(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 tm(e,t,n,r);if(i?.[2]===`walkTo`)return nm(e,t,Number(i[1]),n);let a=r&&rm(e,t,n);return a?.length?a:null}function am(e,t,n){let{channel:r,f:i}=n;if(typeof r!=`string`||!Zp(i))return null;if(n.hold)return im(e,t,n);let a=e=>n.value===void 0?$p(e,n.delta):n.value,o=em(t,r,i);if(o>=0){let e=a(t.keys[o].set[r]);return e===void 0?null:[{op:`replace`,path:Xp([`keys`,o,`set`,r]),value:Jp(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&&tm(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]:Jp(d)}}}]}function om(e,t){if(typeof t.path!=`string`||!t.path.startsWith(`/`))return null;let n=Yp(t.path),r=Qp(e,n),i=t.delta===void 0?t.value:r===void 0&&t.path.endsWith(`/offset`)?t.delta:$p(r,t.delta);return i===void 0?null:r===void 0?Qp(e,n.slice(0,-1))===void 0?n.length===2&&n[0]===`fix`?[{op:`add`,path:Xp([n[0]]),value:{[n[1]]:Jp(i)}}]:null:[{op:`add`,path:t.path,value:Jp(i)}]:[{op:`replace`,path:t.path,value:Jp(i)}]}function sm(e,t,n){let r=e.ops?e.ops(n):e.channel===void 0?e.path===void 0?null:om(n,e):am(t,n,e);if(!r)return null;try{return JSON.stringify(qp(n,r))===JSON.stringify(n)?null:r}catch{return null}}function cm(e,{X:t,userSpec:n}={}){if(!n||typeof n!=`object`)return e;for(let r of e)if(r.fix)try{let e=sm(r.fix,t,n);e&&(r.patch=e)}catch{}return e}let lm={...$f,...yp,...Tp,...up,...Df,...iu,...kp};function um(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 dm(e,t,n){let r=um(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 fm(e,t,n,r,{limit:i=16,userSpec:a}={}){if(!r.length)return r;let o=dm(e,t,n);for(let e of r.slice(0,i))try{let t=lm[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 cm(r,{X:o,userSpec:a})}let pm=new Set([`ORIENT`,`BELOW`,`CLEARANCE`]),mm=e=>Math.max(0,pm.has(e.kind)?e.limit-e.worst:e.worst-e.limit),hm=e=>e.count*(1+Math.min(3,mm(e)/Math.max(Math.abs(e.limit),.01))),gm=e=>Math.round(hm(e)*100)/100,_m=/(?<![\w\-.[])[-+]?\d+(\.\d+)?|\bf\d+\b/g,vm=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(_m,e=>e[0]===`f`?`f#`:`#`)}`};function ym(e){for(let t of e)t.excess=Math.round(mm(t)*1e4)/1e4,t.severity=gm(t);return e.sort((e,t)=>(t.kind===`GOAL`)-(e.kind===`GOAL`)||t.severity-e.severity||t.count-e.count)}function bm(e){let t=new Map;for(let n of e){let e=vm({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),(pm.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 ym([...t.values()])}function xm(e,t,n,r={}){let i=n.checks??{},a=Qu(t,n.goals??[]),o=[...Zi(e,t,{allow:i.allow,limits:i.limits,mouthPoints:i.mouthPoints}),...Pd(t,i),...Qd(e,t,n),...pf(e,t),...nu(e,t),...Ri(t,i),...Bi(e,t,i,{...Yi,...i.limits}),...Tf(e,t),...kf(e,t,i),...a.issues];if(t.f0)for(let e of o)e.frame+=t.f0;let s=bm(o);r.explain!==!1&&fm(e,t,n,s,{userSpec:r.userSpec});let c=Bf(e,t);return{ok:o.length===0,total:o.length,problems:s.length,...n.goals?{goals:a.results}:{},groups:s,...c.length?{warnings:c}:{}}}let Sm=[`contacts`,`orient`,`below`,`clearance`,`appear`];function Cm(e){let t=e?.checks;if(!t||typeof t!=`object`||!Sm.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 Sm)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 wm=(e,t)=>({...t,...e?{description:e}:{}}),Z=(e,t)=>wm(e,{type:`number`,...t}),Tm=(e,t)=>wm(e,{type:`integer`,...t}),Q=(e,t)=>wm(e,{type:`string`,...t}),Em=(e,t)=>wm(e,{type:`boolean`,...t}),Dm=(e,t)=>wm(t,{$ref:`#/$defs/${e}`}),Om=e=>wm(e,{enum:[`l`,`r`]}),km=(e,t)=>wm(t,{type:`array`,prefixItems:[e,e],minItems:2,maxItems:2});`${Jt.join(`|`)}`,`${ft.map(e=>e[0]).join(`, `)}`,Tm(`frame`,{minimum:0}),Q(`written by the skills expander; leave it out`),Q(`a skill name (topic skills)`),Tm(`first frame`,{minimum:0});let Am={type:{const:`walk`},f0:Tm(`first frame; needs a standing pose keyed before it`,{minimum:0}),steps:Tm(`footsteps`,{minimum:1,default:8}),step:Z(`stride, m`,{default:.5}),period:Tm(`frames per step`,{default:20}),swing:Tm(`frames a foot is in the air`,{default:14}),halfWidth:Z(`feet half-width, m`,{default:.08}),lift:Z(`foot lift, m`,{default:.08}),heading:km({type:`number`},`[h0, h1] degrees; ${sn.yaw}`),turnDistance:Z(`m walked while the heading turns h0 -> h1`,{default:.4}),pelvisHigh:Z(`hips offset over the stance foot, m`),pelvisLow:Z(`hips offset at double support, m`),firstStep:Z(`first stride, m`,{default:.4}),armSwing:Z(`degrees`,{default:14}),vary:Z(`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})};Tm(null,{minimum:1,maximum:2e4}),Q(`a short name for the take`),Z(`m; a hand moving between targets arcs around the torso this far out`,{default:.25}),Tm(`compiles at most`,{minimum:1,maximum:64,default:32}),Dm(`vec3`,`world: a world point (m); body: root-relative, ${sn.pole}`),Q(`bone name`),Dm(`vec3`,`world offset, m`),Q(`bone`),Q(`bone`),Z(`0 = from .. 1 = to`,{default:.5}),Z(`with reach: the lowest t to slide back to`,{default:.15}),Om("slide toward `from` until this arm reaches"),Q(`scene object id`),Dm(`vec3`,`point in the object's frame, m`),Q(`a prop held by the other hand`),Q(`a point of that prop, or origin`),Q(`a character id`),Q(`a socket of the object`),Om(),(e=>Dm(`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:`),Om(),Q(`an anchor name`),Q(),Q(),Dm(`vec3`,`world offset, m`);let jm={f:Tm(),pos:Dm(`vec3`),rot:{type:`array`,items:{type:`number`},minItems:4,maxItems:4,description:`quat x, y, z, w`},aim:Dm(`vec3`,`or rot: +Y dir`),roll:Z(`deg about aim`),lean:Z(`aim tilt`,{minimum:0,maximum:45})},Mm={type:`object`,properties:{at:Tm(),onto:Q(),bounce:Z(null,{default:.25,minimum:0,maximum:.6}),tumble:{type:`boolean`},v:{oneOf:[{$ref:`#/$defs/vec3`},{const:`hand`}],description:`m/s`}},required:[`at`],additionalProperties:!1};Dm(`vec3`,`world hinge point`),Dm(`vec3`,`default +Y`),Q(`angle channel, deg`),Q(`spec.props name: it rides that hand`),Dm(`vec3`,`palm offset [d, l, n]`),Dm(`vec3`,`+Y on the palm, fixed`),Q(`channel: shown > 0.5`),Q(`channel: strength 0..4`),Tm(`hand takes it`,{default:0}),Tm(`let go (else end)`),Object.fromEntries(Ht.map(e=>[e,jm[e]])),{...Mm},Z(`kg`,{minimum:.01,maximum:60}),Dm(`ref`),Dm(`vec3`),Q(`the QA group's name`),Dm(`checkRef`),Dm(`checkRef`),Z(`m; default contacts 0.01, appear 0.035, supports 0.04`),Dm(`when`),Dm(`checkRef`),Dm(`checkRef`),Z(`default 0.64`),Q(`bone`),Q(`bone`),Z(`m; below default 0.08`),Dm(`part`),Q(`scene object id`),Dm(`frameSpan`),Q(`a visibility channel`),Q(`an anchor name`),Z(`allowed depth, m`),Object.fromEntries(Object.entries(Yi).map(([e,t])=>[e,Z(null,{default:t})])),Q(`prop.point`),Q(),km({$ref:`#/$defs/ref`},`distance <= tol`),km({$ref:`#/$defs/ref`},`a.y - b.y >= by`),Q("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])"),Q("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])"),Q("a point ref: its speed reaches `by` m/s (mode any by default)"),Z(`m; near default 0.05, still 0.01`),Z(`above m, default 0; moved m, default 0.05; speed m/s, needed`),Tm(null,{minimum:0}),Q(`a goal's name: at starts after the first frame that goal holds`),Q(`a goal's name: at ends before the first frame that goal holds`),Q(`the key of the answer`),km({$ref:`#/$defs/ref`},`min/max/mean/first/last m`),Dm(`ref`,`world points`),Dm(`frameSpan`,`absolute, default the whole take`),Tm(`position: every n frames (<= 16 rows)`,{minimum:1}),Em(`wrist targets past reach go onto it softly; hips lower for near-miss ankles`,{default:!0}),Em(`lifts sunk soles; a planted heel or ball drifting under 1 cm stays put; a heel-up foot pivots on its ball`,{default:!0}),Em(`arms kept out of the body's skin (wrists, elbows)`,{default:!0}),Em(`speed spikes borrow frames from the neighbouring keys`,{default:!1}),Em(`a standIdle stance in a scene body slides out of it (observations.placed)`,{default:!0}),Em(`fingers close on what the hand holds; it rests in the hand`,{default:!0});let Nm=e=>e.f0+16+(e.steps??8)*(e.period??20)+18,Pm=e=>({type:`array`,items:{type:`number`},minItems:2,maxItems:2,description:e}),Fm=e=>Object.fromEntries(e.map(e=>[e,Q(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)`)])),Im=Object.fromEntries(Ut.filter(e=>e!==`type`&&e!==`f0`).map(e=>[e,Am[e]])),Lm={standIdle:{args:{at:Pm(`[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:Z(`degrees (yaw); default the keyed yaw`),until:Tm(`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:Pm(`[x, z] world m: the heading turns toward it and the steps cover the distance (given steps, the stride or the step count and cadence is fitted to it in a natural gait, or the walk is refused)`),...Im,heading:{oneOf:[Z(),Am.heading],description:`degrees: h1, or [h0, h1]`},arrive:Tm(`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:Q(`not ending in _l/_r`),side:Om(`the hand`),reach:Tm(`frames to reach`,{default:20}),hold:Tm(`frames held`,{default:22}),release:Tm(`frames to let go`,{default:12}),elbow:Dm(`vec3`,`pole`),fingers:{enum:en,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:{...Fm([`car`,`seat`,`seatBack`,`wheel`]),offset:{...Dm(`vec3`,`hips from the seat top, seat frame`),default:[0,.17,.02]},floor:Z(`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:{...Fm([`car`,`seat`,`door`,`handle`]),side:Om(`driver door side, default l`),channel:Q(`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:Z(`back/front: m along the heading at landing (default back -0.25, front 0.4)`),side:Om(`cartwheel: toward the character's l (default) or r`),heading:Z(`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:Tm(`hold to this frame; default the last frame; under f0+264 the beats are paced to fit`),kit:{type:`object`,properties:{cig:Q(`the cigarette kit's rootId (object.createKit)`),lighter:Q(`the lighter kit's rootId`),exhale:Q(`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:Pm(`[x, z] the way the body is thrown`),from:Q(`or what hits it: object:<id> | person:<id> (thrown away from it)`),speed:Z(`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:Q(`the carried object's id (objects.<id>.prop on the hand's prop)`),side:Om(`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:Q(`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`}}}},Rm={_l:`_r`,_r:`_l`},zm=/^(yaw|roll|spine_yaw|spine_roll|head_yaw|head_roll|fyaw_[lr]|wtwist_[lr]|wflex_[lr])$/,Bm=/^(root|foot_[lr]|hand_[lr])$/,Vm=/^(knee|elbow)_[lr]$/,Hm=/^(yaw|fyaw_[lr])$/,Um=e=>e&&typeof e==`object`?structuredClone(e):e,Wm=e=>e.replace(/_(l|r)$/,e=>Rm[e]).replace(/^(Left|Right)/,e=>e===`Left`?`Right`:`Left`),Gm=()=>({origin:[0,0,0],yaw:0,mirror:!1,negate:new Set,names:{}}),Km=(t,n)=>e(t.origin,p(_(t.yaw),t.mirror?[-n[0],n[1],n[2]]:n)),qm=(e,t)=>e.mirror?[-t[0],t[1],t[2]]:t.slice();function Jm(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)?Wm(t):t}function Ym(e,t){let n={};for(let[r,i]of Object.entries(t)){let t=Um(i);Bm.test(r)?t=Km(e,t):Vm.test(r)?t=qm(e,t):typeof t==`number`&&(e.mirror&&(zm.test(r)||e.negate.has(r))&&(t=-t),Hm.test(r)&&(t+=e.yaw)),n[Jm(e,r)]=t}return n}function Xm(e,t=0,n=Gm()){let r=[],i=e=>Jm(n,e);return{state:e,keys:r,frame:n,at(i,a){let o=Ym(n,a);Object.assign(e,Um(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),Um(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 $=(e,t)=>{throw Error(`MOTION_SPEC_INVALID: ${e}: ${t}`)};function Zm(e,t,n){let r=e.scene?.objects?.[n];return r||$(e.tag,`args.${t} "${n}" is not a scene object`),r}let Qm=e=>Math.atan2(e[0][2],e[2][2])*180/Math.PI,$m=.01,eh=[`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]]),th=[[0,-1],[1,0],[-1,0],[Math.SQRT1_2,-Math.SQRT1_2],[-Math.SQRT1_2,-Math.SQRT1_2]];function nh(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 eh.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 rh=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 ih(e,t,n,r){let i=1/0;for(let[a,o]of nh(e,n,r))for(let e of t)i=Math.min(i,U(a,e)-o-.01);return i}function ah(e,t,n,r){let i=rh(t);if(!i.length||ih(e,i,n,r)>=0)return null;let a=_(r);for(let t=$m;t<=.600000001;t+=$m)for(let[o,s]of th){let c=p(a,[o*t,0,s*t]),l=[n[0]+c[0],n[1]+c[2]];if(ih(e,i,l,r)>=0)return{at:l.map(e=>+e.toFixed(4)),moved:+t.toFixed(2)}}return null}function oh(e,t,n,r,i=new Set){let a=rh({objects:Object.fromEntries(Object.entries(t?.objects??{}).filter(([e])=>!i.has(e)))});return a.length?ih(e,a,n,r):1/0}function sh(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=K(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 ch(e){let{sk:n,scene:r,use:i=[],index:o=-1}=e,s=i.slice(o+1).find(e=>e?.skill===`reachGrip`),c=sh(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=rh(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&&ih(n,x,T,y)<0;w+=$m){let e=p(b,[0,0,-(w+$m)]);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)}}function lh(e){let t=Gc(e,1/0);return[(t.foot_l[0]+t.foot_r[0])/2,(t.foot_l[2]+t.foot_r[2])/2]}let uh=e=>Math.round(e*100)/100,dh=e=>`[${e.map(uh).join(`, `)}]`,fh=[.3,.85],ph=[.18,.85],mh=[1,1.8],hh=[.7,1.4],gh=Math.cos(Math.PI/3),_h=e=>{let n=e.restWorldPos,r=e.index;return a(t(n[r.LeftLeg],n[r.LeftUpLeg]))+a(t(n[r.LeftFoot],n[r.LeftLeg]))};function vh(e,t){let n=e.scene,r=new Set,i=e=>{let t=n?.objects?.[e]?.ancestors?.at(-1)??e;for(let e of K(n,t))r.add(e)};for(let t of(e.use??[]).filter((t,n)=>n!==e.index&&t?.f0>=e.f0&&t?.skill===`sitInSeat`))for(let e of[t.args?.seat,t.args?.seatBack,t.args?.car].filter(e=>typeof e==`string`))i(e);for(let[i,a]of Object.entries(e.objects??{}))if(Number.isFinite(a?.grab)&&a.grab<=t&&!(a.release<e.f0))for(let e of K(n,i))r.add(e);return r}function yh(e,t){let n=e.R,r=t[0]-e.pos[0],i=t[1]-e.pos[2],a=n[0][0]*r+n[2][0]*i,o=n[0][2]*r+n[2][2]*i,s=Math.max(-e.half[0],Math.min(e.half[0],a)),c=Math.max(-e.half[2],Math.min(e.half[2],o)),l=[e.pos[0]+n[0][0]*s+n[0][2]*c,e.pos[2]+n[2][0]*s+n[2][2]*c];return[Math.hypot(t[0]-l[0],t[1]-l[1]),l]}function bh(e,t,n,r,i,a,o){let s=[t[0]+n[0]*r,t[1]+n[1]*r],c=_h(e.sk),l=Object.entries(e.scene?.objects??{}).filter(([e,t])=>!i.has(e)&&t.half&&t.pos&&t.R&&t.pos[1]<1.7&&t.pos[1]+2*t.half[1]*t.R[1][1]>.12).map(([e,t])=>{let[n,r]=yh(t,s);return{id:e,o:t,dist:n,q:r,low:t.pos[1]+2*t.half[1]*t.R[1][1]<.65*c}}).filter(e=>e.dist<a&&(e.dist<1e-6||(e.q[0]-s[0])*n[0]+(e.q[1]-s[1])*n[1]>=gh*e.dist)).sort((e,t)=>e.dist-t.dist);if(!l.length)return null;let u=e=>[t[0]+n[0]*e,t[1]+n[1]*e],d=new Set(Object.keys(e.scene.objects).filter(e=>!l.some(t=>t.id===e&&!t.low))),f=t=>l.some(e=>e.low&&yh(e.o,u(t))[0]<a)||l.some(e=>!e.low)&&oh(e.sk,e.scene,u(t),o,d)<.05;if(!f(r))return null;let p=r;for(;p>0&&f(p);)p-=.01;return p>.1?{t:p,at:u(p),near:l[0].id,gap:yh(l[0].o,u(p))[0]}:null}let xh=(e,t)=>Math.min(e.fit.last??1/0,...(e.use??[]).filter((t,n)=>n!==e.index&&t?.f0>e.f0).map(e=>e.f0),...e.keys.filter(e=>e.f>t&&/^keys\[/.test(e.src??``)).map(e=>e.f),...(e.fit.busy??[]).filter(e=>e>t));function Sh(e,t,n,r,i){let{args:a,tag:o,sk:s}=e,c=t.steps,l=Array.isArray(a.heading)?a.heading[1]:a.heading,u=l===void 0?0:Math.abs(((l-i)%360+540)%360-180);if(u>20)return e.fit.refusals.push(`${o}: heading ${l} points ${Math.round(u)} deg away from to ${dh(a.to)} (it lies at heading ${Math.round(i)}), so the walk would not go there; with to the walk turns toward it by itself: leave heading out, or give heading without to to walk that way`),{gen:t};let d=t.period??20,f=t.swing??14,p=t.step??.5,m=t.firstStep??.4;if(a.steps===void 0||a.arrive!==void 0||!(c>=2)||![c,d,f,p,m].every(Number.isFinite))return{gen:t};let h=[Math.sin(i*Math.PI/180),Math.cos(i*Math.PI/180)],g=!e.fit.noStop?.has(e.index)&&bh(e,n,h,r,vh(e,e.f0+16+c*d+18),.5*_h(s),i),_=g&&Ch(e,t,n,h,g.t,g);return _&&!_.refuse&&e.fit.stopped?.add(e.index),(!_||_.refuse)&&(_=Ch(e,t,n,h,r,null)),_.refuse&&e.fit.refusals.push(_.refuse),{gen:_.gen,..._.adjust?{adjust:_.adjust}:{}}}function Ch(e,t,n,r,i,a){let{args:o,tag:s,sk:c}=e,l=t.steps,u=t.period??20,d=t.swing??14,f=t.step??.5,p=t.firstStep??.4,m=a?`${dh(a.at)}, ${uh(a.gap)} m before ${a.near} (to ${dh(o.to)} is against it)`:`to ${dh(o.to)}`,h=t=>{let i=lh(Kc(t,e.keys,c,s));return{end:i,along:(i[0]-n[0])*r[0]+(i[1]-n[1])*r[1]}},{end:g,along:_}=h(t);if(Math.abs(i-_)<=.1)return{gen:t};let v=e.fps??24,y=_h(c),b=.47*y,x=l*u,S=e=>(e>=3?fh:ph).map(e=>uh(e*y)),C=Math.ceil(v/mh[1]),w=Math.floor(v/mh[0]),T=e.fit.stretch===!0&&!(e.use??[]).some((t,n)=>n!==e.index&&t?.f0>e.f0)&&!e.keys.some(t=>t.f>e.f0&&/^keys\[/.test(t.src??``)),E=_-p-(l-2)*f,D=o.firstStep===void 0?p/f:0,O=(e,t)=>{let n=t??(T||e===l?u:Math.min(Math.max(w,u),Math.floor(x/e))),r=e>=3?`step`:`firstStep`,a=e>=3?(i-E-(D?0:p))/(D+e-2):i-E,o=e>=3&&D?D*a:p,[s,c]=S(e),d=a*n/v/b,f=(e,[t,n])=>e>=t&&e<=n;return{n:e,p:n,name:r,value:a,ratio:d,ok:f(a,[s,c])&&f(o,S(2))&&(n===u||f(n,[C,w]))}},k=O(l),A=Math.min(60,Math.ceil(i/S(3)[0])+2),j=k.ok&&k.ratio>=hh[0]&&k.ratio<=hh[1]?k:null,M,N,P=e=>Math.abs(Math.log(e.ratio))+.1*Math.abs(e.n-l),ee=e=>e.filter(e=>e.ok).sort((e,t)=>P(e)-P(t)||e.n-t.n)[0],F=Array.from({length:A-1},(e,t)=>t+2);if(j||=ee(F.map(e=>O(e))),!j&&!T&&(M=xh(e,e.f0+16+x),j=ee(F.filter(e=>e>l).map(t=>O(t,Math.min(u,Math.floor((M-e.f0-34)/t)))).filter(e=>e.p>=C&&e.n*e.p>x))),!j){let[e,n]=S(l),r=Math.min(n,Math.max(e,k.value)),a=r*u/v/b,o=l>=3&&D?D*r:p,s={...t,[k.name]:uh(r),...l>=3&&D?{firstStep:uh(o)}:{}},c=a>=hh[0]&&a<=hh[1]&&o>=S(2)[0]&&o<=S(2)[1]?i-h(s).along:1/0;Math.abs(c)<=.1&&({pick:j,short:N}={pick:{...k,value:uh(r),ratio:a},short:c})}let[I,L]=S(3),R=`a natural gait (step ${I}..${L} m, ${mh[0]}..${mh[1]} steps a second: period ${C}..${w} at ${v} fps`,z=`${uh(Math.abs(i-_))} m ${_<i?`short of`:`past`} ${a?`the point ${dh(a.at)} clear of ${a.near} before to ${dh(o.to)}`:`to ${dh(o.to)}`} (${uh(i)} m away)`;if(!j){let n=2+Math.ceil((i-E-p)/L-1e-9),r=e.f0+16+n*C+18;return{gen:t,refuse:`${s}: steps ${l} at period ${u} (${x} frames of stepping) walk ${uh(_)} m and end at ${dh(g)}, ${z}; ${_<i?`in ${R}) that takes ${n} steps and ${n*C} frames of stepping or more (the walk would end at f${r} or later, not f${e.f0+16+x+18}): leave steps${o.step===void 0?``:` and step`} out (walkTo picks the count), or give steps: ${n} and room for the walk before what follows`:`no natural step count ends there: leave steps out (walkTo picks the count)`}`}}let te=j.p===u?{}:{period:j.p,swing:Math.max(2,Math.min(j.p-1,Math.round(d*j.p/u)))},ne=j.n>=3&&D>0,B=e=>({[j.name]:uh(e),...ne?{firstStep:uh(D*e)}:{}}),V={...t,steps:j.n,...te,...B(j.value)},re=N??i-h(V).along;N===void 0&&Math.abs(re)>.02&&(V={...V,...B(j.value+re/(j.n>=3?D+j.n-2:1))});let ie=V[j.name],ae=j.name===`step`?f:p,oe=e.f0+16+x+18,se=e.f0+16+j.n*j.p+18,ce=[j.n!==l&&`steps ${l} -> ${j.n}`,te.period&&`period ${u} -> ${te.period} (swing ${d} -> ${te.swing})`,`${j.name} ${ae} -> ${ie} m`,ne&&V.firstStep!==p&&`firstStep ${p} -> ${V.firstStep} m`].filter(Boolean).join(`, `),le=se===oe?``:`; it ends f${se}, not f${oe} (${T?`nothing follows it`:se>oe?`nothing uses f${oe+1}-f${se}`:`the pose holds to f${oe}`})`,H=`steps ${l} would need a ${k.name} of ${uh(k.value)} m at ${uh(v/u)} steps a second`,ue=N===void 0?j.n===l&&!te.period?` (at ${j.name} ${ae} they walk ${uh(_)} of ${uh(i)} m)`:` in ${R}; ${H})${le}`:` (${R}; ${H})`,de=N===void 0?`end at ${m}`:`end ${uh(Math.abs(N))} m ${N>0?`short of`:`past`} ${m}, the ${N>0?`longest`:`shortest`} natural step`,fe={args:{...j.n===l?{}:{steps:j.n},...te,[j.name]:ie,...ne&&V.firstStep!==p?{firstStep:V.firstStep}:{}},note:`${s}: ${ce}, so steps ${j.n} ${de}${ue}`};return{gen:V,adjust:fe}}let wh=.75,Th=.08,Eh=[.15,.3,.45,.6,.7,.8,.9],Dh=e=>e*e*e*(10-15*e+6*e*e),Oh=.9,kh=.7,Ah=(e,t)=>{let n=Math.min(1,Math.max(0,e));return n<kh?Oh*Dh((n/kh)**t):Oh+.09999999999999998*Dh((n-kh)/.30000000000000004)},jh=e=>[...new Map(Eh.map(t=>Math.round(t*e)).filter(t=>t>0&&t<e).map(t=>[t,[Ah(t/e,.65),Ah(t/e,.75)]])).entries()].map(([e,[t,n]])=>[e,t,n]),Mh=180/Math.PI,Nh=(e,t,n)=>Math.max(t,Math.min(n,e));function Ph(e,t){let n=Fh(e,t),r=e.sk.index;return!!n&&n[1]>=e.sk.restWorldPos[r.LeftLeg][1]+.1}function Fh(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=>Ih(t,e))?null:e(r.pos,p(r.R,n.local??[0,r.half?.[1]??0,0]))}function Ih(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 Lh=new WeakMap;function Rh(e){if(!Lh.has(e)){let n=D(e),r=e.index;le(e,n,`Right`,{}),Lh.set(e,a(t(n.P[r.RightHandThumb4],n.P[r.RightHandIndex4])))}return Lh.get(e)}function zh(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&&U(r,e)<=.01).sort((e,t)=>U(r,e)-U(r,t))[0]??null}function Bh(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*Th,n[1],n[2]+o/s*Th];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 U(c,n)>0&&U(r,n)<=0})}function Vh(t,n,r){let i=zh(t,n),a=Fh(t,n)??(i&&e(i.pos,p(i.R,[0,i.half[1],0])));if(!i||!a||2*Math.min(...i.half)+.025>Rh(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)&&Bh(t,a,r.root,o))}function Hh(e,t,n){let r=Fh(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])*Mh-(n.yaw??0)-(n.spine_yaw??0);for(;u>180;)u-=360;for(;u<-180;)u+=360;let d=Math.atan2(-c[1],l)*Mh-(n.pitch??0)-(n.spine_pitch??0);return{head_yaw:Nh(wh*u,-60,60),head_pitch:Nh(wh*d,-30,50)}}let Uh=(e,t,n)=>e.some(e=>e.f>=t&&e.f<=n&&(e.set?.head_yaw!==void 0||e.set?.head_pitch!==void 0)),Wh=(e,t)=>rn[e]?rn[e].map((e,n)=>n===2&&t===`r`?-e:e):[0,0,0];function Gh(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=ch(t);e||$(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=ah(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 Kh(e){let{args:t,state:n}=e;(!n.foot_l||!n.foot_r||!n.root)&&$(e.tag,`needs a standing pose keyed before f0 (use standIdle or another skill first)`);let r={type:`walk`,f0:e.f0,...t},i,a,o;delete r.to,delete r.arrive;let s=n.yaw??0,c=t.step??.5,l=t.firstStep??.4;if(t.to){a=[(n.foot_l[0]+n.foot_r[0])/2,(n.foot_l[2]+n.foot_r[2])/2];let u=t.to[0]-a[0],d=t.to[t.to.length-1]-a[1],f=Math.hypot(u,d);for(o=Math.atan2(u,d)*180/Math.PI;o-s>180;)o-=360;for(;o-s<-180;)o+=360;r.heading=t.heading??[s,o],r.steps=t.steps??Math.max(2,2+Math.round((f-l)/c)),typeof r.heading==`number`&&(r.heading=[s,r.heading]),e.fit&&f>.1&&({gen:r,adjust:i}=Sh(e,r,a,f,o))}typeof r.heading==`number`&&(r.heading=[s,r.heading]);let u=Kc(r,e.keys,e.sk,e.tag),d=Nm(r),f=e.frames-1,p=t.to&&lh(u),m=p?Math.hypot(p[0]-t.to[0],p[1]-t.to[t.to.length-1]):0,h={...m>.1?{placed:{at:p.map(uh),heading:Math.round(r.heading?.[1]??s),why:`ends ${uh(m)} m from to ${dh(t.to)}`}}:{},...i?{adjust:i}:{}};return d>f&&Math.max(...u.filter(e=>e.f<d).map(e=>e.f))<=f?{keys:u.filter(e=>e.f<=f),end:f,cut:`settle cut at f${f} (hold to the end)`,...h}:{keys:u,end:d,...h}}function qh(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 Jh=(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 Yh(e){let{args:t,state:n}=e,r=t.side,i=t.anchor;r!==`l`&&r!==`r`&&$(e.tag,`args.side must be "l" or "r", got ${JSON.stringify(r)}`),(typeof i!=`string`||/_[lr]$/.test(i))&&$(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??nn[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=!Uh(e.keys??[],e.f0,e.f0+v)&&Hh(y,t.define??e.anchors?.[i],n),x=b?{head_yaw:n.head_yaw??0,head_pitch:n.head_pitch??0}:null,S=Xm(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(Ph(y,C))for(let[e,t,n]of jh(a))S.at(e,{[u]:E(w,1,t),[f]:E(T,1,n)});let D=Math.round(.4*a);D>0&&D<a&&Vh(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:Wh(c,r)}},A=qh(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:Jh(e.scene,N).map(e=>({part:`hand`,object:e,depth:.04}))}}}let Xh=[.54,.4,-.075],Zh=.4,Qh=new Set([`l`,`r`]);function $h(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 eg(e,n,r){let i=Zm(e,`seat`,n);Math.abs(i.pos[1]-Zh)>.05&&$(e.tag,`seat "${n}" stands at y ${i.pos[1]}; the car skills are tuned for a seat base at y ${Zh} (a car on the ground)`);let a=Gm();a.yaw=Qm(i.R),a.mirror=r===`r`;let o=t(i.pos,p(_(a.yaw),a.mirror?[-Xh[0],Xh[1],Xh[2]]:Xh));return a.origin=[o[0],0,o[2]],a.names={lthigh:`rthigh`,rthigh:`lthigh`},a}let tg=[[`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})),ng=e=>({A:e.restWorldPos[e.index.LeftFoot][1],H:t=>t+(e.restWorldPos[e.index.Hips][1]-1.0427487182617188)});function rg(t){let{args:n,sk:r}=t,i=$h(n),a=Zm(t,`seat`,i.seat),{A:o}=ng(r),s=Qm(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 ig(t){let n=t.args.side??`l`;Qh.has(n)||$(t.tag,`args.side must be "l" or "r", got ${JSON.stringify(n)}`);let r=$h(t.args,n),i=eg(t,r.seat,n),a=t.args.channel??`door`;for(let[e,n]of[[`door`,r.door],[`handle`,r.handle]])Zm(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=Xm(t.state,t.f0,i),c={};for(let[e,n]of Object.entries(o))t.state[Jm(i,e)]===void 0&&(c[e]=n);Object.keys(c).length&&s.at(0,c),ag(s,a,ng(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?Wm(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:qm(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},...tg]},end:t.f0+246}}function ag(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,Zh+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,Zh+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 og=[`cig`,`lighter`,`exhale`],sg={follow:`landmark:mouth`,aim:`face`,visible:`exhale`},cg={cig:`cigarette`,lighter:`lighter`},lg=[-.05,.07,.1],ug=.88,dg=[.245,.08,.02],fg={l:[.13,-.035,.08],r:[-.13,-.035,.08]};function pg(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 mg(e,t,n){let r=t[n];return(typeof r!=`string`||!Cn(e.scene)[r])&&$(e.tag,`args.kit.${n} ${JSON.stringify(r)} is not a group in the scene; object.createKit { kit: "${cg[n]}" } answers its rootId`),r}let hg={cig:.03,lighter:.045},gg=.07,_g=.04;function vg(e,t){let n=e.args.kit;if(n===void 0)return null;(!n||typeof n!=`object`||Array.isArray(n))&&$(e.tag,`args.kit must be { cig, lighter, exhale? } (kit root ids), got ${JSON.stringify(n)}`);for(let t of Object.keys(n))og.includes(t)||$(e.tag,`args.kit has unknown field "${t}"; fields: ${og.join(`, `)}`);let r=mg(e,n,`cig`),i=mg(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 pg(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 pg(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]||Cn(e.scene)[n.exhale]))&&$(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]={...sg});let o=e=>t.squat?gg:e;return{objects:a,allow:[[`thigh_r`,r,o(hg.cig)],[`thigh_l`,i,o(hg.lighter)],[`hips`,r,o(hg.cig)],[`hips`,i,o(hg.lighter)],...t.squat?[[`shin`,r,gg],[`shin`,i,gg],[`hand_l`,`thigh_l`,_g],[`hand_r`,`thigh_r`,_g],[`hand`,`hips`,_g]]:[]].map(([e,t,n])=>({part:e,object:t,depth:n}))}}function yg(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}=Ds(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),lg)),g=a(t(h,d)),v=f(g>ug*m?e(d,n(t(h,d),ug*m/g)):h),y=e(f(A(l,s.Head,i.landmarks.mouth)),[0,0,.2]),b=Ue(i),x=(t,n)=>{let r=e(o.root,p(_(u),[n*dg[0],dg[1],dg[2]]));return f(b?Ve(b.body,i,l,r,He,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 bg={TW_M:40,FX_M:0,TW_H:0,FX_H:10,TW_L:-80,FX_L:-50},xg=[[`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})),Sg=[.4,-1,.6],Cg=[-.4,-1,-.3],wg=[.9,-.6,.3],Tg=[`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`],Eg=e=>(e/24).toFixed(1),Dg={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 Og(e){let t=e.args.until??(e.auto?e.f0+264:e.frames-1),n=t-e.f0;n<198&&$(e.tag,`needs frames f${e.f0}..f${e.f0+198} at least: 199 frames, ${Eg(199)} s at 24 fps, paced 0.75 (265 frames, ${Eg(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)||$(e.tag,`needs a pose keyed before f0 (root): standing or squatting`);let r=Object.fromEntries(Object.entries(Dg).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),Sg):Sg,c=a?p(g(a),Cg):Cg,l=yg(e),u=e.self?l?20:15:bg.FX_M;l&&(e.state.w_mouthOut_r=0);let d=n<264?n/264:1,f=e=>Math.round(e*d),m=Xm(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:bg.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:bg.TW_H,wflex_r:bg.FX_H,elbow_r:c,head_pitch:o(0)})};h.hold(0,...Tg),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:bg.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:bg.TW_L,wflex_l:bg.FX_L,elbow_l:wg}),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,...Tg,...l?[`w_mouthOut_r`]:[]);let C=!!l&&e.self,w=vg(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||fg.l},rpocket:{kind:`body`,p:e.self&&l?.rpocket||fg.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??[],...xg]}}}let kg=9.81/576,Ag=e=>Math.min(1,Math.max(0,e)),jg=e=>{let t=Ag(e);return t*t*(3-2*t)},Mg=(e,t,n)=>e+(t-e)*n,Ng=(e,t,n)=>e.map((e,r)=>e+(t[r]-e)*n),Pg=(e,t,n,r,i)=>jg((e-t)/(n-t))-jg((e-r)/(i-r)),Fg={l:1,r:-1},Ig=(e,t,n)=>f(_(e),f(g(t),v(n))),Lg=(e,t,n,r,i)=>p(m(Es(e,t,n)?.R??_(e)),p(r,i));function Rg(e,t,n,r,i,a){let o=Es(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 zg(e){let{sk:n,args:r,state:i}=e,a=n.index,s=n.restWorldPos;(!i.foot_l||!i.foot_r||!i.root)&&$(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 Bg(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/kg)+Math.sqrt(2*(l+.5-.05-u)/kg))),f=(u-l+kg*d*d/2)/d,h=f-kg*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=Mg(i.root[1],g,jg((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-kg*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=Mg(_,i.y0,jg((t-C)/14)),a=c;return e([i.root[0],r,i.root[2]],n(i.F,a))},O=e=>s*jg((e-x+2)/(d+5)),k=e=>Ig(i.h,O(e),0),A=(e,n)=>p(m(k(e)),t(n,D(e))),j={l:A(x,Cc(E(`l`),i.h,50,i.feet.l)),r:A(x,Cc(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=Ig(i.h,r,0),o=D(t),s=(t-x)/d,c=t>x&&t<S?Pg(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*jg((t-b)/5),d=Cc(E(u),i.h,f,i.feet[u]);else if(t<S){let t=1-jg(s/.2),r=jg((s-.72)/.28),l=e(i.hipOff[u],Ng([0,-i.leg*.97,0],[Fg[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=Mg(Mg(50,30,jg(s/.2)),0,jg((s-.7)/.3))}else d=T(u),f=0;let m=t>x&&t<S?Rg(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}`]:Lg(i.h,r,0,a,[Fg[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 Vg(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],[Fg[t]*.3,-i.leg*.93,0]),hand:t=>e(i.shoulder[t],[Fg[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*jg((e-l)/34):s*360,b=(t,n,r,a)=>e(n,p(Ig(i.h,0,y(t)),g[r](a))),x=Ig(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(Ng(i.root,S,jg((e-c)/12)));continue}e===l+1&&(D=C.slice());let n=t(D,p(Ig(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=Ng(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(Ig(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])*jg((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 Ng(o,s.p,jg((a-s.a+6)/6));if(a>s.b&&a<=s.b+10){let o=Ig(i.h,0,y(s.b)),c=p(m(o),t(s.p,j(s.b))),l=e(j(a),p(Ig(i.h,0,y(a)),Ng(c,g[n](r),jg((a-s.b-1)/9))));return Ng(s.p,l,jg((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=Ig(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=jg((t-c-1)/8);d=e(Ng(i.feet[s],C,n),[0,.07*Math.sin(Math.PI*n),0])}else if(t>u)d=ee[s];else{let n=jg((t-u+4)/4);d=M(Ng(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?Rg(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}`]:Lg(i.h,0,n,r,[Fg[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}`]:Lg(i.h,0,n,r,[Fg[s]*.4,0,-1]),[`armw_${s}`]:t<=l?0:t<=u?jg((t-l)/6):1-jg((t-u)/8)})}o.arm_rest=t<=l?Mg(72,-56,jg((t-c)/12)):t<=u?-56:Mg(-56,72,jg((t-u)/14)),F.push({f:t,set:o})}return{keys:F,end:d+1}}function Hg(e){let t=e.args.kind??`back`;[`back`,`front`,`cartwheel`].includes(t)||$(e.tag,`kind must be "back", "front" or "cartwheel", got ${JSON.stringify(t)}`);let n=zg(e);return t===`cartwheel`?Vg(e,n):Bg(e,n,t)}let Ug=9.81,Wg=.75,Gg={back:.16,front:.11},Kg=.12,qg=e=>e-Kg*(1-Math.exp(-e/Kg)),Jg=(e,t,n)=>t+n*Math.log1p(Math.exp((e-t)/n)),Yg=3/24,Xg=.03,Zg=e=>{let t=Math.min(1,Math.max(0,e));return t*t*t*(t*(6*t-15)+10)},Qg=e=>(e+540)%360-180,$g=e=>e.map(e=>Math.round(e*1e3)/1e3),e_=e=>Math.round(e*10)/10;function t_(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 n_(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`?Dn(r,o,`Hips`,e.f0):null;s||$(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))&&$(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 r_(t){let{sk:n,args:r,state:i}=t,a=n.index,o=n.restWorldPos;i.root||$(t.tag,`needs a pose keyed before f0 (standIdle, a walk, ...)`);let s=t_(t.keys,`root`,t.f0)??i.root.slice(),c={};for(let e of[`l`,`r`]){let n=t_(t.keys,`foot_${e}`,t.f0),r=t_(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]=n_(t,s),f=r.speed??4;Number.isFinite(f)&&f>0||$(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=Qg(b-l),S=v?-90:90,C=v?Gg.back:Gg.front,w=s[1],T=e=>w+m*qg(e)-.5*Ug*e*e,E=e=>m*(1-Math.exp(-e/Kg))-Ug*e,D=0,O=(m+Math.sqrt(m*m+2*Ug*Math.max(0,w-C)))/Ug+1;for(let e=0;e<60;e++){let e=(D+O)/2;T(e)+E(e)*Yg/2>C-Xg||E(e)>0?D=e:O=e}let k=D,A=Math.min(0,E(k)),j=T(k),M=k+Yg,N=f/(Wg*Ug),P=M+N,ee=e=>e<k?T(e):e<M?j+A*(e-k)-A/(2*Yg)*(e-k)**2:C-Xg*(1-Zg((e-M)/.25)),F=o[a.LeftFoot][1],I=o[a.Hips][1],L=v?1:-1,R=.9*Math.hypot(...[0,1,2].map(e=>o[a.LeftFoot][e]-o[a.Hips][e])),z=t.f0+Math.ceil((P+.6)*24),te=[];for(let n=t.f0;n<=z;n++){let r=(n-t.f0)/24,i=r<M,a=Math.min(Math.max(0,r-M),N),o=i?f*qg(r):f*qg(M)+f*a-.5*Wg*Ug*a*a,m=ee(r),h=[s[0]+u*o,m,s[2]+d*o],y=Math.max(.35,.9*M),b=l+x*Zg(r/Math.max(.25,y)),C=S*Zg(r/y),w=(e=>p(_(b),p(g(C),e))),T=Zg(r/Kg)*Math.exp(-r/.18)*Math.sin(2*Math.PI*r/.55),E=Zg(r/.3)*Math.exp(-r/.45)*Math.sin(2*Math.PI*r/.6),D=Zg((r-M)/.3),O=-(I-F)+.06*(1-D)+(v?.03:.01)*D,k=Zg(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]=Jg(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=R-.02*Math.log1p(Math.exp((R-n)/.02));return n>1e-9?h.map((e,i)=>e+t[i]*(1+(r/n-1)*k)):e},z=Es(0,C,0),ne=z?p(z.R,[0,1,0]):[0,1,0],B=Math.atan2(ne[2],ne[1])*180/Math.PI,V=p(g(-B),[0,-Math.sin(C*Math.PI/180),Math.max(.05,Math.cos(C*Math.PI/180))]),re={root:$g(h),yaw:e_(b),pitch:e_(C),roll:0,foot_l:$g(j(`l`,.11,.04+.1*E*k-(v?.1:0)*D)),foot_r:$g(j(`r`,-.11,.04-.1*E*k-(v?.1:0)*D)),fyaw_l:e_(b),fyaw_r:e_(b),fpitch_l:e_(v?-50*D:-.5*B),fpitch_r:e_(v?-50*D:-.5*B),knee_l:$g([.05,V[1],V[2]]),knee_r:$g([-.05,V[1],V[2]]),spine_pitch:e_(L*22*T*Math.min(1,f/4)),spine_yaw:0,spine_roll:e_(10*E*(1-D)),head_pitch:e_(L*30*T*Math.min(1,f/4)+(v?-8:8)*D),head_roll:e_(12*D),arm_rest:e_(72-22*D),aswing_l:e_(L*45*E+8*D),aswing_r:e_(-L*35*E-8*D),abend_l:e_(25+30*E*E),abend_r:e_(30+25*E*E)};n===t.f0&&Object.assign(re,{armw_l:0,armw_r:0,fingers_l:{relaxed:1},fingers_r:{relaxed:1}}),te.push({f:n,set:re})}return{keys:te,end:z}}let i_=e=>e.map(e=>Math.round(e*1e3)/1e3);function a_(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 o_(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`&&$(l,`args.side must be "l" or "r", got ${JSON.stringify(d)}`),(!f?.prop||r.props?.[f.prop]?.hand!==d)&&$(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)&&$(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&&$(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]+a_(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||$(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(_o*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*_o*Math.max(0,E[1]-D)))/_o,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}`,R=e(x,e(e(n(w,-.12),n(F,.25)),[0,-.05,0])),z=e(x,e(e(n(F,.32),n(w,.12)),[0,.08,0])),te=e(T,n(M,.45/h)),ne=e(x,e(n(w,.46),[0,-.1,0])),B=e(x,e(e(n(w,.3),n(F,-.15)),[0,-.4,0])),V=`fingers_${d}`,re=nn[f.grasp]??`grip`,ie={...o[V],[re]:1},ae={...o[V],[re]:0},oe=[{f:r.f0,set:{[L]:o[L]??0,[V]:ie,...o[I]?{[I]:o[I]}:{}}},{f:P,set:{[V]:ie}},{f:P+3,set:{[V]:ae}},{f:r.f0+10,set:{[L]:1,[I]:i_(R)}},{f:r.f0+10+4,set:{[L]:1,[I]:i_(z)}},{f:P-1,set:{[L]:1,[I]:i_(t(T,n(M,1/h)))}},{f:P,set:{[L]:1,[I]:i_(T)}},{f:P+1,set:{[L]:1,[I]:i_(te)}},{f:P+3,set:{[L]:1,[I]:i_(ne)}},{f:P+10,set:{[L]:1,[I]:i_(B)}}],se=`${u}Target`;return{keys:oe,end:P+10,anchors:{[se]:{kind:`world`,p:i_([i.to[0],D,i.to[2]])}},fills:{[u]:{release:P,fall:{at:P,v:i_(M)}}},goals:[{name:`${u} lands at the target`,near:[`object:${u}`,`anchor:${se}`],at:ee,tol:.25}],throw:{release:P,speed:Math.round(j*100)/100,v:i_(M),flight:Math.round(N*100)/100}}}let s_=[.35,.65],c_=e=>e.map(e=>Math.round(e*1e3)/1e3),l_=(e,t,n)=>Math.min(n,Math.max(t,e));function u_(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 d_(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`&&$(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)||$(d,`args.object "${f}" must fly: objects.${f}.fall { at, v: [vx, vy, vz] }`);let y=u_(l,c,`Right`),b={spec:{objects:{[f]:{fall:h.fall}},fps:g},scene:u,timeMap:null},x=e=>Lo(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,s_[0]-i,i-s_[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||$(d,`${f} never comes within reach of the right hand (in front of the chest, ${s_[0]}-${s_[1]} m from the shoulder): nearest at f${C.f}, [${c_(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=Kl({objects:i.objects},u,f),k=l_(.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:c_(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]}},...jh(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:c_(e(M[r],a))},I.fits.push({side:t,anchor:n,effector:{prop:i,point:`fingers`}}),I.channels.push(p(r))})}return I}let f_={standIdle:Gh,sitInSeat:rg,exitCar:ig,walkTo:Kh,reachGrip:Yh,smokeCigarette:Og,flip:Hg,hitReaction:r_,throwObject:o_,catchObject:d_},p_=Object.keys(f_),m_=e=>{throw Error(`MOTION_SPEC_INVALID: ${e}`)},h_=(e,t)=>JSON.stringify(e)===JSON.stringify(t);function g_(e,t,n,r){for(let[i,a]of Object.entries(t??{}))e[i]&&!h_(e[i],a)&&m_(`${r} defines ${n}.${i} differently from an earlier skill; rename one or define it yourself in spec.${n}`),e[i]=a}function __(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=>h_(t,e))||t.push(e)}}let v_=e=>!!e&&typeof e==`object`&&!Array.isArray(e)&&(e.range??e.search)!==void 0,y_=e=>e.description?` (${e.description})`:``;function b_(e,t,n){return!n||t===void 0||v_(t)?null:n.enum&&!n.enum.includes(t)?`${e} must be one of ${n.enum.map(e=>JSON.stringify(e)).join(`, `)}${y_(n)}, got ${W(t)}${G(String(t),n.enum.map(String))}`:(n.type===`number`||n.type===`integer`)&&!Number.isFinite(t)?`${e} must be a number${y_(n)}, got ${W(t)}`:n.type===`string`&&typeof t!=`string`?`${e} must be a string${y_(n)}, got ${W(t)}`:n.type===`boolean`&&typeof t!=`boolean`?`${e} must be true or false${y_(n)}, got ${W(t)}`:n.type===`array`&&n.items?.type===`number`&&!(Array.isArray(t)&&t.length>=(n.minItems??0)&&t.every(e=>Number.isFinite(e)||v_(e)))?`${e} must be ${n.minItems===2?`[x, z]`:n.minItems===3?`[x, y, z]`:`an array of numbers`}${y_(n)}, got ${W(t)}`:null}function x_(e,t=!1){if(!Array.isArray(e.use))return[`spec.use must be an array of { skill, args, f0 }`];let n=[];return e.use.forEach((r,i)=>{if(!r||typeof r!=`object`||Array.isArray(r))return n.push(`use[${i}] must be { skill, args, f0 }, got ${W(r)}`);if(!f_[r.skill])return n.push(`use[${i}].skill "${r.skill}" is not a skill${G(String(r.skill),p_)} (skills: ${p_.join(`, `)})`);if(!t)for(let e of Object.keys(r))It.includes(e)||n.push(`use[${i}] has unknown field "${e}"${ln(e,It)}; an entry is { skill, args, f0 }`);if(r.args!==void 0&&(!r.args||typeof r.args!=`object`||Array.isArray(r.args)))n.push(`use[${i}].args must be an object, got ${JSON.stringify(r.args)}`);else if(!t){let e=Lm[r.skill],t=Object.keys(e.args);for(let[a,o]of Object.entries(r.args??{}))if(!t.includes(a))n.push(`use[${i}].args.${a} is not an arg of ${r.skill}${ln(a,t)} (args: ${t.join(`, `)})`);else{let t=b_(`use[${i}].args.${a}`,o,e.args[a]);t&&n.push(t)}for(let t of e.required??[])r.args?.[t]===void 0&&n.push(`use[${i}] ${r.skill} needs args.${t}${y_(e.args[t])}`)}(!Number.isInteger(r.f0)||r.f0<0)&&n.push(`use[${i}].f0 must be an integer frame within 0..${e.frames-1}, got ${JSON.stringify(r.f0)}`)}),n}function S_(e,{sk:t,scene:n,lenient:r=!1,auto:i=!1,fit:a}){if(!e.use?.length)return{spec:e,used:[]};let o=x_(e,r);o.length&&m_(o.join(`
3
+ `));let s=(e.keys??[]).map((e,t)=>({...e,src:`keys[${t}]`})),c={anchors:{},props:{},objects:{},fits:[],checks:{},channels:[],fills:{},goals:[]},l=[],u=[];e.use.forEach((r,o)=>{let d=`use[${o}] ${r.skill}`;r.f0>e.frames-1&&m_(`use[${o}].f0 must be an integer frame within 0..${e.frames-1}, got ${JSON.stringify(r.f0)}`);let f=[...l,...s],p={sk:t,scene:n,frames:e.frames,fps:e.fps??24,auto:i,fit:a,props:{...c.props,...e.props},self:e.fix!==!1&&e.fix?.self!==!1,f0:r.f0,args:structuredClone(r.args??{}),tag:d,keys:f,use:e.use,index:o,place:Vl(e.fix).stance,state:structuredClone(Gc(f,r.f0)),anchors:{...c.anchors,...e.anchors},objects:{...c.objects,...e.objects}},m=f_[r.skill](p),h=Math.max(m.end??r.f0,...m.keys.map(e=>e.f));h>e.frames-1&&m_(`${d} needs ${h-r.f0} frames from f${r.f0} (to f${h}), past the last frame f${e.frames-1}; set frames >= ${h+1} or frames: "auto"`),l.push(...m.keys.map(e=>({...e,src:e.src??d}))),g_(c.anchors,m.anchors,`anchors`,d),g_(c.props,m.props,`props`,d),g_(c.objects,m.objects,`objects`,d);for(let e of m.fits??[])c.fits.some(t=>h_(t,e))||c.fits.push(e);for(let e of m.channels??[])c.channels.includes(e)||c.channels.push(e);__(c.checks,m.checks);for(let[e,t]of Object.entries(m.fills??{}))c.fills[e]={...c.fills[e],...t};c.goals.push(...m.goals??[]),u.push({skill:r.skill,f0:r.f0,end:m.end,...m.paced?{paced:m.paced}:{},...m.cut?{cut:m.cut}:{},...m.placed?{placed:m.placed}:{},...m.adjust?{adjust:m.adjust}:{}})});let d={...e,keys:[...l,...s],anchors:{...c.anchors,...e.anchors},props:{...c.props,...e.props},objects:{...c.objects,...e.objects}},f=[...new Set([...c.channels,...Array.isArray(e.channels)?e.channels:[]])];f.length&&(d.channels=f);let p=e.fits??[];d.fits=[...c.fits.filter(e=>!p.some(t=>t.side===e.side&&t.anchor===e.anchor)),...p],__(c.checks,e.checks),d.checks=c.checks;for(let[e,t]of Object.entries(c.fills))d.objects[e]&&(d.objects[e]={...t,...d.objects[e]});return c.goals.length&&(d.goals=[...c.goals,...e.goals??[]]),delete d.use,{spec:d,used:u}}let C_=new Set([`f`,`f0`,`steps`,`period`,`swing`,`until`,`reach`,`hold`,`release`]),w_=e=>e&&typeof e==`object`&&!Array.isArray(e)&&(e.range??e.search)!==void 0&&Object.keys(e).every(e=>[`range`,`search`,`int`].includes(e)),T_=e=>e.reduce((e,t)=>typeof t==`number`?`${e}[${t}]`:e?`${e}.${t}`:t,``);function E_(e){let t=[],n=(e,r)=>{if(w_(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: ${T_(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??C_.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 D_=(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 O_(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 k_=e=>e.groups.reduce((e,t)=>e+hm(t),0);function A_(e,t,n,r,i){let a=n=>r(O_(e,t,n)),o=a(n),s=e=>JSON.stringify({...e,keys:void 0,generators:void 0}),c=e=>Ur(qc(e,i),e.frames,Br(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?D_(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 j_=[2,3,5,7,11,13,17,19,23,29],M_=(e,t)=>Array.from({length:t},(t,n)=>{let r=1,i=0,a=e,o=j_[n%j_.length];for(;a>0;)r/=o,i+=a%o*r,a=Math.floor(a/o);return i});function N_(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=>D_(e,(e.lo+e.hi)/2)),c=A_(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(O_(e,t,a));f=k_(xm(n,uu({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)=>D_(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)=>M_(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]=D_(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:O_(e,t,d.values),search:{values:Object.fromEntries(t.map((e,t)=>[T_(e.path),d.values[t]])),evals:u,cost:Math.round(d.cost*100)/100,window:c,ms:Math.round(performance.now()-a)}}}function P_(e){let t=structuredClone(e);for(let n of E_(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 F_(e,{sk:t,scene:n,lenient:r=!1}={}){if(e?.frames!==`auto`)return e;let{spec:i,used:a}=S_({...P_(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)?Nm(e):null):[]];return{...e,frames:Math.max(0,...o.filter(Number.isFinite))+1}}let I_=new WeakSet;function L_(e){return I_.add(e),e}let R_=e=>Math.round(e*1e3)/1e3,z_=e=>Math.round(e*10)/10,B_=e=>e.map(R_);function V_(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 H_(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?Ci(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]<Yi.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])<=Yi.slide);r[t][n+i]=+!!s})}return r}function U_(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 W_(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=Vo(t,i-1),a=Vo(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 G_(e,t,n,r){for(let[t,i]of Object.entries(e.objects??{}))if(e.props?.[i?.prop]?.hand===n&&Vo(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 K_(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:B_(i.P[o.Hips]),yaw:z_(a.yaw??0),hand_l:B_(i.P[o.LeftHand]),hand_r:B_(i.P[o.RightHand]),on_l:G_(t,a,`l`,n),on_r:G_(t,a,`r`,n)}}function q_(e,{used:t,spec:n}){let r=V_(e,t??[],n);if(!r.length)return[];let i=H_(e),a=W_(e,n);return r.map(t=>({...t,end:K_(e,n,t.f[1]),planted:{l:U_(i.l,t.f),r:U_(i.r,t.f)},events:a.filter(e=>e.f>=t.f[0]&&e.f<=t.f[1])}))}let J_=e=>`[${e[0]},${e[1]}]`,Y_=(e,t)=>{e&&!e.includes(t)&&e.push(t)};function X_(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 Z_(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=X_(e.at,n);!i||i[0]===e.at[0]&&i[1]===e.at[1]||(Y_(t,`goals[${r}]${e.name?` "${e.name}"`:``} at ${J_(e.at)} -> ${J_(i)}: the clip is 0..${n}`),e.at=i)})}let Q_=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},$_=e=>e&&(e.rot!==void 0||e.aim!==void 0),ev=e=>e&&typeof e==`object`&&!Array.isArray(e)&&!$_(e)&&e.roll===void 0&&e.lean===void 0;function tv(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})=>ev(e));if(!a.length)return;let o=n?.objects?.[e]??Cn(n??{})[e],s=o?Os(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=>$_(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,Q_(n.k)),l.push(t)):s&&(Object.assign(e,s),u.push(t))}l.length&&Y_(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&&Y_(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 nv(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),Y_(n,`objects.${e}.keys[${r}].rot had length ${i.toFixed(3)}: normalized`))})}let rv=e=>e.map(e=>Math.round(e*1e3)/1e3);function iv(e,t){Array.isArray(e.keys)&&e.keys.forEach((n,r)=>{for(let i of[`knee_l`,`knee_r`]){let a=n?.set?.[i];if(!(Array.isArray(a)&&a.length===3&&a.every(Number.isFinite)))continue;let o=Math.hypot(...a);if(!(o>1e-6&&a[2]<-.7*o))continue;let s=Gc(e.keys,n.f).yaw;if(!(Number.isFinite(s)&&Math.abs((s%360+540)%360-180)>=90))continue;let c=rv(p(_(-s),a));c[2]<.7*o||(n.set[i]=c,Y_(t,`corrected: ${n.src?.startsWith(`keys[`)?n.src:`keys[${r}]`} (f${n.f}) ${i} [${rv(a)}] is a world direction at root yaw ${Math.round(s)}: body frame [${c}] (a pole is +z forward)`))}})}function av(e,t,n){Array.isArray(e.goals)&&e.goals.forEach((r,i)=>{for(let a of[`near`,`above`])(Array.isArray(r?.[a])?r[a]:[]).forEach((o,s)=>{if(typeof o!=`string`||/^[a-z]+:/.test(o))return;let c=ur(e,t,o);c.length===1&&(r[a][s]=c[0],Y_(n,`corrected: goals[${i}].${a}[${s}] "${o}" -> "${c[0]}"`))})})}function ov(e,t,n,r){if(!e||typeof e!=`object`)return e;Z_(e,n),av(e,{sk:r,scene:t},n),iv(e,n);for(let[r,i]of Object.entries(e.objects??{}))!i||typeof i!=`object`||!Array.isArray(i.keys)||!(Os(i)||yo(i))||(tv(r,i,t,n),nv(r,i,n));return e}function sv(e,t,n,r){let i=Number.isInteger(e)?[e,e]:e,a=X_(i,t);return a&&(a[0]!==i[0]||a[1]!==i[1])&&Y_(r,`${n}.frames ${J_(i)} -> ${J_(a)}: the take is 0..${t}`),a}function cv(e,t,n,r,i){if(Math.ceil(t/e)<=n)return e;let a=Math.ceil(t/n);return Y_(i,`${r}.every ${e} -> ${a}: a position probe answers at most ${n} rows`),a}let lv=`{ name, distance: [refA, refB] | position: ref, frames: [f0, f1], every }`,uv=e=>Math.round(e*1e6)/1e6,dv=e=>Math.round(e*1e4)/1e4;function fv(e,t){let n=/^(joint|bone):(.+)$/s.exec(t);return n?`${n[1]}:${vn(e,n[2]).bone??n[2]}`:t}function pv(e,t,n,r,i){let a=e.frames===void 0?[0,n]:sv(e.frames,n,t,i);return a||r(`${t}.frames must be [f0, f1], integer frames within 0..${n} with f0 <= f1, got ${W(e.frames)}`),a}function mv(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 ${lv}`);for(let t of Object.keys(e))qt.includes(t)||s(`${o} has unknown field "${t}"${G(t,qt)}; a probe is ${lv}`);let d=e.name??o;if(typeof d!=`string`||!d?s(`${o}.name must be a non-empty string, got ${W(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 ${W(e.distance)}`);if(f===`position`&&typeof e.position!=`string`)return s(`${o}.position must be a point ref, got ${W(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=pv(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 ${W(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`?cv(_,g,16,o,a):_;l.push({name:d,kind:f,refs:p.map(e=>fv(r,e)),range:h,every:v})}):s(`probes must be an array of ${lv}`),o.length)throw Error(`MOTION_SPEC_INVALID: ${o.join(`
4
+ `)}${o.length>=12?`
5
+ (more problems not listed)`:``}`);return l}function hv([e,t],n){let r=[];for(let i=e;i<=t;i+=n)r.push(i);return r}function gv(e,t){let n=hv(t.range,t.every).map(n=>[n,lr(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:uv(r[1]),minAt:r[0],max:uv(i[1]),maxAt:i[0],mean:uv(a/n.length),first:uv(n[0][1]),last:uv(n[n.length-1][1])}}function _v(e,t){return{at:hv(t.range,t.every).map(n=>{let r=cr(e,n,t.refs[0]);return{f:n,p:r?r.map(dv):null}})}}function vv(e,t,n){return Object.fromEntries(mv(e,t,n).map(t=>[t.name,t.kind===`distance`?gv(e,t):_v(e,t)]))}let yv=.3,bv=[`Thumb4`,`Index4`,`Middle4`,`Ring4`,`Pinky4`],xv=e=>Math.round(e*1e3)/1e3;function Sv(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,...bv.map(e=>r.P[i[`${o}Hand${e}`]]).filter(Boolean)],a[`foot_${s}`]=Object.values(vt(t,r,o))}return a}let Cv=(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 wv(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=>ks(i.objects?.[e]))||Cv(t,s,n))continue;let l=is(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:rs(t,s,n),box:u,c:e(u.pos,p(u.R,[0,c.half[1],0])),r:a(c.half)})}return o}function Tv(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]:yv))continue;let n=Math.min(...t.map(t=>U(t,e.box)));n<=yv&&(!c||n<c[2])&&(c=[e.root,e.id,n])}return c}function Ev(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:[xv(r[0]),r[1],xv(r[2]),r[3]]}))}function Dv(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=wv(t,a+n),s=Sv(e,i);for(let e of Object.keys(r))r[e].push(Tv(s[e],o,e.startsWith(`hand_`)?e.slice(-1):null))});let i=Object.fromEntries(Object.entries(r).map(([e,t])=>[e,Ev(t,n)]).filter(([,e])=>e.length));return Object.keys(i).length?i:null}let Ov=[`Hips`,`Spine`,`Spine2`,`Head`,`LeftArm`,`RightArm`,`LeftForeArm`,`RightForeArm`,`LeftHand`,`RightHand`,`LeftUpLeg`,`RightUpLeg`,`LeftLeg`,`RightLeg`,`LeftFoot`,`RightFoot`],kv=.25,Av=.15,jv=e=>Math.round(e*100)/100,Mv=e=>{let t=[...e].sort((e,t)=>e-t);return t.length?t[Math.min(t.length-1,Math.floor(.95*t.length))]:0},Nv=e=>{let t=e.reduce((e,t)=>e+t,0)/e.length,n=e.reduce((e,n)=>e+(n-t)**2,0)/e.length;return t>0?Math.sqrt(n)/t:0};function Pv(e,t){let n=e.length,r=e.reduce((e,t)=>e+t,0)/n,i=t.reduce((e,t)=>e+t,0)/n,a=0,o=0,s=0;for(let c=0;c<n;c++)a+=(e[c]-r)*(t[c]-i),o+=(e[c]-r)**2,s+=(t[c]-i)**2;return o>0&&s>0?a/Math.sqrt(o*s):0}function Fv(e,t){let n=e.index,r=t.length,i=180/Math.PI;return Ov.map(a=>{let o=n[a],s=a===`Hips`?-1:e.parent[o],c=new Float64Array(r),l=new Float64Array(9),u=new Float64Array(9);for(let e=0;e<r;e++){let{R:n,P:r}=t[e].s,a=n[o],d=s<0?null:n[s];for(let e=0;e<3;e++)for(let t=0;t<3;t++)l[e*3+t]=d?d[0][e]*a[0][t]+d[1][e]*a[1][t]+d[2][e]*a[2][t]:a[e][t];if(e){let n=0;for(let e=0;e<9;e++)n+=l[e]*u[e];let a=Math.acos(Math.max(-1,Math.min(1,(n-1)/2)))*i,d=t[e-1].s.P[o];c[e]=s<0?Math.max(a,Math.hypot(r[o][0]-d[0],r[o][1]-d[1],r[o][2]-d[2])*100):a}[l,u]=[u,l]}return c})}function Iv(e,t){let n=[],r=-1,i=0;for(let a=1;a<t;a++)e.some(e=>e[a]>kv)?(r<0&&(r=a),i=0):r>=0&&++i>=3&&(n.push([r,a-i]),r=-1,i=0);return r>=0&&n.push([r,t-1-i]),n.filter(([e,t])=>t-e>=3)}function Lv(e,t,n){let r=[[],[]];e.forEach((e,i)=>{let a=[...e].filter(e=>e>kv).sort((e,t)=>e-t),o=Math.max(kv,.2*(a[Math.floor(.9*a.length)]??0)),s=t=>e[t]>o;for(let e=1;e<n;e++)if(!t[e]){if(s(e)&&!s(e-1)){let t=1;for(;t<=3&&e-t>=0&&!s(e-t);)t++;(t>3||e-t<0)&&r[0].push([e,i])}if(!s(e)&&s(e-1)){let t=0;for(;t<3&&e+t<n&&!s(e+t);)t++;t>=3&&e+t<n&&r[1].push([e-1,i])}}});let i=0,a=0,o=null;for(let e of r){e.sort((e,t)=>e[0]-t[0]);for(let t=0;t<e.length;){let n=t+1;for(;n<e.length&&e[n][0]-e[n-1][0]<=3;)n++;let r=e.slice(t,n),s=new Set(r.map(e=>e[1])).size;if(t=n,s<4)continue;let c=0,l=r[0][0];for(let e=0,t=0;t<r.length;t++){for(;r[t][0]-r[e][0]>2;)e++;let n=new Set(r.slice(e,t+1).map(e=>e[1])).size;n>c&&(c=n,l=r[e][0])}i++,c>=Math.max(4,Math.ceil(.6*s))&&a++,(!o||c/s>o.share||c/s===o.share&&c>o.k)&&(o={share:c/s,k:c,of:s,at:[l,l+2]})}}return{score:i?jv(a/i):0,total:i,worst:o}}function Rv(e,t){let n=new Map,r=new Set;for(let t of e??[])if(Number.isFinite(t?.f)){r.add(t.f);for(let e of Object.keys(t.set??{}))(n.get(e)??n.set(e,new Set).get(e)).add(t.f)}let i=e=>{let t=[...e].sort((e,t)=>e-t);return t.slice(1).map((e,n)=>e-t[n])},a=[...n].filter(([,e])=>e.size>=4),o=a.filter(([,e])=>Nv(i(e))<Av).map(([e])=>e),s=[...r].filter(e=>e>0),c=Math.round(t/2);return{cv:r.size>=4?jv(Nv(i(r))):null,grid:o,eligible:a.length,halfSec:s.length>=4?jv(s.filter(e=>e%c===0).length/s.length):null}}function zv(e,t,n,r){let i=r**3,a=[0,null],o=null,s=new Set;for(let r of Ov){let c=t.map(t=>t.s.P[e.index[r]]),l=new Float64Array(t.length),u=[];for(let e=3;e<t.length;e++){let t=t=>c[e][t]-3*c[e-1][t]+3*c[e-2][t]-c[e-3][t];l[e]=Math.hypot(t(0),t(1),t(2))*i,n[e]&&u.push(l[e])}let d=Mv(u);d>a[0]&&(a=[jv(d),r]);for(let e=3;e<t.length;e++){if(l[e]<=Math.max(20,4*d))continue;s.add(e);let t=l[e]/Math.max(d,1e-9);(!o||t>o.ratio)&&(o={ratio:t,f:e,j:r})}}return{p95:a,spikes:s.size,worst:o}}function Bv(e,n,r,i){let o=e.index,s=n.P,c=t(s[o.Spine2],s[o.Hips]),l=(e,t)=>Math.acos(Math.max(-1,Math.min(1,(e[0]*t[0]+e[1]*t[1]+e[2]*t[2])/Math.max(1e-9,a(e)*a(t)))))*180/Math.PI,[u,d,f]=i?[`${r}Arm`,`${r}ForeArm`,`${r}Hand`]:[`${r}UpLeg`,`${r}Leg`,`${r}Foot`],p=t(s[o[d]],s[o[u]]);return[l(p,t(s[o[f]],s[o[d]])),l(p,i?c:[-c[0],-c[1],-c[2]])]}function Vv(e,t,n){let r={};for(let[i,a]of[[`arms`,!0],[`legs`,!1]]){let o=t.map(({s:t})=>Bv(e,t,`Left`,a)),s=t.map(({s:t})=>Bv(e,t,`Right`,a)),c=0,l=0,u=null;for(let e=1;e+48<=t.length;e+=48/4){if(n.slice(e,e+48).some(Boolean))continue;let t=[],r=[];for(let n=e;n<e+48;n++)for(let e=0;e<2;e++)t.push(o[n][e]-o[n-1][e]),r.push(s[n][e]-s[n-1][e]);let i=e=>e.reduce((e,t)=>e+Math.abs(t),0);if(i(t)<15||i(r)<15)continue;let a=Pv(t,r);c++,a>.9&&l++,(!u||a>u.c)&&(u={c:a,at:[e,e+48-1]})}r[i]={score:c?jv(l/c):0,windows:c,worst:u}}return r}function Hv(e,n,r,i){let o=new Uint8Array(n.length);for(let e of r.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.length-1,Math.round(t[t.length-1]));e++)o[e]=1}let s=null,c=0;for(let r=1,l=-1;r<=n.length;r++){if(r<n.length&&!o[r]&&e.names.every((e,i)=>a(t(n[r].s.P[i],n[r-1].s.P[i]))<1e-4)){l<0&&(l=r);continue}l>=0&&r-l>=i&&(c+=r-l,(!s||r-l>s[1]-s[0]+1)&&(s=[l,r-1])),l=-1}return{frames:c,best:s}}function Uv(e,n,r){let i=n.frames,o=i.length,s=n.fps??24,c=n.f0??0,l=n.ctx.spec,u=e.index,d=[],f=Fv(e,i),p=Iv(f,o),m=new Uint8Array(o);for(let[e,t]of p)m.fill(1,e,t+1);let h=new Uint8Array(o);for(let e=1;e<o;e++){let t=i[e].s.P[u.Hips],n=i[e-1].s.P[u.Hips];Math.hypot(t[0]-n[0],t[2]-n[2])*s>.25&&h.fill(1,Math.max(0,e-24),Math.min(o,e+24))}let g=h.slice();for(let e=1;e<o;e++)[`LeftFoot`,`RightFoot`].every(n=>a(t(i[e].s.P[u[n]],i[e-1].s.P[u[n]]))>.005)&&g.fill(1,Math.max(0,e-24),Math.min(o,e+24));let _=Lv(f,h,o),v=Rv(r,s),y=zv(e,i,m,s),b=Vv(e,i,g),x=Hv(e,i,l,s),S=([e,t])=>`f${e+c}-${t+c}`;_.total>=2&&_.score>=.5&&d.push([_.score,`sync`,`${_.worst.k} of ${_.worst.of} parts start or stop together ${S(_.worst.at)}: lead with pelvis or a hand, spine/head/arms 2-4 frames later`]),(v.cv!==null&&v.cv<Av||v.eligible&&v.grid.length/v.eligible>=.5||(v.halfSec??0)>=.7)&&d.push([.9,`keys`,`keys on a grid: key the extremes (contact, peak, turn), vary the gaps`]),y.worst&&y.worst.ratio>8&&d.push([.6,`jerk`,`${y.worst.j} jerks ${Math.round(y.worst.ratio)}x at f${y.worst.f+c}: a key snaps; ease or move it`]);for(let[e,t]of Object.entries(b))t.windows>=2&&t.score>=.4&&d.push([t.score,e,`${e} mirror ${S(t.worst.at)}: ${e===`legs`?`weight onto one foot, not straight down`:`offset one side 2-4 frames or pose it apart`}`]);x.best&&d.push([.7,`dead`,`dead still ${S(x.best)}${l.idle===!1?` (idle false)`:``}: let idle run or key a settle`]),d.sort((e,t)=>t[0]-e[0]);let C=(e,t)=>t?[e,t[0]+c,t[1]+c]:[e];return{sync:C(_.score,_.worst?.at),keys:[v.cv,v.grid.length,v.halfSec],jerk:[...y.p95,y.spikes],arms:C(b.arms.score,b.arms.worst?.at),legs:C(b.legs.score,b.legs.worst?.at),dead:C(jv(x.frames/s),x.best),...d.length?{flags:d.map(e=>e[1]),warn:d[0][2]}:{}}}let Wv=.25,Gv=.75,Kv=Array.from({length:16},(e,t)=>9+t),qv=[`steps`,`period`,`step`,`swing`],Jv=new Set([`POP`,`REACH`,`SLIDE`,`FLOAT`,`FLIP`,`KNEE`]),Yv=new Map,Xv=e=>Math.round(e*100)/100,Zv=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)&&!qv.some(t=>e.args[t]!==void 0)&&Object.values(e.args).every(e=>!(e&&typeof e==`object`&&!Array.isArray(e))));function Qv(e,t){if(t===2)return e>=Wv&&e<=Gv?{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>=Wv&&r<=Gv?{first:n,step:r}:null}function $v(e,t,n){let r=[];for(let i of Kv)for(let a=2;a<=40;a++){let o=Qv(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:Xv(o.step),firstStep:Xv(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 ey(e,t){let{steps:n,period:r}=e.args,i=Math.round(.8*r),a=n>2&&e.args.firstStep<.62?{firstStep:.62,step:Xv((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>=Wv&&e.args.step<=Gv)}let ty=(e,t,n)=>e.groups.filter(e=>Jv.has(e.kind)&&e.last>=t&&e.first<=n).reduce((e,t)=>e+t.count,0);function ny(e,{sk:t,scene:n,trial:r}){let i=[];for(let{i:a,u:o}of Zv(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(Yv.has(s)){i.push(Yv.get(s));continue}let c=e.use.slice(0,a).filter(e=>e.f0<o.f0),l,u=[];try{let{spec:r,used:i}=S_({...e,frames:2e4,use:c},{sk:t,scene:n,lenient:!0});l=Gc(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}=$v(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=>ey(e,p))].slice(0,18);for(let e of[...x,...h.slice(0,22-x.length)]){let t;try{t=ty(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`:``} (${Xv(_.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 ${Xv(p)} m (a step every ${Kv[0]} frames, strides up to ${Gv} 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)`};Yv.size>=64&&Yv.delete(Yv.keys().next().value),Yv.set(s,O),i.push(O)}return i}function ry(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 iy=.05,ay=e=>`[${e.map(X).join(`, `)}]`,oy=e=>`${Math.round(e*1e3)/10}`,sy=e=>(e%360+540)%360-180,cy={top:` (box top)`,base:` (box base)`,center:` (box centre)`,rim:` (opening)`};function ly(e,t){let n=/^object:.+?(?:\.(center|top|base|rim))?$/s.exec(e);return n?`${e}${n[1]?cy[n[1]]:t?` (surface)`:cy.center}`:e}function uy(e,t){let n=e.frames[t-(e.f0??0)];if(!n)return null;let{P:r}=n.s,i=e.ctx.sk.index,a=r[i.LeftFoot],o=r[i.RightFoot];return{at:[(a[0]+o[0])/2,(a[2]+o[2])/2],yaw:n.c?.yaw??0}}function dy(e,t,n,r,i){let a=e.frameCount-1,o=e=>Math.max(0,Math.min(a,Math.round(i(e)))),s=o(t.f0),c=o(t.end??t.f0),l=uy(e,c),u=r?.args??{};if(!l)return null;let d=u.side?` ${u.side}`:``,f=u.anchor?` ${u.anchor}`:``,p=`use[${n}] ${t.skill}${d}${f} f${s}-${c}: at ${ay(l.at)} yaw ${Math.round(sy(l.yaw))}`,m=Array.isArray(u.heading)?u.heading[u.heading.length-1]:u.heading;typeof m==`number`&&Math.abs(sy(l.yaw-m))>5&&(p+=` (asked ${m})`);let h=Array.isArray(u.to)?[u.to[0],u.to[u.to.length-1]]:Array.isArray(u.at)&&!t.placed?u.at:null;if(h){let t=[l.at[0]-h[0],l.at[1]-h[1]],n=Math.hypot(...t);if(n>iy){let r=u.to&&uy(e,s),i=r&&Math.hypot(h[0]-r.at[0],h[1]-r.at[1])>1e-6?[h[0]-r.at[0],h[1]-r.at[1]].map((e,t,n)=>e/Math.hypot(...n)):null,a=i?t[0]*i[0]+t[1]*i[1]:null,o=i?Math.abs(t[0]*i[1]-t[1]*i[0]):null,c=i?`${X(Math.abs(a))} m ${a<0?`short`:`past`}${o>iy?`, ${X(o)} m aside`:``}`:`${X(n)} m off`;p+=`; ${u.to?`to`:`at`} ${ay(h)}: ${c}${u.to&&u.steps!==void 0?`, args.steps set the distance (to only aims)`:``}`}}return p}function fy(e,t){let n=e.f0??0,r=null,i=null,a=null,o=null;for(let s=0;s<e.frames.length;s++){if(!jd(e,s,t.when))continue;a??=s+n,o=s+n;let c=Nd(e,s,t);c!==null&&((!r||c<r[0])&&(r=[c,s+n]),(!i||c>i[0])&&(i=[c,s+n]))}return a===null?`${t.name}: never on${typeof t.when==`string`?` (${t.when} never > 0.99)`:``}`:`${t.name} f${a}-${o}: gap ${r?`${oy(r[0])} cm f${r[1]} .. ${oy(i[0])} cm f${i[1]}`:`unknown`} (tol ${oy(t.tol??.01)})`}function py(e,t,n,r){let i=ju(t)[0];if(!r||r.at===null||!(i===`near`||i===`above`||!r.ok&&(i===`moved`||i===`still`)))return null;let a=(i===`near`||i===`above`?t[i]:[Fu(t[i].includes(`:`)?t[i]:`object:${t[i]}`)]).map(t=>[t,cr(e,r.at,t)]);if(a.some(([,e])=>!e))return null;let o=`goal ${Mu(t,n)} f${r.at}: ${a.map(([e,t])=>`${ly(e,i===`near`)} ${ay(t)}`).join(`, `)}`;if(i===`near`&&Array.isArray(t.at)&&t.at[0]!==t.at[1]&&(t.mode??`all`)===`all`){let n=e.f0??0,i=null;for(let r=Math.max(t.at[0],n);r<=Math.min(t.at[1],n+e.frames.length-1);r++){let n=lr(e,r,t.near[0],t.near[1]);n!==null&&(!i||n<i[0])&&(i=[n,r])}i&&i[1]!==r.at&&(o+=`; best ${X(i[0])} f${i[1]}`)}return o}function my(e,{used:t=[],use:n=[],checks:r={},goals:i=[],results:a=[]}){let o=e.timeMap??(e=>e),s=new Map(a.map(e=>[e.name,e])),c=[...t.slice(0,8).map((t,r)=>dy(e,t,r,n[r],o)),...(r.contacts??[]).slice(0,6).map(t=>fy(e,t)),...i.map((t,n)=>py(e,t,n,s.get(Mu(t,n)))).filter(Boolean).slice(0,8)].filter(Boolean);return c.length?c:null}let hy=e=>!!e&&typeof e==`object`&&!Array.isArray(e),gy=e=>String(e).toLowerCase().replace(/[\s_-]/g,``),_y=e=>String(e).replace(/[_-]?(left|Left|LEFT)$/,`_l`).replace(/[_-]?(right|Right|RIGHT)$/,`_r`).replace(/[_-]?L$/,`_l`).replace(/[_-]?R$/,`_r`),vy={arm_swing:`aswing`,armswing:`aswing`,elbow_bend:`abend`,arm_bend:`abend`,wrist_twist:`wtwist`,wrist_flex:`wflex`,foot_yaw:`fyaw`,foot_pitch:`fpitch`,arm_weight:`armw`,ik_weight:`armw`,hips:`root`,pelvis:`root`,finger:`fingers`,hand_pole:`elbow`,knee_pole:`knee`},yy={objectKey:{position:`pos`,rotation:`rot`,quat:`rot`,quaternion:`rot`,frame:`f`},anchor:{pos:`p`,position:`p`,point:`p`,id:`object`,joint:`bone`},use:{frame:`f0`,f:`f0`}};function by(e,t,n={}){if(t.includes(e))return e;let r=n[e]??n[gy(e)];if(r&&t.includes(r))return r;let i=new Set(t.filter(t=>gy(t)===gy(e)||t===`${e}s`||`${t}s`===e));return i.size===1?[...i][0]:null}function xy(e,t,n,r,i){if(!hy(e))return e;let a=e;for(let o of Object.keys(e)){let e=t.includes(o)?null:by(o,t,i);!e||e in a||(a=Object.fromEntries(Object.entries(a).map(([t,n])=>t===o?[e,n]:[t,n])),r.push(`corrected: ${n}.${o} -> ${n}.${e}`))}return a}let Sy=(e,t)=>Array.isArray(e)?e.map(t):e,Cy=e=>hy(e)&&(e.to!==void 0||e.arrive!==void 0||typeof e.heading==`number`||by(String(e.type),[`walkTo`])===`walkTo`);function wy(e,t){if(!Array.isArray(e.generators)||!e.generators.some(Cy))return e;let n=Array.isArray(e.use)?[...e.use]:[],r=[];e.generators.forEach((e,i)=>{if(!Cy(e))return r.push(e);let{type:a,f0:o,...s}=e,c=n.findIndex(e=>hy(e)&&Number.isFinite(e.f0)&&e.f0>o),l=c<0?n.length:c;n.splice(l,0,{skill:`walkTo`,f0:o,args:s});let u=e.to===void 0?e.arrive===void 0?typeof e.heading==`number`?`a single heading`:`type "${a}"`:`arrive`:`to`;t.push(`corrected: generators[${i}] (a walk with ${u}) -> use[${l}] walkTo { ${Object.keys(s).join(`, `)} }: a generator walks by steps and [h0, h1]; walkTo is the walk that heads for a point`)});let{generators:i,...a}=e;return{...a,...r.length?{generators:r}:{},use:n}}function Ty(e,t,n){if(!hy(e))return e;let r=e,i=p_.includes(e.skill)?e.skill:by(String(e.skill),p_);i&&i!==e.skill&&(r={...r,skill:i},n.push(`corrected: use[${t}].skill "${e.skill}" -> "${i}"`));let a=Lm[r.skill];if(!a)return r;let o=Object.keys(a.args);if(hy(r.args)||r.args===void 0){let e=r.args??{},i=xy(e,o,`use[${t}].args`,n);for(let[e,r]of Object.entries(i)){let o=a.args[e]?.enum,s=o&&typeof r==`string`&&!o.includes(r)?by(r,o,{left:`l`,right:`r`}):null;s&&(i={...i,[e]:s},n.push(`corrected: use[${t}].args.${e} "${r}" -> "${s}"`))}if(i.f0!==void 0&&!o.includes(`f0`)&&r.f0===void 0){let{f0:e,...a}=i;i=a,r={...r,f0:e},n.push(`corrected: use[${t}].args.f0 -> use[${t}].f0`)}for(let e of Object.keys(r)){let a=It.includes(e)?null:by(e,o);!a||a in i||(i={...i,[a]:r[e]},r=Object.fromEntries(Object.entries(r).filter(([t])=>t!==e)),n.push(`corrected: use[${t}].${e} -> use[${t}].args.${a}`))}if(hy(i.define)&&Lt[i.define.kind]){let e=xy(i.define,[`kind`,...Lt[i.define.kind]],`use[${t}].args.define`,n,yy.anchor);e!==i.define&&(i={...i,define:e})}i!==e&&(r={...r,args:i})}return xy(r,It,`use[${t}]`,n,yy.use)}let Ey=e=>[...Xt,...Qt,...$t,...bd(e)];function Dy(e,t){let n=_y(e),r=/_[lr]$/.exec(n)?.[0]??``,i=r?n.slice(0,-2):n,a=vy[i]??vy[gy(i)];return a&&t.includes(a+r)?a+r:by(n,t)}function Oy(e,t,n,r){if(!hy(e)||!hy(e.set))return e;let i=e.set,a=e.step;for(let o of Object.keys(e.set)){if(n.includes(o)||o.endsWith(`_vis`)||o.startsWith(`w_`))continue;let s=Dy(o,n);!s||s in i||(i=Object.fromEntries(Object.entries(i).map(([e,t])=>e===o?[s,t]:[e,t])),Array.isArray(a)&&(a=a.map(e=>e===o?s:e)),r.push(`corrected: keys[${t}] (f${e.f}) channel "${o}" -> "${s}"`))}for(let n of[`l`,`r`]){let a=i[`fingers_${n}`];if(!hy(a))continue;let o=xy(a,en,`keys[${t}] (f${e.f}) fingers_${n}`,r);o!==a&&(i={...i,[`fingers_${n}`]:o})}return i===e.set?e:{...e,set:i,...a?{step:a}:{}}}function ky(e,t,n){if(!hy(t))return t;let r=Object.keys(t).filter(e=>[`free`,`carry`,`hinge`,`follow`,`switch`].includes(e)&&hy(t[e]));if(r.length!==1)return t;let{[r[0]]:i,...a}=t;return Object.keys(i).some(e=>e in a)?t:(n.push(`corrected: objects.${e}.${r[0]} { ${Object.keys(i).join(`, `)} } -> objects.${e} { ${Object.keys(i).join(`, `)} }: an entry's kind is read from its fields, not named`),{...a,...i})}function Ay(e,t,n){let r=`objects.${e}`,i=ky(e,t,n);if(!hy(i))return i;let a=xy(i,[...new Set(Object.values(Bt).flat())],r,n);return Array.isArray(a.keys)&&(a={...a,keys:a.keys.map((e,t)=>xy(e,Ht,`${r}.keys[${t}]`,n,yy.objectKey))}),hy(a.fall)?a={...a,fall:xy(a.fall,Vt,`${r}.fall`,n)}:Array.isArray(a.fall)&&(a={...a,fall:a.fall.map((e,t)=>xy(e,Vt,`${r}.fall[${t}]`,n))}),a}let jy=(e,t,n)=>xy(e,Kt,`goals[${t}]`,n);function My(e,t){return hy(e)?Object.fromEntries(Object.entries(e).map(([e,n])=>[e,hy(n)&&Lt[n.kind]?xy(n,[`kind`,...Lt[n.kind]],`anchors.${e}`,t,yy.anchor):n])):e}function Ny(e,t){return hy(e)?Object.fromEntries(Object.entries(e).map(([e,n])=>[e,Gt[e]?Sy(n,(n,r)=>xy(n,Gt[e],`checks.${e}[${r}]`,t)):n])):e}function Py(e,t=[]){if(!hy(e))return e;let n=t.length,r=xy(e,[...Pt,`probes`],`spec`,t);if(r=wy(r,t),Array.isArray(r.use)&&(r={...r,use:r.use.map((e,n)=>Ty(e,n,t))}),r.anchors&&(r={...r,anchors:My(r.anchors,t)}),Array.isArray(r.keys)){let e=Ey(r);r={...r,keys:r.keys.map((n,r)=>Oy(n,r,e,t))}}return hy(r.objects)&&(r={...r,objects:Object.fromEntries(Object.entries(r.objects).map(([e,n])=>[e,Ay(e,n,t)]))}),Array.isArray(r.goals)&&(r={...r,goals:r.goals.map((e,n)=>jy(e,n,t))}),Array.isArray(r.fits)&&(r={...r,fits:r.fits.map((e,n)=>xy(e,zt,`fits[${n}]`,t))}),r.checks&&(r={...r,checks:Ny(r.checks,t)}),t.length>n?r:e}function Fy(e,{sk:t,scene:n}={},r=[],i=[]){if(!hy(e)||!Array.isArray(e.use)||!e.use.some(e=>e?.skill===`walkTo`&&hy(e.args)&&e.args.to!==void 0))return e;let a=Object.values(hy(e.objects)?e.objects:{}).flatMap(e=>hy(e)?[e.grab,e.release,...Array.isArray(e.keys)?e.keys.map(e=>e?.f):[]]:[]).filter(Number.isFinite),o=r=>{let i={refusals:[],stretch:e.frames===`auto`,busy:a,last:Number.isFinite(e.frames)?e.frames-1:1/0,noStop:r,stopped:new Set};try{return{used:S_({...e,frames:2e4},{sk:t,scene:n,fit:i,auto:!0}).used,fit:i}}catch{return null}},s=o(new Set);if(!s)return e;if(s.fit.stopped.size){let e=new Set(s.fit.stopped),t=o(e);if(t&&t.fit.refusals.length<s.fit.refusals.length){for(let n of[...s.fit.stopped].sort((e,t)=>e-t)){e.delete(n);let r=o(e);(!r||r.fit.refusals.length>t.fit.refusals.length)&&e.add(n)}s=o(e)??t}}let{used:c,fit:l}=s;if(i.push(...l.refusals),!c.some(e=>e.adjust))return e;for(let e of c)e.adjust&&r.push(`corrected: ${e.adjust.note}`);return{...e,use:e.use.map((e,t)=>c[t]?.adjust?{...e,args:{...e.args,...c[t].adjust.args}}:e)}}let Iy=(e,t,n,r)=>Fy(Py(e,n),t,n,r),Ly=e=>e?.groups??[],Ry=e=>Ly(e).reduce((e,t)=>e+(t.severity??0),0),zy=e=>(e?.goals??[]).filter(e=>e.ok).map(e=>e.name),By=e=>(e?.goals??[]).filter(e=>!e.ok).length,Vy=e=>Math.round(e*100)/100;function Hy(e){let t=new Map;for(let n of Ly(e))t.set(vm(n),(t.get(vm(n))??0)+(n.severity??0));return t}function Uy(e,t){let n=Hy(t),r=Hy(e),i=0,a=0,o=[];for(let[e,t]of r){let r=n.get(e)??0;t>r+1e-9?(i+=t-r,o.push({key:e,severity:[Vy(r),Vy(t)]})):a+=r-t}for(let[e,t]of n)r.has(e)||(a+=t);o.sort((e,t)=>t.severity[1]-t.severity[0]-(e.severity[1]-e.severity[0]));let s=[Vy(Ry(t)),Vy(Ry(e))],c=[Ly(t).length,Ly(e).length],l=zy(t).filter(t=>!zy(e).includes(t)),u=Ry(e)<Ry(t)-1e-9?By(e)>By(t)||l.length?`goal ${l.length?`"${l[0]}" no longer holds`:`failures rose`}`:c[1]>c[0]?`problems ${c[0]} -> ${c[1]}`:i>.5*a+1e-9?`${o[0]?.key} got worse (${o[0]?.severity.join(` -> `)})`:``:`summed severity ${s[0]} -> ${s[1]}`;return{better:!u,why:u,sev:s,problems:c,worse:o.slice(0,3)}}let Wy=(e,t)=>(Ly(e).length<Ly(t).length||Ly(e).length===Ly(t).length&&Ry(e)<=Ry(t)+1e-9)&&By(e)<=By(t)&&zy(t).every(t=>zy(e).includes(t)),Gy=(e,t)=>Ly(e).filter(e=>vm(e)===t).reduce((e,t)=>e+(t.severity??0),0),Ky=e=>Math.round(performance.now()-e);function qy(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 Jy(e,t,n,r){let i=r(e),a=e.timeMap?qy(t,e.timeMap):t;if(!e.fixed?.retime)return{out:e,measured:a,qa:i};let o=n({...t,fix:{...t.fix,retime:!1}}),s=r(o);if(Wy(i,s))return{out:e,measured:a,qa:i};let c=Uy(i,s);return o.fixed={...o.fixed,retime:{...e.fixed.retime,kept:!1,how:`${e.fixed.retime.how}; not kept: QA was worse with it (summed severity ${c.sev[0]} -> ${c.sev[1]}, problems ${c.problems[0]} -> ${c.problems[1]}), so the take is compiled without it`}},{out:o,measured:t,qa:s}}function Yy(e,t){if(!e||typeof e!=`object`||Array.isArray(e))return{spec:e,probes:t};let n=ad(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 Xy(e,{sk:t,scene:n},r={}){let i=[],a=[];try{return Zy(r.lenient?e:Iy(e,{sk:t,scene:n},i,a),{sk:t,scene:n},r,i,a)}catch(e){throw a.length&&/^MOTION_SPEC_INVALID/.test(e.message)&&!e.message.includes(a[0])&&(e.message=e.message.replace(/^MOTION_SPEC_INVALID: /,`MOTION_SPEC_INVALID: ${a.join(`
6
+ `)}\n`)),i.length&&/^MOTION_SPEC_INVALID/.test(e.message)&&(e.message+=`\n(adjusted before these checks: ${i.join(`; `)})`),e}}function Zy(e,{sk:t,scene:n},r,i,a){let{spec:o,probes:s}=Yy(e,r.probes),c=performance.now(),l={sk:t,scene:n,lenient:r.lenient===!0,problems:a},u=r.checks&&Cm({checks:r.checks}).checks,d=[],f=e=>{let{spec:r,used:a}=S_(Cm(ry(e,d)),l),o=ov(JSON.parse(JSON.stringify(u?{...r,checks:u}:r)),n,i,t);return Dd(o,l),{full:o,used:a}};d=ny(o,{sk:t,scene:n,trial:e=>{let n=f(e).full;return xm(t,uu({fps:24,...n},l),n,{explain:!1})}});let p=o?.frames===`auto`&&d.length?{...o,frames:F_(ry(o,d),l).frames}:F_(o,l),m=E_(p),h=p,g=null;m.length&&({spec:h,search:g}=N_(p,m,l,e=>f(e).full)),o?.frames===`auto`&&L_(h);let _=performance.now(),{full:v,used:y}=f(h),{out:b,measured:x,qa:S}=Jy(uu({fps:24,...v},l),v,e=>uu({fps:24,...e},l),e=>xm(t,e,e.timeMap?qy(v,e.timeMap):v,{userSpec:h})),C=Ky(_),w=performance.now(),T=Vi(t,b,x.checks),E=Hd(b);E&&Object.assign(T,{units:`${T.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, lean = the lean a mouth key took by default (give lean 0 to hold aim exactly)`,held:E});let D=Dv(t,b),O=vf(t,b);D&&Object.assign(T,{units:`${T.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:D}),O&&Object.assign(T,{units:`${T.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:O});let k=eu(t,b);k&&Object.assign(T,{units:`${T.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:k}),Object.assign(T,{units:`${T.units}; life (warnings, not QA): [share or s, worst f0, f1]; keys [gap cv, even channels, half-second share]; jerk [p95 m/s^3, joint, spikes]`,life:Uv(t,b,h?.keys)});let A=y.filter(e=>e.paced).map(e=>`${e.skill} paced ${e.paced.toFixed(2)} to fit f${e.f0}..f${e.end}`);A.length&&(T.paced=A);let j=y.filter(e=>e.cut).map(e=>`${e.skill}: ${e.cut}`);j.length&&(T.cut=j);let M=y.filter(e=>e.placed).map(e=>`${e.skill} f${e.f0}: ${e.placed.why}: at [${e.placed.at.join(`, `)}], heading ${e.placed.heading}`);M.length&&(T.placed=M),d.length&&(T.planned=d.map(e=>e.note));let N=my(b,{used:y,use:h?.use,checks:x.checks,goals:x.goals,results:S.goals});N&&(T.asked=N);let P=q_(b,{used:y,spec:v}),ee=s&&vv(b,s,{spec:v,sk:t,scene:n,notes:i});return{out:b,qa:S,observations:T,spec:h,expanded:v,used:y,fixed:b.fixed,search:g,timeline:P,...ee?{probes:ee}:{},...i.length?{notes:i}:{},ms:{compile:C,qa:Ky(w),total:Ky(c)}}}let Qy=[-1,1,-2,2],$y=e=>typeof e==`number`&&Number.isFinite(e),eb=e=>mm(e)<=.5*Math.max(Math.abs(e.limit),.01);function tb(e,t){let n=/\buse\[(\d+)\] (\w+)/.exec(e),r=Number(n?.[1]),i=n&&t.use?.[r],a=n&&i?.skill===n[2]?Lm[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($y(c))for(let a of Qy.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 nb(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 rb=e=>Math.round(e*1e4)/1e4;function ib(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&&$y(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?rb(n+t*.03):n)})));else if(a){let{u:e,i:t}=a,r=Lm[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]??Lm[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?rb(n+t*s):n)));else if($y(r))for(let e of[-n,n])r+e*s>.05&&i.push(c(rb(r+e*s)))}}let s=r.map((e,t)=>({f:e?.f,j:t})).filter(({j:e})=>$y(r[e]?.set?.spine_pitch)&&$y(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 ab=[10,20,30,-10];function ob(e,t){let n=e.keys??[],r=Math.round(t),i=Number.isInteger(e.frames)?e.frames-1:1/0,a=n.filter(e=>$y(e?.f)&&$y(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 ab.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 sb(e,t){if(typeof e.cause!=`string`||e.kind===`WRIST`&&e.cause.startsWith(`fix.self`))return[];if(e.kind===`WRIST`&&$y(e.worstFrame))return[...ob(t,e.worstFrame),...[1,2].flatMap(n=>ib(t,e.worstFrame,n)).filter(e=>e.every(e=>e.path.endsWith(`/spine_pitch`)))];let n=!e.patch&&eb(e)?[...tb(e.cause,t),...nb(e,e.cause,t)]:[];return e.kind!==`GOAL`||!$y(e.worstFrame)||mm(e)>.1?n:[...n,...ib(t,e.worstFrame,1),...ib(t,e.worstFrame,2)]}let cb=e=>e?.qa?.groups??[],lb=e=>cb(e).reduce((e,t)=>e+(t.severity??0),0),ub=e=>(e?.qa?.goals??[]).filter(e=>!e.ok).length,db=(e,t)=>cb(e).find(e=>vm(e)===t),fb=(e,t)=>Uy(e?.qa,t?.qa).better,pb=/^(hand|farm|uarm)_[lr]\b|^(Left|Right)(Arm|ForeArm|Hand)/,mb=e=>e.kind===`REACH`&&/^arm_miss_[lr]$/.test(e.subject)||[`WRIST`,`ELBOW`].includes(e.kind)||[`BODY`,`FACE`,`COLLIDE`,`POP`,`ACC`,`SPEED`,`FLIP`].includes(e.kind)&&pb.test(e.subject),hb=e=>e.some(e=>/^\/use\/\d+\/args(\/(at|step|firstStep|to))?$/.test(e.path)&&(!e.path.endsWith(`/args`)||[`at`,`step`,`firstStep`,`to`].some(t=>e.value?.[t]!==void 0))||/^\/keys\/\d+\/set\/root$/.test(e.path)||e.path===`/keys/-`&&e.value?.set?.root!==void 0);function gb(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=new Set,f=[],p=e=>{try{let t=qp(o,e);return c+=1,{next:t,r:n(t)}}catch{return l+=1,null}},m=(e,t,n,{next:r,r:i})=>{let a=mb(e)&&hb(n);f.push({kind:e.kind,subject:e.subject,patch:n,severity:[e.severity,db(i,t)?.severity??0],...a?{movedBody:`the body moved: no change that keeps it in place fixed this arm's failure`}:{}}),o=r,s=i},h=(e,t)=>{let n=null;for(let e of t){if(a())break;let t=p(e);if(t&&(fb(t.r,n?.r??s)?(n&&(l+=1),n={...t,patch:e}):l+=1,n&&lb(n.r)===0&&!ub(n.r)))break}return n&&m(e,vm(e),n.patch,n),!!n},g=e=>{let t=sb(e,o);return mb(e)?h(e,t.filter(e=>!hb(e)))||h(e,t.filter(hb)):h(e,t)};for(;u<r&&!a();){let e=cb(s).filter(e=>e.patch&&!d.has(JSON.stringify(e.patch))).slice(0,3).map(vm),t=cb(s).filter(e=>sb(e,o).length).slice(0,2).map(vm);if(!e.length&&!t.length)break;u+=1;let n=0,r=[];for(let t of e){let e=db(s,t);if(!e?.patch||a())continue;if(mb(e)&&hb(e.patch)){r.push(t);continue}let i=p(e.patch);i&&fb(i.r,s)?(m(e,t,e.patch,i),n+=1):(d.add(JSON.stringify(e.patch)),i&&(l+=1))}for(let e of n?[]:t){let t=db(s,e);if(t&&!a()&&g(t)){n+=1;break}}for(let e of n?[]:r){let t=db(s,e);if(!t?.patch||a())continue;let r=p(t.patch);if(r&&fb(r.r,s)){m(t,e,t.patch,r),n+=1;break}d.add(JSON.stringify(t.patch)),r&&(l+=1)}if(!n)break}return{spec:o,result:s,repair:{rounds:u,compiles:c,applied:f,rejected:l}}}let _b=e=>Math.round(e*100)/100;function vb(e,t,n){let r=0;for(let i of t?.groups??[]){if(!i.patch)continue;if(r>=2){delete i.patch;continue}r+=1;let a=vm(i),o;try{o=n(qp(e,i.patch))}catch(e){o={error:String(e?.message??e).slice(0,160)}}let s=i.patch.map(e=>`${e.op} ${e.path}`).join(`, `),c=i.patch;if(delete i.patch,o?.error||!o?.groups){i.patchTried=`${s}: does not compile (${o?.error??`no QA`})`;continue}let l=[_b(Gy(t,a)),_b(Gy(o,a))],u=Uy(o,t);if(l[1]<l[0]&&u.better){i.patch=c,i.patchChecked={severity:l,total:u.sev,problems:u.problems};continue}let d=l[1]<l[0]?u.why:`this group ${l[0]} -> ${l[1]}`;i.patchTried=`${s}: compiled, did not help (${d}${d.startsWith(`problems`)?``:`; problems ${u.problems[0]} -> ${u.problems[1]}`})`}return t}function yb(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=Os(t),i=ks(t),o=r||i,s=!As(t),c=s?new Float32Array(a*3):null,l=s?new Float32Array(a*4):null,f=o||!s||yo(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?Wo(n,t):[0,0],v=r&&Zo(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=ts(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 bb=(e,t)=>{throw Object.assign(Error(t),{code:e})};function xb(e){try{return e()}catch(e){throw/^MOTION_SPEC_INVALID/.test(e.message)&&bb(`INVALID_ARGUMENT`,e.message.replace(/^MOTION_SPEC_INVALID:\s*/,``)),e}}let Sb=e=>Array.isArray(e)?e.map(Sb):e&&typeof e==`object`&&!ArrayBuffer.isView(e)?Object.fromEntries(Object.keys(e).map(t=>[t,Sb(e[t])])):e;function Cb({model:e,scene:t,spec:n,probes:r,checks:i,lenient:a,repair:o,bake:s}){let c=En(e),l={...i?{checks:i}:{},...a?{lenient:!0}:{}},u=e=>{let{spec:n,probes:i}=Yy(e,r);n?.fps!==void 0&&n.fps!==24&&bb(`INVALID_ARGUMENT`,`spec.fps must be 24, the timeline rate.`);let a={fps:24,...n},o=xb(()=>Xy(a,{sk:c,scene:t},{...l,probes:i}));return{...o,stored:o.spec===a?n:o.spec}},d=[],f=o&&!a?Iy(n,{sk:c,scene:t},d):n,p=new Map,m=e=>{let t=u(e);return p.set(JSON.stringify(e),t.qa),t},h=performance.now(),g=o?gb(f,o,m):{spec:n,result:m(n)};if(o)vb(g.spec,g.result.qa,e=>p.get(JSON.stringify(e))??m(e).qa);else for(let e of g.result.qa?.groups??[])delete e.patch;let _=g.result,v=g.spec?.label??null,y=g.repair?{..._.ms,repair:Math.round(performance.now()-h)}:_.ms,b=s?yb(c,_.out,{position:s.position,yaw:s.yaw},{spec:_.stored,prompt:(`prompt`in s?s.prompt:v)??``}):void 0,x=b&&kn(t,[_.expanded,_.stored]);return x&&(b.people=x),{stored:_.stored,qa:_.qa,observations:_.observations,used:_.used,fixed:_.fixed,search:_.search,timeline:_.timeline,..._.probes?{probes:_.probes}:{},..._.notes||d.length?{notes:[...d,..._.notes??[]]}:{},ms:y,frameCount:_.out.frameCount,label:v,...g.repair?{repair:g.repair}:{},...b?{take:b}:{}}}function wb(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))wb(n,t);return[...t]}function Tb({id:e,job:t}){try{let n=Cb(Sb(t));return{message:{id:e,value:n},transfer:wb(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}=Tb(e);self.postMessage(t,n)}})();