<!-- RUBY-BACKUP · part 331 -->

    g.userData.isPrimitive = true;
    if(typeof scene!=='undefined') scene.add(g);
    window._infra = g;
    if(typeof codeStatus==='function') codeStatus('infrastructure stands');
    if(typeof pushCommand==='function'){
      var sr=(typeof scene!=='undefined')?scene:null;
      pushCommand({name:'build infrastructure', execute:function(){ if(sr) sr.add(g); }, undo:function(){ if(sr) sr.remove(g); }});
    }
  }catch(e){
    if(typeof codeStatus==='function') codeStatus('infrastructure failed');
  }
}
setTimeout(function(){ try{ if(typeof buildInfrastructure==='function') buildInfrastructure(); }catch(e){} }, 4000);
/* TOPOGRAPHIC METRICS — the game watches itself, fixes its inconsistencies */
/* Terrain height function (must match buildFloorMesh) */
function getTerrainH(x, z){
  try{
    var h = Math.sin(x*0.01)*Math.cos(z*0.01)*8
          + Math.sin(x*0.03+1.7)*Math.cos(z*0.025+0.6)*4
          + Math.sin(x*0.08+4.2)*Math.cos(z*0.07+2.1)*1.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-