<!-- RUBY-BACKUP · part 289 -->
= 1-d/2; h = h*(1-sh) + (-0.24)*sh; }
  return h;
}
window.isWater = function(x, z){
  try{ var h = landBaseRaw(x, z); return h < WATER_LEVEL; }
  catch(e){ return false; }
};
function landBase(x, z){
  /* the plateau — 2 grids of sure flat ground, for us to stand */
  var P = (typeof PLATEAU!=='undefined' && PLATEAU) ? PLATEAU : {x:8, z:8, r:2.5, h:0.9};
  var pdx = x-P.x, pdz = z-P.z, pd = Math.sqrt(pdx*pdx+pdz*pdz);
  if(pd < P.r) return P.h;
  var h = landBaseRaw(x, z);
  /* blend the plateau's edge */
  if(pd < P.r+3){ var t = (pd-P.r)/3; h = P.h*(1-t*t*(3-2*t)) + h*(t*t*(3-2*t)); }
  /* the sacred hollow — the ruby's cell stays open */
  if(d < 2){ var sh = 1-d/2; h = h*(1-sh) + (-0.24)*sh; }
  return h;
}
function applyDigs(h, x, z){
  for(var i=0;i<digs.length;i++){
    var dg = digs[i], dx = x-dg.x, dz = z-dg.z, dist = Math.sqrt(dx*dx+dz*dz);
    if(dist < dg.r){ var t = 1-dist/dg.r; h -= dg.d * t*t*(3-2*t); }
  }
  return h;
}
function landH(x, z){
  if(!landMesh) return 0;
  var h = applyDigs(landBaseRaw(x, z), x, z);
  try{ if(typeof applyLifts==='function') h = applyLifts(h, x, z); }catch(e){}
  var d = Math.sqrt(x*x+z*z);
  if(d < 2) return h;  /* the sacred hollow keeps its depth */
  return Math.max(0.02, h);
}
function persistDigs(){
  try{ localStorage.setItem(DIG_KEY, JSON.stringify(digs)); }catch(e){}
}
function loadDigs(){
  try{ digs = JSON.parse(localStorage.getItem(DIG_KEY)||'[]'); }catch(e){ digs=[]; }
}
function refreshLandMesh(cx, cz, cr){
  if(!landMesh || !landMesh.geometry.userData.topCount) return;
  var pa = landMesh.geometry.attributes.position;
  var topN = landMesh.geometry.userData.topCount;
  for(var i=0; i<topN; i++){
    var x = pa.getX(i), z = pa.getZ(i);
    var dx = x-cx, dz = z-cz;
    if(dx*dx+dz*dz < (cr+3)*(cr+3)) pa.setY(i, FLOOR_Y + 0.02 + landH(x, z));
  }
  pa.needsUpdate = true;
  landMesh.geometry.computeVertexNormals();
}
/* Tool health — digging solid blocks damages tools. 70000 strikes to break one. */
window._toolHealth = window._toolHealth || 100;
window._blockStrikes = window._blockStrikes || {};
function digAt(x, z){
  /* Check if digging near a solid block */
  try{
    if(window._solidBlocks && window._solidBlocks.length > 0){
      for(var _bi = 0; _bi < window._solidBlocks.length; _bi++){
        var _blk = window._solidBlocks[_bi];
        var _dx = x - _blk.position.x, _dz = z - _blk.position.z;
        if(Math.sqrt(_dx*_dx + _dz*_dz) < 3){
          /* Struck the solid block — tool takes damage, block counts the strike */
          var _key = _bi;
          window._blockStrikes[_key] = (window._blockStrikes[_key] || 0) + 1;
          window._toolHealth = Math.max(0, window._toolHealth - 2);
          /* Tool breaks at 0 */
          if(window._toolHealth <= 0){
            if(typeof codeStatus === 'function') codeStatus('tool shattered');
            window._toolHealth = 100; /* new tool, fresh */
          }
          /* 70000 strikes breaks the block */
          if(window._blockStrikes[_key] >= 70000){
            scene.remove(_blk);
            window._solidBlocks.splice(_bi, 1);
            if(typeof codeStatus === 'function') codeStatus('the block yields — 70000 strikes');
          }
          return; /* the earth does not open here */
        }
      }
    }
  }catch(e){}
  digs.push({x:x, z:z, r:4.5, d:1.6});
  persistDigs();
  refreshLandMesh(x, z, 4.5);
  /* topo stays true — re-chart the dug area */
  try{
    if(TOPO && typeof groundY!=='undefined'){
      for(var ix=Math.max(TOPO_MIN,Math.floor(x-6)); ix<=Math.min(TOPO_MAX,Math.ceil(x+6)); ix++){
        for(var iz=Math.max(TOPO_MIN,Math.floor(z-6)); iz<=Math.min(TOPO_MAX,Math.ceil(z+6)); iz++){
          TOPO[ix+','+iz]=groundY(ix,iz);
        }
      }
    }
  }catch(e){}
}
function groundY(x, z){
  var base = FLOOR_Y + 0.02 + landH(x, z);
  // Check solid node surfaces (standable, impenetrable)
  if(typeof SOLID_SURFACES==='undefined') return base;
  for(var i=0; i<SOLID_SURFACES.length; i++){
    var sf = SOLID_SURFACES[i];
    var dx = x - sf.x, dz = z - sf.z;
    if(dx*dx + dz*dz < sf.r*sf.r && sf.y > base) base = sf.y;
  }
  return base;
}
function landTop(){
  return {x:PLATEAU.x, z:PLATEAU.z};
}
function landTopOld(){
  var bx=0, bz=20, bh=-1;
  for(var x=-44; x<=44; x+=4){
    for(var z=-44; z<=44; z+=4){
      var h = landH(x, z);
      if(h > bh){ bh = h; bx = x; bz = z; }
    }
  }
  return {x:bx, z:bz};
}
function buildLandMesh(){
  if(landMesh) return;
  var SEG = 64, SIZE = 98;
  var g = new T.PlaneGeometry(SIZE, SIZE, SEG, SEG);
  g.rotateX(-Math.PI/2);
  var pa = g.attributes.position, colors = [];
  var cA = new T.Color(0x4a5a38), cB = new T.Color(0x7a8a52), cC = new T.Color(0xa8a06a), cD = new T.Color(0x6a5a42);
  var tmp = new T.Color();
  for(var i=0; i<pa.count; i++){
    var x = pa.getX(i), z = pa.getZ(i);
    /* reality 2 — the land pure, our plateau lives in reality 1 alone */
    var hb = applyDigs(landBaseRaw(x, z), x