<!-- RUBY-BACKUP · part 238 -->
0;
    var state = 'idle'; /* idle, graze, alert, flee, hop, soar, scuttle, swim */
    var stateT = 0;
    var homeX = x*0.13, homeZ = z*0.13;
    var anim = setInterval(function(){
      t += 0.05; stateT += 0.05;
      if(typeof mePos==='undefined') return;
      var px = mePos.x*0.13, pz = mePos.z*0.13;
      var dx = px - group.position.x, dz = pz - group.position.z;
      var distToPlayer = Math.sqrt(dx*dx+dz*dz);
      /* state changes */
      if(stateT > 4){
        stateT = 0;
        var r = Math.random();
        if(type.id==='deer'){
          if(distToPlayer < 3) state = 'flee';
          else if(r<0.4) state = 'graze';
          else if(r<0.6) state = 'alert';
          else state = 'idle';
        }else if(type.id==='rabbit'){
          if(distToPlayer < 2) state = 'flee';
          else if(r<0.5) state = 'hop';
          else state = 'idle';
        }else if(type.id==='eagle'){
          if(r<0.3) state = 'soar';
          else state = 'idle';
        }else if(type.id==='crab'){
          if(distToPlayer < 1.5) state = 'flee';
          else if(r<0.5) state = 'scuttle';
          else state = 'idle';
        }else if(type.id==='dolphin'){
          if(r<0.4) state = 'swim';
          else state = 'idle';
        }
      }
      /* behaviors */
      if(state==='flee'){
        /* run away from player */
        var fleeSpeed = 0.03;
        group.position.x -= (dx/distToPlayer)*fleeSpeed;
        group.position.z -= (dz/distToPlayer)*fleeSpeed;
        group.position.y = Math.abs(Math.sin(t*8))*0.08; /* bounding */
        group.rotation.y = Math.atan2(-dz, -dx);
        if(distToPlayer > 8) state = 'idle';
      }else if(state==='graze' && type.id==='deer'){
        /* head down */
        group.rotation.x = 0.3;
        group.position.y = 0;
      }else if(state==='alert' && type.id==='deer'){
        /* head up, still, watching */
        group.rotation.x = 0;
        group.rotation.y += 0.02;
      }else if(state==='hop' && type.id==='rabbit'){
        group.position.y = Math.abs(Math.sin(t*6))*0.15;
        group.position.x += 0.01;
      }else if(state==='soar' && type.id==='eagle'){
        /* circle upward */
        group.position.y = 1 + Math.sin(t*0.5)*0.5;
        group.rotation.y += 0.03;
        group.position.x += Math.cos(group.rotation.y)*0.02;
        group.position.z += Math.sin(group.rotation.y)*0.02;
      }else if(state==='scuttle' && type.id==='crab'){
        /* sideways */
        group.position.x += Math.sin(t*2)*0.015;
        group.position.z += 0.01; /* sideways walk */
      }else if(state==='swim' && type.id==='dolphin'){
        /* arc through */
        group.position.y = 0.3 + Math.sin(t*1.5)*0.3;
        group.position.x += 0.015;
        group.rotation.z = Math.sin(t*1.5)*0.2;
      }else{
        /* idle — subtle */
        group.rotation.x *= 0.9;
        group.position.y += (0 - group.position.y)*0.05;
      }
      /* stay near home */
      var hx = group.position.x - homeX, hz = group.position.z - homeZ;
      if(Math.sqrt(hx*hx+hz*hz) > 5){
        group.position.x = homeX;
        group.position.z = homeZ;
      }
    }, 50);
    WILD_ANIMALS.push({mesh: group, anim: anim, type: type.id});
  });
  codeStatus('wildlife — real deer, rabbit, eagle, crab, dolphin.');
}
function clearWildlife(){
  WILD_ANIMALS.forEach(function(w){
    try{ scene.remove(w.mesh); }catch(e){}
    if(w.anim) clearInterval(w.anim);
  });
  WILD_ANIMALS = [];
  codeStatus('wildlife cleared');
}
function showPets(){
  var p = document.getElementById('pets');
  if(!p){
    p = document.createElement('div');
    p.id = 'pets';
    p.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);z-index:10000;background:rgba(0,20,0,0.95);border:2px solid #44ff44;border-radius:12px;padding:20px;color:#fff;font:13px monospace;max-width:400px;max-height:80vh;overflow:auto;';
    document.body.appendChild(p);
  }
  p.style.display = 'block';
  var html = '<b style="color:#4f4;font-size:16px;">CHOOSE YOUR PET</b> ';
  html += '<button onclick="hidePets()">CLOSE</button> ';
  if(ACTIVE_PET) html += '<button onclick="dismissPet()">DISMISS</button>';
  html += '<br><br>';
  PETS.forEach(function(pet){
    var sel = ACTIVE_PET===pet.id;
    html += '<div style="padding:8px;margin-bottom:6px;border:2px solid '+(sel?'#4f4':'#333')+';border-radius:8px">';
    html += '<div style="width:30px;height:30px;border-radius:50%;background:'+pet.color+';display:inline-block;vertical-align:middle;margin-right:10px"></div>';
    html += '<b style="color:#4f4">'+pet.name+'</b> <i style="color:#aaa">'+pet.desc+'</i><br>';
    if(!sel) html += '<button onclick="selectPet(\''+pet.id+'\')">ADOPT</button>';
    else html += '<span style="color:#4f4">WITH YOU</span>';
    html += '</div>';
  });
  p.innerHTML = html;
}
function hidePets(){
  var p = document.getElementById('pets');
  if(p) p.style.display='none';
}
function selectPet(id){
  ACTIVE_PET = id;
