<!-- RUBY-BACKUP · part 549 -->
.5;
    var dc = Math.sqrt(x*x+z*z);
    var flat = Math.min(1, dc/100);
    return h * flat;
  }catch(e){ return 0; }
}
/* Metrics collector */
var _metrics = { positions: [], builds: [], fixes: 0 };
try{
  var _m = localStorage.getItem('ruby-metrics-v1');
  if(_m) _metrics = JSON.parse(_m);
}catch(e){}
function saveMetrics(){
  try{ localStorage.setItem('ruby-metrics-v1', JSON.stringify(_metrics)); }catch(e){}
}
setInterval(function(){
  try{
    if(typeof mePos!=='undefined'){
      _metrics.positions.push({x: Math.round(mePos.x), z: Math.round(mePos.z), t: Date.now()});
      if(_metrics.positions.length > 1000) _metrics.positions.shift();
      saveMetrics();
    }
  }catch(e){}
}, 10000); /* every 10s — not a 5s check */
/* Inconsistency fixer — runs once on load, fixes what doesn't sit right */
function fixInconsistencies(){
  try{
    if(typeof scene==='undefined') return;
    var fixed = 0;
    scene.traverse(function(obj){
      if(!obj.isMesh || !obj.position) return;
      // skip terrain, highways, infrastructure (they're placed deliberately)
      var n = obj.name || '';
      if(n.indexOf('street')===0 || n==='plaza' || n.indexOf('power-')===0 ||
         n.indexOf('intersection')===0 || n.indexOf('train-')===0 || n.indexOf('crossing-')===0 ||
         n.indexOf('bus-')===0 || n.indexOf('runway')===0 || n.indexOf('airport-')===0 ||
         n.indexOf('control-')===0 || n.indexOf('tower-')===0 || n.indexOf('launch-')===0 ||
         n.indexOf('rocket-')===0 || n.indexOf('gantry')===0 || n.indexOf('spaceport-')===0 ||
         n.indexOf('highway-')===0 || n==='hwy-post' || n.indexOf('hwy-')===0) return;
      // check if floating or buried relative to terrain
      var th = getTerrainH(obj.position.x, obj.position.z);
      var dy = obj.position.y - th;
      // if more than 3 units off the terrain (and not meant to fly), fix it
      if(Math.abs(dy) > 3 && obj.position.y < 50){
        // don't move the player or camera-attached things
        if(n.indexOf('player')>=0 || n.indexOf('avatar')>=0 || n.indexOf('king')>=0) return;
        obj.position.y = th + 0.5;
        fixed++;
      }
    });
    _metrics.fixes += fixed;
    saveMetrics();
    if(fixed>0 && typeof codeStatus==='function') codeStatus('topography fixed '+fixed+' inconsistencies');
  }catch(e){}
}
setTimeout(fixInconsistencies, 8000); /* after everything loads */
/* Topographic map overlay — 'topo' code shows it */
(function(){
  var _t2=0;
  var _iv=setInterval(function(){
    _t2++;
    if(typeof processCode==='function'){
      var _op = processCode;
      processCode = function(line){
        var tl=(line||'').trim().toLowerCase();
        if(tl==='topo'){ showTopoMap(); return true; }
        if(tl==='metrics'){ showMetrics(); return true; }
        if(tl==='skyview'){
          try{
            if(typeof camera!=='undefined'){
              camera.position.set(0, 900, 900);
              camera.lookAt(0, 0, 0);
              if(typeof controls!=='undefined' && controls.target){
                controls.target.set(0, 0, 0);
                controls.update();
              }
              codeStatus('skyview — the full highways below you');
            }
          }catch(e){}
          return true;
        }
        return _op.apply(this, arguments);
      };
      clearInterval(_iv);
    }
    if(_t2>60) clearInterval(_iv);
  }, 500);
})();
function showTopoMap(){
  try{
    // full-screen topographic overlay — contour lines + heatmap
    var existing = document.getElementById('topo-overlay');
    if(existing){ existing.remove(); return; } // toggle off
    var ov = document.createElement('div');
    ov.id = 'topo-overlay';
    ov.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;z-index:9999;background:rgba(0,0,0,0.85);display:flex;flex-direction:column;align-items:center;justify-content:center;';
    var title = document.createElement('div');
    title.innerHTML = 'TOPOGRAPHIC MAP';
    title.style.cssText = 'color:#d8a920;font-size:20px;font-weight:bold;margin-bottom:10px;font-family:sans-serif;';
    ov.appendChild(title);
    var cv = document.createElement('canvas');
    var SZ = Math.min(window.innerWidth-40, window.innerHeight-120);
    cv.width = SZ; cv.height = SZ;
    cv.style.cssText = 'border:2px solid #d8a920;border-radius:10px;';
    ov.appendChild(cv);
    var ctx = cv.getContext('2d');
    var MAP = 2400, scale = SZ/MAP, cx = SZ/2, cy = SZ/2;
    // background
    ctx.fillStyle = '#0a1a0a';
    ctx.fillRect(0, 0, SZ, SZ);
    // contour lines — sample terrain
    var step = 20;
    for(var gx=-MAP/2; gx<MAP/2; gx+=step){
      for(var gz=-MAP/2; gz<MAP/2; gz+=step){
        var h = getTerrainH(gx, gz);
        // color by elevation
        var t = Math.max(0, Math.min(1, (h+14)/28));
        var r = Math.floor(20+t*60), gg = Math.floor(60+t*80), b = Math.floor(20+t*30);
        ctx.fillStyle = 'rgb('+r+','+gg+','+b+')';
        var px = cx+gx*scale, py = c