// Locate complete silhouettes before slicing. Use one scale per animation and a shared motion anchor. const fs=require('fs'),path=require('path'); const sharp=require('C:/Users/EDY/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/sharp'); const root=path.resolve(__dirname,'../..'),art=path.join(root,'Assets/BuffaloSiege/Art'); function components(data,w,h){ const seen=new Uint8Array(w*h),queue=new Int32Array(w*h),out=[]; for(let p=0;p=w||ny<0||ny>=h||seen[n]||data[n*4+3]<32)continue;seen[n]=1;queue[tail++]=n;} } if(count>200)out.push({left:Math.max(0,l-3),top:Math.max(0,t-3),width:Math.min(w-1,r+3)-Math.max(0,l-3)+1,height:Math.min(h-1,b+3)-Math.max(0,t-3)+1,count,cx:sx/count,cy:sy/count}); } return out.sort((a,b)=>Math.floor(a.cy/(h/2))-Math.floor(b.cy/(h/2))||a.cx-b.cx); } async function main(){ const report=[];const previews=[]; for(const id of ['rabbit','bison','wolf','vulture','scorpion','king']){ const source=path.join(__dirname,'sources/animation.'+id+'.png');const {data,info}=await sharp(source).ensureAlpha().raw().toBuffer({resolveWithObject:true}); const boxes=components(data,info.width,info.height);if(boxes.length!==6)throw Error(id+' expected six isolated silhouettes; found '+boxes.length); const anchors=boxes.map((b,i)=>({x:b.cx,y:b.top+b.height+(id==='rabbit'?[0,12,30,14,0,0][i]:0)})); if(id==='vulture')[[300,365],[790,365],[1280,365],[285,800],[790,828],[1300,830]].forEach(([x,y],i)=>anchors[i]={x,y}); const left=Math.max(...boxes.map((b,i)=>anchors[i].x-b.left)),right=Math.max(...boxes.map((b,i)=>b.left+b.width-anchors[i].x)); const top=Math.max(...boxes.map((b,i)=>anchors[i].y-b.top)),bottom=Math.max(...boxes.map((b,i)=>b.top+b.height-anchors[i].y)); const scale=Math.min(222/(left+right),222/(top+bottom));const targetX=128+(left-right)*scale/2,targetY=128+(top-bottom)*scale/2; const folder=path.join(art,'EnemyAnimations',id);fs.mkdirSync(folder,{recursive:true}); const frameBuffers=[]; for(let i=0;i<6;i++){ const b=boxes[i],crop={left:b.left,top:b.top,width:b.width,height:b.height};const input=await sharp(source).extract(crop).resize(Math.round(b.width*scale),Math.round(b.height*scale),{fit:'fill'}).png().toBuffer(); const x=Math.round(targetX-(anchors[i].x-b.left)*scale),y=Math.round(targetY-(anchors[i].y-b.top)*scale); const frame=await sharp({create:{width:256,height:256,channels:4,background:'#00000000'}}).composite([{input,left:x,top:y}]).png().toBuffer(); fs.writeFileSync(path.join(folder,'frame-'+String(i).padStart(2,'0')+'.png'),frame);frameBuffers.push(frame); previews.push({input:await sharp(frame).resize(150).toBuffer(),left:i*160+5,top:report.length*182+27}); } fs.writeFileSync(path.join(art,'enemy.'+id+'.png'),frameBuffers[0]);fs.copyFileSync(path.join(art,'enemy.'+id+'.png'),path.join(root,'docs/ui-style-a/assets/sprites/enemy.'+id+'.png')); report.push({id,frameCount:6,fps:id==='rabbit'?12:id==='vulture'||id==='king'?8:10,canvas:[256,256],pivot:[.5,.5],source:'sources/animation.'+id+'.png',scale,anchors,sourceRects:boxes,files:Array.from({length:6},(_,i)=>'EnemyAnimations/'+id+'/frame-'+String(i).padStart(2,'0')+'.png')}); } const labels=Buffer.from(''+report.map((r,i)=>''+r.id+' · '+r.fps+' FPS · 6 frames').join('')+''); await sharp({create:{width:960,height:1092,channels:4,background:'#523e51'}}).composite([...previews,{input:labels}]).png().toFile(path.join(__dirname,'enemy-frames.png')); fs.writeFileSync(path.join(__dirname,'animation-manifest.json'),JSON.stringify(report,null,2)+'\n'); for(const file of [path.join(art,'asset-manifest.json'),path.join(root,'docs/ui-style-a/asset-manifest.json')]){ const doc=JSON.parse(fs.readFileSync(file));for(const a of report){const e=doc.assets.find(x=>x.id==='enemy.'+a.id);for(const key of ['atlas','grid','cellIndex','sourceRect','uv'])delete e[key];Object.assign(e,{width:256,height:256,transparentPadding:[16,16,16,16],source:'docs/art-refresh-2026-10-29/sources/animation.'+a.id+'.png',sourceRoot:'project',revision:'2026-10-29',animation:a.files,animationFps:a.fps});} doc.enemyAnimationManifest='docs/art-refresh-2026-10-29/animation-manifest.json';fs.writeFileSync(file,JSON.stringify(doc,null,2)+'\n'); } console.log(report.map(x=>({id:x.id,frames:x.frameCount,scale:x.scale,boxes:x.sourceRects.map(b=>[b.left,b.top,b.width,b.height])}))); } main().catch(e=>{console.error(e);process.exit(1);});