<!-- RUBY-BACKUP · part 478 -->
;
    var x = px + Math.cos(ang)*15, z = pz + Math.sin(ang)*15;
    var house = buildCosmicHouse(x, z, 0.55 + i*0.08);
    scene.add(house);
    COSMIC_HOUSES.push(house);
  }
  codeStatus('cosmic houses — 5 crystalline homes');
}
function cosmicEnhanced(){
  cosmicSky();
  cosmicHouses();
  spawnWildlife();
  COSMIC_BUILT = true;
  codeStatus('COSMIC ENHANCED — houses, sky, wildlife. The whole world.');
}
function clearCosmic(){
  ['cosmic-stars','cosmic-nebula-0','cosmic-nebula-1','cosmic-nebula-2','cosmic-planet','cosmic-ring'].forEach(function(n){
    var o = scene.getObjectByName(n);
    if(o) scene.remove(o);
  });
  COSMIC_HOUSES.forEach(function(h){ try{ scene.remove(h); }catch(e){} });
  COSMIC_HOUSES = [];
  COSMIC_BUILT = false;
  codeStatus('cosmic cleared');
}
function spawnWildlife(){
  if(typeof THREE==='undefined' || typeof scene==='undefined') return;
  if(typeof mePos==='undefined') return;
  WILD_ANIMALS.forEach(function(w){
    try{ scene.remove(w.mesh); }catch(e){}
    if(w.anim) clearInterval(w.anim);
  });
  WILD_ANIMALS = [];
  var builders = {deer: buildDeer, rabbit: buildRabbit, eagle: buildEagle, crab: buildCrab, dolphin: buildDolphin};
  var px = mePos.x, pz = mePos.z;
  WILD_TYPES.forEach(function(type, i){
    var ang = (i/WILD_TYPES.length)*Math.PI*2;
    var x = px + Math.cos(ang)*10, z = pz + Math.sin(ang)*10;
    var group = builders[type.id]();
    group.position.set(x*0.13, 0, z*0.13);
    /* face outward */
    group.rotation.y = -ang;
    scene.add(group);
    var t = Math.random()*10;
    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